<?php

header("Expires: Tue,03 Jul 2001 06:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s"). "GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

if(isset($_POST['usuario']) and isset($_POST['password'])){

include("bd/sql_variables.php");
$con= mysqli_connect("$sql_host", "$sql_usuario", "$sql_pass","$sql_db") or die(header ("No Disponible Servidor de Base de Datos"));
//mysqli_select_db($con, "$sql_db");


$usuario = $_POST['usuario'];
$password = $_POST['password'];

$sql = "select concat(up_apepat,' ',up_apemat,' ',up_nom) as nombre, up_pass, ug_id from tb_usuprog where up_usu='$usuario'";
$result =mysqli_query($con, $sql);
if(mysqli_num_rows($result)!=0){
while ($row= mysqli_fetch_assoc($result)){
$pass = $row['up_pass'];
//echo $pass."<br>";
	if ($pass==$password){
	$ugid=$row['ug_id'];
	$sqlpermiso="select b.pu_url as url, b.pu_tipo as tipo from tb_permusu a inner join tb_perfilusu b on a.pu_id = b.pu_id where a.ug_id='$ugid'";
	//echo $sqlpermiso;
	$sqlparametros="select s_id as ids, j_id as idj from tb_usuprogdep where ug_id='$ugid'";
	$resultpermiso = mysqli_query($con,$sqlpermiso);
	$resultparametros = mysqli_query($con,$sqlparametros);
	while ($rowpm= mysqli_fetch_assoc($resultpermiso)){
		$url = $rowpm['url'];
		$tipo = $rowpm['tipo'];
	}
	while ($rowpr= mysqli_fetch_assoc($resultparametros)){
		$ids = $rowpr['ids'];
		$idj = $rowpr['idj'];
	}
	 //$url = "http://www.ciideg.com/sgth/files/personal/".$url."?idj=".$idj."&ids=".$ids."&ugid=".$ugid;
	$url = "personal/".$url."?idj=".$idj."&ids=".$ids."&ugid=".$ugid;
	//echo $url;
	  //echo "<script>window.location='$url'; </script>";
	
	
	//Consulta del IP
		
			if ($_SERVER) { 
			//echo "Salida del Servidor<br>";
			if ( $_SERVER["HTTP_X_FORWARDED_FOR"] ) { 
			$realip = $_SERVER["HTTP_X_FORWARDED_FOR"]; 
			} elseif ( $_SERVER["HTTP_CLIENT_IP"] ) { 
			$realip = $_SERVER["HTTP_CLIENT_IP"]; 
			} else { 
			$realip = $_SERVER["REMOTE_ADDR"]; 
			} 
			} else { 
			//echo "Salida del Evento<br>";
			if ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) { 
			$realip = getenv( 'HTTP_X_FORWARDED_FOR' ); 
			} elseif ( getenv( 'HTTP_CLIENT_IP' ) ) { 
			$realip = getenv( 'HTTP_CLIENT_IP' ); 
			} else { 
			$realip = getenv( 'REMOTE_ADDR' ); 
			} 
			} 
		
		
		$resultpermiso = mysqli_query($con,"call registrar_ingreso ('$realip', '$ugid')");
		
		
			//iniciando session
			session_start();

			$_SESSION['loggedin'] = true;

			$_SESSION['username'] = $ugid;

			$_SESSION["ultimoAcceso"]= date("Y-n-j H:i:s"); 
			
	
	header("location:$url");
	}else{
	$texto ="CLAVE INCORRECTA";
	}
  }
}else{
$texto = "USUARIO NO REGISTRADO";
}
$ids="";
$idj="";
if($ids=='' && $idj==''){
//////////////
$sql = "select concat(a.ua_apepat,' ',a.ua_apemat,' ',a.ua_nom) as nombre, a.ua_password as pass, b.ua_estado as estado, a.ug_id as ugid from tb_usuasist a inner join tb_usudep b on a.ug_id=b.ug_id where a.ua_dni='$usuario' ";
$result = mysqli_query($con,$sql);
//echo $sql;
if(mysqli_num_rows($result)!=0){
while ($row= mysqli_fetch_assoc($result)){
$pass = $row['pass'];
$estado = $row['estado'];
//echo $pass."<br>";
	if ($pass==$password && $estado!=4){
		if($estado!=0){
	$ugid=$row['ugid'];
	$sqlpermiso="select b.pu_url as url, b.pu_tipo as tipo from tb_permusu a inner join tb_perfilusu b on a.pu_id = b.pu_id where a.ug_id='$ugid'";
	$sqlparametros="select s_id as ids, j_id as idj, tp_id as idtp from tb_usudep where ug_id='$ugid' and ua_estado not in (0)";
	
	//echo $sqlpermiso;
	$resultpermiso = mysqli_query($con,$sqlpermiso);
	$resultparametros = mysqli_query($con,$sqlparametros);
	while ($rowpm= mysqli_fetch_assoc($resultpermiso)){
		$url = $rowpm['url'];
		$tipo = $rowpm['tipo'];
	}
	while ($rowpr= mysqli_fetch_assoc($resultparametros)){
		$ids = $rowpr['ids'];
		$idj = $rowpr['idj'];
		$idtp = $rowpr['idtp'];
	}
	
		$url = "personal/".$url."?idj=".$idj."&ids=".$ids."&idtp=".$idtp."&ugid=".$ugid;
		//echo "<br>".$url;
		//"http://www.ciideg.com/sgth/files/personal/".$url."?idj=".$idj."&ids=".$ids."&idtp=".$idtp."&ugid=".$ugid;
		
		//Consulta del IP
		
			if ($_SERVER) { 
			//echo "Salida del Servidor<br>";
			if ( $_SERVER["HTTP_X_FORWARDED_FOR"] ) { 
			$realip = $_SERVER["HTTP_X_FORWARDED_FOR"]; 
			} elseif ( $_SERVER["HTTP_CLIENT_IP"] ) { 
			$realip = $_SERVER["HTTP_CLIENT_IP"]; 
			} else { 
			$realip = $_SERVER["REMOTE_ADDR"]; 
			} 
			} else { 
			//echo "Salida del Evento<br>";
			if ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) { 
			$realip = getenv( 'HTTP_X_FORWARDED_FOR' ); 
			} elseif ( getenv( 'HTTP_CLIENT_IP' ) ) { 
			$realip = getenv( 'HTTP_CLIENT_IP' ); 
			} else { 
			$realip = getenv( 'REMOTE_ADDR' ); 
			} 
			} 
			
			
		$resultpermiso = mysqli_query($con,"call registrar_ingreso ('$realip', '$ugid')");
		
		//iniciando session
			session_start();

			$_SESSION['loggedin'] = true;

			$_SESSION['username'] = $ugid;

			$_SESSION["ultimoAcceso"]= date("Y-n-j H:i:s"); 
			
		
	//echo $url;
	   header("location:".$url);
	  //echo "<script>window.location='$url'; </script>";
	  }else{
	  $texto = "USUARIO SUSPENDIDO";
	  }
	}elseif($estado == 4){
	$texto = "USTED NO HA ACTUALIZADO SUS DATOS";
	}
	else{
	$texto ="CLAVE INCORRECTA";
	}
  }
}else{
$texto = "USUARIO NO REGISTRADO";
}
//////////////
}
}else{
$texto = "";
}
?>
<html>
<head>
<title>SIPA - SISTEMA INTEGRADO DE LA PROGRAMACION ASISTENCIAL</title>
<link rel="stylesheet" type="text/css" href="styles/Style.css">
<link rel="shortcut icon" href="logo.ico" type="image/x-icon">
<script language="JavaScript">
function validar() {
var usuario = document.getElementById("usuario").value;
var password = document.getElementById("password").value;
document.location= "login.php?usuario="+usuario+"&password="+password;
}
</script>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache"> 
</head>
<body>
<form action="login.php" method="POST">
<table align='center'><tr>
<td width='150'>
</td><td>
<table align="center">
<tr><td><img src="img/logo2.jpg" width="500" height="200"></td></tr>
<tr><td align='center'><font color='blue' size='15'><b>SISTEMA INTEGRADO DE LA PROGRAMACION ASISTENCIAL</b></font></td></tr>
</table>
<table align="center">
<tr><td colspan="2"><center><b><br><br><br><br>INGRESO SIPA</b></center></td><td rowspan="5" width="80"></td>
<td rowspan="4" width="250"><br><center><b>FORMA DE INGRESO</b></center><br><b> Usuario </b>: Para Programadores<br><b> DNI </b>   :  Para Personal Asistencial
  <br><br>* Todo Personal Asistencial tiene que actualizar sus datos, para visualizar su programacion...<br><br>>>>>Sistema Creado por Carlos Meza<<<<<br>>>>>>>><b>Made by Carlos Meza</b><<<<<<<<<br><center><!--a href="registronuevopersonal.php">Actualizar Datos</a--></center></td></tr>
<tr><td align='center' bgcolor='red'><b><?php if($texto!=''){ echo $texto;}?></b></td></tr>
<tr><td>Usuario / DNI</td><td><input type="text" size="10" name="usuario" id="usuario"></td></tr>
<tr><td>Password</td><td><input type="password" size="10" name="password" id="password"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="enviar" value="Ingresar"></td></tr>
<tr><td align="center"><?php 
if(isset($_GET['error'])){ echo $_GET['error'];}?></td></tr>
</table>
<table align="center">
<tr><td><img src="img/logoincor2.jpg" width="300" height="100"></td></tr>
</table>
<td width='150'>
</td><td>
<table>
<tr><td></td></tr><tr><td><!--img src="img/comunicado4.bmp" width="500" height="260"--></td></tr>
<tr><td align='center'><img src="img/3.gif" width="360" height="140"></td></tr>
</table>
<?php  include("onomastico.php");?>
</td></tr>
</table>
</form>
</body>
</html>
<?php //} ?>