130 lines
3.7 KiB
PHP
130 lines
3.7 KiB
PHP
<?php
|
|
require __DIR__ . '/vendor/autoload.php';
|
|
require __DIR__ . '/includes/wrapper-swoole.php';
|
|
|
|
use Hitlabs\Hitagi\AGIEntrada;
|
|
use Swoole\Runtime;
|
|
use Swoole\Table;
|
|
use Swoole\Server;
|
|
|
|
Runtime::enableCoroutine();
|
|
$table = new Table(1024);
|
|
$table->column('agi', Swoole\Table::TYPE_INT);
|
|
$table->column('data', Swoole\Table::TYPE_STRING, 65536);
|
|
$table->column('agi_arg', Swoole\Table::TYPE_STRING, 65536);
|
|
$table->create();
|
|
$server = new Swoole\Server("0.0.0.0", 8073);
|
|
$server->table = $table;
|
|
|
|
$server->on('start', function ($server) {
|
|
echo "LOG ======> Servidor iniciou na porta 8073...." . PHP_EOL;
|
|
});
|
|
|
|
$server->on("connect", function (Server $server, $fd) {
|
|
echo "LOG ======> Cliente $fd conectado...." . PHP_EOL;
|
|
});
|
|
|
|
$server->on("receive", function ($server, $fd, $reactor_id, $data) {
|
|
|
|
$table_data = $server->table->get($fd);
|
|
// echo "LOG ======> table_data: " . print_r($table_data, true) . PHP_EOL;
|
|
|
|
if (!is_array($table_data)) {
|
|
// FIRST CONNECTION
|
|
echo 'LOG ======> Receive data (FIRST): ' . PHP_EOL . $data . PHP_EOL;
|
|
$table_data = [
|
|
'data' => $data,
|
|
'agi_arg' => json_encode([])
|
|
];
|
|
$server->table->set($fd, $table_data);
|
|
} else {
|
|
// ALREADY CONNECTED
|
|
echo 'LOG ======> Receive data (MORE): ' . PHP_EOL . $data . PHP_EOL;
|
|
$table_data['data'] .= $data;
|
|
$server->table->set($fd, $table_data);
|
|
}
|
|
|
|
// GET ARGV
|
|
$argv = json_decode($table_data['agi_arg'], true);
|
|
$explodido = explode(PHP_EOL, $data);
|
|
foreach ($explodido as $linha) {
|
|
$linha = trim($linha);
|
|
if (empty($linha)) {
|
|
continue;
|
|
}
|
|
$partes = explode(':', $linha);
|
|
$comando = trim($partes[0]);
|
|
if (substr($comando, 0, 8) == 'agi_arg_') {
|
|
$number = str_replace('agi_arg_', '', $comando);
|
|
$argv[$number] = trim($partes[$number]);
|
|
}
|
|
}
|
|
$table_data['agi_arg'] = json_encode($argv);
|
|
$server->table->set($fd, $table_data);
|
|
|
|
$server_close = false;
|
|
if (strpos($table_data['data'], 'HANGUP') !== false) {
|
|
$server_close = true;
|
|
}
|
|
|
|
if ((isset($argv['1'])) && ($table_data['agi'] != 1)) {
|
|
|
|
$table_data = [
|
|
'agi' => 1
|
|
];
|
|
$server->table->set($fd, $table_data);
|
|
|
|
$fgId = $argv[1]; // Numero do DID
|
|
// $hostname = $argv[2];
|
|
// $hostip = $argv[3];
|
|
// $fgId = '8999';
|
|
$hostname = 'hitpbx-050';
|
|
$hostip = '177.107.205.248';
|
|
|
|
$logEnabled = true;
|
|
$isXampp = false;
|
|
$appdir = dirname(__FILE__);
|
|
chdir($appdir);
|
|
date_default_timezone_set('America/Bahia');
|
|
|
|
$agi = new AGIEntrada(
|
|
array(
|
|
'agiRunCmd' => agiRunCmdWrapper($fd, $server),
|
|
'agiLog' => agiLogWrapper()
|
|
),
|
|
array(
|
|
'fgId' => $fgId,
|
|
'hostname' => $hostname,
|
|
'hostip' => $hostip,
|
|
'logEnabled' => $logEnabled,
|
|
'appdir' => $appdir,
|
|
'isXampp' => $isXampp
|
|
)
|
|
);
|
|
$agi->run();
|
|
|
|
// agiRunCmdWrapper($fd,$server)('EXEC NOOP HITAGI2');
|
|
// agiRunCmdWrapper($fd,$server)('ANSWER');
|
|
// agiRunCmdWrapper($fd,$server)('EXEC WAIT 1');
|
|
// agiRunCmdWrapper($fd,$server)('EXEC PLAYBACK beep');
|
|
// agiRunCmdWrapper($fd,$server)('EXEC WAIT 1');
|
|
// agiRunCmdWrapper($fd,$server)('GET VARIABLE CALLERID(all)');
|
|
// // agiRunCmdWrapper($fd,$server)('HANGUP');
|
|
|
|
$server_close = true;
|
|
|
|
}
|
|
|
|
if ($server_close) {
|
|
$server->close($fd);
|
|
}
|
|
|
|
|
|
});
|
|
|
|
$server->on("close", function ($server, $fd) {
|
|
echo "LOG ======> Cliente $fd desconectado...." . PHP_EOL;
|
|
});
|
|
|
|
$server->start();
|