<?php require_once('../../Connections/netboxManagerMitru.php'); ?>
<?

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}


function tokenize($sext)
	{
	$hash=md5($sext."-JDA_MITRU:PBX");
	$hash=md5(substr($hash, 16, 10)."JHSDUY83JSDF873QDKMG83".substr($hash, 0, 10));
	$hash=md5("JDA".$hash);
	return $hash;
    }
mysql_select_db($database_netboxManagerMitru, $netboxManagerMitru);

if (isset($_POST["usuario"]))
	{
	$usuario=$_POST["usuario"];
	$token=$_POST["token"];
	$nombre=$_POST["nombre"];
	$apellido=$_POST["apellido"];
	$hab=$_POST["hab"];
	$email=$_POST["email"];
	$celular=$_POST["celular"];
	$idioma=$_POST["idioma"];
	if (isset($_POST["netbox"])) $netbox=$_POST["netbox"];
	else $netbox=0;
	}

if (isset($_GET["usuario"]))
	{
	$usuario=$_GET["usuario"];
	$token=$_GET["token"];
	$nombre=$_GET["nombre"];
	$apellido=$_GET["apellido"];
	$hab=$_GET["hab"];
	$email=$_GET["email"];
	$celular=$_GET["celular"];
	$idioma=$_GET["idioma"];
	if (isset($_GET["netbox"])) $netbox=$_GET["netbox"];
	else $netbox=0;
	}
///////////////////////////////
if ($usuario!="NetboxMitru" || $token!="W0OKjdJUH1j5InOsw9YNGH72DzneSqMm")
	{
	echo("ERR-10 Credenciales incorrectas (U/T) netbox Manager");
	exit();
	}
////////////PARAMETROS //////////
$bw_standard=7500; //kbps
$bwtext="STANDARD";
$secret="!mitruHotel_06072018!";
$ippbx="192.168.22.2";
$usuariopbx="jcadmin";
$passpbx="Sapoop2382";

$checkHabOc="SELECT * FROM hab_check WHERE hab_nrohab=$hab AND hab_activo=1";
$xcheckHabOc=mysql_query($checkHabOc);
$cantPaxHab=mysql_num_rows($xcheckHabOc);
if ($cantPaxHab>1) $habNew=false;
else $habNew=true;
$insertSQL = sprintf("INSERT INTO hab_check (hab_bw, hab_nombre, hab_apellido, hab_email, hab_idioma, hab_nrohab) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($bwtext, "text"),
                       GetSQLValueString($nombre, "text"),
                       GetSQLValueString($apellido, "text"),
                       GetSQLValueString($email, "text"),
                       GetSQLValueString($idioma, "text"),
                       GetSQLValueString($hab, "int"));

  //mysql_select_db($database_netboxManagerMitru, $netboxManagerMitru);
  $Result1 = mysql_query($insertSQL, $netboxManagerMitru) or die(mysql_error());



$codigoRadius=$hab.genusername($apellido);
$insertSQL2 = "INSERT INTO radcheck VALUES (NULL, '$codigoRadius', 'Cleartext-Password', ':=', '$codigoRadius')";
//mysql_select_db($database_RadiusNetbox, $RadiusNetbox);
$Result12 = mysql_query($insertSQL2, $netboxManagerMitru) or die(mysql_error());
$maxbw=$bw_standard*1024;
$insBW1="INSERT INTO radreply VALUES (NULL, '$codigoRadius', 'WISPr-Bandwidth-Max-Down', ':=', '$maxbw')";
$newBW1=mysql_query($insBW1);
$insBW2="INSERT INTO radreply VALUES (NULL, '$codigoRadius', 'WISPr-Bandwidth-Max-Up', ':=', '$maxbw')";
$newBW2=mysql_query($insBW2);
//$urlPBX="http://$ippbx/jda_scripts/add_new_ext.php?hab=$hab&nombre=$nombre&apellido=$apellido&email=$email&idioma=$idioma";
if ($habNew)
	{
	$token2=tokenize($hab);
	$nombre=urlencode($nombre);
	$apellido=urlencode($apellido);
	$email=urlencode($email);
	$idioma=urlencode($idioma);
	$AddNewExt=file_get_contents("http://$ippbx/jda_scripts/Pre_CheckinTelHab.php?hab=$hab&token=$token2&nombre=$nombre&apellido=$apellido&email=$email&idioma=$idioma");
	}

//if ($netbox==1) header("Location: ");
?>