imap_messages.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. foreach ($read as $read_part) {
  159. if (eregi ("^to:(.*)$", $read_part, $regs)) {
  160. $to = $regs[1];
  161. } else if (eregi ("^from:(.*)$", $read_part, $regs)) {
  162. $from = $regs[1];
  163. } else if (eregi ("^x-priority:(.*)$", $read_part, $regs)) {
  164. $priority = trim($regs[1]);
  165. } else if (eregi ("^message-id:(.*)$", $read_part, $regs)) {
  166. $messageid = trim($regs[1]);
  167. } else if (eregi ("^cc:(.*)$", $read_part, $regs)) {
  168. $cc = $regs[1];
  169. } else if (eregi ("^date:(.*)$", $read_part, $regs)) {
  170. $date = $regs[1];
  171. } else if (eregi ("^subject:(.*)$", $read_part, $regs)) {
  172. $subject = htmlspecialchars(trim($regs[1]));
  173. if ($subject == "") {
  174. $subject = _("(no subject)");
  175. }
  176. } else if (eregi ("^content-type:(.*)$", $read_part, $regs)) {
  177. $type = strtolower(trim($regs[1]));
  178. if ($pos = strpos($type, ";")) {
  179. $type = substr($type, 0, $pos);
  180. }
  181. $type = explode("/", $type);
  182. if (!isset($type[1])) {
  183. $type[1] = '';
  184. }
  185. } else if (eregi ("^in-reply-to:(.*)$", $read_part, $regs)) {
  186. $inrepto = trim($regs[1]);
  187. }
  188. }
  189. $internaldate = getPref($data_dir, $username, 'internal_date_sort');
  190. if (trim($date) == "" || $internaldate) {
  191. fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
  192. $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
  193. if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
  194. $date_list = explode(' ', trim($regs[1]));
  195. $date_list[0] = str_replace("-", ' ', $date_list[0]);
  196. $date = implode(' ', $date_list);
  197. }
  198. }
  199. eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
  200. $size = $regs[1];
  201. $header = new small_header;
  202. if ($issent) {
  203. $header->from = (trim($to) != '' ? $to : '(' ._("No To Address") . ')');
  204. } else {
  205. $header->from = $from;
  206. }
  207. $header->date = $date;
  208. $header->subject = $subject;
  209. $header->to = $to;
  210. $header->priority = $priority;
  211. $header->message_id = $messageid;
  212. $header->cc = $cc;
  213. $header->size = $size;
  214. $header->type0 = $type[0];
  215. $header->type1 = $type[1];
  216. $header->inrepto = $inrepto;
  217. $result[] = $header;
  218. }
  219. return $result;
  220. }
  221. /* Returns the flags for the specified messages */
  222. function sqimap_get_flags ($imap_stream, $i) {
  223. $read = sqimap_run_command ($imap_stream, "FETCH $i:$i FLAGS", true, $response, $message);
  224. if (ereg('FLAGS(.*)', $read[0], $regs)) {
  225. return explode(' ', trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
  226. }
  227. return array('None');
  228. }
  229. function sqimap_get_flags_list ($imap_stream, $msg_list) {
  230. $msgs_str = sqimap_message_list_squisher($msg_list);
  231. for ($i = 0; $i < sizeof($msg_list); $i++) {
  232. $id2index[$msg_list[$i]] = $i;
  233. }
  234. $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
  235. $result_flags = array();
  236. for ($i = 0; $i < sizeof($result_list); $i++) {
  237. if (eregi('^\* ([0-9]+).*FETCH.*FLAGS(.*)', $result_list[$i][0], $regs)
  238. && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
  239. $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
  240. } else {
  241. set_up_language($squirrelmail_language);
  242. echo "<br><b><font color=$color[2]>\n" .
  243. _("ERROR : Could not complete request.") .
  244. "</b><br>\n" .
  245. _("Unknown response from IMAP server: ") .
  246. $result_list[$i][0] . "</font><br>\n";
  247. exit;
  248. }
  249. }
  250. arsort($result_flags);
  251. return $result_flags;
  252. }
  253. /*
  254. * Returns a message array with all the information about a message.
  255. * See the documentation folder for more information about this array.
  256. */
  257. function sqimap_get_message ($imap_stream, $id, $mailbox) {
  258. $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
  259. return sqimap_get_message_body($imap_stream, $header);
  260. }
  261. /* Wrapper function that reformats the header information. */
  262. function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
  263. $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
  264. $header = sqimap_get_header($imap_stream, $read);
  265. $header->id = $id;
  266. $header->mailbox = $mailbox;
  267. return $header;
  268. }
  269. /* Wrapper function that reformats the entity header information. */
  270. function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
  271. $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.HEADER]", true, $response, $message);
  272. $header = sqimap_get_header($imap_stream, $read);
  273. $header->id = $id;
  274. $header->mailbox = $mailbox;
  275. return $header;
  276. }
  277. /* Wrapper function that returns entity headers for use by decodeMime */
  278. /*
  279. function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
  280. $header = sqimap_get_header($imap_stream, $read);
  281. $type0 = $header["TYPE0"];
  282. $type1 = $header["TYPE1"];
  283. $bound = $header["BOUNDARY"];
  284. $encoding = $header["ENCODING"];
  285. $charset = $header["CHARSET"];
  286. $filename = $header["FILENAME"];
  287. }
  288. */
  289. /* Queries the IMAP server and gets all header information. */
  290. function sqimap_get_header ($imap_stream, $read) {
  291. global $where, $what;
  292. $hdr = new msg_header();
  293. $i = 0;
  294. /* Set up some defaults */
  295. $hdr->type0 = "text";
  296. $hdr->type1 = "plain";
  297. $hdr->charset = "us-ascii";
  298. while ($i < count($read)) {
  299. if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
  300. $hdr->mime = true;
  301. $i++;
  302. }
  303. /* ENCODING TYPE */
  304. else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
  305. $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
  306. $i++;
  307. }
  308. /* CONTENT-TYPE */
  309. else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
  310. $cont = strtolower(trim(substr($read[$i], 13)));
  311. if (strpos($cont, ";")) {
  312. $cont = substr($cont, 0, strpos($cont, ";"));
  313. }
  314. if (strpos($cont, "/")) {
  315. $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
  316. $hdr->type1 = substr($cont, strpos($cont, "/")+1);
  317. } else {
  318. $hdr->type0 = $cont;
  319. }
  320. $line = $read[$i];
  321. $i++;
  322. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  323. str_replace("\n", "", $line);
  324. str_replace("\n", "", $read[$i]);
  325. $line = "$line $read[$i]";
  326. $i++;
  327. }
  328. /* Detect the boundary of a multipart message */
  329. if (eregi('boundary="([^"]+)"', $line, $regs)) {
  330. $hdr->boundary = $regs[1];
  331. }
  332. /* Detect the charset */
  333. if (strpos(strtolower(trim($line)), "charset=")) {
  334. $pos = strpos($line, "charset=") + 8;
  335. $charset = trim($line);
  336. if (strpos($line, ";", $pos) > 0) {
  337. $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
  338. } else {
  339. $charset = substr($charset, $pos);
  340. }
  341. $charset = str_replace("\"", "", $charset);
  342. $hdr->charset = $charset;
  343. } else {
  344. $hdr->charset = "us-ascii";
  345. }
  346. }
  347. else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
  348. /* Add better content-disposition support */
  349. $line = $read[$i];
  350. $i++;
  351. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  352. str_replace("\n", "", $line);
  353. str_replace("\n", "", $read[$i]);
  354. $line = "$line $read[$i]";
  355. $i++;
  356. }
  357. /* Detects filename if any */
  358. if (strpos(strtolower(trim($line)), "filename=")) {
  359. $pos = strpos($line, "filename=") + 9;
  360. $name = trim($line);
  361. if (strpos($line, " ", $pos) > 0) {
  362. $name = substr($name, $pos, strpos($line, " ", $pos));
  363. } else {
  364. $name = substr($name, $pos);
  365. }
  366. $name = str_replace("\"", "", $name);
  367. $hdr->filename = $name;
  368. }
  369. }
  370. /* REPLY-TO */
  371. else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
  372. $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
  373. $i++;
  374. }
  375. /* FROM */
  376. else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
  377. $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
  378. if (! isset($hdr->replyto) || $hdr->replyto == "") {
  379. $hdr->replyto = $hdr->from;
  380. }
  381. $i++;
  382. }
  383. /* DATE */
  384. else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
  385. $d = substr($read[$i], 5);
  386. $d = trim($d);
  387. $d = strtr($d, array(' ' => ' '));
  388. $d = explode(' ', $d);
  389. $hdr->date = getTimeStamp($d);
  390. $i++;
  391. }
  392. /* SUBJECT */
  393. else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
  394. $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
  395. if (strlen(Chop($hdr->subject)) == 0) {
  396. $hdr->subject = _("(no subject)");
  397. }
  398. /*
  399. if ($where == 'SUBJECT') {
  400. $hdr->subject = $what;
  401. // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
  402. }
  403. */
  404. $i++;
  405. }
  406. /* CC */
  407. else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
  408. $pos = 0;
  409. $hdr->cc[$pos] = trim(substr($read[$i], 4));
  410. $i++;
  411. while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
  412. $pos++;
  413. $hdr->cc[$pos] = trim($read[$i]);
  414. $i++;
  415. }
  416. }
  417. /* BCC */
  418. else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
  419. $pos = 0;
  420. $hdr->bcc[$pos] = trim(substr($read[$i], 5));
  421. $i++;
  422. while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
  423. $pos++;
  424. $hdr->bcc[$pos] = trim($read[$i]);
  425. $i++;
  426. }
  427. }
  428. /* TO */
  429. else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
  430. $pos = 0;
  431. $hdr->to[$pos] = trim(substr($read[$i], 4));
  432. $i++;
  433. while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
  434. $pos++;
  435. $hdr->to[$pos] = trim($read[$i]);
  436. $i++;
  437. }
  438. }
  439. /* MESSAGE ID */
  440. else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
  441. $hdr->message_id = trim(substr($read[$i], 11));
  442. $i++;
  443. }
  444. /* ERROR CORRECTION */
  445. else if (substr($read[$i], 0, 1) == ")") {
  446. if (strlen(trim($hdr->subject)) == 0) {
  447. $hdr->subject = _("(no subject)");
  448. }
  449. if (strlen(trim($hdr->from)) == 0) {
  450. $hdr->from = _("(unknown sender)");
  451. }
  452. if (strlen(trim($hdr->date)) == 0) {
  453. $hdr->date = time();
  454. }
  455. $i++;
  456. }
  457. /* X-PRIORITY */
  458. else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
  459. $hdr->priority = trim(substr($read[$i], 11));
  460. $i++;
  461. }
  462. else {
  463. $i++;
  464. }
  465. }
  466. return $hdr;
  467. }
  468. /* Returns the body of a message. */
  469. function sqimap_get_message_body ($imap_stream, &$header) {
  470. $id = $header->id;
  471. return decodeMime($imap_stream, $header);
  472. }
  473. ?>