|
1
|
|
|
<?php |
|
2
|
|
|
namespace MallardDuck\Whois; |
|
3
|
|
|
|
|
4
|
|
|
use TrueBV\Punycode; |
|
5
|
|
|
use MallardDuck\Whois\Exceptions\MissingArgException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* The Whois Client Class. |
|
9
|
|
|
* |
|
10
|
|
|
* @author mallardduck <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* @copyright lucidinternets.com 2018 |
|
13
|
|
|
* |
|
14
|
|
|
* @version 0.4.0 |
|
15
|
|
|
*/ |
|
16
|
|
|
class Client extends AbstractWhoisClient |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* The Unicode for IDNA. |
|
20
|
|
|
* @var \TrueBV\Punycode |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $punycode; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The parsed input after validating and encoding. |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
public $parsedInput; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Construct the Whois Client Class. |
|
32
|
|
|
*/ |
|
33
|
52 |
|
public function __construct() |
|
34
|
|
|
{ |
|
35
|
52 |
|
$this->punycode = new Punycode(); |
|
36
|
52 |
|
} |
|
37
|
|
|
/** |
|
38
|
|
|
* Performs a Whois look up on the domain provided. |
|
39
|
|
|
* @param string $input The domain or ip being looked up via whois. |
|
40
|
|
|
* @param string $whoisServer The whois server to preform the lookup on. |
|
41
|
|
|
* |
|
42
|
|
|
* @return string The output of the Whois look up. |
|
43
|
|
|
*/ |
|
44
|
28 |
|
public function lookup(string $input = "", string $whoisServer = "") : string |
|
45
|
|
|
{ |
|
46
|
28 |
|
$this->validateLookupArgs($input, $whoisServer); |
|
47
|
22 |
|
$this->parseWhoisInput($input); |
|
48
|
|
|
|
|
49
|
22 |
|
return $this->makeRequest($this->parsedInput, $whoisServer); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Validates the input for the `lookup` method. |
|
54
|
|
|
* @param string $input A string that should represent a domain name or IP. |
|
55
|
|
|
* @param string $whoisServer A string that represents a domain or IP of a whois server. |
|
56
|
|
|
*/ |
|
57
|
34 |
|
private function validateLookupArgs(string $input = "", string $whoisServer = "") : void |
|
58
|
|
|
{ |
|
59
|
34 |
|
$primaryMissing = empty($input); |
|
60
|
34 |
|
$serverMissing = empty($whoisServer); |
|
61
|
34 |
|
if ($primaryMissing && $serverMissing) { |
|
62
|
4 |
|
throw new MissingArgException( |
|
63
|
4 |
|
"No input provided. Must provide both primary input (domain or IP) and whois server to this method." |
|
64
|
|
|
); |
|
65
|
30 |
|
} elseif ($primaryMissing) { |
|
66
|
4 |
|
throw new MissingArgException( |
|
67
|
4 |
|
"No primary input provided. Cannot lookup empty domain, or IP string." |
|
68
|
|
|
); |
|
69
|
26 |
|
} elseif ($serverMissing) { |
|
70
|
4 |
|
throw new MissingArgException( |
|
71
|
4 |
|
"No whois server provided. Must provide IP or domain for whois server with this method." |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
22 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Takes the user provided input and parses then encodes the string. |
|
78
|
|
|
* @param string $input The user provided input. |
|
79
|
|
|
* |
|
80
|
|
|
* @return string Returns the parsed whois input. |
|
81
|
|
|
*/ |
|
82
|
36 |
|
public function parseWhoisInput(string $input) : string |
|
83
|
|
|
{ |
|
84
|
|
|
// Check domain encoding |
|
85
|
36 |
|
$encoding = mb_detect_encoding($input); |
|
86
|
|
|
|
|
87
|
|
|
// Punycode the domain if it's Unicode |
|
88
|
36 |
|
if ("UTF-8" === $encoding) { |
|
89
|
10 |
|
$input = $this->punycode->encode($input); |
|
90
|
|
|
} |
|
91
|
36 |
|
$this->parsedInput = $input; |
|
92
|
|
|
|
|
93
|
36 |
|
return $input; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|