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');
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;
113 $sid = sqimap_session_id();
116 $server_sort_array = array();
117 $sort_test = array();
119 $sort_on = array (0=> 'DATE',
125 if ($internal_date_sort == true) {
126 $sort_on[0] = 'ARRIVAL';
127 $sort_on[1] = 'ARRIVAL';
129 if ($sent_folder == $mailbox) {
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);
138 if (isset($sort_test[0])) {
139 if (preg_match("/^\* SORT (.+)$/", $sort_test[0], $regs)) {
140 $server_sort_array = preg_split("/ /", trim($regs[1]));
143 if ($sort == 0 ||
$sort == 2 ||
$sort == 4) {
144 $server_sort_array = array_reverse($server_sort_array);
146 if (!preg_match("/OK/", $response)) {
147 $server_sort_array = 'no';
149 session_register('server_sort_array');
150 return $server_sort_array;
153 /* returns an indent array for printMessageinfo()
154 this represents the amount of indent needed (value)
155 for this message number (key)
158 function get_parent_level ($imap_stream) {
159 global $sort_by_ref, $default_charset, $thread_new;
164 /* loop through the threads and take unwanted characters out
165 of the thread string then chop it up
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
);
172 $indent_array = array();
174 $thread_new = array();
176 /* looping through the parts of one message thread */
178 for ($i=0;$i<count($thread_new);$i++
) {
179 /* first grab the parent, it does not indent */
181 if (isset($thread_new[$i][0])) {
182 if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) {
186 $indent_array[$parent] = 0;
188 /* now the children, checking each thread portion for
189 ),(, and space, adjusting the level and space values
190 to get the indent level
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'];
201 if (isset($chars['41'])) { /* testing for ) */
202 $level = $level - $chars['41'];
204 /* if we were faking lets stop, this portion
205 of the thread is over
207 if ($level == $cutoff) {
211 if (isset($chars['32'])) { /* testing for space */
212 $spaces = $spaces +
$chars['32'];
214 $indent = $level +
$spaces;
215 /* must have run into a message that broke the thread
216 so we are adjusting for that portion
219 $indent = $indent +
1;
221 if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
224 /* the thread must be broken if $indent == 0
225 so indent the message once and start faking it
232 /* dont need abs but if indent was negative
235 $indent_array[$child] = abs($indent);
238 return $indent_array;
242 /* returns an array with each element as a string
243 representing one message thread as returned by
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');
252 if (session_is_registered('server_sort_array')) {
253 session_unregister('server_srot_array');
255 $sid = sqimap_session_id();
256 $thread_temp = array ();
257 if ($sort_by_ref == 1) {
258 $sort_type = 'REFERENCES';
261 $sort_type = 'ORDEREDSUBJECT';
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]);
274 if (!preg_match("/OK/", $response)) {
275 $server_sort_array = 'no';
276 return $server_sort_array;
278 $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
279 $char_count = count($thread_temp);
281 $thread_new = array();
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];
288 elseif ($thread_temp[$i] == '(') {
289 $thread_new[$k] .= $thread_temp[$i];
292 elseif ($thread_temp[$i] == ')') {
294 $thread_new[$k] .= $thread_temp[$i];
295 $counter = $counter - 1;
298 $thread_new[$k] .= $thread_temp[$i];
300 $thread_new[$k] = "";
301 $counter = $counter - 1;
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');
318 function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
319 global $squirrelmail_language, $color, $data_dir, $username;
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);
326 $read_list = array();
327 $sizes_list = array();
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
333 for ($i = 0; $i < sizeof($msg_list); $i++
) {
334 $id2index[$msg_list[$i]] = $i;
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);
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.") .
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.") .
354 _("Unknown message number in reply from server: ") .
355 $regs[1] . "</font><br>\n";
357 $read_list[$id2index[$regs[1]]] = $r;
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);
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.");
372 echo _("Unknown response from IMAP server: ") . ' 2.';
373 echo $r[0] . "</font><br>\n";
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.");
381 echo _("Unknown messagenumber in reply from server: ");
382 echo $regs[1] . "</font><br>\n";
385 $sizes_list[$id2index[$regs[1]]] = $r;
389 for ($msgi = 0; $msgi < $maxmsg; $msgi++
) {
390 $subject = _("(no subject)");
391 $from = _("Unknown Sender");
400 $read = $read_list[$msgi];
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);
408 $prevline = $read_part;
409 if (eregi ("^to:(.*)$", $read_part, $regs)) {
411 } else if (eregi ("^from:(.*)$", $read_part, $regs)) {
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)) {
419 } else if (eregi ("^date:(.*)$", $read_part, $regs)) {
421 } else if (eregi ("^subject:(.*)$", $read_part, $regs)) {
422 $subject = htmlspecialchars(trim($regs[1]));
423 if ($subject == "") {
424 $subject = _("(no subject)");
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);
431 $type = explode("/", $type);
432 if (!isset($type[1])) {
435 } else if (eregi ("^in-reply-to:(.*)$", $read_part, $regs)) {
436 $inrepto = trim($regs[1]);
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);
449 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
452 $header = new small_header
;
454 $header->from
= (trim($to) != '' ?
$to : '(' ._("No To Address") . ')');
456 $header->from
= $from;
459 $header->date
= $date;
460 $header->subject
= $subject;
462 $header->priority
= $priority;
463 $header->message_id
= $messageid;
465 $header->size
= $size;
466 $header->type0
= $type[0];
467 $header->type1
= $type[1];
468 $header->inrepto
= $inrepto;
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])));
480 return array('None');
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;
488 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
489 $result_flags = array();
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])));
496 set_up_language($squirrelmail_language);
497 echo "<br><b><font color=$color[2]>\n" .
498 _("ERROR : Could not complete request.") .
500 _("Unknown response from IMAP server: ") .
501 $result_list[$i][0] . "</font><br>\n";
505 arsort($result_flags);
506 return $result_flags;
510 * Returns a message array with all the information about a message.
511 * See the documentation folder for more information about this array.
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);
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);
523 $header->mailbox
= $mailbox;
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);
532 $header->mailbox
= $mailbox;
537 /* Wrapper function that returns entity headers for use by decodeMime */
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"];
549 /* Queries the IMAP server and gets all header information. */
550 function sqimap_get_header ($imap_stream, $read) {
551 global $where, $what;
553 $hdr = new msg_header();
556 /* Set up some defaults */
557 $hdr->type0
= "text";
558 $hdr->type1
= "plain";
559 $hdr->charset
= "us-ascii";
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);
568 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
573 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
574 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
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, ";"));
584 if (strpos($cont, "/")) {
585 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
586 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
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]";
600 /* Detect the boundary of a multipart message */
601 if (eregi('boundary="([^"]+)"', $line, $regs)) {
602 $hdr->boundary
= $regs[1];
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);
612 $charset = substr($charset, $pos);
614 $charset = str_replace("\"", "", $charset);
615 $hdr->charset
= $charset;
617 $hdr->charset
= "us-ascii";
619 /* Detect type in case of multipart/related */
620 if (strpos(strtolower(trim($line)), "type=")) {
621 $pos = strpos($line, "type=") +
6;
623 if (strpos($line, ";", $pos) > 0) {
624 $type = substr($type, $pos, strpos($line, ";", $pos)-$pos);
626 $type = substr($type, $pos);
631 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
632 /* Add better content-disposition support */
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]";
642 /* Detects filename if any */
643 if (strpos(strtolower(trim($line)), "filename=")) {
644 $pos = strpos($line, "filename=") +
9;
646 if (strpos($line, " ", $pos) > 0) {
647 $name = substr($name, $pos, strpos($line, " ", $pos));
649 $name = substr($name, $pos);
651 $name = str_replace("\"", "", $name);
652 $hdr->filename
= $name;
656 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
657 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
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
;
669 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
670 $d = substr($read[$i], 5);
672 $d = strtr($d, array(' ' => ' '));
673 $d = explode(' ', $d);
674 $hdr->date
= getTimeStamp($d);
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)");
684 if ($where == 'SUBJECT') {
685 $hdr->subject = $what;
686 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
692 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
694 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
696 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
698 $hdr->cc
[$pos] = trim($read[$i]);
703 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
705 $hdr->bcc
[$pos] = trim(substr($read[$i], 5));
707 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
709 $hdr->bcc
[$pos] = trim($read[$i]);
714 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
716 $hdr->to
[$pos] = trim(substr($read[$i], 4));
718 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
720 $hdr->to
[$pos] = trim($read[$i]);
725 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
726 $hdr->message_id
= trim(substr($read[$i], 11));
729 /* ERROR CORRECTION */
730 else if (substr($read[$i], 0, 1) == ")") {
731 if (strlen(trim($hdr->subject
)) == 0) {
732 $hdr->subject
= _("(no subject)");
734 if (strlen(trim($hdr->from
)) == 0) {
735 $hdr->from
= _("(unknown sender)");
737 if (strlen(trim($hdr->date
)) == 0) {
743 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
744 $hdr->priority
= trim(substr($read[$i], 11));
754 /* Returns the body of a message. */
755 function sqimap_get_message_body ($imap_stream, &$header) {
757 return decodeMime($imap_stream, $header);