imap_messages.php 27 KB

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