-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUWhois.php
81 lines (64 loc) · 2.07 KB
/
UWhois.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
<?php
//https://github.com/Netrvin/UWhois
//v1.0.3
//License under MIT
// Learn some codes from regru/php-whois
class UWhois
{
//Connect to whois server
private function getWhois($domain, $host)
{
$fp = fsockopen($host, 43);
if (!$fp) {
return false;
}
fputs($fp, "$domain\r\n");
$string = '';
while (!feof($fp)) {
$string .= fgets($fp, 128);
}
fclose($fp);
$string_encoding = mb_detect_encoding($string, "UTF-8, ISO-8859-1, ISO-8859-15", true);
return mb_convert_encoding($string, "UTF-8", $string_encoding);
}
//Get whois server for a TLD
private function getWhoisServer($domain)
{
$res = $this->getWhois($domain, 'whois.iana.org');
if (!$res) {
return -1; //Connection error
}
$lines = explode(PHP_EOL, $res);
foreach ($lines as $line) {
if (strpos($line, 'whois') === 0) {
return str_replace(' ', '', substr($line, 6));
}
}
return -2; //No whois server
}
public function get($domain)
{
if (preg_match('/^([\p{L}\d\-]+)\.((?:[\p{L}\-]+\.?)+)$/ui', $domain, $matches) || preg_match('/^(xn\-\-[\p{L}\d\-]+)\.(xn\-\-(?:[a-z\d-]+\.?1?)+)$/ui', $domain, $matches)) {
$TLD = strtolower($matches[2]);
} else {
return 'Invalid domain name';
}
//Special TLDs
if (($TLD == 'int') || ($TLD == 'arpa')) {
$server = 'whois.iana.org';
} else {
$server = $this->getWhoisServer($domain);
}
if ($server === -1) {
return 'Cannot connect to whois.iana.org:43';
} elseif ($server === -2) {
return 'No whois server for this TLD or domain';
}
$res = $this->getWhois($domain, $server);
if (!$res) {
return 'Cannot connect to ' . $domain . ':43';
} else {
return $res;
}
}
}