3 class HTMLPurifier_HTMLModuleManager
7 * Instance of HTMLPurifier_DoctypeRegistry
12 * Instance of current doctype
17 * Instance of HTMLPurifier_AttrTypes
22 * Active instances of modules for the specified doctype are
23 * indexed, by name, in this array.
25 public $modules = array();
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();
35 * List of extra modules that were added by the user using addModule().
36 * These get unconditionally merged into the current doctype, whatever
39 public $userModules = array();
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
64 'CommonAttributes', 'Text', 'Hypertext', 'List',
65 'Presentation', 'Edit', 'Bdo', 'Tables', 'Image',
68 'Scripting', 'Object', 'Forms',
69 // Sorta legacy, but present in strict:
72 $transitional = array('Legacy', 'Target');
73 $xml = array('XMLCommonAttributes');
74 $non_xml = array('NonXMLCommonAttributes');
76 // setup basic doctypes
77 $this->doctypes
->register(
78 'HTML 4.01 Transitional', false,
79 array_merge($common, $transitional, $non_xml),
80 array('Tidy_Transitional', 'Tidy_Proprietary'),
82 '-//W3C//DTD HTML 4.01 Transitional//EN',
83 'http://www.w3.org/TR/html4/loose.dtd'
86 $this->doctypes
->register(
87 'HTML 4.01 Strict', false,
88 array_merge($common, $non_xml),
89 array('Tidy_Strict', 'Tidy_Proprietary', 'Tidy_Name'),
91 '-//W3C//DTD HTML 4.01//EN',
92 'http://www.w3.org/TR/html4/strict.dtd'
95 $this->doctypes
->register(
96 'XHTML 1.0 Transitional', true,
97 array_merge($common, $transitional, $xml, $non_xml),
98 array('Tidy_Transitional', 'Tidy_XHTML', 'Tidy_Proprietary', 'Tidy_Name'),
100 '-//W3C//DTD XHTML 1.0 Transitional//EN',
101 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
104 $this->doctypes
->register(
105 'XHTML 1.0 Strict', true,
106 array_merge($common, $xml, $non_xml),
107 array('Tidy_Strict', 'Tidy_XHTML', 'Tidy_Strict', 'Tidy_Proprietary', 'Tidy_Name'),
109 '-//W3C//DTD XHTML 1.0 Strict//EN',
110 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
113 $this->doctypes
->register(
115 array_merge($common, $xml, array('Ruby')),
116 array('Tidy_Strict', 'Tidy_XHTML', 'Tidy_Proprietary', 'Tidy_Strict', 'Tidy_Name'), // Tidy_XHTML1_1
118 '-//W3C//DTD XHTML 1.1//EN',
119 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'
125 * Registers a module to the recognized module list, useful for
126 * overloading pre-existing modules.
127 * @param $module Mixed: string module name, with or without
128 * HTMLPurifier_HTMLModule prefix, or instance of
129 * subclass of HTMLPurifier_HTMLModule.
130 * @param $overload Boolean whether or not to overload previous modules.
131 * If this is not set, and you do overload a module,
132 * HTML Purifier will complain with a warning.
133 * @note This function will not call autoload, you must instantiate
134 * (and thus invoke) autoload outside the method.
135 * @note If a string is passed as a module name, different variants
136 * will be tested in this order:
137 * - Check for HTMLPurifier_HTMLModule_$name
138 * - Check all prefixes with $name in order they were added
139 * - Check for literal object name
140 * - Throw fatal error
141 * If your object name collides with an internal class, specify
142 * your module manually. All modules must have been included
143 * externally: registerModule will not perform inclusions for you!
145 public function registerModule($module, $overload = false) {
146 if (is_string($module)) {
147 // attempt to load the module
148 $original_module = $module;
150 foreach ($this->prefixes
as $prefix) {
151 $module = $prefix . $original_module;
152 if (class_exists($module)) {
158 $module = $original_module;
159 if (!class_exists($module)) {
160 trigger_error($original_module . ' module does not exist',
165 $module = new $module();
167 if (empty($module->name
)) {
168 trigger_error('Module instance of ' . get_class($module) . ' must have name');
171 if (!$overload && isset($this->registeredModules
[$module->name
])) {
172 trigger_error('Overloading ' . $module->name
. ' without explicit overload parameter', E_USER_WARNING
);
174 $this->registeredModules
[$module->name
] = $module;
178 * Adds a module to the current doctype by first registering it,
179 * and then tacking it on to the active doctype
181 public function addModule($module) {
182 $this->registerModule($module);
183 if (is_object($module)) $module = $module->name
;
184 $this->userModules
[] = $module;
188 * Adds a class prefix that registerModule() will use to resolve a
189 * string name to a concrete class
191 public function addPrefix($prefix) {
192 $this->prefixes
[] = $prefix;
196 * Performs processing on modules, after being called you may
197 * use getElement() and getElements()
198 * @param $config Instance of HTMLPurifier_Config
200 public function setup($config) {
202 $this->trusted
= $config->get('HTML.Trusted');
205 $this->doctype
= $this->doctypes
->make($config);
206 $modules = $this->doctype
->modules
;
208 // take out the default modules that aren't allowed
209 $lookup = $config->get('HTML.AllowedModules');
210 $special_cases = $config->get('HTML.CoreModules');
212 if (is_array($lookup)) {
213 foreach ($modules as $k => $m) {
214 if (isset($special_cases[$m])) continue;
215 if (!isset($lookup[$m])) unset($modules[$k]);
220 if ($config->get('HTML.Proprietary')) {
221 $modules[] = 'Proprietary';
223 if ($config->get('HTML.SafeObject')) {
224 $modules[] = 'SafeObject';
226 if ($config->get('HTML.SafeEmbed')) {
227 $modules[] = 'SafeEmbed';
229 if ($config->get('HTML.Nofollow')) {
230 $modules[] = 'Nofollow';
232 if ($config->get('HTML.TargetBlank')) {
233 $modules[] = 'TargetBlank';
236 // merge in custom modules
237 $modules = array_merge($modules, $this->userModules
);
239 foreach ($modules as $module) {
240 $this->processModule($module);
241 $this->modules
[$module]->setup($config);
244 foreach ($this->doctype
->tidyModules
as $module) {
245 $this->processModule($module);
246 $this->modules
[$module]->setup($config);
249 // prepare any injectors
250 foreach ($this->modules
as $module) {
252 foreach ($module->info_injector
as $i => $injector) {
253 if (!is_object($injector)) {
254 $class = "HTMLPurifier_Injector_$injector";
255 $injector = new $class;
257 $n[$injector->name
] = $injector;
259 $module->info_injector
= $n;
262 // setup lookup table based on all valid modules
263 foreach ($this->modules
as $module) {
264 foreach ($module->info
as $name => $def) {
265 if (!isset($this->elementLookup
[$name])) {
266 $this->elementLookup
[$name] = array();
268 $this->elementLookup
[$name][] = $module->name
;
272 // note the different choice
273 $this->contentSets
= new HTMLPurifier_ContentSets(
274 // content set assembly deals with all possible modules,
275 // not just ones deemed to be "safe"
278 $this->attrCollections
= new HTMLPurifier_AttrCollections(
280 // there is no way to directly disable a global attribute,
281 // but using AllowedAttributes or simply not including
282 // the module in your custom doctype should be sufficient
288 * Takes a module and adds it to the active module collection,
289 * registering it if necessary.
291 public function processModule($module) {
292 if (!isset($this->registeredModules
[$module]) ||
is_object($module)) {
293 $this->registerModule($module);
295 $this->modules
[$module] = $this->registeredModules
[$module];
299 * Retrieves merged element definitions.
300 * @return Array of HTMLPurifier_ElementDef
302 public function getElements() {
305 foreach ($this->modules
as $module) {
306 if (!$this->trusted
&& !$module->safe
) continue;
307 foreach ($module->info
as $name => $v) {
308 if (isset($elements[$name])) continue;
309 $elements[$name] = $this->getElement($name);
313 // remove dud elements, this happens when an element that
314 // appeared to be safe actually wasn't
315 foreach ($elements as $n => $v) {
316 if ($v === false) unset($elements[$n]);
324 * Retrieves a single merged element definition
325 * @param $name Name of element
326 * @param $trusted Boolean trusted overriding parameter: set to true
327 * if you want the full version of an element
328 * @return Merged HTMLPurifier_ElementDef
329 * @note You may notice that modules are getting iterated over twice (once
330 * in getElements() and once here). This
333 public function getElement($name, $trusted = null) {
335 if (!isset($this->elementLookup
[$name])) {
339 // setup global state variables
341 if ($trusted === null) $trusted = $this->trusted
;
343 // iterate through each module that has registered itself to this
345 foreach($this->elementLookup
[$name] as $module_name) {
347 $module = $this->modules
[$module_name];
349 // refuse to create/merge from a module that is deemed unsafe--
350 // pretend the module doesn't exist--when trusted mode is not on.
351 if (!$trusted && !$module->safe
) {
355 // clone is used because, ideally speaking, the original
356 // definition should not be modified. Usually, this will
357 // make no difference, but for consistency's sake
358 $new_def = clone $module->info
[$name];
360 if (!$def && $new_def->standalone
) {
363 // This will occur even if $new_def is standalone. In practice,
364 // this will usually result in a full replacement.
365 $def->mergeIn($new_def);
368 // non-standalone definitions that don't have a standalone
369 // to merge into could be deferred to the end
373 // attribute value expansions
374 $this->attrCollections
->performInclusions($def->attr
);
375 $this->attrCollections
->expandIdentifiers($def->attr
, $this->attrTypes
);
377 // descendants_are_inline, for ChildDef_Chameleon
378 if (is_string($def->content_model
) &&
379 strpos($def->content_model
, 'Inline') !== false) {
380 if ($name != 'del' && $name != 'ins') {
381 // this is for you, ins/del
382 $def->descendants_are_inline
= true;
386 $this->contentSets
->generateChildDef($def, $module);
389 // This can occur if there is a blank definition, but no base to
391 if (!$def) return false;
393 // add information on required attributes
394 foreach ($def->attr
as $attr_name => $attr_def) {
395 if ($attr_def->required
) {
396 $def->required_attr
[] = $attr_name;
406 // vim: et sw=4 sts=4