* 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:51:02 +00:00
parent b17b1043ce
commit ea6e8c8e94
2 changed files with 25 additions and 8 deletions

View File

@ -71,7 +71,7 @@
$preference = $GLOBALS['egw_info']['user']['preferences']['common']['csv_charset']; $preference = $GLOBALS['egw_info']['user']['preferences']['common']['csv_charset'];
} }
$required = $options['charset'] == 'user' || !$options['charset'] ? $preference : $options['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)) if($encoding && strtoupper($required) != strtoupper($encoding))
{ {
$this->message = lang("Encoding mismatch. Expected %1 file, you uploaded %2.<br />\n", $this->message = lang("Encoding mismatch. Expected %1 file, you uploaded %2.<br />\n",
@ -79,7 +79,7 @@
$encoding $encoding
); );
} }
$file = fopen($content['file']['tmp_name'], 'rb'); $file = fopen($content['file']['tmp_name'], 'rb');
$count = 0; $count = 0;
@ -158,7 +158,7 @@
'import_'.md5($content['file']['name'].$GLOBALS['egw_info']['user']['account_id']))); 'import_'.md5($content['file']['name'].$GLOBALS['egw_info']['user']['account_id'])));
unset($dst_file); unset($dst_file);
} }
} catch (Exception $e) { } catch (Exception $e) {
$this->message .= $e->getMessage(); $this->message .= $e->getMessage();
} }

View File

@ -839,20 +839,37 @@ class translation
/** /**
* detect_encoding - try to detect the encoding * detect_encoding - try to detect the encoding
* only to be used if the string in question has no structure that determines his encoding * only to be used if the string in question has no structure that determines his encoding
*
* @param string - to be evaluated * @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 * @return string - encoding
*/ */
static function detect_encoding($string) { static function detect_encoding($string, $verify=null)
static $list = array('utf-8', 'iso-8859-1', 'windows-1251'); // list may be extended {
if (function_exists('iconv')) 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); $sample = iconv($item, $item, $string);
if (md5($sample) == md5($string)) if ($sample == $string)
{
return $item; 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
} }
/** /**