<?php
if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
//tra
error_reporting(E_ERROR | E_PARSE);
$_SESSION['database'] = $_GET['database'];
$bd = $_SESSION['database'] . ".usuarios";
$pers = $_SESSION['database'] . ".personal";
$ind = $_GET['ind']; // 0 = usuario, 1 = usuario
$usuario = $_GET['usu'];
$pw = $_GET['pw'];

include 'conn.php';
include 'funciones.php';

//$json->bd = $bd;

$jsonData = array();
$jsonTempData = array();

/////////INICIO usuarios
/////////
/////////

if ($ind == "0") {
    $montoesql = "SELECT usu.u_id, usu.u_descripcion, usu.u_usuario,  AES_DECRYPT(pers.pers_hash, 'RC#TECH@') as u_clave, usu.u_activo
			FROM " . $bd . " usu join " . $pers . " pers on usu.pers_id = pers.pers_id where pers.pers_hash is not null and usu.u_activo = 1";

    $qur = $connection->query($montoesql);

    if ($_SESSION['codigo'] == "99") {

        $json->datos = "false";

    } else {
//logs($montoesql);
        $json->datos = "true";

        while ($r = mysqli_fetch_assoc($qur)) {

            $jsonTempData = array();
            $jsonTempData['u_id'] = $r['u_id'];
            $jsonTempData['u_descripcion'] = utf8_encode($r['u_descripcion']);
            $jsonTempData['u_usuario'] = utf8_encode($r['u_usuario']);
            $jsonTempData['u_clave'] = utf8_encode($r['u_clave']);
            $jsonTempData['u_acceso1'] = $r['u_acceso1'];
            $jsonTempData['u_activo'] = $r['u_activo'];

            $jsonData[] = $jsonTempData;
        }
    }

    $json->usuarios = $jsonData;

}
/////////
///////// FIN usuarios
/////////
/////////

/////////INICIO usuario
/////////
/////////

if ($ind == "1") {
    $jsonData = array();
    $jsonTempData = array();

    $montoesql = "SELECT usu.u_id, usu.u_descripcion, usu.u_usuario,  AES_DECRYPT(pers.pers_hash,
			'RC#TECH@') as u_clave, usu.u_activo FROM " . $bd . " usu join " . $pers . " pers on usu.pers_id = pers.pers_id
			 where pers.pers_hash is not null and usu.activo = 1 and usu.u_usuario = '" . $usuario . "' and
			 pers.pers_hash = AES_ENCRYPT('" . $pw . "','RC#TECH@')";

    $qur = $connection->query($montoesql);

    if ($_SESSION['codigo'] == "99") {

        $json->datos = "false";

    } else {
//logs($montoesql);
        $json->datos = "true";

        while ($r = mysqli_fetch_assoc($qur)) {

            $jsonTempData = array();
            $jsonTempData['u_id'] = $r['u_id'];
            $jsonTempData['u_descripcion'] = utf8_encode($r['u_descripcion']);
            $jsonTempData['u_acceso1'] = $r['u_acceso1'];
            $jsonTempData['u_activo'] = $r['u_activo'];

            $jsonData[] = $jsonTempData;
        }
    }

    $json->usuario = $jsonData;

}
/////////
///////// FIN usuario
/////////
/////////

/////////INICIO usuario global
/////////
/////////

if ($ind == "2") {
    $jsonData = array();
    $jsonTempData = array();

//$usuario = 'carlos';
    //$pw = '123456';

    $sql = "SELECT u_id, based FROM rctech_global.usuario_global
			 WHERE u_usuario = '" . $usuario . "'"; // and pass = AES_ENCRYPT('" .$pw. "','RC#TECH@')";

//$sql= "SELECT usu.u_id, usu.u_descripcion, usu.u_usuario,  AES_DECRYPT(pers.pers_hash, 'RC#TECH@') as u_clave, usu.u_activo FROM " .$bd. " usu join " .$pers. " pers on usu.pers_id = pers.pers_id where pers.pers_hash is not null and usu.activo = 1" ;

    $qur = $connection->query($sql);

    if ($_SESSION['codigo'] == "99") {

        $json->datos = "false";

    } else {

        $json->datos = "true";

        while ($r = mysqli_fetch_assoc($qur)) {

            $jsonTempData = array();
            $jsonTempData['u_id'] = $r['u_id'];
            $jsonTempData['based'] = $r['based'];
            $jsonData[] = $jsonTempData;
        }
    }

//print_r( $connection);
    $json->usuarioGlobal = $jsonData;

}
/////////
///////// FIN usuario global
/////////
/////////

print_r(json_encode($json));

$qur=null;
@mysqli_close($connection);
?>