Issues (4)

src/Client.php (1 issue)

1
<?php
2
3
namespace MallardDuck\Whois;
4
5
use League\Uri\Components\Domain;
6
use League\Uri\Components\Host;
0 ignored issues
show
The type League\Uri\Components\Host was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use League\Uri\Components\UserInfo;
8
use Pdp\Rules;
9
use TrueBV\Punycode;
10
use MallardDuck\Whois\WhoisServerList\AbstractLocator;
11
use MallardDuck\Whois\WhoisServerList\DomainLocator;
12
use MallardDuck\Whois\Exceptions\MissingArgException;
13
use MallardDuck\Whois\Exceptions\UnknownWhoisException;
14
15
/**
16
 * The Whois Client Class.
17
 *
18
 * @author mallardduck <[email protected]>
19
 *
20
 * @copyright lucidinternets.com 2018
21
 *
22
 * @version 1.0.0
23
 */
24
class Client extends AbstractWhoisClient
25
{
26
    /**
27
     * The TLD Whois locator class.
28
     * @var AbstractLocator
29
     */
30
    protected $whoisLocator;
31
32
    /**
33
     * The Unicode for IDNA.
34
     * @var Punycode
35
     */
36
    protected $punycode;
37
38
    /**
39
     * The input domain provided by the user.
40
     * @var string
41
     */
42
    public $inputDomain;
43
44
    /**
45
     * The parsed domain after validating and encoding.
46
     * @var string
47
     */
48
    public $parsedDomain;
49
50 70
    /**
51
     * Construct the Whois Client Class.
52 70
     */
53 70
    public function __construct()
54 70
    {
55
        $this->punycode = new Punycode();
56
        $this->whoisLocator = new DomainLocator();
57
    }
58
59
    /**
60
     * A unicode safe method for making whois requests.
61
     *
62
     * The main difference with this method is the benefit of punycode domains.
63
     *
64
     * @param string $domain      The domain or IP being looked up.
65
     * @param string $whoisServer The whois server being queried.
66
     *
67
     * @return string              The raw results of the query response.
68 2
     * @throws Exceptions\SocketClientException
69
     * @throws MissingArgException
70 2
     */
71
    public function makeSafeWhoisRequest(string $domain, string $whoisServer): string
72 2
    {
73
        $this->parseWhoisDomain($domain);
74
        // Form a socket connection to the whois server.
75
        return $this->makeWhoisRequest($this->parsedDomain, $whoisServer);
76
    }
77
78
    /**
79
     * Uses the League Uri Hosts component to get the search able hostname in PHP 5.6 and 7.
80
     *
81
     * @param string $domain The domain or IP being looked up.
82 60
     *
83
     * @return string
84
     */
85 60
    protected function getSearchableHostname(string $domain): string
86
    {
87 56
        $publicSuffixList = Rules::fromPath(dirname(__DIR__) . '/blobs/public_suffix_list.dat');
88 56
        $domain = $publicSuffixList->resolve(trim($domain, '.'));
89
90 14
        if (
91
            true !== empty($domain->subDomain()->toString()) &&
92 42
            false === strpos($domain->subDomain()->toString(), '.')
93
        ) {
94
            return $domain->domain()->toString();
95
        }
96
97
98
        // Attempt to parse the domains Host component and get the registrable parts.
99
        return $domain->registrableDomain()->toString();
100
    }
101
102
    /**
103 62
     * Takes the user provided domain and parses then encodes just the registerable domain.
104
     *
105 62
     * @param string $domain The user provided domain.
106 2
     *
107
     * @return string Returns the parsed domain.
108 60
     * @throws MissingArgException
109
     */
110 60
    protected function parseWhoisDomain(string $domain): string
111 56
    {
112 8
        $this->inputDomain = $domain;
113
114
        $processedDomain = $this->getSearchableHostname($domain);
115
        if ('' === $processedDomain) {
116 48
            throw new UnknownWhoisException("Unable to parse input to searchable hostname.");
117 10
        }
118
119 48
        // Punycode the domain if it's Unicode
120
        if ("UTF-8" === mb_detect_encoding($domain)) {
121 48
            $processedDomain = $this->punycode->encode($processedDomain);
122
        }
123
        $this->parsedDomain = $processedDomain;
124
125
        return $processedDomain;
126
    }
127
128
    /**
129
     * Performs a Whois look up on the domain provided.
130
     *
131
     * @param ?string $domain The domain being looked up via whois.
132
     *
133
     * @return string         The output of the Whois look up.
134 42
     * @throws Exceptions\SocketClientException
135
     * @throws Exceptions\UnknownWhoisException
136 42
     * @throws MissingArgException
137 4
     */
138
    public function lookup(string $domain): string
139 38
    {
140
        if ('' === $domain) {
141
            throw new MissingArgException("Input domain must be provided and cannot be an empty string.");
142 32
        }
143
        $this->parseWhoisDomain($domain);
144
145 22
        // Get the domains whois server.
146
        $whoisServer = $this->whoisLocator->getWhoisServer($this->parsedDomain);
147
148
        // Get the full output of the whois lookup.
149
        return $this->makeWhoisRequest($this->parsedDomain, $whoisServer);
150
    }
151
}
152