imap_messages.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <?php
  2. /**
  3. * imap_messages.php
  4. *
  5. * Copyright (c) 1999-2002 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This implements functions that manipulate messages
  9. *
  10. * $Id$
  11. */
  12. /* Copies specified messages to specified folder */
  13. function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
  14. $read = sqimap_run_command ($imap_stream, "COPY $start:$end \"$mailbox\"", true, $response, $message);
  15. }
  16. /* Deletes specified messages and moves them to trash if possible */
  17. function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
  18. global $move_to_trash, $trash_folder, $auto_expunge;
  19. if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
  20. sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
  21. }
  22. sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
  23. }
  24. /* Sets the specified messages with specified flag */
  25. function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
  26. $read = sqimap_run_command ($imap_stream, "STORE $start:$end +FLAGS (\\$flag)", true, $response, $message);
  27. }
  28. /* Remove specified flag from specified messages */
  29. function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag) {
  30. $read = sqimap_run_command ($imap_stream, "STORE $start:$end -FLAGS (\\$flag)", true, $response, $message);
  31. }
  32. /* Returns some general header information -- FROM, DATE, and SUBJECT */
  33. class small_header {
  34. var $from = '', $subject = '', $date = '', $to = '',
  35. $priority = 0, $message_id = 0, $cc = '';
  36. }
  37. function sqimap_get_small_header ($imap_stream, $id, $sent) {
  38. $res = sqimap_get_small_header_list($imap_stream, array($id), $sent);
  39. return $res[0];
  40. }
  41. /*
  42. * Sort the message list and crunch to be as small as possible
  43. * (overflow could happen, so make it small if possible)
  44. */
  45. function sqimap_message_list_squisher($messages_array) {
  46. if( !is_array( $messages_array ) ) {
  47. return;
  48. }
  49. sort($messages_array, SORT_NUMERIC);
  50. $msgs_str = '';
  51. while ($messages_array) {
  52. $start = array_shift($messages_array);
  53. $end = $start;
  54. while (isset($messages_array[0]) && $messages_array[0] == $end + 1) {
  55. $end = array_shift($messages_array);
  56. }
  57. if ($msgs_str != '') {
  58. $msgs_str .= ',';
  59. }
  60. $msgs_str .= $start;
  61. if ($start != $end) {
  62. $msgs_str .= ':' . $end;
  63. }
  64. }
  65. return $msgs_str;
  66. }
  67. function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
  68. global $squirrelmail_language, $color, $data_dir, $username;
  69. /* Get the small headers for each message in $msg_list */
  70. $sid = sqimap_session_id();
  71. $maxmsg = sizeof($msg_list);
  72. $msgs_str = sqimap_message_list_squisher($msg_list);
  73. $results = array();
  74. $read_list = array();
  75. $sizes_list = array();
  76. /*
  77. * We need to return the data in the same order as the caller supplied
  78. * in $msg_list, but IMAP servers are free to return responses in
  79. * whatever order they wish... So we need to re-sort manually
  80. */
  81. for ($i = 0; $i < sizeof($msg_list); $i++) {
  82. $id2index[$msg_list[$i]] = $i;
  83. }
  84. $query = "$sid FETCH $msgs_str BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type References In-Reply-To)]\r\n";
  85. fputs ($imap_stream, $query);
  86. $readin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
  87. foreach ($readin_list as $r) {
  88. if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
  89. set_up_language($squirrelmail_language);
  90. echo '<br><b><font color=$color[2]>' .
  91. _("ERROR : Could not complete request.") .
  92. '</b><br>' .
  93. _("Unknown response from IMAP server: ") . ' 1.' .
  94. $r[0] . "</font><br>\n";
  95. } else if (! isset($id2index[$regs[1]]) || !count($id2index[$regs[1]])) {
  96. set_up_language($squirrelmail_language);
  97. echo '<br><b><font color=$color[2]>' .
  98. _("ERROR : Could not complete request.") .
  99. '</b><br>' .
  100. _("Unknown message number in reply from server: ") .
  101. $regs[1] . "</font><br>\n";
  102. } else {
  103. $read_list[$id2index[$regs[1]]] = $r;
  104. }
  105. }
  106. arsort($read_list);
  107. $query = "$sid FETCH $msgs_str RFC822.SIZE\r\n";
  108. fputs ($imap_stream, $query);
  109. $sizesin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
  110. foreach ($sizesin_list as $r) {
  111. if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
  112. set_up_language($squirrelmail_language);
  113. echo "<br><b><font color=$color[2]>\n";
  114. echo _("ERROR : Could not complete request.");
  115. echo "</b><br>\n";
  116. echo _("Unknown response from IMAP server: ") . ' 2.';
  117. echo $r[0] . "</font><br>\n";
  118. exit;
  119. }
  120. if (!count($id2index[$regs[1]])) {
  121. set_up_language($squirrelmail_language);
  122. echo "<br><b><font color=$color[2]>\n";
  123. echo _("ERROR : Could not complete request.");
  124. echo "</b><br>\n";
  125. echo _("Unknown messagenumber in reply from server: ");
  126. echo $regs[1] . "</font><br>\n";
  127. exit;
  128. }
  129. $sizes_list[$id2index[$regs[1]]] = $r;
  130. }
  131. arsort($sizes_list);
  132. for ($msgi = 0; $msgi < $maxmsg; $msgi++) {
  133. $subject = _("(no subject)");
  134. $from = _("Unknown Sender");
  135. $priority = 0;
  136. $messageid = "<>";
  137. $cc = "";
  138. $to = "";
  139. $date = "";
  140. $type[0] = "";
  141. $type[1] = "";
  142. $ref = "";
  143. $inreplyto = "";
  144. $read = $read_list[$msgi];
  145. foreach ($read as $read_part) {
  146. if (eregi ("^to:(.*)$", $read_part, $regs)) {
  147. $to = $regs[1];
  148. } else if (eregi ("^from:(.*)$", $read_part, $regs)) {
  149. $from = $regs[1];
  150. } else if (eregi ("^x-priority:(.*)$", $read_part, $regs)) {
  151. $priority = trim($regs[1]);
  152. } else if (eregi ("^message-id:(.*)$", $read_part, $regs)) {
  153. $messageid = trim($regs[1]);
  154. } else if (eregi ("^cc:(.*)$", $read_part, $regs)) {
  155. $cc = $regs[1];
  156. } else if (eregi ("^date:(.*)$", $read_part, $regs)) {
  157. $date = $regs[1];
  158. } else if (eregi ("^subject:(.*)$", $read_part, $regs)) {
  159. $subject = htmlspecialchars(trim($regs[1]));
  160. if ($subject == "") {
  161. $subject = _("(no subject)");
  162. }
  163. } else if (eregi ("^content-type:(.*)$", $read_part, $regs)) {
  164. $type = strtolower(trim($regs[1]));
  165. if ($pos = strpos($type, ";")) {
  166. $type = substr($type, 0, $pos);
  167. }
  168. $type = explode("/", $type);
  169. if (!isset($type[1])) {
  170. $type[1] = '';
  171. }
  172. } else if (eregi ("^references:(.*)$", $read_part, $regs)) {
  173. $ref = $regs[1];
  174. } else if (eregi ("^in-reply-to:(.*)$", $read_part, $regs)) {
  175. $inreplyto = $regs[1];
  176. }
  177. }
  178. $internaldate = getPref($data_dir, $username, 'internal_date_sort');
  179. if (trim($date) == "" || $internaldate) {
  180. fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
  181. $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
  182. if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
  183. $date_list = explode(' ', trim($regs[1]));
  184. $date_list[0] = str_replace("-", ' ', $date_list[0]);
  185. $date = implode(' ', $date_list);
  186. }
  187. }
  188. eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
  189. $size = $regs[1];
  190. $header = new small_header;
  191. if ($issent) {
  192. $header->from = (trim($to) != '' ? $to : '(' ._("No To Address") . ')');
  193. } else {
  194. $header->from = $from;
  195. }
  196. $header->date = $date;
  197. $header->subject = $subject;
  198. $header->to = $to;
  199. $header->priority = $priority;
  200. $header->message_id = $messageid;
  201. $header->cc = $cc;
  202. $header->size = $size;
  203. $header->type0 = $type[0];
  204. $header->type1 = $type[1];
  205. $header->references = $ref;
  206. $header->inreplyto = $inreplyto;
  207. $result[] = $header;
  208. }
  209. return $result;
  210. }
  211. /* Returns the flags for the specified messages */
  212. function sqimap_get_flags ($imap_stream, $i) {
  213. $read = sqimap_run_command ($imap_stream, "FETCH $i:$i FLAGS", true, $response, $message);
  214. if (ereg('FLAGS(.*)', $read[0], $regs)) {
  215. return explode(' ', trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
  216. }
  217. return array('None');
  218. }
  219. function sqimap_get_flags_list ($imap_stream, $msg_list) {
  220. $msgs_str = sqimap_message_list_squisher($msg_list);
  221. for ($i = 0; $i < sizeof($msg_list); $i++) {
  222. $id2index[$msg_list[$i]] = $i;
  223. }
  224. $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
  225. $result_flags = array();
  226. for ($i = 0; $i < sizeof($result_list); $i++) {
  227. if (eregi('^\* ([0-9]+).*FETCH.*FLAGS(.*)', $result_list[$i][0], $regs)
  228. && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
  229. $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
  230. } else {
  231. set_up_language($squirrelmail_language);
  232. echo "<br><b><font color=$color[2]>\n" .
  233. _("ERROR : Could not complete request.") .
  234. "</b><br>\n" .
  235. _("Unknown response from IMAP server: ") .
  236. $result_list[$i][0] . "</font><br>\n";
  237. exit;
  238. }
  239. }
  240. arsort($result_flags);
  241. return $result_flags;
  242. }
  243. /*
  244. * Returns a message array with all the information about a message.
  245. * See the documentation folder for more information about this array.
  246. */
  247. function sqimap_get_message ($imap_stream, $id, $mailbox) {
  248. $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
  249. return sqimap_get_message_body($imap_stream, $header);
  250. }
  251. /* Wrapper function that reformats the header information. */
  252. function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
  253. $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
  254. $header = sqimap_get_header($imap_stream, $read);
  255. $header->id = $id;
  256. $header->mailbox = $mailbox;
  257. return $header;
  258. }
  259. /* Wrapper function that reformats the entity header information. */
  260. function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
  261. $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.HEADER]", true, $response, $message);
  262. $header = sqimap_get_header($imap_stream, $read);
  263. $header->id = $id;
  264. $header->mailbox = $mailbox;
  265. return $header;
  266. }
  267. /* Wrapper function that returns entity headers for use by decodeMime */
  268. /*
  269. function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
  270. $header = sqimap_get_header($imap_stream, $read);
  271. $type0 = $header["TYPE0"];
  272. $type1 = $header["TYPE1"];
  273. $bound = $header["BOUNDARY"];
  274. $encoding = $header["ENCODING"];
  275. $charset = $header["CHARSET"];
  276. $filename = $header["FILENAME"];
  277. }
  278. */
  279. /* Queries the IMAP server and gets all header information. */
  280. function sqimap_get_header ($imap_stream, $read) {
  281. global $where, $what;
  282. $hdr = new msg_header();
  283. $i = 0;
  284. /* Set up some defaults */
  285. $hdr->type0 = "text";
  286. $hdr->type1 = "plain";
  287. $hdr->charset = "us-ascii";
  288. while ($i < count($read)) {
  289. if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
  290. $hdr->mime = true;
  291. $i++;
  292. }
  293. /* ENCODING TYPE */
  294. else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
  295. $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
  296. $i++;
  297. }
  298. /* CONTENT-TYPE */
  299. else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
  300. $cont = strtolower(trim(substr($read[$i], 13)));
  301. if (strpos($cont, ";")) {
  302. $cont = substr($cont, 0, strpos($cont, ";"));
  303. }
  304. if (strpos($cont, "/")) {
  305. $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
  306. $hdr->type1 = substr($cont, strpos($cont, "/")+1);
  307. } else {
  308. $hdr->type0 = $cont;
  309. }
  310. $line = $read[$i];
  311. $i++;
  312. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  313. str_replace("\n", "", $line);
  314. str_replace("\n", "", $read[$i]);
  315. $line = "$line $read[$i]";
  316. $i++;
  317. }
  318. /* Detect the boundary of a multipart message */
  319. if (eregi('boundary="([^"]+)"', $line, $regs)) {
  320. $hdr->boundary = $regs[1];
  321. }
  322. /* Detect the charset */
  323. if (strpos(strtolower(trim($line)), "charset=")) {
  324. $pos = strpos($line, "charset=") + 8;
  325. $charset = trim($line);
  326. if (strpos($line, ";", $pos) > 0) {
  327. $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
  328. } else {
  329. $charset = substr($charset, $pos);
  330. }
  331. $charset = str_replace("\"", "", $charset);
  332. $hdr->charset = $charset;
  333. } else {
  334. $hdr->charset = "us-ascii";
  335. }
  336. }
  337. else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
  338. /* Add better content-disposition support */
  339. $line = $read[$i];
  340. $i++;
  341. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  342. str_replace("\n", "", $line);
  343. str_replace("\n", "", $read[$i]);
  344. $line = "$line $read[$i]";
  345. $i++;
  346. }
  347. /* Detects filename if any */
  348. if (strpos(strtolower(trim($line)), "filename=")) {
  349. $pos = strpos($line, "filename=") + 9;
  350. $name = trim($line);
  351. if (strpos($line, " ", $pos) > 0) {
  352. $name = substr($name, $pos, strpos($line, " ", $pos));
  353. } else {
  354. $name = substr($name, $pos);
  355. }
  356. $name = str_replace("\"", "", $name);
  357. $hdr->filename = $name;
  358. }
  359. }
  360. /* REPLY-TO */
  361. else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
  362. $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
  363. $i++;
  364. }
  365. /* FROM */
  366. else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
  367. $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
  368. if (! isset($hdr->replyto) || $hdr->replyto == "") {
  369. $hdr->replyto = $hdr->from;
  370. }
  371. $i++;
  372. }
  373. /* DATE */
  374. else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
  375. $d = substr($read[$i], 5);
  376. $d = trim($d);
  377. $d = strtr($d, array(' ' => ' '));
  378. $d = explode(' ', $d);
  379. $hdr->date = getTimeStamp($d);
  380. $i++;
  381. }
  382. /* SUBJECT */
  383. else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
  384. $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
  385. if (strlen(Chop($hdr->subject)) == 0) {
  386. $hdr->subject = _("(no subject)");
  387. }
  388. /*
  389. if ($where == 'SUBJECT') {
  390. $hdr->subject = $what;
  391. // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
  392. }
  393. */
  394. $i++;
  395. }
  396. /* CC */
  397. else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
  398. $pos = 0;
  399. $hdr->cc[$pos] = trim(substr($read[$i], 4));
  400. $i++;
  401. while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
  402. $pos++;
  403. $hdr->cc[$pos] = trim($read[$i]);
  404. $i++;
  405. }
  406. }
  407. /* BCC */
  408. else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
  409. $pos = 0;
  410. $hdr->bcc[$pos] = trim(substr($read[$i], 5));
  411. $i++;
  412. while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
  413. $pos++;
  414. $hdr->bcc[$pos] = trim($read[$i]);
  415. $i++;
  416. }
  417. }
  418. /* TO */
  419. else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
  420. $pos = 0;
  421. $hdr->to[$pos] = trim(substr($read[$i], 4));
  422. $i++;
  423. while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
  424. $pos++;
  425. $hdr->to[$pos] = trim($read[$i]);
  426. $i++;
  427. }
  428. }
  429. /* MESSAGE ID */
  430. else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
  431. $hdr->message_id = trim(substr($read[$i], 11));
  432. $i++;
  433. }
  434. /* ERROR CORRECTION */
  435. else if (substr($read[$i], 0, 1) == ")") {
  436. if (strlen(trim($hdr->subject)) == 0) {
  437. $hdr->subject = _("(no subject)");
  438. }
  439. if (strlen(trim($hdr->from)) == 0) {
  440. $hdr->from = _("(unknown sender)");
  441. }
  442. if (strlen(trim($hdr->date)) == 0) {
  443. $hdr->date = time();
  444. }
  445. $i++;
  446. }
  447. /* X-PRIORITY */
  448. else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
  449. $hdr->priority = trim(substr($read[$i], 11));
  450. $i++;
  451. }
  452. else {
  453. $i++;
  454. }
  455. }
  456. return $hdr;
  457. }
  458. /* Returns the body of a message. */
  459. function sqimap_get_message_body ($imap_stream, &$header) {
  460. $id = $header->id;
  461. return decodeMime($imap_stream, $header);
  462. }
  463. ?>