2008-03-22 20:26:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs validations on HTMLPurifier_ConfigSchema_Interchange
|
2008-03-23 02:35:47 +00:00
|
|
|
*
|
|
|
|
* @note If you see '// handled by InterchangeBuilder', that means a
|
|
|
|
* design decision in that class would prevent this validation from
|
|
|
|
* ever being necessary. We have them anyway, however, for
|
|
|
|
* redundancy.
|
2008-03-22 20:26:04 +00:00
|
|
|
*/
|
|
|
|
class HTMLPurifier_ConfigSchema_Validator
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $interchange;
|
|
|
|
|
|
|
|
/**
|
2008-03-23 01:29:57 +00:00
|
|
|
* Context-stack to provide easy to read error messages.
|
2008-03-22 20:26:04 +00:00
|
|
|
*/
|
2008-03-23 01:29:57 +00:00
|
|
|
protected $context = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTMLPurifier_VarParser to test variable types.
|
|
|
|
*/
|
|
|
|
protected $parser;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->parser = new HTMLPurifier_VarParser();
|
|
|
|
}
|
2008-03-22 20:26:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates a fully-formed interchange object. Throws an
|
|
|
|
* HTMLPurifier_ConfigSchema_Exception if there's a problem.
|
|
|
|
*/
|
|
|
|
public function validate($interchange) {
|
|
|
|
$this->interchange = $interchange;
|
2008-03-23 02:35:47 +00:00
|
|
|
// PHP is a bit lax with integer <=> string conversions in
|
|
|
|
// arrays, so we don't use the identical !== comparison
|
|
|
|
foreach ($interchange->namespaces as $i => $namespace) {
|
|
|
|
if ($i != $namespace->namespace) $this->error(false, "Integrity violation: key '$i' does not match internal id '{$namespace->namespace}'");
|
2008-03-22 20:26:04 +00:00
|
|
|
$this->validateNamespace($namespace);
|
|
|
|
}
|
2008-03-23 02:35:47 +00:00
|
|
|
foreach ($interchange->directives as $i => $directive) {
|
|
|
|
if ($i != "{$directive->id}") $this->error(false, "Integrity violation: key '$i' does not match internal id '{$directive->id}'");
|
2008-03-22 20:26:04 +00:00
|
|
|
$this->validateDirective($directive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validateNamespace($n) {
|
2008-03-23 01:06:35 +00:00
|
|
|
$this->context[] = "namespace '{$n->namespace}'";
|
2008-03-22 20:26:04 +00:00
|
|
|
$this->with($n, 'namespace')
|
|
|
|
->assertNotEmpty()
|
2008-03-23 02:35:47 +00:00
|
|
|
->assertAlnum(); // implicit assertIsString handled by InterchangeBuilder
|
2008-03-22 20:26:04 +00:00
|
|
|
$this->with($n, 'description')
|
2008-03-23 01:06:35 +00:00
|
|
|
->assertNotEmpty()
|
2008-03-23 02:35:47 +00:00
|
|
|
->assertIsString(); // handled by InterchangeBuilder
|
2008-03-23 01:06:35 +00:00
|
|
|
array_pop($this->context);
|
2008-03-22 20:26:04 +00:00
|
|
|
}
|
|
|
|
|
2008-03-22 21:06:55 +00:00
|
|
|
public function validateId($id) {
|
2008-03-23 01:06:35 +00:00
|
|
|
$this->context[] = "id '$id'";
|
2008-03-23 02:35:47 +00:00
|
|
|
if (!$id instanceof HTMLPurifier_ConfigSchema_Interchange_Id) {
|
|
|
|
// handled by InterchangeBuilder
|
|
|
|
$this->error(false, 'is not an instance of HTMLPurifier_ConfigSchema_Interchange_Id');
|
|
|
|
}
|
2008-03-23 01:06:35 +00:00
|
|
|
if (!isset($this->interchange->namespaces[$id->namespace])) {
|
2008-03-23 02:35:47 +00:00
|
|
|
$this->error('namespace', 'does not exist'); // assumes that the namespace was validated already
|
2008-03-23 01:06:35 +00:00
|
|
|
}
|
2008-03-22 21:06:55 +00:00
|
|
|
$this->with($id, 'directive')
|
|
|
|
->assertNotEmpty()
|
2008-03-23 02:35:47 +00:00
|
|
|
->assertAlnum(); // implicit assertIsString handled by InterchangeBuilder
|
2008-03-23 01:06:35 +00:00
|
|
|
array_pop($this->context);
|
2008-03-22 20:26:04 +00:00
|
|
|
}
|
|
|
|
|
2008-03-23 01:29:57 +00:00
|
|
|
public function validateDirective($d) {
|
|
|
|
$this->context[] = "directive '{$d->id}'";
|
|
|
|
$this->validateId($d->id);
|
|
|
|
$this->with($d, 'description')
|
|
|
|
->assertNotEmpty();
|
|
|
|
$this->with($d, 'type')
|
2008-03-23 02:35:47 +00:00
|
|
|
->assertNotEmpty(); // handled by InterchangeBuilder
|
|
|
|
// Much stricter default check, since we're using the base implementation.
|
|
|
|
// handled by InterchangeBuilder
|
|
|
|
try {
|
|
|
|
$this->parser->parse($d->default, $d->type, $d->typeAllowsNull);
|
|
|
|
} catch (HTMLPurifier_VarParserException $e) {
|
|
|
|
$this->error('default', 'had error: ' . $e->getMessage());
|
2008-03-23 01:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
array_pop($this->context);
|
|
|
|
}
|
|
|
|
|
2008-03-22 20:26:04 +00:00
|
|
|
// protected helper functions
|
|
|
|
|
|
|
|
protected function with($obj, $member) {
|
2008-03-23 01:06:35 +00:00
|
|
|
return new HTMLPurifier_ConfigSchema_ValidatorAtom($this->getFormattedContext(), $obj, $member);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function error($target, $msg) {
|
2008-03-23 02:35:47 +00:00
|
|
|
if ($target !== false) $prefix = ucfirst($target) . ' in ' . $this->getFormattedContext();
|
|
|
|
else $prefix = ucfirst($this->getFormattedContext());
|
|
|
|
throw new HTMLPurifier_ConfigSchema_Exception(trim($prefix . ' ' . $msg));
|
2008-03-22 21:06:55 +00:00
|
|
|
}
|
|
|
|
|
2008-03-23 01:06:35 +00:00
|
|
|
protected function getFormattedContext() {
|
|
|
|
return implode(' in ', array_reverse($this->context));
|
2008-03-22 20:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|