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
12 use Zend\Escaper\Escaper
;
18 class Uri
implements UriInterface
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
= ':\/\?#\[\]@!\$&\'\(\)\*\+,;=';
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
56 * URI userInfo part (usually user:password in HTTP URLs)
98 * Which host part types are valid for this URI?
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.
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
124 protected static $defaultPorts = array();
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)) {
141 } elseif ($uri instanceof UriInterface
) {
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
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
190 public function isValid()
193 if (strlen($this->path
) > 0 && substr($this->path
, 0, 1) != '/') {
199 if ($this->userInfo ||
$this->port
) {
204 // Check path-only (no host) URI
205 if (substr($this->path
, 0, 2) == '//') {
211 if (! ($this->query ||
$this->fragment
)) {
212 // No host, path, query or fragment - this is not a valid URI
220 * Check if the URI is a valid relative URI
224 public function isValidRelative()
226 if ($this->scheme ||
$this->host ||
$this->userInfo ||
$this->port
) {
231 // Check path-only (no host) URI
232 if (substr($this->path
, 0, 2) == '//') {
238 if (! ($this->query ||
$this->fragment
)) {
239 // No host, path, query or fragment - this is not a valid URI
247 * Check if the URI is an absolute or relative URI
251 public function isAbsolute()
253 return ($this->scheme
!== null);
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);
276 public function parse($uri)
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);
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);
319 if (preg_match('|^[^\?#]*|', $uri, $match)) {
320 $this->setPath($match[0]);
321 $uri = substr($uri, strlen($match[0]));
329 if (preg_match('|^\?([^#]*)|', $uri, $match)) {
330 $this->setQuery($match[1]);
331 $uri = substr($uri, strlen($match[0]));
337 // All that's left is the fragment
338 if ($uri && substr($uri, 0, 1) == '#') {
339 $this->setFragment(substr($uri, 1));
346 * Compose the URI into a 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'
364 $uri .= $this->scheme
. ':';
367 if ($this->host
!== null) {
369 if ($this->userInfo
) {
370 $uri .= $this->userInfo
. '@';
374 $uri .= ':' . $this->port
;
379 $uri .= static::encodePath($this->path
);
380 } elseif ($this->host
&& ($this->query ||
$this->fragment
)) {
385 $uri .= "?" . static::encodeQueryFragment($this->query
);
388 if ($this->fragment
) {
389 $uri .= "#" . static::encodeQueryFragment($this->fragment
);
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
403 * Eventually, two normalized URLs pointing to the same resource should be
404 * equal even if they were originally represented by two different strings
408 public function normalize()
411 $this->scheme
= static::normalizeScheme($this->scheme
);
415 $this->host
= static::normalizeHost($this->host
);
419 $this->port
= static::normalizePort($this->port
, $this->scheme
);
423 $this->path
= static::normalizePath($this->path
);
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
)) {
444 * Convert a relative URI into an absolute URI using a base absolute URI as
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
457 public function resolve($baseUri)
459 // Ignore if URI is absolute
460 if ($this->isAbsolute()) {
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()));
476 $basePath = $baseUri->getPath();
477 $relPath = $this->getPath();
479 $this->setPath($basePath);
480 if (!$this->getQuery()) {
481 $this->setQuery($baseUri->getQuery());
484 if (substr($relPath, 0, 1) == '/') {
485 $this->setPath(static::removePathDotSegments($relPath));
487 if ($baseUri->getHost() && !$basePath) {
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());
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
519 public function makeRelative($baseUri)
521 // Copy base URI, we should not modify it
522 $baseUri = new static($baseUri);
525 $baseUri->normalize();
527 $host = $this->getHost();
528 $baseHost = $baseUri->getHost();
529 if ($host && $baseHost && ($host != $baseHost)) {
530 // Not the same hostname
534 $port = $this->getPort();
535 $basePort = $baseUri->getPort();
536 if ($port && $basePort && ($port != $basePort)) {
541 $scheme = $this->getScheme();
542 $baseScheme = $baseUri->getScheme();
543 if ($scheme && $baseScheme && ($scheme != $baseScheme)) {
544 // Not the same scheme (e.g. HTTP vs. HTTPS)
548 // Remove host, port and scheme
554 if ($this->getPath() == $baseUri->getPath()) {
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
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, '../');
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));
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
;
610 * @return string|null
612 public function getHost()
622 public function getPort()
630 * @return string|null
632 public function getPath()
640 * @return string|null
642 public function getQuery()
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
655 public function getQueryAsArray()
659 parse_str($this->query
, $query);
666 * Get the URI fragment
668 * @return string|null
670 public function getFragment()
672 return $this->fragment
;
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
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
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',
697 ), Exception\InvalidUriPartException
::INVALID_SCHEME
);
700 $this->scheme
= $scheme;
705 * Set the URI User-info part (usually user:password)
707 * @param string $userInfo
709 * @throws Exception\InvalidUriPartException If the schema definition
710 * does not have this part
712 public function setUserInfo($userInfo)
714 $this->userInfo
= $userInfo;
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
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
736 public function setHost($host)
740 && !self
::validateHost($host, $this->validHostTypes
)
742 throw new Exception\
InvalidUriPartException(sprintf(
743 'Host "%s" is not valid or is not accepted by %s',
746 ), Exception\InvalidUriPartException
::INVALID_HOSTNAME
);
754 * Set the port part of the URI
759 public function setPort($port)
768 * @param string $path
771 public function setPath($path)
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
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;
800 * Set the URI fragment part
802 * @param string $fragment
804 * @throws Exception\InvalidUriPartException If the schema definition
805 * does not have this part
807 public function setFragment($fragment)
809 $this->fragment
= $fragment;
814 * Magic method to convert the URI to a string
818 public function __toString()
821 return $this->toString();
822 } catch (\Exception
$e) {
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
841 public static function validateScheme($scheme)
843 if (!empty(static::$validSchemes)
844 && !in_array(strtolower($scheme), static::$validSchemes)
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
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
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)) {
894 if ($allowed & self
::HOST_REGNAME
) {
895 if (static::isValidRegName($host)) {
900 if ($allowed & self
::HOST_DNS
) {
901 if (static::isValidDnsHostname($host)) {
912 * Valid values include numbers between 1 and 65535, and empty values
917 public static function validatePort($port)
925 if ($port < 1 ||
$port > 0xffff) {
936 * @param string $path
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
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
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);
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
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
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
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)) {
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
1088 public static function removePathDotSegments($path)
1093 if ($path == '..' ||
$path == '.') {
1098 case ($path == '/.'):
1101 case ($path == '/..'):
1103 $output = substr($output, 0, strrpos($output, '/', -1));
1105 case (substr($path, 0, 4) == '/../'):
1106 $path = '/' . substr($path, 4);
1107 $output = substr($output, 0, strrpos($output, '/', -1));
1109 case (substr($path, 0, 3) == '/./'):
1110 $path = substr($path, 2);
1112 case (substr($path, 0, 2) == './'):
1113 $path = substr($path, 2);
1115 case (substr($path, 0, 3) == '../'):
1116 $path = substr($path, 3);
1119 $slash = strpos($path, '/', 1);
1120 if ($slash === false) {
1123 $seg = substr($path, 0, $slash);
1127 $path = substr($path, strlen($seg));
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
1142 * If objects are passed in, none of the passed objects will be modified.
1144 * @param Uri|string $baseUri
1145 * @param Uri|string $relativeUri
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
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,
1171 $validator = new Validator\
Ip($validatorParams);
1172 $return = $validator->isValid($host);
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
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
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
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
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.
1257 * @param string $scheme
1260 protected static function normalizePort($port, $scheme = null)
1263 && isset(static::$defaultPorts[$scheme])
1264 && ($port == static::$defaultPorts[$scheme])
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
1281 protected static function normalizePath($path)
1283 $path = self
::encodePath(
1284 self
::decodeUrlEncodedChars(
1285 self
::removePathDotSegments($path),
1286 '/[' . self
::CHAR_UNRESERVED
. ':@&=\+\$,\/;%]/'
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
1302 protected static function normalizeQuery($query)
1304 $query = self
::encodeQueryFragment(
1305 self
::decodeUrlEncodedChars(
1307 '/[' . self
::CHAR_UNRESERVED
. self
::CHAR_SUB_DELIMS
. '%:@\/\?]/'
1315 * Normalize the fragment part
1317 * Currently this is exactly the same as normalizeQuery().
1319 * @param string $fragment
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
1336 protected static function decodeUrlEncodedChars($input, $allowed = '')
1338 $decodeCb = function ($match) use ($allowed) {
1339 $char = rawurldecode($match[0]);
1340 if (preg_match($allowed, $char)) {
1343 return strtoupper($match[0]);
1346 return preg_replace_callback('/%[A-Fa-f0-9]{2}/', $decodeCb, $input);