| | |
| | | */ |
| | | |
| | | |
| | | /** |
| | | * Obtain classes from the Iloha IMAP library |
| | | */ |
| | | require_once('lib/imap.inc'); |
| | | require_once('lib/mime.inc'); |
| | | require_once('lib/utf7.inc'); |
| | | |
| | | |
| | | /** |
| | | * Interface class for accessing an IMAP server |
| | | * |
| | | * This is a wrapper that implements the Iloha IMAP Library (IIL) |
| | | * |
| | | * @package RoundCube Webmail |
| | | * @author Thomas Bruederli <roundcube@gmail.com> |
| | | * @version 1.22 |
| | | * @link http://ilohamail.org |
| | | */ |
| | | class rcube_imap |
| | | { |
| | | var $db; |
| | |
| | | var $uid_id_map = array(); |
| | | var $msg_headers = array(); |
| | | var $capabilities = array(); |
| | | var $skip_deleted = FALSE; |
| | | var $debug_level = 1; |
| | | |
| | | |
| | | // PHP 5 constructor |
| | | /** |
| | | * Object constructor |
| | | * |
| | | * @param object Database connection |
| | | */ |
| | | function __construct($db_conn) |
| | | { |
| | | $this->db = $db_conn; |
| | | $this->db = $db_conn; |
| | | } |
| | | |
| | | // PHP 4 compatibility |
| | | |
| | | /** |
| | | * PHP 4 object constructor |
| | | * |
| | | * @see rcube_imap::__construct |
| | | */ |
| | | function rcube_imap($db_conn) |
| | | { |
| | | $this->__construct($db_conn); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Connect to an IMAP server |
| | | * |
| | | * @param string Host to connect |
| | | * @param string Username for IMAP account |
| | | * @param string Password for IMAP account |
| | | * @param number Port to connect to |
| | | * @param boolean Use SSL connection |
| | | * @return boolean TRUE on success, FALSE on failure |
| | | * @access public |
| | | */ |
| | | function connect($host, $user, $pass, $port=143, $use_ssl=FALSE) |
| | | { |
| | | global $ICL_SSL, $ICL_PORT, $CONFIG; |
| | | global $ICL_SSL, $ICL_PORT; |
| | | |
| | | // check for Open-SSL support in PHP build |
| | | if ($use_ssl && in_array('openssl', get_loaded_extensions())) |
| | | $ICL_SSL = TRUE; |
| | | else if ($use_ssl) |
| | | { |
| | | raise_error(array('code' => 403, |
| | | 'type' => 'imap', |
| | | 'file' => __FILE__, |
| | | raise_error(array('code' => 403, 'type' => 'imap', 'file' => __FILE__, |
| | | 'message' => 'Open SSL not available;'), TRUE, FALSE); |
| | | $port = 143; |
| | | } |
| | |
| | | $this->ssl = $use_ssl; |
| | | |
| | | // print trace mesages |
| | | if ($this->conn && ($CONFIG['debug_level'] & 8)) |
| | | if ($this->conn && ($this->debug_level & 8)) |
| | | console($this->conn->message); |
| | | |
| | | // write error log |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Close IMAP connection |
| | | * Usually done on script shutdown |
| | | * |
| | | * @access public |
| | | */ |
| | | function close() |
| | | { |
| | | if ($this->conn) |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Close IMAP connection and re-connect |
| | | * This is used to avoid some strange socket errors when talking to Courier IMAP |
| | | * |
| | | * @access public |
| | | */ |
| | | function reconnect() |
| | | { |
| | | $this->close(); |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Set a root folder for the IMAP connection. |
| | | * |
| | | * Only folders within this root folder will be displayed |
| | | * and all folder paths will be translated using this folder name |
| | | * |
| | | * @param string Root folder |
| | | * @access public |
| | | */ |
| | | function set_rootdir($root) |
| | | { |
| | | if (ereg('[\.\/]$', $root)) //(substr($root, -1, 1)==='/') |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * This list of folders will be listed above all other folders |
| | | * |
| | | * @param array Indexed list of folder names |
| | | * @access public |
| | | */ |
| | | function set_default_mailboxes($arr) |
| | | { |
| | | if (is_array($arr)) |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Set internal mailbox reference. |
| | | * |
| | | * All operations will be perfomed on this mailbox/folder |
| | | * |
| | | * @param string Mailbox/Folder name |
| | | * @access public |
| | | */ |
| | | function set_mailbox($mbox) |
| | | { |
| | | $mailbox = $this->_mod_mailbox($mbox); |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Set internal list page |
| | | * |
| | | * @param number Page number to list |
| | | * @access public |
| | | */ |
| | | function set_page($page) |
| | | { |
| | | $this->list_page = (int)$page; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Set internal page size |
| | | * |
| | | * @param number Number of messages to display on one page |
| | | * @access public |
| | | */ |
| | | function set_pagesize($size) |
| | | { |
| | | $this->page_size = (int)$size; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Returns the currently used mailbox name |
| | | * |
| | | * @return string Name of the mailbox/folder |
| | | * @access public |
| | | */ |
| | | function get_mailbox_name() |
| | | { |
| | | return $this->conn ? $this->_mod_mailbox($this->mailbox, 'out') : ''; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Returns the IMAP server's capability |
| | | * |
| | | * @param string Capability name |
| | | * @return mixed Capability value or TRUE if supported, FALSE if not |
| | | * @access public |
| | | */ |
| | | function get_capability($cap) |
| | | { |
| | | $cap = strtoupper($cap); |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Returns the delimiter that is used by the IMAP server for folder separation |
| | | * |
| | | * @return string Delimiter string |
| | | * @access public |
| | | */ |
| | | function get_hierarchy_delimiter() |
| | | { |
| | | if ($this->conn && empty($this->delimiter)) |
| | |
| | | return $this->delimiter; |
| | | } |
| | | |
| | | // public method for mailbox listing |
| | | // convert mailbox name with root dir first |
| | | |
| | | /** |
| | | * Public method for mailbox listing. |
| | | * |
| | | * Converts mailbox name with root dir first |
| | | * |
| | | * @param string Optional root folder |
| | | * @param string Optional filter for mailbox listing |
| | | * @return array List of mailboxes/folders |
| | | * @access public |
| | | */ |
| | | function list_mailboxes($root='', $filter='*') |
| | | { |
| | | $a_out = array(); |
| | |
| | | return $a_out; |
| | | } |
| | | |
| | | // private method for mailbox listing |
| | | |
| | | /** |
| | | * Private method for mailbox listing |
| | | * |
| | | * @return array List of mailboxes/folders |
| | | * @access private |
| | | * @see rcube_imap::list_mailboxes |
| | | */ |
| | | function _list_mailboxes($root='', $filter='*') |
| | | { |
| | | $a_defaults = $a_out = array(); |
| | |
| | | } |
| | | |
| | | |
| | | // get message count for a specific mailbox; acceptes modes are: ALL, UNSEEN |
| | | /** |
| | | * Get message count for a specific mailbox |
| | | * |
| | | * @param string Mailbox/folder name |
| | | * @param string Mode for count [ALL|UNSEEN|RECENT] |
| | | * @param boolean Force reading from server and update cache |
| | | * @return number Number of messages |
| | | * @access public |
| | | */ |
| | | function messagecount($mbox='', $mode='ALL', $force=FALSE) |
| | | { |
| | | $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox; |
| | | return $this->_messagecount($mailbox, $mode, $force); |
| | | } |
| | | |
| | | // private method for getting nr of mesages |
| | | |
| | | /** |
| | | * Private method for getting nr of messages |
| | | * |
| | | * @access private |
| | | * @see rcube_imap::messagecount |
| | | */ |
| | | function _messagecount($mailbox='', $mode='ALL', $force=FALSE) |
| | | { |
| | | $a_mailbox_cache = FALSE; |
| | | $mode = strtoupper($mode); |
| | | |
| | | if (!$mailbox) |
| | | if (empty($mailbox)) |
| | | $mailbox = $this->mailbox; |
| | | |
| | | $a_mailbox_cache = $this->get_cache('messagecount'); |
| | |
| | | // return cached value |
| | | if (!$force && is_array($a_mailbox_cache[$mailbox]) && isset($a_mailbox_cache[$mailbox][$mode])) |
| | | return $a_mailbox_cache[$mailbox][$mode]; |
| | | |
| | | $search_str = "ALL UNDELETED"; |
| | | |
| | | // get message count and store in cache |
| | | if ($mode == 'UNSEEN') |
| | | $search_str .= " UNSEEN"; |
| | | // RECENT count is fetched abit different |
| | | if ($mode == 'RECENT') |
| | | $count = iil_C_CheckForRecent($this->conn, $mailbox); |
| | | |
| | | // get message count using SEARCH |
| | | // not very performant but more precise (using UNDELETED) |
| | | $count = 0; |
| | | $index = $this->_search_index($mailbox, $search_str); |
| | | if (is_array($index)) |
| | | // use SEARCH for message counting |
| | | else if ($this->skip_deleted) |
| | | { |
| | | $str = implode(",", $index); |
| | | if (!empty($str)) |
| | | $count = count($index); |
| | | $search_str = "ALL UNDELETED"; |
| | | |
| | | // get message count and store in cache |
| | | if ($mode == 'UNSEEN') |
| | | $search_str .= " UNSEEN"; |
| | | |
| | | // get message count using SEARCH |
| | | // not very performant but more precise (using UNDELETED) |
| | | $count = 0; |
| | | $index = $this->_search_index($mailbox, $search_str); |
| | | if (is_array($index)) |
| | | { |
| | | $str = implode(",", $index); |
| | | if (!empty($str)) |
| | | $count = count($index); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | if ($mode == 'UNSEEN') |
| | | $count = iil_C_CountUnseen($this->conn, $mailbox); |
| | | else |
| | | $count = iil_C_CountMessages($this->conn, $mailbox); |
| | | } |
| | | |
| | | if (is_array($a_mailbox_cache[$mailbox])) |
| | |
| | | } |
| | | |
| | | |
| | | // public method for listing headers |
| | | // convert mailbox name with root dir first |
| | | /** |
| | | * Public method for listing headers |
| | | * convert mailbox name with root dir first |
| | | * |
| | | * @param string Mailbox/folder name |
| | | * @param number Current page to list |
| | | * @param string Header field to sort by |
| | | * @param string Sort order [ASC|DESC] |
| | | * @return array Indexed array with message header objects |
| | | * @access public |
| | | */ |
| | | function list_headers($mbox='', $page=NULL, $sort_field=NULL, $sort_order=NULL) |
| | | { |
| | | $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox; |
| | |
| | | } |
| | | |
| | | |
| | | // private method for listing message header |
| | | /** |
| | | * Private method for listing message header |
| | | * |
| | | * @access private |
| | | * @see rcube_imap::list_headers |
| | | */ |
| | | function _list_headers($mailbox='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $recursive=FALSE) |
| | | { |
| | | if (!strlen($mailbox)) |
| | |
| | | else |
| | | { |
| | | $begin = $start_msg; |
| | | $end = $start_msg + $this->page_size; |
| | | $end = $start_msg + $this->page_size; |
| | | } |
| | | |
| | | if ($begin < 0) $begin = 0; |
| | |
| | | else |
| | | { |
| | | // retrieve headers from IMAP |
| | | if ($this->get_capability('sort') && ($msg_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field))) |
| | | if ($this->get_capability('sort') && ($msg_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, $this->skip_deleted ? 'UNDELETED' : ''))) |
| | | { |
| | | //console("$mailbox: ".count($msg_index)); |
| | | //console("$mailbox: ".join(',', $msg_index)); |
| | | |
| | | $msgs = $msg_index[$begin]; |
| | | for ($i=$begin; $i < $end; $i++) |
| | | for ($i=$begin+1; $i < $end; $i++) |
| | | { |
| | | if ($this->sort_order == 'DESC') |
| | | $msgs = $msg_index[$i].','.$msgs; |
| | | else |
| | | //if ($this->sort_order == 'DESC') |
| | | // $msgs = $msg_index[$i].','.$msgs; |
| | | //else |
| | | $msgs = $msgs.','.$msg_index[$i]; |
| | | } |
| | | |
| | |
| | | return $this->_list_headers($mailbox, $page, $this->sort_field, $this->sort_order, TRUE); |
| | | } |
| | | |
| | | |
| | | // cache is incomplete |
| | | $cache_index = $this->get_message_cache_index($cache_key); |
| | | |
| | | // fetch reuested headers from server |
| | | $a_header_index = iil_C_FetchHeaders($this->conn, $mailbox, $msgs); |
| | | $a_msg_headers = array(); |
| | | $deleted_count = 0; |
| | | |
| | | if (!empty($a_header_index)) |
| | | { |
| | | foreach ($a_header_index as $i => $headers) |
| | | { |
| | | if ($headers->deleted) |
| | | { |
| | | // delete from cache |
| | | if ($cache_index[$headers->id] && $cache_index[$headers->id] == $headers->uid) |
| | | $this->remove_message_cache($cache_key, $headers->id); |
| | | |
| | | $deleted_count++; |
| | | continue; |
| | | } |
| | | |
| | | // add message to cache |
| | | if ($this->caching_enabled && $cache_index[$headers->id] != $headers->uid) |
| | | $this->add_message_cache($cache_key, $headers->id, $headers); |
| | | |
| | | $a_msg_headers[$headers->uid] = $headers; |
| | | } |
| | | } |
| | | $deleted_count = $this->_fetch_headers($mailbox, $msgs, $a_msg_headers, $cache_key); |
| | | |
| | | // delete cached messages with a higher index than $max |
| | | $this->clear_message_cache($cache_key, $max); |
| | | |
| | | |
| | | // fetch more headers of there were any deleted messages |
| | | // ... |
| | | |
| | | |
| | | // kick child process to sync cache |
| | | // ... |
| | | |
| | |
| | | |
| | | return array_values($a_msg_headers); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Fetches message headers |
| | | * Used for loop |
| | | * |
| | | * @param string Mailbox name |
| | | * @param string Message indey to fetch |
| | | * @param array Reference to message headers array |
| | | * @param array Array with cache index |
| | | * @return number Number of deleted messages |
| | | * @access private |
| | | */ |
| | | function _fetch_headers($mailbox, $msgs, &$a_msg_headers, $cache_key) |
| | | { |
| | | // cache is incomplete |
| | | $cache_index = $this->get_message_cache_index($cache_key); |
| | | |
| | | // fetch reuested headers from server |
| | | $a_header_index = iil_C_FetchHeaders($this->conn, $mailbox, $msgs); |
| | | $deleted_count = 0; |
| | | |
| | | if (!empty($a_header_index)) |
| | | { |
| | | foreach ($a_header_index as $i => $headers) |
| | | { |
| | | if ($headers->deleted && $this->skip_deleted) |
| | | { |
| | | // delete from cache |
| | | if ($cache_index[$headers->id] && $cache_index[$headers->id] == $headers->uid) |
| | | $this->remove_message_cache($cache_key, $headers->id); |
| | | |
| | | $deleted_count++; |
| | | continue; |
| | | } |
| | | |
| | | // add message to cache |
| | | if ($this->caching_enabled && $cache_index[$headers->id] != $headers->uid) |
| | | $this->add_message_cache($cache_key, $headers->id, $headers); |
| | | |
| | | $a_msg_headers[$headers->uid] = $headers; |
| | | } |
| | | } |
| | | |
| | | return $deleted_count; |
| | | } |
| | | |
| | | |
| | | |
| | | // return sorted array of message UIDs |
| | |
| | | // cache is OK |
| | | if ($cache_status>0) |
| | | { |
| | | $a_index = get_message_cache_index($cache_key, FALSE, $this->sort_field); |
| | | $a_index = $this->get_message_cache_index($cache_key, TRUE, $this->sort_field, $this->sort_order); |
| | | return array_values($a_index); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | function get_headers($uid, $mbox=NULL) |
| | | function get_headers($id, $mbox=NULL, $is_uid=TRUE) |
| | | { |
| | | $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox; |
| | | |
| | |
| | | if ($headers = $this->get_cached_message($mailbox.'.msg', $uid)) |
| | | return $headers; |
| | | |
| | | $msg_id = $this->_uid2id($uid); |
| | | $msg_id = $is_uid ? $this->_uid2id($id) : $id; |
| | | $headers = iil_C_FetchHeader($this->conn, $mailbox, $msg_id); |
| | | |
| | | // write headers cache |
| | |
| | | |
| | | |
| | | // clear all messages in a specific mailbox |
| | | function clear_mailbox($mbox) |
| | | function clear_mailbox($mbox=NULL) |
| | | { |
| | | $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox; |
| | | $mailbox = !empty($mbox) ? $this->_mod_mailbox($mbox) : $this->mailbox; |
| | | $msg_count = $this->_messagecount($mailbox, 'ALL'); |
| | | |
| | | if ($msg_count>0) |
| | |
| | | } |
| | | |
| | | |
| | | function get_message_cache_index($key, $force=FALSE, $sort_col='idx') |
| | | function get_message_cache_index($key, $force=FALSE, $sort_col='idx', $sort_order='ASC') |
| | | { |
| | | static $sa_message_index = array(); |
| | | |
| | |
| | | FROM ".get_table_name('messages')." |
| | | WHERE user_id=? |
| | | AND cache_key=? |
| | | ORDER BY ".$sort_col." ASC", |
| | | ORDER BY ".$this->db->quote_identifier($sort_col)." ".$sort_order, |
| | | $_SESSION['user_id'], |
| | | $key); |
| | | |
| | |
| | | |
| | | $this->db->query( |
| | | "INSERT INTO ".get_table_name('messages')." |
| | | (user_id, del, cache_key, idx, uid, subject, ".$this->db->quoteIdentifier('from').", ".$this->db->quoteIdentifier('to').", cc, date, size, headers) |
| | | VALUES (?, 0, ?, ?, ?, ?, ?, ?, ?, ".$this->db->fromunixtime($headers->timestamp).", ?, ?)", |
| | | (user_id, del, cache_key, created, idx, uid, subject, ".$this->db->quoteIdentifier('from').", ".$this->db->quoteIdentifier('to').", cc, date, size, headers) |
| | | VALUES (?, 0, ?, now(), ?, ?, ?, ?, ?, ?, ".$this->db->fromunixtime($headers->timestamp).", ?, ?)", |
| | | $_SESSION['user_id'], |
| | | $key, |
| | | $index, |
| | | $headers->uid, |
| | | $this->decode_header($headers->subject, TRUE), |
| | | $this->decode_header($headers->from, TRUE), |
| | | $this->decode_header($headers->to, TRUE), |
| | | $this->decode_header($headers->cc, TRUE), |
| | | substr($this->decode_header((string)$headers->subject, TRUE), 0, 128), |
| | | substr($this->decode_header((string)$headers->from, TRUE), 0, 128), |
| | | substr($this->decode_header((string)$headers->to, TRUE), 0, 128), |
| | | substr($this->decode_header((string)$headers->cc, TRUE), 0, 128), |
| | | (int)$headers->size, |
| | | serialize($headers)); |
| | | } |
| | |
| | | $rest = quoted_printable_decode($rest); |
| | | } |
| | | |
| | | return decode_specialchars($rest, $a[0]); |
| | | return rcube_charset_convert($rest, $a[0]); |
| | | } |
| | | else |
| | | return $str; //we dont' know what to do with this |
| | | return $str; // we dont' know what to do with this |
| | | } |
| | | |
| | | |
| | |
| | | function charset_decode($body, $ctype_param) |
| | | { |
| | | if (is_array($ctype_param) && !empty($ctype_param['charset'])) |
| | | return decode_specialchars($body, $ctype_param['charset']); |
| | | return rcube_charset_convert($body, $ctype_param['charset']); |
| | | |
| | | return $body; |
| | | } |