Commit 20462b52 authored by YANO Takashi's avatar YANO Takashi

ZP-1202 Part of subject or from may be in junk string. Released under the...

ZP-1202 Part of subject or from may be in junk string. Released under the Affero GNU General Public License (AGPL) version 3.
parent 18c88cd9
...@@ -1034,8 +1034,7 @@ class BackendIMAP extends BackendDiff implements ISearchProvider { ...@@ -1034,8 +1034,7 @@ class BackendIMAP extends BackendDiff implements ISearchProvider {
$mobj = new Mail_mimeDecode($mail); $mobj = new Mail_mimeDecode($mail);
$message = $mobj->decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'rfc_822bodies' => true, 'charset' => 'utf-8')); $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::CheckAndFixEncodingInHeaders($mail, $message);
Utils::CheckAndFixEncoding($message->headers["from"]);
$is_multipart = is_multipart($message); $is_multipart = is_multipart($message);
$is_smime = is_smime($message); $is_smime = is_smime($message);
......
...@@ -335,8 +335,7 @@ class BackendMaildir extends BackendDiff { ...@@ -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')); $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::CheckAndFixEncodingInHeaders($mail, $message);
Utils::CheckAndFixEncoding($message->headers["from"]);
$output = new SyncMail(); $output = new SyncMail();
......
...@@ -1191,6 +1191,93 @@ class Utils { ...@@ -1191,6 +1191,93 @@ class Utils {
return isset($folder->displayname) && substr($folder->displayname, -3) == hex2bin("e2808b"); return isset($folder->displayname) && substr($folder->displayname, -3) == hex2bin("e2808b");
} }
/**
* if string is ISO-2022-JP, convert this into utf-8
*
* @access private
* @param $nonencstr, $utf8str
* @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");
} else if ( 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
*
* @access private
* @param &$mail: this is reference of the caller's $mail,
* not copy. So the call to
* Utils::GetRawMailHeaders() will not require
* memory for $mail.
* @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("GetRawMailHeaders(): subject = %s", $headers["subject"]));
ZLog::Write(LOGLEVEL_DEBUG, sprintf("GetRawMailHeaders(): from = %s", $headers["from"]));
return $headers;
}
/** /**
* Check if the UTF-8 string has ISO-2022-JP esc seq * 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 * if so, it is ISO-2022-JP, not UTF-8 and convert it into UTF-8
...@@ -1240,6 +1327,29 @@ class Utils { ...@@ -1240,6 +1327,29 @@ class Utils {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("Utils::CheckAndFixEncodingInHeadersOfSentMail(): addresses %s", $addresses)); ZLog::Write(LOGLEVEL_DEBUG, sprintf("Utils::CheckAndFixEncodingInHeadersOfSentMail(): addresses %s", $addresses));
return $addresses; return $addresses;
} }
/**
* set expected subject and from in utf-8 even if in wrong
* decoded
*
* @access public
* @param &$mail, $message
*
* &$mail is reference of the caller's, not copy. So the
* call to Utils::CheckAndFixEncodingInHeaders() will not
* require memory for $mail.
* $message is a instance of a class. So the call to
* Utils::CheckAndFixEncodingInHeaders() will not
* require memory for $message
*/
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