2006-08-12 01:12:35 +00:00
|
|
|
<?php
|
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
|
|
|
* Validates a URI as defined by RFC 3986.
|
|
|
|
* @note Scheme-specific mechanics deferred to HTMLPurifier_URIScheme
|
|
|
|
*/
|
2006-08-12 01:12:35 +00:00
|
|
|
class HTMLPurifier_AttrDef_URI extends HTMLPurifier_AttrDef
|
|
|
|
{
|
2006-08-12 03:35:05 +00:00
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $parser, $percentEncoder;
|
|
|
|
protected $embedsResource;
|
2006-08-17 01:05:35 +00:00
|
|
|
|
2006-11-17 23:09:10 +00:00
|
|
|
/**
|
2006-11-23 23:59:20 +00:00
|
|
|
* @param $embeds_resource_resource Does the URI here result in an extra HTTP request?
|
2006-11-17 23:09:10 +00:00
|
|
|
*/
|
2007-11-29 04:29:51 +00:00
|
|
|
public function __construct($embeds_resource = false) {
|
2007-08-01 14:55:09 +00:00
|
|
|
$this->parser = new HTMLPurifier_URIParser();
|
2007-08-01 18:34:46 +00:00
|
|
|
$this->percentEncoder = new HTMLPurifier_PercentEncoder();
|
|
|
|
$this->embedsResource = (bool) $embeds_resource;
|
2006-08-17 01:05:35 +00:00
|
|
|
}
|
|
|
|
|
2008-01-05 00:10:43 +00:00
|
|
|
public function validate($uri, $config, $context) {
|
2006-08-12 01:12:35 +00:00
|
|
|
|
2007-02-14 01:57:06 +00:00
|
|
|
if ($config->get('URI', 'Disable')) return false;
|
|
|
|
|
2007-08-01 14:55:09 +00:00
|
|
|
// initial operations
|
2006-08-12 01:12:35 +00:00
|
|
|
$uri = $this->parseCDATA($uri);
|
2007-08-01 18:34:46 +00:00
|
|
|
$uri = $this->percentEncoder->normalize($uri);
|
2006-11-07 17:15:28 +00:00
|
|
|
|
2007-08-01 14:55:09 +00:00
|
|
|
// parse the URI
|
2007-08-01 18:34:46 +00:00
|
|
|
$uri = $this->parser->parse($uri);
|
|
|
|
if ($uri === false) return false;
|
2006-08-12 03:35:05 +00:00
|
|
|
|
2007-08-02 01:12:27 +00:00
|
|
|
// add embedded flag to context for validators
|
|
|
|
$context->register('EmbeddedURI', $this->embedsResource);
|
2006-08-12 03:35:05 +00:00
|
|
|
|
2007-08-02 01:12:27 +00:00
|
|
|
$ok = false;
|
|
|
|
do {
|
|
|
|
|
|
|
|
// generic validation
|
|
|
|
$result = $uri->validate($config, $context);
|
|
|
|
if (!$result) break;
|
|
|
|
|
2007-09-27 00:07:27 +00:00
|
|
|
// chained filtering
|
2008-04-23 02:40:17 +00:00
|
|
|
$uri_def = $config->getDefinition('URI');
|
2007-08-02 01:12:27 +00:00
|
|
|
$result = $uri_def->filter($uri, $config, $context);
|
|
|
|
if (!$result) break;
|
|
|
|
|
|
|
|
// scheme-specific validation
|
|
|
|
$scheme_obj = $uri->getSchemeObj($config, $context);
|
|
|
|
if (!$scheme_obj) break;
|
|
|
|
if ($this->embedsResource && !$scheme_obj->browsable) break;
|
|
|
|
$result = $scheme_obj->validate($uri, $config, $context);
|
|
|
|
if (!$result) break;
|
|
|
|
|
|
|
|
// survived gauntlet
|
|
|
|
$ok = true;
|
|
|
|
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
$context->destroy('EmbeddedURI');
|
|
|
|
if (!$ok) return false;
|
2006-08-12 03:35:05 +00:00
|
|
|
|
2007-08-02 01:41:37 +00:00
|
|
|
// munge scheme off if necessary (this must be last)
|
|
|
|
if (!is_null($uri->scheme) && is_null($uri->host)) {
|
2007-08-02 21:47:24 +00:00
|
|
|
if ($uri_def->defaultScheme == $uri->scheme) {
|
2007-08-02 01:41:37 +00:00
|
|
|
$uri->scheme = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-01 18:34:46 +00:00
|
|
|
// back to string
|
|
|
|
$result = $uri->toString();
|
2006-08-12 03:35:05 +00:00
|
|
|
|
2007-08-02 01:41:37 +00:00
|
|
|
// munge entire URI if necessary
|
2007-08-01 18:34:46 +00:00
|
|
|
if (
|
|
|
|
!is_null($uri->host) && // indicator for authority
|
|
|
|
!empty($scheme_obj->browsable) &&
|
|
|
|
!is_null($munge = $config->get('URI', 'Munge'))
|
|
|
|
) {
|
|
|
|
$result = str_replace('%s', rawurlencode($result), $munge);
|
2006-11-24 00:29:16 +00:00
|
|
|
}
|
|
|
|
|
2006-08-12 03:35:05 +00:00
|
|
|
return $result;
|
|
|
|
|
2006-08-12 01:12:35 +00:00
|
|
|
}
|
2006-08-12 03:35:05 +00:00
|
|
|
|
2006-08-12 01:12:35 +00:00
|
|
|
}
|
|
|
|
|
2007-06-27 13:58:32 +00:00
|
|
|
|