alecpl
2010-05-13 bb8012cfcde4ee598c2281373252a84ce0c96cc2
program/include/rcube_imap.php
@@ -26,7 +26,8 @@
 *
 * @package    Mail
 * @author     Thomas Bruederli <roundcube@gmail.com>
 * @version    1.6
 * @author     Aleksander Machniak <alec@alec.pl>
 * @version    2.0
 */
class rcube_imap
{
@@ -91,7 +92,7 @@
     */
    function connect($host, $user, $pass, $port=143, $use_ssl=null)
    {
        // check for Open-SSL support in PHP build
        // check for OpenSSL support in PHP build
        if ($use_ssl && extension_loaded('openssl'))
            $this->options['ssl_mode'] = $use_ssl == 'imaps' ? 'ssl' : $use_ssl;
        else if ($use_ssl) {
@@ -411,14 +412,15 @@
     * @param  string  Mailbox/folder name
     * @param  string  Mode for count [ALL|THREADS|UNSEEN|RECENT]
     * @param  boolean Force reading from server and update cache
     * @param  boolean Enables MAXUIDs checking
     * @param  boolean Enables storing folder status info (max UID/count),
     *                 required for mailbox_status()
     * @return int     Number of messages
     * @access public
     */
    function messagecount($mbox_name='', $mode='ALL', $force=false, $maxuid=true)
    function messagecount($mbox_name='', $mode='ALL', $force=false, $status=true)
    {
        $mailbox = $mbox_name ? $this->mod_mailbox($mbox_name) : $this->mailbox;
        return $this->_messagecount($mailbox, $mode, $force, $maxuid);
        return $this->_messagecount($mailbox, $mode, $force, $status);
    }
@@ -428,7 +430,7 @@
     * @access  private
     * @see     rcube_imap::messagecount()
     */
    private function _messagecount($mailbox='', $mode='ALL', $force=false, $maxuid=true)
    private function _messagecount($mailbox='', $mode='ALL', $force=false, $status=true)
    {
        $mode = strtoupper($mode);
@@ -454,8 +456,10 @@
        if ($mode == 'THREADS') {
            $count = $this->_threadcount($mailbox, $msg_count);
            if ($maxuid)
                $_SESSION['maxuid'][$mailbox] = $msg_count ? $this->_id2uid($msg_count, $mailbox) : 0;
            if ($status) {
                $this->set_folder_stats($mailbox, 'cnt', $msg_count);
                $this->set_folder_stats($mailbox, 'maxuid', $msg_count ? $this->_id2uid($msg_count, $mailbox) : 0);
            }
        }
        // RECENT count is fetched a bit different
        else if ($mode == 'RECENT') {
@@ -479,16 +483,20 @@
      
            $count = is_array($index) ? count($index) : 0;
            if ($mode == 'ALL' && $maxuid)
                $_SESSION['maxuid'][$mailbox] = $index ? $this->_id2uid(max($index), $mailbox) : 0;
            if ($mode == 'ALL' && $status) {
                $this->set_folder_stats($mailbox, 'cnt', $count);
                $this->set_folder_stats($mailbox, 'maxuid', $index ? $this->_id2uid(max($index), $mailbox) : 0);
            }
        }
        else {
            if ($mode == 'UNSEEN')
                $count = $this->conn->countUnseen($mailbox);
            else {
                $count = $this->conn->countMessages($mailbox);
                if ($maxuid)
                    $_SESSION['maxuid'][$mailbox] = $count ? $this->_id2uid($count, $mailbox) : 0;
                if ($status) {
                    $this->set_folder_stats($mailbox,'cnt', $count);
                    $this->set_folder_stats($mailbox, 'maxuid', $count ? $this->_id2uid($count, $mailbox) : 0);
                }
            }
        }
@@ -1032,32 +1040,70 @@
        return count($a_msg_headers);
    }
  
    /**
     * Fetches IDS of pseudo recent messages.
     * Returns current status of mailbox
     *
     * We compare the maximum UID to determine the number of
     * new messages because the RECENT flag is not reliable.
     *
     * @param string  Mailbox/folder name
     * @return array  List of recent message UIDs
     * @param string Mailbox/folder name
     * @return int   Folder status
     */
    function recent_uids($mbox_name = null, $nofetch = false)
    function mailbox_status($mbox_name = null)
    {
        $mailbox = $mbox_name ? $this->mod_mailbox($mbox_name) : $this->mailbox;
        $old_maxuid = intval($_SESSION['maxuid'][$mailbox]);
        // refresh message count -> will update $_SESSION['maxuid'][$mailbox]
        $old = $this->get_folder_stats($mailbox);
        // refresh message count -> will update
        $this->_messagecount($mailbox, 'ALL', true);
        if ($_SESSION['maxuid'][$mailbox] > $old_maxuid) {
            $maxuid = max(1, $old_maxuid+1);
            return array_values((array)$this->conn->fetchHeaderIndex(
                $mailbox, "$maxuid:*", 'UID', $this->skip_deleted, true));
        }
        return array();
        $result = 0;
        $new = $this->get_folder_stats($mailbox);
        // got new messages
        if ($new['maxuid'] > $old['maxuid'])
            $result += 1;
        // some messages has been deleted
        if ($new['cnt'] < $old['cnt'])
            $result += 2;
        // @TODO: optional checking for messages flags changes (?)
        // @TODO: UIDVALIDITY checking
        return $result;
    }
    /**
     * Stores folder statistic data in session
     * @TODO: move to separate DB table (cache?)
     *
     * @param string Mailbox name
     * @param string Data name
     * @param mixed  Data value
     */
    private function set_folder_stats($mbox_name, $name, $data)
    {
        $_SESSION['folders'][$mbox_name][$name] = $data;
    }
    /**
     * Gets folder statistic data
     *
     * @param string Mailbox name
     * @return array Stats data
     */
    private function get_folder_stats($mbox_name)
    {
        if ($_SESSION['folders'][$mbox_name])
            return (array) $_SESSION['folders'][$mbox_name];
        else
            return array();
    }
    /**
     * Return sorted array of message IDs (not UIDs)
     *
@@ -1537,9 +1583,8 @@
    {
        if (!empty($this->search_string))
            $this->search_set = $this->search('', $this->search_string, $this->search_charset,
               $this->search_sort_field, $this->search_threads);
       $this->search_sort_field, $this->search_threads);
        return $this->get_search_set();
    }
  
@@ -1588,7 +1633,7 @@
            if ($headers->uid && $headers->id)
                $this->uid_id_map[$mailbox][$headers->uid] = $headers->id;
            $this->add_message_cache($mailbox.'.msg', $headers->id, $headers, NULL, true);
            $this->add_message_cache($mailbox.'.msg', $headers->id, $headers, NULL);
        }
        return $headers;
@@ -2481,10 +2526,10 @@
        $a_out = array();
        $a_mboxes = $this->_list_mailboxes($root, $filter);
        foreach ($a_mboxes as $mbox_row) {
            $name = $this->mod_mailbox($mbox_row, 'out');
            if (strlen($name))
        foreach ($a_mboxes as $idx => $mbox_row) {
            if ($name = $this->mod_mailbox($mbox_row, 'out'))
                $a_out[] = $name;
            unset($a_mboxes[$idx]);
        }
        // INBOX should always be available
@@ -2553,10 +2598,15 @@
        $a_mboxes = $this->conn->listMailboxes($this->mod_mailbox($root), '*');
        // modify names with root dir
        foreach ($a_mboxes as $mbox_name) {
        foreach ($a_mboxes as $idx => $mbox_name) {
            if ($name = $this->mod_mailbox($mbox_name, 'out'))
                $a_folders[] = $name;
            unset($a_mboxes[$idx]);
        }
        // INBOX should always be available
        if (!in_array('INBOX', $a_folders))
            array_unshift($a_folders, 'INBOX');
        // filter folders and sort them
        $a_folders = $this->_sort_mailbox_list($a_folders);
@@ -2707,7 +2757,7 @@
            foreach ($a_mboxes as $mbox_name) {
                $mailbox = $this->mod_mailbox($mbox_name);
                $sub_mboxes = $this->conn->listMailboxes($this->mod_mailbox(''),
               $mbox_name . $this->delimiter . '*');
                   $mbox_name . $this->delimiter . '*');
                // unsubscribe mailbox before deleting
                $this->conn->unsubscribe($mailbox);
@@ -2718,7 +2768,7 @@
                    $deleted = true;
                    $this->clear_message_cache($mailbox.'.msg');
               }
                foreach ($sub_mboxes as $c_mbox) {
                    if ($c_mbox != 'INBOX') {
                        $this->conn->unsubscribe($c_mbox);
@@ -2768,14 +2818,19 @@
            if ($mbox_name == 'INBOX')
                return true;
            $key = $subscription ? 'subscribed' : 'existing';
            if (is_array($this->icache[$key]) && in_array($mbox_name, $this->icache[$key]))
                return true;
            if ($subscription) {
                if ($a_folders = $this->conn->listSubscribed($this->mod_mailbox(''), $mbox_name))
                    return true;
                $a_folders = $this->conn->listSubscribed($this->mod_mailbox(''), $mbox_name);
            }
            else {
                $a_folders = $this->conn->listMailboxes($this->mod_mailbox(''), $mbox_mbox);
           if (is_array($a_folders) && in_array($this->mod_mailbox($mbox_name), $a_folders))
                $a_folders = $this->conn->listMailboxes($this->mod_mailbox(''), $mbox_name);
           }
            if (is_array($a_folders) && in_array($this->mod_mailbox($mbox_name), $a_folders)) {
                $this->icache[$key][] = $mbox_name;
                return true;
            }
        }
@@ -3149,7 +3204,7 @@
        if (!$this->caching_enabled)
            return;
    
        // check for an existing record (probly headers are cached but structure not)
        // check for an existing record (probably headers are cached but structure not)
        if (!$force) {
            $sql_result = $this->db->query(
                "SELECT message_id".