-
Notifications
You must be signed in to change notification settings - Fork 190
/
Whois.php
201 lines (166 loc) · 6.11 KB
/
Whois.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
namespace Phois\Whois;
class Whois
{
private $domain;
private $TLDs;
private $subDomain;
private $servers;
/**
* @param string $domain full domain name (without trailing dot)
*/
public function __construct($domain)
{
$this->domain = $domain;
// check $domain syntax and split full domain name on subdomain and TLDs
if (
preg_match('/^([\p{L}\d\-]+)\.((?:[\p{L}\-]+\.?)+)$/ui', $this->domain, $matches)
|| preg_match('/^(xn\-\-[\p{L}\d\-]+)\.(xn\-\-(?:[a-z\d-]+\.?1?)+)$/ui', $this->domain, $matches)
) {
$this->subDomain = $matches[1];
$this->TLDs = $matches[2];
} else
throw new \InvalidArgumentException("Invalid $domain syntax");
// setup whois servers array from json file
$this->servers = json_decode(file_get_contents( __DIR__.'/whois.servers.json' ), true);
}
public function info()
{
if ($this->isValid()) {
$whois_server = $this->servers[$this->TLDs][0];
// If TLDs have been found
if ($whois_server != '') {
// if whois server serve replay over HTTP protocol instead of WHOIS protocol
if (preg_match("/^https?:\/\//i", $whois_server)) {
// curl session to get whois reposnse
$ch = curl_init();
$url = $whois_server . $this->subDomain . '.' . $this->TLDs;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
if (curl_error($ch)) {
return "Connection error!";
} else {
$string = strip_tags($data);
}
curl_close($ch);
} else {
// Getting whois information
$fp = fsockopen($whois_server, 43);
if (!$fp) {
return "Connection error!";
}
$dom = $this->subDomain . '.' . $this->TLDs;
fputs($fp, "$dom\r\n");
// Getting string
$string = '';
// Checking whois server for .com and .net
if ($this->TLDs == 'com' || $this->TLDs == 'net') {
while (!feof($fp)) {
$line = trim(fgets($fp, 128));
$string .= $line;
$lineArr = explode (":", $line);
if (strtolower($lineArr[0]) == 'whois server') {
$whois_server = trim($lineArr[1]);
}
}
// Getting whois information
$fp = fsockopen($whois_server, 43);
if (!$fp) {
return "Connection error!";
}
$dom = $this->subDomain . '.' . $this->TLDs;
fputs($fp, "$dom\r\n");
// Getting string
$string = '';
while (!feof($fp)) {
$string .= fgets($fp, 128);
}
// Checking for other tld's
} else {
while (!feof($fp)) {
$string .= fgets($fp, 128);
}
}
fclose($fp);
}
$string_encoding = mb_detect_encoding($string, "UTF-8, ISO-8859-1, ISO-8859-15", true);
$string_utf8 = mb_convert_encoding($string, "UTF-8", $string_encoding);
return htmlspecialchars($string_utf8, ENT_COMPAT, "UTF-8", true);
} else {
return "No whois server for this tld in list!";
}
} else {
return "Domain name isn't valid!";
}
}
public function htmlInfo()
{
return nl2br($this->info());
}
/**
* @return string full domain name
*/
public function getDomain()
{
return $this->domain;
}
/**
* @return string top level domains separated by dot
*/
public function getTLDs()
{
return $this->TLDs;
}
/**
* @return string return subdomain (low level domain)
*/
public function getSubDomain()
{
return $this->subDomain;
}
public function isAvailable()
{
$whois_string = $this->info();
$not_found_string = '';
if (isset($this->servers[$this->TLDs][1])) {
$not_found_string = $this->servers[$this->TLDs][1];
}
$whois_string2 = @preg_replace('/' . $this->domain . '/', '', $whois_string);
$whois_string = @preg_replace("/\s+/", ' ', $whois_string);
$array = explode (":", $not_found_string);
if ($array[0] == "MAXCHARS") {
if (strlen($whois_string2) <= $array[1]) {
return true;
} else {
return false;
}
} else {
if (preg_match("/" . $not_found_string . "/i", $whois_string)) {
return true;
} else {
return false;
}
}
}
public function isValid()
{
if (
isset($this->servers[$this->TLDs][0])
&& strlen($this->servers[$this->TLDs][0]) > 6
) {
$tmp_domain = strtolower($this->subDomain);
if (
preg_match("/^[a-z0-9\-]{3,}$/", $tmp_domain)
&& !preg_match("/^-|-$/", $tmp_domain) //&& !preg_match("/--/", $tmp_domain)
) {
return true;
}
}
return false;
}
}