6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This implements functions that manipulate messages
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);
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;
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);
26 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
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);
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);
39 /* Returns some general header information -- FROM, DATE, and SUBJECT */
41 var $from = '', $subject = '', $date = '', $to = '',
42 $priority = 0, $message_id = 0, $cc = '';
45 function sqimap_get_small_header ($imap_stream, $id, $sent) {
46 $res = sqimap_get_small_header_list($imap_stream, array($id), $sent);
51 * Sort the message list and crunch to be as small as possible
52 * (overflow could happen, so make it small if possible)
54 function sqimap_message_list_squisher($messages_array) {
55 if( !is_array( $messages_array ) ) {
58 sort($messages_array, SORT_NUMERIC
);
60 while ($messages_array) {
61 $start = array_shift($messages_array);
63 while (isset($messages_array[0]) && $messages_array[0] == $end +
1) {
64 $end = array_shift($messages_array);
66 if ($msgs_str != '') {
71 $msgs_str .= ':' . $end;
78 /* returns the references header lines */
79 function get_reference_header ($imap_stream, $message) {
80 $responses = array ();
81 $sid = sqimap_session_id();
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 ();
94 /* get sort order from server and
95 * return it as the $id array for
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;
104 if (session_is_registered('server_sort_array')) {
105 session_unregister('server_sort_array');
107 $sid = sqimap_session_id();
110 $server_sort_array = array();
111 $sort_test = array();
113 $sort_on = array (0=> 'DATE',
120 if ($internal_date_sort == true) {
121 $sort_on[0] = 'ARRIVAL';
122 $sort_on[1] = 'ARRIVAL';
124 if ($sent_folder == $mailbox) {
128 if (!empty($sort_on[$sort])) {
129 $sort_query = "$sid SORT ($sort_on[$sort]) ".strtoupper($default_charset)." ALL\r\n";
130 fputs($imap_stream, $sort_query);
131 $sort_test = sqimap_read_data($imap_stream, $sid, false, $response, $message);
133 if (isset($sort_test[0])) {
134 if (preg_match("/^\* SORT (.+)$/", $sort_test[0], $regs)) {
135 $server_sort_array = preg_split("/ /", trim($regs[1]));
138 if ($sort == 0 ||
$sort == 2 ||
$sort == 4) {
139 $server_sort_array = array_reverse($server_sort_array);
141 if (!preg_match("/OK/", $response)) {
142 $server_sort_array = 'no';
144 session_register('server_sort_array');
145 return $server_sort_array;
148 /* returns an indent array for printMessageinfo()
149 this represents the amount of indent needed (value)
150 for this message number (key)
153 function get_parent_level ($imap_stream) {
154 global $sort_by_ref, $default_charset, $thread_new;
159 /* loop through the threads and take unwanted characters out
160 of the thread string then chop it up
162 for ($i=0;$i<count($thread_new);$i++
) {
163 $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]);
164 $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]);
165 $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY
);
167 $indent_array = array();
169 $thread_new = array();
171 /* looping through the parts of one message thread */
173 for ($i=0;$i<count($thread_new);$i++
) {
174 /* first grab the parent, it does not indent */
176 if (isset($thread_new[$i][0])) {
177 if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) {
181 $indent_array[$parent] = 0;
183 /* now the children, checking each thread portion for
184 ),(, and space, adjusting the level and space values
185 to get the indent level
191 for ($k=1;$k<(count($thread_new[$i]))-1;$k++
) {
192 $chars = count_chars($thread_new[$i][$k], 1);
193 if (isset($chars['40'])) { /* testing for ( */
194 $level = $level +
$chars['40'];
196 if (isset($chars['41'])) { /* testing for ) */
197 $level = $level - $chars['41'];
199 /* if we were faking lets stop, this portion
200 of the thread is over
202 if ($level == $cutoff) {
206 if (isset($chars['32'])) { /* testing for space */
207 $spaces = $spaces +
$chars['32'];
209 $indent = $level +
$spaces;
210 /* must have run into a message that broke the thread
211 so we are adjusting for that portion
214 $indent = $indent +
1;
216 if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
219 /* the thread must be broken if $indent == 0
220 so indent the message once and start faking it
227 /* dont need abs but if indent was negative
230 $indent_array[$child] = abs($indent);
233 return $indent_array;
237 /* returns an array with each element as a string
238 representing one message thread as returned by
242 function get_thread_sort ($imap_stream) {
243 global $thread_new, $sort_by_ref, $default_charset, $server_sort_array;
244 if (session_is_registered('thread_new')) {
245 session_unregister('thread_new');
247 if (session_is_registered('server_sort_array')) {
248 session_unregister('server_srot_array');
250 $sid = sqimap_session_id();
251 $thread_temp = array ();
252 if ($sort_by_ref == 1) {
253 $sort_type = 'REFERENCES';
256 $sort_type = 'ORDEREDSUBJECT';
258 $thread_query = "$sid THREAD $sort_type ".strtoupper($default_charset)." ALL\r\n";
259 fputs($imap_stream, $thread_query);
260 $thread_test = sqimap_read_data($imap_stream, $sid, false, $response, $message);
261 if (isset($thread_test[0])) {
262 if (preg_match("/^\* THREAD (.+)$/", $thread_test[0], $regs)) {
263 $thread_list = trim($regs[1]);
269 if (!preg_match("/OK/", $response)) {
270 $server_sort_array = 'no';
271 return $server_sort_array;
273 $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
274 $char_count = count($thread_temp);
276 $thread_new = array();
279 for ($i=0;$i<$char_count;$i++
) {
280 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
281 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
283 elseif ($thread_temp[$i] == '(') {
284 $thread_new[$k] .= $thread_temp[$i];
287 elseif ($thread_temp[$i] == ')') {
289 $thread_new[$k] .= $thread_temp[$i];
290 $counter = $counter - 1;
293 $thread_new[$k] .= $thread_temp[$i];
295 $thread_new[$k] = "";
296 $counter = $counter - 1;
300 session_register('thread_new');
301 $thread_new = array_reverse($thread_new);
302 $thread_list = implode(" ", $thread_new);
303 $thread_list = str_replace("(", " ", $thread_list);
304 $thread_list = str_replace(")", " ", $thread_list);
305 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
306 $server_sort_array = $thread_list;
307 session_register('server_sort_array');
313 function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
314 global $squirrelmail_language, $color, $data_dir, $username;
316 /* Get the small headers for each message in $msg_list */
317 $sid = sqimap_session_id();
318 $maxmsg = sizeof($msg_list);
319 $msgs_str = sqimap_message_list_squisher($msg_list);
321 $read_list = array();
322 $sizes_list = array();
324 * We need to return the data in the same order as the caller supplied
325 * in $msg_list, but IMAP servers are free to return responses in
326 * whatever order they wish... So we need to re-sort manually
328 for ($i = 0; $i < sizeof($msg_list); $i++
) {
329 $id2index[$msg_list[$i]] = $i;
332 $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";
333 fputs ($imap_stream, $query);
334 $readin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
336 foreach ($readin_list as $r) {
337 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
338 set_up_language($squirrelmail_language);
339 echo '<br><b><font color=$color[2]>' .
340 _("ERROR : Could not complete request.") .
342 _("Unknown response from IMAP server: ") . ' 1.' .
343 $r[0] . "</font><br>\n";
344 } else if (! isset($id2index[$regs[1]]) ||
!count($id2index[$regs[1]])) {
345 set_up_language($squirrelmail_language);
346 echo '<br><b><font color=$color[2]>' .
347 _("ERROR : Could not complete request.") .
349 _("Unknown message number in reply from server: ") .
350 $regs[1] . "</font><br>\n";
352 $read_list[$id2index[$regs[1]]] = $r;
357 $query = "$sid FETCH $msgs_str RFC822.SIZE\r\n";
358 fputs ($imap_stream, $query);
359 $sizesin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
361 foreach ($sizesin_list as $r) {
362 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
363 set_up_language($squirrelmail_language);
364 echo "<br><b><font color=$color[2]>\n";
365 echo _("ERROR : Could not complete request.");
367 echo _("Unknown response from IMAP server: ") . ' 2.';
368 echo $r[0] . "</font><br>\n";
371 if (!count($id2index[$regs[1]])) {
372 set_up_language($squirrelmail_language);
373 echo "<br><b><font color=$color[2]>\n";
374 echo _("ERROR : Could not complete request.");
376 echo _("Unknown messagenumber in reply from server: ");
377 echo $regs[1] . "</font><br>\n";
380 $sizes_list[$id2index[$regs[1]]] = $r;
384 for ($msgi = 0; $msgi < $maxmsg; $msgi++
) {
385 $subject = _("(no subject)");
386 $from = _("Unknown Sender");
395 $read = $read_list[$msgi];
398 foreach ($read as $read_part) {
399 //unfold multi-line headers
400 while ($prevline && strspn($read_part, "\t ") > 0) {
401 $read_part = substr($prevline, 0, -2) . ' ' . ltrim($read_part);
403 $prevline = $read_part;
404 if (eregi ("^to:(.*)$", $read_part, $regs)) {
406 } else if (eregi ("^from:(.*)$", $read_part, $regs)) {
408 } else if (eregi ("^x-priority:(.*)$", $read_part, $regs)) {
409 $priority = trim($regs[1]);
410 } else if (eregi ("^message-id:(.*)$", $read_part, $regs)) {
411 $messageid = trim($regs[1]);
412 } else if (eregi ("^cc:(.*)$", $read_part, $regs)) {
414 } else if (eregi ("^date:(.*)$", $read_part, $regs)) {
416 } else if (eregi ("^subject:(.*)$", $read_part, $regs)) {
417 $subject = htmlspecialchars(trim($regs[1]));
418 if ($subject == "") {
419 $subject = _("(no subject)");
421 } else if (eregi ("^content-type:(.*)$", $read_part, $regs)) {
422 $type = strtolower(trim($regs[1]));
423 if ($pos = strpos($type, ";")) {
424 $type = substr($type, 0, $pos);
426 $type = explode("/", $type);
427 if (!isset($type[1])) {
430 } else if (eregi ("^in-reply-to:(.*)$", $read_part, $regs)) {
431 $inrepto = trim($regs[1]);
434 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
435 if (trim($date) == "" ||
$internaldate) {
436 fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
437 $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
438 if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
439 $date_list = explode(' ', trim($regs[1]));
440 $date_list[0] = str_replace("-", ' ', $date_list[0]);
441 $date = implode(' ', $date_list);
444 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
447 $header = new small_header
;
449 $header->from
= (trim($to) != '' ?
$to : '(' ._("No To Address") . ')');
451 $header->from
= $from;
454 $header->date
= $date;
455 $header->subject
= $subject;
457 $header->priority
= $priority;
458 $header->message_id
= $messageid;
460 $header->size
= $size;
461 $header->type0
= $type[0];
462 $header->type1
= $type[1];
463 $header->inrepto
= $inrepto;
469 /* Returns the flags for the specified messages */
470 function sqimap_get_flags ($imap_stream, $i) {
471 $read = sqimap_run_command ($imap_stream, "FETCH $i:$i FLAGS", true, $response, $message);
472 if (ereg('FLAGS(.*)', $read[0], $regs)) {
473 return explode(' ', trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
475 return array('None');
478 function sqimap_get_flags_list ($imap_stream, $msg_list) {
479 $msgs_str = sqimap_message_list_squisher($msg_list);
480 for ($i = 0; $i < sizeof($msg_list); $i++
) {
481 $id2index[$msg_list[$i]] = $i;
483 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
484 $result_flags = array();
486 for ($i = 0; $i < sizeof($result_list); $i++
) {
487 if (eregi('^\* ([0-9]+).*FETCH.*FLAGS(.*)', $result_list[$i][0], $regs)
488 && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
489 $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
491 set_up_language($squirrelmail_language);
492 echo "<br><b><font color=$color[2]>\n" .
493 _("ERROR : Could not complete request.") .
495 _("Unknown response from IMAP server: ") .
496 $result_list[$i][0] . "</font><br>\n";
500 arsort($result_flags);
501 return $result_flags;
505 * Returns a message array with all the information about a message.
506 * See the documentation folder for more information about this array.
508 function sqimap_get_message ($imap_stream, $id, $mailbox) {
509 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
510 return sqimap_get_message_body($imap_stream, $header);
513 /* Wrapper function that reformats the header information. */
514 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
515 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
516 $header = sqimap_get_header($imap_stream, $read);
518 $header->mailbox
= $mailbox;
522 /* Wrapper function that reformats the entity header information. */
523 function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
524 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.HEADER]", true, $response, $message);
525 $header = sqimap_get_header($imap_stream, $read);
527 $header->mailbox
= $mailbox;
532 /* Wrapper function that returns entity headers for use by decodeMime */
534 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
535 $header = sqimap_get_header($imap_stream, $read);
536 $type0 = $header["TYPE0"];
537 $type1 = $header["TYPE1"];
538 $bound = $header["BOUNDARY"];
539 $encoding = $header["ENCODING"];
540 $charset = $header["CHARSET"];
541 $filename = $header["FILENAME"];
544 /* Queries the IMAP server and gets all header information. */
545 function sqimap_get_header ($imap_stream, $read) {
546 global $where, $what;
548 $hdr = new msg_header();
551 /* Set up some defaults */
552 $hdr->type0
= "text";
553 $hdr->type1
= "plain";
554 $hdr->charset
= "us-ascii";
556 while ($i < count($read)) {
557 //unfold multi-line headers
558 while ($i +
1 < count($read) && strspn($read[$i +
1], "\t ") > 0) {
559 $read[$i +
1] = substr($read[$i], 0, -2) . ' ' . ltrim($read[$i +
1]);
560 array_splice($read, $i, 1);
563 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
568 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
569 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
573 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
574 $cont = strtolower(trim(substr($read[$i], 13)));
575 if (strpos($cont, ";")) {
576 $cont = substr($cont, 0, strpos($cont, ";"));
579 if (strpos($cont, "/")) {
580 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
581 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
588 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
589 str_replace("\n", "", $line);
590 str_replace("\n", "", $read[$i]);
591 $line = "$line $read[$i]";
595 /* Detect the boundary of a multipart message */
596 if (eregi('boundary="([^"]+)"', $line, $regs)) {
597 $hdr->boundary
= $regs[1];
600 /* Detect the charset */
601 if (strpos(strtolower(trim($line)), "charset=")) {
602 $pos = strpos($line, "charset=") +
8;
603 $charset = trim($line);
604 if (strpos($line, ";", $pos) > 0) {
605 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
607 $charset = substr($charset, $pos);
609 $charset = str_replace("\"", "", $charset);
610 $hdr->charset
= $charset;
612 $hdr->charset
= "us-ascii";
614 /* Detect type in case of multipart/related */
615 if (strpos(strtolower(trim($line)), "type=")) {
616 $pos = strpos($line, "type=") +
6;
618 if (strpos($line, ";", $pos) > 0) {
619 $type = substr($type, $pos, strpos($line, ";", $pos)-$pos);
621 $type = substr($type, $pos);
626 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
627 /* Add better content-disposition support */
630 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
631 str_replace("\n", "", $line);
632 str_replace("\n", "", $read[$i]);
633 $line = "$line $read[$i]";
637 /* Detects filename if any */
638 if (strpos(strtolower(trim($line)), "filename=")) {
639 $pos = strpos($line, "filename=") +
9;
641 if (strpos($line, " ", $pos) > 0) {
642 $name = substr($name, $pos, strpos($line, " ", $pos));
644 $name = substr($name, $pos);
646 $name = str_replace("\"", "", $name);
647 $hdr->filename
= $name;
651 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
652 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
656 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
657 $hdr->from
= trim(substr($read[$i], 5, strlen($read[$i]) - 6));
658 if (! isset($hdr->replyto
) ||
$hdr->replyto
== "") {
659 $hdr->replyto
= $hdr->from
;
664 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
665 $d = substr($read[$i], 5);
667 $d = strtr($d, array(' ' => ' '));
668 $d = explode(' ', $d);
669 $hdr->date
= getTimeStamp($d);
673 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
674 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
675 if (strlen(Chop($hdr->subject
)) == 0) {
676 $hdr->subject
= _("(no subject)");
679 if ($where == 'SUBJECT') {
680 $hdr->subject = $what;
681 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
687 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
689 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
691 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
693 $hdr->cc
[$pos] = trim($read[$i]);
698 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
700 $hdr->bcc
[$pos] = trim(substr($read[$i], 5));
702 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
704 $hdr->bcc
[$pos] = trim($read[$i]);
709 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
711 $hdr->to
[$pos] = trim(substr($read[$i], 4));
713 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
715 $hdr->to
[$pos] = trim($read[$i]);
720 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
721 $hdr->message_id
= trim(substr($read[$i], 11));
724 /* ERROR CORRECTION */
725 else if (substr($read[$i], 0, 1) == ")") {
726 if (strlen(trim($hdr->subject
)) == 0) {
727 $hdr->subject
= _("(no subject)");
729 if (strlen(trim($hdr->from
)) == 0) {
730 $hdr->from
= _("(unknown sender)");
732 if (strlen(trim($hdr->date
)) == 0) {
738 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
739 $hdr->priority
= trim(substr($read[$i], 11));
749 /* Returns the body of a message. */
750 function sqimap_get_message_body ($imap_stream, &$header) {
752 return decodeMime($imap_stream, $header);