imap_messages.php 27 KB

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