6 * This implements all functions that manipulate mailboxes
8 * @copyright © 1999-2007 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
16 require_once(SM_PATH
. 'functions/imap_utf7_local.php');
22 * FIXME. This class should be extracted and placed in a separate file that
23 * can be included before we start the session. That makes caching of the tree
24 * possible. On a refresh mailboxes from left_main.php the only function that
25 * should be called is the sqimap_get_status_mbx_tree. In case of subscribe
26 * / rename / delete / new we have to create methods for adding/changing the
27 * mailbox in the mbx_tree without the need for a refresh.
29 * Some code fragments are present in 1.3.0 - 1.4.4.
30 * @package squirrelmail
35 var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false, $is_noinferiors = false,
36 $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
37 $is_trash = false, $is_draft = false, $mbxs = array(),
38 $unseen = false, $total = false, $recent = false;
40 function addMbx($mbx, $delimiter, $start, $specialfirst) {
41 $ary = explode($delimiter, $mbx->mailboxname_full
);
43 for ($i = $start, $c = count($ary)-1; $i < $c; $i++
) {
44 $mbx_childs =& $mbx_parent->mbxs
;
47 foreach ($mbx_childs as $key => $parent) {
48 if ($parent->mailboxname_sub
== $ary[$i]) {
49 $mbx_parent =& $mbx_parent->mbxs
[$key];
56 $no_select_mbx = new mailboxes();
57 if (isset($mbx_parent->mailboxname_full
) && $mbx_parent->mailboxname_full
!= '') {
58 $no_select_mbx->mailboxname_full
= $mbx_parent->mailboxname_full
.$delimiter.$ary[$i];
60 $no_select_mbx->mailboxname_full
= $ary[$i];
62 $no_select_mbx->mailboxname_sub
= $ary[$i];
63 $no_select_mbx->is_noselect
= true;
64 $mbx_parent->mbxs
[] = $no_select_mbx;
68 $mbx_parent->mbxs
[] = $mbx;
69 if ($mbx->is_special
&& $specialfirst) {
70 usort($mbx_parent->mbxs
, 'sortSpecialMbx');
76 * array callback used for sorting in mailboxes class
79 * @return integer see php strnatcasecmp()
82 function sortSpecialMbx($a, $b) {
84 $acmp = '0'. $a->mailboxname_full
;
85 } else if ($a->is_special
) {
86 $acmp = '1'. $a->mailboxname_full
;
88 $acmp = '2' . $a->mailboxname_full
;
91 $bcmp = '0'. $b->mailboxname_full
;
92 }else if ($b->is_special
) {
93 $bcmp = '1' . $b->mailboxname_full
;
95 $bcmp = '2' . $b->mailboxname_full
;
97 return strnatcasecmp($acmp, $bcmp);
105 function compact_mailboxes_response($ary) {
107 * Workaround for mailboxes returned as literal
108 * FIXME : Doesn't work if the mailbox name is multiple lines
109 * (larger then fgets buffer)
111 for ($i = 0, $iCnt=count($ary); $i < $iCnt; $i++
) {
112 if (isset($ary[$i +
1]) && substr($ary[$i], -3) == "}\r\n") {
113 if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
115 $ary[$i] = $regs[1] . '"' . addslashes(trim($ary[$i+
1])) . '"' . $regs[2];
116 array_splice($ary, $i+
1, 2);
120 /* remove duplicates and ensure array is contiguous */
121 return array_values(array_unique($ary));
125 * Extract the mailbox name from an untagged LIST (7.2.2) or LSUB (7.2.3) answer
126 * (LIST|LSUB) (<Flags list>) (NIL|"<separator atom>") <mailbox name string>\r\n
127 * mailbox name in quoted string MUST be unquoted and stripslashed (sm API)
129 * Originally stored in functions/strings.php. Since 1.2.6 stored in
130 * functions/imap_mailbox.php
131 * @param string $line imap LIST/LSUB response line
132 * @return string mailbox name
134 function find_mailbox_name($line) {
135 if (preg_match('/^\* (?:LIST|LSUB) \([^\)]*\) (?:NIL|\"[^\"]*\") ([^\r\n]*)[\r\n]*$/i', $line, $regs)) {
136 if (substr($regs[1], 0, 1) == '"')
137 return stripslashes(substr($regs[1], 1, -1));
144 * Detects if mailbox has noselect flag (can't store messages)
145 * In versions older than 1.4.5 function checks only LSUB responses
146 * and can produce pcre warnings.
147 * @param string $lsub_line mailbox line from untagged LIST or LSUB response
148 * @return bool whether this is a Noselect mailbox.
151 function check_is_noselect ($lsub_line) {
152 return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noselect[^\)]*\)/i", $lsub_line);
156 * Detects if mailbox has noinferiors flag (can't store subfolders)
157 * @param string $lsub_line mailbox line from untagged LIST or LSUB response
158 * @return bool whether this is a Noinferiors mailbox.
161 function check_is_noinferiors ($lsub_line) {
162 return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noinferiors[^\)]*\)/i", $lsub_line);
166 * Detects mailbox's parent folder
168 * If $haystack is a full mailbox name, and $needle is the mailbox
169 * separator character, returns the second last part of the full
170 * mailbox name (i.e. the mailbox's parent mailbox)
172 * Originally stored in functions/strings.php. Since 1.2.6 stored in
173 * functions/imap_mailbox.php
174 * @param string $haystack full mailbox name
175 * @param string $needle delimiter
176 * @return string parent mailbox
178 function readMailboxParent($haystack, $needle) {
182 $parts = explode($needle, $haystack);
183 $elem = array_pop($parts);
184 while ($elem == '' && count($parts)) {
185 $elem = array_pop($parts);
187 $ret = join($needle, $parts);
193 * Check if $subbox is below the specified $parentbox
194 * @param string $subbox potential sub folder
195 * @param string $parentbox potential parent
199 function isBoxBelow( $subbox, $parentbox ) {
202 * Eliminate the obvious mismatch, where the
203 * subfolder path is shorter than that of the potential parent
205 if ( strlen($subbox) < strlen($parentbox) ) {
208 /* check for delimiter */
209 if (substr($parentbox,-1) != $delimiter) {
210 $parentbox .= $delimiter;
213 return (substr($subbox,0,strlen($parentbox)) == $parentbox);
217 * Defines special mailboxes: given a mailbox name, it checks if this is a
218 * "special" one: INBOX, Trash, Sent or Draft.
220 * Since 1.2.5 function includes special_mailbox hook.<br>
221 * Since 1.4.3 hook supports more than one plugin.
222 * @param string $box mailbox name
223 * @param boolean $include_subs (since 1.5.2) if true, subfolders of system
224 * folders are special. if false, subfolders are not special mailboxes
225 * unless they are tagged as special in 'special_mailbox' hook.
229 function isSpecialMailbox($box,$include_subs=true) {
230 $ret = ( (strtolower($box) == 'inbox') ||
231 isTrashMailbox($box,$include_subs) ||
232 isSentMailbox($box,$include_subs) ||
233 isDraftMailbox($box,$include_subs) );
236 $ret = boolean_hook_function('special_mailbox', $box, 1);
242 * Detects if mailbox is a Trash folder or subfolder of Trash
243 * @param string $box mailbox name
244 * @param boolean $include_subs (since 1.5.2) if true, subfolders of system
245 * folders are special. if false, subfolders are not special mailboxes.
246 * @return bool whether this is a Trash folder
249 function isTrashMailbox ($box,$include_subs=true) {
250 global $trash_folder, $move_to_trash;
251 return $move_to_trash && $trash_folder &&
252 ( $box == $trash_folder ||
253 ($include_subs && isBoxBelow($box, $trash_folder)) );
257 * Detects if mailbox is a Sent folder or subfolder of Sent
258 * @param string $box mailbox name
259 * @param boolean $include_subs (since 1.5.2) if true, subfolders of system
260 * folders are special. if false, subfolders are not special mailboxes.
261 * @return bool whether this is a Sent folder
264 function isSentMailbox($box,$include_subs=true) {
265 global $sent_folder, $move_to_sent;
266 return $move_to_sent && $sent_folder &&
267 ( $box == $sent_folder ||
268 ($include_subs && isBoxBelow($box, $sent_folder)) );
272 * Detects if mailbox is a Drafts folder or subfolder of Drafts
273 * @param string $box mailbox name
274 * @param boolean $include_subs (since 1.5.2) if true, subfolders of system
275 * folders are special. if false, subfolders are not special mailboxes.
276 * @return bool whether this is a Draft folder
279 function isDraftMailbox($box,$include_subs=true) {
280 global $draft_folder, $save_as_draft;
281 return $save_as_draft &&
282 ( $box == $draft_folder ||
283 ($include_subs && isBoxBelow($box, $draft_folder)) );
289 * WARNING: Select mailbox before calling this function.
291 * permanently removes all messages that have the \Deleted flag
292 * set from the selected mailbox. See EXPUNGE command chapter in
294 * @param stream $imap_stream imap connection resource
295 * @param string $mailbox mailbox name (unused since 1.1.3).
296 * @param boolean $handle_errors error handling control (displays error_box on error).
297 * @param mixed $id (since 1.3.0) integer message id or array with integer ids
298 * @return integer number of expunged messages
299 * @since 1.0 or older
301 function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
304 $id = sqimap_message_list_squisher($id);
311 $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
312 $response, $message, $uid);
315 if (is_array($read)) {
316 foreach ($read as $r) {
317 if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
326 * Checks whether or not the specified mailbox exists
328 * @param stream $imap_stream imap connection resource
329 * @param string $mailbox mailbox name
330 * @param array $mailboxlist (since 1.5.1) optional array of mailboxes from
331 * sqimap_get_mailboxes() (to avoid having to talk to imap server)
333 * @since 1.0 or older
335 function sqimap_mailbox_exists ($imap_stream, $mailbox, $mailboxlist=null) {
336 if (!isset($mailbox) ||
empty($mailbox)) {
340 if (is_array($mailboxlist)) {
341 // use previously retrieved mailbox list
342 foreach ($mailboxlist as $mbox) {
343 if ($mbox['unformatted-dm'] == $mailbox) { return true; }
348 $mbx = sqimap_run_command($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mailbox),
349 true, $response, $message);
350 return isset($mbx[0]);
356 * Before 1.3.0 used more arguments and returned data depended on those arguments.
357 * @param stream $imap_stream imap connection resource
358 * @param string $mailbox mailbox name
359 * @return array results of select command (on success - permanentflags, flags and rights)
360 * @since 1.0 or older
362 function sqimap_mailbox_select ($imap_stream, $mailbox) {
363 if (empty($mailbox)) {
367 // cleanup $mailbox in order to prevent IMAP injection attacks
368 $mailbox = str_replace(array("\r","\n"), array("",""),$mailbox);
371 * Default UW IMAP server configuration allows to access other files
372 * on server. $imap_server_type is not checked because interface can
373 * be used with 'other' or any other server type setting. $mailbox
374 * variable can be modified in any script that uses variable from GET
375 * or POST. This code blocks all standard SquirrelMail IMAP API requests
376 * that use mailbox with full path (/etc/passwd) or with ../ characters
377 * in path (../../etc/passwd)
379 if (strstr($mailbox, '../') ||
substr($mailbox, 0, 1) == '/') {
381 error_box(sprintf(_("Invalid mailbox name: %s"),htmlspecialchars($mailbox)));
382 sqimap_logout($imap_stream);
383 $oTemplate->display('footer.tpl');
387 $read = sqimap_run_command($imap_stream, 'SELECT ' . sqimap_encode_mailbox_name($mailbox),
388 true, $response, $message);
390 for ($i = 0, $cnt = count($read); $i < $cnt; $i++
) {
391 if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
392 $result[strtoupper($regs[1])] = $regs[2];
393 } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
394 $result[strtoupper($regs[2])] = $regs[1];
396 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
397 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
398 $result['PERMANENTFLAGS'] = explode(' ',strtolower($regs[1]));
399 } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
400 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
401 $result['FLAGS'] = explode(' ',strtolower($regs[1]));
405 if (!isset($result['PERMANENTFLAGS'])) {
406 $result['PERMANENTFLAGS'] = $result['FLAGS'];
408 if (preg_match('/^\[(.+)\]/',$message, $regs)) {
409 $result['RIGHTS']=strtoupper($regs[1]);
418 * Mailbox is automatically subscribed.
420 * Set $type to string that does not match 'noselect' (case insensitive),
421 * if you don't want to prepend delimiter to mailbox name. Please note
422 * that 'noinferiors' might be used someday as keyword for folders
423 * that store only messages.
424 * @param stream $imap_steam imap connection resource
425 * @param string $mailbox mailbox name
426 * @param string $type folder type.
427 * @since 1.0 or older
429 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
431 if (strtolower($type) == 'noselect') {
432 $create_mailbox = $mailbox . $delimiter;
434 $create_mailbox = $mailbox;
437 $read_ary = sqimap_run_command($imap_stream, 'CREATE ' .
438 sqimap_encode_mailbox_name($create_mailbox),
439 true, $response, $message);
440 sqimap_subscribe ($imap_stream, $mailbox);
444 * Subscribes to an existing folder.
445 * @param stream $imap_stream imap connection resource
446 * @param string $mailbox mailbox name
447 * @param boolean $debug (since 1.5.1)
448 * @since 1.0 or older
450 function sqimap_subscribe ($imap_stream, $mailbox,$debug=true) {
451 $read_ary = sqimap_run_command($imap_stream, 'SUBSCRIBE ' .
452 sqimap_encode_mailbox_name($mailbox),
453 $debug, $response, $message);
457 * Unsubscribes from an existing folder
458 * @param stream $imap_stream imap connection resource
459 * @param string $mailbox mailbox name
460 * @since 1.0 or older
462 function sqimap_unsubscribe ($imap_stream, $mailbox) {
463 $read_ary = sqimap_run_command($imap_stream, 'UNSUBSCRIBE ' .
464 sqimap_encode_mailbox_name($mailbox),
465 false, $response, $message);
469 * Deletes the given folder
470 * Since 1.2.6 and 1.3.0 contains rename_or_delete_folder hook
471 * @param stream $imap_stream imap connection resource
472 * @param string $mailbox mailbox name
473 * @since 1.0 or older
475 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
476 global $data_dir, $username;
477 sqimap_unsubscribe ($imap_stream, $mailbox);
479 if (sqimap_mailbox_exists($imap_stream, $mailbox)) {
481 $read_ary = sqimap_run_command($imap_stream, 'DELETE ' .
482 sqimap_encode_mailbox_name($mailbox),
483 true, $response, $message);
484 if ($response !== 'OK') {
486 sqimap_subscribe ($imap_stream, $mailbox);
488 $temp = array(&$mailbox, 'delete', '');
489 do_hook('rename_or_delete_folder', $temp);
490 removePref($data_dir, $username, "thread_$mailbox");
491 removePref($data_dir, $username, "collapse_folder_$mailbox");
497 * Determines if the user is subscribed to the folder or not
498 * @param stream $imap_stream imap connection resource
499 * @param string $mailbox mailbox name
503 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
504 $boxesall = sqimap_mailbox_list ($imap_stream);
505 foreach ($boxesall as $ref) {
506 if ($ref['unformatted'] == $folder) {
515 * Since 1.2.6 and 1.3.0 contains rename_or_delete_folder hook
516 * @param stream $imap_stream imap connection resource
517 * @param string $old_name mailbox name
518 * @param string $new_name new mailbox name
521 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
522 if ( $old_name != $new_name ) {
523 global $delimiter, $imap_server_type, $data_dir, $username;
524 if ( substr( $old_name, -1 ) == $delimiter ) {
525 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
526 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
527 $postfix = $delimiter;
532 $boxesall = sqimap_mailbox_list_all($imap_stream);
533 $cmd = 'RENAME ' . sqimap_encode_mailbox_name($old_name) .
534 ' ' . sqimap_encode_mailbox_name($new_name);
535 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
536 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
537 $oldpref_thread = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
538 $oldpref_collapse = getPref($data_dir, $username, 'collapse_folder_'.$old_name.$postfix);
539 removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
540 removePref($data_dir, $username, 'collapse_folder_'.$old_name.$postfix);
541 sqimap_subscribe($imap_stream, $new_name.$postfix);
542 setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref_thread);
543 setPref($data_dir, $username, 'collapse_folder_'.$new_name.$postfix, $oldpref_collapse);
544 $temp = array(&$old_name, 'rename', &$new_name);
545 do_hook('rename_or_delete_folder', $temp);
546 $l = strlen( $old_name ) +
1;
549 foreach ($boxesall as $box) {
550 if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
551 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
552 /* With Cyrus IMAPd >= 2.0 rename is recursive, so don't check for errors here */
553 if ($imap_server_type == 'cyrus') {
554 $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
555 $data = sqimap_run_command($imap_stream, $cmd, false,
556 $response, $message);
558 $was_subscribed = sqimap_mailbox_is_subscribed($imap_stream, $box[$p]);
559 if ( $was_subscribed ) {
560 sqimap_unsubscribe($imap_stream, $box[$p]);
562 $oldpref_thread = getPref($data_dir, $username, 'thread_'.$box[$p]);
563 $oldpref_collapse = getPref($data_dir, $username, 'collapse_folder_'.$box[$p]);
564 removePref($data_dir, $username, 'thread_'.$box[$p]);
565 removePref($data_dir, $username, 'collapse_folder_'.$box[$p]);
566 if ( $was_subscribed ) {
567 sqimap_subscribe($imap_stream, $new_sub);
569 setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref_thread);
570 setPref($data_dir, $username, 'collapse_folder_'.$new_sub, $oldpref_collapse);
571 $temp = array(&$box[$p], 'rename', &$new_sub);
572 do_hook('rename_or_delete_folder', $temp);
579 * Formats a mailbox into parts for the $boxesall array
583 * <li>raw - Raw LIST/LSUB response from the IMAP server
584 * <li>formatted - nicely formatted folder name
585 * <li>unformatted - unformatted, but with delimiter at end removed
586 * <li>unformatted-dm - folder name as it appears in raw response
587 * <li>unformatted-disp - unformatted without $folder_prefix
588 * <li>id - TODO: document me
589 * <li>flags - TODO: document me
591 * Before 1.2.0 used third argument for delimiter.
593 * Before 1.5.1 used second argument for lsub line. Argument was removed in order to use
594 * find_mailbox_name() on the raw input. Since 1.5.1 includes RFC3501 names in flags
595 * array (for example, "\NoSelect" in addition to "noselect")
598 * @since 1.0 or older
599 * @todo document id and flags keys in boxes array and function arguments.
601 function sqimap_mailbox_parse ($line) {
602 global $folder_prefix, $delimiter;
604 /* Process each folder line */
605 for ($g = 0, $cnt = count($line); $g < $cnt; ++
$g) {
606 /* Store the raw IMAP reply */
607 if (isset($line[$g])) {
608 $boxesall[$g]['raw'] = $line[$g];
610 $boxesall[$g]['raw'] = '';
613 /* Count number of delimiters ($delimiter) in folder name */
614 $mailbox = find_mailbox_name($line[$g]);
615 $dm_count = substr_count($mailbox, $delimiter);
616 if (substr($mailbox, -1) == $delimiter) {
617 /* If name ends in delimiter, decrement count by one */
621 /* Format folder name, but only if it's a INBOX.* or has a parent. */
622 $boxesallbyname[$mailbox] = $g;
623 $parentfolder = readMailboxParent($mailbox, $delimiter);
624 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
625 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
626 (isset($boxesallbyname[$parentfolder]) &&
627 (strlen($parentfolder) > 0) ) ) {
628 $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
630 $boxesall[$g]['formatted'] = str_repeat(' ', $indent);
632 $boxesall[$g]['formatted'] = '';
634 $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
636 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
639 $boxesall[$g]['unformatted-dm'] = $mailbox;
640 if (substr($mailbox, -1) == $delimiter) {
641 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
643 $boxesall[$g]['unformatted'] = $mailbox;
644 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
645 $mailbox = substr($mailbox, strlen($folder_prefix));
647 $boxesall[$g]['unformatted-disp'] = $mailbox;
648 $boxesall[$g]['id'] = $g;
650 $boxesall[$g]['flags'] = array();
651 if (isset($line[$g])) {
652 ereg("\(([^)]*)\)",$line[$g],$regs);
654 * Since 1.5.1 flags are stored with RFC3501 naming
655 * and also the old way for backwards compatibility
656 * so for example "\NoSelect" and "noselect"
658 $flags = trim($regs[1]);
660 $flagsarr = explode(' ',$flags);
661 $flagsarrnew=$flagsarr;
663 foreach ($flagsarr as $flag) {
664 $flagsarrnew[]=strtolower(str_replace('\\', '',$flag));
666 $boxesall[$g]['flags']=$flagsarrnew;
674 * Returns an array of mailboxes available. Separated from sqimap_mailbox_option_list()
675 * below for template development.
677 * @author Steve Brown
680 function sqimap_mailbox_option_array($imap_stream, $folder_skip = 0, $boxes = 0,
681 $flag = 'noselect', $use_long_format = false ) {
682 global $username, $data_dir, $translate_special_folders, $sent_folder,
683 $trash_folder, $draft_folder;
685 $delimiter = sqimap_get_delimiter($imap_stream);
688 if ( $use_long_format ) {
689 $shorten_box_names = 0;
691 $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_MAILBOX_SELECT_INDENTED
);
695 $boxes = sqimap_mailbox_list($imap_stream);
699 foreach ($boxes as $boxes_part) {
700 if ($flag == NULL ||
(is_array($boxes_part['flags'])
701 && !in_array($flag, $boxes_part['flags']))) {
702 $box = $boxes_part['unformatted'];
704 if ($folder_skip != 0 && in_array($box, $folder_skip) ) {
707 $lowerbox = strtolower($box);
708 // mailboxes are casesensitive => inbox.sent != inbox.Sent
709 // nevermind, to many dependencies this should be fixed!
711 if (strtolower($box) == 'inbox') { // inbox is special and not casesensitive
714 switch ($shorten_box_names)
716 case SMPREF_MAILBOX_SELECT_DELIMITED
:
717 if ($translate_special_folders && $boxes_part['unformatted-dm']==$sent_folder) {
719 * calculate pad level from number of delimiters. do it inside if control in order
720 * to reduce number of calculations. Other folders don't need it.
722 $pad = str_pad('',7 * (count(explode($delimiter,$boxes_part['unformatted-dm']))-1),'. ');
723 // i18n: Name of Sent folder
724 $box2 = $pad . _("Sent");
725 } elseif ($translate_special_folders && $boxes_part['unformatted-dm']==$trash_folder) {
726 $pad = str_pad('',7 * (count(explode($delimiter,$boxes_part['unformatted-dm']))-1),'. ');
727 // i18n: Name of Trash folder
728 $box2 = $pad . _("Trash");
729 } elseif ($translate_special_folders && $boxes_part['unformatted-dm']==$draft_folder) {
730 $pad = str_pad('',7 * (count(explode($delimiter,$boxes_part['unformatted-dm']))-1),'. ');
731 // i18n: Name of Drafts folder
732 $box2 = $pad . _("Drafts");
734 $box2 = str_replace('&nbsp;&nbsp;', '. ', htmlspecialchars($boxes_part['formatted']));
737 case SMPREF_MAILBOX_SELECT_INDENTED
:
738 if ($translate_special_folders && $boxes_part['unformatted-dm']==$sent_folder) {
739 $pad = str_pad('',12 * (count(explode($delimiter,$boxes_part['unformatted-dm']))-1),' ');
740 $box2 = $pad . _("Sent");
741 } elseif ($translate_special_folders && $boxes_part['unformatted-dm']==$trash_folder) {
742 $pad = str_pad('',12 * (count(explode($delimiter,$boxes_part['unformatted-dm']))-1),' ');
743 $box2 = $pad . _("Trash");
744 } elseif ($translate_special_folders && $boxes_part['unformatted-dm']==$draft_folder) {
745 $pad = str_pad('',12 * (count(explode($delimiter,$boxes_part['unformatted-dm']))-1),' ');
746 $box2 = $pad . _("Drafts");
748 $box2 = str_replace('&nbsp;&nbsp;', ' ', htmlspecialchars($boxes_part['formatted']));
751 default: /* default, long names, style = 0 */
752 $box2 = str_replace(' ', ' ', htmlspecialchars(imap_utf7_decode_local($boxes_part['unformatted-disp'])));
757 $a[htmlspecialchars($box)] = $box2;
765 * Returns list of options (to be echoed into select statement
766 * based on available mailboxes and separators
767 * Caller should surround options with <select ...> </select> and
769 * @param stream $imap_stream imap connection resource to query for mailboxes
770 * @param array $show_selected array containing list of mailboxes to pre-select (0 if none)
771 * @param array $folder_skip array of folders to keep out of option list (compared in lower)
772 * @param $boxes list of already fetched boxes (for places like folder panel, where
773 * you know these options will be shown 3 times in a row.. (most often unset).
774 * @param string $flag (since 1.4.1) flag to check for in mailbox flags, used to filter out mailboxes.
775 * 'noselect' by default to remove unselectable mailboxes.
776 * 'noinferiors' used to filter out folders that can not contain subfolders.
777 * NULL to avoid flag check entirely.
778 * NOTE: noselect and noiferiors are used internally. The IMAP representation is
779 * \NoSelect and \NoInferiors
780 * @param boolean $use_long_format (since 1.4.1) override folder display preference and always show full folder name.
781 * @return string html formated mailbox selection options
784 function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0,
785 $flag = 'noselect', $use_long_format = false ) {
786 global $username, $data_dir, $translate_special_folders, $sent_folder,
787 $trash_folder, $draft_folder;
789 $boxes = sqimap_mailbox_option_array($imap_stream, $folder_skip, $boxes, $flag, $use_long_format);
792 foreach ($boxes as $value=>$option) {
793 $lowerbox = strtolower(htmlspecialchars($value));
795 if ($show_selected != 0) {
796 reset($show_selected);
797 while (!$sel && (list($x, $val) = each($show_selected))) {
798 if (strtolower($value) == strtolower(htmlspecialchars($val))) {
804 $str .= '<option value="'. $value .'"'. ($sel ?
' selected="selected"' : '').'>'. $option ."</option>\n";
811 * Returns sorted mailbox lists in several different ways.
813 * Since 1.5.1 most of the functionality has been moved to new function sqimap_get_mailboxes
815 * See comment on sqimap_mailbox_parse() for info about the returned array.
816 * @param resource $imap_stream imap connection resource
817 * @param boolean $force force update of mailbox listing. available since 1.4.2 and 1.5.0
818 * @return array list of mailboxes
819 * @since 1.0 or older
821 function sqimap_mailbox_list($imap_stream, $force=false) {
822 global $boxesnew,$show_only_subscribed_folders;
823 if (!sqgetGlobalVar('boxesnew',$boxesnew,SQ_SESSION
) ||
$force) {
824 $boxesnew=sqimap_get_mailboxes($imap_stream,$force,$show_only_subscribed_folders);
830 * Returns a list of all folders, subscribed or not
832 * Since 1.5.1 code moved to sqimap_get_mailboxes()
834 * @param stream $imap_stream imap connection resource
835 * @return array see sqimap_mailbox_parse()
836 * @since 1.0 or older
838 function sqimap_mailbox_list_all($imap_stream) {
839 global $show_only_subscribed_folders;
840 // fourth argument prevents registration of retrieved list of mailboxes in session
841 $boxes=sqimap_get_mailboxes($imap_stream,true,false,false);
847 * Gets the list of mailboxes for sqimap_maolbox_tree and sqimap_mailbox_list
849 * This is because both of those functions had duplicated logic, but with slightly different
850 * implementations. This will make both use the same implementation, which should make it
851 * easier to maintain and easier to modify in the future
852 * @param stream $imap_stream imap connection resource
853 * @param bool $force force a reload and ignore cache
854 * @param bool $show_only_subscribed controls listing of visible or all folders
855 * @param bool $session_register controls registration of retrieved data in session.
856 * @return object boxesnew - array of mailboxes and their attributes
859 function sqimap_get_mailboxes($imap_stream,$force=false,$show_only_subscribed=true,$session_register=true) {
860 global $show_only_subscribed_folders,$noselect_fix_enable,$folder_prefix,
861 $list_special_folders_first,$imap_server_type;
862 $inbox_subscribed = false;
863 $listsubscribed = sqimap_capability($imap_stream,'LIST-SUBSCRIBED');
865 if ($show_only_subscribed) { $show_only_subscribed=$show_only_subscribed_folders; }
867 //require_once(SM_PATH . 'include/load_prefs.php');
870 * There are three main listing commands we can use in IMAP:
871 * LSUB shows just the list of subscribed folders
872 * may include flags, but these are not necessarily accurate or authoratative
873 * \NoSelect has special meaning: the folder does not exist -OR- it means this
874 * folder is not subscribed but children may be
876 * LIST this shows every mailbox on the system
877 * flags are always included and are accurate and authoratative
878 * \NoSelect means folder should not be selected
880 * LIST (SUBSCRIBED) implemented with LIST-SUBSCRIBED extension
881 * this is like list but returns only subscribed folders
882 * flag meanings are like LIST, not LSUB
883 * \NonExistent means mailbox doesn't exist
884 * \PlaceHolder means parent is not valid (selectable), but one or more children are
885 * \NoSelect indeed means that the folder should not be selected
886 * IMAPEXT-LIST-EXTENSIONS-04 August 2003 B. Leiba
888 if (!$show_only_subscribed) {
890 $sub_cache_name='list_cache';
891 } elseif ($listsubscribed) {
892 $lsub = 'LIST (SUBSCRIBED)';
893 $sub_cache_name='listsub_cache';
896 $sub_cache_name='lsub_cache';
899 // Some IMAP servers allow subfolders to exist even if the parent folders do not
900 // This fixes some problems with the folder list when this is the case, causing the
901 // NoSelect folders to be displayed
902 if ($noselect_fix_enable) {
903 $lsub_args = "$lsub \"$folder_prefix\" \"*%\"";
904 $list_args = "LIST \"$folder_prefix\" \"*%\"";
906 $lsub_args = "$lsub \"$folder_prefix\" \"*\"";
907 $list_args = "LIST \"$folder_prefix\" \"*\"";
910 // get subscribed mailbox list from cache (session)
911 // if not there, then get it from the imap server and store in cache
914 sqgetGlobalVar($sub_cache_name,$lsub_cache,SQ_SESSION
);
917 $lsub_assoc_ary=array();
918 if (!empty($lsub_cache)) {
919 $lsub_assoc_ary=$lsub_cache;
921 $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args, true, $response, $message);
922 $lsub_ary = compact_mailboxes_response($lsub_ary);
923 if (!empty($lsub_ary)) {
924 foreach ($lsub_ary as $rawline) {
925 $temp_mailbox_name=find_mailbox_name($rawline);
926 $lsub_assoc_ary[$temp_mailbox_name]=$rawline;
929 sqsession_register($lsub_assoc_ary,$sub_cache_name);
933 // Now to get the mailbox flags
934 // The LSUB response may return \NoSelect flags, etc. but it is optional
935 // according to RFC3501, and even when returned it may not be accurate
936 // or authoratative. LIST will always return accurate results.
937 if (($lsub == 'LIST') ||
($lsub == 'LIST (SUBSCRIBED)')) {
938 // we've already done a LIST or LIST (SUBSCRIBED)
939 // and NOT a LSUB, so no need to do it again
940 $list_assoc_ary = $lsub_assoc_ary;
942 // we did a LSUB so now we need to do a LIST
943 // first see if it is in cache
944 $list_cache_name='list_cache';
946 sqgetGlobalVar($list_cache_name,$list_cache,SQ_SESSION
);
949 if (!empty($list_cache)) {
950 $list_assoc_ary=$list_cache;
951 // we could store this in list_cache_name but not necessary
953 // not in cache so we need to go get it from the imap server
954 $list_assoc_ary = array();
955 $list_ary = sqimap_run_command($imap_stream, $list_args,
956 true, $response, $message);
957 $list_ary = compact_mailboxes_response($list_ary);
958 if (!empty($list_ary)) {
959 foreach ($list_ary as $rawline) {
960 $temp_mailbox_name=find_mailbox_name($rawline);
961 $list_assoc_ary[$temp_mailbox_name]=$rawline;
964 sqsession_register($list_assoc_ary,$list_cache_name);
969 // If they aren't subscribed to the inbox, then add it anyway (if its in LIST)
970 $inbox_subscribed=false;
971 if (!empty($lsub_assoc_ary)) {
972 foreach ($lsub_assoc_ary as $temp_mailbox_name=>$rawline) {
973 if (strtoupper($temp_mailbox_name) == 'INBOX') {
974 $inbox_subscribed=true;
978 if (!$inbox_subscribed) {
979 if (!empty($list_assoc_ary)) {
980 foreach ($list_assoc_ary as $temp_mailbox_name=>$rawline) {
981 if (strtoupper($temp_mailbox_name) == 'INBOX') {
982 $lsub_assoc_ary[$temp_mailbox_name]=$rawline;
988 // Now we have the raw output, we need to create an array of mailbox names we will return
989 if (!$show_only_subscribed) {
990 $final_folders_assoc_ary=$list_assoc_ary;
993 * only show subscribed folders
994 * we need to merge the folders here... we can't trust the flags, etc. from the lsub_assoc_array
995 * so we use the lsub_assoc_array as the list of folders and the values come from list_assoc_array
997 if (!empty($lsub_assoc_ary)) {
998 foreach ($lsub_assoc_ary as $temp_mailbox_name=>$rawline) {
999 if (!empty($list_assoc_ary[$temp_mailbox_name])) {
1000 $final_folders_assoc_ary[$temp_mailbox_name]=$list_assoc_ary[$temp_mailbox_name];
1007 // Now produce a flat, sorted list
1008 if (!empty($final_folders_assoc_ary)) {
1009 uksort($final_folders_assoc_ary,'strnatcasecmp');
1010 foreach ($final_folders_assoc_ary as $temp_mailbox_name=>$rawline) {
1011 $final_folders_ary[]=$rawline;
1015 // this will put it into an array we can use later
1017 // raw - Raw LIST/LSUB response from the IMAP server
1018 // formatted - formatted folder name
1019 // unformatted - unformatted, but with the delimiter at the end removed
1020 // unformated-dm - folder name as it appears in raw response
1021 // unformatted-disp - unformatted without $folder_prefix
1022 // id - the array element number (0, 1, 2, etc.)
1023 // flags - mailbox flags
1024 if (!empty($final_folders_ary)) {
1025 $boxesall = sqimap_mailbox_parse($final_folders_ary);
1027 // they have no mailboxes
1031 /* Now, lets sort for special folders */
1032 $boxesnew = $used = array();
1035 $cnt = count($boxesall);
1036 $used = array_pad($used,$cnt,false);
1038 for($k = 0; $k < $cnt; ++
$k) {
1039 if (strtoupper($boxesall[$k]['unformatted']) == 'INBOX') {
1040 $boxesnew[] = $boxesall[$k];
1047 if ($has_inbox == false) {
1048 // do a list request for inbox because we should always show
1049 // inbox even if the user isn't subscribed to it.
1050 $inbox_ary = sqimap_run_command($imap_stream, 'LIST "" "INBOX"',
1051 true, $response, $message);
1052 $inbox_ary = compact_mailboxes_response($inbox_ary);
1053 if (count($inbox_ary)) {
1054 $inbox_entry = sqimap_mailbox_parse($inbox_ary);
1055 // add it on top of the list
1056 if (!empty($boxesnew)) {
1057 array_unshift($boxesnew,$inbox_entry[0]);
1059 $boxesnew[]=$inbox_entry[0];
1061 /* array_unshift($used,true); */
1065 /* List special folders and their subfolders, if requested. */
1066 if ($list_special_folders_first) {
1067 for($k = 0; $k < $cnt; ++
$k) {
1068 if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
1069 $boxesnew[] = $boxesall[$k];
1075 /* Find INBOX's children */
1076 for($k = 0; $k < $cnt; ++
$k) {
1077 $isboxbelow=isBoxBelow(strtoupper($boxesall[$k]['unformatted']),'INBOX');
1078 if (strtoupper($boxesall[$k]['unformatted']) == 'INBOX') {
1084 if (!$used[$k] && $isboxbelow && $is_inbox) {
1085 $boxesnew[] = $boxesall[$k];
1090 /* Rest of the folders */
1091 for($k = 0; $k < $cnt; $k++
) {
1093 $boxesnew[] = $boxesall[$k];
1097 * Don't register boxes in session, if $session_register is set to false
1098 * Prevents registration of sqimap_mailbox_list_all() results.
1100 if ($session_register) sqsession_register($boxesnew,'boxesnew');
1105 * Fills mailbox object
1107 * this is passed the mailbox array by left_main.php
1108 * who has previously obtained it from sqimap_get_mailboxes
1109 * that way, the raw mailbox list is available in left_main to other
1110 * things besides just sqimap_mailbox_tree
1111 * imap_stream is just used now to get status info
1113 * most of the functionality is moved to sqimap_get_mailboxes
1114 * also takes care of TODO items:
1115 * caching mailbox tree
1116 * config setting for UW imap section (not needed now)
1118 * Some code fragments are present in 1.3.0 - 1.4.4.
1119 * @param stream $imap_stream imap connection resource
1120 * @param array $lsub_ary output array from sqimap_get_mailboxes (contains mailboxes and flags)
1121 * @return object see mailboxes class.
1124 function sqimap_mailbox_tree($imap_stream,$lsub_ary) {
1126 $sorted_lsub_ary = array();
1127 $cnt = count($lsub_ary);
1128 for ($i = 0; $i < $cnt; $i++
) {
1129 $mbx=$lsub_ary[$i]['unformatted'];
1130 $flags=$lsub_ary[$i]['flags'];
1133 if (in_array('\Noinferiors',$flags)) { $noinferiors=1; }
1134 if (in_array('\NoInferiors',$flags)) { $noinferiors=1; }
1135 if (in_array('\HasNoChildren',$flags)) { $noinferiors=1; }
1138 if (in_array('\NoSelect',$flags)) { $noselect=1; }
1140 * LIST (SUBSCRIBED) has two new flags, \NonExistent which means the mailbox is subscribed to
1141 * but doesn't exist, and \PlaceHolder which is similar (but not the same) as \NoSelect
1142 * For right now, we'll treat these the same as \NoSelect and this behavior can be changed
1145 if (in_array('\NonExistent',$flags)) { $noselect=1; }
1146 if (in_array('\PlaceHolder',$flags)) { $noselect=1; }
1147 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
1150 $sorted_lsub_ary = array_values($sorted_lsub_ary);
1151 usort($sorted_lsub_ary, 'mbxSort');
1152 $boxestree = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream);
1157 * Callback function used for sorting mailboxes in sqimap_mailbox_tree
1160 * @return integer see php strnatcasecmp()
1163 function mbxSort($a, $b) {
1164 return strnatcasecmp($a['mbx'], $b['mbx']);
1168 * Fills mailbox object
1170 * Some code fragments are present in 1.3.0 - 1.4.4.
1171 * @param array $mbx_ary
1173 * @param stream $imap_stream imap connection resource
1174 * @return object see mailboxes class
1177 function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
1178 global $data_dir, $username, $list_special_folders_first,
1179 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
1180 $move_to_trash, $move_to_sent, $save_as_draft,
1181 $delimiter, $imap_server_type;
1183 // $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
1185 /* create virtual root node */
1186 $mailboxes= new mailboxes();
1187 $mailboxes->is_root
= true;
1191 if (isset($folder_prefix) && ($folder_prefix != '')) {
1192 $start = substr_count($folder_prefix,$delimiter);
1193 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
1194 $mailboxes->mailboxname_full
= substr($folder_prefix,0, (strlen($folder_prefix)-1));
1196 $mailboxes->mailboxname_full
= $folder_prefix;
1199 $mailboxes->mailboxname_sub
= $mailboxes->mailboxname_full
;
1204 $cnt = count($mbx_ary);
1205 for ($i=0; $i < $cnt; $i++
) {
1206 if ($mbx_ary[$i]['mbx'] !='' ) {
1207 $mbx = new mailboxes();
1208 $mailbox = $mbx_ary[$i]['mbx'];
1211 * Set the is_special flag if it concerned a special mailbox.
1212 * Used for displaying the special folders on top in the mailbox
1213 * tree displaying code.
1215 $mbx->is_special |
= ($mbx->is_inbox
= (strtoupper($mailbox) == 'INBOX'));
1216 $mbx->is_special |
= ($mbx->is_trash
= isTrashMailbox($mailbox));
1217 $mbx->is_special |
= ($mbx->is_sent
= isSentMailbox($mailbox));
1218 $mbx->is_special |
= ($mbx->is_draft
= isDraftMailbox($mailbox));
1220 if (!$mbx->is_special
)
1221 $mbx->is_special
= boolean_hook_function('special_mailbox', $mailbox, 1);
1223 if (isset($mbx_ary[$i]['unseen'])) {
1224 $mbx->unseen
= $mbx_ary[$i]['unseen'];
1226 if (isset($mbx_ary[$i]['nummessages'])) {
1227 $mbx->total
= $mbx_ary[$i]['nummessages'];
1230 $mbx->is_noselect
= $mbx_ary[$i]['noselect'];
1231 $mbx->is_noinferiors
= $mbx_ary[$i]['noinferiors'];
1233 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
1235 $mbx->mailboxname_sub
= substr($mbx_ary[$i]['mbx'],$r_del_pos+
1);
1236 } else { /* mailbox is root folder */
1237 $mbx->mailboxname_sub
= $mbx_ary[$i]['mbx'];
1239 $mbx->mailboxname_full
= $mbx_ary[$i]['mbx'];
1241 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
1244 sqimap_utf7_decode_mbx_tree($mailboxes);
1245 sqimap_get_status_mbx_tree($imap_stream,$mailboxes);
1250 * @param object $mbx_tree
1253 function sqimap_utf7_decode_mbx_tree(&$mbx_tree) {
1254 global $draft_folder, $sent_folder, $trash_folder, $translate_special_folders;
1256 /* decode folder name and set mailboxname_sub */
1257 if ($translate_special_folders && strtoupper($mbx_tree->mailboxname_full
) == 'INBOX') {
1258 $mbx_tree->mailboxname_sub
= _("INBOX");
1259 } elseif ($translate_special_folders && $mbx_tree->mailboxname_full
== $draft_folder) {
1260 $mbx_tree->mailboxname_sub
= _("Drafts");
1261 } elseif ($translate_special_folders && $mbx_tree->mailboxname_full
== $sent_folder) {
1262 $mbx_tree->mailboxname_sub
= _("Sent");
1263 } elseif ($translate_special_folders && $mbx_tree->mailboxname_full
== $trash_folder) {
1264 $mbx_tree->mailboxname_sub
= _("Trash");
1266 $mbx_tree->mailboxname_sub
= imap_utf7_decode_local($mbx_tree->mailboxname_sub
);
1269 if ($mbx_tree->mbxs
) {
1270 $iCnt = count($mbx_tree->mbxs
);
1271 for ($i=0;$i<$iCnt;++
$i) {
1272 sqimap_utf7_decode_mbx_tree($mbx_tree->mbxs
[$i]);
1278 * @param object $mbx_tree
1279 * @param array $aMbxs
1282 function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) {
1284 $aMbxs[] =& $mbx_tree;
1285 if ($mbx_tree->mbxs
) {
1286 $iCnt = count($mbx_tree->mbxs
);
1287 for ($i=0;$i<$iCnt;++
$i) {
1288 sqimap_tree_to_ref_array($mbx_tree->mbxs
[$i],$aMbxs);
1294 * @param stream $imap_stream imap connection resource
1295 * @param object $mbx_tree
1296 * @since since 1.5.0
1298 function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
1299 global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash;
1300 $aMbxs = $aQuery = array();
1301 sqimap_tree_to_ref_array($mbx_tree,$aMbxs);
1302 // remove the root node
1303 array_shift($aMbxs);
1305 if($unseen_notify == 3) {
1306 $cnt = count($aMbxs);
1307 for($i=0;$i<$cnt;++
$i) {
1308 $oMbx =& $aMbxs[$i];
1309 if (!$oMbx->is_noselect
) {
1310 $mbx = $oMbx->mailboxname_full
;
1311 if ($unseen_type == 2 ||
1312 ($move_to_trash && $oMbx->mailboxname_full
== $trash_folder)) {
1313 $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (MESSAGES UNSEEN RECENT)';
1315 $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (UNSEEN RECENT)';
1317 sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
1319 $oMbx->unseen
= $oMbx->total
= $oMbx->recent
= false;
1323 $aMbxs[$i] =& $oMbx;
1325 // execute all the queries at once
1326 $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
1327 $cnt = count($aMbxs);
1328 for($i=0;$i<$cnt;++
$i) {
1329 $oMbx =& $aMbxs[$i];
1331 if ($tag && $aServerResponse[$tag] == 'OK') {
1332 $sResponse = implode('', $aResponse[$tag]);
1333 if (preg_match('/UNSEEN\s+([0-9]+)/i', $sResponse, $regs)) {
1334 $oMbx->unseen
= $regs[1];
1336 if (preg_match('/MESSAGES\s+([0-9]+)/i', $sResponse, $regs)) {
1337 $oMbx->total
= $regs[1];
1339 if (preg_match('/RECENT\s+([0-9]+)/i', $sResponse, $regs)) {
1340 $oMbx->recent
= $regs[1];
1346 } else if ($unseen_notify == 2) { // INBOX only
1347 $cnt = count($aMbxs);
1348 for($i=0;$i<$cnt;++
$i) {
1349 $oMbx =& $aMbxs[$i];
1350 if (strtoupper($oMbx->mailboxname_full
) == 'INBOX' ||
1351 ($move_to_trash && $oMbx->mailboxname_full
== $trash_folder)) {
1352 if ($unseen_type == 2 ||
1353 ($oMbx->mailboxname_full
== $trash_folder && $move_to_trash)) {
1354 $aStatus = sqimap_status_messages($imap_stream,$oMbx->mailboxname_full
);
1355 $oMbx->unseen
= $aStatus['UNSEEN'];
1356 $oMbx->total
= $aStatus['MESSAGES'];
1357 $oMbx->recent
= $aStatus['RECENT'];
1359 $oMbx->unseen
= sqimap_unseen_messages($imap_stream,$oMbx->mailboxname_full
);
1361 $aMbxs[$i] =& $oMbx;
1362 if (!$move_to_trash && $trash_folder) {
1365 // trash comes after INBOX
1366 if ($oMbx->mailboxname_full
== $trash_folder) {
1374 $cnt = count($aMbxs);
1375 for($i=0;$i<$cnt;++
$i) {
1376 $oMbx =& $aMbxs[$i];
1377 unset($hook_status);
1378 if (!empty($oMbx->unseen
)) { $hook_status['UNSEEN']=$oMbx->unseen
; }
1379 if (!empty($oMbx->total
)) { $hook_status['MESSAGES']=$oMbx->total
; }
1380 if (!empty($oMbx->recent
)) { $hook_status['RECENT']=$oMbx->recent
; }
1381 if (!empty($hook_status))
1383 $hook_status['MAILBOX']=$oMbx->mailboxname_full
;
1384 $hook_status['CALLER']='sqimap_get_status_mbx_tree'; // helps w/ debugging
1385 do_hook('folder_status', $hook_status);
1391 * Checks if folder is noselect (can't store messages)
1393 * Function does not check if folder subscribed.
1394 * @param stream $oImapStream imap connection resource
1395 * @param string $sImapFolder imap folder name
1396 * @param object $oBoxes mailboxes class object.
1397 * @return boolean true, when folder has noselect flag. false in any other case.
1400 function sqimap_mailbox_is_noselect($oImapStream,$sImapFolder,&$oBoxes) {
1401 // build mailbox object if it is not available
1402 if (! is_object($oBoxes)) $oBoxes=sqimap_mailbox_list($oImapStream);
1403 foreach($oBoxes as $box) {
1404 if ($box['unformatted']==$sImapFolder) {
1405 return (bool) check_is_noselect($box['raw']);
1412 * Checks if folder is noinferiors (can't store other folders)
1414 * Function does not check if folder subscribed.
1415 * @param stream $oImapStream imap connection resource
1416 * @param string $sImapFolder imap folder name
1417 * @param object $oBoxes mailboxes class object.
1418 * @return boolean true, when folder has noinferiors flag. false in any other case.
1421 function sqimap_mailbox_is_noinferiors($oImapStream,$sImapFolder,&$oBoxes) {
1422 // build mailbox object if it is not available
1423 if (! is_object($oBoxes)) $oBoxes=sqimap_mailbox_list($oImapStream);
1424 foreach($oBoxes as $box) {
1425 if ($box['unformatted']==$sImapFolder) {
1426 return (bool) check_is_noinferiors($box['raw']);