alecpl
2010-10-19 a85f8892086d84f5aca5f744f11bf1c2321b5dc6
- Use better method for string escaping, don't add quotes when the string is a token


1 files modified
83 ■■■■■ changed files
program/include/rcube_imap_generic.php 83 ●●●●● patch | view | raw | blame | history
program/include/rcube_imap_generic.php
@@ -236,7 +236,7 @@
                $out .= $line;
            }
            $line = $a[1][0] . '"' . ($escape ? $this->Escape($out) : $out) . '"';
            $line = $a[1][0] . ($escape ? $this->escape($out) : $out);
        }
        return $line;
@@ -415,7 +415,8 @@
    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);
@@ -733,7 +734,7 @@
            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");
@@ -1472,7 +1473,7 @@
            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");
@@ -1666,9 +1667,7 @@
            $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)) {
@@ -1904,7 +1903,7 @@
    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");
@@ -1920,7 +1919,7 @@
    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");
@@ -1935,7 +1934,7 @@
    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");
@@ -1959,7 +1958,7 @@
    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");
@@ -1972,7 +1971,7 @@
    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");
@@ -1997,7 +1996,7 @@
            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)) {
@@ -2057,7 +2056,7 @@
        }
        // 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)) {
@@ -2200,7 +2199,7 @@
        }
        $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)) {
@@ -2226,7 +2225,7 @@
    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)) {
@@ -2250,7 +2249,7 @@
    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");
@@ -2304,7 +2303,7 @@
    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)) {
@@ -2349,7 +2348,7 @@
    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");        
@@ -2399,12 +2398,12 @@
            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)) {
@@ -2463,7 +2462,7 @@
        // create entries string
        foreach ($entries as $idx => $name) {
            $entries[$idx] = '"' . $this->escape($name) . '"';
            $entries[$idx] = $this->escape($name);
        }
        $optlist = '';
@@ -2486,7 +2485,7 @@
        $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)) {
@@ -2565,13 +2564,13 @@
            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)) {
@@ -2624,7 +2623,7 @@
        }
        // create entries string
        foreach ($entries as $idx => $name) {
            $entries[$idx] = '"' . $this->escape($name) . '"';
            $entries[$idx] = $this->escape($name);
        }
        $entries = '(' . implode(' ', $entries) . ')';
@@ -2633,12 +2632,12 @@
        }
        // 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)) {
@@ -2875,12 +2874,34 @@
        }
    }
    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('"'=>'\\"', '\\' => '\\\\')) . '"';
    }
    private function unEscape($string)
        // atom
        return $string;
    }
    static function unEscape($string)
    {
        return strtr($string, array('\\"'=>'"', '\\\\' => '\\'));
    }