|
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\Locator; |
|
8
|
|
|
use MallardDuck\Whois\Exceptions\MissingArgException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* The Whois Client Class. |
|
12
|
|
|
* |
|
13
|
|
|
* @author mallardduck <[email protected]> |
|
14
|
|
|
* @copyright lucidinternets.com 2018 |
|
15
|
|
|
* @version 1.0.0 |
|
16
|
|
|
*/ |
|
17
|
|
|
class Client |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The TLD Whois locator class. |
|
22
|
|
|
* @var Locator |
|
23
|
|
|
*/ |
|
24
|
|
|
private $tldLocator; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The Unicode for IDNA. |
|
28
|
|
|
* @var TrueBV\Punycode |
|
|
|
|
|
|
29
|
|
|
*/ |
|
30
|
|
|
private $punycode; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
9 |
|
* The carriage return line feed character comobo. |
|
34
|
|
|
* @var string |
|
35
|
9 |
|
*/ |
|
36
|
9 |
|
private $clrf = "\r\n"; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The input domain provided by the user. |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
public $inputDomain; |
|
43
|
6 |
|
|
|
44
|
|
|
/** |
|
45
|
6 |
|
* The encoded domain after parsing with Punycode. |
|
46
|
3 |
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
public $encodedDomain; |
|
49
|
3 |
|
|
|
50
|
|
|
/** |
|
51
|
3 |
|
* Construct the Whois Client Class. |
|
52
|
3 |
|
*/ |
|
53
|
|
|
public function __construct() |
|
54
|
3 |
|
{ |
|
55
|
|
|
$this->punycode = new Punycode(); |
|
|
|
|
|
|
56
|
|
|
$this->tldLocator = new Locator; |
|
57
|
3 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Takes the user provided domain and parses then encodes just the registerable domain. |
|
61
|
|
|
* @param string $domain The user provided domain. |
|
62
|
|
|
* @return string Just the registrable part of a domain encoded for IDNA. |
|
63
|
|
|
*/ |
|
64
|
|
|
private function getRegistrableDomain($domain) |
|
65
|
|
|
{ |
|
66
|
|
|
$host = (new Host($domain))->getRegistrableDomain(); |
|
67
|
|
|
if ( strlen($host) === 0 && strlen($host) >= 0) { |
|
68
|
|
|
$host = $domain; |
|
69
|
|
|
} |
|
70
|
|
|
return $this->punycode->encode($host); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Performs a Whois look up on the domain provided. |
|
75
|
|
|
* @param string $domain The domain being looked up via whois. |
|
76
|
|
|
* @return string The output of the Whois look up. |
|
77
|
|
|
*/ |
|
78
|
|
|
public function lookup($domain = '') |
|
79
|
|
|
{ |
|
80
|
|
|
if (empty($domain)) { |
|
81
|
|
|
throw new MissingArgException("Must provide a domain name when using lookup method."); |
|
82
|
|
|
} |
|
83
|
|
|
$this->inputDomain = $domain; |
|
84
|
|
|
|
|
85
|
|
|
$this->encodedDomain = $this->getRegistrableDomain($domain); |
|
86
|
|
|
// Get the domains whois server. |
|
87
|
|
|
$whoisServer = $this->tldLocator->getWhoisServer($this->encodedDomain); |
|
88
|
|
|
// Form a socket connection to the whois server. |
|
89
|
|
|
$client = new SocketClient('tcp://' . $whoisServer . ':43', 10); |
|
90
|
|
|
$client->connect(); |
|
91
|
|
|
// Send the domain name requested for whois lookup. |
|
92
|
|
|
$client->writeString($this->encodedDomain . $this->clrf); |
|
93
|
|
|
// Read and return the full output of the whois lookup. |
|
94
|
|
|
$response = $client->readAll(); |
|
95
|
|
|
$client->disconnect(); |
|
96
|
|
|
|
|
97
|
|
|
return $response; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|