+ }
+ }
+
+ /* Find INBOX's children */
+ for($k = 0; $k < $cnt; ++$k) {
+ $isboxbelow=isBoxBelow(strtoupper($boxesall[$k]['unformatted']),'INBOX');
+ if (strtoupper($boxesall[$k]['unformatted']) == 'INBOX') {
+ $is_inbox=1;
+ } else {
+ $is_inbox=0;
+ }
+
+ if (!$used[$k] && $isboxbelow && $is_inbox) {
+ $boxesnew[] = $boxesall[$k];
+ $used[$k] = true;
+ }
+ }
+
+ /* Rest of the folders */
+ for($k = 0; $k < $cnt; $k++) {
+ if (!$used[$k]) {
+ $boxesnew[] = $boxesall[$k];
+ }
+ }
+ /**
+ * Don't register boxes in session, if $session_register is set to false
+ * Prevents registration of sqimap_mailbox_list_all() results.
+ */
+ if ($session_register) sqsession_register($boxesnew,'boxesnew');
+ return $boxesnew;
+}
+
+/**
+ * Fills mailbox object
+ *
+ * this is passed the mailbox array by left_main.php
+ * who has previously obtained it from sqimap_get_mailboxes
+ * that way, the raw mailbox list is available in left_main to other
+ * things besides just sqimap_mailbox_tree
+ * imap_stream is just used now to get status info
+ *
+ * most of the functionality is moved to sqimap_get_mailboxes
+ * also takes care of TODO items:
+ * caching mailbox tree
+ * config setting for UW imap section (not needed now)
+ *
+ * Some code fragments are present in 1.3.0 - 1.4.4.
+ * @param stream $imap_stream imap connection resource
+ * @param array $lsub_ary output array from sqimap_get_mailboxes (contains mailboxes and flags)
+ * @return object see mailboxes class.
+ * @since 1.5.0
+ */
+function sqimap_mailbox_tree($imap_stream,$lsub_ary) {
+
+ $sorted_lsub_ary = array();
+ $cnt = count($lsub_ary);
+ for ($i = 0; $i < $cnt; $i++) {
+ $mbx=$lsub_ary[$i]['unformatted'];
+ $flags=$lsub_ary[$i]['flags'];
+
+ $noinferiors=0;
+ if (in_array('\Noinferiors',$flags)) { $noinferiors=1; }
+ if (in_array('\NoInferiors',$flags)) { $noinferiors=1; }
+ if (in_array('\HasNoChildren',$flags)) { $noinferiors=1; }
+
+ $noselect=0;
+ if (in_array('\NoSelect',$flags)) { $noselect=1; }
+ /**
+ * LIST (SUBSCRIBED) has two new flags, \NonExistent which means the mailbox is subscribed to
+ * but doesn't exist, and \PlaceHolder which is similar (but not the same) as \NoSelect
+ * For right now, we'll treat these the same as \NoSelect and this behavior can be changed
+ * later if needed
+ */
+ if (in_array('\NonExistent',$flags)) { $noselect=1; }
+ if (in_array('\PlaceHolder',$flags)) { $noselect=1; }
+ $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
+ }
+
+ $sorted_lsub_ary = array_values($sorted_lsub_ary);
+ usort($sorted_lsub_ary, 'mbxSort');
+ $boxestree = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream);
+ return $boxestree;
+}
+
+/**
+ * Callback function used for sorting mailboxes in sqimap_mailbox_tree
+ * @param string $a
+ * @param string $b
+ * @return integer see php strnatcasecmp()
+ * @since 1.5.1
+ */
+function mbxSort($a, $b) {
+ return strnatcasecmp($a['mbx'], $b['mbx']);
+}
+
+/**
+ * Fills mailbox object
+ *
+ * Some code fragments are present in 1.3.0 - 1.4.4.
+ * @param array $mbx_ary
+ * @param $mbxs
+ * @param stream $imap_stream imap connection resource
+ * @return object see mailboxes class
+ * @since 1.5.0
+ */
+function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
+ global $data_dir, $username, $list_special_folders_first,
+ $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
+ $move_to_trash, $move_to_sent, $save_as_draft,
+ $delimiter, $imap_server_type;
+
+ // $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
+
+ /* create virtual root node */
+ $mailboxes= new mailboxes();
+ $mailboxes->is_root = true;
+ $trail_del = false;
+ $start = 0;
+
+ if (isset($folder_prefix) && ($folder_prefix != '')) {
+ $start = substr_count($folder_prefix,$delimiter);
+ if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
+ $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
+ } else {
+ $mailboxes->mailboxname_full = $folder_prefix;
+ $start++;
+ }
+ $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
+ } else {
+ $start = 0;
+ }
+
+ $cnt = count($mbx_ary);
+ for ($i=0; $i < $cnt; $i++) {
+ if ($mbx_ary[$i]['mbx'] !='' ) {
+ $mbx = new mailboxes();
+ $mailbox = $mbx_ary[$i]['mbx'];
+
+ /*
+ * Set the is_special flag if it concerned a special mailbox.
+ * Used for displaying the special folders on top in the mailbox
+ * tree displaying code.
+ */
+ $mbx->is_special |= ($mbx->is_inbox = (strtoupper($mailbox) == 'INBOX'));
+ $mbx->is_special |= ($mbx->is_trash = isTrashMailbox($mailbox));
+ $mbx->is_special |= ($mbx->is_sent = isSentMailbox($mailbox));
+ $mbx->is_special |= ($mbx->is_draft = isDraftMailbox($mailbox));
+
+ if (!$mbx->is_special)
+ $mbx->is_special = boolean_hook_function('special_mailbox', $mailbox, 1);
+
+ if (isset($mbx_ary[$i]['unseen'])) {
+ $mbx->unseen = $mbx_ary[$i]['unseen'];
+ }
+ if (isset($mbx_ary[$i]['nummessages'])) {
+ $mbx->total = $mbx_ary[$i]['nummessages'];