2006-08-12 20:22:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/AttrDef.php';
|
|
|
|
require_once 'HTMLPurifier/CSSDefinition.php';
|
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
|
|
|
* Validates the HTML attribute style, otherwise known as CSS.
|
|
|
|
* @note We don't implement the whole CSS specification, so it might be
|
|
|
|
* difficult to reuse this component in the context of validating
|
|
|
|
* actual stylesheet declarations.
|
2007-01-15 00:48:54 +00:00
|
|
|
* @note If we were really serious about validating the CSS, we would
|
|
|
|
* tokenize the styles and then parse the tokens. Obviously, we
|
|
|
|
* are not doing that. Doing that could seriously harm performance,
|
|
|
|
* but would make these components a lot more viable for a CSS
|
|
|
|
* filtering solution.
|
2006-08-20 21:47:15 +00:00
|
|
|
*/
|
2006-08-13 21:44:52 +00:00
|
|
|
class HTMLPurifier_AttrDef_CSS extends HTMLPurifier_AttrDef
|
2006-08-12 20:22:09 +00:00
|
|
|
{
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
public function validate($css, $config, &$context) {
|
2006-08-12 20:22:09 +00:00
|
|
|
|
2006-08-13 21:44:52 +00:00
|
|
|
$css = $this->parseCDATA($css);
|
|
|
|
|
2006-08-31 20:33:07 +00:00
|
|
|
$definition = $config->getCSSDefinition();
|
2006-08-12 20:22:09 +00:00
|
|
|
|
|
|
|
// we're going to break the spec and explode by semicolons.
|
|
|
|
// This is because semicolon rarely appears in escaped form
|
2007-01-14 15:54:05 +00:00
|
|
|
// Doing this is generally flaky but fast
|
|
|
|
// IT MIGHT APPEAR IN URIs, see HTMLPurifier_AttrDef_CSSURI
|
|
|
|
// for details
|
2006-08-12 20:22:09 +00:00
|
|
|
|
|
|
|
$declarations = explode(';', $css);
|
2006-08-13 04:52:40 +00:00
|
|
|
$propvalues = array();
|
2006-08-12 20:22:09 +00:00
|
|
|
|
|
|
|
foreach ($declarations as $declaration) {
|
|
|
|
if (!$declaration) continue;
|
|
|
|
if (!strpos($declaration, ':')) continue;
|
|
|
|
list($property, $value) = explode(':', $declaration, 2);
|
2006-09-04 23:01:47 +00:00
|
|
|
$property = trim($property);
|
|
|
|
$value = trim($value);
|
2007-12-01 17:00:55 +00:00
|
|
|
$ok = false;
|
|
|
|
do {
|
|
|
|
if (isset($definition->info[$property])) {
|
|
|
|
$ok = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ctype_lower($property)) break;
|
|
|
|
$property = strtolower($property);
|
|
|
|
if (isset($definition->info[$property])) {
|
|
|
|
$ok = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(0);
|
|
|
|
if (!$ok) continue;
|
2006-08-13 16:52:31 +00:00
|
|
|
// inefficient call, since the validator will do this again
|
|
|
|
if (strtolower(trim($value)) !== 'inherit') {
|
2006-08-23 02:11:04 +00:00
|
|
|
// inherit works for everything (but only on the base property)
|
2006-08-13 16:52:31 +00:00
|
|
|
$result = $definition->info[$property]->validate(
|
|
|
|
$value, $config, $context );
|
|
|
|
} else {
|
|
|
|
$result = 'inherit';
|
|
|
|
}
|
2006-08-12 20:22:09 +00:00
|
|
|
if ($result === false) continue;
|
2006-08-13 04:52:40 +00:00
|
|
|
$propvalues[$property] = $result;
|
|
|
|
}
|
|
|
|
|
2006-11-12 19:26:49 +00:00
|
|
|
// procedure does not write the new CSS simultaneously, so it's
|
2006-08-13 16:52:31 +00:00
|
|
|
// slightly inefficient, but it's the only way of getting rid of
|
|
|
|
// duplicates. Perhaps config to optimize it, but not now.
|
|
|
|
|
2006-08-13 04:52:40 +00:00
|
|
|
$new_declarations = '';
|
|
|
|
foreach ($propvalues as $prop => $value) {
|
|
|
|
$new_declarations .= "$prop:$value;";
|
2006-08-12 20:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $new_declarations ? $new_declarations : false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|