Location: PHPKode > projects > web-cp - Web Hosting Control Panel > webcp/server/lib/tinydns.wcp.phps
<?	/*
	// File:	tinydns.wcp.phps
	// Purpose:	tinydns domain manipulation
	// Author:	Jez Hancock <hide@address.com>
	*/
// Nothing to do if action is (un)suspend:
if($data["action"] == "suspend" || $data["action"] == "unsuspend")
  return;

if ($type == "domain") {
	$curr_data_file=$cfg["basedir"]."/tinydns/root/".$data["domain"].".data";

	// default data file:
	$default_data_file=$cfg["basedir"]."/tinydns/root/data-default";

	// system master data file:
	$system_data_file=$cfg["tinydns_dir"]."/root/data";

	// DNS admin email:
	$dns_admin_email = str_replace("@", ".", $cfg['dns_email']);

	// Only need to create a data file for domain if $action is "create" or "update":
	if($data["action"] == "create" || $data["action"] == "update"){
		// data file for current domain:
		
		// Set the serial number:
		$dns_serial = date("Ymd");

		// Make serial unique (append update count for domain to it):
		$query = "SELECT max(ucount) AS ucount FROM domains WHERE domain='".$data['domain']."'";
		$dbp = mysql_query($query);
		if(!$dbp){
			// Log error:
			webcp_log(2, 0, "tinydns.wcp.phps", "Unable to execute query: $query" ,0 , $echo);

			// Just append hour/mins/secs to serial:
			$dns_serial.=date("His");
		} else {
			$ddata = mysql_fetch_array($dbp);
			$dns_serial = date("Ymd").sprintf("%02d", $ddata['ucount']);
		}

		// 72 #'s :)
		$mark="#######################################################################";

		// Build up the data file for this $data["domain"]
		// SOA record:
		$tinydns_data=	$mark."\n# $data[domain]\n# Created by webcp - do not edit by hand\n".$mark."\n"
			."# SOA Record:\n"
			."Z".$data["domain"].":".$cfg['dns_server1']
			.":".$dns_admin_email.":".$dns_serial.":".$cfg["dns_refresh"]
			.":".$cfg["dns_retry"]."\n\n";
		
		// Add an entry for each namserver defined in config:
		for($i=1; $i<=4; $i++){
			if( !preg_match("/^$|none/", $cfg["dns_server".$i])){
				$i==1 && $tinydns_data.="# Nameserver record(s):\n";
				$tinydns_data.= ".".$data["domain"]."::".$cfg["dns_server".$i]
					.":".$cfg["dns_ttl"]."\n";
			}
		}
		$tinydns_data.="\n";
		
		// Add an entry for each mailserver defined in config:
		for($i=1; $i<=4; $i++){
			if( !preg_match("/^$|none/", $cfg["mail_exchanger".$i])){
				$i==1 && $tinydns_data.="# MX record(s):\n";
				$tinydns_data.=	"@".$data["domain"].":".$data["ip"].":".$cfg["mail_exchanger".$i].":".$i."\n";
			}
		}
		$tinydns_data.="\n";

		// Add the A/PTR records / subdomains:
		$query="SELECT ip,host FROM domains WHERE domain='".$data['domain']."' AND action != 'remove'";
		$dbp = mysql_query($query);
		if(!$dbp){
			webcp_log(2, 0, "tinydns.wcp.phps", "Unable to execute query: $query" ,0 , $echo);
		} else {
			$tinydns_data.="# A record(s):\n";
			while ($ddata = mysql_fetch_array($dbp)) {
				$tinydns_data.= "=".$ddata["host"].".".$data["domain"].":".$data["ip"]."\n";
			}
		}

		// If nohost supported:
		if($data["nohost"] === "true"){
			// Add a record for plain old domain.com:
			$tinydns_data.="=".$data["domain"].":".$data["ip"]."\n";
		}
		
		$tinydns_data.="\n";

		// Alias A records:
		if (!empty($data['aliases'])) {
			$tmp = explode(" ",$data['aliases']);
			$tinydns_data.="#A alias record(s):\n";
			foreach($tmp as $alias){
				$tinydns_data.="+".$alias.".".$data["domain"].":".$data["ip"]."\n";
			}
			$tinydns_data.="\n";
		}

		// If catchall supported:
		if ($data['catchall'] == 'true'){
			// Add wildcard record:
			$tinydns_data.="# Wildcard record:\n+*.".$data["domain"].":".$data["ip"]."\n";
		}

		// Write the conf file for $data["domain"] to disk:
		$fp=fopen($curr_data_file, "w");
		if(!$fp){
			// This should really throw out a mail to an admin:
			webcp_log(2, 0, "tinydns.wcp.phps", "could not open $curr_data_file for writing" ,0 , $echo);
			break 3;
		} else {
			// say 'created' or 'updated':
			webcp_log(2, 0, "tinydns.wcp.phps", $data["action"]."d tinydns data file: $curr_data_file" ,0 , $echo);
			fwrite($fp, $tinydns_data);
			
			fclose($fp);
		}
	}


	if($data["action"]=="create"){
		if(!file_exists($system_data_file)){
			// No system data file exists, check to see if we have a data-default file:
			if(file_exists($default_data_file)){
				// Read in the default data:
				$default_tinydns=implode("", file($default_data_file));
				$tinydns_data=$default_tinydns."\n\n".$tinydns_data;
				webcp_log(3, 0, "tinydns.wcp.phps", "Reading in default data from $default_data_file", 0, $echo);
			} else {
				webcp_log(2, 0, "tinydns.wcp.phps", "No tinydns data-default file found in ".$cfg["basedir"]."tinydns/data-default and no data file found in $system_data_file - this could cause problems", 0, $echo);
			}
		}

		// If in creation mode, just append to the system data file:
		$write_mode="a";
	} 

	if($data["action"] == "remove"){
		$rc=@unlink($curr_data_file);
		webcp_log(2, 0, "tinydns.wcp.phps", "Unlink of file $curr_data_file: ".($rc?"OK":"FAILED"), 0, $echo);
	}

	if($data["action"] == "update" || $data["action"] == "remove"){
		// Rebuild system datafile:
		$wcp_tinydns_dir = $cfg["basedir"]."/tinydns/root";
		$dp=opendir($wcp_tinydns_dir);

		if(!$dp){
			// Should throw out email to admin here:
			webcp_log(2, 0, "tinydns.wcp.phps", "Unable to open directory ".$cfg["basedir"]."tinydns/root for reading", 0, $echo);
			break 2;
		} else {
			while($file=readdir($dp)){
				if($file=="data-default"){
					$tinydns_data=implode("", file($wcp_tinydns_dir."/$file"))."\n\n".$tinydns_data;
				} 
				elseif ( preg_match("/^data-default|.*\.data$/", $file) && $file != $curr_data_file) {
					// append contents of $file to $tinydns_data:
					$tinydns_data.="\n\n".implode("", file($wcp_tinydns_dir."/$file"));
				}
			}
		}
		// and set write mode to 'clobber' a new data file:
		$write_mode="w";
	}

	// Do for all $data["action"]'s:
	// Write the data file back to the system:
	$fp=fopen($system_data_file, $write_mode);
	
	if(!$fp){
		// Should throw out email to admin here:
		webcp_log(2, 0, "tinydns.wcp.phps", "could not open $system_data_file for writing" ,0 , $echo);
	} else {
		fwrite($fp, "\n".$tinydns_data);
		fclose($fp);
		webcp_log(2, 0, "tinydns.wcp.phps", "system tinydns data file $system_data_file modified successfully" ,0 , $echo);
	}

	
	// Rebuild the tinydns data file:
	$restartdns = TRUE;
}
?>
Return current item: web-cp - Web Hosting Control Panel