2007-01-14 15:54:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates a URI in CSS syntax, which uses url('http://example.com')
|
2007-02-14 16:22:28 +00:00
|
|
|
* @note While theoretically speaking a URI in a CSS document could
|
2007-01-14 15:54:05 +00:00
|
|
|
* be non-embedded, as of CSS2 there is no such usage so we're
|
|
|
|
* generalizing it. This may need to be changed in the future.
|
|
|
|
* @warning Since HTMLPurifier_AttrDef_CSS blindly uses semicolons as
|
|
|
|
* the separator, you cannot put a literal semicolon in
|
|
|
|
* in the URI. Try percent encoding it, in that case.
|
|
|
|
*/
|
2007-02-14 20:38:51 +00:00
|
|
|
class HTMLPurifier_AttrDef_CSS_URI extends HTMLPurifier_AttrDef_URI
|
2007-01-14 15:54:05 +00:00
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-11-29 04:29:51 +00:00
|
|
|
public function __construct() {
|
|
|
|
parent::__construct(true); // always embedded
|
2007-01-14 15:54:05 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-05 00:10:43 +00:00
|
|
|
public function validate($uri_string, $config, $context) {
|
2007-01-14 15:54:05 +00:00
|
|
|
// parse the URI out of the string and then pass it onto
|
|
|
|
// the parent object
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-01-14 15:54:05 +00:00
|
|
|
$uri_string = $this->parseCDATA($uri_string);
|
|
|
|
if (strpos($uri_string, 'url(') !== 0) return false;
|
|
|
|
$uri_string = substr($uri_string, 4);
|
|
|
|
$new_length = strlen($uri_string) - 1;
|
|
|
|
if ($uri_string[$new_length] != ')') return false;
|
|
|
|
$uri = trim(substr($uri_string, 0, $new_length));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-27 14:27:54 +00:00
|
|
|
if (!empty($uri) && ($uri[0] == "'" || $uri[0] == '"')) {
|
2007-01-14 15:54:05 +00:00
|
|
|
$quote = $uri[0];
|
|
|
|
$new_length = strlen($uri) - 1;
|
|
|
|
if ($uri[$new_length] !== $quote) return false;
|
|
|
|
$uri = substr($uri, 1, $new_length - 1);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-01-14 15:54:05 +00:00
|
|
|
$keys = array( '(', ')', ',', ' ', '"', "'");
|
|
|
|
$values = array('\\(', '\\)', '\\,', '\\ ', '\\"', "\\'");
|
|
|
|
$uri = str_replace($values, $keys, $uri);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-01-14 15:54:05 +00:00
|
|
|
$result = parent::validate($uri, $config, $context);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-01-14 15:54:05 +00:00
|
|
|
if ($result === false) return false;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-01-14 15:54:05 +00:00
|
|
|
// escape necessary characters according to CSS spec
|
|
|
|
// except for the comma, none of these should appear in the
|
|
|
|
// URI at all
|
|
|
|
$result = str_replace($keys, $values, $result);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2010-04-26 16:06:06 +00:00
|
|
|
return "url('$result')";
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-01-14 15:54:05 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-01-14 15:54:05 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|