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