Fix iconv unsupported `//IGNORE//TRANSLIT` on IBM i

Fixes #791
This commit is contained in:
Adrien Crivelli 2019-01-02 14:23:34 +11:00
parent 699da09176
commit 3b0c686630
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
4 changed files with 23 additions and 26 deletions

View File

@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Fix color from CSS when reading from HTML - [#831](https://github.com/PHPOffice/PhpSpreadsheet/pull/831) - Fix color from CSS when reading from HTML - [#831](https://github.com/PHPOffice/PhpSpreadsheet/pull/831)
- Fix infinite loop when reading invalid ODS files - [#832](https://github.com/PHPOffice/PhpSpreadsheet/pull/832) - Fix infinite loop when reading invalid ODS files - [#832](https://github.com/PHPOffice/PhpSpreadsheet/pull/832)
- Fix time format for duration is incorrect - [#666](https://github.com/PHPOffice/PhpSpreadsheet/pull/666) - Fix time format for duration is incorrect - [#666](https://github.com/PHPOffice/PhpSpreadsheet/pull/666)
- Fix iconv unsupported `//IGNORE//TRANSLIT` on IBM i - [#791](https://github.com/PHPOffice/PhpSpreadsheet/issues/791)
## [1.5.2] - 2018-11-25 ## [1.5.2] - 2018-11-25

View File

@ -44,6 +44,7 @@
"ext-dom": "*", "ext-dom": "*",
"ext-gd": "*", "ext-gd": "*",
"ext-iconv": "*", "ext-iconv": "*",
"ext-fileinfo": "*",
"ext-libxml": "*", "ext-libxml": "*",
"ext-mbstring": "*", "ext-mbstring": "*",
"ext-SimpleXML": "*", "ext-SimpleXML": "*",

3
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "5e66d1016f24ad7d6495ed4d7a04234d", "content-hash": "d70247696f417581626dde9641b53bdc",
"packages": [ "packages": [
{ {
"name": "markbaker/complex", "name": "markbaker/complex",
@ -3197,6 +3197,7 @@
"ext-dom": "*", "ext-dom": "*",
"ext-gd": "*", "ext-gd": "*",
"ext-iconv": "*", "ext-iconv": "*",
"ext-fileinfo": "*",
"ext-libxml": "*", "ext-libxml": "*",
"ext-mbstring": "*", "ext-mbstring": "*",
"ext-simplexml": "*", "ext-simplexml": "*",

View File

@ -53,6 +53,13 @@ class StringHelper
*/ */
private static $isIconvEnabled; private static $isIconvEnabled;
/**
* iconv options.
*
* @var string
*/
private static $iconvOptions = '//IGNORE//TRANSLIT';
/** /**
* Build control characters array. * Build control characters array.
*/ */
@ -243,39 +250,26 @@ class StringHelper
return self::$isIconvEnabled; return self::$isIconvEnabled;
} }
// Assume no problems with iconv
self::$isIconvEnabled = true;
// Fail if iconv doesn't exist // Fail if iconv doesn't exist
if (!function_exists('iconv')) { if (!function_exists('iconv')) {
self::$isIconvEnabled = false; self::$isIconvEnabled = false;
} elseif (!@iconv('UTF-8', 'UTF-16LE', 'x')) {
return false;
}
// Sometimes iconv is not working, and e.g. iconv('UTF-8', 'UTF-16LE', 'x') just returns false, // Sometimes iconv is not working, and e.g. iconv('UTF-8', 'UTF-16LE', 'x') just returns false,
if (!@iconv('UTF-8', 'UTF-16LE', 'x')) {
self::$isIconvEnabled = false; self::$isIconvEnabled = false;
} elseif (defined('PHP_OS') && @stristr(PHP_OS, 'AIX') && defined('ICONV_IMPL') && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && defined('ICONV_VERSION') && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
return false;
}
// Sometimes iconv_substr('A', 0, 1, 'UTF-8') just returns false in PHP 5.2.0
// we cannot use iconv in that case either (http://bugs.php.net/bug.php?id=37773)
if (!@iconv_substr('A', 0, 1, 'UTF-8')) {
self::$isIconvEnabled = false;
return false;
}
// CUSTOM: IBM AIX iconv() does not work // CUSTOM: IBM AIX iconv() does not work
if (defined('PHP_OS') && @stristr(PHP_OS, 'AIX') && defined('ICONV_IMPL') && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && defined('ICONV_VERSION') && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
self::$isIconvEnabled = false; self::$isIconvEnabled = false;
return false;
} }
// If we reach here no problems were detected with iconv // Deactivate iconv default options if they fail (as seen on IMB i)
self::$isIconvEnabled = true; if (self::$isIconvEnabled && !@iconv('UTF-8', 'UTF-16LE' . self::$iconvOptions, 'x')) {
self::$iconvOptions = '';
}
return true; return self::$isIconvEnabled;
} }
private static function buildCharacterSets() private static function buildCharacterSets()
@ -453,7 +447,7 @@ class StringHelper
public static function convertEncoding($value, $to, $from) public static function convertEncoding($value, $to, $from)
{ {
if (self::getIsIconvEnabled()) { if (self::getIsIconvEnabled()) {
$result = iconv($from, $to . '//IGNORE//TRANSLIT', $value); $result = iconv($from, $to . self::$iconvOptions, $value);
if (false !== $result) { if (false !== $result) {
return $result; return $result;
} }