fixes sort order when using server-side and $sort=6
[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 = 0;
194 $indent = 0;
195 $fake = FALSE;
196 for ($k=1;$k<(count($thread_new[$i]))-1;$k++) {
197 $chars = count_chars($thread_new[$i][$k], 1);
198 if (isset($chars['40'])) { /* testing for ( */
199 $level = $level + $chars['40'];
200 }
201 if (isset($chars['41'])) { /* testing for ) */
202 $level = $level - $chars['41'];
203 $spaces = 0;
204 /* if we were faking lets stop, this portion
205 of the thread is over
206 */
207 if ($level == $cutoff) {
208 $fake = FALSE;
209 }
210 }
211 if (isset($chars['32'])) { /* testing for space */
212 $spaces = $spaces + $chars['32'];
213 }
214 $indent = $level + $spaces;
215 /* must have run into a message that broke the thread
216 so we are adjusting for that portion
217 */
218 if ($fake == TRUE) {
219 $indent = $indent +1;
220 }
221 if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
222 $child = $regs[1];
223 }
224 /* the thread must be broken if $indent == 0
225 so indent the message once and start faking it
226 */
227 if ($indent == 0) {
228 $indent = 1;
229 $fake = TRUE;
230 $cutoff = $level;
231 }
232 /* dont need abs but if indent was negative
233 errors would occur
234 */
235 $indent_array[$child] = abs($indent);
236 }
237 }
238 return $indent_array;
239 }
240
241
242 /* returns an array with each element as a string
243 representing one message thread as returned by
244 the IMAP server
245 */
246
247 function get_thread_sort ($imap_stream) {
248 global $thread_new, $sort_by_ref, $default_charset, $server_sort_array;
249 if (session_is_registered('thread_new')) {
250 session_unregister('thread_new');
251 }
252 if (session_is_registered('server_sort_array')) {
253 session_unregister('server_srot_array');
254 }
255 $sid = sqimap_session_id();
256 $thread_temp = array ();
257 if ($sort_by_ref == 1) {
258 $sort_type = 'REFERENCES';
259 }
260 else {
261 $sort_type = 'ORDEREDSUBJECT';
262 }
263 $thread_query = "$sid THREAD $sort_type ".strtoupper($default_charset)." ALL\r\n";
264 fputs($imap_stream, $thread_query);
265 $thread_test = sqimap_read_data($imap_stream, $sid, false, $response, $message);
266 if (isset($thread_test[0])) {
267 if (preg_match("/^\* THREAD (.+)$/", $thread_test[0], $regs)) {
268 $thread_list = trim($regs[1]);
269 }
270 }
271 else {
272 $thread_list = "";
273 }
274 if (!preg_match("/OK/", $response)) {
275 $server_sort_array = 'no';
276 return $server_sort_array;
277 }
278 $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
279 $char_count = count($thread_temp);
280 $counter = 0;
281 $thread_new = array();
282 $k = 0;
283 $thread_new[0] = "";
284 for ($i=0;$i<$char_count;$i++) {
285 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
286 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
287 }
288 elseif ($thread_temp[$i] == '(') {
289 $thread_new[$k] .= $thread_temp[$i];
290 $counter++;
291 }
292 elseif ($thread_temp[$i] == ')') {
293 if ($counter > 1) {
294 $thread_new[$k] .= $thread_temp[$i];
295 $counter = $counter - 1;
296 }
297 else {
298 $thread_new[$k] .= $thread_temp[$i];
299 $k++;
300 $thread_new[$k] = "";
301 $counter = $counter - 1;
302 }
303 }
304 }
305 session_register('thread_new');
306 $thread_new = array_reverse($thread_new);
307 $thread_list = implode(" ", $thread_new);
308 $thread_list = str_replace("(", " ", $thread_list);
309 $thread_list = str_replace(")", " ", $thread_list);
310 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
311 $server_sort_array = $thread_list;
312 session_register('server_sort_array');
313 return $thread_list;
314 }
315
316
317
318 function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
319 global $squirrelmail_language, $color, $data_dir, $username;
320
321 /* Get the small headers for each message in $msg_list */
322 $sid = sqimap_session_id();
323 $maxmsg = sizeof($msg_list);
324 $msgs_str = sqimap_message_list_squisher($msg_list);
325 $results = array();
326 $read_list = array();
327 $sizes_list = array();
328 /*
329 * We need to return the data in the same order as the caller supplied
330 * in $msg_list, but IMAP servers are free to return responses in
331 * whatever order they wish... So we need to re-sort manually
332 */
333 for ($i = 0; $i < sizeof($msg_list); $i++) {
334 $id2index[$msg_list[$i]] = $i;
335 }
336
337 $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";
338 fputs ($imap_stream, $query);
339 $readin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
340
341 foreach ($readin_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]>' .
345 _("ERROR : Could not complete request.") .
346 '</b><br>' .
347 _("Unknown response from IMAP server: ") . ' 1.' .
348 $r[0] . "</font><br>\n";
349 } else if (! isset($id2index[$regs[1]]) || !count($id2index[$regs[1]])) {
350 set_up_language($squirrelmail_language);
351 echo '<br><b><font color=$color[2]>' .
352 _("ERROR : Could not complete request.") .
353 '</b><br>' .
354 _("Unknown message number in reply from server: ") .
355 $regs[1] . "</font><br>\n";
356 } else {
357 $read_list[$id2index[$regs[1]]] = $r;
358 }
359 }
360 arsort($read_list);
361
362 $query = "$sid FETCH $msgs_str RFC822.SIZE\r\n";
363 fputs ($imap_stream, $query);
364 $sizesin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
365
366 foreach ($sizesin_list as $r) {
367 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
368 set_up_language($squirrelmail_language);
369 echo "<br><b><font color=$color[2]>\n";
370 echo _("ERROR : Could not complete request.");
371 echo "</b><br>\n";
372 echo _("Unknown response from IMAP server: ") . ' 2.';
373 echo $r[0] . "</font><br>\n";
374 exit;
375 }
376 if (!count($id2index[$regs[1]])) {
377 set_up_language($squirrelmail_language);
378 echo "<br><b><font color=$color[2]>\n";
379 echo _("ERROR : Could not complete request.");
380 echo "</b><br>\n";
381 echo _("Unknown messagenumber in reply from server: ");
382 echo $regs[1] . "</font><br>\n";
383 exit;
384 }
385 $sizes_list[$id2index[$regs[1]]] = $r;
386 }
387 arsort($sizes_list);
388
389 for ($msgi = 0; $msgi < $maxmsg; $msgi++) {
390 $subject = _("(no subject)");
391 $from = _("Unknown Sender");
392 $priority = 0;
393 $messageid = "<>";
394 $cc = "";
395 $to = "";
396 $date = "";
397 $type[0] = "";
398 $type[1] = "";
399 $inrepto = "";
400 $read = $read_list[$msgi];
401
402 $prevline = false;
403 foreach ($read as $read_part) {
404 //unfold multi-line headers
405 while ($prevline && strspn($read_part, "\t ") > 0) {
406 $read_part = substr($prevline, 0, -2) . ' ' . ltrim($read_part);
407 }
408 $prevline = $read_part;
409 if (eregi ("^to:(.*)$", $read_part, $regs)) {
410 $to = $regs[1];
411 } else if (eregi ("^from:(.*)$", $read_part, $regs)) {
412 $from = $regs[1];
413 } else if (eregi ("^x-priority:(.*)$", $read_part, $regs)) {
414 $priority = trim($regs[1]);
415 } else if (eregi ("^message-id:(.*)$", $read_part, $regs)) {
416 $messageid = trim($regs[1]);
417 } else if (eregi ("^cc:(.*)$", $read_part, $regs)) {
418 $cc = $regs[1];
419 } else if (eregi ("^date:(.*)$", $read_part, $regs)) {
420 $date = $regs[1];
421 } else if (eregi ("^subject:(.*)$", $read_part, $regs)) {
422 $subject = htmlspecialchars(trim($regs[1]));
423 if ($subject == "") {
424 $subject = _("(no subject)");
425 }
426 } else if (eregi ("^content-type:(.*)$", $read_part, $regs)) {
427 $type = strtolower(trim($regs[1]));
428 if ($pos = strpos($type, ";")) {
429 $type = substr($type, 0, $pos);
430 }
431 $type = explode("/", $type);
432 if (!isset($type[1])) {
433 $type[1] = '';
434 }
435 } else if (eregi ("^in-reply-to:(.*)$", $read_part, $regs)) {
436 $inrepto = trim($regs[1]);
437 }
438 }
439 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
440 if (trim($date) == "" || $internaldate) {
441 fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
442 $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
443 if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
444 $date_list = explode(' ', trim($regs[1]));
445 $date_list[0] = str_replace("-", ' ', $date_list[0]);
446 $date = implode(' ', $date_list);
447 }
448 }
449 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
450 $size = $regs[1];
451
452 $header = new small_header;
453 if ($issent) {
454 $header->from = (trim($to) != '' ? $to : '(' ._("No To Address") . ')');
455 } else {
456 $header->from = $from;
457 }
458
459 $header->date = $date;
460 $header->subject = $subject;
461 $header->to = $to;
462 $header->priority = $priority;
463 $header->message_id = $messageid;
464 $header->cc = $cc;
465 $header->size = $size;
466 $header->type0 = $type[0];
467 $header->type1 = $type[1];
468 $header->inrepto = $inrepto;
469 $result[] = $header;
470 }
471 return $result;
472 }
473
474 /* Returns the flags for the specified messages */
475 function sqimap_get_flags ($imap_stream, $i) {
476 $read = sqimap_run_command ($imap_stream, "FETCH $i:$i FLAGS", true, $response, $message);
477 if (ereg('FLAGS(.*)', $read[0], $regs)) {
478 return explode(' ', trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
479 }
480 return array('None');
481 }
482
483 function sqimap_get_flags_list ($imap_stream, $msg_list) {
484 $msgs_str = sqimap_message_list_squisher($msg_list);
485 for ($i = 0; $i < sizeof($msg_list); $i++) {
486 $id2index[$msg_list[$i]] = $i;
487 }
488 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
489 $result_flags = array();
490
491 for ($i = 0; $i < sizeof($result_list); $i++) {
492 if (eregi('^\* ([0-9]+).*FETCH.*FLAGS(.*)', $result_list[$i][0], $regs)
493 && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
494 $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
495 } else {
496 set_up_language($squirrelmail_language);
497 echo "<br><b><font color=$color[2]>\n" .
498 _("ERROR : Could not complete request.") .
499 "</b><br>\n" .
500 _("Unknown response from IMAP server: ") .
501 $result_list[$i][0] . "</font><br>\n";
502 exit;
503 }
504 }
505 arsort($result_flags);
506 return $result_flags;
507 }
508
509 /*
510 * Returns a message array with all the information about a message.
511 * See the documentation folder for more information about this array.
512 */
513 function sqimap_get_message ($imap_stream, $id, $mailbox) {
514 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
515 return sqimap_get_message_body($imap_stream, $header);
516 }
517
518 /* Wrapper function that reformats the header information. */
519 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
520 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
521 $header = sqimap_get_header($imap_stream, $read);
522 $header->id = $id;
523 $header->mailbox = $mailbox;
524 return $header;
525 }
526
527 /* Wrapper function that reformats the entity header information. */
528 function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
529 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.HEADER]", true, $response, $message);
530 $header = sqimap_get_header($imap_stream, $read);
531 $header->id = $id;
532 $header->mailbox = $mailbox;
533 return $header;
534 }
535
536
537 /* Wrapper function that returns entity headers for use by decodeMime */
538 /*
539 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
540 $header = sqimap_get_header($imap_stream, $read);
541 $type0 = $header["TYPE0"];
542 $type1 = $header["TYPE1"];
543 $bound = $header["BOUNDARY"];
544 $encoding = $header["ENCODING"];
545 $charset = $header["CHARSET"];
546 $filename = $header["FILENAME"];
547 }
548 */
549 /* Queries the IMAP server and gets all header information. */
550 function sqimap_get_header ($imap_stream, $read) {
551 global $where, $what;
552
553 $hdr = new msg_header();
554 $i = 0;
555
556 /* Set up some defaults */
557 $hdr->type0 = "text";
558 $hdr->type1 = "plain";
559 $hdr->charset = "us-ascii";
560
561 while ($i < count($read)) {
562 //unfold multi-line headers
563 while ($i + 1 < count($read) && strspn($read[$i + 1], "\t ") > 0) {
564 $read[$i + 1] = substr($read[$i], 0, -2) . ' ' . ltrim($read[$i + 1]);
565 array_splice($read, $i, 1);
566 }
567
568 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
569 $hdr->mime = true;
570 $i++;
571 }
572 /* ENCODING TYPE */
573 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
574 $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
575 $i++;
576 }
577 /* CONTENT-TYPE */
578 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
579 $cont = strtolower(trim(substr($read[$i], 13)));
580 if (strpos($cont, ";")) {
581 $cont = substr($cont, 0, strpos($cont, ";"));
582 }
583
584 if (strpos($cont, "/")) {
585 $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
586 $hdr->type1 = substr($cont, strpos($cont, "/")+1);
587 } else {
588 $hdr->type0 = $cont;
589 }
590
591 $line = $read[$i];
592 $i++;
593 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
594 str_replace("\n", "", $line);
595 str_replace("\n", "", $read[$i]);
596 $line = "$line $read[$i]";
597 $i++;
598 }
599
600 /* Detect the boundary of a multipart message */
601 if (eregi('boundary="([^"]+)"', $line, $regs)) {
602 $hdr->boundary = $regs[1];
603 }
604
605 /* Detect the charset */
606 if (strpos(strtolower(trim($line)), "charset=")) {
607 $pos = strpos($line, "charset=") + 8;
608 $charset = trim($line);
609 if (strpos($line, ";", $pos) > 0) {
610 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
611 } else {
612 $charset = substr($charset, $pos);
613 }
614 $charset = str_replace("\"", "", $charset);
615 $hdr->charset = $charset;
616 } else {
617 $hdr->charset = "us-ascii";
618 }
619 /* Detect type in case of multipart/related */
620 if (strpos(strtolower(trim($line)), "type=")) {
621 $pos = strpos($line, "type=") + 6;
622 $type = trim($line);
623 if (strpos($line, ";", $pos) > 0) {
624 $type = substr($type, $pos, strpos($line, ";", $pos)-$pos);
625 } else {
626 $type = substr($type, $pos);
627 }
628 $hdr->type = $type;
629 }
630 }
631 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
632 /* Add better content-disposition support */
633 $line = $read[$i];
634 $i++;
635 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
636 str_replace("\n", "", $line);
637 str_replace("\n", "", $read[$i]);
638 $line = "$line $read[$i]";
639 $i++;
640 }
641
642 /* Detects filename if any */
643 if (strpos(strtolower(trim($line)), "filename=")) {
644 $pos = strpos($line, "filename=") + 9;
645 $name = trim($line);
646 if (strpos($line, " ", $pos) > 0) {
647 $name = substr($name, $pos, strpos($line, " ", $pos));
648 } else {
649 $name = substr($name, $pos);
650 }
651 $name = str_replace("\"", "", $name);
652 $hdr->filename = $name;
653 }
654 }
655 /* REPLY-TO */
656 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
657 $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
658 $i++;
659 }
660 /* FROM */
661 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
662 $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
663 if (! isset($hdr->replyto) || $hdr->replyto == "") {
664 $hdr->replyto = $hdr->from;
665 }
666 $i++;
667 }
668 /* DATE */
669 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
670 $d = substr($read[$i], 5);
671 $d = trim($d);
672 $d = strtr($d, array(' ' => ' '));
673 $d = explode(' ', $d);
674 $hdr->date = getTimeStamp($d);
675 $i++;
676 }
677 /* SUBJECT */
678 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
679 $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
680 if (strlen(Chop($hdr->subject)) == 0) {
681 $hdr->subject = _("(no subject)");
682 }
683 /*
684 if ($where == 'SUBJECT') {
685 $hdr->subject = $what;
686 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
687 }
688 */
689 $i++;
690 }
691 /* CC */
692 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
693 $pos = 0;
694 $hdr->cc[$pos] = trim(substr($read[$i], 4));
695 $i++;
696 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
697 $pos++;
698 $hdr->cc[$pos] = trim($read[$i]);
699 $i++;
700 }
701 }
702 /* BCC */
703 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
704 $pos = 0;
705 $hdr->bcc[$pos] = trim(substr($read[$i], 5));
706 $i++;
707 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
708 $pos++;
709 $hdr->bcc[$pos] = trim($read[$i]);
710 $i++;
711 }
712 }
713 /* TO */
714 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
715 $pos = 0;
716 $hdr->to[$pos] = trim(substr($read[$i], 4));
717 $i++;
718 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
719 $pos++;
720 $hdr->to[$pos] = trim($read[$i]);
721 $i++;
722 }
723 }
724 /* MESSAGE ID */
725 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
726 $hdr->message_id = trim(substr($read[$i], 11));
727 $i++;
728 }
729 /* ERROR CORRECTION */
730 else if (substr($read[$i], 0, 1) == ")") {
731 if (strlen(trim($hdr->subject)) == 0) {
732 $hdr->subject = _("(no subject)");
733 }
734 if (strlen(trim($hdr->from)) == 0) {
735 $hdr->from = _("(unknown sender)");
736 }
737 if (strlen(trim($hdr->date)) == 0) {
738 $hdr->date = time();
739 }
740 $i++;
741 }
742 /* X-PRIORITY */
743 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
744 $hdr->priority = trim(substr($read[$i], 11));
745 $i++;
746 }
747 else {
748 $i++;
749 }
750 }
751 return $hdr;
752 }
753
754 /* Returns the body of a message. */
755 function sqimap_get_message_body ($imap_stream, &$header) {
756 $id = $header->id;
757 return decodeMime($imap_stream, $header);
758 }
759
760 ?>