|
1
|
|
|
<?php |
|
2
|
|
|
namespace MallardDuck\Whois\WhoisServerList; |
|
3
|
|
|
|
|
4
|
|
|
use MallardDuck\Whois\Exceptions\UnknownWhoisException; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Whois Server List Locator Class |
|
8
|
|
|
* |
|
9
|
|
|
* This class loads a TLD whois list and allows for easy look up. |
|
10
|
|
|
* |
|
11
|
|
|
* @author mallardduck <[email protected]> |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright lucidinternets.com 2018 |
|
14
|
|
|
* |
|
15
|
|
|
* @version 1.0.0 |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class AbstractLocator |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The status of loading the whois server list. |
|
22
|
|
|
* |
|
23
|
|
|
* @var bool |
|
24
|
|
|
*/ |
|
25
|
|
|
private $loadStatus = false; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The path where the tld json file exists. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $whoisListPath; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* A collection of the TLDs and whois server list. |
|
36
|
|
|
* |
|
37
|
|
|
* @var \Tightenco\Collect\Support\Collection |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $whoisCollection; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The results of the last looked up domain. |
|
43
|
|
|
* |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $lastMatch; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Build the TLD Whois Server Locator class. |
|
50
|
|
|
*/ |
|
51
|
165 |
|
public function __construct() |
|
52
|
|
|
{ |
|
53
|
165 |
|
$fileData = file_get_contents($this->whoisListPath); |
|
54
|
165 |
|
$tldData = json_decode($fileData); |
|
55
|
165 |
|
if (null !== $tldData && json_last_error() === JSON_ERROR_NONE) { |
|
56
|
165 |
|
$this->loadStatus = true; |
|
57
|
|
|
} |
|
58
|
165 |
|
$this->whoisCollection = collect((array) $tldData); |
|
59
|
165 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns the TLD list load status. |
|
63
|
|
|
* |
|
64
|
|
|
* @return bool The class status of loading the list and decoding the json. |
|
65
|
|
|
*/ |
|
66
|
3 |
|
public function getLoadStatus() |
|
67
|
|
|
{ |
|
68
|
3 |
|
return $this->loadStatus; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Gets and returns the last match looked up. |
|
73
|
|
|
* |
|
74
|
|
|
* @return array The results of the last looked up domain. |
|
75
|
|
|
*/ |
|
76
|
3 |
|
public function getLastMatch() |
|
77
|
|
|
{ |
|
78
|
3 |
|
return $this->lastMatch; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Finds and returns the last match looked up. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $domain Either an ID or a username. |
|
85
|
|
|
* |
|
86
|
|
|
* @return self Returns the same instance for fluent usage. |
|
87
|
|
|
*/ |
|
88
|
|
|
abstract public function findWhoisServer($domain); |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get the Whois server of the domain provided, or previously found domain. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $domain The domain being looked up via whois. |
|
94
|
|
|
* |
|
95
|
|
|
* @return string Returns the domain name of the whois server. |
|
96
|
|
|
*/ |
|
97
|
102 |
|
public function getWhoisServer($domain = '') |
|
98
|
|
|
{ |
|
99
|
102 |
|
if (!empty($domain) || empty($this->lastMatch)) { |
|
100
|
78 |
|
$this->findWhoisServer($domain); |
|
101
|
|
|
} |
|
102
|
87 |
|
$server = current($this->lastMatch); |
|
103
|
87 |
|
if ('UNKNOWN' === strtoupper($server)) { |
|
104
|
6 |
|
throw new UnknownWhoisException("This domain doesn't have a valid whois server."); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
81 |
|
return $server; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|