From 63d6e6dfc35e6d82c4a64f37c408794c163becd4 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Wed, 28 Sep 2011 15:16:41 -0400
Subject: [PATCH] Bump versions to 0.6 stable

---
 program/include/rcube_imap.php |  494 +++++++++++++++++++++++++++---------------------------
 1 files changed, 247 insertions(+), 247 deletions(-)

diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index 484e70b..379c8c6 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -52,19 +52,23 @@
      * @var rcube_mdb2
      */
     private $db;
+
+    /**
+     * Instance of rcube_cache
+     *
+     * @var rcube_cache
+     */
+    private $cache;
     private $mailbox = 'INBOX';
     private $delimiter = NULL;
     private $namespace = NULL;
     private $sort_field = '';
     private $sort_order = 'DESC';
-    private $caching_enabled = false;
     private $default_charset = 'ISO-8859-1';
     private $struct_charset = NULL;
     private $default_folders = array('INBOX');
+    private $messages_caching = false;
     private $icache = array();
-    private $cache = array();
-    private $cache_keys = array();
-    private $cache_changes = array();
     private $uid_id_map = array();
     private $msg_headers = array();
     public  $search_set = NULL;
@@ -76,19 +80,18 @@
     private $db_header_fields = array('idx', 'uid', 'subject', 'from', 'to', 'cc', 'date', 'size');
     private $options = array('auth_method' => 'check');
     private $host, $user, $pass, $port, $ssl;
+    private $caching = false;
 
     /**
      * All (additional) headers used (in any way) by Roundcube
-     * Not listed here: DATE, FROM, TO, SUBJECT, CONTENT-TYPE, LIST-POST
+     * Not listed here: DATE, FROM, TO, CC, REPLY-TO, SUBJECT, CONTENT-TYPE, LIST-POST
      * (used for messages listing) are hardcoded in rcube_imap_generic::fetchHeaders()
      *
      * @var array
      * @see rcube_imap::fetch_add_headers
      */
     private $all_headers = array(
-        'REPLY-TO',
         'IN-REPLY-TO',
-        'CC',
         'BCC',
         'MESSAGE-ID',
         'CONTENT-TRANSFER-ENCODING',
@@ -112,14 +115,18 @@
 
 
     /**
-     * Object constructor
-     *
-     * @param object DB Database connection
+     * Object constructor.
      */
-    function __construct($db_conn)
+    function __construct()
     {
-        $this->db = $db_conn;
         $this->conn = new rcube_imap_generic();
+
+        // Set namespace and delimiter from session,
+        // so some methods would work before connection
+        if (isset($_SESSION['imap_namespace']))
+            $this->namespace = $_SESSION['imap_namespace'];
+        if (isset($_SESSION['imap_delimiter']))
+            $this->delimiter = $_SESSION['imap_delimiter'];
     }
 
 
@@ -207,7 +214,6 @@
     function close()
     {
         $this->conn->closeConnection();
-        $this->write_cache();
     }
 
 
@@ -412,7 +418,7 @@
     function set_search_set($str=null, $msgs=null, $charset=null, $sort_field=null, $threads=false, $sorted=false)
     {
         if (is_array($str) && $msgs == null)
-            list($str, $msgs, $charset, $sort_field, $threads) = $str;
+            list($str, $msgs, $charset, $sort_field, $threads, $sorted) = $str;
         if ($msgs === false)
             $msgs = array();
         else if ($msgs != null && !is_array($msgs))
@@ -549,12 +555,6 @@
     private function set_env()
     {
         if ($this->delimiter !== null && $this->namespace !== null) {
-            return;
-        }
-
-        if (isset($_SESSION['imap_namespace']) && isset($_SESSION['imap_delimiter'])) {
-            $this->namespace = $_SESSION['imap_namespace'];
-            $this->delimiter = $_SESSION['imap_delimiter'];
             return;
         }
 
@@ -706,7 +706,7 @@
                 $search_str .= " UNSEEN";
             }
             else {
-                if ($this->caching_enabled) {
+                if ($this->messages_caching) {
                     $keys[] = 'ALL';
                 }
                 if ($status) {
@@ -722,7 +722,7 @@
             $count = is_array($index) ? $index['COUNT'] : 0;
 
             if ($mode == 'ALL') {
-                if ($need_uid && $this->caching_enabled) {
+                if ($need_uid && $this->messages_caching) {
                     // Save messages index for check_cache_status()
                     $this->icache['all_undeleted_idx'] = $index['ALL'];
                 }
@@ -838,7 +838,7 @@
         $page         = $page ? $page : $this->list_page;
         $cache_key    = $mailbox.'.msg';
 
-        if ($this->caching_enabled) {
+        if ($this->messages_caching) {
             // cache is OK, we can get messages from local cache
             // (assume cache is in sync when in recursive mode)
             if ($recursive || $this->check_cache_status($mailbox, $cache_key)>0) {
@@ -1320,7 +1320,7 @@
         }
 
         // Update cache
-        if ($this->caching_enabled && $cache_key) {
+        if ($this->messages_caching && $cache_key) {
             // cache is incomplete?
             $cache_index = $this->get_message_cache_index($cache_key);
 
@@ -1505,7 +1505,10 @@
         // use message index sort as default sorting
         if (!$this->sort_field) {
             if ($this->skip_deleted) {
-                $a_index = $this->_search_index($mailbox, 'ALL');
+                $a_index = $this->conn->search($mailbox, 'ALL UNDELETED');
+                // I didn't found that SEARCH should return sorted IDs
+                if (is_array($a_index))
+                    sort($a_index);
             } else if ($max = $this->_messagecount($mailbox)) {
                 $a_index = range(1, $max);
             }
@@ -2104,7 +2107,7 @@
         }
 
         // write structure to cache
-        if ($this->caching_enabled)
+        if ($this->messages_caching)
             $this->add_message_cache($cache_key, $this->_msg_id, $headers, $struct,
                 $this->icache['message.id'][$uid], true);
         }
@@ -2163,7 +2166,7 @@
                     if (strtolower($part[$i][0]) == 'message' && strtolower($part[$i][1]) == 'rfc822') {
                         $mime_part_headers[] = $tmp_part_id;
                     }
-                    else if (in_array('name', (array)$part[$i][2]) && (empty($part[$i][3]) || $part[$i][3]=='NIL')) {
+                    else if (in_array('name', (array)$part[$i][2]) && empty($part[$i][3])) {
                         $mime_part_headers[] = $tmp_part_id;
                     }
                 }
@@ -2231,13 +2234,13 @@
         }
 
         // read content encoding
-        if (!empty($part[5]) && $part[5]!='NIL') {
+        if (!empty($part[5])) {
             $struct->encoding = strtolower($part[5]);
             $struct->headers['content-transfer-encoding'] = $struct->encoding;
         }
 
         // get part size
-        if (!empty($part[6]) && $part[6]!='NIL')
+        if (!empty($part[6]))
             $struct->size = intval($part[6]);
 
         // read part disposition
@@ -2264,7 +2267,7 @@
         }
 
         // get part ID
-        if (!empty($part[3]) && $part[3]!='NIL') {
+        if (!empty($part[3])) {
             $struct->content_id = $part[3];
             $struct->headers['content-id'] = $part[3];
 
@@ -2492,13 +2495,16 @@
         }
 
         // convert charset (if text or message part)
-        if ($body && !$skip_charset_conv &&
-            preg_match('/^(text|message)$/', $o_part->ctype_primary)
-        ) {
-            if (!$o_part->charset || strtoupper($o_part->charset) == 'US-ASCII') {
-                $o_part->charset = $this->default_charset;
+        if ($body && preg_match('/^(text|message)$/', $o_part->ctype_primary)) {
+            // Remove NULL characters (#1486189)
+            $body = str_replace("\x00", '', $body);
+
+           if (!$skip_charset_conv) {
+                if (!$o_part->charset || strtoupper($o_part->charset) == 'US-ASCII') {
+                    $o_part->charset = $this->default_charset;
+                }
+                $body = rcube_charset_convert($body, $o_part->charset);
             }
-            $body = rcube_charset_convert($body, $o_part->charset);
         }
 
         return $body;
@@ -2521,14 +2527,17 @@
 
 
     /**
-     * Returns the whole message source as string
+     * Returns the whole message source as string (or saves to a file)
      *
-     * @param int $uid Message UID
+     * @param int      $uid Message UID
+     * @param resource $fp  File pointer to save the message
+     *
      * @return string Message source string
      */
-    function &get_raw_body($uid)
+    function &get_raw_body($uid, $fp=null)
     {
-        return $this->conn->handlePartBody($this->mailbox, $uid, true);
+        return $this->conn->handlePartBody($this->mailbox, $uid,
+            true, null, null, false, $fp);
     }
 
 
@@ -2581,7 +2590,7 @@
 
         if ($result) {
             // reload message headers if cached
-            if ($this->caching_enabled && !$skip_cache) {
+            if ($this->messages_caching && !$skip_cache) {
                 $cache_key = $mailbox.'.msg';
                 if ($all_mode)
                     $this->clear_message_cache($cache_key);
@@ -3049,10 +3058,12 @@
      */
     private function _list_mailboxes($root='', $name='*', $filter=null)
     {
-        $cache_key = 'mailboxes';
+        $cache_key = $root.':'.$name;
         if (!empty($filter)) {
-            $cache_key .= ':'.substr((is_string($filter) ? $filter : serialize($filter)), 0, 90);
+            $cache_key .= ':'.(is_string($filter) ? $filter : serialize($filter));
         }
+
+        $cache_key = 'mailboxes.'.md5($cache_key);
 
         // get cached folder list
         $a_mboxes = $this->get_cache($cache_key);
@@ -3069,6 +3080,9 @@
         if (isset($data['folders'])) {
             $a_folders = $data['folders'];
         }
+        else if (!$this->conn->connected()) {
+           return array();
+        }
         else {
             // Server supports LIST-EXTENDED, we can use selection options
             $config = rcmail::get_instance()->config;
@@ -3078,20 +3092,36 @@
                 $a_folders = $this->conn->listMailboxes($root, $name,
                     NULL, array('SUBSCRIBED'));
 
-                // remove non-existent folders
-                if (is_array($a_folders)) {
+                // unsubscribe non-existent folders, remove from the list
+                if (is_array($a_folders) && $name == '*') {
                     foreach ($a_folders as $idx => $folder) {
                         if ($this->conn->data['LIST'] && ($opts = $this->conn->data['LIST'][$folder])
                             && in_array('\\NonExistent', $opts)
                         ) {
+                            $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 from the list
+                if (is_array($a_folders) && $name == '*') {
+                    foreach ($a_folders as $idx => $folder) {
+                        if ($this->conn->data['LIST'] && ($opts = $this->conn->data['LIST'][$folder])
+                            && in_array('\\Noselect', $opts)
+                        ) {
+                            // Some servers returns \Noselect for existing folders
+                            if (!$this->mailbox_exists($folder)) {
+                                $this->conn->unsubscribe($folder);
+                                unset($a_folders[$idx]);
+                            }
+                        }
+                    }
+                }
             }
         }
 
@@ -3225,8 +3255,13 @@
         $result = $this->conn->createFolder($mailbox);
 
         // try to subscribe it
-        if ($result && $subscribe)
-            $this->subscribe($mailbox);
+        if ($result) {
+            // clear cache
+            $this->clear_cache('mailboxes', true);
+
+            if ($subscribe)
+                $this->subscribe($mailbox);
+        }
 
         return $result;
     }
@@ -3278,7 +3313,7 @@
 
             // clear cache
             $this->clear_message_cache($mailbox.'.msg');
-            $this->clear_cache('/^mailboxes.*/', true);
+            $this->clear_cache('mailboxes', true);
         }
 
         return $result;
@@ -3320,7 +3355,7 @@
 
             // clear mailbox-related cache
             $this->clear_message_cache($mailbox.'.msg');
-            $this->clear_cache('/^mailboxes.*/', true);
+            $this->clear_cache('mailboxes', true);
         }
 
         return $result;
@@ -3479,6 +3514,87 @@
 
 
     /**
+     * Returns extended information about the folder
+     *
+     * @param string $mailbox Folder name
+     *
+     * @return array Data
+     */
+    function mailbox_info($mailbox)
+    {
+        if ($this->icache['options'] && $this->icache['options']['name'] == $mailbox) {
+            return $this->icache['options'];
+        }
+
+        $acl       = $this->get_capability('ACL');
+        $namespace = $this->get_namespace();
+        $options   = array();
+
+        // check if the folder is a namespace prefix
+        if (!empty($namespace)) {
+            $mbox = $mailbox . $this->delimiter;
+            foreach ($namespace as $ns) {
+                if (!empty($ns)) {
+                    foreach ($ns as $item) {
+                        if ($item[0] === $mbox) {
+                            $options['is_root'] = true;
+                            break 2;
+                        }
+                    }
+                }
+            }
+        }
+        // check if the folder is other user virtual-root
+        if (!$options['is_root'] && !empty($namespace) && !empty($namespace['other'])) {
+            $parts = explode($this->delimiter, $mailbox);
+            if (count($parts) == 2) {
+                $mbox = $parts[0] . $this->delimiter;
+                foreach ($namespace['other'] as $item) {
+                    if ($item[0] === $mbox) {
+                        $options['is_root'] = true;
+                        break;
+                    }
+                }
+            }
+        }
+
+        $options['name']      = $mailbox;
+        $options['options']   = $this->mailbox_options($mailbox, true);
+        $options['namespace'] = $this->mailbox_namespace($mailbox);
+        $options['rights']    = $acl && !$options['is_root'] ? (array)$this->my_rights($mailbox) : array();
+        $options['special']   = in_array($mailbox, $this->default_folders);
+
+        // Set 'noselect' and 'norename' flags
+        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('d', $options['rights']);
+
+            if (!$options['noselect']) {
+                $options['noselect'] = !in_array('r', $options['rights']);
+            }
+        }
+        else {
+            $options['norename'] = $options['is_root'] || $options['namespace'] != 'personal';
+        }
+
+        $this->icache['options'] = $options;
+
+        return $options;
+    }
+
+
+    /**
      * Get message header names for rcube_imap_generic::fetchHeader(s)
      *
      * @return string Space-separated list of header names
@@ -3488,7 +3604,7 @@
         $headers = explode(' ', $this->fetch_add_headers);
         $headers = array_map('strtoupper', $headers);
 
-        if ($this->caching_enabled || $this->get_all_headers)
+        if ($this->messages_caching || $this->get_all_headers)
             $headers = array_merge($headers, $this->all_headers);
 
         return implode(' ', array_unique($headers));
@@ -3617,7 +3733,7 @@
             return $this->conn->setMetadata($mailbox, $entries);
         }
         else if ($this->get_capability('ANNOTATEMORE') || $this->get_capability('ANNOTATEMORE2')) {
-            foreach ($entries as $entry => $value) {
+            foreach ((array)$entries as $entry => $value) {
                 list($ent, $attr) = $this->md2annotate($entry);
                 $entries[$entry] = array($ent, $attr, $value);
             }
@@ -3647,7 +3763,7 @@
             return $this->conn->deleteMetadata($mailbox, $entries);
         }
         else if ($this->get_capability('ANNOTATEMORE') || $this->get_capability('ANNOTATEMORE2')) {
-            foreach ($entries as $idx => $entry) {
+            foreach ((array)$entries as $idx => $entry) {
                 list($ent, $attr) = $this->md2annotate($entry);
                 $entries[$idx] = array($ent, $attr, NULL);
             }
@@ -3673,7 +3789,7 @@
     function get_metadata($mailbox, $entries, $options=array())
     {
         if ($this->get_capability('METADATA') || 
-            !strlen(($mailbox) && $this->get_capability('METADATA-SERVER'))
+            (!strlen($mailbox) && $this->get_capability('METADATA-SERVER'))
         ) {
             return $this->conn->getMetadata($mailbox, $entries, $options);
         }
@@ -3682,7 +3798,7 @@
             $res     = array();
 
             // Convert entry names
-            foreach ($entries as $entry) {
+            foreach ((array)$entries as $entry) {
                 list($ent, $attr) = $this->md2annotate($entry);
                 $queries[$attr][] = $ent;
             }
@@ -3690,7 +3806,7 @@
             // @TODO: Honor MAXSIZE and DEPTH options
             foreach ($queries as $attrib => $entry)
                 if ($result = $this->conn->getAnnotation($mailbox, $entry, $attrib))
-                    $res = array_merge($res, $result);
+                    $res = array_merge_recursive($res, $result);
 
             return $res;
         }
@@ -3703,11 +3819,11 @@
      * Converts the METADATA extension entry name into the correct
      * entry-attrib names for older ANNOTATEMORE version.
      *
-     * @param string Entry name
+     * @param string $entry Entry name
      *
      * @return array Entry-attribute list, NULL if not supported (?)
      */
-    private function md2annotate($name)
+    private function md2annotate($entry)
     {
         if (substr($entry, 0, 7) == '/shared') {
             return array(substr($entry, 7), 'value.shared');
@@ -3726,19 +3842,36 @@
      * --------------------------------*/
 
     /**
-     * Enable or disable caching
+     * Enable or disable indexes caching
      *
-     * @param boolean $set Flag
+     * @param string $type Cache type (@see rcmail::get_cache)
      * @access public
      */
-    function set_caching($set)
+    function set_caching($type)
     {
-        if ($set && is_object($this->db))
-            $this->caching_enabled = true;
-        else
-            $this->caching_enabled = false;
+        if ($type) {
+            $this->caching = $type;
+        }
+        else {
+            if ($this->cache)
+                $this->cache->close();
+            $this->cache = null;
+            $this->caching = false;
+        }
     }
 
+    /**
+     * Getter for IMAP cache object
+     */
+    private function get_cache_engine()
+    {
+        if ($this->caching && !$this->cache) {
+            $rcmail = rcmail::get_instance();
+            $this->cache = $rcmail->get_cache('IMAP', $this->caching);
+        }
+
+        return $this->cache;
+    }
 
     /**
      * Returns cached value
@@ -3749,188 +3882,63 @@
      */
     function get_cache($key)
     {
-        // read cache (if it was not read before)
-        if (!count($this->cache) && $this->caching_enabled) {
-            return $this->_read_cache_record($key);
+        if ($cache = $this->get_cache_engine()) {
+            return $cache->get($key);
         }
-
-        return $this->cache[$key];
     }
-
 
     /**
      * Update cache
      *
      * @param string $key  Cache key
      * @param mixed  $data Data
-     * @access private
+     * @access public
      */
-    private function update_cache($key, $data)
+    function update_cache($key, $data)
     {
-        $this->cache[$key] = $data;
-        $this->cache_changed = true;
-        $this->cache_changes[$key] = true;
-    }
-
-
-    /**
-     * Writes the cache
-     *
-     * @access private
-     */
-    private function write_cache()
-    {
-        if ($this->caching_enabled && $this->cache_changed) {
-            foreach ($this->cache as $key => $data) {
-                if ($this->cache_changes[$key])
-                    $this->_write_cache_record($key, serialize($data));
-            }
+        if ($cache = $this->get_cache_engine()) {
+            $cache->set($key, $data);
         }
     }
-
 
     /**
      * Clears the cache.
      *
-     * @param string  $key          Cache key name or pattern
-     * @param boolean $pattern_mode Enable it to clear all keys with name
-     *                              matching PREG pattern in $key
+     * @param string  $key         Cache key name or pattern
+     * @param boolean $prefix_mode Enable it to clear all keys starting
+     *                             with prefix specified in $key
      * @access public
      */
-    function clear_cache($key=null, $pattern_mode=false)
+    function clear_cache($key=null, $prefix_mode=false)
     {
-        if (!$this->caching_enabled)
-            return;
-
-        if ($key === null) {
-            foreach (array_keys($this->cache) as $key)
-                $this->_clear_cache_record($key);
-
-            $this->cache = array();
-            $this->cache_changed = false;
-            $this->cache_changes = array();
-        }
-        else if ($pattern_mode) {
-            foreach (array_keys($this->cache) as $k) {
-                if (preg_match($key, $k)) {
-                    $this->_clear_cache_record($k);
-                    $this->cache_changes[$k] = false;
-                    unset($this->cache[$key]);
-                }
-            }
-            if (!count($this->cache)) {
-                $this->cache_changed = false;
-            }
-        }
-        else {
-            $this->_clear_cache_record($key);
-            $this->cache_changes[$key] = false;
-            unset($this->cache[$key]);
+        if ($cache = $this->get_cache_engine()) {
+            $cache->remove($key, $prefix_mode);
         }
     }
-
-
-    /**
-     * Returns cached entry
-     *
-     * @param string $key Cache key
-     * @return mixed Cached value
-     * @access private
-     */
-    private function _read_cache_record($key)
-    {
-        if ($this->db) {
-            // get cached data from DB
-            $sql_result = $this->db->query(
-                "SELECT cache_id, data, cache_key ".
-                "FROM ".get_table_name('cache').
-                " WHERE user_id=? ".
-	            "AND cache_key LIKE 'IMAP.%'",
-                $_SESSION['user_id']);
-
-            while ($sql_arr = $this->db->fetch_assoc($sql_result)) {
-	            $sql_key = preg_replace('/^IMAP\./', '', $sql_arr['cache_key']);
-                $this->cache_keys[$sql_key] = $sql_arr['cache_id'];
-	            if (!isset($this->cache[$sql_key]))
-	                $this->cache[$sql_key] = $sql_arr['data'] ? unserialize($sql_arr['data']) : false;
-            }
-        }
-
-        return $this->cache[$key];
-    }
-
-
-    /**
-     * Writes single cache record
-     *
-     * @param string $key  Cache key
-     * @param mxied  $data Cache value
-     * @access private
-     */
-    private function _write_cache_record($key, $data)
-    {
-        if (!$this->db)
-            return false;
-
-        // update existing cache record
-        if ($this->cache_keys[$key]) {
-            $this->db->query(
-                "UPDATE ".get_table_name('cache').
-                " SET created=". $this->db->now().", data=? ".
-                "WHERE user_id=? ".
-                "AND cache_key=?",
-                $data,
-                $_SESSION['user_id'],
-                'IMAP.'.$key);
-        }
-        // add new cache record
-        else {
-            $this->db->query(
-                "INSERT INTO ".get_table_name('cache').
-                " (created, user_id, cache_key, data) ".
-                "VALUES (".$this->db->now().", ?, ?, ?)",
-                $_SESSION['user_id'],
-                'IMAP.'.$key,
-                $data);
-
-            // get cache entry ID for this key
-            $sql_result = $this->db->query(
-                "SELECT cache_id ".
-                "FROM ".get_table_name('cache').
-                " WHERE user_id=? ".
-                "AND cache_key=?",
-                $_SESSION['user_id'],
-                'IMAP.'.$key);
-
-            if ($sql_arr = $this->db->fetch_assoc($sql_result))
-                $this->cache_keys[$key] = $sql_arr['cache_id'];
-        }
-    }
-
-
-    /**
-     * Clears cache for single record
-     *
-     * @param string $ket Cache key
-     * @access private
-     */
-    private function _clear_cache_record($key)
-    {
-        $this->db->query(
-            "DELETE FROM ".get_table_name('cache').
-            " WHERE user_id=? ".
-            "AND cache_key=?",
-            $_SESSION['user_id'],
-            'IMAP.'.$key);
-
-        unset($this->cache_keys[$key]);
-    }
-
 
 
     /* --------------------------------
      *   message caching methods
      * --------------------------------*/
+
+    /**
+     * Enable or disable messages caching
+     *
+     * @param boolean $set Flag
+     * @access public
+     */
+    function set_messages_caching($set)
+    {
+        $rcmail = rcmail::get_instance();
+
+        if ($set && ($dbh = $rcmail->get_dbh())) {
+            $this->db = $dbh;
+            $this->messages_caching = true;
+        }
+        else {
+            $this->messages_caching = false;
+        }
+    }
 
     /**
      * Checks if the cache is up-to-date
@@ -3941,7 +3949,7 @@
      */
     private function check_cache_status($mailbox, $cache_key)
     {
-        if (!$this->caching_enabled)
+        if (!$this->messages_caching)
             return -3;
 
         $cache_index = $this->get_message_cache_index($cache_key);
@@ -3999,7 +4007,7 @@
      */
     private function get_message_cache($key, $from, $to, $sort_field, $sort_order)
     {
-        if (!$this->caching_enabled)
+        if (!$this->messages_caching)
             return NULL;
 
         // use idx sort as default sorting
@@ -4044,7 +4052,7 @@
     {
         $internal_key = 'message';
 
-        if ($this->caching_enabled && !isset($this->icache[$internal_key][$uid])) {
+        if ($this->messages_caching && !isset($this->icache[$internal_key][$uid])) {
             $sql_result = $this->db->query(
                 "SELECT idx, headers, structure, message_id".
                 " FROM ".get_table_name('messages').
@@ -4078,7 +4086,7 @@
      */
     private function get_message_cache_index($key, $sort_field='idx', $sort_order='ASC')
     {
-        if (!$this->caching_enabled || empty($key))
+        if (!$this->messages_caching || empty($key))
             return NULL;
 
         // use idx sort as default
@@ -4133,7 +4141,7 @@
         }
 
         // no further caching
-        if (!$this->caching_enabled)
+        if (!$this->messages_caching)
             return;
 
         // known message id
@@ -4200,7 +4208,7 @@
      */
     private function remove_message_cache($key, $ids, $idx=false)
     {
-        if (!$this->caching_enabled)
+        if (!$this->messages_caching)
             return;
 
         $this->db->query(
@@ -4222,7 +4230,7 @@
      */
     private function clear_message_cache($key, $start_index=1)
     {
-        if (!$this->caching_enabled)
+        if (!$this->messages_caching)
             return;
 
         $this->db->query(
@@ -4241,7 +4249,7 @@
      */
     private function get_message_cache_index_min($key, $uids=NULL)
     {
-        if (!$this->caching_enabled)
+        if (!$this->messages_caching)
             return;
 
         if (!empty($uids) && !is_array($uids)) {
@@ -4275,7 +4283,7 @@
      */
     private function get_cache_id2uid($key, $id)
     {
-        if (!$this->caching_enabled)
+        if (!$this->messages_caching)
             return null;
 
         if (array_key_exists('index', $this->icache)
@@ -4307,7 +4315,7 @@
      */
     private function get_cache_uid2id($key, $uid)
     {
-        if (!$this->caching_enabled)
+        if (!$this->messages_caching)
             return null;
 
         if (array_key_exists('index', $this->icache)
@@ -4693,20 +4701,9 @@
                     $updated = $this->conn->unsubscribe($mailbox);
             }
 
-        // get cached mailbox list
+        // clear cached mailbox list(s)
         if ($updated) {
-            $a_mailbox_cache = $this->get_cache('mailboxes');
-            if (!is_array($a_mailbox_cache))
-                return $updated;
-
-            // modify cached list
-            if ($mode == 'subscribe')
-                $a_mailbox_cache = array_merge($a_mailbox_cache, $a_mboxes);
-            else if ($mode == 'unsubscribe')
-                $a_mailbox_cache = array_diff($a_mailbox_cache, $a_mboxes);
-
-            // write mailboxlist to cache
-            $this->update_cache('mailboxes', $this->_sort_mailbox_list($a_mailbox_cache));
+            $this->clear_cache('mailboxes', true);
         }
 
         return $updated;
@@ -4795,16 +4792,19 @@
         $str = self::explode_header_string(',;', $str, true);
         $result = array();
 
+        // simplified regexp, supporting quoted local part
+        $email_rx = '(\S+|("\s*(?:[^"\f\n\r\t\v\b\s]+\s*)+"))@\S+';
+
         foreach ($str as $key => $val) {
             $name    = '';
             $address = '';
             $val     = trim($val);
 
-            if (preg_match('/(.*)<(\S+@\S+)>$/', $val, $m)) {
+            if (preg_match('/(.*)<('.$email_rx.')>$/', $val, $m)) {
                 $address = $m[2];
                 $name    = trim($m[1]);
             }
-            else if (preg_match('/^(\S+@\S+)$/', $val, $m)) {
+            else if (preg_match('/^('.$email_rx.')$/', $val, $m)) {
                 $address = $m[1];
                 $name    = '';
             }
@@ -4814,7 +4814,7 @@
 
             // dequote and/or decode name
             if ($name) {
-                if ($name[0] == '"') {
+                if ($name[0] == '"' && $name[strlen($name)-1] == '"') {
                     $name = substr($name, 1, -1);
                     $name = stripslashes($name);
                 }

--
Gitblit v1.9.1