[3.1.1] Allow injectors to be specified by modules.
[htmlpurifier.git] / library / HTMLPurifier / HTMLModuleManager.php
blobd1620e370bbc9953e9e2a0cb87ab8e56b0fafd30
1 <?php
3 class HTMLPurifier_HTMLModuleManager
6 /**
7 * Instance of HTMLPurifier_DoctypeRegistry
8 */
9 public $doctypes;
11 /**
12 * Instance of current doctype
14 public $doctype;
16 /**
17 * Instance of HTMLPurifier_AttrTypes
19 public $attrTypes;
21 /**
22 * Active instances of modules for the specified doctype are
23 * indexed, by name, in this array.
25 public $modules = array();
27 /**
28 * Array of recognized HTMLPurifier_Module instances, indexed by
29 * module's class name. This array is usually lazy loaded, but a
30 * user can overload a module by pre-emptively registering it.
32 public $registeredModules = array();
34 /**
35 * List of extra modules that were added by the user using addModule().
36 * These get unconditionally merged into the current doctype, whatever
37 * it may be.
39 public $userModules = array();
41 /**
42 * Associative array of element name to list of modules that have
43 * definitions for the element; this array is dynamically filled.
45 public $elementLookup = array();
47 /** List of prefixes we should use for registering small names */
48 public $prefixes = array('HTMLPurifier_HTMLModule_');
50 public $contentSets; /**< Instance of HTMLPurifier_ContentSets */
51 public $attrCollections; /**< Instance of HTMLPurifier_AttrCollections */
53 /** If set to true, unsafe elements and attributes will be allowed */
54 public $trusted = false;
56 public function __construct() {
58 // editable internal objects
59 $this->attrTypes = new HTMLPurifier_AttrTypes();
60 $this->doctypes = new HTMLPurifier_DoctypeRegistry();
62 // setup basic modules
63 $common = array(
64 'CommonAttributes', 'Text', 'Hypertext', 'List',
65 'Presentation', 'Edit', 'Bdo', 'Tables', 'Image',
66 'StyleAttribute', 'Scripting', 'Object'
68 $transitional = array('Legacy', 'Target');
69 $xml = array('XMLCommonAttributes');
70 $non_xml = array('NonXMLCommonAttributes');
72 // setup basic doctypes
73 $this->doctypes->register(
74 'HTML 4.01 Transitional', false,
75 array_merge($common, $transitional, $non_xml),
76 array('Tidy_Transitional', 'Tidy_Proprietary'),
77 array(),
78 '-//W3C//DTD HTML 4.01 Transitional//EN',
79 'http://www.w3.org/TR/html4/loose.dtd'
82 $this->doctypes->register(
83 'HTML 4.01 Strict', false,
84 array_merge($common, $non_xml),
85 array('Tidy_Strict', 'Tidy_Proprietary'),
86 array(),
87 '-//W3C//DTD HTML 4.01//EN',
88 'http://www.w3.org/TR/html4/strict.dtd'
91 $this->doctypes->register(
92 'XHTML 1.0 Transitional', true,
93 array_merge($common, $transitional, $xml, $non_xml),
94 array('Tidy_Transitional', 'Tidy_XHTML', 'Tidy_Proprietary'),
95 array(),
96 '-//W3C//DTD XHTML 1.0 Transitional//EN',
97 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
100 $this->doctypes->register(
101 'XHTML 1.0 Strict', true,
102 array_merge($common, $xml, $non_xml),
103 array('Tidy_Strict', 'Tidy_XHTML', 'Tidy_Strict', 'Tidy_Proprietary'),
104 array(),
105 '-//W3C//DTD XHTML 1.0 Strict//EN',
106 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
109 $this->doctypes->register(
110 'XHTML 1.1', true,
111 array_merge($common, $xml, array('Ruby')),
112 array('Tidy_Strict', 'Tidy_XHTML', 'Tidy_Proprietary', 'Tidy_Strict'), // Tidy_XHTML1_1
113 array(),
114 '-//W3C//DTD XHTML 1.1//EN',
115 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'
121 * Registers a module to the recognized module list, useful for
122 * overloading pre-existing modules.
123 * @param $module Mixed: string module name, with or without
124 * HTMLPurifier_HTMLModule prefix, or instance of
125 * subclass of HTMLPurifier_HTMLModule.
126 * @param $overload Boolean whether or not to overload previous modules.
127 * If this is not set, and you do overload a module,
128 * HTML Purifier will complain with a warning.
129 * @note This function will not call autoload, you must instantiate
130 * (and thus invoke) autoload outside the method.
131 * @note If a string is passed as a module name, different variants
132 * will be tested in this order:
133 * - Check for HTMLPurifier_HTMLModule_$name
134 * - Check all prefixes with $name in order they were added
135 * - Check for literal object name
136 * - Throw fatal error
137 * If your object name collides with an internal class, specify
138 * your module manually. All modules must have been included
139 * externally: registerModule will not perform inclusions for you!
141 public function registerModule($module, $overload = false) {
142 if (is_string($module)) {
143 // attempt to load the module
144 $original_module = $module;
145 $ok = false;
146 foreach ($this->prefixes as $prefix) {
147 $module = $prefix . $original_module;
148 if (class_exists($module)) {
149 $ok = true;
150 break;
153 if (!$ok) {
154 $module = $original_module;
155 if (!class_exists($module)) {
156 trigger_error($original_module . ' module does not exist',
157 E_USER_ERROR);
158 return;
161 $module = new $module();
163 if (empty($module->name)) {
164 trigger_error('Module instance of ' . get_class($module) . ' must have name');
165 return;
167 if (!$overload && isset($this->registeredModules[$module->name])) {
168 trigger_error('Overloading ' . $module->name . ' without explicit overload parameter', E_USER_WARNING);
170 $this->registeredModules[$module->name] = $module;
174 * Adds a module to the current doctype by first registering it,
175 * and then tacking it on to the active doctype
177 public function addModule($module) {
178 $this->registerModule($module);
179 if (is_object($module)) $module = $module->name;
180 $this->userModules[] = $module;
184 * Adds a class prefix that registerModule() will use to resolve a
185 * string name to a concrete class
187 public function addPrefix($prefix) {
188 $this->prefixes[] = $prefix;
192 * Performs processing on modules, after being called you may
193 * use getElement() and getElements()
194 * @param $config Instance of HTMLPurifier_Config
196 public function setup($config) {
198 $this->trusted = $config->get('HTML', 'Trusted');
200 // generate
201 $this->doctype = $this->doctypes->make($config);
202 $modules = $this->doctype->modules;
204 // take out the default modules that aren't allowed
205 $lookup = $config->get('HTML', 'AllowedModules');
206 $special_cases = $config->get('HTML', 'CoreModules');
208 if (is_array($lookup)) {
209 foreach ($modules as $k => $m) {
210 if (isset($special_cases[$m])) continue;
211 if (!isset($lookup[$m])) unset($modules[$k]);
215 // merge in custom modules
216 $modules = array_merge($modules, $this->userModules);
218 // add proprietary module (this gets special treatment because
219 // it is completely removed from doctypes, etc.)
220 if ($config->get('HTML', 'Proprietary')) {
221 $modules[] = 'Proprietary';
224 foreach ($modules as $module) {
225 $this->processModule($module);
226 $this->modules[$module]->setup($config);
229 foreach ($this->doctype->tidyModules as $module) {
230 $this->processModule($module);
231 $this->modules[$module]->setup($config);
234 // prepare any injectors
235 foreach ($this->modules as $module) {
236 $n = array();
237 foreach ($module->info_injector as $i => $injector) {
238 if (!is_object($injector)) {
239 $class = "HTMLPurifier_Injector_$injector";
240 $injector = new $class;
242 $n[$injector->name] = $injector;
244 $module->info_injector = $n;
247 // setup lookup table based on all valid modules
248 foreach ($this->modules as $module) {
249 foreach ($module->info as $name => $def) {
250 if (!isset($this->elementLookup[$name])) {
251 $this->elementLookup[$name] = array();
253 $this->elementLookup[$name][] = $module->name;
257 // note the different choice
258 $this->contentSets = new HTMLPurifier_ContentSets(
259 // content set assembly deals with all possible modules,
260 // not just ones deemed to be "safe"
261 $this->modules
263 $this->attrCollections = new HTMLPurifier_AttrCollections(
264 $this->attrTypes,
265 // there is no way to directly disable a global attribute,
266 // but using AllowedAttributes or simply not including
267 // the module in your custom doctype should be sufficient
268 $this->modules
273 * Takes a module and adds it to the active module collection,
274 * registering it if necessary.
276 public function processModule($module) {
277 if (!isset($this->registeredModules[$module]) || is_object($module)) {
278 $this->registerModule($module);
280 $this->modules[$module] = $this->registeredModules[$module];
284 * Retrieves merged element definitions.
285 * @return Array of HTMLPurifier_ElementDef
287 public function getElements() {
289 $elements = array();
290 foreach ($this->modules as $module) {
291 if (!$this->trusted && !$module->safe) continue;
292 foreach ($module->info as $name => $v) {
293 if (isset($elements[$name])) continue;
294 $elements[$name] = $this->getElement($name);
298 // remove dud elements, this happens when an element that
299 // appeared to be safe actually wasn't
300 foreach ($elements as $n => $v) {
301 if ($v === false) unset($elements[$n]);
304 return $elements;
309 * Retrieves a single merged element definition
310 * @param $name Name of element
311 * @param $trusted Boolean trusted overriding parameter: set to true
312 * if you want the full version of an element
313 * @return Merged HTMLPurifier_ElementDef
314 * @note You may notice that modules are getting iterated over twice (once
315 * in getElements() and once here). This
316 * is because
318 public function getElement($name, $trusted = null) {
320 if (!isset($this->elementLookup[$name])) {
321 return false;
324 // setup global state variables
325 $def = false;
326 if ($trusted === null) $trusted = $this->trusted;
328 // iterate through each module that has registered itself to this
329 // element
330 foreach($this->elementLookup[$name] as $module_name) {
332 $module = $this->modules[$module_name];
334 // refuse to create/merge from a module that is deemed unsafe--
335 // pretend the module doesn't exist--when trusted mode is not on.
336 if (!$trusted && !$module->safe) {
337 continue;
340 // clone is used because, ideally speaking, the original
341 // definition should not be modified. Usually, this will
342 // make no difference, but for consistency's sake
343 $new_def = clone $module->info[$name];
345 if (!$def && $new_def->standalone) {
346 $def = $new_def;
347 } elseif ($def) {
348 // This will occur even if $new_def is standalone. In practice,
349 // this will usually result in a full replacement.
350 $def->mergeIn($new_def);
351 } else {
352 // :TODO:
353 // non-standalone definitions that don't have a standalone
354 // to merge into could be deferred to the end
355 continue;
358 // attribute value expansions
359 $this->attrCollections->performInclusions($def->attr);
360 $this->attrCollections->expandIdentifiers($def->attr, $this->attrTypes);
362 // descendants_are_inline, for ChildDef_Chameleon
363 if (is_string($def->content_model) &&
364 strpos($def->content_model, 'Inline') !== false) {
365 if ($name != 'del' && $name != 'ins') {
366 // this is for you, ins/del
367 $def->descendants_are_inline = true;
371 $this->contentSets->generateChildDef($def, $module);
374 // add information on required attributes
375 foreach ($def->attr as $attr_name => $attr_def) {
376 if ($attr_def->required) {
377 $def->required_attr[] = $attr_name;
381 return $def;