Changeset 4105 in subversion


Ignore:
Timestamp:
Oct 19, 2010 3:19:27 AM (3 years ago)
Author:
alec
Message:
  • Use better method for string escaping, don't add quotes when the string is a token
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/roundcubemail/program/include/rcube_imap_generic.php

    r4102 r4105  
    162162                    if ($this->prefs['literal+']) 
    163163                        $parts[$i+1] = preg_replace('/([0-9]+)/', '\\1+', $parts[$i+1]); 
    164                  
     164 
    165165                                    $bytes = $this->putLine($parts[$i].$parts[$i+1], false); 
    166166                    if ($bytes === false) 
     
    237237                    } 
    238238 
    239                     $line = $a[1][0] . '"' . ($escape ? $this->Escape($out) : $out) . '"'; 
     239                    $line = $a[1][0] . ($escape ? $this->escape($out) : $out); 
    240240            } 
    241241 
     
    416416    function login($user, $password) 
    417417    { 
    418         $this->putLine('a001 LOGIN "'.$this->escape($user).'" "'.$this->escape($password).'"'); 
     418        $this->putLine(sprintf("a001 LOGIN %s %s", 
     419            $this->escape($user), $this->escape($password))); 
    419420 
    420421        $line = $this->readReply($untagged); 
     
    734735            } 
    735736 
    736         $command = "sel1 SELECT \"".$this->escape($mailbox).'"'; 
     737        $command = "sel1 SELECT " . $this->escape($mailbox); 
    737738 
    738739            if (!$this->putLine($command)) { 
     
    14731474            } 
    14741475 
    1475         $command = "cpy1 UID COPY $messages \"".$this->escape($to)."\""; 
     1476        $command = "cpy1 UID COPY $messages ".$this->escape($to); 
    14761477 
    14771478        if (!$this->putLine($command)) { 
     
    16671668        } 
    16681669 
    1669         $ref = $this->escape($ref); 
    1670         $mailbox = $this->escape($mailbox); 
    1671         $query = $key." ".$command." \"". $ref ."\" \"". $mailbox ."\""; 
     1670        $query = sprintf("%s %s %s %s", $key, $command, $this->escape($ref), $this->escape($mailbox)); 
    16721671 
    16731672        // send command 
     
    19051904    function createFolder($folder) 
    19061905    { 
    1907         $command = 'c CREATE "' . $this->escape($folder) . '"'; 
    1908      
     1906        $command = sprintf("c CREATE %s", $this->escape($folder)); 
     1907 
    19091908            if (!$this->putLine($command)) { 
    19101909            $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); 
     
    19211920    function renameFolder($from, $to) 
    19221921    { 
    1923         $command = 'r RENAME "' . $this->escape($from) . '" "' . $this->escape($to) . '"'; 
    1924      
     1922        $command = sprintf("r RENAME %s %s", $this->escape($from), $this->escape($to)); 
     1923 
    19251924            if (!$this->putLine($command)) { 
    19261925            $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); 
     
    19361935    function deleteFolder($folder) 
    19371936    { 
    1938         $command = 'd DELETE "' . $this->escape($folder). '"'; 
    1939      
     1937        $command = sprintf("d DELETE %s", $this->escape($folder)); 
     1938 
    19401939            if (!$this->putLine($command)) { 
    19411940            $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); 
     
    19601959    function subscribe($folder) 
    19611960    { 
    1962             $command = 'sub1 SUBSCRIBE "' . $this->escape($folder). '"'; 
     1961            $command = sprintf("sub1 SUBSCRIBE %s", $this->escape($folder)); 
    19631962 
    19641963            if (!$this->putLine($command)) { 
     
    19731972    function unsubscribe($folder) 
    19741973    { 
    1975         $command = 'usub1 UNSUBSCRIBE "' . $this->escape($folder) . '"'; 
     1974        $command = sprintf("usub1 UNSUBSCRIBE %s", $this->escape($folder)); 
    19761975 
    19771976            if (!$this->putLine($command)) { 
     
    19981997            } 
    19991998 
    2000             $request = sprintf("a APPEND \"%s\" (\\Seen) {%d%s}", $this->escape($folder), 
     1999            $request = sprintf("a APPEND %s (\\Seen) {%d%s}", $this->escape($folder), 
    20012000            $len, ($this->prefs['literal+'] ? '+' : '')); 
    20022001 
     
    20582057 
    20592058        // send APPEND command 
    2060             $request = sprintf("a APPEND \"%s\" (\\Seen) {%d%s}", $this->escape($folder), 
     2059            $request = sprintf("a APPEND %s (\\Seen) {%d%s}", $this->escape($folder), 
    20612060            $len, ($this->prefs['literal+'] ? '+' : '')); 
    20622061 
     
    20932092                    } while (!$this->startsWith($line, 'a ', true, true)); 
    20942093 
    2095                      
     2094 
    20962095                    return ($this->parseResult($line, 'APPEND: ') == self::ERROR_OK); 
    20972096            } 
     
    22012200 
    22022201        $key     = 'acl1'; 
    2203         $command = sprintf("%s SETACL \"%s\" \"%s\" %s", 
     2202        $command = sprintf("%s SETACL %s %s %s", 
    22042203            $key, $this->escape($mailbox), $this->escape($user), strtolower($acl)); 
    22052204 
    22062205                if (!$this->putLine($command)) { 
    2207             $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command");         
     2206            $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); 
    22082207            return false; 
    22092208        } 
     
    22272226    { 
    22282227        $key     = 'acl2'; 
    2229         $command = sprintf("%s DELETEACL \"%s\" \"%s\"",  
     2228        $command = sprintf("%s DELETEACL %s %s", 
    22302229            $key, $this->escape($mailbox), $this->escape($user)); 
    22312230 
    22322231                if (!$this->putLine($command)) { 
    2233             $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command");         
     2232            $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); 
    22342233            return false; 
    22352234        } 
     
    22512250    { 
    22522251        $key     = 'acl3'; 
    2253         $command = sprintf("%s GETACL \"%s\"", $key, $this->escape($mailbox)); 
     2252        $command = sprintf("%s GETACL %s", $key, $this->escape($mailbox)); 
    22542253 
    22552254                if (!$this->putLine($command)) { 
     
    22642263            $response .= $line; 
    22652264        } while (!$this->startsWith($line, $key, true, true)); 
    2266          
     2265 
    22672266        if ($this->parseResult($line, 'GETACL: ') == self::ERROR_OK) { 
    22682267            // Parse server response (remove "* ACL " and "\r\nacl3 OK...") 
     
    23052304    { 
    23062305        $key     = 'acl4'; 
    2307         $command = sprintf("%s LISTRIGHTS \"%s\" \"%s\"", 
     2306        $command = sprintf("%s LISTRIGHTS %s %s", 
    23082307            $key, $this->escape($mailbox), $this->escape($user)); 
    23092308 
    23102309                if (!$this->putLine($command)) { 
    2311             $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command");         
     2310            $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); 
    23122311            return NULL; 
    23132312        } 
     
    23502349    { 
    23512350        $key = 'acl5'; 
    2352         $command = sprintf("%s MYRIGHTS \"%s\"", $key, $this->escape(mailbox)); 
     2351        $command = sprintf("%s MYRIGHTS %s", $key, $this->escape(mailbox)); 
    23532352 
    23542353                if (!$this->putLine($command)) { 
    2355             $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command");         
     2354            $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); 
    23562355            return NULL; 
    23572356        } 
     
    23902389    { 
    23912390        if (!is_array($entries) || empty($entries)) { 
    2392             $this->set_error(self::ERROR_COMMAND, "Wrong argument for SETMETADATA command");         
     2391            $this->set_error(self::ERROR_COMMAND, "Wrong argument for SETMETADATA command"); 
    23932392            return false; 
    23942393        } 
     
    24002399                $value = sprintf("{%d}\r\n%s", strlen($value), $value); 
    24012400 
    2402             $entries[$name] = '"' . $this->escape($name) . '" ' . $value; 
     2401            $entries[$name] = $this->escape($name) . ' ' . $value; 
    24032402        } 
    24042403 
    24052404        $entries = implode(' ', $entries); 
    24062405        $key     = 'md1'; 
    2407         $command = sprintf("%s SETMETADATA \"%s\" (%s)",  
     2406        $command = sprintf("%s SETMETADATA %s (%s)", 
    24082407            $key, $this->escape($mailbox), $entries); 
    24092408 
    24102409                if (!$this->putLineC($command)) { 
    2411             $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command");         
     2410            $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); 
    24122411            return false; 
    24132412        } 
     
    24402439        foreach ($entries as $entry) 
    24412440            $data[$entry] = NULL; 
    2442      
     2441 
    24432442        return $this->setMetadata($mailbox, $data); 
    24442443    } 
     
    24642463        // create entries string 
    24652464        foreach ($entries as $idx => $name) { 
    2466             $entries[$idx] = '"' . $this->escape($name) . '"'; 
     2465            $entries[$idx] = $this->escape($name); 
    24672466        } 
    24682467 
     
    24872486 
    24882487        $key     = 'md2'; 
    2489         $command = sprintf("%s GETMETADATA \"%s\" %s", 
     2488        $command = sprintf("%s GETMETADATA %s %s", 
    24902489            $key, $this->escape($mailbox), $optlist); 
    24912490 
    24922491                if (!$this->putLine($command)) { 
    2493             $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command");         
     2492            $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); 
    24942493            return NULL; 
    24952494        } 
     
    25152514                        $size_sub = count($data[$i]); 
    25162515                        for ($x=0; $x<$size_sub; $x++) { 
    2517                             $data[$data[$i][$x]] = $data[$i][++$x];                         
     2516                            $data[$data[$i][$x]] = $data[$i][++$x]; 
    25182517                        } 
    25192518                        unset($data[$i]); 
     
    25522551    { 
    25532552        if (!is_array($data) || empty($data)) { 
    2554             $this->set_error(self::ERROR_COMMAND, "Wrong argument for SETANNOTATION command");         
     2553            $this->set_error(self::ERROR_COMMAND, "Wrong argument for SETANNOTATION command"); 
    25552554            return false; 
    25562555        } 
     
    25662565                $value = sprintf("{%d}\r\n%s", strlen($value), $value); 
    25672566 
    2568             $entries[] = sprintf('"%s" ("%s" %s)', 
     2567            $entries[] = sprintf('%s (%s %s)', 
    25692568                $this->escape($name), $this->escape($attr), $value); 
    25702569        } 
     
    25722571        $entries = implode(' ', $entries); 
    25732572        $key     = 'an1'; 
    2574         $command = sprintf("%s SETANNOTATION \"%s\" %s", 
     2573        $command = sprintf("%s SETANNOTATION %s %s", 
    25752574            $key, $this->escape($mailbox), $entries); 
    25762575 
    25772576                if (!$this->putLineC($command)) { 
    2578             $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command");         
     2577            $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); 
    25792578            return false; 
    25802579        } 
     
    26252624        // create entries string 
    26262625        foreach ($entries as $idx => $name) { 
    2627             $entries[$idx] = '"' . $this->escape($name) . '"'; 
     2626            $entries[$idx] = $this->escape($name); 
    26282627        } 
    26292628        $entries = '(' . implode(' ', $entries) . ')'; 
     
    26342633        // create entries string 
    26352634        foreach ($attribs as $idx => $name) { 
    2636             $attribs[$idx] = '"' . $this->escape($name) . '"'; 
     2635            $attribs[$idx] = $this->escape($name); 
    26372636        } 
    26382637        $attribs = '(' . implode(' ', $attribs) . ')'; 
    26392638 
    26402639        $key     = 'an2'; 
    2641         $command = sprintf("%s GETANNOTATION \"%s\" %s %s", 
     2640        $command = sprintf("%s GETANNOTATION %s %s %s", 
    26422641            $key, $this->escape($mailbox), $entries, $attribs); 
    26432642 
    26442643                if (!$this->putLine($command)) { 
    2645             $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command");         
     2644            $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command"); 
    26462645            return NULL; 
    26472646        } 
     
    26902689            return $res; 
    26912690        } 
    2692          
     2691 
    26932692        return NULL; 
    26942693    } 
     
    27612760            // String atom, number, NIL, *, % 
    27622761            default: 
    2763                 // empty or one character       
     2762                // empty or one character 
    27642763                if ($str === '') { 
    27652764                    break 2; 
     
    28762875    } 
    28772876 
    2878     private function escape($string) 
    2879     { 
    2880             return strtr($string, array('"'=>'\\"', '\\' => '\\\\')); 
    2881     } 
    2882  
    2883     private function unEscape($string) 
     2877    /** 
     2878     * Escapes a string when it contains special characters (RFC3501) 
     2879     * 
     2880     * @param string $string IMAP string 
     2881     * 
     2882     * @return string Escaped string 
     2883     * @todo String literals, lists 
     2884     */ 
     2885    static function escape($string) 
     2886    { 
     2887        // NIL 
     2888        if ($string === null) { 
     2889            return 'NIL'; 
     2890        } 
     2891        // empty string 
     2892        else if ($string === '') { 
     2893            return '""'; 
     2894        } 
     2895        // string: special chars: SP, CTL, (, ), {, %, *, ", \, ] 
     2896        else if (preg_match('/([\x00-\x20\x28-\x29\x7B\x25\x2A\x22\x5C\x5D\x7F]+)/', $string)) { 
     2897                return '"' . strtr($string, array('"'=>'\\"', '\\' => '\\\\')) . '"'; 
     2898        } 
     2899 
     2900        // atom 
     2901        return $string; 
     2902    } 
     2903 
     2904    static function unEscape($string) 
    28842905    { 
    28852906            return strtr($string, array('\\"'=>'"', '\\\\' => '\\')); 
Note: See TracChangeset for help on using the changeset viewer.