|
| 1 | +<?php |
| 2 | +/* |
| 3 | + This class is inteded to provide easy access to the DNSbl lists |
| 4 | + to help find open proxies and possible spammers |
| 5 | + I'll properly document the class later |
| 6 | +*/ |
| 7 | + |
| 8 | +class DNSbl { |
| 9 | + |
| 10 | + public $defaultbl = 'dnsbl.dronebl.org'; |
| 11 | + public $dnsbls = array(); |
| 12 | + |
| 13 | + /* |
| 14 | + Simple lookup method |
| 15 | + Checks if a ip is in the BL or not |
| 16 | + */ |
| 17 | + public function lookup($ip,$bl='dnsbl.dronebl.org',$type='A'){ |
| 18 | + /* This function will not check why a host is banned or do multiple |
| 19 | + checks but it will check if it exists which is normally enough. */ |
| 20 | + $ip_parts_array = explode(".",$ip); |
| 21 | + $reverse_ip = ""; // Create it so we can add to it in the loop |
| 22 | + for ($ip_segment = 3; $ip_segment >= 0; $ip_segment--) { |
| 23 | + // Reverse all the segments and create a valid a |
| 24 | + $reverse_ip .= $ip_parts_array[$ip_segment]."."; |
| 25 | + } |
| 26 | + // Now we need to look up (even if its basic lookup |
| 27 | + $lookup = $reverse_ip.$bl; |
| 28 | + |
| 29 | + return checkdnsrr($lookup,$type); |
| 30 | + } |
| 31 | + |
| 32 | + /* |
| 33 | + Find the reason from the DNS lookup |
| 34 | + */ |
| 35 | + private function dnsbl_reason($dns_lookup){ |
| 36 | + // We process the dns lookup and process loop till we get the first |
| 37 | + // result if we find none assume false |
| 38 | + $result = false; |
| 39 | + // This IS VERY BUGGY - Any thoughts would be helpful. |
| 40 | + for($i = 0; $i <= (count($dns_lookup)-1); $i++){ |
| 41 | + if(isset($dns_lookup[$i]['txt'])){ |
| 42 | + return $dns_lookup[$i]['txt']; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /* |
| 48 | + Converts ip address into the reverse address for dnsbl lookup |
| 49 | + */ |
| 50 | + protected function convert_ipaddr($ip){ |
| 51 | + $ip_parts_array = explode(".",$ip); |
| 52 | + $reverse_ip = ""; // Create it so we can add to it in the loop |
| 53 | + // Loop through the list till we can convert the reverse of the ip |
| 54 | + for ($ip_segment = 3; $ip_segment >= 0; $ip_segment--) { |
| 55 | + // Reverse all the segments and create a valid a |
| 56 | + $reverse_ip .= $ip_parts_array[$ip_segment]."."; |
| 57 | + } |
| 58 | + // Now we need to look up (even if its basic lookup) |
| 59 | + return $reverse_ip; |
| 60 | + } |
| 61 | + |
| 62 | + /* |
| 63 | + Gets the default DNSbl from the variable defaultbl |
| 64 | + */ |
| 65 | + private function default_dnsbl(){ |
| 66 | + return $this->defaultbl; |
| 67 | + } |
| 68 | + |
| 69 | + /* |
| 70 | + Used to look through the DNSbls and check if the ip is found |
| 71 | + in any. This is very dangerous because not all BLs might be |
| 72 | + updated. |
| 73 | + */ |
| 74 | + private function lookup_loop($ip){ |
| 75 | + $return_array = array(); |
| 76 | + foreach($this->dnsbls as $bl){ |
| 77 | + // Return the bool result of the lookup |
| 78 | + $return_array[$bl] = $this->lookup($ip,$bl); |
| 79 | + } |
| 80 | + return $return_array; |
| 81 | + } |
| 82 | + |
| 83 | + /* |
| 84 | + Function is used get the dnsbl lookups |
| 85 | + |
| 86 | + */ |
| 87 | + public function dnsbls($list){ |
| 88 | + if(is_array($list)){ |
| 89 | + $this->dnsbls = $list; |
| 90 | + } else { |
| 91 | + return false; |
| 92 | + } |
| 93 | + } |
| 94 | + /* |
| 95 | + method is used to check through a list of DNSbls |
| 96 | + |
| 97 | + This is very dangerous because not all BLs might be |
| 98 | + updated. |
| 99 | + */ |
| 100 | + public function multi_check($ip,$return_array=TRUE){ |
| 101 | + // Check for an array of DNSbls |
| 102 | + // If none are found use the default |
| 103 | + if(!is_array($this->dnsbls)){ |
| 104 | + $this->lookup($ip,$this->default_dnsbl()); |
| 105 | + } else { |
| 106 | + // We have an array and we loop through the results. |
| 107 | + return $this->lookup_loop($ip); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /* |
| 112 | + method used to check the reason for being banned |
| 113 | + it depends on getting accurate results from the DNS server |
| 114 | + use |
| 115 | + */ |
| 116 | + public function dnsbl_type_check($ip,$bl=NULL,$type=DNS_ALL){ |
| 117 | + if(!isset($bl)){ |
| 118 | + $bl = $this->default_dnsbl(); |
| 119 | + } |
| 120 | + $return_type = 'text'; |
| 121 | + /* This function will not check why a host is banned or do multiple |
| 122 | + checks but it will check if it exists which is normally enough. */ |
| 123 | + $lookup = $this->convert_ipaddr($ip).$bl; |
| 124 | + // Look up the dns record |
| 125 | + $result = dns_get_record($lookup,$type); |
| 126 | + // Check to make sure it worked |
| 127 | + //return $this->dnsbl_reason($result); |
| 128 | + if($result){ |
| 129 | + // Perhaps in another version return option bool or text |
| 130 | + if($return_type == 'bool'){ |
| 131 | + return true; |
| 132 | + } else { |
| 133 | + // and default + return |
| 134 | + return $this->dnsbl_reason($result); |
| 135 | + } |
| 136 | + } else { |
| 137 | + // If the value is invalid, its returned false |
| 138 | + return false; |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +?> |
0 commit comments