Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.

237 lines
8.6 KiB

  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.1.1 - 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.1.1';
  52. /** Constant with version of HTML Purifier */
  53. const VERSION = '4.1.1';
  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. $this->config = HTMLPurifier_Config::create($config);
  76. $this->strategy = new HTMLPurifier_Strategy_Core();
  77. }
  78. /**
  79. * Adds a filter to process the output. First come first serve
  80. * @param $filter HTMLPurifier_Filter object
  81. */
  82. public function addFilter($filter) {
  83. trigger_error('HTMLPurifier->addFilter() is deprecated, use configuration directives in the Filter namespace or Filter.Custom', E_USER_WARNING);
  84. $this->filters[] = $filter;
  85. }
  86. /**
  87. * Filters an HTML snippet/document to be XSS-free and standards-compliant.
  88. *
  89. * @param $html String of HTML to purify
  90. * @param $config HTMLPurifier_Config object for this operation, if omitted,
  91. * defaults to the config object specified during this
  92. * object's construction. The parameter can also be any type
  93. * that HTMLPurifier_Config::create() supports.
  94. * @return Purified HTML
  95. */
  96. public function purify($html, $config = null) {
  97. // :TODO: make the config merge in, instead of replace
  98. $config = $config ? HTMLPurifier_Config::create($config) : $this->config;
  99. // implementation is partially environment dependant, partially
  100. // configuration dependant
  101. $lexer = HTMLPurifier_Lexer::create($config);
  102. $context = new HTMLPurifier_Context();
  103. // setup HTML generator
  104. $this->generator = new HTMLPurifier_Generator($config, $context);
  105. $context->register('Generator', $this->generator);
  106. // set up global context variables
  107. if ($config->get('Core.CollectErrors')) {
  108. // may get moved out if other facilities use it
  109. $language_factory = HTMLPurifier_LanguageFactory::instance();
  110. $language = $language_factory->create($config, $context);
  111. $context->register('Locale', $language);
  112. $error_collector = new HTMLPurifier_ErrorCollector($context);
  113. $context->register('ErrorCollector', $error_collector);
  114. }
  115. // setup id_accumulator context, necessary due to the fact that
  116. // AttrValidator can be called from many places
  117. $id_accumulator = HTMLPurifier_IDAccumulator::build($config, $context);
  118. $context->register('IDAccumulator', $id_accumulator);
  119. $html = HTMLPurifier_Encoder::convertToUTF8($html, $config, $context);
  120. // setup filters
  121. $filter_flags = $config->getBatch('Filter');
  122. $custom_filters = $filter_flags['Custom'];
  123. unset($filter_flags['Custom']);
  124. $filters = array();
  125. foreach ($filter_flags as $filter => $flag) {
  126. if (!$flag) continue;
  127. if (strpos($filter, '.') !== false) continue;
  128. $class = "HTMLPurifier_Filter_$filter";
  129. $filters[] = new $class;
  130. }
  131. foreach ($custom_filters as $filter) {
  132. // maybe "HTMLPurifier_Filter_$filter", but be consistent with AutoFormat
  133. $filters[] = $filter;
  134. }
  135. $filters = array_merge($filters, $this->filters);
  136. // maybe prepare(), but later
  137. for ($i = 0, $filter_size = count($filters); $i < $filter_size; $i++) {
  138. $html = $filters[$i]->preFilter($html, $config, $context);
  139. }
  140. // purified HTML
  141. $html =
  142. $this->generator->generateFromTokens(
  143. // list of tokens
  144. $this->strategy->execute(
  145. // list of un-purified tokens
  146. $lexer->tokenizeHTML(
  147. // un-purified HTML
  148. $html, $config, $context
  149. ),
  150. $config, $context
  151. )
  152. );
  153. for ($i = $filter_size - 1; $i >= 0; $i--) {
  154. $html = $filters[$i]->postFilter($html, $config, $context);
  155. }
  156. $html = HTMLPurifier_Encoder::convertFromUTF8($html, $config, $context);
  157. $this->context =& $context;
  158. return $html;
  159. }
  160. /**
  161. * Filters an array of HTML snippets
  162. * @param $config Optional HTMLPurifier_Config object for this operation.
  163. * See HTMLPurifier::purify() for more details.
  164. * @return Array of purified HTML
  165. */
  166. public function purifyArray($array_of_html, $config = null) {
  167. $context_array = array();
  168. foreach ($array_of_html as $key => $html) {
  169. $array_of_html[$key] = $this->purify($html, $config);
  170. $context_array[$key] = $this->context;
  171. }
  172. $this->context = $context_array;
  173. return $array_of_html;
  174. }
  175. /**
  176. * Singleton for enforcing just one HTML Purifier in your system
  177. * @param $prototype Optional prototype HTMLPurifier instance to
  178. * overload singleton with, or HTMLPurifier_Config
  179. * instance to configure the generated version with.
  180. */
  181. public static function instance($prototype = null) {
  182. if (!self::$instance || $prototype) {
  183. if ($prototype instanceof HTMLPurifier) {
  184. self::$instance = $prototype;
  185. } elseif ($prototype) {
  186. self::$instance = new HTMLPurifier($prototype);
  187. } else {
  188. self::$instance = new HTMLPurifier();
  189. }
  190. }
  191. return self::$instance;
  192. }
  193. /**
  194. * @note Backwards compatibility, see instance()
  195. */
  196. public static function getInstance($prototype = null) {
  197. return HTMLPurifier::instance($prototype);
  198. }
  199. }
  200. // vim: et sw=4 sts=4