imap_messages.php 30 KB

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