hitagi-entrada/classes/AGIEntrada.php

3035 lines
94 KiB
PHP
Raw Normal View History

2024-12-14 01:14:11 +00:00
<?php
namespace Hitlabs\Hitagi;
class AGIEntrada
{
private $globais = array();
private $injection = array();
public function __construct($injection, $globais = array())
{
2024-12-16 19:01:16 +00:00
$globais['fgId'] = $globais['fgId'] ?? null;
$globais['hostname'] = $globais['hostname'] ?? null;
$globais['hostip'] = $globais['hostip'] ?? null;
$globais['logEnabled'] = $globais['logEnabled'] ?? null;
$globais['appdir'] = $globais['appdir'] ?? null;
$globais['isXampp'] = $globais['isXampp'] ?? null;
2024-12-14 01:14:11 +00:00
$this->globais = $globais;
$this->injection = $injection;
}
public function agiRunCmd($command)
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog($command);
$return = '';
if ($this->globais['isXampp']) {
$return = '200 result=1 (isXampp)';
} else {
$return = $this->injection['agiRunCmd']($command);
}
$this->agiLog($return);
return $return;
}
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>";
$target = $this->globais['appdir'] . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . 'log.html';
$fp = fopen($target, 'w');
fwrite($fp, $texto);
fclose($fp);
}
}
public function logAdd($texto)
{
if ($this->globais['logEnabled']) {
$target = $this->globais['appdir'] . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . 'log.html';
$fp = fopen($target, 'a');
fwrite($fp, $texto);
fclose($fp);
}
}
// --------------------------------------------------------------------------------------------------------------------------------
public function fgGet()
{
$this->globais['fgItens'] = array();
$this->globais['fgItemStart'] = array();
2024-12-16 19:01:16 +00:00
if (!is_null($this->globais['fgId'])) {
if ($this->globais['isXampp']) {
$target = $this->globais['appdir'] . DIRECTORY_SEPARATOR . 'dial' . DIRECTORY_SEPARATOR . $this->globais['fgId'] . '.json';
} else if (!is_null($this->globais['hostip'])) {
2024-12-14 01:14:11 +00:00
// http://177.107.205.248/fluxograma/data-json/8999.json
2024-12-16 19:01:16 +00:00
$target = 'http://' . $this->globais['hostip'] . '/fluxograma/data-json/' . $this->globais['fgId'] . '.json';
} else if (file_exists('/etc/asterisk')) {
$target = '/var/www/html/fluxograma/data-json/' . $this->globais['fgId'] . '.json';
2024-12-14 01:14:11 +00:00
}
$itens = file_get_contents($target);
if ($itens !== false) {
$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);
}
}
}
// --------------------------------------------------------------------------------------------------------------------------------
public function agiAddCallback($args = array())
{
2024-12-20 16:08:25 +00:00
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($args, true));
2024-12-14 01:14:11 +00:00
// https://www.voip-info.org/asterisk-auto-dial-out/
$args['ramal'] = isset($args['ramal']) ? $args['ramal'] : '';
$args['numero'] = isset($args['numero']) ? $args['numero'] : '';
//$calldata = "Channel: PJSIP/" . $args['ramal'] . "\n";
//$calldata.= "Context: padrao\n";
//$calldata.= "Extension: " . $args['numero'] . "\n";
$calldata = "Channel: Local/" . $args['ramal'] . "@padrao" . "\n";
$calldata .= "Context: padrao\n";
$calldata .= "Extension: 0" . $args['numero'] . "\n";
// MaxRetries
// RetryTime
// WaitTime
$filename = time() . '-' . rand(100, 999) . '.call';
2024-12-20 16:08:25 +00:00
/*
2024-12-14 01:14:11 +00:00
if (file_exists('/var/www/html/hitmanager/dial/')) {
$target = '/var/www/html/hitmanager/dial/' . $filename;
file_put_contents($target, $calldata);
$time = time() + 30;
touch($target, $time);
}
if (file_exists('/var/spool/asterisk/outgoing/')) {
$target2 = '/var/spool/asterisk/outgoing/' . $filename;
rename($target, $target2);
}
2024-12-20 16:08:25 +00:00
*/
/*
$target = $this->globais['appdir'] . DIRECTORY_SEPARATOR . 'outgoing' . DIRECTORY_SEPARATOR . $filename;
file_put_contents($target, $calldata);
$time = time() + 30;
touch($target, $time);
*/
$argssend = array();
// $argssend['filedir'] = '/var/spool/asterisk/outgoing';
$argssend['filedir'] = '/var/www/html/apagar';
$argssend['filename'] = $filename;
$argssend['filebase64'] = base64_encode($calldata);
$arquivoremoto = new ArquivoRemoto();
$arquivoremoto->ip = $this->globais['hostip'];
$this->agiLog(print_r($arquivoremoto, true));
$response = $arquivoremoto->salvar($argssend);
$this->agiLog(print_r($response, true));
2024-12-14 01:14:11 +00:00
}
public function agiRecordFilaIfAuditoria($args = array())
{
// $this->agiLog("===> " . __FUNCTION__ . " <===");
// $this->agiLog(print_r($args, true));
$return = '';
$args['idqueue'] = isset($args['idqueue']) ? $args['idqueue'] : '';
$gravar = $this->hitpbxCheckAuditoria(array('idqueue' => $args['idqueue']));
if ($gravar) {
$response = $this->agiRunRecord(array('tipo' => 'auditoria_fila', 'idqueue' => $args['idqueue']));
}
return $return;
}
public function agiRecordRamaisIfAuditoria($args = array())
{
// $this->agiLog("===> " . __FUNCTION__ . " <===");
// $this->agiLog(print_r($args, true));
$return = '';
$args['ramal'] = isset($args['ramal']) ? $args['ramal'] : '';
$args['ramais'] = isset($args['ramais']) ? $args['ramais'] : array();
if ($args['ramal'] != '') {
$args['ramais'][] = $args['ramal'];
}
foreach ($args['ramais'] as $ramal) {
$gravar = $this->hitpbxCheckAuditoria(array('ramal' => $ramal));
if ($gravar) {
//$response = agiRunCmd('EXEC NOOP SIM'.$ramal);
$response = $this->agiRunRecord(array('tipo' => 'auditoria_ramal', 'ramal' => $ramal));
} else {
//$response = agiRunCmd('EXEC NOOP NAO'.$ramal);
}
}
return $return;
}
public function agiRunDialNumeroExterno($numero = '')
{
// $this->agiLog("===> " . __FUNCTION__ . " <===");
// $this->agiLog($numero);
$return = '';
//$cmd = 'EXEC Dial SIP/SER2105/'.$numero.',30,Tt ""';
$cmd = 'EXEC Dial Local/' . $numero . '@checksaida,30,Tt ""';
$response = $this->agiRunCmd($cmd);
$cmd = 'GET VARIABLE DIALSTATUS';
$response = $this->agiRunCmd($cmd);
$explodido = explode("(", $response);
if (count($explodido) > 1) {
$return = substr($explodido[1], 0, -1);
if ($return != 'ANSWER') {
$cmd = 'EXEC PLAYBACK /var/lib/asterisk/sounds/pt_BR/exten-unavail ""';
$response = $this->agiRunCmd($cmd);
}
}
return $return;
}
public function agiRunDialRamal($ramal = '')
{
// $this->agiLog("===> " . __FUNCTION__ . " <===");
// $this->agiLog($ramal);
$return = '';
$runNext = true;
$desvio = $this->hitpbxGetDesvioFrom(array('tipo' => 'ramal', 'value' => $ramal));
if (count($desvio)) {
$runNext = false;
if ($desvio['shortname'] == 'numeroexterno') {
$return = $this->agiRunDialNumeroExterno($desvio['numero']);
}
if ($desvio['shortname'] == 'queue') {
$queuestatus = $this->agiRunQueue(array('id' => $desvio['queue']));
if ($queuestatus == '') {
$return = 'ANSWER';
}
}
if ($desvio['shortname'] == 'ramal') {
$return = $this->agiRunDialRamal($desvio['ramal']);
}
if ($desvio['shortname'] == 'voicemail') {
$this->agiRunVoicemail($desvio['ramal']);
$return = 'ANSWER';
}
if ($desvio['shortname'] == 'numeroexterno') {
$this->agiRunDialNumeroExterno($desvio['numero']);
}
}
if ($runNext) {
$ramais_vai_tocar = array();
$cmd = 'EXEC Dial ';
$ramais_secretaria = $this->hitpbxGetRamaisSecretariaFromRamal($ramal);
if (count($ramais_secretaria)) {
foreach ($ramais_secretaria as $ramal) {
$response = $this->agiRunCmd('GET FULL VARIABLE ${PJSIP_DIAL_CONTACTS(' . $ramal . ')}');
$contacts = str_replace('200 result=1 (', '', $response);
$contacts = substr($contacts, 0, -1);
$ramais_vai_tocar[] = $ramal;
if ($cmd != 'EXEC Dial ') {
$cmd .= '&';
}
//$cmd.= 'PJSIP/'.$ramal;
//$cmd.= '${PJSIP_DIAL_CONTACTS('.$ramal.')}';
$cmd .= $contacts;
$response = $this->agiRunCmd('GET FULL VARIABLE ${PJSIP_DIAL_CONTACTS(SIP' . $ramal . ')}');
$contacts2 = str_replace('200 result=1 (', '', $response);
$contacts2 = substr($contacts2, 0, -1);
$cmd .= '&' . $contacts2;
}
$cmd .= ',30,Tt ""';
} else {
$response = $this->agiRunCmd('GET FULL VARIABLE ${PJSIP_DIAL_CONTACTS(' . $ramal . ')}');
$contacts = str_replace('200 result=1 (', '', $response);
$contacts = substr($contacts, 0, -1);
$ramais_vai_tocar[] = $ramal;
//$cmd.= 'PJSIP/' . $ramal;
//$cmd.= '${PJSIP_DIAL_CONTACTS('.$ramal.')}';
$cmd .= $contacts;
$response = $this->agiRunCmd('GET FULL VARIABLE ${PJSIP_DIAL_CONTACTS(SIP' . $ramal . ')}');
$contacts2 = str_replace('200 result=1 (', '', $response);
$contacts2 = substr($contacts2, 0, -1);
$cmd .= '&' . $contacts2;
$ring_timeout = $this->hitpbxGetRingTimeoutFromRamal($ramal);
if (empty($ring_timeout)) {
$cmd .= ',30,Tt ""';
} else {
$cmd .= ',' . $ring_timeout . ',Tt ""';
}
}
/*
$uniqueid = agiVarGet('UNIQUEID');
$argssend = array();
$argssend["companyId"] = "1";
$argssend["crmPhone"] = $GLOBALS['callerid'];
$argssend["operation"] = "inboundAnsweredCall";
$argssend["crmAgent"] = $ramal;
$argssend["srcPhone"] = $GLOBALS['callerid'];
$argssend["dstPhone"] = $ramal;
$argssend["operationStatus"] = "update-answer";
$argssend["uniqueid"] = $uniqueid;
crmSend($argssend);
*/
// ...
$response = $this->agiRecordRamaisIfAuditoria(array('ramais' => $ramais_vai_tocar));
$response = $this->agiRunCmd($cmd);
// ...
//$argssend["operationStatus"] = "hangup";
//crmSend($argssend);
$cmd = 'GET VARIABLE DIALSTATUS';
$response = $this->agiRunCmd($cmd);
$explodido = explode("(", $response);
if (count($explodido) > 1) {
$return = substr($explodido[1], 0, -1);
if ($return != 'ANSWER') {
$cmd = 'EXEC PLAYBACK /var/lib/asterisk/sounds/pt_BR/exten-unavail ""';
$response = $this->agiRunCmd($cmd);
$callback = $this->hitpbxGetCallbackFromRamal($ramal);
if ($callback == '1') {
$callerid = $this->agiVarGet('CALLERID');
$arr = array(
'ramal' => $ramal,
'numero' => $callerid
);
$this->agiAddCallback($arr);
}
}
}
}
return $return;
}
public function agiRunDialRamalGrupo($callgroup = '')
{
// $this->agiLog("===> " . __FUNCTION__ . " <===");
// $this->agiLog($callgroup);
$return = '';
$runNext = true;
$desvio = $this->hitpbxGetDesvioFrom(array('tipo' => 'ramalgrupo', 'value' => $callgroup));
if (count($desvio)) {
$runNext = false;
if ($desvio['shortname'] == 'queue') {
$queuestatus = $this->agiRunQueue(array('id' => $desvio['queue']));
if ($queuestatus == '') {
$return = 'ANSWER';
}
}
if ($desvio['shortname'] == 'ramal') {
$return = $this->agiRunDialRamal($desvio['ramal']);
}
if ($desvio['shortname'] == 'ramalgrupo') {
$return = $this->agiRunDialRamalGrupo($desvio['callgroup']);
}
if ($desvio['shortname'] == 'voicemail') {
$this->agiRunVoicemail($desvio['ramal']);
$return = 'ANSWER';
}
if ($desvio['shortname'] == 'numeroexterno') {
$this->agiRunDialNumeroExterno($desvio['numero']);
}
}
if ($runNext) {
$ramais_vai_tocar = array();
$cmd = 'EXEC Dial ';
$query = "SELECT ramal FROM tab_ramal WHERE callgroup = '" . mysqli_real_escape_string($this->globais['db'], $callgroup) . "'";
$query_result = mysqli_query($this->globais['db'], $query) or die(mysqli_error($this->globais['db']));
while ($resultLinha = mysqli_fetch_assoc($query_result)) {
if ($cmd != 'EXEC Dial ') {
$cmd .= '&';
}
$cmd .= 'PJSIP/' . $resultLinha['ramal'];
$ramais_vai_tocar[] = $resultLinha['ramal'];
}
$cmd .= ' ""';
$response = $this->agiRecordRamaisIfAuditoria(array('ramais' => $ramais_vai_tocar));
$response = $this->agiRunCmd($cmd);
$cmd = 'GET VARIABLE DIALSTATUS';
$response = $this->agiRunCmd($cmd);
$explodido = explode("(", $response);
if (count($explodido) > 1) {
$return = substr($explodido[1], 0, -1);
if ($return != 'ANSWER') {
$cmd = 'EXEC PLAYBACK /var/lib/asterisk/sounds/pt_BR/exten-unavail ""';
$response = $this->agiRunCmd($cmd);
}
}
}
return $return;
}
public function agiRunQueue($args = array())
{
// $this->agiLog("===> " . __FUNCTION__ . " <===");
// $this->agiLog(print_r($args, true));
$return = '';
$args['id'] = isset($args['id']) ? $args['id'] : '';
$args['timeout'] = isset($args['timeout']) ? $args['timeout'] : '';
$runNext = true;
$cmd = 'SET VARIABLE __AGIQUEUEID "Q' . $args['id'] . '"';
$response = $this->agiRunCmd($cmd);
$queuenome = $this->hitpbxGetQueuenomeFromId($args['id']);
$cmd = 'SET VARIABLE __AGIQUEUENOME "' . $queuenome . '"';
$response = $this->agiRunCmd($cmd);
// teste
// $cmd = 'GET FULL VARIABLE ${QUEUE_WAITING_COUNT(' . $args['id'] . ')}';
// $cmd = 'GET FULL VARIABLE ${QUEUE_MEMBER(' . $args['id'] . ',ready)}';
// $response = agiRunCmd($cmd);
// $cmd = 'SET VARIABLE AGICALLBACKQUEUEID 32';
// $response = agiRunCmd($cmd);
// ...
// MONITOR_FILENAME=FILA_${STRFTIME(${EPOCH},,%Y-%m-%d_%H-%M-%S)}_${UNIQUEID}_${CALLERID(num)}_${EXTEN}
// $cmd = "SET VARIABLE MONITOR_FILENAME FILA_" . date("Y-m-d_H-i-s") . "_" . $GLOBALS['uniqueid'] . "_" . $GLOBALS['callerid'] . "_" . $GLOBALS['fgId'];
$cmd = "SET VARIABLE MONITOR_FILENAME QUEUE_" . date("Y-m-d") . "_" . date("H-i-s") . "_" . $this->globais['uniqueid'] . "_" . $this->globais['callerid'] . "_" . $this->globais['fgId'];
$response = $this->agiRunCmd($cmd);
// $cmd = "SET VARIABLE GLOBAL(AGIRECORDNAME) QUEUE_" . date("Y-m-d") . "_" . date("H-i-s") . "_" . $GLOBALS['uniqueid'] . "_" . $GLOBALS['callerid'] . "_" . $GLOBALS['fgId'];
$cmd = "SET VARIABLE __AGIRECORDNAME QUEUE_" . date("Y-m-d") . "_" . date("H-i-s") . "_" . $this->globais['uniqueid'] . "_" . $this->globais['callerid'] . "_" . $this->globais['fgId'];
$response = $this->agiRunCmd($cmd);
$desvio = $this->hitpbxGetDesvioFrom(array('tipo' => 'queue', 'value' => $args['id']));
if ($this->globais['isXampp']) {
$rnd = rand(1, 100);
if ($rnd >= 80) {
$desvio = array();
} else if ($rnd >= 60) {
$desvio = array('shortname' => 'queue', 'queue' => '3');
} else if ($rnd >= 40) {
$desvio = array('shortname' => 'ramal', 'ramal' => '1001');
} else if ($rnd >= 20) {
$desvio = array('shortname' => 'voicemail', 'ramal' => '1001');
} else {
$desvio = array('shortname' => 'numeroexterno', 'numero' => '551138118400');
}
}
if (count($desvio)) {
// logAdd("desvio: ".json_encode($desvio)."<br>");
$runNext = false;
if ($desvio['shortname'] == 'queue') {
$return = $this->agiRunQueue(array('id' => $desvio['queue']));
}
if ($desvio['shortname'] == 'ramal') {
$response = $this->agiRunDialRamal($desvio['ramal']);
if ($response == 'ANSWER') {
$return = '';
}
}
if ($desvio['shortname'] == 'voicemail') {
$this->agiRunVoicemail($desvio['ramal']);
$return = '';
}
if ($desvio['shortname'] == 'numeroexterno') {
$this->agiRunDialNumeroExterno($desvio['numero']);
}
}
if ($runNext) {
// $response = agiRunCmd('SET VARIABLE GLOBAL(AGIIVRSTOP) NAO');
$response = $this->agiRunCmd('SET VARIABLE __AGIIVRSTOP NAO');
if (empty($args['timeout'])) {
$cmd = 'EXEC Queue Q' . $args['id'] . ',ctT';
} else {
$cmd = 'EXEC Queue Q' . $args['id'] . ',ctT,,,' . $args['timeout'];
}
$response = $this->agiRecordFilaIfAuditoria(array('idqueue' => $args['id']));
$response = $this->agiRunCmd($cmd);
$cmd = 'GET VARIABLE QUEUESTATUS';
$response = $this->agiRunCmd($cmd);
// $this->logAdd('GET VARIABLE QUEUESTATUS: ' . $response . '<br>');
if ($this->globais['isXampp']) {
$rnd = rand(1, 100);
if ($rnd >= 50) {
$response = 'TIMEOUT';
} else {
$response = '';
}
}
// logAdd("QUEUESTATUS: ".$response);
if (strpos($response, 'TIMEOUT')) {
$return = 'timeout';
}
// ...
// $agiivrstop = agiVarGet("GLOBAL(AGIIVRSTOP)");
$agiivrstop = $this->agiVarGet("AGIIVRSTOP");
$agiivrstop = str_replace('"', '', $agiivrstop);
// logAdd('AGIIVRSTOP: '.$agiivrstop.'<br>');
if ($agiivrstop == 'SIM') {
// logAdd('FOI SIM<br>');
$return = 'stopivr';
}
// ...
}
return $return;
}
public function agiRunRecord($args = array())
{
// $this->agiLog("===> " . __FUNCTION__ . " <===");
// $this->agiLog(print_r($args, true));
/*
Layout dos arquivos:
TIPO_DATA_HORA_UNIQUEID_CALLERID_ORIGEM_DESTINO
Exemplo ramal:
AUDITORIA-RAMAL_2023-11-29_10-33-55_1701264825.168_2004_2005.wav
Exemplo fial de atendimento (o destino é o id da tab_queue)
AUDITORIA-QUEUE_2023-11-29_10-54-36_1701266065.175_2004_14.wav
*/
$return = '';
$args['tipo'] = isset($args['tipo']) ? $args['tipo'] : '';
if ($args['tipo'] == 'auditoria_fila') {
$args['idqueue'] = isset($args['idqueue']) ? $args['idqueue'] : '';
if (is_numeric($args['idqueue'])) {
$uniqueid = $this->agiVarGet('UNIQUEID');
$callerid = $this->agiVarGet('CALLERID');
//$cmd = 'EXEC MixMonitor AUDITORIA-QUEUE_'.date("Y-m-d").'_'.date("H-i-s").'_'.$uniqueid.'_'.$callerid.'_'.$args['idqueue'].'.wav';
$cmd = 'EXEC MixMonitor AUDITORIA-QUEUE_' . date("Y-m-d") . '_' . date("H-i-s") . '_' . $uniqueid . '_' . $callerid . '_' . $args['idqueue'] . '.ogg';
$response = $this->agiRunCmd($cmd);
}
}
if ($args['tipo'] == 'auditoria_ramal') {
$args['ramal'] = isset($args['ramal']) ? $args['ramal'] : '';
if (is_numeric($args['ramal'])) {
$uniqueid = $this->agiVarGet('UNIQUEID');
$callerid = $this->agiVarGet('CALLERID');
//$cmd = 'EXEC MixMonitor AUDITORIA-RAMAL_'.date("Y-m-d").'_'.date("H-i-s").'_'.$uniqueid.'_'.$callerid.'_'.$args['ramal'].'.wav';
$cmd = 'EXEC MixMonitor AUDITORIA-RAMAL_' . date("Y-m-d") . '_' . date("H-i-s") . '_' . $uniqueid . '_' . $callerid . '_' . $args['ramal'] . '.ogg';
$response = $this->agiRunCmd($cmd);
}
}
return $return;
}
public function agiRunVoicemail($ramal = '')
{
// $this->agiLog("===> " . __FUNCTION__ . " <===");
// $this->agiLog($ramal);
$return = $this->agiRunCmd('EXEC VOICEMAIL ' . $ramal);
return $return;
}
public function agiVarGet($varName = '')
{
// $this->agiLog("===> " . __FUNCTION__ . " <===");
// $this->agiLog($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);
2024-12-20 16:08:25 +00:00
// $return = str_replace('>)', '', trim($explodido[1]));
$return = substr(trim($explodido[1]), 0, -2);
2024-12-14 01:14:11 +00:00
}
} else {
$explodido = explode("(", $response);
if (count($explodido) > 1) {
$explodido = explode(")", $explodido[1]);
$return = $explodido[0];
}
}
}
return $return;
}
public function agiVarSet($varName = '', $varValue = '')
{
// $this->agiLog("===> " . __FUNCTION__ . " <===");
// $this->agiLog($varName);
// $this->agiLog($varValue);
if ($this->globais['isXampp']) {
return '';
} else {
if ($varName == 'CALLERID') {
$varName = 'CALLERID(num)';
}
$return = $this->agiRunCmd('SET VARIABLE ' . $varName . ' ' . $varValue);
return $return;
}
}
// --------------------------------------------------------------------------------------------------------------------------------
public function fgItemGetChildrenFrom($fgItemFrom = array())
{
// $this->agiLog("===> " . __FUNCTION__ . " <===");
// $this->agiLog(print_r($fgItemFrom, true));
$return = array();
reset($this->globais['fgItens']);
foreach ($this->globais['fgItens'] as $fgItem) {
if ($fgItem['cmd'] != 'start') {
if ($fgItem['idmaster'] == $fgItemFrom['id']) {
$return[$fgItem['id']] = $fgItem;
}
}
}
return $return;
}
public function fgItemGetMasterFrom($fgItemFrom = array())
{
// $this->agiLog("===> " . __FUNCTION__ . " <===");
// $this->agiLog(print_r($fgItemFrom, true));
$return = array();
if (isset($this->globais['fgItens'][$fgItemFrom['idmaster']])) {
$return = $this->globais['fgItens'][$fgItemFrom['idmaster']];
}
return $return;
}
public function fgItemIsDialplanCheckvar($fgItem = array())
{
// $this->agiLog("===> " . __FUNCTION__ . " <===");
// $this->agiLog(print_r($fgItem, true));
if (isset($fgItem['defaultvalues']['cmd'])) {
if ($fgItem['defaultvalues']['cmd'] == 'dialplan.checkvar') {
return true;
}
}
if ($fgItem['cmd'] == 'dialplan.checkvar') {
return true;
}
return false;
}
public function fgItemRun($fgItem = array())
{
// $this->agiLog("===> " . __FUNCTION__ . " <===");
// $this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRun<br>");
$this->logAdd("<pre>" . print_r($fgItem, true) . "</pre><br>");
// teste arquivoremoto
// $content = file_get_contents('/var/www/html/hitmanager/agi-debug.html');
// $argssend = array();
// $argssend['filedir'] = '/var/www/html/apagar';
// $argssend['filename'] = 'teste.html';
// $argssend['filebase64'] = base64_encode($content);
// $arquivo_removo = new ArquivoRemoto();
// $response = $arquivo_removo->salvar($argssend);
// ...
if (count($fgItem) > 0) {
if ($fgItem['cmd'] == 'agi.exec') {
$this->fgItemRunAgiExec($fgItem);
return;
}
if ($fgItem['cmd'] == 'blacklist.check') {
$this->fgItemRunBlacklistCheck($fgItem);
return;
}
if ($fgItem['cmd'] == 'blacklist.true') {
$this->fgItemRunBlacklistTrue($fgItem);
return;
}
if ($fgItem['cmd'] == 'blacklist.false') {
$this->fgItemRunBlacklistFalse($fgItem);
return;
}
if ($fgItem['cmd'] == 'callerid.change') {
$this->fgItemRunCalleridChange($fgItem);
return;
}
if ($fgItem['cmd'] == 'cnpj.check') {
$this->fgItemRunCNPJCheck($fgItem);
return;
}
if ($fgItem['cmd'] == 'cnpj.true') {
$this->fgItemRunCNPJCheckTrue($fgItem);
return;
}
if ($fgItem['cmd'] == 'cnpj.false') {
$this->fgItemRunCNPJCheckFalse($fgItem);
return;
}
if ($fgItem['cmd'] == 'condtempo.check') {
$this->fgItemRunCondTempoCheck($fgItem);
return;
}
if ($fgItem['cmd'] == 'condtempo.true') {
$this->fgItemRunCondTempoCheckTrue($fgItem);
return;
}
if ($fgItem['cmd'] == 'condtempo.false') {
$this->fgItemRunCondTempoCheckFalse($fgItem);
return;
}
if ($fgItem['cmd'] == 'cpf.check') {
$this->fgItemRunCPFCheck($fgItem);
return;
}
if ($fgItem['cmd'] == 'cpf.true') {
$this->fgItemRunCPFCheckTrue($fgItem);
return;
}
if ($fgItem['cmd'] == 'cpf.false') {
$this->fgItemRunCPFCheckFalse($fgItem);
return;
}
if ($fgItem['cmd'] == 'dialextension') {
$this->fgItemRunDialExtension($fgItem);
return;
}
if ($fgItem['cmd'] == 'dialgroupextension') {
$this->fgItemRunDialGroupExtension($fgItem);
return;
}
if ($fgItem['cmd'] == 'dialplan.checkvar') {
$this->fgItemRunDialplanCheckvar($fgItem);
return;
}
if ($fgItem['cmd'] == 'dialplan.gosub') {
$this->fgItemRunDialplanGosub($fgItem);
return;
}
if ($fgItem['cmd'] == 'dialplan.setvar') {
$this->fgItemRunDialplanSetvar($fgItem);
return;
}
if ($fgItem['cmd'] == 'dialdigit') {
$this->fgItemRunDialdigit($fgItem);
return;
}
if ($fgItem['cmd'] == 'digit') {
$this->fgItemRunDigit($fgItem);
return;
}
if ($fgItem['cmd'] == 'digitinvalid') {
$this->fgItemRunDigitInvalid($fgItem);
return;
}
if ($fgItem['cmd'] == 'expediente.check') {
$this->fgItemRunExpedienteCheck($fgItem);
return;
}
if ($fgItem['cmd'] == 'expediente.false') {
$this->fgItemRunExpedienteFalse($fgItem);
return;
}
if ($fgItem['cmd'] == 'expediente.true') {
$this->fgItemRunExpedienteTrue($fgItem);
return;
}
if ($fgItem['cmd'] == 'feriado.check') {
$this->fgItemRunFeriadoCheck($fgItem);
return;
}
if ($fgItem['cmd'] == 'feriado.false') {
$this->fgItemRunFeriadoFalse($fgItem);
return;
}
if ($fgItem['cmd'] == 'feriado.true') {
$this->fgItemRunFeriadoTrue($fgItem);
return;
}
if ($fgItem['cmd'] == 'ivr.redirect') {
$this->fgItemRunIVRRedirect($fgItem);
return;
}
if ($fgItem['cmd'] == 'nomatch') {
$this->fgItemRunNoMatch($fgItem);
return;
}
if ($fgItem['cmd'] == 'pesqsat') {
$this->fgItemRunPesqSat($fgItem);
return;
}
if ($fgItem['cmd'] == 'playaudio') {
$this->fgItemRunPlayaudio($fgItem);
return;
}
if ($fgItem['cmd'] == 'queue') {
$this->fgItemRunQueue($fgItem);
return;
}
if ($fgItem['cmd'] == 'queue.waitingcount') {
$this->fgItemRunQueueWaitingCount($fgItem);
return;
}
if ($fgItem['cmd'] == 'queue.waitingcount-greater') {
$this->fgItemRunQueueWaitingCountGreater($fgItem);
return;
}
if ($fgItem['cmd'] == 'queue.withcallback') {
$this->fgItemRunQueueWithCallback($fgItem);
return;
}
if ($fgItem['cmd'] == 'queue.withcallback-noready') {
$this->fgItemRunQueueWithCallbackNoReady($fgItem);
return;
}
if ($fgItem['cmd'] == 'record') {
$this->fgItemRunRecord($fgItem);
return;
}
if ($fgItem['cmd'] == 'start') {
$this->fgItemRunStart($fgItem);
return;
}
if ($fgItem['cmd'] == 'timeout') {
$this->fgItemRunTimeout($fgItem);
return;
}
if ($fgItem['cmd'] == 'timeoutwithlimit') {
$this->fgItemRunTimeoutWithLimit($fgItem);
return;
}
if ($fgItem['cmd'] == 'timeoutwithlimit.limit') {
$this->fgItemRunTimeoutWithLimitLimit($fgItem);
return;
}
if ($fgItem['cmd'] == 'voicemail') {
$this->fgItemRunVoicemail($fgItem);
return;
}
if ($fgItem['cmd'] == 'whitelist.check') {
$this->fgItemRunWhitelistCheck($fgItem);
return;
}
if ($fgItem['cmd'] == 'whitelist.false') {
$this->fgItemRunWhitelistFalse($fgItem);
return;
}
if ($fgItem['cmd'] == 'whitelist.true') {
$this->fgItemRunWhitelistTrue($fgItem);
return;
}
if (isset($fgItem['defaultvalues']['cmd'])) {
if ($fgItem['defaultvalues']['cmd'] == 'dialplan.checkvar') {
$this->fgItemRunDialplanCheckvar($fgItem);
return;
}
if ($fgItem['defaultvalues']['cmd'] == 'dialplan.gosub') {
$this->fgItemRunDialplanGosub($fgItem);
return;
}
if ($fgItem['defaultvalues']['cmd'] == 'dialplan.setvar') {
$this->fgItemRunDialplanSetvar($fgItem);
return;
}
}
}
}
public function fgItemRunAgiExec($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunAgiExec<br><br>");
$scriptname = isset($fgItem['defaultvalues']['scriptname']) ? $fgItem['defaultvalues']['scriptname'] : '';
$value = isset($fgItem['value']) ? $fgItem['value'] : '';
$value2 = isset($fgItem['value2']) ? $fgItem['value2'] : '';
$value3 = isset($fgItem['value3']) ? $fgItem['value3'] : '';
$defaultvalues_value = isset($fgItem['defaultvalues']['value']) ? $fgItem['defaultvalues']['value'] : '';
$defaultvalues_value2 = isset($fgItem['defaultvalues']['value2']) ? $fgItem['defaultvalues']['value2'] : '';
$defaultvalues_value3 = isset($fgItem['defaultvalues']['value3']) ? $fgItem['defaultvalues']['value3'] : '';
$cmd = 'EXEC AGI ' . $scriptname . ',"' . $value . '","' . $value2 . '","' . $value3 . '","' . $defaultvalues_value . '","' . $defaultvalues_value2 . '","' . $defaultvalues_value3 . '"';
$response = $this->agiRunCmd($cmd);
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunBlacklistCheck($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunBlacklistCheck<br><br>");
if ($this->globais['isXampp']) {
$rnd = rand(1, 100);
$esta_na_lista = ($rnd > 50) ? true : false;
} else {
$esta_na_lista = $this->hitpbxCheckBlacklist($this->globais['callerid']);
}
$run_next = true;
$cmdTarget = $esta_na_lista ? 'blacklist.true' : 'blacklist.false';
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == $cmdTarget) {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
}
public function fgItemRunBlacklistFalse($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunBlacklistFalse<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunBlacklistTrue($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunBlacklistTrue<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunCalleridChange($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd('<hr>fgItemRunCalleridChange<br><br>');
$varname = 'CALLERID(num)';
if (isset($fgItem['value2'])) {
if (trim($fgItem['value2']) != '') {
$varname = 'CALLERID(' . $fgItem['value2'] . ')';
}
}
$varvalue = $fgItem['value'];
$cmd = 'SET VARIABLE ' . $varname . ' ' . $varvalue;
$response = $this->agiRunCmd($cmd);
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunCNPJCheck($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd('<hr>fgItemRunCNPJCheck<br><br>');
if ($this->globais['isXampp']) {
$rnd = rand(1, 100);
$validado = ($rnd > 50) ? true : false;
} else {
$validado = $this->validarCNPJ($this->globais['digitado']);
}
$run_next = true;
$cmdTarget = $validado ? 'cnpj.true' : 'cnpj.false';
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == $cmdTarget) {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
}
public function fgItemRunCNPJCheckFalse($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunCNPJCheckFalse<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunCNPJCheckTrue($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunCNPJCheckTrue<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunCondTempoCheck($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd('<hr>fgItemRunCondTempoCheck<br><br>');
if ($this->globais['isXampp']) {
$rnd = rand(1, 100);
$periodo_match = ($rnd > 50) ? true : false;
} else {
$periodo_match = false;
$periodo_id = isset($fgItem['value']) ? $fgItem['value'] : '';
$query = "SELECT * FROM tab_periodo ";
$query .= "WHERE id = '" . mysqli_real_escape_string($this->globais['db'], $periodo_id) . "' ";
$weekday = date('D');
if ($weekday == 'Mon') {
$query .= "AND (monday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($weekday == 'Tue') {
$query .= "AND (tuesday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($weekday == 'Wed') {
$query .= "AND (wednesday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($weekday == 'Thu') {
$query .= "AND (thursday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($weekday == 'Fri') {
$query .= "AND (friday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($weekday == 'Sat') {
$query .= "AND (saturday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($weekday == 'Sun') {
$query .= "AND (sunday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
$hhmmss = date("H:i:s");
$query .= "AND ((time_start <= '" . $hhmmss . "' AND time_stop >= '" . $hhmmss . "') OR (time_start = '00:00:00' AND time_stop = '00:00:00')) ";
$day = date('d');
$query .= "AND ((day_start <= '" . $day . "' AND day_stop >= '" . $day . "') OR (day_start = '0' AND day_stop = '0')) ";
$month = date('m');
$query .= "AND ((month_start <= '" . $month . "' AND month_stop >= '" . $month . "') OR (month_start = '0' AND month_stop = '0')) ";
$year = date('Y');
$query .= "AND ((year_start <= '" . $year . "' AND year_stop >= '" . $year . "') OR (year_start = '0' AND year_stop = '0')) ";
$query_result = mysqli_query($this->globais['db'], $query);
if (mysqli_num_rows($query_result) > 0) {
$periodo_match = true;
}
}
$run_next = true;
$cmdTarget = $periodo_match ? 'condtempo.true' : 'condtempo.false';
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == $cmdTarget) {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
}
public function fgItemRunCondTempoCheckFalse($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunCondTempoCheckFalse<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunCondTempoCheckTrue($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunCondTempoCheckTrue<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunCPFCheck($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd('<hr>fgItemRunCPFCheck<br><br>');
if ($this->globais['isXampp']) {
$rnd = rand(1, 100);
$validado = ($rnd > 50) ? true : false;
} else {
$validado = $this->validarCPF($this->globais['digitado']);
}
$run_next = true;
$cmdTarget = $validado ? 'cpf.true' : 'cpf.false';
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == $cmdTarget) {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
}
public function fgItemRunCPFCheckFalse($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunCPFCheckFalse<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunCPFCheckTrue($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunCPFCheckTrue<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunDialdigit($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunDialdigit<br><br>");
$run_next = true;
$tamanho = strlen($this->globais['digitado']);
if (($tamanho != 3) && ($tamanho != 4)) {
$this->fgItemRunNextFrom($fgItem);
$run_next = false;
}
if ($run_next) {
$queue = $this->hitpbxGetQueueFromNumeroExterno(array('numero_externo' => $this->globais['digitado']));
if (count($queue) > 0) {
$response = $this->agiRunQueue(array('id' => $queue['id']));
$this->fgItemRunNextFrom($fgItem);
$run_next = false;
}
}
if ($run_next) {
$dialcontacts = '';
$ramais_secretaria = $this->hitpbxGetRamaisSecretariaFromRamal($this->globais['digitado']);
if (count($ramais_secretaria)) {
foreach ($ramais_secretaria as $ramal) {
$cmd = 'GET FULL VARIABLE ${PJSIP_DIAL_CONTACTS(' . $ramal . ')}';
$response = $this->agiRunCmd($cmd);
if ($this->globais['isXampp']) {
$response = '200 result=1 (' . $ramal . ')';
}
$contacts = str_replace('200 result=1 (', '', $response);
$contacts = substr($contacts, 0, -1);
if ($dialcontacts != '') {
$dialcontacts .= '&';
}
$dialcontacts .= $contacts;
$cmd = 'GET FULL VARIABLE ${PJSIP_DIAL_CONTACTS(SIP' . $ramal . ')}';
$response = $this->agiRunCmd($cmd);
if ($this->globais['isXampp']) {
$response = '200 result=1 (SIP' . $ramal . ')';
}
$contacts = str_replace('200 result=1 (', '', $response);
$contacts = substr($contacts, 0, -1);
if ($dialcontacts != '') {
$dialcontacts .= '&';
}
$dialcontacts .= $contacts;
}
} else {
$cmd = 'GET FULL VARIABLE ${PJSIP_DIAL_CONTACTS(' . $this->globais['digitado'] . ')}';
$response = $this->agiRunCmd($cmd);
if ($this->globais['isXampp']) {
$response = '200 result=1 (' . $this->globais['digitado'] . ')';
}
$contacts = str_replace('200 result=1 (', '', $response);
$contacts = substr($contacts, 0, -1);
if ($dialcontacts != '') {
$dialcontacts .= '&';
}
$dialcontacts .= $contacts;
$cmd = 'GET FULL VARIABLE ${PJSIP_DIAL_CONTACTS(SIP' . $this->globais['digitado'] . ')}';
$response = $this->agiRunCmd($cmd);
if ($this->globais['isXampp']) {
$response = '200 result=1 (SIP' . $this->globais['digitado'] . ')';
}
$contacts = str_replace('200 result=1 (', '', $response);
$contacts = substr($contacts, 0, -1);
if ($dialcontacts != '') {
$dialcontacts .= '&';
}
$dialcontacts .= $contacts;
}
$cmd = 'EXEC Dial ' . $dialcontacts . ',30,TtU(update_call^' . $this->globais['callerid'] . '^' . $this->globais['digitado'] . '^' . $this->globais['uniqueid'] . ') ""';
$response = $this->agiRunCmd($cmd);
$this->globais['numerodiscado'] = $dialcontacts;
$cmd = 'GET VARIABLE DIALSTATUS';
$response = $this->agiRunCmd($cmd);
if ($this->globais['isXampp']) {
$rnd = rand(1, 100);
if ($rnd >= 50) {
$response = '200 result=1 (ANSWER)';
} else {
$response = '200 result=1 (NO ANSWER)';
}
}
$explodido = explode("(", $response);
if (count($explodido) > 1) {
$dialstatus = substr($explodido[1], 0, -1);
} else {
$dialstatus = '';
}
$this->globais['dialstatus'] = $dialstatus;
if ($dialstatus == 'ANSWER') {
$this->fgItemRunNextFrom($fgItem);
} else {
$desvio = $this->hitpbxGetDesvioFrom(array('tipo' => 'ramal', 'value' => $this->globais['digitado']));
if (count($desvio)) {
$run_next = false;
if ($desvio['shortname'] == 'numeroexterno') {
$return = $this->agiRunDialNumeroExterno($desvio['numero']);
}
if ($desvio['shortname'] == 'queue') {
$queuestatus = $this->agiRunQueue(array('id' => $desvio['queue']));
if ($queuestatus == '') {
$return = 'ANSWER';
}
}
if ($desvio['shortname'] == 'ramal') {
$return = $this->agiRunDialRamal($desvio['ramal']);
}
if ($desvio['shortname'] == 'voicemail') {
$this->agiRunVoicemail($desvio['ramal']);
$return = 'ANSWER';
}
if ($desvio['shortname'] == 'numeroexterno') {
$this->agiRunDialNumeroExterno($desvio['numero']);
}
}
if ($run_next) {
$cmd = 'EXEC PLAYBACK /var/lib/asterisk/sounds/pt_BR/exten-unavail ""';
$response = $this->agiRunCmd($cmd);
$run_next = true;
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'timeoutwithlimit') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'timeout') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'voicemail') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
if ($run_next) {
$this->fgItemRunNextFrom($fgItem);
}
}
}
}
}
public function fgItemRunDialplanCheckvar($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunDialplanCheckvar<br><br>");
$nomatch = true;
$run_next = true;
$fgItemMaster = $this->fgItemGetMasterFrom($fgItem);
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItemMaster);
foreach ($fgItemChildren as $fgItemChild) {
if (($run_next) && ($this->fgItemIsDialplanCheckvar($fgItemChild))) {
$varname = (isset($fgItemChild['defaultvalues']['varname'])) ? $fgItemChild['defaultvalues']['varname'] : '';
$varvalue = (isset($fgItemChild['defaultvalues']['varvalue'])) ? $fgItemChild['defaultvalues']['varvalue'] : '';
if (isset($fgItemChild['value2'])) {
if ($fgItemChild['value2'] != '') {
$varname = $fgItemChild['value2'];
}
}
if (isset($fgItemChild['value'])) {
if ($fgItemChild['value'] != '') {
$varvalue = $fgItemChild['value'];
}
}
if (isset($fgItemChild['varname'])) {
$varname = $fgItemChild['varname'];
}
if (isset($fgItemChild['varvalue'])) {
$varvalue = $fgItemChild['varvalue'];
}
$response = $this->agiVarGet(strtoupper($varname));
if ($this->globais['isXampp']) {
$rnd = rand(1, 100);
if ($rnd >= 50) {
$response = 'ABC';
} else {
$response = 'XYZ';
}
}
if ($response == $varvalue) {
$this->fgItemRunNextFrom($fgItemChild);
$nomatch = false;
$run_next = false;
}
}
}
if ($nomatch) {
$run_next = true;
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItemMaster);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'nomatch') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
}
}
public function fgItemRunDialExtension($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunDialExtension<br><br>");
$dialstatus = $this->agiRunDialRamal($fgItem['value']);
if ($this->globais['isXampp']) {
$this->globais['numerodiscado'] = $fgItem['value'];
$rnd = rand(1, 100);
if ($rnd >= 50) {
$dialstatus = 'ANSWER';
} else {
$dialstatus = 'NO ANSWER';
}
}
if ($dialstatus != 'ANSWER') {
$run_next = true;
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'timeoutwithlimit') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'timeout') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'voicemail') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
if ($run_next) {
$this->fgItemRunNextFrom($fgItem);
}
} else {
$this->fgItemRunNextFrom($fgItem);
}
}
public function fgItemRunDialGroupExtension($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunDialGroupExtension<br><br>");
$dialstatus = $this->agiRunDialRamalGrupo($fgItem['value']);
if ($this->globais['isXampp']) {
$this->globais['numerodiscado'] = '1111';
$rnd = rand(1, 100);
if ($rnd >= 50) {
$dialstatus = 'ANSWER';
} else {
$dialstatus = 'NO ANSWER';
}
}
if ($dialstatus != 'ANSWER') {
$run_next = true;
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'timeoutwithlimit') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'timeout') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'voicemail') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
if ($run_next) {
$this->fgItemRunNextFrom($fgItem);
}
} else {
$this->fgItemRunNextFrom($fgItem);
}
}
public function fgItemRunDialplanGosub($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunDialplanGosub<br><br>");
$ctxname = isset($fgItem['defaultvalues']['ctxname']) ? $fgItem['defaultvalues']['ctxname'] : '';
$ctxname = isset($fgItem['ctxname']) ? $fgItem['ctxname'] : $ctxname;
$ctxexten = isset($fgItem['defaultvalues']['ctxexten']) ? $fgItem['defaultvalues']['ctxexten'] : '1001';
$ctxexten = isset($fgItem['ctxexten']) ? $fgItem['ctxexten'] : $ctxexten;
if (isset($fgItem['value'])) {
if ($fgItem['value'] != '') {
$ctxexten = $fgItem['value'];
}
}
if (($ctxname != '') && ($ctxexten != '')) {
$cmd = 'GOSUB ' . $ctxname . ' ' . $ctxexten . ' 1';
$response = $this->agiRunCmd($cmd);
}
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunDialplanSetvar($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunDialplanSetvar<br><br>");
$varnameget = isset($fgItem['defaultvalues']['varnameget']) ? $fgItem['defaultvalues']['varnameget'] : 'AGIDIGITADO';
$varnameget = isset($fgItem['value2']) ? $fgItem['value2'] : $varnameget;
$varnameget = isset($fgItem['varnameget']) ? $fgItem['varnameget'] : $varnameget;
$varnameset = isset($fgItem['defaultvalues']['varname']) ? $fgItem['defaultvalues']['varname'] : '';
$varnameset = isset($fgItem['defaultvalues']['varnameset']) ? $fgItem['defaultvalues']['varnameset'] : $varnameset;
$varnameset = isset($fgItem['value3']) ? $fgItem['value3'] : $varnameset;
$varnameset = isset($fgItem['varnameset']) ? $fgItem['varnameset'] : $varnameset;
$varvalue = isset($fgItem['value']) ? $fgItem['value'] : '';
if ($varvalue == '') {
if ($varnameget != '') {
$varvalue = $this->agiVarGet(strtoupper($varnameget));
if ($this->globais['isXampp']) {
$varvalue = 'ABC';
}
}
}
if ($varvalue != '') {
$response = $this->agiVarSet(strtoupper($varnameset), $varvalue);
} else {
$cmd = 'SET VARIABLE ' . strtoupper($varnameset) . ' ""';
$response = $this->agiRunCmd($cmd);
}
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunDigit($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd('<hr>fgItemRunDigit<br><br>');
// queue_log
// 1353461660|1353461627.33271|NONE|NONE|INFO|IVRAPPEND|1|ivr-2
// QueueLog(queuename,uniqueid,agent,event,[additionalinfo])
// $cmd = 'EXEC QUEUELOG NONE,' . $GLOBALS['uniqueid'] . ',NONE,NONE,INFO|IVRAPPEND|' . $fgItem['value'] . '|' . $GLOBALS['fgId'];
// $result[] = $cmd;
// $response = agiRunCmd($cmd);
// $result[] = $response;
/*
$cmd = 'EXEC QUEUELOG NONE,' . $GLOBALS['uniqueid'] . ',NONE,NONE,INFO|IVRAPPEND|' . $GLOBALS['digitado'];
$result[] = $cmd;
$response = agiRunCmd($cmd);
$result[] = $response;
*/
/*
$cmd = 'EXEC QUEUELOG NONE,' . $GLOBALS['uniqueid'] . ',NONE,INFO|IVRAPPEND|' . $GLOBALS['digitado'];
$result[] = $cmd;
$response = agiRunCmd($cmd);
$result[] = $response;
*/
// ...
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunDigitInvalid($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunDigitInvalid<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunExpedienteCheck($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunExpedienteCheck<br><br>");
$arrDay = array(
'0' => array('dom_inicio', 'dom_fim'),
'1' => array('seg_inicio', 'seg_fim'),
'2' => array('ter_inicio', 'ter_fim'),
'3' => array('qua_inicio', 'qua_fim'),
'4' => array('qui_inicio', 'qui_fim'),
'5' => array('sex_inicio', 'sex_fim'),
'6' => array('sab_inicio', 'sab_fim'),
);
$w = date("w");
$agora = date("H:i:s");
$query = "SELECT * FROM tab_expediente ";
$query .= "WHERE " . $arrDay[$w][0] . " <= '" . $agora . "' AND " . $arrDay[$w][1] . " >= '" . $agora . "' ";
if (isset($fgItem['value'])) {
if ($fgItem['value'] != '') {
$query .= "AND id = '" . mysqli_real_escape_string($this->globais['db'], $fgItem['value']) . "'";
}
}
$query_result = mysqli_query($this->globais['db'], $query);
if (mysqli_num_rows($query_result) > 0) {
$isTrue = true;
} else {
$isTrue = false;
}
if ($this->globais['isXampp']) {
$rnd = rand(1, 100);
$isTrue = ($rnd >= 50);
}
$run_next = true;
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
$cmdTarget = $isTrue ? 'expediente.true' : 'expediente.false';
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == $cmdTarget) {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
}
public function fgItemRunExpedienteFalse($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunExpedienteFalse<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunExpedienteTrue($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunExpedienteTrue<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunFeriadoCheck($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunFeriadoCheck<br><br>");
$hojeDia = date("d");
$hojeMes = date("m");
$query = "SELECT * FROM tab_feriado WHERE mes = '" . $hojeMes . "' AND dia = '" . $hojeDia . "'";
$query_result = mysqli_query($this->globais['db'], $query);
$isTrue = (mysqli_num_rows($query_result) > 0);
if ($this->globais['isXampp']) {
$rnd = rand(1, 100);
$isTrue = ($rnd >= 50);
}
$run_next = true;
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
$cmdTarget = $isTrue ? 'feriado.true' : 'feriado.false';
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == $cmdTarget) {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
}
public function fgItemRunFeriadoFalse($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunFeriadoFalse<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunFeriadoTrue($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunFeriadoTrue<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunIVRRedirect($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunIVRRedirect<br><br>");
$this->globais['fgId'] = $fgItem['value'];
$this->fgGet();
if (count($this->globais['fgItemStart']) > 0) {
$this->fgItemRunNextFrom($this->globais['fgItemStart']);
}
}
public function fgItemRunNextFrom($fgItemFrom = array())
{
// $this->agiLog("===> " . __FUNCTION__ . " <===");
// $this->agiLog(print_r($fgItemFrom, true));
$cmds_nonext = array();
$cmds_nonext[] = 'blacklist.false';
$cmds_nonext[] = 'blacklist.true';
$cmds_nonext[] = 'cnpf.false';
$cmds_nonext[] = 'cnpf.true';
$cmds_nonext[] = 'condtempo.false';
$cmds_nonext[] = 'condtempo.true';
$cmds_nonext[] = 'cpf.false';
$cmds_nonext[] = 'cpf.true';
$cmds_nonext[] = 'dialdigit';
$cmds_nonext[] = 'digit';
$cmds_nonext[] = 'expediente.false';
$cmds_nonext[] = 'expediente.true';
$cmds_nonext[] = 'feriado.false';
$cmds_nonext[] = 'feriado.true';
$cmds_nonext[] = 'nomatch';
$cmds_nonext[] = 'queue.waitingcount-greater';
$cmds_nonext[] = 'queue.withcallback-noready';
$cmds_nonext[] = 'timeout';
$cmds_nonext[] = 'timeoutwithlimit';
$cmds_nonext[] = 'timeoutwithlimit.limit';
$cmds_nonext[] = 'voicemail';
$cmds_nonext[] = 'whitelist.false';
$cmds_nonext[] = 'whitelist.true';
$run_next = true;
if (isset($fgItemFrom['idnext'])) {
if (isset($this->globais['fgItens'][$fgItemFrom['idnext']])) {
$this->fgItemRun($this->globais['fgItens'][$fgItemFrom['idnext']]);
$run_next = false;
}
}
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItemFrom);
foreach ($fgItemChildren as $fgItemChild) {
if (($run_next) && (!in_array($fgItemChild['cmd'], $cmds_nonext))) {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
public function fgItemRunNoMatch($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunNoMatch<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunPesqSat($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunPesqSat ???<br><br>");
$id_pesqsat = is_numeric($fgItem['value']) ? $fgItem['value'] : '';
$audiofile = '';
if ($this->globais['isXampp']) {
$audiofile = 'pesquisa';
} else {
$query = "INSERT INTO tab_pesqsat_nota SET ";
$query .= "id_pesqsat = '" . mysqli_real_escape_string($this->globais['db'], $id_pesqsat) . "', ";
$query .= "uniqueid = '" . mysqli_real_escape_string($this->globais['db'], $this->globais['uniqueid']) . "', ";
$query .= "nota = '100'";
$query_result = mysqli_query($this->globais['db'], $query);
$query = "SELECT audio FROM tab_pesqsat WHERE id = '" . mysqli_real_escape_string($this->globais['db'], $id_pesqsat) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
if (mysqli_num_rows($query_result) > 0) {
$resultLinha = mysqli_fetch_assoc($query_result);
$explodido = explode('.', $resultLinha['audio']);
$audiofile = $explodido[0];
}
}
if ($audiofile != '') {
$digito_timeout = '2000'; // milisegundos
$digito_qtd = '2';
$this->globais['digitado'] = '';
$cmd = 'SET VARIABLE AGIDIGITADO ""';
$response = $this->agiRunCmd($cmd);
$cmd = 'GET DATA /var/lib/asterisk/sounds/cliente/' . $audiofile . ' ' . $digito_timeout . ' ' . $digito_qtd;
$response = $this->agiRunCmd($cmd);
/*
if (strpos($response,'= (timeout)') === false) {
$explodido = explode('=',$response);
if (isset($explodido[1])) {
$GLOBALS['digitado'] = $explodido[1];
}
}
*/
// 200 result= (timeout)
// 200 result=4 (timeout)
if (strpos($response, '= (timeout)') === false) {
if (strpos($response, '(timeout)') !== false) {
$response = trim(str_replace('(timeout)', '', $response));
}
$explodido = explode('=', $response);
$this->globais['digitado'] = $explodido[1];
}
$run_next = true;
if ($this->globais == '') {
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'timeoutwithlimit') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'timeout') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
$run_next = false;
}
if ($run_next) {
if (!$this->globais['isXampp']) {
$query = "UPDATE tab_pesqsat_nota SET ";
$query .= "nota = '" . mysqli_real_escape_string($this->globais['db'], $this->globais['digitado']) . "' ";
$query .= "WHERE id_pesqsat = '" . mysqli_real_escape_string($this->globais['db'], $id_pesqsat) . "' ";
$query .= "AND uniqueid = '" . mysqli_real_escape_string($this->globais['db'], $this->globais['uniqueid']) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
}
$this->fgItemRunNextFrom($fgItem);
}
} else {
$this->fgItemRunNextFrom($fgItem);
}
}
public function fgItemRunPlayaudio($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->globais['digitado'] = '';
$cmd = 'SET VARIABLE AGIDIGITADO ""';
$response = $this->agiRunCmd($cmd);
if ($this->globais['isXampp']) {
$rnd = rand(1, 100);
if ($rnd >= 60) {
$this->globais['digitado'] = rand(0, 9);
} else if ($rnd >= 40) {
$this->globais['digitado'] = 'A';
} else if ($rnd >= 20) {
$this->globais['digitado'] = '1001';
} else {
$this->globais['digitado'] = '';
}
} else {
// Nome do arquivo sem a extensão
$explodido = explode('.', $fgItem['value']);
$audiofile = $explodido[0];
$digito_timeout = isset($fgItem['value2']) ? trim($fgItem['value2']) : 2;
$digito_timeout = !empty($digito_timeout) ? $digito_timeout : 0;
$digito_timeout = $digito_timeout * 1000; // segundos para milisegundos
$digito_qtd = isset($fgItem['value3']) ? $fgItem['value3'] : 1;
$digito_qtd = (!empty($digito_qtd)) ? $digito_qtd : '1';
// $digito_say = isset($fgItem['value4']) ? $fgItem['value4'] : 'false';
$cmd = 'GET DATA /var/lib/asterisk/sounds/cliente/' . $audiofile . ' ' . $digito_timeout . ' ' . $digito_qtd;
$response = $this->agiRunCmd($cmd);
// 200 result= (timeout)
// 200 result=4 (timeout)
if (strpos($response, '= (timeout)') === false) {
if (strpos($response, '(timeout)') !== false) {
$response = trim(str_replace('(timeout)', '', $response));
}
$explodido = explode('=', $response);
$this->globais['digitado'] = $explodido[1];
}
}
$run_next = true;
if ($this->globais['digitado'] == '') {
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'timeoutwithlimit') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'timeout') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
if ($run_next) {
$this->fgItemRunNextFrom($fgItem);
}
$run_next = false;
} else {
$cmd = 'SET VARIABLE AGIDIGITADO ' . $this->globais['digitado'];
$response = $this->agiRunCmd($cmd);
$cmd = 'EXEC QUEUELOG NONE,' . $this->globais['uniqueid'] . ',NONE,INFO|IVRAPPEND|->' . $this->globais['digitado'];
$response = $this->agiRunCmd($cmd);
}
if ($run_next) {
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'digit') {
if ($fgItemChild['value'] == $this->globais['digitado']) {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
}
}
if ($run_next) {
$tamanho = strlen($this->globais['digitado']);
if (($tamanho >= 3) && ($tamanho <= 4)) {
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'dialdigit') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
}
}
if ($run_next) {
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'digitinvalid') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
}
if ($run_next) {
$this->fgItemRunNextFrom($fgItem);
}
}
public function fgItemRunQueue($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunQueue<br><br>");
$timeout = isset($fgItem['value2']) ? $fgItem['value2'] : '';
$timeout = is_numeric($timeout) ? $timeout : '';
/*
$id_queue_callback = isset($fgItem['value2']) ? $fgItem['value2'] : '';
$cmd = 'SET VARIABLE DIDCALLBACKQUEUEID ' . $id_queue_callback;
$result[] = $cmd;
$response = agiRunCmd($cmd);
$result[] = $response;
*/
$run_next = true;
// same => n,QueueLog(Q21,${UNIQUEID},NONE,INFO,IVRSTART)
// same => n,QueueLog(Q21,${UNIQUEID},NONE,INFO,IVRAPPEND|DDR-FILA-INSUMOS-TUPA)
/*
$cmd = 'EXEC QUEUELOG Q'.$fgItem['value'].',' . $GLOBALS['uniqueid'] . ',NONE,NONE,INFO|IVRAPPEND|' . $GLOBALS['digitado'];
$result[] = $cmd;
$response = agiRunCmd($cmd);
$result[] = $response;
*/
/*
$cmd = 'EXEC QUEUELOG Q'.$fgItem['value'].',' . $GLOBALS['uniqueid'] . ',NONE,INFO|IVRAPPEND|' . $GLOBALS['digitado'];
$result[] = $cmd;
$response = agiRunCmd($cmd);
$result[] = $response;
*/
// ...
$response = $this->agiRunQueue(array('id' => $fgItem['value'], 'timeout' => $timeout));
if ($response == 'stopivr') {
$run_next = false;
}
if (($run_next) && ($response == 'timeout')) {
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'timeoutwithlimit') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'timeout') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
$run_next = false;
}
if ($run_next) {
$this->fgItemRunNextFrom($fgItem);
}
}
public function fgItemRunQueueWaitingCount($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunQueueWaitingCount<br><br>");
$id_queue = is_numeric($fgItem['value']) ? 'Q' . $fgItem['value'] : $fgItem['value'];
$cmd = 'GET FULL VARIABLE ${QUEUE_WAITING_COUNT(' . $id_queue . ')}';
$response = $this->agiRunCmd($cmd);
if ($this->globais['isXampp']) {
$rnd = rand(0, 20);
$response = '200 result=1 (' . $rnd . ')';
}
$waiting_count = '';
if (substr($response, 0, 14) == '200 result=1 (') {
$explodido = explode('(', $response);
$waiting_count = substr($explodido[1], 0, -1);
}
$run_next = true;
if ($waiting_count == '') {
$run_next = false;
}
if ($run_next) {
$arr = array();
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($fgItemChild['cmd'] == 'queue.waitingcount-greater') {
$arr[$fgItemChild['value']] = $fgItemChild['id'];
}
}
if (count($arr) > 0) {
ksort($arr);
$fgItemNextId = end($arr);
$this->fgItemRun($this->globais['fgItens'][$fgItemNextId]);
$run_next = false;
}
}
if ($run_next) {
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'nomatch') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
}
}
public function fgItemRunQueueWaitingCountGreater($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunQueueWaitingCountGreater<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunQueueWithCallback($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$id_queue_callback = isset($fgItem['value2']) ? $fgItem['value2'] : '';
if ($id_queue_callback == '') {
$cmd = 'SET VARIABLE AGICALLBACKQUEUEID ""';
} else {
$cmd = 'SET VARIABLE AGICALLBACKQUEUEID ' . $id_queue_callback;
}
$response = $this->agiRunCmd($cmd);
$timeout = isset($fgItem['value3']) ? $fgItem['value3'] : '';
$timeout = is_numeric($timeout) ? $timeout : '';
$run_next = true;
$queue_status = '';
$cmd = 'GET FULL VARIABLE ${QUEUE_MEMBER(Q' . $fgItem['value'] . ',ready)}';
$response = $this->agiRunCmd($cmd);
if ($this->globais['isXampp']) {
$rnd = rand(1, 100);
if ($rnd >= 50) {
$response = '200 result=1 (0)';
}
}
if (substr($response, 0, 14) == '200 result=1 (') {
$qtd = str_replace('200 result=1 (', '', $response);
$qtd = substr($qtd, 0, -1);
if ($qtd == '0') {
$queue_status = 'full';
}
}
if ($queue_status == 'full') {
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'queue.withcallback-noready') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
}
if ($run_next) {
// logAdd('===> agiRunQueue<br>');
$queue_status = $this->agiRunQueue(array('id' => $fgItem['value'], 'timeout' => $timeout));
if ($queue_status == 'stopivr') {
// logAdd('===> agiRunQueue stopivr<br>');
$run_next = false;
}
}
if (($run_next) && ($queue_status == 'timeout')) {
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'timeoutwithlimit') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'timeout') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
$run_next = false;
}
if ($run_next) {
$this->fgItemRunNextFrom($fgItem);
}
}
public function fgItemRunQueueWithCallbackNoReady($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunQueueWithCallbackNoReady<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunRecord($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunRecord<br><br>");
$uniqueid = $this->globais['uniqueid'];
$callerid = $this->globais['callerid'];
$exten = $this->globais['fgId'];
//$cmd = 'EXEC MixMonitor IN_'.date("dmY").'_'.date("H-i-s").'_'.$uniqueid.'_'.$callerid.'_'.$exten.'.wav';
//$cmd = 'EXEC MixMonitor IN_2023-10-31_10-05-01_123456.78_2001_2221.wav';
//$cmd = 'EXEC MixMonitor IN_'.date("Y-m-d").'_'.date("H-i-s").'_'.$uniqueid.'_'.$callerid.'_'.$exten.'.wav';
$cmd = 'EXEC MixMonitor IN_' . date("Y-m-d") . '_' . date("H-i-s") . '_' . $uniqueid . '_' . $callerid . '_' . $exten . '.ogg';
$response = $this->agiRunCmd($cmd);
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunStart($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
// queue_log
// 1353461650|1353461627.33271|NONE|NONE|INFO|IVRSTART|1234|5556777
// QueueLog(queuename,uniqueid,agent,event,[additionalinfo])
// $cmd = 'EXEC QUEUELOG NONE,' . $GLOBALS['uniqueid'] . ',NONE,NONE,INFO|IVRSTART|' . $GLOBALS['callerid'] . '|' . $GLOBALS['fgId'];
// ...
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunTimeout($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$run_next = true;
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'voicemail') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
if ($run_next) {
$this->fgItemRunNextFrom($fgItem);
}
}
public function fgItemRunTimeoutWithLimit($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunTimeoutWithLimit<br><br>");
$run_next = true;
if (!isset($this->globais['fgItemExecutedCount'][$fgItem['id']])) {
$this->globais['fgItemExecutedCount'][$fgItem['id']] = 0;
}
$this->globais['fgItemExecutedCount'][$fgItem['id']]++;
if ($this->globais['fgItemExecutedCount'][$fgItem['id']] >= $fgItem['value']) {
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == 'timeoutwithlimit.limit') {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
$run_next = false;
}
if ($run_next) {
$this->fgItemRunNextFrom($fgItem);
}
}
public function fgItemRunTimeoutWithLimitLimit($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunTimeoutWithLimitLimit<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunVoicemail($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunVoicemail<br><br>");
if (isset($this->globais['numerodiscado'])) {
if ($this->globais['numerodiscado'] != '') {
$cmd = 'EXEC VOICEMAIL ' . $this->globais['numerodiscado'];
$response = $this->agiRunCmd($cmd);
}
}
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunWhitelistCheck($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunWhitelistCheck<br><br>");
$esta_na_lista = $this->hitpbxCheckWhitelist($GLOBALS['callerid']);
if ($this->globais['isXampp']) {
$rnd = rand(1, 100);
$esta_na_lista = ($rnd >= 50);
}
$run_next = true;
$fgItemChildren = $this->fgItemGetChildrenFrom($fgItem);
$cmdTarget = $esta_na_lista ? 'whitelist.true' : 'whitelist.false';
foreach ($fgItemChildren as $fgItemChild) {
if ($run_next) {
if ($fgItemChild['cmd'] == $cmdTarget) {
$this->fgItemRun($fgItemChild);
$run_next = false;
}
}
}
}
public function fgItemRunWhitelistFalse($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunWhitelistFalse<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
public function fgItemRunWhitelistTrue($fgItem = array())
{
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($fgItem, true));
$this->logAdd("<hr>fgItemRunWhitelistTrue<br><br>");
$this->fgItemRunNextFrom($fgItem);
}
// --------------------------------------------------------------------------------------------------------------------------------
public function hitpbxCheckAuditoria($args = array())
{
$args['ramal'] = isset($args['ramal']) ? $args['ramal'] : '';
$args['ramais'] = isset($args['ramais']) ? $args['ramais'] : array();
$args['idqueue'] = isset($args['idqueue']) ? $args['idqueue'] : '';
if ($args['ramal'] != '') {
$args['ramais'][] = $args['ramal'];
}
$ramal_ids = array();
foreach ($args['ramais'] as $ramal) {
$query = "SELECT id FROM tab_ramal WHERE ramal = '" . mysqli_real_escape_string($this->globais['db'], $ramal) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
if (mysqli_num_rows($query_result)) {
$resultLinha = mysqli_fetch_assoc($query_result);
$ramal_ids[] = $resultLinha['id'];
}
}
$auditorias = array();
foreach ($ramal_ids as $idramal) {
$query = "SELECT * FROM tab_auditoria WHERE gravar = '1' AND id_origem_tab_ramal = '" . mysqli_real_escape_string($this->globais['db'], $idramal) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
while ($resultLinha = mysqli_fetch_assoc($query_result)) {
$auditorias[] = $resultLinha;
}
}
$query = "SELECT * FROM tab_auditoria WHERE gravar = '1' AND id_origem_tab_queue = '" . mysqli_real_escape_string($this->globais['db'], $args['idqueue']) . "' ";
$query_result = mysqli_query($this->globais['db'], $query);
while ($resultLinha = mysqli_fetch_assoc($query_result)) {
$auditorias[] = $resultLinha;
}
$periodo_match = false;
foreach ($auditorias as $auditoria) {
$periodo_match = $this->hitpbxCheckPeriodoMatch(array('id_periodo' => $auditoria['id_tab_condicao_tempo']));
}
return $periodo_match;
}
public function hitpbxCheckBlacklist($callerid = '')
{
$return = false;
//$query = "SELECT id FROM tab_blacklist WHERE telefone = '" . mysqli_real_escape_string($GLOBALS['db'],$callerid) . "'";
$query = "SELECT id FROM tab_blacklist WHERE CAST(REGEXP_REPLACE(telefone, '[^0-9]', '') AS SIGNED) = '" . mysqli_real_escape_string($this->globais['db'], $callerid) . "'";
if (($return != true) && ((strlen($callerid) == 12) || (strlen($callerid) == 13))) {
// Exemplos de callerid: 551133224455 ou 5511988887777
$tmp = '(' . substr($callerid, 2, 2) . ')' . substr($callerid, 4);
$query = "SELECT id FROM tab_blacklist WHERE telefone = '" . mysqli_real_escape_string($this->globais['db'], $tmp) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
if (mysqli_num_rows($query_result) > 0) {
$return = true;
}
}
if (($return != true) && ((strlen($callerid) == 10) || (strlen($callerid) == 11))) {
// Exemplos de callerid: 1133224455 ou 11988887777
$tmp = '55(' . substr($callerid, 0, 2) . ')' . substr($callerid, 2);
$query = "SELECT id FROM tab_blacklist WHERE telefone = '" . mysqli_real_escape_string($this->globais['db'], $tmp) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
if (mysqli_num_rows($query_result) > 0) {
$return = true;
}
}
$query_result = mysqli_query($this->globais['db'], $query);
if (mysqli_num_rows($query_result) > 0) {
$return = true;
}
return $return;
}
public function hitpbxCheckPeriodoMatch($args = array())
{
$return = false;
$args['id_periodo'] = isset($args['id_periodo']) ? $args['id_periodo'] : '';
$args['weekday'] = isset($args['weekday']) ? $args['weekday'] : date('D');
$args['hhmmss'] = isset($args['hhmmss']) ? $args['hhmmss'] : date("H:i:s");
$args['day'] = isset($args['day']) ? $args['day'] : date("d");
$args['month'] = isset($args['month']) ? $args['month'] : date("m");
$args['year'] = isset($args['year']) ? $args['year'] : date("Y");
if ($args['id_periodo'] != '') {
$query = "SELECT id FROM tab_periodo ";
$query .= "WHERE id = '" . mysqli_real_escape_string($this->globais['db'], $args['id_periodo']) . "'";
if ($args['weekday'] == 'Mon') {
$query .= "AND (monday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($args['weekday'] == 'Tue') {
$query .= "AND (tuesday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($args['weekday'] == 'Wed') {
$query .= "AND (wednesday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($args['weekday'] == 'Thu') {
$query .= "AND (thursday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($args['weekday'] == 'Fri') {
$query .= "AND (friday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($args['weekday'] == 'Sat') {
$query .= "AND (saturday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($args['weekday'] == 'Sun') {
$query .= "AND (sunday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
$query .= "AND ((time_start <= '" . $args['hhmmss'] . "' AND time_stop >= '" . $args['hhmmss'] . "') OR (time_start = '00:00:00' AND time_stop = '00:00:00')) ";
$query .= "AND ((day_start <= '" . $args['day'] . "' AND day_stop >= '" . $args['day'] . "') OR (day_start = '0' AND day_stop = '0')) ";
$query .= "AND ((month_start <= '" . $args['month'] . "' AND month_stop >= '" . $args['month'] . "') OR (month_start = '0' AND month_stop = '0')) ";
$query .= "AND ((year_start <= '" . $args['year'] . "' AND year_stop >= '" . $args['year'] . "') OR (year_start = '0' AND year_stop = '0')) ";
$query_result = mysqli_query($this->globais['db'], $query);
if (mysqli_num_rows($query_result) > 0) {
$return = true;
}
}
return $return;
}
public function hitpbxCheckWhitelist($callerid = '')
{
$return = false;
//$query = "SELECT id FROM tab_whitelist WHERE telefone = '" . mysqli_real_escape_string($GLOBALS['db'],$callerid) . "'";
$query = "SELECT id FROM tab_whitelist WHERE CAST(REGEXP_REPLACE(telefone, '[^0-9]', '') AS SIGNED) = '" . mysqli_real_escape_string($this->globais['db'], $callerid) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
if (mysqli_num_rows($query_result) > 0) {
$return = true;
}
if (($return != true) && ((strlen($callerid) == 12) || (strlen($callerid) == 13))) {
// Exemplos de callerid: 551133224455 ou 5511988887777
$tmp = '(' . substr($callerid, 2, 2) . ')' . substr($callerid, 4);
$query = "SELECT id FROM tab_whitelist WHERE telefone = '" . mysqli_real_escape_string($this->globais['db'], $tmp) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
if (mysqli_num_rows($query_result) > 0) {
$return = true;
}
}
if (($return != true) && ((strlen($callerid) == 10) || (strlen($callerid) == 11))) {
// Exemplos de callerid: 1133224455 ou 11988887777
$tmp = '55(' . substr($callerid, 0, 2) . ')' . substr($callerid, 2);
$query = "SELECT id FROM tab_whitelist WHERE telefone = '" . mysqli_real_escape_string($this->globais['db'], $tmp) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
if (mysqli_num_rows($query_result) > 0) {
$return = true;
}
}
return $return;
}
public function hitpbxGetCallbackFromRamal($ramal = '')
{
$return = '';
$query = "SELECT callback FROM tab_ramal WHERE ramal = '" . mysqli_real_escape_string($this->globais['db'], $ramal) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
if (mysqli_num_rows($query_result) > 0) {
$resultLinha = mysqli_fetch_assoc($query_result);
$return = $resultLinha['callback'];
}
return $return;
}
public function hitpbxGetDesvioFrom($args = array())
{
$return = array();
$args['tipo'] = $args['tipo'] ? $args['tipo'] : ''; // shortname
$args['value'] = $args['value'] ? $args['value'] : '';
$origem_tipo_id = '';
$query = "SELECT id FROM tab_desvio_origem_tipo WHERE shortname = '" . mysqli_real_escape_string($this->globais['db'], $args['tipo']) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
while ($resultLinha = mysqli_fetch_assoc($query_result)) {
$origem_tipo_id = $resultLinha['id'];
}
$origem_id = $args['value'];
if ($args['tipo'] == 'ramal') {
$query = "SELECT id FROM tab_ramal WHERE ramal = '" . mysqli_real_escape_string($this->globais['db'], $args['value']) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
while ($resultLinha = mysqli_fetch_assoc($query_result)) {
$origem_id = $resultLinha['id'];
}
}
$query = "SELECT tab_desvio.periodo_id, tab_desvio.destino_id, tab_desvio_destino_tipo.shortname ";
$query .= "FROM tab_desvio ";
$query .= "INNER JOIN tab_desvio_destino_tipo ON tab_desvio_destino_tipo.id = tab_desvio.destino_tipo_id ";
$query .= "WHERE tab_desvio.origem_tipo_id = '" . mysqli_real_escape_string($this->globais['db'], $origem_tipo_id) . "'";
$query .= "AND tab_desvio.origem_id = '" . mysqli_real_escape_string($this->globais['db'], $origem_id) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
while ($resultLinha = mysqli_fetch_assoc($query_result)) {
$periodo_match = false;
if (empty($resultLinha['periodo_id'])) {
$periodo_match = true;
} else {
$query2 = "SELECT * FROM tab_periodo ";
$query2 .= "WHERE id = '" . mysqli_real_escape_string($this->globais['db'], $resultLinha['periodo_id']) . "' ";
$weekday = date('D');
if ($weekday == 'Mon') {
$query2 .= "AND (monday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($weekday == 'Tue') {
$query2 .= "AND (tuesday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($weekday == 'Wed') {
$query2 .= "AND (wednesday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($weekday == 'Thu') {
$query2 .= "AND (thursday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($weekday == 'Fri') {
$query2 .= "AND (friday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($weekday == 'Sat') {
$query2 .= "AND (saturday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
if ($weekday == 'Sun') {
$query2 .= "AND (sunday = '1' OR (monday = '0' AND tuesday = '0' AND wednesday = '0' AND thursday = '0' AND friday = '0' AND saturday = '0' AND sunday = '0')) ";
}
$hhmmss = date("H:i:s");
$query2 .= "AND ((time_start <= '" . $hhmmss . "' AND time_stop >= '" . $hhmmss . "') OR (time_start = '00:00:00' AND time_stop = '00:00:00')) ";
$day = date('d');
$query2 .= "AND ((day_start <= '" . $day . "' AND day_stop >= '" . $day . "') OR (day_start = '0' AND day_stop = '0')) ";
$month = date('m');
$query2 .= "AND ((month_start <= '" . $month . "' AND month_stop >= '" . $month . "') OR (month_start = '0' AND month_stop = '0')) ";
$year = date('Y');
$query2 .= "AND ((year_start <= '" . $year . "' AND year_stop >= '" . $year . "') OR (year_start = '0' AND year_stop = '0')) ";
$result2 = mysqli_query($this->globais['db'], $query2);
if (mysqli_num_rows($result2) > 0) {
$periodo_match = true;
}
}
if ($periodo_match) {
if ($resultLinha['shortname'] == 'queue') {
$query2 = "SELECT id FROM tab_queue WHERE id = '" . mysqli_real_escape_string($this->globais['db'], $resultLinha['destino_id']) . "'";
$result2 = mysqli_query($this->globais['db'], $query2);
while ($resultLinha2 = mysqli_fetch_assoc($result2)) {
$return = array(
'shortname' => $resultLinha['shortname'],
'queue' => $resultLinha2['id']
);
}
}
if (($resultLinha['shortname'] == 'ramal') || ($resultLinha['shortname'] == 'voicemail')) {
$query2 = "SELECT ramal FROM tab_ramal WHERE id = '" . mysqli_real_escape_string($this->globais['db'], $resultLinha['destino_id']) . "'";
$result2 = mysqli_query($this->globais['db'], $query2);
while ($resultLinha2 = mysqli_fetch_assoc($result2)) {
$return = array(
'shortname' => $resultLinha['shortname'],
'ramal' => $resultLinha2['ramal']
);
}
}
if ($resultLinha['shortname'] == 'ramalgrupo') {
$query2 = "SELECT id FROM tab_group WHERE id = '" . mysqli_real_escape_string($this->globais['db'], $resultLinha['destino_id']) . "'";
$result2 = mysqli_query($this->globais['db'], $query2);
while ($resultLinha2 = mysqli_fetch_assoc($result2)) {
$return = array(
'shortname' => $resultLinha['shortname'],
'callgroup' => $resultLinha2['id']
);
}
}
if ($resultLinha['shortname'] == 'numeroexterno') {
$return = array(
'shortname' => $resultLinha['shortname'],
'numero' => $resultLinha['destino_id']
);
}
}
}
return $return;
}
public function hitpbxGetQueuenomeFromId($queueid = '')
{
$return = '';
$query = "SELECT nome FROM tab_queue WHERE id = '" . mysqli_real_escape_string($this->globais['db'], $queueid) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
while ($resultLinha = mysqli_fetch_assoc($query_result)) {
$return = $resultLinha['nome'];
}
return $return;
}
public function hitpbxGetQueueFromNumeroExterno($args = array())
{
$return = array();
$args['numero_externo'] = $args['numero_externo'] ? $args['numero_externo'] : '';
if ($args['numero_externo'] != '') {
$query = "SELECT * FROM tab_queue WHERE numero_externo = '" . mysqli_real_escape_string($this->globais['db'], $args['numero_externo']) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
while ($resultLinha = mysqli_fetch_assoc($query_result)) {
$return = $resultLinha;
}
}
return $return;
}
public function hitpbxGetRamais($args = array())
{
$return = array();
$query = "SELECT ramal FROM tab_ramal ";
$query .= "WHERE ramal IS NOT NULL ";
if (isset($args['callgroup'])) {
$query .= "AND callgroup = '" . mysqli_real_escape_string($this->globais['db'], $args['callgroup']) . "'";
}
$query_result = mysqli_query($this->globais['db'], $query);
while ($resultLinha = mysqli_fetch_assoc($query_result)) {
$return[] = $resultLinha['ramal'];
}
return $return;
}
public function hitpbxGetRamaisSecretariaFromRamal($ramal = '')
{
$return = array();
$query = "SELECT callgroup_secretaria FROM tab_ramal WHERE ramal = '" . mysqli_real_escape_string($this->globais['db'], $ramal) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
if (mysqli_num_rows($query_result) > 0) {
$resultLinha = mysqli_fetch_assoc($query_result);
$callgroup_secretaria = $resultLinha['callgroup_secretaria'];
$query = "SELECT ramal FROM tab_ramal WHERE callgroup = '" . mysqli_real_escape_string($this->globais['db'], $callgroup_secretaria) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
while ($resultLinha = mysqli_fetch_assoc($query_result)) {
$return[] = $resultLinha['ramal'];
}
}
return $return;
}
public function hitpbxGetRingTimeoutFromRamal($ramal = '')
{
$return = '';
$query = "SELECT ring_timeout FROM tab_ramal WHERE ramal = '" . mysqli_real_escape_string($this->globais['db'], $ramal) . "'";
$query_result = mysqli_query($this->globais['db'], $query);
if (mysqli_num_rows($query_result) > 0) {
$resultLinha = mysqli_fetch_assoc($query_result);
$return = $resultLinha['ring_timeout'];
}
return $return;
}
// --------------------------------------------------------------------------------------------------------------------------------
public function extrairNumeros($string)
{
preg_match_all('/\d+/', $string, $matches);
$numeros = implode('', $matches[0]);
return $numeros;
}
public function validarCNPJ($cnpj = '')
{
// Remover caracteres não numéricos
$cnpj = preg_replace('/[^0-9]/', '', $cnpj);
// Verificar se o CNPJ tem 14 dígitos
if (strlen($cnpj) !== 14) {
return false;
}
// Verificar CNPJs com dígitos repetidos (00.000.000/0000-00, 11.111.111/1111-11, etc.)
if (preg_match('/(\d)\1{13}/', $cnpj)) {
return false;
}
// Calcular o primeiro dígito verificador
$soma = 0;
for ($i = 0; $i < 12; $i++) {
$soma += $cnpj[$i] * (($i < 4) ? (5 - $i) : (13 - $i));
}
$resto = $soma % 11;
$dv1 = ($resto < 2) ? 0 : 11 - $resto;
// Calcular o segundo dígito verificador
$soma = 0;
for ($i = 0; $i < 13; $i++) {
$soma += $cnpj[$i] * (($i < 5) ? (6 - $i) : (14 - $i));
}
$resto = $soma % 11;
$dv2 = ($resto < 2) ? 0 : 11 - $resto;
// Verificar se os dígitos verificadores são válidos
if ($cnpj[12] == $dv1 && $cnpj[13] == $dv2) {
return true;
} else {
return false;
}
}
public function validarCPF($cpf = '')
{
// Remover caracteres não numéricos
$cpf = preg_replace('/[^0-9]/', '', $cpf);
// Verificar se o CPF tem 11 dígitos
if (strlen($cpf) !== 11) {
return false;
}
// Verificar CPFs com dígitos repetidos (111.111.111-11, 222.222.222-22, etc.)
if (preg_match('/(\d)\1{10}/', $cpf)) {
return false;
}
// Separar os dígitos
$digitos = str_split($cpf);
// Calcular o primeiro dígito verificador
$soma = 0;
for ($i = 0; $i < 9; $i++) {
$soma += $digitos[$i] * (10 - $i);
}
$resto = $soma % 11;
$dv1 = ($resto < 2) ? 0 : 11 - $resto;
// Calcular o segundo dígito verificador
$soma = 0;
for ($i = 0; $i < 10; $i++) {
$soma += $digitos[$i] * (11 - $i);
}
$resto = $soma % 11;
$dv2 = ($resto < 2) ? 0 : 11 - $resto;
// Verificar se os dígitos verificadores são válidos
if ($digitos[9] == $dv1 && $digitos[10] == $dv2) {
return true;
} else {
return false;
}
}
// --------------------------------------------------------------------------------------------------------------------------------
public function teste()
{
$this->agiLog($this->globais['fgId']);
}
function tocarFila($queueId, $timeout)
{
return $this->agiRunCmd('EXEC Queue Q' . $queueId . ',ctT,,,' . $timeout);
}
// --------------------------------------------------------------------------------------------------------------------------------
public function run()
{
2024-12-16 19:01:16 +00:00
// $this->logStart();
// $this->logAdd("<pre>" . print_r($this->globais, true) . "</pre><br>");
$this->agiLog("===> " . __FUNCTION__ . " <===");
$this->agiLog(print_r($this->globais, true));
if (is_null($this->globais['fgId'])) {
$this->agiRunCmd('EXEC NOOP fgIdIsNull');
return;
}
if (is_null($this->globais['hostname'])) {
$this->agiRunCmd('EXEC NOOP hostnameIsNull');
return;
}
if (is_null($this->globais['hostip'])) {
$this->agiRunCmd('EXEC NOOP hostipIsNull');
return;
}
2024-12-14 01:14:11 +00:00
$this->fgGet();
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';
2024-12-16 19:01:16 +00:00
// $this->globais['hostname'] = gethostname();
if ($this->globais['hostname'] == 'hitpbx-001') {
2024-12-14 01:14:11 +00:00
$database = 'hitpbx-001';
2024-12-16 19:01:16 +00:00
} else {
$database = str_replace('hitpbx-', 'hitpbx_', $this->globais['hostname']);
2024-12-14 01:14:11 +00:00
}
$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($this->globais['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);
2024-12-20 16:08:25 +00:00
if (1 == 1) {
// Teste
$callerid = $this->agiVarGet('CALLERID');
$this->agiLog('callerid: ' . $callerid);
$arr = array(
'ramal' => '6999',
'numero' => $callerid
);
$this->agiAddCallback($arr);
} else {
$this->fgItemRun($this->globais['fgItemStart']);
}
2024-12-14 01:14:11 +00:00
// 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";
*/
}
}