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

// Check to see if we are supposed to do anything
if ($type == "domain") {
	
	if ($data['action'] == "create" || $data['action'] == "update") {
		
		// These have to happen before switching databases
		// so they are selected early - if this is a creation (or update).

		// Select hosts
		$query = "SELECT ip, host FROM domains WHERE (id='".$data['id']."' OR owner='".$data['id']."') AND domain = '".$data['domain']."' 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
	if (!@mysql_select_db($cfg['mydns_db'])) {
		webcp_log(1, 0, "mydns.wcp.phps", "Failed to select mydns database: ".$cfg['mydns_db'],0 , $echo);
	} else {
		webcp_log(3, 0, "mydns.wcp.phps", "Database selected: ".$cfg['mydns_db'],0 , $echo);
	}

	// DNS admin email:
	$dns_admin_email = str_replace("@", ".", $cfg['dns_email']);
	if (strrpos($dns_admin_email, ".") != strlen($dns_admin_email)) {
		$dns_admin_email .= ".";
	}
	
	//Create serial number
	$dns_serial = date("Ymd");
	
	// Make it unique
	$SQL = "SELECT MAX(id) as MaxID FROM soa";
	$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
		//Set to restart DNS
		$restartdns = FALSE;
		break;
	case "update":
	case "remove":
		// Select old SOA to work with
		$SQL = "SELECT id FROM soa WHERE origin = '".$data["domain1"].".'";
		$res = mysql_query($SQL);
		if ($id = mysql_fetch_array($res)) {
			switch ($data['type']) {
				case "pointer":
				case "domain":
					// Delete old SOA
					$SQL = "DELETE FROM soa WHERE id = ".$id["id"];
					mysql_query($SQL);
			
					// Delete old RR
					$SQL = "DELETE FROM rr WHERE zone = ".$id["id"];
					mysql_query($SQL);

					break;
				case "subdomain":
					// Delete subdomain
					$SQL = "DELETE FROM rr WHERE zone = ".$id["id"]." AND name='".$data['host']."'";
					mysql_query($SQL);
					break;
			}
		}
		//Set to restart DNS
		$restartdns = TRUE;
		if ($data["action"] == "remove") { break; }
	case "create":
		switch ($data['type']) {
			case "pointer":
			case "domain":
				// Select old SOA to work with
				$SQL = "SELECT id FROM soa WHERE origin = '".$olddom["domain"].".'";
				$res = mysql_query($SQL);
				if ($id = mysql_fetch_array($res)) {
					// Delete old SOA
					$SQL = "DELETE FROM soa WHERE id = ".$id["id"];
					mysql_query($SQL);
			
					// Delete old RR
					$SQL = "DELETE FROM rr WHERE zone = ".$id["id"];
					mysql_query($SQL);
				}
				
				if (strrpos($cfg["dns_server1"], ".") != strlen($cfg["dns_server1"])) {
					$dns_server1 = $cfg["dns_server1"].".";
				} else {
					$dns_server1 = $cfg["dns_server1"];
				}

				// create SOA record
				$SQL = "INSERT INTO soa(origin, ns, mbox, serial, refresh, retry, minimum, ttl) VALUES('";
					$SQL .= $data["domain"].".','".$dns_server1."','".$dns_admin_email."',".$dns_serial.",".$cfg["dns_refresh"].",";
					$SQL .= $cfg['dns_retry'].",".$cfg['dns_minttl'].",".$cfg['dns_ttl'].")";
				mysql_query($SQL);
				$SOAID = mysql_insert_id();
		
				// create NS records
				for($i=1; $i<=4; $i++){
					if( !preg_match("/^$|none/", $cfg["dns_server".$i])){
						if (strrpos($cfg["dns_server".$i], ".") != strlen($cfg["dns_server".$i])) {
							$dns_server = $cfg["dns_server".$i].".";
						} else {
							$dns_server = $cfg["dns_server".$i];
						}
						$SQL = "INSERT INTO rr(zone, type, data, ttl) VALUES(";
							$SQL .= $SOAID.",'NS','".$dns_server."',".$cfg["dns_ttl"].")";
						mysql_query($SQL);
					}
				}
		
				// create MX records
				for($i=1; $i<=4; $i++){
					if( !preg_match("/^$|none/", $cfg["mail_exchanger".$i])){
						if (strrpos($cfg["mail_exchanger".$i], ".") != strlen($cfg["mail_exchanger".$i])) {
							$mail_server = $cfg["mail_exchanger".$i].".";
						} else {
							$mail_server = $cfg["mail_exchanger".$i];
						}
						$SQL = "INSERT INTO rr(zone, type, data, ttl) VALUES(";
							$SQL .= $SOAID.",'MX','".$mail_server."',".$cfg["dns_ttl"].")";
						mysql_query($SQL);
					}
				}
		
				// create A records for hosts
				while ($ddata = mysql_fetch_array($dbp)) {
					$SQL = "INSERT INTO rr(zone, name, type, data, ttl) VALUES(";
						$SQL .= $SOAID.",'".$ddata["host"]."','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 rr(zone, name, type, data, 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 rr(zone, name, type, data, 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 rr(zone, name, type, data, ttl) VALUES(";
						$SQL .= $SOAID.",'*','A','".$data["ip"]."',".$cfg["dns_ttl"].")";
					mysql_query($SQL);
				}
				
				// create all pre-existing subdomains
				
				break;

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

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

					// Create subdomain
					$SQL = "INSERT INTO rr(zone, name, type, data, ttl) VALUES(";
						$SQL .= $id['id'].",'".$data["host"]."','A','".$data["ip"]."',".$cfg["dns_ttl"].")";
					mysql_query($SQL);
				}
				break;
		}
		//Set to restart DNS
		$restartdns = TRUE;
	break;
}

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

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

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