imap_messages.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. if (preg_match("/^\* THREAD (.+)$/", $thread_test[0], $regs)) {
  318. $thread_list = trim($regs[1]);
  319. }
  320. }
  321. else {
  322. $thread_list = "";
  323. }
  324. if (!preg_match("/OK/", $response)) {
  325. $server_sort_array = 'no';
  326. return $server_sort_array;
  327. }
  328. if (isset($thread_list)) {
  329. $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
  330. }
  331. $char_count = count($thread_temp);
  332. $counter = 0;
  333. $thread_new = array();
  334. $k = 0;
  335. $thread_new[0] = "";
  336. for ($i=0;$i<$char_count;$i++) {
  337. if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
  338. $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
  339. }
  340. elseif ($thread_temp[$i] == '(') {
  341. $thread_new[$k] .= $thread_temp[$i];
  342. $counter++;
  343. }
  344. elseif ($thread_temp[$i] == ')') {
  345. if ($counter > 1) {
  346. $thread_new[$k] .= $thread_temp[$i];
  347. $counter = $counter - 1;
  348. }
  349. else {
  350. $thread_new[$k] .= $thread_temp[$i];
  351. $k++;
  352. $thread_new[$k] = "";
  353. $counter = $counter - 1;
  354. }
  355. }
  356. }
  357. sqsession_register($thread_new, 'thread_new');
  358. $thread_new = array_reverse($thread_new);
  359. $thread_list = implode(" ", $thread_new);
  360. $thread_list = str_replace("(", " ", $thread_list);
  361. $thread_list = str_replace(")", " ", $thread_list);
  362. $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
  363. $server_sort_array = $thread_list;
  364. sqsession_register($server_sort_array, 'server_sort_array');
  365. return $thread_list;
  366. }
  367. function elapsedTime($start) {
  368. $stop = gettimeofday();
  369. $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec'];
  370. return $timepassed;
  371. }
  372. // only used in sqimap_get_small_header_list
  373. function parseString($read,&$i) {
  374. $char = $read{$i};
  375. $s = '';
  376. if ($char == '"') {
  377. $iPos = ++$i;
  378. while (true) {
  379. $iPos = strpos($read,'"',$iPos);
  380. if (!$iPos) break;
  381. if ($iPos && $read{$iPos -1} != '\\') {
  382. $s = substr($read,$i,($iPos-$i));
  383. $i = $iPos;
  384. break;
  385. }
  386. $iPos++;
  387. if ($iPos > strlen($read)) {
  388. break;
  389. }
  390. }
  391. } else if ($char == '{') {
  392. $lit_cnt = '';
  393. ++$i;
  394. $iPos = strpos($read,'}',$i);
  395. if ($iPos) {
  396. $lit_cnt = substr($read, $i, $iPos - $i);
  397. $i += strlen($lit_cnt) + 3; /* skip } + \r + \n */
  398. /* Now read the literal */
  399. $s = ($lit_cnt ? substr($read,$i,$lit_cnt): '');
  400. $i += $lit_cnt;
  401. /* temp bugfix (SM 1.5 will have a working clean version)
  402. too much work to implement that version right now */
  403. --$i;
  404. } else { /* should never happen */
  405. $i += 3; /* } + \r + \n */
  406. $s = '';
  407. }
  408. } else {
  409. return false;
  410. }
  411. ++$i;
  412. return $s;
  413. }
  414. // only used in sqimap_get_small_header_list
  415. function parseArray($read,&$i) {
  416. $i = strpos($read,'(',$i);
  417. $i_pos = strpos($read,')',$i);
  418. $s = substr($read,$i+1,$i_pos - $i -1);
  419. $a = explode(' ',$s);
  420. if ($i_pos) {
  421. $i = $i_pos+1;
  422. return $a;
  423. } else {
  424. return false;
  425. }
  426. }
  427. function sqimap_get_small_header_list ($imap_stream, $msg_list, $show_num=false) {
  428. global $squirrelmail_language, $color, $data_dir, $username, $imap_server_type;
  429. global $uid_support, $allow_server_sort;
  430. /* Get the small headers for each message in $msg_list */
  431. $maxmsg = sizeof($msg_list);
  432. if ($show_num != '999999') {
  433. $msgs_str = sqimap_message_list_squisher($msg_list);
  434. } else {
  435. $msgs_str = '1:*';
  436. }
  437. $messages = array();
  438. $read_list = array();
  439. /*
  440. * We need to return the data in the same order as the caller supplied
  441. * in $msg_list, but IMAP servers are free to return responses in
  442. * whatever order they wish... So we need to re-sort manually
  443. */
  444. for ($i = 0; $i < sizeof($msg_list); $i++) {
  445. $messages["$msg_list[$i]"] = array();
  446. }
  447. $internaldate = getPref($data_dir, $username, 'internal_date_sort');
  448. if ($internaldate) {
  449. $query = "FETCH $msgs_str (FLAGS UID RFC822.SIZE INTERNALDATE BODY.PEEK[HEADER.FIELDS (Date To Cc From Subject X-Priority Content-Type)])";
  450. } else {
  451. $query = "FETCH $msgs_str (FLAGS UID RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Date To Cc From Subject X-Priority Content-Type)])";
  452. }
  453. $read_list = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, $uid_support);
  454. $i = 0;
  455. foreach ($read_list as $r) {
  456. $subject = _("(no subject)");
  457. $from = _("Unknown Sender");
  458. $priority = 0;
  459. $messageid = '<>';
  460. $cc = $to = $date = $type[0] = $type[1] = $inrepto = '';
  461. $flag_seen = $flag_answered = $flag_deleted = $flag_flagged = false;
  462. $read = implode('',$r);
  463. /*
  464. * #id<space>FETCH<space>(
  465. */
  466. /* extract the message id */
  467. $i_space = strpos($read,' ',2);
  468. $id = substr($read,2,$i_space-2);
  469. $fetch = substr($read,$i_space+1,5);
  470. if (!is_numeric($id) && $fetch !== 'FETCH') {
  471. set_up_language($squirrelmail_language);
  472. echo '<br><b><font color=$color[2]>' .
  473. _("ERROR : Could not complete request.") .
  474. '</b><br>' .
  475. _("Unknown response from IMAP server: ") . ' 1.' .
  476. htmlspecialchars($read) . "</font><br>\n";
  477. break;
  478. }
  479. $i = strpos($read,'(',$i_space+5);
  480. $read = substr($read,$i+1);
  481. $i_len = strlen($read);
  482. $i = 0;
  483. while ($i < $i_len && $i !== false) {
  484. /* get argument */
  485. $read = trim(substr($read,$i));
  486. $i_len = strlen($read);
  487. $i = strpos($read,' ');
  488. $arg = substr($read,0,$i);
  489. ++$i;
  490. switch ($arg)
  491. {
  492. case 'UID':
  493. $i_pos = strpos($read,' ',$i);
  494. if (!$i_pos) {
  495. $i_pos = strpos($read,')',$i);
  496. }
  497. if ($i_pos) {
  498. $unique_id = substr($read,$i,$i_pos-$i);
  499. $i = $i_pos+1;
  500. } else {
  501. break 3;
  502. }
  503. break;
  504. case 'FLAGS':
  505. $flags = parseArray($read,$i);
  506. if (!$flags) break 3;
  507. foreach ($flags as $flag) {
  508. $flag = strtolower($flag);
  509. switch ($flag)
  510. {
  511. case '\\seen': $flag_seen = true; break;
  512. case '\\answered': $flag_answered = true; break;
  513. case '\\deleted': $flag_deleted = true; break;
  514. case '\\flagged': $flag_flagged = true; break;
  515. default: break;
  516. }
  517. }
  518. break;
  519. case 'RFC822.SIZE':
  520. $i_pos = strpos($read,' ',$i);
  521. if (!$i_pos) {
  522. $i_pos = strpos($read,')',$i);
  523. }
  524. if ($i_pos) {
  525. $size = substr($read,$i,$i_pos-$i);
  526. $i = $i_pos+1;
  527. } else {
  528. break 3;
  529. }
  530. break;
  531. case 'INTERNALDATE':
  532. $date = parseString($read,$i);
  533. //if ($tmpdate === false) break 3;
  534. //$tmpdate = str_replace(' ',' ',$tmpdate);
  535. //$tmpdate = explode(' ',$tmpdate);
  536. //$date = str_replace('-',' ',$tmpdate[0]) . " " .
  537. // $tmpdate[1] . ' ' . $tmpdate[2];
  538. break;
  539. case 'BODY.PEEK[HEADER.FIELDS':
  540. case 'BODY[HEADER.FIELDS':
  541. $i = strpos($read,'{',$i);
  542. $header = parseString($read,$i);
  543. if ($header === false) break 3;
  544. /* First we unfold the header */
  545. $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array('', ''), $header));
  546. /* Now we can make a new header array with */
  547. /* each element representing a headerline */
  548. $hdr = explode("\r\n" , $hdr);
  549. foreach ($hdr as $line) {
  550. $pos = strpos($line, ':');
  551. if ($pos > 0) {
  552. $field = strtolower(substr($line, 0, $pos));
  553. if (!strstr($field,' ')) { /* valid field */
  554. $value = trim(substr($line, $pos+1));
  555. switch($field)
  556. {
  557. case 'to': $to = $value; break;
  558. case 'cc': $cc = $value; break;
  559. case 'from': $from = $value; break;
  560. case 'date': $date = $value; break;
  561. case 'x-priority': $priority = $value; break;
  562. case 'subject':
  563. $subject = $value;
  564. if ($subject == "") {
  565. $subject = _("(no subject)");
  566. }
  567. break;
  568. case 'content-type':
  569. $type = $value;
  570. if ($pos = strpos($type, ";")) {
  571. $type = substr($type, 0, $pos);
  572. }
  573. $type = explode("/", $type);
  574. if(!is_array($type)) {
  575. $type[0] = 'text';
  576. }
  577. if (!isset($type[1])) {
  578. $type[1] = '';
  579. }
  580. break;
  581. default: break;
  582. }
  583. }
  584. }
  585. }
  586. break;
  587. default:
  588. ++$i;
  589. break;
  590. }
  591. }
  592. if (isset($date)) {
  593. $date = str_replace(' ', ' ', $date);
  594. $tmpdate = explode(' ', trim($date));
  595. } else {
  596. $tmpdate = $date = array('', '', '', '', '', '');
  597. }
  598. if ($uid_support) {
  599. $msgi ="$unique_id";
  600. $messages[$msgi]['ID'] = $unique_id;
  601. } else {
  602. $msgi = "$id";
  603. $messages[$msgi]['ID'] = $id;
  604. }
  605. $messages[$msgi]['TIME_STAMP'] = getTimeStamp($tmpdate);
  606. $messages[$msgi]['DATE_STRING'] = getDateString($messages[$msgi]['TIME_STAMP']);
  607. $messages[$msgi]['FROM'] = $from; //parseAddress($from);
  608. $messages[$msgi]['SUBJECT'] = $subject;
  609. // if (handleAsSent($mailbox)) {
  610. $messages[$msgi]['TO'] = $to; //parseAddress($to);
  611. // }
  612. $messages[$msgi]['PRIORITY'] = $priority;
  613. $messages[$msgi]['CC'] = $cc; //parseAddress($cc);
  614. $messages[$msgi]['SIZE'] = $size;
  615. $messages[$msgi]['TYPE0'] = $type[0];
  616. $messages[$msgi]['FLAG_DELETED'] = $flag_deleted;
  617. $messages[$msgi]['FLAG_ANSWERED'] = $flag_answered;
  618. $messages[$msgi]['FLAG_SEEN'] = $flag_seen;
  619. $messages[$msgi]['FLAG_FLAGGED'] = $flag_flagged;
  620. /* non server sort stuff */
  621. if (!$allow_server_sort) {
  622. $from = parseAddress($from);
  623. if ($from[0][1]) {
  624. $from = decodeHeader($from[0][1]);
  625. } else {
  626. $from = $from[0][0];
  627. }
  628. $messages[$msgi]['FROM-SORT'] = $from;
  629. $subject_sort = strtolower(decodeHeader($subject));
  630. if (preg_match("/^(vedr|sv|re|aw):\s*(.*)$/si", $subject_sort, $matches)){
  631. $messages[$msgi]['SUBJECT-SORT'] = $matches[2];
  632. } else {
  633. $messages[$msgi]['SUBJECT-SORT'] = $subject_sort;
  634. }
  635. }
  636. ++$msgi;
  637. }
  638. array_reverse($messages);
  639. $new_messages = array();
  640. foreach ($messages as $i =>$message) {
  641. $new_messages[] = $message;
  642. }
  643. return $new_messages;
  644. }
  645. // obsolete?
  646. function sqimap_get_headerfield($imap_stream, $field) {
  647. global $uid_support;
  648. $sid = sqimap_session_id(false);
  649. $results = array();
  650. $read_list = array();
  651. $query = "FETCH 1:* (UID BODY.PEEK[HEADER.FIELDS ($field)])";
  652. $readin_list = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, $uid_support);
  653. $i = 0;
  654. foreach ($readin_list as $r) {
  655. $r = implode('',$r);
  656. /* first we unfold the header */
  657. $r = str_replace(array("\r\n\t","\r\n\s"),array('',''),$r);
  658. /*
  659. * now we can make a new header array with each element representing
  660. * a headerline
  661. */
  662. $r = explode("\r\n" , $r);
  663. if (!$uid_support) {
  664. if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH/iAU",$r[0], $regs)) {
  665. set_up_language($squirrelmail_language);
  666. echo '<br><b><font color=$color[2]>' .
  667. _("ERROR : Could not complete request.") .
  668. '</b><br>' .
  669. _("Unknown response from IMAP server: ") . ' 1.' .
  670. $r[0] . "</font><br>\n";
  671. } else {
  672. $id = $regs[1];
  673. }
  674. } else {
  675. if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH.*UID\s+([0-9]+)\s+/iAU",$r[0], $regs)) {
  676. set_up_language($squirrelmail_language);
  677. echo '<br><b><font color=$color[2]>' .
  678. _("ERROR : Could not complete request.") .
  679. '</b><br>' .
  680. _("Unknown response from IMAP server: ") . ' 1.' .
  681. $r[0] . "</font><br>\n";
  682. } else {
  683. $id = $regs[2];
  684. }
  685. }
  686. $field = $r[1];
  687. $field = substr($field,strlen($field)+2);
  688. $result[] = array($id,$field);
  689. }
  690. return $result;
  691. }
  692. /*
  693. * Returns a message array with all the information about a message.
  694. * See the documentation folder for more information about this array.
  695. */
  696. function sqimap_get_message ($imap_stream, $id, $mailbox) {
  697. global $uid_support;
  698. $flags = array();
  699. $read = sqimap_run_command ($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, $uid_support);
  700. if ($read) {
  701. if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) {
  702. if (trim($regs[1])) {
  703. $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY');
  704. }
  705. }
  706. } else {
  707. /* the message was not found, maybe the mailbox was modified? */
  708. global $sort, $startMessage, $color;
  709. $errmessage = _("The server couldn't find the message you requested.") .
  710. '<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).");
  711. /* this will include a link back to the message list */
  712. error_message($errmessage, $mailbox, $sort, $startMessage, $color);
  713. exit;
  714. }
  715. $bodystructure = implode('',$read);
  716. $msg = mime_structure($bodystructure,$flags);
  717. $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support);
  718. $rfc822_header = new Rfc822Header();
  719. $rfc822_header->parseHeader($read);
  720. $msg->rfc822_header = $rfc822_header;
  721. return $msg;
  722. }
  723. /* Wrapper function that reformats the header information. */
  724. // obsolete?
  725. function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
  726. global $uid_support;
  727. $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support);
  728. $header = sqimap_get_header($imap_stream, $read);
  729. $header->id = $id;
  730. $header->mailbox = $mailbox;
  731. return $header;
  732. }
  733. /* Wrapper function that reformats the entity header information. */
  734. // obsolete?
  735. function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
  736. global $uid_support;
  737. $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent.HEADER]", true, $response, $message, $uid_support);
  738. $header = sqimap_get_header($imap_stream, $read);
  739. $header->id = $id;
  740. $header->mailbox = $mailbox;
  741. return $header;
  742. }
  743. /* function to get the mime headers */
  744. // obsolete?
  745. function sqimap_get_mime_ent_header ($imap_stream, $id, $mailbox, $ent) {
  746. global $uid_support;
  747. $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.MIME]", true, $response, $message, $uid_support);
  748. $header = sqimap_get_header($imap_stream, $read);
  749. $header->id = $id;
  750. $header->mailbox = $mailbox;
  751. return $header;
  752. }
  753. ?>