Moved the INBOX check higher to save code duplication... something else in
[squirrelmail.git] / functions / imap_mailbox.php
index 7bc1313d8ccfb2cd4554a65489f12a7cb7699d45..4e496bd4e9d34377978c8e65c85c2f5c28ab5035 100755 (executable)
@@ -188,7 +188,7 @@ function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true,
 
 /* Checks whether or not the specified mailbox exists */
 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
-    if (!isset($mailbox)) {
+    if (!isset($mailbox) || empty($mailbox)) {
         return false;
     }
     $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
@@ -419,18 +419,29 @@ function user_strcasecmp($a, $b) {
  *   $folder_skip - array of folders to keep out of option list (compared in lower)
  *   $boxes - list of already fetched boxes (for places like folder panel, where
  *            you know these options will be shown 3 times in a row.. (most often unset).
+ *   $flag - flag to check for in mailbox flags, used to filter out mailboxes.
+ *           'noselect' by default to remove unselectable mailboxes.
+ *           'noinferiors' used to filter out folders that can not contain subfolders.
+ *           NULL to avoid flag check entirely.
+ *   $use_long_format - override folder display preference and always show full folder name.
  */
-function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0 ) {
+function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0, 
+                                    $flag = 'noselect', $use_long_format = false ) {
     global $username, $data_dir;
     $mbox_options = '';
 
-    $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF);
+    if ( $use_long_format ) {
+        $shorten_box_names = 0;
+    } else {
+        $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF);
+    }
 
     if ($boxes == 0) {
         $boxes = sqimap_mailbox_list($imap_stream);
     }
+
     foreach ($boxes as $boxes_part) {
-        if (!in_array('noselect', $boxes_part['flags'])) {
+        if ($flag == NULL || !in_array($flag, $boxes_part['flags'])) {
             $box = $boxes_part['unformatted'];
             $lowerbox = strtolower($box);
 
@@ -553,7 +564,7 @@ function sqimap_mailbox_list($imap_stream) {
             $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
                                              true, $response, $message);
             /* Another workaround for literals */
-            if (isset($inbox_ary[1]) && substr($inbox_ary[$i],-3) == "}\r\n") {
+            if (isset($inbox_ary[1]) && substr($inbox_ary[0],-3) == "}\r\n") {
                if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
                      $inbox_ary[0], $regs)) {
                    $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
@@ -707,6 +718,25 @@ function sqimap_mailbox_tree($imap_stream) {
         $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
                                         true, $response, $message);
 
+
+        $has_inbox = false;
+
+        for ($i = 0, $cnt = count($lsub_ary); $i < $cnt; $i++) {
+            if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.{1})\"\s+\"INBOX\".*$/",$lsub_ary[$i])) {
+                $has_inbox = true;
+                break;
+            }
+        }
+
+        if ($has_inbox == false) {
+            $lsub_ibx = sqimap_run_command( $imap_stream, "LSUB \"\" \"INBOX\"", true, $response, $message );
+            if (isset($lsub_ibx[0])) {
+                if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.{1})\"\s+\"INBOX\".*$/",$lsub_ibx[0])) {
+                    $lsub_ary[] = $lsub_ibx[0];
+                }
+            }
+        }
+
         /*
          * Section about removing the last element was removed 
          * We don't return "* OK" anymore from sqimap_read_data
@@ -725,13 +755,6 @@ function sqimap_mailbox_tree($imap_stream) {
                 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
             }
 
-            /*
-            if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
-                $flag = $regs[1];
-                $mbx = trim($regs[3]);
-                $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag); 
-            }
-            */
             $mbx = find_mailbox_name($lsub_ary[$i]);
             $noselect = check_is_noselect($lsub_ary[$i]);
             if (substr($mbx, -1) == $delimiter) {
@@ -741,54 +764,12 @@ function sqimap_mailbox_tree($imap_stream) {
         }
         array_multisort($sorted_lsub_ary, SORT_ASC, SORT_REGULAR);
 
-        for($i = 0; $i < $cnt; $i++) {
-            if ($sorted_lsub_ary[$i]['mbx'] == 'INBOX') {
-                $inbox_in_list = true;
-                break;
-            }
-        }
-
-        /*
-         * Just in case they're not subscribed to their inbox,
-         * we'll get it for them anyway
-         */
-        if (!$inbox_in_list) {
-            $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
-                                             true, $response, $message);
-            /* Another workaround for EIMS */
-            if (isset($inbox_ary[1]) &&
-                ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
-                     $inbox_ary[0], $regs)) {
-                $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
-                                '"' . $regs[2];
-            }
-            $mbx = find_mailbox_name($inbox_ary[0]);
-            if (substr($mbx, -1) == $delimiter) {
-                $mbx = substr($mbx, 0, strlen($mbx) - 1);
-            }
-            if ($mbx == 'INBOX') {
-                $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => ''); 
-                sqimap_subscribe($imap_stream, 'INBOX');
-                $cnt++;
-            }
-
-            /*
-            if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
-                $flag = $regs[1];
-                $mbx = trim($regs[3]);
-                if (substr($mbx, -1) == $delimiter) {
-                    $mbx = substr($mbx, 0, strlen($mbx) - 1);
-                }
-                $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag); 
-            }
-            */
-        }
         for ($i = 0 ; $i < $cnt; $i++) {
             $mbx = $sorted_lsub_ary[$i]['mbx'];
             if (($unseen_notify == 2 && $mbx == 'INBOX') ||
                 ($unseen_notify == 3) ||
                 ($move_to_trash && ($mbx == $trash_folder))) {
-                if($sorted_lsub_ary[$i]['noselect']) {
+                if( $sorted_lsub_ary[$i]['noselect'] ) {
                     $sorted_lsub_ary[$i]['unseen'] = 0;
                 } else {
                     $sorted_lsub_ary[$i]['unseen'] = 
@@ -824,6 +805,8 @@ function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
     $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)) {
@@ -837,6 +820,7 @@ function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
     } else {
         $start = 0;
     }
+
     $cnt =  count($mbx_ary);
     for ($i=0; $i < $cnt; $i++) {
         if ($mbx_ary[$i]['mbx'] !='' ) {
@@ -844,6 +828,11 @@ function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
             $mailbox = $mbx_ary[$i]['mbx'];
             switch ($mailbox) {
                 case 'INBOX':
+                    // $mailboxes IS the INBOX, change $mbx to refer to $mailboxes
+                    // for unread count, attribute setting, etc.
+                    unset($mbx);
+                    $mbx =& $mailboxes;
+
                     $mbx->is_inbox = true;
                     $mbx->is_special = true;
                     break;
@@ -877,10 +866,12 @@ function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
                 $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
             }
             $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
-            $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
+
+            // Don't add INBOX to itself
+            if ( $mailbox != 'INBOX' )
+                $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
         }
     }
-
     return $mailboxes;
 }