Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Uri / Uri.php
blob32e7be5fc303c3bca87606fa7705a6e247aa58ad
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Uri;
12 use Zend\Escaper\Escaper;
13 use Zend\Validator;
15 /**
16 * Generic URI handler
18 class Uri implements UriInterface
20 /**
21 * Character classes defined in RFC-3986
23 const CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~';
24 const CHAR_GEN_DELIMS = ':\/\?#\[\]@';
25 const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;=';
26 const CHAR_RESERVED = ':\/\?#\[\]@!\$&\'\(\)\*\+,;=';
28 /**
29 * Host part types represented as binary masks
30 * The binary mask consists of 5 bits in the following order:
31 * <RegName> | <DNS> | <IPvFuture> | <IPv6> | <IPv4>
32 * Place 1 or 0 in the different positions for enable or disable the part.
33 * Finally use a hexadecimal representation.
35 const HOST_IPV4 = 0x01; //00001
36 const HOST_IPV6 = 0x02; //00010
37 const HOST_IPVFUTURE = 0x04; //00100
38 const HOST_IPVANY = 0x07; //00111
39 const HOST_DNS = 0x08; //01000
40 const HOST_DNS_OR_IPV4 = 0x09; //01001
41 const HOST_DNS_OR_IPV6 = 0x0A; //01010
42 const HOST_DNS_OR_IPV4_OR_IPV6 = 0x0B; //01011
43 const HOST_DNS_OR_IPVANY = 0x0F; //01111
44 const HOST_REGNAME = 0x10; //10000
45 const HOST_DNS_OR_IPV4_OR_IPV6_OR_REGNAME = 0x13; //10011
46 const HOST_ALL = 0x1F; //11111
48 /**
49 * URI scheme
51 * @var string
53 protected $scheme;
55 /**
56 * URI userInfo part (usually user:password in HTTP URLs)
58 * @var string
60 protected $userInfo;
62 /**
63 * URI hostname
65 * @var string
67 protected $host;
69 /**
70 * URI port
72 * @var int
74 protected $port;
76 /**
77 * URI path
79 * @var string
81 protected $path;
83 /**
84 * URI query string
86 * @var string
88 protected $query;
90 /**
91 * URI fragment
93 * @var string
95 protected $fragment;
97 /**
98 * Which host part types are valid for this URI?
100 * @var int
102 protected $validHostTypes = self::HOST_ALL;
105 * Array of valid schemes.
107 * Subclasses of this class that only accept specific schemes may set the
108 * list of accepted schemes here. If not empty, when setScheme() is called
109 * it will only accept the schemes listed here.
111 * @var array
113 protected static $validSchemes = array();
116 * List of default ports per scheme
118 * Inheriting URI classes may set this, and the normalization methods will
119 * automatically remove the port if it is equal to the default port for the
120 * current scheme
122 * @var array
124 protected static $defaultPorts = array();
127 * @var Escaper
129 protected static $escaper;
132 * Create a new URI object
134 * @param Uri|string|null $uri
135 * @throws Exception\InvalidArgumentException
137 public function __construct($uri = null)
139 if (is_string($uri)) {
140 $this->parse($uri);
141 } elseif ($uri instanceof UriInterface) {
142 // Copy constructor
143 $this->setScheme($uri->getScheme());
144 $this->setUserInfo($uri->getUserInfo());
145 $this->setHost($uri->getHost());
146 $this->setPort($uri->getPort());
147 $this->setPath($uri->getPath());
148 $this->setQuery($uri->getQuery());
149 $this->setFragment($uri->getFragment());
150 } elseif ($uri !== null) {
151 throw new Exception\InvalidArgumentException(sprintf(
152 'Expecting a string or a URI object, received "%s"',
153 (is_object($uri) ? get_class($uri) : gettype($uri))
159 * Set Escaper instance
161 * @param Escaper $escaper
163 public static function setEscaper(Escaper $escaper)
165 static::$escaper = $escaper;
169 * Retrieve Escaper instance
171 * Lazy-loads one if none provided
173 * @return Escaper
175 public static function getEscaper()
177 if (null === static::$escaper) {
178 static::setEscaper(new Escaper());
180 return static::$escaper;
184 * Check if the URI is valid
186 * Note that a relative URI may still be valid
188 * @return bool
190 public function isValid()
192 if ($this->host) {
193 if (strlen($this->path) > 0 && substr($this->path, 0, 1) != '/') {
194 return false;
196 return true;
199 if ($this->userInfo || $this->port) {
200 return false;
203 if ($this->path) {
204 // Check path-only (no host) URI
205 if (substr($this->path, 0, 2) == '//') {
206 return false;
208 return true;
211 if (! ($this->query || $this->fragment)) {
212 // No host, path, query or fragment - this is not a valid URI
213 return false;
216 return true;
220 * Check if the URI is a valid relative URI
222 * @return bool
224 public function isValidRelative()
226 if ($this->scheme || $this->host || $this->userInfo || $this->port) {
227 return false;
230 if ($this->path) {
231 // Check path-only (no host) URI
232 if (substr($this->path, 0, 2) == '//') {
233 return false;
235 return true;
238 if (! ($this->query || $this->fragment)) {
239 // No host, path, query or fragment - this is not a valid URI
240 return false;
243 return true;
247 * Check if the URI is an absolute or relative URI
249 * @return bool
251 public function isAbsolute()
253 return ($this->scheme !== null);
257 * Reset URI parts
259 protected function reset()
261 $this->setScheme(null);
262 $this->setPort(null);
263 $this->setUserInfo(null);
264 $this->setHost(null);
265 $this->setPath(null);
266 $this->setFragment(null);
267 $this->setQuery(null);
271 * Parse a URI string
273 * @param string $uri
274 * @return Uri
276 public function parse($uri)
278 $this->reset();
280 // Capture scheme
281 if (($scheme = self::parseScheme($uri)) !== null) {
282 $this->setScheme($scheme);
283 $uri = substr($uri, strlen($scheme) + 1);
286 // Capture authority part
287 if (preg_match('|^//([^/\?#]*)|', $uri, $match)) {
288 $authority = $match[1];
289 $uri = substr($uri, strlen($match[0]));
291 // Split authority into userInfo and host
292 if (strpos($authority, '@') !== false) {
293 // The userInfo can also contain '@' symbols; split $authority
294 // into segments, and set it to the last segment.
295 $segments = explode('@', $authority);
296 $authority = array_pop($segments);
297 $userInfo = implode('@', $segments);
298 unset($segments);
299 $this->setUserInfo($userInfo);
302 $nMatches = preg_match('/:[\d]{1,5}$/', $authority, $matches);
303 if ($nMatches === 1) {
304 $portLength = strlen($matches[0]);
305 $port = substr($matches[0], 1);
307 $this->setPort((int) $port);
308 $authority = substr($authority, 0, -$portLength);
311 $this->setHost($authority);
314 if (!$uri) {
315 return $this;
318 // Capture the path
319 if (preg_match('|^[^\?#]*|', $uri, $match)) {
320 $this->setPath($match[0]);
321 $uri = substr($uri, strlen($match[0]));
324 if (!$uri) {
325 return $this;
328 // Capture the query
329 if (preg_match('|^\?([^#]*)|', $uri, $match)) {
330 $this->setQuery($match[1]);
331 $uri = substr($uri, strlen($match[0]));
333 if (!$uri) {
334 return $this;
337 // All that's left is the fragment
338 if ($uri && substr($uri, 0, 1) == '#') {
339 $this->setFragment(substr($uri, 1));
342 return $this;
346 * Compose the URI into a string
348 * @return string
349 * @throws Exception\InvalidUriException
351 public function toString()
353 if (!$this->isValid()) {
354 if ($this->isAbsolute() || !$this->isValidRelative()) {
355 throw new Exception\InvalidUriException(
356 'URI is not valid and cannot be converted into a string'
361 $uri = '';
363 if ($this->scheme) {
364 $uri .= $this->scheme . ':';
367 if ($this->host !== null) {
368 $uri .= '//';
369 if ($this->userInfo) {
370 $uri .= $this->userInfo . '@';
372 $uri .= $this->host;
373 if ($this->port) {
374 $uri .= ':' . $this->port;
378 if ($this->path) {
379 $uri .= static::encodePath($this->path);
380 } elseif ($this->host && ($this->query || $this->fragment)) {
381 $uri .= '/';
384 if ($this->query) {
385 $uri .= "?" . static::encodeQueryFragment($this->query);
388 if ($this->fragment) {
389 $uri .= "#" . static::encodeQueryFragment($this->fragment);
392 return $uri;
396 * Normalize the URI
398 * Normalizing a URI includes removing any redundant parent directory or
399 * current directory references from the path (e.g. foo/bar/../baz becomes
400 * foo/baz), normalizing the scheme case, decoding any over-encoded
401 * characters etc.
403 * Eventually, two normalized URLs pointing to the same resource should be
404 * equal even if they were originally represented by two different strings
406 * @return Uri
408 public function normalize()
410 if ($this->scheme) {
411 $this->scheme = static::normalizeScheme($this->scheme);
414 if ($this->host) {
415 $this->host = static::normalizeHost($this->host);
418 if ($this->port) {
419 $this->port = static::normalizePort($this->port, $this->scheme);
422 if ($this->path) {
423 $this->path = static::normalizePath($this->path);
426 if ($this->query) {
427 $this->query = static::normalizeQuery($this->query);
430 if ($this->fragment) {
431 $this->fragment = static::normalizeFragment($this->fragment);
434 // If path is empty (and we have a host), path should be '/'
435 // Isn't this valid ONLY for HTTP-URI?
436 if ($this->host && empty($this->path)) {
437 $this->path = '/';
440 return $this;
444 * Convert a relative URI into an absolute URI using a base absolute URI as
445 * a reference.
447 * This is similar to merge() - only it uses the supplied URI as the
448 * base reference instead of using the current URI as the base reference.
450 * Merging algorithm is adapted from RFC-3986 section 5.2
451 * (@link http://tools.ietf.org/html/rfc3986#section-5.2)
453 * @param Uri|string $baseUri
454 * @throws Exception\InvalidArgumentException
455 * @return Uri
457 public function resolve($baseUri)
459 // Ignore if URI is absolute
460 if ($this->isAbsolute()) {
461 return $this;
464 if (is_string($baseUri)) {
465 $baseUri = new static($baseUri);
466 } elseif (!$baseUri instanceof Uri) {
467 throw new Exception\InvalidArgumentException(
468 'Provided base URI must be a string or a Uri object'
472 // Merging starts here...
473 if ($this->getHost()) {
474 $this->setPath(static::removePathDotSegments($this->getPath()));
475 } else {
476 $basePath = $baseUri->getPath();
477 $relPath = $this->getPath();
478 if (!$relPath) {
479 $this->setPath($basePath);
480 if (!$this->getQuery()) {
481 $this->setQuery($baseUri->getQuery());
483 } else {
484 if (substr($relPath, 0, 1) == '/') {
485 $this->setPath(static::removePathDotSegments($relPath));
486 } else {
487 if ($baseUri->getHost() && !$basePath) {
488 $mergedPath = '/';
489 } else {
490 $mergedPath = substr($basePath, 0, strrpos($basePath, '/') + 1);
492 $this->setPath(static::removePathDotSegments($mergedPath . $relPath));
496 // Set the authority part
497 $this->setUserInfo($baseUri->getUserInfo());
498 $this->setHost($baseUri->getHost());
499 $this->setPort($baseUri->getPort());
502 $this->setScheme($baseUri->getScheme());
503 return $this;
508 * Convert the link to a relative link by substracting a base URI
510 * This is the opposite of resolving a relative link - i.e. creating a
511 * relative reference link from an original URI and a base URI.
513 * If the two URIs do not intersect (e.g. the original URI is not in any
514 * way related to the base URI) the URI will not be modified.
516 * @param Uri|string $baseUri
517 * @return Uri
519 public function makeRelative($baseUri)
521 // Copy base URI, we should not modify it
522 $baseUri = new static($baseUri);
524 $this->normalize();
525 $baseUri->normalize();
527 $host = $this->getHost();
528 $baseHost = $baseUri->getHost();
529 if ($host && $baseHost && ($host != $baseHost)) {
530 // Not the same hostname
531 return $this;
534 $port = $this->getPort();
535 $basePort = $baseUri->getPort();
536 if ($port && $basePort && ($port != $basePort)) {
537 // Not the same port
538 return $this;
541 $scheme = $this->getScheme();
542 $baseScheme = $baseUri->getScheme();
543 if ($scheme && $baseScheme && ($scheme != $baseScheme)) {
544 // Not the same scheme (e.g. HTTP vs. HTTPS)
545 return $this;
548 // Remove host, port and scheme
549 $this->setHost(null)
550 ->setPort(null)
551 ->setScheme(null);
553 // Is path the same?
554 if ($this->getPath() == $baseUri->getPath()) {
555 $this->setPath('');
556 return $this;
559 $pathParts = preg_split('|(/)|', $this->getPath(), null,
560 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
561 $baseParts = preg_split('|(/)|', $baseUri->getPath(), null,
562 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
564 // Get the intersection of existing path parts and those from the
565 // provided URI
566 $matchingParts = array_intersect_assoc($pathParts, $baseParts);
568 // Loop through the matches
569 foreach ($matchingParts as $index => $segment) {
570 // If we skip an index at any point, we have parent traversal, and
571 // need to prepend the path accordingly
572 if ($index && !isset($matchingParts[$index - 1])) {
573 array_unshift($pathParts, '../');
574 continue;
577 // Otherwise, we simply unset the given path segment
578 unset($pathParts[$index]);
581 // Reset the path by imploding path segments
582 $this->setPath(implode($pathParts));
584 return $this;
588 * Get the scheme part of the URI
590 * @return string|null
592 public function getScheme()
594 return $this->scheme;
598 * Get the User-info (usually user:password) part
600 * @return string|null
602 public function getUserInfo()
604 return $this->userInfo;
608 * Get the URI host
610 * @return string|null
612 public function getHost()
614 return $this->host;
618 * Get the URI port
620 * @return int|null
622 public function getPort()
624 return $this->port;
628 * Get the URI path
630 * @return string|null
632 public function getPath()
634 return $this->path;
638 * Get the URI query
640 * @return string|null
642 public function getQuery()
644 return $this->query;
648 * Return the query string as an associative array of key => value pairs
650 * This is an extension to RFC-3986 but is quite useful when working with
651 * most common URI types
653 * @return array
655 public function getQueryAsArray()
657 $query = array();
658 if ($this->query) {
659 parse_str($this->query, $query);
662 return $query;
666 * Get the URI fragment
668 * @return string|null
670 public function getFragment()
672 return $this->fragment;
676 * Set the URI scheme
678 * If the scheme is not valid according to the generic scheme syntax or
679 * is not acceptable by the specific URI class (e.g. 'http' or 'https' are
680 * the only acceptable schemes for the Zend\Uri\Http class) an exception
681 * will be thrown.
683 * You can check if a scheme is valid before setting it using the
684 * validateScheme() method.
686 * @param string $scheme
687 * @throws Exception\InvalidUriPartException
688 * @return Uri
690 public function setScheme($scheme)
692 if (($scheme !== null) && (!self::validateScheme($scheme))) {
693 throw new Exception\InvalidUriPartException(sprintf(
694 'Scheme "%s" is not valid or is not accepted by %s',
695 $scheme,
696 get_class($this)
697 ), Exception\InvalidUriPartException::INVALID_SCHEME);
700 $this->scheme = $scheme;
701 return $this;
705 * Set the URI User-info part (usually user:password)
707 * @param string $userInfo
708 * @return Uri
709 * @throws Exception\InvalidUriPartException If the schema definition
710 * does not have this part
712 public function setUserInfo($userInfo)
714 $this->userInfo = $userInfo;
715 return $this;
719 * Set the URI host
721 * Note that the generic syntax for URIs allows using host names which
722 * are not necessarily IPv4 addresses or valid DNS host names. For example,
723 * IPv6 addresses are allowed as well, and also an abstract "registered name"
724 * which may be any name composed of a valid set of characters, including,
725 * for example, tilda (~) and underscore (_) which are not allowed in DNS
726 * names.
728 * Subclasses of Uri may impose more strict validation of host names - for
729 * example the HTTP RFC clearly states that only IPv4 and valid DNS names
730 * are allowed in HTTP URIs.
732 * @param string $host
733 * @throws Exception\InvalidUriPartException
734 * @return Uri
736 public function setHost($host)
738 if (($host !== '')
739 && ($host !== null)
740 && !self::validateHost($host, $this->validHostTypes)
742 throw new Exception\InvalidUriPartException(sprintf(
743 'Host "%s" is not valid or is not accepted by %s',
744 $host,
745 get_class($this)
746 ), Exception\InvalidUriPartException::INVALID_HOSTNAME);
749 $this->host = $host;
750 return $this;
754 * Set the port part of the URI
756 * @param int $port
757 * @return Uri
759 public function setPort($port)
761 $this->port = $port;
762 return $this;
766 * Set the path
768 * @param string $path
769 * @return Uri
771 public function setPath($path)
773 $this->path = $path;
774 return $this;
778 * Set the query string
780 * If an array is provided, will encode this array of parameters into a
781 * query string. Array values will be represented in the query string using
782 * PHP's common square bracket notation.
784 * @param string|array $query
785 * @return Uri
787 public function setQuery($query)
789 if (is_array($query)) {
790 // We replace the + used for spaces by http_build_query with the
791 // more standard %20.
792 $query = str_replace('+', '%20', http_build_query($query));
795 $this->query = $query;
796 return $this;
800 * Set the URI fragment part
802 * @param string $fragment
803 * @return Uri
804 * @throws Exception\InvalidUriPartException If the schema definition
805 * does not have this part
807 public function setFragment($fragment)
809 $this->fragment = $fragment;
810 return $this;
814 * Magic method to convert the URI to a string
816 * @return string
818 public function __toString()
820 try {
821 return $this->toString();
822 } catch (\Exception $e) {
823 return '';
828 * Encoding and Validation Methods
832 * Check if a scheme is valid or not
834 * Will check $scheme to be valid against the generic scheme syntax defined
835 * in RFC-3986. If the class also defines specific acceptable schemes, will
836 * also check that $scheme is one of them.
838 * @param string $scheme
839 * @return bool
841 public static function validateScheme($scheme)
843 if (!empty(static::$validSchemes)
844 && !in_array(strtolower($scheme), static::$validSchemes)
846 return false;
849 return (bool) preg_match('/^[A-Za-z][A-Za-z0-9\-\.+]*$/', $scheme);
853 * Check that the userInfo part of a URI is valid
855 * @param string $userInfo
856 * @return bool
858 public static function validateUserInfo($userInfo)
860 $regex = '/^(?:[' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . ':]+|%[A-Fa-f0-9]{2})*$/';
861 return (bool) preg_match($regex, $userInfo);
865 * Validate the host part
867 * Users may control which host types to allow by passing a second parameter
868 * with a bitmask of HOST_* constants which are allowed. If not specified,
869 * all address types will be allowed.
871 * Note that the generic URI syntax allows different host representations,
872 * including IPv4 addresses, IPv6 addresses and future IP address formats
873 * enclosed in square brackets, and registered names which may be DNS names
874 * or even more complex names. This is different (and is much more loose)
875 * from what is commonly accepted as valid HTTP URLs for example.
877 * @param string $host
878 * @param int $allowed bitmask of allowed host types
879 * @return bool
881 public static function validateHost($host, $allowed = self::HOST_ALL)
884 * "first-match-wins" algorithm (RFC 3986):
885 * If host matches the rule for IPv4address, then it should be
886 * considered an IPv4 address literal and not a reg-name
888 if ($allowed & self::HOST_IPVANY) {
889 if (static::isValidIpAddress($host, $allowed)) {
890 return true;
894 if ($allowed & self::HOST_REGNAME) {
895 if (static::isValidRegName($host)) {
896 return true;
900 if ($allowed & self::HOST_DNS) {
901 if (static::isValidDnsHostname($host)) {
902 return true;
906 return false;
910 * Validate the port
912 * Valid values include numbers between 1 and 65535, and empty values
914 * @param int $port
915 * @return bool
917 public static function validatePort($port)
919 if ($port === 0) {
920 return false;
923 if ($port) {
924 $port = (int) $port;
925 if ($port < 1 || $port > 0xffff) {
926 return false;
930 return true;
934 * Validate the path
936 * @param string $path
937 * @return bool
939 public static function validatePath($path)
941 $pchar = '(?:[' . self::CHAR_UNRESERVED . ':@&=\+\$,]+|%[A-Fa-f0-9]{2})*';
942 $segment = $pchar . "(?:;{$pchar})*";
943 $regex = "/^{$segment}(?:\/{$segment})*$/";
944 return (bool) preg_match($regex, $path);
948 * Check if a URI query or fragment part is valid or not
950 * Query and Fragment parts are both restricted by the same syntax rules,
951 * so the same validation method can be used for both.
953 * You can encode a query or fragment part to ensure it is valid by passing
954 * it through the encodeQueryFragment() method.
956 * @param string $input
957 * @return bool
959 public static function validateQueryFragment($input)
961 $regex = '/^(?:[' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . ':@\/\?]+|%[A-Fa-f0-9]{2})*$/';
962 return (bool) preg_match($regex, $input);
966 * URL-encode the user info part of a URI
968 * @param string $userInfo
969 * @return string
970 * @throws Exception\InvalidArgumentException
972 public static function encodeUserInfo($userInfo)
974 if (!is_string($userInfo)) {
975 throw new Exception\InvalidArgumentException(sprintf(
976 'Expecting a string, got %s',
977 (is_object($userInfo) ? get_class($userInfo) : gettype($userInfo))
981 $regex = '/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:]|%(?![A-Fa-f0-9]{2}))/';
982 $escaper = static::getEscaper();
983 $replace = function ($match) use ($escaper) {
984 return $escaper->escapeUrl($match[0]);
987 return preg_replace_callback($regex, $replace, $userInfo);
991 * Encode the path
993 * Will replace all characters which are not strictly allowed in the path
994 * part with percent-encoded representation
996 * @param string $path
997 * @throws Exception\InvalidArgumentException
998 * @return string
1000 public static function encodePath($path)
1002 if (!is_string($path)) {
1003 throw new Exception\InvalidArgumentException(sprintf(
1004 'Expecting a string, got %s',
1005 (is_object($path) ? get_class($path) : gettype($path))
1009 $regex = '/(?:[^' . self::CHAR_UNRESERVED . ':@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/';
1010 $escaper = static::getEscaper();
1011 $replace = function ($match) use ($escaper) {
1012 return $escaper->escapeUrl($match[0]);
1015 return preg_replace_callback($regex, $replace, $path);
1019 * URL-encode a query string or fragment based on RFC-3986 guidelines.
1021 * Note that query and fragment encoding allows more unencoded characters
1022 * than the usual rawurlencode() function would usually return - for example
1023 * '/' and ':' are allowed as literals.
1025 * @param string $input
1026 * @return string
1027 * @throws Exception\InvalidArgumentException
1029 public static function encodeQueryFragment($input)
1031 if (!is_string($input)) {
1032 throw new Exception\InvalidArgumentException(sprintf(
1033 'Expecting a string, got %s',
1034 (is_object($input) ? get_class($input) : gettype($input))
1038 $regex = '/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/\?]+|%(?![A-Fa-f0-9]{2}))/';
1039 $escaper = static::getEscaper();
1040 $replace = function ($match) use ($escaper) {
1041 return $escaper->escapeUrl($match[0]);
1044 return preg_replace_callback($regex, $replace, $input);
1048 * Extract only the scheme part out of a URI string.
1050 * This is used by the parse() method, but is useful as a standalone public
1051 * method if one wants to test a URI string for it's scheme before doing
1052 * anything with it.
1054 * Will return the scheme if found, or NULL if no scheme found (URI may
1055 * still be valid, but not full)
1057 * @param string $uriString
1058 * @throws Exception\InvalidArgumentException
1059 * @return string|null
1061 public static function parseScheme($uriString)
1063 if (! is_string($uriString)) {
1064 throw new Exception\InvalidArgumentException(sprintf(
1065 'Expecting a string, got %s',
1066 (is_object($uriString) ? get_class($uriString) : gettype($uriString))
1070 if (preg_match('/^([A-Za-z][A-Za-z0-9\.\+\-]*):/', $uriString, $match)) {
1071 return $match[1];
1074 return null;
1078 * Remove any extra dot segments (/../, /./) from a path
1080 * Algorithm is adapted from RFC-3986 section 5.2.4
1081 * (@link http://tools.ietf.org/html/rfc3986#section-5.2.4)
1083 * @todo consider optimizing
1085 * @param string $path
1086 * @return string
1088 public static function removePathDotSegments($path)
1090 $output = '';
1092 while ($path) {
1093 if ($path == '..' || $path == '.') {
1094 break;
1097 switch (true) {
1098 case ($path == '/.'):
1099 $path = '/';
1100 break;
1101 case ($path == '/..'):
1102 $path = '/';
1103 $output = substr($output, 0, strrpos($output, '/', -1));
1104 break;
1105 case (substr($path, 0, 4) == '/../'):
1106 $path = '/' . substr($path, 4);
1107 $output = substr($output, 0, strrpos($output, '/', -1));
1108 break;
1109 case (substr($path, 0, 3) == '/./'):
1110 $path = substr($path, 2);
1111 break;
1112 case (substr($path, 0, 2) == './'):
1113 $path = substr($path, 2);
1114 break;
1115 case (substr($path, 0, 3) == '../'):
1116 $path = substr($path, 3);
1117 break;
1118 default:
1119 $slash = strpos($path, '/', 1);
1120 if ($slash === false) {
1121 $seg = $path;
1122 } else {
1123 $seg = substr($path, 0, $slash);
1126 $output .= $seg;
1127 $path = substr($path, strlen($seg));
1128 break;
1132 return $output;
1136 * Merge a base URI and a relative URI into a new URI object
1138 * This convenience method wraps ::resolve() to allow users to quickly
1139 * create new absolute URLs without the need to instantiate and clone
1140 * URI objects.
1142 * If objects are passed in, none of the passed objects will be modified.
1144 * @param Uri|string $baseUri
1145 * @param Uri|string $relativeUri
1146 * @return Uri
1148 public static function merge($baseUri, $relativeUri)
1150 $uri = new static($relativeUri);
1151 return $uri->resolve($baseUri);
1155 * Check if a host name is a valid IP address, depending on allowed IP address types
1157 * @param string $host
1158 * @param int $allowed allowed address types
1159 * @return bool
1161 protected static function isValidIpAddress($host, $allowed)
1163 $validatorParams = array(
1164 'allowipv4' => (bool) ($allowed & self::HOST_IPV4),
1165 'allowipv6' => false,
1166 'allowipvfuture' => false,
1167 'allowliteral' => false,
1170 // Test only IPv4
1171 $validator = new Validator\Ip($validatorParams);
1172 $return = $validator->isValid($host);
1173 if ($return) {
1174 return true;
1177 // IPv6 & IPvLiteral must be in literal format
1178 $validatorParams = array(
1179 'allowipv4' => false,
1180 'allowipv6' => (bool) ($allowed & self::HOST_IPV6),
1181 'allowipvfuture' => (bool) ($allowed & self::HOST_IPVFUTURE),
1182 'allowliteral' => true,
1184 static $regex = '/^\[.*\]$/';
1185 $validator->setOptions($validatorParams);
1186 return (preg_match($regex, $host) && $validator->isValid($host));
1190 * Check if an address is a valid DNS hostname
1192 * @param string $host
1193 * @return bool
1195 protected static function isValidDnsHostname($host)
1197 $validator = new Validator\Hostname(array(
1198 'allow' => Validator\Hostname::ALLOW_DNS | Validator\Hostname::ALLOW_LOCAL,
1201 return $validator->isValid($host);
1205 * Check if an address is a valid registered name (as defined by RFC-3986) address
1207 * @param string $host
1208 * @return bool
1210 protected static function isValidRegName($host)
1212 $regex = '/^(?:[' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . ':@\/\?]+|%[A-Fa-f0-9]{2})+$/';
1213 return (bool) preg_match($regex, $host);
1217 * Part normalization methods
1219 * These are called by normalize() using static::_normalize*() so they may
1220 * be extended or overridden by extending classes to implement additional
1221 * scheme specific normalization rules
1225 * Normalize the scheme
1227 * Usually this means simply converting the scheme to lower case
1229 * @param string $scheme
1230 * @return string
1232 protected static function normalizeScheme($scheme)
1234 return strtolower($scheme);
1238 * Normalize the host part
1240 * By default this converts host names to lower case
1242 * @param string $host
1243 * @return string
1245 protected static function normalizeHost($host)
1247 return strtolower($host);
1251 * Normalize the port
1253 * If the class defines a default port for the current scheme, and the
1254 * current port is default, it will be unset.
1256 * @param int $port
1257 * @param string $scheme
1258 * @return int|null
1260 protected static function normalizePort($port, $scheme = null)
1262 if ($scheme
1263 && isset(static::$defaultPorts[$scheme])
1264 && ($port == static::$defaultPorts[$scheme])
1266 return null;
1269 return $port;
1273 * Normalize the path
1275 * This involves removing redundant dot segments, decoding any over-encoded
1276 * characters and encoding everything that needs to be encoded and is not
1278 * @param string $path
1279 * @return string
1281 protected static function normalizePath($path)
1283 $path = self::encodePath(
1284 self::decodeUrlEncodedChars(
1285 self::removePathDotSegments($path),
1286 '/[' . self::CHAR_UNRESERVED . ':@&=\+\$,\/;%]/'
1290 return $path;
1294 * Normalize the query part
1296 * This involves decoding everything that doesn't need to be encoded, and
1297 * encoding everything else
1299 * @param string $query
1300 * @return string
1302 protected static function normalizeQuery($query)
1304 $query = self::encodeQueryFragment(
1305 self::decodeUrlEncodedChars(
1306 $query,
1307 '/[' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/\?]/'
1311 return $query;
1315 * Normalize the fragment part
1317 * Currently this is exactly the same as normalizeQuery().
1319 * @param string $fragment
1320 * @return string
1322 protected static function normalizeFragment($fragment)
1324 return static::normalizeQuery($fragment);
1328 * Decode all percent encoded characters which are allowed to be represented literally
1330 * Will not decode any characters which are not listed in the 'allowed' list
1332 * @param string $input
1333 * @param string $allowed Pattern of allowed characters
1334 * @return mixed
1336 protected static function decodeUrlEncodedChars($input, $allowed = '')
1338 $decodeCb = function ($match) use ($allowed) {
1339 $char = rawurldecode($match[0]);
1340 if (preg_match($allowed, $char)) {
1341 return $char;
1343 return strtoupper($match[0]);
1346 return preg_replace_callback('/%[A-Fa-f0-9]{2}/', $decodeCb, $input);