From bbce3edd61d05e0aa0bade7364315f7840549d9e Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Mon, 28 Feb 2011 12:59:13 -0500
Subject: [PATCH] - Add simple ACL rights/namespace handling in folder manager (display folder's namespace, modify edit form fields according to MYRIGHTS)

---
 program/localization/de_DE/labels.inc  |    4 +
 CHANGELOG                              |    1 
 program/steps/settings/edit_folder.inc |  112 ++++++++++++++++-----------
 program/include/rcube_imap.php         |   32 ++++++++
 program/steps/settings/func.inc        |   40 ++++++++++
 program/steps/settings/save_folder.inc |    9 -
 program/localization/en_US/labels.inc  |    4 +
 7 files changed, 150 insertions(+), 52 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index a72d8d6..0d7d394 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
+- Add simple ACL rights/namespace handling in folder manager
 - Fix parsing links with non-printable characters inside (#1487805)
 - Force IE to send referers (#1487806)
 - Fixed de_CH Localization bugs (#1487773)
diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index f5a9368..b8cb017 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -3321,6 +3321,38 @@
 
 
     /**
+     * Returns the namespace where the folder is in
+     *
+     * @param string $mbox_name Folder name
+     *
+     * @return string One of 'personal', 'other' or 'shared'
+     * @access public
+     */
+    function mailbox_namespace($mbox_name)
+    {
+        if ($mbox_name == 'INBOX') {
+            return 'personal';
+        }
+
+        foreach ($this->namespace as $type => $namespace) {
+            if (is_array($namespace)) {
+                foreach ($namespace as $ns) {
+                    if (strlen($ns[0])) {
+                        if ((strlen($ns[0])>1 && $mbox_name == substr($ns[0], 0, -1))
+                            || strpos($mbox_name, $ns[0]) === 0
+                        ) {
+                            return $type;
+                        }
+                    }
+                }
+            }
+        }
+
+        return 'personal';
+    }
+
+
+    /**
      * Modify folder name for input/output according to root dir and namespace
      *
      * @param string  $mbox_name Folder name
diff --git a/program/localization/de_DE/labels.inc b/program/localization/de_DE/labels.inc
index 4312c9b..b34508c 100644
--- a/program/localization/de_DE/labels.inc
+++ b/program/localization/de_DE/labels.inc
@@ -374,6 +374,10 @@
 $labels['info'] = 'Informationen';
 $labels['getfoldersize'] = 'Ordnergrösse anzeigen';
 $labels['changesubscription'] = 'Abonnieren';
+$labels['foldertype'] = 'Ordnertyp';
+$labels['personalfolder']  = 'Persönlicher Ordner';
+$labels['otherfolder']  = 'Ordner eines anderen Benutzers';
+$labels['sharedfolder']  = 'Öffentlicher Ordner';
 $labels['sortby'] = 'Sortieren nach';
 $labels['sortasc'] = 'aufsteigend sortieren';
 $labels['sortdesc'] = 'absteigend sortieren';
diff --git a/program/localization/en_US/labels.inc b/program/localization/en_US/labels.inc
index 8e99038..2175aea 100644
--- a/program/localization/en_US/labels.inc
+++ b/program/localization/en_US/labels.inc
@@ -427,6 +427,10 @@
 $labels['info'] = 'Information';
 $labels['getfoldersize'] = 'Click to get folder size';
 $labels['changesubscription'] = 'Click to change subscription';
+$labels['foldertype'] = 'Folder Type';
+$labels['personalfolder']  = 'Private Folder';
+$labels['otherfolder']  = 'Other User\'s Folder';
+$labels['sharedfolder']  = 'Public Folder';
 
 $labels['sortby'] = 'Sort by';
 $labels['sortasc']  = 'Sort ascending';
diff --git a/program/steps/settings/edit_folder.inc b/program/steps/settings/edit_folder.inc
index 36e3df0..34dc064 100644
--- a/program/steps/settings/edit_folder.inc
+++ b/program/steps/settings/edit_folder.inc
@@ -36,25 +36,24 @@
     $parent      = trim(get_input_value('_path', RCUBE_INPUT_GPC, true));
     $parent_imap = rcube_charset_convert($parent, RCMAIL_CHARSET, 'UTF7-IMAP');
 
-    $delimiter = $RCMAIL->imap->get_hierarchy_delimiter();
-    $special   = (strlen($mbox_imap) && in_array($mbox_imap, (array) $RCMAIL->config->get('default_imap_folders')));
-    $protected = ($special && $RCMAIL->config->get('protect_default_folders'));
     $threading_supported = $RCMAIL->imap->get_capability('THREAD');
+    $delimiter = $RCMAIL->imap->get_hierarchy_delimiter();
 
-    // Get mailbox stats (messages count, etc.), mailbox name and parent
+    // Get mailbox parameters
     if (strlen($mbox)) {
-        $msgcount = $RCMAIL->imap->messagecount($mbox_imap, 'ALL', true, false);
+        $options   = rcube_folder_options($mbox_imap);
+        $namespace = $RCMAIL->imap->get_namespace();
 
         $path   = explode($delimiter, $mbox_imap);
         $folder = array_pop($path);
         $path   = implode($delimiter, $path);
-
         $folder = rcube_charset_convert($folder, 'UTF7-IMAP');
 
         $hidden_fields = array('name' => '_mbox', 'value' => $mbox);
     }
     else {
-        $path = $parent_imap;
+        $options = array();
+        $path    = $parent_imap;
     }
 
     $form = array();
@@ -65,8 +64,12 @@
     );
 
     // Location (name)
-    if ($protected)
-        $foldername = rcmail_localize_foldername($mbox_imap);
+    if ($options['protected']) {
+        $foldername = Q(rcmail_localize_foldername($mbox_imap));
+    }
+    else if ($options['norename']) {
+        $foldername = Q($folder);
+    }
     else {
         if (isset($_POST['_name']))
             $folder = trim(get_input_value('_name', RCUBE_INPUT_POST, true));
@@ -74,8 +77,9 @@
         $foldername = new html_inputfield(array('name' => '_name', 'id' => '_name', 'size' => 30));
         $foldername = $foldername->show($folder);
 
-        if ($special)
-            $foldername .= '&nbsp;(' . rcmail_localize_foldername($mbox_imap) .')';
+        if ($options['special']) {
+            $foldername .= '&nbsp;(' . Q(rcmail_localize_foldername($mbox_imap)) .')';
+        }
     }
 
     $form['props']['fieldsets']['location'] = array(
@@ -89,19 +93,26 @@
     );
 
     if (strlen($path)) {
-        $radio1 = new html_radiobutton(array('name' => '_parent', 'value' => ''));
-        $radio2 = new html_radiobutton(array('name' => '_parent', 'value' => $path));
-        $selected  = isset($_POST['_parent']) ? $_POST['_parent'] : $path;
+        if ($options['norename'] || $options['namespace'] != 'personal') {
+            // prevent user from moving folder
+            $hidden_path = new html_hiddenfield(array('name' => '_parent', 'value' => $path));
+            $form['props']['fieldsets']['location']['content']['name']['value'] .= $hidden_path->show();
+        }
+        else {
+            $radio1 = new html_radiobutton(array('name' => '_parent', 'value' => ''));
+            $radio2 = new html_radiobutton(array('name' => '_parent', 'value' => $path));
+            $selected  = isset($_POST['_parent']) ? $_POST['_parent'] : $path;
 
-        $html_path = str_replace($delimiter, ' &raquo; ', rcmail_localize_folderpath($path));
+            $html_path = str_replace($delimiter, ' &raquo; ', rcmail_localize_folderpath($path));
 
-        $folderpath = $radio1->show($selected) . Q(rcube_label('none')) . '&nbsp;'
-            .$radio2->show($selected) . Q($html_path);
+            $folderpath = $radio1->show($selected) . Q(rcube_label('none')) . '&nbsp;'
+                .$radio2->show($selected) . Q($html_path);
 
-        $form['props']['fieldsets']['location']['content']['path'] = array(
-            'label' => rcube_label('parentfolder'),
-            'value' => $folderpath,
-        );
+            $form['props']['fieldsets']['location']['content']['path'] = array(
+                'label' => rcube_label('parentfolder'),
+                'value' => $folderpath,
+            );
+        }
     }
 
     // Settings
@@ -110,7 +121,7 @@
     );
 
     // Settings: threading
-    if ($threading_supported) {
+    if ($threading_supported && !$options['noselect']) {
         $select = new html_select(array('name' => '_viewmode', 'id' => '_listmode'));
         $select->add(rcube_label('list'), 0);
         $select->add(rcube_label('threads'), 1);
@@ -164,34 +175,46 @@
         // Number of messages
         $form['props']['fieldsets']['info'] = array(
             'name'  => rcube_label('info'),
-            'content' => array(
-                'count' => array(
-                    'label' => rcube_label('messagecount'),
-                    'value' => (int) $msgcount,
-                ),
-            ),
+            'content' => array()
         );
 
-        // Size
-        if ($msgcount) {
-            // create link with folder-size command
-            $onclick = sprintf("return %s.command('folder-size', '%s', this)",
-                JS_OBJECT_NAME, JQ($mbox_imap));
-            $size = html::a(array('href' => '#', 'onclick' => $onclick, 'id' => 'folder-size'),
-                rcube_label('getfoldersize'));
+        if (!$options['noselect']) {
+            $msgcount = $RCMAIL->imap->messagecount($mbox_imap, 'ALL', true, false);
+
+            // Size
+            if ($msgcount) {
+                // create link with folder-size command
+                $onclick = sprintf("return %s.command('folder-size', '%s', this)",
+                    JS_OBJECT_NAME, JQ($mbox_imap));
+                $size = html::a(array('href' => '#', 'onclick' => $onclick,
+                    'id' => 'folder-size'), rcube_label('getfoldersize'));
+            }
+            else {
+                // no messages -> zero size
+                $size = 0;
+            }
+
+            $form['props']['fieldsets']['info']['content']['count'] = array(
+                'label' => rcube_label('messagecount'),
+                'value' => (int) $msgcount
+            );
+            $form['props']['fieldsets']['info']['content']['size'] = array(
+                'label' => rcube_label('size'),
+                'value' => $size,
+            );
         }
-        else {
-            // no messages -> zero size
-            $size = 0;
+
+        // show folder type only if we have non-private namespaces
+        if (!empty($namespace['shared']) || !empty($namespace['others'])) {
+            $form['props']['fieldsets']['info']['content']['foldertype'] = array(
+                'label' => rcube_label('foldertype'),
+                'value' => rcube_label($options['namespace'] . 'folder'));
         }
-        $form['props']['fieldsets']['info']['content']['size'] = array(
-            'label' => rcube_label('size'),
-            'value' => $size,
-        );
     }
 
     // Allow plugins to modify folder form content
-    $plugin = $RCMAIL->plugins->exec_hook('folder_form', array('form' => $form));
+    $plugin = $RCMAIL->plugins->exec_hook('folder_form',
+        array('form' => $form, 'options' => $options));
 
     $form = $plugin['form'];
 
@@ -267,9 +290,6 @@
         if ($protect_folders && in_array($directory, $default_folders)) {
             unset($result);
             $result[] = rcmail_localize_foldername($directory);
-        }
-        else if ($protect_folders && in_array($dir, $default_folders)) {
-            $result[] = rcmail_localize_foldername($dir);
         }
         else {
             $result[] = rcube_charset_convert($dir, 'UTF7-IMAP');
diff --git a/program/steps/settings/func.inc b/program/steps/settings/func.inc
index 271ee40..6d3919f 100644
--- a/program/steps/settings/func.inc
+++ b/program/steps/settings/func.inc
@@ -756,6 +756,46 @@
   return $skins;
 }
 
+
+function rcube_folder_options($mailbox)
+{
+    global $RCMAIL;
+
+    $acl             = $RCMAIL->imap->get_capability('ACL');
+    $default_folders = (array) $RCMAIL->config->get('default_imap_folders');
+    $options         = array();
+
+    $options['name']      = $mailbox;
+    $options['options']   = $RCMAIL->imap->mailbox_options($mailbox, true);
+    $options['namespace'] = $RCMAIL->imap->mailbox_namespace($mailbox);
+    $options['rights']    = $acl ? (array)$RCMAIL->imap->my_rights($mailbox) : array();
+    $options['special']   = in_array($mailbox, $default_folders);
+    $options['protected'] = $options['special'] && $RCMAIL->config->get('protect_default_folders');
+
+    if (is_array($options['options'])) {
+        foreach ($options['options'] as $opt) {
+            $opt = strtolower($opt);
+            if ($opt == '\noselect' || $opt == '\nonexistent') {
+                $options['noselect'] = true;
+            }
+        }
+    }
+    else {
+        $options['noselect'] = true;
+    }
+
+    if (!empty($options['rights'])) {
+        $options['norename'] = !in_array('x', $options['rights']) &&
+            (!in_array('c', $options['rights']) || !in_array('d', $options['rights']));
+        if (!$options['noselect']) {
+            $options['noselect'] = !in_array('r', $options['rights']);
+        }
+    }
+
+    return $options;    
+}
+
+
 // register UI objects
 $OUTPUT->add_handlers(array(
   'prefsframe' => 'rcmail_preferences_frame',
diff --git a/program/steps/settings/save_folder.inc b/program/steps/settings/save_folder.inc
index d8ba87b..70aad6d 100644
--- a/program/steps/settings/save_folder.inc
+++ b/program/steps/settings/save_folder.inc
@@ -34,12 +34,10 @@
 // $path is in UTF7-IMAP already
 
 $delimiter = $IMAP->get_hierarchy_delimiter();
-$special   = (strlen($old_imap) && in_array($old_imap, (array) $RCMAIL->config->get('default_imap_folders')));
-$protected = ($special && $RCMAIL->config->get('protect_default_folders'));
-
+$options = strlen($old_imap) ? rcube_folder_options($old_imap) : array();
 
 // Folder name checks
-if ($protected) {
+if ($options['protected'] || $options['norename']) {
 }
 else if (!strlen($name)) {
     $error = rcube_label('cannotbeempty');
@@ -61,7 +59,7 @@
     $OUTPUT->command('display_message', $error, 'error');
 }
 else {
-    if ($protected) {
+    if ($options['protected'] || $options['norename']) {
         $name_imap = $old_imap;
     }
     else if (strlen($path)) {
@@ -141,7 +139,6 @@
 
             // In case of name change update names of childrens in settings
             if ($rename) {
-                $delimiter  = $RCMAIL->imap->get_hierarchy_delimiter();
                 $oldprefix  = '/^' . preg_quote($folder['oldname'] . $delimiter, '/') . '/';
                 foreach ($a_threaded as $key => $val) {
                     if ($key == $folder['oldname']) {

--
Gitblit v1.9.1