imap_messages.php 27 KB

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