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 ();
93 /* returns an indent array for printMessageinfo()
94 this represents the amount of indent needed
95 for this message number
98 function get_parent_level ($imap_stream) {
99 global $sort_by_ref, $default_charset, $thread_new;
102 for ($i=0;$i<count($thread_new);$i++
) {
103 $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]);
104 $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]);
105 $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY
);
107 $indent_array = array();
109 $thread_new = array();
111 for ($i=0;$i<count($thread_new);$i++
) {
112 if (isset($thread_new[$i][0])) {
113 if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) {
117 $indent_array[$parent] = 0;
122 for ($k=1;$k<(count($thread_new[$i]))-1;$k++
) {
123 $chars = count_chars($thread_new[$i][$k], 1);
124 if (isset($chars['40']) && isset($chars['41'])) {
127 if (isset($chars['40'])) { // (
128 $indent = $indent +
$chars[40];
132 if (isset($chars['41'])) { // )
133 if ($go == 'start') {
134 if (!isset($spaces[$l])) {
137 $indent = $indent - $spaces[$l];
138 $indent = $indent - $chars[41] ;
143 $indent = $indent - $chars[41];
146 if (isset($chars['32'])) { // space
147 $indent = $indent +
$chars[32];
148 if ($go == 'start') {
149 if (!isset($spaces[$l])) {
152 $spaces[$l] = $spaces[$l] +
$chars[32];
155 if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
158 $indent_array[$child] = abs($indent);
161 return $indent_array;
165 /* returns an array with each element as a string
166 representing one message thread as returned by
170 function get_thread_sort ($imap_stream) {
171 global $thread_new, $sort_by_ref, $default_charset;
173 if (session_register('thread_new')) {
174 session_unregister('thread_new');
176 $sid = sqimap_session_id();
177 $thread_temp = array ();
178 if ($sort_by_ref == 1) {
179 $sort_type = 'REFERENCES';
182 $sort_type = 'ORDEREDSUBJECT';
184 $thread_query = "$sid THREAD $sort_type $default_charset ALL\r\n";
185 fputs($imap_stream, $thread_query);
186 $thread_test = sqimap_read_data($imap_stream, $sid, true, $response, $message);
187 if (preg_match("/^\* THREAD (.+)$/", $thread_test[0], $regs)) {
188 $thread_list = trim($regs[1]);
193 $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
194 $char_count = count($thread_temp);
196 $thread_new = array();
199 for ($i=0;$i<$char_count;$i++
) {
200 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
201 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
203 elseif ($thread_temp[$i] == '(') {
204 $thread_new[$k] .= $thread_temp[$i];
207 elseif ($thread_temp[$i] == ')') {
209 $thread_new[$k] .= $thread_temp[$i];
210 $counter = $counter - 1;
213 $thread_new[$k] .= $thread_temp[$i];
215 $thread_new[$k] = "";
216 $counter = $counter - 1;
220 session_register('$thread_new');
221 $thread_new = array_reverse($thread_new);
222 $thread_list = implode(" ", $thread_new);
223 $thread_list = str_replace("(", " ", $thread_list);
224 $thread_list = str_replace(")", " ", $thread_list);
225 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
232 function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
233 global $squirrelmail_language, $color, $data_dir, $username;
235 /* Get the small headers for each message in $msg_list */
236 $sid = sqimap_session_id();
237 $maxmsg = sizeof($msg_list);
238 $msgs_str = sqimap_message_list_squisher($msg_list);
240 $read_list = array();
241 $sizes_list = array();
243 * We need to return the data in the same order as the caller supplied
244 * in $msg_list, but IMAP servers are free to return responses in
245 * whatever order they wish... So we need to re-sort manually
247 for ($i = 0; $i < sizeof($msg_list); $i++
) {
248 $id2index[$msg_list[$i]] = $i;
251 $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";
252 fputs ($imap_stream, $query);
253 $readin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
255 foreach ($readin_list as $r) {
256 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
257 set_up_language($squirrelmail_language);
258 echo '<br><b><font color=$color[2]>' .
259 _("ERROR : Could not complete request.") .
261 _("Unknown response from IMAP server: ") . ' 1.' .
262 $r[0] . "</font><br>\n";
263 } else if (! isset($id2index[$regs[1]]) ||
!count($id2index[$regs[1]])) {
264 set_up_language($squirrelmail_language);
265 echo '<br><b><font color=$color[2]>' .
266 _("ERROR : Could not complete request.") .
268 _("Unknown message number in reply from server: ") .
269 $regs[1] . "</font><br>\n";
271 $read_list[$id2index[$regs[1]]] = $r;
276 $query = "$sid FETCH $msgs_str RFC822.SIZE\r\n";
277 fputs ($imap_stream, $query);
278 $sizesin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
280 foreach ($sizesin_list as $r) {
281 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
282 set_up_language($squirrelmail_language);
283 echo "<br><b><font color=$color[2]>\n";
284 echo _("ERROR : Could not complete request.");
286 echo _("Unknown response from IMAP server: ") . ' 2.';
287 echo $r[0] . "</font><br>\n";
290 if (!count($id2index[$regs[1]])) {
291 set_up_language($squirrelmail_language);
292 echo "<br><b><font color=$color[2]>\n";
293 echo _("ERROR : Could not complete request.");
295 echo _("Unknown messagenumber in reply from server: ");
296 echo $regs[1] . "</font><br>\n";
299 $sizes_list[$id2index[$regs[1]]] = $r;
303 for ($msgi = 0; $msgi < $maxmsg; $msgi++
) {
304 $subject = _("(no subject)");
305 $from = _("Unknown Sender");
314 $read = $read_list[$msgi];
317 foreach ($read as $read_part) {
318 //unfold multi-line headers
319 while ($prevline && strspn($read_part, "\t ") > 0) {
320 $read_part = substr($prevline, 0, -2) . ' ' . ltrim($read_part);
322 $prevline = $read_part;
323 if (eregi ("^to:(.*)$", $read_part, $regs)) {
325 } else if (eregi ("^from:(.*)$", $read_part, $regs)) {
327 } else if (eregi ("^x-priority:(.*)$", $read_part, $regs)) {
328 $priority = trim($regs[1]);
329 } else if (eregi ("^message-id:(.*)$", $read_part, $regs)) {
330 $messageid = trim($regs[1]);
331 } else if (eregi ("^cc:(.*)$", $read_part, $regs)) {
333 } else if (eregi ("^date:(.*)$", $read_part, $regs)) {
335 } else if (eregi ("^subject:(.*)$", $read_part, $regs)) {
336 $subject = htmlspecialchars(trim($regs[1]));
337 if ($subject == "") {
338 $subject = _("(no subject)");
340 } else if (eregi ("^content-type:(.*)$", $read_part, $regs)) {
341 $type = strtolower(trim($regs[1]));
342 if ($pos = strpos($type, ";")) {
343 $type = substr($type, 0, $pos);
345 $type = explode("/", $type);
346 if (!isset($type[1])) {
349 } else if (eregi ("^in-reply-to:(.*)$", $read_part, $regs)) {
350 $inrepto = trim($regs[1]);
353 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
354 if (trim($date) == "" ||
$internaldate) {
355 fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
356 $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
357 if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
358 $date_list = explode(' ', trim($regs[1]));
359 $date_list[0] = str_replace("-", ' ', $date_list[0]);
360 $date = implode(' ', $date_list);
363 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
366 $header = new small_header
;
368 $header->from
= (trim($to) != '' ?
$to : '(' ._("No To Address") . ')');
370 $header->from
= $from;
373 $header->date
= $date;
374 $header->subject
= $subject;
376 $header->priority
= $priority;
377 $header->message_id
= $messageid;
379 $header->size
= $size;
380 $header->type0
= $type[0];
381 $header->type1
= $type[1];
382 $header->inrepto
= $inrepto;
388 /* Returns the flags for the specified messages */
389 function sqimap_get_flags ($imap_stream, $i) {
390 $read = sqimap_run_command ($imap_stream, "FETCH $i:$i FLAGS", true, $response, $message);
391 if (ereg('FLAGS(.*)', $read[0], $regs)) {
392 return explode(' ', trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
394 return array('None');
397 function sqimap_get_flags_list ($imap_stream, $msg_list) {
398 $msgs_str = sqimap_message_list_squisher($msg_list);
399 for ($i = 0; $i < sizeof($msg_list); $i++
) {
400 $id2index[$msg_list[$i]] = $i;
402 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
403 $result_flags = array();
405 for ($i = 0; $i < sizeof($result_list); $i++
) {
406 if (eregi('^\* ([0-9]+).*FETCH.*FLAGS(.*)', $result_list[$i][0], $regs)
407 && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
408 $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
410 set_up_language($squirrelmail_language);
411 echo "<br><b><font color=$color[2]>\n" .
412 _("ERROR : Could not complete request.") .
414 _("Unknown response from IMAP server: ") .
415 $result_list[$i][0] . "</font><br>\n";
419 arsort($result_flags);
420 return $result_flags;
424 * Returns a message array with all the information about a message.
425 * See the documentation folder for more information about this array.
427 function sqimap_get_message ($imap_stream, $id, $mailbox) {
428 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
429 return sqimap_get_message_body($imap_stream, $header);
432 /* Wrapper function that reformats the header information. */
433 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
434 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
435 $header = sqimap_get_header($imap_stream, $read);
437 $header->mailbox
= $mailbox;
441 /* Wrapper function that reformats the entity header information. */
442 function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
443 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.HEADER]", true, $response, $message);
444 $header = sqimap_get_header($imap_stream, $read);
446 $header->mailbox
= $mailbox;
451 /* Wrapper function that returns entity headers for use by decodeMime */
453 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
454 $header = sqimap_get_header($imap_stream, $read);
455 $type0 = $header["TYPE0"];
456 $type1 = $header["TYPE1"];
457 $bound = $header["BOUNDARY"];
458 $encoding = $header["ENCODING"];
459 $charset = $header["CHARSET"];
460 $filename = $header["FILENAME"];
463 /* Queries the IMAP server and gets all header information. */
464 function sqimap_get_header ($imap_stream, $read) {
465 global $where, $what;
467 $hdr = new msg_header();
470 /* Set up some defaults */
471 $hdr->type0
= "text";
472 $hdr->type1
= "plain";
473 $hdr->charset
= "us-ascii";
475 while ($i < count($read)) {
476 //unfold multi-line headers
477 while ($i +
1 < count($read) && strspn($read[$i +
1], "\t ") > 0) {
478 $read[$i +
1] = substr($read[$i], 0, -2) . ' ' . ltrim($read[$i +
1]);
479 array_splice($read, $i, 1);
482 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
487 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
488 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
492 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
493 $cont = strtolower(trim(substr($read[$i], 13)));
494 if (strpos($cont, ";")) {
495 $cont = substr($cont, 0, strpos($cont, ";"));
498 if (strpos($cont, "/")) {
499 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
500 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
507 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
508 str_replace("\n", "", $line);
509 str_replace("\n", "", $read[$i]);
510 $line = "$line $read[$i]";
514 /* Detect the boundary of a multipart message */
515 if (eregi('boundary="([^"]+)"', $line, $regs)) {
516 $hdr->boundary
= $regs[1];
519 /* Detect the charset */
520 if (strpos(strtolower(trim($line)), "charset=")) {
521 $pos = strpos($line, "charset=") +
8;
522 $charset = trim($line);
523 if (strpos($line, ";", $pos) > 0) {
524 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
526 $charset = substr($charset, $pos);
528 $charset = str_replace("\"", "", $charset);
529 $hdr->charset
= $charset;
531 $hdr->charset
= "us-ascii";
534 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
535 /* Add better content-disposition support */
538 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
539 str_replace("\n", "", $line);
540 str_replace("\n", "", $read[$i]);
541 $line = "$line $read[$i]";
545 /* Detects filename if any */
546 if (strpos(strtolower(trim($line)), "filename=")) {
547 $pos = strpos($line, "filename=") +
9;
549 if (strpos($line, " ", $pos) > 0) {
550 $name = substr($name, $pos, strpos($line, " ", $pos));
552 $name = substr($name, $pos);
554 $name = str_replace("\"", "", $name);
555 $hdr->filename
= $name;
559 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
560 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
564 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
565 $hdr->from
= trim(substr($read[$i], 5, strlen($read[$i]) - 6));
566 if (! isset($hdr->replyto
) ||
$hdr->replyto
== "") {
567 $hdr->replyto
= $hdr->from
;
572 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
573 $d = substr($read[$i], 5);
575 $d = strtr($d, array(' ' => ' '));
576 $d = explode(' ', $d);
577 $hdr->date
= getTimeStamp($d);
581 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
582 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
583 if (strlen(Chop($hdr->subject
)) == 0) {
584 $hdr->subject
= _("(no subject)");
587 if ($where == 'SUBJECT') {
588 $hdr->subject = $what;
589 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
595 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
597 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
599 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
601 $hdr->cc
[$pos] = trim($read[$i]);
606 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
608 $hdr->bcc
[$pos] = trim(substr($read[$i], 5));
610 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
612 $hdr->bcc
[$pos] = trim($read[$i]);
617 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
619 $hdr->to
[$pos] = trim(substr($read[$i], 4));
621 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
623 $hdr->to
[$pos] = trim($read[$i]);
628 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
629 $hdr->message_id
= trim(substr($read[$i], 11));
632 /* ERROR CORRECTION */
633 else if (substr($read[$i], 0, 1) == ")") {
634 if (strlen(trim($hdr->subject
)) == 0) {
635 $hdr->subject
= _("(no subject)");
637 if (strlen(trim($hdr->from
)) == 0) {
638 $hdr->from
= _("(unknown sender)");
640 if (strlen(trim($hdr->date
)) == 0) {
646 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
647 $hdr->priority
= trim(substr($read[$i], 11));
657 /* Returns the body of a message. */
658 function sqimap_get_message_body ($imap_stream, &$header) {
660 return decodeMime($imap_stream, $header);