You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

460 line
15KB

  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. trigger_error(
  166. $original_module . ' module does not exist',
  167. E_USER_ERROR
  168. );
  169. return;
  170. }
  171. }
  172. $module = new $module();
  173. }
  174. if (empty($module->name)) {
  175. trigger_error('Module instance of ' . get_class($module) . ' must have name');
  176. return;
  177. }
  178. if (!$overload && isset($this->registeredModules[$module->name])) {
  179. trigger_error('Overloading ' . $module->name . ' without explicit overload parameter', E_USER_WARNING);
  180. }
  181. $this->registeredModules[$module->name] = $module;
  182. }
  183. /**
  184. * Adds a module to the current doctype by first registering it,
  185. * and then tacking it on to the active doctype
  186. */
  187. public function addModule($module)
  188. {
  189. $this->registerModule($module);
  190. if (is_object($module)) {
  191. $module = $module->name;
  192. }
  193. $this->userModules[] = $module;
  194. }
  195. /**
  196. * Adds a class prefix that registerModule() will use to resolve a
  197. * string name to a concrete class
  198. */
  199. public function addPrefix($prefix)
  200. {
  201. $this->prefixes[] = $prefix;
  202. }
  203. /**
  204. * Performs processing on modules, after being called you may
  205. * use getElement() and getElements()
  206. * @param HTMLPurifier_Config $config
  207. */
  208. public function setup($config)
  209. {
  210. $this->trusted = $config->get('HTML.Trusted');
  211. // generate
  212. $this->doctype = $this->doctypes->make($config);
  213. $modules = $this->doctype->modules;
  214. // take out the default modules that aren't allowed
  215. $lookup = $config->get('HTML.AllowedModules');
  216. $special_cases = $config->get('HTML.CoreModules');
  217. if (is_array($lookup)) {
  218. foreach ($modules as $k => $m) {
  219. if (isset($special_cases[$m])) {
  220. continue;
  221. }
  222. if (!isset($lookup[$m])) {
  223. unset($modules[$k]);
  224. }
  225. }
  226. }
  227. // custom modules
  228. if ($config->get('HTML.Proprietary')) {
  229. $modules[] = 'Proprietary';
  230. }
  231. if ($config->get('HTML.SafeObject')) {
  232. $modules[] = 'SafeObject';
  233. }
  234. if ($config->get('HTML.SafeEmbed')) {
  235. $modules[] = 'SafeEmbed';
  236. }
  237. if ($config->get('HTML.SafeScripting') !== array()) {
  238. $modules[] = 'SafeScripting';
  239. }
  240. if ($config->get('HTML.Nofollow')) {
  241. $modules[] = 'Nofollow';
  242. }
  243. if ($config->get('HTML.TargetBlank')) {
  244. $modules[] = 'TargetBlank';
  245. }
  246. // merge in custom modules
  247. $modules = array_merge($modules, $this->userModules);
  248. foreach ($modules as $module) {
  249. $this->processModule($module);
  250. $this->modules[$module]->setup($config);
  251. }
  252. foreach ($this->doctype->tidyModules as $module) {
  253. $this->processModule($module);
  254. $this->modules[$module]->setup($config);
  255. }
  256. // prepare any injectors
  257. foreach ($this->modules as $module) {
  258. $n = array();
  259. foreach ($module->info_injector as $injector) {
  260. if (!is_object($injector)) {
  261. $class = "HTMLPurifier_Injector_$injector";
  262. $injector = new $class;
  263. }
  264. $n[$injector->name] = $injector;
  265. }
  266. $module->info_injector = $n;
  267. }
  268. // setup lookup table based on all valid modules
  269. foreach ($this->modules as $module) {
  270. foreach ($module->info as $name => $def) {
  271. if (!isset($this->elementLookup[$name])) {
  272. $this->elementLookup[$name] = array();
  273. }
  274. $this->elementLookup[$name][] = $module->name;
  275. }
  276. }
  277. // note the different choice
  278. $this->contentSets = new HTMLPurifier_ContentSets(
  279. // content set assembly deals with all possible modules,
  280. // not just ones deemed to be "safe"
  281. $this->modules
  282. );
  283. $this->attrCollections = new HTMLPurifier_AttrCollections(
  284. $this->attrTypes,
  285. // there is no way to directly disable a global attribute,
  286. // but using AllowedAttributes or simply not including
  287. // the module in your custom doctype should be sufficient
  288. $this->modules
  289. );
  290. }
  291. /**
  292. * Takes a module and adds it to the active module collection,
  293. * registering it if necessary.
  294. */
  295. public function processModule($module)
  296. {
  297. if (!isset($this->registeredModules[$module]) || is_object($module)) {
  298. $this->registerModule($module);
  299. }
  300. $this->modules[$module] = $this->registeredModules[$module];
  301. }
  302. /**
  303. * Retrieves merged element definitions.
  304. * @return Array of HTMLPurifier_ElementDef
  305. */
  306. public function getElements()
  307. {
  308. $elements = array();
  309. foreach ($this->modules as $module) {
  310. if (!$this->trusted && !$module->safe) {
  311. continue;
  312. }
  313. foreach ($module->info as $name => $v) {
  314. if (isset($elements[$name])) {
  315. continue;
  316. }
  317. $elements[$name] = $this->getElement($name);
  318. }
  319. }
  320. // remove dud elements, this happens when an element that
  321. // appeared to be safe actually wasn't
  322. foreach ($elements as $n => $v) {
  323. if ($v === false) {
  324. unset($elements[$n]);
  325. }
  326. }
  327. return $elements;
  328. }
  329. /**
  330. * Retrieves a single merged element definition
  331. * @param string $name Name of element
  332. * @param bool $trusted Boolean trusted overriding parameter: set to true
  333. * if you want the full version of an element
  334. * @return HTMLPurifier_ElementDef Merged HTMLPurifier_ElementDef
  335. * @note You may notice that modules are getting iterated over twice (once
  336. * in getElements() and once here). This
  337. * is because
  338. */
  339. public function getElement($name, $trusted = null)
  340. {
  341. if (!isset($this->elementLookup[$name])) {
  342. return false;
  343. }
  344. // setup global state variables
  345. $def = false;
  346. if ($trusted === null) {
  347. $trusted = $this->trusted;
  348. }
  349. // iterate through each module that has registered itself to this
  350. // element
  351. foreach ($this->elementLookup[$name] as $module_name) {
  352. $module = $this->modules[$module_name];
  353. // refuse to create/merge from a module that is deemed unsafe--
  354. // pretend the module doesn't exist--when trusted mode is not on.
  355. if (!$trusted && !$module->safe) {
  356. continue;
  357. }
  358. // clone is used because, ideally speaking, the original
  359. // definition should not be modified. Usually, this will
  360. // make no difference, but for consistency's sake
  361. $new_def = clone $module->info[$name];
  362. if (!$def && $new_def->standalone) {
  363. $def = $new_def;
  364. } elseif ($def) {
  365. // This will occur even if $new_def is standalone. In practice,
  366. // this will usually result in a full replacement.
  367. $def->mergeIn($new_def);
  368. } else {
  369. // :TODO:
  370. // non-standalone definitions that don't have a standalone
  371. // to merge into could be deferred to the end
  372. // HOWEVER, it is perfectly valid for a non-standalone
  373. // definition to lack a standalone definition, even
  374. // after all processing: this allows us to safely
  375. // specify extra attributes for elements that may not be
  376. // enabled all in one place. In particular, this might
  377. // be the case for trusted elements. WARNING: care must
  378. // be taken that the /extra/ definitions are all safe.
  379. continue;
  380. }
  381. // attribute value expansions
  382. $this->attrCollections->performInclusions($def->attr);
  383. $this->attrCollections->expandIdentifiers($def->attr, $this->attrTypes);
  384. // descendants_are_inline, for ChildDef_Chameleon
  385. if (is_string($def->content_model) &&
  386. strpos($def->content_model, 'Inline') !== false) {
  387. if ($name != 'del' && $name != 'ins') {
  388. // this is for you, ins/del
  389. $def->descendants_are_inline = true;
  390. }
  391. }
  392. $this->contentSets->generateChildDef($def, $module);
  393. }
  394. // This can occur if there is a blank definition, but no base to
  395. // mix it in with
  396. if (!$def) {
  397. return false;
  398. }
  399. // add information on required attributes
  400. foreach ($def->attr as $attr_name => $attr_def) {
  401. if ($attr_def->required) {
  402. $def->required_attr[] = $attr_name;
  403. }
  404. }
  405. return $def;
  406. }
  407. }
  408. // vim: et sw=4 sts=4