From 7786ba1adb415fc8fd4478380d7201702a799483 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Fri, 20 May 2011 04:00:40 -0400
Subject: [PATCH] - Fix error when rcube_cache::remove() was used in pattern mode

---
 program/include/rcube_imap_generic.php |  189 +++++++++++++++++++++-------------------------
 1 files changed, 87 insertions(+), 102 deletions(-)

diff --git a/program/include/rcube_imap_generic.php b/program/include/rcube_imap_generic.php
index 5bf1ce5..59e314c 100644
--- a/program/include/rcube_imap_generic.php
+++ b/program/include/rcube_imap_generic.php
@@ -171,13 +171,15 @@
         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)
@@ -201,7 +203,6 @@
                 }
             }
         }
-
         return $res;
     }
 
@@ -953,6 +954,16 @@
 
             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];
             }
@@ -1193,8 +1204,8 @@
     {
         if (is_array($items) && !empty($items)) {
             foreach ($items as $key => $value) {
-                $args[] = $this->escape($key);
-                $args[] = $this->escape($value);
+                $args[] = $this->escape($key, true);
+                $args[] = $this->escape($value, true);
             }
         }
 
@@ -1205,7 +1216,7 @@
 
         if ($code == self::ERROR_OK && preg_match('/\* ID /i', $response)) {
             $response = substr($response, 5); // remove prefix "* ID "
-            $items    = $this->tokenizeResponse($response);
+            $items    = $this->tokenizeResponse($response, 1);
             $result   = null;
 
             for ($i=0, $len=count($items); $i<$len; $i += 2) {
@@ -1558,36 +1569,21 @@
                 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
@@ -1635,7 +1631,7 @@
 
                     // 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;
                     }
 
@@ -1659,10 +1655,10 @@
 
                     // 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';
@@ -1726,28 +1722,31 @@
                 }
 
                 // 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 == 'ANSWERED') {
-                                $result[$id]->answered = true;
-                            } else if ($flag == '$FORWARDED') {
-                                $result[$id]->forwarded = 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;
                     }
                 }
             }
@@ -2800,13 +2799,7 @@
         }
 
         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);
@@ -2958,20 +2951,9 @@
         }
 
         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);
-            }
-
             // ANNOTATEMORE drafts before version 08 require quoted parameters
-            $entries[] = sprintf('%s (%s %s)',
-                $this->escape($name, true), $this->escape($attr, true), $value);
+            $entries[] = sprintf('%s (%s %s)', $this->escape($entry[0], true),
+                $this->escape($entry[1], true), $this->escape($entry[2], true));
         }
 
         $entries = implode(' ', $entries);
@@ -3278,21 +3260,23 @@
      *
      * @return int Unix timestamp
      */
-    private function strToTime($date)
+    static function strToTime($date)
     {
-        $ts = (int) rcube_strtotime($date);
-        return $ts < 0 ? 0 : $ts;
-    }
+        // support non-standard "GMTXXXX" literal
+        $date = preg_replace('/GMT\s*([+-][0-9]+)/', '\\1', $date);
 
-    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;
+        // if date parsing fails, we have a date in non-rfc format
+        // remove token from the end and try again
+        while (($ts = intval(@strtotime($date))) <= 0) {
+            $d = explode(' ', $date);
+            array_pop($d);
+            if (empty($d)) {
+                break;
+            }
+            $date = implode(' ', $d);
         }
-        return $string;
+
+        return $ts < 0 ? 0 : $ts;
     }
 
     private function parseCapability($str, $trusted=false)
@@ -3314,29 +3298,30 @@
      * Escapes a string when it contains special characters (RFC3501)
      *
      * @param string  $string       IMAP string
-     * @param boolean $force_quotes Forces string quoting
+     * @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, $force_quotes=false)
     {
         if ($string === null) {
             return 'NIL';
         }
-        else if ($string === '') {
+        if ($string === '') {
             return '""';
         }
-        // need quoted-string? find special chars: SP, CTL, (, ), {, %, *, ", \, ]
-        // plus [ character as a workaround for DBMail's bug (#1487766)
-        else if ($force_quotes ||
-            preg_match('/([\x00-\x20\x28-\x29\x7B\x25\x2A\x22\x5B\x5C\x5D\x7F]+)/', $string)
-        ) {
+        // 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)

--
Gitblit v1.9.1