| | |
| | | /** |
| | | +-----------------------------------------------------------------------+ |
| | | | 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 DEBUG_LINE_LENGTH = 4098; // 4KB + 2B for \r\n |
| | | |
| | | /** |
| | | * Object constructor |
| | | */ |
| | | function __construct() |
| | | { |
| | | } |
| | | |
| | | /** |
| | | * Send simple (one line) command to the connection stream |
| | |
| | | */ |
| | | 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); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | $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; |
| | | } |
| | | } |
| | |
| | | |
| | | 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; |
| | | } |
| | | |
| | |
| | | { |
| | | $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; |
| | |
| | | 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; |
| | | } |
| | | |
| | |
| | | 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; |
| | | } |
| | | |
| | |
| | | /** |
| | | * 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 |
| | |
| | | return ($this->prefs['delimiter'] = $delimiter); |
| | | } |
| | | } |
| | | |
| | | return NULL; |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | $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 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; |
| | |
| | | */ |
| | | 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"; |
| | | } |
| | | } |
| | | |
| | | } |