PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/sites/all/libraries/htmlpurifier/library/HTMLPurifier/HTMLModuleManager.php

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