2006-07-23 00:11:03 +00:00
|
|
|
<?php
|
|
|
|
|
2008-01-07 02:54:16 +00:00
|
|
|
/*! @mainpage
|
2006-07-23 03:43:53 +00:00
|
|
|
*
|
2006-09-27 02:09:54 +00:00
|
|
|
* HTML Purifier is an HTML filter that will take an arbitrary snippet of
|
2006-07-23 03:43:53 +00:00
|
|
|
* HTML and rigorously test, validate and filter it into a version that
|
|
|
|
* is safe for output onto webpages. It achieves this by:
|
|
|
|
*
|
|
|
|
* -# Lexing (parsing into tokens) the document,
|
2006-07-24 02:49:37 +00:00
|
|
|
* -# Executing various strategies on the tokens:
|
|
|
|
* -# Removing all elements not in the whitelist,
|
|
|
|
* -# Making the tokens well-formed,
|
|
|
|
* -# Fixing the nesting of the nodes, and
|
|
|
|
* -# Validating attributes of the nodes; and
|
2006-07-23 03:43:53 +00:00
|
|
|
* -# Generating HTML from the purified tokens.
|
|
|
|
*
|
2006-08-17 20:29:34 +00:00
|
|
|
* However, most users will only need to interface with the HTMLPurifier
|
2008-04-22 17:32:42 +00:00
|
|
|
* and HTMLPurifier_Config.
|
2006-07-23 03:43:53 +00:00
|
|
|
*/
|
|
|
|
|
2006-09-01 01:07:09 +00:00
|
|
|
/*
|
2008-04-22 07:24:47 +00:00
|
|
|
HTML Purifier 3.1.0rc1 - Standards Compliant HTML Filtering
|
2008-01-07 02:54:16 +00:00
|
|
|
Copyright (C) 2006-2008 Edward Z. Yang
|
2006-09-01 01:07:09 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2006-09-07 02:13:17 +00:00
|
|
|
|
2006-07-23 03:43:53 +00:00
|
|
|
/**
|
2007-09-25 02:42:35 +00:00
|
|
|
* Facade that coordinates HTML Purifier's subsystems in order to purify HTML.
|
2006-07-23 03:43:53 +00:00
|
|
|
*
|
2007-09-25 02:42:35 +00:00
|
|
|
* @note There are several points in which configuration can be specified
|
|
|
|
* for HTML Purifier. The precedence of these (from lowest to
|
|
|
|
* highest) is as follows:
|
|
|
|
* -# Instance: new HTMLPurifier($config)
|
|
|
|
* -# Invocation: purify($html, $config)
|
|
|
|
* These configurations are entirely independent of each other and
|
2008-02-17 18:21:45 +00:00
|
|
|
* are *not* merged (this behavior may change in the future).
|
2006-08-30 02:24:43 +00:00
|
|
|
*
|
2008-02-17 18:21:45 +00:00
|
|
|
* @todo We need an easier way to inject strategies using the configuration
|
|
|
|
* object.
|
2006-07-23 03:43:53 +00:00
|
|
|
*/
|
2006-07-23 00:11:03 +00:00
|
|
|
class HTMLPurifier
|
|
|
|
{
|
|
|
|
|
2008-02-17 18:21:45 +00:00
|
|
|
/** Version of HTML Purifier */
|
2008-04-22 07:24:47 +00:00
|
|
|
public $version = '3.1.0rc1';
|
2006-12-26 04:31:48 +00:00
|
|
|
|
2008-02-17 18:21:45 +00:00
|
|
|
/** Global configuration object */
|
2007-11-25 02:24:39 +00:00
|
|
|
public $config;
|
2008-02-17 18:21:45 +00:00
|
|
|
|
2008-04-22 06:40:04 +00:00
|
|
|
/** Array of extra HTMLPurifier_Filter objects to run on HTML, for backwards compatibility */
|
|
|
|
private $filters = array();
|
2006-08-14 00:27:15 +00:00
|
|
|
|
2008-02-17 18:21:45 +00:00
|
|
|
/** Single instance of HTML Purifier */
|
|
|
|
private static $instance;
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $strategy, $generator;
|
2006-08-17 20:29:34 +00:00
|
|
|
|
2006-12-15 02:12:03 +00:00
|
|
|
/**
|
2007-09-25 02:42:35 +00:00
|
|
|
* Resultant HTMLPurifier_Context of last run purification. Is an array
|
|
|
|
* of contexts if the last called method was purifyArray().
|
2006-12-15 02:12:03 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $context;
|
2006-12-15 02:12:03 +00:00
|
|
|
|
2006-07-23 03:43:53 +00:00
|
|
|
/**
|
|
|
|
* Initializes the purifier.
|
2006-08-17 20:29:34 +00:00
|
|
|
* @param $config Optional HTMLPurifier_Config object for all instances of
|
|
|
|
* the purifier, if omitted, a default configuration is
|
2006-08-30 02:24:43 +00:00
|
|
|
* supplied (which can be overridden on a per-use basis).
|
2006-12-15 02:12:03 +00:00
|
|
|
* The parameter can also be any type that
|
|
|
|
* HTMLPurifier_Config::create() supports.
|
2006-07-23 03:43:53 +00:00
|
|
|
*/
|
2007-11-29 04:29:51 +00:00
|
|
|
public function __construct($config = null) {
|
2006-08-30 02:24:43 +00:00
|
|
|
|
2006-12-15 02:12:03 +00:00
|
|
|
$this->config = HTMLPurifier_Config::create($config);
|
2006-08-17 20:29:34 +00:00
|
|
|
|
2006-08-30 02:24:43 +00:00
|
|
|
$this->strategy = new HTMLPurifier_Strategy_Core();
|
|
|
|
$this->generator = new HTMLPurifier_Generator();
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
2007-01-21 15:09:07 +00:00
|
|
|
/**
|
|
|
|
* Adds a filter to process the output. First come first serve
|
|
|
|
* @param $filter HTMLPurifier_Filter object
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function addFilter($filter) {
|
2008-04-22 06:40:04 +00:00
|
|
|
trigger_error('HTMLPurifier->addFilter() is deprecated, use configuration directives in the Filter namespace or Filter.Custom', E_USER_WARNING);
|
2007-01-21 15:09:07 +00:00
|
|
|
$this->filters[] = $filter;
|
|
|
|
}
|
|
|
|
|
2006-07-23 03:43:53 +00:00
|
|
|
/**
|
2006-08-17 20:29:34 +00:00
|
|
|
* Filters an HTML snippet/document to be XSS-free and standards-compliant.
|
2006-07-23 03:43:53 +00:00
|
|
|
*
|
|
|
|
* @param $html String of HTML to purify
|
2006-08-17 20:29:34 +00:00
|
|
|
* @param $config HTMLPurifier_Config object for this operation, if omitted,
|
|
|
|
* defaults to the config object specified during this
|
2006-12-15 02:12:03 +00:00
|
|
|
* object's construction. The parameter can also be any type
|
|
|
|
* that HTMLPurifier_Config::create() supports.
|
2006-07-23 03:43:53 +00:00
|
|
|
* @return Purified HTML
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function purify($html, $config = null) {
|
2006-12-15 02:12:03 +00:00
|
|
|
|
2008-02-17 18:21:45 +00:00
|
|
|
// :TODO: make the config merge in, instead of replace
|
2006-12-15 02:12:03 +00:00
|
|
|
$config = $config ? HTMLPurifier_Config::create($config) : $this->config;
|
|
|
|
|
2007-06-17 21:27:39 +00:00
|
|
|
// implementation is partially environment dependant, partially
|
|
|
|
// configuration dependant
|
|
|
|
$lexer = HTMLPurifier_Lexer::create($config);
|
|
|
|
|
2007-01-16 22:22:08 +00:00
|
|
|
$context = new HTMLPurifier_Context();
|
2007-06-18 03:05:18 +00:00
|
|
|
|
2007-06-26 02:49:21 +00:00
|
|
|
// our friendly neighborhood generator, all primed with configuration too!
|
|
|
|
$this->generator->generateFromTokens(array(), $config, $context);
|
|
|
|
$context->register('Generator', $this->generator);
|
|
|
|
|
2007-06-18 03:05:18 +00:00
|
|
|
// set up global context variables
|
|
|
|
if ($config->get('Core', 'CollectErrors')) {
|
|
|
|
// may get moved out if other facilities use it
|
|
|
|
$language_factory = HTMLPurifier_LanguageFactory::instance();
|
2007-06-26 19:33:37 +00:00
|
|
|
$language = $language_factory->create($config, $context);
|
2007-06-18 03:05:18 +00:00
|
|
|
$context->register('Locale', $language);
|
|
|
|
|
2007-06-25 01:08:57 +00:00
|
|
|
$error_collector = new HTMLPurifier_ErrorCollector($context);
|
2007-06-25 00:48:26 +00:00
|
|
|
$context->register('ErrorCollector', $error_collector);
|
2007-06-18 03:05:18 +00:00
|
|
|
}
|
|
|
|
|
2007-09-26 23:18:24 +00:00
|
|
|
// setup id_accumulator context, necessary due to the fact that
|
|
|
|
// AttrValidator can be called from many places
|
2007-09-26 23:36:37 +00:00
|
|
|
$id_accumulator = HTMLPurifier_IDAccumulator::build($config, $context);
|
2007-09-26 23:18:24 +00:00
|
|
|
$context->register('IDAccumulator', $id_accumulator);
|
|
|
|
|
2007-01-18 22:55:44 +00:00
|
|
|
$html = HTMLPurifier_Encoder::convertToUTF8($html, $config, $context);
|
2006-12-15 02:12:03 +00:00
|
|
|
|
2008-04-22 06:40:04 +00:00
|
|
|
// setup filters
|
|
|
|
$filter_flags = $config->getBatch('Filter');
|
|
|
|
$custom_filters = $filter_flags['Custom'];
|
|
|
|
unset($filter_flags['Custom']);
|
|
|
|
$filters = array();
|
|
|
|
foreach ($filter_flags as $filter => $flag) {
|
|
|
|
if (!$flag) continue;
|
|
|
|
$class = "HTMLPurifier_Filter_$filter";
|
|
|
|
$filters[] = new $class;
|
|
|
|
}
|
|
|
|
foreach ($custom_filters as $filter) {
|
|
|
|
// maybe "HTMLPurifier_Filter_$filter", but be consistent with AutoFormat
|
|
|
|
$filters[] = $filter;
|
|
|
|
}
|
|
|
|
$filters = array_merge($filters, $this->filters);
|
|
|
|
// maybe prepare(), but later
|
|
|
|
|
|
|
|
for ($i = 0, $filter_size = count($filters); $i < $filter_size; $i++) {
|
|
|
|
$html = $filters[$i]->preFilter($html, $config, $context);
|
2007-01-21 15:09:07 +00:00
|
|
|
}
|
|
|
|
|
2006-12-15 02:12:03 +00:00
|
|
|
// purified HTML
|
2006-09-01 00:54:38 +00:00
|
|
|
$html =
|
2006-08-17 20:29:34 +00:00
|
|
|
$this->generator->generateFromTokens(
|
2006-12-15 02:12:03 +00:00
|
|
|
// list of tokens
|
2006-08-17 20:29:34 +00:00
|
|
|
$this->strategy->execute(
|
2006-12-15 02:12:03 +00:00
|
|
|
// list of un-purified tokens
|
2007-06-17 21:27:39 +00:00
|
|
|
$lexer->tokenizeHTML(
|
2006-12-15 02:12:03 +00:00
|
|
|
// un-purified HTML
|
|
|
|
$html, $config, $context
|
|
|
|
),
|
2006-10-01 20:47:07 +00:00
|
|
|
$config, $context
|
2006-09-01 00:54:38 +00:00
|
|
|
),
|
2006-10-01 20:47:07 +00:00
|
|
|
$config, $context
|
2006-09-01 00:54:38 +00:00
|
|
|
);
|
2006-12-15 02:12:03 +00:00
|
|
|
|
2008-04-22 06:40:04 +00:00
|
|
|
for ($i = $filter_size - 1; $i >= 0; $i--) {
|
|
|
|
$html = $filters[$i]->postFilter($html, $config, $context);
|
2007-01-21 15:09:07 +00:00
|
|
|
}
|
|
|
|
|
2007-01-18 22:55:44 +00:00
|
|
|
$html = HTMLPurifier_Encoder::convertFromUTF8($html, $config, $context);
|
2006-12-15 02:12:03 +00:00
|
|
|
$this->context =& $context;
|
2006-09-01 00:54:38 +00:00
|
|
|
return $html;
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
2006-12-20 23:51:09 +00:00
|
|
|
/**
|
|
|
|
* Filters an array of HTML snippets
|
|
|
|
* @param $config Optional HTMLPurifier_Config object for this operation.
|
|
|
|
* See HTMLPurifier::purify() for more details.
|
|
|
|
* @return Array of purified HTML
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function purifyArray($array_of_html, $config = null) {
|
2006-12-20 23:51:09 +00:00
|
|
|
$context_array = array();
|
|
|
|
foreach ($array_of_html as $key => $html) {
|
|
|
|
$array_of_html[$key] = $this->purify($html, $config);
|
|
|
|
$context_array[$key] = $this->context;
|
|
|
|
}
|
|
|
|
$this->context = $context_array;
|
|
|
|
return $array_of_html;
|
|
|
|
}
|
|
|
|
|
2007-05-28 03:33:12 +00:00
|
|
|
/**
|
|
|
|
* Singleton for enforcing just one HTML Purifier in your system
|
2007-09-25 02:42:35 +00:00
|
|
|
* @param $prototype Optional prototype HTMLPurifier instance to
|
|
|
|
* overload singleton with.
|
2007-05-28 03:33:12 +00:00
|
|
|
*/
|
2008-02-17 18:21:45 +00:00
|
|
|
public static function getInstance($prototype = null) {
|
|
|
|
if (!self::$instance || $prototype) {
|
2007-11-25 02:24:39 +00:00
|
|
|
if ($prototype instanceof HTMLPurifier) {
|
2008-02-17 18:21:45 +00:00
|
|
|
self::$instance = $prototype;
|
2007-05-28 03:33:12 +00:00
|
|
|
} elseif ($prototype) {
|
2008-02-17 18:21:45 +00:00
|
|
|
self::$instance = new HTMLPurifier($prototype);
|
2007-05-28 03:33:12 +00:00
|
|
|
} else {
|
2008-02-17 18:21:45 +00:00
|
|
|
self::$instance = new HTMLPurifier();
|
2007-05-28 03:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-17 18:21:45 +00:00
|
|
|
return self::$instance;
|
2007-05-28 03:33:12 +00:00
|
|
|
}
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|