prevent possible endless recursion on decodeMailHeader

This commit is contained in:
Klaus Leithoff 2016-04-15 08:50:02 +00:00
parent ea4932b1d7
commit 9f6104f53a

View File

@ -25,18 +25,18 @@ class Html
* *
* @param string $_string -> part of an mailheader * @param string $_string -> part of an mailheader
* @param string $displayCharset the charset parameter specifies the character set to represent the result by (if iconv_mime_decode is to be used) * @param string $displayCharset the charset parameter specifies the character set to represent the result by (if iconv_mime_decode is to be used)
* @param integer $reclevel param to control recursive calls (to prevent endless recursion)
* @return string * @return string
*/ */
static function decodeMailHeader($_string, $displayCharset='utf-8') static function decodeMailHeader($_string, $displayCharset='utf-8', $reclevel=0)
{ {
/*
$maxreclevel=25; $maxreclevel=25;
if ($reclevel > $maxreclevel) { if ($reclevel > $maxreclevel) {
error_log( __METHOD__.__LINE__." Recursion Level Exeeded ($reclevel) while decoding $_string "); error_log( __METHOD__.__LINE__." Recursion Level Exeeded ($reclevel) while decoding $_string ");
return $_string; return $_string;
} }
$reclevel++; $reclevel++;
*/
//error_log(__FILE__.','.__METHOD__.':'."called with $_string and CHARSET $displayCharset"); //error_log(__FILE__.','.__METHOD__.':'."called with $_string and CHARSET $displayCharset");
if(function_exists('imap_mime_header_decode')) if(function_exists('imap_mime_header_decode'))
{ {
@ -73,20 +73,10 @@ class Html
$openTags = substr_count($element->text,'?='); $openTags = substr_count($element->text,'?=');
if(preg_match('/\?=.+=\?/', $element->text) && $openTags>0 && $openTags==substr_count($element->text,'=?') && $element->text != $_string) if(preg_match('/\?=.+=\?/', $element->text) && $openTags>0 && $openTags==substr_count($element->text,'=?') && $element->text != $_string)
{ {
$element->text = self::decodeMailHeader($element->text, $element->charset); $element->text = self::decodeMailHeader($element->text, $element->charset, $reclevel);
$element->charset = $displayCharset; $element->charset = $displayCharset;
} }
$translatedString = Api\Translation::convert($element->text,$element->charset); $newString .= Api\Translation::convert($element->text,$element->charset);
/* //try to be smart about concatenating
error_log(__METHOD__.__LINE__.$translatedString);
error_log(__METHOD__.__LINE__.$newString);
if ($translatedString && $newString && strpos($translatedString,$newString)===0)
{
$newString = $translatedString;
}
else
*/
$newString .= $translatedString;
} }
else else
{ {
@ -94,7 +84,7 @@ error_log(__METHOD__.__LINE__.$newString);
$convertAtEnd = true; $convertAtEnd = true;
} }
} }
if ($convertAtEnd) $newString = self::decodeMailHeader($newString,$displayCharset); if ($convertAtEnd) $newString = self::decodeMailHeader($newString,$displayCharset,$reclevel);
return preg_replace('/([\000-\012\015\016\020-\037\075])/','',$newString); return preg_replace('/([\000-\012\015\016\020-\037\075])/','',$newString);
} }
elseif(function_exists(mb_decode_mimeheader)) elseif(function_exists(mb_decode_mimeheader))