/class/Helpers/Valida.class.php
PHP | 234 lines | 193 code | 36 blank | 5 comment | 37 complexity | c852355b931aa9eb863efb0a54c18caf MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
- <?php
- /**
- * <strong>Valida.class</strong>
- * Classe responsável por validar informações do sistema
- * @copyright (c) 2016, André Cristhian
- */
- class Valida{
- private static $Data;
- private static $Format;
-
- public static function Email($email){
- self::$Data = (string) $email;
- self::$Format = "/^[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*\\.[A-Za-z0-9]{2,4}$/";
- if (preg_match(self::$Format, self::$Data)) {
- return true;
- } else {
- return false;
- }
- }
-
- public static function validaCPF($cpf){
- $cpf = preg_replace('/[^0-9]/is', '', $cpf);
-
- if (strlen($cpf) != 11) {
- return false;
- }
- if (preg_match('/(\d)\1{10}/', $cpf)) {
- return false;
- }
- for ($t = 9; $t < 11; $t++) {
- for ($d = 0, $c = 0; $c < $t; $c++) {
- $d += $cpf{$c} * (($t + 1) - $c);
- }
- $d = ((10 * $d) % 11) % 10;
- if ($cpf{$c} != $d) {
- return false;
- }
- }
- return true;
- }
- public static function validaCNPJ($cnpj){
- $cnpj = preg_replace('/[^0-9]/', '', (string) $cnpj);
- $invalidos = ['00000000000000', '11111111111111', '22222222222222', '33333333333333', '44444444444444', '55555555555555', '66666666666666', '77777777777777', '88888888888888', '99999999999999'];
- if(strlen($cnpj) <> 14){
- return false;
- }elseif (in_array($cnpj, $invalidos)){
- return false;
- }
- for ($i = 0, $j = 5, $soma = 0; $i < 12; $i++){
- $soma += $cnpj{$i} * $j;
- $j = ($j == 2) ? 9 : $j - 1;
- }
- $resto = $soma % 11;
- if ($cnpj{12} <> ($resto < 2 ? 0 : 11 - $resto)){
- return false;
- }
- for ($i = 0, $j = 6, $soma = 0; $i < 13; $i++){
- $soma += $cnpj{$i} * $j;
- $j = ($j == 2) ? 9 : $j - 1;
- }
- $resto = $soma % 11;
- return $cnpj{13} == ($resto < 2 ? 0 : 11 - $resto);
- }
- public static function validaData($data){
- $data = explode("/", $data);
- if (checkdate($data[1], $data[0], $data[2])) {
- return true;
- }else{
- return false;
- }
- }
- public static function StrUrl($string){
- $a = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜüÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿŔŕ"!@#$%&*()_-+={[}]/?;:.,\\\'<>°ºª';
- $b = 'aaaaaaaceeeeiiiidnoooooouuuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyrr ';
- $string = utf8_decode($string);
- $string = strtr($string, utf8_decode($a), $b);
- $string = strip_tags(trim($string));
- $string = str_replace(" ","-",$string);
- $string = str_replace(array("-----","----","---","--"),"-",$string);
- return strtolower(utf8_encode($string));
- }
-
- public static function UrlAtual(){
- return "http://" . $_SERVER['SERVER_NAME'] . $_SERVER ['REQUEST_URI'];
- }
-
- public static function DataForSql($data){
- return implode("-", array_reverse(explode("/", $data)));
- }
-
- public static function DataForUser($data){
- return str_replace("-", "/", implode('-', array_reverse(explode('-', $data))));
- }
-
- public static function GeraAes($pass){
- self::$Data = $pass;
- self::$Data = AesCtr::encrypt(self::$Data, "mi@dia#", 256);
- self::$Data = base64_encode(self::$Data);
- return self::$Data;
- }
-
- public static function CheckAes($pass){
- self::$Data = $pass;
- self::$Data = base64_decode(self::$Data);
- self::$Data = AesCtr::decrypt(self::$Data, "mi@dia#", 256);
- return self::$Data;
- }
-
- public static function GeraCodigo($lenght = 8){
- $returnCode = '';
- $chars = '';
- $chars .= strtoupper('abcdefghijkmnpqrstuvwxyz');
- $chars .= 'ABCDEFGHJKLMNPQRSTUVWXYZ';
- $chars .= '123456789';
- $count = strlen($chars);
- for ($n = 1; $n <= $lenght; $n++) {
- $rand = mt_rand(1, $count);
- $returnCode .= $chars[$rand - 1];
- }
- return $returnCode;
- }
-
- public static function EnviarEmail($assunto,$msg_mail,$emailremetente,$nomeremetente,$emaildestino,$nomedestino){
- require_once(__DIR__."/../../class/Library/PHPMailer/class.phpmailer.php");
- $mail = new PHPMailer();
- $mail->IsSMTP();
- $mail->SMTPDebug = 1;
- $mail->SMTPAuth = true;
- $mail->IsHTML(true);
- $mail->Host = MAILHOST;
- $mail->Username = MAILUSER;
- $mail->Password = MAILPASS;
- $mail->Port = 587;
- $mail->CharSet = 'UTF-8';
- $mail->FromName = $nomeremetente;
- $mail->From = MAILUSER;
- $mail->AddReplyTo($emailremetente, $nomeremetente);
- $mail->Subject = $assunto;
- $mail->MsgHTML($msg_mail);
- $mail->AddAddress($emaildestino,$nomedestino);
- if($mail->Send()){
- return true;
- }else{
- return false;
- }
- }
-
- public static function GeraHash($tempo, $slat, $senha){
- return crypt($senha, '$2a$'.$tempo.'$'.$slat.'$');
- }
- public static function CheckHash($senha_post, $senha_bdcrypt){
- return (crypt($senha_post, $senha_bdcrypt) === $senha_bdcrypt);
- }
- public static function Base3($senha){
- return base64_encode(base64_encode(base64_encode($senha)));
- }
- public static function Rebase3($senha){
- return base64_decode(base64_decode(base64_decode($senha)));
- }
- public static function StatusCompra($status){
- if($status == "pending"){return array("Aguardando", "Fatura pendente, ainda não paga", "label-warning");}
- elseif($status == "paid"){return array("Aprovado", "Fatura paga", "label-success");}
- elseif($status == "canceled"){return array("Cancelada", "Cobrança cancelada", "label-danger");}
- elseif($status == "draft"){return array("Rascunho", "Ao criar uma fatura pelo painel somente, pode usar a chave rascunho para salvar os dados de uma fatura, a fatura em si ainda não foi gerada, não há uma cobrança, apenas dados salvos que podem se tornar uma cobrança.", "label-rascunho");}
- elseif($status == "partially_paid"){return array("Parcial", "Fatura parcialmente paga; em geral, quando um boleto não é pago o valor total da cobrança ou não é pago o valor da multa e juros pelo atraso. (pagamento após o vencimento).", "label-default");}
- elseif($status == "refunded"){return array("Reembolsada", "Fatura reembolsada", "label-muted");}
- elseif($status == "expired"){return array("Expirada", "Cobrança atingiu o tempo limite após o vencimento e expirou; não pode mais ser paga.", "label-muted");}
- elseif($status == "in_protest"){return array("Protesto", "Quando uma fatura já paga recebe uma notificação de não reconhecimento da compra.", "label-purple");}
- elseif($status == "chargeback"){return array("Reembolsada", "Reembolsado para o cliente, que ganhou a disputa.", "label-inverse");}
- else{return array("Erro", "Houve algum erro com o pagamento", "label-black");}
- }
- public static function StatusAutomacao($status){
- if($status == "0"){return array("Iniciada", "Automação iniciada", "label-assativa");}
- elseif($status == "1"){return array("Não iniciada", "Automação não iniciada", "label-assinativa");}
- else{return array("Erro", "Houve algum erro", "label-black");}
- }
- public static function StatusAssinatura($status){
- if($status == "0"){return array("Aguardando", "Aguardando inclusão de recorrência", "label-muted");}
- elseif($status == "1"){return array("Inativa", "Assinatura inativa", "label-assinativa");}
- elseif($status == "2"){return array("Ativa", "Assinatura ativa", "label-assativa");}
- else{return array("Erro", "Houve algum erro", "label-black");}
- }
- public static function criptografia_segura($data) {
- $first_key = base64_decode(FIRSTKEY);
- $second_key = base64_decode(SECONDKEY);
- $method = "aes-256-cbc";
- $iv_length = openssl_cipher_iv_length($method);
- $iv = openssl_random_pseudo_bytes($iv_length);
-
- $first_encrypted = openssl_encrypt($data, $method, $first_key, OPENSSL_RAW_DATA, $iv);
- $second_encrypted = hash_hmac('sha512', $first_encrypted, $second_key, TRUE);
-
- $output = self::Base3($iv.$second_encrypted.$first_encrypted);
- return $output;
- }
- public static function descriptografia_segura($input) {
- $first_key = base64_decode(FIRSTKEY);
- $second_key = base64_decode(SECONDKEY);
- $mix = self::Rebase3($input);
-
- $method = "aes-256-cbc";
- $iv_length = openssl_cipher_iv_length($method);
-
- $iv = substr($mix,0,$iv_length);
- $second_encrypted = substr($mix,$iv_length,64);
- $first_encrypted = substr($mix,$iv_length+64);
-
- $data = openssl_decrypt($first_encrypted,$method,$first_key,OPENSSL_RAW_DATA,$iv);
- $second_encrypted_new = hash_hmac('sha512', $first_encrypted, $second_key, TRUE);
-
- if (hash_equals($second_encrypted,$second_encrypted_new))
- return $data;
-
- return false;
- }
- }