fixes "orphaned" sub folders when renaming parent folder
[squirrelmail.git] / functions / imap_mailbox.php
index 670c119588bf8c666f3dd5bcf0d4b8162e3d9cb2..63e9dafa9766c67c4262acc99c2c38209aa2bcfd 100755 (executable)
@@ -207,37 +207,35 @@ function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
 
     if ( $old_name <> $new_name ) {
 
-        global $delimiter;
+        global $delimiter, $imap_server_type;
 
         if ( substr( $old_name, -1 ) == $delimiter  ) {
             $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
             $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
             $postfix = $delimiter;
-            $boxesall = sqimap_mailbox_list($imap_stream);
         } else {
             $postfix = '';
-            $boxesall = FALSE;
         }
-
+        $boxesall = sqimap_mailbox_list($imap_stream);
         $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' .  quoteIMAP($new_name) . '"';
         $data = sqimap_run_command($imap_stream, $cmd,
                                  TRUE, $response, $message);
         sqimap_unsubscribe($imap_stream, $old_name.$postfix);
         sqimap_subscribe($imap_stream, $new_name.$postfix);
-
-        if ( $boxesall ) {
-            // Sub-unsub subfolders
-            $l = strlen( $old_name ) + 1;
-            $p = 'unformatted';
-            foreach ( $boxesall as $box ) {
-                if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
-                    sqimap_unsubscribe($imap_stream, $box[$p]);
-                    sqimap_subscribe($imap_stream,
-                                     $new_name . $delimiter . substr( $box[$p], $l ) );
+        $l = strlen( $old_name ) + 1;
+        $p = 'unformatted';
+        foreach ( $boxesall as $box ) {
+            if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
+                $new_sub = $new_name . $delimiter . substr($box[$p], $l);
+                if ($imap_server_type == 'cyrus') {
+                    $cmd = 'RENAME "' . quoteIMAP($box[$p]) . '" "' .  quoteIMAP($new_sub) . '"';
+                    $data = sqimap_run_command($imap_stream, $cmd, TRUE, 
+                                               $response, $message);
                 }
+                sqimap_unsubscribe($imap_stream, $box[$p]);
+                sqimap_subscribe($imap_stream, $new_sub);
             }
         }
-
     }
 
 }
@@ -631,4 +629,4 @@ function sqimap_mailbox_list_all($imap_stream) {
     return $boxes;
 }
 
-?>
\ No newline at end of file
+?>