diff --git a/NEWS b/NEWS index 2b2700e0..a46d4be3 100644 --- a/NEWS +++ b/NEWS @@ -32,6 +32,7 @@ ERRATA - Iconv uses set_error_handler instead of shut-up operator - Add protection against imagecrash attack with CSS height/width - HTMLPurifier::getInstance() renamed to HTMLPurifier::instance() for consistency +- Fixed bug with fallback languages in LanguageFactory 2.1.3, released 2007-11-05 ! tests/multitest.php allows you to test multiple versions by running diff --git a/library/HTMLPurifier/Language.php b/library/HTMLPurifier/Language.php index c9a3c20f..c0833b7f 100644 --- a/library/HTMLPurifier/Language.php +++ b/library/HTMLPurifier/Language.php @@ -25,6 +25,13 @@ class HTMLPurifier_Language */ var $errorNames = array(); + /** + * True if no message file was found for this language, so English + * is being used instead. Check this if you'd like to notify the + * user that they've used a non-supported language. + */ + var $error = false; + /** * Has the language object been loaded yet? * @private diff --git a/library/HTMLPurifier/Language/messages/en-x-testmini.php b/library/HTMLPurifier/Language/messages/en-x-testmini.php new file mode 100644 index 00000000..4b16cd20 --- /dev/null +++ b/library/HTMLPurifier/Language/messages/en-x-testmini.php @@ -0,0 +1,11 @@ + 'HTML Purifier XNone' +); + diff --git a/library/HTMLPurifier/LanguageFactory.php b/library/HTMLPurifier/LanguageFactory.php index 9d26cd70..715c3fee 100644 --- a/library/HTMLPurifier/LanguageFactory.php +++ b/library/HTMLPurifier/LanguageFactory.php @@ -16,6 +16,7 @@ This directive has been available since 2.0.0. * caching and fallbacks. * @note Thanks to MediaWiki for the general logic, although this version * has been entirely rewritten + * @todo Serialized cache for languages */ class HTMLPurifier_LanguageFactory { @@ -89,40 +90,42 @@ class HTMLPurifier_LanguageFactory * Creates a language object, handles class fallbacks * @param $config Instance of HTMLPurifier_Config * @param $context Instance of HTMLPurifier_Context + * @param $code Code to override configuration with. Private parameter. */ - function create($config, &$context) { + function create($config, &$context, $code = false) { // validate language code - $code = $this->validator->validate( - $config->get('Core', 'Language'), $config, $context - ); + if ($code === false) { + $code = $this->validator->validate( + $config->get('Core', 'Language'), $config, $context + ); + } else { + $code = $this->validator->validate($code, $config, $context); + } if ($code === false) $code = 'en'; // malformed code becomes English $pcode = str_replace('-', '_', $code); // make valid PHP classname static $depth = 0; // recursion protection if ($code == 'en') { - $class = 'HTMLPurifier_Language'; - $file = $this->dir . '/Language.php'; + $lang = new HTMLPurifier_Language($config, $context); } else { $class = 'HTMLPurifier_Language_' . $pcode; $file = $this->dir . '/Language/classes/' . $code . '.php'; - // PHP5/APC deps bug workaround can go here - // you can bypass the conditional include by loading the - // file yourself - if (file_exists($file) && !class_exists($class)) { - include_once $file; - } - } - - if (!class_exists($class)) { - // go fallback - $fallback = HTMLPurifier_LanguageFactory::getFallbackFor($code); - $depth++; - $lang = HTMLPurifier_LanguageFactory::factory( $fallback ); - $depth--; - } else { - $lang = new $class($config, $context); + if (file_exists($file)) { + include $file; + $lang = new $class($config, $context); + } else { + // Go fallback + $raw_fallback = $this->getFallbackFor($code); + $fallback = $raw_fallback ? $raw_fallback : 'en'; + $depth++; + $lang = $this->create($config, $context, $fallback); + if (!$raw_fallback) { + $lang->error = true; + } + $depth--; + } } $lang->code = $code; diff --git a/tests/HTMLPurifier/LanguageFactoryTest.php b/tests/HTMLPurifier/LanguageFactoryTest.php index 2cadb1c1..f9a58bfe 100644 --- a/tests/HTMLPurifier/LanguageFactoryTest.php +++ b/tests/HTMLPurifier/LanguageFactoryTest.php @@ -5,13 +5,20 @@ require_once 'HTMLPurifier/LanguageFactory.php'; class HTMLPurifier_LanguageFactoryTest extends HTMLPurifier_Harness { + /** + * Protected reference of global factory we're testing. + */ + var $factory; + + function setUp() { + $this->factory = HTMLPurifier_LanguageFactory::instance(); + parent::setUp(); + } + function test() { - $factory = HTMLPurifier_LanguageFactory::instance(); - - $config = HTMLPurifier_Config::create(array('Core.Language' => 'en')); - $context = new HTMLPurifier_Context(); - $language = $factory->create($config, $context); + $this->config->set('Core', 'Language', 'en'); + $language = $this->factory->create($this->config, $this->context); $this->assertIsA($language, 'HTMLPurifier_Language'); $this->assertIdentical($language->code, 'en'); @@ -21,18 +28,12 @@ class HTMLPurifier_LanguageFactoryTest extends HTMLPurifier_Harness $language->load(); $this->assertNotEqual(count($language->messages), 0); - // actual tests for content can be found in LanguageTest - } function testFallback() { - $factory = HTMLPurifier_LanguageFactory::instance(); - - $config = HTMLPurifier_Config::create(array('Core.Language' => 'en-x-test')); - $context = new HTMLPurifier_Context(); - - $language = $factory->create($config, $context); + $this->config->set('Core', 'Language', 'en-x-test'); + $language = $this->factory->create($this->config, $this->context); $this->assertIsA($language, 'HTMLPurifier_Language_en_x_test'); $this->assertIdentical($language->code, 'en-x-test'); @@ -47,5 +48,24 @@ class HTMLPurifier_LanguageFactoryTest extends HTMLPurifier_Harness } + function testFallbackWithNoClass() { + $this->config->set('Core', 'Language', 'en-x-testmini'); + $language = $this->factory->create($this->config, $this->context); + $this->assertIsA($language, 'HTMLPurifier_Language'); + $this->assertIdentical($language->code, 'en-x-testmini'); + $language->load(); + $this->assertIdentical($language->getMessage('HTMLPurifier'), 'HTML Purifier XNone'); + $this->assertIdentical($language->getMessage('LanguageFactoryTest: Pizza'), 'Pizza'); + $this->assertIdentical($language->error, false); + } + + function testNoSuchLanguage() { + $this->config->set('Core', 'Language', 'en-x-testnone'); + $language = $this->factory->create($this->config, $this->context); + $this->assertIsA($language, 'HTMLPurifier_Language'); + $this->assertIdentical($language->code, 'en-x-testnone'); + $this->assertIdentical($language->error, true); + } + }