| 1 |
<?php |
| 2 |
|
| 3 |
class HTMLPurifier_HTMLModuleManager |
| 4 |
{ |
| 5 |
|
| 6 |
/** |
| 7 |
* @type HTMLPurifier_DoctypeRegistry |
| 8 |
*/ |
| 9 |
public $doctypes; |
| 10 |
|
| 11 |
/** |
| 12 |
* Instance of current doctype. |
| 13 |
* @type string |
| 14 |
*/ |
| 15 |
public $doctype; |
| 16 |
|
| 17 |
/** |
| 18 |
* @type HTMLPurifier_AttrTypes |
| 19 |
*/ |
| 20 |
public $attrTypes; |
| 21 |
|
| 22 |
/** |
| 23 |
* Active instances of modules for the specified doctype are |
| 24 |
* indexed, by name, in this array. |
| 25 |
* @type HTMLPurifier_HTMLModule[] |
| 26 |
*/ |
| 27 |
public $modules = array(); |
| 28 |
|
| 29 |
/** |
| 30 |
* Array of recognized HTMLPurifier_HTMLModule instances, |
| 31 |
* indexed by module's class name. This array is usually lazy loaded, but a |
| 32 |
* user can overload a module by pre-emptively registering it. |
| 33 |
* @type HTMLPurifier_HTMLModule[] |
| 34 |
*/ |
| 35 |
public $registeredModules = array(); |
| 36 |
|
| 37 |
/** |
| 38 |
* List of extra modules that were added by the user |
| 39 |
* using addModule(). These get unconditionally merged into the current doctype, whatever |
| 40 |
* it may be. |
| 41 |
* @type HTMLPurifier_HTMLModule[] |
| 42 |
*/ |
| 43 |
public $userModules = array(); |
| 44 |
|
| 45 |
/** |
| 46 |
* Associative array of element name to list of modules that have |
| 47 |
* definitions for the element; this array is dynamically filled. |
| 48 |
* @type array |
| 49 |
*/ |
| 50 |
public $elementLookup = array(); |
| 51 |
|
| 52 |
/** |
| 53 |
* List of prefixes we should use for registering small names. |
| 54 |
* @type array |
| 55 |
*/ |
| 56 |
public $prefixes = array('HTMLPurifier_HTMLModule_'); |
| 57 |
|
| 58 |
/** |
| 59 |
* @type HTMLPurifier_ContentSets |
| 60 |
*/ |
| 61 |
public $contentSets; |
| 62 |
|
| 63 |
/** |
| 64 |
* @type HTMLPurifier_AttrCollections |
| 65 |
*/ |
| 66 |
public $attrCollections; |
| 67 |
|
| 68 |
/** |
| 69 |
* If set to true, unsafe elements and attributes will be allowed. |
| 70 |
* @type bool |
| 71 |
*/ |
| 72 |
public $trusted = false; |
| 73 |
|
| 74 |
public function __construct() |
| 75 |
{ |
| 76 |
// editable internal objects |
| 77 |
$this->attrTypes = new HTMLPurifier_AttrTypes(); |
| 78 |
$this->doctypes = new HTMLPurifier_DoctypeRegistry(); |
| 79 |
|
| 80 |
// setup basic modules |
| 81 |
$common = array( |
| 82 |
'CommonAttributes', 'Text', 'Hypertext', 'List', |
| 83 |
'Presentation', 'Edit', 'Bdo', 'Tables', 'Image', |
| 84 |
'StyleAttribute', |
| 85 |
// Unsafe: |
| 86 |
'Scripting', 'Object', 'Forms', |
| 87 |
// Sorta legacy, but present in strict: |
| 88 |
'Name', |
| 89 |
); |
| 90 |
$transitional = array('Legacy', 'Target', 'Iframe'); |
| 91 |
$xml = array('XMLCommonAttributes'); |
| 92 |
$non_xml = array('NonXMLCommonAttributes'); |
| 93 |
|
| 94 |
// setup basic doctypes |
| 95 |
$this->doctypes->register( |
| 96 |
'HTML 4.01 Transitional', |
| 97 |
false, |
| 98 |
array_merge($common, $transitional, $non_xml), |
| 99 |
array('Tidy_Transitional', 'Tidy_Proprietary'), |
| 100 |
array(), |
| 101 |
'-//W3C//DTD HTML 4.01 Transitional//EN', |
| 102 |
'http://www.w3.org/TR/html4/loose.dtd' |
| 103 |
); |
| 104 |
|
| 105 |
$this->doctypes->register( |
| 106 |
'HTML 4.01 Strict', |
| 107 |
false, |
| 108 |
array_merge($common, $non_xml), |
| 109 |
array('Tidy_Strict', 'Tidy_Proprietary', 'Tidy_Name'), |
| 110 |
array(), |
| 111 |
'-//W3C//DTD HTML 4.01//EN', |
| 112 |
'http://www.w3.org/TR/html4/strict.dtd' |
| 113 |
); |
| 114 |
|
| 115 |
$this->doctypes->register( |
| 116 |
'XHTML 1.0 Transitional', |
| 117 |
true, |
| 118 |
array_merge($common, $transitional, $xml, $non_xml), |
| 119 |
array('Tidy_Transitional', 'Tidy_XHTML', 'Tidy_Proprietary', 'Tidy_Name'), |
| 120 |
array(), |
| 121 |
'-//W3C//DTD XHTML 1.0 Transitional//EN', |
| 122 |
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd' |
| 123 |
); |
| 124 |
|
| 125 |
$this->doctypes->register( |
| 126 |
'XHTML 1.0 Strict', |
| 127 |
true, |
| 128 |
array_merge($common, $xml, $non_xml), |
| 129 |
array('Tidy_Strict', 'Tidy_XHTML', 'Tidy_Strict', 'Tidy_Proprietary', 'Tidy_Name'), |
| 130 |
array(), |
| 131 |
'-//W3C//DTD XHTML 1.0 Strict//EN', |
| 132 |
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd' |
| 133 |
); |
| 134 |
|
| 135 |
$this->doctypes->register( |
| 136 |
'XHTML 1.1', |
| 137 |
true, |
| 138 |
// Iframe is a real XHTML 1.1 module, despite being |
| 139 |
// "transitional"! |
| 140 |
array_merge($common, $xml, array('Ruby', 'Iframe')), |
| 141 |
array('Tidy_Strict', 'Tidy_XHTML', 'Tidy_Proprietary', 'Tidy_Strict', 'Tidy_Name'), // Tidy_XHTML1_1 |
| 142 |
array(), |
| 143 |
'-//W3C//DTD XHTML 1.1//EN', |
| 144 |
'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd' |
| 145 |
); |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Registers a module to the recognized module list, useful for |
| 151 |
* overloading pre-existing modules. |
| 152 |
* @param $module Mixed: string module name, with or without |
| 153 |
* HTMLPurifier_HTMLModule prefix, or instance of |
| 154 |
* subclass of HTMLPurifier_HTMLModule. |
| 155 |
* @param $overload Boolean whether or not to overload previous modules. |
| 156 |
* If this is not set, and you do overload a module, |
| 157 |
* HTML Purifier will complain with a warning. |
| 158 |
* @note This function will not call autoload, you must instantiate |
| 159 |
* (and thus invoke) autoload outside the method. |
| 160 |
* @note If a string is passed as a module name, different variants |
| 161 |
* will be tested in this order: |
| 162 |
* - Check for HTMLPurifier_HTMLModule_$name |
| 163 |
* - Check all prefixes with $name in order they were added |
| 164 |
* - Check for literal object name |
| 165 |
* - Throw fatal error |
| 166 |
* If your object name collides with an internal class, specify |
| 167 |
* your module manually. All modules must have been included |
| 168 |
* externally: registerModule will not perform inclusions for you! |
| 169 |
*/ |
| 170 |
public function registerModule($module, $overload = false) |
| 171 |
{ |
| 172 |
if (is_string($module)) { |
| 173 |
// attempt to load the module |
| 174 |
$original_module = $module; |
| 175 |
$ok = false; |
| 176 |
foreach ($this->prefixes as $prefix) { |
| 177 |
$module = $prefix . $original_module; |
| 178 |
if (class_exists($module)) { |
| 179 |
$ok = true; |
| 180 |
break; |
| 181 |
} |
| 182 |
} |
| 183 |
if (!$ok) { |
| 184 |
$module = $original_module; |
| 185 |
if (!class_exists($module)) { |
| 186 |
throw new Exception($original_module . ' module does not exist'); |
| 187 |
} |
| 188 |
} |
| 189 |
$module = new $module(); |
| 190 |
} |
| 191 |
if (empty($module->name)) { |
| 192 |
trigger_error('Module instance of ' . get_class($module) . ' must have name'); |
| 193 |
return; |
| 194 |
} |
| 195 |
if (!$overload && isset($this->registeredModules[$module->name])) { |
| 196 |
trigger_error('Overloading ' . $module->name . ' without explicit overload parameter', E_USER_WARNING); |
| 197 |
} |
| 198 |
$this->registeredModules[$module->name] = $module; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Adds a module to the current doctype by first registering it, |
| 203 |
* and then tacking it on to the active doctype |
| 204 |
*/ |
| 205 |
public function addModule($module) |
| 206 |
{ |
| 207 |
$this->registerModule($module); |
| 208 |
if (is_object($module)) { |
| 209 |
$module = $module->name; |
| 210 |
} |
| 211 |
$this->userModules[] = $module; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Adds a class prefix that registerModule() will use to resolve a |
| 216 |
* string name to a concrete class |
| 217 |
*/ |
| 218 |
public function addPrefix($prefix) |
| 219 |
{ |
| 220 |
$this->prefixes[] = $prefix; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Performs processing on modules, after being called you may |
| 225 |
* use getElement() and getElements() |
| 226 |
* @param HTMLPurifier_Config $config |
| 227 |
*/ |
| 228 |
public function setup($config) |
| 229 |
{ |
| 230 |
$this->trusted = $config->get('HTML.Trusted'); |
| 231 |
|
| 232 |
// generate |
| 233 |
$this->doctype = $this->doctypes->make($config); |
| 234 |
$modules = $this->doctype->modules; |
| 235 |
|
| 236 |
// take out the default modules that aren't allowed |
| 237 |
$lookup = $config->get('HTML.AllowedModules'); |
| 238 |
$special_cases = $config->get('HTML.CoreModules'); |
| 239 |
|
| 240 |
if (is_array($lookup)) { |
| 241 |
foreach ($modules as $k => $m) { |
| 242 |
if (isset($special_cases[$m])) { |
| 243 |
continue; |
| 244 |
} |
| 245 |
if (!isset($lookup[$m])) { |
| 246 |
unset($modules[$k]); |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
// custom modules |
| 252 |
if ($config->get('HTML.Proprietary')) { |
| 253 |
$modules[] = 'Proprietary'; |
| 254 |
} |
| 255 |
if ($config->get('HTML.SafeObject')) { |
| 256 |
$modules[] = 'SafeObject'; |
| 257 |
} |
| 258 |
if ($config->get('HTML.SafeEmbed')) { |
| 259 |
$modules[] = 'SafeEmbed'; |
| 260 |
} |
| 261 |
if ($config->get('HTML.SafeScripting') !== array()) { |
| 262 |
$modules[] = 'SafeScripting'; |
| 263 |
} |
| 264 |
if ($config->get('HTML.Nofollow')) { |
| 265 |
$modules[] = 'Nofollow'; |
| 266 |
} |
| 267 |
if ($config->get('HTML.TargetBlank')) { |
| 268 |
$modules[] = 'TargetBlank'; |
| 269 |
} |
| 270 |
// NB: HTML.TargetNoreferrer and HTML.TargetNoopener must be AFTER HTML.TargetBlank |
| 271 |
// so that its post-attr-transform gets run afterwards. |
| 272 |
if ($config->get('HTML.TargetNoreferrer')) { |
| 273 |
$modules[] = 'TargetNoreferrer'; |
| 274 |
} |
| 275 |
if ($config->get('HTML.TargetNoopener')) { |
| 276 |
$modules[] = 'TargetNoopener'; |
| 277 |
} |
| 278 |
|
| 279 |
// merge in custom modules |
| 280 |
$modules = array_merge($modules, $this->userModules); |
| 281 |
|
| 282 |
foreach ($modules as $module) { |
| 283 |
$this->processModule($module); |
| 284 |
$this->modules[$module]->setup($config); |
| 285 |
} |
| 286 |
|
| 287 |
foreach ($this->doctype->tidyModules as $module) { |
| 288 |
$this->processModule($module); |
| 289 |
$this->modules[$module]->setup($config); |
| 290 |
} |
| 291 |
|
| 292 |
// prepare any injectors |
| 293 |
foreach ($this->modules as $module) { |
| 294 |
$n = array(); |
| 295 |
foreach ($module->info_injector as $injector) { |
| 296 |
if (!is_object($injector)) { |
| 297 |
$class = "HTMLPurifier_Injector_$injector"; |
| 298 |
$injector = new $class; |
| 299 |
} |
| 300 |
$n[$injector->name] = $injector; |
| 301 |
} |
| 302 |
$module->info_injector = $n; |
| 303 |
} |
| 304 |
|
| 305 |
// setup lookup table based on all valid modules |
| 306 |
foreach ($this->modules as $module) { |
| 307 |
foreach ($module->info as $name => $def) { |
| 308 |
if (!isset($this->elementLookup[$name])) { |
| 309 |
$this->elementLookup[$name] = array(); |
| 310 |
} |
| 311 |
$this->elementLookup[$name][] = $module->name; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
// note the different choice |
| 316 |
$this->contentSets = new HTMLPurifier_ContentSets( |
| 317 |
// content set assembly deals with all possible modules, |
| 318 |
// not just ones deemed to be "safe" |
| 319 |
$this->modules |
| 320 |
); |
| 321 |
$this->attrCollections = new HTMLPurifier_AttrCollections( |
| 322 |
$this->attrTypes, |
| 323 |
// there is no way to directly disable a global attribute, |
| 324 |
// but using AllowedAttributes or simply not including |
| 325 |
// the module in your custom doctype should be sufficient |
| 326 |
$this->modules |
| 327 |
); |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Takes a module and adds it to the active module collection, |
| 332 |
* registering it if necessary. |
| 333 |
*/ |
| 334 |
public function processModule($module) |
| 335 |
{ |
| 336 |
if (!isset($this->registeredModules[$module]) || is_object($module)) { |
| 337 |
$this->registerModule($module); |
| 338 |
} |
| 339 |
$this->modules[$module] = $this->registeredModules[$module]; |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Retrieves merged element definitions. |
| 344 |
* @return Array of HTMLPurifier_ElementDef |
| 345 |
*/ |
| 346 |
public function getElements() |
| 347 |
{ |
| 348 |
$elements = array(); |
| 349 |
foreach ($this->modules as $module) { |
| 350 |
if (!$this->trusted && !$module->safe) { |
| 351 |
continue; |
| 352 |
} |
| 353 |
foreach ($module->info as $name => $v) { |
| 354 |
if (isset($elements[$name])) { |
| 355 |
continue; |
| 356 |
} |
| 357 |
$elements[$name] = $this->getElement($name); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
// remove dud elements, this happens when an element that |
| 362 |
// appeared to be safe actually wasn't |
| 363 |
foreach ($elements as $n => $v) { |
| 364 |
if ($v === false) { |
| 365 |
unset($elements[$n]); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
return $elements; |
| 370 |
|
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Retrieves a single merged element definition |
| 375 |
* @param string $name Name of element |
| 376 |
* @param bool $trusted Boolean trusted overriding parameter: set to true |
| 377 |
* if you want the full version of an element |
| 378 |
* @return HTMLPurifier_ElementDef Merged HTMLPurifier_ElementDef |
| 379 |
* @note You may notice that modules are getting iterated over twice (once |
| 380 |
* in getElements() and once here). This |
| 381 |
* is because |
| 382 |
*/ |
| 383 |
public function getElement($name, $trusted = null) |
| 384 |
{ |
| 385 |
if (!isset($this->elementLookup[$name])) { |
| 386 |
return false; |
| 387 |
} |
| 388 |
|
| 389 |
// setup global state variables |
| 390 |
$def = false; |
| 391 |
if ($trusted === null) { |
| 392 |
$trusted = $this->trusted; |
| 393 |
} |
| 394 |
|
| 395 |
// iterate through each module that has registered itself to this |
| 396 |
// element |
| 397 |
foreach ($this->elementLookup[$name] as $module_name) { |
| 398 |
$module = $this->modules[$module_name]; |
| 399 |
|
| 400 |
// refuse to create/merge from a module that is deemed unsafe-- |
| 401 |
// pretend the module doesn't exist--when trusted mode is not on. |
| 402 |
if (!$trusted && !$module->safe) { |
| 403 |
continue; |
| 404 |
} |
| 405 |
|
| 406 |
// clone is used because, ideally speaking, the original |
| 407 |
// definition should not be modified. Usually, this will |
| 408 |
// make no difference, but for consistency's sake |
| 409 |
$new_def = clone $module->info[$name]; |
| 410 |
|
| 411 |
if (!$def && $new_def->standalone) { |
| 412 |
$def = $new_def; |
| 413 |
} elseif ($def) { |
| 414 |
// This will occur even if $new_def is standalone. In practice, |
| 415 |
// this will usually result in a full replacement. |
| 416 |
$def->mergeIn($new_def); |
| 417 |
} else { |
| 418 |
// :TODO: |
| 419 |
// non-standalone definitions that don't have a standalone |
| 420 |
// to merge into could be deferred to the end |
| 421 |
// HOWEVER, it is perfectly valid for a non-standalone |
| 422 |
// definition to lack a standalone definition, even |
| 423 |
// after all processing: this allows us to safely |
| 424 |
// specify extra attributes for elements that may not be |
| 425 |
// enabled all in one place. In particular, this might |
| 426 |
// be the case for trusted elements. WARNING: care must |
| 427 |
// be taken that the /extra/ definitions are all safe. |
| 428 |
continue; |
| 429 |
} |
| 430 |
|
| 431 |
// attribute value expansions |
| 432 |
$this->attrCollections->performInclusions($def->attr); |
| 433 |
$this->attrCollections->expandIdentifiers($def->attr, $this->attrTypes); |
| 434 |
|
| 435 |
// descendants_are_inline, for ChildDef_Chameleon |
| 436 |
if (is_string($def->content_model) && |
| 437 |
strpos($def->content_model, 'Inline') !== false) { |
| 438 |
if ($name != 'del' && $name != 'ins') { |
| 439 |
// this is for you, ins/del |
| 440 |
$def->descendants_are_inline = true; |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
$this->contentSets->generateChildDef($def, $module); |
| 445 |
} |
| 446 |
|
| 447 |
// This can occur if there is a blank definition, but no base to |
| 448 |
// mix it in with |
| 449 |
if (!$def) { |
| 450 |
return false; |
| 451 |
} |
| 452 |
|
| 453 |
// add information on required attributes |
| 454 |
foreach ($def->attr as $attr_name => $attr_def) { |
| 455 |
if ($attr_def->required) { |
| 456 |
$def->required_attr[] = $attr_name; |
| 457 |
} |
| 458 |
} |
| 459 |
return $def; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
// vim: et sw=4 sts=4 |