From f8c96f737c1916377e361e3fbaa8a415c4101ca4 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Thu, 07 Jun 2012 04:34:56 -0400
Subject: [PATCH] Enable attachments drag&drop upload for default skin

---
 program/include/rcube_imap.php |  350 +++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 211 insertions(+), 139 deletions(-)

diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index cec0ad8..00a4158 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -19,9 +19,6 @@
  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
  | Author: Aleksander Machniak <alec@alec.pl>                            |
  +-----------------------------------------------------------------------+
-
- $Id$
-
 */
 
 
@@ -404,15 +401,56 @@
      */
     public function check_permflag($flag)
     {
-        $flag = strtoupper($flag);
-        $imap_flag = $this->conn->flags[$flag];
+        $flag       = strtoupper($flag);
+        $imap_flag  = $this->conn->flags[$flag];
+        $perm_flags = $this->get_permflags($this->folder);
 
-        if ($this->folder !== null) {
-            $this->check_connection();
+        return in_array_nocase($imap_flag, $perm_flags);
+    }
+
+
+    /**
+     * Returns PERMANENTFLAGS of the specified folder
+     *
+     * @param  string $folder Folder name
+     *
+     * @return array Flags
+     */
+    public function get_permflags($folder)
+    {
+        if (!strlen($folder)) {
+            return array();
         }
-        // @TODO: cache permanent flags (?)
+/*
+        Checking PERMANENTFLAGS is rather rare, so we disable caching of it
+        Re-think when we'll use it for more than only MDNSENT flag
 
-        return (in_array_nocase($imap_flag, $this->conn->data['PERMANENTFLAGS']));
+        $cache_key = 'mailboxes.permanentflags.' . $folder;
+        $permflags = $this->get_cache($cache_key);
+
+        if ($permflags !== null) {
+            return explode(' ', $permflags);
+        }
+*/
+        if (!$this->check_connection()) {
+            return array();
+        }
+
+        if ($this->conn->select($folder)) {
+            $permflags = $this->conn->data['PERMANENTFLAGS'];
+        }
+        else {
+            return array();
+        }
+
+        if (!is_array($permflags)) {
+            $permflags = array();
+        }
+/*
+        // Store permflags as string to limit cached object size
+        $this->update_cache($cache_key, implode(' ', $permflags));
+*/
+        return $permflags;
     }
 
 
@@ -1597,11 +1635,24 @@
                 $structure[1] = $m[2];
             }
             else {
-                return $headers;
+                // Try to parse the message using Mail_mimeDecode package
+                // We need a better solution, Mail_mimeDecode parses message
+                // in memory, which wouldn't work for very big messages,
+                // (it uses up to 10x more memory than the message size)
+                // it's also buggy and not actively developed
+                if ($headers->size && rcube_utils::mem_check($headers->size * 10)) {
+                    $raw_msg = $this->get_raw_body($uid);
+                    $struct = rcube_mime::parse_message($raw_msg);
+                }
+                else {
+                    return $headers;
+                }
             }
         }
 
-        $struct = $this->structure_part($structure, 0, '', $headers);
+        if (empty($struct)) {
+            $struct = $this->structure_part($structure, 0, '', $headers);
+        }
 
         // don't trust given content-type
         if (empty($struct->parts) && !empty($headers->ctype)) {
@@ -2468,7 +2519,16 @@
             return $a_mboxes;
         }
 
-        $a_mboxes = $this->_list_folders_subscribed($root, $name, $filter, $rights);
+        // Give plugins a chance to provide a list of folders
+        $data = rcube::get_instance()->plugins->exec_hook('storage_folders',
+            array('root' => $root, 'name' => $name, 'filter' => $filter, 'mode' => 'LSUB'));
+
+        if (isset($data['folders'])) {
+            $a_mboxes = $data['folders'];
+        }
+        else {
+            $a_mboxes = $this->list_folders_subscribed_direct($root, $name);
+        }
 
         if (!is_array($a_mboxes)) {
             return array();
@@ -2497,76 +2557,74 @@
 
 
     /**
-     * protected method for folders listing (LSUB)
+     * Method for direct folders listing (LSUB)
      *
      * @param   string  $root   Optional root folder
      * @param   string  $name   Optional name pattern
-     * @param   mixed   $filter Optional filter
-     * @param   string  $rights Optional ACL requirements
      *
      * @return  array   List of subscribed folders
      * @see     rcube_imap::list_folders_subscribed()
      */
-    protected function _list_folders_subscribed($root='', $name='*', $filter=null, $rights=null)
+    public function list_folders_subscribed_direct($root='', $name='*')
     {
-        $a_defaults = $a_out = array();
-
-        // Give plugins a chance to provide a list of folders
-        $data = rcube::get_instance()->plugins->exec_hook('storage_folders',
-            array('root' => $root, 'name' => $name, 'filter' => $filter, 'mode' => 'LSUB'));
-
-        if (isset($data['folders'])) {
-            $a_folders = $data['folders'];
-        }
-        else if (!$this->check_connection()) {
+        if (!$this->check_connection()) {
            return null;
         }
-        else {
-            // Server supports LIST-EXTENDED, we can use selection options
-            $config = rcube::get_instance()->config;
-            // #1486225: Some dovecot versions returns wrong result using LIST-EXTENDED
-            if (!$config->get('imap_force_lsub') && $this->get_capability('LIST-EXTENDED')) {
-                // This will also set folder options, LSUB doesn't do that
-                $a_folders = $this->conn->listMailboxes($root, $name,
-                    NULL, array('SUBSCRIBED'));
 
-                // unsubscribe non-existent folders, remove from the list
-                // we can do this only when LIST response is available
-                if (is_array($a_folders) && $name == '*' && !empty($this->conn->data['LIST'])) {
-                    foreach ($a_folders as $idx => $folder) {
-                        if (($opts = $this->conn->data['LIST'][$folder])
-                            && in_array('\\NonExistent', $opts)
-                        ) {
+        $config = rcube::get_instance()->config;
+
+        // Server supports LIST-EXTENDED, we can use selection options
+        // #1486225: Some dovecot versions returns wrong result using LIST-EXTENDED
+        $list_extended = !$config->get('imap_force_lsub') && $this->get_capability('LIST-EXTENDED');
+        if ($list_extended) {
+            // This will also set folder options, LSUB doesn't do that
+            $a_folders = $this->conn->listMailboxes($root, $name,
+                NULL, array('SUBSCRIBED'));
+        }
+        else {
+            // retrieve list of folders from IMAP server using LSUB
+            $a_folders = $this->conn->listSubscribed($root, $name);
+        }
+
+        if (!is_array($a_folders)) {
+            return array();
+        }
+
+        // #1486796: some server configurations doesn't return folders in all namespaces
+        if ($root == '' && $name == '*' && $config->get('imap_force_ns')) {
+            $this->list_folders_update($a_folders, ($list_extended ? 'ext-' : '') . 'subscribed');
+        }
+
+        if ($list_extended) {
+            // unsubscribe non-existent folders, remove from the list
+            // we can do this only when LIST response is available
+            if (is_array($a_folders) && $name == '*' && !empty($this->conn->data['LIST'])) {
+                foreach ($a_folders as $idx => $folder) {
+                    if (($opts = $this->conn->data['LIST'][$folder])
+                        && in_array('\\NonExistent', $opts)
+                    ) {
+                        $this->conn->unsubscribe($folder);
+                        unset($a_folders[$idx]);
+                    }
+                }
+            }
+        }
+        else {
+            // unsubscribe non-existent folders, remove them from the list,
+            // we can do this only when LIST response is available
+            if (is_array($a_folders) && $name == '*' && !empty($this->conn->data['LIST'])) {
+                foreach ($a_folders as $idx => $folder) {
+                    if (!isset($this->conn->data['LIST'][$folder])
+                        || in_array('\\Noselect', $this->conn->data['LIST'][$folder])
+                    ) {
+                        // Some servers returns \Noselect for existing folders
+                        if (!$this->folder_exists($folder)) {
                             $this->conn->unsubscribe($folder);
                             unset($a_folders[$idx]);
                         }
                     }
                 }
             }
-            // retrieve list of folders from IMAP server using LSUB
-            else {
-                $a_folders = $this->conn->listSubscribed($root, $name);
-
-                // unsubscribe non-existent folders, remove them from the list,
-                // we can do this only when LIST response is available
-                if (is_array($a_folders) && $name == '*' && !empty($this->conn->data['LIST'])) {
-                    foreach ($a_folders as $idx => $folder) {
-                        if (!isset($this->conn->data['LIST'][$folder])
-                            || in_array('\\Noselect', $this->conn->data['LIST'][$folder])
-                        ) {
-                            // Some servers returns \Noselect for existing folders
-                            if (!$this->folder_exists($folder)) {
-                                $this->conn->unsubscribe($folder);
-                                unset($a_folders[$idx]);
-                            }
-                        }
-                    }
-                }
-            }
-        }
-
-        if (!is_array($a_folders) || !sizeof($a_folders)) {
-            $a_folders = array();
         }
 
         return $a_folders;
@@ -2608,7 +2666,7 @@
         }
         else {
             // retrieve list of folders from IMAP server
-            $a_mboxes = $this->_list_folders($root, $name);
+            $a_mboxes = $this->list_folders_direct($root, $name);
         }
 
         if (!is_array($a_mboxes)) {
@@ -2643,7 +2701,7 @@
 
 
     /**
-     * protected method for folders listing (LIST)
+     * Method for direct folders listing (LIST)
      *
      * @param   string  $root   Optional root folder
      * @param   string  $name   Optional name pattern
@@ -2651,7 +2709,7 @@
      * @return  array   List of folders
      * @see     rcube_imap::list_folders()
      */
-    protected function _list_folders($root='', $name='*')
+    public function list_folders_direct($root='', $name='*')
     {
         if (!$this->check_connection()) {
             return null;
@@ -2663,50 +2721,71 @@
             return array();
         }
 
-        // #1486796: some server configurations doesn't
-        // return folders in all namespaces, we'll try to detect that situation
-        // and ask for these namespaces separately
-        if ($root == '' && $name == '*') {
-            $delim     = $this->get_hierarchy_delimiter();
-            $namespace = $this->get_namespace();
-            $search    = array();
+        $config = rcube::get_instance()->config;
 
-            // build list of namespace prefixes
-            foreach ((array)$namespace as $ns) {
-                if (is_array($ns)) {
-                    foreach ($ns as $ns_data) {
-                        if (strlen($ns_data[0])) {
-                            $search[] = $ns_data[0];
-                        }
-                    }
-                }
-            }
+        // #1486796: some server configurations doesn't return folders in all namespaces
+        if ($root == '' && $name == '*' && $config->get('imap_force_ns')) {
+            $this->list_folders_update($result);
+        }
 
-            if (!empty($search)) {
-                // go through all folders detecting namespace usage
-                foreach ($result as $folder) {
-                    foreach ($search as $idx => $prefix) {
-                        if (strpos($folder, $prefix) === 0) {
-                            unset($search[$idx]);
-                        }
-                    }
-                    if (empty($search)) {
-                        break;
-                    }
-                }
+        return $result;
+    }
 
-                // get folders in hidden namespaces and add to the result
-                foreach ($search as $prefix) {
-                    $list = $this->conn->listMailboxes($prefix, $name);
 
-                    if (!empty($list)) {
-                        $result = array_merge($result, $list);
+    /**
+     * Fix folders list by adding folders from other namespaces.
+     * Needed on some servers eg. Courier IMAP
+     *
+     * @param array  $result  Reference to folders list
+     * @param string $type    Listing type (ext-subscribed, subscribed or all)
+     */
+    private function list_folders_update(&$result, $type = null)
+    {
+        $delim     = $this->get_hierarchy_delimiter();
+        $namespace = $this->get_namespace();
+        $search    = array();
+
+        // build list of namespace prefixes
+        foreach ((array)$namespace as $ns) {
+            if (is_array($ns)) {
+                foreach ($ns as $ns_data) {
+                    if (strlen($ns_data[0])) {
+                        $search[] = $ns_data[0];
                     }
                 }
             }
         }
 
-        return $result;
+        if (!empty($search)) {
+            // go through all folders detecting namespace usage
+            foreach ($result as $folder) {
+                foreach ($search as $idx => $prefix) {
+                    if (strpos($folder, $prefix) === 0) {
+                        unset($search[$idx]);
+                    }
+                }
+                if (empty($search)) {
+                    break;
+                }
+            }
+
+            // get folders in hidden namespaces and add to the result
+            foreach ($search as $prefix) {
+                if ($type == 'ext-subscribed') {
+                    $list = $this->conn->listMailboxes('', $prefix . '*', null, array('SUBSCRIBED'));
+                }
+                else if ($type == 'subscribed') {
+                    $list = $this->conn->listSubscribed('', $prefix . '*');
+                }
+                else {
+                    $list = $this->conn->listMailboxes('', $prefix . '*');
+                }
+
+                if (!empty($list)) {
+                    $result = array_merge($result, $list);
+                }
+            }
+        }
     }
 
 
@@ -2847,11 +2926,11 @@
 
         // get list of subscribed folders
         if ((strpos($folder, '%') === false) && (strpos($folder, '*') === false)) {
-            $a_subscribed = $this->_list_folders_subscribed('', $folder . $delm . '*');
+            $a_subscribed = $this->list_folders_subscribed('', $folder . $delm . '*');
             $subscribed   = $this->folder_exists($folder, true);
         }
         else {
-            $a_subscribed = $this->_list_folders_subscribed();
+            $a_subscribed = $this->list_folders_subscribed();
             $subscribed   = in_array($folder, $a_subscribed);
         }
 
@@ -2919,7 +2998,7 @@
                 if (strpos($c_mbox, $folder.$delm) === 0) {
                     $this->conn->unsubscribe($c_mbox);
                     if ($this->conn->deleteFolder($c_mbox)) {
-	                    $this->clear_message_cache($c_mbox);
+                        $this->clear_message_cache($c_mbox);
                     }
                 }
             }
@@ -3398,7 +3477,7 @@
             return false;
         }
 
-        $this->clear_cache($this->metadata_cache_key($folder));
+        $this->clear_cache('mailboxes.metadata.', true);
 
         if ($this->get_capability('METADATA') ||
             (!strlen($folder) && $this->get_capability('METADATA-SERVER'))
@@ -3432,9 +3511,9 @@
             return false;
         }
 
-        $this->clear_cache($this->metadata_cache_key($folder));
+        $this->clear_cache('mailboxes.metadata.', true);
 
-        if ($this->get_capability('METADATA') || 
+        if ($this->get_capability('METADATA') ||
             (!strlen($folder) && $this->get_capability('METADATA-SERVER'))
         ) {
             return $this->conn->deleteMetadata($folder, $entries);
@@ -3463,35 +3542,31 @@
      */
     public function get_metadata($folder, $entries, $options=array())
     {
-        if (!$this->check_connection()) {
-            return null;
-        }
-
         $entries = (array)$entries;
 
-        // check cached data
-        $cache_key = $this->metadata_cache_key($folder);
-        $cached_data = (array)$this->get_cache($cache_key);
-        $cached_result = array();
-        $cached_count = 0;
-        foreach ($entries as $entry_key) {
-            if (isset($cached_data[$folder][$entry_key])) {
-                $cached_result[$folder][$entry_key] = $cached_data[$folder][$entry_key];
-                $cached_count++;
-            }
+        // create cache key
+        // @TODO: this is the simplest solution, but we do the same with folders list
+        //        maybe we should store data per-entry and merge on request
+        sort($options);
+        sort($entries);
+        $cache_key = 'mailboxes.metadata.' . $folder;
+        $cache_key .= '.' . md5(serialize($options).serialize($entries));
+
+        // get cached data
+        $cached_data = $this->get_cache($cache_key);
+
+        if (is_array($cached_data)) {
+            return $cached_data;
         }
 
-        // all requested entries are cached
-        if ($cached_count == count($entries)) {
-            return $cached_result;
+        if (!$this->check_connection()) {
+            return null;
         }
 
         if ($this->get_capability('METADATA') ||
             (!strlen($folder) && $this->get_capability('METADATA-SERVER'))
         ) {
             $res = $this->conn->getMetadata($folder, $entries, $options);
-            $this->update_cache($cache_key, array_merge_recursive($cached_data, $res));
-            return $res;
         }
         else if ($this->get_capability('ANNOTATEMORE') || $this->get_capability('ANNOTATEMORE2')) {
             $queries = array();
@@ -3509,22 +3584,14 @@
                     $res = array_merge_recursive($res, $result);
                 }
             }
+        }
 
-            $this->update_cache($cache_key, array_merge_recursive($cached_data, $res));
+        if (isset($res)) {
+            $this->update_cache($cache_key, $res);
             return $res;
         }
 
         return null;
-    }
-
-
-    /**
-     * Helper method to compose the cache key for the given folder metadata
-     */
-    protected function metadata_cache_key($folder)
-    {
-        $suffix = $folder == '' ? '[SERVER]' : (strpos($folder, '*') === false ? $folder : '');
-        return 'mailboxes.metadata.' . $suffix;
     }
 
 
@@ -3954,6 +4021,11 @@
         return $this->list_messages($folder, $page, $sort_field, $sort_order, $slice);
     }
 
+    public function get_headers($uid, $folder = null, $force = false)
+    {
+        return $this->get_message_headers($uid, $folder, $force);
+    }
+
     public function mailbox_status($folder = null)
     {
         return $this->folder_status($folder);

--
Gitblit v1.9.1