4c113dc9757095bfae7fcc45fcc25b97190749c1
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) {
17 $read = sqimap_run_command ($imap_stream, "COPY $start:$end \"$mailbox\"", true, $response, $message, $uid_support);
20 /* Deletes specified messages and moves them to trash if possible */
21 function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
22 global $move_to_trash, $trash_folder, $auto_expunge, $uid_support;
24 if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
25 sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
27 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted", true);
30 /* Sets the specified messages with specified flag */
31 function sqimap_messages_flag ($imap_stream, $start, $end, $flag, $handle_errors) {
33 $read = sqimap_run_command ($imap_stream, "STORE $start:$end +FLAGS (\\$flag)", $handle_errors, $response, $message, $uid_support);
36 /* Remove specified flag from specified messages */
37 function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag, $handle_errors) {
39 $read = sqimap_run_command ($imap_stream, "STORE $start:$end -FLAGS (\\$flag)", $handle_errors, $response, $message, $uid_support);
42 /* Returns some general header information -- FROM, DATE, and SUBJECT */
44 var $from = '', $subject = '', $date = '', $to = '',
45 $priority = 0, $message_id = 0, $cc = '', $uid = '';
48 function sqimap_get_small_header ($imap_stream, $id, $sent) {
49 $res = sqimap_get_small_header_list($imap_stream, array($id), $sent);
54 * Sort the message list and crunch to be as small as possible
55 * (overflow could happen, so make it small if possible)
57 function sqimap_message_list_squisher($messages_array) {
58 if( !is_array( $messages_array ) ) {
62 sort($messages_array, SORT_NUMERIC
);
64 while ($messages_array) {
65 $start = array_shift($messages_array);
67 while (isset($messages_array[0]) && $messages_array[0] == $end +
1) {
68 $end = array_shift($messages_array);
70 if ($msgs_str != '') {
75 $msgs_str .= ':' . $end;
81 /* returns the references header lines */
82 function get_reference_header ($imap_stream, $message) {
84 $responses = array ();
85 $sid = sqimap_session_id($uid_support);
88 $query = "$sid FETCH $message BODY[HEADER.FIELDS (References)]\r\n";
89 fputs ($imap_stream, $query);
90 $responses = sqimap_read_data_list($imap_stream, $sid, true, $responses, $message);
91 if (!eregi("^\\* ([0-9]+) FETCH", $responses[0][0], $regs)) {
92 $responses = array ();
98 /* get sort order from server and
99 * return it as the $id array for
103 function sqimap_get_sort_order ($imap_stream, $sort, $mbxresponse) {
104 global $default_charset, $thread_sort_messages,
105 $internal_date_sort, $server_sort_array,
106 $sent_folder, $mailbox, $uid_support;
108 if (session_is_registered('server_sort_array')) {
109 sqsession_unregister('server_sort_array');
112 $sid = sqimap_session_id($uid_support);
115 $server_sort_array = array();
116 $sort_test = array();
121 $uidnext = $mbxresponse['UIDNEXT']-1;
122 $uid_query = "$sid SEARCH UID 1:$uidnext\r\n";
123 fputs($imap_stream, $uid_query);
124 $uids = sqimap_read_data($imap_stream, $sid, true ,$response, $message);
125 if (isset($uids[0])) {
126 if (preg_match("/^\* SEARCH (.+)$/", $uids[0], $regs)) {
127 $server_sort_array = preg_split("/ /", trim($regs[1]));
130 if (!preg_match("/OK/", $response)) {
131 $server_sort_array = 'no';
134 $qty = $mbxresponse['EXISTS'];
135 $server_sort_array = range(1, $qty);
137 sqsession_register($server_sort_array, 'server_sort_array');
138 return $server_sort_array;
141 $sort_on = array (0=> 'DATE',
147 if ($internal_date_sort == true) {
148 $sort_on[0] = 'ARRIVAL';
149 $sort_on[1] = 'ARRIVAL';
151 if ($sent_folder == $mailbox) {
155 if (!empty($sort_on[$sort])) {
156 $sort_query = "$sid SORT ($sort_on[$sort]) ".strtoupper($default_charset)." ALL\r\n";
157 fputs($imap_stream, $sort_query);
158 $sort_test = sqimap_read_data($imap_stream, $sid, true ,$response, $message);
160 if (isset($sort_test[0])) {
161 if (preg_match("/^\* SORT (.+)$/", $sort_test[0], $regs)) {
162 $server_sort_array = preg_split("/ /", trim($regs[1]));
165 if ($sort == 0 ||
$sort == 2 ||
$sort == 4) {
166 $server_sort_array = array_reverse($server_sort_array);
168 if (!preg_match("/OK/", $response)) {
169 $server_sort_array = 'no';
171 sqsession_register($server_sort_array, 'server_sort_array');
172 return $server_sort_array;
176 function sqimap_get_php_sort_order ($imap_stream, $mbxresponse) {
179 if (session_is_registered('php_sort_array')) {
180 sqsession_unregister('php_sort_array');
183 $sid = sqimap_session_id($uid_support);
184 $php_sort_array = array();
187 if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) {
188 $uidnext = $mbxresponse['UIDNEXT']-1;
192 $uid_query = "$sid SEARCH UID 1:$uidnext\r\n";
193 fputs($imap_stream, $uid_query);
194 $uids = sqimap_read_data($imap_stream, $sid, true ,$response, $message);
195 if (isset($uids[0])) {
196 if (preg_match("/^\* SEARCH (.+)$/", $uids[0], $regs)) {
197 $php_sort_array = preg_split("/ /", trim($regs[1]));
200 if (!preg_match("/OK/", $response)) {
201 $php_sort_array = 'no';
204 $qty = $mbxresponse['EXISTS'];
205 $php_sort_array = range(1, $qty);
207 sqsession_register($php_sort_array, 'php_sort_array');
208 return $php_sort_array;
212 /* returns an indent array for printMessageinfo()
213 this represents the amount of indent needed (value)
214 for this message number (key)
217 function get_parent_level ($imap_stream) {
218 global $sort_by_ref, $default_charset, $thread_new;
223 /* loop through the threads and take unwanted characters out
224 of the thread string then chop it up
226 for ($i=0;$i<count($thread_new);$i++
) {
227 $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]);
228 $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]);
229 $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY
);
231 $indent_array = array();
233 $thread_new = array();
235 /* looping through the parts of one message thread */
237 for ($i=0;$i<count($thread_new);$i++
) {
238 /* first grab the parent, it does not indent */
240 if (isset($thread_new[$i][0])) {
241 if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) {
245 $indent_array[$parent] = 0;
247 /* now the children, checking each thread portion for
248 ),(, and space, adjusting the level and space values
249 to get the indent level
256 for ($k=1;$k<(count($thread_new[$i]))-1;$k++
) {
257 $chars = count_chars($thread_new[$i][$k], 1);
258 if (isset($chars['40'])) { /* testing for ( */
259 $level = $level +
$chars['40'];
261 if (isset($chars['41'])) { /* testing for ) */
262 $level = $level - $chars['41'];
264 /* if we were faking lets stop, this portion
265 of the thread is over
267 if ($level == $cutoff) {
271 if (isset($chars['32'])) { /* testing for space */
272 if (!isset($spaces[$level])) {
275 $spaces[$level] = $spaces[$level] +
$chars['32'];
277 for ($x=0;$x<=$level;$x++
) {
278 if (isset($spaces[$x])) {
279 $spaces_total = $spaces_total +
$spaces[$x];
282 $indent = $level +
$spaces_total;
283 /* must have run into a message that broke the thread
284 so we are adjusting for that portion
287 $indent = $indent +
1;
289 if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
292 /* the thread must be broken if $indent == 0
293 so indent the message once and start faking it
300 /* dont need abs but if indent was negative
303 $indent_array[$child] = abs($indent);
307 return $indent_array;
311 /* returns an array with each element as a string
312 representing one message thread as returned by
316 function get_thread_sort ($imap_stream) {
317 global $thread_new, $sort_by_ref, $default_charset, $server_sort_array, $uid_support;
318 if (session_is_registered('thread_new')) {
319 sqsession_unregister('thread_new');
321 if (session_is_registered('server_sort_array')) {
322 sqsession_unregister('server_sort_array');
324 $sid = sqimap_session_id($uid_support);
325 $thread_temp = array ();
326 if ($sort_by_ref == 1) {
327 $sort_type = 'REFERENCES';
330 $sort_type = 'ORDEREDSUBJECT';
332 $thread_query = "$sid THREAD $sort_type ".strtoupper($default_charset)." ALL\r\n";
333 fputs($imap_stream, $thread_query);
334 $thread_test = sqimap_read_data($imap_stream, $sid, false, $response, $message);
335 if (isset($thread_test[0])) {
336 if (preg_match("/^\* THREAD (.+)$/", $thread_test[0], $regs)) {
337 $thread_list = trim($regs[1]);
343 if (!preg_match("/OK/", $response)) {
344 $server_sort_array = 'no';
345 return $server_sort_array;
347 if (isset($thread_list)) {
348 $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
350 $char_count = count($thread_temp);
352 $thread_new = array();
355 for ($i=0;$i<$char_count;$i++
) {
356 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
357 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
359 elseif ($thread_temp[$i] == '(') {
360 $thread_new[$k] .= $thread_temp[$i];
363 elseif ($thread_temp[$i] == ')') {
365 $thread_new[$k] .= $thread_temp[$i];
366 $counter = $counter - 1;
369 $thread_new[$k] .= $thread_temp[$i];
371 $thread_new[$k] = "";
372 $counter = $counter - 1;
376 sqsession_register($thread_new, 'thread_new');
377 $thread_new = array_reverse($thread_new);
378 $thread_list = implode(" ", $thread_new);
379 $thread_list = str_replace("(", " ", $thread_list);
380 $thread_list = str_replace(")", " ", $thread_list);
381 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
382 $server_sort_array = $thread_list;
383 sqsession_register($server_sort_array, 'server_sort_array');
387 function elapsedTime($start) {
388 $stop = gettimeofday();
389 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) +
$stop['usec'] - $start['usec'];
393 function sqimap_get_small_header_list ($imap_stream, $msg_list) {
394 global $squirrelmail_language, $color, $data_dir, $username, $imap_server_type;
397 /* Get the small headers for each message in $msg_list */
398 $sid = sqimap_session_id($uid_support);
400 $maxmsg = sizeof($msg_list);
401 $msgs_str = sqimap_message_list_squisher($msg_list);
403 $read_list = array();
405 * We need to return the data in the same order as the caller supplied
406 * in $msg_list, but IMAP servers are free to return responses in
407 * whatever order they wish... So we need to re-sort manually
409 for ($i = 0; $i < sizeof($msg_list); $i++
) {
410 $id2index[$msg_list[$i]] = $i;
413 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
415 $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";
417 $query = "$sid FETCH $msgs_str (FLAGS UID RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject X-Priority Content-Type)])\r\n";
419 fputs ($imap_stream, $query);
420 $readin_list = sqimap_read_data_list($imap_stream, $sid, false, $response, $message);
422 foreach ($readin_list as $r) {
424 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH/iAU",$r[0], $regs)) {
425 set_up_language($squirrelmail_language);
426 echo '<br><b><font color=$color[2]>' .
427 _("ERROR : Could not complete request.") .
429 _("Unknown response from IMAP server: ") . ' 1.' .
430 $r[0] . "</font><br>\n";
431 } else if (! isset($id2index[$regs[1]]) ||
!count($id2index[$regs[1]])) {
432 set_up_language($squirrelmail_language);
433 echo '<br><b><font color=$color[2]>' .
434 _("ERROR : Could not complete request.") .
436 _("Unknown message number in reply from server: ") .
437 $regs[1] . "</font><br>\n";
439 $read_list[$id2index[$regs[1]]] = $r;
442 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH.*UID\s+([0-9]+)\s+/iAU",$r[0], $regs)) {
443 set_up_language($squirrelmail_language);
444 echo '<br><b><font color=$color[2]>' .
445 _("ERROR : Could not complete request.") .
447 _("Unknown response from IMAP server: ") . ' 1.' .
448 $r[0] . "</font><br>\n";
449 } else if (! isset($id2index[$regs[2]]) ||
!count($id2index[$regs[2]])) {
450 set_up_language($squirrelmail_language);
451 echo '<br><b><font color=$color[2]>' .
452 _("ERROR : Could not complete request.") .
454 _("Unknown message number in reply from server: ") .
455 $regs[2] . "</font><br>\n";
457 $read_list[$id2index[$regs[2]]] = $r;
458 $unique_id = $regs[2];
467 "/^X-Priority:(.*)\$/AUi",
470 "/^Subject:(.*)\$/AUi",
471 "/^Content-Type:(.*)\$/AUi"
475 for ($msgi = 0; $msgi < $maxmsg; $msgi++
) {
476 $subject = _("(no subject)");
477 $from = _("Unknown Sender");
487 $flag_answered = false;
488 $flag_deleted = false;
489 $flag_flagged = false;
490 $read = $read_list[$msgi];
493 foreach ($read as $read_part) {
494 //unfold multi-line headers
495 if ($prevline && strpos($read_part, "\t ") === true) {
496 $read_part = substr($prevline, 0, -2) . preg_replace('/(\t\s+)/',' ',$read_part);
498 $prevline = $read_part;
499 if ($read_part{0} == '*') {
501 if (preg_match ("/^.+INTERNALDATE\s+\"(.+)\".+/iUA",$read_part, $reg)) {
502 $tmpdate = trim($reg[1]);
503 $tmpdate = str_replace(' ',' ',$tmpdate);
504 $tmpdate = explode(' ',$tmpdate);
505 $date = str_replace('-',' ',$tmpdate[0]) . " " .
510 if (preg_match ("/^.+RFC822.SIZE\s+(\d+).+/iA",$read_part, $reg)) {
513 if (preg_match("/^.+FLAGS\s+\((.*)\).+/iUA", $read_part, $regs)) {
514 $flags = explode(' ',trim($regs[1]));
515 foreach ($flags as $flag) {
516 $flag = strtolower($flag);
517 if ($flag == '\\seen') {
519 } else if ($flag == '\\answered') {
520 $flag_answered = true;
521 } else if ($flag == '\\deleted') {
522 $flag_deleted = true;
523 } else if ($flag == '\\flagged') {
524 $flag_flagged = true;
528 if (preg_match ("/^.+UID\s+(\d+).+/iA",$read_part, $reg)) {
529 $unique_id = $reg[1];
532 $firstchar = $read_part{0};
533 if ($firstchar == 'T') {
534 $regpattern = $patterns[0];
536 } else if ($firstchar == 'F') {
537 $regpattern = $patterns[1];
539 } else if ($firstchar == 'X') {
540 $regpattern = $patterns[2];
542 } else if ($firstchar == 'C') {
543 if (strtolower($read_part{1}) == 'c') {
544 $regpattern = $patterns[3];
546 } else if (strtolower($read_part{1}) == 'o') {
547 $regpattern = $patterns[6];
550 } else if ($firstchar == 'D' && !$internaldate ) {
551 $regpattern = $patterns[4];
553 } else if ($firstchar == 'S') {
554 $regpattern = $patterns[5];
556 } else $regpattern = '';
559 if (preg_match ($regpattern, $read_part, $regs)) {
568 $priority = $regs[1];
577 $subject = htmlspecialchars(trim($regs[1]));
578 if ($subject == "") {
579 $subject = _("(no subject)");
583 $type = strtolower(trim($regs[1]));
584 if ($pos = strpos($type, ";")) {
585 $type = substr($type, 0, $pos);
587 $type = explode("/", $type);
588 if (!isset($type[1])) {
601 $header = new small_header
;
604 $header->uid
= $unique_id;
606 $header->uid
= $msg_list[$msgi];
608 $header->date
= $date;
609 $header->subject
= $subject;
611 $header->from
= $from;
612 $header->priority
= $priority;
613 $header->message_id
= $messageid;
615 $header->size
= $size;
616 $header->type0
= $type[0];
617 $header->type1
= $type[1];
618 $header->flag_seen
= $flag_seen;
619 $header->flag_answered
= $flag_answered;
620 $header->flag_deleted
= $flag_deleted;
621 $header->flag_flagged
= $flag_flagged;
622 $header->inrepto
= $inrepto;
628 function sqimap_get_headerfield($imap_stream, $field) {
629 $sid = sqimap_session_id(false);
632 $read_list = array();
634 $query = "$sid FETCH 1:* (UID BODY.PEEK[HEADER.FIELDS ($field)])\r\n";
635 fputs ($imap_stream, $query);
636 $readin_list = sqimap_read_data_list($imap_stream, $sid, false, $response, $message);
639 foreach ($readin_list as $r) {
641 /* first we unfold the header */
642 $r = str_replace(array("\r\n\t","\r\n\s"),array('',''),$r);
644 * now we can make a new header array with each element representing
647 $r = explode("\r\n" , $r);
649 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH/iAU",$r[0], $regs)) {
650 set_up_language($squirrelmail_language);
651 echo '<br><b><font color=$color[2]>' .
652 _("ERROR : Could not complete request.") .
654 _("Unknown response from IMAP server: ") . ' 1.' .
655 $r[0] . "</font><br>\n";
660 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH.*UID\s+([0-9]+)\s+/iAU",$r[0], $regs)) {
661 set_up_language($squirrelmail_language);
662 echo '<br><b><font color=$color[2]>' .
663 _("ERROR : Could not complete request.") .
665 _("Unknown response from IMAP server: ") . ' 1.' .
666 $r[0] . "</font><br>\n";
672 $field = substr($field,strlen($field)+
2);
673 $result[] = array($id,$field);
683 * Returns a message array with all the information about a message.
684 * See the documentation folder for more information about this array.
686 function sqimap_get_message ($imap_stream, $id, $mailbox) {
690 $read = sqimap_run_command ($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, $uid_support);
692 if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) {
693 if (trim($regs[1])) {
694 $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY');
698 echo "ERROR Yeah I know, not a very usefull errormessage (id = $id, mailbox = $mailbox sqimap_get_message)";
701 $bodystructure = implode('',$read);
702 $msg = mime_structure($bodystructure,$flags);
703 $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support);
704 $rfc822_header = new Rfc822Header();
705 $rfc822_header->parseHeader($read);
706 $msg->rfc822_header
= $rfc822_header;
710 /* Wrapper function that reformats the header information. */
711 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
713 $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support);
714 $header = sqimap_get_header($imap_stream, $read);
716 $header->mailbox
= $mailbox;
720 /* Wrapper function that reformats the entity header information. */
721 function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
723 $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent.HEADER]", true, $response, $message, $uid_support);
724 $header = sqimap_get_header($imap_stream, $read);
726 $header->mailbox
= $mailbox;
731 /* Wrapper function that returns entity headers for use by decodeMime */
733 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
734 $header = sqimap_get_header($imap_stream, $read);
735 $type0 = $header["TYPE0"];
736 $type1 = $header["TYPE1"];
737 $bound = $header["BOUNDARY"];
738 $encoding = $header["ENCODING"];
739 $charset = $header["CHARSET"];
740 $filename = $header["FILENAME"];
743 /* function to get the mime headers */
744 function sqimap_get_mime_ent_header ($imap_stream, $id, $mailbox, $ent) {
746 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.MIME]", true, $response, $message, $uid_support);
747 $header = sqimap_get_header($imap_stream, $read);
749 $header->mailbox
= $mailbox;
753 /* Returns the body of a message. */
754 function sqimap_get_message_body ($imap_stream, &$header) {
755 // return decodeMime($imap_stream, $header->id);