Exemplo n.º 1
0
 /**
  * Reads cache entry.
  *
  * @param string  $key     Cache key name
  * @param boolean $nostore Enable to skip in-memory store
  *
  * @return mixed Cached value
  */
 private function read_record($key, $nostore = false)
 {
     if (!$this->db) {
         return null;
     }
     if ($this->type != 'db') {
         $this->load_index();
         // Consistency check (#1490390)
         if (!in_array($key, $this->index)) {
             // we always check if the key exist in the index
             // to have data in consistent state. Keeping the index consistent
             // is needed for keys delete operation when we delete all keys or by prefix.
         } else {
             $ckey = $this->ckey($key);
             if ($this->type == 'memcache') {
                 $data = $this->db->get($ckey);
             } else {
                 if ($this->type == 'apc') {
                     $data = apc_fetch($ckey);
                 }
             }
             if ($this->debug) {
                 $this->debug('get', $ckey, $data);
             }
         }
         if ($data !== false) {
             $md5sum = md5($data);
             $data = $this->unserialize($data);
             if ($nostore) {
                 return $data;
             }
             $this->cache_sums[$key] = $md5sum;
             $this->cache[$key] = $data;
         } else {
             $this->cache[$key] = null;
         }
     } else {
         $sql_result = $this->db->query("SELECT `data`, `cache_key` FROM {$this->table}" . " WHERE `cache_key` = ?", $this->prefix . '.' . $key);
         if ($sql_arr = $this->db->fetch_assoc($sql_result)) {
             if (strlen($sql_arr['data']) > 0) {
                 $md5sum = md5($sql_arr['data']);
                 $data = $this->unserialize($sql_arr['data']);
             }
             $this->db->reset();
             if ($nostore) {
                 return $data;
             }
             $this->cache[$key] = $data;
             $this->cache_sums[$key] = $md5sum;
         } else {
             $this->cache[$key] = null;
         }
     }
     return $this->cache[$key];
 }
Exemplo n.º 2
0
 /**
  * Return saved search data.
  *
  * @param int  $id  Row identifier
  *
  * @return array Data
  */
 function get_search($id)
 {
     $plugin = $this->rc->plugins->exec_hook('saved_search_get', array('id' => $id));
     if ($plugin['abort']) {
         return $plugin['result'];
     }
     $sql_result = $this->db->query("SELECT " . $this->db->quote_identifier('name') . ", " . $this->db->quote_identifier('data') . ", " . $this->db->quote_identifier('type') . " FROM " . $this->db->table_name('searches') . " WHERE user_id = ?" . " AND search_id = ?", (int) $this->ID, (int) $id);
     while ($sql_arr = $this->db->fetch_assoc($sql_result)) {
         return array('id' => $id, 'name' => $sql_arr['name'], 'type' => $sql_arr['type'], 'data' => unserialize($sql_arr['data']));
     }
     return null;
 }
Exemplo n.º 3
0
 /**
  * Reads cache entry.
  *
  * @param string  $key     Cache key name
  * @param boolean $nostore Enable to skip in-memory store
  *
  * @return mixed Cached value
  */
 private function read_record($key, $nostore = false)
 {
     if (!$this->db) {
         return null;
     }
     if ($this->type != 'db') {
         $this->load_index();
         // Consistency check (#1490390)
         if (!in_array($key, $this->index)) {
             // we always check if the key exist in the index
             // to have data in consistent state. Keeping the index consistent
             // is needed for keys delete operation when we delete all keys or by prefix.
         } else {
             if ($this->type == 'memcache') {
                 $data = $this->db->get($this->ckey($key));
             } else {
                 if ($this->type == 'apc') {
                     $data = apc_fetch($this->ckey($key));
                 }
             }
         }
         if ($data) {
             $md5sum = md5($data);
             $data = $this->unserialize($data);
             if ($nostore) {
                 return $data;
             }
             $this->cache_sums[$key] = $md5sum;
             $this->cache[$key] = $data;
         } else {
             $this->cache[$key] = null;
         }
     } else {
         $sql_result = $this->db->limitquery("SELECT `data`, `cache_key`" . " FROM {$this->table}" . " WHERE `user_id` = ? AND `cache_key` = ?" . " ORDER BY `created` DESC", 0, 1, $this->userid, $this->prefix . '.' . $key);
         if ($sql_arr = $this->db->fetch_assoc($sql_result)) {
             $key = substr($sql_arr['cache_key'], strlen($this->prefix) + 1);
             $md5sum = $sql_arr['data'] ? md5($sql_arr['data']) : null;
             if ($sql_arr['data']) {
                 $data = $this->unserialize($sql_arr['data']);
             }
             if ($nostore) {
                 return $data;
             }
             $this->cache[$key] = $data;
             $this->cache_sums[$key] = $md5sum;
         } else {
             $this->cache[$key] = null;
         }
     }
     return $this->cache[$key];
 }
Exemplo n.º 4
0
 /**
  * Reads cache entry.
  *
  * @param string  $key     Cache key name
  * @param boolean $nostore Enable to skip in-memory store
  *
  * @return mixed Cached value
  */
 private function read_record($key, $nostore = false)
 {
     if (!$this->db) {
         return null;
     }
     if ($this->type != 'db') {
         if ($this->type == 'memcache') {
             $data = $this->db->get($this->ckey($key));
         } else {
             if ($this->type == 'apc') {
                 $data = apc_fetch($this->ckey($key));
             }
         }
         if ($data) {
             $md5sum = md5($data);
             $data = $this->unserialize($data);
             if ($nostore) {
                 return $data;
             }
             $this->cache_sums[$key] = $md5sum;
             $this->cache[$key] = $data;
         } else {
             $this->cache[$key] = null;
         }
     } else {
         $sql_result = $this->db->limitquery("SELECT data, cache_key" . " FROM " . $this->table . " WHERE user_id = ?" . " AND cache_key = ?" . " ORDER BY created DESC", 0, 1, $this->userid, $this->prefix . '.' . $key);
         if ($sql_arr = $this->db->fetch_assoc($sql_result)) {
             $key = substr($sql_arr['cache_key'], strlen($this->prefix) + 1);
             $md5sum = $sql_arr['data'] ? md5($sql_arr['data']) : null;
             if ($sql_arr['data']) {
                 $data = $this->unserialize($sql_arr['data']);
             }
             if ($nostore) {
                 return $data;
             }
             $this->cache[$key] = $data;
             $this->cache_sums[$key] = $md5sum;
         } else {
             $this->cache[$key] = null;
         }
     }
     return $this->cache[$key];
 }
 /**
  * Reads cache entry.
  *
  * @param string  $key     Cache key name
  * @param boolean $nostore Enable to skip in-memory store
  *
  * @return mixed Cached value
  */
 private function read_record($key, $nostore = false)
 {
     if (!$this->db) {
         return null;
     }
     if ($this->type != 'db') {
         if ($this->type == 'memcache') {
             $data = $this->db->get($this->ckey($key));
         } else {
             if ($this->type == 'apc') {
                 $data = apc_fetch($this->ckey($key));
             }
         }
         if ($data) {
             $md5sum = md5($data);
             $data = $this->unserialize($data);
             if ($nostore) {
                 return $data;
             }
             $this->cache_sums[$key] = $md5sum;
             $this->cache[$key] = $data;
         } else {
             $this->cache[$key] = null;
         }
     } else {
         $sql_result = $this->db->limitquery("SELECT `data`, `cache_key`" . " FROM {$this->table}" . " WHERE `cache_key` = ?" . " ORDER BY `created` DESC", 0, 1, $this->prefix . '.' . $key);
         if ($sql_arr = $this->db->fetch_assoc($sql_result)) {
             $md5sum = $sql_arr['data'] ? md5($sql_arr['data']) : null;
             if ($sql_arr['data']) {
                 $data = $this->unserialize($sql_arr['data']);
             }
             if ($nostore) {
                 return $data;
             }
             $this->cache[$key] = $data;
             $this->cache_sums[$key] = $md5sum;
         } else {
             $this->cache[$key] = null;
         }
     }
     return $this->cache[$key];
 }
Exemplo n.º 6
0
function hmail_db_connect()
{
    $rcmail = rcube::get_instance();
    if ($dsn = $rcmail->config->get('companyaddressbook_db_dsnw')) {
        $db = new rcube_db($dsn, '', FALSE);
        $db->set_debug((bool) $rcmail->config->get('sql_debug'));
        $db->db_connect('w');
        $sql = 'SELECT * FROM hm_dbversion LIMIT 1';
        $result = $db->query($sql);
        if ($db->error) {
            return false;
        }
        $v = $db->fetch_assoc($result);
        if ($v['value'] >= HMAIL_DB_VERSION_MIN && $v['value'] <= HMAIL_DB_VERSION_MAX) {
            return $db;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
 /**
  * Add the given contact records the a certain group
  *
  * @param string  Group identifier
  * @param array   List of contact identifiers to be added
  * @return int    Number of contacts added 
  */
 function add_to_group($group_id, $ids)
 {
     if (!is_array($ids)) {
         $ids = explode(self::SEPARATOR, $ids);
     }
     $added = 0;
     $exists = array();
     // get existing assignments ...
     $sql_result = $this->db->query("SELECT contact_id FROM " . $this->db->table_name($this->db_groupmembers) . " WHERE contactgroup_id=?" . " AND contact_id IN (" . $this->db->array2list($ids, 'integer') . ")", $group_id);
     while ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result))) {
         $exists[] = $sql_arr['contact_id'];
     }
     // ... and remove them from the list
     $ids = array_diff($ids, $exists);
     foreach ($ids as $contact_id) {
         $this->db->query("INSERT INTO " . $this->db->table_name($this->db_groupmembers) . " (contactgroup_id, contact_id, created)" . " VALUES (?, ?, " . $this->db->now() . ")", $group_id, $contact_id);
         if ($error = $this->db->is_error()) {
             $this->set_error(self::ERROR_SAVING, $error);
         } else {
             $added++;
         }
     }
     return $added;
 }
 /**
  * Synchronizes the mailbox.
  *
  * @param string $mailbox Folder name
  */
 function synchronize($mailbox)
 {
     // RFC4549: Synchronization Operations for Disconnected IMAP4 Clients
     // RFC4551: IMAP Extension for Conditional STORE Operation
     //          or Quick Flag Changes Resynchronization
     // RFC5162: IMAP Extensions for Quick Mailbox Resynchronization
     // @TODO: synchronize with other methods?
     $qresync = $this->imap->get_capability('QRESYNC');
     $condstore = $qresync ? true : $this->imap->get_capability('CONDSTORE');
     if (!$qresync && !$condstore) {
         return;
     }
     // Get stored index
     $index = $this->get_index_row($mailbox);
     // database is empty
     if (empty($index)) {
         // set the flag that DB was already queried for index
         // this way we'll be able to skip one SELECT in get_index()
         $this->icache[$mailbox]['index_queried'] = true;
         return;
     }
     $this->icache[$mailbox]['index'] = $index;
     // no last HIGHESTMODSEQ value
     if (empty($index['modseq'])) {
         return;
     }
     if (!$this->imap->check_connection()) {
         return;
     }
     // Enable QRESYNC
     $res = $this->imap->conn->enable($qresync ? 'QRESYNC' : 'CONDSTORE');
     if ($res === false) {
         return;
     }
     // Close mailbox if already selected to get most recent data
     if ($this->imap->conn->selected == $mailbox) {
         $this->imap->conn->close();
     }
     // Get mailbox data (UIDVALIDITY, HIGHESTMODSEQ, counters, etc.)
     $mbox_data = $this->imap->folder_data($mailbox);
     if (empty($mbox_data)) {
         return;
     }
     // Check UIDVALIDITY
     if ($index['validity'] != $mbox_data['UIDVALIDITY']) {
         $this->clear($mailbox);
         return;
     }
     // QRESYNC not supported on specified mailbox
     if (!empty($mbox_data['NOMODSEQ']) || empty($mbox_data['HIGHESTMODSEQ'])) {
         return;
     }
     // Nothing new
     if ($mbox_data['HIGHESTMODSEQ'] == $index['modseq']) {
         return;
     }
     $uids = array();
     $removed = array();
     // Get known UIDs
     if ($this->mode & self::MODE_MESSAGE) {
         $sql_result = $this->db->query("SELECT `uid`" . " FROM {$this->messages_table}" . " WHERE `user_id` = ?" . " AND `mailbox` = ?", $this->userid, $mailbox);
         while ($sql_arr = $this->db->fetch_assoc($sql_result)) {
             $uids[] = $sql_arr['uid'];
         }
     }
     // Synchronize messages data
     if (!empty($uids)) {
         // Get modified flags and vanished messages
         // UID FETCH 1:* (FLAGS) (CHANGEDSINCE 0123456789 VANISHED)
         $result = $this->imap->conn->fetch($mailbox, $uids, true, array('FLAGS'), $index['modseq'], $qresync);
         if (!empty($result)) {
             foreach ($result as $msg) {
                 $uid = $msg->uid;
                 // Remove deleted message
                 if ($this->skip_deleted && !empty($msg->flags['DELETED'])) {
                     $removed[] = $uid;
                     // Invalidate index
                     $index['valid'] = false;
                     continue;
                 }
                 $flags = 0;
                 if (!empty($msg->flags)) {
                     foreach ($this->flags as $idx => $flag) {
                         if (!empty($msg->flags[$flag])) {
                             $flags += $idx;
                         }
                     }
                 }
                 $this->db->query("UPDATE {$this->messages_table}" . " SET `flags` = ?, `expires` = " . ($this->ttl ? $this->db->now($this->ttl) : 'NULL') . " WHERE `user_id` = ?" . " AND `mailbox` = ?" . " AND `uid` = ?" . " AND `flags` <> ?", $flags, $this->userid, $mailbox, $uid, $flags);
             }
         }
         // VANISHED found?
         if ($qresync) {
             $mbox_data = $this->imap->folder_data($mailbox);
             // Removed messages found
             $uids = rcube_imap_generic::uncompressMessageSet($mbox_data['VANISHED']);
             if (!empty($uids)) {
                 $removed = array_merge($removed, $uids);
                 // Invalidate index
                 $index['valid'] = false;
             }
         }
         // remove messages from database
         if (!empty($removed)) {
             $this->remove_message($mailbox, $removed);
         }
     }
     $sort_field = $index['sort_field'];
     $sort_order = $index['object']->get_parameters('ORDER');
     $exists = true;
     // Validate index
     if (!$this->validate($mailbox, $index, $exists)) {
         // Invalidate (remove) thread index
         // if $exists=false it was already removed in validate()
         if ($exists) {
             $this->remove_thread($mailbox);
         }
         // Update index
         $data = $this->get_index_data($mailbox, $sort_field, $sort_order, $mbox_data);
     } else {
         $data = $index['object'];
     }
     // update index and/or HIGHESTMODSEQ value
     $this->add_index_row($mailbox, $sort_field, $data, $mbox_data, $exists);
     // update internal cache for get_index()
     $this->icache[$mailbox]['index']['object'] = $data;
 }