| | |
| | | | program/include/rcube_imap_generic.php | |
| | | | | |
| | | | This file is part of the Roundcube Webmail client | |
| | | | Copyright (C) 2005-2010, Roundcube Dev. - Switzerland | |
| | | | Copyright (C) 2005-2010, The Roundcube Dev Team | |
| | | | Licensed under the GNU GPL | |
| | | | | |
| | | | PURPOSE: | |
| | |
| | | public $priority; |
| | | public $mdn_to; |
| | | public $mdn_sent = false; |
| | | public $is_draft = false; |
| | | public $seen = false; |
| | | public $deleted = false; |
| | | public $recent = false; |
| | | public $answered = false; |
| | | public $forwarded = false; |
| | | public $junk = false; |
| | | public $flagged = false; |
| | | public $has_children = false; |
| | | public $depth = 0; |
| | |
| | | public $flags = array( |
| | | 'SEEN' => '\\Seen', |
| | | 'DELETED' => '\\Deleted', |
| | | 'RECENT' => '\\Recent', |
| | | 'ANSWERED' => '\\Answered', |
| | | 'DRAFT' => '\\Draft', |
| | | 'FLAGGED' => '\\Flagged', |
| | |
| | | private $prefs; |
| | | private $cmd_tag; |
| | | private $cmd_num = 0; |
| | | private $_debug = false; |
| | | private $_debug_handler = false; |
| | | |
| | | const ERROR_OK = 0; |
| | | const ERROR_NO = -1; |
| | |
| | | if (!$this->fp) |
| | | return false; |
| | | |
| | | if (!empty($this->prefs['debug_mode'])) { |
| | | write_log('imap', 'C: '. rtrim($string)); |
| | | if ($this->_debug) { |
| | | $this->debug('C: '. rtrim($string)); |
| | | } |
| | | |
| | | $res = fwrite($this->fp, $string . ($endln ? "\r\n" : '')); |
| | |
| | | if ($endln) |
| | | $string .= "\r\n"; |
| | | |
| | | |
| | | $res = 0; |
| | | if ($parts = preg_split('/(\{[0-9]+\}\r\n)/m', $string, -1, PREG_SPLIT_DELIM_CAPTURE)) { |
| | | for ($i=0, $cnt=count($parts); $i<$cnt; $i++) { |
| | | if (preg_match('/^\{[0-9]+\}\r\n$/', $parts[$i+1])) { |
| | | if (preg_match('/^\{([0-9]+)\}\r\n$/', $parts[$i+1], $matches)) { |
| | | // LITERAL+ support |
| | | if ($this->prefs['literal+']) |
| | | $parts[$i+1] = preg_replace('/([0-9]+)/', '\\1+', $parts[$i+1]); |
| | | if ($this->prefs['literal+']) { |
| | | $parts[$i+1] = sprintf("{%d+}\r\n", $matches[1]); |
| | | } |
| | | |
| | | $bytes = $this->putLine($parts[$i].$parts[$i+1], false); |
| | | if ($bytes === false) |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | return $res; |
| | | } |
| | | |
| | |
| | | { |
| | | $line = ''; |
| | | |
| | | if (!$this->fp) { |
| | | return NULL; |
| | | } |
| | | |
| | | if (!$size) { |
| | | $size = 1024; |
| | | } |
| | | |
| | | do { |
| | | if (feof($this->fp)) { |
| | | if ($this->eof()) { |
| | | return $line ? $line : NULL; |
| | | } |
| | | |
| | | $buffer = fgets($this->fp, $size); |
| | | |
| | | if ($buffer === false) { |
| | | @fclose($this->fp); |
| | | $this->fp = null; |
| | | $this->closeSocket(); |
| | | break; |
| | | } |
| | | if (!empty($this->prefs['debug_mode'])) { |
| | | write_log('imap', 'S: '. rtrim($buffer)); |
| | | if ($this->_debug) { |
| | | $this->debug('S: '. rtrim($buffer)); |
| | | } |
| | | $line .= $buffer; |
| | | } while ($buffer[strlen($buffer)-1] != "\n"); |
| | | } while (substr($buffer, -1) != "\n"); |
| | | |
| | | return $line; |
| | | } |
| | |
| | | { |
| | | $data = ''; |
| | | $len = 0; |
| | | while ($len < $bytes && !feof($this->fp)) |
| | | while ($len < $bytes && !$this->eof()) |
| | | { |
| | | $d = fread($this->fp, $bytes-$len); |
| | | if (!empty($this->prefs['debug_mode'])) { |
| | | write_log('imap', 'S: '. $d); |
| | | if ($this->_debug) { |
| | | $this->debug('S: '. $d); |
| | | } |
| | | $data .= $d; |
| | | $data_len = strlen($data); |
| | |
| | | } else if ($res == 'BAD') { |
| | | $this->errornum = self::ERROR_BAD; |
| | | } else if ($res == 'BYE') { |
| | | @fclose($this->fp); |
| | | $this->fp = null; |
| | | $this->closeSocket(); |
| | | $this->errornum = self::ERROR_BYE; |
| | | } |
| | | |
| | |
| | | return self::ERROR_UNKNOWN; |
| | | } |
| | | |
| | | private function eof() |
| | | { |
| | | if (!is_resource($this->fp)) { |
| | | return true; |
| | | } |
| | | |
| | | // If a connection opened by fsockopen() wasn't closed |
| | | // by the server, feof() will hang. |
| | | $start = microtime(true); |
| | | |
| | | if (feof($this->fp) || |
| | | ($this->prefs['timeout'] && (microtime(true) - $start > $this->prefs['timeout'])) |
| | | ) { |
| | | $this->closeSocket(); |
| | | return true; |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | private function closeSocket() |
| | | { |
| | | @fclose($this->fp); |
| | | $this->fp = null; |
| | | } |
| | | |
| | | function setError($code, $msg='') |
| | | { |
| | | $this->errornum = $code; |
| | |
| | | } |
| | | if ($error && preg_match('/^\* (BYE|BAD) /i', $string, $m)) { |
| | | if (strtoupper($m[1]) == 'BYE') { |
| | | @fclose($this->fp); |
| | | $this->fp = null; |
| | | $this->closeSocket(); |
| | | } |
| | | return true; |
| | | } |
| | |
| | | return false; |
| | | } |
| | | |
| | | function getCapability($name) |
| | | private function hasCapability($name) |
| | | { |
| | | if (empty($this->capability) || $name == '') { |
| | | return false; |
| | | } |
| | | |
| | | if (in_array($name, $this->capability)) { |
| | | return true; |
| | | } |
| | | else if (strpos($name, '=')) { |
| | | return false; |
| | | } |
| | | |
| | | $result = array(); |
| | | foreach ($this->capability as $cap) { |
| | | $entry = explode('=', $cap); |
| | | if ($entry[0] == $name) { |
| | | $result[] = $entry[1]; |
| | | } |
| | | } |
| | | |
| | | return !empty($result) ? $result : false; |
| | | } |
| | | |
| | | /** |
| | | * Capabilities checker |
| | | * |
| | | * @param string $name Capability name |
| | | * |
| | | * @return mixed Capability values array for key=value pairs, true/false for others |
| | | */ |
| | | function getCapability($name) |
| | | { |
| | | $result = $this->hasCapability($name); |
| | | |
| | | if (!empty($result)) { |
| | | return $result; |
| | | } |
| | | else if ($this->capability_readed) { |
| | | return false; |
| | |
| | | |
| | | $this->capability_readed = true; |
| | | |
| | | if (in_array($name, $this->capability)) { |
| | | return true; |
| | | } |
| | | |
| | | return false; |
| | | return $this->hasCapability($name); |
| | | } |
| | | |
| | | function clearCapability() |
| | |
| | | $host = $this->prefs['ssl_mode'] . '://' . $host; |
| | | } |
| | | |
| | | if ($this->prefs['timeout'] <= 0) { |
| | | $this->prefs['timeout'] = ini_get('default_socket_timeout'); |
| | | } |
| | | |
| | | // Connect |
| | | if ($this->prefs['timeout'] > 0) |
| | | $this->fp = @fsockopen($host, $this->prefs['port'], $errno, $errstr, $this->prefs['timeout']); |
| | | else |
| | | $this->fp = @fsockopen($host, $this->prefs['port'], $errno, $errstr); |
| | | $this->fp = @fsockopen($host, $this->prefs['port'], $errno, $errstr, $this->prefs['timeout']); |
| | | |
| | | if (!$this->fp) { |
| | | $this->setError(self::ERROR_BAD, sprintf("Could not connect to %s:%d: %s", $host, $this->prefs['port'], $errstr)); |
| | |
| | | |
| | | $line = trim(fgets($this->fp, 8192)); |
| | | |
| | | if ($this->prefs['debug_mode'] && $line) { |
| | | write_log('imap', 'S: '. $line); |
| | | if ($this->_debug && $line) { |
| | | $this->debug('S: '. $line); |
| | | } |
| | | |
| | | // Connected to wrong port or connection error? |
| | |
| | | } |
| | | } |
| | | |
| | | // Send ID info |
| | | if (!empty($this->prefs['ident']) && $this->getCapability('ID')) { |
| | | $this->id($this->prefs['ident']); |
| | | } |
| | | |
| | | $auth_methods = array(); |
| | | $result = null; |
| | | |
| | | // check for supported auth methods |
| | | if ($auth_method == 'CHECK') { |
| | | if ($this->getCapability('AUTH=DIGEST-MD5')) { |
| | | $auth_methods[] = 'DIGEST-MD5'; |
| | | } |
| | | if ($this->getCapability('AUTH=CRAM-MD5') || $this->getCapability('AUTH=CRAM_MD5')) { |
| | | $auth_methods[] = 'CRAM-MD5'; |
| | | } |
| | | if ($this->getCapability('AUTH=PLAIN')) { |
| | | $auth_methods[] = 'PLAIN'; |
| | | if ($auth_caps = $this->getCapability('AUTH')) { |
| | | $auth_methods = $auth_caps; |
| | | } |
| | | // RFC 2595 (LOGINDISABLED) LOGIN disabled when connection is not secure |
| | | if (!$this->getCapability('LOGINDISABLED')) { |
| | | $login_disabled = $this->getCapability('LOGINDISABLED'); |
| | | if (($key = array_search('LOGIN', $auth_methods)) !== false) { |
| | | if ($login_disabled) { |
| | | unset($auth_methods[$key]); |
| | | } |
| | | } |
| | | else if (!$login_disabled) { |
| | | $auth_methods[] = 'LOGIN'; |
| | | } |
| | | |
| | | // Use best (for security) supported authentication method |
| | | foreach (array('DIGEST-MD5', 'CRAM-MD5', 'CRAM_MD5', 'PLAIN', 'LOGIN') as $auth_method) { |
| | | if (in_array($auth_method, $auth_methods)) { |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | else { |
| | |
| | | return false; |
| | | } |
| | | // replace AUTH with CRAM-MD5 for backward compat. |
| | | $auth_methods[] = $auth_method == 'AUTH' ? 'CRAM-MD5' : $auth_method; |
| | | if ($auth_method == 'AUTH') { |
| | | $auth_method = 'CRAM-MD5'; |
| | | } |
| | | } |
| | | |
| | | // pre-login capabilities can be not complete |
| | | $this->capability_readed = false; |
| | | |
| | | // Authenticate |
| | | foreach ($auth_methods as $method) { |
| | | switch ($method) { |
| | | case 'DIGEST-MD5': |
| | | switch ($auth_method) { |
| | | case 'CRAM_MD5': |
| | | $auth_method = 'CRAM-MD5'; |
| | | case 'CRAM-MD5': |
| | | case 'DIGEST-MD5': |
| | | case 'PLAIN': |
| | | $result = $this->authenticate($user, $password, $method); |
| | | $result = $this->authenticate($user, $password, $auth_method); |
| | | break; |
| | | case 'LOGIN': |
| | | $result = $this->login($user, $password); |
| | | break; |
| | | default: |
| | | $this->setError(self::ERROR_BAD, "Configuration error. Unknown auth method: $method"); |
| | | } |
| | | |
| | | if (is_resource($result)) { |
| | | break; |
| | | } |
| | | $this->setError(self::ERROR_BAD, "Configuration error. Unknown auth method: $auth_method"); |
| | | } |
| | | |
| | | // Connected and authenticated |
| | |
| | | $this->readReply(); |
| | | } |
| | | |
| | | @fclose($this->fp); |
| | | $this->fp = false; |
| | | $this->closeSocket(); |
| | | } |
| | | |
| | | /** |
| | |
| | | $response = substr($response, 9); // remove prefix "* STATUS " |
| | | |
| | | list($mbox, $items) = $this->tokenizeResponse($response, 2); |
| | | |
| | | // Fix for #1487859. Some buggy server returns not quoted |
| | | // folder name with spaces. Let's try to handle this situation |
| | | if (!is_array($items) && ($pos = strpos($response, '(')) !== false) { |
| | | $response = substr($response, $pos); |
| | | $items = $this->tokenizeResponse($response, 1); |
| | | if (!is_array($items)) { |
| | | return $result; |
| | | } |
| | | } |
| | | |
| | | for ($i=0, $len=count($items); $i<$len; $i += 2) { |
| | | $result[$items[$i]] = (int) $items[$i+1]; |
| | |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * Executes ID command (RFC2971) |
| | | * |
| | | * @param array $items Client identification information key/value hash |
| | | * |
| | | * @return array Server identification information key/value hash |
| | | * @access public |
| | | * @since 0.6 |
| | | */ |
| | | function id($items=array()) |
| | | { |
| | | if (is_array($items) && !empty($items)) { |
| | | foreach ($items as $key => $value) { |
| | | $args[] = $this->escape($key, true); |
| | | $args[] = $this->escape($value, true); |
| | | } |
| | | } |
| | | |
| | | list($code, $response) = $this->execute('ID', array( |
| | | !empty($args) ? '(' . implode(' ', (array) $args) . ')' : $this->escape(null) |
| | | )); |
| | | |
| | | |
| | | if ($code == self::ERROR_OK && preg_match('/\* ID /i', $response)) { |
| | | $response = substr($response, 5); // remove prefix "* ID " |
| | | $items = $this->tokenizeResponse($response, 1); |
| | | $result = null; |
| | | |
| | | for ($i=0, $len=count($items); $i<$len; $i += 2) { |
| | | $result[$items[$i]] = $items[$i+1]; |
| | | } |
| | | |
| | | return $result; |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | function sort($mailbox, $field, $add='', $is_uid=FALSE, $encoding = 'US-ASCII') |
| | | { |
| | | $field = strtoupper($field); |
| | |
| | | $result[$id] = ''; |
| | | } |
| | | } else if ($mode == 2) { |
| | | if (preg_match('/\((UID|RFC822\.SIZE) ([0-9]+)/', $line, $matches)) { |
| | | if (preg_match('/(UID|RFC822\.SIZE) ([0-9]+)/', $line, $matches)) { |
| | | $result[$id] = trim($matches[2]); |
| | | } else { |
| | | $result[$id] = 0; |
| | |
| | | if ($bodystr) |
| | | $request .= "BODYSTRUCTURE "; |
| | | $request .= "BODY.PEEK[HEADER.FIELDS (DATE FROM TO SUBJECT CONTENT-TYPE "; |
| | | $request .= "LIST-POST DISPOSITION-NOTIFICATION-TO".$add.")])"; |
| | | $request .= "CC REPLY-TO LIST-POST DISPOSITION-NOTIFICATION-TO".$add.")])"; |
| | | |
| | | if (!$this->putLine($request)) { |
| | | $this->setError(self::ERROR_COMMAND, "Unable to send command: $request"); |
| | |
| | | // INTERNALDATE "16-Nov-2008 21:08:46 +0100" BODYSTRUCTURE (...) |
| | | // BODY[HEADER.FIELDS ... |
| | | |
| | | if (preg_match('/^\* [0-9]+ FETCH \((.*) BODY/s', $line, $matches)) { |
| | | if (preg_match('/^\* [0-9]+ FETCH \((.*) BODY/sU', $line, $matches)) { |
| | | $str = $matches[1]; |
| | | |
| | | // swap parents with quotes, then explode |
| | | $str = preg_replace('/[()]/', '"', $str); |
| | | $a = rcube_explode_quoted_string(' ', $str); |
| | | |
| | | // did we get the right number of replies? |
| | | $parts_count = count($a); |
| | | if ($parts_count>=6) { |
| | | for ($i=0; $i<$parts_count; $i=$i+2) { |
| | | if ($a[$i] == 'UID') { |
| | | $result[$id]->uid = intval($a[$i+1]); |
| | | } |
| | | else if ($a[$i] == 'RFC822.SIZE') { |
| | | $result[$id]->size = intval($a[$i+1]); |
| | | } |
| | | else if ($a[$i] == 'INTERNALDATE') { |
| | | $time_str = $a[$i+1]; |
| | | } |
| | | else if ($a[$i] == 'FLAGS') { |
| | | $flags_str = $a[$i+1]; |
| | | } |
| | | while (list($name, $value) = $this->tokenizeResponse($str, 2)) { |
| | | if ($name == 'UID') { |
| | | $result[$id]->uid = intval($value); |
| | | } |
| | | |
| | | $time_str = str_replace('"', '', $time_str); |
| | | |
| | | // if time is gmt... |
| | | $time_str = str_replace('GMT','+0000',$time_str); |
| | | |
| | | $result[$id]->internaldate = $time_str; |
| | | $result[$id]->timestamp = $this->StrToTime($time_str); |
| | | $result[$id]->date = $time_str; |
| | | else if ($name == 'RFC822.SIZE') { |
| | | $result[$id]->size = intval($value); |
| | | } |
| | | else if ($name == 'INTERNALDATE') { |
| | | $result[$id]->internaldate = $value; |
| | | $result[$id]->date = $value; |
| | | $result[$id]->timestamp = $this->StrToTime($value); |
| | | } |
| | | else if ($name == 'FLAGS') { |
| | | $flags_a = $value; |
| | | } |
| | | } |
| | | |
| | | // BODYSTRUCTURE |
| | | if ($bodystr) { |
| | | while (!preg_match('/ BODYSTRUCTURE (.*) BODY\[HEADER.FIELDS/s', $line, $m)) { |
| | | while (!preg_match('/ BODYSTRUCTURE (.*) BODY\[HEADER.FIELDS/sU', $line, $m)) { |
| | | $line2 = $this->readLine(1024); |
| | | $line .= $this->multLine($line2, true); |
| | | } |
| | |
| | | |
| | | // handle FLAGS reply after headers (AOL, Zimbra?) |
| | | if (preg_match('/\s+FLAGS \((.*)\)\)$/', $line, $matches)) { |
| | | $flags_str = $matches[1]; |
| | | $flags_a = $this->tokenizeResponse($matches[1]); |
| | | break; |
| | | } |
| | | |
| | |
| | | |
| | | // create array with header field:data |
| | | while (list($lines_key, $str) = each($lines)) { |
| | | list($field, $string) = $this->splitHeaderLine($str); |
| | | list($field, $string) = explode(':', $str, 2); |
| | | |
| | | $field = strtolower($field); |
| | | $string = preg_replace('/\n\s*/', ' ', $string); |
| | | $string = preg_replace('/\n[\t\s]*/', ' ', trim($string)); |
| | | |
| | | switch ($field) { |
| | | case 'date'; |
| | |
| | | break; |
| | | case 'content-type': |
| | | $ctype_parts = preg_split('/[; ]/', $string); |
| | | $result[$id]->ctype = array_shift($ctype_parts); |
| | | $result[$id]->ctype = strtolower(array_shift($ctype_parts)); |
| | | if (preg_match('/charset\s*=\s*"?([a-z0-9\-\.\_]+)"?/i', $string, $regs)) { |
| | | $result[$id]->charset = $regs[1]; |
| | | } |
| | |
| | | } |
| | | |
| | | // process flags |
| | | if (!empty($flags_str)) { |
| | | $flags_str = preg_replace('/[\\\"]/', '', $flags_str); |
| | | $flags_a = explode(' ', $flags_str); |
| | | if (!empty($flags_a)) { |
| | | foreach ($flags_a as $flag) { |
| | | $flag = str_replace('\\', '', $flag); |
| | | $result[$id]->flags[] = $flag; |
| | | |
| | | if (is_array($flags_a)) { |
| | | foreach($flags_a as $flag) { |
| | | $flag = strtoupper($flag); |
| | | if ($flag == 'SEEN') { |
| | | $result[$id]->seen = true; |
| | | } else if ($flag == 'DELETED') { |
| | | $result[$id]->deleted = true; |
| | | } else if ($flag == 'RECENT') { |
| | | $result[$id]->recent = true; |
| | | } else if ($flag == 'ANSWERED') { |
| | | $result[$id]->answered = true; |
| | | } else if ($flag == '$FORWARDED') { |
| | | $result[$id]->forwarded = true; |
| | | } else if ($flag == 'DRAFT') { |
| | | $result[$id]->is_draft = true; |
| | | } else if ($flag == '$MDNSENT') { |
| | | $result[$id]->mdn_sent = true; |
| | | } else if ($flag == 'FLAGGED') { |
| | | $result[$id]->flagged = true; |
| | | } |
| | | switch (strtoupper($flag)) { |
| | | case 'SEEN': |
| | | $result[$id]->seen = true; |
| | | break; |
| | | case 'DELETED': |
| | | $result[$id]->deleted = true; |
| | | break; |
| | | case 'ANSWERED': |
| | | $result[$id]->answered = true; |
| | | break; |
| | | case '$FORWARDED': |
| | | $result[$id]->forwarded = true; |
| | | break; |
| | | case '$MDNSENT': |
| | | $result[$id]->mdn_sent = true; |
| | | break; |
| | | case 'FLAGGED': |
| | | $result[$id]->flagged = true; |
| | | break; |
| | | } |
| | | $result[$id]->flags = $flags_a; |
| | | } |
| | | } |
| | | } |
| | |
| | | while ($this->tokenizeResponse($response, 1) == '*') { |
| | | $cmd = strtoupper($this->tokenizeResponse($response, 1)); |
| | | // * LIST (<options>) <delimiter> <mailbox> |
| | | if (!$lstatus || $cmd == 'LIST' || $cmd == 'LSUB') { |
| | | if ($cmd == 'LIST' || $cmd == 'LSUB') { |
| | | list($opts, $delim, $mailbox) = $this->tokenizeResponse($response, 3); |
| | | |
| | | // Add to result array |
| | |
| | | list($name, $value) = $this->tokenizeResponse($status, 2); |
| | | $folders[$mailbox][$name] = $value; |
| | | } |
| | | } |
| | | // other untagged response line, skip it |
| | | else { |
| | | $response = ltrim($response); |
| | | if (($position = strpos($response, "\n")) !== false) |
| | | $response = substr($response, $position+1); |
| | | else |
| | | $response = ''; |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | function getACL($mailbox) |
| | | { |
| | | list($code, $response) = $this->execute('GETACL', $this->escape($mailbox)); |
| | | list($code, $response) = $this->execute('GETACL', array($this->escape($mailbox))); |
| | | |
| | | if ($code == self::ERROR_OK && preg_match('/^\* ACL /i', $response)) { |
| | | // Parse server response (remove "* ACL ") |
| | | $response = substr($response, 6); |
| | | $ret = $this->tokenizeResponse($response); |
| | | $mbox = array_unshift($ret); |
| | | $mbox = array_shift($ret); |
| | | $size = count($ret); |
| | | |
| | | // Create user-rights hash array |
| | |
| | | */ |
| | | function myRights($mailbox) |
| | | { |
| | | list($code, $response) = $this->execute('MYRIGHTS', array($this->escape(mailbox))); |
| | | list($code, $response) = $this->execute('MYRIGHTS', array($this->escape($mailbox))); |
| | | |
| | | if ($code == self::ERROR_OK && preg_match('/^\* MYRIGHTS /i', $response)) { |
| | | // Parse server response (remove "* MYRIGHTS ") |
| | |
| | | } |
| | | |
| | | foreach ($entries as $name => $value) { |
| | | if ($value === null) { |
| | | $value = 'NIL'; |
| | | } |
| | | else { |
| | | $value = sprintf("{%d}\r\n%s", strlen($value), $value); |
| | | } |
| | | $entries[$name] = $this->escape($name) . ' ' . $value; |
| | | $entries[$name] = $this->escape($name) . ' ' . $this->escape($value); |
| | | } |
| | | |
| | | $entries = implode(' ', $entries); |
| | |
| | | list($code, $response) = $this->execute('GETMETADATA', array( |
| | | $this->escape($mailbox), $optlist)); |
| | | |
| | | if ($code == self::ERROR_OK && preg_match('/^\* METADATA /i', $response)) { |
| | | // Parse server response (remove "* METADATA ") |
| | | $response = substr($response, 11); |
| | | $ret_mbox = $this->tokenizeResponse($response, 1); |
| | | $data = $this->tokenizeResponse($response); |
| | | if ($code == self::ERROR_OK) { |
| | | $result = array(); |
| | | $data = $this->tokenizeResponse($response); |
| | | |
| | | // The METADATA response can contain multiple entries in a single |
| | | // response or multiple responses for each entry or group of entries |
| | | if (!empty($data) && ($size = count($data))) { |
| | | for ($i=0; $i<$size; $i++) { |
| | | if (is_array($data[$i])) { |
| | | if (isset($mbox) && is_array($data[$i])) { |
| | | $size_sub = count($data[$i]); |
| | | for ($x=0; $x<$size_sub; $x++) { |
| | | $data[$data[$i][$x]] = $data[$i][++$x]; |
| | | $result[$mbox][$data[$i][$x]] = $data[$i][++$x]; |
| | | } |
| | | unset($data[$i]); |
| | | } |
| | | else if ($data[$i] == '*' && $data[$i+1] == 'METADATA') { |
| | | unset($data[$i]); // "*" |
| | | unset($data[++$i]); // "METADATA" |
| | | unset($data[++$i]); // Mailbox |
| | | else if ($data[$i] == '*') { |
| | | if ($data[$i+1] == 'METADATA') { |
| | | $mbox = $data[$i+2]; |
| | | unset($data[$i]); // "*" |
| | | unset($data[++$i]); // "METADATA" |
| | | unset($data[++$i]); // Mailbox |
| | | } |
| | | // get rid of other untagged responses |
| | | else { |
| | | unset($mbox); |
| | | unset($data[$i]); |
| | | } |
| | | } |
| | | else { |
| | | $data[$data[$i]] = $data[++$i]; |
| | | else if (isset($mbox)) { |
| | | $result[$mbox][$data[$i]] = $data[++$i]; |
| | | unset($data[$i]); |
| | | unset($data[$i-1]); |
| | | } |
| | | else { |
| | | unset($data[$i]); |
| | | } |
| | | } |
| | | } |
| | | |
| | | return $data; |
| | | return $result; |
| | | } |
| | | |
| | | return NULL; |
| | |
| | | } |
| | | |
| | | foreach ($data as $entry) { |
| | | $name = $entry[0]; |
| | | $attr = $entry[1]; |
| | | $value = $entry[2]; |
| | | |
| | | if ($value === null) { |
| | | $value = 'NIL'; |
| | | } |
| | | else { |
| | | $value = sprintf("{%d}\r\n%s", strlen($value), $value); |
| | | } |
| | | |
| | | $entries[] = sprintf('%s (%s %s)', |
| | | $this->escape($name), $this->escape($attr), $value); |
| | | // ANNOTATEMORE drafts before version 08 require quoted parameters |
| | | $entries[] = sprintf('%s (%s %s)', $this->escape($entry[0], true), |
| | | $this->escape($entry[1], true), $this->escape($entry[2], true)); |
| | | } |
| | | |
| | | $entries = implode(' ', $entries); |
| | |
| | | $entries = array($entries); |
| | | } |
| | | // create entries string |
| | | // ANNOTATEMORE drafts before version 08 require quoted parameters |
| | | foreach ($entries as $idx => $name) { |
| | | $entries[$idx] = $this->escape($name); |
| | | $entries[$idx] = $this->escape($name, true); |
| | | } |
| | | $entries = '(' . implode(' ', $entries) . ')'; |
| | | |
| | |
| | | } |
| | | // create entries string |
| | | foreach ($attribs as $idx => $name) { |
| | | $attribs[$idx] = $this->escape($name); |
| | | $attribs[$idx] = $this->escape($name, true); |
| | | } |
| | | $attribs = '(' . implode(' ', $attribs) . ')'; |
| | | |
| | | list($code, $response) = $this->execute('GETANNOTATION', array( |
| | | $this->escape($mailbox), $entries, $attribs)); |
| | | |
| | | if ($code == self::ERROR_OK && preg_match('/^\* ANNOTATION /i', $response)) { |
| | | // Parse server response (remove "* ANNOTATION ") |
| | | $response = substr($response, 13); |
| | | $ret_mbox = $this->tokenizeResponse($response, 1); |
| | | $data = $this->tokenizeResponse($response); |
| | | $res = array(); |
| | | if ($code == self::ERROR_OK) { |
| | | $result = array(); |
| | | $data = $this->tokenizeResponse($response); |
| | | |
| | | // Here we returns only data compatible with METADATA result format |
| | | if (!empty($data) && ($size = count($data))) { |
| | | for ($i=0; $i<$size; $i++) { |
| | | $entry = $data[$i++]; |
| | | if (is_array($entry)) { |
| | | $entry = $data[$i]; |
| | | if (isset($mbox) && is_array($entry)) { |
| | | $attribs = $entry; |
| | | $entry = $last_entry; |
| | | } |
| | | else |
| | | $attribs = $data[$i++]; |
| | | else if ($entry == '*') { |
| | | if ($data[$i+1] == 'ANNOTATION') { |
| | | $mbox = $data[$i+2]; |
| | | unset($data[$i]); // "*" |
| | | unset($data[++$i]); // "ANNOTATION" |
| | | unset($data[++$i]); // Mailbox |
| | | } |
| | | // get rid of other untagged responses |
| | | else { |
| | | unset($mbox); |
| | | unset($data[$i]); |
| | | } |
| | | continue; |
| | | } |
| | | else if (isset($mbox)) { |
| | | $attribs = $data[++$i]; |
| | | } |
| | | else { |
| | | unset($data[$i]); |
| | | continue; |
| | | } |
| | | |
| | | if (!empty($attribs)) { |
| | | for ($x=0, $len=count($attribs); $x<$len;) { |
| | | $attr = $attribs[$x++]; |
| | | $value = $attribs[$x++]; |
| | | if ($attr == 'value.priv') { |
| | | $res['/private' . $entry] = $value; |
| | | $result[$mbox]['/private' . $entry] = $value; |
| | | } |
| | | else if ($attr == 'value.shared') { |
| | | $res['/shared' . $entry] = $value; |
| | | $result[$mbox]['/shared' . $entry] = $value; |
| | | } |
| | | } |
| | | } |
| | | $last_entry = $entry; |
| | | unset($data[$i-1]); |
| | | unset($data[$i-2]); |
| | | unset($data[$i]); |
| | | } |
| | | } |
| | | |
| | | return $res; |
| | | return $result; |
| | | } |
| | | |
| | | return NULL; |
| | |
| | | break; |
| | | } |
| | | |
| | | // excluded chars: SP, CTL, (, ), {, ", ], % |
| | | if (preg_match('/^([\x21\x23\x24\x26\x27\x2A-\x5C\x5E-\x7A\x7C-\x7E]+)/', $str, $m)) { |
| | | // excluded chars: SP, CTL, ) |
| | | if (preg_match('/^([^\x00-\x20\x29\x7F]+)/', $str, $m)) { |
| | | $result[] = $m[1] == 'NIL' ? NULL : $m[1]; |
| | | $str = substr($str, strlen($m[1])); |
| | | } |
| | |
| | | * |
| | | * @return int Unix timestamp |
| | | */ |
| | | private function strToTime($date) |
| | | static function strToTime($date) |
| | | { |
| | | // support non-standard "GMTXXXX" literal |
| | | $date = preg_replace('/GMT\s*([+-][0-9]+)/', '\\1', $date); |
| | | // if date parsing fails, we have a date in non-rfc format. |
| | | |
| | | // if date parsing fails, we have a date in non-rfc format |
| | | // remove token from the end and try again |
| | | while ((($ts = @strtotime($date))===false) || ($ts < 0)) { |
| | | while (($ts = intval(@strtotime($date))) <= 0) { |
| | | $d = explode(' ', $date); |
| | | array_pop($d); |
| | | if (!$d) { |
| | | if (empty($d)) { |
| | | break; |
| | | } |
| | | $date = implode(' ', $d); |
| | | } |
| | | |
| | | $ts = (int) $ts; |
| | | |
| | | return $ts < 0 ? 0 : $ts; |
| | | } |
| | | |
| | | private function splitHeaderLine($string) |
| | | { |
| | | $pos = strpos($string, ':'); |
| | | if ($pos>0) { |
| | | $res[0] = substr($string, 0, $pos); |
| | | $res[1] = trim(substr($string, $pos+1)); |
| | | return $res; |
| | | } |
| | | return $string; |
| | | } |
| | | |
| | | private function parseCapability($str, $trusted=false) |
| | |
| | | /** |
| | | * Escapes a string when it contains special characters (RFC3501) |
| | | * |
| | | * @param string $string IMAP string |
| | | * @param string $string IMAP string |
| | | * @param boolean $force_quotes Forces string quoting (for atoms) |
| | | * |
| | | * @return string Escaped string |
| | | * @todo String literals, lists |
| | | * @return string String atom, quoted-string or string literal |
| | | * @todo lists |
| | | */ |
| | | static function escape($string) |
| | | static function escape($string, $force_quotes=false) |
| | | { |
| | | if ($string === null) { |
| | | return 'NIL'; |
| | | } |
| | | else if ($string === '') { |
| | | if ($string === '') { |
| | | return '""'; |
| | | } |
| | | else if (preg_match('/([\x00-\x20\x28-\x29\x7B\x25\x2A\x22\x5C\x5D\x7F]+)/', $string)) { |
| | | // string: special chars: SP, CTL, (, ), {, %, *, ", \, ] |
| | | return '"' . strtr($string, array('"'=>'\\"', '\\' => '\\\\')) . '"'; |
| | | // atom-string (only safe characters) |
| | | if (!$force_quotes && !preg_match('/[\x00-\x20\x22\x28-\x2A\x5B-\x5D\x7B\x7D\x80-\xFF]/', $string)) { |
| | | return $string; |
| | | } |
| | | // quoted-string |
| | | if (!preg_match('/[\r\n\x00\x80-\xFF]/', $string)) { |
| | | return '"' . addcslashes($string, '\\"') . '"'; |
| | | } |
| | | |
| | | // atom |
| | | return $string; |
| | | // literal-string |
| | | return sprintf("{%d}\r\n%s", strlen($string), $string); |
| | | } |
| | | |
| | | static function unEscape($string) |
| | | { |
| | | return strtr($string, array('\\"'=>'"', '\\\\' => '\\')); |
| | | return stripslashes($string); |
| | | } |
| | | |
| | | /** |
| | | * Set the value of the debugging flag. |
| | | * |
| | | * @param boolean $debug New value for the debugging flag. |
| | | * |
| | | * @access public |
| | | * @since 0.5-stable |
| | | */ |
| | | function setDebug($debug, $handler = null) |
| | | { |
| | | $this->_debug = $debug; |
| | | $this->_debug_handler = $handler; |
| | | } |
| | | |
| | | /** |
| | | * Write the given debug text to the current debug output handler. |
| | | * |
| | | * @param string $message Debug mesage text. |
| | | * |
| | | * @access private |
| | | * @since 0.5-stable |
| | | */ |
| | | private function debug($message) |
| | | { |
| | | if ($this->_debug_handler) { |
| | | call_user_func_array($this->_debug_handler, array(&$this, $message)); |
| | | } else { |
| | | echo "DEBUG: $message\n"; |
| | | } |
| | | } |
| | | |
| | | } |