imap_messages.php 29 KB

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