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