Commit 1b59bc7d authored by Sebastian Kummer's avatar Sebastian Kummer

Merge pull request #587 in ZP/z-push from...

Merge pull request #587 in ZP/z-push from bugfix/ZP-1202-imap-part-of-subject-or-from-may to develop

* commit 'fe3c93dd':
  ZP-1202 Code style fixes.
  ZP-1202 Part of subject or from may be in junk string. Released under the Affero GNU General Public License (AGPL) version 3.
parents b443280f fe3c93dd
......@@ -1034,8 +1034,7 @@ class BackendIMAP extends BackendDiff implements ISearchProvider {
$mobj = new Mail_mimeDecode($mail);
$message = $mobj->decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'rfc_822bodies' => true, 'charset' => 'utf-8'));
Utils::CheckAndFixEncoding($message->headers["subject"]);
Utils::CheckAndFixEncoding($message->headers["from"]);
Utils::CheckAndFixEncodingInHeaders($mail, $message);
$is_multipart = is_multipart($message);
$is_smime = is_smime($message);
......
......@@ -335,8 +335,7 @@ class BackendMaildir extends BackendDiff {
$message = Mail_mimeDecode::decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'input' => $rfc822, 'crlf' => "\n", 'charset' => 'utf-8'));
Utils::CheckAndFixEncoding($message->headers["subject"]);
Utils::CheckAndFixEncoding($message->headers["from"]);
Utils::CheckAndFixEncodingInHeaders($mail, $message);
$output = new SyncMail();
......
......@@ -1191,6 +1191,98 @@ class Utils {
return isset($folder->displayname) && substr($folder->displayname, -3) == hex2bin("e2808b");
}
/**
* If string is ISO-2022-JP, convert this into utf-8.
*
* @param string $nonencstr
* @param string $utf8str
*
* @access private
* @return string
*/
private static function convertRawHeader2Utf8($nonencstr, $utf8str) {
if (!isset($nonencstr)) {
return $utf8str;
}
// if php-imap option is not installed, there is no noconversion
if (!function_exists("imap_mime_header_decode")) {
return $utf8str;
}
$isiso2022jp = false;
$issamecharset = true;
$charset = NULL;
$str = "";
$striso2022jp = "";
foreach (@imap_mime_header_decode($nonencstr) as $val) {
if (is_null($charset)) {
$charset = strtolower($val->charset);
}
if ($charset != strtolower($val->charset)) {
$issamecharset = false;
}
if (strtolower($val->charset) == "iso-2022-jp") {
$isiso2022jp = true;
$striso2022jp .= $val->text;
$str .= @mb_convert_encoding($val->text, "utf-8", "ISO-2022-JP-MS");
}
elseif (strtolower($val->charset) == "default") {
$str .= $val->text;
}
else {
$str .= @mb_convert_encoding($val->text, "utf-8", $val->charset);
}
}
if (!$isiso2022jp) {
return $utf8str;
}
if ($charset == 'iso-2022-jp' && $issamecharset) {
$str = @mb_convert_encoding($striso2022jp, "utf-8", "ISO-2022-JP-MS");
}
return $str;
}
/**
* Get raw mail headers as key-value pair array.
*
* @param &$mail: this is reference of the caller's $mail,
* not copy. So the call to
* Utils::getRawMailHeaders() will not require
* memory for $mail.
*
* @access private
* @return string array
*/
private static function getRawMailHeaders(&$mail) {
// if no headers, return FALSE
if (!preg_match("/^(.*?)\r?\n\r?\n/s", $mail, $match)) {
ZLog::Write(LOGLEVEL_DEBUG, "Utils::getRawMailHeaders(): no header");
return false;
}
$input = $match[1];
// if no headers, return FALSE
if ($input == "") {
ZLog::Write(LOGLEVEL_DEBUG, "Utils::getRawMailHeaders(): no header");
return false;
}
// parse headers
$input = preg_replace("/\r?\n/", "\r\n", $input);
$input = preg_replace("/=\r\n(\t| )+/", '=', $input);
$input = preg_replace("/\r\n(\t| )+/", ' ', $input);
$headersonly = explode("\r\n", trim($input));
unset($input);
$headers = array("subject" => NULL, "from" => NULL);
foreach ($headersonly as $value) {
if (!preg_match("/^(.+):[ \t]*(.+)$/", $value, $match)) {
continue;
}
$headers[strtolower($match[1])] = $match[2];
}
unset($headersonly);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("Utils::getRawMailHeaders(): subject = %s", $headers["subject"]));
ZLog::Write(LOGLEVEL_DEBUG, sprintf("Utils::getRawMailHeaders(): from = %s", $headers["from"]));
return $headers;
}
/**
* Check if the UTF-8 string has ISO-2022-JP esc seq
* if so, it is ISO-2022-JP, not UTF-8 and convert it into UTF-8
......@@ -1210,7 +1302,7 @@ class Utils {
* Get to or cc header in mime-header-encoded UTF-8 text.
*
* @access public
* @param $addrstruncs
* @param $addrstruncs
* $addrstruncts is a return value of
* Mail_RFC822->parseAddressList(). Convert this into
* plain text. If the phrase part is in plain UTF-8,
......@@ -1240,6 +1332,31 @@ class Utils {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("Utils::CheckAndFixEncodingInHeadersOfSentMail(): addresses %s", $addresses));
return $addresses;
}
/**
* Set expected subject and from in utf-8 even if in wrong
* decoded.
*
* @param &$mail
* &$mail is reference of the caller's, not copy. So the
* call to Utils::CheckAndFixEncodingInHeaders() will not
* require memory for $mail.
* @param $message
* $message is an instance of a class. So the call to
* Utils::CheckAndFixEncodingInHeaders() will not
* require memory for $message
*
* @access public
* @return void
*/
public static function CheckAndFixEncodingInHeaders(&$mail, $message) {
$rawheaders = Utils::getRawMailHeaders($mail);
if (!$rawheaders) {
return;
}
$message->headers["subject"] = Utils::convertRawHeader2Utf8($rawheaders["subject"], $message->headers["subject"]);
$message->headers["from"] = Utils::convertRawHeader2Utf8($rawheaders["from"], $message->headers["from"]);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment