PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/vjCommentPlugin/lib/tools/htmlpurifier/library/HTMLPurifier/HTMLModuleManager.php

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