imap_messages.php 29 KB

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