/usr/share/php/Sabre/Uri/functions.php is in php-sabre-uri 1.0.1-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | <?php
namespace Sabre\Uri;
/**
* Resolves relative urls, like a browser would.
*
* This function takes a basePath, which itself _may_ also be relative, and
* then applies the relative path on top of it.
*
* @param string $basePath
* @param string $newPath
* @return string
*/
function resolve($basePath, $newPath) {
$base = parse($basePath);
$delta = parse($newPath);
$pick = function($part) use ($base, $delta) {
if ($delta[$part]) {
return $delta[$part];
} elseif ($base[$part]) {
return $base[$part];
}
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');
$newParts['host'] = $pick('host');
$newParts['port'] = $pick('port');
$path = '';
if ($delta['path']) {
// If the path starts with a slash
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, '/'));
}
$path .= '/' . $delta['path'];
}
} else {
$path = $base['path'] ?: '/';
}
// Removing .. and .
$pathParts = explode('/', $path);
$newPathParts = [];
foreach($pathParts as $pathPart) {
switch($pathPart) {
//case '' :
case '.' :
break;
case '..' :
array_pop($newPathParts);
break;
default :
$newPathParts[] = $pathPart;
break;
}
}
$path = implode('/', $newPathParts);
// If the source url ended with a /, we want to preserve that.
$newParts['path'] = $path;
if ($delta['query']) {
$newParts['query'] = $delta['query'];
} elseif (!empty($base['query']) && empty($delta['host']) && empty($delta['path'])) {
// Keep the old query if host and path didn't change
$newParts['query'] = $base['query'];
}
if ($delta['fragment']) {
$newParts['fragment'] = $delta['fragment'];
}
return build($newParts);
}
/**
* Takes a URI or partial URI as its argument, and normalizes it.
*
* After normalizing a URI, you can safely compare it to other URIs.
* This function will for instance convert a %7E into a tilde, according to
* rfc3986.
*
* It will also change a %3a into a %3A.
*
* @param string $uri
* @return string
*/
function normalize($uri) {
$parts = parse($uri);
if (!empty($parts['path'])) {
$pathParts = explode('/', ltrim($parts['path'], '/'));
$newPathParts = [];
foreach($pathParts as $pathPart) {
switch($pathPart) {
case '.':
// skip
break;
case '..' :
// One level up in the hierarchy
array_pop($newPathParts);
break;
default :
// Ensuring that everything is correctly percent-encoded.
$newPathParts[] = rawurlencode(rawurldecode($pathPart));
break;
}
}
$parts['path'] = '/' . implode('/', $newPathParts);
}
if ($parts['scheme']) {
$parts['scheme'] = strtolower($parts['scheme']);
$defaultPorts = [
'http' => '80',
'https' => '443',
];
if (!empty($parts['port']) && isset($defaultPorts[$parts['scheme']]) && $defaultPorts[$parts['scheme']] == $parts['port']) {
// Removing default ports.
unset($parts['port']);
}
// A few HTTP specific rules.
switch($parts['scheme']) {
case 'http' :
case 'https' :
if (empty($parts['path'])) {
// An empty path is equivalent to / in http.
$parts['path'] = '/';
}
break;
}
}
if ($parts['host']) $parts['host'] = strtolower($parts['host']);
return build($parts);
}
/**
* Parses a URI and returns its individual components.
*
* This method largely behaves the same as PHP's parse_url, except that it will
* return an array with all the array keys, including the ones that are not
* set by parse_url, which makes it a bit easier to work with.
*
* @param string $uri
* @return array
*/
function parse($uri) {
return
parse_url($uri) + [
'scheme' => null,
'host' => null,
'path' => null,
'port' => null,
'user' => null,
'query' => null,
'fragment' => null,
];
}
/**
* This function takes the components returned from PHP's parse_url, and uses
* it to generate a new uri.
*
* @param array $parts
* @return string
*/
function build(array $parts) {
$uri = '';
$authority = '';
if (!empty($parts['host'])) {
$authority = $parts['host'];
if (!empty($parts['user'])) {
$authority = $parts['user'] . '@' . $authority;
}
if (!empty($parts['port'])) {
$authority = $authority . ':' . $parts['port'];
}
}
if (!empty($parts['scheme'])) {
// If there's a scheme, there's also a host.
$uri = $parts['scheme'] . ':';
}
if ($authority) {
// No scheme, but there is a host.
$uri .= '//' . $authority;
}
if (!empty($parts['path'])) {
$uri .= $parts['path'];
}
if (!empty($parts['query'])) {
$uri .= '?' . $parts['query'];
}
if (!empty($parts['fragment'])) {
$uri .= '#' . $parts['fragment'];
}
return $uri;
}
/**
* Returns the 'dirname' and 'basename' for a path.
*
* The reason there is a custom function for this purpose, is because
* basename() is locale aware (behaviour changes if C locale or a UTF-8 locale
* is used) and we need a method that just operates on UTF-8 characters.
*
* In addition basename and dirname are platform aware, and will treat
* backslash (\) as a directory separator on windows.
*
* This method returns the 2 components as an array.
*
* If there is no dirname, it will return an empty string. Any / appearing at
* the end of the string is stripped off.
*
* @param string $path
* @return array
*/
function split($path) {
$matches = [];
if(preg_match('/^(?:(?:(.*)(?:\/+))?([^\/]+))(?:\/?)$/u', $path, $matches)) {
return [$matches[1], $matches[2]];
}
return [null,null];
}
|