imap_messages.php 27 KB

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