|
|
|
|
<?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('integracao');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retorna os registros armazenados na variavel registros pra ser disponibilizado
|
|
|
|
|
* no decorrer de uma integracao;
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getRegistros() {
|
|
|
|
|
return $this->registros;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Informa<EFBFBD><EFBFBD>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<EFBFBD>m de gravar logs de registros de dados,
|
|
|
|
|
* e qualquer erro <EFBFBD> repassado a classe que est<EFBFBD> executando para tratar o erro;
|
|
|
|
|
*
|
|
|
|
|
* @param string $type
|
|
|
|
|
* @return boolean|array
|
|
|
|
|
*/
|
|
|
|
|
private function execute($type = '') {
|
|
|
|
|
try {
|
|
|
|
|
if (!$this->connection) {
|
|
|
|
|
$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());
|
|
|
|
|
} else {
|
|
|
|
|
throw new Exception('Nao foi possivel conectar na base de dados');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
## ASTERISK ##
|
|
|
|
|
########################################################################
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Coleta em qual Asterisk a central est<EFBFBD> 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;
|
|
|
|
|
*/
|
|
|
|
|
private function beginTransaction() {
|
|
|
|
|
$this->debug = debug_backtrace();
|
|
|
|
|
$this->query = "BEGIN;";
|
|
|
|
|
$this->execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Registra uma Transacao.
|
|
|
|
|
*/
|
|
|
|
|
private function commitTransaction() {
|
|
|
|
|
$this->debug = debug_backtrace();
|
|
|
|
|
$this->query = "COMMIT;";
|
|
|
|
|
$this->execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retorna as informacoes antes da Transacao.
|
|
|
|
|
*/
|
|
|
|
|
private function rollbackTransaction() {
|
|
|
|
|
$this->debug = debug_backtrace();
|
|
|
|
|
$this->query = "ROLLBACK;";
|
|
|
|
|
$this->execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 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');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
VALUES('{$this->registros['reg_id_metodo']}','{$this->registros['reg_uniqueid']}','{$this->registros['reg_uniqueid_old']}','{$this->registros['reg_fone']}', now(), '$tronco', '$ura')";
|
|
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|