Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

HTMLModuleManager.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. // NB: HTML.TargetNoreferrer must be AFTER HTML.TargetBlank
  247. // so that its post-attr-transform gets run afterwards.
  248. if ($config->get('HTML.TargetNoreferrer')) {
  249. $modules[] = 'TargetNoreferrer';
  250. }
  251. // merge in custom modules
  252. $modules = array_merge($modules, $this->userModules);
  253. foreach ($modules as $module) {
  254. $this->processModule($module);
  255. $this->modules[$module]->setup($config);
  256. }
  257. foreach ($this->doctype->tidyModules as $module) {
  258. $this->processModule($module);
  259. $this->modules[$module]->setup($config);
  260. }
  261. // prepare any injectors
  262. foreach ($this->modules as $module) {
  263. $n = array();
  264. foreach ($module->info_injector as $injector) {
  265. if (!is_object($injector)) {
  266. $class = "HTMLPurifier_Injector_$injector";
  267. $injector = new $class;
  268. }
  269. $n[$injector->name] = $injector;
  270. }
  271. $module->info_injector = $n;
  272. }
  273. // setup lookup table based on all valid modules
  274. foreach ($this->modules as $module) {
  275. foreach ($module->info as $name => $def) {
  276. if (!isset($this->elementLookup[$name])) {
  277. $this->elementLookup[$name] = array();
  278. }
  279. $this->elementLookup[$name][] = $module->name;
  280. }
  281. }
  282. // note the different choice
  283. $this->contentSets = new HTMLPurifier_ContentSets(
  284. // content set assembly deals with all possible modules,
  285. // not just ones deemed to be "safe"
  286. $this->modules
  287. );
  288. $this->attrCollections = new HTMLPurifier_AttrCollections(
  289. $this->attrTypes,
  290. // there is no way to directly disable a global attribute,
  291. // but using AllowedAttributes or simply not including
  292. // the module in your custom doctype should be sufficient
  293. $this->modules
  294. );
  295. }
  296. /**
  297. * Takes a module and adds it to the active module collection,
  298. * registering it if necessary.
  299. */
  300. public function processModule($module)
  301. {
  302. if (!isset($this->registeredModules[$module]) || is_object($module)) {
  303. $this->registerModule($module);
  304. }
  305. $this->modules[$module] = $this->registeredModules[$module];
  306. }
  307. /**
  308. * Retrieves merged element definitions.
  309. * @return Array of HTMLPurifier_ElementDef
  310. */
  311. public function getElements()
  312. {
  313. $elements = array();
  314. foreach ($this->modules as $module) {
  315. if (!$this->trusted && !$module->safe) {
  316. continue;
  317. }
  318. foreach ($module->info as $name => $v) {
  319. if (isset($elements[$name])) {
  320. continue;
  321. }
  322. $elements[$name] = $this->getElement($name);
  323. }
  324. }
  325. // remove dud elements, this happens when an element that
  326. // appeared to be safe actually wasn't
  327. foreach ($elements as $n => $v) {
  328. if ($v === false) {
  329. unset($elements[$n]);
  330. }
  331. }
  332. return $elements;
  333. }
  334. /**
  335. * Retrieves a single merged element definition
  336. * @param string $name Name of element
  337. * @param bool $trusted Boolean trusted overriding parameter: set to true
  338. * if you want the full version of an element
  339. * @return HTMLPurifier_ElementDef Merged HTMLPurifier_ElementDef
  340. * @note You may notice that modules are getting iterated over twice (once
  341. * in getElements() and once here). This
  342. * is because
  343. */
  344. public function getElement($name, $trusted = null)
  345. {
  346. if (!isset($this->elementLookup[$name])) {
  347. return false;
  348. }
  349. // setup global state variables
  350. $def = false;
  351. if ($trusted === null) {
  352. $trusted = $this->trusted;
  353. }
  354. // iterate through each module that has registered itself to this
  355. // element
  356. foreach ($this->elementLookup[$name] as $module_name) {
  357. $module = $this->modules[$module_name];
  358. // refuse to create/merge from a module that is deemed unsafe--
  359. // pretend the module doesn't exist--when trusted mode is not on.
  360. if (!$trusted && !$module->safe) {
  361. continue;
  362. }
  363. // clone is used because, ideally speaking, the original
  364. // definition should not be modified. Usually, this will
  365. // make no difference, but for consistency's sake
  366. $new_def = clone $module->info[$name];
  367. if (!$def && $new_def->standalone) {
  368. $def = $new_def;
  369. } elseif ($def) {
  370. // This will occur even if $new_def is standalone. In practice,
  371. // this will usually result in a full replacement.
  372. $def->mergeIn($new_def);
  373. } else {
  374. // :TODO:
  375. // non-standalone definitions that don't have a standalone
  376. // to merge into could be deferred to the end
  377. // HOWEVER, it is perfectly valid for a non-standalone
  378. // definition to lack a standalone definition, even
  379. // after all processing: this allows us to safely
  380. // specify extra attributes for elements that may not be
  381. // enabled all in one place. In particular, this might
  382. // be the case for trusted elements. WARNING: care must
  383. // be taken that the /extra/ definitions are all safe.
  384. continue;
  385. }
  386. // attribute value expansions
  387. $this->attrCollections->performInclusions($def->attr);
  388. $this->attrCollections->expandIdentifiers($def->attr, $this->attrTypes);
  389. // descendants_are_inline, for ChildDef_Chameleon
  390. if (is_string($def->content_model) &&
  391. strpos($def->content_model, 'Inline') !== false) {
  392. if ($name != 'del' && $name != 'ins') {
  393. // this is for you, ins/del
  394. $def->descendants_are_inline = true;
  395. }
  396. }
  397. $this->contentSets->generateChildDef($def, $module);
  398. }
  399. // This can occur if there is a blank definition, but no base to
  400. // mix it in with
  401. if (!$def) {
  402. return false;
  403. }
  404. // add information on required attributes
  405. foreach ($def->attr as $attr_name => $attr_def) {
  406. if ($attr_def->required) {
  407. $def->required_attr[] = $attr_name;
  408. }
  409. }
  410. return $def;
  411. }
  412. }
  413. // vim: et sw=4 sts=4