forked from Claudio/integracao
Lucas Ayala
2 years ago
20 changed files with 2945 additions and 284 deletions
@ -0,0 +1,33 @@ |
|||||||
|
<?php |
||||||
|
$url = isset($_GET['url']) ? base64_decode($_GET['url']) : null; |
||||||
|
|
||||||
|
if ($url) { |
||||||
|
$redirect = "http://{$_SERVER['SERVER_NAME']}/" . $url; |
||||||
|
} |
||||||
|
?> |
||||||
|
<!DOCTYPE html> |
||||||
|
<html> |
||||||
|
<head> |
||||||
|
<title>Simples IP</title> |
||||||
|
<script> |
||||||
|
setTimeout(redirectPage, 1); |
||||||
|
function redirectPage() { |
||||||
|
window.location.href = '<?= $redirect ?>';
|
||||||
|
} |
||||||
|
</script> |
||||||
|
</head> |
||||||
|
<body> |
||||||
|
<table align="center" width="70%"> |
||||||
|
<tr align="center"> |
||||||
|
<td> |
||||||
|
<h4>Você está sendo redirecionado!</h4> |
||||||
|
</td> |
||||||
|
</tr> |
||||||
|
<tr align="center"> |
||||||
|
<td> |
||||||
|
<h5>Aguarde alguns segundos...</h5> |
||||||
|
</td> |
||||||
|
</tr> |
||||||
|
</table> |
||||||
|
</body> |
||||||
|
</html> |
@ -0,0 +1,33 @@ |
|||||||
|
<?php |
||||||
|
/* |
||||||
|
* Verifica se o tipo de execução é custom, se for desvia a execução |
||||||
|
* para um script customizado para integração, capturando antes os |
||||||
|
* parametros de entrada. O programas ou scripts ficam localizados |
||||||
|
* no diretorio /var/lib/asterisk/scripts/integracao/custom. |
||||||
|
*/ |
||||||
|
|
||||||
|
if ($agi->get_variable('MEMBERINTERFACE', true) || $evento == 'ABANDON') { |
||||||
|
$query = "SELECT itgm_id FROM pbx_integracao_reg a, pbx_integracao_metodo b WHERE b.itgm_id_pai = a.reg_id_metodo AND b.evento = 2 AND a.reg_uniqueid = '{$arg1}'"; |
||||||
|
$result = pg_query($query); |
||||||
|
$idMetodo = pg_fetch_assoc($result)['itgm_id']; |
||||||
|
|
||||||
|
$pathIntegra = "/var/lib/asterisk/scripts/integracao/custom/"; |
||||||
|
|
||||||
|
if ($idMetodo && ExecuteCustom($idMetodo, $nomeMetodo)) { |
||||||
|
__logStr("ExecuteCustom", $nomeMetodo, 'serverAgi', true); |
||||||
|
|
||||||
|
/** Movidesk * */ |
||||||
|
if (file_exists($pathIntegra . "Movidesk.php") && $evento == 'ABANDON') { |
||||||
|
include $pathIntegra . "abandonadaChamada.php"; |
||||||
|
} else { |
||||||
|
include($nomeMetodo); |
||||||
|
} |
||||||
|
|
||||||
|
exit; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
?> |
@ -0,0 +1,432 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
require_once 'phpagi/phpagi.php'; |
||||||
|
require_once 'IntegracaoDataBase.php'; |
||||||
|
require_once 'Logger.php'; |
||||||
|
require_once 'SimpleMail.php'; |
||||||
|
require_once 'conf.php'; |
||||||
|
// include("util/util.php"); |
||||||
|
|
||||||
|
/* |
||||||
|
|************************************************************************* |
||||||
|
| SISTEMA UNICO DE INTEGRACAO * |
||||||
|
|************************************************************************* |
||||||
|
*/ |
||||||
|
|
||||||
|
/** |
||||||
|
* Classe para implementacao das integracoes da Simples IP |
||||||
|
* @author Lucas Awade |
||||||
|
* @Desenvolvedor |
||||||
|
* @SimplesIP |
||||||
|
* @version 1.1.1 |
||||||
|
*/ |
||||||
|
class Integracao { |
||||||
|
|
||||||
|
protected $log; |
||||||
|
protected $mail; |
||||||
|
private $agi; |
||||||
|
private $db; |
||||||
|
|
||||||
|
######################################################################## |
||||||
|
## CONSTANTES DA CLASSE ## |
||||||
|
######################################################################## |
||||||
|
|
||||||
|
const PATH_SOUND = '/var/lib/asterisk/sounds/customizados/'; |
||||||
|
const AGI_ERROR_HANDLER = false; |
||||||
|
|
||||||
|
######################################################################## |
||||||
|
## FUNCOES UTILITARIAS ## |
||||||
|
######################################################################## |
||||||
|
|
||||||
|
/** |
||||||
|
* - Retorno é os dados referentes ao retornado da API |
||||||
|
* - Params são os dados que serão disponibilizados na tela do agente. |
||||||
|
* |
||||||
|
* @param array $retorno |
||||||
|
* @param array $params |
||||||
|
*/ |
||||||
|
public function integracaoAgente($retorno, $params) { |
||||||
|
$registros = 0; |
||||||
|
$data = $this->db()->getRegistros(); |
||||||
|
$retorno_cliente = "{$data['reg_uniqueid']}|{$data['reg_fone']}|{$data['id']}|"; |
||||||
|
foreach ($retorno as $key => $value) { |
||||||
|
if (in_array($key, $params)) { |
||||||
|
$registros++; |
||||||
|
$retorno_cliente .= sprintf("%s:%s", |
||||||
|
strtoupper(str_replace("_", " ", $key)), |
||||||
|
strtoupper(str_replace("|", "", $value))); |
||||||
|
if (count($params) > $registros) { |
||||||
|
$retorno_cliente .= "|"; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
$this->db()->setRegistros(array('retorno_cliente' => $retorno_cliente)); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Converte um CPF ou CNPJ com marcara referente a cada modelo. |
||||||
|
* |
||||||
|
* @param string $string |
||||||
|
* @return boolean|string |
||||||
|
*/ |
||||||
|
protected function mask_cpf_cnpj($string) { |
||||||
|
if (!empty($string)) { |
||||||
|
if (strlen($string) == 11) { |
||||||
|
$mask = "%s%s%s.%s%s%s.%s%s%s-%s%s"; |
||||||
|
} elseif (strlen($string) == 14) { |
||||||
|
$mask = "%s%s.%s%s%s.%s%s%s/%s%s%s%s-%s%s"; |
||||||
|
} |
||||||
|
return vsprintf($mask, str_split($string)); |
||||||
|
} else { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Retorna telefone com o (DDD)Numero de acordo com o parametro passado caso nao possua DDD sera o padrao da central. |
||||||
|
* @param string $telefone |
||||||
|
* @return array |
||||||
|
*/ |
||||||
|
protected function mask_phone_tel($telefone) { |
||||||
|
$retorno = array('telefone' => '', 'type' => ''); |
||||||
|
if (strlen($telefone) == 11) { |
||||||
|
$numero_1 = substr($telefone, 2, 5); |
||||||
|
$numero_2 = substr($telefone, 7, 4); |
||||||
|
$ddd = substr($telefone, 0, 2); |
||||||
|
$telefone = "({$ddd}) " . $numero_1 . '-' . $numero_2; |
||||||
|
$retorno['telefone'] = $telefone; |
||||||
|
$retorno['type'] = 'celular'; |
||||||
|
} else if (strlen($telefone) == 12) { |
||||||
|
$ddd = substr($telefone, 1, 2); |
||||||
|
$numero = substr($telefone, 3, 10); |
||||||
|
$telefone = sprintf("(%s) %s", $ddd, $numero); |
||||||
|
$retorno['telefone'] = $telefone; |
||||||
|
$retorno['type'] = 'telefone'; |
||||||
|
} else if (strlen($telefone) == 13) { |
||||||
|
$ddd = substr($telefone, 2, 2); |
||||||
|
$numero = substr($telefone, 4, 10); |
||||||
|
$telefone = "(" . $ddd . ") " . $numero; |
||||||
|
$retorno['telefone'] = $telefone; |
||||||
|
$retorno['type'] = 'celular'; |
||||||
|
} else if (strlen($telefone) == 10) { |
||||||
|
$numero_1 = substr($telefone, 2, 4); |
||||||
|
$numero_2 = substr($telefone, 6, 9); |
||||||
|
$ddd = substr($telefone, 0, 2); |
||||||
|
$telefone = "({$ddd}) " . $numero_1 . '-' . $numero_2; |
||||||
|
$retorno['telefone'] = $telefone; |
||||||
|
$retorno['type'] = 'fixo'; |
||||||
|
} else { |
||||||
|
return null; |
||||||
|
} |
||||||
|
return $retorno; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Verifica se todos os parametros passados foram completados. |
||||||
|
* |
||||||
|
* @param array $args |
||||||
|
* @return true|false |
||||||
|
*/ |
||||||
|
protected function getArgs($args) { |
||||||
|
foreach ($args as $value) { |
||||||
|
if (!$value) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Verifica a diferenca em dias entre a data de entrada e a data atual. |
||||||
|
* |
||||||
|
* @param type $dateParam |
||||||
|
* @return type |
||||||
|
*/ |
||||||
|
public function dateDiff($dateParam) { |
||||||
|
$dateIni = strtotime(date('Y-m-d')); |
||||||
|
|
||||||
|
if (strstr($dateParam, "/")) { |
||||||
|
$dateParam = date(strtotime(str_replace('/', '-', $dateParam))); |
||||||
|
} else { |
||||||
|
$dateParam = date(strtotime($dateParam)); |
||||||
|
} |
||||||
|
|
||||||
|
$diff = ($dateIni - $dateParam); |
||||||
|
$days = (int) floor($diff / (60 * 60 * 24)); |
||||||
|
|
||||||
|
return $days; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Realiza a conversao de datas no formato brasileiro para a Defualt. |
||||||
|
* @param string $data |
||||||
|
* @return string|boolean |
||||||
|
*/ |
||||||
|
public function convertDateDefault($data) { |
||||||
|
if ($data) { |
||||||
|
$data = explode('/', $data); |
||||||
|
return "{$data[2]}-{$data[1]}-{$data[0]}"; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Realiza a validação de pendencia financeira de acordo com a data informada |
||||||
|
* |
||||||
|
* OBS: O array deve possui nível = array( array('data' => 'yyyy-mm-dd')) |
||||||
|
* OBS: O indice deve ser o nome do valor onde se encontra a data. |
||||||
|
* @param array $array |
||||||
|
* @param string $indice |
||||||
|
* @param string $datavalida |
||||||
|
* @return boolean |
||||||
|
*/ |
||||||
|
public function verificaDataPendencia($array, $indice, $datavalida = null) { |
||||||
|
if (!$datavalida) { |
||||||
|
$datavalida = strototime(date('Y-m-d')); |
||||||
|
} else { |
||||||
|
$datavalida = strtotime($datavalida); |
||||||
|
} |
||||||
|
|
||||||
|
foreach ($array as $value) { |
||||||
|
if (strtotime($value[$indice]) < $datavalida) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Adiciona mascara no telefone para realizar a consulta |
||||||
|
* |
||||||
|
* @param type $value |
||||||
|
* @return boolean |
||||||
|
*/ |
||||||
|
protected function phoneMask($value) { |
||||||
|
if (!empty($value)) { |
||||||
|
if (strlen($value) == 10) { |
||||||
|
$mask = '(%s%s)%s%s%s%s-%s%s%s%s'; |
||||||
|
} elseif (strlen($value) == 11) { |
||||||
|
$mask = '(%s%s)%s%s%s%s%s-%s%s%s%s'; |
||||||
|
} |
||||||
|
return vsprintf($mask, str_split($value)); |
||||||
|
} else { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Verifica se um servidor esta dando resposta de ping |
||||||
|
* @param type $ip |
||||||
|
* @return boolean |
||||||
|
*/ |
||||||
|
public function verificaLatencia($ip) { |
||||||
|
$ping = explode(",", shell_exec("ping -c 4 $ip")); |
||||||
|
$latencia = substr($ping[1], 1, 2); //resposta do ping |
||||||
|
if ($latencia >= 2) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Verbaliza digitos |
||||||
|
* @param string|int $string |
||||||
|
*/ |
||||||
|
public function verbaliza($string) { |
||||||
|
$vocaliza = str_split($string); |
||||||
|
foreach ($vocaliza as $value) { |
||||||
|
$this->agi()->say_digits($value); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/* |
||||||
|
* Objetct to array |
||||||
|
*/ |
||||||
|
|
||||||
|
public function objectToArray($d) { |
||||||
|
if (is_object($d)) { |
||||||
|
$d = get_object_vars($d); |
||||||
|
} |
||||||
|
if (is_array($d)) { |
||||||
|
return array_map($this->objectToArray(), $d); |
||||||
|
} else { |
||||||
|
return $d; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
############################################################################ |
||||||
|
######## CONFIGURACAO DE EMAIL ######## |
||||||
|
############################################################################ |
||||||
|
|
||||||
|
/** |
||||||
|
* Inicia a instanciacao da Classe para envio de Email |
||||||
|
* @param string $email |
||||||
|
* @param string $password |
||||||
|
* @param string $subject |
||||||
|
*/ |
||||||
|
public function SimpleMail($email, $password, $subject) { |
||||||
|
$this->mail = new SimpleMail($email, $password, $subject); |
||||||
|
$this->log->debug("Instanciacao SimpleMail : " . ($this->mail ? "SUCCESS" : "FAIL"), debug_backtrace()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Configura o servidor de Envio dos Emails. |
||||||
|
* @param string $host |
||||||
|
* @param string|int $port |
||||||
|
* @param string $title |
||||||
|
* @return boolean |
||||||
|
*/ |
||||||
|
public function confSMTP($host, $port, $title, $smtpAuth = true, $smtpSecure = 'TLS', $charset = 'UTF8') { |
||||||
|
$this->log->debug("Configuracao SMTP", debug_backtrace()); |
||||||
|
if ($this->mail) { |
||||||
|
$this->mail->config($host, $port, $title, $smtpAuth, $smtpSecure, $charset); |
||||||
|
} else { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function geraProtocoloSimples($uid){ |
||||||
|
$connPG = $this->db()->getConnection(); |
||||||
|
$protoAgente = GeraProtocolo($connPG, $uid); |
||||||
|
return $protoAgente; |
||||||
|
} |
||||||
|
######################################################################## |
||||||
|
## FUNCOES IMPORTANTES ## |
||||||
|
######################################################################## |
||||||
|
|
||||||
|
/** |
||||||
|
* Instancia um objeto de Logger. |
||||||
|
* |
||||||
|
* @param Logger $log |
||||||
|
*/ |
||||||
|
protected function setLog($log = false) { |
||||||
|
if (!$this->log) { |
||||||
|
$this->log = new Logger(CONF_LOGGER_PATH . CONF_NOME_EMPRESA, $log); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Retorna o objeto de Log. |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function log() { |
||||||
|
return $this->log; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Instancia um objeto de AGI para se conectar com as informações do Asterisk. |
||||||
|
* |
||||||
|
* @return AGI |
||||||
|
*/ |
||||||
|
public function agi() { |
||||||
|
if (!$this->agi) { |
||||||
|
$data = $this->db()->getAuthAgi(); |
||||||
|
$config = array(); |
||||||
|
$config['phpagi']['error_handler'] = self::AGI_ERROR_HANDLER; |
||||||
|
$config['asmanager']['server'] = $data['host_sck']; |
||||||
|
$config['asmanager']['port'] = $data['porta_sck']; |
||||||
|
$config['asmanager']['username'] = $data['usuario_sck']; |
||||||
|
$config['asmanager']['secret'] = $data['senha_sck']; |
||||||
|
$agi = new AGI('phpagi.conf', $config); |
||||||
|
$this->agi = $agi; |
||||||
|
return $agi; |
||||||
|
} |
||||||
|
return $this->agi; |
||||||
|
} |
||||||
|
|
||||||
|
private function readAgiVariable() { |
||||||
|
ob_implicit_flush(true); |
||||||
|
set_time_limit(6); |
||||||
|
$in = fopen("php://stdin", "r"); |
||||||
|
$input = str_replace("\n", "", fgets($in, 4096)); |
||||||
|
fclose($in); |
||||||
|
$this->log->debug("Reading: " . $input); |
||||||
|
return $input; |
||||||
|
} |
||||||
|
|
||||||
|
public function getAgiVariable($variable = null) { |
||||||
|
$this->log->debug("Executing...", debug_backtrace()); |
||||||
|
while ($env = $this->readAgiVariable()) { |
||||||
|
$s = explode(": ", $env); |
||||||
|
$agi[str_replace("agi_", "", $s[0])] = trim($s[1]); |
||||||
|
$this->log->debug("Data: " . print_r($agi, true), debug_backtrace()); |
||||||
|
if (($env == "") || ($env == "\n")) { |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
$this->log->debug("Reading: " . print_r($agi, true)); |
||||||
|
if (!$variable || $agi[$variable]) { |
||||||
|
return !$variable ? $agi : $agi[$variable]; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Instancia um objeto de IntegracaoDataBase para se conectar com dados do |
||||||
|
* Banco de dados e credenciais do Manager. |
||||||
|
* |
||||||
|
* @return IntegracaoDataBase |
||||||
|
*/ |
||||||
|
public function db() { |
||||||
|
if (!$this->db) { |
||||||
|
$db = new IntegracaoDataBase(); |
||||||
|
$this->db = $db; |
||||||
|
} |
||||||
|
return $this->db; |
||||||
|
} |
||||||
|
|
||||||
|
protected function audioError() { |
||||||
|
$this->log->debug('Audio de error'); |
||||||
|
$exten = $this->db()->getAnuncio(); |
||||||
|
$this->log->error('Encaminhando para áudio alternativo de Erros', debug_backtrace()); |
||||||
|
$this->agi()->exec_goto($exten); |
||||||
|
exit(); |
||||||
|
} |
||||||
|
|
||||||
|
protected function integracaoReg(){ |
||||||
|
global $ura, $tronco, $uid, $idMetodo, $uidOld, $numero; |
||||||
|
$registros = array( |
||||||
|
"id" => '', |
||||||
|
"reg_ura" => $ura, |
||||||
|
"reg_tronco" => $tronco, |
||||||
|
"reg_uniqueid" => $uid, |
||||||
|
"reg_id_metodo" => $idMetodo, |
||||||
|
"reg_uniqueid" => $uid, |
||||||
|
"reg_uniqueid_old" => $uidOld, |
||||||
|
"reg_fone" => $numero, |
||||||
|
"reg_status_exec" => '1', |
||||||
|
"reg_inicio" => date()); |
||||||
|
|
||||||
|
$this->db()->setRegistros($registros); |
||||||
|
|
||||||
|
if (CONF_AUDIO_ERROR) { |
||||||
|
$this->db()->setIdAudioError($this->db()->getAnuncioIdByName(strtoupper(CONF_AUDIO_ERROR.CONF_NOME_EMPRESA.CONF_VERSAO))); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function executarFluxo($tipo, $nome){ |
||||||
|
#INEGRACAO DE TELA |
||||||
|
if(CONF_INTEGRACAO_TELA){ |
||||||
|
$parametrosTela = unserialize(CONF_PARAMETROS_TELA); |
||||||
|
|
||||||
|
$itgcTela = array(); |
||||||
|
$itgcTelaNomes = array(); |
||||||
|
|
||||||
|
foreach($parametrosTela as $parametro){ |
||||||
|
$itgcTela[$parametro] = str_replace(array("|",":"),"-",utf8_decode($this->agi()->get_variable($parametro, true))); |
||||||
|
$itgcTelaNomes[] = "$parametro"; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
$this->integracaoAgente($itgcTela, $itgcTelaNomes); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
$dbFunction = "get".ucfirst(strtolower($tipo));//CONTRUIR NOME FUNCAO BUSCAR ID |
||||||
|
$this->agi()->exec_goto($this->db()->$dbFunction(strtoupper($nome)));//REDIRECIONAR FLUXO |
||||||
|
|
||||||
|
|
||||||
|
$this->db()->atualizaIntegracao(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
@ -0,0 +1,929 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
require_once 'Logger.php'; |
||||||
|
|
||||||
|
// ************************************************************************* |
||||||
|
// * SISTEMA UNICO DE INTEGRACAO * |
||||||
|
// ************************************************************************* |
||||||
|
|
||||||
|
/** |
||||||
|
* Classe para utilizar os registros do Banco de Dados; |
||||||
|
* |
||||||
|
* OBS: Deixar como usuario de execucao e acesso pbx:pbx |
||||||
|
* |
||||||
|
* @author Lucas Awade |
||||||
|
* @function developer |
||||||
|
* @company SimplesIP |
||||||
|
* @version 1.0.1 |
||||||
|
*/ |
||||||
|
class IntegracaoDataBase { |
||||||
|
|
||||||
|
private $query; |
||||||
|
private $connection; |
||||||
|
private $registros; |
||||||
|
private $log; |
||||||
|
private $credentials = array(); |
||||||
|
private $debug; |
||||||
|
private $idAudioError; |
||||||
|
|
||||||
|
/** |
||||||
|
* @file |
||||||
|
* Arquivo de configuracoes do banco e manager |
||||||
|
*/ |
||||||
|
const FILE_DB = "/var/www/html/include/bd"; |
||||||
|
|
||||||
|
public function __construct() { |
||||||
|
$this->log = new Logger(CONF_LOGGER_PATH, CONF_LOGGER_DB_ATIVO); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Retorna os registros armazenados na variavel registros pra ser disponibilizado |
||||||
|
* no decorrer de uma integracao; |
||||||
|
* |
||||||
|
* @return array |
||||||
|
*/ |
||||||
|
public function getRegistros() { |
||||||
|
return $this->registros; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Informações coletadas do banco de dados para ser escritas no log. |
||||||
|
* |
||||||
|
* @param array $array |
||||||
|
*/ |
||||||
|
private function logResult($array) { |
||||||
|
if ($array) { |
||||||
|
$this->log->success(print_r($array, true), $this->debug); |
||||||
|
} else { |
||||||
|
$this->log->warning('Nenhum resultado encontrado!', $this->debug); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Audio de erro padrao |
||||||
|
* |
||||||
|
* @param string $idAudioError |
||||||
|
*/ |
||||||
|
public function setIdAudioError($idAudioError) { |
||||||
|
$this->idAudioError = $idAudioError; |
||||||
|
} |
||||||
|
|
||||||
|
######################################################################## |
||||||
|
## BANCO DE DADOS ## |
||||||
|
######################################################################## |
||||||
|
|
||||||
|
/** |
||||||
|
* Pega as informacoes das credenciais no arquivos padrao "bd"; |
||||||
|
*/ |
||||||
|
private function filedb() { |
||||||
|
if (file_exists(self::FILE_DB)) { |
||||||
|
$contents = fopen(self::FILE_DB, 'r'); |
||||||
|
while (!feof($contents) && $contents) { |
||||||
|
$str = fgets($contents); |
||||||
|
$dados = explode('=', $str); |
||||||
|
$this->credentials[strtolower($dados[0])] = str_replace('"', '', $dados[1]); |
||||||
|
} |
||||||
|
fclose($contents); |
||||||
|
$this->credentials = array_filter($this->credentials); |
||||||
|
$this->log->debug("Credenciais banco de dados: " . print_r($this->credentials, true), debug_backtrace()); |
||||||
|
} else { |
||||||
|
$this->log->error("Nao foi possivel encontrar o arquivo 'bd' em " . self::FILE_DB); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Executa as querys da consulta a serem feitas, além de gravar logs de registros de dados, |
||||||
|
* e qualquer erro é repassado a classe que está executando para tratar o erro; |
||||||
|
* |
||||||
|
* @param string $type |
||||||
|
* @return boolean|array |
||||||
|
*/ |
||||||
|
private function execute($type = '') { |
||||||
|
try { |
||||||
|
if (!$this->connection) { |
||||||
|
$this->connection = $this->getConnection(); |
||||||
|
} |
||||||
|
|
||||||
|
$result = pg_query($this->connection, $this->query); |
||||||
|
switch (strtolower($type)) { |
||||||
|
case 'assoc': |
||||||
|
$data = pg_fetch_assoc($result); |
||||||
|
break; |
||||||
|
case 'row': |
||||||
|
$data = pg_fetch_row($result); |
||||||
|
break; |
||||||
|
case 'array': |
||||||
|
$data = pg_fetch_array($result); |
||||||
|
break; |
||||||
|
case 'all': |
||||||
|
$data = pg_fetch_all($result); |
||||||
|
break; |
||||||
|
} |
||||||
|
$this->logResult($data); |
||||||
|
return $data; |
||||||
|
} catch (Exception $ex) { |
||||||
|
$this->log->error("Exception: {$ex->getMessage()} | Postgres : " . pg_last_error(), $this->debug); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function getConnection() { |
||||||
|
$this->filedb(); |
||||||
|
$this->connection = pg_connect(sprintf('host=%s port=%s dbname=%s user=%s password=%s', $this->credentials['host_db'], $this->credentials['porta_db'], $this->credentials['base_db'], $this->credentials['usuario'], $this->credentials['senha'])); |
||||||
|
if (pg_connection_status($this->connection) === 0) { |
||||||
|
$this->log->success("Conectado na base {$this->credentials['base_db']}.", debug_backtrace()); |
||||||
|
return $this->connection; |
||||||
|
} else { |
||||||
|
throw new Exception('Nao foi possivel conectar na base de dados'); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private function getPDO() { |
||||||
|
try { |
||||||
|
if (!$this->connection) { |
||||||
|
$drive = CONF_DB_DRIVER; |
||||||
|
$host = CONF_DB_HOST; |
||||||
|
$dbname = CONF_DB_BASE; |
||||||
|
$user = CONF_DB_USER; |
||||||
|
$pass = CONF_DB_PASSWD; |
||||||
|
|
||||||
|
$this->connection = new pdo("$drive:host=$host;dbname=$dbname", $user, $pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); |
||||||
|
} |
||||||
|
} catch (PDOException $ex) { |
||||||
|
$this->log->error("Exception: {$ex->getMessage()} ", $this->debug); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
protected function strquery($stmt, $params) { |
||||||
|
foreach ($params as $key => $value) { |
||||||
|
if (!$value || $value == "null") { |
||||||
|
$value = null; |
||||||
|
} |
||||||
|
$stmt->bindValue(":{$key}", $value, (is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function read($query, $params) { |
||||||
|
try{ |
||||||
|
|
||||||
|
$this->getPDO(); |
||||||
|
$stmt = $this->connection->prepare($query); |
||||||
|
$this->strquery($stmt, $params); |
||||||
|
$stmt->execute(); |
||||||
|
$data = $stmt->fetchAll(PDO::FETCH_ASSOC); |
||||||
|
|
||||||
|
return $data; |
||||||
|
} catch (PDOException $ex) { |
||||||
|
$this->log->error("Exception: {$ex->getMessage()} ", $this->debug); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function create($query, $params) { |
||||||
|
try{ |
||||||
|
|
||||||
|
$this->getPDO(); |
||||||
|
$stmt = $this->connection->prepare($query); |
||||||
|
$this->strquery($stmt, $params); |
||||||
|
$stmt->execute(); |
||||||
|
$data = $this->connection->lastInsertId(); |
||||||
|
|
||||||
|
return $data; |
||||||
|
} catch (PDOException $ex) { |
||||||
|
$this->log->error("Exception: {$ex->getMessage()} ", $this->debug); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function delete($query, $params) { |
||||||
|
try{ |
||||||
|
|
||||||
|
$this->getPDO(); |
||||||
|
|
||||||
|
$stmt = $this->connection->prepare($query); |
||||||
|
$this->strquery($stmt, $params); |
||||||
|
$stmt->execute(); |
||||||
|
$count = $stmt->rowCount(); |
||||||
|
$data = (!$count) ? false : true; |
||||||
|
|
||||||
|
return $data; |
||||||
|
} catch (PDOException $ex) { |
||||||
|
$this->log->error("Exception: {$ex->getMessage()} ", $this->debug); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function update($query, $params) { |
||||||
|
try{ |
||||||
|
|
||||||
|
$this->getPDO(); |
||||||
|
|
||||||
|
$stmt = $this->connection->prepare($query); |
||||||
|
$this->strquery($stmt, $params); |
||||||
|
$stmt->execute(); |
||||||
|
$count = $stmt->rowCount(); |
||||||
|
$data = (!$count) ? false : true; |
||||||
|
|
||||||
|
return $data; |
||||||
|
} catch (PDOException $ex) { |
||||||
|
$this->log->error("Exception: {$ex->getMessage()} ", $this->debug); |
||||||
|
} |
||||||
|
} |
||||||
|
######################################################################## |
||||||
|
## ASTERISK ## |
||||||
|
######################################################################## |
||||||
|
|
||||||
|
/** |
||||||
|
* Coleta em qual Asterisk a central está rodadando e retorna o modelo padrao |
||||||
|
* de configuracao do exten; |
||||||
|
* |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
private function getVersionAsterisk() { |
||||||
|
$result = array(); |
||||||
|
exec("asterisk -V", $result); |
||||||
|
$this->log->info("Versao Asterisk: " . $result[0], debug_backtrace()); |
||||||
|
if (strpos($result[0], '1.4') !== false) { |
||||||
|
return "|"; |
||||||
|
} else { |
||||||
|
return ","; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Transforma uma string exten com "," para "|" de acordo com a versao do Asterisk |
||||||
|
* |
||||||
|
* @param string $string |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
private function string_exten($string) { |
||||||
|
return str_replace(",", $this->getVersionAsterisk(), $string); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Coleta os dados de autenticacao do manager. |
||||||
|
* |
||||||
|
* @return array |
||||||
|
*/ |
||||||
|
public function getAuthAgi() { |
||||||
|
$data = array(); |
||||||
|
foreach ($this->credentials as $key => $value) { |
||||||
|
if (strpos($key, '_sck') !== false) { |
||||||
|
$data[$key] = $value; |
||||||
|
} |
||||||
|
} |
||||||
|
return $data; |
||||||
|
} |
||||||
|
|
||||||
|
######################################################################## |
||||||
|
## QUERYS ## |
||||||
|
######################################################################## |
||||||
|
|
||||||
|
/** |
||||||
|
* Inicia um Transacao; |
||||||
|
*/ |
||||||
|
public function beginTransaction() { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "BEGIN;"; |
||||||
|
$this->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Registra uma Transacao. |
||||||
|
*/ |
||||||
|
public function commitTransaction() { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "COMMIT;"; |
||||||
|
$this->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Retorna as informacoes antes da Transacao. |
||||||
|
*/ |
||||||
|
public function rollbackTransaction() { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "ROLLBACK;"; |
||||||
|
$this->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Busca o DDD padrao cadastrado na central. |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getDDD() { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT prm_ddd_padrao FROM pbx_parametros"; |
||||||
|
return $this->execute('assoc') ? $this->execute('assoc')['prm_ddd_padrao'] : ''; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Consulta no banco o nome do Anuncio e retorna o ID para ser escrito no exten; |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getAnuncio($name = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT id FROM pbx_anuncios WHERE nome = '$name'"; |
||||||
|
$result = $this->execute('row')[0] ? $this->execute('row')[0] : $this->idAudioError; |
||||||
|
return sprintf($this->string_exten('ext-anuncios,a%s,1'), $result); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Consulta no banco o nome do Ura e retorna o ID para ser escrito no exten; |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getUra($name = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT id FROM pbx_ura WHERE nome = '$name'"; |
||||||
|
$result = $this->execute('row')[0] ? $this->execute('row')[0] : $this->idAudioError; |
||||||
|
return sprintf($this->string_exten('ura-%s,s,1'), $result); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Consulta no banco a Ura por ID e retorna suas informacoes; |
||||||
|
* |
||||||
|
* @param string|int $id |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getURAById($id) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT * FROM pbx_ura WHERE id = '{$id}'"; |
||||||
|
$result = $this->execute('assoc') ? $this->execute('assoc') : $this->idAudioError; |
||||||
|
return $result; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Consulta no banco o nome do Fila e retorna o ID para ser escrito no exten; |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getFila($name = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT numero FROM pbx_dacs WHERE nome = '$name'"; |
||||||
|
$result = $this->execute('row')[0] ? $this->execute('row')[0] : $this->idAudioError; |
||||||
|
return sprintf($this->string_exten('ext-fila,%s,1'), $result); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Consulta no banco o nome do Ramal e retorna o ID para ser escrito no exten; |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getRamal($name = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT nome FROM pbx_ramais WHERE nome = '$name'"; |
||||||
|
$result = $this->execute('row')[0] ? $this->execute('row')[0] : $this->idAudioError; |
||||||
|
return sprintf($this->string_exten('ext-ramais,%s,1'), $result); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Coleta as opcoes que o usuario passou nas uras da sua chamada. |
||||||
|
* |
||||||
|
* @param string $uniqueid |
||||||
|
* @param int $ura_id |
||||||
|
* @return boolean|array |
||||||
|
*/ |
||||||
|
public function getOptionsURA($uniqueid, $ura_id) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT umv_ura_id,umv_ura_nome,uniqueid,umv_ura_opcao " |
||||||
|
. "FROM pbx_ura_movimento WHERE uniqueid = '$uniqueid' " |
||||||
|
. "AND umv_ura_opcao IS NOT NULL AND umv_opcao = 'ura' " |
||||||
|
. "AND umv_ura_id = $ura_id " |
||||||
|
. "ORDER BY umv_id"; |
||||||
|
return $this->execute('all'); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Retorna o ID e Data Hora da primeira entrada da ligacao em um URA de |
||||||
|
* acordo com o uniqueid; |
||||||
|
* @param string $uniqueid |
||||||
|
* @return array |
||||||
|
*/ |
||||||
|
public function getFristURA($uniqueid) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT destino, min(data_registro) " |
||||||
|
. "FROM ast_bilhetes_complemento " |
||||||
|
. "WHERE direcao = 'ura' " |
||||||
|
. "AND uniqueid2 = '{$uniqueid}' GROUP BY destino LIMIT 1"; |
||||||
|
$result = $this->execute('assoc'); |
||||||
|
return $result; |
||||||
|
} |
||||||
|
|
||||||
|
public function registraIntegracao() { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
/** |
||||||
|
* Parametros REGISTROS |
||||||
|
* |
||||||
|
* id -> Id do qual gerou a integracao para o retorno_cliente |
||||||
|
* |
||||||
|
* Todos os parametros devem ser igual a da tabela pbx_integracao_reg |
||||||
|
*/ |
||||||
|
$ura = trim($this->registros['reg_ura']) ? trim($this->registros['reg_ura']) : 'null'; |
||||||
|
$tronco = trim($this->registros['reg_tronco']) ? trim($this->registros['reg_tronco']) : 'null'; |
||||||
|
|
||||||
|
$this->query = "INSERT INTO pbx_integracao_reg (reg_id_metodo,reg_uniqueid,reg_uniqueid_old,reg_fone,reg_inicio, reg_tronco, reg_ura) |
||||||
|
SELECT * FROM ( |
||||||
|
SELECT {$this->registros['reg_id_metodo']},'{$this->registros['reg_uniqueid']}','{$this->registros['reg_uniqueid_old']}','{$this->registros['reg_fone']}', now(), '$tronco', '$ura') AS dados |
||||||
|
WHERE NOT EXISTS ( |
||||||
|
SELECT reg_uniqueid FROM pbx_integracao_reg WHERE reg_uniqueid = '{$this->registros['reg_uniqueid']}' |
||||||
|
) LIMIT 1;"; |
||||||
|
|
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
return $this->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
public function atualizaIntegracao() { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$tronco = trim($this->registros['reg_tronco']) ? sprintf(",\nreg_tronco='%s'", trim($this->registros['reg_tronco'])) : ''; |
||||||
|
$ura = trim($this->registros['reg_ura']) ? sprintf(",\nreg_ura='%s'", trim($this->registros['reg_ura'])) : ''; |
||||||
|
$reg_msg = QuotedStr($this->registros['reg_msg']); |
||||||
|
$reg_retorno = QuotedStr($this->registros['reg_fone']); |
||||||
|
$retorno_cliente = substr($this->registros['retorno_cliente'], 0, 255); |
||||||
|
$this->query = "UPDATE pbx_integracao_reg |
||||||
|
SET reg_fim = NOW(), |
||||||
|
reg_retorno = $reg_retorno, |
||||||
|
reg_msg = $reg_msg, |
||||||
|
reg_status_exec = '{$this->registros['reg_status_exec']}', |
||||||
|
retorno_cliente = '{$retorno_cliente}'{$tronco}{$ura} |
||||||
|
WHERE reg_uniqueid = '{$this->registros['reg_uniqueid']}'"; |
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
return $this->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Informa os dados para ser inseridos na tabela de integracao_reg; |
||||||
|
* |
||||||
|
* @param array $registros |
||||||
|
*/ |
||||||
|
public function setRegistros($registros) { |
||||||
|
if (is_array($registros) && !$this->registros) { |
||||||
|
$this->registros = $registros; |
||||||
|
$this->registraIntegracao(); |
||||||
|
} else { |
||||||
|
foreach ($registros as $key => $value) { |
||||||
|
$this->registros[$key] = $value; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Retorna todas as informacoes da tabela Supervisor_Dacs |
||||||
|
* @param array $dacs |
||||||
|
* @return array |
||||||
|
*/ |
||||||
|
public function getSupervisorDAC($dacs = array()) { |
||||||
|
$this->query = "SELECT * FROM pbx_supervisor_dacs "; |
||||||
|
if ($dacs) { |
||||||
|
$this->query .= sprintf("WHERE id IN (%s)", implode(",", $dacs)); |
||||||
|
} |
||||||
|
return $this->execute('all'); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Retorna credenciais cadastradas nos metodos de integracao |
||||||
|
* @param int $id |
||||||
|
* @return array |
||||||
|
*/ |
||||||
|
public function getConexaoAPI($id) { |
||||||
|
$this->query = "SELECT itgc_nome AS name, itgc_port AS id_user, itgc_host AS url, |
||||||
|
itgc_database AS token, itgc_user AS user, itgc_password AS password, |
||||||
|
itgc_timeout AS timeout |
||||||
|
FROM pbx_integracao_configuracao |
||||||
|
WHERE itgc_id = {$id}"; |
||||||
|
return $this->execute('assoc'); |
||||||
|
} |
||||||
|
|
||||||
|
public function getScriptCredenciais($script) { |
||||||
|
$this->query = "SELECT itgc_nome AS name, itgc_port AS id_user, itgc_host AS url, |
||||||
|
itgc_database AS token, itgc_user AS user, itgc_password AS password, |
||||||
|
itgc_timeout AS timeout |
||||||
|
FROM pbx_integracao_configuracao a |
||||||
|
INNER JOIN pbx_integracao_metodo b ON a.itgc_id = b.itgc_id |
||||||
|
WHERE itgm_comando = '{$script}'"; |
||||||
|
return $this->execute('assoc'); |
||||||
|
} |
||||||
|
|
||||||
|
public function getFilaPorNome($name) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT * FROM pbx_dacs WHERE nome = '$name'"; |
||||||
|
return $this->execute('assoc'); |
||||||
|
} |
||||||
|
|
||||||
|
public function getLastUraPorUniqueid($uniqueid) { |
||||||
|
$this->query = "SELECT data_reg, umv_ura_id,umv_ura_nome,uniqueid,umv_ura_opcao |
||||||
|
FROM pbx_ura_movimento WHERE uniqueid = '{$uniqueid}' |
||||||
|
ORDER BY data_reg DESC |
||||||
|
LIMIT 1"; |
||||||
|
return $this->execute('assoc'); |
||||||
|
} |
||||||
|
|
||||||
|
public function getUraMovimentoByUniqueid($uniqueid, $orderByAtt = "data_reg", $orderByType = "ASC") { |
||||||
|
$this->query = "SELECT data_reg, umv_ura_id,umv_ura_nome,uniqueid,umv_ura_opcao, umv_opcao |
||||||
|
FROM pbx_ura_movimento WHERE uniqueid = '{$uniqueid}' |
||||||
|
ORDER BY $orderByAtt $orderByType |
||||||
|
LIMIT 1"; |
||||||
|
return $this->execute('assoc'); |
||||||
|
} |
||||||
|
|
||||||
|
public function atualizaProtocoloParceiro($uid, $protocoloParceiro) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "UPDATE pbx_protocolo_reg |
||||||
|
SET protoparceiro='$protocoloParceiro' |
||||||
|
WHERE uniqueid='$uid'"; |
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
return $this->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
######################################################################## |
||||||
|
#### INSTALACAO INTEGRACAO #### |
||||||
|
######################################################################## |
||||||
|
|
||||||
|
public function findIntegracaoCustom() { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT itgp_id FROM pbx_integracao_protocolo WHERE itgp_descricao = 'Custom'"; |
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
$return = $this->execute('assoc'); |
||||||
|
if (!$return['itgp_id']) { |
||||||
|
$this->query = "INSERT INTO pbx_integracao_protocolo (itgt_id,itgp_id,itgp_descricao,itgp_status,itgp_prefix) VALUES(1,6,'Custom',0,'custom');"; |
||||||
|
$this->execute('assoc'); |
||||||
|
} |
||||||
|
return 6; |
||||||
|
} |
||||||
|
|
||||||
|
public function findVersion($empresa = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT MAX(itgc_status) AS versao FROM pbx_integracao_configuracao where 1=1"; |
||||||
|
if($empresa){ |
||||||
|
$this->query .= "AND itgc_nome like '%$empresa%'"; |
||||||
|
} |
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
return $this->execute('assoc'); |
||||||
|
} |
||||||
|
|
||||||
|
public function createIntegracaoAtiva($nome, $versao, $comando) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$query = "insert into pbx_integracao_metodo(itgc_id, itgm_nome, itgm_comando, itgm_tipo, itgm_retorno, itgm_status, opcao, evento, stored_params, itgm_id_pai) values(%s,'%s','%s',%s,%s,%s,'%s',%s,'%s',%s) returning itgm_id;"; |
||||||
|
|
||||||
|
$this->query = "INSERT INTO pbx_integracao_configuracao " |
||||||
|
. "(itgt_id, itgp_id, itgc_nome, itgc_host, itgc_port, itgc_database, itgc_user, itgc_password, itgc_timeout, itgc_status) " |
||||||
|
. "VALUES (1, 6, '$nome', '127.0.0.1', '0', '0', '0', '0', '10', '$versao') RETURNING itgc_id; "; |
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
$return = $this->execute('assoc'); |
||||||
|
|
||||||
|
$this->query = sprintf($query, $return['itgc_id'], $nome, $comando, 1, 1, 0, 'anuncios', 'null', 'null', 'null'); |
||||||
|
$res = $this->execute('assoc'); |
||||||
|
unset($this->query); |
||||||
|
|
||||||
|
for ($x = 2; $x < 4; $x++) { |
||||||
|
$this->query .= sprintf($this->query, $return['itgc_id'], 'NOEXEC', 'NOEXEC', 1, 0, 0, 'null', $x, 'null', $res['itgm_id']); |
||||||
|
} |
||||||
|
|
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
return $this->execute('assoc'); |
||||||
|
} |
||||||
|
|
||||||
|
/* Consulta no banco o nome da integracao metodo e retorna o ID; |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getIntegracaoMetodoByNameIntegracao($name = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT pim.itgm_id |
||||||
|
FROM pbx_integracao_metodo pim INNER JOIN pbx_integracao_configuracao pic on pic.itgc_id = pim.itgc_id |
||||||
|
WHERE pim.itgm_retorno = 1 AND pic.itgc_nome = '$name'"; |
||||||
|
$result = $this->execute('row')[0]; |
||||||
|
return $result; |
||||||
|
} |
||||||
|
|
||||||
|
######################### ANUNCIOS ######################### |
||||||
|
/* |
||||||
|
* Inserir Anuncio |
||||||
|
*/ |
||||||
|
public function addAnuncio($nome, $musica, $opcao = null, $acao = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = sprintf("INSERT INTO pbx_anuncios (nome, musica, opcao, acao) |
||||||
|
VALUES('%s','%s','%s', '%s')", $nome, $musica, strtolower($opcao), $acao); |
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
return $this->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
/* Consulta no banco o nome do Anuncio e retorna o ID; |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getAnuncioIdByName($name = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT id FROM pbx_anuncios WHERE nome = '$name'"; |
||||||
|
$result = $this->execute('row')[0]; |
||||||
|
return $result; |
||||||
|
} |
||||||
|
|
||||||
|
/* Consulta no banco o id do Anuncio e retorna o nome; |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getAnuncioNameById($id = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT nome FROM pbx_anuncios WHERE id = '$id'"; |
||||||
|
$result = $this->execute('row')[0]; |
||||||
|
return $result; |
||||||
|
} |
||||||
|
|
||||||
|
/* Atualiza no banco a acao e opcao do Anuncio; |
||||||
|
* |
||||||
|
* @param $opcao $acao Iid |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
|
||||||
|
public function updateAnuncio($opcao, $acao, $id) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = sprintf("UPDATE pbx_anuncios |
||||||
|
SET opcao='%s', acao='%s' |
||||||
|
WHERE id=%d", strtolower($opcao), $acao, $id); |
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
return $this->execute(); |
||||||
|
} |
||||||
|
######################### URAS ######################### |
||||||
|
/* |
||||||
|
* Inserir Ura |
||||||
|
*/ |
||||||
|
public function addUra($nome, $som_ura, $tempo_espera = 5, $timeout_digito = 3, $permite_ligacao = 'N', $opcao = null, $acao = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = sprintf("INSERT INTO pbx_ura (nome, permite_ligacao, tempo_espera, som_ura, opcao, acao, timeout_digito) |
||||||
|
VALUES('%s', '%s', %d, '%s', '%s', %d, '%s')", $nome, $permite_ligacao, $tempo_espera, $som_ura, strtolower($opcao), $acao, $timeout_digito); |
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
return $this->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
/* Consulta no banco o nome do Ura e retorna o ID; |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getUraIdByName($name = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT id FROM pbx_ura WHERE nome = '$name'"; |
||||||
|
$result = $this->execute('row')[0]; |
||||||
|
return $result; |
||||||
|
} |
||||||
|
|
||||||
|
/* Consulta no banco o ID do Ura e retorna o nome; |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getUraNameById($id = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT nome FROM pbx_ura WHERE id = '$id'"; |
||||||
|
$result = $this->execute('row')[0]; |
||||||
|
return $result; |
||||||
|
} |
||||||
|
|
||||||
|
/* Atualiza no banco acao e opcao do Ura; |
||||||
|
* |
||||||
|
* @param $opcao $acao Iid |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
|
||||||
|
public function updateUra($id, $opcao, $acao) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = sprintf("UPDATE pbx_ura |
||||||
|
SET opcao='%s', acao='%s' |
||||||
|
WHERE id=%d", strtolower($opcao), $acao, $id); |
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
return $this->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
######################### URAS DESTINO ######################### |
||||||
|
/* |
||||||
|
* Inserir Ura Destino |
||||||
|
*/ |
||||||
|
public function addUraDestino($id_ura, $numero, $tipo, $comando = null, $sequencia, $nome_comando) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = sprintf("INSERT INTO pbx_ura_destino |
||||||
|
(id_ura, numero, tipo, comando, sequencia, nome_comando) |
||||||
|
VALUES(%d, '%s', '%s', '%s', %d, '%s')", $id_ura, $numero, $tipo, $comando, $sequencia, $nome_comando); |
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
return $this->execute(s); |
||||||
|
} |
||||||
|
|
||||||
|
/* Consulta no banco o nome do Ura e retorna os destinos; |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getUraDestinosIdByUraName($name = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT pud.* FROM pbx_ura_destino pud inner join pbx_ura pu on pu.id = pud.id_ura WHERE pu.nome ='$name'"; |
||||||
|
$result = $this->execute('all'); |
||||||
|
return $result; |
||||||
|
} |
||||||
|
|
||||||
|
/* Atualiza no banco acao e opcao do Ura; |
||||||
|
* |
||||||
|
* @param $opcao $acao Iid |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
|
||||||
|
public function updateUraDestino($opcao, $acao, $id) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = sprintf("UPDATE pbx_ura |
||||||
|
SET opcao='%s', acao='%s' |
||||||
|
WHERE id=%d", strtolower($opcao), $acao, $id); |
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
return $this->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
######################### HORARIOS ######################### |
||||||
|
/* |
||||||
|
* Inserir Horario |
||||||
|
*/ |
||||||
|
public function addHorario($nome, $opcao = null, $acao = null, $status = 1) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = sprintf("INSERT INTO pbx_horarios |
||||||
|
(nome, opcao_nao, acao_nao, status) |
||||||
|
VALUES('%s', '%s', %d, %d)", $nome, strtolower($opcao), $acao, $status); |
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
return $this->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
/* Consulta no banco o nome do Horario e retorna o ID; |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getHorarioIdByName($name = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT id FROM pbx_horarios WHERE nome = '$name'"; |
||||||
|
$result = $this->execute('row')[0]; |
||||||
|
return $result; |
||||||
|
} |
||||||
|
|
||||||
|
/* Consulta no banco o ID do Horario e retorna o nome; |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getHorarioNameById($id = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT nome FROM pbx_horarios WHERE nome = '$id'"; |
||||||
|
$result = $this->execute('row')[0]; |
||||||
|
return $result; |
||||||
|
} |
||||||
|
|
||||||
|
/* Atualiza no banco opcao e acao do Horario; |
||||||
|
* |
||||||
|
* @param $opcao $acao Iid |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
|
||||||
|
public function updateHorario($id, $opcao, $acao) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = sprintf("UPDATE pbx_horarios |
||||||
|
SET opcao_nao='%s', acao_nao='%s' |
||||||
|
WHERE id=%d", strtolower($opcao), $acao, $id); |
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
return $this->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
######################### HORARIOS ITENS ######################### |
||||||
|
/* |
||||||
|
* Inserir Horario |
||||||
|
*/ |
||||||
|
public function addHorarioItens($id_horario, $horario_ini = '08:00', $horario_fim = '17:59', $dias_semana = 0, $semana = 'mon', $semana_fim = 'fri', $opcao = null, $acao = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = sprintf("INSERT INTO pbx_horarios_itens |
||||||
|
(id_horario, horario_inicio, horario_fim, todos_dias_semana, semana, semana_fim, opcao, acao) |
||||||
|
VALUES(%d, '%s', '%s', %d, '%s', '%s', '%s', '%s')", $id_horario, $horario_ini, $horario_fim, $dias_semana, $semana, $semana_fim, $opcao, $acao); |
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
return $this->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
/* Consulta no banco o nome do Horario retornar Horario Itens; |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getHorarioItensByHorarioName($name = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT phi.* FROM pbx_horarios_itens phi INNER JOIN pbx_horarios ph ON ph.id = phi.id_horario WHERE ph.nome = '$name'"; |
||||||
|
$result = $this->execute('all'); |
||||||
|
return $result; |
||||||
|
} |
||||||
|
|
||||||
|
/* Atualiza no banco a opcao e acao do Horario Item; |
||||||
|
* |
||||||
|
* @param $opcao $acao Iid |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
|
||||||
|
public function updateItemHorario($opcao, $acao, $id) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = sprintf("UPDATE pbx_horarios_itens |
||||||
|
SET opcao='%s', acao='%s' |
||||||
|
WHERE id=%d", strtolower($opcao), $acao, $id); |
||||||
|
$this->log->debug("Query: {$this->query}", $this->debug); |
||||||
|
return $this->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
######################### FILAS ######################### |
||||||
|
/* Consulta no banco o nome da Fila e retorna o numero; |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getFilaNumeroByName($name = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT numero FROM pbx_queues_grupos WHERE nome = '$name'"; |
||||||
|
$result = $this->execute('row')[0]; |
||||||
|
return $result; |
||||||
|
} |
||||||
|
|
||||||
|
/* Consulta no banco o numero da Fila e retorna o Nome; |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getFilaNameByNumero($numero = null) { |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->query = "SELECT nome FROM pbx_queues_grupos WHERE numero = '$numero'"; |
||||||
|
$result = $this->execute('row')[0]; |
||||||
|
return $result; |
||||||
|
} |
||||||
|
|
||||||
|
######################### HELPERS INSTALL ######################## |
||||||
|
public function getIdDirecionamento($tipo, $nome){ |
||||||
|
switch ($tipo) { |
||||||
|
case CONF_URA: |
||||||
|
return $this->getUraIdByName($nome) . "-" . $nome; |
||||||
|
break; |
||||||
|
|
||||||
|
case CONF_ANUNCIOS: |
||||||
|
return $this->getAnuncioIdByName($nome); |
||||||
|
break; |
||||||
|
|
||||||
|
case CONF_HORARIO: |
||||||
|
return $this->getHorarioIdByName($nome); |
||||||
|
break; |
||||||
|
|
||||||
|
case CONF_INTEGRACAO: |
||||||
|
return $this->getIntegracaoMetodoByNameIntegracao($nome); |
||||||
|
break; |
||||||
|
|
||||||
|
case CONF_FILAS: |
||||||
|
return $this->getFilaNumeroByName($nome); |
||||||
|
break; |
||||||
|
default: |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function getNomeDirecionamento($tipo, $id){ |
||||||
|
$id = intval($id); |
||||||
|
switch ($tipo) { |
||||||
|
case CONF_URA: |
||||||
|
return $this->getUraNameById($id); |
||||||
|
break; |
||||||
|
|
||||||
|
case CONF_ANUNCIOS: |
||||||
|
return $this->getAnuncioNameById($id); |
||||||
|
break; |
||||||
|
|
||||||
|
case CONF_HORARIO: |
||||||
|
return $this->getHorarioNameById($id); |
||||||
|
break; |
||||||
|
|
||||||
|
case CONF_INTEGRACAO: |
||||||
|
return null; |
||||||
|
break; |
||||||
|
|
||||||
|
case CONF_FILAS: |
||||||
|
return $this->getFilaNameByNumero($id); |
||||||
|
break; |
||||||
|
default: |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function redirectUraDestino($ura, $status, $nomenclatura){ |
||||||
|
$ura_destinos = $this->getUraDestinosIdByUraName($ura.$nomenclatura); |
||||||
|
foreach($ura_destinos as $destino){ |
||||||
|
if($destino['numero'] == strtoupper($status)){ |
||||||
|
return array( |
||||||
|
"TIPO" => str_replace("s","",trim($destino['tipo'])), |
||||||
|
"NOME" => $this->getNomeDirecionamento(trim($destino['tipo']), $destino['comando']) |
||||||
|
); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return array("TIPO" => "ANUNCIO", "NOME" => CONF_AUDIO_ERROR.$nomenclatura);//DEFAULT |
||||||
|
} |
||||||
|
} |
||||||
|
|
@ -0,0 +1,157 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
/** |
||||||
|
* Classe para utilizar |
||||||
|
* |
||||||
|
* @documentation: |
||||||
|
* @author Lucas Awade |
||||||
|
* @function developer |
||||||
|
* @company SimplesIP |
||||||
|
* @version 1.0.0 |
||||||
|
*/ |
||||||
|
class Logger { |
||||||
|
/* |
||||||
|
* GENERATE LOG |
||||||
|
*/ |
||||||
|
|
||||||
|
private $active; |
||||||
|
private $log; |
||||||
|
private $type; |
||||||
|
private $text; |
||||||
|
|
||||||
|
/* |
||||||
|
* CONF. FILE LOG |
||||||
|
*/ |
||||||
|
private $file; |
||||||
|
private $path; |
||||||
|
private $name; |
||||||
|
|
||||||
|
/* |
||||||
|
* CONST. MESSAGE TYPE |
||||||
|
*/ |
||||||
|
|
||||||
|
const LOG_SUCCESS = "SUCCESS"; |
||||||
|
const LOG_DEBUG = "DEBUG"; |
||||||
|
const LOG_INFO = "INFO"; |
||||||
|
const LOG_WARNING = "WARNING"; |
||||||
|
const LOG_ERROR = "ERROR"; |
||||||
|
|
||||||
|
public function __construct($nameLog, $active = false, $path = "/var/log/asterisk/") { |
||||||
|
$this->name = $nameLog; |
||||||
|
$this->path = $path; |
||||||
|
$this->active = $active; |
||||||
|
$this->config($nameLog); |
||||||
|
} |
||||||
|
|
||||||
|
######################################################################## |
||||||
|
## TYPES ## |
||||||
|
######################################################################## |
||||||
|
|
||||||
|
public function success($log, $debug_trace = null) { |
||||||
|
$this->type = self::LOG_SUCCESS; |
||||||
|
$this->text = $log; |
||||||
|
$this->header($log, $debug_trace ? $debug_trace : debug_backtrace()); |
||||||
|
$this->write(); |
||||||
|
} |
||||||
|
|
||||||
|
public function debug($log, $debug_trace = null) { |
||||||
|
$this->type = self::LOG_DEBUG; |
||||||
|
$this->text = $log; |
||||||
|
$this->header($log, $debug_trace ? $debug_trace : $this->name); |
||||||
|
$this->write(); |
||||||
|
} |
||||||
|
|
||||||
|
public function info($log, $debug_trace = null) { |
||||||
|
$this->type = self::LOG_INFO; |
||||||
|
$this->text = $log; |
||||||
|
$this->header($log, $debug_trace ? $debug_trace : $this->name); |
||||||
|
$this->write(); |
||||||
|
} |
||||||
|
|
||||||
|
public function error($log, $debug_trace = null) { |
||||||
|
$this->type = self::LOG_ERROR; |
||||||
|
$this->text = $log; |
||||||
|
$this->header($log, $debug_trace ? $debug_trace : $this->name); |
||||||
|
$this->write(); |
||||||
|
} |
||||||
|
|
||||||
|
public function warning($log, $debug_trace = null) { |
||||||
|
$this->type = self::LOG_WARNING; |
||||||
|
$this->text = $log; |
||||||
|
$this->header($log, $debug_trace ? $debug_trace : $this->name); |
||||||
|
$this->write(); |
||||||
|
} |
||||||
|
|
||||||
|
######################################################################## |
||||||
|
## IMPORTANT ## |
||||||
|
######################################################################## |
||||||
|
|
||||||
|
private function write() { |
||||||
|
if ($this->active) { |
||||||
|
file_put_contents($this->file, $this->log, FILE_APPEND); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private function header($log, $debug_trace) { |
||||||
|
$this->log = "________________________________________________________________________________________\n"; |
||||||
|
if(is_array($debug_trace)){ |
||||||
|
$method = $debug_trace[0]['class'] ? "{$debug_trace[0]['class']}::{$debug_trace[0]['function']}" : $debug_trace[0]['function']; |
||||||
|
$args = null; |
||||||
|
|
||||||
|
if(count($debug_trace[0]['args']) > 0){ |
||||||
|
foreach($debug_trace[0]['args'] as $key => $arg){ |
||||||
|
$x++; |
||||||
|
$args .= $arg; |
||||||
|
if(count($debug_trace[0]['args']) != $x){ |
||||||
|
$args .= ","; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
$this->log .= sprintf("\n[ %s ][ LINE %s ][ %s ][ ARGS ($args) ][ %s ]\n\n", date('d/m/Y H:i:s'), $debug_trace[0]['line'], $method, $this->type); |
||||||
|
} else { |
||||||
|
$this->log .= sprintf("\n[ %s ][ %s ][ %s ]\n\n", date('d/m/Y H:i:s'), $debug_trace, $this->type); |
||||||
|
} |
||||||
|
$this->log .= "> " . $log; |
||||||
|
$this->log .= "\n\n ---------------------------------- [ FINISH LOGGER ] ----------------------------------\n\n"; |
||||||
|
} |
||||||
|
|
||||||
|
public function openLog() { |
||||||
|
//$file = fopen($this->file, 'rb'); |
||||||
|
} |
||||||
|
|
||||||
|
public function locateLog() { |
||||||
|
echo "\n\n {$this->file} \n\n"; |
||||||
|
} |
||||||
|
|
||||||
|
######################################################################## |
||||||
|
## CONFIGS ## |
||||||
|
######################################################################## |
||||||
|
|
||||||
|
public function config($name, $exten = ".log", $prefix = null) { |
||||||
|
if (file_exists($this->file)) { |
||||||
|
$contents = file_get_contents($this->file); |
||||||
|
} |
||||||
|
|
||||||
|
$this->file = trim($this->path . $prefix . ($name ? $name : $this->name) . $exten); |
||||||
|
file_put_contents($this->file, $contents ? $contents : "", FILE_APPEND); |
||||||
|
} |
||||||
|
|
||||||
|
public function getType() { |
||||||
|
return $this->type; |
||||||
|
} |
||||||
|
|
||||||
|
public function getText(){ |
||||||
|
return $this->text; |
||||||
|
} |
||||||
|
|
||||||
|
public function setLogger($active) { |
||||||
|
if ($this->active === true) { |
||||||
|
$this->active = $active; |
||||||
|
} else if ($active === false) { |
||||||
|
$this->active = $active; |
||||||
|
} else { |
||||||
|
$this->active = true; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -1,260 +0,0 @@ |
|||||||
<?php |
|
||||||
|
|
||||||
require_once "Http.php"; |
|
||||||
require_once "WebHeader.php"; |
|
||||||
require_once "Positus.php"; |
|
||||||
|
|
||||||
/** |
|
||||||
* Description of Middleware Positus |
|
||||||
* |
|
||||||
* @author Lucas Awade |
|
||||||
*/ |
|
||||||
class Middleware extends Http { |
|
||||||
|
|
||||||
/** @class Positus */ |
|
||||||
private $positus; |
|
||||||
|
|
||||||
/** @var return Positus request */ |
|
||||||
private $request; |
|
||||||
|
|
||||||
/** @var agente in conversation */ |
|
||||||
private $agente; |
|
||||||
|
|
||||||
/** @class WebHeader */ |
|
||||||
private $header; |
|
||||||
|
|
||||||
/** @var Link local downloads image|document|video|sticker|audio */ |
|
||||||
private $link = "http://voip.simplesip.com.br:8080/Positus/"; |
|
||||||
|
|
||||||
public function __construct($config = null) { |
|
||||||
$this->header = new WebHeader($config); |
|
||||||
$this->positus = new Positus(); |
|
||||||
$this->hook(); |
|
||||||
$this->router(); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Start API headers |
|
||||||
* @param array $config |
|
||||||
*/ |
|
||||||
public function api() { |
|
||||||
$this->header->API(); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Instance class Positus |
|
||||||
* @return Positus |
|
||||||
*/ |
|
||||||
public function positus() { |
|
||||||
return $this->positus; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Set new link download |
|
||||||
* @param string $link |
|
||||||
*/ |
|
||||||
public function setLink($link) { |
|
||||||
$this->link = $link; |
|
||||||
$this->formatLink(); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Get link download |
|
||||||
* @return string |
|
||||||
*/ |
|
||||||
public function getLink() { |
|
||||||
$this->formatLink(); |
|
||||||
return $this->link; |
|
||||||
} |
|
||||||
|
|
||||||
private function router() { |
|
||||||
$this->baseUri(); |
|
||||||
switch (strtolower($this->param()[1])) { |
|
||||||
case 'download': |
|
||||||
$this->getLinkDownload(); |
|
||||||
return null; |
|
||||||
case 'message': |
|
||||||
$this->message(); |
|
||||||
return null; |
|
||||||
default: |
|
||||||
$this->header->response(301); |
|
||||||
$this->header->redirect(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Read input requests |
|
||||||
*/ |
|
||||||
private function hook() { |
|
||||||
$this->request = file_get_contents('php://input'); |
|
||||||
$this->positus()->log()->debug($this->request); |
|
||||||
$this->request = json_decode($this->request, true); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Create file and download in browser |
|
||||||
*/ |
|
||||||
private function getLinkDownload($link = null) { |
|
||||||
if ($link) { |
|
||||||
return $this->getLink() . "download/{$this->getId()}/{$this->getMimetype()}"; |
|
||||||
} |
|
||||||
|
|
||||||
if ($this->header && strtolower($this->param()[1]) == 'download' && $this->param()[2] && $this->param()[3]) { |
|
||||||
$file = $this->positus->baixarMidia($this->param()[2]); |
|
||||||
$this->positus()->log()->debug("Name: " . $this->param()[2] . " | file: " . $file . " | Mime: " . base64_decode($this->param()[3])); |
|
||||||
$this->header->fileTransfer($this->param()[2], $file, base64_decode($this->param()[3])); |
|
||||||
exit(0); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
######################################################################## |
|
||||||
##### POSITUS RESPONSE ##### |
|
||||||
######################################################################## |
|
||||||
|
|
||||||
/**s |
|
||||||
* Send the file to the active contact |
|
||||||
* |
|
||||||
* @return null |
|
||||||
*/ |
|
||||||
private function message() { |
|
||||||
switch ($this->getType()) { |
|
||||||
case 'text': |
|
||||||
$this->positus()->log()->debug("Message: " . $this->getMessage() . " | Type: " . $this->getType() . " | Agente: " . $this->agente); |
|
||||||
$this->positus->enviarMsg($this->agente, "*{$this->getProfile()}*: " . $this->getMessage()); |
|
||||||
return null; |
|
||||||
case 'image': |
|
||||||
$this->positus->enviaImagem($this->agente, $this->getLinkDownload(1)); |
|
||||||
return null; |
|
||||||
case 'sticker': |
|
||||||
$this->positus->enviaSticker($this->agente, $this->getLinkDownload(1)); |
|
||||||
return null; |
|
||||||
case 'video': |
|
||||||
$this->positus->enviaVideo($this->agente, $this->getLinkDownload(1)); |
|
||||||
return null; |
|
||||||
case 'voice': |
|
||||||
case 'audio': |
|
||||||
$this->positus->enviaAudio($this->agente, $this->getLinkDownload(1)); |
|
||||||
return null; |
|
||||||
case 'document': |
|
||||||
$this->positus->enviaDocumento($this->agente, $this->getLinkDownload(1)); |
|
||||||
return null; |
|
||||||
case 'contacts': |
|
||||||
$this->positus->enviarContato($this->agente, $this->getContactFormatted(), $this->getContactPhone()); |
|
||||||
return null; |
|
||||||
case 'location': |
|
||||||
$this->positus->enviarLocalizacao($this->agente, $this->getGeolocation('longitude'), $this->getGeolocation('latitude')); |
|
||||||
return null; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Profile WhatsApp |
|
||||||
* @return string |
|
||||||
*/ |
|
||||||
public function getProfile() { |
|
||||||
return $this->request['contacts'][0]['profile']['name']; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Returns the type of the message |
|
||||||
* @return string|boolean |
|
||||||
*/ |
|
||||||
public function getType() { |
|
||||||
if ($this->request) { |
|
||||||
return $this->request['messages'][0]['type']; |
|
||||||
} |
|
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Returns the mime of the message |
|
||||||
* @return string|boolean |
|
||||||
*/ |
|
||||||
public function getMimetype() { |
|
||||||
if ($this->request['messages'][0][$this->getType()]['mime_type']) { |
|
||||||
return base64_encode($this->request['messages'][0][$this->getType()]['mime_type']); |
|
||||||
} |
|
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Returns the id of the message |
|
||||||
* @return string|boolean |
|
||||||
*/ |
|
||||||
public function getId() { |
|
||||||
if ($this->request && $this->getType()) { |
|
||||||
return $this->request['messages'][0][$this->getType()]['id']; |
|
||||||
} |
|
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Returns the (text, body) of the message |
|
||||||
* @return string|boolean |
|
||||||
*/ |
|
||||||
public function getMessage() { |
|
||||||
$message = $this->request['messages'][0]['text']['body']; |
|
||||||
return ($message ? $message : false); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Returns the name of the contact |
|
||||||
* @return string|boolean |
|
||||||
*/ |
|
||||||
public function getContactFormatted() { |
|
||||||
$formatted = $this->request['messages'][0]['contacts'][0]['name']['formatted_name']; |
|
||||||
if ($formatted) { |
|
||||||
return $formatted; |
|
||||||
} |
|
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Returns the phone of the contact |
|
||||||
* @return string|boolean |
|
||||||
*/ |
|
||||||
public function getContactPhone() { |
|
||||||
$contact = $this->request['messages'][0]['contacts'][0]['phones'][0]['wa_id']; |
|
||||||
if ($contact) { |
|
||||||
return $contact; |
|
||||||
} |
|
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Returns the latitude|longitude of the location |
|
||||||
* @return string|boolean |
|
||||||
*/ |
|
||||||
public function getGeolocation($type) { |
|
||||||
$geolocation = $this->request['messages'][0]['location'][strtolower($type)]; |
|
||||||
if ($geolocation) { |
|
||||||
return $geolocation; |
|
||||||
} |
|
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
public function setAgente($agente) { |
|
||||||
$this->agente = $agente; |
|
||||||
} |
|
||||||
|
|
||||||
public function getAgente() { |
|
||||||
$this->positus->db()->buscarAgenteDisponivel(); |
|
||||||
return $this->agente; |
|
||||||
} |
|
||||||
|
|
||||||
######################################################################## |
|
||||||
##### FUNCTION UTIL ##### |
|
||||||
######################################################################## |
|
||||||
|
|
||||||
private function formatLink() { |
|
||||||
$link = str_split($this->link); |
|
||||||
|
|
||||||
if (strpos($this->link, 'http://') === false && strpos($this->link, 'http://') === false) { |
|
||||||
$this->link = "http://" . $this->link; |
|
||||||
} |
|
||||||
|
|
||||||
if (end($link) != "/") { |
|
||||||
$this->link .= "/"; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,333 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
//include 'config.php'; |
||||||
|
include 'RequestURL.php'; |
||||||
|
include 'Logger.php'; |
||||||
|
|
||||||
|
class Positus |
||||||
|
{ |
||||||
|
|
||||||
|
private $token; |
||||||
|
private $url; |
||||||
|
private $metodo; |
||||||
|
|
||||||
|
######################################################################## |
||||||
|
## VARIAVEIS DA CLASSE ## |
||||||
|
######################################################################## |
||||||
|
private $query; |
||||||
|
private $requestType; |
||||||
|
private $request; |
||||||
|
private $params = array(); |
||||||
|
|
||||||
|
######################################################################## |
||||||
|
## RECURSOS DA API ## |
||||||
|
######################################################################## |
||||||
|
|
||||||
|
function enviarMsg($whatsapp, $mensagem) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if ($this->getArgs(func_get_args())) { |
||||||
|
$this->params = array( |
||||||
|
"to" => "+$whatsapp", |
||||||
|
"type" => "text", |
||||||
|
"text" => array("body" => "$mensagem") |
||||||
|
); |
||||||
|
$this->requestType("POST"); |
||||||
|
$this->setMetodo('messages'); |
||||||
|
return $this->exec(); |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
function enviarContato($whatsapp, $nome, $contato) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if ($this->getArgs(func_get_args())) { |
||||||
|
$this->params = array( |
||||||
|
"to" => "+$whatsapp", |
||||||
|
"type" => "contacts", |
||||||
|
"contacts" => array( |
||||||
|
array( |
||||||
|
"name" => array( |
||||||
|
"first_name" => "$nome", |
||||||
|
"formatted_name" => "$nome" |
||||||
|
), |
||||||
|
"phones" => array( |
||||||
|
array( |
||||||
|
"phone" => "+$contato", |
||||||
|
"type" => "CELL", |
||||||
|
"wa_id" => "$contato" |
||||||
|
) |
||||||
|
) |
||||||
|
) |
||||||
|
) |
||||||
|
); |
||||||
|
$this->requestType("POST"); |
||||||
|
$this->setMetodo('messages'); |
||||||
|
return $this->exec(); |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
function enviarLocalizacao($whatsapp, $longitude, $latitude, $nome = null, $endereco = null) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if ($this->getArgs(func_get_args())) { |
||||||
|
$this->params = array( |
||||||
|
"to" => "+$whatsapp", |
||||||
|
"type" => "location", |
||||||
|
"location" => array( |
||||||
|
"longitude" => "$longitude", |
||||||
|
"latitude" => "$latitude", |
||||||
|
"name" => "$nome", |
||||||
|
"address" => utf8_encode("$endereco") |
||||||
|
) |
||||||
|
); |
||||||
|
$this->requestType("POST"); |
||||||
|
$this->setMetodo('messages'); |
||||||
|
return $this->exec(); |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
function enviaDocumento($whatsapp, $link, $titulo) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if ($this->getArgs(func_get_args())) { |
||||||
|
$this->params = array( |
||||||
|
"to" => "+$whatsapp", |
||||||
|
"type" => "document", |
||||||
|
"document" => array("link" => "$link", "caption" => "$titulo") |
||||||
|
); |
||||||
|
$this->requestType("POST"); |
||||||
|
$this->setMetodo('messages'); |
||||||
|
return $this->exec(); |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
function enviaImagem($whatsapp, $link, $titulo = null) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if ($this->getArgs(func_get_args())) { |
||||||
|
$this->params = array( |
||||||
|
"to" => "+$whatsapp", |
||||||
|
"type" => "image", |
||||||
|
"image" => array("link" => "$link", "caption" => "$titulo") |
||||||
|
); |
||||||
|
$this->requestType("POST"); |
||||||
|
$this->setMetodo('messages'); |
||||||
|
return $this->exec(); |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
function enviaSticker($whatsapp, $link) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if ($this->getArgs(func_get_args())) { |
||||||
|
$this->params = array( |
||||||
|
"to" => "+$whatsapp", |
||||||
|
"type" => "sticker", |
||||||
|
"sticker" => array("link" => "$link") |
||||||
|
); |
||||||
|
$this->requestType("POST"); |
||||||
|
$this->setMetodo('messages'); |
||||||
|
return $this->exec(); |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
function enviaVideo($whatsapp, $link, $titulo = null) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if ($this->getArgs(func_get_args())) { |
||||||
|
$this->params = array( |
||||||
|
"to" => "+$whatsapp", |
||||||
|
"type" => "video", |
||||||
|
"video" => array("link" => "$link", "caption" => "$titulo") |
||||||
|
); |
||||||
|
$this->requestType("POST"); |
||||||
|
$this->setMetodo('messages'); |
||||||
|
return $this->exec(); |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
function enviaAudio($whatsapp, $link) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if ($this->getArgs(func_get_args())) { |
||||||
|
$this->params = array( |
||||||
|
"to" => "+$whatsapp", |
||||||
|
"type" => "audio", |
||||||
|
"audio" => array("link" => "$link") |
||||||
|
); |
||||||
|
$this->requestType("POST"); |
||||||
|
$this->setMetodo('messages'); |
||||||
|
return $this->exec(); |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
function baixarMidia($name) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if ($this->getArgs(func_get_args())) { |
||||||
|
$this->requestType("GET"); |
||||||
|
$this->setMetodo('media/' . $name); |
||||||
|
$file = $this->storage . $name; |
||||||
|
file_put_contents($file, $this->exec()); |
||||||
|
if (file_exists($file)) { |
||||||
|
return $file; |
||||||
|
} |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
function enviarHSM($numero, $namespace, $nameTemplate, $parametrosHeader = [], $parametrosBody = []) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->params = array( |
||||||
|
'to' => "+$numero", |
||||||
|
'type' => 'template', |
||||||
|
'template' => |
||||||
|
array( |
||||||
|
'namespace' => $namespace, |
||||||
|
'language' => |
||||||
|
array( |
||||||
|
'policy' => 'deterministic', |
||||||
|
'code' => 'pt_BR', |
||||||
|
), |
||||||
|
'name' => $nameTemplate, |
||||||
|
'components' => |
||||||
|
array( |
||||||
|
0 => |
||||||
|
array( |
||||||
|
'type' => 'header', |
||||||
|
'parameters' => $parametrosHeader, |
||||||
|
), |
||||||
|
1 => |
||||||
|
array( |
||||||
|
'type' => 'body', |
||||||
|
'parameters' => $parametrosBody, |
||||||
|
), |
||||||
|
), |
||||||
|
), |
||||||
|
); |
||||||
|
$this->requestType("POST"); |
||||||
|
$this->setMetodo('messages'); |
||||||
|
return $this->exec(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
######################################################################## |
||||||
|
## FUNCOES DO SISTEMA ## |
||||||
|
######################################################################## |
||||||
|
|
||||||
|
function __construct() |
||||||
|
{ |
||||||
|
$this->token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiMjZmNWQ5OGZhOGMzMWEwMzc1Yzg2NzlhOWEwZGUxZGEzNzYzMDRiNzY0NDYxY2Q0YjBiOWZjOWUxZmM3ZDc4N2ZmNmYzMTZmYTc2OGQ3OGYiLCJpYXQiOjE2NTYzNTcxODQuNTU2MjI3LCJuYmYiOjE2NTYzNTcxODQuNTU2MjMsImV4cCI6MTY4Nzg5MzE4NC41NTQzNDksInN1YiI6IjQxMTgiLCJzY29wZXMiOltdfQ.S3D4kVgJprt3Ez_GzWdTgWf8zlm6dy9C4Fvcb_eHzZnRfyIffCQLcLsH2RqZ8oJOdNk2vB58SRBZ-iIg7wa2diIObbEi0UWtDEUPa1Z7kw3rD8z3fvxCy8MF4dOmGarQKxA3qm2rdu-zYOcKMPKS6YY2aCx6iy8jEp_7hEdS1sUhU8LvZ2TzfZ7EOJ3VaauF1qxQa6cBSxLR9zIdce9u6SQmRhQQlYivUxdwWP2KL3lFl6LjQoIL6yvZX63X1xaq8btjaWK1TkxfRlL7JdCrQH5daQD5K5eI6bJxl4-4EqpJMpXTctaye5jBjEvcyPxBcYCY-N4LAjgMUGkz88hNKb-7lWUC5-GryB3DxvV9VGbTR7T4bHEwuvo_YlMK1Wo06hOMq2T60-7S4fJr2CHYNaR-PLRC7LOTFfPqGt_8ssIcXFbdRnNGyhcgoxC_oFbPjY9oaFEYaUx3EXgUAYNHNFSj7-k2IRdeW9nnV2t2AX_uDFAmgSq6wBSE1O1MoXPAVULMR5pSonY199ylSkYVYwEqURuRdgNPKi_TnMl2JOllBWcU0ivp4fNCqTH1tAEJ1Rcz24MyVErWiwmmZRoaV3zg_N4hcp5LiPPB0NQY3SveFTxu1dtek3tF90jxCHweI2gnug7aVJHRyHgxzXgM5c9fIXDEYowIovhE8A-gTk0"; |
||||||
|
$this->url = "https://api.positus.global/v2/whatsapp/numbers/4661066a-43a3-4ccd-9bb8-eafdd04f85e8/"; |
||||||
|
$this->request = new RequestURL(); |
||||||
|
} |
||||||
|
|
||||||
|
private function setMetodo($metodo) |
||||||
|
{ |
||||||
|
$this->metodo = $metodo; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Escreve a query para ser passada para o curl |
||||||
|
* |
||||||
|
* @param string $query |
||||||
|
*/ |
||||||
|
private function setQuery($query) |
||||||
|
{ |
||||||
|
return $this->query .= $query; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* retorna a string pronta da query do curl e limpa a variavel. |
||||||
|
* |
||||||
|
* @return string $query |
||||||
|
*/ |
||||||
|
private function getQuery() |
||||||
|
{ |
||||||
|
$query = $this->query; |
||||||
|
$this->log()->debug('QUERY:' . $query); |
||||||
|
unset($this->query); |
||||||
|
return $query; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Verifica se todos os parametros passados foram completados. |
||||||
|
* |
||||||
|
* @param array $args |
||||||
|
* @return true|false |
||||||
|
*/ |
||||||
|
private function getArgs($args) |
||||||
|
{ |
||||||
|
foreach ($args as $value) { |
||||||
|
if (!$value) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Recebe o tipo de Requisição GET/POST |
||||||
|
* |
||||||
|
* @return boolean |
||||||
|
*/ |
||||||
|
private function requestType($req = null) |
||||||
|
{ |
||||||
|
if (!$req) { |
||||||
|
return $this->requestType; |
||||||
|
} |
||||||
|
|
||||||
|
if (strtoupper($req) == "GET") { |
||||||
|
return $this->requestType = "GET"; |
||||||
|
} else if (strtoupper($req) == "POST") { |
||||||
|
return $this->requestType = "POST"; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private function exec() |
||||||
|
{ |
||||||
|
$this->setQuery(json_encode($this->params)); //SET QUERY |
||||||
|
$this->request->setUrl($this->url . $this->metodo); |
||||||
|
|
||||||
|
$this->log()->debug('URL QUERY:' . $this->url . $this->metodo); |
||||||
|
|
||||||
|
$header = array(); |
||||||
|
$header[] = "Authorization: Bearer {$this->token}"; |
||||||
|
if ($this->requestType == 'POST') { |
||||||
|
$header[] = 'Content-Type: application/json'; |
||||||
|
$this->request->post_field($this->getQuery(), true); |
||||||
|
} |
||||||
|
$this->request->header($header); |
||||||
|
$this->request->method_request($this->requestType); |
||||||
|
|
||||||
|
$response = $this->request->exec_request(); |
||||||
|
return $this->response($response); |
||||||
|
} |
||||||
|
|
||||||
|
private function response($result) |
||||||
|
{ |
||||||
|
if ($result) { |
||||||
|
if (json_decode($result, true) !== null) { |
||||||
|
return json_decode($result, true); |
||||||
|
} |
||||||
|
return $result; |
||||||
|
} else { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private function log() |
||||||
|
{ |
||||||
|
return new Logger('integracao'); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,101 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
class RequestURL |
||||||
|
{ |
||||||
|
|
||||||
|
private $method; |
||||||
|
private $curl; |
||||||
|
private $ssl; |
||||||
|
private $post_field; |
||||||
|
private $header; |
||||||
|
|
||||||
|
const CONF_TIMEOUT = 30; |
||||||
|
|
||||||
|
public function setUrl($url, $ssl = true) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->curl = curl_init($url); |
||||||
|
$this->ssl = is_bool($ssl) ? $ssl : true; |
||||||
|
} |
||||||
|
|
||||||
|
public function header($header) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if (is_array($header)) { |
||||||
|
$this->header = $header; |
||||||
|
} else { |
||||||
|
$this->header = array($header); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function post_field($data, $json = false) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if ($json) { |
||||||
|
$this->post_field = $data; |
||||||
|
} else { |
||||||
|
$this->post_field = http_build_query($data); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function method_request($method = 'GET') |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->method = strtoupper($method); |
||||||
|
} |
||||||
|
|
||||||
|
public function exec_request() |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->conf_request(); |
||||||
|
|
||||||
|
if (curl_error($this->curl)) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
$this->response = curl_exec($this->curl); |
||||||
|
curl_close($this->curl); |
||||||
|
return $this->response($this->response); |
||||||
|
} |
||||||
|
|
||||||
|
public function convert_xml_to_object() |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if (!$this->response) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
return simplexml_load_string($this->response); |
||||||
|
} |
||||||
|
|
||||||
|
private function response($result) |
||||||
|
{ |
||||||
|
// logger('deburguer')->info(print_r($result, true)); |
||||||
|
if ($result) { |
||||||
|
if (json_decode($result, true) !== null) { |
||||||
|
return json_decode($result, true); |
||||||
|
} |
||||||
|
return $result; |
||||||
|
} else { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
############################################################################ |
||||||
|
### CONFIG. CURL ### |
||||||
|
############################################################################ |
||||||
|
|
||||||
|
private function conf_request() |
||||||
|
{ |
||||||
|
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true); |
||||||
|
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->header); |
||||||
|
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $this->method); |
||||||
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); |
||||||
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, $this->ssl); |
||||||
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, $this->ssl); |
||||||
|
|
||||||
|
if ($this->post_field) { |
||||||
|
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->post_field); |
||||||
|
} |
||||||
|
|
||||||
|
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, self::CONF_TIMEOUT); |
||||||
|
curl_setopt($this->curl, CURLOPT_TIMEOUT, self::CONF_TIMEOUT); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,258 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
/** |
||||||
|
* Classe para implementacao envio de Emails em Massa ou único! |
||||||
|
* |
||||||
|
* @author Lucas Awade |
||||||
|
* @Desenvolvedor |
||||||
|
* @version 1.1.0 |
||||||
|
*/ |
||||||
|
require("phpmailer/class.phpmailer.php"); |
||||||
|
|
||||||
|
class SimpleMail { |
||||||
|
######################################################################## |
||||||
|
## CONFIGURACOES CLASSE ## |
||||||
|
######################################################################## |
||||||
|
|
||||||
|
private $log; |
||||||
|
private $limit; |
||||||
|
private $sended = 0; |
||||||
|
private $mailer; |
||||||
|
|
||||||
|
######################################################################## |
||||||
|
## CONFIGURACOES MAIL ## |
||||||
|
######################################################################## |
||||||
|
private $host; |
||||||
|
private $port; |
||||||
|
private $smtpAuth; |
||||||
|
private $smtpSecure; |
||||||
|
private $charset; |
||||||
|
private $mail; |
||||||
|
private $passwd; |
||||||
|
private $title; |
||||||
|
private $subject; |
||||||
|
|
||||||
|
######################################################################## |
||||||
|
## CONFIGURACOES DEFAULT ## |
||||||
|
######################################################################## |
||||||
|
|
||||||
|
public function __construct($mail, $passwd, $subject, $logger = false) { |
||||||
|
$this->log = new Logger('simplesmailer', $logger, "/var/log/"); |
||||||
|
if ($mail && $passwd && $subject) { |
||||||
|
$this->mail = $mail; |
||||||
|
$this->passwd = $passwd; |
||||||
|
$this->subject = $subject; |
||||||
|
$this->phpMailer(); |
||||||
|
} else { |
||||||
|
$this->log->error('Nao foi possivel instanciar a Classe SimplesMail,informacoes incompletas!', debug_backtrace()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Metodo de configuração do Host de Envio; |
||||||
|
* @param string $host |
||||||
|
* @param string|int $port |
||||||
|
* @param string $title |
||||||
|
* @param boolean $smtpAuth |
||||||
|
* @param string $smtpSecure |
||||||
|
* @param string $charset |
||||||
|
*/ |
||||||
|
public function config($host, $port, $title, $smtpAuth = true, $smtpSecure = 'TLS', $charset = 'UTF8') { |
||||||
|
$this->host = $host; |
||||||
|
$this->port = $port; |
||||||
|
$this->title = $title; |
||||||
|
$this->smtpAuth = $smtpAuth; |
||||||
|
$this->smtpSecure = $smtpSecure; |
||||||
|
$this->charset = $charset; |
||||||
|
$this->phpMailer(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Informa um limite de email em massa a ser enviado. |
||||||
|
* |
||||||
|
* @param int $limit |
||||||
|
*/ |
||||||
|
public function limitSend($limit) { |
||||||
|
$this->log->debug('Limite de Email: ' . $limit, debug_backtrace()); |
||||||
|
$this->limit = $limit; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Mensagem escrito em HTML para envio de um email elaborado. |
||||||
|
* |
||||||
|
* Os dados em $data devem ser em formato array como o exemplo: |
||||||
|
* array("MSG_HEADER" => 'Seja Bem vindo', "MSG_FOOTER" => 'Volte Sempre'); |
||||||
|
* |
||||||
|
* @param string $path |
||||||
|
* @param array $data |
||||||
|
*/ |
||||||
|
public function pathBodyMail($path, $data) { |
||||||
|
$this->log->info('Path HTML Body: ' . $path, debug_backtrace()); |
||||||
|
$contents = file_get_contents($path); |
||||||
|
foreach ($data as $key => $value) { |
||||||
|
$contents = str_replace($key, $value, $contents); |
||||||
|
} |
||||||
|
$this->mailer->Body = $contents; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Mensagem simples para o envio rapido de um email. |
||||||
|
* |
||||||
|
* @param string $message |
||||||
|
*/ |
||||||
|
public function bodyMessage($message) { |
||||||
|
$this->mailer->Body = $message; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Adiciona no email anexos para serem enviados |
||||||
|
* @param array|string $files |
||||||
|
*/ |
||||||
|
public function setAddAttachment($files) { |
||||||
|
if ($files) { |
||||||
|
if (is_array($files)) { |
||||||
|
foreach ($files as $name => $file) { |
||||||
|
$this->mailer->AddAttachment($file, (is_numeric($name) ? '' : $name)); |
||||||
|
} |
||||||
|
} else { |
||||||
|
$this->mailer->AddAttachment($files); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Adiciona no email imagens para visualizadas. |
||||||
|
* |
||||||
|
* #=> Caso o paramtro for um array seu corpo deve ser enviado da seguinte forma: |
||||||
|
* $cid = array('minha_img_jpg' => 'img/path/mail/header.jpg'); |
||||||
|
* |
||||||
|
* #=> Caso for uma string deve se passar o cid caso contrario gera um cid randomico; |
||||||
|
* |
||||||
|
* @param array|string $images |
||||||
|
*/ |
||||||
|
public function setAddEmbeddedImage($images, $cid = null) { |
||||||
|
if ($images) { |
||||||
|
if (is_array($images)) { |
||||||
|
foreach ($images as $name => $image) { |
||||||
|
$this->mailer->AddEmbeddedImage($image, $name); |
||||||
|
} |
||||||
|
} else { |
||||||
|
$cid = $cid ? $cid : rand(100, 999999); |
||||||
|
$this->mailer->AddEmbeddedImage($image, $cid); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Esta funcao envia os email em massa ou unicos. |
||||||
|
* |
||||||
|
* Email em massa é necessario passar um array com os emails seu indice que representa o email. |
||||||
|
* |
||||||
|
* Email unico so é preciso passar o email na variavel $data; |
||||||
|
* |
||||||
|
* @param array|string $data |
||||||
|
* @param string $indice |
||||||
|
* @return array|boolean |
||||||
|
*/ |
||||||
|
public function mailing($data, $indice = '') { |
||||||
|
if (is_array($data)) { |
||||||
|
$invalid = array(); |
||||||
|
foreach ($data as $mail) { |
||||||
|
if ($this->sended <= $this->limit) { |
||||||
|
$mail = $indice ? $mail[$indice] : $mail; |
||||||
|
$this->setSended(); |
||||||
|
if (!$this->send($mail)) { |
||||||
|
$invalid[] = $mail; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return $invalid; |
||||||
|
} else { |
||||||
|
if ($this->send($data)) { |
||||||
|
$this->setSended(); |
||||||
|
return true; |
||||||
|
} else { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function getErrorSend(){ |
||||||
|
return $this->mailer->ErrorInfo; |
||||||
|
} |
||||||
|
|
||||||
|
######################################################################## |
||||||
|
## ENVIO MAIL ## |
||||||
|
######################################################################## |
||||||
|
|
||||||
|
/** |
||||||
|
* Gerencia a quantidade de email que esta sendo enviado. |
||||||
|
*/ |
||||||
|
private function setSended() { |
||||||
|
$this->sended++; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Gerencia o envio do email para estar sempre no limite de envio. |
||||||
|
* |
||||||
|
* @return boolean |
||||||
|
*/ |
||||||
|
public function getSended() { |
||||||
|
$this->log->debug("Sended: " . $this->sended, debug_backtrace()); |
||||||
|
if ($this->sended < $this->limit) { |
||||||
|
return true; |
||||||
|
} else { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Metodo responsavel para realizar o envio do email. |
||||||
|
* |
||||||
|
* @param string $mailFrom |
||||||
|
* @return boolean |
||||||
|
*/ |
||||||
|
private function send($mailFrom) { |
||||||
|
if (!$this->mailer) { |
||||||
|
$this->log->error("Objeto nao criado!", debug_backtrace()); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
$this->mailer->setFrom($this->mail, $this->title); |
||||||
|
$this->mailer->addAddress($mailFrom, $this->title); |
||||||
|
$this->mailer->send(); |
||||||
|
$this->mailer->clearAllRecipients(); |
||||||
|
$this->log->success("Mail: {$mailFrom} | Send Queue: {$this->sended}", debug_backtrace()); |
||||||
|
return true; |
||||||
|
} catch (Exception $ex) { |
||||||
|
if ($this->mailer->ErrorInfo) { |
||||||
|
$this->log->error("ErrorInfo Mail: " . $this->mailer->ErrorInfo . " | Exception Mail: {$mailFrom} >> " . $ex->getMessage(), debug_backtrace()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Cria as configurações e parametros para ser implementado e enviado os emails. |
||||||
|
* |
||||||
|
* @void null |
||||||
|
*/ |
||||||
|
private function phpMailer() { |
||||||
|
$this->log->debug("Instanciando objeto PHPMailer", debug_backtrace()); |
||||||
|
$this->mailer = new PHPMailer(true); |
||||||
|
$this->mailer->isSMTP(); |
||||||
|
$this->mailer->Host = $this->host; |
||||||
|
$this->mailer->SMTPAuth = $this->smtpAuth; |
||||||
|
$this->mailer->isHTML(true); |
||||||
|
|
||||||
|
if ($this->smtpAuth) { |
||||||
|
$this->mailer->SMTPSecure = $this->smtpSecure; |
||||||
|
$this->mailer->Username = $this->mail; |
||||||
|
$this->mailer->Password = $this->passwd; |
||||||
|
} |
||||||
|
|
||||||
|
$this->mailer->CharSet = $this->charset; |
||||||
|
$this->mailer->Port = $this->port; |
||||||
|
$this->mailer->Subject = $this->subject; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,82 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
########################################################################### |
||||||
|
##### ##### |
||||||
|
##### ARQUIVO PARA A CONFIGURACAO DE ACESSO API ##### |
||||||
|
##### ----------------------------------------- ##### |
||||||
|
##### CREDENCIAIS DE ACESSO ##### |
||||||
|
########################################################################### |
||||||
|
|
||||||
|
define('CONF_ID_CREDENCIAIS', ''); |
||||||
|
define('CONF_TOKEN_API', 'KZojmoAm6YEArEYMBNO7s9dWYcp7tuz9BWnxrmAX'); |
||||||
|
define('CONF_URL_API', 'https://api.redesmartnet.hubsoft.com.br/'); |
||||||
|
define('CONF_USER_API', 'api@simplesip.com.br'); |
||||||
|
define('CONF_PASSWORD_API', '^x7j#ivjzrC9&Fuaqu'); |
||||||
|
define('CONF_USERID_API', '5'); |
||||||
|
|
||||||
|
########################################################################### |
||||||
|
##### CONFIGURACOES ##### |
||||||
|
########################################################################### |
||||||
|
|
||||||
|
define("CONF_INTEGRACAO_TELA", true); |
||||||
|
define("CONF_PARAMETROS_TELA", serialize(array("NOME", "PLANO", "ENDERECO", "IP", "STATUS", "OBSERVACOES"))); |
||||||
|
|
||||||
|
/** @CONF_NOME_EMPRESA => colocar _EMPRESA */ |
||||||
|
define('CONF_NOME_EMPRESA', ''); |
||||||
|
|
||||||
|
/** @CONF_AUDIO_ERROR => ID Áudio */ |
||||||
|
define('CONF_AUDIO_ERROR', ''); |
||||||
|
|
||||||
|
define('CONF_LOGGER_PATH', 'integracao'); |
||||||
|
define('CONF_LOGGER_ATIVO', true); |
||||||
|
define('CONF_LOGGER_DB_ATIVO', false); |
||||||
|
|
||||||
|
define('CONF_FATURA_DIAS_ANTES', 30); |
||||||
|
define('CONF_FATURA_DIAS_APOS', 5); |
||||||
|
|
||||||
|
define('CONF_AUDIO_ERROR', 'INTEGRACAO_ERRO_INT'); |
||||||
|
|
||||||
|
########################################################################### |
||||||
|
##### CREDENCIAIS DE ENVIO DE EMAIL ##### |
||||||
|
########################################################################### |
||||||
|
|
||||||
|
define('CONF_EMAIL_TITLE_INVOICE', '[NO-REPLY] Envio de Fatura'); |
||||||
|
define('CONF_EMAIL_SUBTITLE_INVOICE', 'Estamos enviando o pedido de Segunda Via da sua Fatura!'); |
||||||
|
define('CONF_SENDER_EMAIL_USER', ''); |
||||||
|
define('CONF_SENDER_EMAIL_PASSWORD', ''); |
||||||
|
define('CONF_SMTP_HOST', ''); |
||||||
|
define('CONF_SMTP_POST', '587'); |
||||||
|
|
||||||
|
|
||||||
|
############################################################################ |
||||||
|
##### CONF. KING CAMPANHA ##### |
||||||
|
############################################################################ |
||||||
|
|
||||||
|
define('CONF_CAMP_LOGIN', "novahelp"); |
||||||
|
define('CONF_CAMP_DATAINI', date('Y-m-d 08:00:00')); |
||||||
|
define('CONF_CAMP_DATAFIM', date('Y-m-d 18:00:00')); |
||||||
|
define('CONF_CAMP_RAMAL', "2000"); |
||||||
|
define('CONF_CAMP_FLUXO', 0); |
||||||
|
define('CONF_CAMP_PAUSA', 1); |
||||||
|
define('CONF_CAMP_NUMREDISCAGEM', 1); |
||||||
|
define('CONF_CAMP_MAXCALL', 10); |
||||||
|
define('CONF_CAMP_RINGTIME', 45); |
||||||
|
define('CONF_CAMP_INTERVALO', 1); |
||||||
|
define('CONF_CAMP_AMD', 0); |
||||||
|
define('CONF_CAMP_NDS', 0); |
||||||
|
define('CONF_CAMP_PRIOMOVEL', 0); |
||||||
|
define('CONF_CAMP_MAXCALLAGENT', 3); |
||||||
|
|
||||||
|
|
||||||
|
############################################################################ |
||||||
|
##### CONF. INTEGRACAO DE TELA ##### |
||||||
|
############################################################################ |
||||||
|
|
||||||
|
define("CONF_INTEGRACAO_TELA", true); |
||||||
|
define("CONF_PARAMETROS_TELA", serialize(array("DOCUMENTO", "NOME", "PLANO", "ENDERECO", "STATUS", "EMAIL"))); |
||||||
|
|
||||||
|
########################################################################### |
||||||
|
##### CONFIGURACAO DE PARADA TECNICA HUBSOFT ##### |
||||||
|
########################################################################### |
||||||
|
|
||||||
|
define('CONF_PARADA_TECNICA_MSG', "PROBLEMA MASSIVO"); |
@ -0,0 +1,33 @@ |
|||||||
|
<meta charset="UTF-8"> |
||||||
|
<table align="center" width="60%" style="border: 1px solid #ddd; background-color: #fff;border-radius: 8px;"> |
||||||
|
<tr> |
||||||
|
<td> |
||||||
|
<br /> |
||||||
|
<h2 style="text-align: center;">$titulo</h2> |
||||||
|
<hr style="border: 0.05px solid rgb(241, 241, 241);" /> |
||||||
|
</td> |
||||||
|
|
||||||
|
</tr> |
||||||
|
<tr> |
||||||
|
<td> |
||||||
|
<div style="margin: auto;margin-left: 20px"> |
||||||
|
<p>Olá $nome, estamos enviando a segunda via de sua fatura, |
||||||
|
para mais informação entre em contato com nosso atendimento. |
||||||
|
</p> |
||||||
|
<p><b>Para visualizar seu Boleto clique em visualizar para abrir ou verifique nos anexos!</b></p> |
||||||
|
<p>$link</p> |
||||||
|
</div> |
||||||
|
</td> |
||||||
|
</tr> |
||||||
|
<tr> |
||||||
|
<td> |
||||||
|
<br/> |
||||||
|
<hr style="border: 0.05px solid rgb(241, 241, 241);" /> |
||||||
|
<div style="width: 80%; margin: auto;"> |
||||||
|
<p style="text-align: center;"><b>Este é uma mensagem gerada automaticamente, pedimos que não responda essa mensagem!</b></p> |
||||||
|
<p style="text-align: center;"><i><b>A agradeçemos o seu contato!</b></i></p> |
||||||
|
</div> |
||||||
|
<br/> |
||||||
|
</td> |
||||||
|
</tr> |
||||||
|
</table> |
@ -0,0 +1,8 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
include "Positus.php"; |
||||||
|
|
||||||
|
$positus = new Positus(); |
||||||
|
$positus->enviarHSM('556581282842', 'bd4a396f_11b1_4986_8efd_6442eaf2e52a', 'apresentacao_com_botao'); |
||||||
|
|
||||||
|
?> |
@ -0,0 +1,378 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace app\Providers; |
||||||
|
|
||||||
|
use app\Interfaces\IApiMedia; |
||||||
|
|
||||||
|
class Positus extends Requests implements IApiMedia |
||||||
|
{ |
||||||
|
/** @var string $hook resposta do webhook */ |
||||||
|
private $hook; |
||||||
|
|
||||||
|
function getContentType() |
||||||
|
{ |
||||||
|
return "application/json; charset=UTF-8"; |
||||||
|
} |
||||||
|
|
||||||
|
public function getchannel() |
||||||
|
{ |
||||||
|
return CONF_WHATSAPP_CHANNEL; |
||||||
|
} |
||||||
|
|
||||||
|
function setHook($hook) |
||||||
|
{ |
||||||
|
$this->hook = json_decode($hook, true); |
||||||
|
} |
||||||
|
|
||||||
|
function enviarMedia($whatsapp, $link, $type, $titulo = null) |
||||||
|
{ |
||||||
|
$tipos = []; |
||||||
|
$tipos['link'] = $link; |
||||||
|
if (!empty($titulo)) { |
||||||
|
$tipos['caption'] = $titulo; |
||||||
|
} |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->params = [ |
||||||
|
"to" => "+$whatsapp", |
||||||
|
"type" => "$type", |
||||||
|
"$type" => $tipos |
||||||
|
]; |
||||||
|
$this->requestType("POST"); |
||||||
|
$this->setMetodo('messages'); |
||||||
|
return $this->exec(); |
||||||
|
} |
||||||
|
|
||||||
|
function enviarMsg($whatsapp, $mensagem, $encode = true) |
||||||
|
{ |
||||||
|
if ($encode) { |
||||||
|
$mensagem = utf8_encode($mensagem); |
||||||
|
} |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->params = array( |
||||||
|
"to" => "+$whatsapp", |
||||||
|
"type" => "text", |
||||||
|
"text" => array("body" => "$mensagem") |
||||||
|
); |
||||||
|
$this->requestType("POST"); |
||||||
|
$this->setMetodo('messages'); |
||||||
|
return $this->exec(); |
||||||
|
} |
||||||
|
|
||||||
|
function enviarHSM($numero, $namespace, $nameTemplate, $parametrosHeader = [], $parametrosBody = []) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
$this->params = array( |
||||||
|
'to' => "+$numero", |
||||||
|
'type' => 'template', |
||||||
|
'template' => |
||||||
|
array( |
||||||
|
'namespace' => $namespace, |
||||||
|
'language' => |
||||||
|
array( |
||||||
|
'policy' => 'deterministic', |
||||||
|
'code' => 'pt_BR', |
||||||
|
), |
||||||
|
'name' => $nameTemplate, |
||||||
|
'components' => |
||||||
|
array( |
||||||
|
0 => |
||||||
|
array( |
||||||
|
'type' => 'header', |
||||||
|
'parameters' => $parametrosHeader, |
||||||
|
), |
||||||
|
1 => |
||||||
|
array( |
||||||
|
'type' => 'body', |
||||||
|
'parameters' => $parametrosBody, |
||||||
|
), |
||||||
|
), |
||||||
|
), |
||||||
|
); |
||||||
|
$this->requestType("POST"); |
||||||
|
$this->setMetodo('messages'); |
||||||
|
return $this->exec(); |
||||||
|
} |
||||||
|
|
||||||
|
function enviarMsgIterativaLista($whatsapp, $mensagem, $nomeButton, $lista, $prex = '') |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if ($this->getArgs(func_get_args())) { |
||||||
|
$this->params = array( |
||||||
|
"to" => "+$whatsapp", |
||||||
|
"type" => "interactive", |
||||||
|
"interactive" => array( |
||||||
|
"type" => "list", |
||||||
|
"body" => array( |
||||||
|
"text" => utf8_encode($mensagem) |
||||||
|
), |
||||||
|
"action" => array( |
||||||
|
"button" => utf8_encode($nomeButton), |
||||||
|
"sections" => $this->montaSection($lista, $prex) |
||||||
|
) |
||||||
|
) |
||||||
|
); |
||||||
|
$this->requestType("POST"); |
||||||
|
$this->setMetodo('messages'); |
||||||
|
$ret = $this->exec(); |
||||||
|
return $ret; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
function montaSection($lista, $prex = '') |
||||||
|
{ |
||||||
|
$sections = []; |
||||||
|
for ($i = 0; $i < count($lista); $i++) { |
||||||
|
array_push( |
||||||
|
$sections, |
||||||
|
array( |
||||||
|
"id" => "$prex$i", |
||||||
|
"title" => utf8_encode("{$lista[$i]['title']}"), |
||||||
|
"description" => utf8_encode("{$lista[$i]['sub']}") |
||||||
|
) |
||||||
|
); |
||||||
|
} |
||||||
|
return array( |
||||||
|
array( |
||||||
|
"title" => utf8_encode("Comandos"), |
||||||
|
"rows" => $sections |
||||||
|
), |
||||||
|
|
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
function enviarMsgIterativaBotao($whatsapp, $mensagem, $buttons) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if ($this->getArgs(func_get_args())) { |
||||||
|
$this->params = array( |
||||||
|
"to" => "+$whatsapp", |
||||||
|
"type" => "interactive", |
||||||
|
"recipient_type" => "individual", |
||||||
|
"interactive" => array( |
||||||
|
"type" => "button", |
||||||
|
"body" => array( |
||||||
|
"text" => utf8_encode($mensagem) |
||||||
|
), |
||||||
|
"action" => array( |
||||||
|
"buttons" => $buttons |
||||||
|
) |
||||||
|
) |
||||||
|
); |
||||||
|
$this->requestType("POST"); |
||||||
|
$this->setMetodo('messages'); |
||||||
|
$ret = $this->exec(); |
||||||
|
return $ret; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
function enviarContato($whatsapp, $nome, $contato) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if ($whatsapp) { |
||||||
|
$this->params = array( |
||||||
|
"to" => "+$whatsapp", |
||||||
|
"type" => "contacts", |
||||||
|
"contacts" => array( |
||||||
|
array( |
||||||
|
"name" => array( |
||||||
|
"first_name" => "$nome", |
||||||
|
"formatted_name" => "$nome" |
||||||
|
), |
||||||
|
"phones" => array( |
||||||
|
array( |
||||||
|
"phone" => "+$contato", |
||||||
|
"type" => "CELL", |
||||||
|
"wa_id" => "$contato" |
||||||
|
) |
||||||
|
) |
||||||
|
) |
||||||
|
) |
||||||
|
); |
||||||
|
$this->requestType("POST"); |
||||||
|
$this->setMetodo('messages'); |
||||||
|
return $this->exec(); |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
function enviarLocalizacao($whatsapp, $longitude, $latitude, $nome = null, $endereco = null) |
||||||
|
{ |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if ($this->getArgs(func_get_args())) { |
||||||
|
$this->params = array( |
||||||
|
"to" => "+$whatsapp", |
||||||
|
"type" => "location", |
||||||
|
"location" => array( |
||||||
|
"longitude" => "$longitude", |
||||||
|
"latitude" => "$latitude", |
||||||
|
"name" => "$nome", |
||||||
|
"address" => utf8_encode("$endereco") |
||||||
|
) |
||||||
|
); |
||||||
|
$this->requestType("POST"); |
||||||
|
$this->setMetodo('messages'); |
||||||
|
return $this->exec(); |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
function baixarMidia() |
||||||
|
{ |
||||||
|
if (in_array($this->getType(), ['location', 'contacts', 'text'])) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
$name = $this->getId(); |
||||||
|
$this->debug = debug_backtrace(); |
||||||
|
if ($this->getArgs(func_get_args())) { |
||||||
|
$this->requestType("GET"); |
||||||
|
$this->setMetodo('media/' . $name); |
||||||
|
$pathfile = $this->storage . $name; |
||||||
|
$retorno = $this->exec(); |
||||||
|
file_put_contents($pathfile, $retorno); |
||||||
|
if (file_exists($pathfile)) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Profile WhatsApp |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getProfile() |
||||||
|
{ |
||||||
|
return $this->hook['contacts'][0]['profile']['name']; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Phone WhatsApp |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function getPhone() |
||||||
|
{ |
||||||
|
return $this->hook['contacts'][0]['wa_id']; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the type of the message |
||||||
|
* @return string|boolean |
||||||
|
*/ |
||||||
|
public function getType() |
||||||
|
{ |
||||||
|
if ($this->hook) { |
||||||
|
return $this->hook['messages'][0]['type']; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the mime of the message |
||||||
|
* @return string|boolean |
||||||
|
*/ |
||||||
|
public function getMimetype() |
||||||
|
{ |
||||||
|
if ($this->hook['messages'][0][$this->getType()]['mime_type']) { |
||||||
|
return $this->hook['messages'][0][$this->getType()]['mime_type']; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the id of the message |
||||||
|
* @return string|boolean |
||||||
|
*/ |
||||||
|
public function getId() |
||||||
|
{ |
||||||
|
if ($this->hook && $this->getType()) { |
||||||
|
if ($this->getType() == 'text') { |
||||||
|
return $this->hook['messages'][0]['id']; |
||||||
|
} |
||||||
|
return $this->hook['messages'][0][$this->getType()]['id']; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the (text, body) of the message |
||||||
|
* @return string|boolean |
||||||
|
*/ |
||||||
|
public function getIsValidMessage() |
||||||
|
{ |
||||||
|
return $this->hook['contacts']; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the (text, body) of the message |
||||||
|
* @return string|boolean |
||||||
|
*/ |
||||||
|
public function getMessage() |
||||||
|
{ |
||||||
|
if ($this->hook['messages'][0]['interactive']) { |
||||||
|
if ($this->hook['messages'][0]['interactive']['list_reply']) { |
||||||
|
$id = $this->hook['messages'][0]['interactive']['list_reply']['id']; |
||||||
|
if (strpos($id, 'P') === 0) { |
||||||
|
return $this->hook['messages'][0]['interactive']['list_reply']['description']; |
||||||
|
} |
||||||
|
return $this->hook['messages'][0]['interactive']['list_reply']['title']; |
||||||
|
} else { |
||||||
|
return $this->hook['messages'][0]['interactive']['button_reply']['title']; |
||||||
|
} |
||||||
|
} |
||||||
|
$message = $this->hook['messages'][0]['text']['body']; |
||||||
|
return ($message ? $message : false); |
||||||
|
} |
||||||
|
public function setMessage($msg) |
||||||
|
{ |
||||||
|
$this->hook['messages'][0]['text']['body'] = $msg; |
||||||
|
} |
||||||
|
/** |
||||||
|
* Returns the name of the contact |
||||||
|
* @return string|boolean |
||||||
|
*/ |
||||||
|
public function getContactFormatted() |
||||||
|
{ |
||||||
|
$formatted = $this->hook['messages'][0]['contacts'][0]['name']['formatted_name']; |
||||||
|
if ($formatted) { |
||||||
|
return $formatted; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the phone of the contact |
||||||
|
* @return string|boolean |
||||||
|
*/ |
||||||
|
public function getContactPhone() |
||||||
|
{ |
||||||
|
$contact = $this->hook['messages'][0]['contacts'][0]['phones'][0]['wa_id']; |
||||||
|
if ($contact) { |
||||||
|
return $contact; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the latitude|longitude of the location |
||||||
|
* @return string|boolean |
||||||
|
*/ |
||||||
|
public function getGeolocation($type) |
||||||
|
{ |
||||||
|
$geolocation = $this->hook['messages'][0]['location'][strtolower($type)]; |
||||||
|
if ($geolocation) { |
||||||
|
return $geolocation; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public function retornaTituloDocument() |
||||||
|
{ |
||||||
|
if ($this->hook['messages'][0]['document']) { |
||||||
|
return $this->hook['messages'][0]['document']['filename']; |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,144 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace app\Providers; |
||||||
|
|
||||||
|
class Requests |
||||||
|
{ |
||||||
|
/** @var string $token token de autenticação das requesições */ |
||||||
|
protected $token; |
||||||
|
|
||||||
|
/** @var string $url url base das requsições */ |
||||||
|
protected $url; |
||||||
|
|
||||||
|
/** @var string $metodo endpoint da requsição */ |
||||||
|
protected $metodo; |
||||||
|
|
||||||
|
/** @var string $query query da requsição */ |
||||||
|
protected $query; |
||||||
|
|
||||||
|
/** @var string $query tipo da requsição */ |
||||||
|
protected $requestType; |
||||||
|
|
||||||
|
/** @var array $params parametros da request */ |
||||||
|
protected $params = array(); |
||||||
|
|
||||||
|
/** @var RequestURL $request description */ |
||||||
|
protected $request; |
||||||
|
|
||||||
|
/** @var string $contentType tipo da requsição */ |
||||||
|
protected $contentType; |
||||||
|
|
||||||
|
/** @var string $storage caminho dos arquivos */ |
||||||
|
public $storage = CONF_PATH_FILES; |
||||||
|
|
||||||
|
function __construct() |
||||||
|
{ |
||||||
|
$this->request = new RequestURL(); |
||||||
|
$this->setToken(); |
||||||
|
$this->setUrl(CONF_WHATSAPP_AUTH_URL); |
||||||
|
} |
||||||
|
|
||||||
|
function response($result) |
||||||
|
{ |
||||||
|
if ($result) { |
||||||
|
if (json_decode($result, true) !== null) { |
||||||
|
return json_decode($result, true); |
||||||
|
} |
||||||
|
return $result; |
||||||
|
} else { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function exec($typeAuth = 'Bearer') |
||||||
|
{ |
||||||
|
$this->setQuery(json_encode($this->params)); //SET QUERY |
||||||
|
$this->request->setUrl($this->url . $this->metodo, false); |
||||||
|
|
||||||
|
$header = array(); |
||||||
|
|
||||||
|
$header[] = "Authorization: $typeAuth {$this->token}"; |
||||||
|
|
||||||
|
|
||||||
|
if ($this->requestType == 'POST') { |
||||||
|
$header[] = 'Content-Type: application/json'; |
||||||
|
$this->request->post_field($this->getQuery(), true); |
||||||
|
} |
||||||
|
$this->request->header($header); |
||||||
|
$this->request->method_request($this->requestType); |
||||||
|
logger('request')->info(print_r($header, true)); |
||||||
|
logger('request')->info($this->requestType); |
||||||
|
return $this->request->exec_request(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Recebe o tipo de Requisi��o GET/POST |
||||||
|
* |
||||||
|
* @return boolean |
||||||
|
*/ |
||||||
|
function requestType($req = null) |
||||||
|
{ |
||||||
|
if (!$req) { |
||||||
|
return $this->requestType; |
||||||
|
} |
||||||
|
|
||||||
|
if (strtoupper($req) == "GET") { |
||||||
|
return $this->requestType = "GET"; |
||||||
|
} else if (strtoupper($req) == "POST") { |
||||||
|
return $this->requestType = "POST"; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Verifica se todos os parametros passados foram completados. |
||||||
|
* |
||||||
|
* @param array $args |
||||||
|
* @return true|false |
||||||
|
*/ |
||||||
|
function getArgs($args) |
||||||
|
{ |
||||||
|
foreach ($args as $value) { |
||||||
|
if (!$value) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
function setMetodo($metodo) |
||||||
|
{ |
||||||
|
$this->metodo = $metodo; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Escreve a query para ser passada para o curl |
||||||
|
* |
||||||
|
* @param string $query |
||||||
|
*/ |
||||||
|
function setQuery($query) |
||||||
|
{ |
||||||
|
return $this->query .= $query; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* retorna a string pronta da query do curl e limpa a variavel. |
||||||
|
* |
||||||
|
* @return string $query |
||||||
|
*/ |
||||||
|
function getQuery() |
||||||
|
{ |
||||||
|
$query = $this->query; |
||||||
|
unset($this->query); |
||||||
|
return $query; |
||||||
|
} |
||||||
|
|
||||||
|
function setToken($token = CONF_WHATSAPP_AUTH_TOKEN) |
||||||
|
{ |
||||||
|
$this->token = $token; |
||||||
|
} |
||||||
|
|
||||||
|
public function setUrl($url) |
||||||
|
{ |
||||||
|
$this->url = $url; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue