forgot to move global $message
[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, $imap_server_type;
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:(.*)\$/AU",
386 "/^From:(.*)\$/AU",
387 "/^X-Priority:(.*)\$/AU",
388 "/^Cc:(.*)\$/AU",
389 "/^Date:(.*)\$/AU",
390 "/^Subject:(.*)\$/AU",
391 "/^Content-Type:(.*)\$/AU"
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+\"(.+)\".+/iUA",$read_part, $reg)) {
423 $tmpdate = trim($reg[1]);
424 $tmpdate = str_replace(' ',' ',$tmpdate);
425 $tmpdate = explode(' ',$tmpdate);
426 $date = str_replace('-',' ',$tmpdate[0]) . " " .
427 $tmpdate[1] . " " .
428 $tmpdate[2];
429 }
430 }
431 if (preg_match ("/^.+RFC822.SIZE\s+(\d+).+/iA",$read_part, $reg)) {
432 $size = $reg[1];
433 }
434 if (preg_match("/^.+FLAGS\s+\((.*)\).+/iUA", $read_part, $regs)) {
435 $flags = explode(' ',trim($regs[1]));
436 foreach ($flags as $flag) {
437 $flag = strtolower($flag);
438 if ($flag == '\\seen') {
439 $flag_seen = true;
440 } else if ($flag == '\\answered') {
441 $flag_answered = true;
442 } else if ($flag == '\\deleted') {
443 $flag_deleted = true;
444 } else if ($flag == '\\flagged') {
445 $flag_flagged = true;
446 }
447 }
448 }
449 } else {
450 $firstchar = $read_part{0};
451 if ($firstchar == 'T') {
452 $regpattern = $patterns[0];
453 $id = 1;
454 } else if ($firstchar == 'F') {
455 $regpattern = $patterns[1];
456 $id = 2;
457 } else if ($firstchar == 'X') {
458 $regpattern = $patterns[2];
459 $id = 3;
460 } else if ($firstchar == 'C') {
461 if (strtolower($read_part{1}) == 'c') {
462 $regpattern = $patterns[3];
463 $id = 4;
464 } else if (strtolower($read_part{1}) == 'o') {
465 $regpattern = $patterns[6];
466 $id = 7;
467 }
468 } else if ($firstchar == 'D' && !$internaldate ) {
469 $regpattern = $patterns[4];
470 $id = 5;
471 } else if ($firstchar == 'S') {
472 $regpattern = $patterns[5];
473 $id = 6;
474 } else $regpattern = '';
475
476 if ($regpattern) {
477 if (preg_match ($regpattern, $read_part, $regs)) {
478 switch ($id) {
479 case 1:
480 $to = $regs[1];
481 break;
482 case 2:
483 $from = $regs[1];
484 break;
485 case 3:
486 $priority = $regs[1];
487 break;
488 case 4:
489 $cc = $regs[1];
490 break;
491 case 5:
492 $date = $regs[1];
493 break;
494 case 6:
495 $subject = htmlspecialchars(trim($regs[1]));
496 if ($subject == "") {
497 $subject = _("(no subject)");
498 }
499 break;
500 case 7:
501 $type = strtolower(trim($regs[1]));
502 if ($pos = strpos($type, ";")) {
503 $type = substr($type, 0, $pos);
504 }
505 $type = explode("/", $type);
506 if (!isset($type[1])) {
507 $type[1] = '';
508 }
509 break;
510 default:
511 break;
512 }
513 }
514 }
515 }
516
517 }
518 $header = new small_header;
519 if ($issent) {
520 $header->from = (trim($to) != '' ? $to : '(' ._("No To Address") . ')');
521 } else {
522 $header->from = $from;
523 }
524
525 $header->date = $date;
526 $header->subject = $subject;
527 $header->to = $to;
528 $header->priority = $priority;
529 $header->message_id = $messageid;
530 $header->cc = $cc;
531 $header->size = $size;
532 $header->type0 = $type[0];
533 $header->type1 = $type[1];
534 $header->flag_seen = $flag_seen;
535 $header->flag_answered = $flag_answered;
536 $header->flag_deleted = $flag_deleted;
537 $header->flag_flagged = $flag_flagged;
538 $header->inrepto = $inrepto;
539 $result[] = $header;
540 }
541 // echo 'processtime (us): ' . elapsedtime($start) .'<BR>';
542 return $result;
543 }
544
545
546
547
548 /* Returns the flags for the specified messages */
549 function sqimap_get_flags ($imap_stream, $i) {
550 $read = sqimap_run_command ($imap_stream, "FETCH $i:$i FLAGS", true, $response, $message);
551 if (ereg('FLAGS(.*)', $read[0], $regs)) {
552 return explode(' ', trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
553 }
554 return array('None');
555 }
556
557 function sqimap_get_flags_list ($imap_stream, $msg_list) {
558 $msgs_str = sqimap_message_list_squisher($msg_list);
559 for ($i = 0; $i < sizeof($msg_list); $i++) {
560 $id2index[$msg_list[$i]] = $i;
561 }
562 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
563 $result_flags = array();
564
565 for ($i = 0; $i < sizeof($result_list); $i++) {
566 if (eregi('^\* ([0-9]+).*FETCH.*FLAGS(.*)', $result_list[$i][0], $regs)
567 && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
568 $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
569 } else {
570 set_up_language($squirrelmail_language);
571 echo "<br><b><font color=$color[2]>\n" .
572 _("ERROR : Could not complete request.") .
573 "</b><br>\n" .
574 _("Unknown response from IMAP server: ") .
575 $result_list[$i][0] . "</font><br>\n";
576 exit;
577 }
578 }
579 arsort($result_flags);
580 return $result_flags;
581 }
582
583 /*
584 * Returns a message array with all the information about a message.
585 * See the documentation folder for more information about this array.
586 */
587 function sqimap_get_message ($imap_stream, $id, $mailbox) {
588 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
589 return sqimap_get_message_body($imap_stream, $header);
590 }
591
592 /* Wrapper function that reformats the header information. */
593 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
594 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
595 $header = sqimap_get_header($imap_stream, $read);
596 $header->id = $id;
597 $header->mailbox = $mailbox;
598 return $header;
599 }
600
601 /* Wrapper function that reformats the entity header information. */
602 function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
603 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.HEADER]", true, $response, $message);
604 $header = sqimap_get_header($imap_stream, $read);
605 $header->id = $id;
606 $header->mailbox = $mailbox;
607 return $header;
608 }
609
610
611 /* Wrapper function that returns entity headers for use by decodeMime */
612 /*
613 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
614 $header = sqimap_get_header($imap_stream, $read);
615 $type0 = $header["TYPE0"];
616 $type1 = $header["TYPE1"];
617 $bound = $header["BOUNDARY"];
618 $encoding = $header["ENCODING"];
619 $charset = $header["CHARSET"];
620 $filename = $header["FILENAME"];
621 }
622
623 /* function to get the mime headers */
624 function sqimap_get_mime_ent_header ($imap_stream, $id, $mailbox, $ent) {
625 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.MIME]", true, $response, $message);
626 $header = sqimap_get_header($imap_stream, $read);
627 $header->id = $id;
628 $header->mailbox = $mailbox;
629 return $header;
630 }
631
632
633 /* Queries the IMAP server and gets all header information. */
634 function sqimap_get_header ($imap_stream, $read) {
635 global $where, $what;
636
637 $hdr = new msg_header();
638 $i = 0;
639
640 /* Set up some defaults */
641 $hdr->type0 = "text";
642 $hdr->type1 = "plain";
643 $hdr->charset = "us-ascii";
644
645 $read_fold = array();
646
647 while ($i < count($read)) {
648 /* unfold multi-line headers */
649 /* remember line for to, cc and bcc */
650 $read_fold[] = $read[$i];
651 $folded = false;
652 while (($i + 1 < count($read)) && (strspn($read[$i + 1], "\t ") > 0) ) {
653 if ($read[$i+1] != '') $read_fold[] = $read[$i+1];
654 $read[$i + 1] = substr($read[$i], 0, -2) . ' ' . ltrim($read[$i+1]);
655 array_splice($read, $i, 1);
656 $folded = true;
657 }
658 if (!$folded) {
659 $read_fold = array();
660 }
661
662 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
663 $hdr->mime = true;
664 $i++;
665 }
666 /* ENCODING TYPE */
667 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
668 $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
669 $i++;
670 }
671 /* CONTENT-TYPE */
672 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
673 $cont = strtolower(trim(substr($read[$i], 13)));
674 if (strpos($cont, ";")) {
675 $cont = substr($cont, 0, strpos($cont, ";"));
676 }
677
678 if (strpos($cont, "/")) {
679 $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
680 $hdr->type1 = substr($cont, strpos($cont, "/")+1);
681 } else {
682 $hdr->type0 = $cont;
683 }
684
685 $line = $read[$i];
686 $i++;
687 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
688 str_replace("\n", "", $line);
689 str_replace("\n", "", $read[$i]);
690 $line = "$line $read[$i]";
691 $i++;
692 }
693
694 /* Detect the boundary of a multipart message */
695 if (eregi('boundary="([^"]+)"', $line, $regs)) {
696 $hdr->boundary = $regs[1];
697 }
698
699 /* Detect the charset */
700 if (strpos(strtolower(trim($line)), "charset=")) {
701 $pos = strpos($line, "charset=") + 8;
702 $charset = trim($line);
703 if (strpos($line, ";", $pos) > 0) {
704 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
705 } else {
706 $charset = substr($charset, $pos);
707 }
708 $charset = str_replace("\"", "", $charset);
709 $hdr->charset = $charset;
710 } else {
711 $hdr->charset = "us-ascii";
712 }
713 /* Detect type in case of multipart/related */
714 if (strpos(strtolower(trim($line)), "type=")) {
715 $pos = strpos($line, "type=") + 6;
716 $type = trim($line);
717 if (strpos($line, ";", $pos) > 0) {
718 $type = substr($type, $pos, strpos($line, ";", $pos)-$pos);
719 } else {
720 $type = substr($type, $pos);
721 }
722 $hdr->type = $type;
723 }
724 }
725 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
726 /* Add better content-disposition support */
727 $line = $read[$i];
728 $i++;
729 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
730 str_replace("\n", "", $line);
731 str_replace("\n", "", $read[$i]);
732 $line = "$line $read[$i]";
733 $i++;
734 }
735
736 /* Detects filename if any */
737 if (strpos(strtolower(trim($line)), "filename=")) {
738 $pos = strpos($line, "filename=") + 9;
739 $name = trim($line);
740 if (strpos($line, " ", $pos) > 0) {
741 $name = substr($name, $pos, strpos($line, " ", $pos));
742 } else {
743 $name = substr($name, $pos);
744 }
745 $name = str_replace("\"", "", $name);
746 $hdr->filename = $name;
747 }
748 }
749 /* REPLY-TO */
750 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
751 $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
752 $i++;
753 }
754 /* FROM */
755 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
756 $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
757 if (! isset($hdr->replyto) || $hdr->replyto == "") {
758 $hdr->replyto = $hdr->from;
759 }
760 $i++;
761 }
762 /* DATE */
763 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
764 $d = substr($read[$i], 5);
765 $d = trim($d);
766 $d = strtr($d, array(' ' => ' '));
767 $d = explode(' ', $d);
768 $hdr->date = getTimeStamp($d);
769 $i++;
770 }
771 /* SUBJECT */
772 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
773 $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
774 if (strlen(Chop($hdr->subject)) == 0) {
775 $hdr->subject = _("(no subject)");
776 }
777 /*
778 if ($where == 'SUBJECT') {
779 $hdr->subject = $what;
780 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
781 }
782 */
783 $i++;
784 }
785 /* CC */
786 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
787 $pos = 0;
788 if (isset($read_fold[0])) {
789 $hdr->cc[$pos] = trim(substr($read_fold[0], 4));
790 $pos++;
791 while ($pos < count($read_fold)) {
792 $hdr->cc[$pos] = trim($read_fold[$pos]);
793 $pos++;
794 }
795 } else {
796 $hdr->cc[$pos] = trim(substr($read[$i], 4));
797 }
798 $i++;
799 }
800 /* BCC */
801 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
802 $pos = 0;
803 if (isset($read_fold[0])) {
804 $hdr->bcc[$pos] = trim(substr($read_fold[0], 5));
805 $pos++;
806 while ($pos < count($read_fold)) {
807 $hdr->bcc[$pos] = trim($read_fold[$pos]);
808 $pos++;
809 }
810 } else {
811 $hdr->bcc[$pos] = trim(substr($read[$i], 5));
812 }
813 $i++;
814 }
815 /* TO */
816 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
817 $pos = 0;
818 if (isset($read_fold[0])) {
819 $hdr->to[$pos] = trim(substr($read_fold[0], 4));
820 $pos++;
821 while ($pos < count($read_fold)) {
822 $hdr->to[$pos] = trim($read_fold[$pos]);
823 $pos++;
824 }
825 } else {
826 $hdr->to[$pos] = trim(substr($read[$i], 4));
827 }
828 $i++;
829
830 }
831 /* MESSAGE ID */
832 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
833 $hdr->message_id = trim(substr($read[$i], 11));
834 $i++;
835 }
836 /* ERROR CORRECTION */
837 else if (substr($read[$i], 0, 1) == ")") {
838 if (strlen(trim($hdr->subject)) == 0) {
839 $hdr->subject = _("(no subject)");
840 }
841 if (strlen(trim($hdr->from)) == 0) {
842 $hdr->from = _("(unknown sender)");
843 }
844 if (strlen(trim($hdr->date)) == 0) {
845 $hdr->date = time();
846 }
847 $i++;
848 }
849 /* X-PRIORITY */
850 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
851 $hdr->priority = trim(substr($read[$i], 11));
852 $i++;
853 }
854 else {
855 $i++;
856 }
857 $read_fold=array();
858 }
859 return $hdr;
860 }
861
862 /* Returns the body of a message. */
863 function sqimap_get_message_body ($imap_stream, &$header) {
864 $id = $header->id;
865 return decodeMime($imap_stream, $header);
866 }
867
868 ?>