imap_messages.php 30 KB

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