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.
116 lines
3.3 KiB
116 lines
3.3 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/verifica_rota.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
|
||
|
$ramal = $argv[1];
|
||
|
$destino = $argv[2];
|
||
|
|
||
|
/*
|
||
|
$query = "select a.id_rota, a.tipo_acesso, a.ramal,b.modelo from pbx_rotas_saida_ramais a inner join pbx_rotas_saida_modelos b on a.id_rota = b.id_rota
|
||
|
where ramal = '$ramal'
|
||
|
and '$destino' ~ case when (substring(modelo from '.$') <> '.') then replace('^'||replace(replace(modelo||'$','X','[0-9]'),'Z','[1-9]'),'N','[2-9]')
|
||
|
when (substring(modelo from '.$') = '.') then replace('^'||replace(replace(modelo,'X','[0-9]'),'Z','[1-9]'),'N','[2-9]') end
|
||
|
and tipo_acesso <> '0'
|
||
|
order by id_rota limit 1";
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
$query = "select a.id_rota, a.tipo_acesso, a.ramal,b.modelo,c.recorte, c.acrescenta,
|
||
|
case when (substring(modelo from '.$') <> '.') then length(replace(replace(replace(modelo,'X','[0-9]'),'Z','[1-9]'),'N','[2-9]'))
|
||
|
when (substring(modelo from '.$') = '.') then length(replace(replace(replace(replace(modelo,'X','[0-9]'),'Z','[1-9]'),'N','[2-9]'),'.',REPEAT('[0-9]', 100))) end as prioridade
|
||
|
from pbx_rotas_saida_ramais a inner join pbx_rotas_saida_modelos b on a.id_rota = b.id_rota
|
||
|
inner join pbx_rotas_saida c on a.id_rota = c.id
|
||
|
where ramal = '$ramal'
|
||
|
and '$destino' ~ case when (substring(modelo from '.$') <> '.') then replace('^'||replace(replace(modelo||'$','X','[0-9]'),'Z','[1-9]'),'N','[2-9]')
|
||
|
when (substring(modelo from '.$') = '.') then replace('^'||replace(replace(modelo,'X','[0-9]'),'Z','[1-9]'),'N','[2-9]') end
|
||
|
and tipo_acesso <> '0'
|
||
|
order by prioridade limit 1";
|
||
|
|
||
|
|
||
|
$result = pg_query($conexao, $query);
|
||
|
$row = pg_fetch_array($result);
|
||
|
$id_rota = $row['id_rota'];
|
||
|
$tipo_acesso = $row['tipo_acesso'];
|
||
|
$ramal = $row['ramal'];
|
||
|
$modelo = $row['modelo'];
|
||
|
$recorte = $row['recorte'];
|
||
|
$acrescenta = $row['acrescenta'];
|
||
|
|
||
|
if (!$tipo_acesso) {
|
||
|
$tipo_acesso = 0;
|
||
|
}
|
||
|
|
||
|
//Libera ligacao caso tenha agente logado no ramal
|
||
|
else if ($tipo_acesso == '3') {
|
||
|
|
||
|
$query = "select count(*) from pbx_supervisor_agentes where ramal = '$ramal'";
|
||
|
$result = pg_query($conexao, $query);
|
||
|
$row = pg_fetch_array($result);
|
||
|
$resultado = $row['0'];
|
||
|
|
||
|
if (!$resultado) {
|
||
|
$tipo_acesso = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
write("SET VARIABLE ID_ROTA $id_rota");
|
||
|
read();
|
||
|
|
||
|
write("SET VARIABLE TIPO_ACESSO $tipo_acesso");
|
||
|
read();
|
||
|
|
||
|
write("SET VARIABLE NUMERO-RAMAL $ramal");
|
||
|
read();
|
||
|
|
||
|
write("SET VARIABLE MODELO $modelo");
|
||
|
read();
|
||
|
|
||
|
if ($recorte) {
|
||
|
write("SET VARIABLE RECORTAROTA $recorte");
|
||
|
read();
|
||
|
}
|
||
|
if ($acrescenta) {
|
||
|
write("SET VARIABLE ACRESCENTAROTA $acrescenta");
|
||
|
read();
|
||
|
}
|
||
|
|
||
|
pg_close();
|
||
|
fclose($in);
|
||
|
fclose($stdlog);
|
||
|
exit;
|
||
|
?>
|