diff --git a/framework/helper.class.php b/framework/helper.class.php index aedb5de5bd..b2763a5ec6 100644 --- a/framework/helper.class.php +++ b/framework/helper.class.php @@ -75,4 +75,40 @@ class helper extends baseHelper $replacements = array("\\\\", "\\/", "\\\"", "\'", "\\n", "\\r", "\\t", "\\f", "\\b", "\\u"); return str_replace($escapers, $replacements, $json); } + + /** + * Convert encoding. + * + * @param string $string + * @param string $fromEncoding + * @param string $toEncoding + * @static + * @access public + * @return string + */ + static public function convertEncoding($string, $fromEncoding, $toEncoding = 'utf-8') + { + $toEncoding = str_replace('utf8', 'utf-8', $toEncoding); + if($fromEncoding == $toEncoding) return $string; + if(function_exists('mb_convert_encoding')) + { + /* Remove like utf-8//TRANSLIT. */ + $position = strpos($toEncoding, '//'); + if($position !== false) $toEncoding = substr($toEncoding, 0, $position); + + /* Check string encoding. */ + $encoding = strtolower(mb_detect_encoding($string, array('ASCII','UTF-8','GB2312','GBK','BIG5'))); + if($encoding == $toEncoding) return $string; + return mb_convert_encoding($string, $toEncoding, $fromEncoding); + } + elseif(function_exists('iconv')) + { + $convertString = @iconv($fromEncoding, $toEncoding, $string); + /* iconv error then return original. */ + if(!$convertString) return $string; + return $convertString; + } + + return $string; + } } diff --git a/module/file/control.php b/module/file/control.php index eed49afc72..2e72eda9a3 100644 --- a/module/file/control.php +++ b/module/file/control.php @@ -166,17 +166,7 @@ class file extends control $this->view->fields = $this->post->fields; $this->view->rows = $this->post->rows; $output = $this->parse('file', 'export2csv'); - if( $this->post->encode != "utf-8") - { - if(function_exists('mb_convert_encoding')) - { - $output = @mb_convert_encoding($output, $this->post->encode, 'utf-8'); - } - elseif(function_exists('iconv')) - { - $output = @iconv('utf-8', $this->post->encode . '//TRANSLIT', $output); - } - } + if($this->post->encode != "utf-8") $output = helper::convertEncoding($output, 'utf-8', $this->post->encode . '//TRANSLIT'); $this->sendDownHeader($this->post->fileName, 'csv', $output); } diff --git a/module/file/model.php b/module/file/model.php index 3acfa16612..6ba159e842 100644 --- a/module/file/model.php +++ b/module/file/model.php @@ -488,14 +488,7 @@ class fileModel extends model { if($uploadFile['folder']) continue; $fileName = $uploadFile['filename']; - if(function_exists('iconv')) - { - $fileName = iconv('gbk', 'utf-8//TRANSLIT', $fileName); - } - elseif(function_exists('mb_convert_encoding')) - { - $fileName = mb_convert_encoding($fileName, 'utf-8', 'gbk'); - } + $fileName = helper::convertEncoding($fileName, 'gbk', 'utf-8//TRANSLIT'); if(($pos = strrpos($fileName, '/')) !== false) $fileName = substr($fileName, $pos + 1); $file = array(); diff --git a/module/git/model.php b/module/git/model.php index bf6005d83d..6d42a619db 100644 --- a/module/git/model.php +++ b/module/git/model.php @@ -397,7 +397,7 @@ class gitModel extends model foreach($encodings as $encoding) { if($encoding == 'utf-8') continue; - $result = @mb_convert_encoding($comment, 'utf-8', $encoding); + $result = helper::convertEncoding($comment, $encoding, 'utf-8'); if($result) return $result; } diff --git a/module/testcase/control.php b/module/testcase/control.php index a773edc55e..f19224f051 100644 --- a/module/testcase/control.php +++ b/module/testcase/control.php @@ -1074,18 +1074,7 @@ class testcase extends control { $fc = file_get_contents($fileName); $encode = $this->post->encode != "utf-8" ? $this->post->encode : 'gbk'; - if(function_exists('mb_convert_encoding')) - { - $fc = @mb_convert_encoding($fc, 'utf-8', $encode); - } - elseif(function_exists('iconv')) - { - $fc = @iconv($encode, 'utf-8', $fc); - } - else - { - die(js::alert($this->lang->testcase->noFunction)); - } + $fc = helper::convertEncoding($fc, $encode, 'utf-8'); file_put_contents($fileName, $fc); $rows = $this->file->parseCSV($fileName);