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 */
16 function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
18 $read = sqimap_run_command ($imap_stream, "COPY $start:$end \"$mailbox\"", true, $response, $message, $uid_support);
21 function sqimap_msgs_list_copy ($imap_stream, $id, $mailbox) {
23 $msgs_id = sqimap_message_list_squisher($id);
24 $read = sqimap_run_command ($imap_stream, "COPY $msgs_id \"$mailbox\"", true, $response, $message, $uid_support);
25 $read = sqimap_run_command ($imap_stream, "STORE $msgs_id +FLAGS (\\Deleted)", true, $response, $message, $uid_support);
29 /* Deletes specified messages and moves them to trash if possible */
31 function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
32 global $move_to_trash, $trash_folder, $auto_expunge, $uid_support;
34 if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
35 sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
37 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted", true);
40 function sqimap_msgs_list_delete ($imap_stream, $mailbox, $id) {
41 global $move_to_trash, $trash_folder, $uid_support;
42 $msgs_id = sqimap_message_list_squisher($id);
43 if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
44 $read = sqimap_run_command ($imap_stream, "COPY $msgs_id \"$trash_folder\"", true, $response, $message, $uid_support);
46 $read = sqimap_run_command ($imap_stream, "STORE $msgs_id +FLAGS (\\Deleted)", true, $response, $message, $uid_support);
50 /* Sets the specified messages with specified flag */
51 function sqimap_messages_flag ($imap_stream, $start, $end, $flag, $handle_errors) {
53 $read = sqimap_run_command ($imap_stream, "STORE $start:$end +FLAGS (\\$flag)", $handle_errors, $response, $message, $uid_support);
56 /* Remove specified flag from specified messages */
57 function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag, $handle_errors) {
59 $read = sqimap_run_command ($imap_stream, "STORE $start:$end -FLAGS (\\$flag)", $handle_errors, $response, $message, $uid_support);
62 function sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors) {
64 $msgs_id = sqimap_message_list_squisher($id);
65 $set_string = ($set ?
'+' : '-');
66 $read = sqimap_run_command ($imap_stream, "STORE $msgs_id ".$set_string."FLAGS ($flag)", $handle_errors, $response, $message, $uid_support);
70 /* Returns some general header information -- FROM, DATE, and SUBJECT */
72 var $from = '', $subject = '', $date = '', $to = '',
73 $priority = 0, $message_id = 0, $cc = '', $uid = '';
76 function sqimap_get_small_header ($imap_stream, $id, $sent) {
77 $res = sqimap_get_small_header_list($imap_stream, array($id), $sent);
82 * Sort the message list and crunch to be as small as possible
83 * (overflow could happen, so make it small if possible)
85 function sqimap_message_list_squisher($messages_array) {
86 if( !is_array( $messages_array ) ) {
90 sort($messages_array, SORT_NUMERIC
);
92 while ($messages_array) {
93 $start = array_shift($messages_array);
95 while (isset($messages_array[0]) && $messages_array[0] == $end +
1) {
96 $end = array_shift($messages_array);
98 if ($msgs_str != '') {
102 if ($start != $end) {
103 $msgs_str .= ':' . $end;
109 /* returns the references header lines */
110 function get_reference_header ($imap_stream, $message) {
112 $responses = array ();
113 $sid = sqimap_session_id($uid_support);
116 $query = "$sid FETCH $message BODY[HEADER.FIELDS (References)]\r\n";
117 fputs ($imap_stream, $query);
118 $responses = sqimap_read_data_list($imap_stream, $sid, true, $responses, $message);
119 if (!eregi("^\\* ([0-9]+) FETCH", $responses[0][0], $regs)) {
120 $responses = array ();
126 /* get sort order from server and
127 * return it as the $id array for
131 function sqimap_get_sort_order ($imap_stream, $sort, $mbxresponse) {
132 global $default_charset, $thread_sort_messages,
133 $internal_date_sort, $server_sort_array,
134 $sent_folder, $mailbox, $uid_support;
136 if (sqsession_is_registered('server_sort_array')) {
137 sqsession_unregister('server_sort_array');
140 $sid = sqimap_session_id($uid_support);
143 $server_sort_array = array();
144 $sort_test = array();
149 if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) {
150 $uidnext = $mbxresponse['UIDNEXT']-1;
154 $uid_query = "$sid SEARCH UID 1:$uidnext\r\n";
155 fputs($imap_stream, $uid_query);
156 $uids = sqimap_read_data($imap_stream, $sid, true ,$response, $message);
157 if (isset($uids[0])) {
158 if (preg_match("/^\* SEARCH (.+)$/", $uids[0], $regs)) {
159 $server_sort_array = preg_split("/ /", trim($regs[1]));
162 if (!preg_match("/OK/", $response)) {
163 $server_sort_array = 'no';
166 $qty = $mbxresponse['EXISTS'];
167 $server_sort_array = range(1, $qty);
169 sqsession_register($server_sort_array, 'server_sort_array');
170 return $server_sort_array;
173 $sort_on = array (0=> 'DATE',
179 if ($internal_date_sort == true) {
180 $sort_on[0] = 'ARRIVAL';
181 $sort_on[1] = 'ARRIVAL';
183 if ($sent_folder == $mailbox) {
187 if (!empty($sort_on[$sort])) {
188 $sort_query = "$sid SORT ($sort_on[$sort]) ".strtoupper($default_charset)." ALL\r\n";
189 fputs($imap_stream, $sort_query);
190 $sort_test = sqimap_read_data($imap_stream, $sid, true ,$response, $message);
192 if (isset($sort_test[0])) {
193 if (preg_match("/^\* SORT (.+)$/", $sort_test[0], $regs)) {
194 $server_sort_array = preg_split("/ /", trim($regs[1]));
197 if ($sort == 0 ||
$sort == 2 ||
$sort == 4) {
198 $server_sort_array = array_reverse($server_sort_array);
200 if (!preg_match("/OK/", $response)) {
201 $server_sort_array = 'no';
203 sqsession_register($server_sort_array, 'server_sort_array');
204 return $server_sort_array;
208 function sqimap_get_php_sort_order ($imap_stream, $mbxresponse) {
211 if (sqsession_is_registered('php_sort_array')) {
212 sqsession_unregister('php_sort_array');
215 $sid = sqimap_session_id($uid_support);
216 $php_sort_array = array();
219 if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) {
220 $uidnext = $mbxresponse['UIDNEXT']-1;
224 $uid_query = "$sid SEARCH UID 1:$uidnext\r\n";
225 fputs($imap_stream, $uid_query);
226 $uids = sqimap_read_data($imap_stream, $sid, true ,$response, $message);
227 if (isset($uids[0])) {
228 if (preg_match("/^\* SEARCH (.+)$/", $uids[0], $regs)) {
229 $php_sort_array = preg_split("/ /", trim($regs[1]));
232 if (!preg_match("/OK/", $response)) {
233 $php_sort_array = 'no';
236 $qty = $mbxresponse['EXISTS'];
237 $php_sort_array = range(1, $qty);
239 sqsession_register($php_sort_array, 'php_sort_array');
240 return $php_sort_array;
244 /* returns an indent array for printMessageinfo()
245 this represents the amount of indent needed (value)
246 for this message number (key)
249 function get_parent_level ($imap_stream) {
250 global $sort_by_ref, $default_charset, $thread_new;
255 /* loop through the threads and take unwanted characters out
256 of the thread string then chop it up
258 for ($i=0;$i<count($thread_new);$i++
) {
259 $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]);
260 $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]);
261 $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY
);
263 $indent_array = array();
265 $thread_new = array();
267 /* looping through the parts of one message thread */
269 for ($i=0;$i<count($thread_new);$i++
) {
270 /* first grab the parent, it does not indent */
272 if (isset($thread_new[$i][0])) {
273 if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) {
277 $indent_array[$parent] = 0;
279 /* now the children, checking each thread portion for
280 ),(, and space, adjusting the level and space values
281 to get the indent level
288 for ($k=1;$k<(count($thread_new[$i]))-1;$k++
) {
289 $chars = count_chars($thread_new[$i][$k], 1);
290 if (isset($chars['40'])) { /* testing for ( */
291 $level = $level +
$chars['40'];
293 if (isset($chars['41'])) { /* testing for ) */
294 $level = $level - $chars['41'];
296 /* if we were faking lets stop, this portion
297 of the thread is over
299 if ($level == $cutoff) {
303 if (isset($chars['32'])) { /* testing for space */
304 if (!isset($spaces[$level])) {
307 $spaces[$level] = $spaces[$level] +
$chars['32'];
309 for ($x=0;$x<=$level;$x++
) {
310 if (isset($spaces[$x])) {
311 $spaces_total = $spaces_total +
$spaces[$x];
314 $indent = $level +
$spaces_total;
315 /* must have run into a message that broke the thread
316 so we are adjusting for that portion
319 $indent = $indent +
1;
321 if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
324 /* the thread must be broken if $indent == 0
325 so indent the message once and start faking it
332 /* dont need abs but if indent was negative
335 $indent_array[$child] = abs($indent);
339 return $indent_array;
343 /* returns an array with each element as a string
344 representing one message thread as returned by
348 function get_thread_sort ($imap_stream) {
349 global $thread_new, $sort_by_ref, $default_charset, $server_sort_array, $uid_support;
350 if (sqsession_is_registered('thread_new')) {
351 sqsession_unregister('thread_new');
353 if (sqsession_is_registered('server_sort_array')) {
354 sqsession_unregister('server_sort_array');
356 $sid = sqimap_session_id($uid_support);
357 $thread_temp = array ();
358 if ($sort_by_ref == 1) {
359 $sort_type = 'REFERENCES';
362 $sort_type = 'ORDEREDSUBJECT';
364 $thread_query = "$sid THREAD $sort_type ".strtoupper($default_charset)." ALL\r\n";
365 fputs($imap_stream, $thread_query);
366 $thread_test = sqimap_read_data($imap_stream, $sid, false, $response, $message);
367 if (isset($thread_test[0])) {
368 if (preg_match("/^\* THREAD (.+)$/", $thread_test[0], $regs)) {
369 $thread_list = trim($regs[1]);
375 if (!preg_match("/OK/", $response)) {
376 $server_sort_array = 'no';
377 return $server_sort_array;
379 if (isset($thread_list)) {
380 $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
382 $char_count = count($thread_temp);
384 $thread_new = array();
387 for ($i=0;$i<$char_count;$i++
) {
388 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
389 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
391 elseif ($thread_temp[$i] == '(') {
392 $thread_new[$k] .= $thread_temp[$i];
395 elseif ($thread_temp[$i] == ')') {
397 $thread_new[$k] .= $thread_temp[$i];
398 $counter = $counter - 1;
401 $thread_new[$k] .= $thread_temp[$i];
403 $thread_new[$k] = "";
404 $counter = $counter - 1;
408 sqsession_register($thread_new, 'thread_new');
409 $thread_new = array_reverse($thread_new);
410 $thread_list = implode(" ", $thread_new);
411 $thread_list = str_replace("(", " ", $thread_list);
412 $thread_list = str_replace(")", " ", $thread_list);
413 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
414 $server_sort_array = $thread_list;
415 sqsession_register($server_sort_array, 'server_sort_array');
420 function elapsedTime($start) {
421 $stop = gettimeofday();
422 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) +
$stop['usec'] - $start['usec'];
426 function sqimap_get_small_header_list ($imap_stream, $msg_list) {
427 global $squirrelmail_language, $color, $data_dir, $username, $imap_server_type;
430 /* Get the small headers for each message in $msg_list */
431 $sid = sqimap_session_id($uid_support);
433 $maxmsg = sizeof($msg_list);
434 $msgs_str = sqimap_message_list_squisher($msg_list);
436 $read_list = array();
438 * We need to return the data in the same order as the caller supplied
439 * in $msg_list, but IMAP servers are free to return responses in
440 * whatever order they wish... So we need to re-sort manually
442 for ($i = 0; $i < sizeof($msg_list); $i++
) {
443 $id2index[$msg_list[$i]] = $i;
446 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
448 $query = "$sid FETCH $msgs_str (FLAGS UID RFC822.SIZE INTERNALDATE BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject X-Priority Content-Type)])\r\n";
450 $query = "$sid FETCH $msgs_str (FLAGS UID RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject X-Priority Content-Type)])\r\n";
452 fputs ($imap_stream, $query);
453 $readin_list = sqimap_read_data_list($imap_stream, $sid, false, $response, $message);
455 foreach ($readin_list as $r) {
457 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH/iAU",$r[0], $regs)) {
458 set_up_language($squirrelmail_language);
459 echo '<br><b><font color=$color[2]>' .
460 _("ERROR : Could not complete request.") .
462 _("Unknown response from IMAP server: ") . ' 1.' .
463 $r[0] . "</font><br>\n";
464 } else if (! isset($id2index[$regs[1]]) ||
!count($id2index[$regs[1]])) {
465 set_up_language($squirrelmail_language);
466 echo '<br><b><font color=$color[2]>' .
467 _("ERROR : Could not complete request.") .
469 _("Unknown message number in reply from server: ") .
470 $regs[1] . "</font><br>\n";
472 $read_list[$id2index[$regs[1]]] = $r;
475 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH.*UID\s+([0-9]+)\s+/iAU",$r[0], $regs)) {
476 set_up_language($squirrelmail_language);
477 echo '<br><b><font color=$color[2]>' .
478 _("ERROR : Could not complete request.") .
480 _("Unknown response from IMAP server: ") . ' 1.' .
481 $r[0] . "</font><br>\n";
482 } else if (! isset($id2index[$regs[2]]) ||
!count($id2index[$regs[2]])) {
483 set_up_language($squirrelmail_language);
484 echo '<br><b><font color=$color[2]>' .
485 _("ERROR : Could not complete request.") .
487 _("Unknown message number in reply from server: ") .
488 $regs[2] . "</font><br>\n";
490 $read_list[$id2index[$regs[2]]] = $r;
491 $unique_id = $regs[2];
500 "/^X-Priority:(.*)\$/AUi",
503 "/^Subject:(.*)\$/AUi",
504 "/^Content-Type:(.*)\$/AUi"
508 for ($msgi = 0; $msgi < $maxmsg; $msgi++
) {
509 $subject = _("(no subject)");
510 $from = _("Unknown Sender");
520 $flag_answered = false;
521 $flag_deleted = false;
522 $flag_flagged = false;
523 $read = $read_list[$msgi];
526 foreach ($read as $read_part) {
527 //unfold multi-line headers
528 if ($prevline && strpos($read_part, "\t ") === true) {
529 $read_part = substr($prevline, 0, -2) . preg_replace('/(\t\s+)/',' ',$read_part);
531 $prevline = $read_part;
532 if ($read_part{0} == '*') {
534 if (preg_match ("/^.+INTERNALDATE\s+\"(.+)\".+/iUA",$read_part, $reg)) {
535 $tmpdate = trim($reg[1]);
536 $tmpdate = str_replace(' ',' ',$tmpdate);
537 $tmpdate = explode(' ',$tmpdate);
538 $date = str_replace('-',' ',$tmpdate[0]) . " " .
543 if (preg_match ("/^.+RFC822.SIZE\s+(\d+).+/iA",$read_part, $reg)) {
546 if (preg_match("/^.+FLAGS\s+\((.*)\).+/iUA", $read_part, $regs)) {
547 $flags = explode(' ',trim($regs[1]));
548 foreach ($flags as $flag) {
549 $flag = strtolower($flag);
550 if ($flag == '\\seen') {
552 } else if ($flag == '\\answered') {
553 $flag_answered = true;
554 } else if ($flag == '\\deleted') {
555 $flag_deleted = true;
556 } else if ($flag == '\\flagged') {
557 $flag_flagged = true;
561 if (preg_match ("/^.+UID\s+(\d+).+/iA",$read_part, $reg)) {
562 $unique_id = $reg[1];
565 $firstchar = strtoupper($read_part{0});
566 if ($firstchar == 'T') {
567 $regpattern = $patterns[0];
569 } else if ($firstchar == 'F') {
570 $regpattern = $patterns[1];
572 } else if ($firstchar == 'X') {
573 $regpattern = $patterns[2];
575 } else if ($firstchar == 'C') {
576 if (strtolower($read_part{1}) == 'c') {
577 $regpattern = $patterns[3];
579 } else if (strtolower($read_part{1}) == 'o') {
580 $regpattern = $patterns[6];
583 } else if ($firstchar == 'D' && !$internaldate ) {
584 $regpattern = $patterns[4];
586 } else if ($firstchar == 'S') {
587 $regpattern = $patterns[5];
589 } else $regpattern = '';
592 if (preg_match ($regpattern, $read_part, $regs)) {
601 $priority = $regs[1];
610 $subject = htmlspecialchars(trim($regs[1]));
611 if ($subject == "") {
612 $subject = _("(no subject)");
616 $type = strtolower(trim($regs[1]));
617 if ($pos = strpos($type, ";")) {
618 $type = substr($type, 0, $pos);
620 $type = explode("/", $type);
621 if (!isset($type[1])) {
634 $header = new small_header
;
637 $header->uid
= $unique_id;
639 $header->uid
= $msg_list[$msgi];
641 $header->date
= $date;
642 $header->subject
= $subject;
644 $header->from
= $from;
645 $header->priority
= $priority;
646 $header->message_id
= $messageid;
648 $header->size
= $size;
649 $header->type0
= $type[0];
650 $header->type1
= $type[1];
651 $header->flag_seen
= $flag_seen;
652 $header->flag_answered
= $flag_answered;
653 $header->flag_deleted
= $flag_deleted;
654 $header->flag_flagged
= $flag_flagged;
655 $header->inrepto
= $inrepto;
661 function sqimap_get_headerfield($imap_stream, $field) {
662 $sid = sqimap_session_id(false);
665 $read_list = array();
667 $query = "$sid FETCH 1:* (UID BODY.PEEK[HEADER.FIELDS ($field)])\r\n";
668 fputs ($imap_stream, $query);
669 $readin_list = sqimap_read_data_list($imap_stream, $sid, false, $response, $message);
672 foreach ($readin_list as $r) {
674 /* first we unfold the header */
675 $r = str_replace(array("\r\n\t","\r\n\s"),array('',''),$r);
677 * now we can make a new header array with each element representing
680 $r = explode("\r\n" , $r);
682 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH/iAU",$r[0], $regs)) {
683 set_up_language($squirrelmail_language);
684 echo '<br><b><font color=$color[2]>' .
685 _("ERROR : Could not complete request.") .
687 _("Unknown response from IMAP server: ") . ' 1.' .
688 $r[0] . "</font><br>\n";
693 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH.*UID\s+([0-9]+)\s+/iAU",$r[0], $regs)) {
694 set_up_language($squirrelmail_language);
695 echo '<br><b><font color=$color[2]>' .
696 _("ERROR : Could not complete request.") .
698 _("Unknown response from IMAP server: ") . ' 1.' .
699 $r[0] . "</font><br>\n";
705 $field = substr($field,strlen($field)+
2);
706 $result[] = array($id,$field);
716 * Returns a message array with all the information about a message.
717 * See the documentation folder for more information about this array.
719 function sqimap_get_message ($imap_stream, $id, $mailbox) {
723 $read = sqimap_run_command ($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, $uid_support);
725 if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) {
726 if (trim($regs[1])) {
727 $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY');
731 echo "ERROR Yeah I know, not a very usefull errormessage (id = $id, mailbox = $mailbox sqimap_get_message)";
734 $bodystructure = implode('',$read);
735 $msg = mime_structure($bodystructure,$flags);
736 $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support);
737 $rfc822_header = new Rfc822Header();
738 $rfc822_header->parseHeader($read);
739 $msg->rfc822_header
= $rfc822_header;
743 /* Wrapper function that reformats the header information. */
744 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
746 $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support);
747 $header = sqimap_get_header($imap_stream, $read);
749 $header->mailbox
= $mailbox;
753 /* Wrapper function that reformats the entity header information. */
754 function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
756 $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent.HEADER]", true, $response, $message, $uid_support);
757 $header = sqimap_get_header($imap_stream, $read);
759 $header->mailbox
= $mailbox;
764 /* Wrapper function that returns entity headers for use by decodeMime */
766 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
767 $header = sqimap_get_header($imap_stream, $read);
768 $type0 = $header["TYPE0"];
769 $type1 = $header["TYPE1"];
770 $bound = $header["BOUNDARY"];
771 $encoding = $header["ENCODING"];
772 $charset = $header["CHARSET"];
773 $filename = $header["FILENAME"];
776 /* function to get the mime headers */
777 function sqimap_get_mime_ent_header ($imap_stream, $id, $mailbox, $ent) {
779 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.MIME]", true, $response, $message, $uid_support);
780 $header = sqimap_get_header($imap_stream, $read);
782 $header->mailbox
= $mailbox;
786 /* Returns the body of a message. */
787 function sqimap_get_message_body ($imap_stream, &$header) {
788 // return decodeMime($imap_stream, $header->id);