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, $internal_date_sort, $server_sort_array;
101 if (session_is_registered('server_sort_array')) {
102 session_unregister('server_sort_array');
104 $sid = sqimap_session_id();
107 $server_sort_array = array();
108 $sort_test = array();
110 $sort_on = array (0=> 'DATE',
117 if ($internal_date_sort == true) {
118 $sort_on[0] = 'ARRIVAL';
119 $sort_on[1] = 'ARRIVAL';
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);
126 if (preg_match("/^\* SORT (.+)$/", $sort_test[0], $regs)) {
127 $server_sort_array = preg_split("/ /", trim($regs[1]));
129 if ($sort == 0 ||
$sort == 2 ||
$sort == 4) {
130 $server_sort_array = array_reverse($server_sort_array);
132 session_register('server_sort_array');
133 return $server_sort_array;
136 /* returns an indent array for printMessageinfo()
137 this represents the amount of indent needed
138 for this message number
141 function get_parent_level ($imap_stream) {
142 global $sort_by_ref, $default_charset, $thread_new;
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
);
150 $indent_array = array();
152 $thread_new = array();
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)) {
160 $indent_array[$parent] = 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'])) {
170 if (isset($chars['40'])) { // (
171 $indent = $indent +
$chars[40];
175 if (isset($chars['41'])) { // )
176 if ($go == 'start') {
177 if (!isset($spaces[$l])) {
180 $indent = $indent - $spaces[$l];
181 $indent = $indent - $chars[41] ;
186 $indent = $indent - $chars[41];
189 if (isset($chars['32'])) { // space
190 $indent = $indent +
$chars[32];
191 if ($go == 'start') {
192 if (!isset($spaces[$l])) {
195 $spaces[$l] = $spaces[$l] +
$chars[32];
198 if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
201 $indent_array[$child] = abs($indent);
204 return $indent_array;
208 /* returns an array with each element as a string
209 representing one message thread as returned by
213 function get_thread_sort ($imap_stream) {
214 global $thread_new, $sort_by_ref, $default_charset, $server_sort_array;
216 if (session_is_registered('thread_new')) {
217 session_unregister('thread_new');
219 if (session_is_registered('server_sort_array')) {
220 session_unregister('server_srot_array');
222 $sid = sqimap_session_id();
223 $thread_temp = array ();
224 if ($sort_by_ref == 1) {
225 $sort_type = 'REFERENCES';
228 $sort_type = 'ORDEREDSUBJECT';
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]);
239 $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
240 $char_count = count($thread_temp);
242 $thread_new = array();
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];
249 elseif ($thread_temp[$i] == '(') {
250 $thread_new[$k] .= $thread_temp[$i];
253 elseif ($thread_temp[$i] == ')') {
255 $thread_new[$k] .= $thread_temp[$i];
256 $counter = $counter - 1;
259 $thread_new[$k] .= $thread_temp[$i];
261 $thread_new[$k] = "";
262 $counter = $counter - 1;
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');
279 function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
280 global $squirrelmail_language, $color, $data_dir, $username;
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);
287 $read_list = array();
288 $sizes_list = array();
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
294 for ($i = 0; $i < sizeof($msg_list); $i++
) {
295 $id2index[$msg_list[$i]] = $i;
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);
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.") .
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.") .
315 _("Unknown message number in reply from server: ") .
316 $regs[1] . "</font><br>\n";
318 $read_list[$id2index[$regs[1]]] = $r;
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);
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.");
333 echo _("Unknown response from IMAP server: ") . ' 2.';
334 echo $r[0] . "</font><br>\n";
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.");
342 echo _("Unknown messagenumber in reply from server: ");
343 echo $regs[1] . "</font><br>\n";
346 $sizes_list[$id2index[$regs[1]]] = $r;
350 for ($msgi = 0; $msgi < $maxmsg; $msgi++
) {
351 $subject = _("(no subject)");
352 $from = _("Unknown Sender");
361 $read = $read_list[$msgi];
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);
369 $prevline = $read_part;
370 if (eregi ("^to:(.*)$", $read_part, $regs)) {
372 } else if (eregi ("^from:(.*)$", $read_part, $regs)) {
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)) {
380 } else if (eregi ("^date:(.*)$", $read_part, $regs)) {
382 } else if (eregi ("^subject:(.*)$", $read_part, $regs)) {
383 $subject = htmlspecialchars(trim($regs[1]));
384 if ($subject == "") {
385 $subject = _("(no subject)");
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);
392 $type = explode("/", $type);
393 if (!isset($type[1])) {
396 } else if (eregi ("^in-reply-to:(.*)$", $read_part, $regs)) {
397 $inrepto = trim($regs[1]);
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);
410 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
413 $header = new small_header
;
415 $header->from
= (trim($to) != '' ?
$to : '(' ._("No To Address") . ')');
417 $header->from
= $from;
420 $header->date
= $date;
421 $header->subject
= $subject;
423 $header->priority
= $priority;
424 $header->message_id
= $messageid;
426 $header->size
= $size;
427 $header->type0
= $type[0];
428 $header->type1
= $type[1];
429 $header->inrepto
= $inrepto;
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])));
441 return array('None');
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;
449 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
450 $result_flags = array();
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])));
457 set_up_language($squirrelmail_language);
458 echo "<br><b><font color=$color[2]>\n" .
459 _("ERROR : Could not complete request.") .
461 _("Unknown response from IMAP server: ") .
462 $result_list[$i][0] . "</font><br>\n";
466 arsort($result_flags);
467 return $result_flags;
471 * Returns a message array with all the information about a message.
472 * See the documentation folder for more information about this array.
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);
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);
484 $header->mailbox
= $mailbox;
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);
493 $header->mailbox
= $mailbox;
498 /* Wrapper function that returns entity headers for use by decodeMime */
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"];
510 /* Queries the IMAP server and gets all header information. */
511 function sqimap_get_header ($imap_stream, $read) {
512 global $where, $what;
514 $hdr = new msg_header();
517 /* Set up some defaults */
518 $hdr->type0
= "text";
519 $hdr->type1
= "plain";
520 $hdr->charset
= "us-ascii";
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);
529 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
534 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
535 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
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, ";"));
545 if (strpos($cont, "/")) {
546 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
547 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
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]";
561 /* Detect the boundary of a multipart message */
562 if (eregi('boundary="([^"]+)"', $line, $regs)) {
563 $hdr->boundary
= $regs[1];
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);
573 $charset = substr($charset, $pos);
575 $charset = str_replace("\"", "", $charset);
576 $hdr->charset
= $charset;
578 $hdr->charset
= "us-ascii";
581 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
582 /* Add better content-disposition support */
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]";
592 /* Detects filename if any */
593 if (strpos(strtolower(trim($line)), "filename=")) {
594 $pos = strpos($line, "filename=") +
9;
596 if (strpos($line, " ", $pos) > 0) {
597 $name = substr($name, $pos, strpos($line, " ", $pos));
599 $name = substr($name, $pos);
601 $name = str_replace("\"", "", $name);
602 $hdr->filename
= $name;
606 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
607 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
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
;
619 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
620 $d = substr($read[$i], 5);
622 $d = strtr($d, array(' ' => ' '));
623 $d = explode(' ', $d);
624 $hdr->date
= getTimeStamp($d);
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)");
634 if ($where == 'SUBJECT') {
635 $hdr->subject = $what;
636 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
642 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
644 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
646 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
648 $hdr->cc
[$pos] = trim($read[$i]);
653 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
655 $hdr->bcc
[$pos] = trim(substr($read[$i], 5));
657 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
659 $hdr->bcc
[$pos] = trim($read[$i]);
664 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
666 $hdr->to
[$pos] = trim(substr($read[$i], 4));
668 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
670 $hdr->to
[$pos] = trim($read[$i]);
675 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
676 $hdr->message_id
= trim(substr($read[$i], 11));
679 /* ERROR CORRECTION */
680 else if (substr($read[$i], 0, 1) == ")") {
681 if (strlen(trim($hdr->subject
)) == 0) {
682 $hdr->subject
= _("(no subject)");
684 if (strlen(trim($hdr->from
)) == 0) {
685 $hdr->from
= _("(unknown sender)");
687 if (strlen(trim($hdr->date
)) == 0) {
693 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
694 $hdr->priority
= trim(substr($read[$i], 11));
704 /* Returns the body of a message. */
705 function sqimap_get_message_body ($imap_stream, &$header) {
707 return decodeMime($imap_stream, $header);