diff --git a/importexport/inc/class.importexport_import_ui.inc.php b/importexport/inc/class.importexport_import_ui.inc.php index f2b31e1aed..fbddedb6bd 100644 --- a/importexport/inc/class.importexport_import_ui.inc.php +++ b/importexport/inc/class.importexport_import_ui.inc.php @@ -71,7 +71,7 @@ $preference = $GLOBALS['egw_info']['user']['preferences']['common']['csv_charset']; } $required = $options['charset'] == 'user' || !$options['charset'] ? $preference : $options['charset']; - $encoding = translation::detect_encoding($sample); + $encoding = translation::detect_encoding($sample, $required); if($encoding && strtoupper($required) != strtoupper($encoding)) { $this->message = lang("Encoding mismatch. Expected %1 file, you uploaded %2.
\n", @@ -79,7 +79,7 @@ $encoding ); } - + $file = fopen($content['file']['tmp_name'], 'rb'); $count = 0; @@ -158,7 +158,7 @@ 'import_'.md5($content['file']['name'].$GLOBALS['egw_info']['user']['account_id']))); unset($dst_file); } - + } catch (Exception $e) { $this->message .= $e->getMessage(); } diff --git a/phpgwapi/inc/class.translation.inc.php b/phpgwapi/inc/class.translation.inc.php index 28279168e7..0737e899a7 100644 --- a/phpgwapi/inc/class.translation.inc.php +++ b/phpgwapi/inc/class.translation.inc.php @@ -838,20 +838,37 @@ class translation /** * 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 + * @param string $verify=null encoding to verify, get checked first and have a match for only ascii or no detection available * @return string - encoding */ - static function detect_encoding($string) { - static $list = array('utf-8', 'iso-8859-1', 'windows-1251'); // list may be extended + static function detect_encoding($string, $verify=null) + { if (function_exists('iconv')) { - foreach ($list as $item) { + $list = array('utf-8', 'iso-8859-1', 'windows-1251'); // list may be extended + + if ($verify) array_unshift($list, $verify); + + foreach ($list as $item) + { $sample = iconv($item, $item, $string); - if (md5($sample) == md5($string)) + if ($sample == $string) + { return $item; + } } } - return self::$mbstring ? strtolower(mb_detect_encoding($string)) : 'iso-8859-1'; // we choose to return iso-8859-1 as default + if (self::$mbstring) + { + $detected = strtolower(mb_detect_encoding($string)); + } + if ($verify && (!isset($detected) || $detected === 'ascii')) + { + return $verify; // ascii matches all charsets + } + return isset($detected) ? $detected : 'iso-8859-1'; // we choose to return iso-8859-1 as default } /**