1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
+-----------------------------------------------------------------------------+ |
6
|
|
|
| PHPPackage - Domain Checker |
7
|
|
|
+-----------------------------------------------------------------------------+ |
8
|
|
|
| Copyright (c)2018 (http://github.com/phppackage/domaincheck) |
9
|
|
|
+-----------------------------------------------------------------------------+ |
10
|
|
|
| This source file is subject to MIT License |
11
|
|
|
| that is bundled with this package in the file LICENSE. |
12
|
|
|
| |
13
|
|
|
| If you did not receive a copy of the license and are unable to |
14
|
|
|
| obtain it through the world-wide-web, please send an email |
15
|
|
|
| to [email protected] so we can send you a copy immediately. |
16
|
|
|
+-----------------------------------------------------------------------------+ |
17
|
|
|
| Authors: |
18
|
|
|
| Lawrence Cherone <[email protected]> |
19
|
|
|
+-----------------------------------------------------------------------------+ |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace PHPPackage\DomainCheck; |
23
|
|
|
|
24
|
|
|
class Whois |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var |
28
|
|
|
*/ |
29
|
|
|
private $tlds = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
*/ |
34
|
3 |
|
public function __construct(array $tlds = []) |
35
|
|
|
{ |
36
|
3 |
|
$this->tlds = $tlds; |
37
|
3 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
4 |
|
private function filterServers(array $servers, array $tlds = []): array |
43
|
|
|
{ |
44
|
4 |
|
$tlds = array_merge($this->tlds, $tlds); |
45
|
|
|
|
46
|
4 |
|
if (empty($tlds)) { |
47
|
1 |
|
return $servers; |
48
|
|
|
} |
49
|
|
|
|
50
|
3 |
|
return array_values(array_filter($servers, function ($value) use ($tlds) { |
51
|
3 |
|
return in_array($value['tld'], $tlds); |
52
|
3 |
|
})); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* |
57
|
|
|
*/ |
58
|
6 |
|
private function loadServers(string $servers_file = 'whois-servers.json') |
59
|
|
|
{ |
60
|
6 |
|
$path = __DIR__.'/'.$servers_file; |
61
|
|
|
|
62
|
6 |
|
if (!file_exists($path) || !is_readable($path)) { |
63
|
1 |
|
throw new \RuntimeException('whois-servers.json does not exist or is not readable'); |
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
return json_decode(file_get_contents($path), true); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* |
71
|
|
|
*/ |
72
|
9 |
|
public function servers(array $tlds = [], bool $all = false, string $servers_file = 'whois-servers.json'): array |
73
|
|
|
{ |
74
|
9 |
|
$json = $this->loadServers($servers_file); |
75
|
|
|
|
76
|
8 |
|
if (empty($json) || !is_array($json)) { |
77
|
2 |
|
throw new \RuntimeException('invalid whois-servers.json file'); |
78
|
|
|
} |
79
|
|
|
|
80
|
6 |
|
if (!$all) { |
81
|
4 |
|
return $this->filterServers($json, $tlds); |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
return $json; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* |
89
|
|
|
*/ |
90
|
1 |
|
public function allServers(string $servers_file = 'whois-servers.json'): array |
91
|
|
|
{ |
92
|
1 |
|
return $this->servers([], true, $servers_file); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Socket connection to whois server. |
97
|
|
|
* |
98
|
|
|
* @param string $domain |
99
|
|
|
* @param string $server |
100
|
|
|
* @param string $findText |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
5 |
|
public function check($domain, $server, $pattern): bool |
104
|
|
|
{ |
105
|
|
|
// open socket to whois server |
106
|
5 |
|
$socket = @fsockopen($server, 43); |
107
|
|
|
|
108
|
5 |
|
if ($socket === false) { |
109
|
1 |
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// send the requested domain name |
113
|
4 |
|
fputs($socket, $domain."\r\n"); |
114
|
|
|
|
115
|
|
|
// read and store the server response |
116
|
4 |
|
$response = ' :'; |
117
|
|
|
|
118
|
4 |
|
while (!feof($socket)) { |
119
|
4 |
|
$response .= fgets($socket, 512); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// close the connection |
123
|
4 |
|
fclose($socket); |
124
|
|
|
|
125
|
|
|
// check the response stream whether the domain is available |
126
|
4 |
|
return stripos($response, $pattern) ? true : false; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|