/** * @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; } 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']); }
/** * 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(',', $ids); } $added = 0; $exists = array(); // get existing assignments ... $sql_result = $this->db->query("SELECT contact_id FROM " . get_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 " . get_table_name($this->db_groupmembers) . " (contactgroup_id, contact_id, created)" . " VALUES (?, ?, " . $this->db->now() . ")", $group_id, $contact_id); if (!$this->db->db_error) { $added++; } } return $added; }
/** * Update user's last_login timestamp */ function touch() { if ($this->ID) { $this->db->query("UPDATE " . $this->db->table_name('users') . " SET last_login = "******" WHERE user_id = ?", $this->ID); } }
/** * 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; } // NOTE: make sure the mailbox isn't selected, before // enabling QRESYNC and invoking SELECT if ($this->imap->conn->selected !== null) { $this->imap->conn->close(); } // Enable QRESYNC $res = $this->imap->conn->enable($qresync ? 'QRESYNC' : 'CONDSTORE'); if (!is_array($res)) { return; } // Get mailbox data (UIDVALIDITY, HIGHESTMODSEQ, counters, etc.) $mbox_data = $this->imap->mailbox_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; } // Get known uids $uids = array(); $sql_result = $this->db->query("SELECT uid" . " FROM " . get_table_name('cache_messages') . " WHERE user_id = ?" . " AND mailbox = ?", $this->userid, $mailbox); while ($sql_arr = $this->db->fetch_assoc($sql_result)) { $uids[] = $sql_arr['uid']; } // No messages in database, nothing to sync if (empty($uids)) { return; } // Get modified flags and vanished messages // UID FETCH 1:* (FLAGS) (CHANGEDSINCE 0123456789 VANISHED) $result = $this->imap->conn->fetch($mailbox, !empty($uids) ? $uids : '1:*', true, array('FLAGS'), $index['modseq'], $qresync); $invalidated = false; if (!empty($result)) { foreach ($result as $id => $msg) { $uid = $msg->uid; // Remove deleted message if ($this->skip_deleted && !empty($msg->flags['DELETED'])) { $this->remove_message($mailbox, $uid); if (!$invalidated) { $invalidated = true; // Invalidate thread indexes (?) $this->remove_thread($mailbox); // 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 " . get_table_name('cache_messages') . " SET flags = ?, changed = " . $this->db->now() . " WHERE user_id = ?" . " AND mailbox = ?" . " AND uid = ?" . " AND flags <> ?", $flags, $this->userid, $mailbox, $uid, $flags); } } // Get VANISHED if ($qresync) { $mbox_data = $this->imap->mailbox_data($mailbox); // Removed messages if (!empty($mbox_data['VANISHED'])) { $uids = rcube_imap_generic::uncompressMessageSet($mbox_data['VANISHED']); if (!empty($uids)) { // remove messages from database $this->remove_message($mailbox, $uids); // Invalidate thread indexes (?) $this->remove_thread($mailbox); // Invalidate index $index['valid'] = false; } } } $sort_field = $index['sort_field']; $sort_order = $index['sort_order']; $exists = true; // Validate index if (!$this->validate($mailbox, $index, $exists)) { // Update index $data = $this->get_index_data($mailbox, $sort_field, $sort_order, $mbox_data); } else { $data = array_combine($index['seq'], $index['uid']); } // update index and/or HIGHESTMODSEQ value $this->add_index_row($mailbox, $sort_field, $sort_order, $data, $mbox_data, $exists); // update internal cache for get_index() $this->icache[$mailbox]['index']['result'] = $data; }