imap_messages.php 20 KB

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