diff options
author | Mario <mario@mariovavti.com> | 2019-12-04 10:25:11 +0000 |
---|---|---|
committer | Mario <mario@mariovavti.com> | 2019-12-04 10:25:11 +0000 |
commit | bde429cff649237984903a252ba1a718e6d74f53 (patch) | |
tree | b2b2570159cfb37689e6ce3b96c3b1b988d676cc /vendor/sabre/uri/lib/functions.php | |
parent | cc9f41df5f83bcab435d6fb941b5a8f5b1457037 (diff) | |
parent | 4c8d33d1eb2a804aa70a7bc677d6c73d0d94816b (diff) | |
download | volse-hubzilla-bde429cff649237984903a252ba1a718e6d74f53.tar.gz volse-hubzilla-bde429cff649237984903a252ba1a718e6d74f53.tar.bz2 volse-hubzilla-bde429cff649237984903a252ba1a718e6d74f53.zip |
Merge branch '4.6RC'4.6
Diffstat (limited to 'vendor/sabre/uri/lib/functions.php')
-rw-r--r-- | vendor/sabre/uri/lib/functions.php | 180 |
1 files changed, 100 insertions, 80 deletions
diff --git a/vendor/sabre/uri/lib/functions.php b/vendor/sabre/uri/lib/functions.php index 39b4a6f08..161e684d7 100644 --- a/vendor/sabre/uri/lib/functions.php +++ b/vendor/sabre/uri/lib/functions.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + namespace Sabre\Uri; /** @@ -18,30 +20,32 @@ namespace Sabre\Uri; * * @param string $basePath * @param string $newPath + * * @return string + * + * @throws InvalidUriException */ -function resolve($basePath, $newPath) { - - $base = parse($basePath); +function resolve(string $basePath, string $newPath): string +{ $delta = parse($newPath); - $pick = function($part) use ($base, $delta) { + // If the new path defines a scheme, it's absolute and we can just return + // that. + if ($delta['scheme']) { + return build($delta); + } + $base = parse($basePath); + $pick = function ($part) use ($base, $delta) { if ($delta[$part]) { return $delta[$part]; } elseif ($base[$part]) { return $base[$part]; } - return null; + return null; }; - // If the new path defines a scheme, it's absolute and we can just return - // that. - if ($delta['scheme']) { - return build($delta); - } - $newParts = []; $newParts['scheme'] = $pick('scheme'); @@ -49,17 +53,18 @@ function resolve($basePath, $newPath) { $newParts['port'] = $pick('port'); $path = ''; - if ($delta['path']) { + if (is_string($delta['path']) and strlen($delta['path']) > 0) { // If the path starts with a slash - if ($delta['path'][0] === '/') { + if ('/' === $delta['path'][0]) { $path = $delta['path']; } else { // Removing last component from base path. $path = $base['path']; - if (strpos($path, '/') !== false) { - $path = substr($path, 0, strrpos($path, '/')); + $length = strrpos((string) $path, '/'); + if (false !== $length) { + $path = substr($path, 0, $length); } - $path .= '/' . $delta['path']; + $path .= '/'.$delta['path']; } } else { $path = $base['path'] ?: '/'; @@ -68,15 +73,14 @@ function resolve($basePath, $newPath) { $pathParts = explode('/', $path); $newPathParts = []; foreach ($pathParts as $pathPart) { - switch ($pathPart) { //case '' : - case '.' : + case '.': break; - case '..' : + case '..': array_pop($newPathParts); break; - default : + default: $newPathParts[] = $pathPart; break; } @@ -95,8 +99,8 @@ function resolve($basePath, $newPath) { if ($delta['fragment']) { $newParts['fragment'] = $delta['fragment']; } - return build($newParts); + return build($newParts); } /** @@ -109,10 +113,13 @@ function resolve($basePath, $newPath) { * It will also change a %3a into a %3A. * * @param string $uri + * * @return string + * + * @throws InvalidUriException */ -function normalize($uri) { - +function normalize(string $uri): string +{ $parts = parse($uri); if (!empty($parts['path'])) { @@ -123,23 +130,23 @@ function normalize($uri) { case '.': // skip break; - case '..' : + case '..': // One level up in the hierarchy array_pop($newPathParts); break; - default : + default: // Ensuring that everything is correctly percent-encoded. $newPathParts[] = rawurlencode(rawurldecode($pathPart)); break; } } - $parts['path'] = '/' . implode('/', $newPathParts); + $parts['path'] = '/'.implode('/', $newPathParts); } if ($parts['scheme']) { $parts['scheme'] = strtolower($parts['scheme']); $defaultPorts = [ - 'http' => '80', + 'http' => '80', 'https' => '443', ]; @@ -149,8 +156,8 @@ function normalize($uri) { } // A few HTTP specific rules. switch ($parts['scheme']) { - case 'http' : - case 'https' : + case 'http': + case 'https': if (empty($parts['path'])) { // An empty path is equivalent to / in http. $parts['path'] = '/'; @@ -159,10 +166,11 @@ function normalize($uri) { } } - if ($parts['host']) $parts['host'] = strtolower($parts['host']); + if ($parts['host']) { + $parts['host'] = strtolower($parts['host']); + } return build($parts); - } /** @@ -176,10 +184,13 @@ function normalize($uri) { * percent-encoded strings. PHP's parse_url corrupts these characters on OS X. * * @param string $uri + * * @return array + * + * @throws InvalidUriException */ -function parse($uri) { - +function parse(string $uri): array +{ // Normally a URI must be ASCII, however. However, often it's not and // parse_url might corrupt these strings. // @@ -187,7 +198,7 @@ function parse($uri) { // uriencode them first. $uri = preg_replace_callback( '/[^[:ascii:]]/u', - function($matches) { + function ($matches) { return rawurlencode($matches[0]); }, $uri @@ -200,15 +211,14 @@ function parse($uri) { return $result + [ - 'scheme' => null, - 'host' => null, - 'path' => null, - 'port' => null, - 'user' => null, - 'query' => null, + 'scheme' => null, + 'host' => null, + 'path' => null, + 'port' => null, + 'user' => null, + 'query' => null, 'fragment' => null, ]; - } /** @@ -216,46 +226,44 @@ function parse($uri) { * it to generate a new uri. * * @param array $parts + * * @return string */ -function build(array $parts) { - +function build(array $parts): string +{ $uri = ''; $authority = ''; if (!empty($parts['host'])) { $authority = $parts['host']; if (!empty($parts['user'])) { - $authority = $parts['user'] . '@' . $authority; + $authority = $parts['user'].'@'.$authority; } if (!empty($parts['port'])) { - $authority = $authority . ':' . $parts['port']; + $authority = $authority.':'.$parts['port']; } } if (!empty($parts['scheme'])) { // If there's a scheme, there's also a host. - $uri = $parts['scheme'] . ':'; - + $uri = $parts['scheme'].':'; } - if ($authority || (!empty($parts['scheme']) && $parts['scheme'] === 'file')) { + if ($authority || (!empty($parts['scheme']) && 'file' === $parts['scheme'])) { // No scheme, but there is a host. - $uri .= '//' . $authority; - + $uri .= '//'.$authority; } if (!empty($parts['path'])) { $uri .= $parts['path']; } if (!empty($parts['query'])) { - $uri .= '?' . $parts['query']; + $uri .= '?'.$parts['query']; } if (!empty($parts['fragment'])) { - $uri .= '#' . $parts['fragment']; + $uri .= '#'.$parts['fragment']; } return $uri; - } /** @@ -274,16 +282,17 @@ function build(array $parts) { * the end of the string is stripped off. * * @param string $path + * * @return array */ -function split($path) { - +function split(string $path): array +{ $matches = []; if (preg_match('/^(?:(?:(.*)(?:\/+))?([^\/]+))(?:\/?)$/u', $path, $matches)) { return [$matches[1], $matches[2]]; } - return [null,null]; + return [null, null]; } /** @@ -297,10 +306,13 @@ function split($path) { * crude and probably slow, so the original parse_url is usually preferred. * * @param string $uri + * * @return array + * + * @throws InvalidUriException */ -function _parse_fallback($uri) { - +function _parse_fallback(string $uri): array +{ // Normally a URI must be ASCII, however. However, often it's not and // parse_url might corrupt these strings. // @@ -308,45 +320,43 @@ function _parse_fallback($uri) { // uriencode them first. $uri = preg_replace_callback( '/[^[:ascii:]]/u', - function($matches) { + function ($matches) { return rawurlencode($matches[0]); }, $uri ); $result = [ - 'scheme' => null, - 'host' => null, - 'port' => null, - 'user' => null, - 'path' => null, + 'scheme' => null, + 'host' => null, + 'port' => null, + 'user' => null, + 'path' => null, 'fragment' => null, - 'query' => null, + 'query' => null, ]; if (preg_match('% ^([A-Za-z][A-Za-z0-9+-\.]+): %x', $uri, $matches)) { - $result['scheme'] = $matches[1]; // Take what's left. $uri = substr($uri, strlen($result['scheme']) + 1); - } // Taking off a fragment part - if (strpos($uri, '#') !== false) { + if (false !== strpos($uri, '#')) { list($uri, $result['fragment']) = explode('#', $uri, 2); } // Taking off the query part - if (strpos($uri, '?') !== false) { + if (false !== strpos($uri, '?')) { list($uri, $result['query']) = explode('?', $uri, 2); } - if (substr($uri, 0, 3) === '///') { - // The triple slash uris are a bit unusual, but we have special handling - // for them. - $result['path'] = substr($uri, 2); - $result['host'] = ''; - } elseif (substr($uri, 0, 2) === '//') { + if ('///' === substr($uri, 0, 3)) { + // The triple slash uris are a bit unusual, but we have special handling + // for them. + $result['path'] = substr($uri, 2); + $result['host'] = ''; + } elseif ('//' === substr($uri, 0, 2)) { // Uris that have an authority part. $regex = ' %^ @@ -360,11 +370,21 @@ function _parse_fallback($uri) { if (!preg_match($regex, $uri, $matches)) { throw new InvalidUriException('Invalid, or could not parse URI'); } - if ($matches['host']) $result['host'] = $matches['host']; - if ($matches['port']) $result['port'] = (int)$matches['port']; - if (isset($matches['path'])) $result['path'] = $matches['path']; - if ($matches['user']) $result['user'] = $matches['user']; - if ($matches['pass']) $result['pass'] = $matches['pass']; + if ($matches['host']) { + $result['host'] = $matches['host']; + } + if (isset($matches['port'])) { + $result['port'] = (int) $matches['port']; + } + if (isset($matches['path'])) { + $result['path'] = $matches['path']; + } + if ($matches['user']) { + $result['user'] = $matches['user']; + } + if ($matches['pass']) { + $result['pass'] = $matches['pass']; + } } else { $result['path'] = $uri; } |