imap_messages.php 25 KB

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