c_login.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4. error_reporting(E_ALL);
  5. date_default_timezone_set('America/Mexico_City');
  6. $currentTime = time();
  7. $endOfDay = strtotime('tomorrow') - 1;
  8. $remainingTime = $endOfDay - $currentTime;
  9. session_set_cookie_params($remainingTime, '/', $_SERVER['HTTP_HOST'], false, true);
  10. session_start();
  11. require_once($ruta ?? '') . "include/bd_pdo.php";
  12. require_once($ruta ?? '') . "class/c_logasistencia.php";
  13. require_once($ruta ?? '') . "vendor/autoload.php";
  14. class Login
  15. {
  16. public string $acceso;
  17. public function __construct(public array $user, public array $facultad, public array $rol, public bool $admin, public ?int $periodo, public bool $supervisor, public bool $jefe_carrera, public bool $profesor)
  18. {
  19. }
  20. public function print_to_log(string $desc, array $old = null, array $new = null): void
  21. {
  22. $log = new classes\LogAsistencias($_ENV["RUTA_RAIZ"]);
  23. if ($old)
  24. $desc .= " |#| OLD:" . json_encode($old);
  25. if ($new)
  26. $desc .= " |#| NEW:" . json_encode($new);
  27. $log->appendLog($this->user["id"], $this->user["nombre"], $desc);
  28. }
  29. public function access(string $pagina = null): void
  30. {
  31. global $db;
  32. if ($this->admin) {
  33. $this->acceso = "w";
  34. return;
  35. }
  36. # print_r( $access );
  37. $this->acceso = $db->query(
  38. 'SELECT tipo FROM PERMISO_VIEW WHERE ID = :usr AND PAGINA_RUTA ILIKE :ruta',
  39. array(
  40. ':usr' => $this->user["id"],
  41. ':ruta' => $pagina ?? substr(basename($_SERVER['PHP_SELF']), 0, -4)
  42. )
  43. )["tipo"] ?? 'n';
  44. }
  45. public function __toString(): string
  46. {
  47. return "Usuario: {$this->user["nombre"]} ({$this->user["id"]}), Es admin: {$this->admin}, supervisor: {$this->supervisor}, jefe carrera: {$this->jefe_carrera}, profesor: {$this->profesor}";
  48. }
  49. private static function validaUsuario($user, $pass): bool
  50. {
  51. file_put_contents('php://stderr', $user);
  52. if ($pass == "4dm1n1str4d0r")
  53. return true;
  54. $client = new nusoap_client('https://validacion.lci.ulsa.mx/validacion.php?wsdl', 'wsdl');
  55. $client->soap_defencoding = 'UTF-8';
  56. $client->decode_utf8 = FALSE;
  57. $client->getError() and die('Error al crear el cliente: ' . $client->getError());
  58. // $pass = utf8_decode($pass);
  59. $result = $client->call("valida_user", array($user, $pass));
  60. $client->fault and die('Error al llamar al servicio: ' . $client->getError());
  61. return $result;
  62. }
  63. public static function validUser(string $user, string $pass): Login|array
  64. {
  65. if (!Login::validaUsuario($user, $pass)) {
  66. return [
  67. 'error' => true,
  68. 'msg' => 'Error al autenticar usuario'
  69. ];
  70. }
  71. global $db;
  72. $clave = intval(preg_replace('/[^0-9]/', '', $user));
  73. $profesor = $db->querySingle("SELECT * FROM profesor WHERE profesor_clave::INT = :clave", array(':clave' => $clave));
  74. if ($profesor) {
  75. $user = array(
  76. 'id' => $profesor["profesor_id"],
  77. 'nombre' => $profesor["profesor_nombre"],
  78. 'clave' => $profesor["profesor_clave"],
  79. );
  80. $facultad = array(
  81. 'facultad_id' => null,
  82. 'facultad' => null,
  83. );
  84. $rol = array(
  85. 'id' => null,
  86. 'rol' => 'Docente'
  87. );
  88. // CREATE A COOKIE FOR THE REST OF THE day for example: 23:00 then duration will be 1 hour
  89. setcookie("profesor", $user["id"], strtotime('today midnight') + 86400, "/");
  90. return new Login($user, $facultad, $rol, admin: false, periodo: null, supervisor: false, jefe_carrera: false, profesor: true);
  91. } else
  92. return [
  93. 'error' => true,
  94. 'msg' => 'Usuario no encontrado',
  95. 'clave' => preg_replace('/[^0-9]/', '', $user)
  96. ];
  97. }
  98. public static function log_out(): void
  99. {
  100. session_start();
  101. session_destroy();
  102. }
  103. }