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