imap_messages.php 26 KB

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