imap_messages.php 20 KB

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