<?php


if (isset ($_SERVER['HTTP_X_FORWARDED_FOR']))
     $uip = ($_SERVER['HTTP_X_FORWARDED_FOR']);
       else
            $uip = ($_SERVER["REMOTE_ADDR"]);


$blacklists = ['blog.bl.zonecheck.org', 'sbl-xbl.spamhaus.org'];
$blacklists6 = ['blog.bl.zonecheck.org', 'six.bl.zonecheck.org', 'sbl-xbl.spamhaus.org']; 



if (filter_var($uip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {

    $rev = implode('.', array_reverse(explode('.', $uip)));

    foreach ($blacklists as $bl) {
        $check = "$rev.$bl";
        if ($check != gethostbyname($check)) {
            $reas = dns_get_record($check, DNS_TXT);
            $Rreas = $reas[0]['txt'] ?? 'Listed';
            $blpid = getmypid();
            error_log("PHP Security: [DNSBL] WWW ($blpid) - $uip - $bl - $Rreas");
            die('<font color=red><center><big><big><big><strong>ACCESS DENIED</strong></big><p>Listed in DNSBL<p>Feature is currently not available to you.</big></big></center></font>');

        }
    }

 } elseif (filter_var($uip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {

    $expanded = inet_ntop(inet_pton($uip));
    $rev = implode('.', array_reverse(str_split(str_replace(':', '', bin2hex(inet_pton($expanded))))));

    foreach ($blacklists6 as $bl) {
        $check = "$rev.$bl";
        if ($check != gethostbyname($check)) {
            $reas = dns_get_record($check, DNS_TXT);
            $Rreas = $reas[0]['txt'] ?? 'Listed';
            $blpid = getmypid();
            error_log("PHP Security: [DNSBL] WWW ($blpid) - $uip - $bl - $Rreas");
            die('<font color=red><center><big><big><big><strong>ACCESS DENIED</strong></big><p>Listed in DNSBL<p>Feature is currently not available to you.</big></big></center></font>');

        }
    }

 } else {

    error_log("PHP Security: Invalid IP format - $uip");

}



//  ipv4 works fine, I think it works for ipv6, I dont see much 6 traffic let alone 6 jackasses to really tell

?>
