Passed
Push — ipv6 ( 2a70ad...5f0900 )
by Richard
08:24
created

CachedRDnsLookupProvider::getReverseDNS()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 39
rs 8.9617
cc 6
nc 6
nop 1
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 * ACC Development Team. Please see team.json for a list of contributors.     *
5
 *                                                                            *
6
 * This is free and unencumbered software released into the public domain.    *
7
 * Please see LICENSE.md for the full licencing statement.                    *
8
 ******************************************************************************/
9
10
namespace Waca\Providers;
11
12
use Waca\DataObjects\RDnsCache;
13
use Waca\PdoDatabase;
14
use Waca\Providers\Interfaces\IRDnsProvider;
15
16
/**
17
 * Cached rDNS Lookup Provider
18
 *
19
 * Provides a service to look up the reverse DNS of an IP address, and caches
20
 * the result in the database.
21
 */
22
class CachedRDnsLookupProvider implements IRDnsProvider
23
{
24
    private $database;
25
26
    public function __construct(PdoDatabase $database)
27
    {
28
        $this->database = $database;
29
    }
30
31
    public function getReverseDNS($address)
32
    {
33
        $address = trim($address);
34
35
        // lets look in our cache database first.
36
        $rDns = RDnsCache::getByAddress($address, $this->database);
37
38
        if ($rDns instanceof RDnsCache) {
39
            // touch cache timer
40
            $rDns->save();
41
42
            return $rDns->getData();
43
        }
44
45
        // OK, it's not there, let's do an rDNS lookup.
46
        $ptrAddress = inet_pton($address);
47
        if ($ptrAddress === false) {
48
            return null; // Invalid IP address
49
        }
50
51
        $reversePointer = implode('.', array_reverse(explode('.', inet_ntop($ptrAddress)))) . '.in-addr.arpa';
52
        if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
53
            $reversePointer = implode('.', array_reverse(str_split(bin2hex($ptrAddress)))) . '.ip6.arpa';
0 ignored issues
show
Bug introduced by
It seems like str_split(bin2hex($ptrAddress)) can also be of type true; however, parameter $array of array_reverse() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
            $reversePointer = implode('.', array_reverse(/** @scrutinizer ignore-type */ str_split(bin2hex($ptrAddress)))) . '.ip6.arpa';
Loading history...
54
        }
55
56
        $dnsRecords = dns_get_record($reversePointer, DNS_PTR);
57
        if (!empty($dnsRecords) && isset($dnsRecords[0]['target'])) {
58
            $result = $dnsRecords[0]['target'];
59
60
            $rDns = new RDnsCache();
61
            $rDns->setDatabase($this->database);
62
            $rDns->setAddress($address);
63
            $rDns->setData($result);
64
            $rDns->save();
65
66
            return $result;
67
        }
68
69
        return null;
70
    }
71
}
72