| | |
| | | */ |
| | | class rcube_imap_cache |
| | | { |
| | | const MODE_INDEX = 1; |
| | | const MODE_MESSAGE = 2; |
| | | |
| | | /** |
| | | * Instance of rcube_imap |
| | | * |
| | |
| | | private $icache = array(); |
| | | |
| | | private $skip_deleted = false; |
| | | private $mode; |
| | | |
| | | /** |
| | | * List of known flags. Thanks to this we can handle flag changes |
| | |
| | | ); |
| | | |
| | | |
| | | |
| | | /** |
| | | * Object constructor. |
| | | * |
| | |
| | | $this->skip_deleted = $skip_deleted; |
| | | $this->ttl = $ttl; |
| | | $this->threshold = $threshold; |
| | | |
| | | // cache all possible information by default |
| | | $this->mode = self::MODE_INDEX | self::MODE_MESSAGE; |
| | | } |
| | | |
| | | |
| | |
| | | { |
| | | $this->save_icache(); |
| | | $this->icache = null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Set cache mode |
| | | * |
| | | * @param int $mode Cache mode |
| | | */ |
| | | public function set_mode($mode) |
| | | { |
| | | $this->mode = $mode; |
| | | } |
| | | |
| | | |
| | |
| | | return array(); |
| | | } |
| | | |
| | | $msgs = array_flip($msgs); |
| | | $result = array(); |
| | | |
| | | if ($this->mode & self::MODE_MESSAGE) { |
| | | // Fetch messages from cache |
| | | $sql_result = $this->db->query( |
| | | "SELECT uid, data, flags" |
| | |
| | | ." AND mailbox = ?" |
| | | ." AND uid IN (".$this->db->array2list($msgs, 'integer').")", |
| | | $this->userid, $mailbox); |
| | | |
| | | $msgs = array_flip($msgs); |
| | | $result = array(); |
| | | |
| | | while ($sql_arr = $this->db->fetch_assoc($sql_result)) { |
| | | $uid = intval($sql_arr['uid']); |
| | |
| | | unset($msgs[$uid]); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // Fetch not found messages from IMAP server |
| | | if (!empty($msgs)) { |
| | |
| | | // Insert to DB and add to result list |
| | | if (!empty($messages)) { |
| | | foreach ($messages as $msg) { |
| | | if ($this->mode & self::MODE_MESSAGE) { |
| | | $this->add_message($mailbox, $msg, !array_key_exists($msg->uid, $result)); |
| | | } |
| | | |
| | | $result[$msg->uid] = $msg; |
| | | } |
| | | } |
| | |
| | | return $this->icache['__message']['object']; |
| | | } |
| | | |
| | | if ($this->mode & self::MODE_MESSAGE) { |
| | | $sql_result = $this->db->query( |
| | | "SELECT flags, data" |
| | | ." FROM ".$this->db->table_name('cache_messages') |
| | |
| | | $message = $this->build_message($sql_arr); |
| | | $found = true; |
| | | } |
| | | } |
| | | |
| | | // Get the message from IMAP server |
| | | if (empty($message) && $update) { |
| | | $message = $this->imap->get_message_headers($uid, $mailbox, true); |
| | | // cache will be updated in close(), see below |
| | | } |
| | | |
| | | if (!($this->mode & self::MODE_MESSAGE)) { |
| | | return $message; |
| | | } |
| | | |
| | | // Save the message in internal cache, will be written to DB in close() |
| | |
| | | function add_message($mailbox, $message, $force = false) |
| | | { |
| | | if (!is_object($message) || empty($message->uid)) { |
| | | return; |
| | | } |
| | | |
| | | if (!($this->mode & self::MODE_MESSAGE)) { |
| | | return; |
| | | } |
| | | |
| | |
| | | return; |
| | | } |
| | | |
| | | if (!($this->mode & self::MODE_MESSAGE)) { |
| | | return; |
| | | } |
| | | |
| | | $flag = strtoupper($flag); |
| | | $idx = (int) array_search($flag, $this->flags); |
| | | $uids = (array) $uids; |
| | |
| | | */ |
| | | function remove_message($mailbox = null, $uids = null) |
| | | { |
| | | if (!($this->mode & self::MODE_MESSAGE)) { |
| | | return; |
| | | } |
| | | |
| | | if (!strlen($mailbox)) { |
| | | $this->db->query( |
| | | "DELETE FROM ".$this->db->table_name('cache_messages') |
| | |
| | | $removed = array(); |
| | | |
| | | // Get known UIDs |
| | | if ($this->mode & self::MODE_MESSAGE) { |
| | | $sql_result = $this->db->query( |
| | | "SELECT uid" |
| | | ." FROM ".$this->db->table_name('cache_messages') |
| | |
| | | while ($sql_arr = $this->db->fetch_assoc($sql_result)) { |
| | | $uids[] = $sql_arr['uid']; |
| | | } |
| | | } |
| | | |
| | | // Synchronize messages data |
| | | if (!empty($uids)) { |