HTMLModuleManager.php 15 KB

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