1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
namespace GoetasWebservices\XML\XSDReader\Utils; |
4
|
|
|
|
5
|
|
|
class UrlUtils |
6
|
|
|
{ |
7
|
54 |
|
public static function resolveRelativeUrl( |
8
|
|
|
string $base, |
9
|
|
|
string $rel |
10
|
|
|
) : string { |
11
|
54 |
|
if (!$rel) { |
12
|
2 |
|
return $base; |
13
|
|
|
} elseif ( |
14
|
|
|
/* return if already absolute URL */ |
15
|
53 |
|
parse_url($rel, PHP_URL_SCHEME) !== null || |
16
|
9 |
|
substr($rel, 0, 2) === '//' |
17
|
|
|
) { |
18
|
45 |
|
return $rel; |
19
|
|
|
} elseif ( |
20
|
|
|
/* queries and anchors */ |
21
|
9 |
|
in_array( |
22
|
9 |
|
$rel[0], |
23
|
|
|
[ |
24
|
9 |
|
'#', |
25
|
|
|
'?' |
26
|
|
|
] |
27
|
|
|
) |
28
|
|
|
) { |
29
|
2 |
|
return $base.$rel; |
30
|
|
|
} |
31
|
|
|
|
32
|
9 |
|
return static::resolveRelativeUrlAfterEarlyChecks($base, $rel); |
33
|
|
|
} |
34
|
|
|
|
35
|
9 |
|
protected static function resolveRelativeUrlAfterEarlyChecks( |
36
|
|
|
string $base, |
37
|
|
|
string $rel |
38
|
|
|
) : string { |
39
|
|
|
/* fix url file for Windows */ |
40
|
9 |
|
$base = preg_replace('#^file:\/\/([^/])#', 'file:///\1', $base); |
41
|
|
|
|
42
|
|
|
/* |
43
|
|
|
* parse base URL and convert to local variables: |
44
|
|
|
* $scheme, $host, $path |
45
|
|
|
*/ |
46
|
9 |
|
$parts = parse_url($base); |
47
|
|
|
|
48
|
9 |
|
return static::resolveRelativeUrlToAbsoluteUrl( |
49
|
9 |
|
$rel, |
50
|
|
|
( |
51
|
9 |
|
$rel[0] === '/' |
52
|
4 |
|
? '' // destroy path if relative url points to root |
53
|
|
|
: ( // remove non-directory element from path |
54
|
7 |
|
isset($parts['path']) |
55
|
7 |
|
? preg_replace('#/[^/]*$#', '', $parts["path"]) |
56
|
9 |
|
: '' |
57
|
|
|
) |
58
|
|
|
), |
59
|
9 |
|
$parts |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
9 |
|
protected static function resolveRelativeUrlToAbsoluteUrl( |
64
|
|
|
string $rel, |
65
|
|
|
string $path, |
66
|
|
|
array $parts |
67
|
|
|
) : string { |
68
|
|
|
/* Build absolute URL */ |
69
|
9 |
|
$abs = ''; |
70
|
|
|
|
71
|
9 |
|
if (isset($parts["host"])) { |
72
|
5 |
|
$abs .= $parts['host']; |
73
|
|
|
} |
74
|
|
|
|
75
|
9 |
|
if (isset($parts["port"])) { |
76
|
|
|
$abs .= ":".$parts["port"]; |
77
|
|
|
} |
78
|
|
|
|
79
|
9 |
|
$abs .= $path."/".$rel; |
80
|
|
|
|
81
|
|
|
/* |
82
|
|
|
* replace superfluous slashes with a single slash. |
83
|
|
|
* covers: |
84
|
|
|
* // |
85
|
|
|
* /./ |
86
|
|
|
* /foo/../ |
87
|
|
|
*/ |
88
|
9 |
|
$n = 1; |
89
|
|
|
do { |
90
|
9 |
|
$abs = preg_replace( |
91
|
9 |
|
'#(?:(?:/\.?/)|(?!\.\.)[^/]+/\.\./)#', |
92
|
9 |
|
'/', |
93
|
9 |
|
$abs, |
94
|
9 |
|
-1, |
95
|
9 |
|
$n |
96
|
|
|
); |
97
|
9 |
|
} while ($n > 0); |
98
|
|
|
|
99
|
9 |
|
if (isset($parts["scheme"])) { |
100
|
6 |
|
$abs = $parts["scheme"].'://'.$abs; |
101
|
|
|
} |
102
|
|
|
|
103
|
9 |
|
return $abs; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|