imap_messages.php 26 KB

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