<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Url {

    
    private $chave  = "Eoj3pCX5ThSg4XbJKiPiIEjsNPI8TIES"; // CHAVE DO ADMINISTRADOR DA API DE SMS
    private $token  = "7E5JVENDVNJJCF7KARGN92S5S=="; // TOKEN DO ADMINISTRADOR DA API DE SMS
    private $url    = "https://aporfia.com.br/ovum/api_sms/apisms1.7/";
    
    public static function urlamigavel($nome) {

        $subs = array(
            'ar_token' => array(' ', 'á', 'à', 'ã', 'â', 'é', 'ê', 'í', 'ó', 'ô', 'õ', 'ú', 'ü', 'ç', 'Á', 'À', 'Ã', 'Â', 'É', 'Ê', 'Í', 'Ó', 'Ô', 'Õ', 'Ú', 'Ü', 'Ç'),
            'ar_token_replace' => array('+', 'a', 'a', 'a', 'a', 'e', 'e', 'i', 'o', 'o', 'o', 'u', 'u', 'c', 'A', 'A', 'A', 'A', 'E', 'E', 'I', 'O', 'O', 'O', 'U', 'U', 'C')
        );

        return str_replace($subs['ar_token'], $subs['ar_token_replace'], $nome);
    }

    public function get_client_ip_server() {
        $ipaddress = '';
        if (getenv('HTTP_CLIENT_IP'))
            $ipaddress = getenv('HTTP_CLIENT_IP');
        else if (getenv('HTTP_X_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
        else if (getenv('HTTP_X_FORWARDED'))
            $ipaddress = getenv('HTTP_X_FORWARDED');
        else if (getenv('HTTP_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_FORWARDED_FOR');
        else if (getenv('HTTP_FORWARDED'))
            $ipaddress = getenv('HTTP_FORWARDED');
        else if (getenv('REMOTE_ADDR'))
            $ipaddress = getenv('REMOTE_ADDR');
        else
            $ipaddress = 'UNKNOWN';

        return $ipaddress;
    }

    public function getBrowser() {

        $useragent = $_SERVER['HTTP_USER_AGENT'];

        if (strpos($useragent, "MSIE") !== false && strpos($useragent, "Opera") === false && strpos($useragent, "Netscape") === false) {
            $found = preg_match("/MSIE ([0-9]{1}\.[0-9]{1,2})/", $useragent, $mathes);
            if ($found) {
                return "Internet Explorer " . $mathes[1];
            }
        } elseif (strpos($useragent, "Gecko")) {
            $found = preg_match("/Firefox\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes);
            if ($found) {
                return "Mozilla Firefox " . $mathes[1];
            }

            $found = preg_match("/Netscape\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes);
            if ($found) {
                return "Netscape " . $mathes[1];
            }

            $found = preg_match("/Chrome\/([^\s]+)/", $useragent, $mathes);
            if ($found) {
                return "Google Chrome " . $mathes[1];
            }

            $found = preg_match("/Safari\/([0-9]{2,3}(\.[0-9])?)/", $useragent, $mathes);
            if ($found) {
                return "Safari " . $mathes[1];
            }

            $found = preg_match("/Galeon\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes);
            if ($found) {
                return "Galeon " . $mathes[1];
            }

            $found = preg_match("/Konqueror\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes);
            if ($found) {
                return "Konqueror " . $mathes[1];
            }

            return "Gecko based";
        } elseif (strpos($useragent, "Opera") !== false) {
            $found = preg_match("/Opera[\/ ]([0-9]{1}\.[0-9]{1}([0-9])?)/", $useragent, $mathes);
            if ($found) {
                return "Opera " . $mathes[1];
            }
        } elseif (strpos($useragent, "Lynx") !== false) {
            $found = preg_match("/Lynx\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes);
            if ($found) {
                return "Lynx " . $mathes[1];
            }
        } elseif (strpos($useragent, "Netscape") !== false) {
            $found = preg_match("/Netscape\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes);
            if ($found) {
                return "Netscape " . $mathes[1];
            }
        } else {
            return false;
        }
    }
    
     public function dispostivo() {
        
        $iphone = strpos($_SERVER['HTTP_USER_AGENT'], "iPhone");
        $ipad = strpos($_SERVER['HTTP_USER_AGENT'], "iPad");
        $android = strpos($_SERVER['HTTP_USER_AGENT'], "Android");
        $palmpre = strpos($_SERVER['HTTP_USER_AGENT'], "webOS");
        $berry = strpos($_SERVER['HTTP_USER_AGENT'], "BlackBerry");
        $ipod = strpos($_SERVER['HTTP_USER_AGENT'], "iPod");
        $symbian = strpos($_SERVER['HTTP_USER_AGENT'], "Symbian");
        $windowsphone = strpos($_SERVER['HTTP_USER_AGENT'], "Windows Phone");

        if ($iphone || $ipad || $android || $palmpre || $ipod || $berry || $symbian || $windowsphone == true) {
            return $dispositivo = "mobile";
        } else {
            return $dispositivo = "computador";
        }
    }
    public function enviasms_old($telefone, $mensagem) {
        
        $ch = curl_init();

        $post = array(
            'token' => 'iyrt8ur589yfieg78yruij3y7875jfhw',
            'chave' => 'jufdry3289erui23ihf78ty89264f',
            'instituicao' => 'AMAZONIA NOTIFICA',
            'celular' => $telefone,
            'mensagem' => $mensagem
        );

        curl_setopt($ch, CURLOPT_URL, 'https://aporfia.com.br/ovum/api_sms/apisms1.7/');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $requisicao = curl_exec($ch);
        $output = json_decode($requisicao);
        curl_close($ch);
        
        return $output->status;

    }
    
    public function enviasms_1_3($celular, $mensagem, $email) {
        
        
        $identificador = md5($email);
        $ch = curl_init();

        $post = array(
            'token' => $this->token,
            'chave' => $this->chave,
            'instituicao' => 'CIDADEAPRENDIZAGEM', //Opcional
            'celular' => $celular,
            'email' => $email,
            'mensagem' => $mensagem,
            'identificador' => $identificador, //Recomendado
             'market' => 1, //Opcional
        );

        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $requisicao = curl_exec($ch);
        $output = json_decode($requisicao);
        curl_close($ch);
        
        return $output->status;

    }
    
    public function enviasms_market($celular, $mensagem, $email) {
        
        
        $identificador = md5($email);
        $ch = curl_init();

        $post = array(
            'token' => $this->token,
            'chave' => $this->chave,
            'instituicao' => 'AMAZONIACURSOS', //Opcional
            'celular' => $celular,
            'email' => $email,
            'mensagem' => $mensagem,
            'identificador' => $identificador, //Recomendado
            'market' => 1, //Opcional
        );

        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $requisicao = curl_exec($ch);
        
        $output = json_decode($requisicao);
        curl_close($ch);
        
        return $output->status;

    }

}
