/** * Get all the fetchmail account configured (can specify a user id to retrieve only account for a single user) * @param MDB2 $dbm database connection * @param int $user_id */ public function __construct($dbm, $user_id = -1) { $this->dbm = $dbm; $this->position = 0; // Retrieve the accounts list if ($user_id != -1) { $sql_result = $this->dbm->query("SELECT * FROM " . get_table_name('fetchmail_rc') . " WHERE fk_user=?", $user_id); } else { $sql_result = $this->dbm->query("SELECT * FROM " . get_table_name('fetchmail_rc')); } while ($account = $this->dbm->fetch_assoc($sql_result)) { $fetchmailRc = new fetchMailRc(); $fetchmailRc->from_array($account); $this->datas[] = $fetchmailRc; } }
/** * 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->quoteIdentifier('name') . ", " . $this->db->quoteIdentifier('data') . ", " . $this->db->quoteIdentifier('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; }
/** * Mark the given identity as deleted * * @param int $iid Identity ID * @return boolean True if deleted successfully, false if nothing changed */ function delete_identity($iid) { if (!$this->ID) { return false; } $sql_result = $this->db->query("SELECT count(*) AS ident_count FROM " . get_table_name('identities') . " WHERE user_id = ? AND del <> 1", $this->ID); $sql_arr = $this->db->fetch_assoc($sql_result); // we'll not delete last identity if ($sql_arr['ident_count'] <= 1) { return false; } $this->db->query("UPDATE " . get_table_name('identities') . " SET del = 1, changed = " . $this->db->now() . " WHERE user_id = ?" . " AND identity_id = ?", $this->ID, $iid); return $this->db->affected_rows(); }
/** * @param string $key Cache key * @param int $uid Message UID * @return int Message (sequence) ID * @access private */ private function get_cache_uid2id($key, $uid) { if (!$this->caching_enabled) { return null; } if (array_key_exists('index', $this->icache) && $this->icache['index']['key'] == $key) { return array_search($uid, $this->icache['index']['result']); } $sql_result = $this->db->query("SELECT idx" . " 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)) { return intval($sql_arr['idx']); } return null; }
/** * 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; }
private function read_squirrel_prefs($uname) { $rcmail = rcmail::get_instance(); /**** File based backend ****/ if ($rcmail->config->get('squirrelmail_driver') == 'file' && ($srcdir = $rcmail->config->get('squirrelmail_data_dir'))) { if (($hash_level = $rcmail->config->get('squirrelmail_data_dir_hash_level')) > 0) { $srcdir = slashify($srcdir) . chunk_split(substr(base_convert(crc32($uname), 10, 16), 0, $hash_level), 1, '/'); } $prefsfile = slashify($srcdir) . $uname . '.pref'; $abookfile = slashify($srcdir) . $uname . '.abook'; $sigfile = slashify($srcdir) . $uname . '.sig'; $sigbase = slashify($srcdir) . $uname . '.si'; if (is_readable($prefsfile)) { $this->prefs = array(); foreach (file($prefsfile) as $line) { list($key, $value) = explode('=', $line); $this->prefs[$key] = utf8_encode(rtrim($value)); } // also read signature file if exists if (is_readable($sigfile)) { $this->prefs['___signature___'] = utf8_encode(file_get_contents($sigfile)); } if (isset($this->prefs['identities']) && $this->prefs['identities'] > 1) { for ($i = 1; $i < $this->prefs['identities']; $i++) { // read signature file if exists if (is_readable($sigbase . $i)) { $this->prefs['___sig' . $i . '___'] = utf8_encode(file_get_contents($sigbase . $i)); } } } // parse addres book file if (filesize($abookfile)) { foreach (file($abookfile) as $line) { list($rec['name'], $rec['firstname'], $rec['surname'], $rec['email']) = explode('|', utf8_encode(rtrim($line))); if ($rec['name'] && $rec['email']) { $this->abook[] = $rec; } } } } } else { if ($rcmail->config->get('squirrelmail_driver') == 'sql') { $this->prefs = array(); /* connect to squirrelmail database */ $db = new rcube_mdb2($rcmail->config->get('squirrelmail_dsn')); $db->db_connect('r'); // connect in read mode // $db->set_debug(true); /* retrieve prefs */ $userprefs_table = $rcmail->config->get('squirrelmail_userprefs_table'); $address_table = $rcmail->config->get('squirrelmail_address_table'); $db_charset = $rcmail->config->get('squirrelmail_db_charset'); if ($db_charset) { $db->query('SET NAMES ' . $db_charset); } $sql_result = $db->query('SELECT * FROM ' . $userprefs_table . ' WHERE user=?', $uname); // ? is replaced with emailaddress while ($sql_array = $db->fetch_assoc($sql_result)) { // fetch one row from result $this->prefs[$sql_array['prefkey']] = rcube_charset_convert(rtrim($sql_array['prefval']), $db_charset); } /* retrieve address table data */ $sql_result = $db->query('SELECT * FROM ' . $address_table . ' WHERE owner=?', $uname); // ? is replaced with emailaddress // parse addres book while ($sql_array = $db->fetch_assoc($sql_result)) { // fetch one row from result $rec['name'] = rcube_charset_convert(rtrim($sql_array['nickname']), $db_charset); $rec['firstname'] = rcube_charset_convert(rtrim($sql_array['firstname']), $db_charset); $rec['surname'] = rcube_charset_convert(rtrim($sql_array['lastname']), $db_charset); $rec['email'] = rcube_charset_convert(rtrim($sql_array['email']), $db_charset); $rec['note'] = rcube_charset_convert(rtrim($sql_array['label']), $db_charset); if ($rec['name'] && $rec['email']) { $this->abook[] = $rec; } } } } // end if 'sql'-driver }
/** * 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; }