0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-22 08:21:52 +00:00

[3.1.0] Give ConfigSchema non-static function equivalents

- Add todo to StringHashParser

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1523 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-01-27 19:41:40 +00:00
parent 5a6021599a
commit 41e3c091e1
3 changed files with 86 additions and 51 deletions

2
NEWS
View File

@ -29,6 +29,8 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
. Dry runs now supported in SimpleTest; testing facilities improved . Dry runs now supported in SimpleTest; testing facilities improved
. Bootstrap class added for handling autoloading functionality . Bootstrap class added for handling autoloading functionality
. Implemented recursive glob at FSTools->globr . Implemented recursive glob at FSTools->globr
. ConfigSchema now has instance methods for all corresponding define*
static methods.
3.0.0, released 2008-01-06 3.0.0, released 2008-01-06
# HTML Purifier is PHP 5 only! The 2.1.x branch will be maintained # HTML Purifier is PHP 5 only! The 2.1.x branch will be maintained

View File

@ -21,6 +21,10 @@
* *
* We use this as an easy to use file-format for configuration schema * We use this as an easy to use file-format for configuration schema
* files. * files.
*
* @todo
* Put this in its own class hierarchy or something; this class
* is usage agnostic.
*/ */
class ConfigSchema_StringHashParser class ConfigSchema_StringHashParser
{ {

View File

@ -45,6 +45,11 @@ class HTMLPurifier_ConfigSchema {
*/ */
public $info_namespace = array(); public $info_namespace = array();
/**
* Application-wide singleton
*/
static protected $singleton;
/** /**
* Lookup table of allowed types. * Lookup table of allowed types.
*/ */
@ -83,14 +88,19 @@ class HTMLPurifier_ConfigSchema {
* Retrieves an instance of the application-wide configuration definition. * Retrieves an instance of the application-wide configuration definition.
*/ */
public static function &instance($prototype = null) { public static function &instance($prototype = null) {
static $instance;
if ($prototype !== null) { if ($prototype !== null) {
$instance = $prototype; HTMLPurifier_ConfigSchema::$singleton = $prototype;
} elseif ($instance === null || $prototype === true) { } elseif (HTMLPurifier_ConfigSchema::$singleton === null || $prototype === true) {
$instance = new HTMLPurifier_ConfigSchema(); HTMLPurifier_ConfigSchema::$singleton = new HTMLPurifier_ConfigSchema();
$instance->initialize(); HTMLPurifier_ConfigSchema::$singleton->initialize();
} }
return $instance; return HTMLPurifier_ConfigSchema::$singleton;
}
/** @see HTMLPurifier_ConfigSchema->set() */
public static function define($namespace, $name, $default, $type, $description) {
$def =& HTMLPurifier_ConfigSchema::instance();
$def->add($namespace, $name, $default, $type, $description);
} }
/** /**
@ -103,12 +113,10 @@ class HTMLPurifier_ConfigSchema {
* HTMLPurifier_DirectiveDef::$type for allowed values * HTMLPurifier_DirectiveDef::$type for allowed values
* @param $description Description of directive for documentation * @param $description Description of directive for documentation
*/ */
public static function define($namespace, $name, $default, $type, $description) { public function add($namespace, $name, $default, $type, $description) {
$def =& HTMLPurifier_ConfigSchema::instance();
// basic sanity checks // basic sanity checks
if (HTMLPURIFIER_SCHEMA_STRICT) { if (HTMLPURIFIER_SCHEMA_STRICT) {
if (!isset($def->info[$namespace])) { if (!isset($this->info[$namespace])) {
trigger_error('Cannot define directive for undefined namespace', trigger_error('Cannot define directive for undefined namespace',
E_USER_ERROR); E_USER_ERROR);
return; return;
@ -125,11 +133,11 @@ class HTMLPurifier_ConfigSchema {
} }
} }
if (isset($def->info[$namespace][$name])) { if (isset($this->info[$namespace][$name])) {
// already defined // already defined
if ( if (
$def->info[$namespace][$name]->type !== $type || $this->info[$namespace][$name]->type !== $type ||
$def->defaults[$namespace][$name] !== $default $this->defaults[$namespace][$name] !== $default
) { ) {
trigger_error('Inconsistent default or type, cannot redefine'); trigger_error('Inconsistent default or type, cannot redefine');
return; return;
@ -144,30 +152,37 @@ class HTMLPurifier_ConfigSchema {
$allow_null = ($modifier === 'null'); $allow_null = ($modifier === 'null');
if (HTMLPURIFIER_SCHEMA_STRICT) { if (HTMLPURIFIER_SCHEMA_STRICT) {
if (!isset($def->types[$type])) { if (!isset($this->types[$type])) {
trigger_error('Invalid type for configuration directive', trigger_error('Invalid type for configuration directive',
E_USER_ERROR); E_USER_ERROR);
return; return;
} }
$default = $def->validate($default, $type, $allow_null); $default = $this->validate($default, $type, $allow_null);
if ($def->isError($default)) { if ($this->isError($default)) {
trigger_error('Default value does not match directive type', trigger_error('Default value does not match directive type',
E_USER_ERROR); E_USER_ERROR);
return; return;
} }
} }
$def->info[$namespace][$name] = $this->info[$namespace][$name] =
new HTMLPurifier_ConfigDef_Directive(); new HTMLPurifier_ConfigDef_Directive();
$def->info[$namespace][$name]->type = $type; $this->info[$namespace][$name]->type = $type;
$def->info[$namespace][$name]->allow_null = $allow_null; $this->info[$namespace][$name]->allow_null = $allow_null;
$def->defaults[$namespace][$name] = $default; $this->defaults[$namespace][$name] = $default;
} }
if (!HTMLPURIFIER_SCHEMA_STRICT) return; if (!HTMLPURIFIER_SCHEMA_STRICT) return;
// This will be removed soon:
$backtrace = debug_backtrace(); $backtrace = debug_backtrace();
$file = $def->mungeFilename($backtrace[0]['file']); $file = $this->mungeFilename($backtrace[1]['file']);
$line = $backtrace[0]['line']; $line = $backtrace[1]['line'];
$def->info[$namespace][$name]->addDescription($file,$line,$description); $this->info[$namespace][$name]->addDescription($file,$line,$description);
}
/** @see HTMLPurifier_ConfigSchema->addNamespace() */
public static function defineNamespace($namespace, $description) {
$def =& HTMLPurifier_ConfigSchema::instance();
$def->addNamespace($namespace, $description);
} }
/** /**
@ -175,10 +190,9 @@ class HTMLPurifier_ConfigSchema {
* @param $namespace Namespace's name * @param $namespace Namespace's name
* @param $description Description of the namespace * @param $description Description of the namespace
*/ */
public static function defineNamespace($namespace, $description) { public function addNamespace($namespace, $description) {
$def =& HTMLPurifier_ConfigSchema::instance();
if (HTMLPURIFIER_SCHEMA_STRICT) { if (HTMLPURIFIER_SCHEMA_STRICT) {
if (isset($def->info[$namespace])) { if (isset($this->info[$namespace])) {
trigger_error('Cannot redefine namespace', E_USER_ERROR); trigger_error('Cannot redefine namespace', E_USER_ERROR);
return; return;
} }
@ -193,10 +207,16 @@ class HTMLPurifier_ConfigSchema {
return; return;
} }
} }
$def->info[$namespace] = array(); $this->info[$namespace] = array();
$def->info_namespace[$namespace] = new HTMLPurifier_ConfigDef_Namespace(); $this->info_namespace[$namespace] = new HTMLPurifier_ConfigDef_Namespace();
$def->info_namespace[$namespace]->description = $description; $this->info_namespace[$namespace]->description = $description;
$def->defaults[$namespace] = array(); $this->defaults[$namespace] = array();
}
/** @see HTMLPurifier_ConfigSchema->addValueAliases() */
public static function defineValueAliases($namespace, $name, $aliases) {
$def =& HTMLPurifier_ConfigSchema::instance();
$def->addValueAliases($namespace, $name, $aliases);
} }
/** /**
@ -209,46 +229,50 @@ class HTMLPurifier_ConfigSchema {
* @param $alias Name of aliased value * @param $alias Name of aliased value
* @param $real Value aliased value will be converted into * @param $real Value aliased value will be converted into
*/ */
public static function defineValueAliases($namespace, $name, $aliases) { public function addValueAliases($namespace, $name, $aliases) {
$def =& HTMLPurifier_ConfigSchema::instance(); if (HTMLPURIFIER_SCHEMA_STRICT && !isset($this->info[$namespace][$name])) {
if (HTMLPURIFIER_SCHEMA_STRICT && !isset($def->info[$namespace][$name])) {
trigger_error('Cannot set value alias for non-existant directive', trigger_error('Cannot set value alias for non-existant directive',
E_USER_ERROR); E_USER_ERROR);
return; return;
} }
foreach ($aliases as $alias => $real) { foreach ($aliases as $alias => $real) {
if (HTMLPURIFIER_SCHEMA_STRICT) { if (HTMLPURIFIER_SCHEMA_STRICT) {
if (!$def->info[$namespace][$name] !== true && if (!$this->info[$namespace][$name] !== true &&
!isset($def->info[$namespace][$name]->allowed[$real]) !isset($this->info[$namespace][$name]->allowed[$real])
) { ) {
trigger_error('Cannot define alias to value that is not allowed', trigger_error('Cannot define alias to value that is not allowed',
E_USER_ERROR); E_USER_ERROR);
return; return;
} }
if (isset($def->info[$namespace][$name]->allowed[$alias])) { if (isset($this->info[$namespace][$name]->allowed[$alias])) {
trigger_error('Cannot define alias over allowed value', trigger_error('Cannot define alias over allowed value',
E_USER_ERROR); E_USER_ERROR);
return; return;
} }
} }
$def->info[$namespace][$name]->aliases[$alias] = $real; $this->info[$namespace][$name]->aliases[$alias] = $real;
} }
} }
/** @see HTMLPurifier_ConfigSchema->addAllowedValues() */
public static function defineAllowedValues($namespace, $name, $allowed_values) {
$def =& HTMLPurifier_ConfigSchema::instance();
$def->addAllowedValues($namespace, $name, $allowed_values);
}
/** /**
* Defines a set of allowed values for a directive. * Defines a set of allowed values for a directive.
* @param $namespace Namespace of directive * @param $namespace Namespace of directive
* @param $name Name of directive * @param $name Name of directive
* @param $allowed_values Arraylist of allowed values * @param $allowed_values Arraylist of allowed values
*/ */
public static function defineAllowedValues($namespace, $name, $allowed_values) { public function addAllowedValues($namespace, $name, $allowed_values) {
$def =& HTMLPurifier_ConfigSchema::instance(); if (HTMLPURIFIER_SCHEMA_STRICT && !isset($this->info[$namespace][$name])) {
if (HTMLPURIFIER_SCHEMA_STRICT && !isset($def->info[$namespace][$name])) {
trigger_error('Cannot define allowed values for undefined directive', trigger_error('Cannot define allowed values for undefined directive',
E_USER_ERROR); E_USER_ERROR);
return; return;
} }
$directive =& $def->info[$namespace][$name]; $directive =& $this->info[$namespace][$name];
$type = $directive->type; $type = $directive->type;
if (HTMLPURIFIER_SCHEMA_STRICT && $type != 'string' && $type != 'istring') { if (HTMLPURIFIER_SCHEMA_STRICT && $type != 'string' && $type != 'istring') {
trigger_error('Cannot define allowed values for directive whose type is not string', trigger_error('Cannot define allowed values for directive whose type is not string',
@ -263,8 +287,8 @@ class HTMLPurifier_ConfigSchema {
} }
if ( if (
HTMLPURIFIER_SCHEMA_STRICT && HTMLPURIFIER_SCHEMA_STRICT &&
$def->defaults[$namespace][$name] !== null && $this->defaults[$namespace][$name] !== null &&
!isset($directive->allowed[$def->defaults[$namespace][$name]]) !isset($directive->allowed[$this->defaults[$namespace][$name]])
) { ) {
trigger_error('Default value must be in allowed range of variables', trigger_error('Default value must be in allowed range of variables',
E_USER_ERROR); E_USER_ERROR);
@ -273,6 +297,12 @@ class HTMLPurifier_ConfigSchema {
} }
} }
/** @see HTMLPurifier_ConfigSchema->addAlias() */
public static function defineAlias($namespace, $name, $new_namespace, $new_name) {
$def =& HTMLPurifier_ConfigSchema::instance();
$def->addAlias($namespace, $name, $new_namespace, $new_name);
}
/** /**
* Defines a directive alias for backwards compatibility * Defines a directive alias for backwards compatibility
* @param $namespace * @param $namespace
@ -280,10 +310,9 @@ class HTMLPurifier_ConfigSchema {
* @param $new_namespace * @param $new_namespace
* @param $new_name Directive that the alias will be to * @param $new_name Directive that the alias will be to
*/ */
public static function defineAlias($namespace, $name, $new_namespace, $new_name) { public function addAlias($namespace, $name, $new_namespace, $new_name) {
$def =& HTMLPurifier_ConfigSchema::instance();
if (HTMLPURIFIER_SCHEMA_STRICT) { if (HTMLPURIFIER_SCHEMA_STRICT) {
if (!isset($def->info[$namespace])) { if (!isset($this->info[$namespace])) {
trigger_error('Cannot define directive alias in undefined namespace', trigger_error('Cannot define directive alias in undefined namespace',
E_USER_ERROR); E_USER_ERROR);
return; return;
@ -293,26 +322,26 @@ class HTMLPurifier_ConfigSchema {
E_USER_ERROR); E_USER_ERROR);
return; return;
} }
if (isset($def->info[$namespace][$name])) { if (isset($this->info[$namespace][$name])) {
trigger_error('Cannot define alias over directive', trigger_error('Cannot define alias over directive',
E_USER_ERROR); E_USER_ERROR);
return; return;
} }
if (!isset($def->info[$new_namespace][$new_name])) { if (!isset($this->info[$new_namespace][$new_name])) {
trigger_error('Cannot define alias to undefined directive', trigger_error('Cannot define alias to undefined directive',
E_USER_ERROR); E_USER_ERROR);
return; return;
} }
if ($def->info[$new_namespace][$new_name]->class == 'alias') { if ($this->info[$new_namespace][$new_name]->class == 'alias') {
trigger_error('Cannot define alias to alias', trigger_error('Cannot define alias to alias',
E_USER_ERROR); E_USER_ERROR);
return; return;
} }
} }
$def->info[$namespace][$name] = $this->info[$namespace][$name] =
new HTMLPurifier_ConfigDef_DirectiveAlias( new HTMLPurifier_ConfigDef_DirectiveAlias(
$new_namespace, $new_name); $new_namespace, $new_name);
$def->info[$new_namespace][$new_name]->directiveAliases[] = "$namespace.$name"; $this->info[$new_namespace][$new_name]->directiveAliases[] = "$namespace.$name";
} }
/** /**