Adding labels to other columns on message list for checkbox. STABLE admins, please...
[squirrelmail.git] / functions / imap_mailbox.php
index a442359636a77d9cba7278e824a5fea6e790c323..30ecbd5ff40bc7632f6ccb3f97a3db5104efd799 100755 (executable)
@@ -1,8 +1,9 @@
 <?php
+
 /**
  * imap_mailbox.php
  *
- * Copyright (c) 1999-2004 The SquirrelMail Project Team
+ * Copyright (c) 1999-2005 The SquirrelMail Project Team
  * Licensed under the GNU GPL. For full terms see the file COPYING.
  *
  * This impliments all functions that manipulate mailboxes
@@ -229,7 +230,7 @@ function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true,
             $id = sqimap_message_list_squisher($id);
         }
         $id = ' '.$id;
-       $uid = TRUE;
+        $uid = TRUE;
     } else {
         $uid = false;
     }
@@ -247,58 +248,6 @@ function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true,
     return $cnt;
 }
 
-
-/**
- * Expunge specified message, updated $msgs and $msort
- *
- * Until Marc and I come up with a better way to maintain
- * these stupid arrays, we'll use this wrapper function to
- * remove the message with the matching UID .. the order
- * won't be changed - the array element for the message
- * will just be removed.
- */
-function sqimap_mailbox_expunge_dmn($message_id, $aMbxResponse, &$server_sort_array)
-{
-    global $msgs, $msort, $sort, $imapConnection,
-           $mailbox, $auto_expunge,
-           $sort, $allow_server_sort, $thread_sort_messages, $allow_thread_sort,
-           $username, $data_dir;
-    $cnt = 0;
-
-    if (!isset($sort) || $sort === false) {
-        sqgetGlobalVar('sort',$sort,SQ_GET);
-    }
-
-    if ($auto_expunge) {
-         $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
-    } else {
-         return $cnt;
-    }
-
-    // Got to grab this out of prefs, since it isn't saved from mailbox_view.php
-    if ($allow_thread_sort && getPref($data_dir, $username, "thread_$mailbox",0)) {
-        $mode = 'thread';
-    } else if ($allow_server_sort) {
-        $mode = 'server_sort';
-    } else {
-        $mode = '';
-    }
-    $error = '';
-    if (is_array($server_sort_array)) {
-        $key = array_search($message_id,$server_sort_array,true);
-        if ($key !== false) {
-            unset($server_sort_array[$key]);
-            $server_sort_array = array_values($server_sort_array);
-        } else {
-            $server_sort_array = get_sorted_msgs_list($imapConnection,$sort,$mode,$error);
-        }
-    } else {
-        $server_sort_array = get_sorted_msgs_list($imapConnection,$sort,$mode,$error);
-    }
-    sqsession_register($server_sort_array,'server_sort_array');
-    return $cnt;
-}
-
 /**
  * Checks whether or not the specified mailbox exists
  */
@@ -427,7 +376,7 @@ function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
 
         $boxesall = sqimap_mailbox_list($imap_stream);
         $cmd = 'RENAME ' . sqimap_encode_mailbox_name($old_name) .
-                    ' ' . sqimap_encode_mailbox_name($new_name);
+                     ' ' . sqimap_encode_mailbox_name($new_name);
         $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
         sqimap_unsubscribe($imap_stream, $old_name.$postfix);
         $oldpref = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
@@ -536,7 +485,7 @@ function sqimap_mailbox_parse ($line, $line_lsub) {
 /**
  * Returns list of options (to be echoed into select statement
  * based on available mailboxes and separators
- * Caller should surround options with <SELECT..> </SELECT> and
+ * Caller should surround options with <select ...> </select> and
  * any formatting.
  *   $imap_stream - $imapConnection to query for mailboxes
  *   $show_selected - array containing list of mailboxes to pre-select (0 if none)
@@ -566,7 +515,8 @@ function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_sk
     }
 
     foreach ($boxes as $boxes_part) {
-        if ($flag == NULL || !in_array($flag, $boxes_part['flags'])) {
+        if ($flag == NULL || (is_array($boxes_part['flags'])
+                      && !in_array($flag, $boxes_part['flags']))) {
             $box = $boxes_part['unformatted'];
 
             if ($folder_skip != 0 && in_array($box, $folder_skip) ) {
@@ -582,10 +532,10 @@ function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_sk
                 switch ($shorten_box_names)
                 {
                   case 2:   /* delimited, style = 2 */
-                    $box2 = str_replace('&nbsp;&nbsp;', '.&nbsp;', $boxes_part['formatted']);
+                    $box2 = str_replace('&amp;nbsp;&amp;nbsp;', '.&nbsp;', htmlspecialchars($boxes_part['formatted']));
                     break;
                   case 1:   /* indent, style = 1 */
-                    $box2 = $boxes_part['formatted'];
+                    $box2 = str_replace('&amp;nbsp;&amp;nbsp;', '&nbsp;&nbsp;', htmlspecialchars($boxes_part['formatted']));
                     break;
                   default:  /* default, long names, style = 0 */
                     $box2 = str_replace(' ', '&nbsp;', htmlspecialchars(imap_utf7_decode_local($boxes_part['unformatted-disp'])));
@@ -593,9 +543,9 @@ function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_sk
                 }
             }
             if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
-                $mbox_options .= '<OPTION VALUE="' . htmlspecialchars($box) .'" SELECTED>'.$box2.'</OPTION>' . "\n";
+                $mbox_options .= '<option value="' . htmlspecialchars($box) .'" selected="selected">'.$box2.'</option>' . "\n";
             } else {
-                $mbox_options .= '<OPTION VALUE="' . htmlspecialchars($box) .'">'.$box2.'</OPTION>' . "\n";
+                $mbox_options .= '<option value="' . htmlspecialchars($box) .'">'.$box2.'</option>' . "\n";
             }
         }
     }
@@ -609,28 +559,26 @@ function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_sk
 
 
 function sqimap_mailbox_list($imap_stream, $force=false) {
-    global $default_folder_prefix;
-
     if (!sqgetGlobalVar('boxesnew',$boxesnew,SQ_SESSION) || $force) {
         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, $noselect_fix_enable, $imap_server_type;
-        $inbox_in_list = false;
+               $delimiter, $noselect_fix_enable, $imap_server_type,
+               $show_only_subscribed_folders;
         $inbox_subscribed = false;
         $listsubscribed = sqimap_capability($imap_stream,'LIST-SUBSCRIBED');
 
         require_once(SM_PATH . 'include/load_prefs.php');
 
-
-        if ($listsubscribed) {
+        if (!$show_only_subscribed_folders) {
+            $lsub = 'LIST';
+        } elseif ($listsubscribed) {
             $lsub = 'LIST (SUBSCRIBED)';
         } else {
             $lsub = 'LSUB';
         }
 
         if ($noselect_fix_enable) {
-
             $lsub_args = "$lsub \"$folder_prefix\" \"*%\"";
         } else {
             $lsub_args = "$lsub \"$folder_prefix\" \"*\"";
@@ -739,7 +687,6 @@ function sqimap_mailbox_list_all($imap_stream) {
     $read_ary = compact_mailboxes_response($read_ary);
 
     $g = 0;
-    $phase = 'inbox';
     $fld_pre_length = strlen($folder_prefix);
     for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++) {
         /* Store the raw IMAP reply */
@@ -799,23 +746,25 @@ function sqimap_mailbox_list_all($imap_stream) {
 }
 
 function sqimap_mailbox_tree($imap_stream) {
-    global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
+    global $default_folder_prefix;
     if (true) {
-
         global $data_dir, $username, $list_special_folders_first,
                $folder_prefix, $delimiter, $trash_folder, $move_to_trash,
-               $imap_server_type;
-
+               $imap_server_type, $show_only_subscribed_folders;
 
-        $inbox_in_list = false;
-        $inbox_subscribed = false;
         $noselect = false;
         $noinferiors = false;
 
         require_once(SM_PATH . 'include/load_prefs.php');
 
+        if ($show_only_subscribed_folders) {
+            $lsub_cmd = 'LSUB';
+        } else {
+            $lsub_cmd = 'LIST';
+        }
+
         /* LSUB array */
-        $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
+        $lsub_ary = sqimap_run_command ($imap_stream, "$lsub_cmd \"$folder_prefix\" \"*\"",
                                         true, $response, $message);
         $lsub_ary = compact_mailboxes_response($lsub_ary);
 
@@ -823,12 +772,12 @@ function sqimap_mailbox_tree($imap_stream) {
         $has_inbox = false;
 
         for ($i = 0, $cnt = count($lsub_ary); $i < $cnt; $i++) {
-            if (preg_match("/^\*\s+LSUB.*\s\"?INBOX\"?[^(\/\.)].*$/i",$lsub_ary[$i])) {
-               $lsub_ary[$i] = strtoupper($lsub_ary[$i]);
+            if (preg_match("/^\*\s+$lsub_cmd.*\s\"?INBOX\"?[^(\/\.)].*$/i",$lsub_ary[$i])) {
+                $lsub_ary[$i] = strtoupper($lsub_ary[$i]);
                 // in case of an unsubscribed inbox an imap server can
                 // return the inbox in the lsub results with a \NoSelect
                 // flag.
-                if (!preg_match("/\*\s+LSUB\s+\(.*\\\\NoSelect.*\).*/i",$lsub_ary[$i])) {
+                if (!preg_match("/\*\s+$lsub_cmd\s+\(.*\\\\NoSelect.*\).*/i",$lsub_ary[$i])) {
                     $has_inbox = true;
                 } else {
                     // remove the result and request it again  with a list
@@ -845,7 +794,7 @@ function sqimap_mailbox_tree($imap_stream) {
         if ($has_inbox == false) {
             // do a list request for inbox because we should always show
             // inbox even if the user isn't subscribed to it.
-            $inbox_ary = sqimap_run_command ($imap_stream, 'LIST "" INBOX',
+            $inbox_ary = sqimap_run_command ($imap_stream, 'LIST "" "INBOX"',
                                              true, $response, $message);
             $inbox_ary = compact_mailboxes_response($inbox_ary);
             if (count($inbox_ary)) {
@@ -897,19 +846,23 @@ function sqimap_mailbox_tree($imap_stream) {
            $cnt = count($sorted_lsub_ary);
        }
        $sorted_lsub_ary = array_values($sorted_lsub_ary);
-       array_multisort($sorted_lsub_ary, SORT_ASC, SORT_REGULAR);
-       $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream);
-       return $boxesnew;
+       usort($sorted_lsub_ary, 'mbxSort');
+       $boxestree = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream);
+       return $boxestree;
     }
 }
 
+function mbxSort($a, $b) {
+    return strnatcasecmp($a['mbx'], $b['mbx']);
+}
+
 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);
+    // $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
 
     /* create virtual root node */
     $mailboxes= new mailboxes();
@@ -921,7 +874,6 @@ function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
     if (isset($folder_prefix) && ($folder_prefix != '')) {
         $start = substr_count($folder_prefix,$delimiter);
         if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
-            $trail_del = true;
             $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
         } else {
             $mailboxes->mailboxname_full = $folder_prefix;
@@ -932,7 +884,7 @@ function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
         $start = 0;
     }
 
-    $cnt =  count($mbx_ary);
+    $cnt = count($mbx_ary);
     for ($i=0; $i < $cnt; $i++) {
         if ($mbx_ary[$i]['mbx'] !='' ) {
             $mbx = new mailboxes();
@@ -1035,7 +987,7 @@ function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) {
 
 function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
     global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash;
-    $aMbxs = $aQuery = $aTag = array();
+    $aMbxs = $aQuery = array();
     sqimap_tree_to_ref_array($mbx_tree,$aMbxs);
     // remove the root node
     array_shift($aMbxs);