<?php

/* nictool bind export script  - created by Mihai Secasiu - http://patchlog.com */


$db_name="sim_nictool";
$db_user="sim_nictool";
$db_pass="822E8gsW9TNKXA";
$db_host="localhost";

$zones_dir="data/"; // directory used for storing the zone files, the script will delete the zone that are set as deleted from this folder. 
$zone_template="{ZONE_NAME}.zone";  //  template used for zone filename. {ZONE_NAME} will be replaced by script with the domain name
$conf_file="data/zones.conf"; // file where the script will write the zone entries. The script will truncate the file on open 



if($zones_dir[strlen($zones_dir)-1]!='/'){
	$zones_dir.='/';
}

if(!($cf=fopen($conf_file,'w'))){
	die("cannot open configuration file: $conf_file\n");
}

function get_ns($nsid)
{
	global $nameservers;
	if(isset($nameservers[$nsid]))return $nameservers[$nsid];
	$sql="SELECT name,ttl,description FROM nt_nameserver where nt_nameserver_id=".intval($nsid)." and deleted!='1'";
	if(!($res=mysql_query($sql))){
		die("query: $sql: failed with error: ".mysql_error()."\n");
	}
	$nameservers[$nsid]=mysql_fetch_array($res);
	return $nameservers[$nsid];
}


if(!($dbl=mysql_connect($db_host,$db_user,$db_pass))){
	die("cound not connect to the database server\n");
}

mysql_select_db($db_name, $dbl) or die("Could not select database: $db_name\n");


if(count($argv)>1){
	if(get_ns($argv[1])){
		$ns=$argv[1];
		$nsq="AND (ns0 = $ns OR ns1 = $ns OR ns2 = $ns OR ns3 = $ns OR ns4 = $ns OR ns5 = $ns 
			OR ns6 = $ns OR ns7 = $ns OR ns8 = $ns OR ns9 = $ns) ";
	}else{
		die("nameserver id invalid or nameserver deleted\n");
	}
	
}{
	$nsq="";
}


$c=0;
$sql="SELECT * FROM nt_zone $nsq";
if(!($res=mysql_query($sql))){
	die("query: $sql: failed with error: ".mysql_error()."\n");
}
while($z=mysql_fetch_assoc($res)){
	
	$fn=$zones_dir.preg_replace('/\{ZONE_NAME\}/',$z['zone'],$zone_template);
	if($z['deleted']=="1"){
		if(is_file($fn)){
			unlink($fn);
			echo "Deleted zone file for zone ${z['zone']}\n";
		}
		continue;
	} 

	echo "Generating zone file for domain: ".$z['zone']." -";
	$tns="";
	for($i=0;$i<=9;$i++){
		if($z['ns'.$i] && ($ns=get_ns($z['ns'.$i])) ){
			if(!$ns0)$ns0=$ns['name'];
			$tns.="@	${ns['ttl']} IN NS	${ns['name']} ; ${ns['description']}\n";
		}
	}
	$t="; ${z['description']}
\$ttl ${z['ttl']}
@	IN	SOA	$ns0	${z['mailaddr']} (
			${z['serial']}
			${z['refresh']}
			${z['retry']}
			${z['expire']}
			${z['minimum']} )

$tns
";
		

	$sql="SELECT * FROM nt_zone_record where nt_zone_id=".$z['nt_zone_id']. " and deleted!='1'";
	
	if(!($rres=mysql_query($sql))){
		die("query: $sql: failed with error: ".mysql_error()."\n");
	}
	while($r=mysql_fetch_assoc($rres)){
		$t.="${r['name']} ${r['ttl']} IN ${r['type']}";
		if(in_array($r['type'],array('A','AAAA','NS','CNAME'))){
			$t.="	${r['address']}";
		}elseif($r['type']=='TXT'){
			$t.="	\"${r['address']}\"";
		}elseif($r['type']=='MX'){
			$t.="	${r['weight']} ${r['address']}";
		}elseif($r['type']=='SRV'){
			$t.="	${r['priority']} ${r['weight']} ${r['other']} ${r['address']}";
		}
		$t.="	; ${r['description']}\n";
	}
	if(!file_put_contents($fn,$t)){
		die("error writing to file: $fn\n");
	}
	$ze="
zone \"${z['zone']}\" IN {
	type master;
	file \"$fn\";
	allow-update {none; };
	notify no;
};
";

	if(!fwrite($cf,$ze)){
		die("error writing to zones configuration file: $conf_file\n");
	}


	echo " file generated OK\n";
	$c++;
}


echo "Successfully exported $c domains. Exiting..\n";


