|
|
|
|
<?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 <EFBFBD> os dados referentes ao retornado da API
|
|
|
|
|
* - Params s<EFBFBD>o os dados que ser<EFBFBD>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<EFBFBD><EFBFBD>o de pendencia financeira de acordo com a data informada
|
|
|
|
|
*
|
|
|
|
|
* OBS: O array deve possui n<EFBFBD>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<EFBFBD><EFBFBD>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 <EFBFBD>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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|