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