imap_messages.php 29 KB

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