<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class SesionMiddleware
{
    protected $controller;
    protected $ci;

    public function __construct($controller, $ci)
    {
        $this->controller = $controller;
        $this->ci = $ci;
    }

    public function run()
    {   
        $useToken = FALSE;
        $Token2 = FALSE;
        $Token = obtnerToken();
        $Autho = "";
        if($Token != FALSE){
        $Autho =str_replace('Bearer ','', $Token);
        }
        if( $Autho !=""){
        $Autho =str_replace('Bearer ','', $Autho);
        $Token = ValidarToken($Autho);
            }

        if ( $Token === FALSE ){
            $DatosGET= isset($_GET["x"]) ? $_GET["x"]: "" ;
          
           
            $DatosGET =  $DatosGET!=""?ValidarToken($DatosGET) :"";
            $Token  =$DatosGET!=''?$DatosGET: $Token ;
            
           }
         
           $Token2 = obtnerToken();
           $Autho = "";
          if($Token2 != FALSE){
           $Autho =str_replace('Bearer ','', $Token2);
          }
          if( $Autho !=""){
           $Autho =str_replace('Bearer ','', $Autho);
           $Token2 = OptenerDatosToken($Autho);
         }
    
             
               if ( $Token2 === FALSE ){
                $DatosGET= isset($_GET["x"]) ? $_GET["x"]: "" ;
              
               
                $DatosGET =  $DatosGET!=""?OptenerDatosToken($DatosGET) :"";
                $Token2  =$DatosGET!=''?$DatosGET: $Token2 ;
                
               }
               if(isset($this->ci->session)){
                 $IDDB =  $Token2== FALSE ? $this->ci->session->userdata("DB") :  $Token2;
           }else{
               $IDDB =   $Token2== FALSE ? null :  $Token2 ;
           }
           if ( $IDDB != null && $IDDB != FALSE  ){
           
            $this->ci->load->model('UserDbModels');
            $Datos= $this->ci->UserDbModels->getUserDb($IDDB);
            $this->ci->db = $this->ci->load->database(configDB( desencriptar($Datos[0]->Host),desencriptar($Datos[0]->user_db),desencriptar($Datos[0]->password_db),$Datos[0]->DB), TRUE);
            $this->ci->UserDB =  $Datos[0]->ID;
           }
           

     
       
    
      

       // $this->controller->load->library('session');
        if (!$this->ci->session->has_userdata("login") AND    $Token == FALSE ) {
           
            $this->ci->session->set_flashdata('mensaje', 'No puedes acceder al recurso hasta que <strong>inicies sesión</strong>');
            $this->ci->session->set_flashdata('url', $this->controller->router->class);
           // 
           if( $useToken== TRUE){
            header("HTTP/1.0 404 Not Found");
            header('content-type: application/json; charset=utf-8');
            error_log("¡Base de datos Oracle no disponible!", 0);
            echo json_encode(array("Error"=> "Token"));
             exit;
           }else{
            redirect("/");
           }
          
            return;
            //redirect("/");
        }
       


    }
} 