diff --git a/phpgwapi/inc/class.translation.inc.php b/phpgwapi/inc/class.translation.inc.php index c4a688391f..42ff773645 100644 --- a/phpgwapi/inc/class.translation.inc.php +++ b/phpgwapi/inc/class.translation.inc.php @@ -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);