/** * Deletes given saved search record * * @param int $sid Search ID * * @return boolean True if deleted successfully, false if nothing changed */ function delete_search($sid) { if (!$this->ID) { return false; } $this->db->query("DELETE FROM " . $this->db->table_name('searches') . " WHERE user_id = ?" . " AND search_id = ?", (int) $this->ID, $sid); return $this->db->affected_rows(); }
/** * Check for existing groups with the same name * * @param string Name to check * @return string A group name which is unique for the current use */ private function unique_groupname($name) { $checkname = $name; $num = 2; $hit = false; do { $sql_result = $this->db->query("SELECT 1 FROM " . $this->db->table_name($this->db_groups) . " WHERE del<>1" . " AND user_id=?" . " AND name=?", $this->user_id, $checkname); // append number to make name unique if ($hit = $this->db->fetch_array($sql_result)) { $checkname = $name . ' ' . $num++; } } while ($hit); return $checkname; }
/** * Deletes the cache record(s). * * @param string $key Cache key name or pattern * @param boolean $prefix_mode Enable it to clear all keys starting * with prefix specified in $key */ private function remove_record($key = null, $prefix_mode = false) { if (!$this->db) { return; } if ($this->type != 'db') { $this->load_index(); // Remove all keys if ($key === null) { foreach ($this->index as $key) { $this->delete_record($this->ckey($key)); } $this->index = array(); } else { if ($prefix_mode) { foreach ($this->index as $idx => $k) { if (strpos($k, $key) === 0) { $this->delete_record($this->ckey($k)); unset($this->index[$idx]); } } } else { $this->delete_record($this->ckey($key)); if (($idx = array_search($key, $this->index)) !== false) { unset($this->index[$idx]); } } } $this->index_changed = true; return; } // Remove all keys (in specified cache) if ($key === null) { $where = " WHERE `cache_key` LIKE " . $this->db->quote($this->prefix . '.%'); } else { if ($prefix_mode) { $where = " WHERE `cache_key` LIKE " . $this->db->quote($this->prefix . '.' . $key . '%'); } else { $where = " WHERE `cache_key` = " . $this->db->quote($this->prefix . '.' . $key); } } $this->db->query("DELETE FROM " . $this->table . $where); }
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; } }
/** * Deletes the cache record(s). * * @param string $key Cache key name or pattern * @param boolean $prefix_mode Enable it to clear all keys starting * with prefix specified in $key */ private function remove_record($key = null, $prefix_mode = false) { if (!$this->db) { return; } if ($this->type != 'db') { $this->load_index(); // Remove all keys if ($key === null) { foreach ($this->index as $key) { $this->delete_record($key, false); } $this->index = array(); } else { if ($prefix_mode) { foreach ($this->index as $k) { if (strpos($k, $key) === 0) { $this->delete_record($k); } } } else { $this->delete_record($key); } } return; } // Remove all keys (in specified cache) if ($key === null) { $where = " AND cache_key LIKE " . $this->db->quote($this->prefix . '.%'); } else { if ($prefix_mode) { $where = " AND cache_key LIKE " . $this->db->quote($this->prefix . '.' . $key . '%'); } else { $where = " AND cache_key = " . $this->db->quote($this->prefix . '.' . $key); } } $this->db->query("DELETE FROM " . $this->table . " WHERE user_id = ?" . $where, $this->userid); }
/** * 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; }