PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/htmlpurifier/library/HTMLPurifier/HTMLModuleManager.php

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