| | |
| | | /** |
| | | +-----------------------------------------------------------------------+ |
| | | | This file is part of the Roundcube Webmail client | |
| | | | Copyright (C) 2005-2012, The Roundcube Dev Team | |
| | | | Copyright (C) 2005-2015, The Roundcube Dev Team | |
| | | | Copyright (C) 2011-2012, Kolab Systems AG | |
| | | | | |
| | | | Licensed under the GNU General Public License version 3 or | |
| | |
| | | |
| | | protected $fp; |
| | | protected $host; |
| | | protected $logged = false; |
| | | protected $capability = array(); |
| | | protected $capability_readed = false; |
| | | protected $prefs; |
| | | protected $cmd_tag; |
| | | protected $cmd_num = 0; |
| | | protected $resourceid; |
| | | protected $_debug = false; |
| | | protected $_debug_handler = false; |
| | | protected $logged = false; |
| | | protected $capability = array(); |
| | | protected $capability_readed = false; |
| | | protected $debug = false; |
| | | protected $debug_handler = false; |
| | | |
| | | const ERROR_OK = 0; |
| | | const ERROR_NO = -1; |
| | | const ERROR_BAD = -2; |
| | | const ERROR_BYE = -3; |
| | | const ERROR_UNKNOWN = -4; |
| | | const ERROR_COMMAND = -5; |
| | | const ERROR_OK = 0; |
| | | const ERROR_NO = -1; |
| | | const ERROR_BAD = -2; |
| | | const ERROR_BYE = -3; |
| | | const ERROR_UNKNOWN = -4; |
| | | const ERROR_COMMAND = -5; |
| | | const ERROR_READONLY = -6; |
| | | |
| | | const COMMAND_NORESPONSE = 1; |
| | |
| | | |
| | | const DEBUG_LINE_LENGTH = 4098; // 4KB + 2B for \r\n |
| | | |
| | | /** |
| | | * Object constructor |
| | | */ |
| | | function __construct() |
| | | { |
| | | } |
| | | |
| | | /** |
| | | * 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 string $string Command string |
| | | * @param bool $endln True if CRLF need to be added at the end of command |
| | | * @param bool $anonymized Don't write the given data to log but a placeholder |
| | | * |
| | | * @param int Number of bytes sent, False on error |
| | | */ |
| | | function putLine($string, $endln=true, $anonymized=false) |
| | | function putLine($string, $endln = true, $anonymized = false) |
| | | { |
| | | if (!$this->fp) |
| | | if (!$this->fp) { |
| | | return false; |
| | | } |
| | | |
| | | if ($this->_debug) { |
| | | if ($this->debug) { |
| | | // anonymize the sent command for logging |
| | | $cut = $endln ? 2 : 0; |
| | | if ($anonymized && preg_match('/^(A\d+ (?:[A-Z]+ )+)(.+)/', $string, $m)) { |
| | |
| | | else { |
| | | $log = rtrim($string); |
| | | } |
| | | |
| | | $this->debug('C: ' . $log); |
| | | } |
| | | |
| | |
| | | * 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 string $string Command string |
| | | * @param bool $endln True if CRLF need to be added at the end of command |
| | | * @param bool $anonymized Don't write the given data to log but a placeholder |
| | | * |
| | | * @return int|bool Number of bytes sent, False on error |
| | |
| | | } |
| | | |
| | | $bytes = $this->putLine($parts[$i].$parts[$i+1], false, $anonymized); |
| | | if ($bytes === false) |
| | | if ($bytes === false) { |
| | | return false; |
| | | } |
| | | |
| | | $res += $bytes; |
| | | |
| | | // don't wait if server supports LITERAL+ capability |
| | | if (!$this->prefs['literal+']) { |
| | | $line = $this->readLine(1000); |
| | | // handle error in command |
| | | if ($line[0] != '+') |
| | | if ($line[0] != '+') { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | $i++; |
| | | } |
| | | else { |
| | | $bytes = $this->putLine($parts[$i], false, $anonymized); |
| | | if ($bytes === false) |
| | | if ($bytes === false) { |
| | | return false; |
| | | } |
| | | |
| | | $res += $bytes; |
| | | } |
| | | } |
| | |
| | | /** |
| | | * Reads line from the connection stream |
| | | * |
| | | * @param int $size Buffer size |
| | | * @param int $size Buffer size |
| | | * |
| | | * @return string Line of text response |
| | | */ |
| | | function readLine($size=1024) |
| | | function readLine($size = 1024) |
| | | { |
| | | $line = ''; |
| | | |
| | |
| | | |
| | | do { |
| | | if ($this->eof()) { |
| | | return $line ? $line : NULL; |
| | | return $line ? $line : null; |
| | | } |
| | | |
| | | $buffer = fgets($this->fp, $size); |
| | |
| | | $this->closeSocket(); |
| | | break; |
| | | } |
| | | if ($this->_debug) { |
| | | |
| | | if ($this->debug) { |
| | | $this->debug('S: '. rtrim($buffer)); |
| | | } |
| | | |
| | | $line .= $buffer; |
| | | } while (substr($buffer, -1) != "\n"); |
| | | |
| | |
| | | |
| | | while (strlen($out) < $bytes) { |
| | | $line = $this->readBytes($bytes); |
| | | if ($line === NULL) |
| | | if ($line === null) { |
| | | break; |
| | | } |
| | | |
| | | $out .= $line; |
| | | } |
| | | |
| | |
| | | /** |
| | | * Reads specified number of bytes from the connection stream |
| | | * |
| | | * @param int $bytes Number of bytes to get |
| | | * @param int $bytes Number of bytes to get |
| | | * |
| | | * @return string Response text |
| | | */ |
| | |
| | | { |
| | | $data = ''; |
| | | $len = 0; |
| | | while ($len < $bytes && !$this->eof()) |
| | | { |
| | | |
| | | while ($len < $bytes && !$this->eof()) { |
| | | $d = fread($this->fp, $bytes-$len); |
| | | if ($this->_debug) { |
| | | if ($this->debug) { |
| | | $this->debug('S: '. $d); |
| | | } |
| | | $data .= $d; |
| | |
| | | /** |
| | | * Reads complete response to the IMAP command |
| | | * |
| | | * @param array $untagged Will be filled with untagged response lines |
| | | * @param array $untagged Will be filled with untagged response lines |
| | | * |
| | | * @return string Response text |
| | | */ |
| | | function readReply(&$untagged=null) |
| | | function readReply(&$untagged = null) |
| | | { |
| | | do { |
| | | $line = trim($this->readLine(1024)); |
| | | // store untagged response lines |
| | | if ($line[0] == '*') |
| | | if ($line[0] == '*') { |
| | | $untagged[] = $line; |
| | | } |
| | | } while ($line[0] == '*'); |
| | | |
| | | if ($untagged) |
| | | if ($untagged) { |
| | | $untagged = join("\n", $untagged); |
| | | } |
| | | |
| | | return $line; |
| | | } |
| | |
| | | |
| | | return $this->errornum; |
| | | } |
| | | |
| | | return self::ERROR_UNKNOWN; |
| | | } |
| | | |
| | |
| | | /** |
| | | * Error code/message setter. |
| | | */ |
| | | function setError($code, $msg='') |
| | | function setError($code, $msg = '') |
| | | { |
| | | $this->errornum = $code; |
| | | $this->error = $msg; |
| | |
| | | * |
| | | * @return bool True any check is true or connection is closed. |
| | | */ |
| | | function startsWith($string, $match, $error=false, $nonempty=false) |
| | | function startsWith($string, $match, $error = false, $nonempty = false) |
| | | { |
| | | if (!$this->fp) { |
| | | return true; |
| | | } |
| | | |
| | | if (strncmp($string, $match, strlen($match)) == 0) { |
| | | return true; |
| | | } |
| | | |
| | | if ($error && preg_match('/^\* (BYE|BAD) /i', $string, $m)) { |
| | | if (strtoupper($m[1]) == 'BYE') { |
| | | $this->closeSocket(); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | if ($nonempty && !strlen($string)) { |
| | | return true; |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | |
| | | |
| | | function clearCapability() |
| | | { |
| | | $this->capability = array(); |
| | | $this->capability = array(); |
| | | $this->capability_readed = false; |
| | | } |
| | | |
| | | /** |
| | | * DIGEST-MD5/CRAM-MD5/PLAIN Authentication |
| | | * |
| | | * @param string $user |
| | | * @param string $pass |
| | | * @param string $user Username |
| | | * @param string $pass Password |
| | | * @param string $type Authentication type (PLAIN/CRAM-MD5/DIGEST-MD5) |
| | | * |
| | | * @return resource Connection resourse on success, error code on error |
| | | */ |
| | | function authenticate($user, $pass, $type='PLAIN') |
| | | function authenticate($user, $pass, $type = 'PLAIN') |
| | | { |
| | | if ($type == 'CRAM-MD5' || $type == 'DIGEST-MD5') { |
| | | if ($type == 'DIGEST-MD5' && !class_exists('Auth_SASL')) { |
| | |
| | | return ($this->prefs['delimiter'] = $delimiter); |
| | | } |
| | | } |
| | | |
| | | return NULL; |
| | | } |
| | | |
| | | /** |
| | |
| | | /** |
| | | * Connects to IMAP server and authenticates. |
| | | * |
| | | * @param string $host Server hostname or IP |
| | | * @param string $user User name |
| | | * @param string $password Password |
| | | * @param array $options Connection and class options |
| | | * @param string $host Server hostname or IP |
| | | * @param string $user User name |
| | | * @param string $password Password |
| | | * @param array $options Connection and class options |
| | | * |
| | | * @return bool True on success, False on failure |
| | | */ |
| | | function connect($host, $user, $password, $options=null) |
| | | function connect($host, $user, $password, $options = null) |
| | | { |
| | | // configure |
| | | $this->set_prefs($options); |
| | |
| | | |
| | | $line = trim(fgets($this->fp, 8192)); |
| | | |
| | | if ($this->_debug) { |
| | | if ($this->debug) { |
| | | // set connection identifier for debug output |
| | | preg_match('/#([0-9]+)/', (string) $this->fp, $m); |
| | | $this->resourceid = strtoupper(substr(md5($m[1].$this->user.microtime()), 0, 4)); |
| | |
| | | // 3. an optional parenthesized list of known sequence ranges and their |
| | | // corresponding UIDs. |
| | | if (!empty($qresync_data)) { |
| | | if (!empty($qresync_data[2])) |
| | | if (!empty($qresync_data[2])) { |
| | | $qresync_data[2] = self::compressMessageSet($qresync_data[2]); |
| | | } |
| | | |
| | | $params[] = array('QRESYNC', $qresync_data); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | $this->data['READ-WRITE'] = $this->resultcode != 'READ-ONLY'; |
| | | |
| | | $this->selected = $mailbox; |
| | | |
| | | return true; |
| | | } |
| | | |
| | |
| | | * @return array Status item-value hash |
| | | * @since 0.5-beta |
| | | */ |
| | | function status($mailbox, $items=array()) |
| | | function status($mailbox, $items = array()) |
| | | { |
| | | if (!strlen($mailbox)) { |
| | | return false; |
| | |
| | | * |
| | | * @return boolean True on success, False on error |
| | | */ |
| | | function expunge($mailbox, $messages=NULL) |
| | | function expunge($mailbox, $messages = null) |
| | | { |
| | | if (!$this->select($mailbox)) { |
| | | return false; |
| | |
| | | */ |
| | | function close() |
| | | { |
| | | $result = $this->execute('CLOSE', NULL, self::COMMAND_NORESPONSE); |
| | | $result = $this->execute('CLOSE', null, self::COMMAND_NORESPONSE); |
| | | |
| | | if ($result == self::ERROR_OK) { |
| | | $this->selected = null; |
| | |
| | | * @return array|bool List of mailboxes or hash of options if STATUS/MYROGHTS response |
| | | * is requested, False on error. |
| | | */ |
| | | function listSubscribed($ref, $mailbox, $return_opts=array()) |
| | | function listSubscribed($ref, $mailbox, $return_opts = array()) |
| | | { |
| | | return $this->_listMailboxes($ref, $mailbox, true, $return_opts, NULL); |
| | | return $this->_listMailboxes($ref, $mailbox, true, $return_opts, null); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | if (preg_match('/^\* ([0-9]+) FETCH/', $line, $m)) { |
| | | $id = $m[1]; |
| | | $flags = NULL; |
| | | $flags = null; |
| | | |
| | | if ($return_uid) { |
| | | if (preg_match('/UID ([0-9]+)/', $line, $matches)) |
| | |
| | | |
| | | while (strlen($out) < $bytes) { |
| | | $out = $this->readBytes($bytes); |
| | | if ($out === NULL) |
| | | if ($out === null) |
| | | break; |
| | | $line .= $out; |
| | | } |
| | |
| | | return $result; |
| | | } |
| | | |
| | | function fetchPartHeader($mailbox, $id, $is_uid=false, $part=NULL) |
| | | function fetchPartHeader($mailbox, $id, $is_uid = false, $part = null) |
| | | { |
| | | $part = empty($part) ? 'HEADER' : $part.'.MIME'; |
| | | |
| | | return $this->handlePartBody($mailbox, $id, $is_uid, $part); |
| | | } |
| | | |
| | | function handlePartBody($mailbox, $id, $is_uid=false, $part='', $encoding=NULL, $print=NULL, $file=NULL, $formatted=false, $max_bytes=0) |
| | | function handlePartBody($mailbox, $id, $is_uid=false, $part='', $encoding=null, $print=null, $file=null, $formatted=false, $max_bytes=0) |
| | | { |
| | | if (!$this->select($mailbox)) { |
| | | return false; |
| | |
| | | else while ($bytes > 0) { |
| | | $line = $this->readLine(8192); |
| | | |
| | | if ($line === NULL) { |
| | | if ($line === null) { |
| | | break; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | $this->setError(self::ERROR_COMMAND, "Incomplete ACL response"); |
| | | return NULL; |
| | | } |
| | | |
| | | return NULL; |
| | | } |
| | | |
| | | /** |
| | |
| | | 'optional' => explode(' ', $optional), |
| | | ); |
| | | } |
| | | |
| | | return NULL; |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | return str_split($rights); |
| | | } |
| | | |
| | | return NULL; |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | foreach ($entries as $entry) { |
| | | $data[$entry] = NULL; |
| | | $data[$entry] = null; |
| | | } |
| | | |
| | | return $this->setMetadata($mailbox, $data); |
| | |
| | | |
| | | return $result; |
| | | } |
| | | |
| | | return NULL; |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | return $result; |
| | | } |
| | | |
| | | return NULL; |
| | | } |
| | | |
| | | /** |
| | |
| | | // excluded chars: SP, CTL, ), DEL |
| | | // we do not exclude [ and ] (#1489223) |
| | | if (preg_match('/^([^\x00-\x20\x29\x7F]+)/', $str, $m)) { |
| | | $result[] = $m[1] == 'NIL' ? NULL : $m[1]; |
| | | $result[] = $m[1] == 'NIL' ? null : $m[1]; |
| | | $str = substr($str, strlen($m[1])); |
| | | } |
| | | break; |
| | |
| | | */ |
| | | function setDebug($debug, $handler = null) |
| | | { |
| | | $this->_debug = $debug; |
| | | $this->_debug_handler = $handler; |
| | | $this->debug = $debug; |
| | | $this->debug_handler = $handler; |
| | | } |
| | | |
| | | /** |
| | |
| | | $message = sprintf('[%s] %s', $this->resourceid, $message); |
| | | } |
| | | |
| | | if ($this->_debug_handler) { |
| | | call_user_func_array($this->_debug_handler, array(&$this, $message)); |
| | | if ($this->debug_handler) { |
| | | call_user_func_array($this->debug_handler, array(&$this, $message)); |
| | | } else { |
| | | echo "DEBUG: $message\n"; |
| | | } |
| | | } |
| | | |
| | | } |