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

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

    public function run()
    {   
      
       
        $useToken = FALSE;
        $Token = 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!=""?ValidarToken($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 ;
           }
           $porciones = [];
           if ( $IDDB != null){
            $porciones = explode(".",   $IDDB );
       }
           
           if(count($porciones) > 1){
            $IDDB=  intval($porciones[count($porciones)-1]);
           }
          
           if ( $IDDB != null && $IDDB != FALSE && DbMultiple== TRUE ){
            $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;
           }else{
            $method = $_SERVER['REQUEST_METHOD'];// optenemos el metodo
            $Ruta   = $_SERVER['REQUEST_URI']; // optenemos la ruta 
            $array = explode ( '/', $Ruta );
            $porciones = explode(".", $array[count($array)-1] );
            if(count($porciones) > 1 && DbMultiple== TRUE ){
                $IDDB=  intval($porciones[count($porciones)-1]);
                $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;
            }

          
           }
          
          
          
         
      

     
          
       


    }
} 