From f70ac99f4945ae3224cfbf88e18a10f6516baa2c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 15 Jun 2015 17:03:25 +0200 Subject: [PATCH] Move header encoding from Mailbox into a generic function --- .../lib/system/email/EmailGrammar.class.php | 23 ++++++++++++++++++- .../files/lib/system/email/Mailbox.class.php | 11 +-------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/wcfsetup/install/files/lib/system/email/EmailGrammar.class.php b/wcfsetup/install/files/lib/system/email/EmailGrammar.class.php index 049153de38..b185df430b 100644 --- a/wcfsetup/install/files/lib/system/email/EmailGrammar.class.php +++ b/wcfsetup/install/files/lib/system/email/EmailGrammar.class.php @@ -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() { } } diff --git a/wcfsetup/install/files/lib/system/email/Mailbox.class.php b/wcfsetup/install/files/lib/system/email/Mailbox.class.php index 8205c1e041..f94858e4e5 100644 --- a/wcfsetup/install/files/lib/system/email/Mailbox.class.php +++ b/wcfsetup/install/files/lib/system/email/Mailbox.class.php @@ -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.'>'; } } -- 2.20.1