imap_messages.php 26 KB

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