escape() should only add quotes if string needs to be quoted
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 21 Apr 2010 21:00:28 +0000 (15:00 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 22 Apr 2010 17:33:28 +0000 (11:33 -0600)
framework/Imap_Client/lib/Horde/Imap/Client/Utils.php

index 7d8dff5..105b675 100644 (file)
@@ -161,15 +161,25 @@ class Horde_Imap_Client_Utils
     }
 
     /**
-     * Escape IMAP output via a quoted string (see RFC 3501 [4.3]).
+     * Escape IMAP output via a quoted string (see RFC 3501 [4.3]). Note that
+     * IMAP quoted strings support 7-bit characters only and can not contain
+     * either CR or LF.
      *
-     * @param string $str  The unescaped string.
+     * @param string $str     The unescaped string.
+     * @param boolean $force  Always add quotes?
      *
      * @return string  The escaped string.
      */
-    public function escape($str)
+    public function escape($str, $force = false)
     {
-        return '"' . addcslashes($str, '"\\') . '"';
+        if (!strlen($str)) {
+            return '""';
+        }
+
+        $newstr = addcslashes($str, '"\\');
+        return (!$force && ($str == $newstr))
+            ? $str
+            : '"' . $newstr . '"';
     }
 
     /**