<?php
require_once 'sessao.php'; 

$jtsdns = sql($_GET['id']);
$tsdns = sql($_GET['ts']);
$query_jtsdns = mysql_query("SELECT * FROM `jtsdns` WHERE `id`='$jtsdns' AND `id_user`='$id_user'");
$query_tsdns = mysql_query("SELECT * FROM `tsdns` WHERE `id`='$tsdns' AND `id_user`='$id_user'");
if(mysql_num_rows($query_tsdns) == 0){
header("Location: index.php");
}
if(mysql_num_rows($query_jtsdns) == 0){
header("Location: index.php");
}

$dominio = sql($_POST['dominio']);
if(strlen($dominio) == 0){
header("Location: editar.php?id=$jtsdns&ts=$tsdns&1");
exit;
}
$ip = sql($_POST['ipaddress']);
if(strlen($ip) == 0){
header("Location: editar.php?id=$jtsdns&ts=$tsdns&2");
exit;
}
$hostname = sql($_POST['hostname']);
if(strlen($hostname) == 0){
header("Location: editar.php?id=$jtsdns&ts=$tsdns&3");
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: editar.php?id=$jtsdns&ts=$tsdns&add");
exit;
}else{
	$query_dominio = mysql_query("SELECT * FROM `domains` WHERE `id`='$dominio' AND `id_user`='$id_user' or `id_user`='0'");
	$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: editar.php?id=$jtsdns&ts=$tsdns&1");
			exit;
		}
	}
	$num = mysql_query("SELECT * FROM `tsdns` WHERE `ip_port`='$ip_port' AND `host`='$hostname' AND `id_dominio`='$dominio' AND `id`<>'$tsdns'");
	if(mysql_num_rows($num) == 0){
		$query = mysql_query("UPDATE `tsdns` SET  `id_dominio` =  '$dominio',`host` =  '$hostname',`ip_port` =  '$ip_port' WHERE `id` ='$tsdns' AND `id_user`='$id_user'");
		if($query){
			$query = mysql_query("UPDATE `jtsdns` SET  `hostname` =  '$host', `ipaddress` =  '$ip_port' WHERE `id` ='$jtsdns' AND `id_user`='$id_user'");
			if($query){
				header("Location: editar.php?id=$jtsdns&ts=$tsdns&sucesso");
				exit;
			}else{
				header("Location: editar.php?id=$jtsdns&ts=$tsdns&error");
				exit;
			}	
		}else{
			header("Location: editar.php?id=$jtsdns&ts=$tsdns&error");
			exit;
		}	
			}else{
			echo "rola";
			}
}

?>