-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuri.php
51 lines (42 loc) · 1.21 KB
/
uri.php
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
<?php
/*
* This software is the property of its authors.
* See the copyright.txt file for more details.
*
*/
class uri {
static function cleanPath ($path) {
version_assert and assertTrue(isString($path) && strlen($path) > 0);
if (opEquals($path, '.') || opEquals($path, '..'))
return $path;
$parts = explode('/', $path);
foreach ($parts as $k => $v)
switch ($v) {
case '':
if ($k > 0)
unset($parts[$k]);
break;
case '.':
unset($parts[$k]);
break;
case '..':
unset($parts[$k]);
$i = $k - 1;
while ($i >= 0 && !isset($parts[$i]))
$i--;
if ($i >= 0)
unset($parts[$i]);
break;
}
$newPath = implode('/', $parts);
if ($newPath == '' || substr($path, -1) == '/')
$newPath .= '/';
$newPath = preg_replace('/\\/+/', '/', $newPath);
return $newPath;
}
static function unittest_cleanPath () {
assertTrue(self::cleanPath('.') === '.');
assertTrue(self::cleanPath('..') === '..');
assertTrue(self::cleanPath('/test/a/b/.././../first') === '/test/first');
}
}