Thomas Bruederli
2013-10-15 b6e24c6946606cd504d522451c36b6dc574fe75d
program/lib/Roundcube/rcube_imap.php
@@ -680,6 +680,41 @@
    /**
     * 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;
    }
    /**
     * Public method for listing headers
     *
     * @param   string   $folder     Folder name
@@ -910,6 +945,50 @@
            return array();
        }
        // gather messages from a multi-folder search
        if ($this->search_set->multi) {
            $page_size = $this->page_size;
            $sort_field = $this->sort_field;
            $search_set = $this->search_set;
            $this->sort_field = null;
            $this->page_size = 100;  // limit to 100 messages per folder
            $a_msg_headers = array();
            foreach ($search_set->sets as $resultset) {
                if (!$resultset->is_empty()) {
                    $this->search_set = $resultset;
                    $this->search_threads = $resultset instanceof rcube_result_thread;
                    $a_msg_headers = array_merge($a_msg_headers, $this->list_search_messages($resultset->get_parameters('MAILBOX'), 1));
                }
            }
            // do sorting and paging
            $cnt   = $search_set->count();
            $from  = ($page-1) * $page_size;
            $to    = $from + $page_size;
            // sort headers
            if (!$this->threading && !empty($a_msg_headers)) {
                $a_msg_headers = $this->conn->sortHeaders($a_msg_headers, $this->sort_field, $this->sort_order);
            }
            // only return the requested part of the set
            $slice_length  = min($page_size, $cnt - ($to > $cnt ? $from : $to));
            $a_msg_headers = array_slice(array_values($a_msg_headers), $from, $slice_length);
            if ($slice) {
                $a_msg_headers = array_slice($a_msg_headers, -$slice, $slice);
            }
            // restore members
            $this->sort_field = $sort_field;
            $this->page_size = $page_size;
            $this->search_set = $search_set;
            return $a_msg_headers;
        }
        // use saved messages from searching
        if ($this->threading) {
            return $this->list_search_thread_messages($folder, $page, $slice);
@@ -1076,6 +1155,7 @@
        }
        foreach ($headers as $h) {
            $h->folder = $folder;
            $a_msg_headers[$h->uid] = $h;
        }
@@ -1387,11 +1467,31 @@
            $str = 'ALL';
        }
        if (!strlen($folder)) {
        if (empty($folder)) {
            $folder = $this->folder;
        }
        $results = $this->search_index($folder, $str, $charset, $sort_field);
        // multi-folder search
        if (is_array($folder) && count($folder) > 1 && $str != 'ALL') {
            new rcube_result_index; // trigger autoloader and make these classes available for threaded context
            new rcube_result_thread;
            // connect IMAP to have all the required classes and settings loaded
            $this->check_connection();
            $searcher = new rcube_imap_search($this->options, $this->conn);
            $results = $searcher->exec(
                $folder,
                $str,
                $charset ? $charset : $this->default_charset,
                $sort_field && $this->get_capability('SORT') ? $sort_field : null,
                $this->threading
            );
        }
        else {
            $folder = is_array($folder) ? $folder[0] : $folder;
            $results = $this->search_index($folder, $str, $charset, $sort_field);
        }
        $this->set_search_set(array($str, $results, $charset, $sort_field,
            $this->threading || $this->search_sorted ? true : false));
@@ -1409,7 +1509,7 @@
    public function search_once($folder = null, $str = 'ALL')
    {
        if (!$str) {
            return 'ALL';
            $str = 'ALL';
        }
        if (!strlen($folder)) {
@@ -1465,7 +1565,7 @@
            // but I've seen that Courier doesn't support UTF-8)
            if ($threads->is_error() && $charset && $charset != 'US-ASCII') {
                $threads = $this->conn->thread($folder, $this->threading,
                    $this->convert_criteria($criteria, $charset), true, 'US-ASCII');
                    self::convert_criteria($criteria, $charset), true, 'US-ASCII');
            }
            return $threads;
@@ -1479,7 +1579,7 @@
            // but I've seen Courier with disabled UTF-8 support)
            if ($messages->is_error() && $charset && $charset != 'US-ASCII') {
                $messages = $this->conn->sort($folder, $sort_field,
                    $this->convert_criteria($criteria, $charset), true, 'US-ASCII');
                    self::convert_criteria($criteria, $charset), true, 'US-ASCII');
            }
            if (!$messages->is_error()) {
@@ -1494,7 +1594,7 @@
        // Error, try with US-ASCII (some servers may support only US-ASCII)
        if ($messages->is_error() && $charset && $charset != 'US-ASCII') {
            $messages = $this->conn->search($folder,
                $this->convert_criteria($criteria, $charset), true);
                self::convert_criteria($criteria, $charset), true);
        }
        $this->search_sorted = false;
@@ -1512,7 +1612,7 @@
     *
     * @return string  Search string
     */
    protected function convert_criteria($str, $charset, $dest_charset='US-ASCII')
    public static function convert_criteria($str, $charset, $dest_charset='US-ASCII')
    {
        // convert strings to US_ASCII
        if (preg_match_all('/\{([0-9]+)\}\r\n/', $str, $matches, PREG_OFFSET_CAPTURE)) {
@@ -2121,7 +2221,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);
            }
@@ -2374,7 +2474,7 @@
                    $this->refresh_search();
                }
                else {
                    $this->search_set->filter(explode(',', $uids));
                    $this->search_set->filter(explode(',', $uids), $this->folder);
                }
            }
@@ -2843,12 +2943,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]);
            }
@@ -3760,12 +3869,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) {
@@ -3800,7 +3914,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)
    {
@@ -3843,9 +3957,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();
@@ -3857,7 +3974,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 {