00001 <?php 00002 00011 class HTMLPurifier_URI 00012 { 00013 00014 public $scheme, $userinfo, $host, $port, $path, $query, $fragment; 00015 00019 public function __construct($scheme, $userinfo, $host, $port, $path, $query, $fragment) { 00020 $this->scheme = is_null($scheme) || ctype_lower($scheme) ? $scheme : strtolower($scheme); 00021 $this->userinfo = $userinfo; 00022 $this->host = $host; 00023 $this->port = is_null($port) ? $port : (int) $port; 00024 $this->path = $path; 00025 $this->query = $query; 00026 $this->fragment = $fragment; 00027 } 00028 00035 public function getSchemeObj($config, $context) { 00036 $registry = HTMLPurifier_URISchemeRegistry::instance(); 00037 if ($this->scheme !== null) { 00038 $scheme_obj = $registry->getScheme($this->scheme, $config, $context); 00039 if (!$scheme_obj) return false; // invalid scheme, clean it out 00040 } else { 00041 // no scheme: retrieve the default one 00042 $def = $config->getDefinition('URI'); 00043 $scheme_obj = $registry->getScheme($def->defaultScheme, $config, $context); 00044 if (!$scheme_obj) { 00045 // something funky happened to the default scheme object 00046 trigger_error( 00047 'Default scheme object "' . $def->defaultScheme . '" was not readable', 00048 E_USER_WARNING 00049 ); 00050 return false; 00051 } 00052 } 00053 return $scheme_obj; 00054 } 00055 00063 public function validate($config, $context) { 00064 00065 // ABNF definitions from RFC 3986 00066 $chars_sub_delims = '!$&\'()*+,;='; 00067 $chars_gen_delims = ':/?#[]@'; 00068 $chars_pchar = $chars_sub_delims . ':@'; 00069 00070 // validate scheme (MUST BE FIRST!) 00071 if (!is_null($this->scheme) && is_null($this->host)) { 00072 $def = $config->getDefinition('URI'); 00073 if ($def->defaultScheme === $this->scheme) { 00074 $this->scheme = null; 00075 } 00076 } 00077 00078 // validate host 00079 if (!is_null($this->host)) { 00080 $host_def = new HTMLPurifier_AttrDef_URI_Host(); 00081 $this->host = $host_def->validate($this->host, $config, $context); 00082 if ($this->host === false) $this->host = null; 00083 } 00084 00085 // validate username 00086 if (!is_null($this->userinfo)) { 00087 $encoder = new HTMLPurifier_PercentEncoder($chars_sub_delims . ':'); 00088 $this->userinfo = $encoder->encode($this->userinfo); 00089 } 00090 00091 // validate port 00092 if (!is_null($this->port)) { 00093 if ($this->port < 1 || $this->port > 65535) $this->port = null; 00094 } 00095 00096 // validate path 00097 $path_parts = array(); 00098 $segments_encoder = new HTMLPurifier_PercentEncoder($chars_pchar . '/'); 00099 if (!is_null($this->host)) { 00100 // path-abempty (hier and relative) 00101 $this->path = $segments_encoder->encode($this->path); 00102 } elseif ($this->path !== '' && $this->path[0] === '/') { 00103 // path-absolute (hier and relative) 00104 if (strlen($this->path) >= 2 && $this->path[1] === '/') { 00105 // This shouldn't ever happen! 00106 $this->path = ''; 00107 } else { 00108 $this->path = $segments_encoder->encode($this->path); 00109 } 00110 } elseif (!is_null($this->scheme) && $this->path !== '') { 00111 // path-rootless (hier) 00112 // Short circuit evaluation means we don't need to check nz 00113 $this->path = $segments_encoder->encode($this->path); 00114 } elseif (is_null($this->scheme) && $this->path !== '') { 00115 // path-noscheme (relative) 00116 // (once again, not checking nz) 00117 $segment_nc_encoder = new HTMLPurifier_PercentEncoder($chars_sub_delims . '@'); 00118 $c = strpos($this->path, '/'); 00119 if ($c !== false) { 00120 $this->path = 00121 $segment_nc_encoder->encode(substr($this->path, 0, $c)) . 00122 $segments_encoder->encode(substr($this->path, $c)); 00123 } else { 00124 $this->path = $segment_nc_encoder->encode($this->path); 00125 } 00126 } else { 00127 // path-empty (hier and relative) 00128 $this->path = ''; // just to be safe 00129 } 00130 00131 // qf = query and fragment 00132 $qf_encoder = new HTMLPurifier_PercentEncoder($chars_pchar . '/?'); 00133 00134 if (!is_null($this->query)) { 00135 $this->query = $qf_encoder->encode($this->query); 00136 } 00137 00138 if (!is_null($this->fragment)) { 00139 $this->fragment = $qf_encoder->encode($this->fragment); 00140 } 00141 00142 return true; 00143 00144 } 00145 00150 public function toString() { 00151 // reconstruct authority 00152 $authority = null; 00153 if (!is_null($this->host)) { 00154 $authority = ''; 00155 if(!is_null($this->userinfo)) $authority .= $this->userinfo . '@'; 00156 $authority .= $this->host; 00157 if(!is_null($this->port)) $authority .= ':' . $this->port; 00158 } 00159 00160 // reconstruct the result 00161 $result = ''; 00162 if (!is_null($this->scheme)) $result .= $this->scheme . ':'; 00163 if (!is_null($authority)) $result .= '//' . $authority; 00164 $result .= $this->path; 00165 if (!is_null($this->query)) $result .= '?' . $this->query; 00166 if (!is_null($this->fragment)) $result .= '#' . $this->fragment; 00167 00168 return $result; 00169 } 00170 00171 } 00172 00173 // vim: et sw=4 sts=4
| Copyright © 2003 - 2009 MyOOS [Shopsystem]. All rights reserved. MyOOS [Shopsystem] is Free Software released under the GNU/GPL License. Webmaster: info@r23.de (Impressum) |
|
