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