2006-07-23 00:11:03 +00:00
|
|
|
<?php
|
|
|
|
|
2008-01-07 02:54:16 +00:00
|
|
|
/*! @mainpage
|
2008-12-06 07:28:20 +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:
|
2008-12-06 07:28:20 +00:00
|
|
|
*
|
2006-07-23 03:43:53 +00:00
|
|
|
* -# 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.
|
2008-12-06 07:28:20 +00:00
|
|
|
*
|
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
|
|
|
/*
|
2024-11-01 03:51:45 +00:00
|
|
|
HTML Purifier 4.18.0 - 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.
|
2008-12-06 07:28:20 +00:00
|
|
|
*
|
|
|
|
* @note There are several points in which configuration can be specified
|
2007-09-25 02:42:35 +00:00
|
|
|
* 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).
|
2008-12-06 07:28:20 +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-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 12:20:01 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* Version of HTML Purifier.
|
|
|
|
* @type string
|
2013-07-16 12:20:01 +00:00
|
|
|
*/
|
2024-11-01 03:51:45 +00:00
|
|
|
public $version = '4.18.0';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 12:20:01 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* Constant with version of HTML Purifier.
|
2013-07-16 12:20:01 +00:00
|
|
|
*/
|
2024-11-01 03:51:45 +00:00
|
|
|
const VERSION = '4.18.0';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 12:20:01 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* Global configuration object.
|
|
|
|
* @type HTMLPurifier_Config
|
2013-07-16 12:20:01 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $config;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 12:20:01 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* Array of extra filter objects to run on HTML,
|
|
|
|
* for backwards compatibility.
|
|
|
|
* @type HTMLPurifier_Filter[]
|
2013-07-16 12:20:01 +00:00
|
|
|
*/
|
2008-04-22 06:40:04 +00:00
|
|
|
private $filters = array();
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 12:20:01 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* Single instance of HTML Purifier.
|
|
|
|
* @type HTMLPurifier
|
2013-07-16 12:20:01 +00:00
|
|
|
*/
|
2008-02-17 18:21:45 +00:00
|
|
|
private static $instance;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
/**
|
|
|
|
* @type HTMLPurifier_Strategy_Core
|
|
|
|
*/
|
|
|
|
protected $strategy;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type HTMLPurifier_Generator
|
|
|
|
*/
|
|
|
|
protected $generator;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-12-15 02:12:03 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* Resultant context of last run purification.
|
2013-07-16 12:20:01 +00:00
|
|
|
* Is an array of contexts if the last called method was purifyArray().
|
2013-07-16 11:56:14 +00:00
|
|
|
* @type HTMLPurifier_Context
|
2006-12-15 02:12:03 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $context;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-07-23 03:43:53 +00:00
|
|
|
/**
|
|
|
|
* Initializes the purifier.
|
2013-07-16 12:20:01 +00:00
|
|
|
*
|
2015-06-10 09:50:40 +00:00
|
|
|
* @param HTMLPurifier_Config|mixed $config Optional HTMLPurifier_Config object
|
2013-07-16 12:20:01 +00:00
|
|
|
* for all instances of the purifier, if omitted, a default
|
|
|
|
* configuration is 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
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function __construct($config = null)
|
|
|
|
{
|
2006-12-15 02:12:03 +00:00
|
|
|
$this->config = HTMLPurifier_Config::create($config);
|
2013-07-16 11:56:14 +00:00
|
|
|
$this->strategy = new HTMLPurifier_Strategy_Core();
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-01-21 15:09:07 +00:00
|
|
|
/**
|
|
|
|
* Adds a filter to process the output. First come first serve
|
2013-07-16 12:20:01 +00:00
|
|
|
*
|
|
|
|
* @param HTMLPurifier_Filter $filter HTMLPurifier_Filter object
|
2007-01-21 15:09:07 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function addFilter($filter)
|
|
|
|
{
|
2013-07-16 12:20:01 +00:00
|
|
|
trigger_error(
|
2013-07-16 11:56:14 +00:00
|
|
|
'HTMLPurifier->addFilter() is deprecated, use configuration directives' .
|
2013-07-16 12:20:01 +00:00
|
|
|
' in the Filter namespace or Filter.Custom',
|
|
|
|
E_USER_WARNING
|
|
|
|
);
|
2007-01-21 15:09:07 +00:00
|
|
|
$this->filters[] = $filter;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
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.
|
2008-12-06 07:28:20 +00:00
|
|
|
*
|
2013-07-16 12:20:01 +00:00
|
|
|
* @param string $html String of HTML to purify
|
|
|
|
* @param HTMLPurifier_Config $config 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.
|
2013-07-16 12:20:01 +00:00
|
|
|
*
|
|
|
|
* @return string Purified HTML
|
2006-07-23 03:43:53 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function purify($html, $config = null)
|
|
|
|
{
|
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;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-17 21:27:39 +00:00
|
|
|
// implementation is partially environment dependant, partially
|
|
|
|
// configuration dependant
|
|
|
|
$lexer = HTMLPurifier_Lexer::create($config);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-01-16 22:22:08 +00:00
|
|
|
$context = new HTMLPurifier_Context();
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-04-28 01:35:07 +00:00
|
|
|
// setup HTML generator
|
|
|
|
$this->generator = new HTMLPurifier_Generator($config, $context);
|
2007-06-26 02:49:21 +00:00
|
|
|
$context->register('Generator', $this->generator);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-18 03:05:18 +00:00
|
|
|
// set up global context variables
|
2009-02-20 00:17:49 +00:00
|
|
|
if ($config->get('Core.CollectErrors')) {
|
2007-06-18 03:05:18 +00:00
|
|
|
// 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);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
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
|
|
|
}
|
2008-12-06 07:28:20 +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);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-01-18 22:55:44 +00:00
|
|
|
$html = HTMLPurifier_Encoder::convertToUTF8($html, $config, $context);
|
2008-12-06 07:28:20 +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) {
|
2013-07-16 11:56:14 +00:00
|
|
|
if (!$flag) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strpos($filter, '.') !== false) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-04-22 06:40:04 +00:00
|
|
|
$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
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-04-22 06:40:04 +00:00
|
|
|
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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-12-15 02:12:03 +00:00
|
|
|
// purified HTML
|
2008-12-06 07:28:20 +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
|
2013-07-16 11:56:14 +00:00
|
|
|
$html,
|
|
|
|
$config,
|
|
|
|
$context
|
2006-12-15 02:12:03 +00:00
|
|
|
),
|
2013-07-16 11:56:14 +00:00
|
|
|
$config,
|
|
|
|
$context
|
2008-04-28 01:35:07 +00:00
|
|
|
)
|
2006-09-01 00:54:38 +00:00
|
|
|
);
|
2008-12-06 07:28:20 +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
|
|
|
}
|
2008-12-06 07:28:20 +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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-12-20 23:51:09 +00:00
|
|
|
/**
|
|
|
|
* Filters an array of HTML snippets
|
2013-07-16 12:20:01 +00:00
|
|
|
*
|
|
|
|
* @param string[] $array_of_html Array of html snippets
|
|
|
|
* @param HTMLPurifier_Config $config Optional config object for this operation.
|
2006-12-20 23:51:09 +00:00
|
|
|
* See HTMLPurifier::purify() for more details.
|
2013-07-16 12:20:01 +00:00
|
|
|
*
|
|
|
|
* @return string[] Array of purified HTML
|
2006-12-20 23:51:09 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function purifyArray($array_of_html, $config = null)
|
|
|
|
{
|
2006-12-20 23:51:09 +00:00
|
|
|
$context_array = array();
|
2020-02-22 17:12:02 +00:00
|
|
|
$array = array();
|
2019-07-14 18:10:33 +00:00
|
|
|
foreach($array_of_html as $key=>$value){
|
|
|
|
if (is_array($value)) {
|
|
|
|
$array[$key] = $this->purifyArray($value, $config);
|
|
|
|
} else {
|
|
|
|
$array[$key] = $this->purify($value, $config);
|
|
|
|
}
|
2006-12-20 23:51:09 +00:00
|
|
|
$context_array[$key] = $this->context;
|
|
|
|
}
|
|
|
|
$this->context = $context_array;
|
2019-07-14 18:10:33 +00:00
|
|
|
return $array;
|
2006-12-20 23:51:09 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-28 03:33:12 +00:00
|
|
|
/**
|
|
|
|
* Singleton for enforcing just one HTML Purifier in your system
|
2013-07-16 12:20:01 +00:00
|
|
|
*
|
|
|
|
* @param HTMLPurifier|HTMLPurifier_Config $prototype Optional prototype
|
|
|
|
* HTMLPurifier instance to overload singleton with,
|
|
|
|
* or HTMLPurifier_Config instance to configure the
|
|
|
|
* generated version with.
|
|
|
|
*
|
|
|
|
* @return HTMLPurifier
|
2007-05-28 03:33:12 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public static function instance($prototype = null)
|
|
|
|
{
|
2008-02-17 18:21:45 +00:00
|
|
|
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
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-04-23 02:40:17 +00:00
|
|
|
/**
|
2013-07-16 12:20:01 +00:00
|
|
|
* Singleton for enforcing just one HTML Purifier in your system
|
|
|
|
*
|
|
|
|
* @param HTMLPurifier|HTMLPurifier_Config $prototype Optional prototype
|
|
|
|
* HTMLPurifier instance to overload singleton with,
|
|
|
|
* or HTMLPurifier_Config instance to configure the
|
|
|
|
* generated version with.
|
|
|
|
*
|
|
|
|
* @return HTMLPurifier
|
2008-04-23 02:40:17 +00:00
|
|
|
* @note Backwards compatibility, see instance()
|
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public static function getInstance($prototype = null)
|
|
|
|
{
|
2008-04-23 02:40:17 +00:00
|
|
|
return HTMLPurifier::instance($prototype);
|
|
|
|
}
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
2008-12-06 09:24:59 +00:00
|
|
|
|
|
|
|
// vim: et sw=4 sts=4
|