| 1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
// why is this a top level function? Because PHP 5.2.0 doesn't seem to
|
|
|
4 |
// understand how to interpret this filter if it's a static method.
|
|
|
5 |
// It's all really silly, but if we go this route it might be reasonable
|
|
|
6 |
// to coalesce all of these methods into one.
|
|
|
7 |
function htmlpurifier_filter_extractstyleblocks_muteerrorhandler()
|
|
|
8 |
{
|
|
|
9 |
}
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* This filter extracts <style> blocks from input HTML, cleans them up
|
|
|
13 |
* using CSSTidy, and then places them in $purifier->context->get('StyleBlocks')
|
|
|
14 |
* so they can be used elsewhere in the document.
|
|
|
15 |
*
|
|
|
16 |
* @note
|
|
|
17 |
* See tests/HTMLPurifier/Filter/ExtractStyleBlocksTest.php for
|
|
|
18 |
* sample usage.
|
|
|
19 |
*
|
|
|
20 |
* @note
|
|
|
21 |
* This filter can also be used on stylesheets not included in the
|
|
|
22 |
* document--something purists would probably prefer. Just directly
|
|
|
23 |
* call HTMLPurifier_Filter_ExtractStyleBlocks->cleanCSS()
|
|
|
24 |
*/
|
|
|
25 |
class HTMLPurifier_Filter_ExtractStyleBlocks extends HTMLPurifier_Filter
|
|
|
26 |
{
|
|
|
27 |
/**
|
|
|
28 |
* @type string
|
|
|
29 |
*/
|
|
|
30 |
public $name = 'ExtractStyleBlocks';
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* @type array
|
|
|
34 |
*/
|
|
|
35 |
private $_styleMatches = array();
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* @type csstidy
|
|
|
39 |
*/
|
|
|
40 |
private $_tidy;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* @type HTMLPurifier_AttrDef_HTML_ID
|
|
|
44 |
*/
|
|
|
45 |
private $_id_attrdef;
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* @type HTMLPurifier_AttrDef_CSS_Ident
|
|
|
49 |
*/
|
|
|
50 |
private $_class_attrdef;
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* @type HTMLPurifier_AttrDef_Enum
|
|
|
54 |
*/
|
|
|
55 |
private $_enum_attrdef;
|
|
|
56 |
|
| 1441 |
ariadna |
57 |
/**
|
|
|
58 |
* @type HTMLPurifier_AttrDef_Enum
|
|
|
59 |
*/
|
|
|
60 |
private $_universal_attrdef;
|
|
|
61 |
|
| 1 |
efrain |
62 |
public function __construct()
|
|
|
63 |
{
|
|
|
64 |
$this->_tidy = new csstidy();
|
|
|
65 |
$this->_tidy->set_cfg('lowercase_s', false);
|
|
|
66 |
$this->_id_attrdef = new HTMLPurifier_AttrDef_HTML_ID(true);
|
|
|
67 |
$this->_class_attrdef = new HTMLPurifier_AttrDef_CSS_Ident();
|
|
|
68 |
$this->_enum_attrdef = new HTMLPurifier_AttrDef_Enum(
|
|
|
69 |
array(
|
|
|
70 |
'first-child',
|
|
|
71 |
'link',
|
|
|
72 |
'visited',
|
|
|
73 |
'active',
|
|
|
74 |
'hover',
|
|
|
75 |
'focus'
|
|
|
76 |
)
|
|
|
77 |
);
|
| 1441 |
ariadna |
78 |
$this->_universal_attrdef = new HTMLPurifier_AttrDef_Enum(
|
|
|
79 |
array(
|
|
|
80 |
'initial',
|
|
|
81 |
'inherit',
|
|
|
82 |
'unset',
|
|
|
83 |
)
|
|
|
84 |
);
|
| 1 |
efrain |
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Save the contents of CSS blocks to style matches
|
|
|
89 |
* @param array $matches preg_replace style $matches array
|
|
|
90 |
*/
|
|
|
91 |
protected function styleCallback($matches)
|
|
|
92 |
{
|
|
|
93 |
$this->_styleMatches[] = $matches[1];
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Removes inline <style> tags from HTML, saves them for later use
|
|
|
98 |
* @param string $html
|
|
|
99 |
* @param HTMLPurifier_Config $config
|
|
|
100 |
* @param HTMLPurifier_Context $context
|
|
|
101 |
* @return string
|
|
|
102 |
* @todo Extend to indicate non-text/css style blocks
|
|
|
103 |
*/
|
|
|
104 |
public function preFilter($html, $config, $context)
|
|
|
105 |
{
|
|
|
106 |
$tidy = $config->get('Filter.ExtractStyleBlocks.TidyImpl');
|
|
|
107 |
if ($tidy !== null) {
|
|
|
108 |
$this->_tidy = $tidy;
|
|
|
109 |
}
|
|
|
110 |
// NB: this must be NON-greedy because if we have
|
|
|
111 |
// <style>foo</style> <style>bar</style>
|
|
|
112 |
// we must not grab foo</style> <style>bar
|
|
|
113 |
$html = preg_replace_callback('#<style(?:\s.*)?>(.*)<\/style>#isU', array($this, 'styleCallback'), $html);
|
|
|
114 |
$style_blocks = $this->_styleMatches;
|
|
|
115 |
$this->_styleMatches = array(); // reset
|
|
|
116 |
$context->register('StyleBlocks', $style_blocks); // $context must not be reused
|
|
|
117 |
if ($this->_tidy) {
|
|
|
118 |
foreach ($style_blocks as &$style) {
|
|
|
119 |
$style = $this->cleanCSS($style, $config, $context);
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
return $html;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* Takes CSS (the stuff found in <style>) and cleans it.
|
|
|
127 |
* @warning Requires CSSTidy <http://csstidy.sourceforge.net/>
|
|
|
128 |
* @param string $css CSS styling to clean
|
|
|
129 |
* @param HTMLPurifier_Config $config
|
|
|
130 |
* @param HTMLPurifier_Context $context
|
|
|
131 |
* @throws HTMLPurifier_Exception
|
|
|
132 |
* @return string Cleaned CSS
|
|
|
133 |
*/
|
|
|
134 |
public function cleanCSS($css, $config, $context)
|
|
|
135 |
{
|
|
|
136 |
// prepare scope
|
|
|
137 |
$scope = $config->get('Filter.ExtractStyleBlocks.Scope');
|
|
|
138 |
if ($scope !== null) {
|
|
|
139 |
$scopes = array_map('trim', explode(',', $scope));
|
|
|
140 |
} else {
|
|
|
141 |
$scopes = array();
|
|
|
142 |
}
|
|
|
143 |
// remove comments from CSS
|
|
|
144 |
$css = trim($css);
|
|
|
145 |
if (strncmp('<!--', $css, 4) === 0) {
|
|
|
146 |
$css = substr($css, 4);
|
|
|
147 |
}
|
|
|
148 |
if (strlen($css) > 3 && substr($css, -3) == '-->') {
|
|
|
149 |
$css = substr($css, 0, -3);
|
|
|
150 |
}
|
|
|
151 |
$css = trim($css);
|
|
|
152 |
set_error_handler('htmlpurifier_filter_extractstyleblocks_muteerrorhandler');
|
|
|
153 |
$this->_tidy->parse($css);
|
|
|
154 |
restore_error_handler();
|
|
|
155 |
$css_definition = $config->getDefinition('CSS');
|
|
|
156 |
$html_definition = $config->getDefinition('HTML');
|
|
|
157 |
$new_css = array();
|
|
|
158 |
foreach ($this->_tidy->css as $k => $decls) {
|
|
|
159 |
// $decls are all CSS declarations inside an @ selector
|
|
|
160 |
$new_decls = array();
|
|
|
161 |
if (is_array($decls)) {
|
|
|
162 |
foreach ($decls as $selector => $style) {
|
|
|
163 |
$selector = trim($selector);
|
|
|
164 |
if ($selector === '') {
|
|
|
165 |
continue;
|
|
|
166 |
} // should not happen
|
|
|
167 |
// Parse the selector
|
|
|
168 |
// Here is the relevant part of the CSS grammar:
|
|
|
169 |
//
|
|
|
170 |
// ruleset
|
|
|
171 |
// : selector [ ',' S* selector ]* '{' ...
|
|
|
172 |
// selector
|
|
|
173 |
// : simple_selector [ combinator selector | S+ [ combinator? selector ]? ]?
|
|
|
174 |
// combinator
|
|
|
175 |
// : '+' S*
|
|
|
176 |
// : '>' S*
|
|
|
177 |
// simple_selector
|
|
|
178 |
// : element_name [ HASH | class | attrib | pseudo ]*
|
|
|
179 |
// | [ HASH | class | attrib | pseudo ]+
|
|
|
180 |
// element_name
|
|
|
181 |
// : IDENT | '*'
|
|
|
182 |
// ;
|
|
|
183 |
// class
|
|
|
184 |
// : '.' IDENT
|
|
|
185 |
// ;
|
|
|
186 |
// attrib
|
|
|
187 |
// : '[' S* IDENT S* [ [ '=' | INCLUDES | DASHMATCH ] S*
|
|
|
188 |
// [ IDENT | STRING ] S* ]? ']'
|
|
|
189 |
// ;
|
|
|
190 |
// pseudo
|
|
|
191 |
// : ':' [ IDENT | FUNCTION S* [IDENT S*]? ')' ]
|
|
|
192 |
// ;
|
|
|
193 |
//
|
|
|
194 |
// For reference, here are the relevant tokens:
|
|
|
195 |
//
|
|
|
196 |
// HASH #{name}
|
|
|
197 |
// IDENT {ident}
|
|
|
198 |
// INCLUDES ==
|
|
|
199 |
// DASHMATCH |=
|
|
|
200 |
// STRING {string}
|
|
|
201 |
// FUNCTION {ident}\(
|
|
|
202 |
//
|
|
|
203 |
// And the lexical scanner tokens
|
|
|
204 |
//
|
|
|
205 |
// name {nmchar}+
|
|
|
206 |
// nmchar [_a-z0-9-]|{nonascii}|{escape}
|
|
|
207 |
// nonascii [\240-\377]
|
|
|
208 |
// escape {unicode}|\\[^\r\n\f0-9a-f]
|
|
|
209 |
// unicode \\{h}}{1,6}(\r\n|[ \t\r\n\f])?
|
|
|
210 |
// ident -?{nmstart}{nmchar*}
|
|
|
211 |
// nmstart [_a-z]|{nonascii}|{escape}
|
|
|
212 |
// string {string1}|{string2}
|
|
|
213 |
// string1 \"([^\n\r\f\\"]|\\{nl}|{escape})*\"
|
|
|
214 |
// string2 \'([^\n\r\f\\"]|\\{nl}|{escape})*\'
|
|
|
215 |
//
|
|
|
216 |
// We'll implement a subset (in order to reduce attack
|
|
|
217 |
// surface); in particular:
|
|
|
218 |
//
|
|
|
219 |
// - No Unicode support
|
|
|
220 |
// - No escapes support
|
|
|
221 |
// - No string support (by proxy no attrib support)
|
|
|
222 |
// - element_name is matched against allowed
|
|
|
223 |
// elements (some people might find this
|
|
|
224 |
// annoying...)
|
|
|
225 |
// - Pseudo-elements one of :first-child, :link,
|
|
|
226 |
// :visited, :active, :hover, :focus
|
|
|
227 |
|
|
|
228 |
// handle ruleset
|
|
|
229 |
$selectors = array_map('trim', explode(',', $selector));
|
|
|
230 |
$new_selectors = array();
|
|
|
231 |
foreach ($selectors as $sel) {
|
|
|
232 |
// split on +, > and spaces
|
|
|
233 |
$basic_selectors = preg_split('/\s*([+> ])\s*/', $sel, -1, PREG_SPLIT_DELIM_CAPTURE);
|
|
|
234 |
// even indices are chunks, odd indices are
|
|
|
235 |
// delimiters
|
|
|
236 |
$nsel = null;
|
|
|
237 |
$delim = null; // guaranteed to be non-null after
|
|
|
238 |
// two loop iterations
|
|
|
239 |
for ($i = 0, $c = count($basic_selectors); $i < $c; $i++) {
|
|
|
240 |
$x = $basic_selectors[$i];
|
|
|
241 |
if ($i % 2) {
|
|
|
242 |
// delimiter
|
|
|
243 |
if ($x === ' ') {
|
|
|
244 |
$delim = ' ';
|
|
|
245 |
} else {
|
|
|
246 |
$delim = ' ' . $x . ' ';
|
|
|
247 |
}
|
|
|
248 |
} else {
|
|
|
249 |
// simple selector
|
|
|
250 |
$components = preg_split('/([#.:])/', $x, -1, PREG_SPLIT_DELIM_CAPTURE);
|
|
|
251 |
$sdelim = null;
|
|
|
252 |
$nx = null;
|
|
|
253 |
for ($j = 0, $cc = count($components); $j < $cc; $j++) {
|
|
|
254 |
$y = $components[$j];
|
|
|
255 |
if ($j === 0) {
|
|
|
256 |
if ($y === '*' || isset($html_definition->info[$y = strtolower($y)])) {
|
|
|
257 |
$nx = $y;
|
|
|
258 |
} else {
|
|
|
259 |
// $nx stays null; this matters
|
|
|
260 |
// if we don't manage to find
|
|
|
261 |
// any valid selector content,
|
|
|
262 |
// in which case we ignore the
|
|
|
263 |
// outer $delim
|
|
|
264 |
}
|
|
|
265 |
} elseif ($j % 2) {
|
|
|
266 |
// set delimiter
|
|
|
267 |
$sdelim = $y;
|
|
|
268 |
} else {
|
|
|
269 |
$attrdef = null;
|
|
|
270 |
if ($sdelim === '#') {
|
|
|
271 |
$attrdef = $this->_id_attrdef;
|
|
|
272 |
} elseif ($sdelim === '.') {
|
|
|
273 |
$attrdef = $this->_class_attrdef;
|
|
|
274 |
} elseif ($sdelim === ':') {
|
|
|
275 |
$attrdef = $this->_enum_attrdef;
|
|
|
276 |
} else {
|
|
|
277 |
throw new HTMLPurifier_Exception('broken invariant sdelim and preg_split');
|
|
|
278 |
}
|
|
|
279 |
$r = $attrdef->validate($y, $config, $context);
|
|
|
280 |
if ($r !== false) {
|
|
|
281 |
if ($r !== true) {
|
|
|
282 |
$y = $r;
|
|
|
283 |
}
|
|
|
284 |
if ($nx === null) {
|
|
|
285 |
$nx = '';
|
|
|
286 |
}
|
|
|
287 |
$nx .= $sdelim . $y;
|
|
|
288 |
}
|
|
|
289 |
}
|
|
|
290 |
}
|
|
|
291 |
if ($nx !== null) {
|
|
|
292 |
if ($nsel === null) {
|
|
|
293 |
$nsel = $nx;
|
|
|
294 |
} else {
|
|
|
295 |
$nsel .= $delim . $nx;
|
|
|
296 |
}
|
|
|
297 |
} else {
|
|
|
298 |
// delimiters to the left of invalid
|
|
|
299 |
// basic selector ignored
|
|
|
300 |
}
|
|
|
301 |
}
|
|
|
302 |
}
|
|
|
303 |
if ($nsel !== null) {
|
|
|
304 |
if (!empty($scopes)) {
|
|
|
305 |
foreach ($scopes as $s) {
|
|
|
306 |
$new_selectors[] = "$s $nsel";
|
|
|
307 |
}
|
|
|
308 |
} else {
|
|
|
309 |
$new_selectors[] = $nsel;
|
|
|
310 |
}
|
|
|
311 |
}
|
|
|
312 |
}
|
|
|
313 |
if (empty($new_selectors)) {
|
|
|
314 |
continue;
|
|
|
315 |
}
|
|
|
316 |
$selector = implode(', ', $new_selectors);
|
|
|
317 |
foreach ($style as $name => $value) {
|
|
|
318 |
if (!isset($css_definition->info[$name])) {
|
|
|
319 |
unset($style[$name]);
|
|
|
320 |
continue;
|
|
|
321 |
}
|
| 1441 |
ariadna |
322 |
$uni_ret = $this->_universal_attrdef->validate($value, $config, $context);
|
|
|
323 |
if ($uni_ret !== false) {
|
|
|
324 |
$style[$name] = $uni_ret;
|
|
|
325 |
continue;
|
|
|
326 |
}
|
| 1 |
efrain |
327 |
$def = $css_definition->info[$name];
|
|
|
328 |
$ret = $def->validate($value, $config, $context);
|
|
|
329 |
if ($ret === false) {
|
|
|
330 |
unset($style[$name]);
|
|
|
331 |
} else {
|
|
|
332 |
$style[$name] = $ret;
|
|
|
333 |
}
|
|
|
334 |
}
|
|
|
335 |
$new_decls[$selector] = $style;
|
|
|
336 |
}
|
|
|
337 |
} else {
|
|
|
338 |
continue;
|
|
|
339 |
}
|
|
|
340 |
$new_css[$k] = $new_decls;
|
|
|
341 |
}
|
|
|
342 |
// remove stuff that shouldn't be used, could be reenabled
|
|
|
343 |
// after security risks are analyzed
|
|
|
344 |
$this->_tidy->css = $new_css;
|
|
|
345 |
$this->_tidy->import = array();
|
|
|
346 |
$this->_tidy->charset = null;
|
|
|
347 |
$this->_tidy->namespace = null;
|
|
|
348 |
$css = $this->_tidy->print->plain();
|
|
|
349 |
// we are going to escape any special characters <>& to ensure
|
|
|
350 |
// that no funny business occurs (i.e. </style> in a font-family prop).
|
|
|
351 |
if ($config->get('Filter.ExtractStyleBlocks.Escaping')) {
|
|
|
352 |
$css = str_replace(
|
|
|
353 |
array('<', '>', '&'),
|
|
|
354 |
array('\3C ', '\3E ', '\26 '),
|
|
|
355 |
$css
|
|
|
356 |
);
|
|
|
357 |
}
|
|
|
358 |
return $css;
|
|
|
359 |
}
|
|
|
360 |
}
|
|
|
361 |
|
|
|
362 |
// vim: et sw=4 sts=4
|