Location: PHPKode > projects > TLD Manager > tldmgr/zone_write.php
#!/usr/bin/php -q
<?
$TLD = strtolower($argv[1]);

if (! $TLD) {
	echo "Please specify a TLD when calling this script.\n";
	exit;
}

include("include/config.php"); 
mysql_connect($db_host,$db_user,$db_pass);
@mysql_select_db($db_name) or die( "Unable to select database \"$db_name @ $db_host\"");


// Write the root record //
$result = mysql_query("SELECT * FROM root WHERE TLD='$TLD'");
if (mysql_numrows($result) == 0) {
	echo "Please supply a valid TLD when calling this script.\n";
	exit;
}

$SOA = mysql_result($result,0,"SOA");
$contact = mysql_result($result,0,"contact");
$TTL = mysql_result($result,0,"TTL");
$refresh = mysql_result($result,0,"refresh");
$retry = mysql_result($result,0,"retry");
$expire = mysql_result($result,0,"expire");
$filename = mysql_result($result,0,"filename");
$serial = date("Ymd") . "01";

echo "\$ORIGIN .\n";
echo "\$TTL $TTL\n";
echo "$TLD		IN SOA	$SOA.	$contact (\n";
echo "			$serial	; serial\n";
echo "			$refresh		; refresh\n";
echo "			$retry		; retry\n";
echo "			$expire		; expire\n";
echo "			$TTL		; TTL\n";
echo "			)\n";

$result = mysql_query("SELECT * FROM domain WHERE name='@' AND TLD='$TLD'");
$A = mysql_result($result,0,"A");
$CNAME = mysql_result($result,0,"CNAME");
$MX = mysql_result($result,0,"MX");
$NS = mysql_result($result,0,"NS");
$RP = mysql_result($result,0,"RP");
$TXT = mysql_result($result,0,"TXT");

while ($NS) {
	$x = strpos($NS, "||");
	if ($x==0) { $x=strlen($NS)+1; }
	$IP = substr($NS, 0, $x);
	$NS = substr($NS, $x+2);
	echo "		NS	" . cleanAddr($IP) . "\n";
}

$count=0;
while ($A) {
	$x = strpos($A, "||");
	if ($x==0) { $x=strlen($A)+1; }
	$y = strpos($A, "=");
	$name = substr($A, 0, $y);
	$IP = substr($A, $y+1, $x-$y-1);
	$A = substr($A, $x+2);
	if ($name) {
		$A_name[$count] = $name;
		$A_IP[$count] = cleanAddr($IP);
		$count++;
	} else {
		echo "		A	" . cleanAddr($IP) . "\n";
	}
}

while ($MX) {
	$x = strpos($MX, "||");
	if ($x==0) { $x=strlen($MX)+1; }
	$y = strpos($MX, "=");
	$value = substr($MX, 0, $y);
	$IP = substr($MX, $y+1, $x-$y-1);
	$MX = substr($MX, $x+2);
	echo "		MX	$value	" . cleanAddr($IP) . "\n";
}

// Add in the extras specific to the TLD //

echo ";\n";
echo "\$ORIGIN $TLD.\n";
if (count($A_name)) {
	for ($c=0; $c<count($A_name); $c++) {
		echo $A_name[$c];
		if (strlen($A_name[$c]) < 9) echo "	";
		echo "	A	" . $A_IP[$c] . "\n";
	}
}

while ($CNAME) {
	$x = strpos($CNAME, "||");
	if ($x==0) { $x=strlen($CNAME)+1; }
	$y = strpos($CNAME, "=");
	$name = substr($CNAME, 0, $y);
	$ref = substr($CNAME, $y+1, $x-$y-1);
	$CNAME = substr($CNAME, $x+2);
	echo $name;
	if (strlen($name) < 9) echo "	";
	echo "	CNAME	$ref";
	$test="";
	for ($c=0; $c<count($A_name); $c++) {
		if ($A_name[$c] == $ref) $test=TRUE;
	}
	if (! $test) echo ".";
	echo "\n";
}

// Loop through and write each domain //

echo ";\n";
echo "\$ORIGIN $TLD.\n";
$result = mysql_query("SELECT * FROM domain ORDER BY name");
while ($row = mysql_fetch_assoc($result)) {
  // echo "--- Name: " . $row['name'] . "\n";
	if (($row['name'] != "") && ($row['name'] != "@")) {
	  $first=""; 

		// Main domain info
		while ($row['NS']) {
			$x = strpos($row['NS'], "||");
			if ($x==0) { $x=strlen($row['NS'])+1; }
			$IP = substr($row['NS'], 0, $x);
			$row['NS'] = substr($row['NS'], $x+2);
			$first = PrintLine("NS", $row['name'], cleanAddr($IP), "", $first);
		}
		$count=0;
		while ($row['A']) {
			$x = strpos($row['A'], "||");
			if ($x==0) { $x=strlen($row['A'])+1; }
			$y = strpos($row['A'], "=");
			$name = substr($row['A'], 0, $y);
			$IP = substr($row['A'], $y+1, $x-$y-1);
			$row['A'] = substr($row['A'], $x+2);
			if ($name) {
				$A_name[$count] = $name;
				$A_IP[$count] = cleanAddr($IP);
				$count++;
			} else {
				$first = PrintLine("A", $row['name'], cleanAddr($IP), "", $first);
			}
		}
		while ($row['MX']) {
			$x = strpos($row['MX'], "||");
			if ($x==0) { $x=strlen($row['MX'])+1; }
			$y = strpos($row['MX'], "=");
			$value = substr($row['MX'], 0, $y);
			$IP = substr($row['MX'], $y+1, $x-$y-1);
			$row['MX'] = substr($row['MX'], $x+2);
			$first = PrintLine("MX", $row['name'], $value, cleanAddr($IP), $first);
		}

		while ($row['RP']) {
			$x = strpos($row['RP'], "||");
			if ($x==0) { $x=strlen($row['RP'])+1; }
			$info = substr($row['RP'], 0, $x);
			$row['RP'] = substr($row['RP'], $x+2);
			$first = PrintLine("RP", $row['name'], $info, "", $first);
		}
		while ($row['TXT']) {
			$x = strpos($row['TXT'], "||");
			if ($x==0) { $x=strlen($row['TXT'])+1; }
			$info = substr($row['TXT'], 0, $x);
			$row['TXT'] = substr($row['TXT'], $x+2);
			$first = PrintLine('TXT', $row['name'], '"'.$info.'"', '', $first);
		}


		// Info requiring $ORIGIN
		$firstSUB = "\$ORIGIN " . $row['name'] . ".$TLD.\n";
		
		while ($row['nameserver']) {
			$x = strpos($row['nameserver'], "||");
			if ($x==0) $x=strlen($row['nameserver'])+1;
			$y = strpos($row['nameserver'], "=");
			$name = substr($row['nameserver'], 0, $y);
			$IP = substr($row['nameserver'], $y+1, $x-$y-1);
			$row['nameserver'] = substr($row['nameserver'], $x+2);
			$firstSUB = PrintSubline("A", $name, cleanAddr($IP), "", $firstSUB);
		}		
		if (count($A_name)) {
			for ($c=0; $c<count($A_name); $c++) {
				$firstSUB = PrintSubline("A", $A_name[$c], $A_IP[$c], "", $firstSUB);
			}
			unset($A_name);
			unset($A_IP);
		}

		while ($row['CNAME']) {
			$x = strpos($row['CNAME'], "||");
			if ($x==0) { $x=strlen($row['CNAME'])+1; }
			$y = strpos($row['CNAME'], "=");
			$name = substr($row['CNAME'], 0, $y);
			if ($name == "") $name = "*";
			$IP = substr($row['CNAME'], $y+1, $x-$y-1);
			$row['CNAME'] = substr($row['CNAME'], $x+2);
			$firstSUB = PrintSubline("CNAME", $name, $IP, "", $firstSUB);
		}


		// Subdomain info
		// Print subdomain info here
		

		if (($first) || (! $firstSUB)) echo ";\n";
		if (! $firstSUB) echo "\$ORIGIN $TLD.\n";
	}
}

mysql_close();


function cleanAddr($info) {
	if (($binIp = ip2long($info)) === false) $info = "$info.";
	return "$info";
}

function PrintLine ($type, $name, $info1, $info2, $firsttime) {
	if ($firsttime)	echo "		$type	$info1	$info2\n";
	else {
		echo $name;
		if (strlen($name) < 9) echo "	";
		echo "	$type	$info1	$info2\n";
		$firsttime = TRUE;
	}
	return $firsttime;
}
function PrintSubline ($type, $name, $info1, $info2, $firsttime) {
	echo $firsttime;
	echo $name;
	if (strlen($name) < 9) echo "	";
	echo "	$type	$info1	$info2\n";
	return "";
}

?>
Return current item: TLD Manager