imap_messages.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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, $data_dir, $username;
  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. $internaldate = getPref($data_dir, $username, 'internal_date_sort');
  188. if (trim($date) == "" || $internaldate) {
  189. fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
  190. $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
  191. if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
  192. $date_list = explode(" ", trim($regs[1]));
  193. $date_list[0] = str_replace("-", " ", $date_list[0]);
  194. $date = implode(" ", $date_list);
  195. }
  196. }
  197. eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
  198. $size = $regs[1];
  199. $header = new small_header;
  200. if ($issent == true) {
  201. $header->from = (trim($to) != '' ? $to : '(' ._("No To Address") . ')');
  202. } else {
  203. $header->from = $from;
  204. }
  205. $header->date = $date;
  206. $header->subject = $subject;
  207. $header->to = $to;
  208. $header->priority = $priority;
  209. $header->message_id = $messageid;
  210. $header->cc = $cc;
  211. $header->size = $size;
  212. $header->type0 = $type[0];
  213. $header->type1 = $type[1];
  214. $result[] = $header;
  215. }
  216. return $result;
  217. }
  218. /****************************************************************************
  219. ** Returns the flags for the specified messages
  220. ****************************************************************************/
  221. function sqimap_get_flags ($imap_stream, $i) {
  222. $read = sqimap_run_command ($imap_stream, "FETCH $i:$i FLAGS", true, $response, $message);
  223. if (ereg("FLAGS(.*)", $read[0], $regs)) {
  224. return explode(' ', trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
  225. }
  226. return Array('None');
  227. }
  228. function sqimap_get_flags_list ($imap_stream, $msg_list) {
  229. $msgs_str = sqimap_message_list_squisher($msg_list);
  230. for ($i = 0; $i < sizeof($msg_list); $i++) {
  231. $id2index[$msg_list[$i]] = $i;
  232. }
  233. $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
  234. $result_flags = array();
  235. for ($i = 0; $i < sizeof($result_list); $i++) {
  236. if (eregi("^\\* ([0-9]+).*FETCH.*FLAGS(.*)", $result_list[$i][0], $regs)
  237. && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
  238. $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
  239. } else {
  240. set_up_language($squirrelmail_language);
  241. echo "<br><b><font color=$color[2]>\n" .
  242. _("ERROR : Could not complete request.") .
  243. "</b><br>\n" .
  244. _("Unknown response from IMAP server: ") .
  245. $result_list[$i][0] . "</font><br>\n";
  246. exit;
  247. }
  248. }
  249. arsort($result_flags);
  250. return $result_flags;
  251. }
  252. /****************************************************************************
  253. ** Returns a message array with all the information about a message. See
  254. ** the documentation folder for more information about this array.
  255. ****************************************************************************/
  256. function sqimap_get_message ($imap_stream, $id, $mailbox) {
  257. $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
  258. return sqimap_get_message_body($imap_stream, $header);
  259. }
  260. /****************************************************************************
  261. ** Wrapper function that reformats the header information.
  262. ****************************************************************************/
  263. function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
  264. $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
  265. $header = sqimap_get_header($imap_stream, $read);
  266. $header->id = $id;
  267. $header->mailbox = $mailbox;
  268. return $header;
  269. }
  270. /****************************************************************************
  271. ** Wrapper function that reformats the entity header information.
  272. ****************************************************************************/
  273. function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
  274. $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.HEADER]", 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. }
  321. if (strpos($cont, "/")) {
  322. $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
  323. $hdr->type1 = substr($cont, strpos($cont, "/")+1);
  324. } else {
  325. $hdr->type0 = $cont;
  326. }
  327. $line = $read[$i];
  328. $i++;
  329. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  330. str_replace("\n", "", $line);
  331. str_replace("\n", "", $read[$i]);
  332. $line = "$line $read[$i]";
  333. $i++;
  334. }
  335. /** Detect the boundary of a multipart message **/
  336. if (eregi('boundary="([^"]+)"', $line, $regs)) {
  337. $hdr->boundary = $regs[1];
  338. }
  339. /** Detect the charset **/
  340. if (strpos(strtolower(trim($line)), "charset=")) {
  341. $pos = strpos($line, "charset=") + 8;
  342. $charset = trim($line);
  343. if (strpos($line, ";", $pos) > 0) {
  344. $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
  345. } else {
  346. $charset = substr($charset, $pos);
  347. }
  348. $charset = str_replace("\"", "", $charset);
  349. $hdr->charset = $charset;
  350. } else {
  351. $hdr->charset = "us-ascii";
  352. }
  353. }
  354. else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
  355. /** Add better dontent-disposition support **/
  356. $line = $read[$i];
  357. $i++;
  358. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  359. str_replace("\n", "", $line);
  360. str_replace("\n", "", $read[$i]);
  361. $line = "$line $read[$i]";
  362. $i++;
  363. }
  364. /** Detects filename if any **/
  365. if (strpos(strtolower(trim($line)), "filename=")) {
  366. $pos = strpos($line, "filename=") + 9;
  367. $name = trim($line);
  368. if (strpos($line, " ", $pos) > 0) {
  369. $name = substr($name, $pos, strpos($line, " ", $pos));
  370. } else {
  371. $name = substr($name, $pos);
  372. }
  373. $name = str_replace("\"", "", $name);
  374. $hdr->filename = $name;
  375. }
  376. }
  377. /** REPLY-TO **/
  378. else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
  379. $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
  380. $i++;
  381. }
  382. /** FROM **/
  383. else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
  384. $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
  385. if (! isset($hdr->replyto) || $hdr->replyto == "") {
  386. $hdr->replyto = $hdr->from;
  387. }
  388. $i++;
  389. }
  390. /** DATE **/
  391. else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
  392. $d = substr($read[$i], 5);
  393. $d = trim($d);
  394. $d = strtr($d, array(' ' => ' '));
  395. $d = explode(' ', $d);
  396. $hdr->date = getTimeStamp($d);
  397. $i++;
  398. }
  399. /** SUBJECT **/
  400. else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
  401. $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
  402. if (strlen(Chop($hdr->subject)) == 0) {
  403. $hdr->subject = _("(no subject)");
  404. }
  405. /*
  406. if ($where == 'SUBJECT') {
  407. $hdr->subject = $what;
  408. // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
  409. }
  410. */
  411. $i++;
  412. }
  413. /** CC **/
  414. else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
  415. $pos = 0;
  416. $hdr->cc[$pos] = trim(substr($read[$i], 4));
  417. $i++;
  418. while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
  419. $pos++;
  420. $hdr->cc[$pos] = trim($read[$i]);
  421. $i++;
  422. }
  423. }
  424. /** BCC **/
  425. else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
  426. $pos = 0;
  427. $hdr->bcc[$pos] = trim(substr($read[$i], 5));
  428. $i++;
  429. while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
  430. $pos++;
  431. $hdr->bcc[$pos] = trim($read[$i]);
  432. $i++;
  433. }
  434. }
  435. /** TO **/
  436. else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
  437. $pos = 0;
  438. $hdr->to[$pos] = trim(substr($read[$i], 4));
  439. $i++;
  440. while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
  441. $pos++;
  442. $hdr->to[$pos] = trim($read[$i]);
  443. $i++;
  444. }
  445. }
  446. /** MESSAGE ID **/
  447. else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
  448. $hdr->message_id = trim(substr($read[$i], 11));
  449. $i++;
  450. }
  451. /** ERROR CORRECTION **/
  452. else if (substr($read[$i], 0, 1) == ")") {
  453. if (strlen(trim($hdr->subject)) == 0) {
  454. $hdr->subject = _("(no subject)");
  455. }
  456. if (strlen(trim($hdr->from)) == 0) {
  457. $hdr->from = _("(unknown sender)");
  458. }
  459. if (strlen(trim($hdr->date)) == 0) {
  460. $hdr->date = time();
  461. }
  462. $i++;
  463. }
  464. /** X-PRIORITY **/
  465. else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
  466. $hdr->priority = trim(substr($read[$i], 11));
  467. $i++;
  468. }
  469. else {
  470. $i++;
  471. }
  472. }
  473. return $hdr;
  474. }
  475. /****************************************************************************
  476. ** Returns the body of a message.
  477. ****************************************************************************/
  478. function sqimap_get_message_body ($imap_stream, &$header) {
  479. $id = $header->id;
  480. return decodeMime($imap_stream, $header);
  481. }
  482. /****************************************************************************
  483. ** Returns an array with the body structure
  484. ****************************************************************************/
  485. ?>