imap_messages.php 24 KB

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