imap_messages.php 19 KB

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