6 * This implements all functions that manipulate mailboxes
8 * @copyright 1999-2017 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 (preg_match('/^(\* [A-Z]+.*)\{[0-9]+\}([ \n\r\t]*)$/', $ary[$i], $regs)) {
114 $ary[$i] = $regs[1] . '"' . addslashes(trim($ary[$i+
1])) . '"' . $regs[2];
115 array_splice($ary, $i+
1, 2);
119 /* remove duplicates and ensure array is contiguous */
120 return array_values(array_unique($ary));
124 * Extract the mailbox name from an untagged LIST (7.2.2) or LSUB (7.2.3) answer
125 * (LIST|LSUB) (<Flags list>) (NIL|"<separator atom>") <mailbox name string>\r\n
126 * mailbox name in quoted string MUST be unquoted and stripslashed (sm API)
128 * Originally stored in functions/strings.php. Since 1.2.6 stored in
129 * functions/imap_mailbox.php
130 * @param string $line imap LIST/LSUB response line
131 * @return string mailbox name
133 function find_mailbox_name($line) {
134 if (preg_match('/^\* (?:LIST|LSUB) \([^\)]*\) (?:NIL|\"[^\"]*\") ([^\r\n]*)[\r\n]*$/i', $line, $regs)) {
135 if (substr($regs[1], 0, 1) == '"')
136 return stripslashes(substr($regs[1], 1, -1));
143 * Detects if mailbox has noselect flag (can't store messages)
144 * In versions older than 1.4.5 function checks only LSUB responses
145 * and can produce pcre warnings.
146 * @param string $lsub_line mailbox line from untagged LIST or LSUB response
147 * @return bool whether this is a Noselect mailbox.
150 function check_is_noselect ($lsub_line) {
151 return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noselect[^\)]*\)/i", $lsub_line);
155 * Detects if mailbox has noinferiors flag (can't store subfolders)
156 * @param string $lsub_line mailbox line from untagged LIST or LSUB response
157 * @return bool whether this is a Noinferiors mailbox.
160 function check_is_noinferiors ($lsub_line) {
161 return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noinferiors[^\)]*\)/i", $lsub_line);
165 * Detects mailbox's parent folder
167 * If $haystack is a full mailbox name, and $needle is the mailbox
168 * separator character, returns the second last part of the full
169 * mailbox name (i.e. the mailbox's parent mailbox)
171 * Originally stored in functions/strings.php. Since 1.2.6 stored in
172 * functions/imap_mailbox.php
173 * @param string $haystack full mailbox name
174 * @param string $needle delimiter
175 * @return string parent mailbox
177 function readMailboxParent($haystack, $needle) {
181 $parts = explode($needle, $haystack);
182 $elem = array_pop($parts);
183 while ($elem == '' && count($parts)) {
184 $elem = array_pop($parts);
186 $ret = join($needle, $parts);
192 * Check if $subbox is below the specified $parentbox
193 * @param string $subbox potential sub folder
194 * @param string $parentbox potential parent
198 function isBoxBelow( $subbox, $parentbox ) {
201 * Eliminate the obvious mismatch, where the
202 * subfolder path is shorter than that of the potential parent
204 if ( strlen($subbox) < strlen($parentbox) ) {
207 /* check for delimiter */
208 if (substr($parentbox,-1) != $delimiter) {
209 $parentbox .= $delimiter;
212 return (substr($subbox,0,strlen($parentbox)) == $parentbox);
216 * Defines special mailboxes: given a mailbox name, it checks if this is a
217 * "special" one: INBOX, Trash, Sent or Draft.
219 * Since 1.2.5 function includes special_mailbox hook.
221 * Since 1.4.3 hook supports more than one plugin.
223 //FIXME: make $subfolders_of_inbox_are_special a configuration setting in conf.pl and config.php
224 * Since 1.4.22/1.5.2, the administrator can add
225 * $subfolders_of_inbox_are_special = TRUE;
226 * to config/config_local.php and all subfolders
227 * of the INBOX will be treated as special.
229 * @param string $box mailbox name
230 * @param boolean $include_subs (since 1.5.2) if true, subfolders of system
231 * folders are special. if false, subfolders are not special mailboxes
232 * unless they are tagged as special in 'special_mailbox' hook.
236 function isSpecialMailbox($box,$include_subs=true) {
237 global $subfolders_of_inbox_are_special;
238 $ret = ( ($subfolders_of_inbox_are_special && isInboxMailbox($box,$include_subs)) ||
239 (!$subfolders_of_inbox_are_special && strtolower($box) == 'inbox') ||
240 isTrashMailbox($box,$include_subs) ||
241 isSentMailbox($box,$include_subs) ||
242 isDraftMailbox($box,$include_subs) );
245 $ret = boolean_hook_function('special_mailbox', $box, 1);
251 * Detects if mailbox is the Inbox folder or subfolder of the Inbox
253 * @param string $box The mailbox name to test
254 * @param boolean $include_subs If true, subfolders of system folders
255 * are special. If false, subfolders are
256 * not special mailboxes.
258 * @return boolean Whether this is the Inbox or a child thereof.
262 function isInboxMailbox($box, $include_subs=TRUE) {
263 return ((strtolower($box) == 'inbox')
264 ||
($include_subs && isBoxBelow(strtolower($box), 'inbox')));
268 * Detects if mailbox is a Trash folder or subfolder of Trash
269 * @param string $box mailbox name
270 * @param boolean $include_subs (since 1.5.2) if true, subfolders of system
271 * folders are special. if false, subfolders are not special mailboxes.
272 * @return bool whether this is a Trash folder
275 function isTrashMailbox ($box,$include_subs=true) {
276 global $trash_folder, $move_to_trash;
277 return $move_to_trash && $trash_folder &&
278 ( $box == $trash_folder ||
279 ($include_subs && isBoxBelow($box, $trash_folder)) );
283 * Detects if mailbox is a Sent folder or subfolder of Sent
284 * @param string $box mailbox name
285 * @param boolean $include_subs (since 1.5.2) if true, subfolders of system
286 * folders are special. if false, subfolders are not special mailboxes.
287 * @return bool whether this is a Sent folder
290 function isSentMailbox($box,$include_subs=true) {
291 global $sent_folder, $move_to_sent;
292 return $move_to_sent && $sent_folder &&
293 ( $box == $sent_folder ||
294 ($include_subs && isBoxBelow($box, $sent_folder)) );
298 * Detects if mailbox is a Drafts folder or subfolder of Drafts
299 * @param string $box mailbox name
300 * @param boolean $include_subs (since 1.5.2) if true, subfolders of system
301 * folders are special. if false, subfolders are not special mailboxes.
302 * @return bool whether this is a Draft folder
305 function isDraftMailbox($box,$include_subs=true) {
306 global $draft_folder, $save_as_draft;
307 return $save_as_draft &&
308 ( $box == $draft_folder ||
309 ($include_subs && isBoxBelow($box, $draft_folder)) );
313 * Is the given folder "sent-like" in nature?
315 * The most obvious use of this is to know what folders you usually
316 * want to show the To field instead of the From field on the mailbox list
318 * This function returns TRUE if the given folder is the sent
319 * folder (or any of its subfolders) or if it is the draft
320 * folder (or any of its subfolders)
322 * @param string $mailbox
324 * @return boolean See explanation above
327 function handleAsSent($mailbox) {
328 global $handleAsSent_result;
330 /* First check if this is the sent or draft folder. */
331 $handleAsSent_result = isSentMailbox($mailbox) ||
isDraftMailbox($mailbox);
333 /* Then check the result of the handleAsSent hook. */
334 do_hook('check_handleAsSent_result', $mailbox);
336 /* And return the result. */
337 return $handleAsSent_result;
343 * WARNING: Select mailbox before calling this function.
345 * permanently removes all messages that have the \Deleted flag
346 * set from the selected mailbox. See EXPUNGE command chapter in
348 * @param stream $imap_stream imap connection resource
349 * @param string $mailbox mailbox name (unused since 1.1.3).
350 * @param boolean $handle_errors error handling control (displays error_box on error).
351 * @param mixed $id (since 1.3.0) integer message id or array with integer ids
352 * @return integer number of expunged messages
353 * @since 1.0 or older
355 function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
358 $id = sqimap_message_list_squisher($id);
365 $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
366 $response, $message, $uid);
369 if (is_array($read)) {
370 foreach ($read as $r) {
371 if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
380 * Checks whether or not the specified mailbox exists
382 * @param stream $imap_stream imap connection resource
383 * @param string $mailbox mailbox name
384 * @param array $mailboxlist (since 1.5.1) optional array of mailboxes from
385 * sqimap_get_mailboxes() (to avoid having to talk to imap server)
387 * @since 1.0 or older
389 function sqimap_mailbox_exists ($imap_stream, $mailbox, $mailboxlist=null) {
390 if (!isset($mailbox) ||
empty($mailbox)) {
394 if (is_array($mailboxlist)) {
395 // use previously retrieved mailbox list
396 foreach ($mailboxlist as $mbox) {
397 if ($mbox['unformatted-dm'] == $mailbox) { return true; }
402 $mbx = sqimap_run_command($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mailbox),
403 true, $response, $message);
404 return isset($mbx[0]);
410 * Before 1.3.0 used more arguments and returned data depended on those arguments.
411 * @param stream $imap_stream imap connection resource
412 * @param string $mailbox mailbox name
413 * @param boolean $handle_errors When TRUE, IMAP errors
414 * are handled herein, causing
415 * an error to be displayed on
416 * screen and execution to stop
417 * and when FALSE, error status
418 * is returned to the caller
419 * (OPTIONAL; default is TRUE)
420 * @return array results of select command (on success - permanentflags, flags and rights)
421 * @since 1.0 or older
423 function sqimap_mailbox_select ($imap_stream, $mailbox, $handle_errors=true) {
424 if (empty($mailbox)) {
428 // cleanup $mailbox in order to prevent IMAP injection attacks
429 $mailbox = str_replace(array("\r","\n"), array("",""),$mailbox);
432 * Default UW IMAP server configuration allows to access other files
433 * on server. $imap_server_type is not checked because interface can
434 * be used with 'other' or any other server type setting. $mailbox
435 * variable can be modified in any script that uses variable from GET
436 * or POST. This code blocks all standard SquirrelMail IMAP API requests
437 * that use mailbox with full path (/etc/passwd) or with ../ characters
438 * in path (../../etc/passwd)
440 if (strstr($mailbox, '../') ||
substr($mailbox, 0, 1) == '/') {
442 error_box(sprintf(_("Invalid mailbox name: %s"),sm_encode_html_special_chars($mailbox)));
443 sqimap_logout($imap_stream);
444 $oTemplate->display('footer.tpl');
448 $read = sqimap_run_command($imap_stream, 'SELECT ' . sqimap_encode_mailbox_name($mailbox),
449 $handle_errors, $response, $message);
451 for ($i = 0, $cnt = count($read); $i < $cnt; $i++
) {
452 if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
453 $result[strtoupper($regs[1])] = $regs[2];
454 } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
455 $result[strtoupper($regs[2])] = $regs[1];
457 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
458 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
459 $result['PERMANENTFLAGS'] = explode(' ',strtolower($regs[1]));
460 } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
461 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
462 $result['FLAGS'] = explode(' ',strtolower($regs[1]));
466 if (!isset($result['PERMANENTFLAGS'])) {
467 $result['PERMANENTFLAGS'] = $result['FLAGS'];
469 if (preg_match('/^\[(.+)\]/',$message, $regs)) {
470 $result['RIGHTS']=strtoupper($regs[1]);
479 * Mailbox is automatically subscribed.
481 * Set $type to string that does not match 'noselect' (case insensitive),
482 * if you don't want to prepend delimiter to mailbox name. Please note
483 * that 'noinferiors' might be used someday as keyword for folders
484 * that store only messages.
485 * @param stream $imap_steam imap connection resource
486 * @param string $mailbox mailbox name
487 * @param string $type folder type.
488 * @since 1.0 or older
490 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
492 if (strtolower($type) == 'noselect') {
493 $create_mailbox = $mailbox . $delimiter;
495 $create_mailbox = $mailbox;
498 $read_ary = sqimap_run_command($imap_stream, 'CREATE ' .
499 sqimap_encode_mailbox_name($create_mailbox),
500 true, $response, $message);
501 sqimap_subscribe ($imap_stream, $mailbox);
505 * Subscribes to an existing folder.
506 * @param stream $imap_stream imap connection resource
507 * @param string $mailbox mailbox name
508 * @param boolean $debug (since 1.5.1)
509 * @since 1.0 or older
511 function sqimap_subscribe ($imap_stream, $mailbox,$debug=true) {
512 $read_ary = sqimap_run_command($imap_stream, 'SUBSCRIBE ' .
513 sqimap_encode_mailbox_name($mailbox),
514 $debug, $response, $message);
518 * Unsubscribes from an existing folder
519 * @param stream $imap_stream imap connection resource
520 * @param string $mailbox mailbox name
521 * @since 1.0 or older
523 function sqimap_unsubscribe ($imap_stream, $mailbox) {
524 $read_ary = sqimap_run_command($imap_stream, 'UNSUBSCRIBE ' .
525 sqimap_encode_mailbox_name($mailbox),
526 false, $response, $message);
530 * Deletes the given folder
531 * Since 1.2.6 and 1.3.0 contains rename_or_delete_folder hook
532 * @param stream $imap_stream imap connection resource
533 * @param string $mailbox mailbox name
534 * @since 1.0 or older
536 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
537 global $data_dir, $username;
538 sqimap_unsubscribe ($imap_stream, $mailbox);
540 if (sqimap_mailbox_exists($imap_stream, $mailbox)) {
542 $read_ary = sqimap_run_command($imap_stream, 'DELETE ' .
543 sqimap_encode_mailbox_name($mailbox),
544 true, $response, $message);
545 if ($response !== 'OK') {
547 sqimap_subscribe ($imap_stream, $mailbox);
549 $temp = array(&$mailbox, 'delete', '');
550 do_hook('rename_or_delete_folder', $temp);
551 removePref($data_dir, $username, "thread_$mailbox");
552 removePref($data_dir, $username, "collapse_folder_$mailbox");
558 * Determines if the user is subscribed to the folder or not
559 * @param stream $imap_stream imap connection resource
560 * @param string $mailbox mailbox name
564 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
565 $boxesall = sqimap_mailbox_list ($imap_stream);
566 foreach ($boxesall as $ref) {
567 if ($ref['unformatted'] == $folder) {
576 * Since 1.2.6 and 1.3.0 contains rename_or_delete_folder hook
577 * @param stream $imap_stream imap connection resource
578 * @param string $old_name mailbox name
579 * @param string $new_name new mailbox name
582 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
583 if ( $old_name != $new_name ) {
584 global $delimiter, $imap_server_type, $data_dir, $username;
585 if ( substr( $old_name, -1 ) == $delimiter ) {
586 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
587 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
588 $postfix = $delimiter;
593 $boxesall = sqimap_mailbox_list_all($imap_stream);
594 $cmd = 'RENAME ' . sqimap_encode_mailbox_name($old_name) .
595 ' ' . sqimap_encode_mailbox_name($new_name);
596 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
597 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
598 $oldpref_thread = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
599 $oldpref_collapse = getPref($data_dir, $username, 'collapse_folder_'.$old_name.$postfix);
600 removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
601 removePref($data_dir, $username, 'collapse_folder_'.$old_name.$postfix);
602 sqimap_subscribe($imap_stream, $new_name.$postfix);
603 setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref_thread);
604 setPref($data_dir, $username, 'collapse_folder_'.$new_name.$postfix, $oldpref_collapse);
605 $temp = array(&$old_name, 'rename', &$new_name);
606 do_hook('rename_or_delete_folder', $temp);
607 $l = strlen( $old_name ) +
1;
610 foreach ($boxesall as $box) {
611 if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
612 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
613 /* With Cyrus IMAPd >= 2.0 rename is recursive, so don't check for errors here */
614 if ($imap_server_type == 'cyrus') {
615 $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
616 $data = sqimap_run_command($imap_stream, $cmd, false,
617 $response, $message);
619 $was_subscribed = sqimap_mailbox_is_subscribed($imap_stream, $box[$p]);
620 if ( $was_subscribed ) {
621 sqimap_unsubscribe($imap_stream, $box[$p]);
623 $oldpref_thread = getPref($data_dir, $username, 'thread_'.$box[$p]);
624 $oldpref_collapse = getPref($data_dir, $username, 'collapse_folder_'.$box[$p]);
625 removePref($data_dir, $username, 'thread_'.$box[$p]);
626 removePref($data_dir, $username, 'collapse_folder_'.$box[$p]);
627 if ( $was_subscribed ) {
628 sqimap_subscribe($imap_stream, $new_sub);
630 setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref_thread);
631 setPref($data_dir, $username, 'collapse_folder_'.$new_sub, $oldpref_collapse);
632 $temp = array(&$box[$p], 'rename', &$new_sub);
633 do_hook('rename_or_delete_folder', $temp);
640 * Formats a mailbox into parts for the $boxesall array
644 * <li>raw - Raw LIST/LSUB response from the IMAP server
645 * <li>formatted - nicely formatted folder name
646 * <li>unformatted - unformatted, but with delimiter at end removed
647 * <li>unformatted-dm - folder name as it appears in raw response
648 * <li>unformatted-disp - unformatted without $folder_prefix
649 * <li>id - TODO: document me
650 * <li>flags - TODO: document me
652 * Before 1.2.0 used third argument for delimiter.
654 * Before 1.5.1 used second argument for lsub line. Argument was removed in order to use
655 * find_mailbox_name() on the raw input. Since 1.5.1 includes RFC3501 names in flags
656 * array (for example, "\NoSelect" in addition to "noselect")
659 * @since 1.0 or older
660 * @todo document id and flags keys in boxes array and function arguments.
662 function sqimap_mailbox_parse ($line) {
663 global $folder_prefix, $delimiter;
665 /* Process each folder line */
666 ksort($line); // get physical ordering same as alphabetical sort we did before now (might be a better place for this)
667 foreach ($line as $g => $l)
668 // was this but array not guaranteed to be contiguous: for ($g = 0, $cnt = count($line); $g < $cnt; ++$g)
670 /* Store the raw IMAP reply */
671 if (isset($line[$g])) {
672 $boxesall[$g]['raw'] = $line[$g];
674 $boxesall[$g]['raw'] = '';
677 /* Count number of delimiters ($delimiter) in folder name */
678 $mailbox = find_mailbox_name($line[$g]);
679 $dm_count = substr_count($mailbox, $delimiter);
680 if (substr($mailbox, -1) == $delimiter) {
681 /* If name ends in delimiter, decrement count by one */
685 /* Format folder name, but only if it's a INBOX.* or has a parent. */
686 $boxesallbyname[$mailbox] = $g;
687 $parentfolder = readMailboxParent($mailbox, $delimiter);
688 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
689 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
690 (isset($boxesallbyname[$parentfolder]) &&
691 (strlen($parentfolder) > 0) ) ) {
692 $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
694 $boxesall[$g]['formatted'] = str_repeat(' ', $indent);
696 $boxesall[$g]['formatted'] = '';
698 $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
700 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
703 $boxesall[$g]['unformatted-dm'] = $mailbox;
704 if (substr($mailbox, -1) == $delimiter) {
705 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
707 $boxesall[$g]['unformatted'] = $mailbox;
708 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
709 $mailbox = substr($mailbox, strlen($folder_prefix));
711 $boxesall[$g]['unformatted-disp'] = $mailbox;
712 $boxesall[$g]['id'] = $g;
714 $boxesall[$g]['flags'] = array();
715 if (isset($line[$g]) && preg_match('/\(([^)]*)\)/',$line[$g],$regs) ) {
717 * Since 1.5.1 flags are stored with RFC3501 naming
718 * and also the old way for backwards compatibility
719 * so for example "\NoSelect" and "noselect"
721 $flags = trim($regs[1]);
723 $flagsarr = explode(' ',$flags);
724 $flagsarrnew=$flagsarr;
726 foreach ($flagsarr as $flag) {
727 $flagsarrnew[]=strtolower(str_replace('\\', '',$flag));
729 $boxesall[$g]['flags']=$flagsarrnew;
737 * Returns an array of mailboxes available. Separated from sqimap_mailbox_option_list()
738 * below for template development.
740 * @author Steve Brown
743 function sqimap_mailbox_option_array($imap_stream, $folder_skip = 0, $boxes = 0,
744 $flag = 'noselect', $use_long_format = false ) {
745 global $username, $data_dir, $translate_special_folders, $sent_folder,
746 $trash_folder, $draft_folder;
748 $delimiter = sqimap_get_delimiter($imap_stream);
751 if ( $use_long_format ) {
752 $shorten_box_names = 0;
754 $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_MAILBOX_SELECT_INDENTED
);
758 $boxes = sqimap_mailbox_list($imap_stream);
762 foreach ($boxes as $boxes_part) {
763 if ($flag == NULL ||
(is_array($boxes_part['flags'])
764 && !in_array($flag, $boxes_part['flags']))) {
765 $box = $boxes_part['unformatted'];
767 if ($folder_skip != 0 && in_array($box, $folder_skip) ) {
770 $lowerbox = strtolower($box);
771 // mailboxes are casesensitive => inbox.sent != inbox.Sent
772 // nevermind, to many dependencies this should be fixed!
774 if (strtolower($box) == 'inbox') { // inbox is special and not casesensitive
777 switch ($shorten_box_names)
779 case SMPREF_MAILBOX_SELECT_DELIMITED
:
780 if ($translate_special_folders && $boxes_part['unformatted-dm']==$sent_folder) {
782 * calculate pad level from number of delimiters. do it inside if control in order
783 * to reduce number of calculations. Other folders don't need it.
785 $pad = str_pad('',7 * (count(explode($delimiter,$boxes_part['unformatted-dm']))-1),'. ');
786 // i18n: Name of Sent folder
787 $box2 = $pad . _("Sent");
788 } elseif ($translate_special_folders && $boxes_part['unformatted-dm']==$trash_folder) {
789 $pad = str_pad('',7 * (count(explode($delimiter,$boxes_part['unformatted-dm']))-1),'. ');
790 // i18n: Name of Trash folder
791 $box2 = $pad . _("Trash");
792 } elseif ($translate_special_folders && $boxes_part['unformatted-dm']==$draft_folder) {
793 $pad = str_pad('',7 * (count(explode($delimiter,$boxes_part['unformatted-dm']))-1),'. ');
794 // i18n: Name of Drafts folder
795 $box2 = $pad . _("Drafts");
797 $box2 = str_replace('&nbsp;&nbsp;', '. ', sm_encode_html_special_chars($boxes_part['formatted']));
800 case SMPREF_MAILBOX_SELECT_INDENTED
:
801 if ($translate_special_folders && $boxes_part['unformatted-dm']==$sent_folder) {
802 $pad = str_pad('',12 * (count(explode($delimiter,$boxes_part['unformatted-dm']))-1),' ');
803 $box2 = $pad . _("Sent");
804 } elseif ($translate_special_folders && $boxes_part['unformatted-dm']==$trash_folder) {
805 $pad = str_pad('',12 * (count(explode($delimiter,$boxes_part['unformatted-dm']))-1),' ');
806 $box2 = $pad . _("Trash");
807 } elseif ($translate_special_folders && $boxes_part['unformatted-dm']==$draft_folder) {
808 $pad = str_pad('',12 * (count(explode($delimiter,$boxes_part['unformatted-dm']))-1),' ');
809 $box2 = $pad . _("Drafts");
811 $box2 = str_replace('&nbsp;&nbsp;', ' ', sm_encode_html_special_chars($boxes_part['formatted']));
814 default: /* default, long names, style = 0 */
815 $box2 = str_replace(' ', ' ', sm_encode_html_special_chars(imap_utf7_decode_local($boxes_part['unformatted-disp'])));
820 $a[sm_encode_html_special_chars($box)] = $box2;
828 * Returns list of options (to be echoed into select statement
829 * based on available mailboxes and separators
830 * Caller should surround options with <select ...> </select> and
832 * @param stream $imap_stream imap connection resource to query for mailboxes
833 * @param array $show_selected array containing list of mailboxes to pre-select (0 if none)
834 * @param array $folder_skip array of folders to keep out of option list (compared in lower)
835 * @param $boxes list of already fetched boxes (for places like folder panel, where
836 * you know these options will be shown 3 times in a row.. (most often unset).
837 * @param string $flag (since 1.4.1) flag to check for in mailbox flags, used to filter out mailboxes.
838 * 'noselect' by default to remove unselectable mailboxes.
839 * 'noinferiors' used to filter out folders that can not contain subfolders.
840 * NULL to avoid flag check entirely.
841 * NOTE: noselect and noiferiors are used internally. The IMAP representation is
842 * \NoSelect and \NoInferiors
843 * @param boolean $use_long_format (since 1.4.1) override folder display preference and always show full folder name.
844 * @return string html formated mailbox selection options
847 function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0,
848 $flag = 'noselect', $use_long_format = false ) {
849 global $username, $data_dir, $translate_special_folders, $sent_folder,
850 $trash_folder, $draft_folder;
852 $boxes = sqimap_mailbox_option_array($imap_stream, $folder_skip, $boxes, $flag, $use_long_format);
855 foreach ($boxes as $value=>$option) {
856 $lowerbox = strtolower(sm_encode_html_special_chars($value));
858 if ($show_selected != 0) {
859 reset($show_selected);
860 while (!$sel && (list($x, $val) = each($show_selected))) {
861 if (strtolower($value) == strtolower(sm_encode_html_special_chars($val))) {
867 $str .= '<option value="'. $value .'"'. ($sel ?
' selected="selected"' : '').'>'. $option ."</option>\n";
874 * Returns sorted mailbox lists in several different ways.
876 * Since 1.5.1 most of the functionality has been moved to new function sqimap_get_mailboxes
878 * See comment on sqimap_mailbox_parse() for info about the returned array.
879 * @param resource $imap_stream imap connection resource
880 * @param boolean $force force update of mailbox listing. available since 1.4.2 and 1.5.0
881 * @return array list of mailboxes
882 * @since 1.0 or older
884 function sqimap_mailbox_list($imap_stream, $force=false) {
885 global $boxesnew,$show_only_subscribed_folders;
886 if (!sqgetGlobalVar('boxesnew',$boxesnew,SQ_SESSION
) ||
$force) {
887 $boxesnew=sqimap_get_mailboxes($imap_stream,$force,$show_only_subscribed_folders);
893 * Returns a list of all folders, subscribed or not
895 * Since 1.5.1 code moved to sqimap_get_mailboxes()
897 * @param stream $imap_stream imap connection resource
898 * @return array see sqimap_mailbox_parse()
899 * @since 1.0 or older
901 function sqimap_mailbox_list_all($imap_stream) {
902 global $show_only_subscribed_folders;
903 // fourth argument prevents registration of retrieved list of mailboxes in session
904 $boxes=sqimap_get_mailboxes($imap_stream,true,false,false);
910 * Gets the list of mailboxes for sqimap_maolbox_tree and sqimap_mailbox_list
912 * This is because both of those functions had duplicated logic, but with slightly different
913 * implementations. This will make both use the same implementation, which should make it
914 * easier to maintain and easier to modify in the future
915 * @param stream $imap_stream imap connection resource
916 * @param bool $force force a reload and ignore cache
917 * @param bool $show_only_subscribed controls listing of visible or all folders
918 * @param bool $session_register controls registration of retrieved data in session.
919 * @return object boxesnew - array of mailboxes and their attributes
922 function sqimap_get_mailboxes($imap_stream,$force=false,$show_only_subscribed=true,$session_register=true) {
923 global $show_only_subscribed_folders,$noselect_fix_enable,$folder_prefix,
924 $list_special_folders_first,$imap_server_type;
925 $inbox_subscribed = false;
926 $listsubscribed = sqimap_capability($imap_stream,'LIST-SUBSCRIBED');
928 if ($show_only_subscribed) { $show_only_subscribed=$show_only_subscribed_folders; }
930 //require_once(SM_PATH . 'include/load_prefs.php');
933 * There are three main listing commands we can use in IMAP:
934 * LSUB shows just the list of subscribed folders
935 * may include flags, but these are not necessarily accurate or authoratative
936 * \NoSelect has special meaning: the folder does not exist -OR- it means this
937 * folder is not subscribed but children may be
939 * LIST this shows every mailbox on the system
940 * flags are always included and are accurate and authoratative
941 * \NoSelect means folder should not be selected
943 * LIST (SUBSCRIBED) implemented with LIST-SUBSCRIBED extension
944 * this is like list but returns only subscribed folders
945 * flag meanings are like LIST, not LSUB
946 * \NonExistent means mailbox doesn't exist
947 * \PlaceHolder means parent is not valid (selectable), but one or more children are
948 * \NoSelect indeed means that the folder should not be selected
949 * IMAPEXT-LIST-EXTENSIONS-04 August 2003 B. Leiba
951 if (!$show_only_subscribed) {
953 $sub_cache_name='list_cache';
954 } elseif ($listsubscribed) {
955 $lsub = 'LIST (SUBSCRIBED)';
956 $sub_cache_name='listsub_cache';
959 $sub_cache_name='lsub_cache';
962 // Some IMAP servers allow subfolders to exist even if the parent folders do not
963 // This fixes some problems with the folder list when this is the case, causing the
964 // NoSelect folders to be displayed
965 if ($noselect_fix_enable) {
966 $lsub_args = "$lsub \"$folder_prefix\" \"*%\"";
967 $list_args = "LIST \"$folder_prefix\" \"*%\"";
969 $lsub_args = "$lsub \"$folder_prefix\" \"*\"";
970 $list_args = "LIST \"$folder_prefix\" \"*\"";
973 // get subscribed mailbox list from cache (session)
974 // if not there, then get it from the imap server and store in cache
977 sqgetGlobalVar($sub_cache_name,$lsub_cache,SQ_SESSION
);
980 $lsub_assoc_ary=array();
981 if (!empty($lsub_cache)) {
982 $lsub_assoc_ary=$lsub_cache;
984 $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args, true, $response, $message);
985 $lsub_ary = compact_mailboxes_response($lsub_ary);
986 if (!empty($lsub_ary)) {
987 foreach ($lsub_ary as $rawline) {
988 $temp_mailbox_name=find_mailbox_name($rawline);
989 $lsub_assoc_ary[$temp_mailbox_name]=$rawline;
992 sqsession_register($lsub_assoc_ary,$sub_cache_name);
996 // Now to get the mailbox flags
997 // The LSUB response may return \NoSelect flags, etc. but it is optional
998 // according to RFC3501, and even when returned it may not be accurate
999 // or authoratative. LIST will always return accurate results.
1000 if (($lsub == 'LIST') ||
($lsub == 'LIST (SUBSCRIBED)')) {
1001 // we've already done a LIST or LIST (SUBSCRIBED)
1002 // and NOT a LSUB, so no need to do it again
1003 $list_assoc_ary = $lsub_assoc_ary;
1005 // we did a LSUB so now we need to do a LIST
1006 // first see if it is in cache
1007 $list_cache_name='list_cache';
1009 sqgetGlobalVar($list_cache_name,$list_cache,SQ_SESSION
);
1012 if (!empty($list_cache)) {
1013 $list_assoc_ary=$list_cache;
1014 // we could store this in list_cache_name but not necessary
1016 // not in cache so we need to go get it from the imap server
1017 $list_assoc_ary = array();
1018 $list_ary = sqimap_run_command($imap_stream, $list_args,
1019 true, $response, $message);
1020 $list_ary = compact_mailboxes_response($list_ary);
1021 if (!empty($list_ary)) {
1022 foreach ($list_ary as $rawline) {
1023 $temp_mailbox_name=find_mailbox_name($rawline);
1024 $list_assoc_ary[$temp_mailbox_name]=$rawline;
1027 sqsession_register($list_assoc_ary,$list_cache_name);
1032 // If they aren't subscribed to the inbox, then add it anyway (if its in LIST)
1033 $inbox_subscribed=false;
1034 if (!empty($lsub_assoc_ary)) {
1035 foreach ($lsub_assoc_ary as $temp_mailbox_name=>$rawline) {
1036 if (strtoupper($temp_mailbox_name) == 'INBOX') {
1037 $inbox_subscribed=true;
1041 if (!$inbox_subscribed) {
1042 if (!empty($list_assoc_ary)) {
1043 foreach ($list_assoc_ary as $temp_mailbox_name=>$rawline) {
1044 if (strtoupper($temp_mailbox_name) == 'INBOX') {
1045 $lsub_assoc_ary[$temp_mailbox_name]=$rawline;
1051 // Now we have the raw output, we need to create an array of mailbox names we will return
1052 if (!$show_only_subscribed) {
1053 $final_folders_assoc_ary=$list_assoc_ary;
1056 * only show subscribed folders
1057 * we need to merge the folders here... we can't trust the flags, etc. from the lsub_assoc_array
1058 * so we use the lsub_assoc_array as the list of folders and the values come from list_assoc_array
1060 if (!empty($lsub_assoc_ary)) {
1061 foreach ($lsub_assoc_ary as $temp_mailbox_name=>$rawline) {
1062 if (!empty($list_assoc_ary[$temp_mailbox_name])) {
1063 $final_folders_assoc_ary[$temp_mailbox_name]=$list_assoc_ary[$temp_mailbox_name];
1070 // Now produce a flat, sorted list
1071 if (!empty($final_folders_assoc_ary)) {
1072 uksort($final_folders_assoc_ary,'strnatcasecmp');
1073 foreach ($final_folders_assoc_ary as $temp_mailbox_name=>$rawline) {
1074 $final_folders_ary[]=$rawline;
1078 // this will put it into an array we can use later
1080 // raw - Raw LIST/LSUB response from the IMAP server
1081 // formatted - formatted folder name
1082 // unformatted - unformatted, but with the delimiter at the end removed
1083 // unformated-dm - folder name as it appears in raw response
1084 // unformatted-disp - unformatted without $folder_prefix
1085 // id - the array element number (0, 1, 2, etc.)
1086 // flags - mailbox flags
1087 if (!empty($final_folders_ary)) {
1088 $boxesall = sqimap_mailbox_parse($final_folders_ary);
1090 // they have no mailboxes
1094 /* Now, lets sort for special folders */
1095 $boxesnew = $used = array();
1098 $cnt = count($boxesall);
1099 $used = array_pad($used,$cnt,false);
1101 foreach ($boxesall as $k => $b)
1102 // was this but array not guaranteed to be contiguous: for($k = 0; $k < $cnt; ++$k)
1104 if (strtoupper($boxesall[$k]['unformatted']) == 'INBOX') {
1105 $boxesnew[] = $boxesall[$k];
1112 if ($has_inbox == false) {
1113 // do a list request for inbox because we should always show
1114 // inbox even if the user isn't subscribed to it.
1115 $inbox_ary = sqimap_run_command($imap_stream, 'LIST "" "INBOX"',
1116 true, $response, $message);
1117 $inbox_ary = compact_mailboxes_response($inbox_ary);
1118 if (count($inbox_ary)) {
1119 $inbox_entry = sqimap_mailbox_parse($inbox_ary);
1120 // add it on top of the list
1121 if (!empty($boxesnew)) {
1122 array_unshift($boxesnew,$inbox_entry[0]);
1124 $boxesnew[]=$inbox_entry[0];
1126 /* array_unshift($used,true); */
1130 /* List special folders and their subfolders, if requested. */
1131 if ($list_special_folders_first) {
1132 foreach ($boxesall as $k => $b)
1133 // was this but array not guaranteed to be contiguous: for($k = 0; $k < $cnt; ++$k)
1135 if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
1136 $boxesnew[] = $boxesall[$k];
1142 /* Find INBOX's children */
1143 foreach ($boxesall as $k => $b)
1144 // was this but array not guaranteed to be contiguous: for($k = 0; $k < $cnt; ++$k)
1146 $isboxbelow=isBoxBelow(strtoupper($boxesall[$k]['unformatted']),'INBOX');
1147 if (strtoupper($boxesall[$k]['unformatted']) == 'INBOX') {
1153 if (!$used[$k] && $isboxbelow && $is_inbox) {
1154 $boxesnew[] = $boxesall[$k];
1159 /* Rest of the folders */
1160 foreach ($boxesall as $k => $b)
1161 // was this but array not guaranteed to be contiguous: for($k = 0; $k < $cnt; ++$k)
1164 $boxesnew[] = $boxesall[$k];
1168 * Don't register boxes in session, if $session_register is set to false
1169 * Prevents registration of sqimap_mailbox_list_all() results.
1171 if ($session_register) sqsession_register($boxesnew,'boxesnew');
1176 * Fills mailbox object
1178 * this is passed the mailbox array by left_main.php
1179 * who has previously obtained it from sqimap_get_mailboxes
1180 * that way, the raw mailbox list is available in left_main to other
1181 * things besides just sqimap_mailbox_tree
1182 * imap_stream is just used now to get status info
1184 * most of the functionality is moved to sqimap_get_mailboxes
1185 * also takes care of TODO items:
1186 * caching mailbox tree
1187 * config setting for UW imap section (not needed now)
1189 * Some code fragments are present in 1.3.0 - 1.4.4.
1190 * @param stream $imap_stream imap connection resource
1191 * @param array $lsub_ary output array from sqimap_get_mailboxes (contains mailboxes and flags)
1192 * @return object see mailboxes class.
1195 function sqimap_mailbox_tree($imap_stream,$lsub_ary) {
1197 $sorted_lsub_ary = array();
1198 $cnt = count($lsub_ary);
1199 for ($i = 0; $i < $cnt; $i++
) {
1200 $mbx=$lsub_ary[$i]['unformatted'];
1201 $flags=$lsub_ary[$i]['flags'];
1204 if (in_array('\Noinferiors',$flags)) { $noinferiors=1; }
1205 if (in_array('\NoInferiors',$flags)) { $noinferiors=1; }
1206 if (in_array('\HasNoChildren',$flags)) { $noinferiors=1; }
1209 if (in_array('\NoSelect',$flags)) { $noselect=1; }
1211 * LIST (SUBSCRIBED) has two new flags, \NonExistent which means the mailbox is subscribed to
1212 * but doesn't exist, and \PlaceHolder which is similar (but not the same) as \NoSelect
1213 * For right now, we'll treat these the same as \NoSelect and this behavior can be changed
1216 if (in_array('\NonExistent',$flags)) { $noselect=1; }
1217 if (in_array('\PlaceHolder',$flags)) { $noselect=1; }
1218 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
1221 $sorted_lsub_ary = array_values($sorted_lsub_ary);
1222 usort($sorted_lsub_ary, 'mbxSort');
1223 $boxestree = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream);
1228 * Callback function used for sorting mailboxes in sqimap_mailbox_tree
1231 * @return integer see php strnatcasecmp()
1234 function mbxSort($a, $b) {
1235 return strnatcasecmp($a['mbx'], $b['mbx']);
1239 * Fills mailbox object
1241 * Some code fragments are present in 1.3.0 - 1.4.4.
1242 * @param array $mbx_ary
1244 * @param stream $imap_stream imap connection resource
1245 * @return object see mailboxes class
1248 function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
1249 global $data_dir, $username, $list_special_folders_first,
1250 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
1251 $move_to_trash, $move_to_sent, $save_as_draft,
1252 $delimiter, $imap_server_type;
1254 // $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
1256 /* create virtual root node */
1257 $mailboxes= new mailboxes();
1258 $mailboxes->is_root
= true;
1262 if (isset($folder_prefix) && ($folder_prefix != '')) {
1263 $start = substr_count($folder_prefix,$delimiter);
1264 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
1265 $mailboxes->mailboxname_full
= substr($folder_prefix,0, (strlen($folder_prefix)-1));
1267 $mailboxes->mailboxname_full
= $folder_prefix;
1270 $mailboxes->mailboxname_sub
= $mailboxes->mailboxname_full
;
1275 $cnt = count($mbx_ary);
1276 for ($i=0; $i < $cnt; $i++
) {
1277 if ($mbx_ary[$i]['mbx'] !='' ) {
1278 $mbx = new mailboxes();
1279 $mailbox = $mbx_ary[$i]['mbx'];
1282 * Set the is_special flag if it concerned a special mailbox.
1283 * Used for displaying the special folders on top in the mailbox
1284 * tree displaying code.
1286 $mbx->is_special |
= ($mbx->is_inbox
= (strtoupper($mailbox) == 'INBOX'));
1287 $mbx->is_special |
= ($mbx->is_trash
= isTrashMailbox($mailbox));
1288 $mbx->is_special |
= ($mbx->is_sent
= isSentMailbox($mailbox));
1289 $mbx->is_special |
= ($mbx->is_draft
= isDraftMailbox($mailbox));
1291 if (!$mbx->is_special
)
1292 $mbx->is_special
= boolean_hook_function('special_mailbox', $mailbox, 1);
1294 if (isset($mbx_ary[$i]['unseen'])) {
1295 $mbx->unseen
= $mbx_ary[$i]['unseen'];
1297 if (isset($mbx_ary[$i]['nummessages'])) {
1298 $mbx->total
= $mbx_ary[$i]['nummessages'];
1301 $mbx->is_noselect
= $mbx_ary[$i]['noselect'];
1302 $mbx->is_noinferiors
= $mbx_ary[$i]['noinferiors'];
1304 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
1306 $mbx->mailboxname_sub
= substr($mbx_ary[$i]['mbx'],$r_del_pos+
1);
1307 } else { /* mailbox is root folder */
1308 $mbx->mailboxname_sub
= $mbx_ary[$i]['mbx'];
1310 $mbx->mailboxname_full
= $mbx_ary[$i]['mbx'];
1312 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
1315 sqimap_utf7_decode_mbx_tree($mailboxes);
1316 sqimap_get_status_mbx_tree($imap_stream,$mailboxes);
1321 * @param object $mbx_tree
1324 function sqimap_utf7_decode_mbx_tree(&$mbx_tree) {
1325 global $draft_folder, $sent_folder, $trash_folder, $translate_special_folders;
1327 /* decode folder name and set mailboxname_sub */
1328 if ($translate_special_folders && strtoupper($mbx_tree->mailboxname_full
) == 'INBOX') {
1329 $mbx_tree->mailboxname_sub
= _("INBOX");
1330 } elseif ($translate_special_folders && $mbx_tree->mailboxname_full
== $draft_folder) {
1331 $mbx_tree->mailboxname_sub
= _("Drafts");
1332 } elseif ($translate_special_folders && $mbx_tree->mailboxname_full
== $sent_folder) {
1333 $mbx_tree->mailboxname_sub
= _("Sent");
1334 } elseif ($translate_special_folders && $mbx_tree->mailboxname_full
== $trash_folder) {
1335 $mbx_tree->mailboxname_sub
= _("Trash");
1337 $mbx_tree->mailboxname_sub
= imap_utf7_decode_local($mbx_tree->mailboxname_sub
);
1340 if ($mbx_tree->mbxs
) {
1341 $iCnt = count($mbx_tree->mbxs
);
1342 for ($i=0;$i<$iCnt;++
$i) {
1343 sqimap_utf7_decode_mbx_tree($mbx_tree->mbxs
[$i]);
1349 * @param object $mbx_tree
1350 * @param array $aMbxs
1353 function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) {
1355 $aMbxs[] =& $mbx_tree;
1356 if ($mbx_tree->mbxs
) {
1357 $iCnt = count($mbx_tree->mbxs
);
1358 for ($i=0;$i<$iCnt;++
$i) {
1359 sqimap_tree_to_ref_array($mbx_tree->mbxs
[$i],$aMbxs);
1365 * @param stream $imap_stream imap connection resource
1366 * @param object $mbx_tree
1367 * @since since 1.5.0
1369 function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
1370 global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash;
1371 $aMbxs = $aQuery = array();
1372 sqimap_tree_to_ref_array($mbx_tree,$aMbxs);
1373 // remove the root node
1374 array_shift($aMbxs);
1376 if($unseen_notify == 3) {
1377 $cnt = count($aMbxs);
1378 for($i=0;$i<$cnt;++
$i) {
1379 $oMbx =& $aMbxs[$i];
1380 if (!$oMbx->is_noselect
) {
1381 $mbx = $oMbx->mailboxname_full
;
1382 if ($unseen_type == 2 ||
1383 ($move_to_trash && $oMbx->mailboxname_full
== $trash_folder)) {
1384 $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (MESSAGES UNSEEN RECENT)';
1386 $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (UNSEEN RECENT)';
1388 sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
1390 $oMbx->unseen
= $oMbx->total
= $oMbx->recent
= false;
1394 $aMbxs[$i] =& $oMbx;
1396 // execute all the queries at once
1397 $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
1398 $cnt = count($aMbxs);
1399 for($i=0;$i<$cnt;++
$i) {
1400 $oMbx =& $aMbxs[$i];
1402 if ($tag && $aServerResponse[$tag] == 'OK') {
1403 $sResponse = implode('', $aResponse[$tag]);
1404 if (preg_match('/UNSEEN\s+([0-9]+)/i', $sResponse, $regs)) {
1405 $oMbx->unseen
= $regs[1];
1407 if (preg_match('/MESSAGES\s+([0-9]+)/i', $sResponse, $regs)) {
1408 $oMbx->total
= $regs[1];
1410 if (preg_match('/RECENT\s+([0-9]+)/i', $sResponse, $regs)) {
1411 $oMbx->recent
= $regs[1];
1417 } else if ($unseen_notify == 2) { // INBOX only
1418 $cnt = count($aMbxs);
1419 for($i=0;$i<$cnt;++
$i) {
1420 $oMbx =& $aMbxs[$i];
1421 if (strtoupper($oMbx->mailboxname_full
) == 'INBOX' ||
1422 ($move_to_trash && $oMbx->mailboxname_full
== $trash_folder)) {
1423 if ($unseen_type == 2 ||
1424 ($oMbx->mailboxname_full
== $trash_folder && $move_to_trash)) {
1425 $aStatus = sqimap_status_messages($imap_stream,$oMbx->mailboxname_full
);
1426 $oMbx->unseen
= $aStatus['UNSEEN'];
1427 $oMbx->total
= $aStatus['MESSAGES'];
1428 $oMbx->recent
= $aStatus['RECENT'];
1430 $oMbx->unseen
= sqimap_unseen_messages($imap_stream,$oMbx->mailboxname_full
);
1432 $aMbxs[$i] =& $oMbx;
1433 if (!$move_to_trash && $trash_folder) {
1436 // trash comes after INBOX
1437 if ($oMbx->mailboxname_full
== $trash_folder) {
1445 $cnt = count($aMbxs);
1446 for($i=0;$i<$cnt;++
$i) {
1447 $oMbx =& $aMbxs[$i];
1448 unset($hook_status);
1449 if (!empty($oMbx->unseen
)) { $hook_status['UNSEEN']=$oMbx->unseen
; }
1450 if (!empty($oMbx->total
)) { $hook_status['MESSAGES']=$oMbx->total
; }
1451 if (!empty($oMbx->recent
)) { $hook_status['RECENT']=$oMbx->recent
; }
1452 if (!empty($hook_status))
1454 $hook_status['MAILBOX']=$oMbx->mailboxname_full
;
1455 $hook_status['CALLER']='sqimap_get_status_mbx_tree'; // helps w/ debugging
1456 do_hook('folder_status', $hook_status);
1462 * Checks if folder is noselect (can't store messages)
1464 * Function does not check if folder subscribed.
1465 * @param stream $oImapStream imap connection resource
1466 * @param string $sImapFolder imap folder name
1467 * @param object $oBoxes mailboxes class object.
1468 * @return boolean true, when folder has noselect flag. false in any other case.
1471 function sqimap_mailbox_is_noselect($oImapStream,$sImapFolder,&$oBoxes) {
1472 // build mailbox object if it is not available
1473 if (! is_object($oBoxes)) $oBoxes=sqimap_mailbox_list($oImapStream);
1474 foreach($oBoxes as $box) {
1475 if ($box['unformatted']==$sImapFolder) {
1476 return (bool) check_is_noselect($box['raw']);
1483 * Checks if folder is noinferiors (can't store other folders)
1485 * Function does not check if folder subscribed.
1486 * @param stream $oImapStream imap connection resource
1487 * @param string $sImapFolder imap folder name
1488 * @param object $oBoxes mailboxes class object.
1489 * @return boolean true, when folder has noinferiors flag. false in any other case.
1492 function sqimap_mailbox_is_noinferiors($oImapStream,$sImapFolder,&$oBoxes) {
1493 // build mailbox object if it is not available
1494 if (! is_object($oBoxes)) $oBoxes=sqimap_mailbox_list($oImapStream);
1495 foreach($oBoxes as $box) {
1496 if ($box['unformatted']==$sImapFolder) {
1497 return (bool) check_is_noinferiors($box['raw']);