Code cleaning
[squirrelmail.git] / functions / imap_mailbox.php
index f83ab6f510d2d0dfc547a3c4764239bb7dd2890a..7fc6889d4ae33551bd2d2af079a0a6a8dede6c5c 100755 (executable)
  * $Id$
  */
 
+function isBoxBelow( $box2, $box1 ) {
+
+    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++;
+        }
+    }
+
+    return( substr( $box1, 0, $i ) == substr( $boxs, 0, $i ) );
+
+}
+
 /*
     Defines Special Mail Boxes
 */
-
 function isSpecialMailbox( $box ) {
 
     global $trash_folder, $sent_folder, $draft_folder,
-           $move_to_trash, $move_to_sent, $save_as_draft,
-           $delimiter, $folder_prefix;
+           $move_to_trash, $move_to_sent, $save_as_draft;
 
-    if ( $move_to_sent  ) {
-        $i = strpos( $sent_folder, $delimiter, strlen( $folder_prefix ) );
-    }
-     // echo '[' . substr( $sent_folder, 0, $i ) . '] (' . $box . ')'; 
     $ret = ( (strtolower($box) == 'inbox') ||
-             ($box == $trash_folder &&
-              $move_to_trash) ||
-             ( substr( $sent_folder, 0, $i ) == substr( $box, 0, $i ) &&
-              $move_to_sent) ||
-             ($box == $draft_folder &&
-              $save_as_draft) );
+             ( $move_to_trash && isBoxBelow( $box, $trash_folder ) ) ||
+             ( $move_to_sent && isBoxBelow( $box, $sent_folder )) ||
+             ($save_as_draft && $box == $draft_folder ) );
+
+    if ( !$ret ) {
+        $ret = do_hook_function( 'special_mailbox', $box );
+    }
 
     return( $ret );
 
@@ -64,14 +85,14 @@ function sqimap_mailbox_exists ($imap_stream, $mailbox)
  **  Selects a mailbox
  ******************************************************************************/
 function sqimap_mailbox_select ($imap_stream, $mailbox,
-                                $hide=TRUE, $recent=false)
+                                $hide=TRUE, $recent=false, $extrainfo=false)
 {
     global $auto_expunge;
-    
+
     if ( $mailbox == 'None' ) {
         return;
     }
-    
+
     $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
                                TRUE, $response, $message);
     if ($recent) {
@@ -81,17 +102,42 @@ function sqimap_mailbox_select ($imap_stream, $mailbox,
             }
         }
         return $r[1];
-    }
-    if ($auto_expunge) {
-        $tmp = sqimap_run_command($imap_stream, 'EXPUNGE',
+    } else {
+        if ($auto_expunge) {
+            $tmp = sqimap_run_command($imap_stream, 'EXPUNGE',
                                   false, $a, $b);
+        }
+        if (isset( $extrainfo ) && $extrainfo) {
+            $result = array();
+            for ($i=0; $i<count($read); $i++) {
+                if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
+                    $regs[1]=trim(preg_replace (  array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
+                    $result['PERMANENTFLAGS'] = $regs[1];
+                }
+                else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
+                    $regs[1]=trim(preg_replace (  array ("/\(/","/\)/") ,'', $regs[1])) ;
+                    $result["FLAGS"] = $regs[1];
+                }
+                else if (preg_match("/(.*)EXISTS/i",$read[$i], $regs)) {
+                    $result['EXISTS']=trim($regs[1]);
+                }
+                else if (preg_match("/(.*)RECENT/i",$read[$i], $regs)) {
+                    $result['RECENT']=trim($regs[1]);
+                }
+                else if (preg_match("/\[UNSEEN(.*)\]/i",$read[$i], $regs)) {
+                    $result['UNSEEN']=trim($regs[1]);
+                }
+
+            }
+            return( $result );
+        }
     }
 }
 
 
 
 /******************************************************************************
- **  Creates a folder 
+ **  Creates a folder
  ******************************************************************************/
 function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
 {
@@ -101,7 +147,7 @@ function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
     }
     $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
                                    TRUE, $response, $message);
-    
+
     sqimap_subscribe ($imap_stream, $mailbox);
 }
 
@@ -339,13 +385,13 @@ function user_strcasecmp($a, $b) {
  **  See comment on sqimap_mailbox_parse() for info about the returned array.
  ******************************************************************************/
 function sqimap_mailbox_list ($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;
 
-    $inbox_in_list = false;
-    $inbox_subscribed = false;
+    $inbox_in_list = $inbox_subscribed = FALSE;
 
     require_once('../src/load_prefs.php');
     require_once('../functions/array.php');
@@ -477,6 +523,7 @@ function sqimap_mailbox_list ($imap_stream) {
     }
 
     return( $boxesnew );
+
 }
 
 /*
@@ -487,9 +534,7 @@ function sqimap_mailbox_list_all ($imap_stream)
     global $list_special_folders_first, $folder_prefix;
     global $delimiter;
 
-    if (!function_exists('ary_sort')) {
-        include_once('../functions/array.php');
-    }
+    require_once('../functions/array.php');
 
     $ssid = sqimap_session_id();
     $lsid = strlen( $ssid );
@@ -511,7 +556,7 @@ function sqimap_mailbox_list_all ($imap_stream)
         if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
 
             /* Store the raw IMAP reply */
-            $boxes[$g]["raw"] = $read_ary[$i];
+            $boxes[$g]['raw'] = $read_ary[$i];
 
             /* Count number of delimiters ($delimiter) in folder name */
             $mailbox = find_mailbox_name($read_ary[$i]);