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 (sqsession_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 if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) {
122 $uidnext = $mbxresponse['UIDNEXT']-1;
126 $uid_query = "$sid SEARCH UID 1:$uidnext\r\n";
127 fputs($imap_stream, $uid_query);
128 $uids = sqimap_read_data($imap_stream, $sid, true ,$response, $message);
129 if (isset($uids[0])) {
130 if (preg_match("/^\* SEARCH (.+)$/", $uids[0], $regs)) {
131 $server_sort_array = preg_split("/ /", trim($regs[1]));
134 if (!preg_match("/OK/", $response)) {
135 $server_sort_array = 'no';
138 $qty = $mbxresponse['EXISTS'];
139 $server_sort_array = range(1, $qty);
141 sqsession_register($server_sort_array, 'server_sort_array');
142 return $server_sort_array;
145 $sort_on = array (0=> 'DATE',
151 if ($internal_date_sort == true) {
152 $sort_on[0] = 'ARRIVAL';
153 $sort_on[1] = 'ARRIVAL';
155 if ($sent_folder == $mailbox) {
159 if (!empty($sort_on[$sort])) {
160 $sort_query = "$sid SORT ($sort_on[$sort]) ".strtoupper($default_charset)." ALL\r\n";
161 fputs($imap_stream, $sort_query);
162 $sort_test = sqimap_read_data($imap_stream, $sid, true ,$response, $message);
164 if (isset($sort_test[0])) {
165 if (preg_match("/^\* SORT (.+)$/", $sort_test[0], $regs)) {
166 $server_sort_array = preg_split("/ /", trim($regs[1]));
169 if ($sort == 0 ||
$sort == 2 ||
$sort == 4) {
170 $server_sort_array = array_reverse($server_sort_array);
172 if (!preg_match("/OK/", $response)) {
173 $server_sort_array = 'no';
175 sqsession_register($server_sort_array, 'server_sort_array');
176 return $server_sort_array;
180 function sqimap_get_php_sort_order ($imap_stream, $mbxresponse) {
183 if (sqsession_is_registered('php_sort_array')) {
184 sqsession_unregister('php_sort_array');
187 $sid = sqimap_session_id($uid_support);
188 $php_sort_array = array();
191 if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) {
192 $uidnext = $mbxresponse['UIDNEXT']-1;
196 $uid_query = "$sid SEARCH UID 1:$uidnext\r\n";
197 fputs($imap_stream, $uid_query);
198 $uids = sqimap_read_data($imap_stream, $sid, true ,$response, $message);
199 if (isset($uids[0])) {
200 if (preg_match("/^\* SEARCH (.+)$/", $uids[0], $regs)) {
201 $php_sort_array = preg_split("/ /", trim($regs[1]));
204 if (!preg_match("/OK/", $response)) {
205 $php_sort_array = 'no';
208 $qty = $mbxresponse['EXISTS'];
209 $php_sort_array = range(1, $qty);
211 sqsession_register($php_sort_array, 'php_sort_array');
212 return $php_sort_array;
216 /* returns an indent array for printMessageinfo()
217 this represents the amount of indent needed (value)
218 for this message number (key)
221 function get_parent_level ($imap_stream) {
222 global $sort_by_ref, $default_charset, $thread_new;
227 /* loop through the threads and take unwanted characters out
228 of the thread string then chop it up
230 for ($i=0;$i<count($thread_new);$i++
) {
231 $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]);
232 $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]);
233 $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY
);
235 $indent_array = array();
237 $thread_new = array();
239 /* looping through the parts of one message thread */
241 for ($i=0;$i<count($thread_new);$i++
) {
242 /* first grab the parent, it does not indent */
244 if (isset($thread_new[$i][0])) {
245 if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) {
249 $indent_array[$parent] = 0;
251 /* now the children, checking each thread portion for
252 ),(, and space, adjusting the level and space values
253 to get the indent level
260 for ($k=1;$k<(count($thread_new[$i]))-1;$k++
) {
261 $chars = count_chars($thread_new[$i][$k], 1);
262 if (isset($chars['40'])) { /* testing for ( */
263 $level = $level +
$chars['40'];
265 if (isset($chars['41'])) { /* testing for ) */
266 $level = $level - $chars['41'];
268 /* if we were faking lets stop, this portion
269 of the thread is over
271 if ($level == $cutoff) {
275 if (isset($chars['32'])) { /* testing for space */
276 if (!isset($spaces[$level])) {
279 $spaces[$level] = $spaces[$level] +
$chars['32'];
281 for ($x=0;$x<=$level;$x++
) {
282 if (isset($spaces[$x])) {
283 $spaces_total = $spaces_total +
$spaces[$x];
286 $indent = $level +
$spaces_total;
287 /* must have run into a message that broke the thread
288 so we are adjusting for that portion
291 $indent = $indent +
1;
293 if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
296 /* the thread must be broken if $indent == 0
297 so indent the message once and start faking it
304 /* dont need abs but if indent was negative
307 $indent_array[$child] = abs($indent);
311 return $indent_array;
315 /* returns an array with each element as a string
316 representing one message thread as returned by
320 function get_thread_sort ($imap_stream) {
321 global $thread_new, $sort_by_ref, $default_charset, $server_sort_array, $uid_support;
322 if (sqsession_is_registered('thread_new')) {
323 sqsession_unregister('thread_new');
325 if (sqsession_is_registered('server_sort_array')) {
326 sqsession_unregister('server_sort_array');
328 $sid = sqimap_session_id($uid_support);
329 $thread_temp = array ();
330 if ($sort_by_ref == 1) {
331 $sort_type = 'REFERENCES';
334 $sort_type = 'ORDEREDSUBJECT';
336 $thread_query = "$sid THREAD $sort_type ".strtoupper($default_charset)." ALL\r\n";
337 fputs($imap_stream, $thread_query);
338 $thread_test = sqimap_read_data($imap_stream, $sid, false, $response, $message);
339 if (isset($thread_test[0])) {
340 if (preg_match("/^\* THREAD (.+)$/", $thread_test[0], $regs)) {
341 $thread_list = trim($regs[1]);
347 if (!preg_match("/OK/", $response)) {
348 $server_sort_array = 'no';
349 return $server_sort_array;
351 if (isset($thread_list)) {
352 $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
354 $char_count = count($thread_temp);
356 $thread_new = array();
359 for ($i=0;$i<$char_count;$i++
) {
360 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
361 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
363 elseif ($thread_temp[$i] == '(') {
364 $thread_new[$k] .= $thread_temp[$i];
367 elseif ($thread_temp[$i] == ')') {
369 $thread_new[$k] .= $thread_temp[$i];
370 $counter = $counter - 1;
373 $thread_new[$k] .= $thread_temp[$i];
375 $thread_new[$k] = "";
376 $counter = $counter - 1;
380 sqsession_register($thread_new, 'thread_new');
381 $thread_new = array_reverse($thread_new);
382 $thread_list = implode(" ", $thread_new);
383 $thread_list = str_replace("(", " ", $thread_list);
384 $thread_list = str_replace(")", " ", $thread_list);
385 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
386 $server_sort_array = $thread_list;
387 sqsession_register($server_sort_array, 'server_sort_array');
391 function elapsedTime($start) {
392 $stop = gettimeofday();
393 $timepassed = 1000000 * ($stop['sec'] - $start['sec']) +
$stop['usec'] - $start['usec'];
397 function sqimap_get_small_header_list ($imap_stream, $msg_list) {
398 global $squirrelmail_language, $color, $data_dir, $username, $imap_server_type;
401 /* Get the small headers for each message in $msg_list */
402 $sid = sqimap_session_id($uid_support);
404 $maxmsg = sizeof($msg_list);
405 $msgs_str = sqimap_message_list_squisher($msg_list);
407 $read_list = array();
409 * We need to return the data in the same order as the caller supplied
410 * in $msg_list, but IMAP servers are free to return responses in
411 * whatever order they wish... So we need to re-sort manually
413 for ($i = 0; $i < sizeof($msg_list); $i++
) {
414 $id2index[$msg_list[$i]] = $i;
417 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
419 $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";
421 $query = "$sid FETCH $msgs_str (FLAGS UID RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject X-Priority Content-Type)])\r\n";
423 fputs ($imap_stream, $query);
424 $readin_list = sqimap_read_data_list($imap_stream, $sid, false, $response, $message);
426 foreach ($readin_list as $r) {
428 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH/iAU",$r[0], $regs)) {
429 set_up_language($squirrelmail_language);
430 echo '<br><b><font color=$color[2]>' .
431 _("ERROR : Could not complete request.") .
433 _("Unknown response from IMAP server: ") . ' 1.' .
434 $r[0] . "</font><br>\n";
435 } else if (! isset($id2index[$regs[1]]) ||
!count($id2index[$regs[1]])) {
436 set_up_language($squirrelmail_language);
437 echo '<br><b><font color=$color[2]>' .
438 _("ERROR : Could not complete request.") .
440 _("Unknown message number in reply from server: ") .
441 $regs[1] . "</font><br>\n";
443 $read_list[$id2index[$regs[1]]] = $r;
446 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH.*UID\s+([0-9]+)\s+/iAU",$r[0], $regs)) {
447 set_up_language($squirrelmail_language);
448 echo '<br><b><font color=$color[2]>' .
449 _("ERROR : Could not complete request.") .
451 _("Unknown response from IMAP server: ") . ' 1.' .
452 $r[0] . "</font><br>\n";
453 } else if (! isset($id2index[$regs[2]]) ||
!count($id2index[$regs[2]])) {
454 set_up_language($squirrelmail_language);
455 echo '<br><b><font color=$color[2]>' .
456 _("ERROR : Could not complete request.") .
458 _("Unknown message number in reply from server: ") .
459 $regs[2] . "</font><br>\n";
461 $read_list[$id2index[$regs[2]]] = $r;
462 $unique_id = $regs[2];
471 "/^X-Priority:(.*)\$/AUi",
474 "/^Subject:(.*)\$/AUi",
475 "/^Content-Type:(.*)\$/AUi"
479 for ($msgi = 0; $msgi < $maxmsg; $msgi++
) {
480 $subject = _("(no subject)");
481 $from = _("Unknown Sender");
491 $flag_answered = false;
492 $flag_deleted = false;
493 $flag_flagged = false;
494 $read = $read_list[$msgi];
497 foreach ($read as $read_part) {
498 //unfold multi-line headers
499 if ($prevline && strpos($read_part, "\t ") === true) {
500 $read_part = substr($prevline, 0, -2) . preg_replace('/(\t\s+)/',' ',$read_part);
502 $prevline = $read_part;
503 if ($read_part{0} == '*') {
505 if (preg_match ("/^.+INTERNALDATE\s+\"(.+)\".+/iUA",$read_part, $reg)) {
506 $tmpdate = trim($reg[1]);
507 $tmpdate = str_replace(' ',' ',$tmpdate);
508 $tmpdate = explode(' ',$tmpdate);
509 $date = str_replace('-',' ',$tmpdate[0]) . " " .
514 if (preg_match ("/^.+RFC822.SIZE\s+(\d+).+/iA",$read_part, $reg)) {
517 if (preg_match("/^.+FLAGS\s+\((.*)\).+/iUA", $read_part, $regs)) {
518 $flags = explode(' ',trim($regs[1]));
519 foreach ($flags as $flag) {
520 $flag = strtolower($flag);
521 if ($flag == '\\seen') {
523 } else if ($flag == '\\answered') {
524 $flag_answered = true;
525 } else if ($flag == '\\deleted') {
526 $flag_deleted = true;
527 } else if ($flag == '\\flagged') {
528 $flag_flagged = true;
532 if (preg_match ("/^.+UID\s+(\d+).+/iA",$read_part, $reg)) {
533 $unique_id = $reg[1];
536 $firstchar = $read_part{0};
537 if ($firstchar == 'T') {
538 $regpattern = $patterns[0];
540 } else if ($firstchar == 'F') {
541 $regpattern = $patterns[1];
543 } else if ($firstchar == 'X') {
544 $regpattern = $patterns[2];
546 } else if ($firstchar == 'C') {
547 if (strtolower($read_part{1}) == 'c') {
548 $regpattern = $patterns[3];
550 } else if (strtolower($read_part{1}) == 'o') {
551 $regpattern = $patterns[6];
554 } else if ($firstchar == 'D' && !$internaldate ) {
555 $regpattern = $patterns[4];
557 } else if ($firstchar == 'S') {
558 $regpattern = $patterns[5];
560 } else $regpattern = '';
563 if (preg_match ($regpattern, $read_part, $regs)) {
572 $priority = $regs[1];
581 $subject = htmlspecialchars(trim($regs[1]));
582 if ($subject == "") {
583 $subject = _("(no subject)");
587 $type = strtolower(trim($regs[1]));
588 if ($pos = strpos($type, ";")) {
589 $type = substr($type, 0, $pos);
591 $type = explode("/", $type);
592 if (!isset($type[1])) {
605 $header = new small_header
;
608 $header->uid
= $unique_id;
610 $header->uid
= $msg_list[$msgi];
612 $header->date
= $date;
613 $header->subject
= $subject;
615 $header->from
= $from;
616 $header->priority
= $priority;
617 $header->message_id
= $messageid;
619 $header->size
= $size;
620 $header->type0
= $type[0];
621 $header->type1
= $type[1];
622 $header->flag_seen
= $flag_seen;
623 $header->flag_answered
= $flag_answered;
624 $header->flag_deleted
= $flag_deleted;
625 $header->flag_flagged
= $flag_flagged;
626 $header->inrepto
= $inrepto;
632 function sqimap_get_headerfield($imap_stream, $field) {
633 $sid = sqimap_session_id(false);
636 $read_list = array();
638 $query = "$sid FETCH 1:* (UID BODY.PEEK[HEADER.FIELDS ($field)])\r\n";
639 fputs ($imap_stream, $query);
640 $readin_list = sqimap_read_data_list($imap_stream, $sid, false, $response, $message);
643 foreach ($readin_list as $r) {
645 /* first we unfold the header */
646 $r = str_replace(array("\r\n\t","\r\n\s"),array('',''),$r);
648 * now we can make a new header array with each element representing
651 $r = explode("\r\n" , $r);
653 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH/iAU",$r[0], $regs)) {
654 set_up_language($squirrelmail_language);
655 echo '<br><b><font color=$color[2]>' .
656 _("ERROR : Could not complete request.") .
658 _("Unknown response from IMAP server: ") . ' 1.' .
659 $r[0] . "</font><br>\n";
664 if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH.*UID\s+([0-9]+)\s+/iAU",$r[0], $regs)) {
665 set_up_language($squirrelmail_language);
666 echo '<br><b><font color=$color[2]>' .
667 _("ERROR : Could not complete request.") .
669 _("Unknown response from IMAP server: ") . ' 1.' .
670 $r[0] . "</font><br>\n";
676 $field = substr($field,strlen($field)+
2);
677 $result[] = array($id,$field);
687 * Returns a message array with all the information about a message.
688 * See the documentation folder for more information about this array.
690 function sqimap_get_message ($imap_stream, $id, $mailbox) {
694 $read = sqimap_run_command ($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, $uid_support);
696 if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) {
697 if (trim($regs[1])) {
698 $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY');
702 echo "ERROR Yeah I know, not a very usefull errormessage (id = $id, mailbox = $mailbox sqimap_get_message)";
705 $bodystructure = implode('',$read);
706 $msg = mime_structure($bodystructure,$flags);
707 $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support);
708 $rfc822_header = new Rfc822Header();
709 $rfc822_header->parseHeader($read);
710 $msg->rfc822_header
= $rfc822_header;
714 /* Wrapper function that reformats the header information. */
715 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
717 $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support);
718 $header = sqimap_get_header($imap_stream, $read);
720 $header->mailbox
= $mailbox;
724 /* Wrapper function that reformats the entity header information. */
725 function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
727 $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent.HEADER]", true, $response, $message, $uid_support);
728 $header = sqimap_get_header($imap_stream, $read);
730 $header->mailbox
= $mailbox;
735 /* Wrapper function that returns entity headers for use by decodeMime */
737 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
738 $header = sqimap_get_header($imap_stream, $read);
739 $type0 = $header["TYPE0"];
740 $type1 = $header["TYPE1"];
741 $bound = $header["BOUNDARY"];
742 $encoding = $header["ENCODING"];
743 $charset = $header["CHARSET"];
744 $filename = $header["FILENAME"];
747 /* function to get the mime headers */
748 function sqimap_get_mime_ent_header ($imap_stream, $id, $mailbox, $ent) {
750 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.MIME]", true, $response, $message, $uid_support);
751 $header = sqimap_get_header($imap_stream, $read);
753 $header->mailbox
= $mailbox;
757 /* Returns the body of a message. */
758 function sqimap_get_message_body ($imap_stream, &$header) {
759 // return decodeMime($imap_stream, $header->id);