Prepended constants with C_ to avoid reserved words as per https://www.php.net/manual...
[htmlpurifier/darkodev.git] / library / HTMLPurifier / AttrDef / URI.php
blobc1cd89772c2604880f6e88132c71a1f7859ff5b6
1 <?php
3 /**
4 * Validates a URI as defined by RFC 3986.
5 * @note Scheme-specific mechanics deferred to HTMLPurifier_URIScheme
6 */
7 class HTMLPurifier_AttrDef_URI extends HTMLPurifier_AttrDef
10 /**
11 * @type HTMLPurifier_URIParser
13 protected $parser;
15 /**
16 * @type bool
18 protected $embedsResource;
20 /**
21 * @param bool $embeds_resource Does the URI here result in an extra HTTP request?
23 public function __construct($embeds_resource = false)
25 $this->parser = new HTMLPurifier_URIParser();
26 $this->embedsResource = (bool)$embeds_resource;
29 /**
30 * @param string $string
31 * @return HTMLPurifier_AttrDef_URI
33 public function make($string)
35 $embeds = ($string === 'embedded');
36 return new HTMLPurifier_AttrDef_URI($embeds);
39 /**
40 * @param string $uri
41 * @param HTMLPurifier_Config $config
42 * @param HTMLPurifier_Context $context
43 * @return bool|string
45 public function validate($uri, $config, $context)
47 if ($config->get('URI.Disable')) {
48 return false;
51 $uri = $this->parseCDATA($uri);
53 // parse the URI
54 $uri = $this->parser->parse($uri);
55 if ($uri === false) {
56 return false;
59 // add embedded flag to context for validators
60 $context->register('EmbeddedURI', $this->embedsResource);
62 $ok = false;
63 do {
65 // generic validation
66 $result = $uri->validate($config, $context);
67 if (!$result) {
68 break;
71 // chained filtering
72 $uri_def = $config->getDefinition('URI');
73 $result = $uri_def->filter($uri, $config, $context);
74 if (!$result) {
75 break;
78 // scheme-specific validation
79 $scheme_obj = $uri->getSchemeObj($config, $context);
80 if (!$scheme_obj) {
81 break;
83 if ($this->embedsResource && !$scheme_obj->browsable) {
84 break;
86 $result = $scheme_obj->validate($uri, $config, $context);
87 if (!$result) {
88 break;
91 // Post chained filtering
92 $result = $uri_def->postFilter($uri, $config, $context);
93 if (!$result) {
94 break;
97 // survived gauntlet
98 $ok = true;
100 } while (false);
102 $context->destroy('EmbeddedURI');
103 if (!$ok) {
104 return false;
106 // back to string
107 return $uri->toString();
111 // vim: et sw=4 sts=4