Fix folder rename to cope with collapsed-prefs, cyrus>=2.0 and renaming
[squirrelmail.git] / functions / imap_mailbox.php
index 56883236e6e4ef334d46a0e90cd5c0944d5d2e2a..066896180a10d496e8bd447b59d04f8a2e469ae5 100755 (executable)
@@ -3,7 +3,7 @@
 /**
  * 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
@@ -230,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;
     }
@@ -344,6 +344,7 @@ function sqimap_mailbox_delete ($imap_stream, $mailbox) {
     } else {
         do_hook_function('rename_or_delete_folder', $args = array($mailbox, 'delete', ''));
         removePref($data_dir, $username, "thread_$mailbox");
+        removePref($data_dir, $username, "collapse_folder_$mailbox");
     }
 }
 
@@ -374,15 +375,18 @@ function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
             $postfix = '';
         }
 
-        $boxesall = sqimap_mailbox_list($imap_stream);
+        $boxesall = sqimap_mailbox_list_all($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);
+        $oldpref_thread = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
+        $oldpref_collapse = getPref($data_dir, $username, 'collapse_folder_'.$old_name.$postfix);
         removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
+        removePref($data_dir, $username, 'collapse_folder_'.$old_name.$postfix);
         sqimap_subscribe($imap_stream, $new_name.$postfix);
-        setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref);
+        setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref_thread);
+        setPref($data_dir, $username, 'collapse_folder_'.$new_name.$postfix, $oldpref_collapse);
         do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name));
         $l = strlen( $old_name ) + 1;
         $p = 'unformatted';
@@ -390,16 +394,25 @@ function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
         foreach ($boxesall as $box) {
             if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
                 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
+                /* With Cyrus IMAPd >= 2.0 rename is recursive, so don't check for errors here */
                 if ($imap_server_type == 'cyrus') {
                     $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
-                    $data = sqimap_run_command($imap_stream, $cmd, true,
+                    $data = sqimap_run_command($imap_stream, $cmd, false,
                                                $response, $message);
                 }
-                sqimap_unsubscribe($imap_stream, $box[$p]);
-                $oldpref = getPref($data_dir, $username, 'thread_'.$box[$p]);
+                $was_subscribed = sqimap_mailbox_is_subscribed($imap_stream, $box[$p]);
+                if ( $was_subscribed ) {
+                    sqimap_unsubscribe($imap_stream, $box[$p]);
+                }
+                $oldpref_thread = getPref($data_dir, $username, 'thread_'.$box[$p]);
+                $oldpref_collapse = getPref($data_dir, $username, 'collapse_folder_'.$box[$p]);
                 removePref($data_dir, $username, 'thread_'.$box[$p]);
-                sqimap_subscribe($imap_stream, $new_sub);
-                setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref);
+                removePref($data_dir, $username, 'collapse_folder_'.$box[$p]);
+                if ( $was_subscribed ) {
+                    sqimap_subscribe($imap_stream, $new_sub);
+                }
+                setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref_thread);
+                setPref($data_dir, $username, 'collapse_folder_'.$new_sub, $oldpref_collapse);
                 do_hook_function('rename_or_delete_folder',
                                  $args = array($box[$p], 'rename', $new_sub));
             }
@@ -532,10 +545,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('  ', '. ', $boxes_part['formatted']);
+                    $box2 = str_replace('  ', '. ', htmlspecialchars($boxes_part['formatted']));
                     break;
                   case 1:   /* indent, style = 1 */
-                    $box2 = $boxes_part['formatted'];
+                    $box2 = str_replace('  ', '  ', htmlspecialchars($boxes_part['formatted']));
                     break;
                   default:  /* default, long names, style = 0 */
                     $box2 = str_replace(' ', ' ', htmlspecialchars(imap_utf7_decode_local($boxes_part['unformatted-disp'])));
@@ -555,32 +568,31 @@ function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_sk
 /**
  * Returns sorted mailbox lists in several different ways.
  * See comment on sqimap_mailbox_parse() for info about the returned array.
+ * @param resource $imap_stream imap connection resource
+ * @param boolean $force force update of mailbox listing. available since 1.4.2 and 1.5.0
+ * @return array list of mailboxes
  */
-
-
 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\" \"*\"";
@@ -689,7 +701,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 */
@@ -749,23 +760,25 @@ function sqimap_mailbox_list_all($imap_stream) {
 }
 
 function sqimap_mailbox_tree($imap_stream) {
-    global $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);
 
@@ -773,12 +786,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
@@ -795,7 +808,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)) {
@@ -847,19 +860,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);
+       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();
@@ -871,7 +888,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;
@@ -882,7 +898,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();
@@ -985,7 +1001,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);
@@ -1055,4 +1071,4 @@ function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
     }
 }
 
-?>
\ No newline at end of file
+?>