index_bypass_action.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /*
  3. * Valida usuario con la BD y devuelve contraseña para validar con PHP
  4. *
  5. * Recibe:
  6. * POST: usuario, password
  7. *
  8. * Error:
  9. * 0 - No se recibieron datos
  10. * 1 - Usuario/Contraseña incorrectos
  11. * 2 - Usuario no esta en BD
  12. * 3 - No existe usuario
  13. *
  14. * Success:
  15. * Redirecciona a inicio.php
  16. */
  17. include_once("../include/nocache.php");//continue on error
  18. require_once("../include/bd_pdo.php");//die on error
  19. require_once("../classes/LogAcceso.php");
  20. require_once("../include/util.php");
  21. require_once("../include/nusoap/nusoap.php");
  22. session_start();
  23. $_SESSION = array();
  24. $pag = "../index_bypass.php";
  25. function limpiaClave($clave){
  26. return intval(str_ireplace(array("ad", "al", "do"), array("","",""), $clave));
  27. }
  28. //Valida usuario, regresa falso con error, 1 si es aceptado, 0 si es rechazado
  29. function validaUsuario($user, $pass){
  30. $client = new nusoap_client('http://200.13.89.2/validacion.php?wsdl', 'wsdl');
  31. $error = $client->getError();
  32. if ($error) {
  33. return false;
  34. }
  35. $result = $client->call("valida_user", array($user, $pass));
  36. if ($client->fault) {
  37. return false;
  38. } else {
  39. $error = $client->getError();
  40. if ($error) {
  41. return false;
  42. } else {
  43. if($result) return 1;
  44. else return 0;
  45. }
  46. }
  47. }
  48. if(!isset($_POST["username"]) || !isset($_POST["passwd"], $_POST["log_as"])){
  49. header("Location: ".$pag."?error=0");
  50. //echo "No hay POST";
  51. exit;
  52. }
  53. $usr = trim(filter_input(INPUT_POST, "username"));//limpia texto
  54. $usr_db = limpiaClave($usr);
  55. $pass = $_POST["passwd"]; //trim(filter_input(INPUT_POST, "passwd"));//limpia texto
  56. $stmt = $pdo->prepare('Select * from fs_validaclaveulsa(:usr) AS "Usuario_id"');
  57. $stmt->bindParam(":usr", $usr_db);
  58. if(!$stmt->execute()){
  59. //print_r($stmt->errorInfo());
  60. header("Location: ".$pag."?error=2");
  61. exit();
  62. }
  63. $usr_rs = $stmt->fetch();//Devuelve sólo 1 resultado
  64. $stmt->closeCursor(); // cierra conexion de resultado
  65. $stmt = null; // cierra conexion
  66. if($usr_rs["Usuario_id"] != ""){//Si existe el usuario
  67. //if(password_verify($pass, $rs["Usuario_password"])){//Obtiene pass de la BD para comparar con función de PHP
  68. $valido = validaUsuario($usr, $pass);//false = error, 0 = sin permiso, 1 = con permiso
  69. if($valido === false){
  70. header("Location: ".$pag."?error=1&sgu=false");
  71. exit();
  72. }else if($valido == 1){
  73. $stmt = $pdo->prepare('Select * from fs_usuario(:id)');
  74. $stmt->bindParam(":id", $usr_rs["Usuario_id"]);
  75. if(!$stmt->execute()){
  76. //print_r($stmt->errorInfo());
  77. header("Location: ".$pag."?error=2");
  78. exit();
  79. }
  80. $rs = $stmt->fetch();//Devuelve sólo 1 resultado
  81. $stmt->closeCursor(); // cierra conexion de resultado
  82. $stmt = null; // cierra conexion
  83. if($rs["SGI_administrador"]){//Si el usuario es administrador se puede loggear como otro
  84. $log_as = trim(filter_input(INPUT_POST, "log_as"));
  85. $stmt = $pdo->prepare('Select * from fs_usuario(:id)');
  86. $stmt->bindParam(":id", $log_as);
  87. if(!$stmt->execute()){
  88. //print_r($stmt->errorInfo());
  89. header("Location: ".$pag."?error=5");
  90. exit();
  91. }
  92. $rs_new = $stmt->fetch();//Devuelve sólo 1 resultado
  93. $stmt->closeCursor(); // cierra conexion de resultado
  94. $stmt = null; // cierra conexion
  95. $stmt = $pdo->prepare('Select * from fs_periodo(:id, NULL, NULL, NULL)');
  96. $stmt->bindParam(":id", $rs["Periodo_activo_id"]);
  97. if(!$stmt->execute()){
  98. //print_r($stmt->errorInfo());
  99. header("Location: ".$pag."?error=5");
  100. exit();
  101. }
  102. $periodo_rs = $stmt->fetch();//Devuelve sólo 1 resultado
  103. $stmt->closeCursor(); // cierra conexion de resultado
  104. $stmt = null; // cierra conexion
  105. //Obtiene 2 corrreos
  106. $stmt = $pdo->prepare('Select * from fs_contacto(:id,3 , NULL) ORDER BY "PerfilContacto_id" LIMIT 2 OFFSET 0');//3 correo, Null todos
  107. $stmt->bindParam(":id", $rs_new["Usuario_id"]);
  108. if(!$stmt->execute()){
  109. header("Location: ".$pag."?error=5");
  110. exit();
  111. }else{
  112. $contacto_rs = $stmt->fetchAll();
  113. $institucional = false;
  114. foreach($contacto_rs as $correo){
  115. if( strpos($alumno["Contacto_valor"], "lasalle.mx") !== false || strpos($alumno["Contacto_valor"], "lasallistas.org.mx") !== false ){
  116. $institucional = true;
  117. }
  118. }
  119. }
  120. $stmt->closeCursor(); // cierra conexion de resultado
  121. //Obtiene estado
  122. if(!$error){
  123. $stmt = $pdo->prepare('Select * from fs_checkusuario_estado(:id)');
  124. $stmt->bindParam(":id", $rs_new["Usuario_id"]);
  125. if(!$stmt->execute()){
  126. header("Location: ".$pag."?error=5");
  127. exit();
  128. }else{
  129. $checkedo_rs = $stmt->fetch();
  130. }
  131. $stmt->closeCursor(); // cierra conexion de resultado
  132. }
  133. //Guarda resultado de autenticación en sesión
  134. $_SESSION["timeout"] = time();
  135. $_SESSION["usuario_id"] = $rs_new["Usuario_id"];
  136. $_SESSION["usuario_nombre"] = $rs_new["Usuario_nombre"];
  137. $_SESSION["usuario_apellidos"] = $rs_new["Usuario_apellidos"];
  138. $_SESSION["periodo_id"] = $rs_new["Periodo_activo_id"];
  139. $_SESSION["periodo_shortname"] = $periodo_rs["Periodo_shortname"];
  140. $_SESSION["periodo_desc"] = $periodo_rs["Periodo_desc"];
  141. $_SESSION["nivel_id"] = $periodo_rs["Nivel_id"];
  142. $_SESSION["administrativo"] = $rs_new["es_Administrativo"];
  143. $_SESSION["profesor"] = $rs_new["es_Profesor"];
  144. $_SESSION["jefe_carrera"] = $rs_new["es_JefeCarrera"];
  145. $_SESSION["sgi_administrador"] = $rs_new["SGI_administrador"];
  146. $stmt = $pdo->prepare('Select * from fs_sistemausuarioacceso(:usr)');
  147. $stmt->bindParam(":usr", $_SESSION["usuario_id"]);
  148. if(!$stmt->execute()){
  149. /*print_r($stmt->errorInfo());
  150. $errorDesc = "Ocurrió un errror al leer los datos de usuario";*/
  151. header("Location: ".$pag."?error=4");
  152. exit();
  153. }else{
  154. $sistema_rs = $stmt->fetchAll();
  155. $stmt->closeCursor();
  156. if(!$_SESSION["sgi_administrador"]){
  157. //if($checkedo_rs["CheckUsuario_Estado_id"]!=1 && ( strlen($rs_new["Usuario_curp"]) < 17 || $institucional == false)){
  158. if($checkedo_rs["CheckUsuario_Estado_id"]==3 || (empty($checkedo_rs["CheckUsuario_Estado_id"]) && ( strlen($rs_new["Usuario_curp"]) < 17 || $institucional == false))
  159. ){
  160. header("Location: ../datos_revisa.php");
  161. exit();
  162. }
  163. if(count($sistema_rs) == 1){
  164. //guarda Log acceso
  165. $LogObj = new LogAcceso();
  166. $LogObj->appendLog($rs["Usuario_id"], $rs["Usuario_apellidos"]." ".$rs["Usuario_nombre"]);
  167. header("Location: ../".$sistema_rs[0]["Sistema_pagina"]);//todo OK manda a inicio del sistema
  168. exit();
  169. }else if(count($sistema_rs) == 0){//no tiene sistemas
  170. //echo "No tiene sistemas"; print_r($sistema_rs);
  171. header("Location: ".$pag."?error=4");
  172. exit();
  173. }
  174. }
  175. }
  176. }else{
  177. header("Location: ".$pag."?error=5");
  178. exit();
  179. }
  180. //guarda Log acceso
  181. $LogObj = new LogAcceso();
  182. $LogObj->appendLog($rs["Usuario_id"], $rs["Usuario_apellidos"]." ".$rs["Usuario_nombre"]);
  183. header("Location: ../main.php");//todo OK manda a sellección del sistema
  184. exit();
  185. }else{//error de autenticación
  186. //echo "fallo autenticación";
  187. header("Location: ".$pag."?error=1");
  188. exit();
  189. }
  190. }else{//no existe en la BD
  191. //echo "no existe en BD";
  192. header("Location: ".$pag."?error=2");
  193. exit();
  194. }
  195. //Si no entró a ninguna, no tiene permisos
  196. //echo "No existe usuario"; exit();
  197. header("Location: ".$pag."?error=3");
  198. $pdo = null;
  199. ?>