imap_messages.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. <?php
  2. /**
  3. * imap_messages.php
  4. *
  5. * Copyright (c) 1999-2004 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. * NOTE: Quite a few functions in this file are obsolete
  10. *
  11. * @version $Id$
  12. * @package squirrelmail
  13. * @subpackage imap
  14. */
  15. /**
  16. * Copies specified messages to specified folder
  17. * @param int $imap_stream The resource ID for the IMAP connection
  18. * @param string $start Beginning of range to copy
  19. * @param string $end End of the range to copy
  20. * @param string $mailbox Which box to copy to
  21. * @deprecated This function is obsolete and should not be used
  22. */
  23. function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
  24. $read = sqimap_run_command ($imap_stream, "COPY $start:$end " . sqimap_encode_mailbox_name($mailbox), true, $response, $message, TRUE);
  25. }
  26. /**
  27. * copy a range of messages ($id) to another mailbox ($mailbox)
  28. * @param int $imap_stream The resource ID for the IMAP socket
  29. * @param string $id The list of messages to copy
  30. * @param string $mailbox The destination to copy to
  31. * @return void
  32. */
  33. function sqimap_msgs_list_copy ($imap_stream, $id, $mailbox) {
  34. $msgs_id = sqimap_message_list_squisher($id);
  35. $read = sqimap_run_command ($imap_stream, "COPY $msgs_id " . sqimap_encode_mailbox_name($mailbox), true, $response, $message, TRUE);
  36. }
  37. /**
  38. * move a range of messages ($id) to another mailbox. Deletes the originals.
  39. * @param int $imap_stream The resource ID for the IMAP socket
  40. * @param string $id The list of messages to move
  41. * @param string $mailbox The destination to move to
  42. * @return void
  43. */
  44. function sqimap_msgs_list_move ($imap_stream, $id, $mailbox) {
  45. $msgs_id = sqimap_message_list_squisher($id);
  46. $read = sqimap_run_command ($imap_stream, "COPY $msgs_id " . sqimap_encode_mailbox_name($mailbox), true, $response, $message, TRUE);
  47. $read = sqimap_run_command ($imap_stream, "STORE $msgs_id +FLAGS (\\Deleted)", true, $response,$message, TRUE);
  48. }
  49. /**
  50. * Deletes specified messages and moves them to trash if possible
  51. * @deprecated This function is obsolete and should no longer be used
  52. * @param int $imap_steam The resource ID for the IMAP connection
  53. * @param string $start Start of range
  54. * @param string $end End of range
  55. * @param string $mailbox Mailbox messages are being deleted from
  56. * @return void
  57. */
  58. function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox, $bypass_trash=false) {
  59. global $move_to_trash, $trash_folder, $auto_expunge;
  60. if (($move_to_trash == true) && ($bypass_trash != true) &&
  61. (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
  62. sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
  63. }
  64. sqimap_messages_flag ($imap_stream, $start, $end, "Deleted", true);
  65. }
  66. function sqimap_msgs_list_delete ($imap_stream, $mailbox, $id, $bypass_trash=false) {
  67. global $move_to_trash, $trash_folder;
  68. $msgs_id = sqimap_message_list_squisher($id);
  69. if (($move_to_trash == true) && ($bypass_trash != true) &&
  70. (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder)) ) {
  71. $read = sqimap_run_command ($imap_stream, "COPY $msgs_id " . sqimap_encode_mailbox_name($trash_folder), true, $response, $message, TRUE);
  72. }
  73. $read = sqimap_run_command ($imap_stream, "STORE $msgs_id +FLAGS (\\Deleted)", true, $response, $message, TRUE);
  74. }
  75. /**
  76. * Sets the specified messages with specified flag
  77. */
  78. function sqimap_messages_flag ($imap_stream, $start, $end, $flag, $handle_errors) {
  79. $read = sqimap_run_command ($imap_stream, "STORE $start:$end +FLAGS (\\$flag)", $handle_errors, $response, $message, TRUE);
  80. }
  81. function sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors) {
  82. $msgs_id = sqimap_message_list_squisher($id);
  83. $set_string = ($set ? '+' : '-');
  84. $read = sqimap_run_command ($imap_stream, "STORE $msgs_id ".$set_string."FLAGS ($flag)", $handle_errors, $response, $message, TRUE);
  85. }
  86. /** @deprecated */
  87. function sqimap_get_small_header ($imap_stream, $id, $sent) {
  88. $res = sqimap_get_small_header_list($imap_stream, $id, $sent);
  89. return $res[0];
  90. }
  91. /**
  92. * Sort the message list and crunch to be as small as possible
  93. * (overflow could happen, so make it small if possible)
  94. */
  95. function sqimap_message_list_squisher($messages_array) {
  96. if( !is_array( $messages_array ) ) {
  97. return $messages_array;
  98. }
  99. sort($messages_array, SORT_NUMERIC);
  100. $msgs_str = '';
  101. while ($messages_array) {
  102. $start = array_shift($messages_array);
  103. $end = $start;
  104. while (isset($messages_array[0]) && $messages_array[0] == $end + 1) {
  105. $end = array_shift($messages_array);
  106. }
  107. if ($msgs_str != '') {
  108. $msgs_str .= ',';
  109. }
  110. $msgs_str .= $start;
  111. if ($start != $end) {
  112. $msgs_str .= ':' . $end;
  113. }
  114. }
  115. return $msgs_str;
  116. }
  117. /**
  118. * Get sort order from server and return it as the $id array for mailbox_display.
  119. */
  120. function sqimap_get_sort_order ($imap_stream, $sort, $mbxresponse) {
  121. global $default_charset, $thread_sort_messages,
  122. $internal_date_sort, $server_sort_array,
  123. $sent_folder, $mailbox;
  124. if (sqsession_is_registered('server_sort_array')) {
  125. sqsession_unregister('server_sort_array');
  126. }
  127. $sort_on = array();
  128. $reverse = 0;
  129. $server_sort_array = array();
  130. $sort_test = array();
  131. $sort_query = '';
  132. if ($sort == 6) {
  133. if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) {
  134. $uidnext = $mbxresponse['UIDNEXT']-1;
  135. } else {
  136. $uidnext = '*';
  137. }
  138. $query = "SEARCH UID 1:$uidnext";
  139. $uids = sqimap_run_command ($imap_stream, $query, true, $response, $message, true);
  140. if (isset($uids[0])) {
  141. if (preg_match("/^\* SEARCH (.+)$/", $uids[0], $regs)) {
  142. $server_sort_array = preg_split("/ /", trim($regs[1]));
  143. }
  144. }
  145. if (!preg_match("/OK/", $response)) {
  146. $server_sort_array = 'no';
  147. }
  148. $server_sort_array = array_reverse($server_sort_array);
  149. sqsession_register($server_sort_array, 'server_sort_array');
  150. return $server_sort_array;
  151. }
  152. $sort_on = array (0=> 'DATE',
  153. 1=> 'DATE',
  154. 2=> 'FROM',
  155. 3=> 'FROM',
  156. 4=> 'SUBJECT',
  157. 5=> 'SUBJECT');
  158. if ($internal_date_sort == true) {
  159. $sort_on[0] = 'ARRIVAL';
  160. $sort_on[1] = 'ARRIVAL';
  161. }
  162. if ($sent_folder == $mailbox) {
  163. $sort_on[2] = 'TO';
  164. $sort_on[3] = 'TO';
  165. }
  166. if (!empty($sort_on[$sort])) {
  167. $query = "SORT ($sort_on[$sort]) ".strtoupper($default_charset).' ALL';
  168. $sort_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE);
  169. }
  170. if (isset($sort_test[0])) {
  171. for ($i=0,$iCnt=count($sort_test);$i<$iCnt;++$i) {
  172. if (preg_match("/^\* SORT (.+)$/", $sort_test[$i], $regs)) {
  173. $server_sort_array = preg_split("/ /", trim($regs[1]));
  174. break;
  175. }
  176. }
  177. }
  178. if ($sort == 0 || $sort == 2 || $sort == 4) {
  179. $server_sort_array = array_reverse($server_sort_array);
  180. }
  181. if (!preg_match("/OK/", $response)) {
  182. $server_sort_array = 'no';
  183. }
  184. sqsession_register($server_sort_array, 'server_sort_array');
  185. return $server_sort_array;
  186. }
  187. /**
  188. * Get sort order from server if server does not have the SORT extension
  189. * and return it as array for mailbox_display.
  190. *
  191. * @param resource $imap_stream
  192. * @param array $mbxresponse response from a sqimap_mailbox_select
  193. * @return array $php_sort_array
  194. */
  195. function sqimap_get_php_sort_order ($imap_stream, $mbxresponse) {
  196. if (sqsession_is_registered('php_sort_array')) {
  197. sqsession_unregister('php_sort_array');
  198. }
  199. $php_sort_array = array();
  200. if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) {
  201. $uidnext = $mbxresponse['UIDNEXT']-1;
  202. } else {
  203. $uidnext = '*';
  204. }
  205. $query = "SEARCH UID 1:$uidnext";
  206. $uids = sqimap_run_command ($imap_stream, $query, true, $response, $message, true);
  207. if (isset($uids[0])) {
  208. $php_sort_array = array();
  209. // EIMS workaround. EIMS returns the result as multiple untagged SEARCH responses
  210. foreach($uids as $line) {
  211. if (preg_match("/^\* SEARCH (.+)$/", $line, $regs)) {
  212. $php_sort_array += preg_split("/ /", trim($regs[1]));
  213. }
  214. }
  215. }
  216. if (!preg_match("/OK/", $response)) {
  217. $php_sort_array = 'no';
  218. }
  219. sqsession_register($php_sort_array, 'php_sort_array');
  220. return $php_sort_array;
  221. }
  222. /**
  223. * Returns an indent array for printMessageinfo()
  224. * This represents the amount of indent needed (value),
  225. * for this message number (key)
  226. */
  227. function get_parent_level ($imap_stream) {
  228. global $sort_by_ref, $default_charset, $thread_new;
  229. $parent = '';
  230. $child = '';
  231. $cutoff = 0;
  232. /* loop through the threads and take unwanted characters out
  233. of the thread string then chop it up
  234. */
  235. for ($i=0;$i<count($thread_new);$i++) {
  236. $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]);
  237. $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]);
  238. $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY);
  239. }
  240. $indent_array = array();
  241. if (!$thread_new) {
  242. $thread_new = array();
  243. }
  244. /* looping through the parts of one message thread */
  245. for ($i=0;$i<count($thread_new);$i++) {
  246. /* first grab the parent, it does not indent */
  247. if (isset($thread_new[$i][0])) {
  248. if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) {
  249. $parent = $regs[1];
  250. }
  251. }
  252. $indent_array[$parent] = 0;
  253. /* now the children, checking each thread portion for
  254. ),(, and space, adjusting the level and space values
  255. to get the indent level
  256. */
  257. $level = 0;
  258. $spaces = array();
  259. $spaces_total = 0;
  260. $indent = 0;
  261. $fake = FALSE;
  262. for ($k=1;$k<(count($thread_new[$i]))-1;$k++) {
  263. $chars = count_chars($thread_new[$i][$k], 1);
  264. if (isset($chars['40'])) { /* testing for ( */
  265. $level = $level + $chars['40'];
  266. }
  267. if (isset($chars['41'])) { /* testing for ) */
  268. $level = $level - $chars['41'];
  269. $spaces[$level] = 0;
  270. /* if we were faking lets stop, this portion
  271. of the thread is over
  272. */
  273. if ($level == $cutoff) {
  274. $fake = FALSE;
  275. }
  276. }
  277. if (isset($chars['32'])) { /* testing for space */
  278. if (!isset($spaces[$level])) {
  279. $spaces[$level] = 0;
  280. }
  281. $spaces[$level] = $spaces[$level] + $chars['32'];
  282. }
  283. for ($x=0;$x<=$level;$x++) {
  284. if (isset($spaces[$x])) {
  285. $spaces_total = $spaces_total + $spaces[$x];
  286. }
  287. }
  288. $indent = $level + $spaces_total;
  289. /* must have run into a message that broke the thread
  290. so we are adjusting for that portion
  291. */
  292. if ($fake == TRUE) {
  293. $indent = $indent +1;
  294. }
  295. if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
  296. $child = $regs[1];
  297. }
  298. /* the thread must be broken if $indent == 0
  299. so indent the message once and start faking it
  300. */
  301. if ($indent == 0) {
  302. $indent = 1;
  303. $fake = TRUE;
  304. $cutoff = $level;
  305. }
  306. /* dont need abs but if indent was negative
  307. errors would occur
  308. */
  309. $indent_array[$child] = abs($indent);
  310. $spaces_total = 0;
  311. }
  312. }
  313. return $indent_array;
  314. }
  315. /**
  316. * Returns an array with each element as a string representing one
  317. * message-thread as returned by the IMAP server.
  318. */
  319. function get_thread_sort ($imap_stream) {
  320. global $thread_new, $sort_by_ref, $default_charset, $server_sort_array;
  321. if (sqsession_is_registered('thread_new')) {
  322. sqsession_unregister('thread_new');
  323. }
  324. if (sqsession_is_registered('server_sort_array')) {
  325. sqsession_unregister('server_sort_array');
  326. }
  327. $thread_temp = array ();
  328. if ($sort_by_ref == 1) {
  329. $sort_type = 'REFERENCES';
  330. }
  331. else {
  332. $sort_type = 'ORDEREDSUBJECT';
  333. }
  334. $query = "THREAD $sort_type ".strtoupper($default_charset)." ALL";
  335. $thread_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE);
  336. if (isset($thread_test[0])) {
  337. for ($i=0,$iCnt=count($thread_test);$i<$iCnt;++$i) {
  338. if (preg_match("/^\* THREAD (.+)$/", $thread_test[$i], $regs)) {
  339. $thread_list = trim($regs[1]);
  340. break;
  341. }
  342. }
  343. }
  344. else {
  345. $thread_list = "";
  346. }
  347. if (!preg_match("/OK/", $response)) {
  348. $server_sort_array = 'no';
  349. return $server_sort_array;
  350. }
  351. if (isset($thread_list)) {
  352. $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
  353. }
  354. $char_count = count($thread_temp);
  355. $counter = 0;
  356. $thread_new = array();
  357. $k = 0;
  358. $thread_new[0] = "";
  359. for ($i=0;$i<$char_count;$i++) {
  360. if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
  361. $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
  362. }
  363. elseif ($thread_temp[$i] == '(') {
  364. $thread_new[$k] .= $thread_temp[$i];
  365. $counter++;
  366. }
  367. elseif ($thread_temp[$i] == ')') {
  368. if ($counter > 1) {
  369. $thread_new[$k] .= $thread_temp[$i];
  370. $counter = $counter - 1;
  371. }
  372. else {
  373. $thread_new[$k] .= $thread_temp[$i];
  374. $k++;
  375. $thread_new[$k] = "";
  376. $counter = $counter - 1;
  377. }
  378. }
  379. }
  380. sqsession_register($thread_new, 'thread_new');
  381. $thread_new = array_reverse($thread_new);
  382. $thread_list = implode(" ", $thread_new);
  383. $thread_list = str_replace("(", " ", $thread_list);
  384. $thread_list = str_replace(")", " ", $thread_list);
  385. $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
  386. $server_sort_array = $thread_list;
  387. sqsession_register($server_sort_array, 'server_sort_array');
  388. return $thread_list;
  389. }
  390. function elapsedTime($start) {
  391. $stop = gettimeofday();
  392. $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec'];
  393. return $timepassed;
  394. }
  395. // only used in sqimap_get_small_header_list
  396. function parseString($read,&$i) {
  397. $char = $read{$i};
  398. $s = '';
  399. if ($char == '"') {
  400. $iPos = ++$i;
  401. while (true) {
  402. $iPos = strpos($read,'"',$iPos);
  403. if (!$iPos) break;
  404. if ($iPos && $read{$iPos -1} != '\\') {
  405. $s = substr($read,$i,($iPos-$i));
  406. $i = $iPos;
  407. break;
  408. }
  409. $iPos++;
  410. if ($iPos > strlen($read)) {
  411. break;
  412. }
  413. }
  414. } else if ($char == '{') {
  415. $lit_cnt = '';
  416. ++$i;
  417. $iPos = strpos($read,'}',$i);
  418. if ($iPos) {
  419. $lit_cnt = substr($read, $i, $iPos - $i);
  420. $i += strlen($lit_cnt) + 3; /* skip } + \r + \n */
  421. /* Now read the literal */
  422. $s = ($lit_cnt ? substr($read,$i,$lit_cnt): '');
  423. $i += $lit_cnt;
  424. /* temp bugfix (SM 1.5 will have a working clean version)
  425. too much work to implement that version right now */
  426. --$i;
  427. } else { /* should never happen */
  428. $i += 3; /* } + \r + \n */
  429. $s = '';
  430. }
  431. } else {
  432. return false;
  433. }
  434. ++$i;
  435. return $s;
  436. }
  437. // only used in sqimap_get_small_header_list
  438. function parseArray($read,&$i) {
  439. $i = strpos($read,'(',$i);
  440. $i_pos = strpos($read,')',$i);
  441. $s = substr($read,$i+1,$i_pos - $i -1);
  442. $a = explode(' ',$s);
  443. if ($i_pos) {
  444. $i = $i_pos+1;
  445. return $a;
  446. } else {
  447. return false;
  448. }
  449. }
  450. function sqimap_get_small_header_list ($imap_stream, $msg_list, $show_num=false) {
  451. global $squirrelmail_language, $color, $data_dir, $username, $imap_server_type;
  452. global $allow_server_sort;
  453. /* Get the small headers for each message in $msg_list */
  454. $maxmsg = sizeof($msg_list);
  455. if ($show_num != '999999') {
  456. $msgs_str = sqimap_message_list_squisher($msg_list);
  457. } else {
  458. $msgs_str = '1:*';
  459. }
  460. $messages = array();
  461. $read_list = array();
  462. /*
  463. * We need to return the data in the same order as the caller supplied
  464. * in $msg_list, but IMAP servers are free to return responses in
  465. * whatever order they wish... So we need to re-sort manually
  466. */
  467. for ($i = 0; $i < sizeof($msg_list); $i++) {
  468. $messages["$msg_list[$i]"] = array();
  469. }
  470. $internaldate = getPref($data_dir, $username, 'internal_date_sort');
  471. if ($internaldate) {
  472. $query = "FETCH $msgs_str (FLAGS UID RFC822.SIZE INTERNALDATE BODY.PEEK[HEADER.FIELDS (Date To Cc From Subject X-Priority Content-Type)])";
  473. } else {
  474. $query = "FETCH $msgs_str (FLAGS UID RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Date To Cc From Subject X-Priority Content-Type)])";
  475. }
  476. $read_list = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, TRUE);
  477. $i = 0;
  478. foreach ($read_list as $r) {
  479. /* initialize/reset vars */
  480. $subject = _("(no subject)");
  481. $from = _("Unknown sender");
  482. $priority = 0;
  483. $messageid = '<>';
  484. $type = array('','');
  485. $cc = $to = $inrepto = '';
  486. // use unset because we do isset below
  487. unset($date);
  488. $flag_seen = $flag_answered = $flag_deleted = $flag_flagged = false;
  489. $read = implode('',$r);
  490. /*
  491. * #id<space>FETCH<space>(
  492. */
  493. /* extract the message id */
  494. $i_space = strpos($read,' ',2);
  495. $id = substr($read,2,$i_space-2);
  496. $fetch = substr($read,$i_space+1,5);
  497. if (!is_numeric($id) && $fetch !== 'FETCH') {
  498. set_up_language($squirrelmail_language);
  499. echo '<br><b><font color=$color[2]>' .
  500. _("ERROR : Could not complete request.") .
  501. '</b><br>' .
  502. _("Unknown response from IMAP server: ") . ' 1.' .
  503. htmlspecialchars($read) . "</font><br>\n";
  504. break;
  505. }
  506. $i = strpos($read,'(',$i_space+5);
  507. $read = substr($read,$i+1);
  508. $i_len = strlen($read);
  509. $i = 0;
  510. while ($i < $i_len && $i !== false) {
  511. /* get argument */
  512. $read = trim(substr($read,$i));
  513. $i_len = strlen($read);
  514. $i = strpos($read,' ');
  515. $arg = substr($read,0,$i);
  516. ++$i;
  517. switch ($arg)
  518. {
  519. case 'UID':
  520. $i_pos = strpos($read,' ',$i);
  521. if (!$i_pos) {
  522. $i_pos = strpos($read,')',$i);
  523. }
  524. if ($i_pos) {
  525. $unique_id = substr($read,$i,$i_pos-$i);
  526. $i = $i_pos+1;
  527. } else {
  528. break 3;
  529. }
  530. break;
  531. case 'FLAGS':
  532. $flags = parseArray($read,$i);
  533. if (!$flags) break 3;
  534. foreach ($flags as $flag) {
  535. $flag = strtolower($flag);
  536. switch ($flag)
  537. {
  538. case '\\seen': $flag_seen = true; break;
  539. case '\\answered': $flag_answered = true; break;
  540. case '\\deleted': $flag_deleted = true; break;
  541. case '\\flagged': $flag_flagged = true; break;
  542. default: break;
  543. }
  544. }
  545. break;
  546. case 'RFC822.SIZE':
  547. $i_pos = strpos($read,' ',$i);
  548. if (!$i_pos) {
  549. $i_pos = strpos($read,')',$i);
  550. }
  551. if ($i_pos) {
  552. $size = substr($read,$i,$i_pos-$i);
  553. $i = $i_pos+1;
  554. } else {
  555. break 3;
  556. }
  557. break;
  558. case 'INTERNALDATE':
  559. $date = parseString($read,$i);
  560. //if ($tmpdate === false) break 3;
  561. //$tmpdate = str_replace(' ',' ',$tmpdate);
  562. //$tmpdate = explode(' ',$tmpdate);
  563. //$date = str_replace('-',' ',$tmpdate[0]) . " " .
  564. // $tmpdate[1] . ' ' . $tmpdate[2];
  565. break;
  566. case 'BODY.PEEK[HEADER.FIELDS':
  567. case 'BODY[HEADER.FIELDS':
  568. $i = strpos($read,'{',$i);
  569. $header = parseString($read,$i);
  570. if ($header === false) break 2;
  571. /* First we replace all \r\n by \n, and unfold the header */
  572. $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $header));
  573. /* Now we can make a new header array with */
  574. /* each element representing a headerline */
  575. $hdr = explode("\n" , $hdr);
  576. foreach ($hdr as $line) {
  577. $pos = strpos($line, ':');
  578. if ($pos > 0) {
  579. $field = strtolower(substr($line, 0, $pos));
  580. if (!strstr($field,' ')) { /* valid field */
  581. $value = trim(substr($line, $pos+1));
  582. switch($field)
  583. {
  584. case 'to': $to = $value; break;
  585. case 'cc': $cc = $value; break;
  586. case 'from': $from = $value; break;
  587. case 'date': $date = $value; break;
  588. case 'x-priority': $priority = $value; break;
  589. case 'subject':
  590. $subject = $value;
  591. if ($subject == "") {
  592. $subject = _("(no subject)");
  593. }
  594. break;
  595. case 'content-type':
  596. $type = $value;
  597. if ($pos = strpos($type, ";")) {
  598. $type = substr($type, 0, $pos);
  599. }
  600. $type = explode("/", $type);
  601. if(!is_array($type)) {
  602. $type[0] = 'text';
  603. }
  604. break;
  605. default: break;
  606. }
  607. }
  608. }
  609. }
  610. break;
  611. default:
  612. ++$i;
  613. break;
  614. }
  615. }
  616. if (isset($date)) {
  617. $date = str_replace(' ', ' ', $date);
  618. $tmpdate = explode(' ', trim($date));
  619. } else {
  620. $tmpdate = $date = array();
  621. }
  622. $msgi ="$unique_id";
  623. $messages[$msgi]['ID'] = $unique_id;
  624. $messages[$msgi]['TIME_STAMP'] = getTimeStamp($tmpdate);
  625. $messages[$msgi]['DATE_STRING'] = getDateString($messages[$msgi]['TIME_STAMP']);
  626. $messages[$msgi]['FROM'] = $from; //parseAddress($from);
  627. $messages[$msgi]['SUBJECT'] = $subject;
  628. // if (handleAsSent($mailbox)) {
  629. $messages[$msgi]['TO'] = $to; //parseAddress($to);
  630. // }
  631. $messages[$msgi]['PRIORITY'] = $priority;
  632. $messages[$msgi]['CC'] = $cc; //parseAddress($cc);
  633. $messages[$msgi]['SIZE'] = $size;
  634. $messages[$msgi]['TYPE0'] = $type[0];
  635. $messages[$msgi]['FLAG_DELETED'] = $flag_deleted;
  636. $messages[$msgi]['FLAG_ANSWERED'] = $flag_answered;
  637. $messages[$msgi]['FLAG_SEEN'] = $flag_seen;
  638. $messages[$msgi]['FLAG_FLAGGED'] = $flag_flagged;
  639. /* non server sort stuff */
  640. if (!$allow_server_sort) {
  641. $from = parseAddress($from);
  642. if ($from[0][1]) {
  643. $from = decodeHeader($from[0][1]);
  644. } else {
  645. $from = $from[0][0];
  646. }
  647. $messages[$msgi]['FROM-SORT'] = $from;
  648. $subject_sort = strtolower(decodeHeader($subject));
  649. if (preg_match("/^(vedr|sv|re|aw):\s*(.*)$/si", $subject_sort, $matches)){
  650. $messages[$msgi]['SUBJECT-SORT'] = $matches[2];
  651. } else {
  652. $messages[$msgi]['SUBJECT-SORT'] = $subject_sort;
  653. }
  654. }
  655. ++$msgi;
  656. }
  657. array_reverse($messages);
  658. $new_messages = array();
  659. foreach ($messages as $i =>$message) {
  660. $new_messages[] = $message;
  661. }
  662. return $new_messages;
  663. }
  664. /**
  665. * Returns a message array with all the information about a message.
  666. * See the documentation folder for more information about this array.
  667. */
  668. function sqimap_get_message ($imap_stream, $id, $mailbox) {
  669. $flags = array();
  670. $read = sqimap_run_command ($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, TRUE);
  671. if ($read) {
  672. if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) {
  673. if (trim($regs[1])) {
  674. $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY');
  675. }
  676. }
  677. } else {
  678. /* the message was not found, maybe the mailbox was modified? */
  679. global $sort, $startMessage, $color;
  680. $errmessage = _("The server couldn't find the message you requested.") .
  681. '<p>'._("Most probably your message list was out of date and the message has been moved away or deleted (perhaps by another program accessing the same mailbox).");
  682. /* this will include a link back to the message list */
  683. error_message($errmessage, $mailbox, $sort, $startMessage, $color);
  684. exit;
  685. }
  686. $bodystructure = implode('',$read);
  687. $msg = mime_structure($bodystructure,$flags);
  688. $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, TRUE);
  689. $rfc822_header = new Rfc822Header();
  690. $rfc822_header->parseHeader($read);
  691. $msg->rfc822_header = $rfc822_header;
  692. return $msg;
  693. }
  694. ?>