Location: PHPKode > projects > web-cp - Web Hosting Control Panel > webcp/server/lib/bind.wcp.phps
<?	/*
	// File:	bind.wcp.phps
	// Purpose:	named domain manipulation
	// Author:	Johan <hide@address.com>
	*/

   
// domain system manipulation

// Handle domains manipulation
if ($type == "domain") {
	if ($data['type'] == 'subdomain') {
		$data2 = fetchdata("*","domain",$data['owner']);
		$data['nohost'] = $data2['nohost'];
		$data['aliases'] = $data2['aliases'];
		$data['catchall'] = $data2['catchall'];
	}
	// Generate Bind 8 configuration
	// open domain.named.tpl
	$file = $cfg['basedir']."/headers/domain.named.tpl";
	$namedconf = stripslashes(fread(fopen($file, "r"), filesize($file)));
	// Replace variables by values 

	$namedconf = str_replace('{TTL}', $cfg['dns_ttl'], $namedconf);
	$namedconf = str_replace('{DOMAIN}', $data['domain'], $namedconf);
	// change @ by . in the DNS admin e-mail 
	$tmp = str_replace("@", ".", $cfg['dns_email']);
	$namedconf = str_replace('{ADMMAIL}', $tmp, $namedconf);
	// serial  ( todays date + todays serial => YYYYMMDD + domain count )
	$dbp = mysql_query("SELECT max(ucount) AS ucount FROM domains WHERE domain='".$data['domain']."'");
	$ddata = mysql_fetch_array($dbp);
	$tmp = date("Ymd").sprintf("%02d", $ddata['ucount']);

	$namedconf = str_replace('{SERIAL}', $tmp, $namedconf);
	$namedconf = str_replace('{REFRESH}', $cfg['dns_refresh'], $namedconf);
	$namedconf = str_replace('{RETRY}', $cfg['dns_retry'], $namedconf);
	$namedconf = str_replace('{EXPIRE}', $cfg['dns_expire'], $namedconf);
	$namedconf = str_replace('{NS1}', $cfg['dns_server1'], $namedconf);
	$namedconf = str_replace('{NS2}', $cfg['dns_server2'], $namedconf);
	$namedconf = str_replace('{NS3}', $cfg['dns_server3'], $namedconf);
	$namedconf = str_replace('{NS4}', $cfg['dns_server4'], $namedconf);
	$namedconf = str_replace('{SERVER}', $cfg['sysname'], $namedconf);
	$namedconf = str_replace('{GPOS}', $cfg['dns_gpos'], $namedconf);

	// If nohost is supported
	if ($data['nohost'] == 'true'){
		$tmp = $data['domain'].".\tIN\tA\t".$data['ip']."\t; Nohost\n";
		$namedconf = str_replace('{NOHOST}', $tmp, $namedconf);
	}
	else
		$namedconf = str_replace('{NOHOST}', "", $namedconf);

	// Find every sub-domain and add it
	unset($tmp);
	$dbp = mysql_query("SELECT ip,host FROM domains WHERE domain='".$data['domain']."' AND action != 'remove'");
	while ($ddata = mysql_fetch_array($dbp)) {
		$tmp .= $ddata['host']."\tIN\tA\t".$ddata['ip']."\n";
	}
	$namedconf = str_replace('{SUBDOMAINS}', $tmp, $namedconf);

	// If there are aliases
	if ($data['aliases']) {
		$tmp = explode(" ",$data['aliases']);
		while (current($tmp)) {
			$tmp2 .= current($tmp)."\tIN\tCNAME\t".$data['domain'].".\t\t; Aliases\n";
			next($tmp);
		}
		$namedconf = str_replace('{ALIAS}', $tmp2, $namedconf);
		unset($tmp2);
	}
	else
		$namedconf = str_replace('{ALIAS}', "", $namedconf);

	// If catchall is supported
	if ($data['catchall'] == 'true'){
		$tmp = "*\tIN\tA\t".$data['ip']."\t\t; catchall\n";
		$namedconf = str_replace('{CATCHALL}', $tmp, $namedconf);
	}
	else 
		$namedconf = str_replace('{CATCHALL}', "", $namedconf);

	// Close namedd conf file
	$namedconf .= "\n;end of the file\n";


	//
	//

	// Handle Domain Creation :: expect *
	if ($data['action'] == 'create') {
		
		// Write the named config file
		$namedinclude = $cfg['basedir']."/named/include/".$data['domain'].".named";
		$fp = fopen($namedinclude,"w+");
		fwrite($fp,$namedconf,2048);
		fclose($fp);
		// Set permissions
		chmod($namedinclude,0644);
		// Set NAMED server restart
		$restartdns = TRUE;
	}
	
	// Handle Domain updates :: expect *
	elseif ($data['action'] == 'update') {
	
		// Compare old and newly generated named config file, write if necessary
		$namedinclude = $cfg['basedir']."/named/include/".$data['domain1'].".named";
		if (file_exists($namedinclude))
			$oldconf = implode("",file($namedinclude));
		else
			$oldconf = " ";
		if ($data['host'] != $data['host1'] OR $data['domain'] != $data['domain1'] OR crc32($oldconf) != crc32($namedconf)) {
			// if domain name changed, check if older file must be removed
			if ($data['domain'] != $data['domain1']) {
				$dbp = mysql_query("SELECT id FROM domains WHERE domain = '".$data['domain1']."'");
				if (!mysql_num_rows($dbp)) {
					$namedinclude = $cfg['basedir']."/named/include/".$data['domain1'].".named";
					if (file_exists($namedinclude))
						unlink($namedinclude);
				}
			}
			// Write the named config file
			$namedinclude = $cfg['basedir']."/named/include/".$data['domain'].".named";
			$fp = fopen($namedinclude,"w+");
			fwrite($fp,$namedconf,2048);
			fclose($fp);
			// Set permissions
			chmod($namedinclude,0644);
			// Set NAMED server restart
			$restartdns = TRUE;
		}	
	}
	
	// Remove Domain :: expect *
	elseif ($data['action'] == "remove") {

		// remove old config file only if this (old) domain is not used anymore.
		$dbp = mysql_query("SELECT id FROM domains WHERE domain='".$data['domain']."' AND action != 'remove'");
		if (!mysql_num_rows($dbp)) {
			$namedinclude = $cfg['basedir']."/named/include/".$data['domain'].".named";
			if (file_exists($namedinclude))
				unlink($namedinclude);
		}
		else {
			// Write the named config file
			$namedinclude = $cfg['basedir']."/named/include/".$data['domain'].".named";
			$fp = fopen($namedinclude,"w+");
			fwrite($fp,$namedconf,2048);
			fclose($fp);
			// Set permissions
			chmod($namedinclude,0644);
		}

		// Set NAMED server restart
		$restartdns = TRUE;
	}
}

?>
Return current item: web-cp - Web Hosting Control Panel