Commit fe3c93dd authored by Manfred Kutas's avatar Manfred Kutas

ZP-1202 Code style fixes.

Released under the Affero GNU General Public License (AGPL) version 3.
parent 20462b52
...@@ -1192,18 +1192,20 @@ class Utils { ...@@ -1192,18 +1192,20 @@ class Utils {
} }
/** /**
* if string is ISO-2022-JP, convert this into utf-8 * If string is ISO-2022-JP, convert this into utf-8.
*
* @param string $nonencstr
* @param string $utf8str
* *
* @access private * @access private
* @param $nonencstr, $utf8str
* @return string * @return string
*/ */
private static function ConvertRawHeader2Utf8($nonencstr, $utf8str) { private static function convertRawHeader2Utf8($nonencstr, $utf8str) {
if ( !isset($nonencstr) ) { if (!isset($nonencstr)) {
return $utf8str; return $utf8str;
} }
// if php-imap option is not installed, there is no noconversion // if php-imap option is not installed, there is no noconversion
if ( !function_exists("imap_mime_header_decode") ) { if (!function_exists("imap_mime_header_decode")) {
return $utf8str; return $utf8str;
} }
$isiso2022jp = false; $isiso2022jp = false;
...@@ -1211,52 +1213,55 @@ class Utils { ...@@ -1211,52 +1213,55 @@ class Utils {
$charset = NULL; $charset = NULL;
$str = ""; $str = "";
$striso2022jp = ""; $striso2022jp = "";
foreach ( @imap_mime_header_decode($nonencstr) as $val ) { foreach (@imap_mime_header_decode($nonencstr) as $val) {
if ( is_null($charset) ) { if (is_null($charset)) {
$charset = strtolower($val->charset); $charset = strtolower($val->charset);
} }
if ( $charset != strtolower($val->charset) ) { if ($charset != strtolower($val->charset)) {
$issamecharset = false; $issamecharset = false;
} }
if ( strtolower($val->charset) == "iso-2022-jp" ) { if (strtolower($val->charset) == "iso-2022-jp") {
$isiso2022jp = true; $isiso2022jp = true;
$striso2022jp .= $val->text; $striso2022jp .= $val->text;
$str .= @mb_convert_encoding($val->text, "utf-8", "ISO-2022-JP-MS"); $str .= @mb_convert_encoding($val->text, "utf-8", "ISO-2022-JP-MS");
} else if ( strtolower($val->charset) == "default" ) { }
elseif (strtolower($val->charset) == "default") {
$str .= $val->text; $str .= $val->text;
} else { }
else {
$str .= @mb_convert_encoding($val->text, "utf-8", $val->charset); $str .= @mb_convert_encoding($val->text, "utf-8", $val->charset);
} }
} }
if ( !$isiso2022jp ) { if (!$isiso2022jp) {
return $utf8str; return $utf8str;
} }
if ( $charset == 'iso-2022-jp' && $issamecharset ) { if ($charset == 'iso-2022-jp' && $issamecharset) {
$str = @mb_convert_encoding($striso2022jp, "utf-8", "ISO-2022-JP-MS"); $str = @mb_convert_encoding($striso2022jp, "utf-8", "ISO-2022-JP-MS");
} }
return $str; return $str;
} }
/** /**
* get raw mail headers as key-value pair array * Get raw mail headers as key-value pair array.
* *
* @access private
* @param &$mail: this is reference of the caller's $mail, * @param &$mail: this is reference of the caller's $mail,
* not copy. So the call to * not copy. So the call to
* Utils::GetRawMailHeaders() will not require * Utils::getRawMailHeaders() will not require
* memory for $mail. * memory for $mail.
*
* @access private
* @return string array * @return string array
*/ */
private static function GetRawMailHeaders(&$mail) { private static function getRawMailHeaders(&$mail) {
// if no headers, return FALSE // if no headers, return FALSE
if ( !preg_match("/^(.*?)\r?\n\r?\n/s", $mail, $match) ) { if (!preg_match("/^(.*?)\r?\n\r?\n/s", $mail, $match)) {
ZLog::Write(LOGLEVEL_DEBUG, "Utils::GetRawMailHeaders(): no header"); ZLog::Write(LOGLEVEL_DEBUG, "Utils::getRawMailHeaders(): no header");
return false; return false;
} }
$input = $match[1]; $input = $match[1];
// if no headers, return FALSE // if no headers, return FALSE
if ( $input == "" ) { if ($input == "") {
ZLog::Write(LOGLEVEL_DEBUG, "Utils::GetRawMailHeaders(): no header"); ZLog::Write(LOGLEVEL_DEBUG, "Utils::getRawMailHeaders(): no header");
return false; return false;
} }
// parse headers // parse headers
...@@ -1266,15 +1271,15 @@ class Utils { ...@@ -1266,15 +1271,15 @@ class Utils {
$headersonly = explode("\r\n", trim($input)); $headersonly = explode("\r\n", trim($input));
unset($input); unset($input);
$headers = array("subject" => NULL, "from" => NULL); $headers = array("subject" => NULL, "from" => NULL);
foreach ( $headersonly as $value ) { foreach ($headersonly as $value) {
if ( !preg_match("/^(.+):[ \t]*(.+)$/", $value, $match) ) { if (!preg_match("/^(.+):[ \t]*(.+)$/", $value, $match)) {
continue; continue;
} }
$headers[strtolower($match[1])] = $match[2]; $headers[strtolower($match[1])] = $match[2];
} }
unset($headersonly); unset($headersonly);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("GetRawMailHeaders(): subject = %s", $headers["subject"])); ZLog::Write(LOGLEVEL_DEBUG, sprintf("Utils::getRawMailHeaders(): subject = %s", $headers["subject"]));
ZLog::Write(LOGLEVEL_DEBUG, sprintf("GetRawMailHeaders(): from = %s", $headers["from"])); ZLog::Write(LOGLEVEL_DEBUG, sprintf("Utils::getRawMailHeaders(): from = %s", $headers["from"]));
return $headers; return $headers;
} }
...@@ -1297,7 +1302,7 @@ class Utils { ...@@ -1297,7 +1302,7 @@ class Utils {
* Get to or cc header in mime-header-encoded UTF-8 text. * Get to or cc header in mime-header-encoded UTF-8 text.
* *
* @access public * @access public
* @param $addrstruncs * @param $addrstruncs
* $addrstruncts is a return value of * $addrstruncts is a return value of
* Mail_RFC822->parseAddressList(). Convert this into * Mail_RFC822->parseAddressList(). Convert this into
* plain text. If the phrase part is in plain UTF-8, * plain text. If the phrase part is in plain UTF-8,
...@@ -1329,26 +1334,28 @@ class Utils { ...@@ -1329,26 +1334,28 @@ class Utils {
} }
/** /**
* set expected subject and from in utf-8 even if in wrong * Set expected subject and from in utf-8 even if in wrong
* decoded * decoded.
*
* @access public
* @param &$mail, $message
* *
* @param &$mail
* &$mail is reference of the caller's, not copy. So the * &$mail is reference of the caller's, not copy. So the
* call to Utils::CheckAndFixEncodingInHeaders() will not * call to Utils::CheckAndFixEncodingInHeaders() will not
* require memory for $mail. * require memory for $mail.
* $message is a instance of a class. So the call to * @param $message
* $message is an instance of a class. So the call to
* Utils::CheckAndFixEncodingInHeaders() will not * Utils::CheckAndFixEncodingInHeaders() will not
* require memory for $message * require memory for $message
*
* @access public
* @return void
*/ */
public static function CheckAndFixEncodingInHeaders(&$mail, $message) { public static function CheckAndFixEncodingInHeaders(&$mail, $message) {
$rawheaders = Utils::GetRawMailHeaders($mail); $rawheaders = Utils::getRawMailHeaders($mail);
if ( !$rawheaders ) { if (!$rawheaders) {
return; return;
} }
$message->headers["subject"] = Utils::ConvertRawHeader2Utf8($rawheaders["subject"], $message->headers["subject"]); $message->headers["subject"] = Utils::convertRawHeader2Utf8($rawheaders["subject"], $message->headers["subject"]);
$message->headers["from"] = Utils::ConvertRawHeader2Utf8($rawheaders["from"], $message->headers["from"]); $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