forked from SimplesIP/pabx-app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
1.9 KiB
87 lines
1.9 KiB
2 years ago
|
#!/usr/bin/php -q
|
||
|
<?php
|
||
|
include('bd.php');
|
||
|
|
||
|
ob_implicit_flush(true);
|
||
|
set_time_limit(6);
|
||
|
error_reporting(0);
|
||
|
$in = fopen("php://stdin", "r");
|
||
|
$stdlog = fopen("/var/log/asterisk/bina_tronco.log", "w");
|
||
|
|
||
|
// Habilita modo debugging (mais verbose)
|
||
|
$debug = true;
|
||
|
|
||
|
// Do function definitions before we start the main loop
|
||
|
function read() {
|
||
|
global $in, $debug, $stdlog;
|
||
|
$input = str_replace("\n", "", fgets($in, 4096));
|
||
|
if ($debug)
|
||
|
fputs($stdlog, "read: $input\n");
|
||
|
return $input;
|
||
|
}
|
||
|
|
||
|
function write($line) {
|
||
|
global $debug, $stdlog;
|
||
|
if ($debug)
|
||
|
fputs($stdlog, "write: $line\n");
|
||
|
echo $line . "\n";
|
||
|
}
|
||
|
|
||
|
// Colocamos headers AGI dentro de um array
|
||
|
while ($env = read()) {
|
||
|
$s = split(": ", $env);
|
||
|
$agi[str_replace("agi_", "", $s[0])] = trim($s[1]);
|
||
|
if (($env == "") || ($env == "\n")) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//parametro vindo do dialplan
|
||
|
$id_tronco = $argv[1];
|
||
|
$ramal = $argv[2];
|
||
|
$dial = $argv[3];
|
||
|
|
||
|
$query = "select upper(tipo) as tipo,upper(callerid_tronco) as callerid_tronco,(case when callerid_tronco like '%-%' then '1' else '0' end) as bina_dinamica from pbx_troncos where id = '$id_tronco'";
|
||
|
$result = pg_query($conexao, $query);
|
||
|
$row = pg_fetch_array($result);
|
||
|
$tipo = $row['tipo'];
|
||
|
$bina = $row['callerid_tronco'];
|
||
|
$bina_dinamica = $row['bina_dinamica'];
|
||
|
|
||
|
|
||
|
if($bina_dinamica)
|
||
|
{
|
||
|
$range = explode("-", $bina);
|
||
|
$bina_tronco = rand($range[0],$range[1]);
|
||
|
|
||
|
write("SET VARIABLE BINA_TRONCO $bina_tronco");
|
||
|
read();
|
||
|
|
||
|
} elseif ($bina) {
|
||
|
|
||
|
write("GET VARIABLE RAMAL");
|
||
|
$ramal = substr(strrchr(read(), "("), 1, -1);
|
||
|
|
||
|
$bina_tronco = str_replace('${RAMAL}', $ramal, $bina);
|
||
|
|
||
|
write("SET VARIABLE BINA_TRONCO $bina_tronco");
|
||
|
read();
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
if(($tipo == 'KHOMP') && ($bina_tronco))
|
||
|
{
|
||
|
$dial_khomp = explode("/", $dial);
|
||
|
$bina_khomp = str_replace($dial_khomp[3],"orig=$bina_tronco", "$dial");
|
||
|
write("SET VARIABLE BINA_KHOMP $bina_khomp");
|
||
|
read();
|
||
|
|
||
|
}
|
||
|
|
||
|
pg_close();
|
||
|
fclose($in);
|
||
|
fclose($stdlog);
|
||
|
exit;
|
||
|
?>
|