3
require_once 'HTMLPurifier/URIParser.php';
4
require_once 'HTMLPurifier/URIFilter.php';
7
* HTML Purifier's internal representation of a URI
12
public $scheme, $userinfo, $host, $port, $path, $query, $fragment;
15
* @note Automatically normalizes scheme and port
17
public function __construct($scheme, $userinfo, $host, $port, $path, $query, $fragment) {
18
$this->scheme = is_null($scheme) || ctype_lower($scheme) ? $scheme : strtolower($scheme);
19
$this->userinfo = $userinfo;
21
$this->port = is_null($port) ? $port : (int) $port;
23
$this->query = $query;
24
$this->fragment = $fragment;
28
* Retrieves a scheme object corresponding to the URI's scheme/default
29
* @param $config Instance of HTMLPurifier_Config
30
* @param $context Instance of HTMLPurifier_Context
31
* @return Scheme object appropriate for validating this URI
33
public function getSchemeObj($config, $context) {
34
$registry = HTMLPurifier_URISchemeRegistry::instance();
35
if ($this->scheme !== null) {
36
$scheme_obj = $registry->getScheme($this->scheme, $config, $context);
37
if (!$scheme_obj) return false; // invalid scheme, clean it out
39
// no scheme: retrieve the default one
40
$def = $config->getDefinition('URI');
41
$scheme_obj = $registry->getScheme($def->defaultScheme, $config, $context);
43
// something funky happened to the default scheme object
45
'Default scheme object "' . $def->defaultScheme . '" was not readable',
55
* Generic validation method applicable for all schemes
56
* @param $config Instance of HTMLPurifier_Config
57
* @param $context Instance of HTMLPurifier_Context
58
* @return True if validation/filtering succeeds, false if failure
60
public function validate($config, $context) {
63
if (!is_null($this->host)) {
64
$host_def = new HTMLPurifier_AttrDef_URI_Host();
65
$this->host = $host_def->validate($this->host, $config, $context);
66
if ($this->host === false) $this->host = null;
70
if (!is_null($this->port)) {
71
if ($this->port < 1 || $this->port > 65535) $this->port = null;
74
// query and fragment are quite simple in terms of definition:
75
// *( pchar / "/" / "?" ), so define their validation routines
76
// when we start fixing percent encoding
78
// path gets to be validated against a hodge-podge of rules depending
79
// on the status of authority and scheme, but it's not that important,
80
// esp. since it won't be applicable to everyone
87
* Convert URI back to string
88
* @return String URI appropriate for output
90
public function toString() {
91
// reconstruct authority
93
if (!is_null($this->host)) {
95
if(!is_null($this->userinfo)) $authority .= $this->userinfo . '@';
96
$authority .= $this->host;
97
if(!is_null($this->port)) $authority .= ':' . $this->port;
100
// reconstruct the result
102
if (!is_null($this->scheme)) $result .= $this->scheme . ':';
103
if (!is_null($authority)) $result .= '//' . $authority;
104
$result .= $this->path;
105
if (!is_null($this->query)) $result .= '?' . $this->query;
106
if (!is_null($this->fragment)) $result .= '#' . $this->fragment;
112
* Returns a copy of the URI object
114
public function copy() {
115
return unserialize(serialize($this));