hitagi-entrada/apagar/AGIEntrada.php

483 lines
13 KiB
PHP

<?php
class AGIEntrada
{
private $globais = array();
private $injection = array();
public function __construct($injection, $globais = array())
{
$globais['logEnabled'] = !isset($globais['logEnabled']) ? false : $globais['logEnabled'];
$globais['isXampp'] = !isset($globais['isXampp']) ? false : $globais['isXampp'];
$this->globais = $globais;
$this->injection = $injection;
}
public function agiRunCmd($command)
{
return $this->injection['agiRunCmd']($command);
}
public function agiLog($texto)
{
return $this->injection['agiLog']($texto);
}
// --------------------------------------------------------------------------------------------------------------------------------
public function logStart()
{
if ($this->globais['logEnabled']) {
$texto = date("Y-m-d H:i:s") . "<hr>";
$fp = fopen('log.html', 'w');
fwrite($fp, $texto);
fclose($fp);
}
}
public function logAdd($texto)
{
if ($this->globais['logEnabled']) {
$fp = fopen('log.html', 'a');
fwrite($fp, $texto);
fclose($fp);
}
}
// --------------------------------------------------------------------------------------------------------------------------------
public function agiVarGet($varName = '')
{
$return = '';
if ($this->globais['isXampp']) {
$return = '';
} else {
//$cmd = 'GET VARIABLE UNIQUEID'; // 200 result=1 (1695911399.249)
//$cmd = 'GET VARIABLE CALLERID(all)'; // 200 result=1 ("" <2001>)
//$cmd = 'GET VARIABLE EXTEN'; // 200 result=1 (2501)
$cmd = '';
if ($varName == 'CALLERID') {
$cmd = 'GET VARIABLE CALLERID(all)';
} else {
$cmd = 'GET VARIABLE ' . $varName;
}
$response = $this->agiRunCmd($cmd);
if ($varName == 'CALLERID') {
if (substr($response, 0, 14) == '200 result=1 (') {
$explodido = explode('<', $response);
$return = substr($explodido[1], 0, -2);
}
} else {
$explodido = explode("(", $response);
if (count($explodido) > 1) {
$explodido = explode(")", $explodido[1]);
$return = $explodido[0];
}
}
}
return $return;
}
public function agiVarSet($varName = '', $varValue = '')
{
if ($this->globais['isXampp']) {
return '';
} else {
if ($varName == 'CALLERID') {
$varName = 'CALLERID(num)';
}
$return = $this->agiRunCmd('SET VARIABLE ' . $varName . ' ' . $varValue);
return $return;
}
}
// --------------------------------------------------------------------------------------------------------------------------------
public function fgItemRunPlayaudio($fgItem = array())
{
$this->logAdd("<hr>fgItemRunPlayaudio<br><br>");
$this->logAdd("<pre>" . print_r($fgItem, true) . "</pre><br>");
}
public function fgItemRun($fgItem = array())
{
$this->logAdd("<hr>fgItemRun<br><br>");
$this->logAdd("<pre>" . print_r($fgItem, true) . "</pre><br>");
if (count($fgItem) > 0) {
// if ($fgItem['cmd'] == 'agi.exec') {
// fgItemRunAgiExec($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'blacklist.check') {
// fgItemRunBlacklistCheck($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'blacklist.true') {
// fgItemRunBlacklistTrue($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'blacklist.false') {
// fgItemRunBlacklistFalse($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'callerid.change') {
// fgItemRunCalleridChange($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'cnpj.check') {
// fgItemRunCNPJCheck($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'cnpj.true') {
// fgItemRunCNPJCheckTrue($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'cnpj.false') {
// fgItemRunCNPJCheckFalse($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'condtempo.check') {
// fgItemRunCondTempoCheck($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'condtempo.true') {
// fgItemRunCondTempoCheckTrue($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'condtempo.false') {
// fgItemRunCondTempoCheckFalse($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'cpf.check') {
// fgItemRunCPFCheck($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'cpf.true') {
// fgItemRunCPFCheckTrue($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'cpf.false') {
// fgItemRunCPFCheckFalse($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'dialextension') {
// fgItemRunDialExtension($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'dialgroupextension') {
// fgItemRunDialGroupExtension($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'dialplan.checkvar') {
// fgItemRunDialplanCheckvar($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'dialplan.gosub') {
// fgItemRunDialplanGosub($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'dialplan.setvar') {
// fgItemRunDialplanSetvar($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'dialdigit') {
// fgItemRunDialdigit($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'digit') {
// fgItemRunDigit($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'digitinvalid') {
// fgItemRunDigitInvalid($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'expediente.check') {
// fgItemRunExpedienteCheck($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'expediente.false') {
// fgItemRunExpedienteFalse($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'expediente.true') {
// fgItemRunExpedienteTrue($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'feriado.check') {
// fgItemRunFeriadoCheck($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'feriado.false') {
// fgItemRunFeriadoFalse($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'feriado.true') {
// fgItemRunFeriadoTrue($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'ivr.redirect') {
// fgItemRunIVRRedirect($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'nomatch') {
// fgItemRunNoMatch($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'pesqsat') {
// fgItemRunPesqSat($fgItem);
// return;
// }
if ($fgItem['cmd'] == 'playaudio') {
$this->fgItemRunPlayaudio($fgItem);
return;
}
// if ($fgItem['cmd'] == 'queue') {
// fgItemRunQueue($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'queue.waitingcount') {
// fgItemRunQueueWaitingCount($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'queue.waitingcount-greater') {
// fgItemRunQueueWaitingCountGreater($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'queue.withcallback') {
// fgItemRunQueueWithCallback($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'queue.withcallback-noready') {
// fgItemRunQueueWithCallbackNoReady($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'record') {
// fgItemRunRecord($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'start') {
// fgItemRunStart($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'timeout') {
// fgItemRunTimeout($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'timeoutwithlimit') {
// fgItemRunTimeoutWithLimit($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'timeoutwithlimit.limit') {
// fgItemRunTimeoutWithLimitLimit($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'voicemail') {
// fgItemRunVoicemail($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'whitelist.check') {
// fgItemRunWhitelistCheck($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'whitelist.false') {
// fgItemRunWhitelistFalse($fgItem);
// return;
// }
// if ($fgItem['cmd'] == 'whitelist.true') {
// fgItemRunWhitelistTrue($fgItem);
// return;
// }
// if (isset($fgItem['defaultvalues']['cmd'])) {
// if ($fgItem['defaultvalues']['cmd'] == 'dialplan.checkvar') {
// fgItemRunDialplanCheckvar($fgItem);
// return;
// }
// if ($fgItem['defaultvalues']['cmd'] == 'dialplan.gosub') {
// fgItemRunDialplanGosub($fgItem);
// return;
// }
// if ($fgItem['defaultvalues']['cmd'] == 'dialplan.setvar') {
// fgItemRunDialplanSetvar($fgItem);
// return;
// }
// }
}
}
public function teste()
{
$this->agiLog($this->globais['fgId']);
}
function tocarFila($queueId, $timeout)
{
return $this->agiRunCmd('EXEC Queue Q' . $queueId . ',ctT,,,' . $timeout);
}
// --------------------------------------------------------------------------------------------------------------------------------
public function run()
{
$this->logStart();
$this->logAdd("<pre>".print_r($this->globais, true) . "</pre><br>");
// $this->agiLog(print_r($this->globais, true) . "\n");
$this->globais['fgItens'] = array();
$this->globais['fgItemStart'] = array();
if ($this->globais['fgId'] != '') {
// ...
if (file_exists('/etc/asterisk')) {
$target = '/var/www/html/fluxograma/data-json/' . $this->globais['fgId'] . '.json';
} else {
$target = 'dial/' . $this->globais['fgId'] . '.json';
}
if (file_exists($target)) {
$itens = file_get_contents($target);
$itens = json_decode($itens, true);
foreach ($itens as $item) {
$this->globais['fgItens'][$item['id']] = $item;
if ($item['cmd'] == 'start') {
$this->globais['fgItemStart'] = $item;
}
}
unset($itens);
}
}
$this->logAdd("<pre>".print_r($this->globais['fgItens'], true) . "</pre><br>");
$this->logAdd("<pre>".print_r($this->globais['fgItemStart'], true) . "</pre><br>");
// $this->agiLog(print_r($this->globais['fgItens'], true) . "\n");
if (count($this->globais['fgItemStart']) > 0) {
$this->globais['arrDtmf'] = array(
'35' => '#',
'42' => '*',
'48' => '0',
'49' => '1',
'50' => '2',
'51' => '3',
'52' => '4',
'53' => '5',
'54' => '6',
'55' => '7',
'56' => '8',
'57' => '9'
);
if ($this->globais['isXampp']) {
$this->globais['callerid'] = '1138118400';
$this->globais['uniqueid'] = date("Ymd") . "." . date("His");
$this->globais['digitado'] = '';
$host = 'localhost';
$user = 'root';
$password = '';
$database = 'hitpbx_homologacao';
$port = '3306';
$this->globais['db'] = mysqli_connect($host, $user, $password, $database, $port) or die('Erro de conexão com o banco de dados');
mysqli_set_charset($this->globais['db'], 'utf8');
$this->fgItemRun($this->globais['fgItemStart']);
} else {
$host = '172.31.187.152';
$user = 'appuser';
$password = 'nmvP$x23Vzb@T%Su';
$hostname = gethostname();
$hostname = str_replace('hitpbx-', 'hitpbx_', $hostname);
$database = $hostname;
if ($hostname == 'hitpbx-001') {
$database = 'hitpbx-001';
}
$port = '6033';
$this->globais['db'] = mysqli_connect($host, $user, $password, $database, $port) or die('Erro de conexão com o banco de dados');
mysqli_set_charset($this->globais['db'], 'utf8');
/*
logAdd("111<br>");
$query = "SELECT * FROM tab_ramal LIMIT 1";
$result = mysqli_query($db,$query);
logAdd($query."<br>");
$result = mysqli_query($GLOBALS['db'],$query);
logAdd($query."<br>");
*/
// ...
/*
$host = '172.31.187.150';
$dbcdr = mysqli_connect($host, $user, $password, $database, $port) or die('Erro de conexão com o banco de dados');
mysqli_set_charset($dbcdr, 'utf8');
$query = "
CREATE TABLE IF NOT EXISTS tab_ivr_log2 (
`id` INT NOT NULL AUTO_INCREMENT,
`uniqueid` VARCHAR(32) NOT NULL,
`did` TEXT NULL,
`quando` DATETIME NULL,
`fgitem_json` TEXT NULL,
`fgitem_result` TEXT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
";
$result = mysqli_query($dbcdr,$query);
*/
// ...
$response = $this->agiRunCmd('EXEC NOOP HITAGI');
$response = $this->agiRunCmd('ANSWER');
$response = $this->agiRunCmd('EXEC WAIT 1');
$response = $this->agiRunCmd('EXEC PLAYBACK beep');
$response = $this->agiRunCmd('EXEC WAIT 1');
$this->globais['digitado'] = '';
$this->globais['callerid'] = $this->agiVarGet('CALLERID');
$this->globais['uniqueid'] = $this->agiVarGet('UNIQUEID');
$response = $this->agiVarSet('AGIDID', $this->globais['fgId']);
$cmd = 'EXEC QUEUELOG NONE,' . $this->globais['uniqueid'] . ',NONE,INFO|IVRSTART';
$response = $this->agiRunCmd($cmd);
$cmd = 'EXEC QUEUELOG NONE,' . $this->globais['uniqueid'] . ',NONE,INFO|IVRAPPEND|->' . $this->globais['fgId'];
$response = $this->agiRunCmd($cmd);
$this->fgItemRun($this->globais['fgItemStart']);
// 1353461660|1353461627.33271|NONE|NONE|INFO|IVRHANGUP|
//$cmd = 'EXEC QUEUELOG NONE,' . $GLOBALS['uniqueid'] . ',NONE,NONE,INFO|IVRHANGUP';
//$response = agiRunCmd($cmd);
// $response = agiRunCmd('HANGUP');
}
}
/*
$this->teste();
$this->globais['nomeFila'] = '10';
$response = $this->tocarFila($this->globais['nomeFila'], 1);
echo "\n" . $response . "\n";
*/
}
}