1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-08-02 12:21:09 +02:00

[2.1.4] [MFH] Fixed bug with fallback languages in LanguageFactory

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/branches/php4@1724 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2008-05-15 23:20:21 +00:00
parent a2aca4819d
commit f26eb7551a
5 changed files with 77 additions and 35 deletions

1
NEWS
View File

@@ -32,6 +32,7 @@ ERRATA
- Iconv uses set_error_handler instead of shut-up operator - Iconv uses set_error_handler instead of shut-up operator
- Add protection against imagecrash attack with CSS height/width - Add protection against imagecrash attack with CSS height/width
- HTMLPurifier::getInstance() renamed to HTMLPurifier::instance() for consistency - HTMLPurifier::getInstance() renamed to HTMLPurifier::instance() for consistency
- Fixed bug with fallback languages in LanguageFactory
2.1.3, released 2007-11-05 2.1.3, released 2007-11-05
! tests/multitest.php allows you to test multiple versions by running ! tests/multitest.php allows you to test multiple versions by running

View File

@@ -25,6 +25,13 @@ class HTMLPurifier_Language
*/ */
var $errorNames = array(); 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? * Has the language object been loaded yet?
* @private * @private

View File

@@ -0,0 +1,11 @@
<?php
// private language message file for unit testing purposes
// this language file has no class associated with it
$fallback = 'en';
$messages = array(
'HTMLPurifier' => 'HTML Purifier XNone'
);

View File

@@ -16,6 +16,7 @@ This directive has been available since 2.0.0.
* caching and fallbacks. * caching and fallbacks.
* @note Thanks to MediaWiki for the general logic, although this version * @note Thanks to MediaWiki for the general logic, although this version
* has been entirely rewritten * has been entirely rewritten
* @todo Serialized cache for languages
*/ */
class HTMLPurifier_LanguageFactory class HTMLPurifier_LanguageFactory
{ {
@@ -89,40 +90,42 @@ class HTMLPurifier_LanguageFactory
* Creates a language object, handles class fallbacks * Creates a language object, handles class fallbacks
* @param $config Instance of HTMLPurifier_Config * @param $config Instance of HTMLPurifier_Config
* @param $context Instance of HTMLPurifier_Context * @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 // validate language code
if ($code === false) {
$code = $this->validator->validate( $code = $this->validator->validate(
$config->get('Core', 'Language'), $config, $context $config->get('Core', 'Language'), $config, $context
); );
} else {
$code = $this->validator->validate($code, $config, $context);
}
if ($code === false) $code = 'en'; // malformed code becomes English if ($code === false) $code = 'en'; // malformed code becomes English
$pcode = str_replace('-', '_', $code); // make valid PHP classname $pcode = str_replace('-', '_', $code); // make valid PHP classname
static $depth = 0; // recursion protection static $depth = 0; // recursion protection
if ($code == 'en') { if ($code == 'en') {
$class = 'HTMLPurifier_Language'; $lang = new HTMLPurifier_Language($config, $context);
$file = $this->dir . '/Language.php';
} else { } else {
$class = 'HTMLPurifier_Language_' . $pcode; $class = 'HTMLPurifier_Language_' . $pcode;
$file = $this->dir . '/Language/classes/' . $code . '.php'; $file = $this->dir . '/Language/classes/' . $code . '.php';
// PHP5/APC deps bug workaround can go here if (file_exists($file)) {
// you can bypass the conditional include by loading the include $file;
// 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); $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; $lang->code = $code;

View File

@@ -5,13 +5,20 @@ require_once 'HTMLPurifier/LanguageFactory.php';
class HTMLPurifier_LanguageFactoryTest extends HTMLPurifier_Harness 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() { function test() {
$factory = HTMLPurifier_LanguageFactory::instance(); $this->config->set('Core', 'Language', 'en');
$language = $this->factory->create($this->config, $this->context);
$config = HTMLPurifier_Config::create(array('Core.Language' => 'en'));
$context = new HTMLPurifier_Context();
$language = $factory->create($config, $context);
$this->assertIsA($language, 'HTMLPurifier_Language'); $this->assertIsA($language, 'HTMLPurifier_Language');
$this->assertIdentical($language->code, 'en'); $this->assertIdentical($language->code, 'en');
@@ -21,18 +28,12 @@ class HTMLPurifier_LanguageFactoryTest extends HTMLPurifier_Harness
$language->load(); $language->load();
$this->assertNotEqual(count($language->messages), 0); $this->assertNotEqual(count($language->messages), 0);
// actual tests for content can be found in LanguageTest
} }
function testFallback() { function testFallback() {
$factory = HTMLPurifier_LanguageFactory::instance(); $this->config->set('Core', 'Language', 'en-x-test');
$language = $this->factory->create($this->config, $this->context);
$config = HTMLPurifier_Config::create(array('Core.Language' => 'en-x-test'));
$context = new HTMLPurifier_Context();
$language = $factory->create($config, $context);
$this->assertIsA($language, 'HTMLPurifier_Language_en_x_test'); $this->assertIsA($language, 'HTMLPurifier_Language_en_x_test');
$this->assertIdentical($language->code, '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);
}
} }