Aleksander Machniak
2015-02-02 2646758314b09c46aecd028a45be4c6e5ca4251b
program/lib/Roundcube/rcube_imap.php
@@ -70,7 +70,7 @@
    protected $search_sort_field = '';
    protected $search_threads = false;
    protected $search_sorted = false;
    protected $options = array('auth_method' => 'check');
    protected $options = array('auth_type' => 'check');
    protected $caching = false;
    protected $messages_caching = false;
    protected $threading = false;
@@ -110,13 +110,13 @@
    /**
     * Connect to an IMAP server
     *
     * @param  string   $host    Host to connect
     * @param  string   $user    Username for IMAP account
     * @param  string   $pass    Password for IMAP account
     * @param  integer  $port    Port to connect to
     * @param  string   $use_ssl SSL schema (either ssl or tls) or null if plain connection
     * @param string  $host    Host to connect
     * @param string  $user    Username for IMAP account
     * @param string  $pass    Password for IMAP account
     * @param integer $port    Port to connect to
     * @param string  $use_ssl SSL schema (either ssl or tls) or null if plain connection
     *
     * @return boolean  TRUE on success, FALSE on failure
     * @return boolean True on success, False on failure
     */
    public function connect($host, $user, $pass, $port=143, $use_ssl=null)
    {
@@ -391,10 +391,10 @@
    public function check_permflag($flag)
    {
        $flag       = strtoupper($flag);
        $imap_flag  = $this->conn->flags[$flag];
        $perm_flags = $this->get_permflags($this->folder);
        $imap_flag  = $this->conn->flags[$flag];
        return in_array_nocase($imap_flag, $perm_flags);
        return $imap_flag && !empty($perm_flags) && in_array_nocase($imap_flag, $perm_flags);
    }
@@ -410,17 +410,7 @@
        if (!strlen($folder)) {
            return array();
        }
/*
        Checking PERMANENTFLAGS is rather rare, so we disable caching of it
        Re-think when we'll use it for more than only MDNSENT flag
        $cache_key = 'mailboxes.permanentflags.' . $folder;
        $permflags = $this->get_cache($cache_key);
        if ($permflags !== null) {
            return explode(' ', $permflags);
        }
*/
        if (!$this->check_connection()) {
            return array();
        }
@@ -435,10 +425,7 @@
        if (!is_array($permflags)) {
            $permflags = array();
        }
/*
        // Store permflags as string to limit cached object size
        $this->update_cache($cache_key, implode(' ', $permflags));
*/
        return $permflags;
    }
@@ -689,6 +676,41 @@
        $this->update_cache('messagecount', $a_folder_cache);
        return (int)$count;
    }
    /**
     * Public method for listing message flags
     *
     * @param string $folder  Folder name
     * @param array  $uids    Message UIDs
     * @param int    $mod_seq Optional MODSEQ value (of last flag update)
     *
     * @return array Indexed array with message flags
     */
    public function list_flags($folder, $uids, $mod_seq = null)
    {
        if (!strlen($folder)) {
            $folder = $this->folder;
        }
        if (!$this->check_connection()) {
            return array();
        }
        // @TODO: when cache was synchronized in this request
        // we might already have asked for flag updates, use it.
        $flags  = $this->conn->fetch($folder, $uids, true, array('FLAGS'), $mod_seq);
        $result = array();
        if (!empty($flags)) {
            foreach ($flags as $message) {
                $result[$message->uid] = $message->flags;
            }
        }
        return $result;
    }
@@ -1269,7 +1291,7 @@
    public function index_direct($folder, $sort_field = null, $sort_order = null, $search = null)
    {
        if (!empty($search)) {
            $search = $this->search_set->get_compressed();
            $search = $search->get_compressed();
        }
        // use message index sort as default sorting
@@ -1422,7 +1444,7 @@
    public function search_once($folder = null, $str = 'ALL')
    {
        if (!$str) {
            return 'ALL';
            $str = 'ALL';
        }
        if (!strlen($folder)) {
@@ -1657,7 +1679,7 @@
            $this->struct_charset = $this->structure_charset($structure);
        }
        $headers->ctype = strtolower($headers->ctype);
        $headers->ctype = @strtolower($headers->ctype);
        // Here we can recognize malformed BODYSTRUCTURE and
        // 1. [@TODO] parse the message in other way to create our own message structure
@@ -2134,7 +2156,7 @@
        // convert charset (if text or message part)
        if ($body && preg_match('/^(text|message)$/', $o_part->ctype_primary)) {
            // Remove NULL characters if any (#1486189)
            if (strpos($body, "\x00") !== false) {
            if ($formatted && strpos($body, "\x00") !== false) {
                $body = str_replace("\x00", '', $body);
            }
@@ -2856,12 +2878,21 @@
    /**
     * Filter the given list of folders according to access rights
     *
     * For performance reasons we assume user has full rights
     * on all personal folders.
     */
    protected function filter_rights($a_folders, $rights)
    {
        $regex = '/('.$rights.')/';
        foreach ($a_folders as $idx => $folder) {
            if ($this->folder_namespace($folder) == 'personal') {
                continue;
            }
            $myrights = join('', (array)$this->my_rights($folder));
            if ($myrights !== null && !preg_match($regex, $myrights)) {
                unset($a_folders[$idx]);
            }
@@ -3650,8 +3681,16 @@
            // @TODO: Honor MAXSIZE and DEPTH options
            foreach ($queries as $attrib => $entry) {
                if ($result = $this->conn->getAnnotation($folder, $entry, $attrib)) {
                    $res = array_merge_recursive($res, $result);
                $result = $this->conn->getAnnotation($folder, $entry, $attrib);
                // an error, invalidate any previous getAnnotation() results
                if (!is_array($result)) {
                    return null;
                }
                else {
                    foreach ($result as $fldr => $data) {
                        $res[$fldr] = array_merge((array) $res[$fldr], $data);
                    }
                }
            }
        }
@@ -3773,12 +3812,17 @@
    /**
     * Enable or disable messages caching
     *
     * @param boolean $set Flag
     * @param boolean $set  Flag
     * @param int     $mode Cache mode
     */
    public function set_messages_caching($set)
    public function set_messages_caching($set, $mode = null)
    {
        if ($set) {
            $this->messages_caching = true;
            if ($mode && ($cache = $this->get_mcache_engine())) {
                $cache->set_mode($mode);
            }
        }
        else {
            if ($this->mcache) {
@@ -3798,9 +3842,10 @@
        if ($this->messages_caching && !$this->mcache) {
            $rcube = rcube::get_instance();
            if (($dbh = $rcube->get_dbh()) && ($userid = $rcube->get_user_id())) {
                $ttl = $rcube->config->get('messages_cache_ttl', '10d');
                $ttl       = $rcube->config->get('messages_cache_ttl', '10d');
                $threshold = $rcube->config->get('messages_cache_threshold', 50);
                $this->mcache = new rcube_imap_cache(
                    $dbh, $this, $userid, $this->options['skip_deleted'], $ttl);
                    $dbh, $this, $userid, $this->options['skip_deleted'], $ttl, $threshold);
            }
        }
@@ -3812,7 +3857,7 @@
     * Clears the messages cache.
     *
     * @param string $folder Folder name
     * @param array  $uids    Optional message UIDs to remove from cache
     * @param array  $uids   Optional message UIDs to remove from cache
     */
    protected function clear_message_cache($folder = null, $uids = null)
    {
@@ -3855,9 +3900,12 @@
    /**
     * Sort folders first by default folders and then in alphabethical order
     *
     * @param array $a_folders Folders list
     * @param array $a_folders    Folders list
     * @param bool  $skip_default Skip default folders handling
     *
     * @return array Sorted list
     */
    protected function sort_folder_list($a_folders)
    public function sort_folder_list($a_folders, $skip_default = false)
    {
        $a_out = $a_defaults = $folders = array();
@@ -3869,7 +3917,7 @@
                continue;
            }
            if (($p = array_search($folder, $this->default_folders)) !== false && !$a_defaults[$p]) {
            if (!$skip_default && ($p = array_search($folder, $this->default_folders)) !== false && !$a_defaults[$p]) {
                $a_defaults[$p] = $folder;
            }
            else {