From 3b0c68663012b0bfe3d8e297299c9cd51824cddd Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Wed, 2 Jan 2019 14:23:34 +1100 Subject: [PATCH] Fix iconv unsupported `//IGNORE//TRANSLIT` on IBM i Fixes #791 --- CHANGELOG.md | 1 + composer.json | 1 + composer.lock | 3 +- src/PhpSpreadsheet/Shared/StringHelper.php | 44 ++++++++++------------ 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91c60830..8e015a44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 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 iconv unsupported `//IGNORE//TRANSLIT` on IBM i - [#791](https://github.com/PHPOffice/PhpSpreadsheet/issues/791) ## [1.5.2] - 2018-11-25 diff --git a/composer.json b/composer.json index d533e3c0..6a2a9e37 100644 --- a/composer.json +++ b/composer.json @@ -44,6 +44,7 @@ "ext-dom": "*", "ext-gd": "*", "ext-iconv": "*", + "ext-fileinfo": "*", "ext-libxml": "*", "ext-mbstring": "*", "ext-SimpleXML": "*", diff --git a/composer.lock b/composer.lock index 64aae870..19940ce8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5e66d1016f24ad7d6495ed4d7a04234d", + "content-hash": "d70247696f417581626dde9641b53bdc", "packages": [ { "name": "markbaker/complex", @@ -3197,6 +3197,7 @@ "ext-dom": "*", "ext-gd": "*", "ext-iconv": "*", + "ext-fileinfo": "*", "ext-libxml": "*", "ext-mbstring": "*", "ext-simplexml": "*", diff --git a/src/PhpSpreadsheet/Shared/StringHelper.php b/src/PhpSpreadsheet/Shared/StringHelper.php index 9fa05385..a5702775 100644 --- a/src/PhpSpreadsheet/Shared/StringHelper.php +++ b/src/PhpSpreadsheet/Shared/StringHelper.php @@ -53,6 +53,13 @@ class StringHelper */ private static $isIconvEnabled; + /** + * iconv options. + * + * @var string + */ + private static $iconvOptions = '//IGNORE//TRANSLIT'; + /** * Build control characters array. */ @@ -243,39 +250,26 @@ class StringHelper return self::$isIconvEnabled; } + // Assume no problems with iconv + self::$isIconvEnabled = true; + // Fail if iconv doesn't exist if (!function_exists('iconv')) { self::$isIconvEnabled = false; - - return 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')) { + } elseif (!@iconv('UTF-8', 'UTF-16LE', 'x')) { + // Sometimes iconv is not working, and e.g. iconv('UTF-8', 'UTF-16LE', 'x') just returns false, self::$isIconvEnabled = false; - - 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')) { + } elseif (defined('PHP_OS') && @stristr(PHP_OS, 'AIX') && defined('ICONV_IMPL') && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && defined('ICONV_VERSION') && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) { + // CUSTOM: IBM AIX iconv() does not work self::$isIconvEnabled = false; - - return false; } - // 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; - - return false; + // Deactivate iconv default options if they fail (as seen on IMB i) + if (self::$isIconvEnabled && !@iconv('UTF-8', 'UTF-16LE' . self::$iconvOptions, 'x')) { + self::$iconvOptions = ''; } - // If we reach here no problems were detected with iconv - self::$isIconvEnabled = true; - - return true; + return self::$isIconvEnabled; } private static function buildCharacterSets() @@ -453,7 +447,7 @@ class StringHelper public static function convertEncoding($value, $to, $from) { if (self::getIsIconvEnabled()) { - $result = iconv($from, $to . '//IGNORE//TRANSLIT', $value); + $result = iconv($from, $to . self::$iconvOptions, $value); if (false !== $result) { return $result; }