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
|
11 |
|
public function __construct(array $tlds = []) |
35
|
|
|
{ |
36
|
11 |
|
$this->tlds = $tlds; |
37
|
11 |
|
} |
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
|
7 |
|
public function servers(array $tlds = [], string $servers_file = 'whois-servers.json'): array |
59
|
|
|
{ |
60
|
7 |
|
$path = __DIR__.'/'.$servers_file; |
61
|
|
|
|
62
|
7 |
|
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
|
6 |
|
$json = json_decode(file_get_contents($path), true); |
67
|
|
|
|
68
|
6 |
|
if (empty($json) || !is_array($json)) { |
69
|
2 |
|
throw new \RuntimeException('invalid whois-servers.json file'); |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
return $this->filterServers($json, $tlds); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Socket connection to whois server. |
77
|
|
|
* |
78
|
|
|
* @param string $domain |
79
|
|
|
* @param string $server |
80
|
|
|
* @param string $findText |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
5 |
|
public function check($domain, $server, $pattern): bool |
84
|
|
|
{ |
85
|
|
|
// open socket to whois server |
86
|
5 |
|
$socket = @fsockopen($server, 43); |
87
|
|
|
|
88
|
5 |
|
if ($socket === false) { |
|
|
|
|
89
|
1 |
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// send the requested domain name |
93
|
4 |
|
fputs($socket, $domain."\r\n"); |
94
|
|
|
|
95
|
|
|
// read and store the server response |
96
|
4 |
|
$response = ' :'; |
97
|
|
|
|
98
|
4 |
|
while (!feof($socket)) { |
99
|
4 |
|
$response .= fgets($socket, 512); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// close the connection |
103
|
4 |
|
fclose($socket); |
104
|
|
|
|
105
|
|
|
// check the response stream whether the domain is available |
106
|
4 |
|
return stripos($response, $pattern) ? true : false; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|