$imap_host = $a_host['host']; $imap_ssl = isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl', 'imaps', 'tls')) ? $a_host['scheme'] : null; if (isset($a_host['port'])) { $imap_port = $a_host['port']; } else { if ($imap_ssl && $imap_ssl != 'tls' && (!$imap_port || $imap_port == 143)) { $imap_port = 993; } } } $imap_host = idn_to_ascii($imap_host); $imap_user = idn_to_ascii($_POST['_user']); $imap = new rcube_imap(null); $imap->set_options(array('auth_type' => $RCI->getprop('imap_auth_type'), 'debug' => $RCI->getprop('imap_debug'), 'socket_options' => $RCI->getprop('imap_conn_options'))); if ($imap->connect($imap_host, $imap_user, $_POST['_pass'], $imap_port, $imap_ssl)) { $RCI->pass('IMAP connect', 'SORT capability: ' . ($imap->get_capability('SORT') ? 'yes' : 'no')); $imap->close(); } else { $RCI->fail('IMAP connect', $RCI->get_error()); } } ?> <p><input type="submit" name="imaptest" value="Check login" /></p> </form> <hr /> <p class="warning">
/** * 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; }
/** * 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; }