<?php
include '../comunes_php/cors.php';
 
    $myfile = fopen("logFile.txt", "a") or die("Unable to open file!");
    $parametros = print_r($_POST,TRUE);
    fwrite($myfile, "\n");
    fwrite($myfile, "---------------------------". date("Y-m-d H:i:s") ."--------------------------------------------------------");
    fwrite($myfile, "\n");    
    fwrite($myfile, "----------------------------nuevo registro log-------------------------------------------------------");
    fwrite($myfile, "\n");    
    fwrite($myfile, "-----------------------------------------------------------------------------------------------------");
    
    fwrite($myfile, "\n");
    
    fwrite($myfile, $parametros);
    fclose($myfile);

    $result = array();
    $mensaje = "Error en usuario o clave";
    include '../rdb/conn.php';




    $usuario_id=0;
    $username = $_REQUEST['username'];
    $password =$_REQUEST['password'];




	    
//    $fecha_actual= date("Y-m-d H:i:s");
    $fecha_actual= $_POST['fecha'];
 
  
  
try {    
mysqli_query( $conn,"SET AUTOCOMMIT=0");
mysqli_query( $conn,"START TRANSACTION");

         
            $sql=" select id usuario_id,perfil_id,nombre from usuarios where login='$username' and clave='$password' "; 


      		$myfile = fopen("logFile.txt", "a") or die("Unable to open file!");
      		fwrite($myfile, "\n");  
      		fwrite($myfile, date("Y-m-d H:i:s")."*****************MODIFICACION*****************");
		    fwrite($myfile, "\n");      		    
        	fwrite($myfile, $sql);
        	fwrite($myfile, "\n");
        	fclose($myfile);	

            $visitante ="Acceso desde IP: " .$_SERVER['REMOTE_ADDR'] ;
            $fecha= date("Y-m-d H:i:s");
            $accion = $sql;

            $valor="";
            $sqlv  = "INSERT INTO visitas ( fecha, visitante, accion) VALUES ";
            $sqlv .= " ('$fecha','$visitante','$accion')";
            @mysqli_query( $conn,$sqlv);

        
            $rs = mysqli_query( $conn,$sql);
            $row = mysqli_fetch_row($rs);
            $result["usuario_id"] = $row[0];
            $result["perfil_id"] = $row[1];
            $result["nombre"] = $row[2];
    

            if ($row[0]) {             
                $result["estado"]=true;
                $result["msg"]='Usuario Logueado';
            } else
             {             
                $result["estado"]=false;
                $result["msg"]='Usuario NO Logueado';
            } 
            $result["sql"] = $sql;
            
            
      		$myfile = fopen("logFile.txt", "a") or die("Unable to open file!");
      		fwrite($myfile, "\n");  
      		fwrite($myfile, date("Y-m-d H:i:s")."*****************MODIFICACION*****************");
		    fwrite($myfile, "\n");      		    
        	fwrite($myfile, json_encode($result));
        	fwrite($myfile, "\n");
        	fclose($myfile);	
            
            echo json_encode($result);

} catch(exception $error) {
    
mysqli_query( $conn,"ROLLBACK");
$result["msg"] = "Errores logueando usuario: ".$error->getMessage();
$result["estado"]=false;

echo json_encode($result);
	
}
    
  
?>