friendica/vendor/ezyang/htmlpurifier/library/HTMLPurifier/HTMLModuleManager.php

460 lines
15 KiB
PHP
Raw Normal View History

2010-09-09 03:14:17 +00:00
<?php
class HTMLPurifier_HTMLModuleManager
{
/**
2016-02-09 10:06:17 +00:00
* @type HTMLPurifier_DoctypeRegistry
2010-09-09 03:14:17 +00:00
*/
public $doctypes;
/**
2016-02-09 10:06:17 +00:00
* Instance of current doctype.
* @type string
2010-09-09 03:14:17 +00:00
*/
public $doctype;
/**
2016-02-09 10:06:17 +00:00
* @type HTMLPurifier_AttrTypes
2010-09-09 03:14:17 +00:00
*/
public $attrTypes;
/**
* Active instances of modules for the specified doctype are
* indexed, by name, in this array.
2016-02-09 10:06:17 +00:00
* @type HTMLPurifier_HTMLModule[]
2010-09-09 03:14:17 +00:00
*/
public $modules = array();
/**
2016-02-09 10:06:17 +00:00
* Array of recognized HTMLPurifier_HTMLModule instances,
* indexed by module's class name. This array is usually lazy loaded, but a
2010-09-09 03:14:17 +00:00
* user can overload a module by pre-emptively registering it.
2016-02-09 10:06:17 +00:00
* @type HTMLPurifier_HTMLModule[]
2010-09-09 03:14:17 +00:00
*/
public $registeredModules = array();
/**
2016-02-09 10:06:17 +00:00
* List of extra modules that were added by the user
* using addModule(). These get unconditionally merged into the current doctype, whatever
2010-09-09 03:14:17 +00:00
* it may be.
2016-02-09 10:06:17 +00:00
* @type HTMLPurifier_HTMLModule[]
2010-09-09 03:14:17 +00:00
*/
public $userModules = array();
/**
* Associative array of element name to list of modules that have
* definitions for the element; this array is dynamically filled.
2016-02-09 10:06:17 +00:00
* @type array
2010-09-09 03:14:17 +00:00
*/
public $elementLookup = array();
2016-02-09 10:06:17 +00:00
/**
* List of prefixes we should use for registering small names.
* @type array
*/
2010-09-09 03:14:17 +00:00
public $prefixes = array('HTMLPurifier_HTMLModule_');
2016-02-09 10:06:17 +00:00
/**
* @type HTMLPurifier_ContentSets
*/
public $contentSets;
2010-09-09 03:14:17 +00:00
2016-02-09 10:06:17 +00:00
/**
* @type HTMLPurifier_AttrCollections
*/
public $attrCollections;
2010-09-09 03:14:17 +00:00
2016-02-09 10:06:17 +00:00
/**
* If set to true, unsafe elements and attributes will be allowed.
* @type bool
*/
public $trusted = false;
2010-09-09 03:14:17 +00:00
2016-02-09 10:06:17 +00:00
public function __construct()
{
2010-09-09 03:14:17 +00:00
// editable internal objects
$this->attrTypes = new HTMLPurifier_AttrTypes();
$this->doctypes = new HTMLPurifier_DoctypeRegistry();
// setup basic modules
$common = array(
'CommonAttributes', 'Text', 'Hypertext', 'List',
'Presentation', 'Edit', 'Bdo', 'Tables', 'Image',
'StyleAttribute',
// Unsafe:
2016-02-09 10:06:17 +00:00
'Scripting', 'Object', 'Forms',
2010-09-09 03:14:17 +00:00
// Sorta legacy, but present in strict:
'Name',
);
2016-02-09 10:06:17 +00:00
$transitional = array('Legacy', 'Target', 'Iframe');
2010-09-09 03:14:17 +00:00
$xml = array('XMLCommonAttributes');
$non_xml = array('NonXMLCommonAttributes');
// setup basic doctypes
$this->doctypes->register(
2016-02-09 10:06:17 +00:00
'HTML 4.01 Transitional',
false,
2010-09-09 03:14:17 +00:00
array_merge($common, $transitional, $non_xml),
array('Tidy_Transitional', 'Tidy_Proprietary'),
array(),
'-//W3C//DTD HTML 4.01 Transitional//EN',
'http://www.w3.org/TR/html4/loose.dtd'
);
$this->doctypes->register(
2016-02-09 10:06:17 +00:00
'HTML 4.01 Strict',
false,
2010-09-09 03:14:17 +00:00
array_merge($common, $non_xml),
array('Tidy_Strict', 'Tidy_Proprietary', 'Tidy_Name'),
array(),
'-//W3C//DTD HTML 4.01//EN',
'http://www.w3.org/TR/html4/strict.dtd'
);
$this->doctypes->register(
2016-02-09 10:06:17 +00:00
'XHTML 1.0 Transitional',
true,
2010-09-09 03:14:17 +00:00
array_merge($common, $transitional, $xml, $non_xml),
array('Tidy_Transitional', 'Tidy_XHTML', 'Tidy_Proprietary', 'Tidy_Name'),
array(),
'-//W3C//DTD XHTML 1.0 Transitional//EN',
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
);
$this->doctypes->register(
2016-02-09 10:06:17 +00:00
'XHTML 1.0 Strict',
true,
2010-09-09 03:14:17 +00:00
array_merge($common, $xml, $non_xml),
array('Tidy_Strict', 'Tidy_XHTML', 'Tidy_Strict', 'Tidy_Proprietary', 'Tidy_Name'),
array(),
'-//W3C//DTD XHTML 1.0 Strict//EN',
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
);
$this->doctypes->register(
2016-02-09 10:06:17 +00:00
'XHTML 1.1',
true,
// Iframe is a real XHTML 1.1 module, despite being
// "transitional"!
array_merge($common, $xml, array('Ruby', 'Iframe')),
2010-09-09 03:14:17 +00:00
array('Tidy_Strict', 'Tidy_XHTML', 'Tidy_Proprietary', 'Tidy_Strict', 'Tidy_Name'), // Tidy_XHTML1_1
array(),
'-//W3C//DTD XHTML 1.1//EN',
'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'
);
}
/**
* Registers a module to the recognized module list, useful for
* overloading pre-existing modules.
* @param $module Mixed: string module name, with or without
* HTMLPurifier_HTMLModule prefix, or instance of
* subclass of HTMLPurifier_HTMLModule.
* @param $overload Boolean whether or not to overload previous modules.
* If this is not set, and you do overload a module,
* HTML Purifier will complain with a warning.
* @note This function will not call autoload, you must instantiate
* (and thus invoke) autoload outside the method.
* @note If a string is passed as a module name, different variants
* will be tested in this order:
* - Check for HTMLPurifier_HTMLModule_$name
* - Check all prefixes with $name in order they were added
* - Check for literal object name
* - Throw fatal error
* If your object name collides with an internal class, specify
* your module manually. All modules must have been included
* externally: registerModule will not perform inclusions for you!
*/
2016-02-09 10:06:17 +00:00
public function registerModule($module, $overload = false)
{
2010-09-09 03:14:17 +00:00
if (is_string($module)) {
// attempt to load the module
$original_module = $module;
$ok = false;
foreach ($this->prefixes as $prefix) {
$module = $prefix . $original_module;
if (class_exists($module)) {
$ok = true;
break;
}
}
if (!$ok) {
$module = $original_module;
if (!class_exists($module)) {
2016-02-09 10:06:17 +00:00
trigger_error(
$original_module . ' module does not exist',
E_USER_ERROR
);
2010-09-09 03:14:17 +00:00
return;
}
}
$module = new $module();
}
if (empty($module->name)) {
trigger_error('Module instance of ' . get_class($module) . ' must have name');
return;
}
if (!$overload && isset($this->registeredModules[$module->name])) {
trigger_error('Overloading ' . $module->name . ' without explicit overload parameter', E_USER_WARNING);
}
$this->registeredModules[$module->name] = $module;
}
/**
* Adds a module to the current doctype by first registering it,
* and then tacking it on to the active doctype
*/
2016-02-09 10:06:17 +00:00
public function addModule($module)
{
2010-09-09 03:14:17 +00:00
$this->registerModule($module);
2016-02-09 10:06:17 +00:00
if (is_object($module)) {
$module = $module->name;
}
2010-09-09 03:14:17 +00:00
$this->userModules[] = $module;
}
/**
* Adds a class prefix that registerModule() will use to resolve a
* string name to a concrete class
*/
2016-02-09 10:06:17 +00:00
public function addPrefix($prefix)
{
2010-09-09 03:14:17 +00:00
$this->prefixes[] = $prefix;
}
/**
* Performs processing on modules, after being called you may
* use getElement() and getElements()
2016-02-09 10:06:17 +00:00
* @param HTMLPurifier_Config $config
2010-09-09 03:14:17 +00:00
*/
2016-02-09 10:06:17 +00:00
public function setup($config)
{
2010-09-09 03:14:17 +00:00
$this->trusted = $config->get('HTML.Trusted');
// generate
$this->doctype = $this->doctypes->make($config);
$modules = $this->doctype->modules;
// take out the default modules that aren't allowed
$lookup = $config->get('HTML.AllowedModules');
$special_cases = $config->get('HTML.CoreModules');
if (is_array($lookup)) {
foreach ($modules as $k => $m) {
2016-02-09 10:06:17 +00:00
if (isset($special_cases[$m])) {
continue;
}
if (!isset($lookup[$m])) {
unset($modules[$k]);
}
2010-09-09 03:14:17 +00:00
}
}
2016-02-09 10:06:17 +00:00
// custom modules
2010-09-09 03:14:17 +00:00
if ($config->get('HTML.Proprietary')) {
$modules[] = 'Proprietary';
}
if ($config->get('HTML.SafeObject')) {
$modules[] = 'SafeObject';
}
if ($config->get('HTML.SafeEmbed')) {
$modules[] = 'SafeEmbed';
}
2016-02-09 10:06:17 +00:00
if ($config->get('HTML.SafeScripting') !== array()) {
$modules[] = 'SafeScripting';
}
if ($config->get('HTML.Nofollow')) {
$modules[] = 'Nofollow';
}
if ($config->get('HTML.TargetBlank')) {
$modules[] = 'TargetBlank';
}
2010-09-09 03:14:17 +00:00
// merge in custom modules
$modules = array_merge($modules, $this->userModules);
foreach ($modules as $module) {
$this->processModule($module);
$this->modules[$module]->setup($config);
}
foreach ($this->doctype->tidyModules as $module) {
$this->processModule($module);
$this->modules[$module]->setup($config);
}
// prepare any injectors
foreach ($this->modules as $module) {
$n = array();
2016-02-09 10:06:17 +00:00
foreach ($module->info_injector as $injector) {
2010-09-09 03:14:17 +00:00
if (!is_object($injector)) {
$class = "HTMLPurifier_Injector_$injector";
$injector = new $class;
}
$n[$injector->name] = $injector;
}
$module->info_injector = $n;
}
// setup lookup table based on all valid modules
foreach ($this->modules as $module) {
foreach ($module->info as $name => $def) {
if (!isset($this->elementLookup[$name])) {
$this->elementLookup[$name] = array();
}
$this->elementLookup[$name][] = $module->name;
}
}
// note the different choice
$this->contentSets = new HTMLPurifier_ContentSets(
// content set assembly deals with all possible modules,
// not just ones deemed to be "safe"
$this->modules
);
$this->attrCollections = new HTMLPurifier_AttrCollections(
$this->attrTypes,
// there is no way to directly disable a global attribute,
// but using AllowedAttributes or simply not including
// the module in your custom doctype should be sufficient
$this->modules
);
}
/**
* Takes a module and adds it to the active module collection,
* registering it if necessary.
*/
2016-02-09 10:06:17 +00:00
public function processModule($module)
{
2010-09-09 03:14:17 +00:00
if (!isset($this->registeredModules[$module]) || is_object($module)) {
$this->registerModule($module);
}
$this->modules[$module] = $this->registeredModules[$module];
}
/**
* Retrieves merged element definitions.
* @return Array of HTMLPurifier_ElementDef
*/
2016-02-09 10:06:17 +00:00
public function getElements()
{
2010-09-09 03:14:17 +00:00
$elements = array();
foreach ($this->modules as $module) {
2016-02-09 10:06:17 +00:00
if (!$this->trusted && !$module->safe) {
continue;
}
2010-09-09 03:14:17 +00:00
foreach ($module->info as $name => $v) {
2016-02-09 10:06:17 +00:00
if (isset($elements[$name])) {
continue;
}
2010-09-09 03:14:17 +00:00
$elements[$name] = $this->getElement($name);
}
}
// remove dud elements, this happens when an element that
// appeared to be safe actually wasn't
foreach ($elements as $n => $v) {
2016-02-09 10:06:17 +00:00
if ($v === false) {
unset($elements[$n]);
}
2010-09-09 03:14:17 +00:00
}
return $elements;
}
/**
* Retrieves a single merged element definition
2016-02-09 10:06:17 +00:00
* @param string $name Name of element
* @param bool $trusted Boolean trusted overriding parameter: set to true
2010-09-09 03:14:17 +00:00
* if you want the full version of an element
2016-02-09 10:06:17 +00:00
* @return HTMLPurifier_ElementDef Merged HTMLPurifier_ElementDef
2010-09-09 03:14:17 +00:00
* @note You may notice that modules are getting iterated over twice (once
* in getElements() and once here). This
* is because
*/
2016-02-09 10:06:17 +00:00
public function getElement($name, $trusted = null)
{
2010-09-09 03:14:17 +00:00
if (!isset($this->elementLookup[$name])) {
return false;
}
// setup global state variables
$def = false;
2016-02-09 10:06:17 +00:00
if ($trusted === null) {
$trusted = $this->trusted;
}
2010-09-09 03:14:17 +00:00
// iterate through each module that has registered itself to this
// element
2016-02-09 10:06:17 +00:00
foreach ($this->elementLookup[$name] as $module_name) {
2010-09-09 03:14:17 +00:00
$module = $this->modules[$module_name];
// refuse to create/merge from a module that is deemed unsafe--
// pretend the module doesn't exist--when trusted mode is not on.
if (!$trusted && !$module->safe) {
continue;
}
// clone is used because, ideally speaking, the original
// definition should not be modified. Usually, this will
// make no difference, but for consistency's sake
$new_def = clone $module->info[$name];
if (!$def && $new_def->standalone) {
$def = $new_def;
} elseif ($def) {
// This will occur even if $new_def is standalone. In practice,
// this will usually result in a full replacement.
$def->mergeIn($new_def);
} else {
// :TODO:
// non-standalone definitions that don't have a standalone
// to merge into could be deferred to the end
2016-02-09 10:06:17 +00:00
// HOWEVER, it is perfectly valid for a non-standalone
// definition to lack a standalone definition, even
// after all processing: this allows us to safely
// specify extra attributes for elements that may not be
// enabled all in one place. In particular, this might
// be the case for trusted elements. WARNING: care must
// be taken that the /extra/ definitions are all safe.
2010-09-09 03:14:17 +00:00
continue;
}
// attribute value expansions
$this->attrCollections->performInclusions($def->attr);
$this->attrCollections->expandIdentifiers($def->attr, $this->attrTypes);
// descendants_are_inline, for ChildDef_Chameleon
if (is_string($def->content_model) &&
strpos($def->content_model, 'Inline') !== false) {
if ($name != 'del' && $name != 'ins') {
// this is for you, ins/del
$def->descendants_are_inline = true;
}
}
$this->contentSets->generateChildDef($def, $module);
}
// This can occur if there is a blank definition, but no base to
// mix it in with
2016-02-09 10:06:17 +00:00
if (!$def) {
return false;
}
2010-09-09 03:14:17 +00:00
// add information on required attributes
foreach ($def->attr as $attr_name => $attr_def) {
if ($attr_def->required) {
$def->required_attr[] = $attr_name;
}
}
return $def;
}
}
// vim: et sw=4 sts=4