Changeset 4423 in subversion


Ignore:
Timestamp:
Jan 18, 2011 6:50:46 AM (2 years ago)
Author:
alec
Message:
  • Fix multi-line strings parsing (#1487685)
Location:
trunk/plugins/managesieve
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/plugins/managesieve/Changelog

    r4241 r4423  
    22- Fixed parsing of scripts with \r\n line separator 
    33- Apply forgotten changes for form errors handling 
     4- Fix multi-line strings parsing (#1487685) 
    45 
    56* version 2.10 [2010-10-10] 
  • trunk/plugins/managesieve/lib/rcube_sieve.php

    r4241 r4423  
    586586                    array_push($exts, $action['type']); 
    587587                    if (strpos($action['target'], "\n")!==false) 
    588                         $script .= "\t".$action['type']." text:\n" . $action['target'] . "\n.\n;\n"; 
     588                        $script .= "\t".$action['type']." text:\n" 
     589                            . $this->_escape_multiline_string($action['target']) . "\n.\n;\n"; 
    589590                    else 
    590                         $script .= "\t".$action['type']." \"" . $this->_escape_string($action['target']) . "\";\n"; 
     591                        $script .= "\t".$action['type']." \"" 
     592                            . $this->_escape_string($action['target']) . "\";\n"; 
    591593                    break; 
    592594                case 'keep': 
     
    611613                        $script .= " :mime"; 
    612614                    if (strpos($action['reason'], "\n")!==false) 
    613                         $script .= " text:\n" . $action['reason'] . "\n.\n;\n"; 
     615                        $script .= " text:\n" . $this->_escape_multiline_string($action['reason']) . "\n.\n;\n"; 
    614616                    else 
    615617                        $script .= " \"" . $this->_escape_string($action['reason']) . "\";\n"; 
     
    910912        $content = trim($content); 
    911913 
    912         if (preg_match('/^text:(.*)\.$/sm', $content, $matches)) 
    913             $text = trim($matches[1]); 
    914         else if (preg_match('/^"(.*)"$/', $content, $matches)) 
     914        // multi-line string 
     915        if (preg_match('/^(text:[\t\s\r]*\n)/', $content, $matches)) { 
     916            $content = substr($content, strlen($matches[1])); 
     917            $lines   = preg_split('/(\r?\n)/', $content, -1, PREG_SPLIT_DELIM_CAPTURE); 
     918            $text    = ''; 
     919 
     920            foreach ($lines as $line) { 
     921                // terminator 
     922                if ($line == '.') { 
     923                    break; 
     924                } 
     925                // dot-stuffing 
     926                if ($line[0] == '.' && $line[1] == '.') { 
     927                    $line = substr($line, 1); 
     928                } 
     929                $text .= $line; 
     930            } 
     931            $text = rtrim($text, "\r\n"); 
     932        } 
     933        // quoted string 
     934        else if (preg_match('/^"(.*)"$/', $content, $matches)) { 
    915935            $text = str_replace('\"', '"', $matches[1]); 
     936        } 
    916937 
    917938        return $text; 
     
    919940 
    920941    /** 
    921      * Escape special chars in string value 
     942     * Escape special chars in quoted string value 
    922943     * 
    923944     * @param string Text 
     
    928949 
    929950        if (is_array($content)) { 
    930             for ($x=0, $y=sizeof($content); $x<$y; $x++) 
     951            for ($x=0, $y=sizeof($content); $x<$y; $x++) { 
    931952                $content[$x] = preg_replace(array_keys($replace), 
    932953                    array_values($replace), $content[$x]); 
     954            } 
    933955 
    934956            return $content; 
    935957        } 
    936         else 
     958        else { 
    937959            return preg_replace(array_keys($replace), array_values($replace), $content); 
     960        } 
     961    } 
     962 
     963    /** 
     964     * Escape special chars in multi-line string value 
     965     * 
     966     * @param string Text 
     967     */ 
     968    private function _escape_multiline_string($content) 
     969    { 
     970        $lines = preg_split('/(\r?\n)/', $content, -1, PREG_SPLIT_DELIM_CAPTURE); 
     971 
     972        foreach ($lines as $idx => $line) { 
     973            // dot-stuffing 
     974            if ($line[0] == '.') { 
     975                $lines[$idx] = '.' . $line; 
     976            } 
     977        } 
     978 
     979        return implode($lines); 
    938980    } 
    939981 
Note: See TracChangeset for help on using the changeset viewer.