| | |
| | | public $error; |
| | | public $errornum; |
| | | public $message; |
| | | public $rootdir; |
| | | public $delimiter; |
| | | public $data = array(); |
| | | public $flags = array( |
| | | 'SEEN' => '\\Seen', |
| | |
| | | const ERROR_UNKNOWN = -4; |
| | | |
| | | const COMMAND_NORESPONSE = 1; |
| | | const COMMAND_CAPABILITY = 2; |
| | | |
| | | /** |
| | | * Object constructor |
| | |
| | | { |
| | | } |
| | | |
| | | /** |
| | | * Send simple (one line) command to the connection stream |
| | | * |
| | | * @param string $string Command string |
| | | * @param bool $endln True if CRLF need to be added at the end of command |
| | | * |
| | | * @param int Number of bytes sent, False on error |
| | | */ |
| | | function putLine($string, $endln=true) |
| | | { |
| | | if (!$this->fp) |
| | |
| | | return $res; |
| | | } |
| | | |
| | | // $this->putLine replacement with Command Continuation Requests (RFC3501 7.5) support |
| | | /** |
| | | * Send command to the connection stream with Command Continuation |
| | | * Requests (RFC3501 7.5) and LITERAL+ (RFC2088) support |
| | | * |
| | | * @param string $string Command string |
| | | * @param bool $endln True if CRLF need to be added at the end of command |
| | | * |
| | | * @param int Number of bytes sent, False on error |
| | | */ |
| | | function putLineC($string, $endln=true) |
| | | { |
| | | if (!$this->fp) |
| | |
| | | |
| | | // RFC 4959 (SASL-IR): save one round trip |
| | | if ($this->getCapability('SASL-IR')) { |
| | | $result = $this->execute("AUTHENTICATE PLAIN", array($reply), self::COMMAND_NORESPONSE); |
| | | $result = $this->execute("AUTHENTICATE PLAIN", array($reply), |
| | | self::COMMAND_NORESPONSE | self::COMMAND_CAPABILITY); |
| | | } |
| | | else { |
| | | $this->putLine($this->next_tag() . " AUTHENTICATE PLAIN"); |
| | |
| | | } |
| | | |
| | | if ($result == self::ERROR_OK) { |
| | | // optional CAPABILITY response |
| | | if ($line && preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)) { |
| | | $this->parseCapability($matches[1], true); |
| | | } |
| | | return $this->fp; |
| | | } |
| | | else { |
| | |
| | | function login($user, $password) |
| | | { |
| | | list($code, $response) = $this->execute('LOGIN', array( |
| | | $this->escape($user), $this->escape($password))); |
| | | $this->escape($user), $this->escape($password)), self::COMMAND_CAPABILITY); |
| | | |
| | | // re-set capabilities list if untagged CAPABILITY response provided |
| | | if (preg_match('/\* CAPABILITY (.+)/i', $response, $matches)) { |
| | | $this->parseCapability($matches[1]); |
| | | $this->parseCapability($matches[1], true); |
| | | } |
| | | |
| | | if ($code == self::ERROR_OK) { |
| | |
| | | return $code; |
| | | } |
| | | |
| | | function getNamespace() |
| | | /** |
| | | * Gets the root directory and delimiter (of personal namespace) |
| | | * |
| | | * @return mixed A root directory name, or false. |
| | | */ |
| | | function getRootDir() |
| | | { |
| | | if (isset($this->prefs['rootdir']) && is_string($this->prefs['rootdir'])) { |
| | | $this->rootdir = $this->prefs['rootdir']; |
| | | return true; |
| | | return $this->prefs['rootdir']; |
| | | } |
| | | |
| | | if (!is_array($data = $this->_namespace())) { |
| | | if (!is_array($data = $this->getNamespace())) { |
| | | return false; |
| | | } |
| | | |
| | | $user_space_data = $data[0]; |
| | | $user_space_data = $data['personal']; |
| | | if (!is_array($user_space_data)) { |
| | | return false; |
| | | } |
| | | |
| | | $first_userspace = $user_space_data[0]; |
| | | if (count($first_userspace)!=2) { |
| | | if (count($first_userspace) !=2) { |
| | | return false; |
| | | } |
| | | |
| | | $this->rootdir = $first_userspace[0]; |
| | | $this->delimiter = $first_userspace[1]; |
| | | $this->prefs['rootdir'] = substr($this->rootdir, 0, -1); |
| | | $this->prefs['delimiter'] = $this->delimiter; |
| | | $rootdir = $first_userspace[0]; |
| | | $this->prefs['delimiter'] = $first_userspace[1]; |
| | | $this->prefs['rootdir'] = $rootdir ? substr($rootdir, 0, -1) : ''; |
| | | |
| | | return true; |
| | | return $this->prefs['rootdir']; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Gets the delimiter, for example: |
| | |
| | | */ |
| | | function getHierarchyDelimiter() |
| | | { |
| | | if ($this->delimiter) { |
| | | return $this->delimiter; |
| | | if ($this->prefs['delimiter']) { |
| | | return $this->prefs['delimiter']; |
| | | } |
| | | if (!empty($this->prefs['delimiter'])) { |
| | | return ($this->delimiter = $this->prefs['delimiter']); |
| | | return $this->prefs['delimiter']; |
| | | } |
| | | |
| | | // try (LIST "" ""), should return delimiter (RFC2060 Sec 6.3.8) |
| | |
| | | $delimiter = $args[3]; |
| | | |
| | | if (strlen($delimiter) > 0) { |
| | | $this->delimiter = $delimiter; |
| | | return $delimiter; |
| | | return ($this->prefs['delimiter'] = $delimiter); |
| | | } |
| | | } |
| | | |
| | | // if that fails, try namespace extension |
| | | // try to fetch namespace data |
| | | if (!is_array($data = $this->_namespace())) { |
| | | if (!is_array($data = $this->getNamespace())) { |
| | | return false; |
| | | } |
| | | |
| | | // extract user space data (opposed to global/shared space) |
| | | $user_space_data = $data[0]; |
| | | $user_space_data = $data['personal']; |
| | | if (!is_array($user_space_data)) { |
| | | return false; |
| | | } |
| | |
| | | } |
| | | |
| | | // extract delimiter |
| | | return $this->delimiter = $first_userspace[1]; |
| | | return $this->prefs['delimiter'] = $first_userspace[1]; |
| | | } |
| | | |
| | | function _namespace() |
| | | /** |
| | | * NAMESPACE handler (RFC 2342) |
| | | * |
| | | * @return array Namespace data hash (personal, other, shared) |
| | | */ |
| | | function getNamespace() |
| | | { |
| | | if (array_key_exists('namespace', $this->prefs)) { |
| | | return $this->prefs['namespace']; |
| | | } |
| | | |
| | | if (!$this->getCapability('NAMESPACE')) { |
| | | return false; |
| | | return self::ERROR_BAD; |
| | | } |
| | | |
| | | list($code, $response) = $this->execute('NAMESPACE'); |
| | | |
| | | if ($code == self::ERROR_OK && preg_match('/^\* NAMESPACE /', $response)) { |
| | | $data = $this->parseNamespace(substr($response, 11), $i, 0, 0); |
| | | $data = $this->tokenizeResponse(substr($response, 11)); |
| | | } |
| | | |
| | | if (!is_array($data)) { |
| | | return false; |
| | | return $code; |
| | | } |
| | | |
| | | return $data; |
| | | $this->prefs['namespace'] = array( |
| | | 'personal' => $data[0], |
| | | 'other' => $data[1], |
| | | 'shared' => $data[2], |
| | | ); |
| | | |
| | | return $this->prefs['namespace']; |
| | | } |
| | | |
| | | function connect($host, $user, $password, $options=null) |
| | |
| | | } else { |
| | | $auth_method = 'CHECK'; |
| | | } |
| | | |
| | | $message = "INITIAL: $auth_method\n"; |
| | | |
| | | $result = false; |
| | | |
| | |
| | | // Connected to wrong port or connection error? |
| | | if (!preg_match('/^\* (OK|PREAUTH)/i', $line)) { |
| | | if ($line) |
| | | $this->error = sprintf("Wrong startup greeting (%s:%d): %s", $host, $this->prefs['port'], $line); |
| | | $error = sprintf("Wrong startup greeting (%s:%d): %s", $host, $this->prefs['port'], $line); |
| | | else |
| | | $this->error = sprintf("Empty startup greeting (%s:%d)", $host, $this->prefs['port']); |
| | | $this->errornum = self::ERROR_BAD; |
| | | $error = sprintf("Empty startup greeting (%s:%d)", $host, $this->prefs['port']); |
| | | |
| | | $this->set_error(self::ERROR_BAD, $error); |
| | | $this->close(); |
| | | return false; |
| | | } |
| | | |
| | | // RFC3501 [7.1] optional CAPABILITY response |
| | | if (preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)) { |
| | | $this->parseCapability($matches[1]); |
| | | $this->parseCapability($matches[1], true); |
| | | } |
| | | |
| | | $this->message .= $line; |
| | | $this->message = $line; |
| | | |
| | | // TLS connection |
| | | if ($this->prefs['ssl_mode'] == 'tls' && $this->getCapability('STARTTLS')) { |
| | |
| | | $res = $this->execute('STARTTLS'); |
| | | |
| | | if ($res[0] != self::ERROR_OK) { |
| | | $this->close(); |
| | | return false; |
| | | } |
| | | |
| | | if (!stream_socket_enable_crypto($this->fp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) { |
| | | $this->set_error(self::ERROR_BAD, "Unable to negotiate TLS"); |
| | | $this->close(); |
| | | return false; |
| | | } |
| | | |
| | | // Now we're authenticated, capabilities need to be reread |
| | | // Now we're secure, capabilities need to be reread |
| | | $this->clearCapability(); |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | else { |
| | | // Prevent from sending credentials in plain text when connection is not secure |
| | | if ($auth_method == 'LOGIN' && $this->getCapability('LOGINDISABLED')) { |
| | | $this->set_error(self::ERROR_BAD, "Login disabled by IMAP server"); |
| | | $this->close(); |
| | | return false; |
| | | } |
| | | // replace AUTH with CRAM-MD5 for backward compat. |
| | | $auth_methods[] = $auth_method == 'AUTH' ? 'CRAM-MD5' : $auth_method; |
| | | } |
| | | |
| | | // pre-login capabilities can be not complete |
| | | $this->capability_readed = false; |
| | | |
| | | // Authenticate |
| | | foreach ($auth_methods as $method) { |
| | |
| | | if ($this->prefs['force_caps']) { |
| | | $this->clearCapability(); |
| | | } |
| | | $this->getNamespace(); |
| | | $this->getRootDir(); |
| | | $this->logged = true; |
| | | |
| | | return true; |
| | | } |
| | | |
| | | // Close connection |
| | | @fclose($this->fp); |
| | | $this->fp = false; |
| | | $this->close(); |
| | | |
| | | return false; |
| | | return false; |
| | | } |
| | | |
| | | function connected() |
| | |
| | | |
| | | function close() |
| | | { |
| | | if ($this->logged && $this->putLine($this->next_tag() . ' LOGOUT')) { |
| | | if (!feof($this->fp)) |
| | | fgets($this->fp, 1024); |
| | | } |
| | | if ($this->putLine($this->next_tag() . ' LOGOUT')) { |
| | | $this->readReply(); |
| | | } |
| | | |
| | | @fclose($this->fp); |
| | | $this->fp = false; |
| | | } |
| | |
| | | if ($this->selected == $mailbox) { |
| | | return true; |
| | | } |
| | | |
| | | if (is_array($this->data['LIST']) && is_array($opts = $this->data['LIST'][$mailbox])) { |
| | | if (in_array('\\Noselect', $opts)) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | list($code, $response) = $this->execute('SELECT', array($this->escape($mailbox))); |
| | | |
| | |
| | | $this->selected = ''; |
| | | } |
| | | |
| | | $this->select($mailbox); |
| | | if ($this->selected == $mailbox) { |
| | | return $this->data['EXISTS']; |
| | | } |
| | | |
| | | // Try STATUS, should be faster |
| | | $counts = $this->status($mailbox, array('MESSAGES')); |
| | | if (is_array($counts)) { |
| | | return (int) $counts['MESSAGES']; |
| | | } |
| | | |
| | | return false; |
| | | } |
| | |
| | | if (!$this->select($mailbox)) { |
| | | return false; |
| | | } |
| | | |
| | | // RFC 5957: SORT=DISPLAY |
| | | if (($field == 'FROM' || $field == 'TO') && $this->getCapability('SORT=DISPLAY')) { |
| | | $field = 'DISPLAY' . $field; |
| | | } |
| | | |
| | | // message IDs |
| | | if (is_array($add)) |
| | |
| | | * @param string $ref Reference name |
| | | * @param string $mailbox Mailbox name |
| | | * @param array $status_opts (see self::_listMailboxes) |
| | | * @param array $select_opts (see self::_listMailboxes) |
| | | * |
| | | * @return array List of mailboxes or hash of options if $status_opts argument |
| | | * is non-empty. |
| | | * @access public |
| | | */ |
| | | function listMailboxes($ref, $mailbox, $status_opts=array()) |
| | | function listMailboxes($ref, $mailbox, $status_opts=array(), $select_opts=array()) |
| | | { |
| | | return $this->_listMailboxes($ref, $mailbox, false, $status_opts); |
| | | return $this->_listMailboxes($ref, $mailbox, false, $status_opts, $select_opts); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param string $mailbox Mailbox name |
| | | * @param array $status_opts (see self::_listMailboxes) |
| | | * |
| | | * @return array List of mailboxes or hash of options if $status_ops argument |
| | | * @return array List of mailboxes or hash of options if $status_opts argument |
| | | * is non-empty. |
| | | * @access public |
| | | */ |
| | | function listSubscribed($ref, $mailbox, $status_opts=array()) |
| | | { |
| | | return $this->_listMailboxes($ref, $mailbox, true, $status_opts); |
| | | return $this->_listMailboxes($ref, $mailbox, true, $status_opts, NULL); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param bool $subscribed Enables returning subscribed mailboxes only |
| | | * @param array $status_opts List of STATUS options (RFC5819: LIST-STATUS) |
| | | * Possible: MESSAGES, RECENT, UIDNEXT, UIDVALIDITY, UNSEEN |
| | | * @param array $select_opts List of selection options (RFC5258: LIST-EXTENDED) |
| | | * Possible: SUBSCRIBED, RECURSIVEMATCH, REMOTE |
| | | * |
| | | * @return array List of mailboxes or hash of options if $status_ops argument |
| | | * is non-empty. |
| | | * @access private |
| | | */ |
| | | private function _listMailboxes($ref, $mailbox, $subscribed=false, $status_opts=array()) |
| | | private function _listMailboxes($ref, $mailbox, $subscribed=false, |
| | | $status_opts=array(), $select_opts=array()) |
| | | { |
| | | if (empty($mailbox)) { |
| | | $mailbox = '*'; |
| | | } |
| | | |
| | | if (empty($ref) && $this->rootdir) { |
| | | $ref = $this->rootdir; |
| | | if (empty($ref) && $this->prefs['rootdir']) { |
| | | $ref = $this->prefs['rootdir']; |
| | | } |
| | | |
| | | $args = array($this->escape($ref), $this->escape($mailbox)); |
| | | $args = array(); |
| | | |
| | | if (!empty($select_opts) && $this->getCapability('LIST-EXTENDED')) { |
| | | $select_opts = (array) $select_opts; |
| | | |
| | | $args[] = '(' . implode(' ', $select_opts) . ')'; |
| | | } |
| | | |
| | | $args[] = $this->escape($ref); |
| | | $args[] = $this->escape($mailbox); |
| | | |
| | | if (!empty($status_opts) && $this->getCapability('LIST-STATUS')) { |
| | | $status_opts = array($status_opts); |
| | | $status_opts = (array) $status_opts; |
| | | $lstatus = true; |
| | | |
| | | $args[] = 'RETURN (STATUS (' . implode(' ', $status_opts) . '))'; |
| | |
| | | // * LIST (<options>) <delimiter> <mailbox> |
| | | if (!$lstatus || $cmd == 'LIST' || $cmd == 'LSUB') { |
| | | list($opts, $delim, $folder) = $this->tokenizeResponse($response, 3); |
| | | |
| | | // Add to result array |
| | | if (!$lstatus) { |
| | | $folders[] = $folder; |
| | | } |
| | | else { |
| | | $folders[$folder] = array(); |
| | | } |
| | | |
| | | // Add to options array |
| | | if (!empty($opts)) { |
| | | if (empty($this->data['LIST'][$folder])) |
| | | $this->data['LIST'][$folder] = $opts; |
| | | else |
| | | $this->data['LIST'][$folder] = array_unique(array_merge( |
| | | $this->data['LIST'][$folder], $opts)); |
| | | } |
| | | } |
| | | // * STATUS <mailbox> (<result>) |
| | |
| | | $response = substr($response, 0, -$line_len); |
| | | } |
| | | |
| | | // optional CAPABILITY response |
| | | if (($options & self::COMMAND_CAPABILITY) && $code == self::ERROR_OK |
| | | && preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches) |
| | | ) { |
| | | $this->parseCapability($matches[1], true); |
| | | } |
| | | |
| | | return $noresp ? $code : array($code, $response); |
| | | } |
| | | |
| | |
| | | return $string; |
| | | } |
| | | |
| | | private function parseNamespace($str, &$i, $len=0, $l) |
| | | { |
| | | if (!$l) { |
| | | $str = str_replace('NIL', '()', $str); |
| | | } |
| | | if (!$len) { |
| | | $len = strlen($str); |
| | | } |
| | | $data = array(); |
| | | $in_quotes = false; |
| | | $elem = 0; |
| | | |
| | | for ($i; $i<$len; $i++) { |
| | | $c = (string)$str[$i]; |
| | | if ($c == '(' && !$in_quotes) { |
| | | $i++; |
| | | $data[$elem] = $this->parseNamespace($str, $i, $len, $l++); |
| | | $elem++; |
| | | } else if ($c == ')' && !$in_quotes) { |
| | | return $data; |
| | | } else if ($c == '\\') { |
| | | $i++; |
| | | if ($in_quotes) { |
| | | $data[$elem] .= $str[$i]; |
| | | } |
| | | } else if ($c == '"') { |
| | | $in_quotes = !$in_quotes; |
| | | if (!$in_quotes) { |
| | | $elem++; |
| | | } |
| | | } else if ($in_quotes) { |
| | | $data[$elem].=$c; |
| | | } |
| | | } |
| | | |
| | | return $data; |
| | | } |
| | | |
| | | private function parseCapability($str) |
| | | private function parseCapability($str, $trusted=false) |
| | | { |
| | | $str = preg_replace('/^\* CAPABILITY /i', '', $str); |
| | | |
| | |
| | | if (!isset($this->prefs['literal+']) && in_array('LITERAL+', $this->capability)) { |
| | | $this->prefs['literal+'] = true; |
| | | } |
| | | |
| | | if ($trusted) { |
| | | $this->capability_readed = true; |
| | | } |
| | | } |
| | | |
| | | /** |