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