imap_messages.php 20 KB

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