imap_messages.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. ** imap_messages.php
  4. **
  5. ** This implements functions that manipulate messages
  6. **/
  7. if (!isset($mime_php)) include "../functions/mime.php";
  8. /******************************************************************************
  9. ** Copies specified messages to specified folder
  10. ******************************************************************************/
  11. function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
  12. fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\r\n");
  13. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  14. }
  15. /******************************************************************************
  16. ** Deletes specified messages and moves them to trash if possible
  17. ******************************************************************************/
  18. function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
  19. global $move_to_trash, $trash_folder, $auto_expunge;
  20. if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
  21. sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
  22. sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
  23. } else {
  24. sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
  25. }
  26. }
  27. /******************************************************************************
  28. ** Sets the specified messages with specified flag
  29. ******************************************************************************/
  30. function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
  31. fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\r\n");
  32. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  33. }
  34. /******************************************************************************
  35. ** Returns some general header information -- FROM, DATE, and SUBJECT
  36. ******************************************************************************/
  37. class small_header {
  38. var $from, $subject, $date, $to, $priority, $message_id;
  39. }
  40. function sqimap_get_small_header ($imap_stream, $id, $sent) {
  41. fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority)]\r\n");
  42. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  43. $subject = _("(no subject)");
  44. $from = _("Unknown Sender");
  45. $priority = "0";
  46. $messageid = "<>";
  47. $g = 0;
  48. for ($i = 0; $i < count($read); $i++) {
  49. if (eregi ("^to:", $read[$i])) {
  50. //$to = sqimap_find_displayable_name(substr($read[$i], 3));
  51. $to = substr($read[$i], 3);
  52. } else if (eregi ("^from:", $read[$i])) {
  53. //$from = sqimap_find_displayable_name(substr($read[$i], 5));
  54. $from = substr($read[$i], 5);
  55. } else if (eregi ("^x-priority:", $read[$i])) {
  56. $priority = trim(substr($read[$i], 11));
  57. } else if (eregi ("^message-id:", $read[$i])) {
  58. $messageid = trim(substr($read[$i], 11));
  59. } else if (eregi ("^cc:", $read[$i])) {
  60. $cc = substr($read[$i], 3);
  61. } else if (eregi ("^date:", $read[$i])) {
  62. $date = substr($read[$i], 5);
  63. } else if (eregi ("^subject:", $read[$i])) {
  64. $subject = htmlspecialchars(eregi_replace ("^subject: ", "", $read[$i]));
  65. if (trim($subject) == "")
  66. $subject = _("(no subject)");
  67. }
  68. }
  69. // If there isn't a date, it takes the internal date and uses
  70. // that as the normal date.
  71. if (trim($date) == "") {
  72. fputs ($imap_stream, "a002 FETCH $id INTERNALDATE\r\n");
  73. $internal_read = sqimap_read_data ($imap_stream, "a002", true, $r, $m);
  74. // * 22 FETCH (INTERNALDATE " 8-Sep-2000 13:17:07 -0500")
  75. $date = $internal_read[0];
  76. $date = eregi_replace(".*internaldate \"", "", $date);
  77. $date = eregi_replace("\".*", "", $date);
  78. $date_ary = explode(" ", trim($date));
  79. $date_ary[0] = str_replace("-", " ", $date_ary[0]);
  80. $date = implode (" ", $date_ary);
  81. }
  82. fputs ($imap_stream, "a003 FETCH $id RFC822.SIZE\r\n");
  83. $read = sqimap_read_data($imap_stream, "a003", true, $r, $m);
  84. preg_match("/([0-9]+)\)\s*$/i", $read[0], $regs);
  85. $size = $regs[1] / 1024;
  86. settype($size, "integer");
  87. $header = new small_header;
  88. if ($sent == true)
  89. $header->from = $to;
  90. else
  91. $header->from = $from;
  92. $header->date = $date;
  93. $header->subject = $subject;
  94. $header->to = $to;
  95. $header->priority = $priority;
  96. $header->message_id = $messageid;
  97. $header->cc = $cc;
  98. $header->size = $size;
  99. return $header;
  100. }
  101. /******************************************************************************
  102. ** Returns the flags for the specified messages
  103. ******************************************************************************/
  104. function sqimap_get_flags ($imap_stream, $i) {
  105. fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
  106. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  107. if (strpos($read[0], "FLAGS")) {
  108. $tmp = ereg_replace("\(", "", $read[0]);
  109. $tmp = ereg_replace("\)", "", $tmp);
  110. $tmp = str_replace("\\", "", $tmp);
  111. $tmp = substr($tmp, strpos($tmp, "FLAGS")+6, strlen($tmp));
  112. $tmp = trim($tmp);
  113. $flags = explode(" ", $tmp);
  114. } else {
  115. $flags[0] = "None";
  116. }
  117. return $flags;
  118. }
  119. /******************************************************************************
  120. ** Returns a message array with all the information about a message. See
  121. ** the documentation folder for more information about this array.
  122. ******************************************************************************/
  123. function sqimap_get_message ($imap_stream, $id, $mailbox) {
  124. $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
  125. $msg = sqimap_get_message_body($imap_stream, &$header);
  126. return $msg;
  127. }
  128. /******************************************************************************
  129. ** Wrapper function that reformats the header information.
  130. ******************************************************************************/
  131. function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
  132. fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
  133. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  134. $header = sqimap_get_header($imap_stream, $read);
  135. $header->id = $id;
  136. $header->mailbox = $mailbox;
  137. return $header;
  138. }
  139. /******************************************************************************
  140. ** Wrapper function that returns entity headers for use by decodeMime
  141. ******************************************************************************/
  142. /*
  143. function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
  144. $header = sqimap_get_header($imap_stream, $read);
  145. $type0 = $header["TYPE0"];
  146. $type1 = $header["TYPE1"];
  147. $bound = $header["BOUNDARY"];
  148. $encoding = $header["ENCODING"];
  149. $charset = $header["CHARSET"];
  150. $filename = $header["FILENAME"];
  151. }
  152. */
  153. /******************************************************************************
  154. ** Queries the IMAP server and gets all header information.
  155. ******************************************************************************/
  156. function sqimap_get_header ($imap_stream, $read) {
  157. global $where, $what;
  158. $hdr = new msg_header();
  159. $i = 0;
  160. // Set up some defaults
  161. $hdr->type0 = "text";
  162. $hdr->type1 = "plain";
  163. $hdr->charset = "us-ascii";
  164. preg_match("/\{([0-9]+)\}/", $read[$i], $regs);
  165. preg_match("/[0-9]+/", $regs[0], $regs);
  166. while ($i < count($read)) {
  167. if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
  168. $hdr->mime = true;
  169. $i++;
  170. }
  171. /** ENCODING TYPE **/
  172. else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
  173. $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
  174. $i++;
  175. }
  176. /** CONTENT-TYPE **/
  177. else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
  178. $cont = strtolower(trim(substr($read[$i], 13)));
  179. if (strpos($cont, ";"))
  180. $cont = substr($cont, 0, strpos($cont, ";"));
  181. if (strpos($cont, "/")) {
  182. $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
  183. $hdr->type1 = substr($cont, strpos($cont, "/")+1);
  184. } else {
  185. $hdr->type0 = $cont;
  186. }
  187. $line = $read[$i];
  188. $i++;
  189. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  190. str_replace("\n", "", $line);
  191. str_replace("\n", "", $read[$i]);
  192. $line = "$line $read[$i]";
  193. $i++;
  194. }
  195. /** Detect the boundary of a multipart message **/
  196. if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {
  197. $hdr->boundary = $regs[1];
  198. }
  199. /** Detect the charset **/
  200. if (strpos(strtolower(trim($line)), "charset=")) {
  201. $pos = strpos($line, "charset=") + 8;
  202. $charset = trim($line);
  203. if (strpos($line, " ", $pos) > 0) {
  204. $charset = substr($charset, $pos, strpos($line, " ", $pos));
  205. } else {
  206. $charset = substr($charset, $pos);
  207. }
  208. $charset = str_replace("\"", "", $charset);
  209. $hdr->charset = $charset;
  210. } else {
  211. $hdr->charset = "us-ascii";
  212. }
  213. }
  214. else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
  215. /** Add better dontent-disposition support **/
  216. $line = $read[$i];
  217. $i++;
  218. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  219. str_replace("\n", "", $line);
  220. str_replace("\n", "", $read[$i]);
  221. $line = "$line $read[$i]";
  222. $i++;
  223. }
  224. /** Detects filename if any **/
  225. if (strpos(strtolower(trim($line)), "filename=")) {
  226. $pos = strpos($line, "filename=") + 9;
  227. $name = trim($line);
  228. if (strpos($line, " ", $pos) > 0) {
  229. $name = substr($name, $pos, strpos($line, " ", $pos));
  230. } else {
  231. $name = substr($name, $pos);
  232. }
  233. $name = str_replace("\"", "", $name);
  234. $hdr->filename = $name;
  235. }
  236. }
  237. /** REPLY-TO **/
  238. else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
  239. $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
  240. $i++;
  241. }
  242. /** FROM **/
  243. else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
  244. $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
  245. if ($hdr->replyto == "")
  246. $hdr->replyto = $hdr->from;
  247. $i++;
  248. }
  249. /** DATE **/
  250. else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
  251. $d = substr($read[$i], 5);
  252. $d = trim($d);
  253. $d = ereg_replace(" ", " ", $d);
  254. $d = explode(" ", $d);
  255. $hdr->date = getTimeStamp($d);
  256. $i++;
  257. }
  258. /** SUBJECT **/
  259. else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
  260. $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
  261. if (strlen(Chop($hdr->subject)) == 0)
  262. $hdr->subject = _("(no subject)");
  263. if ($where == "SUBJECT") {
  264. $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
  265. }
  266. $i++;
  267. }
  268. /** CC **/
  269. else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
  270. $pos = 0;
  271. $hdr->cc[$pos] = trim(substr($read[$i], 4));
  272. $i++;
  273. while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
  274. $pos++;
  275. $hdr->cc[$pos] = trim($read[$i]);
  276. $i++;
  277. }
  278. }
  279. /** TO **/
  280. else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
  281. $pos = 0;
  282. $hdr->to[$pos] = trim(substr($read[$i], 4));
  283. $i++;
  284. while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
  285. $pos++;
  286. $hdr->to[$pos] = trim($read[$i]);
  287. $i++;
  288. }
  289. }
  290. /** MESSAGE ID **/
  291. else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
  292. $hdr->message_id = trim(substr($read[$i], 11));
  293. $i++;
  294. }
  295. /** ERROR CORRECTION **/
  296. else if (substr($read[$i], 0, 1) == ")") {
  297. if (strlen(trim($hdr->subject)) == 0)
  298. $hdr->subject = _("(no subject)");
  299. if (strlen(trim($hdr->from)) == 0)
  300. $hdr->from = _("(unknown sender)");
  301. if (strlen(trim($hdr->date)) == 0)
  302. $hdr->date = time();
  303. $i++;
  304. }
  305. else {
  306. $i++;
  307. }
  308. }
  309. return $hdr;
  310. }
  311. /******************************************************************************
  312. ** Returns the body of a message.
  313. ******************************************************************************/
  314. function sqimap_get_message_body ($imap_stream, &$header) {
  315. $id = $header->id;
  316. return decodeMime($imap_stream, $body, &$header);
  317. }
  318. /******************************************************************************
  319. ** Returns an array with the body structure
  320. ******************************************************************************/
  321. ?>