2007-08-01 18:34:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/URIParser.php';
|
2007-08-02 01:12:27 +00:00
|
|
|
require_once 'HTMLPurifier/URIFilter.php';
|
2007-08-01 18:34:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* HTML Purifier's internal representation of a URI
|
|
|
|
*/
|
|
|
|
class HTMLPurifier_URI
|
|
|
|
{
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
public $scheme, $userinfo, $host, $port, $path, $query, $fragment;
|
2007-08-01 18:34:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @note Automatically normalizes scheme and port
|
|
|
|
*/
|
2007-11-29 04:29:51 +00:00
|
|
|
public function __construct($scheme, $userinfo, $host, $port, $path, $query, $fragment) {
|
2007-08-01 18:34:46 +00:00
|
|
|
$this->scheme = is_null($scheme) || ctype_lower($scheme) ? $scheme : strtolower($scheme);
|
|
|
|
$this->userinfo = $userinfo;
|
|
|
|
$this->host = $host;
|
|
|
|
$this->port = is_null($port) ? $port : (int) $port;
|
|
|
|
$this->path = $path;
|
|
|
|
$this->query = $query;
|
|
|
|
$this->fragment = $fragment;
|
|
|
|
}
|
|
|
|
|
2007-08-02 01:41:37 +00:00
|
|
|
/**
|
|
|
|
* Retrieves a scheme object corresponding to the URI's scheme/default
|
|
|
|
* @param $config Instance of HTMLPurifier_Config
|
|
|
|
* @param $context Instance of HTMLPurifier_Context
|
|
|
|
* @return Scheme object appropriate for validating this URI
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function getSchemeObj($config, &$context) {
|
2007-08-01 18:34:46 +00:00
|
|
|
$registry =& HTMLPurifier_URISchemeRegistry::instance();
|
|
|
|
if ($this->scheme !== null) {
|
|
|
|
$scheme_obj = $registry->getScheme($this->scheme, $config, $context);
|
|
|
|
if (!$scheme_obj) return false; // invalid scheme, clean it out
|
|
|
|
} else {
|
|
|
|
// no scheme: retrieve the default one
|
2007-08-02 21:47:24 +00:00
|
|
|
$def = $config->getDefinition('URI');
|
|
|
|
$scheme_obj = $registry->getScheme($def->defaultScheme, $config, $context);
|
2007-08-01 18:34:46 +00:00
|
|
|
if (!$scheme_obj) {
|
|
|
|
// something funky happened to the default scheme object
|
|
|
|
trigger_error(
|
2007-08-02 21:47:24 +00:00
|
|
|
'Default scheme object "' . $def->defaultScheme . '" was not readable',
|
2007-08-01 18:34:46 +00:00
|
|
|
E_USER_WARNING
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $scheme_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic validation method applicable for all schemes
|
2007-08-02 01:41:37 +00:00
|
|
|
* @param $config Instance of HTMLPurifier_Config
|
|
|
|
* @param $context Instance of HTMLPurifier_Context
|
|
|
|
* @return True if validation/filtering succeeds, false if failure
|
2007-08-01 18:34:46 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function validate($config, &$context) {
|
2007-08-01 18:34:46 +00:00
|
|
|
|
|
|
|
// validate host
|
|
|
|
if (!is_null($this->host)) {
|
|
|
|
$host_def = new HTMLPurifier_AttrDef_URI_Host();
|
|
|
|
$this->host = $host_def->validate($this->host, $config, $context);
|
|
|
|
if ($this->host === false) $this->host = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate port
|
|
|
|
if (!is_null($this->port)) {
|
|
|
|
if ($this->port < 1 || $this->port > 65535) $this->port = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// query and fragment are quite simple in terms of definition:
|
|
|
|
// *( pchar / "/" / "?" ), so define their validation routines
|
|
|
|
// when we start fixing percent encoding
|
|
|
|
|
|
|
|
// path gets to be validated against a hodge-podge of rules depending
|
|
|
|
// on the status of authority and scheme, but it's not that important,
|
|
|
|
// esp. since it won't be applicable to everyone
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert URI back to string
|
2007-08-02 01:41:37 +00:00
|
|
|
* @return String URI appropriate for output
|
2007-08-01 18:34:46 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function toString() {
|
2007-08-01 18:34:46 +00:00
|
|
|
// reconstruct authority
|
|
|
|
$authority = null;
|
|
|
|
if (!is_null($this->host)) {
|
|
|
|
$authority = '';
|
|
|
|
if(!is_null($this->userinfo)) $authority .= $this->userinfo . '@';
|
|
|
|
$authority .= $this->host;
|
|
|
|
if(!is_null($this->port)) $authority .= ':' . $this->port;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reconstruct the result
|
|
|
|
$result = '';
|
|
|
|
if (!is_null($this->scheme)) $result .= $this->scheme . ':';
|
|
|
|
if (!is_null($authority)) $result .= '//' . $authority;
|
|
|
|
$result .= $this->path;
|
|
|
|
if (!is_null($this->query)) $result .= '?' . $this->query;
|
|
|
|
if (!is_null($this->fragment)) $result .= '#' . $this->fragment;
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2007-08-02 21:47:24 +00:00
|
|
|
/**
|
|
|
|
* Returns a copy of the URI object
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function copy() {
|
2007-08-02 21:47:24 +00:00
|
|
|
return unserialize(serialize($this));
|
|
|
|
}
|
|
|
|
|
2007-08-01 18:34:46 +00:00
|
|
|
}
|
|
|
|
|