imap_messages.php 27 KB

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