|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of WebHelper Parser. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) James <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WebHelper\Parser\Parser; |
|
13
|
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
use League\Uri\Schemes\Http as HttpUri; |
|
16
|
|
|
use Webmozart\PathUtil\Path; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Web Helper Universal Descriptor, a.k.a. WHUD. |
|
20
|
|
|
* |
|
21
|
|
|
* This is used to find what a server is actually meant to serve |
|
22
|
|
|
* |
|
23
|
|
|
* @author James <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class Descriptor |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var string the server name */ |
|
28
|
|
|
private $host; |
|
29
|
|
|
|
|
30
|
|
|
/** @var array a list of paths that exposes filesystem directories in the host context */ |
|
31
|
|
|
private $paths; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Base constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $host The host |
|
37
|
|
|
* @param array $paths The paths |
|
38
|
|
|
*/ |
|
39
|
9 |
|
public function __construct($host = '', array $paths = []) |
|
40
|
|
|
{ |
|
41
|
9 |
|
$this->host = $host; |
|
42
|
9 |
|
$this->paths = $paths; |
|
43
|
9 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Gets the served urls. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $path The path |
|
49
|
|
|
* |
|
50
|
|
|
* @return array The served urls |
|
51
|
|
|
*/ |
|
52
|
4 |
|
public function getServedUrls($path) |
|
53
|
|
|
{ |
|
54
|
4 |
|
$isServedAs = []; |
|
55
|
|
|
|
|
56
|
4 |
|
foreach ($this->paths as $exposedPath => $exposedDirectory) { |
|
57
|
4 |
|
$relative = $this->getRelative($path, $exposedDirectory); |
|
58
|
4 |
|
if (!is_null($relative)) { |
|
59
|
4 |
|
$isServedAs[] = $this->getHost().preg_replace(',/$,', '', $exposedPath).$relative; |
|
60
|
4 |
|
} |
|
61
|
4 |
|
} |
|
62
|
|
|
|
|
63
|
4 |
|
return $isServedAs; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Gets the exposed path. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $url The url |
|
70
|
|
|
* |
|
71
|
|
|
* @return string The exposed path |
|
72
|
|
|
*/ |
|
73
|
5 |
|
public function getExposedPath($url) |
|
74
|
|
|
{ |
|
75
|
5 |
|
$isExposedAs = ''; |
|
76
|
5 |
|
list($host, $path) = $this->getUriHostAndPath($url); |
|
77
|
|
|
|
|
78
|
5 |
|
if ($host == $this->host) { |
|
79
|
4 |
|
foreach ($this->paths as $exposedPath => $exposedDirectory) { |
|
80
|
4 |
|
$relative = $this->getRelative($path, $exposedPath); |
|
81
|
4 |
|
if (!is_null($relative)) { |
|
82
|
4 |
|
$isExposedAs = $exposedDirectory.$relative; |
|
83
|
4 |
|
break; |
|
84
|
|
|
} |
|
85
|
4 |
|
} |
|
86
|
4 |
|
} |
|
87
|
|
|
|
|
88
|
5 |
|
return $isExposedAs; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Gets the relative path if it matches against another path. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $path The path |
|
95
|
|
|
* @param string $against The path to match against |
|
96
|
|
|
* |
|
97
|
|
|
* @return null|string The relative path. |
|
98
|
|
|
*/ |
|
99
|
8 |
|
private function getRelative($path, $against) |
|
100
|
|
|
{ |
|
101
|
8 |
|
$relative = null; |
|
102
|
|
|
|
|
103
|
8 |
|
if (preg_replace(',/$,', '', $path) == $against || |
|
104
|
8 |
|
Path::isBasePath($against, Path::getDirectory($path)) |
|
105
|
8 |
|
) { |
|
106
|
8 |
|
$relative = Path::makeRelative($path, $against); |
|
107
|
8 |
|
$relative = $relative ? '/'.$relative : ''; |
|
108
|
8 |
|
} |
|
109
|
|
|
|
|
110
|
8 |
|
return $relative; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Gets the host. |
|
115
|
|
|
* |
|
116
|
|
|
* @return string The starting uri based on the host |
|
117
|
|
|
*/ |
|
118
|
4 |
|
protected function getHost() |
|
119
|
|
|
{ |
|
120
|
4 |
|
$host = $this->host; |
|
121
|
4 |
|
$scheme = 'http'; |
|
122
|
|
|
|
|
123
|
4 |
|
if (preg_match('/:443$/', $this->host)) { |
|
124
|
1 |
|
$scheme = 'https'; |
|
125
|
1 |
|
$host = preg_replace('/:443$/', '', $this->host); |
|
126
|
1 |
|
} |
|
127
|
|
|
|
|
128
|
4 |
|
return $scheme.'://'.$host; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Gets the uri host and path. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $url The url |
|
135
|
|
|
* |
|
136
|
|
|
* @return array The uri host and path |
|
137
|
|
|
*/ |
|
138
|
5 |
|
private function getUriHostAndPath($url) |
|
139
|
|
|
{ |
|
140
|
|
|
try { |
|
141
|
5 |
|
$uri = HttpUri::createFromString($url); |
|
142
|
5 |
|
} catch (InvalidArgumentException $e) { |
|
143
|
1 |
|
return ['', '']; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
4 |
|
$host = $uri->getHost(); |
|
147
|
4 |
|
$port = $uri->getPort(); |
|
148
|
4 |
|
if (!$port && $uri->getScheme() == 'https') { |
|
149
|
1 |
|
$port = 443; |
|
150
|
1 |
|
} |
|
151
|
4 |
|
if ($port) { |
|
152
|
3 |
|
$host .= ':'.strval($port); |
|
153
|
3 |
|
} |
|
154
|
4 |
|
$path = $uri->getPath(); |
|
155
|
4 |
|
if (!$path) { |
|
156
|
1 |
|
$path = '/'; |
|
157
|
1 |
|
} |
|
158
|
|
|
|
|
159
|
4 |
|
return [$host, $path]; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|