Move header encoding from Mailbox into a generic function
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 15 Jun 2015 15:03:25 +0000 (17:03 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Tue, 23 Jun 2015 22:21:07 +0000 (00:21 +0200)
wcfsetup/install/files/lib/system/email/EmailGrammar.class.php
wcfsetup/install/files/lib/system/email/Mailbox.class.php

index 049153de38d0222145194ca92c6b08cba41abbc2..b185df430b0cfe92a8d329b412716d7390f5bdf2 100644 (file)
@@ -83,9 +83,30 @@ final class EmailGrammar {
         * @param       string  $header         Header to encode
         * @return      string                  Encoded header
         */
-       public static function encodeMimeHeader($header) {
+       public static function encodeQuotedPrintableHeader($header) {
                return mb_encode_mimeheader($header, "UTF-8", "Q", "\r\n");
        }
        
+       /**
+        * Return text either unmodified, if it matches the 'atom' grammar,
+        * in double quotes or encoded in quoted printable. Depending on which
+        * encoding is necessary.
+        *
+        * @param       string  $header         Header to encode
+        * @return      string                  Encoded header
+        */
+       public static function encodeHeader($header) {
+               if (!preg_match('(^'.self::getGrammar('atom').'$)', $header)) {
+                       if (($encoded = self::encodeQuotedPrintableHeader($header)) === $header) {
+                               $header = '"'.addcslashes($header, '\\"').'"';
+                       }
+                       else {
+                               $header = $encoded;
+                       }
+               }
+               
+               return $header;
+       }
+       
        private function __construct() { }
 }
index 8205c1e041c57fdfcd2b9e7e422b07f5c33f74d4..f94858e4e51f7a2df23e6bdde41d7e89694b8142 100644 (file)
@@ -68,16 +68,7 @@ class Mailbox {
                        return $this->address;
                }
                
-               $name = $this->name;
-               if (!preg_match('(^'.EmailGrammar::getGrammar('atom').'$)', $name)) {
-                       if (($encoded = EmailGrammar::encodeMimeHeader($name)) === $name) {
-                               $name = '"'.addcslashes($name, '\\"').'"';
-                       }
-                       else {
-                               $name = $encoded;
-                       }
-               }
-               
+               $name = EmailGrammar::encodeHeader($this->name);
                return $name.' <'.$this->address.'>';
        }
 }