| | |
| | | | This file is part of the Roundcube Webmail client | |
| | | | Copyright (C) 2005-2012, The Roundcube Dev Team | |
| | | | Copyright (C) 2011-2012, Kolab Systems AG | |
| | | | Licensed under the GNU GPL | |
| | | | | |
| | | | Licensed under the GNU General Public License version 3 or | |
| | | | any later version with exceptions for skins & plugins. | |
| | | | See the README file for a full license statement. | |
| | | | | |
| | | | PURPOSE: | |
| | | | IMAP Storage Engine | |
| | |
| | | $this->options['ssl_mode'] = $use_ssl == 'imaps' ? 'ssl' : $use_ssl; |
| | | } |
| | | else if ($use_ssl) { |
| | | raise_error(array('code' => 403, 'type' => 'imap', |
| | | rcube::raise_error(array('code' => 403, 'type' => 'imap', |
| | | 'file' => __FILE__, 'line' => __LINE__, |
| | | 'message' => "OpenSSL not available"), true, false); |
| | | $port = 143; |
| | |
| | | |
| | | $attempt = 0; |
| | | do { |
| | | $data = rcmail::get_instance()->plugins->exec_hook('imap_connect', |
| | | $data = rcube::get_instance()->plugins->exec_hook('imap_connect', |
| | | array_merge($this->options, array('host' => $host, 'user' => $user, |
| | | 'attempt' => ++$attempt))); |
| | | |
| | |
| | | else if ($this->conn->error) { |
| | | if ($pass && $user) { |
| | | $message = sprintf("Login failed for %s from %s. %s", |
| | | $user, rcmail_remote_ip(), $this->conn->error); |
| | | $user, rcube_utils::remote_ip(), $this->conn->error); |
| | | |
| | | raise_error(array('code' => 403, 'type' => 'imap', |
| | | rcube::raise_error(array('code' => 403, 'type' => 'imap', |
| | | 'file' => __FILE__, 'line' => __LINE__, |
| | | 'message' => $message), true, false); |
| | | } |
| | |
| | | |
| | | /** |
| | | * Check connection state, connect if not connected. |
| | | * |
| | | * @return bool Connection state. |
| | | */ |
| | | public function check_connection() |
| | | { |
| | |
| | | return; |
| | | } |
| | | |
| | | $config = rcmail::get_instance()->config; |
| | | $config = rcube::get_instance()->config; |
| | | $imap_personal = $config->get('imap_ns_personal'); |
| | | $imap_other = $config->get('imap_ns_other'); |
| | | $imap_shared = $config->get('imap_ns_shared'); |
| | |
| | | $folder = $this->folder; |
| | | } |
| | | |
| | | return $this->messagecount($folder, $mode, $force, $status); |
| | | return $this->countmessages($folder, $mode, $force, $status); |
| | | } |
| | | |
| | | |
| | |
| | | * @return int Number of messages |
| | | * @see rcube_imap::count() |
| | | */ |
| | | protected function messagecount($folder, $mode='ALL', $force=false, $status=true) |
| | | protected function countmessages($folder, $mode='ALL', $force=false, $status=true) |
| | | { |
| | | $mode = strtoupper($mode); |
| | | |
| | | // count search set |
| | | if ($this->search_string && $folder == $this->folder && ($mode == 'ALL' || $mode == 'THREADS') && !$force) { |
| | | // count search set, assume search set is always up-to-date (don't check $force flag) |
| | | if ($this->search_string && $folder == $this->folder && ($mode == 'ALL' || $mode == 'THREADS')) { |
| | | if ($mode == 'ALL') { |
| | | return $this->search_set->count_messages(); |
| | | } |
| | |
| | | * protected method for setting threaded messages flags: |
| | | * depth, has_children and unread_children |
| | | * |
| | | * @param array $headers Reference to headers array indexed by message UID |
| | | * @param rcube_imap_result $threads Threads data object |
| | | * @param array $headers Reference to headers array indexed by message UID |
| | | * @param rcube_result_thread $threads Threads data object |
| | | * |
| | | * @return array Message headers array indexed by message UID |
| | | */ |
| | |
| | | |
| | | if ($sort) { |
| | | // use this class for message sorting |
| | | $sorter = new rcube_header_sorter(); |
| | | $sorter = new rcube_message_header_sorter(); |
| | | $sorter->set_index($msgs); |
| | | $sorter->sort_headers($a_msg_headers); |
| | | } |
| | |
| | | $old = $this->get_folder_stats($folder); |
| | | |
| | | // refresh message count -> will update |
| | | $this->messagecount($folder, 'ALL', true); |
| | | $this->countmessages($folder, 'ALL', true); |
| | | |
| | | $result = 0; |
| | | |
| | |
| | | } |
| | | // use message index sort as default sorting |
| | | else if (!$sort_field) { |
| | | // use search result from count() if possible |
| | | if ($this->options['skip_deleted'] && !empty($this->icache['undeleted_idx']) |
| | | && $this->icache['undeleted_idx']->get_parameters('ALL') !== null |
| | | && $this->icache['undeleted_idx']->get_parameters('MAILBOX') == $folder |
| | | ) { |
| | | $index = $this->icache['undeleted_idx']; |
| | |
| | | * |
| | | * @return rcube_result_index Search result (UIDs) |
| | | */ |
| | | public function search_once($mailbox = null, $str = 'ALL') |
| | | public function search_once($folder = null, $str = 'ALL') |
| | | { |
| | | if (!$str) { |
| | | return 'ALL'; |
| | | } |
| | | |
| | | if (!strlen($mailbox)) { |
| | | $mailbox = $this->mailbox; |
| | | if (!strlen($folder)) { |
| | | $folder = $this->folder; |
| | | } |
| | | |
| | | if (!$this->check_connection()) { |
| | | return new rcube_result_index(); |
| | | } |
| | | |
| | | $index = $this->conn->search($mailbox, $str, true); |
| | | $index = $this->conn->search($folder, $str, true); |
| | | |
| | | return $index; |
| | | } |
| | |
| | | foreach ($matches[1] as $m) { |
| | | $string_offset = $m[1] + strlen($m[0]) + 4; // {}\r\n |
| | | $string = substr($str, $string_offset - 1, $m[0]); |
| | | $string = rcube_charset_convert($string, $charset, $dest_charset); |
| | | $string = rcube_charset::convert($string, $charset, $dest_charset); |
| | | if ($string === false) { |
| | | continue; |
| | | } |
| | |
| | | * @param string $folder Folder to read from |
| | | * @param bool $force True to skip cache |
| | | * |
| | | * @return rcube_mail_header Message headers |
| | | * @return rcube_message_header Message headers |
| | | */ |
| | | public function get_message_headers($uid, $folder = null, $force = false) |
| | | { |
| | |
| | | * @param int $uid Message UID to fetch |
| | | * @param string $folder Folder to read from |
| | | * |
| | | * @return object rcube_mail_header Message data |
| | | * @return object rcube_message_header Message data |
| | | */ |
| | | public function get_message($uid, $folder = null) |
| | | { |
| | |
| | | $charset = $this->struct_charset; |
| | | } |
| | | else { |
| | | $charset = rc_detect_encoding($filename_mime, $this->default_charset); |
| | | $charset = rcube_charset::detect($filename_mime, $this->default_charset); |
| | | } |
| | | |
| | | $part->filename = rcube_mime::decode_mime_string($filename_mime, $charset); |
| | |
| | | $filename_encoded = $fmatches[2]; |
| | | } |
| | | |
| | | $part->filename = rcube_charset_convert(urldecode($filename_encoded), $filename_charset); |
| | | $part->filename = rcube_charset::convert(urldecode($filename_encoded), $filename_charset); |
| | | } |
| | | } |
| | | |
| | |
| | | $o_part->charset = $this->default_charset; |
| | | } |
| | | } |
| | | $body = rcube_charset_convert($body, $o_part->charset); |
| | | $body = rcube_charset::convert($body, $o_part->charset); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | $config = rcmail::get_instance()->config; |
| | | $config = rcube::get_instance()->config; |
| | | $to_trash = $to_mbox == $config->get('trash_mbox'); |
| | | |
| | | // flag messages as read before moving them |
| | |
| | | $a_defaults = $a_out = array(); |
| | | |
| | | // Give plugins a chance to provide a list of folders |
| | | $data = rcmail::get_instance()->plugins->exec_hook('folders_list', |
| | | $data = rcube::get_instance()->plugins->exec_hook('storage_folders', |
| | | array('root' => $root, 'name' => $name, 'filter' => $filter, 'mode' => 'LSUB')); |
| | | |
| | | if (isset($data['folders'])) { |
| | |
| | | } |
| | | else { |
| | | // Server supports LIST-EXTENDED, we can use selection options |
| | | $config = rcmail::get_instance()->config; |
| | | $config = rcube::get_instance()->config; |
| | | // #1486225: Some dovecot versions returns wrong result using LIST-EXTENDED |
| | | if (!$config->get('imap_force_lsub') && $this->get_capability('LIST-EXTENDED')) { |
| | | // This will also set folder options, LSUB doesn't do that |
| | |
| | | NULL, array('SUBSCRIBED')); |
| | | |
| | | // unsubscribe non-existent folders, remove from the list |
| | | if (is_array($a_folders) && $name == '*') { |
| | | // we can do this only when LIST response is available |
| | | if (is_array($a_folders) && $name == '*' && !empty($this->conn->data['LIST'])) { |
| | | foreach ($a_folders as $idx => $folder) { |
| | | if ($this->conn->data['LIST'] && ($opts = $this->conn->data['LIST'][$folder]) |
| | | if (($opts = $this->conn->data['LIST'][$folder]) |
| | | && in_array('\\NonExistent', $opts) |
| | | ) { |
| | | $this->conn->unsubscribe($folder); |
| | |
| | | else { |
| | | $a_folders = $this->conn->listSubscribed($root, $name); |
| | | |
| | | // unsubscribe non-existent folders, remove from the list |
| | | if (is_array($a_folders) && $name == '*') { |
| | | // unsubscribe non-existent folders, remove them from the list, |
| | | // we can do this only when LIST response is available |
| | | if (is_array($a_folders) && $name == '*' && !empty($this->conn->data['LIST'])) { |
| | | foreach ($a_folders as $idx => $folder) { |
| | | if ($this->conn->data['LIST'] && ($opts = $this->conn->data['LIST'][$folder]) |
| | | && in_array('\\Noselect', $opts) |
| | | if (!isset($this->conn->data['LIST'][$folder]) |
| | | || in_array('\\Noselect', $this->conn->data['LIST'][$folder]) |
| | | ) { |
| | | // Some servers returns \Noselect for existing folders |
| | | if (!$this->folder_exists($folder)) { |
| | |
| | | } |
| | | |
| | | // Give plugins a chance to provide a list of folders |
| | | $data = rcmail::get_instance()->plugins->exec_hook('folders_list', |
| | | $data = rcube::get_instance()->plugins->exec_hook('storage_folders', |
| | | array('root' => $root, 'name' => $name, 'filter' => $filter, 'mode' => 'LIST')); |
| | | |
| | | if (isset($data['folders'])) { |
| | |
| | | */ |
| | | public function get_quota() |
| | | { |
| | | if ($this->get_capability('QUOTA')) { |
| | | if ($this->get_capability('QUOTA') && $this->check_connection()) { |
| | | return $this->conn->getQuota(); |
| | | } |
| | | |
| | |
| | | |
| | | // get list of folders |
| | | if ((strpos($folder, '%') === false) && (strpos($folder, '*') === false)) { |
| | | $sub_mboxes = $this->list_unsubscribed('', $folder . $delm . '*'); |
| | | $sub_mboxes = $this->list_folders('', $folder . $delm . '*'); |
| | | } |
| | | else { |
| | | $sub_mboxes = $this->list_unsubscribed(); |
| | | $sub_mboxes = $this->list_folders(); |
| | | } |
| | | |
| | | // send delete command to server |
| | |
| | | return $this->icache['options']; |
| | | } |
| | | |
| | | // get cached metadata |
| | | $cache_key = 'mailboxes.folder-info.' . $folder; |
| | | $cached = $this->get_cache($cache_key); |
| | | |
| | | if (is_array($cached)) |
| | | return $cached; |
| | | |
| | | $acl = $this->get_capability('ACL'); |
| | | $namespace = $this->get_namespace(); |
| | | $options = array(); |
| | |
| | | $options['norename'] = $options['is_root'] || $options['namespace'] != 'personal'; |
| | | } |
| | | |
| | | // update caches |
| | | $this->icache['options'] = $options; |
| | | $this->update_cache($cache_key, $options); |
| | | |
| | | return $options; |
| | | } |
| | |
| | | if (!$this->check_connection()) { |
| | | return false; |
| | | } |
| | | |
| | | $this->clear_cache('mailboxes.folder-info.' . $folder); |
| | | |
| | | return $this->conn->setACL($folder, $user, $acl); |
| | | } |
| | |
| | | return false; |
| | | } |
| | | |
| | | $this->clear_cache('mailboxes.metadata.' . $folder); |
| | | |
| | | if ($this->get_capability('METADATA') || |
| | | (!strlen($folder) && $this->get_capability('METADATA-SERVER')) |
| | | ) { |
| | |
| | | if (!$this->check_connection()) { |
| | | return false; |
| | | } |
| | | |
| | | $this->clear_cache('mailboxes.metadata.' . $folder); |
| | | |
| | | if ($this->get_capability('METADATA') || |
| | | (!strlen($folder) && $this->get_capability('METADATA-SERVER')) |
| | |
| | | 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')) |
| | | ) { |
| | | return $this->conn->getMetadata($folder, $entries, $options); |
| | | $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(); |
| | |
| | | } |
| | | } |
| | | |
| | | $this->update_cache($cache_key, $res); |
| | | return $res; |
| | | } |
| | | |
| | |
| | | if (substr($entry, 0, 7) == '/shared') { |
| | | return array(substr($entry, 7), 'value.shared'); |
| | | } |
| | | else if (substr($entry, 0, 8) == '/protected') { |
| | | else if (substr($entry, 0, 8) == '/private') { |
| | | return array(substr($entry, 8), 'value.priv'); |
| | | } |
| | | |
| | |
| | | /** |
| | | * Enable or disable indexes caching |
| | | * |
| | | * @param string $type Cache type (@see rcmail::get_cache) |
| | | * @param string $type Cache type (@see rcube::get_cache) |
| | | */ |
| | | public function set_caching($type) |
| | | { |
| | |
| | | protected function get_cache_engine() |
| | | { |
| | | if ($this->caching && !$this->cache) { |
| | | $rcmail = rcmail::get_instance(); |
| | | $this->cache = $rcmail->get_cache('IMAP', $this->caching); |
| | | $rcube = rcube::get_instance(); |
| | | $ttl = $rcube->config->get('message_cache_lifetime', '10d'); |
| | | $this->cache = $rcube->get_cache('IMAP', $this->caching, $ttl); |
| | | } |
| | | |
| | | return $this->cache; |
| | |
| | | * @param string $key Cache key |
| | | * @param mixed $data Data |
| | | */ |
| | | protected function update_cache($key, $data) |
| | | public function update_cache($key, $data) |
| | | { |
| | | if ($cache = $this->get_cache_engine()) { |
| | | $cache->set($key, $data); |
| | |
| | | { |
| | | if ($cache = $this->get_cache_engine()) { |
| | | $cache->remove($key, $prefix_mode); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Delete outdated cache entries |
| | | */ |
| | | public function expunge_cache() |
| | | { |
| | | if ($this->mcache) { |
| | | $ttl = rcube::get_instance()->config->get('message_cache_lifetime', '10d'); |
| | | $this->mcache->expunge($ttl); |
| | | } |
| | | |
| | | if ($this->cache) { |
| | | $this->cache->expunge(); |
| | | } |
| | | } |
| | | |
| | |
| | | protected function get_mcache_engine() |
| | | { |
| | | if ($this->messages_caching && !$this->mcache) { |
| | | $rcmail = rcmail::get_instance(); |
| | | if ($dbh = $rcmail->get_dbh()) { |
| | | $rcube = rcube::get_instance(); |
| | | if ($dbh = $rcube->get_dbh()) { |
| | | $this->mcache = new rcube_imap_cache( |
| | | $dbh, $this, $rcmail->user->ID, $this->options['skip_deleted']); |
| | | $dbh, $this, $rcube->get_user_id(), $this->options['skip_deleted']); |
| | | } |
| | | } |
| | | |
| | |
| | | $a_defaults[$p] = $folder; |
| | | } |
| | | else { |
| | | $folders[$folder] = rcube_charset_convert($folder, 'UTF7-IMAP'); |
| | | $folders[$folder] = rcube_charset::convert($folder, 'UTF7-IMAP'); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | public function debug_handler(&$imap, $message) |
| | | { |
| | | write_log('imap', $message); |
| | | rcube::write_log('imap', $message); |
| | | } |
| | | |
| | | } // end class rcube_imap |
| | | |
| | | /** |
| | | * Deprecated methods (to be removed) |
| | | */ |
| | | |
| | | public function decode_address_list($input, $max = null, $decode = true, $fallback = null) |
| | | { |
| | | return rcube_mime::decode_address_list($input, $max, $decode, $fallback); |
| | | } |
| | | |
| | | public function decode_header($input, $fallback = null) |
| | | { |
| | | return rcube_mime::decode_mime_string((string)$input, $fallback); |
| | | } |
| | | |
| | | public static function decode_mime_string($input, $fallback = null) |
| | | { |
| | | return rcube_mime::decode_mime_string($input, $fallback); |
| | | } |
| | | |
| | | public function mime_decode($input, $encoding = '7bit') |
| | | { |
| | | return rcube_mime::decode($input, $encoding); |
| | | } |
| | | |
| | | public static function explode_header_string($separator, $str, $remove_comments = false) |
| | | { |
| | | return rcube_mime::explode_header_string($separator, $str, $remove_comments); |
| | | } |
| | | |
| | | public function select_mailbox($mailbox) |
| | | { |
| | | // do nothing |
| | | } |
| | | |
| | | public function set_mailbox($folder) |
| | | { |
| | | $this->set_folder($folder); |
| | | } |
| | | |
| | | public function get_mailbox_name() |
| | | { |
| | | return $this->get_folder(); |
| | | } |
| | | |
| | | public function list_headers($folder='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $slice=0) |
| | | { |
| | | return $this->list_messages($folder, $page, $sort_field, $sort_order, $slice); |
| | | } |
| | | |
| | | public function mailbox_status($folder = null) |
| | | { |
| | | return $this->folder_status($folder); |
| | | } |
| | | |
| | | public function message_index($folder = '', $sort_field = NULL, $sort_order = NULL) |
| | | { |
| | | return $this->index($folder, $sort_field, $sort_order); |
| | | } |
| | | |
| | | public function message_index_direct($folder, $sort_field = null, $sort_order = null, $skip_cache = true) |
| | | { |
| | | return $this->index_direct($folder, $sort_field, $sort_order, $skip_cache); |
| | | } |
| | | |
| | | public function list_mailboxes($root='', $name='*', $filter=null, $rights=null, $skip_sort=false) |
| | | { |
| | | return $this->list_folders_subscribed($root, $name, $filter, $rights, $skip_sort); |
| | | } |
| | | |
| | | public function list_unsubscribed($root='', $name='*', $filter=null, $rights=null, $skip_sort=false) |
| | | { |
| | | return $this->list_folders($root, $name, $filter, $rights, $skip_sort); |
| | | } |
| | | |
| | | public function get_mailbox_size($folder) |
| | | { |
| | | return $this->folder_size($folder); |
| | | } |
| | | |
| | | public function create_mailbox($folder, $subscribe=false) |
| | | { |
| | | return $this->create_folder($folder, $subscribe); |
| | | } |
| | | |
| | | public function rename_mailbox($folder, $new_name) |
| | | { |
| | | return $this->rename_folder($folder, $new_name); |
| | | } |
| | | |
| | | function delete_mailbox($folder) |
| | | { |
| | | return $this->delete_folder($folder); |
| | | } |
| | | |
| | | public function mailbox_exists($folder, $subscription=false) |
| | | { |
| | | return $this->folder_exists($folder, $subscription); |
| | | } |
| | | |
| | | public function mailbox_namespace($folder) |
| | | { |
| | | return $this->folder_namespace($folder); |
| | | } |
| | | |
| | | public function mod_mailbox($folder, $mode = 'out') |
| | | { |
| | | return $this->mod_folder($folder, $mode); |
| | | } |
| | | |
| | | public function mailbox_attributes($folder, $force=false) |
| | | { |
| | | return $this->folder_attributes($folder, $force); |
| | | } |
| | | |
| | | public function mailbox_data($folder) |
| | | { |
| | | return $this->folder_data($folder); |
| | | } |
| | | |
| | | public function mailbox_info($folder) |
| | | { |
| | | return $this->folder_info($folder); |
| | | } |
| | | |
| | | public function mailbox_sync($folder) |
| | | { |
| | | return $this->folder_sync($folder); |
| | | } |
| | | |
| | | public function expunge($folder='', $clear_cache=true) |
| | | { |
| | | return $this->expunge_folder($folder, $clear_cache); |
| | | } |
| | | |
| | | } |