| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
define('HTMLPURIFIER_PREFIX', dirname(__FILE__)); |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
require_once 'HTMLPurifier/ConfigSchema.php'; |
|---|
| 48 |
require_once 'HTMLPurifier/Config.php'; |
|---|
| 49 |
require_once 'HTMLPurifier/Context.php'; |
|---|
| 50 |
|
|---|
| 51 |
require_once 'HTMLPurifier/Lexer.php'; |
|---|
| 52 |
require_once 'HTMLPurifier/Generator.php'; |
|---|
| 53 |
require_once 'HTMLPurifier/Strategy/Core.php'; |
|---|
| 54 |
require_once 'HTMLPurifier/Encoder.php'; |
|---|
| 55 |
|
|---|
| 56 |
require_once 'HTMLPurifier/ErrorCollector.php'; |
|---|
| 57 |
require_once 'HTMLPurifier/LanguageFactory.php'; |
|---|
| 58 |
|
|---|
| 59 |
HTMLPurifier_ConfigSchema::define( |
|---|
| 60 |
'Core', 'CollectErrors', false, 'bool', ' |
|---|
| 61 |
Whether or not to collect errors found while filtering the document. This |
|---|
| 62 |
is a useful way to give feedback to your users. <strong>Warning:</strong> |
|---|
| 63 |
Currently this feature is very patchy and experimental, with lots of |
|---|
| 64 |
possible error messages not yet implemented. It will not cause any problems, |
|---|
| 65 |
but it may not help your users either. This directive has been available |
|---|
| 66 |
since 2.0.0. |
|---|
| 67 |
'); |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
class HTMLPurifier |
|---|
| 84 |
{ |
|---|
| 85 |
|
|---|
| 86 |
var $version = '2.1.4'; |
|---|
| 87 |
|
|---|
| 88 |
var $config; |
|---|
| 89 |
var $filters = array(); |
|---|
| 90 |
|
|---|
| 91 |
var $strategy, $generator; |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
* Resultant HTMLPurifier_Context of last run purification. Is an array |
|---|
| 95 |
* of contexts if the last called method was purifyArray(). |
|---|
| 96 |
* @public |
|---|
| 97 |
*/ |
|---|
| 98 |
var $context; |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
* Initializes the purifier. |
|---|
| 102 |
* @param $config Optional HTMLPurifier_Config object for all instances of |
|---|
| 103 |
* the purifier, if omitted, a default configuration is |
|---|
| 104 |
* supplied (which can be overridden on a per-use basis). |
|---|
| 105 |
* The parameter can also be any type that |
|---|
| 106 |
* HTMLPurifier_Config::create() supports. |
|---|
| 107 |
*/ |
|---|
| 108 |
function HTMLPurifier($config = null) { |
|---|
| 109 |
|
|---|
| 110 |
$this->config = HTMLPurifier_Config::create($config); |
|---|
| 111 |
|
|---|
| 112 |
$this->strategy = new HTMLPurifier_Strategy_Core(); |
|---|
| 113 |
$this->generator = new HTMLPurifier_Generator(); |
|---|
| 114 |
|
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
* Adds a filter to process the output. First come first serve |
|---|
| 119 |
* @param $filter HTMLPurifier_Filter object |
|---|
| 120 |
*/ |
|---|
| 121 |
function addFilter($filter) { |
|---|
| 122 |
$this->filters[] = $filter; |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
* Filters an HTML snippet/document to be XSS-free and standards-compliant. |
|---|
| 127 |
* |
|---|
| 128 |
* @param $html String of HTML to purify |
|---|
| 129 |
* @param $config HTMLPurifier_Config object for this operation, if omitted, |
|---|
| 130 |
* defaults to the config object specified during this |
|---|
| 131 |
* object's construction. The parameter can also be any type |
|---|
| 132 |
* that HTMLPurifier_Config::create() supports. |
|---|
| 133 |
* @return Purified HTML |
|---|
| 134 |
*/ |
|---|
| 135 |
function purify($html, $config = null) { |
|---|
| 136 |
|
|---|
| 137 |
$config = $config ? HTMLPurifier_Config::create($config) : $this->config; |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
// configuration dependant |
|---|
| 141 |
$lexer = HTMLPurifier_Lexer::create($config); |
|---|
| 142 |
|
|---|
| 143 |
$context = new HTMLPurifier_Context(); |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
$this->generator->generateFromTokens(array(), $config, $context); |
|---|
| 147 |
$context->register('Generator', $this->generator); |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
if ($config->get('Core', 'CollectErrors')) { |
|---|
| 151 |
|
|---|
| 152 |
$language_factory = HTMLPurifier_LanguageFactory::instance(); |
|---|
| 153 |
$language = $language_factory->create($config, $context); |
|---|
| 154 |
$context->register('Locale', $language); |
|---|
| 155 |
|
|---|
| 156 |
$error_collector = new HTMLPurifier_ErrorCollector($context); |
|---|
| 157 |
$context->register('ErrorCollector', $error_collector); |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
// AttrValidator can be called from many places |
|---|
| 162 |
$id_accumulator = HTMLPurifier_IDAccumulator::build($config, $context); |
|---|
| 163 |
$context->register('IDAccumulator', $id_accumulator); |
|---|
| 164 |
|
|---|
| 165 |
$html = HTMLPurifier_Encoder::convertToUTF8($html, $config, $context); |
|---|
| 166 |
|
|---|
| 167 |
for ($i = 0, $size = count($this->filters); $i < $size; $i++) { |
|---|
| 168 |
$html = $this->filters[$i]->preFilter($html, $config, $context); |
|---|
| 169 |
} |
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
$html = |
|---|
| 173 |
$this->generator->generateFromTokens( |
|---|
| 174 |
|
|---|
| 175 |
$this->strategy->execute( |
|---|
| 176 |
|
|---|
| 177 |
$lexer->tokenizeHTML( |
|---|
| 178 |
|
|---|
| 179 |
$html, $config, $context |
|---|
| 180 |
), |
|---|
| 181 |
$config, $context |
|---|
| 182 |
), |
|---|
| 183 |
$config, $context |
|---|
| 184 |
); |
|---|
| 185 |
|
|---|
| 186 |
for ($i = $size - 1; $i >= 0; $i--) { |
|---|
| 187 |
$html = $this->filters[$i]->postFilter($html, $config, $context); |
|---|
| 188 |
} |
|---|
| 189 |
|
|---|
| 190 |
$html = HTMLPurifier_Encoder::convertFromUTF8($html, $config, $context); |
|---|
| 191 |
$this->context =& $context; |
|---|
| 192 |
return $html; |
|---|
| 193 |
} |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
* Filters an array of HTML snippets |
|---|
| 197 |
* @param $config Optional HTMLPurifier_Config object for this operation. |
|---|
| 198 |
* See HTMLPurifier::purify() for more details. |
|---|
| 199 |
* @return Array of purified HTML |
|---|
| 200 |
*/ |
|---|
| 201 |
function purifyArray($array_of_html, $config = null) { |
|---|
| 202 |
$context_array = array(); |
|---|
| 203 |
foreach ($array_of_html as $key => $html) { |
|---|
| 204 |
$array_of_html[$key] = $this->purify($html, $config); |
|---|
| 205 |
$context_array[$key] = $this->context; |
|---|
| 206 |
} |
|---|
| 207 |
$this->context = $context_array; |
|---|
| 208 |
return $array_of_html; |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
* Singleton for enforcing just one HTML Purifier in your system |
|---|
| 213 |
* @param $prototype Optional prototype HTMLPurifier instance to |
|---|
| 214 |
* overload singleton with. |
|---|
| 215 |
*/ |
|---|
| 216 |
function &instance($prototype = null) { |
|---|
| 217 |
static $htmlpurifier; |
|---|
| 218 |
if (!$htmlpurifier || $prototype) { |
|---|
| 219 |
if (is_a($prototype, 'HTMLPurifier')) { |
|---|
| 220 |
$htmlpurifier = $prototype; |
|---|
| 221 |
} elseif ($prototype) { |
|---|
| 222 |
$htmlpurifier = new HTMLPurifier($prototype); |
|---|
| 223 |
} else { |
|---|
| 224 |
$htmlpurifier = new HTMLPurifier(); |
|---|
| 225 |
} |
|---|
| 226 |
} |
|---|
| 227 |
return $htmlpurifier; |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
function &getInstance($prototype = null) { |
|---|
| 231 |
return HTMLPurifier::instance($prototype); |
|---|
| 232 |
} |
|---|
| 233 |
|
|---|
| 234 |
} |
|---|
| 235 |
|
|---|
| 236 |
|
|---|