Speed related changes to sqimap_get_small_header_list. However it's tested
[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[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 if ($sort == 6) {
108 $qty = sqimap_get_num_messages ($imap_stream, $mailbox);
109 $server_sort_array = range(1, $qty);
110 session_register('server_sort_array');
111 return $server_sort_array;
112 }
113 $sid = sqimap_session_id();
114 $sort_on = array();
115 $reverse = 0;
116 $server_sort_array = array();
117 $sort_test = array();
118 $sort_query = '';
119 $sort_on = array (0=> 'DATE',
120 1=> 'DATE',
121 2=> 'FROM',
122 3=> 'FROM',
123 4=> 'SUBJECT',
124 5=> 'SUBJECT');
125 if ($internal_date_sort == true) {
126 $sort_on[0] = 'ARRIVAL';
127 $sort_on[1] = 'ARRIVAL';
128 }
129 if ($sent_folder == $mailbox) {
130 $sort_on[2] = 'TO';
131 $sort_on[3] = 'TO';
132 }
133 if (!empty($sort_on[$sort])) {
134 $sort_query = "$sid SORT ($sort_on[$sort]) ".strtoupper($default_charset)." ALL\r\n";
135 fputs($imap_stream, $sort_query);
136 $sort_test = sqimap_read_data($imap_stream, $sid, false, $response, $message);
137 }
138 if (isset($sort_test[0])) {
139 if (preg_match("/^\* SORT (.+)$/", $sort_test[0], $regs)) {
140 $server_sort_array = preg_split("/ /", trim($regs[1]));
141 }
142 }
143 if ($sort == 0 || $sort == 2 || $sort == 4) {
144 $server_sort_array = array_reverse($server_sort_array);
145 }
146 if (!preg_match("/OK/", $response)) {
147 $server_sort_array = 'no';
148 }
149 session_register('server_sort_array');
150 return $server_sort_array;
151 }
152
153 /* returns an indent array for printMessageinfo()
154 this represents the amount of indent needed (value)
155 for this message number (key)
156 */
157
158 function get_parent_level ($imap_stream) {
159 global $sort_by_ref, $default_charset, $thread_new;
160 $parent = "";
161 $child = "";
162 $cutoff = 0;
163
164 /* loop through the threads and take unwanted characters out
165 of the thread string then chop it up
166 */
167 for ($i=0;$i<count($thread_new);$i++) {
168 $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]);
169 $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]);
170 $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY);
171 }
172 $indent_array = array();
173 if (!$thread_new) {
174 $thread_new = array();
175 }
176 /* looping through the parts of one message thread */
177
178 for ($i=0;$i<count($thread_new);$i++) {
179 /* first grab the parent, it does not indent */
180
181 if (isset($thread_new[$i][0])) {
182 if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) {
183 $parent = $regs[1];
184 }
185 }
186 $indent_array[$parent] = 0;
187
188 /* now the children, checking each thread portion for
189 ),(, and space, adjusting the level and space values
190 to get the indent level
191 */
192 $level = 0;
193 $spaces = array();
194 $spaces_total = 0;
195 $indent = 0;
196 $fake = FALSE;
197 for ($k=1;$k<(count($thread_new[$i]))-1;$k++) {
198 $chars = count_chars($thread_new[$i][$k], 1);
199 if (isset($chars['40'])) { /* testing for ( */
200 $level = $level + $chars['40'];
201 }
202 if (isset($chars['41'])) { /* testing for ) */
203 $level = $level - $chars['41'];
204 $spaces[$level] = 0;
205 /* if we were faking lets stop, this portion
206 of the thread is over
207 */
208 if ($level == $cutoff) {
209 $fake = FALSE;
210 }
211 }
212 if (isset($chars['32'])) { /* testing for space */
213 if (!isset($spaces[$level])) {
214 $spaces[$level] = 0;
215 }
216 $spaces[$level] = $spaces[$level] + $chars['32'];
217 }
218 for ($x=0;$x<=$level;$x++) {
219 if (isset($spaces[$x])) {
220 $spaces_total = $spaces_total + $spaces[$x];
221 }
222 }
223 $indent = $level + $spaces_total;
224 /* must have run into a message that broke the thread
225 so we are adjusting for that portion
226 */
227 if ($fake == TRUE) {
228 $indent = $indent +1;
229 }
230 if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
231 $child = $regs[1];
232 }
233 /* the thread must be broken if $indent == 0
234 so indent the message once and start faking it
235 */
236 if ($indent == 0) {
237 $indent = 1;
238 $fake = TRUE;
239 $cutoff = $level;
240 }
241 /* dont need abs but if indent was negative
242 errors would occur
243 */
244 $indent_array[$child] = abs($indent);
245 $spaces_total = 0;
246 }
247 }
248 return $indent_array;
249 }
250
251
252 /* returns an array with each element as a string
253 representing one message thread as returned by
254 the IMAP server
255 */
256
257 function get_thread_sort ($imap_stream) {
258 global $thread_new, $sort_by_ref, $default_charset, $server_sort_array;
259 if (session_is_registered('thread_new')) {
260 session_unregister('thread_new');
261 }
262 if (session_is_registered('server_sort_array')) {
263 session_unregister('server_srot_array');
264 }
265 $sid = sqimap_session_id();
266 $thread_temp = array ();
267 if ($sort_by_ref == 1) {
268 $sort_type = 'REFERENCES';
269 }
270 else {
271 $sort_type = 'ORDEREDSUBJECT';
272 }
273 $thread_query = "$sid THREAD $sort_type ".strtoupper($default_charset)." ALL\r\n";
274 fputs($imap_stream, $thread_query);
275 $thread_test = sqimap_read_data($imap_stream, $sid, false, $response, $message);
276 if (isset($thread_test[0])) {
277 if (preg_match("/^\* THREAD (.+)$/", $thread_test[0], $regs)) {
278 $thread_list = trim($regs[1]);
279 }
280 }
281 else {
282 $thread_list = "";
283 }
284 if (!preg_match("/OK/", $response)) {
285 $server_sort_array = 'no';
286 return $server_sort_array;
287 }
288 if (isset($thread_list)) {
289 $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
290 }
291 $char_count = count($thread_temp);
292 $counter = 0;
293 $thread_new = array();
294 $k = 0;
295 $thread_new[0] = "";
296 for ($i=0;$i<$char_count;$i++) {
297 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
298 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
299 }
300 elseif ($thread_temp[$i] == '(') {
301 $thread_new[$k] .= $thread_temp[$i];
302 $counter++;
303 }
304 elseif ($thread_temp[$i] == ')') {
305 if ($counter > 1) {
306 $thread_new[$k] .= $thread_temp[$i];
307 $counter = $counter - 1;
308 }
309 else {
310 $thread_new[$k] .= $thread_temp[$i];
311 $k++;
312 $thread_new[$k] = "";
313 $counter = $counter - 1;
314 }
315 }
316 }
317 session_register('thread_new');
318 $thread_new = array_reverse($thread_new);
319 $thread_list = implode(" ", $thread_new);
320 $thread_list = str_replace("(", " ", $thread_list);
321 $thread_list = str_replace(")", " ", $thread_list);
322 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
323 $server_sort_array = $thread_list;
324 session_register('server_sort_array');
325 return $thread_list;
326 }
327
328 function elapsedTime($start) {
329 $stop = gettimeofday();
330 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec'];
331 return $timepassed;
332 }
333
334
335 function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
336 global $squirrelmail_language, $color, $data_dir, $username;
337 $start = gettimeofday();
338 /* Get the small headers for each message in $msg_list */
339 $sid = sqimap_session_id();
340 $maxmsg = sizeof($msg_list);
341 $msgs_str = sqimap_message_list_squisher($msg_list);
342 $results = array();
343 $read_list = array();
344 /*
345 * We need to return the data in the same order as the caller supplied
346 * in $msg_list, but IMAP servers are free to return responses in
347 * whatever order they wish... So we need to re-sort manually
348 */
349 for ($i = 0; $i < sizeof($msg_list); $i++) {
350 $id2index[$msg_list[$i]] = $i;
351 }
352
353 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
354 if ($internaldate) {
355 $query = "$sid FETCH $msgs_str (FLAGS RFC822.SIZE INTERNALDATE BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject X-Priority Content-Type)])\r\n";
356 } else {
357 $query = "$sid FETCH $msgs_str (FLAGS RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject X-Priority Content-Type)])\r\n";
358 }
359 fputs ($imap_stream, $query);
360 $readin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
361
362 foreach ($readin_list as $r) {
363 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH/iAU",$r[0], $regs)) {
364 set_up_language($squirrelmail_language);
365 echo '<br><b><font color=$color[2]>' .
366 _("ERROR : Could not complete request.") .
367 '</b><br>' .
368 _("Unknown response from IMAP server: ") . ' 1.' .
369 $r[0] . "</font><br>\n";
370
371 } else if (! isset($id2index[$regs[1]]) || !count($id2index[$regs[1]])) {
372 set_up_language($squirrelmail_language);
373 echo '<br><b><font color=$color[2]>' .
374 _("ERROR : Could not complete request.") .
375 '</b><br>' .
376 _("Unknown message number in reply from server: ") .
377 $regs[1] . "</font><br>\n";
378 } else {
379 $read_list[$id2index[$regs[1]]] = $r;
380 }
381 }
382 arsort($read_list);
383
384 $patterns = array (
385 "/^To:(.*)/iA",
386 "/^From:(.*)/iA",
387 "/^X-Priority:(.*)/iA",
388 "/^Cc:(.*)/iA",
389 "/^Date:(.*)/iA",
390 "/^Subject:(.*)/iA",
391 "/^Content-Type:(.*)/iA"
392 );
393 $regpattern = '';
394
395 for ($msgi = 0; $msgi < $maxmsg; $msgi++) {
396 $subject = _("(no subject)");
397 $from = _("Unknown Sender");
398 $priority = 0;
399 $messageid = "<>";
400 $cc = "";
401 $to = "";
402 $date = "";
403 $type[0] = "";
404 $type[1] = "";
405 $inrepto = "";
406 $flag_seen = false;
407 $flag_answered = false;
408 $flag_deleted = false;
409 $flag_flagged = false;
410 $read = $read_list[$msgi];
411 $prevline = false;
412
413 foreach ($read as $read_part) {
414 //unfold multi-line headers
415 while ($prevline && strspn($read_part, "\t ") > 0) {
416 $read_part = substr($prevline, 0, -2) . ' ' . ltrim($read_part);
417 }
418 $prev_line = $read_part;
419
420 if ($read_part{0} == '*') {
421 if ($internaldate) {
422 if (preg_match ("/^.+INTERNALDATE\s\"(.+)\"\s.+/iUA",$read_part, $reg)) {
423 $date = $reg[1];
424 }
425 }
426 if (preg_match ("/^.+RFC822.SIZE\s+(\d+).+/iA",$read_part, $reg)) {
427 $size = $reg[1];
428 }
429 if (preg_match("/^.+FLAGS\s+\((.*)\).+/iUA", $read_part, $regs)) {
430 $flags = explode(' ',trim($regs[1]));
431 foreach ($flags as $flag) {
432 $flag = strtolower($flag);
433 if ($flag == '\\seen') {
434 $flag_seen = true;
435 } else if ($flag == '\\answered') {
436 $flag_answered = true;
437 } else if ($flag == '\\deleted') {
438 $flag_deleted = true;
439 } else if ($flag == '\\flagged') {
440 $flag_flagged = true;
441 }
442 }
443 }
444 } else {
445
446 $firstchar = $read_part{0};
447 if ($firstchar == 'T') {
448 $regpattern = $patterns[0];
449 $id = 1;
450 } else if ($firstchar == 'F') {
451 $regpattern = $patterns[1];
452 $id = 2;
453 } else if ($firstchar == 'X') {
454 $regpattern = $patterns[2];
455 $id = 3;
456 } else if ($firstchar == 'C') {
457 if (strtolower($read_part{1}) == 'c') {
458 $regpattern = $patterns[3];
459 $id = 4;
460 } else if (strtolower($read_part{1}) == 'o') {
461 $regpattern = $patterns[6];
462 $id = 7;
463 }
464 } else if ($firstchar == 'D') {
465 $regpattern = $patterns[4];
466 $id = 5;
467 } else if ($firstchar == 'S') {
468 $regpattern = $patterns[5];
469 $id = 6;
470 } else $regpattern = '';
471
472 if ($regpattern) {
473 if (preg_match ($regpattern, $read_part, $regs)) {
474 switch ($id) {
475 case 1:
476 $to = $regs[1];
477 break;
478 case 2:
479 $from = $regs[1];
480 break;
481 case 3:
482 $priority = $regs[1];
483 break;
484 case 4:
485 $cc = $regs[1];
486 break;
487 case 5:
488 if (!$internaldate) {
489 $date = $regs[1];
490 }
491 break;
492 case 6:
493 $subject = htmlspecialchars(trim($regs[1]));
494 if ($subject == "") {
495 $subject = _("(no subject)");
496 }
497 break;
498 case 7:
499 $type = strtolower(trim($regs[1]));
500 if ($pos = strpos($type, ";")) {
501 $type = substr($type, 0, $pos);
502 }
503 $type = explode("/", $type);
504 if (!isset($type[1])) {
505 $type[1] = '';
506 }
507 break;
508 default:
509 break;
510 }
511 }
512 }
513 }
514
515 }
516 $header = new small_header;
517 if ($issent) {
518 $header->from = (trim($to) != '' ? $to : '(' ._("No To Address") . ')');
519 } else {
520 $header->from = $from;
521 }
522
523 $header->date = $date;
524 $header->subject = $subject;
525 $header->to = $to;
526 $header->priority = $priority;
527 $header->message_id = $messageid;
528 $header->cc = $cc;
529 $header->size = $size;
530 $header->type0 = $type[0];
531 $header->type1 = $type[1];
532 $header->flag_seen = $flag_seen;
533 $header->flag_answered = $flag_answered;
534 $header->flag_deleted = $flag_deleted;
535 $header->flag_flagged = $flag_flagged;
536 $header->inrepto = $inrepto;
537 $result[] = $header;
538 }
539 echo 'processtime (us): ' . elapsedtime($start) .'<BR>';
540 return $result;
541 }
542
543
544
545
546 /* Returns the flags for the specified messages */
547 function sqimap_get_flags ($imap_stream, $i) {
548 $read = sqimap_run_command ($imap_stream, "FETCH $i:$i FLAGS", true, $response, $message);
549 if (ereg('FLAGS(.*)', $read[0], $regs)) {
550 return explode(' ', trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
551 }
552 return array('None');
553 }
554
555 function sqimap_get_flags_list ($imap_stream, $msg_list) {
556 $msgs_str = sqimap_message_list_squisher($msg_list);
557 for ($i = 0; $i < sizeof($msg_list); $i++) {
558 $id2index[$msg_list[$i]] = $i;
559 }
560 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
561 $result_flags = array();
562
563 for ($i = 0; $i < sizeof($result_list); $i++) {
564 if (eregi('^\* ([0-9]+).*FETCH.*FLAGS(.*)', $result_list[$i][0], $regs)
565 && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
566 $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
567 } else {
568 set_up_language($squirrelmail_language);
569 echo "<br><b><font color=$color[2]>\n" .
570 _("ERROR : Could not complete request.") .
571 "</b><br>\n" .
572 _("Unknown response from IMAP server: ") .
573 $result_list[$i][0] . "</font><br>\n";
574 exit;
575 }
576 }
577 arsort($result_flags);
578 return $result_flags;
579 }
580
581 /*
582 * Returns a message array with all the information about a message.
583 * See the documentation folder for more information about this array.
584 */
585 function sqimap_get_message ($imap_stream, $id, $mailbox) {
586 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
587 return sqimap_get_message_body($imap_stream, $header);
588 }
589
590 /* Wrapper function that reformats the header information. */
591 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
592 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
593 $header = sqimap_get_header($imap_stream, $read);
594 $header->id = $id;
595 $header->mailbox = $mailbox;
596 return $header;
597 }
598
599 /* Wrapper function that reformats the entity header information. */
600 function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
601 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.HEADER]", true, $response, $message);
602 $header = sqimap_get_header($imap_stream, $read);
603 $header->id = $id;
604 $header->mailbox = $mailbox;
605 return $header;
606 }
607
608
609 /* Wrapper function that returns entity headers for use by decodeMime */
610 /*
611 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
612 $header = sqimap_get_header($imap_stream, $read);
613 $type0 = $header["TYPE0"];
614 $type1 = $header["TYPE1"];
615 $bound = $header["BOUNDARY"];
616 $encoding = $header["ENCODING"];
617 $charset = $header["CHARSET"];
618 $filename = $header["FILENAME"];
619 }
620 */
621 /* Queries the IMAP server and gets all header information. */
622 function sqimap_get_header ($imap_stream, $read) {
623 global $where, $what;
624
625 $hdr = new msg_header();
626 $i = 0;
627
628 /* Set up some defaults */
629 $hdr->type0 = "text";
630 $hdr->type1 = "plain";
631 $hdr->charset = "us-ascii";
632
633 while ($i < count($read)) {
634 //unfold multi-line headers
635 while ($i + 1 < count($read) && strspn($read[$i + 1], "\t ") > 0) {
636 $read[$i + 1] = substr($read[$i], 0, -2) . ' ' . ltrim($read[$i + 1]);
637 array_splice($read, $i, 1);
638 }
639
640 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
641 $hdr->mime = true;
642 $i++;
643 }
644 /* ENCODING TYPE */
645 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
646 $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
647 $i++;
648 }
649 /* CONTENT-TYPE */
650 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
651 $cont = strtolower(trim(substr($read[$i], 13)));
652 if (strpos($cont, ";")) {
653 $cont = substr($cont, 0, strpos($cont, ";"));
654 }
655
656 if (strpos($cont, "/")) {
657 $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
658 $hdr->type1 = substr($cont, strpos($cont, "/")+1);
659 } else {
660 $hdr->type0 = $cont;
661 }
662
663 $line = $read[$i];
664 $i++;
665 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
666 str_replace("\n", "", $line);
667 str_replace("\n", "", $read[$i]);
668 $line = "$line $read[$i]";
669 $i++;
670 }
671
672 /* Detect the boundary of a multipart message */
673 if (eregi('boundary="([^"]+)"', $line, $regs)) {
674 $hdr->boundary = $regs[1];
675 }
676
677 /* Detect the charset */
678 if (strpos(strtolower(trim($line)), "charset=")) {
679 $pos = strpos($line, "charset=") + 8;
680 $charset = trim($line);
681 if (strpos($line, ";", $pos) > 0) {
682 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
683 } else {
684 $charset = substr($charset, $pos);
685 }
686 $charset = str_replace("\"", "", $charset);
687 $hdr->charset = $charset;
688 } else {
689 $hdr->charset = "us-ascii";
690 }
691 /* Detect type in case of multipart/related */
692 if (strpos(strtolower(trim($line)), "type=")) {
693 $pos = strpos($line, "type=") + 6;
694 $type = trim($line);
695 if (strpos($line, ";", $pos) > 0) {
696 $type = substr($type, $pos, strpos($line, ";", $pos)-$pos);
697 } else {
698 $type = substr($type, $pos);
699 }
700 $hdr->type = $type;
701 }
702 }
703 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
704 /* Add better content-disposition support */
705 $line = $read[$i];
706 $i++;
707 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
708 str_replace("\n", "", $line);
709 str_replace("\n", "", $read[$i]);
710 $line = "$line $read[$i]";
711 $i++;
712 }
713
714 /* Detects filename if any */
715 if (strpos(strtolower(trim($line)), "filename=")) {
716 $pos = strpos($line, "filename=") + 9;
717 $name = trim($line);
718 if (strpos($line, " ", $pos) > 0) {
719 $name = substr($name, $pos, strpos($line, " ", $pos));
720 } else {
721 $name = substr($name, $pos);
722 }
723 $name = str_replace("\"", "", $name);
724 $hdr->filename = $name;
725 }
726 }
727 /* REPLY-TO */
728 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
729 $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
730 $i++;
731 }
732 /* FROM */
733 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
734 $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
735 if (! isset($hdr->replyto) || $hdr->replyto == "") {
736 $hdr->replyto = $hdr->from;
737 }
738 $i++;
739 }
740 /* DATE */
741 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
742 $d = substr($read[$i], 5);
743 $d = trim($d);
744 $d = strtr($d, array(' ' => ' '));
745 $d = explode(' ', $d);
746 $hdr->date = getTimeStamp($d);
747 $i++;
748 }
749 /* SUBJECT */
750 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
751 $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
752 if (strlen(Chop($hdr->subject)) == 0) {
753 $hdr->subject = _("(no subject)");
754 }
755 /*
756 if ($where == 'SUBJECT') {
757 $hdr->subject = $what;
758 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
759 }
760 */
761 $i++;
762 }
763 /* CC */
764 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
765 $pos = 0;
766 $hdr->cc[$pos] = trim(substr($read[$i], 4));
767 $i++;
768 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
769 $pos++;
770 $hdr->cc[$pos] = trim($read[$i]);
771 $i++;
772 }
773 }
774 /* BCC */
775 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
776 $pos = 0;
777 $hdr->bcc[$pos] = trim(substr($read[$i], 5));
778 $i++;
779 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
780 $pos++;
781 $hdr->bcc[$pos] = trim($read[$i]);
782 $i++;
783 }
784 }
785 /* TO */
786 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
787 $pos = 0;
788 $hdr->to[$pos] = trim(substr($read[$i], 4));
789 $i++;
790 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
791 $pos++;
792 $hdr->to[$pos] = trim($read[$i]);
793 $i++;
794 }
795 }
796 /* MESSAGE ID */
797 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
798 $hdr->message_id = trim(substr($read[$i], 11));
799 $i++;
800 }
801 /* ERROR CORRECTION */
802 else if (substr($read[$i], 0, 1) == ")") {
803 if (strlen(trim($hdr->subject)) == 0) {
804 $hdr->subject = _("(no subject)");
805 }
806 if (strlen(trim($hdr->from)) == 0) {
807 $hdr->from = _("(unknown sender)");
808 }
809 if (strlen(trim($hdr->date)) == 0) {
810 $hdr->date = time();
811 }
812 $i++;
813 }
814 /* X-PRIORITY */
815 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
816 $hdr->priority = trim(substr($read[$i], 11));
817 $i++;
818 }
819 else {
820 $i++;
821 }
822 }
823 return $hdr;
824 }
825
826 /* Returns the body of a message. */
827 function sqimap_get_message_body ($imap_stream, &$header) {
828 $id = $header->id;
829 return decodeMime($imap_stream, $header);
830 }
831
832 ?>