Release 2.1.1.
[htmlpurifier.git] / library / HTMLPurifier.php
blobfa42e3bb7843a030a1d8508103ad600cc294c206
1 <?php
3 /*!
4 * @mainpage
5 *
6 * HTML Purifier is an HTML filter that will take an arbitrary snippet of
7 * HTML and rigorously test, validate and filter it into a version that
8 * is safe for output onto webpages. It achieves this by:
9 *
10 * -# Lexing (parsing into tokens) the document,
11 * -# Executing various strategies on the tokens:
12 * -# Removing all elements not in the whitelist,
13 * -# Making the tokens well-formed,
14 * -# Fixing the nesting of the nodes, and
15 * -# Validating attributes of the nodes; and
16 * -# Generating HTML from the purified tokens.
18 * However, most users will only need to interface with the HTMLPurifier
19 * class, so this massive amount of infrastructure is usually concealed.
20 * If you plan on working with the internals, be sure to include
21 * HTMLPurifier_ConfigSchema and HTMLPurifier_Config.
25 HTML Purifier 2.1.1 - Standards Compliant HTML Filtering
26 Copyright (C) 2006 Edward Z. Yang
28 This library is free software; you can redistribute it and/or
29 modify it under the terms of the GNU Lesser General Public
30 License as published by the Free Software Foundation; either
31 version 2.1 of the License, or (at your option) any later version.
33 This library is distributed in the hope that it will be useful,
34 but WITHOUT ANY WARRANTY; without even the implied warranty of
35 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 Lesser General Public License for more details.
38 You should have received a copy of the GNU Lesser General Public
39 License along with this library; if not, write to the Free Software
40 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
43 // constants are slow, but we'll make one exception
44 define('HTMLPURIFIER_PREFIX', dirname(__FILE__));
46 // almost every class has an undocumented dependency to these, so make sure
47 // they get included
48 require_once 'HTMLPurifier/ConfigSchema.php'; // important
49 require_once 'HTMLPurifier/Config.php';
50 require_once 'HTMLPurifier/Context.php';
52 require_once 'HTMLPurifier/Lexer.php';
53 require_once 'HTMLPurifier/Generator.php';
54 require_once 'HTMLPurifier/Strategy/Core.php';
55 require_once 'HTMLPurifier/Encoder.php';
57 require_once 'HTMLPurifier/ErrorCollector.php';
58 require_once 'HTMLPurifier/LanguageFactory.php';
60 HTMLPurifier_ConfigSchema::define(
61 'Core', 'CollectErrors', false, 'bool', '
62 Whether or not to collect errors found while filtering the document. This
63 is a useful way to give feedback to your users. CURRENTLY NOT IMPLEMENTED.
64 This directive has been available since 2.0.0.
65 ');
67 /**
68 * Main library execution class.
70 * Facade that performs calls to the HTMLPurifier_Lexer,
71 * HTMLPurifier_Strategy and HTMLPurifier_Generator subsystems in order to
72 * purify HTML.
74 * @todo We need an easier way to inject strategies, it'll probably end
75 * up getting done through config though.
77 class HTMLPurifier
80 var $version = '2.1.1';
82 var $config;
83 var $filters;
85 var $strategy, $generator;
87 /**
88 * Final HTMLPurifier_Context of last run purification. Might be an array.
89 * @public
91 var $context;
93 /**
94 * Initializes the purifier.
95 * @param $config Optional HTMLPurifier_Config object for all instances of
96 * the purifier, if omitted, a default configuration is
97 * supplied (which can be overridden on a per-use basis).
98 * The parameter can also be any type that
99 * HTMLPurifier_Config::create() supports.
101 function HTMLPurifier($config = null) {
103 $this->config = HTMLPurifier_Config::create($config);
105 $this->strategy = new HTMLPurifier_Strategy_Core();
106 $this->generator = new HTMLPurifier_Generator();
111 * Adds a filter to process the output. First come first serve
112 * @param $filter HTMLPurifier_Filter object
114 function addFilter($filter) {
115 $this->filters[] = $filter;
119 * Filters an HTML snippet/document to be XSS-free and standards-compliant.
121 * @param $html String of HTML to purify
122 * @param $config HTMLPurifier_Config object for this operation, if omitted,
123 * defaults to the config object specified during this
124 * object's construction. The parameter can also be any type
125 * that HTMLPurifier_Config::create() supports.
126 * @return Purified HTML
128 function purify($html, $config = null) {
130 $config = $config ? HTMLPurifier_Config::create($config) : $this->config;
132 // implementation is partially environment dependant, partially
133 // configuration dependant
134 $lexer = HTMLPurifier_Lexer::create($config);
136 $context = new HTMLPurifier_Context();
138 // our friendly neighborhood generator, all primed with configuration too!
139 $this->generator->generateFromTokens(array(), $config, $context);
140 $context->register('Generator', $this->generator);
142 // set up global context variables
143 if ($config->get('Core', 'CollectErrors')) {
144 // may get moved out if other facilities use it
145 $language_factory = HTMLPurifier_LanguageFactory::instance();
146 $language = $language_factory->create($config, $context);
147 $context->register('Locale', $language);
149 $error_collector = new HTMLPurifier_ErrorCollector($context);
150 $context->register('ErrorCollector', $error_collector);
153 $html = HTMLPurifier_Encoder::convertToUTF8($html, $config, $context);
155 for ($i = 0, $size = count($this->filters); $i < $size; $i++) {
156 $html = $this->filters[$i]->preFilter($html, $config, $context);
159 // purified HTML
160 $html =
161 $this->generator->generateFromTokens(
162 // list of tokens
163 $this->strategy->execute(
164 // list of un-purified tokens
165 $lexer->tokenizeHTML(
166 // un-purified HTML
167 $html, $config, $context
169 $config, $context
171 $config, $context
174 for ($i = $size - 1; $i >= 0; $i--) {
175 $html = $this->filters[$i]->postFilter($html, $config, $context);
178 $html = HTMLPurifier_Encoder::convertFromUTF8($html, $config, $context);
179 $this->context =& $context;
180 return $html;
184 * Filters an array of HTML snippets
185 * @param $config Optional HTMLPurifier_Config object for this operation.
186 * See HTMLPurifier::purify() for more details.
187 * @return Array of purified HTML
189 function purifyArray($array_of_html, $config = null) {
190 $context_array = array();
191 foreach ($array_of_html as $key => $html) {
192 $array_of_html[$key] = $this->purify($html, $config);
193 $context_array[$key] = $this->context;
195 $this->context = $context_array;
196 return $array_of_html;
200 * Singleton for enforcing just one HTML Purifier in your system
202 function &getInstance($prototype = null) {
203 static $htmlpurifier;
204 if (!$htmlpurifier || $prototype) {
205 if (is_a($prototype, 'HTMLPurifier')) {
206 $htmlpurifier = $prototype;
207 } elseif ($prototype) {
208 $htmlpurifier = new HTMLPurifier($prototype);
209 } else {
210 $htmlpurifier = new HTMLPurifier();
213 return $htmlpurifier;