From c0ad68108aae9ececf87366550e4c0b116c5b987 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sat, 27 Oct 2012 02:27:57 -0700 Subject: [PATCH] Do checks against iconvAvailable because PHP 5.4 has botched iconv support. Signed-off-by: Edward Z. Yang --- library/HTMLPurifier/Encoder.php | 7 ++++++- tests/HTMLPurifier/EncoderTest.php | 15 +++++++++------ tests/HTMLPurifier/HTMLT/shift-jis-preserve-yen.htmlt | 2 +- tests/HTMLPurifier/HTMLT/shift-jis-remove-yen.htmlt | 2 +- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/library/HTMLPurifier/Encoder.php b/library/HTMLPurifier/Encoder.php index 9fa76bd1..77988a19 100644 --- a/library/HTMLPurifier/Encoder.php +++ b/library/HTMLPurifier/Encoder.php @@ -355,7 +355,12 @@ class HTMLPurifier_Encoder $str = utf8_encode($str); return $str; } - trigger_error('Encoding not supported, please install iconv', E_USER_ERROR); + $bug = HTMLPurifier_Encoder::testIconvTruncateBug(); + if ($bug == self::ICONV_OK) { + trigger_error('Encoding not supported, please install iconv', E_USER_ERROR); + } else { + trigger_error('You have a buggy version of iconv, see https://bugs.php.net/bug.php?id=48147 and http://sourceware.org/bugzilla/show_bug.cgi?id=13541', E_USER_ERROR); + } } /** diff --git a/tests/HTMLPurifier/EncoderTest.php b/tests/HTMLPurifier/EncoderTest.php index 6084c39f..e9593e2c 100644 --- a/tests/HTMLPurifier/EncoderTest.php +++ b/tests/HTMLPurifier/EncoderTest.php @@ -39,6 +39,7 @@ class HTMLPurifier_EncoderTest extends HTMLPurifier_Harness } function test_convertToUTF8_spuriousEncoding() { + if (!HTMLPurifier_Encoder::iconvAvailable()) return; $this->config->set('Core.Encoding', 'utf99'); $this->expectError('Invalid encoding utf99'); $this->assertIdentical( @@ -87,7 +88,7 @@ class HTMLPurifier_EncoderTest extends HTMLPurifier_Harness } function test_convertFromUTF8_iconvNoChars() { - if (!function_exists('iconv')) return; + if (!HTMLPurifier_Encoder::iconvAvailable()) return; $this->config->set('Core.Encoding', 'ISO-8859-1'); $this->assertIdentical( HTMLPurifier_Encoder::convertFromUTF8($this->getZhongWen(), $this->config, $this->context), @@ -169,14 +170,16 @@ class HTMLPurifier_EncoderTest extends HTMLPurifier_Harness } function test_testEncodingSupportsASCII() { - $this->assertASCIISupportCheck('Shift_JIS', array("\xC2\xA5" => '\\', "\xE2\x80\xBE" => '~')); - $this->assertASCIISupportCheck('JOHAB', array("\xE2\x82\xA9" => '\\')); + if (HTMLPurifier_Encoder::iconvAvailable()) { + $this->assertASCIISupportCheck('Shift_JIS', array("\xC2\xA5" => '\\', "\xE2\x80\xBE" => '~')); + $this->assertASCIISupportCheck('JOHAB', array("\xE2\x82\xA9" => '\\')); + } $this->assertASCIISupportCheck('ISO-8859-1', array()); $this->assertASCIISupportCheck('dontexist', array()); // canary } function testShiftJIS() { - if (!function_exists('iconv')) return; + if (!HTMLPurifier_Encoder::iconvAvailable()) return; $this->config->set('Core.Encoding', 'Shift_JIS'); // This actually looks like a Yen, but we're going to treat it differently $this->assertIdentical( @@ -190,7 +193,7 @@ class HTMLPurifier_EncoderTest extends HTMLPurifier_Harness } function testIconvTruncateBug() { - if (!function_exists('iconv')) return; + if (!HTMLPurifier_Encoder::iconvAvailable()) return; if (HTMLPurifier_Encoder::testIconvTruncateBug() !== HTMLPurifier_Encoder::ICONV_TRUNCATES) return; $this->config->set('Core.Encoding', 'ISO-8859-1'); $this->assertIdentical( @@ -200,7 +203,7 @@ class HTMLPurifier_EncoderTest extends HTMLPurifier_Harness } function testIconvChunking() { - if (!function_exists('iconv')) return; + if (!HTMLPurifier_Encoder::iconvAvailable()) return; if (HTMLPurifier_Encoder::testIconvTruncateBug() !== HTMLPurifier_Encoder::ICONV_TRUNCATES) return; $this->assertIdentical(HTMLPurifier_Encoder::iconv('utf-8', 'iso-8859-1//IGNORE', "a\xF3\xA0\x80\xA0b", 4), 'ab'); $this->assertIdentical(HTMLPurifier_Encoder::iconv('utf-8', 'iso-8859-1//IGNORE', "aa\xE4\xB8\xADb", 4), 'aab'); diff --git a/tests/HTMLPurifier/HTMLT/shift-jis-preserve-yen.htmlt b/tests/HTMLPurifier/HTMLT/shift-jis-preserve-yen.htmlt index f22417c0..862ecd97 100644 --- a/tests/HTMLPurifier/HTMLT/shift-jis-preserve-yen.htmlt +++ b/tests/HTMLPurifier/HTMLT/shift-jis-preserve-yen.htmlt @@ -1,5 +1,5 @@ --SKIPIF-- -if (!function_exists('iconv')) return true; +if (!HTMLPurifier_Encoder::iconvAvailable()) return true; --INI-- Core.Encoding = "Shift_JIS" Core.EscapeNonASCIICharacters = true diff --git a/tests/HTMLPurifier/HTMLT/shift-jis-remove-yen.htmlt b/tests/HTMLPurifier/HTMLT/shift-jis-remove-yen.htmlt index 6c2d3bc4..aae7598d 100644 --- a/tests/HTMLPurifier/HTMLT/shift-jis-remove-yen.htmlt +++ b/tests/HTMLPurifier/HTMLT/shift-jis-remove-yen.htmlt @@ -1,5 +1,5 @@ --SKIPIF-- -if (!function_exists('iconv')) return true; +if (!HTMLPurifier_Encoder::iconvAvailable()) return true; --INI-- Core.Encoding = Shift_JIS --HTML-- -- 2.11.4.GIT