4
* Performs validations on HTMLPurifier_ConfigSchema_Interchange
6
* @note If you see '// handled by InterchangeBuilder', that means a
7
* design decision in that class would prevent this validation from
8
* ever being necessary. We have them anyway, however, for
11
class HTMLPurifier_ConfigSchema_Validator
15
* Easy to access global objects.
17
protected $interchange, $aliases;
20
* Context-stack to provide easy to read error messages.
22
protected $context = array();
25
* HTMLPurifier_VarParser to test default's type.
29
public function __construct() {
30
$this->parser = new HTMLPurifier_VarParser();
34
* Validates a fully-formed interchange object. Throws an
35
* HTMLPurifier_ConfigSchema_Exception if there's a problem.
37
public function validate($interchange) {
38
$this->interchange = $interchange;
39
$this->aliases = array();
40
// PHP is a bit lax with integer <=> string conversions in
41
// arrays, so we don't use the identical !== comparison
42
foreach ($interchange->namespaces as $i => $namespace) {
43
if ($i != $namespace->namespace) $this->error(false, "Integrity violation: key '$i' does not match internal id '{$namespace->namespace}'");
44
$this->validateNamespace($namespace);
46
foreach ($interchange->directives as $i => $directive) {
47
$id = $directive->id->toString();
48
if ($i != $id) $this->error(false, "Integrity violation: key '$i' does not match internal id '$id'");
49
$this->validateDirective($directive);
55
* Validates a HTMLPurifier_ConfigSchema_Interchange_Namespace object.
57
public function validateNamespace($n) {
58
$this->context[] = "namespace '{$n->namespace}'";
59
$this->with($n, 'namespace')
61
->assertAlnum(); // implicit assertIsString handled by InterchangeBuilder
62
$this->with($n, 'description')
64
->assertIsString(); // handled by InterchangeBuilder
65
array_pop($this->context);
69
* Validates a HTMLPurifier_ConfigSchema_Interchange_Id object.
71
public function validateId($id) {
72
$id_string = $id->toString();
73
$this->context[] = "id '$id_string'";
74
if (!$id instanceof HTMLPurifier_ConfigSchema_Interchange_Id) {
75
// handled by InterchangeBuilder
76
$this->error(false, 'is not an instance of HTMLPurifier_ConfigSchema_Interchange_Id');
78
if (!isset($this->interchange->namespaces[$id->namespace])) {
79
$this->error('namespace', 'does not exist'); // assumes that the namespace was validated already
81
$this->with($id, 'directive')
83
->assertAlnum(); // implicit assertIsString handled by InterchangeBuilder
84
array_pop($this->context);
88
* Validates a HTMLPurifier_ConfigSchema_Interchange_Directive object.
90
public function validateDirective($d) {
91
$id = $d->id->toString();
92
$this->context[] = "directive '$id'";
93
$this->validateId($d->id);
95
$this->with($d, 'description')
98
// BEGIN - handled by InterchangeBuilder
99
$this->with($d, 'type')
101
$this->with($d, 'typeAllowsNull')
104
// This also tests validity of $d->type
105
$this->parser->parse($d->default, $d->type, $d->typeAllowsNull);
106
} catch (HTMLPurifier_VarParserException $e) {
107
$this->error('default', 'had error: ' . $e->getMessage());
109
// END - handled by InterchangeBuilder
111
if (!is_null($d->allowed) || !empty($d->valueAliases)) {
112
// allowed and valueAliases require that we be dealing with
113
// strings, so check for that early.
114
$d_int = HTMLPurifier_VarParser::$types[$d->type];
115
if (!isset(HTMLPurifier_VarParser::$stringTypes[$d_int])) {
116
$this->error('type', 'must be a string type when used with allowed or value aliases');
120
$this->validateDirectiveAllowed($d);
121
$this->validateDirectiveValueAliases($d);
122
$this->validateDirectiveAliases($d);
124
array_pop($this->context);
128
* Extra validation if $allowed member variable of
129
* HTMLPurifier_ConfigSchema_Interchange_Directive is defined.
131
public function validateDirectiveAllowed($d) {
132
if (is_null($d->allowed)) return;
133
$this->with($d, 'allowed')
135
->assertIsLookup(); // handled by InterchangeBuilder
136
if (is_string($d->default) && !isset($d->allowed[$d->default])) {
137
$this->error('default', 'must be an allowed value');
139
$this->context[] = 'allowed';
140
foreach ($d->allowed as $val => $x) {
141
if (!is_string($val)) $this->error("value $val", 'must be a string');
143
array_pop($this->context);
147
* Extra validation if $valueAliases member variable of
148
* HTMLPurifier_ConfigSchema_Interchange_Directive is defined.
150
public function validateDirectiveValueAliases($d) {
151
if (is_null($d->valueAliases)) return;
152
$this->with($d, 'valueAliases')
153
->assertIsArray(); // handled by InterchangeBuilder
154
$this->context[] = 'valueAliases';
155
foreach ($d->valueAliases as $alias => $real) {
156
if (!is_string($alias)) $this->error("alias $alias", 'must be a string');
157
if (!is_string($real)) $this->error("alias target $real from alias '$alias'", 'must be a string');
158
if ($alias === $real) {
159
$this->error("alias '$alias'", "must not be an alias to itself");
162
if (!is_null($d->allowed)) {
163
foreach ($d->valueAliases as $alias => $real) {
164
if (isset($d->allowed[$alias])) {
165
$this->error("alias '$alias'", 'must not be an allowed value');
166
} elseif (!isset($d->allowed[$real])) {
167
$this->error("alias '$alias'", 'must be an alias to an allowed value');
171
array_pop($this->context);
175
* Extra validation if $aliases member variable of
176
* HTMLPurifier_ConfigSchema_Interchange_Directive is defined.
178
public function validateDirectiveAliases($d) {
179
$this->with($d, 'aliases')
180
->assertIsArray(); // handled by InterchangeBuilder
181
$this->context[] = 'aliases';
182
foreach ($d->aliases as $alias) {
183
$this->validateId($alias);
184
$s = $alias->toString();
185
if (isset($this->interchange->directives[$s])) {
186
$this->error("alias '$s'", 'collides with another directive');
188
if (isset($this->aliases[$s])) {
189
$other_directive = $this->aliases[$s];
190
$this->error("alias '$s'", "collides with alias for directive '$other_directive'");
192
$this->aliases[$s] = $d->id->toString();
194
array_pop($this->context);
197
// protected helper functions
200
* Convenience function for generating HTMLPurifier_ConfigSchema_ValidatorAtom
201
* for validating simple member variables of objects.
203
protected function with($obj, $member) {
204
return new HTMLPurifier_ConfigSchema_ValidatorAtom($this->getFormattedContext(), $obj, $member);
208
* Emits an error, providing helpful context.
210
protected function error($target, $msg) {
211
if ($target !== false) $prefix = ucfirst($target) . ' in ' . $this->getFormattedContext();
212
else $prefix = ucfirst($this->getFormattedContext());
213
throw new HTMLPurifier_ConfigSchema_Exception(trim($prefix . ' ' . $msg));
217
* Returns a formatted context string.
219
protected function getFormattedContext() {
220
return implode(' in ', array_reverse($this->context));
4
* Performs validations on HTMLPurifier_ConfigSchema_Interchange
6
* @note If you see '// handled by InterchangeBuilder', that means a
7
* design decision in that class would prevent this validation from
8
* ever being necessary. We have them anyway, however, for
11
class HTMLPurifier_ConfigSchema_Validator
15
* Easy to access global objects.
17
protected $interchange, $aliases;
20
* Context-stack to provide easy to read error messages.
22
protected $context = array();
25
* HTMLPurifier_VarParser to test default's type.
29
public function __construct() {
30
$this->parser = new HTMLPurifier_VarParser();
34
* Validates a fully-formed interchange object. Throws an
35
* HTMLPurifier_ConfigSchema_Exception if there's a problem.
37
public function validate($interchange) {
38
$this->interchange = $interchange;
39
$this->aliases = array();
40
// PHP is a bit lax with integer <=> string conversions in
41
// arrays, so we don't use the identical !== comparison
42
foreach ($interchange->directives as $i => $directive) {
43
$id = $directive->id->toString();
44
if ($i != $id) $this->error(false, "Integrity violation: key '$i' does not match internal id '$id'");
45
$this->validateDirective($directive);
51
* Validates a HTMLPurifier_ConfigSchema_Interchange_Id object.
53
public function validateId($id) {
54
$id_string = $id->toString();
55
$this->context[] = "id '$id_string'";
56
if (!$id instanceof HTMLPurifier_ConfigSchema_Interchange_Id) {
57
// handled by InterchangeBuilder
58
$this->error(false, 'is not an instance of HTMLPurifier_ConfigSchema_Interchange_Id');
60
// keys are now unconstrained (we might want to narrow down to A-Za-z0-9.)
61
// we probably should check that it has at least one namespace
62
$this->with($id, 'key')
64
->assertIsString(); // implicit assertIsString handled by InterchangeBuilder
65
array_pop($this->context);
69
* Validates a HTMLPurifier_ConfigSchema_Interchange_Directive object.
71
public function validateDirective($d) {
72
$id = $d->id->toString();
73
$this->context[] = "directive '$id'";
74
$this->validateId($d->id);
76
$this->with($d, 'description')
79
// BEGIN - handled by InterchangeBuilder
80
$this->with($d, 'type')
82
$this->with($d, 'typeAllowsNull')
85
// This also tests validity of $d->type
86
$this->parser->parse($d->default, $d->type, $d->typeAllowsNull);
87
} catch (HTMLPurifier_VarParserException $e) {
88
$this->error('default', 'had error: ' . $e->getMessage());
90
// END - handled by InterchangeBuilder
92
if (!is_null($d->allowed) || !empty($d->valueAliases)) {
93
// allowed and valueAliases require that we be dealing with
94
// strings, so check for that early.
95
$d_int = HTMLPurifier_VarParser::$types[$d->type];
96
if (!isset(HTMLPurifier_VarParser::$stringTypes[$d_int])) {
97
$this->error('type', 'must be a string type when used with allowed or value aliases');
101
$this->validateDirectiveAllowed($d);
102
$this->validateDirectiveValueAliases($d);
103
$this->validateDirectiveAliases($d);
105
array_pop($this->context);
109
* Extra validation if $allowed member variable of
110
* HTMLPurifier_ConfigSchema_Interchange_Directive is defined.
112
public function validateDirectiveAllowed($d) {
113
if (is_null($d->allowed)) return;
114
$this->with($d, 'allowed')
116
->assertIsLookup(); // handled by InterchangeBuilder
117
if (is_string($d->default) && !isset($d->allowed[$d->default])) {
118
$this->error('default', 'must be an allowed value');
120
$this->context[] = 'allowed';
121
foreach ($d->allowed as $val => $x) {
122
if (!is_string($val)) $this->error("value $val", 'must be a string');
124
array_pop($this->context);
128
* Extra validation if $valueAliases member variable of
129
* HTMLPurifier_ConfigSchema_Interchange_Directive is defined.
131
public function validateDirectiveValueAliases($d) {
132
if (is_null($d->valueAliases)) return;
133
$this->with($d, 'valueAliases')
134
->assertIsArray(); // handled by InterchangeBuilder
135
$this->context[] = 'valueAliases';
136
foreach ($d->valueAliases as $alias => $real) {
137
if (!is_string($alias)) $this->error("alias $alias", 'must be a string');
138
if (!is_string($real)) $this->error("alias target $real from alias '$alias'", 'must be a string');
139
if ($alias === $real) {
140
$this->error("alias '$alias'", "must not be an alias to itself");
143
if (!is_null($d->allowed)) {
144
foreach ($d->valueAliases as $alias => $real) {
145
if (isset($d->allowed[$alias])) {
146
$this->error("alias '$alias'", 'must not be an allowed value');
147
} elseif (!isset($d->allowed[$real])) {
148
$this->error("alias '$alias'", 'must be an alias to an allowed value');
152
array_pop($this->context);
156
* Extra validation if $aliases member variable of
157
* HTMLPurifier_ConfigSchema_Interchange_Directive is defined.
159
public function validateDirectiveAliases($d) {
160
$this->with($d, 'aliases')
161
->assertIsArray(); // handled by InterchangeBuilder
162
$this->context[] = 'aliases';
163
foreach ($d->aliases as $alias) {
164
$this->validateId($alias);
165
$s = $alias->toString();
166
if (isset($this->interchange->directives[$s])) {
167
$this->error("alias '$s'", 'collides with another directive');
169
if (isset($this->aliases[$s])) {
170
$other_directive = $this->aliases[$s];
171
$this->error("alias '$s'", "collides with alias for directive '$other_directive'");
173
$this->aliases[$s] = $d->id->toString();
175
array_pop($this->context);
178
// protected helper functions
181
* Convenience function for generating HTMLPurifier_ConfigSchema_ValidatorAtom
182
* for validating simple member variables of objects.
184
protected function with($obj, $member) {
185
return new HTMLPurifier_ConfigSchema_ValidatorAtom($this->getFormattedContext(), $obj, $member);
189
* Emits an error, providing helpful context.
191
protected function error($target, $msg) {
192
if ($target !== false) $prefix = ucfirst($target) . ' in ' . $this->getFormattedContext();
193
else $prefix = ucfirst($this->getFormattedContext());
194
throw new HTMLPurifier_ConfigSchema_Exception(trim($prefix . ' ' . $msg));
198
* Returns a formatted context string.
200
protected function getFormattedContext() {
201
return implode(' in ', array_reverse($this->context));
206
// vim: et sw=4 sts=4