imap_messages.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <?php
  2. /**
  3. ** imap_messages.php
  4. **
  5. ** This implements functions that manipulate messages
  6. **
  7. ** $Id$
  8. **/
  9. if (defined ('imap_messages_php'))
  10. return;
  11. define ('imap_messages_php', true);
  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. function sqimap_message_list_squisher($messages_array) {
  59. if( !is_array( $messages_array ) ) return;
  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. if ($msgs_str != '')
  68. $msgs_str .= ',';
  69. $msgs_str .= $start;
  70. if ($start != $end)
  71. $msgs_str .= ':' . $end;
  72. }
  73. return $msgs_str;
  74. }
  75. function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
  76. global $squirrelmail_language, $color;
  77. /* Get the small headers for each message in $msg_list */
  78. $sid = sqimap_session_id();
  79. $maxmsg = sizeof($msg_list);
  80. $msgs_str = sqimap_message_list_squisher($msg_list);
  81. $results = array();
  82. $read_list = array();
  83. $sizes_list = array();
  84. /* We need to return the data in the same order as the caller supplied
  85. in $msg_list, but IMAP servers are free to return responses in
  86. whatever order they wish... So we need to re-sort manually */
  87. for ($i = 0; $i < sizeof($msg_list); $i++) {
  88. $id2index[$msg_list[$i]] = $i;
  89. }
  90. $query = "$sid FETCH $msgs_str BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type)]\r\n";
  91. fputs ($imap_stream, $query);
  92. $readin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
  93. foreach ($readin_list as $r) {
  94. if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
  95. set_up_language($squirrelmail_language);
  96. echo '<br><b><font color=$color[2]>' .
  97. _("ERROR : Could not complete request.") .
  98. '</b><br>' .
  99. _("Unknown response from IMAP server: ") . ' 1.' .
  100. $r[0] . "</font><br>\n";
  101. // exit;
  102. } else if (! isset($id2index[$regs[1]]) || !count($id2index[$regs[1]])) {
  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 message number in reply from server: ") .
  108. $regs[1] . "</font><br>\n";
  109. // exit;
  110. } else {
  111. $read_list[$id2index[$regs[1]]] = $r;
  112. }
  113. }
  114. arsort($read_list);
  115. $query = "$sid FETCH $msgs_str RFC822.SIZE\r\n";
  116. fputs ($imap_stream, $query);
  117. $sizesin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
  118. foreach ($sizesin_list as $r) {
  119. if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
  120. set_up_language($squirrelmail_language);
  121. echo "<br><b><font color=$color[2]>\n";
  122. echo _("ERROR : Could not complete request.");
  123. echo "</b><br>\n";
  124. echo _("Unknown response from IMAP server: ") . ' 2.';
  125. echo $r[0] . "</font><br>\n";
  126. exit;
  127. }
  128. if (!count($id2index[$regs[1]])) {
  129. set_up_language($squirrelmail_language);
  130. echo "<br><b><font color=$color[2]>\n";
  131. echo _("ERROR : Could not complete request.");
  132. echo "</b><br>\n";
  133. echo _("Unknown messagenumber in reply from server: ");
  134. echo $regs[1] . "</font><br>\n";
  135. exit;
  136. }
  137. $sizes_list[$id2index[$regs[1]]] = $r;
  138. }
  139. arsort($sizes_list);
  140. for ($msgi = 0; $msgi < $maxmsg; $msgi++) {
  141. $subject = _("(no subject)");
  142. $from = _("Unknown Sender");
  143. $priority = 0;
  144. $messageid = "<>";
  145. $cc = "";
  146. $to = "";
  147. $date = "";
  148. $type[0] = "";
  149. $type[1] = "";
  150. $read = $read_list[$msgi];
  151. for ($i = 0; $i < count($read); $i++) {
  152. if (eregi ("^to:(.*)$", $read[$i], $regs)) {
  153. //$to = sqimap_find_displayable_name(substr($read[$i], 3));
  154. $to = $regs[1];
  155. } else if (eregi ("^from:(.*)$", $read[$i], $regs)) {
  156. //$from = sqimap_find_displayable_name(substr($read[$i], 5));
  157. $from = $regs[1];
  158. } else if (eregi ("^x-priority:(.*)$", $read[$i], $regs)) {
  159. $priority = trim($regs[1]);
  160. } else if (eregi ("^message-id:(.*)$", $read[$i], $regs)) {
  161. $messageid = trim($regs[1]);
  162. } else if (eregi ("^cc:(.*)$", $read[$i], $regs)) {
  163. $cc = $regs[1];
  164. } else if (eregi ("^date:(.*)$", $read[$i], $regs)) {
  165. $date = $regs[1];
  166. } else if (eregi ("^subject:(.*)$", $read[$i], $regs)) {
  167. $subject = htmlspecialchars(trim($regs[1]));
  168. if ($subject == "")
  169. $subject = _("(no subject)");
  170. } else if (eregi ("^content-type:(.*)$", $read[$i], $regs)) {
  171. $type = strtolower(trim($regs[1]));
  172. if ($pos = strpos($type, ";"))
  173. $type = substr($type, 0, $pos);
  174. $type = explode("/", $type);
  175. if (! isset($type[1]))
  176. $type[1] = '';
  177. }
  178. }
  179. if (trim($date) == "") {
  180. fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
  181. $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
  182. if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
  183. $date_list = explode(" ", trim($regs[1]));
  184. $date_list[0] = str_replace("-", " ", $date_list[0]);
  185. $date = implode(" ", $date_list);
  186. }
  187. }
  188. eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
  189. $size = $regs[1];
  190. $header = new small_header;
  191. if ($issent == true)
  192. $header->from = (trim($to) != '')? $to : _("(only Cc/Bcc)");
  193. else
  194. $header->from = $from;
  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. /** TO **/
  401. else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
  402. $pos = 0;
  403. $hdr->to[$pos] = trim(substr($read[$i], 4));
  404. $i++;
  405. while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
  406. $pos++;
  407. $hdr->to[$pos] = trim($read[$i]);
  408. $i++;
  409. }
  410. }
  411. /** MESSAGE ID **/
  412. else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
  413. $hdr->message_id = trim(substr($read[$i], 11));
  414. $i++;
  415. }
  416. /** ERROR CORRECTION **/
  417. else if (substr($read[$i], 0, 1) == ")") {
  418. if (strlen(trim($hdr->subject)) == 0)
  419. $hdr->subject = _("(no subject)");
  420. if (strlen(trim($hdr->from)) == 0)
  421. $hdr->from = _("(unknown sender)");
  422. if (strlen(trim($hdr->date)) == 0)
  423. $hdr->date = time();
  424. $i++;
  425. }
  426. else {
  427. $i++;
  428. }
  429. }
  430. return $hdr;
  431. }
  432. /******************************************************************************
  433. ** Returns the body of a message.
  434. ******************************************************************************/
  435. function sqimap_get_message_body ($imap_stream, &$header) {
  436. $id = $header->id;
  437. return decodeMime($imap_stream, $header);
  438. }
  439. /******************************************************************************
  440. ** Returns an array with the body structure
  441. ******************************************************************************/
  442. ?>