<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Teste extends CI_Controller
{
    //test3
    function __construct() 
    {
        parent::__construct();
    }
    
    public function teste_dd()
    {
        dd('aqui');
    }
    
    public function atualiza_emails()
    {
        $table = 'cad_cadastro';
        $users = $this->db->get($table)->result();
        
        foreach ($users as $user)
        {
            $validos = [
                'filipe@viewbrasil.com.br',
                'filipe@comdono.com',
                'filipe_f_f@hotmail.com',
                'ricardo.luis@viewbrasil.com.br',
                'ricardo@comdono.com',
                'matheus@comdono.com',
                'matheus.cdj@gmail.com',
                'matheusmcdj@gmail.com'
            ];
            
            $limpa = true;
            foreach ($validos as $valido)
            {
                $pos = strpos($user->email, $valido);
                
                if($pos !== false)
                {
                    $limpa = false;
                }
            }
            if($limpa == true) 
            {
                $array = explode("@", $user->email);
                $email = $array[0].'@mailinator.com';

                e($email.'<br>');
                
                $this->db->where('id_cadastro', $user->id_cadastro)->update($table, ['email'=>$email]);
                
            }
            else
                e($user->email.'<br>');
            
        }
        
        //dd($users);
    }
    
    public function valida_imovel()
    {
        ob_start();
        
        $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => "api.comdono.com:3000/crawler/validar-imovel",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_HEADER => true,
          CURLOPT_FOLLOWLOCATION => true,
          //CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "POST",
          CURLOPT_POSTFIELDS =>"{\n\t\"inscricaoImovel\":\"48652571\"\n}",
          CURLOPT_HTTPHEADER => array(
            "Content-Type: application/json"
          ),
        ));
        $response = curl_exec($curl);
        // Then, after your curl_exec call:
        $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
        $header = substr($response, 0, $header_size);
        $body = substr($response, $header_size);
        
        curl_close($curl);
        //dd($body);
        var_dump(json_decode($body));
        
        ob_flush();
        
    }
    
    public function show_db($pass = null)
    {
        if($pass == 'WERFadq222')
        {
            dd($this->session->user_id);
        }
    }
    
    public function bug($get = null)
    {
        $data = ['get'=>$get,1=>'teste',2=>'bug',3=>3];
        
        $this->bug1($data);
        
        /*if (array_key_exists('HTTP_ORIGIN', $_SERVER)) {
            $origin = $_SERVER['HTTP_ORIGIN'];
        }
        else if (array_key_exists('HTTP_REFERER', $_SERVER)) {
            $origin = $_SERVER['HTTP_REFERER'];
        } else {
            $origin = $_SERVER['REMOTE_ADDR'];
        }
        
        dd($_SERVER['REQUEST_URI']);*/
        
        /*$this->teste->comdono();
        
        
        $this->load->view('teste_view');*/
        //
        
        
        /*$logFile = getenv('LOGS_PATH').'send_logs_email.php';
        
        $fh = fopen($logFile, 'r+');
        
        if ($fh) {
            $send = false;
            $count = 0;
            while (($line = fgets($fh)) !== false) {
                $count++;
                if(trim($line) == 'sendmail') {
                    $send = true;
                    $lineNum = $count;
                    $message = "An error occurred: \n\n";
                    continue;
                }
                if($send)
                {
                    $message .= trim($line)."\n";
                }
            }
            fclose($fh);
        
            if($send && trim($message) != 'An error occurred:')
            {
                $this->updateFile($logFile, $lineNum);
                //$this->send_log_email($message);
            }
        }*/
    }
    
    private function bug1($data)
    {
        //var_dump($_SERVER['REQUEST_URI']);
        
        //var_dump($data);
        
        //$this->testea->comdono(10);
        log_message('error', 'aqui');
    }
        
    private function updateFile($fileName, $lineNum){
        // check the file exists 
          if(!is_writable($fileName))
            {
            // print an error
            print "The file $fileName is not writable";
            // exit the function
            exit;
            }
          else
              {
            // read the file into an array    
            $arr = file($fileName);
            }
        
          // the line to delete is the line number minus 1, because arrays begin at zero
          $lineToDelete = $lineNum-1;
         
          // check if the line to delete is greater than the length of the file
          if($lineToDelete > sizeof($arr))
            {
              // print an error
            print "You have chosen a line number, <b>[$lineNum]</b>,  higher than the length of the file.";
            // exit the function
            exit;
            }
        
          //remove the line
          unset($arr["$lineToDelete"]);
        
          // open the file for reading
          if (!$fp = fopen($fileName, 'w+'))
            {
            // print an error
                print "Cannot open file ($fileName)";
              // exit the function
                exit;
                }
          
          // if $fp is valid
          if($fp)
            {
                flock($fp, LOCK_EX);
                // write the array to the file
                foreach($arr as $line) { fwrite($fp,$line); }
        
                fwrite($fp, "sendmail\n");
        
                flock($fp, LOCK_UN);
                // close the file
                fclose($fp);
            }
        return true;
    }
        
        /*$msg = "Severity: Notice --> Trying to get property 'id_quadro' of non-object /var/www/desenvolvimento/application/modules/administrativo/views/operacao/venda/quadro_view.php 1";
        
        $logFile = getenv('LOGS_PATH').'send_logs_email.php';
        
        if (!file_exists($logFile)) {
            $fh = fopen($logFile, 'x');
        }
        else {
            $fh = fopen($logFile, 'r+');
        }
        
        if ($fh) {
            $send = true;
            
            while (($line = fgets($fh)) !== false) {
                if(trim($line) == trim($msg)) {
                    $send = false;
                    break;
                }
            }
            
            if($send) {
                flock($fh, LOCK_EX);
                fwrite($fh, $msg."\n");
                flock($fh, LOCK_UN);
            }
            fclose($fh);
        }*/
    
}