<?php require_once 'sessao.php'; 

$dominio = sql($_POST['dominio']);
if(strlen($dominio) == 0){
header("Location: criar.php?error");
exit;
}
$ip = sql($_POST['ipaddress']);
if(strlen($ip) == 0){
header("Location: criar.php?error");
exit;
}
$hostname = sql($_POST['hostname']);
if(strlen($hostname) == 0){
header("Location: criar.php?error");
exit;
}
$ip_port = $ip;
switch ($max_ip_perso){
	case 0:
	$total_and_max_perso = 300;
	break;
	default:
	$total_and_max_perso = $max_ip_perso - $total_ip_perso;
	break;
}
if($total_and_max_perso <= 0){
header("Location: criar.php?lim");
exit;
}else{ 
	$query_dominio = mysql_query("SELECT * FROM `domains` WHERE `id`='$dominio'");
	$fetch_dominio = mysql_fetch_assoc($query_dominio);
	$host = $hostname.'.'.$fetch_dominio['dominio'];
	if($fetch_dominio['id_user'] <> 0){
		if($fetch_dominio['id_user'] <> $id_user){
			header("Location: criar.php?error");
			exit;
		}
	}
	$num = mysql_query("SELECT * FROM `tsdns` WHERE `host`='$host'");
	if(mysql_num_rows($num) <> 0){
		header("Location: criar.php?error");
		exit;
	}
	$num = mysql_query("SELECT * FROM `tsdns` WHERE `host`='$hostname' AND `id_dominio`='$dominio'");
	if(mysql_num_rows($num) <> 0){
		header("Location: criar.php?error");
		exit;
	}
	$num = mysql_query("SELECT * FROM `tsdns` WHERE `ip_port`='$ip_port' AND `host`='$hostname' AND `id_dominio`='$dominio'");
	if(mysql_num_rows($num) == 0){
		$query = mysql_query("INSERT INTO `tsdns` (`id_user`, `id_dominio`, `host`, `ip_port`) VALUES ('$id_user', '$dominio', '$host', '$ip_port')");
		if($query){
			$query_recu = mysql_query("SELECT * FROM `tsdns` WHERE `host`='$host' AND `ip_port`='$ip_port' AND `id_user`='$id_user'");
			$fetch_recu = mysql_fetch_assoc($query_recu);
			$id_recu = $fetch_recu['id'];
			$query_final = mysql_query("INSERT INTO `jtsdns` (`hostname`, `ipaddress`, `enabled`, `lastused`, `usecount`, `id_user`, `id_tsdns`) VALUES ('$host', '$ip_port', '1', '0', '0', '$id_user', '$id_recu')");
			if($query_final){
				header("Location: criar.php?sucesso");
				exit;
			}else{
				header("Location: criar.php?error");
				exit;
			}
		}else{
			header("Location: criar.php?error");
			exit;
		}
	}else{
		header("Location: criar.php?exit");
		exit;
	}
}

?>