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.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 ();
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;
155 for ($i=0;$i<count($thread_new);$i++
) {
156 $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]);
157 $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]);
158 $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY
);
160 $indent_array = array();
162 $thread_new = array();
164 for ($i=0;$i<count($thread_new);$i++
) {
165 if (isset($thread_new[$i][0])) {
166 if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) {
170 $indent_array[$parent] = 0;
175 for ($k=1;$k<(count($thread_new[$i]))-1;$k++
) {
176 $chars = count_chars($thread_new[$i][$k], 1);
177 if (isset($chars['40']) && isset($chars['41'])) {
180 if (isset($chars['40'])) { // (
181 $indent = $indent +
$chars[40];
185 if (isset($chars['41'])) { // )
186 if ($go == 'start') {
187 if (!isset($spaces[$l])) {
190 $indent = $indent - $spaces[$l];
191 $indent = $indent - $chars[41] ;
196 $indent = $indent - $chars[41];
199 if (isset($chars['32'])) { // space
200 $indent = $indent +
$chars[32];
201 if ($go == 'start') {
202 if (!isset($spaces[$l])) {
205 $spaces[$l] = $spaces[$l] +
$chars[32];
208 if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
211 $indent_array[$child] = abs($indent);
214 return $indent_array;
218 /* returns an array with each element as a string
219 representing one message thread as returned by
223 function get_thread_sort ($imap_stream) {
224 global $thread_new, $sort_by_ref, $default_charset, $server_sort_array;
226 if (session_is_registered('thread_new')) {
227 session_unregister('thread_new');
229 if (session_is_registered('server_sort_array')) {
230 session_unregister('server_srot_array');
232 $sid = sqimap_session_id();
233 $thread_temp = array ();
234 if ($sort_by_ref == 1) {
235 $sort_type = 'REFERENCES';
238 $sort_type = 'ORDEREDSUBJECT';
240 $thread_query = "$sid THREAD $sort_type ".strtoupper($default_charset)." ALL\r\n";
241 fputs($imap_stream, $thread_query);
242 $thread_test = sqimap_read_data($imap_stream, $sid, false, $response, $message);
243 if (preg_match("/^\* THREAD (.+)$/", $thread_test[0], $regs)) {
244 $thread_list = trim($regs[1]);
249 if (!preg_match("/OK/", $response)) {
250 $server_sort_array = 'no';
251 return $server_sort_array;
253 $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
254 $char_count = count($thread_temp);
256 $thread_new = array();
259 for ($i=0;$i<$char_count;$i++
) {
260 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
261 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
263 elseif ($thread_temp[$i] == '(') {
264 $thread_new[$k] .= $thread_temp[$i];
267 elseif ($thread_temp[$i] == ')') {
269 $thread_new[$k] .= $thread_temp[$i];
270 $counter = $counter - 1;
273 $thread_new[$k] .= $thread_temp[$i];
275 $thread_new[$k] = "";
276 $counter = $counter - 1;
280 session_register('$thread_new');
281 $thread_new = array_reverse($thread_new);
282 $thread_list = implode(" ", $thread_new);
283 $thread_list = str_replace("(", " ", $thread_list);
284 $thread_list = str_replace(")", " ", $thread_list);
285 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
286 $server_sort_array = $thread_list;
287 session_register('server_sort_array');
293 function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
294 global $squirrelmail_language, $color, $data_dir, $username;
296 /* Get the small headers for each message in $msg_list */
297 $sid = sqimap_session_id();
298 $maxmsg = sizeof($msg_list);
299 $msgs_str = sqimap_message_list_squisher($msg_list);
301 $read_list = array();
302 $sizes_list = array();
304 * We need to return the data in the same order as the caller supplied
305 * in $msg_list, but IMAP servers are free to return responses in
306 * whatever order they wish... So we need to re-sort manually
308 for ($i = 0; $i < sizeof($msg_list); $i++
) {
309 $id2index[$msg_list[$i]] = $i;
312 $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";
313 fputs ($imap_stream, $query);
314 $readin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
316 foreach ($readin_list as $r) {
317 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
318 set_up_language($squirrelmail_language);
319 echo '<br><b><font color=$color[2]>' .
320 _("ERROR : Could not complete request.") .
322 _("Unknown response from IMAP server: ") . ' 1.' .
323 $r[0] . "</font><br>\n";
324 } else if (! isset($id2index[$regs[1]]) ||
!count($id2index[$regs[1]])) {
325 set_up_language($squirrelmail_language);
326 echo '<br><b><font color=$color[2]>' .
327 _("ERROR : Could not complete request.") .
329 _("Unknown message number in reply from server: ") .
330 $regs[1] . "</font><br>\n";
332 $read_list[$id2index[$regs[1]]] = $r;
337 $query = "$sid FETCH $msgs_str RFC822.SIZE\r\n";
338 fputs ($imap_stream, $query);
339 $sizesin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
341 foreach ($sizesin_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]>\n";
345 echo _("ERROR : Could not complete request.");
347 echo _("Unknown response from IMAP server: ") . ' 2.';
348 echo $r[0] . "</font><br>\n";
351 if (!count($id2index[$regs[1]])) {
352 set_up_language($squirrelmail_language);
353 echo "<br><b><font color=$color[2]>\n";
354 echo _("ERROR : Could not complete request.");
356 echo _("Unknown messagenumber in reply from server: ");
357 echo $regs[1] . "</font><br>\n";
360 $sizes_list[$id2index[$regs[1]]] = $r;
364 for ($msgi = 0; $msgi < $maxmsg; $msgi++
) {
365 $subject = _("(no subject)");
366 $from = _("Unknown Sender");
375 $read = $read_list[$msgi];
378 foreach ($read as $read_part) {
379 //unfold multi-line headers
380 while ($prevline && strspn($read_part, "\t ") > 0) {
381 $read_part = substr($prevline, 0, -2) . ' ' . ltrim($read_part);
383 $prevline = $read_part;
384 if (eregi ("^to:(.*)$", $read_part, $regs)) {
386 } else if (eregi ("^from:(.*)$", $read_part, $regs)) {
388 } else if (eregi ("^x-priority:(.*)$", $read_part, $regs)) {
389 $priority = trim($regs[1]);
390 } else if (eregi ("^message-id:(.*)$", $read_part, $regs)) {
391 $messageid = trim($regs[1]);
392 } else if (eregi ("^cc:(.*)$", $read_part, $regs)) {
394 } else if (eregi ("^date:(.*)$", $read_part, $regs)) {
396 } else if (eregi ("^subject:(.*)$", $read_part, $regs)) {
397 $subject = htmlspecialchars(trim($regs[1]));
398 if ($subject == "") {
399 $subject = _("(no subject)");
401 } else if (eregi ("^content-type:(.*)$", $read_part, $regs)) {
402 $type = strtolower(trim($regs[1]));
403 if ($pos = strpos($type, ";")) {
404 $type = substr($type, 0, $pos);
406 $type = explode("/", $type);
407 if (!isset($type[1])) {
410 } else if (eregi ("^in-reply-to:(.*)$", $read_part, $regs)) {
411 $inrepto = trim($regs[1]);
414 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
415 if (trim($date) == "" ||
$internaldate) {
416 fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
417 $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
418 if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
419 $date_list = explode(' ', trim($regs[1]));
420 $date_list[0] = str_replace("-", ' ', $date_list[0]);
421 $date = implode(' ', $date_list);
424 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
427 $header = new small_header
;
429 $header->from
= (trim($to) != '' ?
$to : '(' ._("No To Address") . ')');
431 $header->from
= $from;
434 $header->date
= $date;
435 $header->subject
= $subject;
437 $header->priority
= $priority;
438 $header->message_id
= $messageid;
440 $header->size
= $size;
441 $header->type0
= $type[0];
442 $header->type1
= $type[1];
443 $header->inrepto
= $inrepto;
449 /* Returns the flags for the specified messages */
450 function sqimap_get_flags ($imap_stream, $i) {
451 $read = sqimap_run_command ($imap_stream, "FETCH $i:$i FLAGS", true, $response, $message);
452 if (ereg('FLAGS(.*)', $read[0], $regs)) {
453 return explode(' ', trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
455 return array('None');
458 function sqimap_get_flags_list ($imap_stream, $msg_list) {
459 $msgs_str = sqimap_message_list_squisher($msg_list);
460 for ($i = 0; $i < sizeof($msg_list); $i++
) {
461 $id2index[$msg_list[$i]] = $i;
463 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
464 $result_flags = array();
466 for ($i = 0; $i < sizeof($result_list); $i++
) {
467 if (eregi('^\* ([0-9]+).*FETCH.*FLAGS(.*)', $result_list[$i][0], $regs)
468 && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
469 $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
471 set_up_language($squirrelmail_language);
472 echo "<br><b><font color=$color[2]>\n" .
473 _("ERROR : Could not complete request.") .
475 _("Unknown response from IMAP server: ") .
476 $result_list[$i][0] . "</font><br>\n";
480 arsort($result_flags);
481 return $result_flags;
485 * Returns a message array with all the information about a message.
486 * See the documentation folder for more information about this array.
488 function sqimap_get_message ($imap_stream, $id, $mailbox) {
489 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
490 return sqimap_get_message_body($imap_stream, $header);
493 /* Wrapper function that reformats the header information. */
494 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
495 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
496 $header = sqimap_get_header($imap_stream, $read);
498 $header->mailbox
= $mailbox;
502 /* Wrapper function that reformats the entity header information. */
503 function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
504 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.HEADER]", true, $response, $message);
505 $header = sqimap_get_header($imap_stream, $read);
507 $header->mailbox
= $mailbox;
512 /* Wrapper function that returns entity headers for use by decodeMime */
514 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
515 $header = sqimap_get_header($imap_stream, $read);
516 $type0 = $header["TYPE0"];
517 $type1 = $header["TYPE1"];
518 $bound = $header["BOUNDARY"];
519 $encoding = $header["ENCODING"];
520 $charset = $header["CHARSET"];
521 $filename = $header["FILENAME"];
524 /* Queries the IMAP server and gets all header information. */
525 function sqimap_get_header ($imap_stream, $read) {
526 global $where, $what;
528 $hdr = new msg_header();
531 /* Set up some defaults */
532 $hdr->type0
= "text";
533 $hdr->type1
= "plain";
534 $hdr->charset
= "us-ascii";
536 while ($i < count($read)) {
537 //unfold multi-line headers
538 while ($i +
1 < count($read) && strspn($read[$i +
1], "\t ") > 0) {
539 $read[$i +
1] = substr($read[$i], 0, -2) . ' ' . ltrim($read[$i +
1]);
540 array_splice($read, $i, 1);
543 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
548 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
549 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
553 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
554 $cont = strtolower(trim(substr($read[$i], 13)));
555 if (strpos($cont, ";")) {
556 $cont = substr($cont, 0, strpos($cont, ";"));
559 if (strpos($cont, "/")) {
560 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
561 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
568 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
569 str_replace("\n", "", $line);
570 str_replace("\n", "", $read[$i]);
571 $line = "$line $read[$i]";
575 /* Detect the boundary of a multipart message */
576 if (eregi('boundary="([^"]+)"', $line, $regs)) {
577 $hdr->boundary
= $regs[1];
580 /* Detect the charset */
581 if (strpos(strtolower(trim($line)), "charset=")) {
582 $pos = strpos($line, "charset=") +
8;
583 $charset = trim($line);
584 if (strpos($line, ";", $pos) > 0) {
585 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
587 $charset = substr($charset, $pos);
589 $charset = str_replace("\"", "", $charset);
590 $hdr->charset
= $charset;
592 $hdr->charset
= "us-ascii";
595 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
596 /* Add better content-disposition support */
599 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
600 str_replace("\n", "", $line);
601 str_replace("\n", "", $read[$i]);
602 $line = "$line $read[$i]";
606 /* Detects filename if any */
607 if (strpos(strtolower(trim($line)), "filename=")) {
608 $pos = strpos($line, "filename=") +
9;
610 if (strpos($line, " ", $pos) > 0) {
611 $name = substr($name, $pos, strpos($line, " ", $pos));
613 $name = substr($name, $pos);
615 $name = str_replace("\"", "", $name);
616 $hdr->filename
= $name;
620 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
621 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
625 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
626 $hdr->from
= trim(substr($read[$i], 5, strlen($read[$i]) - 6));
627 if (! isset($hdr->replyto
) ||
$hdr->replyto
== "") {
628 $hdr->replyto
= $hdr->from
;
633 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
634 $d = substr($read[$i], 5);
636 $d = strtr($d, array(' ' => ' '));
637 $d = explode(' ', $d);
638 $hdr->date
= getTimeStamp($d);
642 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
643 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
644 if (strlen(Chop($hdr->subject
)) == 0) {
645 $hdr->subject
= _("(no subject)");
648 if ($where == 'SUBJECT') {
649 $hdr->subject = $what;
650 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
656 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
658 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
660 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
662 $hdr->cc
[$pos] = trim($read[$i]);
667 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
669 $hdr->bcc
[$pos] = trim(substr($read[$i], 5));
671 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
673 $hdr->bcc
[$pos] = trim($read[$i]);
678 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
680 $hdr->to
[$pos] = trim(substr($read[$i], 4));
682 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
684 $hdr->to
[$pos] = trim($read[$i]);
689 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
690 $hdr->message_id
= trim(substr($read[$i], 11));
693 /* ERROR CORRECTION */
694 else if (substr($read[$i], 0, 1) == ")") {
695 if (strlen(trim($hdr->subject
)) == 0) {
696 $hdr->subject
= _("(no subject)");
698 if (strlen(trim($hdr->from
)) == 0) {
699 $hdr->from
= _("(unknown sender)");
701 if (strlen(trim($hdr->date
)) == 0) {
707 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
708 $hdr->priority
= trim(substr($read[$i], 11));
718 /* Returns the body of a message. */
719 function sqimap_get_message_body ($imap_stream, &$header) {
721 return decodeMime($imap_stream, $header);