introduce detect_encoding functionality as a static function (used in decodeMailHeader as of now)

This commit is contained in:
Klaus Leithoff 2012-10-24 10:54:44 +00:00
parent 97781b727d
commit 5b295573dd

View File

@ -1029,6 +1029,25 @@ class translation
return self::$db->select(self::LANG_TABLE,'message_id',$where,__LINE__,__FILE__)->fetchColumn();
}
/**
* detect_encoding - try to detect the encoding
* only to be used if the string in question has no structure that determines his encoding
* @param string - to be evaluated
* @return string - encoding
*/
static function detect_encoding($string) {
static $list = array('utf-8', 'iso-8859-1', 'windows-1251'); // list may be extended
if (function_exists('iconv'))
{
foreach ($list as $item) {
$sample = iconv($item, $item, $string);
if (md5($sample) == md5($string))
return $item;
}
}
return self::$mbstring ? strtolower(mb_detect_encoding($string)) : 'iso-8859-1'; // we choose to return iso-8859-1 as default
}
/**
* Return the decoded string meeting some additional requirements for mailheaders
*
@ -1066,7 +1085,7 @@ class translation
$convertAtEnd = false;
foreach((array)$elements as $element)
{
if ($element->charset == 'default') $element->charset = 'iso-8859-1';
if ($element->charset == 'default') $element->charset = self::detect_encoding($element->text);
if ($element->charset != 'x-unknown')
{
if( strtoupper($element->charset) != 'UTF-8') $element->text = preg_replace($sar,$rar,$element->text);