123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- /**
- * Clase para validar la sesión del usuario y que el usuario esté activo.
- *
- * @author Alejandro
- */
- class ValidaSesion {
- private $acceso, $edicion, $periodo;//guarda si el usuario puede acceder y/o editar
- private $error, $hasError = false;
- private $fecha_jefes;//fecha límite para jefes de carrera
-
- /**
- * Constructor
- *
- * @param object $pdo Conexión activa a base de datos a través de PDO
- * @param int $submenu ID del submenú que se busca
- * @return boolean
- */
- function __construct($pdo, $submenu, $sistema){
- if (session_status() == PHP_SESSION_NONE) {
- session_start();
- }
-
- $session_life = 1*60*60;//1 hora convertido a segundos
- // check to see if $_SESSION["timeout"] is set
- if (isset($_SESSION["timeout"])) {
- // calculate the session's "time to live"
- $sessionTTL = time() - $_SESSION["timeout"];
- if ($sessionTTL > $session_life) {
- $this->terminaSesion();
- }
- }else
- $this->terminaSesion();
- $_SESSION["timeout"] = time();
-
- if(!isset($_SESSION["usuario_id"]) || $_SESSION["usuario_id"] == "") $this->terminaSesion();
-
- if(!isset($_SESSION["periodo_id"]) || $_SESSION["periodo_id"] == "") $this->periodo = false;
- else $this->periodo = true;
-
- //existe id en sesión?
- if(array_key_exists("usuario_id", $_SESSION) && is_numeric($_SESSION["usuario_id"]) && $_SESSION["usuario_id"] > 0){
- if(is_int($submenu)){
- //valida permisos
- $stmt = $pdo->prepare('Select * from fs_validapermisos(:usr, :sub, :sist)');//devuelve: permiso, edicion
- $stmt->bindParam(":usr", $_SESSION["usuario_id"]);
- $stmt->bindParam(":sub", $submenu);
- $stmt->bindParam(":sist", $sistema);
- if(!$stmt->execute()){
- $this->hasError = true;
- $this->error = $stmt->errorInfo();
- }else{
- $rs = $stmt->fetch();
- $stmt->closeCursor(); // cierra conexion de resultado
- $stmt = null; // cierra conexion
- //si es administrador de sistema, siempre es true
- $this->acceso = (bool) $rs["permiso"];
- $this->edicion = (bool) $rs["edicion"];
- $rs = null;
- }
- }else if(is_array($submenu)){
- $stmt = $pdo->prepare('Select * from fs_validapermisos(:usr, :sub, :sist)');//devuelve: permiso, edicion
- $stmt->bindParam(":usr", $_SESSION["usuario_id"]);
- $stmt->bindParam(":sist", $sistema);
- foreach($submenu as $subItem){
- if(!$this->hasError){
- //valida permisos
- $stmt->bindParam(":sub", $subItem);
- if(!$stmt->execute()){
- $this->hasError = true;
- $this->error = $stmt->errorInfo();
- }else{
- $rs = $stmt->fetch();
- //si es administrador de sistema, siempre es true
- $this->acceso = $this->acceso || (bool) $rs["permiso"];
- $this->edicion = $this->edicion || (bool) $rs["edicion"];
- $rs = null;
- }
- }
- }
- $stmt->closeCursor(); // cierra conexion de resultado
- $stmt = null; // cierra conexion
- }else if(is_null($submenu)){
- //Valida permisos de acceso sistema
- $stmt = $pdo->prepare('Select * from fs_validasistema(:usr, :sist)');//devuelve: permiso, edicion
- $stmt->bindParam(":usr", $_SESSION["usuario_id"]);
- $stmt->bindParam(":sist", $sistema);
- if(!$stmt->execute()){
- $this->hasError = true;
- $this->error = $stmt->errorInfo();
- }else{
- $rs = $stmt->fetch();
- $stmt->closeCursor(); // cierra conexion de resultado
- $stmt = null; // cierra conexion
- $this->acceso = (bool) $rs["permiso"];
- $this->edicion = false;//acceso a sistema no tiene edición
- $rs = null;
- }
- }else{
- $this->hasError = true;
- $this->error = "Dato no válido en validación de permisos";
- }
-
- //carga datos de periodo
- if(!is_null($pdo) && !$this->hasError && $this->periodo){
- $stmt = $pdo->prepare('Select * from fs_periodo(:id, NULL, NULL, NULL)');
- $stmt->bindParam(":id", $_SESSION["periodo_id"]);
- if(!$stmt->execute()){
- $this->hasError = true;
- $this->error = $stmt->errorInfo();
- }else{
- $rs = $stmt->fetch();
- $stmt->closeCursor(); // cierra conexion de resultado
- $stmt = null; // cierra conexion
- if($rs["Periodo_id"] == "") $this->periodo = false;
- else{
- $this->periodo = true;
- $this->fecha_jefes = $rs["Periodo_fecha_jefes"];
- }
- }
- }
- }else{
- $this->error = "No existe la llave en sesión";
- $this->hasError = true;
- $this->acceso = false;
- $this->edicion = false;
- }
- if($this->hasError){
- $this->acceso = false;
- $this->edicion = false;
- }
-
- }
-
- function fechaJefes(){//devuelve true si están dentro de fecha del periodo
- if($this->periodo){
- if($_SESSION["jefe_carrera"]){
- $hoy = strtotime(date('Y-m-d'));
- if($hoy <= strtotime($this->fecha_jefes)){//dentro del límite
- return true;
- }
- }else
- return true;
- }
- return false;
- }
-
- function getFechaJefes(){//devuelve fecha
- return $this->fecha_jefes;
- }
-
- function tieneAcceso(){
- return $this->acceso;
- }
-
- function puedeEditar(){
- return $this->acceso && $this->edicion;
- }
-
- function tieneError(){
- return $this->hasError;
- }
-
- function getError(){
- return $this->error;
- }
-
-
- function terminaSesion(){
- $_SESSION = array();
- session_destroy();
- //$pag = $this->getFirstDirectory($_SERVER['PHP_SELF'])."/salir.php?expired=1";
- $pag = $_SERVER['SERVER_NAME']."/salir.php?expired=1";
- header("Location: http://".$pag);
- exit();
- }
-
- function validaPeriodoUsuario(){
- if(!$this->periodo){
- $pag = $this->getFirstDirectory($_SERVER['PHP_SELF'])."/main.php?error=0";
- header("Location: ".$pag);
- //$pag = $_SERVER["SERVER_NAME"]."/apsa/main.php?error=0";
- //header("Location: http://".$pag);
- exit();
- }
- //return $this->periodo;
- }
-
- private function getFirstDirectory($pag){
- if(dirname($pag) == '/'){
- return $pag;
- }
- return $this->getFirstDirectory(dirname($pag));
- }
- }
|