Location: PHPKode > projects > web-cp - Web Hosting Control Panel > webcp/server/lib/pdns.wcp.phps
<?	/*
	// File:	pdns.wcp.phps
	// Purpose:	myydns domain manipulation
	// Author:	Peter Drake <hide@address.com>
	// Updated by:  Steve Kondik <hide@address.com> for PDNS
	*/

// Check to see if we are supposed to do anything
if ($type == "domain") {

	if ($data['action'] == "create") {

		// These have to happen before switching databases
		// so they are selected early - if this is a creation.

		// Select hosts
		$query="SELECT ip,host FROM domains WHERE id='".$data['id']."' AND action != 'remove'";
		$dbp = mysql_query($query);

		// Select the old domain name??
		$query="SELECT domain FROM domains WHERE id='".$data['id']."' AND action != 'remove'";
		$oldomain = mysql_query($query);
		$olddom = mysql_fetch_row($olddomain);
	}
	
	// Select MyDNS database as defined in config
	mysql_select_db($cfg['pdns_db']);
	
	// DNS admin email:
	$dns_admin_email = str_replace("@", ".", $cfg['dns_email']);
	
	//Create serial number
	$dns_serial = date("Ymd");
	
	// Make it unique
	$SQL = "SELECT MAX(id) as MaxID FROM domains";
	$res = mysql_query($SQL);
	
	if ($id = mysql_fetch_array($res)) {
		$dns_serial .= ($id["MaxID"] + 1);
	} else {
		$dns_serial .= date("His");
	}


switch($data["action"]) {
	case "suspend":
	case "unsuspend":
		// No action for suspend and unsuspend
		return;
		break;
	case "update":
	case "remove":
		// Select old SOA to work with
		$SQL = "SELECT id FROM domains WHERE name = '".$data["domain1"]."'";
		$res = mysql_query($SQL);
		if ($id = mysql_fetch_array($res)) {
			switch ($data['type']) {
				case "domain":
					// Delete old SOA
					$SQL = "DELETE FROM domains WHERE id = ".$id["id"];
					mysql_query($SQL);
			
					// Delete old RR
					$SQL = "DELETE FROM records WHERE domain_id = ".$id["id"];
					mysql_query($SQL);

					break;
				case "subdomain":
					// Delete subdomain
					$SQL = "DELETE FROM records WHERE domain_id = ".$id["id"]." AND name='".$data['host'].".".$data['domain']."'";
					mysql_query($SQL);
					break;
			}
		}
		if ($data["action"] == "remove") { break; }
	case "create":
		switch ($data['type']) {
			case "domain":
				// Select old SOA to work with
				$SQL = "SELECT id FROM domains WHERE origin = '".$olddom["domain"]."'";
				$res = mysql_query($SQL);
				if ($id = mysql_fetch_array($res)) {
					// Delete old SOA
					$SQL = "DELETE FROM domains WHERE id = ".$id["id"];
					mysql_query($SQL);
			
					// Delete old RR
					$SQL = "DELETE FROM records WHERE domain_id = ".$id["id"];
					mysql_query($SQL);
				}

				// create SOA record
				$SQL = "INSERT INTO domains(name, type) values ('" . $data["domain"] . "', 'NATIVE')";
				mysql_query($SQL);
				$SOAID = mysql_insert_id();
				
				// create SOA record
				$SQL = "INSERT INTO records(domain_id, name, type, content, ttl, prio) values (" . $SOAID . ", '" . $data["domain"] . "', 'SOA', '" . $cfg['dns_server1'] . " " . $dns_admin_email . " " . $dns_serial . " " . $cfg['dns_refresh'] . " " . $cfg['dns_retry'] . " " . $cfg['dns_expire'] . " " . $cfg['dns_ttl'] . "', '" . $cfg['dns_ttl'] . "', '0')";
				
				mysql_query($SQL);

				// create NS records
				for($i=1; $i<=4; $i++){
					if( !preg_match("/^$|none/", $cfg["dns_server".$i])){
						$SQL = "INSERT INTO records (domain_id, name, type, content, ttl) VALUES(";
							$SQL .= $SOAID.",'" . $data["domain"] . "','NS','".$cfg["dns_server".$i]."',".$cfg["dns_ttl"].")";
						mysql_query($SQL);
					}
				}
		
				// create MX records
				for($i=1; $i<=4; $i++){
					if( !preg_match("/^$|none/", $cfg["mail_exchanger".$i])){
						$SQL = "INSERT INTO records(domain_id, name, type, content, ttl, prio) VALUES(";
							$SQL .= $SOAID.",'" . $data["domain"] . "','MX','".$cfg["mail_exchanger".$i]."',".$cfg["dns_ttl"].", ". ($i * 10) .")";
						mysql_query($SQL);
					}
				}
		
				// create A records for hosts
				while ($ddata = mysql_fetch_array($dbp)) {
					$SQL = "INSERT INTO records(domain_id, name, type, content, ttl) VALUES(";
						$SQL .= $SOAID.",'".$ddata["host"].".".$data['domain']."','A','".$data["ip"]."',".$cfg["dns_ttl"].")";
					mysql_query($SQL);
				}

				// create A record for plain domain.ext if requested
				if($data['nohost'] == 'true'){
					$SQL = "INSERT INTO records(zone, name, type, content, ttl) VALUES(";
						$SQL .= $SOAID.",'".$data["domain"]."','A','".$data["ip"]."',".$cfg["dns_ttl"].")";
					mysql_query($SQL);
				}
		
				// create A record for hostname aliases
				if (!empty($data['aliases'])) {
				$tmp = explode(" ",$data['aliases']);
					foreach($tmp as $alias){
						$SQL = "INSERT INTO records(domain_id, name, type, content, ttl) VALUES(";
						$SQL .= $SOAID.",'".$alias."','A','".$data["ip"]."',".$cfg["dns_ttl"].")";
					mysql_query($SQL);
					}
				}
		
				// create A catchall record if requested
				if ($data['catchall'] == 'true'){
					$SQL = "INSERT INTO records(domain_id, name, type, content, ttl) VALUES(";
						$SQL .= $SOAID.",'*','A','".$data["ip"]."',".$cfg["dns_ttl"].")";
					mysql_query($SQL);
				}
				break;

			case "subdomain":
				// Select SOA to use
				$SQL = "SELECT id FROM domains WHERE name = '".$data["domain"]."'";
				$res = mysql_query($SQL);

				if ($id = mysql_fetch_array($res)) {
					// Check if subdomain already exists
					$SQL = "SELECT id FROM records WHERE domain_id = '".$id["id"]."' AND name='".$data["host"]."'";
					$oldsub = mysql_query($SQL);
					if ($osub = mysql_fetch_array($oldsub)) {
						// Delete old subdomain
						$SQL = "DELETE FROM records WHERE id = ".$osub["id"];
						mysql_query($SQL);
					}

					// Create subdomain
					$SQL = "INSERT INTO records(domain_id, name, type, content, ttl) VALUES(";
						$SQL .= $id['id'].",'".$data["host"].".".$data['domain']."','A','".$data["ip"]."',".$cfg["dns_ttl"].")";
					mysql_query($SQL);
				}
				break;
		}
	break;
		
}

	// Log completion
	webcp_log(2, 0, "pdns.wcp.phps", "PDNS entry for ".$data["domain"]." completed successfully. Type: ".$data['type'].". Action: ".$data['action'],0 , $echo);

	//Set to restart DNS
	//$restartdns = TRUE;

	// Select webcp database again
	mysql_select_db($cfg['dbname']);

}

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