Fix for 636577.
[squirrelmail.git] / functions / imap_mailbox.php
index f5ded3266f097f0400d946234c7b626742f54892..ee1e657adb8cddfc1c8520ed73d6b5c24636ed68 100755 (executable)
@@ -3,7 +3,7 @@
 /**
  * imap_mailbox.php
  *
- * Copyright (c) 1999-2002 The SquirrelMail Project Team
+ * Copyright (c) 1999-2003 The SquirrelMail Project Team
  * Licensed under the GNU GPL. For full terms see the file COPYING.
  *
  * This impliments all functions that manipulate mailboxes
@@ -105,29 +105,41 @@ function readMailboxParent($haystack, $needle) {
     return( $ret );
 }
 
-
-function isBoxBelow( $box2, $box1 ) {
+/** 
+ * Check if $subbox is below the specified $parentbox
+ */
+function isBoxBelow( $subbox, $parentbox ) {
     global $delimiter, $folder_prefix, $imap_server_type;
 
-    if ( $imap_server_type == 'uw' ) {
-        $boxs = $box2;
-        $i = strpos( $box1, $delimiter, strlen( $folder_prefix ) );
-        if ( $i === false ) {
-            $i = strlen( $box2 );
-        }
-    } else {
-        $boxs = $box2 . $delimiter;
-        /* Skip next second delimiter */
-        $i = strpos( $box1, $delimiter );
-        $i = strpos( $box1, $delimiter, $i + 1  );
-        if ( $i === false ) {
-            $i = strlen( $box2 );
-        } else {
-            $i++;
-        }
+    /* 
+     * Eliminate the obvious mismatch, where the 
+     * subfolder path is shorter than that of the potential parent
+     */
+    if ( strlen($subbox) < strlen($parentbox) ) {
+      return false;
     }
 
-    return ( substr( $box1, 0, $i ) == substr( $boxs, 0, $i ) );
+       if ( $imap_server_type == 'uw' ) {
+               $boxs = $parentbox;
+               $i = strpos( $subbox, $delimiter, strlen( $folder_prefix ) );
+               if ( $i === false ) {
+                       $i = strlen( $parentbox );
+               }
+       } else {
+               if (substr($parentbox,0,strlen($subbox)) == $subbox) {
+                       return true;
+               }
+               $boxs = $parentbox . $delimiter;
+               /* Skip next second delimiter */
+               $i = strpos( $subbox, $delimiter );
+               $i = strpos( $subbox, $delimiter, $i + 1  );
+               if ( $i === false ) {
+                       $i = strlen( $parentbox );
+               } else {
+                       $i++;
+               }
+       }
+       return ( substr( $subbox, 0, $i ) == substr( $boxs, 0, $i ) );
 }
 
 /* Defines special mailboxes */
@@ -136,17 +148,32 @@ function isSpecialMailbox( $box ) {
            $move_to_trash, $move_to_sent, $save_as_draft;
 
     $ret = ( (strtolower($box) == 'inbox') ||
-             ( $move_to_trash && isBoxBelow( $box, $trash_folder ) ) ||
-             ( $move_to_sent && isBoxBelow( $box, $sent_folder )) ||
-             ($save_as_draft && $box == $draft_folder ) );
+             isTrashMailbox($box) || isSentMailbox($box) || isDraftMailbox($box) );
 
     if ( !$ret ) {
         $ret = do_hook_function( 'special_mailbox', $box );
     }
-
     return $ret;
 }
 
+function isTrashMailbox ($box) {
+    global $trash_folder, $move_to_trash;
+    return $move_to_trash && $trash_folder &&
+           ( $box == $trash_folder || isBoxBelow($box, $trash_folder) );
+}
+
+function isSentMailbox($box) {
+   global $sent_folder, $move_to_sent;
+   return $move_to_sent && $sent_folder &&
+          ( $box == $sent_folder || isBoxBelow($box, $sent_folder) );
+}
+
+function isDraftMailbox($box) {
+   global $draft_folder, $save_as_draft;
+   return $save_as_draft &&
+          ( $box == $draft_folder || isBoxBelow($box, $draft_folder) );
+}
+
 /* Expunges a mailbox */
 function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
     global $uid_support;
@@ -225,7 +252,7 @@ function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
     if (strtolower($type) == 'noselect') {
         $mailbox .= $delimiter;
     }
-    $mailbox  = imap_utf7_encode_local($mailbox);
+
     $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
                                    true, $response, $message);
     sqimap_subscribe ($imap_stream, $mailbox);
@@ -239,7 +266,6 @@ function sqimap_subscribe ($imap_stream, $mailbox) {
 
 /* Unsubscribes to an existing folder */
 function sqimap_unsubscribe ($imap_stream, $mailbox) {
-    global $imap_server_type;
     $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
                                    true, $response, $message);
 }
@@ -250,7 +276,7 @@ function sqimap_mailbox_delete ($imap_stream, $mailbox) {
     $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
                                    true, $response, $message);
     sqimap_unsubscribe ($imap_stream, $mailbox);
-    do_hook_function("rename_or_delete_folder", $args = array($mailbox, 'delete', ''));
+    do_hook_function('rename_or_delete_folder', $args = array($mailbox, 'delete', ''));
     removePref($data_dir, $username, "thread_$mailbox");
 }
 
@@ -276,31 +302,33 @@ function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
         } else {
             $postfix = '';
         }
+
         $boxesall = sqimap_mailbox_list($imap_stream);
-        $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' .  quoteIMAP($new_name) . '"';
+        $cmd = 'RENAME "' . $old_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);
-        removePref($data_dir, $username, "thread_".$old_name.$postfix);
+        $oldpref = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
+        removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
         sqimap_subscribe($imap_stream, $new_name.$postfix);
-        setPref($data_dir, $username, "thread_".$new_name.$postfix, $oldpref);
-        do_hook_function("rename_or_delete_folder",$args = array($old_name, 'rename', $new_name));
+        setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref);
+        do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name));
         $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) . '"';
+                    $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
                     $data = sqimap_run_command($imap_stream, $cmd, true,
                                                $response, $message);
                 }
                 sqimap_unsubscribe($imap_stream, $box[$p]);
-                $oldpref = getPref($data_dir, $username, "thread_".$box[$p]);
-                removePref($data_dir, $username, "thread_".$box[$p]);
+                $oldpref = getPref($data_dir, $username, 'thread_'.$box[$p]);
+                removePref($data_dir, $username, 'thread_'.$box[$p]);
                 sqimap_subscribe($imap_stream, $new_sub);
-                setPref($data_dir, $username, "thread_".$new_sub, $oldpref);
-                do_hook_function("rename_or_delete_folder",
+                setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref);
+                do_hook_function('rename_or_delete_folder',
                                  $args = array($box[$p], 'rename', $new_sub));
             }
         }
@@ -325,9 +353,9 @@ function sqimap_mailbox_parse ($line, $line_lsub) {
     for ($g = 0, $cnt = count($line); $g < $cnt; $g++) {
         /* Store the raw IMAP reply */
         if (isset($line[$g])) {
-            $boxesall[$g]["raw"] = $line[$g];
+            $boxesall[$g]['raw'] = $line[$g];
         } else {
-            $boxesall[$g]["raw"] = '';
+            $boxesall[$g]['raw'] = '';
         }
 
         /* Count number of delimiters ($delimiter) in folder name */
@@ -736,7 +764,7 @@ function sqimap_mailbox_list_all($imap_stream) {
         $g++;
     }
     if(is_array($boxes)) {
-        $boxes = sort ($boxes);
+        sort ($boxes);
     }
 
     return $boxes;
@@ -935,4 +963,5 @@ function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
 
     return $mailboxes;
 }
+
 ?>