- // cache is dirty
- return -1;
- }
-
- // if cache count differs less than 10% report as dirty
- return (abs($msg_count - $cache_count) < $msg_count/10) ? -1 : -2;
- }
-
-
- /**
- * @param string $key Cache key
- * @param string $from
- * @param string $to
- * @param string $sort_field
- * @param string $sort_order
- * @access private
- */
- private function get_message_cache($key, $from, $to, $sort_field, $sort_order)
- {
- if (!$this->caching_enabled)
- return NULL;
-
- // use idx sort as default sorting
- if (!$sort_field || !in_array($sort_field, $this->db_header_fields)) {
- $sort_field = 'idx';
- }
-
- $result = array();
-
- $sql_result = $this->db->limitquery(
- "SELECT idx, uid, headers".
- " FROM ".get_table_name('messages').
- " WHERE user_id=?".
- " AND cache_key=?".
- " ORDER BY ".$this->db->quoteIdentifier($sort_field)." ".strtoupper($sort_order),
- $from,
- $to - $from,
- $_SESSION['user_id'],
- $key);
-
- while ($sql_arr = $this->db->fetch_assoc($sql_result)) {
- $uid = intval($sql_arr['uid']);
- $result[$uid] = $this->db->decode(unserialize($sql_arr['headers']));
-
- // featch headers if unserialize failed
- if (empty($result[$uid]))
- $result[$uid] = $this->conn->fetchHeader(
- preg_replace('/.msg$/', '', $key), $uid, true, false, $this->get_fetch_headers());
- }
-
- return $result;
- }
-
-
- /**
- * @param string $key Cache key
- * @param int $uid Message UID
- * @return mixed
- * @access private
- */
- private function &get_cached_message($key, $uid)
- {
- $internal_key = 'message';
-
- if ($this->caching_enabled && !isset($this->icache[$internal_key][$uid])) {
- $sql_result = $this->db->query(
- "SELECT idx, headers, structure, message_id".
- " 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)) {
- $this->icache['message.id'][$uid] = intval($sql_arr['message_id']);
- $this->uid_id_map[preg_replace('/\.msg$/', '', $key)][$uid] = intval($sql_arr['idx']);
- $this->icache[$internal_key][$uid] = $this->db->decode(unserialize($sql_arr['headers']));
-
- if (is_object($this->icache[$internal_key][$uid]) && !empty($sql_arr['structure']))
- $this->icache[$internal_key][$uid]->structure = $this->db->decode(unserialize($sql_arr['structure']));
- }
- }
-
- return $this->icache[$internal_key][$uid];
- }
-
-
- /**
- * @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 (!$this->caching_enabled || empty($key))
- return NULL;
-
- // use idx sort as default
- if (!$sort_field || !in_array($sort_field, $this->db_header_fields))
- $sort_field = 'idx';
-
- if (array_key_exists('index', $this->icache)
- && $this->icache['index']['key'] == $key
- && $this->icache['index']['sort_field'] == $sort_field
- ) {
- 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(),
- 'key' => $key,
- 'sort_field' => $sort_field,
- 'sort_order' => $sort_order,
- );
-
- $sql_result = $this->db->query(
- "SELECT idx, uid".
- " FROM ".get_table_name('messages').
- " WHERE user_id=?".
- " AND cache_key=?".
- " ORDER BY ".$this->db->quote_identifier($sort_field)." ".$sort_order,
- $_SESSION['user_id'],
- $key);
-
- while ($sql_arr = $this->db->fetch_assoc($sql_result))
- $this->icache['index']['result'][$sql_arr['idx']] = intval($sql_arr['uid']);
-
- return $this->icache['index']['result'];
- }
-
-
- /**
- * @access private
- */
- private function add_message_cache($key, $index, $headers, $struct=null, $force=false, $internal_cache=false)
- {
- if (empty($key) || !is_object($headers) || empty($headers->uid))
- return;
-
- // add to internal (fast) cache
- if ($internal_cache) {
- $this->icache['message'][$headers->uid] = clone $headers;
- $this->icache['message'][$headers->uid]->structure = $struct;
- }
-
- // no further caching
- if (!$this->caching_enabled)
- return;
-
- // known message id
- if (is_int($force) && $force > 0) {
- $message_id = $force;
- }
- // check for an existing record (probably headers are cached but structure not)
- else if (!$force) {
- $sql_result = $this->db->query(
- "SELECT message_id".
- " FROM ".get_table_name('messages').
- " WHERE user_id=?".
- " AND cache_key=?".
- " AND uid=?",
- $_SESSION['user_id'],
- $key,
- $headers->uid);
-
- if ($sql_arr = $this->db->fetch_assoc($sql_result))
- $message_id = $sql_arr['message_id'];
- }
-
- // update cache record
- if ($message_id) {
- $this->db->query(
- "UPDATE ".get_table_name('messages').
- " SET idx=?, headers=?, structure=?".
- " WHERE message_id=?",
- $index,
- serialize($this->db->encode(clone $headers)),
- is_object($struct) ? serialize($this->db->encode(clone $struct)) : NULL,
- $message_id
- );
- }
- else { // insert new record
- $this->db->query(
- "INSERT INTO ".get_table_name('messages').
- " (user_id, del, cache_key, created, idx, uid, subject, ".
- $this->db->quoteIdentifier('from').", ".
- $this->db->quoteIdentifier('to').", ".
- "cc, date, size, headers, structure)".
- " VALUES (?, 0, ?, ".$this->db->now().", ?, ?, ?, ?, ?, ?, ".
- $this->db->fromunixtime($headers->timestamp).", ?, ?, ?)",
- $_SESSION['user_id'],
- $key,
- $index,
- $headers->uid,
- (string)mb_substr($this->db->encode($this->decode_header($headers->subject, true)), 0, 128),
- (string)mb_substr($this->db->encode($this->decode_header($headers->from, true)), 0, 128),
- (string)mb_substr($this->db->encode($this->decode_header($headers->to, true)), 0, 128),
- (string)mb_substr($this->db->encode($this->decode_header($headers->cc, true)), 0, 128),
- (int)$headers->size,
- serialize($this->db->encode(clone $headers)),
- is_object($struct) ? serialize($this->db->encode(clone $struct)) : NULL
- );
- }
-
- unset($this->icache['index']);
- }
-
-
- /**
- * @access private
- */
- private function remove_message_cache($key, $ids, $idx=false)
- {
- if (!$this->caching_enabled)
- return;
-
- $this->db->query(
- "DELETE FROM ".get_table_name('messages').
- " WHERE user_id=?".
- " AND cache_key=?".
- " AND ".($idx ? "idx" : "uid")." IN (".$this->db->array2list($ids, 'integer').")",
- $_SESSION['user_id'],
- $key);
-
- unset($this->icache['index']);
- }
-
-
- /**
- * @param string $key Cache key
- * @param int $start_index Start index
- * @access private
- */
- private function clear_message_cache($key, $start_index=1)
- {
- if (!$this->caching_enabled)
- return;
-
- $this->db->query(
- "DELETE FROM ".get_table_name('messages').
- " WHERE user_id=?".
- " AND cache_key=?".
- " AND idx>=?",
- $_SESSION['user_id'], $key, $start_index);
-
- unset($this->icache['index']);
- }
-
-
- /**
- * @access private
- */
- private function get_message_cache_index_min($key, $uids=NULL)
- {
- if (!$this->caching_enabled)
- return;
-
- if (!empty($uids) && !is_array($uids)) {
- if ($uids == '*' || $uids == '1:*')
- $uids = NULL;
- else
- $uids = explode(',', $uids);