PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/htmlpurifier/library/HTMLPurifier.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 250 lines | 107 code | 35 blank | 108 comment | 8 complexity | b7813da7b78393cef1ac5d7784965aec MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /*! @mainpage
  3. *
  4. * HTML Purifier is an HTML filter that will take an arbitrary snippet of
  5. * HTML and rigorously test, validate and filter it into a version that
  6. * is safe for output onto webpages. It achieves this by:
  7. *
  8. * -# Lexing (parsing into tokens) the document,
  9. * -# Executing various strategies on the tokens:
  10. * -# Removing all elements not in the whitelist,
  11. * -# Making the tokens well-formed,
  12. * -# Fixing the nesting of the nodes, and
  13. * -# Validating attributes of the nodes; and
  14. * -# Generating HTML from the purified tokens.
  15. *
  16. * However, most users will only need to interface with the HTMLPurifier
  17. * and HTMLPurifier_Config.
  18. */
  19. /*
  20. HTML Purifier 4.3.0 - Standards Compliant HTML Filtering
  21. Copyright (C) 2006-2008 Edward Z. Yang
  22. This library is free software; you can redistribute it and/or
  23. modify it under the terms of the GNU Lesser General Public
  24. License as published by the Free Software Foundation; either
  25. version 2.1 of the License, or (at your option) any later version.
  26. This library is distributed in the hope that it will be useful,
  27. but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. Lesser General Public License for more details.
  30. You should have received a copy of the GNU Lesser General Public
  31. License along with this library; if not, write to the Free Software
  32. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  33. */
  34. /**
  35. * Facade that coordinates HTML Purifier's subsystems in order to purify HTML.
  36. *
  37. * @note There are several points in which configuration can be specified
  38. * for HTML Purifier. The precedence of these (from lowest to
  39. * highest) is as follows:
  40. * -# Instance: new HTMLPurifier($config)
  41. * -# Invocation: purify($html, $config)
  42. * These configurations are entirely independent of each other and
  43. * are *not* merged (this behavior may change in the future).
  44. *
  45. * @todo We need an easier way to inject strategies using the configuration
  46. * object.
  47. */
  48. class HTMLPurifier
  49. {
  50. /** Version of HTML Purifier */
  51. public $version = '4.3.0';
  52. /** Constant with version of HTML Purifier */
  53. const VERSION = '4.3.0';
  54. /** Global configuration object */
  55. public $config;
  56. /** Array of extra HTMLPurifier_Filter objects to run on HTML, for backwards compatibility */
  57. private $filters = array();
  58. /** Single instance of HTML Purifier */
  59. private static $instance;
  60. protected $strategy, $generator;
  61. /**
  62. * Resultant HTMLPurifier_Context of last run purification. Is an array
  63. * of contexts if the last called method was purifyArray().
  64. */
  65. public $context;
  66. /**
  67. * Initializes the purifier.
  68. * @param $config Optional HTMLPurifier_Config object for all instances of
  69. * the purifier, if omitted, a default configuration is
  70. * supplied (which can be overridden on a per-use basis).
  71. * The parameter can also be any type that
  72. * HTMLPurifier_Config::create() supports.
  73. */
  74. public function __construct($config = null)
  75. {
  76. $this->config = HTMLPurifier_Config :: create($config);
  77. $this->strategy = new HTMLPurifier_Strategy_Core();
  78. }
  79. /**
  80. * Adds a filter to process the output. First come first serve
  81. * @param $filter HTMLPurifier_Filter object
  82. */
  83. public function addFilter($filter)
  84. {
  85. trigger_error('HTMLPurifier->addFilter() is deprecated, use configuration directives in the Filter namespace or Filter.Custom', E_USER_WARNING);
  86. $this->filters[] = $filter;
  87. }
  88. /**
  89. * Filters an HTML snippet/document to be XSS-free and standards-compliant.
  90. *
  91. * @param $html String of HTML to purify
  92. * @param $config HTMLPurifier_Config object for this operation, if omitted,
  93. * defaults to the config object specified during this
  94. * object's construction. The parameter can also be any type
  95. * that HTMLPurifier_Config::create() supports.
  96. * @return Purified HTML
  97. */
  98. public function purify($html, $config = null)
  99. {
  100. // :TODO: make the config merge in, instead of replace
  101. $config = $config ? HTMLPurifier_Config :: create($config) : $this->config;
  102. // implementation is partially environment dependant, partially
  103. // configuration dependant
  104. $lexer = HTMLPurifier_Lexer :: create($config);
  105. $context = new HTMLPurifier_Context();
  106. // setup HTML generator
  107. $this->generator = new HTMLPurifier_Generator($config, $context);
  108. $context->register('Generator', $this->generator);
  109. // set up global context variables
  110. if ($config->get('Core.CollectErrors'))
  111. {
  112. // may get moved out if other facilities use it
  113. $language_factory = HTMLPurifier_LanguageFactory :: instance();
  114. $language = $language_factory->create($config, $context);
  115. $context->register('Locale', $language);
  116. $error_collector = new HTMLPurifier_ErrorCollector($context);
  117. $context->register('ErrorCollector', $error_collector);
  118. }
  119. // setup id_accumulator context, necessary due to the fact that
  120. // AttrValidator can be called from many places
  121. $id_accumulator = HTMLPurifier_IDAccumulator :: build($config, $context);
  122. $context->register('IDAccumulator', $id_accumulator);
  123. $html = HTMLPurifier_Encoder :: convertToUTF8($html, $config, $context);
  124. // setup filters
  125. $filter_flags = $config->getBatch('Filter');
  126. $custom_filters = $filter_flags['Custom'];
  127. unset($filter_flags['Custom']);
  128. $filters = array();
  129. foreach ($filter_flags as $filter => $flag)
  130. {
  131. if (! $flag)
  132. continue;
  133. if (strpos($filter, '.') !== false)
  134. continue;
  135. $class = "HTMLPurifier_Filter_$filter";
  136. $filters[] = new $class();
  137. }
  138. foreach ($custom_filters as $filter)
  139. {
  140. // maybe "HTMLPurifier_Filter_$filter", but be consistent with AutoFormat
  141. $filters[] = $filter;
  142. }
  143. $filters = array_merge($filters, $this->filters);
  144. // maybe prepare(), but later
  145. for($i = 0, $filter_size = count($filters); $i < $filter_size; $i ++)
  146. {
  147. $html = $filters[$i]->preFilter($html, $config, $context);
  148. }
  149. // purified HTML
  150. $html = $this->generator->generateFromTokens(// list of tokens
  151. $this->strategy->execute(// list of un-purified tokens
  152. $lexer->tokenizeHTML(// un-purified HTML
  153. $html, $config, $context), $config, $context));
  154. for($i = $filter_size - 1; $i >= 0; $i --)
  155. {
  156. $html = $filters[$i]->postFilter($html, $config, $context);
  157. }
  158. $html = HTMLPurifier_Encoder :: convertFromUTF8($html, $config, $context);
  159. $this->context = & $context;
  160. return $html;
  161. }
  162. /**
  163. * Filters an array of HTML snippets
  164. * @param $config Optional HTMLPurifier_Config object for this operation.
  165. * See HTMLPurifier::purify() for more details.
  166. * @return Array of purified HTML
  167. */
  168. public function purifyArray($array_of_html, $config = null)
  169. {
  170. $context_array = array();
  171. foreach ($array_of_html as $key => $html)
  172. {
  173. $array_of_html[$key] = $this->purify($html, $config);
  174. $context_array[$key] = $this->context;
  175. }
  176. $this->context = $context_array;
  177. return $array_of_html;
  178. }
  179. /**
  180. * Singleton for enforcing just one HTML Purifier in your system
  181. * @param $prototype Optional prototype HTMLPurifier instance to
  182. * overload singleton with, or HTMLPurifier_Config
  183. * instance to configure the generated version with.
  184. */
  185. public static function instance($prototype = null)
  186. {
  187. if (! self :: $instance || $prototype)
  188. {
  189. if ($prototype instanceof HTMLPurifier)
  190. {
  191. self :: $instance = $prototype;
  192. }
  193. elseif ($prototype)
  194. {
  195. self :: $instance = new HTMLPurifier($prototype);
  196. }
  197. else
  198. {
  199. self :: $instance = new HTMLPurifier();
  200. }
  201. }
  202. return self :: $instance;
  203. }
  204. /**
  205. * @note Backwards compatibility, see instance()
  206. */
  207. public static function getInstance($prototype = null)
  208. {
  209. return HTMLPurifier :: instance($prototype);
  210. }
  211. }
  212. // vim: et sw=4 sts=4