imap_messages.php 27 KB

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