Aleksander Machniak
2012-06-28 b866a27364a66c436156732553b1fa638e046b8b
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)) {
@@ -2539,11 +2590,8 @@
            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
        // @TODO: make this optional
        if ($root == '' && $name == '*') {
        // #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');
        }
@@ -2673,11 +2721,10 @@
            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
        // @TODO: make this optional
        if ($root == '' && $name == '*') {
        $config = rcube::get_instance()->config;
        // #1486796: some server configurations doesn't return folders in all namespaces
        if ($root == '' && $name == '*' && $config->get('imap_force_ns')) {
            $this->list_folders_update($result);
        }
@@ -2879,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);
        }
@@ -3182,8 +3229,9 @@
        $cache_key = 'mailboxes.folder-info.' . $folder;
        $cached = $this->get_cache($cache_key);
        if (is_array($cached))
        if (is_array($cached)) {
            return $cached;
        }
        $acl       = $this->get_capability('ACL');
        $namespace = $this->get_namespace();
@@ -3220,10 +3268,9 @@
        $options['name']       = $folder;
        $options['attributes'] = $this->folder_attributes($folder, true);
        $options['namespace']  = $this->folder_namespace($folder);
        $options['rights']     = $acl && !$options['is_root'] ? (array)$this->my_rights($folder) : array();
        $options['special']    = in_array($folder, $this->default_folders);
        // Set 'noselect' and 'norename' flags
        // Set 'noselect' flag
        if (is_array($options['attributes'])) {
            foreach ($options['attributes'] as $attrib) {
                $attrib = strtolower($attrib);
@@ -3236,6 +3283,15 @@
            $options['noselect'] = true;
        }
        // Get folder rights (MYRIGHTS)
        if ($acl && !$options['noselect']) {
            // skip shared roots
            if (!$options['is_root'] || $options['namespace'] == 'personal') {
                $options['rights'] =  (array)$this->my_rights($folder);
            }
        }
        // Set 'norename' flag
        if (!empty($options['rights'])) {
            $options['norename'] = !in_array('x', $options['rights']) && !in_array('d', $options['rights']);
@@ -3430,7 +3486,7 @@
            return false;
        }
        $this->clear_cache('mailboxes.metadata.' . $folder);
        $this->clear_cache('mailboxes.metadata.', true);
        if ($this->get_capability('METADATA') ||
            (!strlen($folder) && $this->get_capability('METADATA-SERVER'))
@@ -3464,9 +3520,9 @@
            return false;
        }
        $this->clear_cache('mailboxes.metadata.' . $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);
@@ -3495,27 +3551,38 @@
     */
    public function get_metadata($folder, $entries, $options=array())
    {
        $entries = (array)$entries;
        // 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;
        }
        if (!$this->check_connection()) {
            return null;
        }
        $cache_key = 'mailboxes.metadata.' . $folder;
        if ($cached = $this->get_cache($cache_key))
            return $cached;
        if ($this->get_capability('METADATA') ||
            (!strlen($folder) && $this->get_capability('METADATA-SERVER'))
        ) {
            $res = $this->conn->getMetadata($folder, $entries, $options);
            $this->update_cache($cache_key, $res);
            return $res;
        }
        else if ($this->get_capability('ANNOTATEMORE') || $this->get_capability('ANNOTATEMORE2')) {
            $queries = array();
            $res     = array();
            // Convert entry names
            foreach ((array)$entries as $entry) {
            foreach ($entries as $entry) {
                list($ent, $attr) = $this->md2annotate($entry);
                $queries[$attr][] = $ent;
            }
@@ -3526,7 +3593,9 @@
                    $res = array_merge_recursive($res, $result);
                }
            }
        }
        if (isset($res)) {
            $this->update_cache($cache_key, $res);
            return $res;
        }
@@ -3682,9 +3751,9 @@
    {
        if ($this->messages_caching && !$this->mcache) {
            $rcube = rcube::get_instance();
            if ($dbh = $rcube->get_dbh()) {
            if (($dbh = $rcube->get_dbh()) && ($userid = $rcube->get_user_id())) {
                $this->mcache = new rcube_imap_cache(
                    $dbh, $this, $rcube->get_user_id(), $this->options['skip_deleted']);
                    $dbh, $this, $userid, $this->options['skip_deleted']);
            }
        }
@@ -3961,6 +4030,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);