imap_messages.php 21 KB

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