HTMLModuleManager.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. // merge in custom modules
  201. $modules = array_merge($modules, $this->userModules);
  202. foreach ($modules as $module) {
  203. $this->processModule($module);
  204. $this->modules[$module]->setup($config);
  205. }
  206. foreach ($this->doctype->tidyModules as $module) {
  207. $this->processModule($module);
  208. $this->modules[$module]->setup($config);
  209. }
  210. // prepare any injectors
  211. foreach ($this->modules as $module) {
  212. $n = array();
  213. foreach ($module->info_injector as $i => $injector) {
  214. if (!is_object($injector)) {
  215. $class = "HTMLPurifier_Injector_$injector";
  216. $injector = new $class;
  217. }
  218. $n[$injector->name] = $injector;
  219. }
  220. $module->info_injector = $n;
  221. }
  222. // setup lookup table based on all valid modules
  223. foreach ($this->modules as $module) {
  224. foreach ($module->info as $name => $def) {
  225. if (!isset($this->elementLookup[$name])) {
  226. $this->elementLookup[$name] = array();
  227. }
  228. $this->elementLookup[$name][] = $module->name;
  229. }
  230. }
  231. // note the different choice
  232. $this->contentSets = new HTMLPurifier_ContentSets(
  233. // content set assembly deals with all possible modules,
  234. // not just ones deemed to be "safe"
  235. $this->modules
  236. );
  237. $this->attrCollections = new HTMLPurifier_AttrCollections(
  238. $this->attrTypes,
  239. // there is no way to directly disable a global attribute,
  240. // but using AllowedAttributes or simply not including
  241. // the module in your custom doctype should be sufficient
  242. $this->modules
  243. );
  244. }
  245. /**
  246. * Takes a module and adds it to the active module collection,
  247. * registering it if necessary.
  248. */
  249. public function processModule($module) {
  250. if (!isset($this->registeredModules[$module]) || is_object($module)) {
  251. $this->registerModule($module);
  252. }
  253. $this->modules[$module] = $this->registeredModules[$module];
  254. }
  255. /**
  256. * Retrieves merged element definitions.
  257. * @return Array of HTMLPurifier_ElementDef
  258. */
  259. public function getElements() {
  260. $elements = array();
  261. foreach ($this->modules as $module) {
  262. if (!$this->trusted && !$module->safe) continue;
  263. foreach ($module->info as $name => $v) {
  264. if (isset($elements[$name])) continue;
  265. $elements[$name] = $this->getElement($name);
  266. }
  267. }
  268. // remove dud elements, this happens when an element that
  269. // appeared to be safe actually wasn't
  270. foreach ($elements as $n => $v) {
  271. if ($v === false) unset($elements[$n]);
  272. }
  273. return $elements;
  274. }
  275. /**
  276. * Retrieves a single merged element definition
  277. * @param $name Name of element
  278. * @param $trusted Boolean trusted overriding parameter: set to true
  279. * if you want the full version of an element
  280. * @return Merged HTMLPurifier_ElementDef
  281. * @note You may notice that modules are getting iterated over twice (once
  282. * in getElements() and once here). This
  283. * is because
  284. */
  285. public function getElement($name, $trusted = null) {
  286. if (!isset($this->elementLookup[$name])) {
  287. return false;
  288. }
  289. // setup global state variables
  290. $def = false;
  291. if ($trusted === null) $trusted = $this->trusted;
  292. // iterate through each module that has registered itself to this
  293. // element
  294. foreach($this->elementLookup[$name] as $module_name) {
  295. $module = $this->modules[$module_name];
  296. // refuse to create/merge from a module that is deemed unsafe--
  297. // pretend the module doesn't exist--when trusted mode is not on.
  298. if (!$trusted && !$module->safe) {
  299. continue;
  300. }
  301. // clone is used because, ideally speaking, the original
  302. // definition should not be modified. Usually, this will
  303. // make no difference, but for consistency's sake
  304. $new_def = clone $module->info[$name];
  305. if (!$def && $new_def->standalone) {
  306. $def = $new_def;
  307. } elseif ($def) {
  308. // This will occur even if $new_def is standalone. In practice,
  309. // this will usually result in a full replacement.
  310. $def->mergeIn($new_def);
  311. } else {
  312. // :TODO:
  313. // non-standalone definitions that don't have a standalone
  314. // to merge into could be deferred to the end
  315. continue;
  316. }
  317. // attribute value expansions
  318. $this->attrCollections->performInclusions($def->attr);
  319. $this->attrCollections->expandIdentifiers($def->attr, $this->attrTypes);
  320. // descendants_are_inline, for ChildDef_Chameleon
  321. if (is_string($def->content_model) &&
  322. strpos($def->content_model, 'Inline') !== false) {
  323. if ($name != 'del' && $name != 'ins') {
  324. // this is for you, ins/del
  325. $def->descendants_are_inline = true;
  326. }
  327. }
  328. $this->contentSets->generateChildDef($def, $module);
  329. }
  330. // This can occur if there is a blank definition, but no base to
  331. // mix it in with
  332. if (!$def) return false;
  333. // add information on required attributes
  334. foreach ($def->attr as $attr_name => $attr_def) {
  335. if ($attr_def->required) {
  336. $def->required_attr[] = $attr_name;
  337. }
  338. }
  339. return $def;
  340. }
  341. }
  342. // vim: et sw=4 sts=4