3 require_once 'HTMLPurifier/URIParser.php';
4 require_once 'HTMLPurifier/URIFilter.php';
7 * HTML Purifier's internal representation of a URI.
9 * Internal data-structures are completely escaped. If the data needs
10 * to be used in a non-URI context (which is very unlikely), be sure
11 * to decode it first. The URI may not necessarily be well-formed until
12 * validate() is called.
14 class HTMLPurifier_URI
17 var $scheme, $userinfo, $host, $port, $path, $query, $fragment;
20 * @note Automatically normalizes scheme and port
22 function HTMLPurifier_URI($scheme, $userinfo, $host, $port, $path, $query, $fragment) {
23 $this->scheme
= is_null($scheme) ||
ctype_lower($scheme) ?
$scheme : strtolower($scheme);
24 $this->userinfo
= $userinfo;
26 $this->port
= is_null($port) ?
$port : (int) $port;
28 $this->query
= $query;
29 $this->fragment
= $fragment;
33 * Retrieves a scheme object corresponding to the URI's scheme/default
34 * @param $config Instance of HTMLPurifier_Config
35 * @param $context Instance of HTMLPurifier_Context
36 * @return Scheme object appropriate for validating this URI
38 function getSchemeObj($config, &$context) {
39 $registry =& HTMLPurifier_URISchemeRegistry
::instance();
40 if ($this->scheme
!== null) {
41 $scheme_obj = $registry->getScheme($this->scheme
, $config, $context);
42 if (!$scheme_obj) return false; // invalid scheme, clean it out
44 // no scheme: retrieve the default one
45 $def = $config->getDefinition('URI');
46 $scheme_obj = $registry->getScheme($def->defaultScheme
, $config, $context);
48 // something funky happened to the default scheme object
50 'Default scheme object "' . $def->defaultScheme
. '" was not readable',
60 * Generic validation method applicable for all schemes. May modify
61 * this URI in order to get it into a compliant form.
62 * @param $config Instance of HTMLPurifier_Config
63 * @param $context Instance of HTMLPurifier_Context
64 * @return True if validation/filtering succeeds, false if failure
66 function validate($config, &$context) {
68 // ABNF definitions from RFC 3986
69 $chars_sub_delims = '!$&\'()*+,;=';
70 $chars_gen_delims = ':/?#[]@';
71 $chars_pchar = $chars_sub_delims . ':@';
73 // validate scheme (MUST BE FIRST!)
74 if (!is_null($this->scheme
) && is_null($this->host
)) {
75 $def = $config->getDefinition('URI');
76 if ($def->defaultScheme
=== $this->scheme
) {
82 if (!is_null($this->host
)) {
83 $host_def = new HTMLPurifier_AttrDef_URI_Host();
84 $this->host
= $host_def->validate($this->host
, $config, $context);
85 if ($this->host
=== false) $this->host
= null;
89 if (!is_null($this->userinfo
)) {
90 $encoder = new HTMLPurifier_PercentEncoder($chars_sub_delims . ':');
91 $this->userinfo
= $encoder->encode($this->userinfo
);
95 if (!is_null($this->port
)) {
96 if ($this->port
< 1 ||
$this->port
> 65535) $this->port
= null;
100 $path_parts = array();
101 $segments_encoder = new HTMLPurifier_PercentEncoder($chars_pchar . '/');
102 if (!is_null($this->host
)) {
103 // path-abempty (hier and relative)
104 $this->path
= $segments_encoder->encode($this->path
);
105 } elseif ($this->path
!== '' && $this->path
[0] === '/') {
106 // path-absolute (hier and relative)
107 if (strlen($this->path
) >= 2 && $this->path
[1] === '/') {
108 // This shouldn't ever happen!
111 $this->path
= $segments_encoder->encode($this->path
);
113 } elseif (!is_null($this->scheme
) && $this->path
!== '') {
114 // path-rootless (hier)
115 // Short circuit evaluation means we don't need to check nz
116 $this->path
= $segments_encoder->encode($this->path
);
117 } elseif (is_null($this->scheme
) && $this->path
!== '') {
118 // path-noscheme (relative)
119 // (once again, not checking nz)
120 $segment_nc_encoder = new HTMLPurifier_PercentEncoder($chars_sub_delims . '@');
121 $c = strpos($this->path
, '/');
124 $segment_nc_encoder->encode(substr($this->path
, 0, $c)) .
125 $segments_encoder->encode(substr($this->path
, $c));
127 $this->path
= $segment_nc_encoder->encode($this->path
);
130 // path-empty (hier and relative)
131 $this->path
= ''; // just to be safe
139 * Convert URI back to string
140 * @return String URI appropriate for output
142 function toString() {
143 // reconstruct authority
145 if (!is_null($this->host
)) {
147 if(!is_null($this->userinfo
)) $authority .= $this->userinfo
. '@';
148 $authority .= $this->host
;
149 if(!is_null($this->port
)) $authority .= ':' . $this->port
;
152 // reconstruct the result
154 if (!is_null($this->scheme
)) $result .= $this->scheme
. ':';
155 if (!is_null($authority)) $result .= '//' . $authority;
156 $result .= $this->path
;
157 if (!is_null($this->query
)) $result .= '?' . $this->query
;
158 if (!is_null($this->fragment
)) $result .= '#' . $this->fragment
;
164 * Returns a copy of the URI object
167 return unserialize(serialize($this));