imap_messages.php 29 KB

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