imap_messages.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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;
  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. $results = 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 = '';
  467. $to = '';
  468. $date = '';
  469. $type[0] = '';
  470. $type[1] = '';
  471. $inrepto = '';
  472. $flag_seen = false;
  473. $flag_answered = false;
  474. $flag_deleted = false;
  475. $flag_flagged = false;
  476. $read = $read_list[$msgi];
  477. $prevline = false;
  478. foreach ($read as $read_part) {
  479. //unfold multi-line headers
  480. if ($prevline && strpos($read_part, "\t ") === true) {
  481. $read_part = substr($prevline, 0, -2) . preg_replace('/(\t\s+)/',' ',$read_part);
  482. }
  483. $prevline = $read_part;
  484. if ($read_part{0} == '*') {
  485. if ($internaldate) {
  486. if (preg_match ("/^.+INTERNALDATE\s+\"(.+)\".+/iUA",$read_part, $reg)) {
  487. $tmpdate = trim($reg[1]);
  488. $tmpdate = str_replace(' ',' ',$tmpdate);
  489. $tmpdate = explode(' ',$tmpdate);
  490. $date = str_replace('-',' ',$tmpdate[0]) . " " .
  491. $tmpdate[1] . ' ' .
  492. $tmpdate[2];
  493. }
  494. }
  495. if (preg_match ("/^.+RFC822.SIZE\s+(\d+).+/iA",$read_part, $reg)) {
  496. $size = $reg[1];
  497. }
  498. if (preg_match("/^.+FLAGS\s+\((.*)\).+/iUA", $read_part, $regs)) {
  499. $flags = explode(' ',trim($regs[1]));
  500. foreach ($flags as $flag) {
  501. $flag = strtolower($flag);
  502. if ($flag == '\\seen') {
  503. $flag_seen = true;
  504. } else if ($flag == '\\answered') {
  505. $flag_answered = true;
  506. } else if ($flag == '\\deleted') {
  507. $flag_deleted = true;
  508. } else if ($flag == '\\flagged') {
  509. $flag_flagged = true;
  510. }
  511. }
  512. }
  513. if (preg_match ("/^.+UID\s+(\d+).+/iA",$read_part, $reg)) {
  514. $unique_id = $reg[1];
  515. }
  516. } else {
  517. $firstchar = strtoupper($read_part{0});
  518. if ($firstchar == 'T') {
  519. $regpattern = $patterns[0];
  520. $id = 1;
  521. } else if ($firstchar == 'F') {
  522. $regpattern = $patterns[1];
  523. $id = 2;
  524. } else if ($firstchar == 'X') {
  525. $regpattern = $patterns[2];
  526. $id = 3;
  527. } else if ($firstchar == 'C') {
  528. if (strtolower($read_part{1}) == 'c') {
  529. $regpattern = $patterns[3];
  530. $id = 4;
  531. } else if (strtolower($read_part{1}) == 'o') {
  532. $regpattern = $patterns[6];
  533. $id = 7;
  534. }
  535. } else if ($firstchar == 'D' && !$internaldate ) {
  536. $regpattern = $patterns[4];
  537. $id = 5;
  538. } else if ($firstchar == 'S') {
  539. $regpattern = $patterns[5];
  540. $id = 6;
  541. } else $regpattern = '';
  542. if ($regpattern) {
  543. if (preg_match ($regpattern, $read_part, $regs)) {
  544. switch ($id) {
  545. case 1:
  546. $to = $regs[1];
  547. break;
  548. case 2:
  549. $from = $regs[1];
  550. break;
  551. case 3:
  552. $priority = $regs[1];
  553. break;
  554. case 4:
  555. $cc = $regs[1];
  556. break;
  557. case 5:
  558. $date = $regs[1];
  559. break;
  560. case 6:
  561. $subject = htmlspecialchars(trim($regs[1]));
  562. if ($subject == "") {
  563. $subject = _("(no subject)");
  564. }
  565. break;
  566. case 7:
  567. $type = strtolower(trim($regs[1]));
  568. if ($pos = strpos($type, ";")) {
  569. $type = substr($type, 0, $pos);
  570. }
  571. $type = explode("/", $type);
  572. if (!isset($type[1])) {
  573. $type[1] = '';
  574. }
  575. break;
  576. default:
  577. break;
  578. }
  579. }
  580. }
  581. }
  582. }
  583. $header = new small_header;
  584. if ($uid_support) {
  585. $header->uid = $unique_id;
  586. } else {
  587. $header->uid = $msg_list[$msgi];
  588. }
  589. $header->date = $date;
  590. $header->subject = $subject;
  591. $header->to = $to;
  592. $header->from = $from;
  593. $header->priority = $priority;
  594. $header->message_id = $messageid;
  595. $header->cc = $cc;
  596. $header->size = $size;
  597. $header->type0 = $type[0];
  598. $header->type1 = $type[1];
  599. $header->flag_seen = $flag_seen;
  600. $header->flag_answered = $flag_answered;
  601. $header->flag_deleted = $flag_deleted;
  602. $header->flag_flagged = $flag_flagged;
  603. $header->inrepto = $inrepto;
  604. $result[] = $header;
  605. }
  606. return $result;
  607. }
  608. function sqimap_get_headerfield($imap_stream, $field) {
  609. $sid = sqimap_session_id(false);
  610. $results = array();
  611. $read_list = array();
  612. $query = "$sid FETCH 1:* (UID BODY.PEEK[HEADER.FIELDS ($field)])\r\n";
  613. fputs ($imap_stream, $query);
  614. $readin_list = sqimap_read_data_list($imap_stream, $sid, false, $response, $message);
  615. $i = 0;
  616. foreach ($readin_list as $r) {
  617. $r = implode('',$r);
  618. /* first we unfold the header */
  619. $r = str_replace(array("\r\n\t","\r\n\s"),array('',''),$r);
  620. /*
  621. * now we can make a new header array with each element representing
  622. * a headerline
  623. */
  624. $r = explode("\r\n" , $r);
  625. if (!$uid_support) {
  626. if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH/iAU",$r[0], $regs)) {
  627. set_up_language($squirrelmail_language);
  628. echo '<br><b><font color=$color[2]>' .
  629. _("ERROR : Could not complete request.") .
  630. '</b><br>' .
  631. _("Unknown response from IMAP server: ") . ' 1.' .
  632. $r[0] . "</font><br>\n";
  633. } else {
  634. $id = $regs[1];
  635. }
  636. } else {
  637. if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH.*UID\s+([0-9]+)\s+/iAU",$r[0], $regs)) {
  638. set_up_language($squirrelmail_language);
  639. echo '<br><b><font color=$color[2]>' .
  640. _("ERROR : Could not complete request.") .
  641. '</b><br>' .
  642. _("Unknown response from IMAP server: ") . ' 1.' .
  643. $r[0] . "</font><br>\n";
  644. } else {
  645. $id = $regs[2];
  646. }
  647. }
  648. $field = $r[1];
  649. $field = substr($field,strlen($field)+2);
  650. $result[] = array($id,$field);
  651. }
  652. return $result;
  653. }
  654. /*
  655. * Returns a message array with all the information about a message.
  656. * See the documentation folder for more information about this array.
  657. */
  658. function sqimap_get_message ($imap_stream, $id, $mailbox) {
  659. global $uid_support;
  660. $flags = array();
  661. $read = sqimap_run_command ($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, $uid_support);
  662. if ($read) {
  663. if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) {
  664. if (trim($regs[1])) {
  665. $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY');
  666. }
  667. }
  668. } else {
  669. echo "ERROR Yeah I know, not a very usefull errormessage (id = $id, mailbox = $mailbox sqimap_get_message)";
  670. exit;
  671. }
  672. $bodystructure = implode('',$read);
  673. $msg = mime_structure($bodystructure,$flags);
  674. $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support);
  675. $rfc822_header = new Rfc822Header();
  676. $rfc822_header->parseHeader($read);
  677. $msg->rfc822_header = $rfc822_header;
  678. return $msg;
  679. }
  680. /* Wrapper function that reformats the header information. */
  681. function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
  682. global $uid_support;
  683. $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support);
  684. $header = sqimap_get_header($imap_stream, $read);
  685. $header->id = $id;
  686. $header->mailbox = $mailbox;
  687. return $header;
  688. }
  689. /* Wrapper function that reformats the entity header information. */
  690. function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
  691. global $uid_support;
  692. $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent.HEADER]", true, $response, $message, $uid_support);
  693. $header = sqimap_get_header($imap_stream, $read);
  694. $header->id = $id;
  695. $header->mailbox = $mailbox;
  696. return $header;
  697. }
  698. /* Wrapper function that returns entity headers for use by decodeMime */
  699. /*
  700. function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
  701. $header = sqimap_get_header($imap_stream, $read);
  702. $type0 = $header["TYPE0"];
  703. $type1 = $header["TYPE1"];
  704. $bound = $header["BOUNDARY"];
  705. $encoding = $header["ENCODING"];
  706. $charset = $header["CHARSET"];
  707. $filename = $header["FILENAME"];
  708. }
  709. /* function to get the mime headers */
  710. function sqimap_get_mime_ent_header ($imap_stream, $id, $mailbox, $ent) {
  711. global $uid_support;
  712. $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.MIME]", true, $response, $message, $uid_support);
  713. $header = sqimap_get_header($imap_stream, $read);
  714. $header->id = $id;
  715. $header->mailbox = $mailbox;
  716. return $header;
  717. }
  718. /* Returns the body of a message. */
  719. function sqimap_get_message_body ($imap_stream, &$header) {
  720. // return decodeMime($imap_stream, $header->id);
  721. }
  722. ?>