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 (preg_match("/^\* SORT (.+)$/", $sort_test[0], $regs)) {
134 $server_sort_array = preg_split("/ /", trim($regs[1]));
136 if ($sort == 0 ||
$sort == 2 ||
$sort == 4) {
137 $server_sort_array = array_reverse($server_sort_array);
139 if (!preg_match("/OK/", $response)) {
140 $server_sort_array = 'no';
142 session_register('server_sort_array');
143 return $server_sort_array;
146 /* returns an indent array for printMessageinfo()
147 this represents the amount of indent needed (value)
148 for this message number (key)
151 function get_parent_level ($imap_stream) {
152 global $sort_by_ref, $default_charset, $thread_new;
157 /* loop through the threads and take unwanted characters out
158 of the thread string then chop it up
160 for ($i=0;$i<count($thread_new);$i++
) {
161 $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]);
162 $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]);
163 $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY
);
165 $indent_array = array();
167 $thread_new = array();
169 /* looping through the parts of one message thread */
171 for ($i=0;$i<count($thread_new);$i++
) {
172 /* first grab the parent, it does not indent */
174 if (isset($thread_new[$i][0])) {
175 if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) {
179 $indent_array[$parent] = 0;
181 /* now the children, checking each thread portion for
182 ),(, and space, adjusting the level and space values
183 to get the indent level
189 for ($k=1;$k<(count($thread_new[$i]))-1;$k++
) {
190 $chars = count_chars($thread_new[$i][$k], 1);
191 if (isset($chars['40'])) { /* testing for ( */
192 $level = $level +
$chars['40'];
194 if (isset($chars['41'])) { /* testing for ) */
195 $level = $level - $chars['41'];
197 /* if we were faking lets stop, this portion
198 of the thread is over
200 if ($level == $cutoff) {
204 if (isset($chars['32'])) { /* testing for space */
205 $spaces = $spaces +
$chars['32'];
207 $indent = $level +
$spaces;
208 /* must have run into a message that broke the thread
209 so we are adjusting for that portion
212 $indent = $indent +
1;
214 if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
217 /* the thread must be broken if $indent == 0
218 so indent the message once and start faking it
225 /* dont need abs but if indent was negative
228 $indent_array[$child] = abs($indent);
231 return $indent_array;
235 /* returns an array with each element as a string
236 representing one message thread as returned by
240 function get_thread_sort ($imap_stream) {
241 global $thread_new, $sort_by_ref, $default_charset, $server_sort_array;
242 if (session_is_registered('thread_new')) {
243 session_unregister('thread_new');
245 if (session_is_registered('server_sort_array')) {
246 session_unregister('server_srot_array');
248 $sid = sqimap_session_id();
249 $thread_temp = array ();
250 if ($sort_by_ref == 1) {
251 $sort_type = 'REFERENCES';
254 $sort_type = 'ORDEREDSUBJECT';
256 $thread_query = "$sid THREAD $sort_type ".strtoupper($default_charset)." ALL\r\n";
257 fputs($imap_stream, $thread_query);
258 $thread_test = sqimap_read_data($imap_stream, $sid, false, $response, $message);
259 if (preg_match("/^\* THREAD (.+)$/", $thread_test[0], $regs)) {
260 $thread_list = trim($regs[1]);
265 if (!preg_match("/OK/", $response)) {
266 $server_sort_array = 'no';
267 return $server_sort_array;
269 $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
270 $char_count = count($thread_temp);
272 $thread_new = array();
275 for ($i=0;$i<$char_count;$i++
) {
276 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
277 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
279 elseif ($thread_temp[$i] == '(') {
280 $thread_new[$k] .= $thread_temp[$i];
283 elseif ($thread_temp[$i] == ')') {
285 $thread_new[$k] .= $thread_temp[$i];
286 $counter = $counter - 1;
289 $thread_new[$k] .= $thread_temp[$i];
291 $thread_new[$k] = "";
292 $counter = $counter - 1;
296 session_register('thread_new');
297 $thread_new = array_reverse($thread_new);
298 $thread_list = implode(" ", $thread_new);
299 $thread_list = str_replace("(", " ", $thread_list);
300 $thread_list = str_replace(")", " ", $thread_list);
301 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
302 $server_sort_array = $thread_list;
303 session_register('server_sort_array');
309 function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
310 global $squirrelmail_language, $color, $data_dir, $username;
312 /* Get the small headers for each message in $msg_list */
313 $sid = sqimap_session_id();
314 $maxmsg = sizeof($msg_list);
315 $msgs_str = sqimap_message_list_squisher($msg_list);
317 $read_list = array();
318 $sizes_list = array();
320 * We need to return the data in the same order as the caller supplied
321 * in $msg_list, but IMAP servers are free to return responses in
322 * whatever order they wish... So we need to re-sort manually
324 for ($i = 0; $i < sizeof($msg_list); $i++
) {
325 $id2index[$msg_list[$i]] = $i;
328 $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";
329 fputs ($imap_stream, $query);
330 $readin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
332 foreach ($readin_list as $r) {
333 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
334 set_up_language($squirrelmail_language);
335 echo '<br><b><font color=$color[2]>' .
336 _("ERROR : Could not complete request.") .
338 _("Unknown response from IMAP server: ") . ' 1.' .
339 $r[0] . "</font><br>\n";
340 } else if (! isset($id2index[$regs[1]]) ||
!count($id2index[$regs[1]])) {
341 set_up_language($squirrelmail_language);
342 echo '<br><b><font color=$color[2]>' .
343 _("ERROR : Could not complete request.") .
345 _("Unknown message number in reply from server: ") .
346 $regs[1] . "</font><br>\n";
348 $read_list[$id2index[$regs[1]]] = $r;
353 $query = "$sid FETCH $msgs_str RFC822.SIZE\r\n";
354 fputs ($imap_stream, $query);
355 $sizesin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
357 foreach ($sizesin_list as $r) {
358 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
359 set_up_language($squirrelmail_language);
360 echo "<br><b><font color=$color[2]>\n";
361 echo _("ERROR : Could not complete request.");
363 echo _("Unknown response from IMAP server: ") . ' 2.';
364 echo $r[0] . "</font><br>\n";
367 if (!count($id2index[$regs[1]])) {
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 messagenumber in reply from server: ");
373 echo $regs[1] . "</font><br>\n";
376 $sizes_list[$id2index[$regs[1]]] = $r;
380 for ($msgi = 0; $msgi < $maxmsg; $msgi++
) {
381 $subject = _("(no subject)");
382 $from = _("Unknown Sender");
391 $read = $read_list[$msgi];
394 foreach ($read as $read_part) {
395 //unfold multi-line headers
396 while ($prevline && strspn($read_part, "\t ") > 0) {
397 $read_part = substr($prevline, 0, -2) . ' ' . ltrim($read_part);
399 $prevline = $read_part;
400 if (eregi ("^to:(.*)$", $read_part, $regs)) {
402 } else if (eregi ("^from:(.*)$", $read_part, $regs)) {
404 } else if (eregi ("^x-priority:(.*)$", $read_part, $regs)) {
405 $priority = trim($regs[1]);
406 } else if (eregi ("^message-id:(.*)$", $read_part, $regs)) {
407 $messageid = trim($regs[1]);
408 } else if (eregi ("^cc:(.*)$", $read_part, $regs)) {
410 } else if (eregi ("^date:(.*)$", $read_part, $regs)) {
412 } else if (eregi ("^subject:(.*)$", $read_part, $regs)) {
413 $subject = htmlspecialchars(trim($regs[1]));
414 if ($subject == "") {
415 $subject = _("(no subject)");
417 } else if (eregi ("^content-type:(.*)$", $read_part, $regs)) {
418 $type = strtolower(trim($regs[1]));
419 if ($pos = strpos($type, ";")) {
420 $type = substr($type, 0, $pos);
422 $type = explode("/", $type);
423 if (!isset($type[1])) {
426 } else if (eregi ("^in-reply-to:(.*)$", $read_part, $regs)) {
427 $inrepto = trim($regs[1]);
430 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
431 if (trim($date) == "" ||
$internaldate) {
432 fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
433 $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
434 if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
435 $date_list = explode(' ', trim($regs[1]));
436 $date_list[0] = str_replace("-", ' ', $date_list[0]);
437 $date = implode(' ', $date_list);
440 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
443 $header = new small_header
;
445 $header->from
= (trim($to) != '' ?
$to : '(' ._("No To Address") . ')');
447 $header->from
= $from;
450 $header->date
= $date;
451 $header->subject
= $subject;
453 $header->priority
= $priority;
454 $header->message_id
= $messageid;
456 $header->size
= $size;
457 $header->type0
= $type[0];
458 $header->type1
= $type[1];
459 $header->inrepto
= $inrepto;
465 /* Returns the flags for the specified messages */
466 function sqimap_get_flags ($imap_stream, $i) {
467 $read = sqimap_run_command ($imap_stream, "FETCH $i:$i FLAGS", true, $response, $message);
468 if (ereg('FLAGS(.*)', $read[0], $regs)) {
469 return explode(' ', trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
471 return array('None');
474 function sqimap_get_flags_list ($imap_stream, $msg_list) {
475 $msgs_str = sqimap_message_list_squisher($msg_list);
476 for ($i = 0; $i < sizeof($msg_list); $i++
) {
477 $id2index[$msg_list[$i]] = $i;
479 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
480 $result_flags = array();
482 for ($i = 0; $i < sizeof($result_list); $i++
) {
483 if (eregi('^\* ([0-9]+).*FETCH.*FLAGS(.*)', $result_list[$i][0], $regs)
484 && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
485 $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
487 set_up_language($squirrelmail_language);
488 echo "<br><b><font color=$color[2]>\n" .
489 _("ERROR : Could not complete request.") .
491 _("Unknown response from IMAP server: ") .
492 $result_list[$i][0] . "</font><br>\n";
496 arsort($result_flags);
497 return $result_flags;
501 * Returns a message array with all the information about a message.
502 * See the documentation folder for more information about this array.
504 function sqimap_get_message ($imap_stream, $id, $mailbox) {
505 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
506 return sqimap_get_message_body($imap_stream, $header);
509 /* Wrapper function that reformats the header information. */
510 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
511 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
512 $header = sqimap_get_header($imap_stream, $read);
514 $header->mailbox
= $mailbox;
518 /* Wrapper function that reformats the entity header information. */
519 function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
520 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.HEADER]", true, $response, $message);
521 $header = sqimap_get_header($imap_stream, $read);
523 $header->mailbox
= $mailbox;
528 /* Wrapper function that returns entity headers for use by decodeMime */
530 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
531 $header = sqimap_get_header($imap_stream, $read);
532 $type0 = $header["TYPE0"];
533 $type1 = $header["TYPE1"];
534 $bound = $header["BOUNDARY"];
535 $encoding = $header["ENCODING"];
536 $charset = $header["CHARSET"];
537 $filename = $header["FILENAME"];
540 /* Queries the IMAP server and gets all header information. */
541 function sqimap_get_header ($imap_stream, $read) {
542 global $where, $what;
544 $hdr = new msg_header();
547 /* Set up some defaults */
548 $hdr->type0
= "text";
549 $hdr->type1
= "plain";
550 $hdr->charset
= "us-ascii";
552 while ($i < count($read)) {
553 //unfold multi-line headers
554 while ($i +
1 < count($read) && strspn($read[$i +
1], "\t ") > 0) {
555 $read[$i +
1] = substr($read[$i], 0, -2) . ' ' . ltrim($read[$i +
1]);
556 array_splice($read, $i, 1);
559 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
564 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
565 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
569 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
570 $cont = strtolower(trim(substr($read[$i], 13)));
571 if (strpos($cont, ";")) {
572 $cont = substr($cont, 0, strpos($cont, ";"));
575 if (strpos($cont, "/")) {
576 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
577 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
584 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
585 str_replace("\n", "", $line);
586 str_replace("\n", "", $read[$i]);
587 $line = "$line $read[$i]";
591 /* Detect the boundary of a multipart message */
592 if (eregi('boundary="([^"]+)"', $line, $regs)) {
593 $hdr->boundary
= $regs[1];
596 /* Detect the charset */
597 if (strpos(strtolower(trim($line)), "charset=")) {
598 $pos = strpos($line, "charset=") +
8;
599 $charset = trim($line);
600 if (strpos($line, ";", $pos) > 0) {
601 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
603 $charset = substr($charset, $pos);
605 $charset = str_replace("\"", "", $charset);
606 $hdr->charset
= $charset;
608 $hdr->charset
= "us-ascii";
610 /* Detect type in case of multipart/related */
611 if (strpos(strtolower(trim($line)), "type=")) {
612 $pos = strpos($line, "type=") +
6;
614 if (strpos($line, ";", $pos) > 0) {
615 $type = substr($type, $pos, strpos($line, ";", $pos)-$pos);
617 $type = substr($charset, $pos);
619 $type = str_replace("\"",'',$type);
623 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
624 /* Add better content-disposition support */
627 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
628 str_replace("\n", "", $line);
629 str_replace("\n", "", $read[$i]);
630 $line = "$line $read[$i]";
634 /* Detects filename if any */
635 if (strpos(strtolower(trim($line)), "filename=")) {
636 $pos = strpos($line, "filename=") +
9;
638 if (strpos($line, " ", $pos) > 0) {
639 $name = substr($name, $pos, strpos($line, " ", $pos));
641 $name = substr($name, $pos);
643 $name = str_replace("\"", "", $name);
644 $hdr->filename
= $name;
648 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
649 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
653 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
654 $hdr->from
= trim(substr($read[$i], 5, strlen($read[$i]) - 6));
655 if (! isset($hdr->replyto
) ||
$hdr->replyto
== "") {
656 $hdr->replyto
= $hdr->from
;
661 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
662 $d = substr($read[$i], 5);
664 $d = strtr($d, array(' ' => ' '));
665 $d = explode(' ', $d);
666 $hdr->date
= getTimeStamp($d);
670 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
671 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
672 if (strlen(Chop($hdr->subject
)) == 0) {
673 $hdr->subject
= _("(no subject)");
676 if ($where == 'SUBJECT') {
677 $hdr->subject = $what;
678 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
684 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
686 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
688 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
690 $hdr->cc
[$pos] = trim($read[$i]);
695 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
697 $hdr->bcc
[$pos] = trim(substr($read[$i], 5));
699 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
701 $hdr->bcc
[$pos] = trim($read[$i]);
706 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
708 $hdr->to
[$pos] = trim(substr($read[$i], 4));
710 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
712 $hdr->to
[$pos] = trim($read[$i]);
717 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
718 $hdr->message_id
= trim(substr($read[$i], 11));
721 /* ERROR CORRECTION */
722 else if (substr($read[$i], 0, 1) == ")") {
723 if (strlen(trim($hdr->subject
)) == 0) {
724 $hdr->subject
= _("(no subject)");
726 if (strlen(trim($hdr->from
)) == 0) {
727 $hdr->from
= _("(unknown sender)");
729 if (strlen(trim($hdr->date
)) == 0) {
735 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
736 $hdr->priority
= trim(substr($read[$i], 11));
746 /* Returns the body of a message. */
747 function sqimap_get_message_body ($imap_stream, &$header) {
749 return decodeMime($imap_stream, $header);