#!/usr/bin/env php
<?php
	$root = substr(__DIR__,0,strrpos(__DIR__,'/'));
	require_once($root."/dronebl.config.php");
	require_once($frameworks_path."/DroneBL/Database.framework");
	require_once($frameworks_path."/DroneBL/Cron.framework");
	require_once($frameworks_path."/DroneBL/Logging.framework");
	require_once($frameworks_path."/DroneBL/Validation.framework");

	chkprog('Buildzone Generator');

	$cachefile = "/home/dronebl/rsync.dronebl.org/buildzone.combined";
	$counter = 0;
	$ttl = 300;
	$dbarr = [];

	function dbarr_add($ip, $type)
	{
		global $dbarr, $counter;		
		$type = intval($type);
		if (strpos($ip, '/') > -1)
			$ipv = is_valid_ip(substr($ip, 0, strpos($ip, '/')));
		else
			$ipv = is_valid_ip($ip);
		if (!$ipv) return;
		$dbarr['t'.$ipv]['t'.$type][] = $ip.($ipv==6&&strpos($ip,'/')===false?'/128':'');
		$counter++;
	}
	
	// TTL record
	$data = "\$TTL ".$ttl.PHP_EOL;
	
	// NS record
	$res = get_db_rows("SELECT name FROM rblservers WHERE active=1");
	if (db_num_rows($res))
	{
		$dns = [];
		while ($row = get_db_tuple($res))
			$dns[] = $row['name'];
		$data .= "\$NS 1w ".implode(' ', $dns).PHP_EOL;
	}
	
	// SOA record
	$data .= "\$SOA ".$ttl." ns.scarynet.org outsider.scarynet.org 0 600 300 86400 300".PHP_EOL;

	// Initialize dbarr
	$res = get_db_rows("SELECT classid, description FROM classes ORDER BY classid ASC");
	while($tuple = get_db_tuple($res))
	{
		$class = intval($tuple['classid']);
		foreach([4,6] as $fam)
		{
			$dbarr['t'.$fam]['t'.$class] = [];
			$dbarr['t'.$fam]['t'.$class][] = ":".$class.":".$tuple['description'];
			if ($class > 2)
				$dbarr['t'.$fam]['t'.$class][] = ($fam==6 ? '::FFFF:7F00:'.hexdec($class) : '127.0.0.'.$class);
		}
	}	
	
	dbarr_add('127.0.0.2',1);
	dbarr_add('::FFFF:7F00:2',1);
	
	// Blacklist
	$ips = [];
	$res = get_db_rows("SELECT DISTINCT(ip,type) FROM blacklist WHERE listed=1");
	if (db_num_rows($res))
	{
		while($tuple = get_db_tuple($res))
		{
			list($ip, $type) = explode(',', substr($tuple['row'], 1, -1));
			if (!isset($ips[$ip]))
			{
				$ips[$ip] = 1;
				dbarr_add($ip, $type);
			}
		}
	}
	
	// Blacklist_cidr
	$res = get_db_rows("SELECT cidr, type FROM blacklist_cidr WHERE listed=1");
	if (db_num_rows($res))
	{
		while($tuple = get_db_tuple($res))
			dbarr_add($tuple['cidr'], $tuple['type']);
	}

	foreach([4,6] as $fam)
	{
		$data .= "\$DATASET ip".($fam==6 ? '6trie' : '4set').':dnsbl @'.PHP_EOL;
		$res = get_db_rows("SELECT ip,description FROM whitelist WHERE ip::text LIKE '%".($fam===6?':':'.')."%'");
		if (db_num_rows($res))
		{
			while($tuple = get_db_tuple($res))
				$data .= "!".$tuple['ip'].($fam==6 && strpos($tuple['ip'],'/') === false ? '/128':'').PHP_EOL;
		}
		foreach(array_keys($dbarr['t'.$fam]) AS $name)
			$data .= implode(PHP_EOL, $dbarr['t'.$fam][$name]).PHP_EOL;
	}
		
	file_put_contents($cachefile . ".new", $data);
	rename($cachefile . ".new", $cachefile);
	conlog('Zone contains '.$counter.' entries.');
	// _activity_log_add(0, 1, "Zone files generated");
	endprog();
?>
