0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-22 16:31:53 +00:00

[3.1.0] Feature-parity achieved for validator! Implement alias checking.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1648 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-04-04 22:04:10 +00:00
parent 0ee090bc7b
commit d467af6c4b
5 changed files with 112 additions and 2 deletions

View File

@ -11,7 +11,10 @@
class HTMLPurifier_ConfigSchema_Validator class HTMLPurifier_ConfigSchema_Validator
{ {
protected $interchange; /**
* Easy to access global objects.
*/
protected $interchange, $aliases;
/** /**
* Context-stack to provide easy to read error messages. * Context-stack to provide easy to read error messages.
@ -19,7 +22,7 @@ class HTMLPurifier_ConfigSchema_Validator
protected $context = array(); protected $context = array();
/** /**
* HTMLPurifier_VarParser to test variable types. * HTMLPurifier_VarParser to test default's type.
*/ */
protected $parser; protected $parser;
@ -33,6 +36,7 @@ class HTMLPurifier_ConfigSchema_Validator
*/ */
public function validate($interchange) { public function validate($interchange) {
$this->interchange = $interchange; $this->interchange = $interchange;
$this->aliases = array();
// PHP is a bit lax with integer <=> string conversions in // PHP is a bit lax with integer <=> string conversions in
// arrays, so we don't use the identical !== comparison // arrays, so we don't use the identical !== comparison
foreach ($interchange->namespaces as $i => $namespace) { foreach ($interchange->namespaces as $i => $namespace) {
@ -46,6 +50,9 @@ class HTMLPurifier_ConfigSchema_Validator
} }
} }
/**
* Validates a HTMLPurifier_ConfigSchema_Interchange_Namespace object.
*/
public function validateNamespace($n) { public function validateNamespace($n) {
$this->context[] = "namespace '{$n->namespace}'"; $this->context[] = "namespace '{$n->namespace}'";
$this->with($n, 'namespace') $this->with($n, 'namespace')
@ -57,6 +64,9 @@ class HTMLPurifier_ConfigSchema_Validator
array_pop($this->context); array_pop($this->context);
} }
/**
* Validates a HTMLPurifier_ConfigSchema_Interchange_Id object.
*/
public function validateId($id) { public function validateId($id) {
$id_string = $id->toString(); $id_string = $id->toString();
$this->context[] = "id '$id_string'"; $this->context[] = "id '$id_string'";
@ -73,10 +83,14 @@ class HTMLPurifier_ConfigSchema_Validator
array_pop($this->context); array_pop($this->context);
} }
/**
* Validates a HTMLPurifier_ConfigSchema_Interchange_Directive object.
*/
public function validateDirective($d) { public function validateDirective($d) {
$id = $d->id->toString(); $id = $d->id->toString();
$this->context[] = "directive '$id'"; $this->context[] = "directive '$id'";
$this->validateId($d->id); $this->validateId($d->id);
$this->with($d, 'description') $this->with($d, 'description')
->assertNotEmpty(); ->assertNotEmpty();
@ -103,15 +117,23 @@ class HTMLPurifier_ConfigSchema_Validator
$this->validateDirectiveAllowed($d); $this->validateDirectiveAllowed($d);
$this->validateDirectiveValueAliases($d); $this->validateDirectiveValueAliases($d);
$this->validateDirectiveAliases($d);
array_pop($this->context); array_pop($this->context);
} }
/**
* Extra validation if $allowed member variable of
* HTMLPurifier_ConfigSchema_Interchange_Directive is defined.
*/
public function validateDirectiveAllowed($d) { public function validateDirectiveAllowed($d) {
if (is_null($d->allowed)) return; if (is_null($d->allowed)) return;
$this->with($d, 'allowed') $this->with($d, 'allowed')
->assertNotEmpty() ->assertNotEmpty()
->assertIsLookup(); // handled by InterchangeBuilder ->assertIsLookup(); // handled by InterchangeBuilder
if (!isset($d->allowed[$d->default])) {
$this->error('default', 'must be an allowed value');
}
$this->context[] = 'allowed'; $this->context[] = 'allowed';
foreach ($d->allowed as $val => $x) { foreach ($d->allowed as $val => $x) {
if (!is_string($val)) $this->error("value $val", 'must be a string'); if (!is_string($val)) $this->error("value $val", 'must be a string');
@ -119,6 +141,10 @@ class HTMLPurifier_ConfigSchema_Validator
array_pop($this->context); array_pop($this->context);
} }
/**
* Extra validation if $valueAliases member variable of
* HTMLPurifier_ConfigSchema_Interchange_Directive is defined.
*/
public function validateDirectiveValueAliases($d) { public function validateDirectiveValueAliases($d) {
if (is_null($d->valueAliases)) return; if (is_null($d->valueAliases)) return;
$this->with($d, 'valueAliases') $this->with($d, 'valueAliases')
@ -143,18 +169,51 @@ class HTMLPurifier_ConfigSchema_Validator
array_pop($this->context); array_pop($this->context);
} }
/**
* Extra validation if $aliases member variable of
* HTMLPurifier_ConfigSchema_Interchange_Directive is defined.
*/
public function validateDirectiveAliases($d) {
$this->with($d, 'aliases')
->assertIsArray(); // handled by InterchangeBuilder
$this->context[] = 'aliases';
foreach ($d->aliases as $alias) {
$this->validateId($alias);
$s = $alias->toString();
if (isset($this->interchange->directives[$s])) {
$this->error("alias '$s'", 'collides with another directive');
}
if (isset($this->aliases[$s])) {
$other_directive = $this->aliases[$s];
$this->error("alias '$s'", "collides with alias for directive '$other_directive'");
}
$this->aliases[$s] = $d->id->toString();
}
array_pop($this->context);
}
// protected helper functions // protected helper functions
/**
* Convenience function for generating HTMLPurifier_ConfigSchema_ValidatorAtom
* for validating simple member variables of objects.
*/
protected function with($obj, $member) { protected function with($obj, $member) {
return new HTMLPurifier_ConfigSchema_ValidatorAtom($this->getFormattedContext(), $obj, $member); return new HTMLPurifier_ConfigSchema_ValidatorAtom($this->getFormattedContext(), $obj, $member);
} }
/**
* Emits an error, providing helpful context.
*/
protected function error($target, $msg) { protected function error($target, $msg) {
if ($target !== false) $prefix = ucfirst($target) . ' in ' . $this->getFormattedContext(); if ($target !== false) $prefix = ucfirst($target) . ' in ' . $this->getFormattedContext();
else $prefix = ucfirst($this->getFormattedContext()); else $prefix = ucfirst($this->getFormattedContext());
throw new HTMLPurifier_ConfigSchema_Exception(trim($prefix . ' ' . $msg)); throw new HTMLPurifier_ConfigSchema_Exception(trim($prefix . ' ' . $msg));
} }
/**
* Returns a formatted context string.
*/
protected function getFormattedContext() { protected function getFormattedContext() {
return implode(' in ', array_reverse($this->context)); return implode(' in ', array_reverse($this->context));
} }

View File

@ -0,0 +1,16 @@
ERROR: Alias 'Ns.BothWantThisName' in aliases in directive 'Ns.Dir2' collides with alias for directive 'Ns.Dir'
----
Ns
DESCRIPTION: Namespace
----
Ns.Dir
DESCRIPTION: Directive
TYPE: int
DEFAULT: 3
ALIASES: Ns.BothWantThisName
----
Ns.Dir2
DESCRIPTION: Directive
TYPE: string
DEFAULT: 'a'
ALIASES: Ns.BothWantThisName

View File

@ -0,0 +1,15 @@
ERROR: Alias 'Ns.Innocent' in aliases in directive 'Ns.Dir' collides with another directive
----
Ns
DESCRIPTION: Namespace
----
Ns.Innocent
DESCRIPTION: Innocent directive
TYPE: int
DEFAULT: 3
----
Ns.Dir
DESCRIPTION: Directive
TYPE: string
DEFAULT: 'a'
ALIASES: Ns.Innocent

View File

@ -0,0 +1,10 @@
ERROR: Directive in id 'Ns.R&D' in aliases in directive 'Ns.Dir' must be alphanumeric
----
Ns
DESCRIPTION: Namespace
----
Ns.Dir
DESCRIPTION: Directive
TYPE: string
DEFAULT: 'a'
ALIASES: Ns.R&D

View File

@ -0,0 +1,10 @@
ERROR: Default in directive 'Ns.Dir' must be an allowed value
----
Ns
DESCRIPTION: Namespace
----
Ns.Dir
DESCRIPTION: Directive
TYPE: string
DEFAULT: 'a'
ALLOWED: 'b'