<?php
    require_once 'Net/DNS2.php';
    if(isset($_GET['host'])) { 
        $ip = $_GET['host'];
        if(file_exists('dnsbl.json')) {
            $json = file_get_contents('dnsbl.json');
            $tempArr = json_decode($json,true);
            $host = gethostbyname($ip);

            if($host == $ip) {
                $parsedArr = $tempArr['ip'];
            } else {
                $parsedArr = $tempArr['host']; 
            }
            
        } else {
            echo "script execution stopped.";exit;
        }
    }
    $repeat = count($parsedArr);

    if(isset($_GET['id'])) {
        $id = $_GET['id'];
    }
    
    $msg = '';
    if ($ip === '') {
        $msg = 'No IP given';
    }

    // Make sure output buffering is turned off
    @ob_end_flush();

    // Message times repeat
    $reverse_ip = implode(".", array_reverse(explode(".", $ip)));
  //  $listed = '';
    if(isset($_GET['id'])) {
        $dnsblArr = arraySearch($parsedArr, 'Id',$id)[0];
        if(!empty($dnsblArr)) {
			
			
			
            $dnsr = $reverse_ip . "." . $dnsblArr['DnsZone'];
			
            $time_start = microtime(true);
         //   $listed = $dnsr."|".checkdnsrr($dnsr, "A");
			/*
            if (checkdnsrr($dnsr, "A")) {
                $listed = "Blacklisted";
                if(strpos(gethostbyname("www.".$dnsblArr['DnsZone']), "127.0.0") !== false) {
                    $listed = "Whitelisted";
                }
            } else {
                $listed = "Notlisted";
            }
			*/
			
		
 // $resolver = new Net_DNS2_Resolver(array('nameservers' => 
 /*
if($host == $ip){
$dns_ip = '208.67.222.222';
} else {
$dns_ip = '1.1.1.1';
}
*/

$resolver = new Net_DNS2_Resolver(array('nameservers' => 
array('208.67.222.222')));

$listed = false;
$a_data = null;
$txt_data = null;

try {

        $response = $resolver->query("$dnsr","A");   
        if (count(array($response))) {
                foreach ($response->answer as $rr) {
					$a_data = $rr->address;
				//	var_dump($dnsr .' -> '. $a_data);
					$listed = true;
                }
        }
		
		$response = $resolver->query("$dnsr","TXT");
		if (count(array($response))) {
                foreach ($response->answer as $rr) {
					$txt_data = $rr->text[0];
				//	var_dump($dnsr .' -> '. $txt_data);
					$listed = true;
                }
        }	

} catch(Net_DNS2_Exception $error)
{
 //        print_r($error);
}


		
		if($listed && (!empty($txt_data) && !empty($a_data))) {
			$listed = "Listed";
		} else {
            $listed = "Not Listed";
        }



            $time_end = microtime(true);
            $time = $time_end - $time_start;
            $time = ($time >= 1.0) ? round($time, 2) . 's' : round($time * 1000, 2) . 'ms';
            dataToStreamEvent(
                'item',
                [
                    'name' => $dnsblArr['Name'],
                    'host' => $dnsblArr['DnsZone'],
                    'a_r' => $a_data,
                    'txt_r' => $txt_data,
                    'listed' => $listed,
                    'time' => $time,
                    'url' => $dnsblArr['Url']
                ]
            );
        }
    } else {
        $returnArr = ["success" => 1 ,"count" => count($parsedArr)];
        foreach ($parsedArr as $key => $value) {
            $dnsbl_data[] = $value['DnsZone']."|".$value['Id'];
        }
        $returnArr['result'] = $dnsbl_data; 
        echo json_encode($returnArr);exit;
    }

/**
 * @param string $type
 * @param array $data
 * @param string $id
 * @return string text/event-stream formatted string
 */
function dataToStreamEvent($type, array $data, $id = '') {
    $result = [
        "success" => true,
        "event" => $type,
        "id" => $id,
        "result" => $data
    ];
    echo json_encode($result);exit;
}

function arraySearch($array, $key, $value)
{
    $results = array();
    if (is_array($array)) {
        if (isset($array[$key]) && $array[$key] == $value) {
            $results[] = $array;
        }
        foreach ($array as $subarray) {
            $results = array_merge($results, arraySearch($subarray, $key, $value));
        }
    }
    return $results;
}