* ImportExport: do NOT complain about wrong charset, if text contains only standard ascii chars

This commit is contained in:
Ralf Becker 2013-03-01 09:52:10 +00:00
parent 2be34f9810
commit 95d9dc084f
2 changed files with 25 additions and 8 deletions

View File

@ -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.<br />\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();
}

View File

@ -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
}
/**