Moved get message_id and reply_id to smtp.php because it's insane to get
[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
329
330 function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
331 global $squirrelmail_language, $color, $data_dir, $username;
332
333 /* Get the small headers for each message in $msg_list */
334 $sid = sqimap_session_id();
335 $maxmsg = sizeof($msg_list);
336 $msgs_str = sqimap_message_list_squisher($msg_list);
337 $results = array();
338 $read_list = array();
339 $sizes_list = array();
340 /*
341 * We need to return the data in the same order as the caller supplied
342 * in $msg_list, but IMAP servers are free to return responses in
343 * whatever order they wish... So we need to re-sort manually
344 */
345 for ($i = 0; $i < sizeof($msg_list); $i++) {
346 $id2index[$msg_list[$i]] = $i;
347 }
348
349 $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";
350 fputs ($imap_stream, $query);
351 $readin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
352
353 foreach ($readin_list as $r) {
354 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
355 set_up_language($squirrelmail_language);
356 echo '<br><b><font color=$color[2]>' .
357 _("ERROR : Could not complete request.") .
358 '</b><br>' .
359 _("Unknown response from IMAP server: ") . ' 1.' .
360 $r[0] . "</font><br>\n";
361 } else if (! isset($id2index[$regs[1]]) || !count($id2index[$regs[1]])) {
362 set_up_language($squirrelmail_language);
363 echo '<br><b><font color=$color[2]>' .
364 _("ERROR : Could not complete request.") .
365 '</b><br>' .
366 _("Unknown message number in reply from server: ") .
367 $regs[1] . "</font><br>\n";
368 } else {
369 $read_list[$id2index[$regs[1]]] = $r;
370 }
371 }
372 arsort($read_list);
373
374 $query = "$sid FETCH $msgs_str RFC822.SIZE\r\n";
375 fputs ($imap_stream, $query);
376 $sizesin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
377
378 foreach ($sizesin_list as $r) {
379 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
380 set_up_language($squirrelmail_language);
381 echo "<br><b><font color=$color[2]>\n";
382 echo _("ERROR : Could not complete request.");
383 echo "</b><br>\n";
384 echo _("Unknown response from IMAP server: ") . ' 2.';
385 echo $r[0] . "</font><br>\n";
386 exit;
387 }
388 if (!count($id2index[$regs[1]])) {
389 set_up_language($squirrelmail_language);
390 echo "<br><b><font color=$color[2]>\n";
391 echo _("ERROR : Could not complete request.");
392 echo "</b><br>\n";
393 echo _("Unknown messagenumber in reply from server: ");
394 echo $regs[1] . "</font><br>\n";
395 exit;
396 }
397 $sizes_list[$id2index[$regs[1]]] = $r;
398 }
399 arsort($sizes_list);
400
401 for ($msgi = 0; $msgi < $maxmsg; $msgi++) {
402 $subject = _("(no subject)");
403 $from = _("Unknown Sender");
404 $priority = 0;
405 $messageid = "<>";
406 $cc = "";
407 $to = "";
408 $date = "";
409 $type[0] = "";
410 $type[1] = "";
411 $inrepto = "";
412 $read = $read_list[$msgi];
413
414 $prevline = false;
415 foreach ($read as $read_part) {
416 //unfold multi-line headers
417 while ($prevline && strspn($read_part, "\t ") > 0) {
418 $read_part = substr($prevline, 0, -2) . ' ' . ltrim($read_part);
419 }
420 $prevline = $read_part;
421 if (eregi ("^to:(.*)$", $read_part, $regs)) {
422 $to = $regs[1];
423 } else if (eregi ("^from:(.*)$", $read_part, $regs)) {
424 $from = $regs[1];
425 } else if (eregi ("^x-priority:(.*)$", $read_part, $regs)) {
426 $priority = trim($regs[1]);
427 } else if (eregi ("^message-id:(.*)$", $read_part, $regs)) {
428 $messageid = trim($regs[1]);
429 } else if (eregi ("^cc:(.*)$", $read_part, $regs)) {
430 $cc = $regs[1];
431 } else if (eregi ("^date:(.*)$", $read_part, $regs)) {
432 $date = $regs[1];
433 } else if (eregi ("^subject:(.*)$", $read_part, $regs)) {
434 $subject = htmlspecialchars(trim($regs[1]));
435 if ($subject == "") {
436 $subject = _("(no subject)");
437 }
438 } else if (eregi ("^content-type:(.*)$", $read_part, $regs)) {
439 $type = strtolower(trim($regs[1]));
440 if ($pos = strpos($type, ";")) {
441 $type = substr($type, 0, $pos);
442 }
443 $type = explode("/", $type);
444 if (!isset($type[1])) {
445 $type[1] = '';
446 }
447 } else if (eregi ("^in-reply-to:(.*)$", $read_part, $regs)) {
448 $inrepto = trim($regs[1]);
449 }
450 }
451 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
452 if (trim($date) == "" || $internaldate) {
453 fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
454 $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
455 if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
456 $date_list = explode(' ', trim($regs[1]));
457 $date_list[0] = str_replace("-", ' ', $date_list[0]);
458 $date = implode(' ', $date_list);
459 }
460 }
461 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
462 $size = $regs[1];
463
464 $header = new small_header;
465 if ($issent) {
466 $header->from = (trim($to) != '' ? $to : '(' ._("No To Address") . ')');
467 } else {
468 $header->from = $from;
469 }
470
471 $header->date = $date;
472 $header->subject = $subject;
473 $header->to = $to;
474 $header->priority = $priority;
475 $header->message_id = $messageid;
476 $header->cc = $cc;
477 $header->size = $size;
478 $header->type0 = $type[0];
479 $header->type1 = $type[1];
480 $header->inrepto = $inrepto;
481 $result[] = $header;
482 }
483 return $result;
484 }
485
486 /* Returns the flags for the specified messages */
487 function sqimap_get_flags ($imap_stream, $i) {
488 $read = sqimap_run_command ($imap_stream, "FETCH $i:$i FLAGS", true, $response, $message);
489 if (ereg('FLAGS(.*)', $read[0], $regs)) {
490 return explode(' ', trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
491 }
492 return array('None');
493 }
494
495 function sqimap_get_flags_list ($imap_stream, $msg_list) {
496 $msgs_str = sqimap_message_list_squisher($msg_list);
497 for ($i = 0; $i < sizeof($msg_list); $i++) {
498 $id2index[$msg_list[$i]] = $i;
499 }
500 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
501 $result_flags = array();
502
503 for ($i = 0; $i < sizeof($result_list); $i++) {
504 if (eregi('^\* ([0-9]+).*FETCH.*FLAGS(.*)', $result_list[$i][0], $regs)
505 && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
506 $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
507 } else {
508 set_up_language($squirrelmail_language);
509 echo "<br><b><font color=$color[2]>\n" .
510 _("ERROR : Could not complete request.") .
511 "</b><br>\n" .
512 _("Unknown response from IMAP server: ") .
513 $result_list[$i][0] . "</font><br>\n";
514 exit;
515 }
516 }
517 arsort($result_flags);
518 return $result_flags;
519 }
520
521 /*
522 * Returns a message array with all the information about a message.
523 * See the documentation folder for more information about this array.
524 */
525 function sqimap_get_message ($imap_stream, $id, $mailbox) {
526 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
527 return sqimap_get_message_body($imap_stream, $header);
528 }
529
530 /* Wrapper function that reformats the header information. */
531 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
532 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
533 $header = sqimap_get_header($imap_stream, $read);
534 $header->id = $id;
535 $header->mailbox = $mailbox;
536 return $header;
537 }
538
539 /* Wrapper function that reformats the entity header information. */
540 function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
541 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.HEADER]", true, $response, $message);
542 $header = sqimap_get_header($imap_stream, $read);
543 $header->id = $id;
544 $header->mailbox = $mailbox;
545 return $header;
546 }
547
548
549 /* Wrapper function that returns entity headers for use by decodeMime */
550 /*
551 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
552 $header = sqimap_get_header($imap_stream, $read);
553 $type0 = $header["TYPE0"];
554 $type1 = $header["TYPE1"];
555 $bound = $header["BOUNDARY"];
556 $encoding = $header["ENCODING"];
557 $charset = $header["CHARSET"];
558 $filename = $header["FILENAME"];
559 }
560 */
561 /* Queries the IMAP server and gets all header information. */
562 function sqimap_get_header ($imap_stream, $read) {
563 global $where, $what;
564
565 $hdr = new msg_header();
566 $i = 0;
567
568 /* Set up some defaults */
569 $hdr->type0 = "text";
570 $hdr->type1 = "plain";
571 $hdr->charset = "us-ascii";
572
573 while ($i < count($read)) {
574 //unfold multi-line headers
575 while ($i + 1 < count($read) && strspn($read[$i + 1], "\t ") > 0) {
576 $read[$i + 1] = substr($read[$i], 0, -2) . ' ' . ltrim($read[$i + 1]);
577 array_splice($read, $i, 1);
578 }
579
580 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
581 $hdr->mime = true;
582 $i++;
583 }
584 /* ENCODING TYPE */
585 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
586 $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
587 $i++;
588 }
589 /* CONTENT-TYPE */
590 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
591 $cont = strtolower(trim(substr($read[$i], 13)));
592 if (strpos($cont, ";")) {
593 $cont = substr($cont, 0, strpos($cont, ";"));
594 }
595
596 if (strpos($cont, "/")) {
597 $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
598 $hdr->type1 = substr($cont, strpos($cont, "/")+1);
599 } else {
600 $hdr->type0 = $cont;
601 }
602
603 $line = $read[$i];
604 $i++;
605 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
606 str_replace("\n", "", $line);
607 str_replace("\n", "", $read[$i]);
608 $line = "$line $read[$i]";
609 $i++;
610 }
611
612 /* Detect the boundary of a multipart message */
613 if (eregi('boundary="([^"]+)"', $line, $regs)) {
614 $hdr->boundary = $regs[1];
615 }
616
617 /* Detect the charset */
618 if (strpos(strtolower(trim($line)), "charset=")) {
619 $pos = strpos($line, "charset=") + 8;
620 $charset = trim($line);
621 if (strpos($line, ";", $pos) > 0) {
622 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
623 } else {
624 $charset = substr($charset, $pos);
625 }
626 $charset = str_replace("\"", "", $charset);
627 $hdr->charset = $charset;
628 } else {
629 $hdr->charset = "us-ascii";
630 }
631 /* Detect type in case of multipart/related */
632 if (strpos(strtolower(trim($line)), "type=")) {
633 $pos = strpos($line, "type=") + 6;
634 $type = trim($line);
635 if (strpos($line, ";", $pos) > 0) {
636 $type = substr($type, $pos, strpos($line, ";", $pos)-$pos);
637 } else {
638 $type = substr($type, $pos);
639 }
640 $hdr->type = $type;
641 }
642 }
643 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
644 /* Add better content-disposition support */
645 $line = $read[$i];
646 $i++;
647 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
648 str_replace("\n", "", $line);
649 str_replace("\n", "", $read[$i]);
650 $line = "$line $read[$i]";
651 $i++;
652 }
653
654 /* Detects filename if any */
655 if (strpos(strtolower(trim($line)), "filename=")) {
656 $pos = strpos($line, "filename=") + 9;
657 $name = trim($line);
658 if (strpos($line, " ", $pos) > 0) {
659 $name = substr($name, $pos, strpos($line, " ", $pos));
660 } else {
661 $name = substr($name, $pos);
662 }
663 $name = str_replace("\"", "", $name);
664 $hdr->filename = $name;
665 }
666 }
667 /* REPLY-TO */
668 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
669 $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
670 $i++;
671 }
672 /* FROM */
673 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
674 $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
675 if (! isset($hdr->replyto) || $hdr->replyto == "") {
676 $hdr->replyto = $hdr->from;
677 }
678 $i++;
679 }
680 /* DATE */
681 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
682 $d = substr($read[$i], 5);
683 $d = trim($d);
684 $d = strtr($d, array(' ' => ' '));
685 $d = explode(' ', $d);
686 $hdr->date = getTimeStamp($d);
687 $i++;
688 }
689 /* SUBJECT */
690 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
691 $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
692 if (strlen(Chop($hdr->subject)) == 0) {
693 $hdr->subject = _("(no subject)");
694 }
695 /*
696 if ($where == 'SUBJECT') {
697 $hdr->subject = $what;
698 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
699 }
700 */
701 $i++;
702 }
703 /* CC */
704 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
705 $pos = 0;
706 $hdr->cc[$pos] = trim(substr($read[$i], 4));
707 $i++;
708 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
709 $pos++;
710 $hdr->cc[$pos] = trim($read[$i]);
711 $i++;
712 }
713 }
714 /* BCC */
715 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
716 $pos = 0;
717 $hdr->bcc[$pos] = trim(substr($read[$i], 5));
718 $i++;
719 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
720 $pos++;
721 $hdr->bcc[$pos] = trim($read[$i]);
722 $i++;
723 }
724 }
725 /* TO */
726 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
727 $pos = 0;
728 $hdr->to[$pos] = trim(substr($read[$i], 4));
729 $i++;
730 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
731 $pos++;
732 $hdr->to[$pos] = trim($read[$i]);
733 $i++;
734 }
735 }
736 /* MESSAGE ID */
737 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
738 $hdr->message_id = trim(substr($read[$i], 11));
739 $i++;
740 }
741 /* ERROR CORRECTION */
742 else if (substr($read[$i], 0, 1) == ")") {
743 if (strlen(trim($hdr->subject)) == 0) {
744 $hdr->subject = _("(no subject)");
745 }
746 if (strlen(trim($hdr->from)) == 0) {
747 $hdr->from = _("(unknown sender)");
748 }
749 if (strlen(trim($hdr->date)) == 0) {
750 $hdr->date = time();
751 }
752 $i++;
753 }
754 /* X-PRIORITY */
755 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
756 $hdr->priority = trim(substr($read[$i], 11));
757 $i++;
758 }
759 else {
760 $i++;
761 }
762 }
763 return $hdr;
764 }
765
766 /* Returns the body of a message. */
767 function sqimap_get_message_body ($imap_stream, &$header) {
768 $id = $header->id;
769 return decodeMime($imap_stream, $header);
770 }
771
772 ?>