|
|
|
#!/usr/bin/php -q
|
|
|
|
<?php
|
|
|
|
include('bd.php');
|
|
|
|
include('util/util.php');
|
|
|
|
include('funcoes/shared.php');
|
|
|
|
|
|
|
|
|
|
|
|
$host = '127.0.0.1';
|
|
|
|
$porta = "5038";
|
|
|
|
$usuario = "manager";
|
|
|
|
$senha = "manager007";
|
|
|
|
$uid = $argv[1];
|
|
|
|
$channelSource = '';
|
|
|
|
$channelDest = '';
|
|
|
|
$oldEvt = '';
|
|
|
|
$blocoEvento = array();
|
|
|
|
$Uniqueid1 = '';
|
|
|
|
$Uniqueid2 = '';
|
|
|
|
$CallerID1 = '';
|
|
|
|
$CallerID2 = '';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Abre o socket
|
|
|
|
*/
|
|
|
|
$timeout = 10;
|
|
|
|
$socket = fsockopen($host, $porta, $errno, $errstr, $timeout);
|
|
|
|
//$socket = fsockopen($host,$porta);
|
|
|
|
|
|
|
|
if ($socket) {
|
|
|
|
$startEvt = 0;
|
|
|
|
$printspace = 0;
|
|
|
|
$eventMonitora = array(0 => '#', 1 => 'NEWCHANNEL', 2 => 'HANGUP', 3 => 'CDR', 4 => 'LINK', 5 => 'UNLINK', 6 => 'DIAL', 7 => 'RENAME');
|
|
|
|
$links = array();
|
|
|
|
$channels = array();
|
|
|
|
$rename = array();
|
|
|
|
|
|
|
|
$actionid = rand(000000000, 9999999999);
|
|
|
|
fwrite($socket, "action: login\r\n");
|
|
|
|
fwrite($socket, "username: $usuario\r\n");
|
|
|
|
fwrite($socket, "secret: $senha\r\n");
|
|
|
|
fwrite($socket, "events: on\r\n");
|
|
|
|
fwrite($socket, "ActionID: " . $actionid . "\r\n\r\n");
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
$numTrans = 0;
|
|
|
|
$arq = fopen("/var/log/asterisk/tarif_monitora.log", 'a');
|
|
|
|
while (!feof($socket)) {
|
|
|
|
$bufer = trim(fgets($socket));
|
|
|
|
|
|
|
|
if (empty($bufer)) {
|
|
|
|
$startEvt = 0;
|
|
|
|
if ($printspace) {
|
|
|
|
if (($oldEvt == 'UNLINK') && (empty($channelSource))) {
|
|
|
|
$channelSource = $blocoEvento[$oldEvt]['Channel1'];
|
|
|
|
$channelDest = $blocoEvento[$oldEvt]['Channel2'];
|
|
|
|
$Uniqueid1 = $blocoEvento[$oldEvt]['Uniqueid1'];
|
|
|
|
$Uniqueid2 = $blocoEvento[$oldEvt]['Uniqueid2'];
|
|
|
|
$CallerID1 = $blocoEvento[$oldEvt]['CallerID1'];
|
|
|
|
$CallerID2 = $blocoEvento[$oldEvt]['CallerID2'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($oldEvt == 'NEWCHANNEL')) {
|
|
|
|
$channels[] = $blocoEvento[$oldEvt];
|
|
|
|
//$channels[$blocoEvento[$oldEvt]['Uniqueid']] = $blocoEvento[$oldEvt];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($oldEvt == 'HANGUP')) {
|
|
|
|
//usleep(2000000);
|
|
|
|
$unique = QuotedStr($blocoEvento[$oldEvt]['Uniqueid']);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$query = "select count(*) from pbx_tarif_registra where uniqueid = %s and trr_link = %s";
|
|
|
|
$query = sprintf($query, $unique, QuotedStr($uid));
|
|
|
|
$result = pg_query($query);
|
|
|
|
$row = pg_fetch_row($result);
|
|
|
|
|
|
|
|
|
|
|
|
if ($row[0]) {
|
|
|
|
$unique = ($blocoEvento[$oldEvt]['Uniqueid']);
|
|
|
|
$chn = ($blocoEvento[$oldEvt]['Channel']);
|
|
|
|
$chanRen = $numTrans ? $chn : $channelDest;
|
|
|
|
|
|
|
|
//$ren = print_r($rename, true);
|
|
|
|
$str = "Log Rename ch: $chanRen Uid: $unique $chanDest: $channelDest Chan: $chn \n";
|
|
|
|
//fwrite($arq,$str);
|
|
|
|
//fwrite($arq,$ren);
|
|
|
|
if (ExisteTransfer($rename, $chanRen)) {
|
|
|
|
/* $query = "update pbx_tarif_registra
|
|
|
|
set trr_fim = now(),
|
|
|
|
trr_duracao = (extract(epoch from (now() - trr_inicio))::int ),
|
|
|
|
trr_tipo_chamada = 'T'
|
|
|
|
where uniqueid = '$unique' and trr_link = '$uid' and coalesce(trr_tipo_chamada, '') <> 'T'\n"; */
|
|
|
|
$query = "update pbx_tarif_registra
|
|
|
|
set trr_tipo_chamada = 'T'
|
|
|
|
where uniqueid = '$unique' and trr_link = '$uid' and coalesce(trr_tipo_chamada, '') <> 'T'\n";
|
|
|
|
$numTrans++;
|
|
|
|
} else {
|
|
|
|
$query = "update pbx_tarif_registra
|
|
|
|
set trr_fim = now(),
|
|
|
|
trr_duracao = (extract(epoch from (now() - trr_inicio))::int ),
|
|
|
|
trr_tipo_chamada = 'C'
|
|
|
|
where uniqueid = '$unique' and trr_link = '$uid'\n";
|
|
|
|
$query = "update pbx_tarif_registra
|
|
|
|
set trr_tipo_chamada = 'C'
|
|
|
|
where uniqueid = '$unique' and trr_link = '$uid'\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
pg_query($query);
|
|
|
|
$str = "Query: $query \n";
|
|
|
|
fwrite($arq, $str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($oldEvt == 'LINK')) {
|
|
|
|
$links[] = $blocoEvento[$oldEvt];
|
|
|
|
//$links[$blocoEvento[$oldEvt]['Uniqueid2']] = $blocoEvento[$oldEvt];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($oldEvt == 'RENAME')) {
|
|
|
|
$rename[] = $blocoEvento[$oldEvt];
|
|
|
|
// $rename[$blocoEvento[$oldEvt]['Newname']] = $blocoEvento[$oldEvt];
|
|
|
|
//$rename[$blocoEvento[$oldEvt]['Oldname']] = $blocoEvento[$oldEvt];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($oldEvt == 'HANGUP') && ($channelDest == $blocoEvento[$oldEvt]['Channel'])) {
|
|
|
|
|
|
|
|
$query = "update pbx_tarif_registra
|
|
|
|
set trr_fim_transf = now(),
|
|
|
|
trr_tipo_chamada = 'N',
|
|
|
|
trr_duracao_transf = (extract(epoch from (now() - trr_inicio))::int )
|
|
|
|
where uniqueid = '$uid' \n";
|
|
|
|
pg_query($query);
|
|
|
|
$str = "Query: $query \n";
|
|
|
|
fwrite($arq, $str);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fwrite($arq, "\n");
|
|
|
|
$printspace = 0;
|
|
|
|
$oldEvt = '';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$evt = explode(':', $bufer);
|
|
|
|
$event = isset($evt[0]) ? trim($evt[0]) : null;
|
|
|
|
$value = isset($evt[1]) ? trim($evt[1]) : null;
|
|
|
|
$eventSsearch = strtoupper($value);
|
|
|
|
|
|
|
|
if (array_search($eventSsearch, $eventMonitora) && ($event == 'Event')) {
|
|
|
|
$startEvt = 1;
|
|
|
|
$oldEvt = $eventSsearch;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($startEvt) {
|
|
|
|
$blocoEvento[$oldEvt][$event] = $value;
|
|
|
|
$str = "$event:$value\n";
|
|
|
|
fwrite($arq, $str);
|
|
|
|
$printspace = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//fwrite($arq,$bufer);
|
|
|
|
if ($i++ > 10000)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($arq);
|
|
|
|
}
|
|
|
|
|
|
|
|
function InsertChannel($bloco, $uid) {
|
|
|
|
$query = "insert
|
|
|
|
into pbx_canais(Channel,State,CallerIDNum,CallerIDName,Uniqueid,dispositivo,link)
|
|
|
|
values (%s,%s,%s,%s,%s,%s,%s)";
|
|
|
|
$query = sprintf($query, QuotedStr($bloco['Channel']), QuotedStr($bloco['State']), QuotedStr($bloco['CallerIDNum']), QuotedStr($bloco['CallerIDName']), QuotedStr($bloco['Uniqueid']), QuotedStr($bloco['dispositivo']), QuotedStr($uid));
|
|
|
|
pg_query($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ExistChannel($uid) {
|
|
|
|
$query = "select count(*) from pbx_canais where Uniqueid = '$uid'";
|
|
|
|
$result = pg_query($query);
|
|
|
|
|
|
|
|
$row = pg_fetch_row($result);
|
|
|
|
|
|
|
|
return ($row[0] > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ExisteTransfer($renames, $channelDest) {
|
|
|
|
|
|
|
|
// fwrite($arq, "ChannelDest: $channelDest Channel: $channel Unique: $uid\n");
|
|
|
|
//$ren = print_r($renames, true);
|
|
|
|
//fwrite($arq, $ren);
|
|
|
|
|
|
|
|
foreach ($renames as $rename) {
|
|
|
|
if ($rename["Newname"] == $channelDest) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($channelDest)) {
|
|
|
|
$ch = trim($channelDest);
|
|
|
|
$pos = strpos($ch, '@');
|
|
|
|
$ch = substr($ch, 0, $pos);
|
|
|
|
foreach ($renames as $rename) {
|
|
|
|
if (strpos($rename["Newname"], $ch) !== false) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
?>
|