| | |
| | | // LITERAL+ support |
| | | if ($this->prefs['literal+']) |
| | | $parts[$i+1] = preg_replace('/([0-9]+)/', '\\1+', $parts[$i+1]); |
| | | |
| | | |
| | | $bytes = $this->putLine($parts[$i].$parts[$i+1], false); |
| | | if ($bytes === false) |
| | | return false; |
| | |
| | | $out .= $line; |
| | | } |
| | | |
| | | $line = $a[1][0] . '"' . ($escape ? $this->Escape($out) : $out) . '"'; |
| | | $line = $a[1][0] . ($escape ? $this->escape($out) : $out); |
| | | } |
| | | |
| | | return $line; |
| | |
| | | |
| | | function login($user, $password) |
| | | { |
| | | $this->putLine('a001 LOGIN "'.$this->escape($user).'" "'.$this->escape($password).'"'); |
| | | $this->putLine(sprintf("a001 LOGIN %s %s", |
| | | $this->escape($user), $this->escape($password))); |
| | | |
| | | $line = $this->readReply($untagged); |
| | | |
| | |
| | | return true; |
| | | } |
| | | |
| | | $command = "sel1 SELECT \"".$this->escape($mailbox).'"'; |
| | | $command = "sel1 SELECT " . $this->escape($mailbox); |
| | | |
| | | if (!$this->putLine($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | |
| | | return -1; |
| | | } |
| | | |
| | | $command = "cpy1 UID COPY $messages \"".$this->escape($to)."\""; |
| | | $command = "cpy1 UID COPY $messages ".$this->escape($to); |
| | | |
| | | if (!$this->putLine($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | |
| | | $command = 'LIST'; |
| | | } |
| | | |
| | | $ref = $this->escape($ref); |
| | | $mailbox = $this->escape($mailbox); |
| | | $query = $key." ".$command." \"". $ref ."\" \"". $mailbox ."\""; |
| | | $query = sprintf("%s %s %s %s", $key, $command, $this->escape($ref), $this->escape($mailbox)); |
| | | |
| | | // send command |
| | | if (!$this->putLine($query)) { |
| | |
| | | |
| | | function createFolder($folder) |
| | | { |
| | | $command = 'c CREATE "' . $this->escape($folder) . '"'; |
| | | |
| | | $command = sprintf("c CREATE %s", $this->escape($folder)); |
| | | |
| | | if (!$this->putLine($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | return false; |
| | |
| | | |
| | | function renameFolder($from, $to) |
| | | { |
| | | $command = 'r RENAME "' . $this->escape($from) . '" "' . $this->escape($to) . '"'; |
| | | |
| | | $command = sprintf("r RENAME %s %s", $this->escape($from), $this->escape($to)); |
| | | |
| | | if (!$this->putLine($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | return false; |
| | |
| | | |
| | | function deleteFolder($folder) |
| | | { |
| | | $command = 'd DELETE "' . $this->escape($folder). '"'; |
| | | |
| | | $command = sprintf("d DELETE %s", $this->escape($folder)); |
| | | |
| | | if (!$this->putLine($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | return false; |
| | |
| | | |
| | | function subscribe($folder) |
| | | { |
| | | $command = 'sub1 SUBSCRIBE "' . $this->escape($folder). '"'; |
| | | $command = sprintf("sub1 SUBSCRIBE %s", $this->escape($folder)); |
| | | |
| | | if (!$this->putLine($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | |
| | | |
| | | function unsubscribe($folder) |
| | | { |
| | | $command = 'usub1 UNSUBSCRIBE "' . $this->escape($folder) . '"'; |
| | | $command = sprintf("usub1 UNSUBSCRIBE %s", $this->escape($folder)); |
| | | |
| | | if (!$this->putLine($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | |
| | | return false; |
| | | } |
| | | |
| | | $request = sprintf("a APPEND \"%s\" (\\Seen) {%d%s}", $this->escape($folder), |
| | | $request = sprintf("a APPEND %s (\\Seen) {%d%s}", $this->escape($folder), |
| | | $len, ($this->prefs['literal+'] ? '+' : '')); |
| | | |
| | | if ($this->putLine($request)) { |
| | |
| | | } |
| | | |
| | | // send APPEND command |
| | | $request = sprintf("a APPEND \"%s\" (\\Seen) {%d%s}", $this->escape($folder), |
| | | $request = sprintf("a APPEND %s (\\Seen) {%d%s}", $this->escape($folder), |
| | | $len, ($this->prefs['literal+'] ? '+' : '')); |
| | | |
| | | if ($this->putLine($request)) { |
| | |
| | | $line = $this->readLine(); |
| | | } while (!$this->startsWith($line, 'a ', true, true)); |
| | | |
| | | |
| | | |
| | | return ($this->parseResult($line, 'APPEND: ') == self::ERROR_OK); |
| | | } |
| | | else { |
| | |
| | | } |
| | | |
| | | $key = 'acl1'; |
| | | $command = sprintf("%s SETACL \"%s\" \"%s\" %s", |
| | | $command = sprintf("%s SETACL %s %s %s", |
| | | $key, $this->escape($mailbox), $this->escape($user), strtolower($acl)); |
| | | |
| | | if (!$this->putLine($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | return false; |
| | | } |
| | | |
| | |
| | | function deleteACL($mailbox, $user) |
| | | { |
| | | $key = 'acl2'; |
| | | $command = sprintf("%s DELETEACL \"%s\" \"%s\"", |
| | | $command = sprintf("%s DELETEACL %s %s", |
| | | $key, $this->escape($mailbox), $this->escape($user)); |
| | | |
| | | if (!$this->putLine($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | return false; |
| | | } |
| | | |
| | |
| | | function getACL($mailbox) |
| | | { |
| | | $key = 'acl3'; |
| | | $command = sprintf("%s GETACL \"%s\"", $key, $this->escape($mailbox)); |
| | | $command = sprintf("%s GETACL %s", $key, $this->escape($mailbox)); |
| | | |
| | | if (!$this->putLine($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | |
| | | $line = $this->readLine(4096); |
| | | $response .= $line; |
| | | } while (!$this->startsWith($line, $key, true, true)); |
| | | |
| | | |
| | | if ($this->parseResult($line, 'GETACL: ') == self::ERROR_OK) { |
| | | // Parse server response (remove "* ACL " and "\r\nacl3 OK...") |
| | | $response = substr($response, 6, -(strlen($line)+2)); |
| | |
| | | function listRights($mailbox, $user) |
| | | { |
| | | $key = 'acl4'; |
| | | $command = sprintf("%s LISTRIGHTS \"%s\" \"%s\"", |
| | | $command = sprintf("%s LISTRIGHTS %s %s", |
| | | $key, $this->escape($mailbox), $this->escape($user)); |
| | | |
| | | if (!$this->putLine($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | return NULL; |
| | | } |
| | | |
| | |
| | | function myRights($mailbox) |
| | | { |
| | | $key = 'acl5'; |
| | | $command = sprintf("%s MYRIGHTS \"%s\"", $key, $this->escape(mailbox)); |
| | | $command = sprintf("%s MYRIGHTS %s", $key, $this->escape(mailbox)); |
| | | |
| | | if (!$this->putLine($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | return NULL; |
| | | } |
| | | |
| | |
| | | function setMetadata($mailbox, $entries) |
| | | { |
| | | if (!is_array($entries) || empty($entries)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Wrong argument for SETMETADATA command"); |
| | | $this->set_error(self::ERROR_COMMAND, "Wrong argument for SETMETADATA command"); |
| | | return false; |
| | | } |
| | | |
| | |
| | | else |
| | | $value = sprintf("{%d}\r\n%s", strlen($value), $value); |
| | | |
| | | $entries[$name] = '"' . $this->escape($name) . '" ' . $value; |
| | | $entries[$name] = $this->escape($name) . ' ' . $value; |
| | | } |
| | | |
| | | $entries = implode(' ', $entries); |
| | | $key = 'md1'; |
| | | $command = sprintf("%s SETMETADATA \"%s\" (%s)", |
| | | $command = sprintf("%s SETMETADATA %s (%s)", |
| | | $key, $this->escape($mailbox), $entries); |
| | | |
| | | if (!$this->putLineC($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | return false; |
| | | } |
| | | |
| | |
| | | |
| | | foreach ($entries as $entry) |
| | | $data[$entry] = NULL; |
| | | |
| | | |
| | | return $this->setMetadata($mailbox, $data); |
| | | } |
| | | |
| | |
| | | |
| | | // create entries string |
| | | foreach ($entries as $idx => $name) { |
| | | $entries[$idx] = '"' . $this->escape($name) . '"'; |
| | | $entries[$idx] = $this->escape($name); |
| | | } |
| | | |
| | | $optlist = ''; |
| | |
| | | $optlist .= ($optlist ? ' ' : '') . $entlist; |
| | | |
| | | $key = 'md2'; |
| | | $command = sprintf("%s GETMETADATA \"%s\" %s", |
| | | $command = sprintf("%s GETMETADATA %s %s", |
| | | $key, $this->escape($mailbox), $optlist); |
| | | |
| | | if (!$this->putLine($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | return NULL; |
| | | } |
| | | |
| | |
| | | if (is_array($data[$i])) { |
| | | $size_sub = count($data[$i]); |
| | | for ($x=0; $x<$size_sub; $x++) { |
| | | $data[$data[$i][$x]] = $data[$i][++$x]; |
| | | $data[$data[$i][$x]] = $data[$i][++$x]; |
| | | } |
| | | unset($data[$i]); |
| | | } |
| | |
| | | function setAnnotation($mailbox, $data) |
| | | { |
| | | if (!is_array($data) || empty($data)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Wrong argument for SETANNOTATION command"); |
| | | $this->set_error(self::ERROR_COMMAND, "Wrong argument for SETANNOTATION command"); |
| | | return false; |
| | | } |
| | | |
| | |
| | | else |
| | | $value = sprintf("{%d}\r\n%s", strlen($value), $value); |
| | | |
| | | $entries[] = sprintf('"%s" ("%s" %s)', |
| | | $entries[] = sprintf('%s (%s %s)', |
| | | $this->escape($name), $this->escape($attr), $value); |
| | | } |
| | | |
| | | $entries = implode(' ', $entries); |
| | | $key = 'an1'; |
| | | $command = sprintf("%s SETANNOTATION \"%s\" %s", |
| | | $command = sprintf("%s SETANNOTATION %s %s", |
| | | $key, $this->escape($mailbox), $entries); |
| | | |
| | | if (!$this->putLineC($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | return false; |
| | | } |
| | | |
| | |
| | | } |
| | | // create entries string |
| | | foreach ($entries as $idx => $name) { |
| | | $entries[$idx] = '"' . $this->escape($name) . '"'; |
| | | $entries[$idx] = $this->escape($name); |
| | | } |
| | | $entries = '(' . implode(' ', $entries) . ')'; |
| | | |
| | |
| | | } |
| | | // create entries string |
| | | foreach ($attribs as $idx => $name) { |
| | | $attribs[$idx] = '"' . $this->escape($name) . '"'; |
| | | $attribs[$idx] = $this->escape($name); |
| | | } |
| | | $attribs = '(' . implode(' ', $attribs) . ')'; |
| | | |
| | | $key = 'an2'; |
| | | $command = sprintf("%s GETANNOTATION \"%s\" %s %s", |
| | | $command = sprintf("%s GETANNOTATION %s %s %s", |
| | | $key, $this->escape($mailbox), $entries, $attribs); |
| | | |
| | | if (!$this->putLine($command)) { |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); |
| | | return NULL; |
| | | } |
| | | |
| | |
| | | |
| | | return $res; |
| | | } |
| | | |
| | | |
| | | return NULL; |
| | | } |
| | | |
| | |
| | | |
| | | // String atom, number, NIL, *, % |
| | | default: |
| | | // empty or one character |
| | | // empty or one character |
| | | if ($str === '') { |
| | | break 2; |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | private function escape($string) |
| | | /** |
| | | * Escapes a string when it contains special characters (RFC3501) |
| | | * |
| | | * @param string $string IMAP string |
| | | * |
| | | * @return string Escaped string |
| | | * @todo String literals, lists |
| | | */ |
| | | static function escape($string) |
| | | { |
| | | return strtr($string, array('"'=>'\\"', '\\' => '\\\\')); |
| | | // NIL |
| | | if ($string === null) { |
| | | return 'NIL'; |
| | | } |
| | | // empty string |
| | | else if ($string === '') { |
| | | return '""'; |
| | | } |
| | | // string: special chars: SP, CTL, (, ), {, %, *, ", \, ] |
| | | else if (preg_match('/([\x00-\x20\x28-\x29\x7B\x25\x2A\x22\x5C\x5D\x7F]+)/', $string)) { |
| | | return '"' . strtr($string, array('"'=>'\\"', '\\' => '\\\\')) . '"'; |
| | | } |
| | | |
| | | // atom |
| | | return $string; |
| | | } |
| | | |
| | | private function unEscape($string) |
| | | static function unEscape($string) |
| | | { |
| | | return strtr($string, array('\\"'=>'"', '\\\\' => '\\')); |
| | | } |