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
|
|
|
namespace WebHelper\Parser\Parser; |
12
|
|
|
|
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use Webmozart\PathUtil\Path; |
15
|
|
|
use League\Uri\Schemes\Http as HttpUri; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Web Helper Universal Descriptor, a.k.a. WHUD. |
19
|
|
|
* |
20
|
|
|
* This is used to find what a server is actually meant to serve |
21
|
|
|
* |
22
|
|
|
* @author James <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class Descriptor |
25
|
|
|
{ |
26
|
|
|
private $host; |
27
|
|
|
|
28
|
|
|
private $paths; |
29
|
|
|
|
30
|
|
|
/* TODO |
|
|
|
|
31
|
|
|
* private $proxies; |
32
|
|
|
* private $grants; |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
public function __construct($host = '', array $paths = array()) |
36
|
|
|
{ |
37
|
|
|
$this->host = $host; |
38
|
|
|
$this->paths = $paths; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getServedUrls($path) |
42
|
|
|
{ |
43
|
|
|
$isServedAs = []; |
44
|
|
|
|
45
|
|
|
foreach ($this->paths as $exposedPath => $exposedDirectory) { |
46
|
|
|
if (preg_replace(',/$,', '', $path) == $exposedDirectory || |
47
|
|
|
Path::isBasePath($exposedDirectory, Path::getDirectory($path)) |
48
|
|
|
) { |
49
|
|
|
$relative = Path::makeRelative($path, $exposedDirectory); |
50
|
|
|
$relative = $relative ? '/'.$relative : ''; |
51
|
|
|
$isServedAs[] = $this->getHost().preg_replace(',/$,', '', $exposedPath).$relative; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $isServedAs; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getExposedPath($url) |
59
|
|
|
{ |
60
|
|
|
$isExposedAs = ''; |
61
|
|
|
list($host, $path) = $this->getUriHostAndPath($url); |
62
|
|
|
|
63
|
|
|
if ($host == $this->host) { |
64
|
|
|
foreach ($this->paths as $exposedPath => $exposedDirectory) { |
65
|
|
|
if (preg_replace(',/$,', '', $path) == $exposedPath || |
66
|
|
|
Path::isBasePath($exposedPath, Path::getDirectory($path)) |
67
|
|
|
) { |
68
|
|
|
$relative = Path::makeRelative($path, $exposedPath); |
69
|
|
|
$relative = $relative ? '/'.$relative : ''; |
70
|
|
|
$isExposedAs = $exposedDirectory.$relative; |
71
|
|
|
break; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $isExposedAs; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function getHost() |
80
|
|
|
{ |
81
|
|
|
$host = $this->host; |
82
|
|
|
$scheme = 'http'; |
83
|
|
|
|
84
|
|
|
if (preg_match('/:443$/', $this->host)) { |
85
|
|
|
$scheme = 'https'; |
86
|
|
|
$host = preg_replace('/:443$/', '', $this->host); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $scheme.'://'.$host; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function getUriHostAndPath($url) |
93
|
|
|
{ |
94
|
|
|
try { |
95
|
|
|
$uri = HttpUri::createFromString($url); |
96
|
|
|
} catch (InvalidArgumentException $e) { |
97
|
|
|
return ['', '']; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$host = $uri->getHost(); |
101
|
|
|
$port = $uri->getPort(); |
102
|
|
|
if (!$port && $uri->getScheme() == 'https') { |
103
|
|
|
$port = 443; |
104
|
|
|
} |
105
|
|
|
if ($port) { |
106
|
|
|
$host .= ':'.strval($port); |
107
|
|
|
} |
108
|
|
|
$path = $uri->getPath(); |
109
|
|
|
if (!$path) { |
110
|
|
|
$path = '/'; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return [$host, $path]; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.