Passed
Branch master (9edd41)
by Lawrence
01:29
created

Whois::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 +-----------------------------------------------------------------------------+
4
 | PHPPackage - Domain Checker
5
 +-----------------------------------------------------------------------------+
6
 | Copyright (c)2018 (http://github.com/phppackage/domaincheck)
7
 +-----------------------------------------------------------------------------+
8
 | This source file is subject to MIT License
9
 | that is bundled with this package in the file LICENSE.
10
 |
11
 | If you did not receive a copy of the license and are unable to
12
 | obtain it through the world-wide-web, please send an email
13
 | to [email protected] so we can send you a copy immediately.
14
 +-----------------------------------------------------------------------------+
15
 | Authors:
16
 |   Lawrence Cherone <[email protected]>
17
 +-----------------------------------------------------------------------------+
18
 */
19
20
namespace PHPPackage\DomainCheck;
21
22
class Whois
23
{
24
    /**
25
     * @var
26
     */
27
    private $tlds = [];
28
29
    /**
30
     *
31
     */
32 11
    public function __construct(array $tlds = [])
33
    {
34 11
        $this->tlds = $tlds;
35 11
    }
36
37
    /**
38
     *
39
     */
40 4
    private function filterServers(array $servers, array $tlds = [])
41
    {
42 4
        $tlds = array_merge($this->tlds, $tlds);
43
44 4
        if (empty($tlds)) {
45 1
            return $servers;
46
        }
47
48 3
        return array_values(array_filter($servers, function ($value) use ($tlds) {
49 3
            return in_array($value['tld'], $tlds);
50 3
        }));
51
    }
52
53
    /**
54
     *
55
     */
56 7
    public function servers(array $tlds = [], string $servers_file = 'whois-servers.json')
57
    {
58 7
        $path = __DIR__.'/'.$servers_file;
59
60 7
        if (!file_exists($path) || !is_readable($path)) {
61 1
            throw new \RuntimeException('whois-servers.json does not exist or is not readable');
62
        }
63
64 6
        $json = json_decode(file_get_contents($path), true);
65
66 6
        if (empty($json) || !is_array($json)) {
67 2
            throw new \RuntimeException('invalid whois-servers.json file');
68
        }
69
70 4
        return $this->filterServers($json, $tlds);
71
    }
72
73
    /**
74
     * Socket connection to whois server.
75
     *
76
     * @param string $domain
77
     * @param string $server
78
     * @param string $findText
79
     * @return bool
80
     */
81 5
    public function checkDomain($domain, $server, $pattern)
82
    {
83
        // open socket to whois server
84 5
        $socket = @fsockopen($server, 43);
85
86 5
        if (!$socket) {
87 1
            return false;
88
        }
89
90
        // send the requested domain name
91 4
        fputs($socket, $domain."\r\n");
92
93
        // read and store the server response
94 4
        $response = ' :';
95
96 4
        while (!feof($socket)) {
97 4
            $response .= fgets($socket, 256);
98
        }
99
        
100
        // close the connection
101 4
        fclose($socket);
102
103
        // check the response stream whether the domain is available
104 4
        return stripos($response, $pattern) ? true : false;
105
    }
106
}
107