imap_messages.php 29 KB

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