| | |
| | | class rcube_imap |
| | | { |
| | | public $debug_level = 1; |
| | | public $error_code = 0; |
| | | public $skip_deleted = false; |
| | | public $root_dir = ''; |
| | | public $page_size = 10; |
| | |
| | | public $delimiter = NULL; |
| | | public $threading = false; |
| | | public $fetch_add_headers = ''; |
| | | public $get_all_headers = false; |
| | | |
| | | /** |
| | | * Instance of rcube_imap_generic |
| | |
| | | private $options = array('auth_method' => 'check'); |
| | | private $host, $user, $pass, $port, $ssl; |
| | | |
| | | /** |
| | | * All (additional) headers used (in any way) by Roundcube |
| | | * Not listed here: DATE, FROM, TO, SUBJECT, CONTENT-TYPE, LIST-POST |
| | | * (used for messages listing) are hardcoded in rcube_imap_generic::fetchHeaders() |
| | | * |
| | | * @var array |
| | | * @see rcube_imap::fetch_add_headers |
| | | */ |
| | | private $all_headers = array( |
| | | 'REPLY-TO', |
| | | 'IN-REPLY-TO', |
| | | 'CC', |
| | | 'BCC', |
| | | 'MESSAGE-ID', |
| | | 'CONTENT-TRANSFER-ENCODING', |
| | | 'REFERENCES', |
| | | 'X-PRIORITY', |
| | | 'X-DRAFT-INFO', |
| | | 'MAIL-FOLLOWUP-TO', |
| | | 'MAIL-REPLY-TO', |
| | | 'RETURN-PATH', |
| | | ); |
| | | |
| | | |
| | | /** |
| | | * Object constructor |
| | |
| | | $this->conn = new rcube_imap_generic(); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Connect to an IMAP server |
| | | * |
| | |
| | | } |
| | | |
| | | // get server properties |
| | | if (!empty($this->conn->rootdir)) |
| | | $this->set_rootdir($this->conn->rootdir); |
| | | $rootdir = $this->conn->getRootDir(); |
| | | if (!empty($rootdir)) |
| | | $this->set_rootdir($rootdir); |
| | | if (empty($this->delimiter)) |
| | | $this->get_hierarchy_delimiter(); |
| | | |
| | |
| | | } |
| | | // write error log |
| | | else if ($this->conn->error) { |
| | | $this->error_code = $this->conn->errornum; |
| | | if ($pass && $user) |
| | | raise_error(array('code' => 403, 'type' => 'imap', |
| | | 'file' => __FILE__, 'line' => __LINE__, |
| | |
| | | return false; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Close IMAP connection |
| | | * Usually done on script shutdown |
| | |
| | | $this->write_cache(); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Close IMAP connection and re-connect |
| | | * This is used to avoid some strange socket errors when talking to Courier IMAP |
| | |
| | | $this->conn->select($this->mailbox); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Returns code of last error |
| | | * |
| | | * @return int Error code |
| | | */ |
| | | function get_error_code() |
| | | { |
| | | return ($this->conn) ? $this->conn->errornum : 0; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Returns message of last error |
| | | * |
| | | * @return string Error message |
| | | */ |
| | | function get_error_str() |
| | | { |
| | | return ($this->conn) ? $this->conn->error : ''; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Set options to be used in rcube_imap_generic::connect() |
| | | * |
| | |
| | | $this->options = array_merge($this->options, (array)$opt); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Set a root folder for the IMAP connection. |
| | | * |
| | |
| | | $this->get_hierarchy_delimiter(); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Set default message charset |
| | | * |
| | |
| | | $this->default_charset = $cs; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * This list of folders will be listed above all other folders |
| | | * |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Set internal mailbox reference. |
| | | * |
| | |
| | | $this->_clear_messagecount($mailbox); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Set internal list page |
| | | * |
| | |
| | | $this->list_page = (int)$page; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Set internal page size |
| | | * |
| | |
| | | $this->page_size = (int)$size; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Save a set of message ids for future message listing methods |
| | | * |
| | |
| | | $this->search_sorted = $sorted; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Return the saved search set as hash array |
| | | * @return array Search set |
| | |
| | | ); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Returns the currently used mailbox name |
| | | * |
| | |
| | | return $this->conn->connected() ? $this->mod_mailbox($this->mailbox, 'out') : ''; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Returns the IMAP server's capability |
| | | * |
| | |
| | | return $this->conn->getCapability(strtoupper($cap)); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Sets threading flag to the best supported THREAD algorithm |
| | | * |
| | |
| | | return $this->threading; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Checks the PERMANENTFLAGS capability of the current mailbox |
| | | * and returns true if the given flag is supported by the IMAP server |
| | | * |
| | | * @param string $flag Permanentflag name |
| | | * @return mixed True if this flag is supported |
| | | * @return boolean True if this flag is supported |
| | | * @access public |
| | | */ |
| | | function check_permflag($flag) |
| | | { |
| | | $flag = strtoupper($flag); |
| | | $imap_flag = $this->conn->flags[$flag]; |
| | | return (in_array_nocase($imap_flag, $this->conn->permanentflags)); |
| | | return (in_array_nocase($imap_flag, $this->conn->data['PERMANENTFLAGS'])); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Returns the delimiter that is used by the IMAP server for folder separation |
| | | * |
| | |
| | | return $this->delimiter; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Get message count for a specific mailbox |
| | | * |
| | |
| | | return $this->_messagecount($mailbox, $mode, $force, $status); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Private method for getting nr of messages |
| | | * |
| | |
| | | // use SEARCH for message counting |
| | | else if ($this->skip_deleted) { |
| | | $search_str = "ALL UNDELETED"; |
| | | $keys = array('COUNT'); |
| | | $need_uid = false; |
| | | |
| | | // get message count and store in cache |
| | | if ($mode == 'UNSEEN') |
| | | if ($mode == 'UNSEEN') { |
| | | $search_str .= " UNSEEN"; |
| | | // get message count using SEARCH |
| | | // not very performant but more precise (using UNDELETED) |
| | | $index = $this->conn->search($mailbox, $search_str); |
| | | } |
| | | else if ($status) { |
| | | $keys[] = 'MAX'; |
| | | $need_uid = true; |
| | | } |
| | | |
| | | $count = is_array($index) ? count($index) : 0; |
| | | // get message count using (E)SEARCH |
| | | // not very performant but more precise (using UNDELETED) |
| | | $index = $this->conn->search($mailbox, $search_str, $need_uid, $keys); |
| | | |
| | | $count = is_array($index) ? $index['COUNT'] : 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); |
| | | $this->set_folder_stats($mailbox, 'maxuid', is_array($index) ? $index['MAX'] : 0); |
| | | } |
| | | } |
| | | else { |
| | |
| | | /** |
| | | * Private method for getting nr of threads |
| | | * |
| | | * @param string $mailbox |
| | | * @param int $msg_count |
| | | * @param string $mailbox Folder name |
| | | * @param int $msg_count Number of messages in the folder |
| | | * @access private |
| | | * @see rcube_imap::messagecount() |
| | | */ |
| | | private function _threadcount($mailbox, &$msg_count) |
| | | { |
| | | if (!empty($this->icache['threads'])) |
| | | if (!empty($this->icache['threads'])) { |
| | | $msg_count = count($this->icache['threads']['depth']); |
| | | return count($this->icache['threads']['tree']); |
| | | } |
| | | |
| | | list ($thread_tree, $msg_depth, $has_children) = $this->_fetch_threads($mailbox); |
| | | if (is_array($result = $this->_fetch_threads($mailbox))) { |
| | | $thread_tree = array_shift($result); |
| | | $msg_count = count($result[0]); |
| | | } |
| | | |
| | | $msg_count = count($msg_depth); |
| | | |
| | | // $this->update_thread_cache($mailbox, $thread_tree, $msg_depth, $has_children); |
| | | // list ($thread_tree, $msg_depth, $has_children) = $result; |
| | | // $this->update_thread_cache($mailbox, $thread_tree, $msg_depth, $has_children); |
| | | return count($thread_tree); |
| | | } |
| | | |
| | |
| | | * @param int $page Current page to list |
| | | * @param string $sort_field Header field to sort by |
| | | * @param string $sort_order Sort order [ASC|DESC] |
| | | * @param boolean $recursive |
| | | * @param boolean $recursive True if called recursively |
| | | * @param int $slice Number of slice items to extract from result array |
| | | * @return array Indexed array with message header objects |
| | | * @access private |
| | |
| | | /** |
| | | * Private method for fetching threaded messages headers |
| | | * |
| | | * @param string $mailbox Mailbox name |
| | | * @param string $thread_tree |
| | | * @param int $msg_depth |
| | | * @param boolean $has_children |
| | | * @param int $msg_index |
| | | * @param int $page |
| | | * @param int $slice |
| | | * @param string $mailbox Mailbox name |
| | | * @param array $thread_tree Thread tree data |
| | | * @param array $msg_depth Thread depth data |
| | | * @param array $has_children Thread children data |
| | | * @param array $msg_index Messages index |
| | | * @param int $page List page number |
| | | * @param int $slice Number of threads to slice |
| | | * @return array Messages headers |
| | | * @access private |
| | | */ |
| | | private function _fetch_thread_headers($mailbox, $thread_tree, $msg_depth, $has_children, $msg_index, $page, $slice=0) |
| | |
| | | /** |
| | | * Helper function to get first and last index of the requested set |
| | | * |
| | | * @param int $max message count |
| | | * @param mixed $page page number to show, or string 'all' |
| | | * @return array array with two values: first index, last index |
| | | * @param int $max Messages count |
| | | * @param mixed $page Page number to show, or string 'all' |
| | | * @return array Array with two values: first index, last index |
| | | * @access private |
| | | */ |
| | | private function _get_message_range($max, $page) |
| | |
| | | |
| | | |
| | | /** |
| | | * Fetches message headers |
| | | * Used for loop |
| | | * Fetches message headers (used for loop) |
| | | * |
| | | * @param string Mailbox name |
| | | * @param string Message index to fetch |
| | | * @param array Reference to message headers array |
| | | * @param string Cache index string |
| | | * @param string $mailbox Mailbox name |
| | | * @param string $msgs Message index to fetch |
| | | * @param array $a_msg_headers Reference to message headers array |
| | | * @param string $cache_key Cache index key |
| | | * @return int Messages count |
| | | * @access private |
| | | */ |
| | |
| | | { |
| | | // fetch reqested headers from server |
| | | $a_header_index = $this->conn->fetchHeaders( |
| | | $mailbox, $msgs, false, false, $this->fetch_add_headers); |
| | | $mailbox, $msgs, false, false, $this->get_fetch_headers()); |
| | | |
| | | if (empty($a_header_index)) |
| | | return 0; |
| | |
| | | |
| | | $for_create[] = $headers->uid; |
| | | } |
| | | |
| | | |
| | | if ($for_remove) |
| | | $this->remove_message_cache($cache_key, $for_remove); |
| | | |
| | |
| | | * @param string $mbox_name Mailbox to get index from |
| | | * @param string $sort_field Sort column |
| | | * @param string $sort_order Sort order [ASC, DESC] |
| | | * @return array Indexed array with message ids |
| | | * @return array Indexed array with message IDs |
| | | */ |
| | | function message_index($mbox_name='', $sort_field=NULL, $sort_order=NULL) |
| | | { |
| | |
| | | $key = "{$mailbox}:{$this->sort_field}:{$this->sort_order}:{$this->search_string}.msgi"; |
| | | |
| | | // we have a saved search result, get index from there |
| | | if (!isset($this->cache[$key]) && $this->search_string |
| | | if (!isset($this->icache[$key]) && $this->search_string |
| | | && !$this->search_threads && $mailbox == $this->mailbox) { |
| | | // use message index sort as default sorting |
| | | if (!$this->sort_field) { |
| | |
| | | sort($msgs); |
| | | |
| | | if ($this->sort_order == 'DESC') |
| | | $this->cache[$key] = array_reverse($msgs); |
| | | $this->icache[$key] = array_reverse($msgs); |
| | | else |
| | | $this->cache[$key] = $msgs; |
| | | $this->icache[$key] = $msgs; |
| | | } |
| | | // sort with SORT command |
| | | else if ($this->search_sorted) { |
| | |
| | | $this->search('', $this->search_string, $this->search_charset, $this->sort_field); |
| | | |
| | | if ($this->sort_order == 'DESC') |
| | | $this->cache[$key] = array_reverse($this->search_set); |
| | | $this->icache[$key] = array_reverse($this->search_set); |
| | | else |
| | | $this->cache[$key] = $this->search_set; |
| | | $this->icache[$key] = $this->search_set; |
| | | } |
| | | else { |
| | | $a_index = $this->conn->fetchHeaderIndex($mailbox, |
| | |
| | | else if ($this->sort_order=="DESC") |
| | | arsort($a_index); |
| | | |
| | | $this->cache[$key] = array_keys($a_index); |
| | | $this->icache[$key] = array_keys($a_index); |
| | | } |
| | | else { |
| | | $this->cache[$key] = array(); |
| | | $this->icache[$key] = array(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // have stored it in RAM |
| | | if (isset($this->cache[$key])) |
| | | return $this->cache[$key]; |
| | | if (isset($this->icache[$key])) |
| | | return $this->icache[$key]; |
| | | |
| | | // check local cache |
| | | $cache_key = $mailbox.'.msg'; |
| | |
| | | if ($a_index !== false && $this->sort_order == 'DESC') |
| | | $a_index = array_reverse($a_index); |
| | | |
| | | $this->cache[$key] = $a_index; |
| | | $this->icache[$key] = $a_index; |
| | | } |
| | | // fetch complete message index |
| | | else if ($this->get_capability('SORT') && |
| | |
| | | if ($this->sort_order == 'DESC') |
| | | $a_index = array_reverse($a_index); |
| | | |
| | | $this->cache[$key] = $a_index; |
| | | $this->icache[$key] = $a_index; |
| | | } |
| | | else if ($a_index = $this->conn->fetchHeaderIndex( |
| | | $mailbox, "1:*", $this->sort_field, $this->skip_deleted)) { |
| | |
| | | else if ($this->sort_order=="DESC") |
| | | arsort($a_index); |
| | | |
| | | $this->cache[$key] = array_keys($a_index); |
| | | $this->icache[$key] = array_keys($a_index); |
| | | } |
| | | |
| | | return $this->cache[$key] !== false ? $this->cache[$key] : array(); |
| | | return $this->icache[$key] !== false ? $this->icache[$key] : array(); |
| | | } |
| | | |
| | | |
| | |
| | | $key = "{$mailbox}:{$this->sort_field}:{$this->sort_order}:{$this->search_string}.thi"; |
| | | |
| | | // we have a saved search result, get index from there |
| | | if (!isset($this->cache[$key]) && $this->search_string |
| | | if (!isset($this->icache[$key]) && $this->search_string |
| | | && $this->search_threads && $mailbox == $this->mailbox) { |
| | | // use message IDs for better performance |
| | | $ids = array_keys_recursive($this->search_set['tree']); |
| | | $this->cache[$key] = $this->_flatten_threads($mailbox, $this->search_set['tree'], $ids); |
| | | $this->icache[$key] = $this->_flatten_threads($mailbox, $this->search_set['tree'], $ids); |
| | | } |
| | | |
| | | // have stored it in RAM |
| | | if (isset($this->cache[$key])) |
| | | return $this->cache[$key]; |
| | | if (isset($this->icache[$key])) |
| | | return $this->icache[$key]; |
| | | /* |
| | | // check local cache |
| | | $cache_key = $mailbox.'.msg'; |
| | |
| | | // get all threads (default sort order) |
| | | list ($thread_tree) = $this->_fetch_threads($mailbox); |
| | | |
| | | $this->cache[$key] = $this->_flatten_threads($mailbox, $thread_tree); |
| | | $this->icache[$key] = $this->_flatten_threads($mailbox, $thread_tree); |
| | | |
| | | return $this->cache[$key]; |
| | | return $this->icache[$key]; |
| | | } |
| | | |
| | | |
| | |
| | | while (true) { |
| | | // do this in loop to save memory (1000 msgs ~= 10 MB) |
| | | if ($headers = $this->conn->fetchHeaders($mailbox, |
| | | "$start:$end", false, false, $this->fetch_add_headers) |
| | | "$start:$end", false, false, $this->get_fetch_headers()) |
| | | ) { |
| | | foreach ($headers as $header) { |
| | | $this->add_message_cache($cache_key, $header->id, $header, NULL, true); |
| | |
| | | $for_update = array_chunk($for_update, $chunk_size); |
| | | foreach ($for_update as $uids) { |
| | | if ($headers = $this->conn->fetchHeaders($mailbox, |
| | | $uids, false, false, $this->fetch_add_headers) |
| | | $uids, false, false, $this->get_fetch_headers()) |
| | | ) { |
| | | foreach ($headers as $header) { |
| | | $this->add_message_cache($cache_key, $header->id, $header, NULL, true); |
| | |
| | | /** |
| | | * Invoke search request to IMAP server |
| | | * |
| | | * @param string $mbox_name mailbox name to search in |
| | | * @param string $str search string |
| | | * @param string $charset search string charset |
| | | * @param string $sort_field header field to sort by |
| | | * @return array search results as list of message ids |
| | | * @param string $mbox_name Mailbox name to search in |
| | | * @param string $str Search criteria |
| | | * @param string $charset Search charset |
| | | * @param string $sort_field Header field to sort by |
| | | * @return array search results as list of message IDs |
| | | * @access public |
| | | */ |
| | | function search($mbox_name='', $str=NULL, $charset=NULL, $sort_field=NULL) |
| | |
| | | return $headers; |
| | | |
| | | $headers = $this->conn->fetchHeader( |
| | | $mailbox, $id, $is_uid, $bodystr, $this->fetch_add_headers); |
| | | $mailbox, $id, $is_uid, $bodystr, $this->get_fetch_headers()); |
| | | |
| | | // write headers cache |
| | | if ($headers) { |
| | |
| | | /** |
| | | * Set attachment filename from message part structure |
| | | * |
| | | * @access private |
| | | * @param rcube_message_part $part Part object |
| | | * @param string $headers Part's raw headers |
| | | * @access private |
| | | */ |
| | | private function _set_part_filename(&$part, $headers=null) |
| | | { |
| | |
| | | /** |
| | | * Get charset name from message structure (first part) |
| | | * |
| | | * @access private |
| | | * @param array $structure Message structure |
| | | * @return string Charset name |
| | | * @access private |
| | | */ |
| | | function _structure_charset($structure) |
| | | private function _structure_charset($structure) |
| | | { |
| | | while (is_array($structure)) { |
| | | if (is_array($structure[2]) && $structure[2][0] == 'charset') |
| | |
| | | function clear_mailbox($mbox_name=NULL) |
| | | { |
| | | $mailbox = !empty($mbox_name) ? $this->mod_mailbox($mbox_name) : $this->mailbox; |
| | | $msg_count = $this->_messagecount($mailbox, 'ALL'); |
| | | |
| | | if (!$msg_count) { |
| | | return 0; |
| | | // SELECT will set messages count for clearFolder() |
| | | if ($this->conn->select($mailbox)) { |
| | | $cleared = $this->conn->clearFolder($mailbox); |
| | | } |
| | | |
| | | $cleared = $this->conn->clearFolder($mailbox); |
| | | |
| | | // make sure the message count cache is cleared as well |
| | | if ($cleared) { |
| | |
| | | |
| | | $result = $this->conn->expunge($mailbox, $a_uids); |
| | | |
| | | if ($result>=0 && $clear_cache) { |
| | | if ($result && $clear_cache) { |
| | | $this->clear_message_cache($mailbox.'.msg'); |
| | | $this->_clear_messagecount($mailbox); |
| | | } |
| | |
| | | $a_folders = $data['folders']; |
| | | } |
| | | else { |
| | | // retrieve list of folders from IMAP server |
| | | $a_folders = $this->conn->listSubscribed($this->mod_mailbox($root), $filter); |
| | | // Server supports LIST-EXTENDED, we can use selection options |
| | | if ($this->get_capability('LIST-EXTENDED')) { |
| | | // This will also set mailbox options, LSUB doesn't do that |
| | | $a_folders = $this->conn->listMailboxes($this->mod_mailbox($root), $filter, |
| | | NULL, array('SUBSCRIBED')); |
| | | |
| | | // remove non-existent folders |
| | | if (is_array($a_folders)) { |
| | | foreach ($a_folders as $idx => $folder) { |
| | | if ($this->conn->data['LIST'] && ($opts = $this->conn->data['LIST'][$folder]) |
| | | && in_array('\\NonExistent', $opts) |
| | | ) { |
| | | unset($a_folders[$idx]); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | // retrieve list of folders from IMAP server using LSUB |
| | | else { |
| | | $a_folders = $this->conn->listSubscribed($this->mod_mailbox($root), $filter); |
| | | } |
| | | } |
| | | |
| | | if (!is_array($a_folders) || !sizeof($a_folders)) |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Gets folder options from LIST response, e.g. \Noselect, \Noinferiors |
| | | * |
| | | * @param string $mbox_name Folder name |
| | | * @param bool $force Set to True if options should be refreshed |
| | | * Options are available after LIST command only |
| | | * |
| | | * @return array Options list |
| | | */ |
| | | function mailbox_options($mbox_name, $force=false) |
| | | { |
| | | $mbox = $this->mod_mailbox($mbox_name); |
| | | |
| | | if ($mbox == 'INBOX') { |
| | | return array(); |
| | | } |
| | | |
| | | if (!is_array($this->conn->data['LIST']) || !is_array($this->conn->data['LIST'][$mbox])) { |
| | | if ($force) { |
| | | $this->conn->listMailboxes($this->mod_mailbox(''), $mbox_name); |
| | | } |
| | | else { |
| | | return array(); |
| | | } |
| | | } |
| | | |
| | | $opts = $this->conn->data['LIST'][$mbox]; |
| | | |
| | | return is_array($opts) ? $opts : array(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Get message header names for rcube_imap_generic::fetchHeader(s) |
| | | * |
| | | * @return string Space-separated list of header names |
| | | */ |
| | | private function get_fetch_headers() |
| | | { |
| | | $headers = explode(' ', $this->fetch_add_headers); |
| | | $headers = array_map('strtoupper', $headers); |
| | | |
| | | if ($this->caching_enabled || $this->get_all_headers) |
| | | $headers = array_merge($headers, $this->all_headers); |
| | | |
| | | return implode(' ', array_unique($headers)); |
| | | } |
| | | |
| | | |
| | | /* ----------------------------------------- |
| | | * ACL and METADATA/ANNOTATEMORE methods |
| | | * ----------------------------------------*/ |
| | | |
| | | /** |
| | | * Changes the ACL on the specified mailbox (SETACL) |
| | | * |
| | | * @param string $mailbox Mailbox name |
| | | * @param string $user User name |
| | | * @param string $acl ACL string |
| | | * |
| | | * @return boolean True on success, False on failure |
| | | * |
| | | * @access public |
| | | * @since 0.5-beta |
| | | */ |
| | | function set_acl($mailbox, $user, $acl) |
| | | { |
| | | $mailbox = $this->mod_mailbox($mailbox); |
| | | |
| | | if ($this->get_capability('ACL')) |
| | | return $this->conn->setACL($mailbox, $user, $acl); |
| | | |
| | | return false; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Removes any <identifier,rights> pair for the |
| | | * specified user from the ACL for the specified |
| | | * mailbox (DELETEACL) |
| | | * |
| | | * @param string $mailbox Mailbox name |
| | | * @param string $user User name |
| | | * |
| | | * @return boolean True on success, False on failure |
| | | * |
| | | * @access public |
| | | * @since 0.5-beta |
| | | */ |
| | | function delete_acl($mailbox, $user) |
| | | { |
| | | $mailbox = $this->mod_mailbox($mailbox); |
| | | |
| | | if ($this->get_capability('ACL')) |
| | | return $this->conn->deleteACL($mailbox, $user); |
| | | |
| | | return false; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Returns the access control list for mailbox (GETACL) |
| | | * |
| | | * @param string $mailbox Mailbox name |
| | | * |
| | | * @return array User-rights array on success, NULL on error |
| | | * @access public |
| | | * @since 0.5-beta |
| | | */ |
| | | function get_acl($mailbox) |
| | | { |
| | | $mailbox = $this->mod_mailbox($mailbox); |
| | | |
| | | if ($this->get_capability('ACL')) |
| | | return $this->conn->getACL($mailbox); |
| | | |
| | | return NULL; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Returns information about what rights can be granted to the |
| | | * user (identifier) in the ACL for the mailbox (LISTRIGHTS) |
| | | * |
| | | * @param string $mailbox Mailbox name |
| | | * @param string $user User name |
| | | * |
| | | * @return array List of user rights |
| | | * @access public |
| | | * @since 0.5-beta |
| | | */ |
| | | function list_rights($mailbox, $user) |
| | | { |
| | | $mailbox = $this->mod_mailbox($mailbox); |
| | | |
| | | if ($this->get_capability('ACL')) |
| | | return $this->conn->listRights($mailbox, $user); |
| | | |
| | | return NULL; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Returns the set of rights that the current user has to |
| | | * mailbox (MYRIGHTS) |
| | | * |
| | | * @param string $mailbox Mailbox name |
| | | * |
| | | * @return array MYRIGHTS response on success, NULL on error |
| | | * @access public |
| | | * @since 0.5-beta |
| | | */ |
| | | function my_rights($mailbox) |
| | | { |
| | | $mailbox = $this->mod_mailbox($mailbox); |
| | | |
| | | if ($this->get_capability('ACL')) |
| | | return $this->conn->myRights($mailbox); |
| | | |
| | | return NULL; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Sets IMAP metadata/annotations (SETMETADATA/SETANNOTATION) |
| | | * |
| | | * @param string $mailbox Mailbox name (empty for server metadata) |
| | | * @param array $entries Entry-value array (use NULL value as NIL) |
| | | * |
| | | * @return boolean True on success, False on failure |
| | | * @access public |
| | | * @since 0.5-beta |
| | | */ |
| | | function set_metadata($mailbox, $entries) |
| | | { |
| | | if ($mailbox) |
| | | $mailbox = $this->mod_mailbox($mailbox); |
| | | |
| | | if ($this->get_capability('METADATA') || |
| | | empty($mailbox) && $this->get_capability('METADATA-SERVER') |
| | | ) { |
| | | return $this->conn->setMetadata($mailbox, $entries); |
| | | } |
| | | else if ($this->get_capability('ANNOTATEMORE') || $this->get_capability('ANNOTATEMORE2')) { |
| | | foreach ($entries as $entry => $value) { |
| | | list($ent, $attr) = $this->md2annotate($entry); |
| | | $entries[$entry] = array($ent, $attr, $value); |
| | | } |
| | | return $this->conn->setAnnotation($mailbox, $entries); |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Unsets IMAP metadata/annotations (SETMETADATA/SETANNOTATION) |
| | | * |
| | | * @param string $mailbox Mailbox name (empty for server metadata) |
| | | * @param array $entries Entry names array |
| | | * |
| | | * @return boolean True on success, False on failure |
| | | * |
| | | * @access public |
| | | * @since 0.5-beta |
| | | */ |
| | | function delete_metadata($mailbox, $entries) |
| | | { |
| | | if ($mailbox) |
| | | $mailbox = $this->mod_mailbox($mailbox); |
| | | |
| | | if ($this->get_capability('METADATA') || |
| | | empty($mailbox) && $this->get_capability('METADATA-SERVER') |
| | | ) { |
| | | return $this->conn->deleteMetadata($mailbox, $entries); |
| | | } |
| | | else if ($this->get_capability('ANNOTATEMORE') || $this->get_capability('ANNOTATEMORE2')) { |
| | | foreach ($entries as $idx => $entry) { |
| | | list($ent, $attr) = $this->md2annotate($entry); |
| | | $entries[$idx] = array($ent, $attr, NULL); |
| | | } |
| | | return $this->conn->setAnnotation($mailbox, $entries); |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Returns IMAP metadata/annotations (GETMETADATA/GETANNOTATION) |
| | | * |
| | | * @param string $mailbox Mailbox name (empty for server metadata) |
| | | * @param array $entries Entries |
| | | * @param array $options Command options (with MAXSIZE and DEPTH keys) |
| | | * |
| | | * @return array Metadata entry-value hash array on success, NULL on error |
| | | * |
| | | * @access public |
| | | * @since 0.5-beta |
| | | */ |
| | | function get_metadata($mailbox, $entries, $options=array()) |
| | | { |
| | | if ($mailbox) |
| | | $mailbox = $this->mod_mailbox($mailbox); |
| | | |
| | | if ($this->get_capability('METADATA') || |
| | | empty($mailbox) && $this->get_capability('METADATA-SERVER') |
| | | ) { |
| | | return $this->conn->getMetadata($mailbox, $entries, $options); |
| | | } |
| | | else if ($this->get_capability('ANNOTATEMORE') || $this->get_capability('ANNOTATEMORE2')) { |
| | | $queries = array(); |
| | | $res = array(); |
| | | |
| | | // Convert entry names |
| | | foreach ($entries as $entry) { |
| | | list($ent, $attr) = $this->md2annotate($entry); |
| | | $queries[$attr][] = $ent; |
| | | } |
| | | |
| | | // @TODO: Honor MAXSIZE and DEPTH options |
| | | foreach ($queries as $attrib => $entry) |
| | | if ($result = $this->conn->getAnnotation($mailbox, $entry, $attrib)) |
| | | $res = array_merge($res, $result); |
| | | |
| | | return $res; |
| | | } |
| | | |
| | | return NULL; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Converts the METADATA extension entry name into the correct |
| | | * entry-attrib names for older ANNOTATEMORE version. |
| | | * |
| | | * @param string Entry name |
| | | * |
| | | * @return array Entry-attribute list, NULL if not supported (?) |
| | | */ |
| | | private function md2annotate($name) |
| | | { |
| | | if (substr($entry, 0, 7) == '/shared') { |
| | | return array(substr($entry, 7), 'value.shared'); |
| | | } |
| | | else if (substr($entry, 0, 8) == '/private') { |
| | | return array(substr($entry, 8), 'value.priv'); |
| | | } |
| | | |
| | | // @TODO: log error |
| | | return NULL; |
| | | } |
| | | |
| | | |
| | | /* -------------------------------- |
| | | * internal caching methods |
| | | * --------------------------------*/ |
| | |
| | | $this->caching_enabled = false; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Returns cached value |
| | | * |
| | |
| | | return $this->cache[$key]; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Update cache |
| | | * |
| | |
| | | $this->cache_changed = true; |
| | | $this->cache_changes[$key] = true; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Writes the cache |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Clears the cache. |
| | |
| | | unset($this->cache[$key]); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Returns cached entry |
| | |
| | | |
| | | return $this->cache[$key]; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Writes single cache record |
| | |
| | | $this->cache_keys[$key] = $sql_arr['cache_id']; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Clears cache for single record |
| | |
| | | return -2; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @param string $key Cache key |
| | | * @param string $from |
| | |
| | | // featch headers if unserialize failed |
| | | if (empty($result[$uid])) |
| | | $result[$uid] = $this->conn->fetchHeader( |
| | | preg_replace('/.msg$/', '', $key), $uid, true, false, $this->fetch_add_headers); |
| | | preg_replace('/.msg$/', '', $key), $uid, true, false, $this->get_fetch_headers()); |
| | | } |
| | | |
| | | return $result; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @param string $key Cache key |
| | | * @param int $uid User id |
| | | * @param int $uid Message UID |
| | | * @return mixed |
| | | * @access private |
| | | */ |
| | |
| | | return $this->icache[$internal_key][$uid]; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @param string $key Cache key |
| | | * @param boolean $force Force flag |
| | | * @param string $sort_field |
| | | * @param string $sort_order |
| | | * @param string $key Cache key |
| | | * @param string $sort_field Sorting column |
| | | * @param string $sort_order Sorting order |
| | | * @return array Messages index |
| | | * @access private |
| | | */ |
| | | private function get_message_cache_index($key, $sort_field='idx', $sort_order='ASC') |
| | |
| | | if (!$sort_field || !in_array($sort_field, $this->db_header_fields)) |
| | | $sort_field = 'idx'; |
| | | |
| | | $ord = $sort_field . $sort_order; |
| | | |
| | | if (array_key_exists('index', $this->icache) |
| | | && $this->icache['index']['key'] == $key |
| | | && $this->icache['index']['ord'] == $ord |
| | | && $this->icache['index']['sort_field'] == $sort_field |
| | | ) { |
| | | return $this->icache['index']['result']; |
| | | if ($this->icache['index']['sort_order'] == $sort_order) |
| | | return $this->icache['index']['result']; |
| | | else |
| | | return array_reverse($this->icache['index']['result'], true); |
| | | } |
| | | |
| | | $this->icache['index'] = array( |
| | | 'result' => array(), |
| | | 'ord' => $ord, |
| | | 'key' => $key, |
| | | 'result' => array(), |
| | | 'key' => $key, |
| | | 'sort_field' => $sort_field, |
| | | 'sort_order' => $sort_order, |
| | | ); |
| | | |
| | | $sql_result = $this->db->query( |
| | |
| | | |
| | | return $this->icache['index']['result']; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @access private |
| | |
| | | unset($this->icache['index']); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @access private |
| | | */ |
| | |
| | | |
| | | unset($this->icache['index']); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @param string $key Cache key |
| | |
| | | |
| | | unset($this->icache['index']); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @access private |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @param string $key Cache key |
| | | * @param int $id Message (sequence) ID |
| | | * @return int Message UID |
| | | * @access private |
| | | */ |
| | | private function get_cache_id2uid($key, $id) |
| | | { |
| | | if (!$this->caching_enabled) |
| | | return null; |
| | | |
| | | if (array_key_exists('index', $this->icache) |
| | | && $this->icache['index']['key'] == $key |
| | | ) { |
| | | return $this->icache['index']['result'][$id]; |
| | | } |
| | | |
| | | $sql_result = $this->db->query( |
| | | "SELECT uid". |
| | | " FROM ".get_table_name('messages'). |
| | | " WHERE user_id=?". |
| | | " AND cache_key=?". |
| | | " AND idx=?", |
| | | $_SESSION['user_id'], $key, $id); |
| | | |
| | | if ($sql_arr = $this->db->fetch_assoc($sql_result)) |
| | | return intval($sql_arr['uid']); |
| | | |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @param string $key Cache key |
| | | * @param int $uid Message UID |
| | | * @return int Message (sequence) ID |
| | | * @access private |
| | | */ |
| | | private function get_cache_uid2id($key, $uid) |
| | | { |
| | | if (!$this->caching_enabled) |
| | | return null; |
| | | |
| | | if (array_key_exists('index', $this->icache) |
| | | && $this->icache['index']['key'] == $key |
| | | ) { |
| | | return array_search($uid, $this->icache['index']['result']); |
| | | } |
| | | |
| | | $sql_result = $this->db->query( |
| | | "SELECT idx". |
| | | " FROM ".get_table_name('messages'). |
| | | " WHERE user_id=?". |
| | | " AND cache_key=?". |
| | | " AND uid=?", |
| | | $_SESSION['user_id'], $key, $uid); |
| | | |
| | | if ($sql_arr = $this->db->fetch_assoc($sql_result)) |
| | | return intval($sql_arr['idx']); |
| | | |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /* -------------------------------- |
| | | * encoding/decoding methods |
| | | * --------------------------------*/ |
| | |
| | | foreach ($a as $val) { |
| | | $j++; |
| | | $address = trim($val['address']); |
| | | $name = trim($val['name']); |
| | | |
| | | if ($name && preg_match('/^[\'"]/', $name) && preg_match('/[\'"]$/', $name)) |
| | | $name = trim($name, '\'"'); |
| | | $name = trim($val['name']); |
| | | |
| | | if ($name && $address && $name != $address) |
| | | $string = sprintf('%s <%s>', preg_match("/$special_chars/", $name) ? '"'.addcslashes($name, '"').'"' : $name, $address); |
| | |
| | | $this->sort_order = strtoupper($sort_order) == 'DESC' ? 'DESC' : 'ASC'; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Sort mailboxes first by default folders and then in alphabethical order |
| | | * |
| | |
| | | |
| | | |
| | | /** |
| | | * @param int $uid User id |
| | | * @param int $uid Message UID |
| | | * @param string $mbox_name Mailbox name |
| | | * @return int |
| | | * @return int Message (sequence) ID |
| | | * @access private |
| | | */ |
| | | private function _uid2id($uid, $mbox_name=NULL) |
| | |
| | | if (!$mbox_name) |
| | | $mbox_name = $this->mailbox; |
| | | |
| | | if (!isset($this->uid_id_map[$mbox_name][$uid])) |
| | | $this->uid_id_map[$mbox_name][$uid] = $this->conn->UID2ID($mbox_name, $uid); |
| | | if (!isset($this->uid_id_map[$mbox_name][$uid])) { |
| | | if (!($id = $this->get_cache_uid2id($mbox_name.'.msg', $uid))) |
| | | $id = $this->conn->UID2ID($mbox_name, $uid); |
| | | |
| | | $this->uid_id_map[$mbox_name][$uid] = $id; |
| | | } |
| | | |
| | | return $this->uid_id_map[$mbox_name][$uid]; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @param int $id Id |
| | | * @param int $id Message (sequence) ID |
| | | * @param string $mbox_name Mailbox name |
| | | * @return int |
| | | * @return int Message UID |
| | | * @access private |
| | | */ |
| | | private function _id2uid($id, $mbox_name=NULL) |
| | |
| | | if ($uid = array_search($id, (array)$this->uid_id_map[$mbox_name])) |
| | | return $uid; |
| | | |
| | | $uid = $this->conn->ID2UID($mbox_name, $id); |
| | | if (!($uid = $this->get_cache_id2uid($mbox_name.'.msg', $id))) |
| | | $uid = $this->conn->ID2UID($mbox_name, $id); |
| | | |
| | | $this->uid_id_map[$mbox_name][$uid] = $id; |
| | | |
| | | return $uid; |
| | |
| | | $result = array(); |
| | | |
| | | foreach ($a as $key => $val) { |
| | | $val = preg_replace("/([\"\w])</", "$1 <", $val); |
| | | $sub_a = rcube_explode_quoted_string(' ', $decode ? $this->decode_header($val) : $val); |
| | | $result[$key]['name'] = ''; |
| | | $name = ''; |
| | | $address = ''; |
| | | $val = trim($val); |
| | | |
| | | foreach ($sub_a as $k => $v) { |
| | | // use angle brackets in regexp to not handle names with @ sign |
| | | if (preg_match('/^<\S+@\S+>$/', $v)) |
| | | $result[$key]['address'] = trim($v, '<>'); |
| | | else |
| | | $result[$key]['name'] .= (empty($result[$key]['name'])?'':' ').str_replace("\"",'',stripslashes($v)); |
| | | if (preg_match('/(.*)<(\S+@\S+)>$/', $val, $m)) { |
| | | $address = $m[2]; |
| | | $name = trim($m[1]); |
| | | } |
| | | else if (preg_match('/^(\S+@\S+)$/', $val, $m)) { |
| | | $address = $m[1]; |
| | | $name = ''; |
| | | } |
| | | else { |
| | | $name = $val; |
| | | } |
| | | |
| | | // if (empty($result[$key]['name'])) |
| | | // $result[$key]['name'] = $result[$key]['address']; |
| | | if (empty($result[$key]['address'])) |
| | | $result[$key]['address'] = $result[$key]['name']; |
| | | // dequote and/or decode name |
| | | if ($name) { |
| | | if ($name[0] == '"') { |
| | | $name = substr($name, 1, -1); |
| | | $name = stripslashes($name); |
| | | } |
| | | if ($decode) { |
| | | $name = $this->decode_header($name); |
| | | } |
| | | } |
| | | |
| | | if (!$address && $name) { |
| | | $address = $name; |
| | | } |
| | | |
| | | if ($address) { |
| | | $result[$key] = array('name' => $name, 'address' => $address); |
| | | } |
| | | } |
| | | |
| | | return $result; |