imap_messages.php 27 KB

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