|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LiquidWeb\SslCertificate; |
|
4
|
|
|
|
|
5
|
|
|
use League\Uri\Parser as UriParser; |
|
6
|
|
|
use LiquidWeb\SslCertificate\Exceptions\InvalidUrl; |
|
7
|
|
|
|
|
8
|
|
|
class Url |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var string */ |
|
11
|
|
|
protected $inputUrl; |
|
12
|
|
|
|
|
13
|
|
|
/** @var array */ |
|
14
|
|
|
protected $parsedUrl; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
protected $validatedURL; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $ipAddress; |
|
21
|
|
|
|
|
22
|
1 |
|
public function __toString() |
|
23
|
|
|
{ |
|
24
|
1 |
|
return $this->getInputUrl(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
21 |
|
private static function verifyAndGetDNS($domain): ?string |
|
28
|
|
|
{ |
|
29
|
21 |
|
$domainIp = gethostbyname($domain); |
|
30
|
21 |
|
if (! filter_var($domainIp, FILTER_VALIDATE_IP)) { |
|
31
|
4 |
|
return null; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
19 |
|
return $domainIp; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
22 |
|
public function __construct(string $url) |
|
38
|
|
|
{ |
|
39
|
22 |
|
$this->inputUrl = $url; |
|
40
|
22 |
|
$parser = new UriParser(); |
|
41
|
22 |
|
$this->parsedUrl = $parser($this->inputUrl); |
|
42
|
|
|
|
|
43
|
|
|
// Verify parsing has a host |
|
44
|
22 |
|
if (is_null($this->parsedUrl['host'])) { |
|
45
|
|
|
try { |
|
46
|
18 |
|
$this->parsedUrl = $parser('https://'.$this->inputUrl); |
|
47
|
|
|
} catch (\Exception $e) { |
|
48
|
|
|
throw InvalidUrl::couldNotValidate($url); |
|
49
|
|
|
} |
|
50
|
18 |
|
if (is_null($this->parsedUrl['host'])) { |
|
51
|
|
|
throw InvalidUrl::couldNotDetermineHost($url); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
22 |
|
if (! filter_var($this->getValidUrl(), FILTER_VALIDATE_URL)) { |
|
56
|
1 |
|
throw InvalidUrl::couldNotValidate($url); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
21 |
|
$this->ipAddress = self::verifyAndGetDNS($this->parsedUrl['host']); |
|
60
|
21 |
|
$this->validatedURL = $url; |
|
61
|
21 |
|
} |
|
62
|
|
|
|
|
63
|
8 |
|
public function getIp(): ?string |
|
64
|
|
|
{ |
|
65
|
8 |
|
if (null === $this->ipAddress) { |
|
66
|
1 |
|
throw InvalidUrl::couldNotResolveDns($this->inputUrl); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
7 |
|
return $this->ipAddress; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
6 |
|
public function getInputUrl(): string |
|
73
|
|
|
{ |
|
74
|
6 |
|
return $this->inputUrl; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
22 |
|
public function getHostName(): string |
|
78
|
|
|
{ |
|
79
|
22 |
|
return $this->parsedUrl['host']; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
public function getValidatedURL(): string |
|
83
|
|
|
{ |
|
84
|
2 |
|
return $this->validatedURL; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
22 |
|
public function getPort(): string |
|
88
|
|
|
{ |
|
89
|
22 |
|
return (isset($this->parsedUrl['port'])) ? $this->parsedUrl['port'] : '443'; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
12 |
|
public function getTestURL(): string |
|
93
|
|
|
{ |
|
94
|
12 |
|
return "{$this->getHostName()}:{$this->getPort()}"; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
22 |
|
public function getValidUrl(): string |
|
98
|
|
|
{ |
|
99
|
22 |
|
if ($this->getPort() === '80') { |
|
100
|
1 |
|
return 'http://'.$this->getHostName().'/'; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
21 |
|
return 'https://'.$this->getHostName().'/'; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|