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'; |
|
|
|
|
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
|
|
|
|