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