1
|
|
|
<?php |
2
|
|
|
namespace MallardDuck\Whois; |
3
|
|
|
|
4
|
|
|
use TrueBV\Punycode; |
5
|
|
|
use League\Uri\Components\Host; |
6
|
|
|
use Hoa\Socket\Client as SocketClient; |
7
|
|
|
use MallardDuck\Whois\WhoisServerList\AbstractLocator; |
8
|
|
|
use MallardDuck\Whois\WhoisServerList\DomainLocator; |
9
|
|
|
use MallardDuck\Whois\Exceptions\MissingArgException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The Whois Client Class. |
13
|
|
|
* |
14
|
|
|
* @author mallardduck <[email protected]> |
15
|
|
|
* |
16
|
|
|
* @copyright lucidinternets.com 2018 |
17
|
|
|
* |
18
|
|
|
* @version 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
class BaseClient |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The TLD Whois locator class. |
25
|
|
|
* @var AbstractLocator |
26
|
|
|
*/ |
27
|
|
|
protected $whoisLocator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The Unicode for IDNA. |
31
|
|
|
* @var \TrueBV\Punycode |
32
|
|
|
*/ |
33
|
|
|
protected $punycode; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The carriage return line feed character comobo. |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $clrf = "\r\n"; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The input domain provided by the user. |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
public $inputDomain; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The parsed domain after validating and encoding. |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
public $parsedDomain; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Construct the Whois Client Class. |
55
|
|
|
*/ |
56
|
99 |
|
public function __construct() |
57
|
|
|
{ |
58
|
99 |
|
$this->punycode = new Punycode(); |
59
|
99 |
|
$this->whoisLocator = new DomainLocator(); |
60
|
99 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* A unicode safe method for making whois requests. |
64
|
|
|
* |
65
|
|
|
* The main difference with this method is the benefit of punycode domains. |
66
|
|
|
* |
67
|
|
|
* @param string $domain The domain or IP being looked up. |
68
|
|
|
* @param string $whoisServer The whois server being queried. |
69
|
|
|
* |
70
|
|
|
* @return string The raw results of the query response. |
71
|
|
|
*/ |
72
|
33 |
|
public function makeWhoisRequest($domain, $whoisServer) |
73
|
|
|
{ |
74
|
33 |
|
$this->parseWhoisDomain($domain); |
75
|
|
|
// Form a socket connection to the whois server. |
76
|
33 |
|
return $this->makeWhoisRawRequest($this->parsedDomain, $whoisServer); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* A function for making a raw Whois request. |
81
|
|
|
* @param string $domain The domain or IP being looked up. |
82
|
|
|
* @param string $whoisServer The whois server being queried. |
83
|
|
|
* |
84
|
|
|
* @return string The raw results of the query response. |
85
|
|
|
*/ |
86
|
33 |
|
public function makeWhoisRawRequest($domain, $whoisServer) |
87
|
|
|
{ |
88
|
|
|
// Form a tcp socket connection to the whois server. |
89
|
33 |
|
$client = new SocketClient('tcp://'.$whoisServer.':43', 10); |
90
|
33 |
|
$client->connect(); |
91
|
|
|
// Send the domain name requested for whois lookup. |
92
|
33 |
|
$client->writeString($domain.$this->clrf); |
93
|
|
|
// Read the full output of the whois lookup. |
94
|
33 |
|
$response = $client->readAll(); |
95
|
|
|
// Disconnect the connections to prevent network/performance issues. |
96
|
33 |
|
$client->disconnect(); |
97
|
|
|
|
98
|
33 |
|
return $response; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Uses the League Uri Hosts component to get the search able hostname in PHP 5.6 and 7. |
103
|
|
|
* |
104
|
|
|
* @param string $domain The domain or IP being looked up. |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
78 |
|
protected function getSearchableHostname($domain) |
109
|
|
|
{ |
110
|
|
|
// Attempt to parse the domains Host component and get the registrable parts. |
111
|
78 |
|
$host = new Host($domain); |
112
|
69 |
|
return $host->getRegistrableDomain(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Takes the user provided domain and parses then encodes just the registerable domain. |
117
|
|
|
* @param string $domain The user provided domain. |
118
|
|
|
* |
119
|
|
|
* @return string Returns the parsed domain. |
120
|
|
|
*/ |
121
|
81 |
|
protected function parseWhoisDomain($domain) |
122
|
|
|
{ |
123
|
81 |
|
if (empty($domain)) { |
124
|
3 |
|
throw new MissingArgException("Must provide a domain name when using lookup method."); |
125
|
|
|
} |
126
|
78 |
|
$this->inputDomain = $domain; |
127
|
|
|
|
128
|
|
|
// Check domain encoding |
129
|
78 |
|
$encoding = mb_detect_encoding($domain); |
130
|
|
|
|
131
|
78 |
|
$processedDomain = $this->getSearchableHostname($domain); |
132
|
|
|
|
133
|
|
|
// Punycode the domain if it's Unicode |
134
|
69 |
|
if ("UTF-8" === $encoding) { |
135
|
15 |
|
$processedDomain = $this->punycode->encode($processedDomain); |
136
|
|
|
} |
137
|
69 |
|
$this->parsedDomain = $processedDomain; |
138
|
|
|
|
139
|
69 |
|
return $processedDomain; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|