25 #require_once 'Zend/Uri.php'; 30 #require_once 'Zend/Validate/Hostname.php'; 46 const CHAR_ALNUM =
'A-Za-z0-9';
47 const CHAR_MARK =
'-_.!~*\'()\[\]';
48 const CHAR_RESERVED =
';\/?:@&=+$,';
49 const CHAR_SEGMENT =
':@&=+$,;';
50 const CHAR_UNWISE =
'{}|\\\\^`';
57 protected $_username =
'';
64 protected $_password =
'';
71 protected $_host =
'';
78 protected $_port =
'';
85 protected $_path =
'';
92 protected $_query =
'';
99 protected $_fragment =
'';
106 protected $_regex = array();
116 protected function __construct($scheme, $schemeSpecific =
'')
119 $this->_scheme = $scheme;
125 $this->_regex[
'escaped'] =
'%[[:xdigit:]]{2}';
128 $this->_regex[
'unreserved'] =
'[' . self::CHAR_ALNUM . self::CHAR_MARK .
']';
131 $this->_regex[
'segment'] =
'(?:' . $this->_regex[
'escaped'] .
'|[' .
132 self::CHAR_ALNUM . self::CHAR_MARK . self::CHAR_SEGMENT .
'])*';
135 $this->_regex[
'path'] =
'(?:\/(?:' . $this->_regex[
'segment'] .
')?)+';
138 $this->_regex[
'uric'] =
'(?:' . $this->_regex[
'escaped'] .
'|[' .
139 self::CHAR_ALNUM . self::CHAR_MARK . self::CHAR_RESERVED .
142 (self::$_config[
'allow_unwise'] ? self::CHAR_UNWISE :
'') .
'])';
146 if (strlen($schemeSpecific) === 0) {
151 $this->_parseUri($schemeSpecific);
154 if ($this->
valid() ===
false) {
155 #require_once 'Zend/Uri/Exception.php'; 170 public static function fromString($uri)
172 if (is_string($uri) ===
false) {
173 #require_once 'Zend/Uri/Exception.php'; 177 $uri = explode(
':', $uri, 2);
178 $scheme = strtolower($uri[0]);
179 $schemeSpecific = isset($uri[1]) ===
true ? $uri[1] :
'';
181 if (in_array($scheme, array(
'http',
'https')) ===
false) {
182 #require_once 'Zend/Uri/Exception.php'; 186 $schemeHandler =
new Zend_Uri_Http($scheme, $schemeSpecific);
187 return $schemeHandler;
198 protected function _parseUri($schemeSpecific)
201 $pattern =
'~^((//)([^/?#]*))([^?#]*)(\?([^#]*))?(#(.*))?$~';
204 #require_once 'Zend/Uri/Exception.php'; 214 $this->_path = isset($matches[4]) ===
true ? $matches[4] :
'';
215 $this->_query = isset($matches[6]) ===
true ? $matches[6] :
'';
216 $this->_fragment = isset($matches[8]) ===
true ? $matches[8] :
'';
219 $combo = isset($matches[3]) ===
true ? $matches[3] :
'';
220 $pattern =
'~^(([^:@]*)(:([^@]*))?@)?((?(?=[[])[[][^]]+[]]|[^:]+))(:(.*))?$~';
223 #require_once 'Zend/Uri/Exception.php'; 228 $this->_username = isset($matches[2]) ===
true ? $matches[2] :
'';
229 $this->_password = isset($matches[4]) ===
true ? $matches[4] :
'';
230 $this->_host = isset($matches[5]) ===
true 231 ? preg_replace(
'~^\[([^]]+)\]$~',
'\1', $matches[5])
233 $this->_port = isset($matches[7]) ===
true ? $matches[7] :
'';
245 if ($this->
valid() ===
false) {
246 #require_once 'Zend/Uri/Exception.php'; 250 $password = strlen($this->_password) > 0 ?
":$this->_password" :
'';
251 $auth = strlen($this->_username) > 0 ?
"$this->_username$password@" :
'';
252 $port = strlen($this->_port) > 0 ?
":$this->_port" :
'';
253 $query = strlen($this->_query) > 0 ?
"?$this->_query" :
'';
254 $fragment = strlen($this->_fragment) > 0 ?
"#$this->_fragment" :
'';
256 return $this->_scheme
272 public function valid()
275 return $this->validateUsername()
276 and $this->validatePassword()
277 and $this->validateHost()
278 and $this->validatePort()
279 and $this->validatePath()
280 and $this->validateQuery()
281 and $this->validateFragment();
291 return strlen($this->_username) > 0 ? $this->_username :
false;
303 public function validateUsername($username =
null)
305 if ($username ===
null) {
306 $username = $this->_username;
310 if (strlen($username) === 0) {
315 $status = @preg_match(
'/^(?:' . $this->_regex[
'escaped'] .
'|[' .
316 self::CHAR_ALNUM . self::CHAR_MARK .
';:&=+$,' .
'])+$/', $username);
319 #require_once 'Zend/Uri/Exception.php'; 333 public function setUsername($username)
335 if ($this->validateUsername($username) ===
false) {
336 #require_once 'Zend/Uri/Exception.php'; 340 $oldUsername = $this->_username;
341 $this->_username = $username;
351 public function getPassword()
353 return strlen($this->_password) > 0 ? $this->_password :
false;
365 public function validatePassword($password =
null)
367 if ($password ===
null) {
368 $password = $this->_password;
372 if (strlen($password) === 0) {
377 if (strlen($password) > 0 and strlen($this->_username) === 0) {
382 $status = @preg_match(
'/^(?:' . $this->_regex[
'escaped'] .
'|[' .
383 self::CHAR_ALNUM . self::CHAR_MARK .
';:&=+$,' .
'])+$/', $password);
386 #require_once 'Zend/Uri/Exception.php'; 400 public function setPassword($password)
402 if ($this->validatePassword($password) ===
false) {
403 #require_once 'Zend/Uri/Exception.php'; 407 $oldPassword = $this->_password;
408 $this->_password = $password;
418 public function getHost()
420 return strlen($this->_host) > 0 ? $this->_host :
false;
431 public function validateHost($host =
null)
433 if ($host ===
null) {
434 $host = $this->_host;
438 if (strlen($host) === 0) {
445 return $validate->isValid($host);
455 public function setHost($host)
457 if ($this->validateHost($host) ===
false) {
458 #require_once 'Zend/Uri/Exception.php'; 462 $oldHost = $this->_host;
463 $this->_host = $host;
473 public function getPort()
475 return strlen($this->_port) > 0 ? $this->_port :
false;
485 public function validatePort($port =
null)
487 if ($port ===
null) {
488 $port = $this->_port;
492 if (strlen($port) === 0) {
497 return ctype_digit((
string) $port) and 1 <= $port and $port <= 65535;
507 public function setPort($port)
509 if ($this->validatePort($port) ===
false) {
510 #require_once 'Zend/Uri/Exception.php'; 514 $oldPort = $this->_port;
515 $this->_port = $port;
525 public function getPath()
527 return strlen($this->_path) > 0 ? $this->_path :
'/';
538 public function validatePath(
$path =
null)
540 if (
$path ===
null) {
541 $path = $this->_path;
545 if (strlen(
$path) === 0) {
550 $pattern =
'/^' . $this->_regex[
'path'] .
'$/';
553 #require_once 'Zend/Uri/Exception.php'; 567 public function setPath(
$path)
569 if ($this->validatePath(
$path) ===
false) {
570 #require_once 'Zend/Uri/Exception.php'; 574 $oldPath = $this->_path;
575 $this->_path =
$path;
585 public function getQuery()
587 return strlen($this->_query) > 0 ? $this->_query :
false;
597 public function getQueryAsArray()
599 $query = $this->getQuery();
600 $querryArray = array();
602 parse_str(
$query, $querryArray);
616 public function validateQuery(
$query =
null)
623 if (strlen(
$query) === 0) {
628 $pattern =
'/^' . $this->_regex[
'uric'] .
'*$/';
631 #require_once 'Zend/Uri/Exception.php'; 645 public function addReplaceQueryParameters(array $queryParams)
647 $queryParams = array_merge($this->getQueryAsArray(), $queryParams);
648 return $this->setQuery($queryParams);
658 public function removeQueryParameters(array $queryParamKeys)
660 $queryParams = array_diff_key($this->getQueryAsArray(), array_fill_keys($queryParamKeys, 0));
661 return $this->setQuery($queryParams);
672 public function setQuery(
$query)
674 $oldQuery = $this->_query;
677 if (empty(
$query) ===
true) {
683 if (is_array(
$query) ===
true) {
688 if ($this->validateQuery(
$query) ===
false) {
689 parse_str(
$query, $queryArray);
690 $query = http_build_query($queryArray,
'',
'&');
695 if ($this->validateQuery(
$query) ===
false) {
696 #require_once 'Zend/Uri/Exception.php'; 710 public function getFragment()
712 return strlen($this->_fragment) > 0 ? $this->_fragment :
false;
724 public function validateFragment($fragment =
null)
726 if ($fragment ===
null) {
727 $fragment = $this->_fragment;
731 if (strlen($fragment) === 0) {
736 $pattern =
'/^' . $this->_regex[
'uric'] .
'*$/';
739 #require_once 'Zend/Uri/Exception.php'; 753 public function setFragment($fragment)
755 if ($this->validateFragment($fragment) ===
false) {
756 #require_once 'Zend/Uri/Exception.php'; 760 $oldFragment = $this->_fragment;
761 $this->_fragment = $fragment;
__construct($scheme, $schemeSpecific='')