result)); echo ($ret == "true") ? "OK" : "ERRO"; exit(0); } } else { if (!isPostBack()) { $statusDispositivo = 'todos'; $tipoDispositivo = 3; } else { $statusDispositivo = $_POST['statusDispositivo']; $tipoDispositivo = $_POST['tipoDispositivo']; } $_SESSION['tipoDispositivo'] = $tipoDispositivo; $_SESSION['statusDispositivo'] = $statusDispositivo; $option .= ""; $option .= ""; $option .= ""; } /* * Retorna trocos e ramais cadastrados no banco de dados. */ $troncosCad = array(); $troncosCad = GetRamaisNome($tipoDispositivo); /* * Retorna os troncos monitorados no manager. */ $troncosMonitor = array(); GetDisplayIax($socket, $troncosMonitor); GetDisplaySip($socket, $troncosMonitor); /* * Monta o html do relatório. */ $a = 0; $resultados = ''; asort($troncosCad); foreach ($troncosCad as $dispositivo) { $idDispositivo = trim($dispositivo['nome']); $nomeRamal = $dispositivo['callerid']; $tipoRamal = $dispositivo['tipo']; /* pode haver valores null * $troncosCad = 51 * $troncosMonitor = 43 * Nesse caso, terá erro no array_values */ if( $troncosMonitor[$idDispositivo] == null ){ continue; } list($channeltype, $objectname, $ipaddress, $statusRamal ) = array_values($troncosMonitor[$idDispositivo]); $status = stripos($statusRamal, 'ok') !== false && soNumero($ipaddress); $isRamalOperador = $ramalBase == $numRamal; if(!$status){ $link = 'NÃO REGISTRADO'; }else if($tipoRamal == 'T'){ $link = $ipaddress; }else{ $link = sprintf('%s', $ipaddress, $ipaddress); } $imgRamal = GetImgTipo($tipoRamal, $status); $linkRamal = !$status || !$tipoRamal ? "$imgRamal" : (($isRamalOperador) ? $imgRamal : "$imgRamal"); $nomeRamal = isset($nomeRamal) && ($tipoRamal != 'T') ? $nomeRamal : '-'; $mostrar = true; if($statusDispositivo == 'on'){ $mostrar = $status; }else if($statusDispositivo == 'off'){ $mostrar = !$status; } if($mostrar){ $resultados .= " $idDispositivo $nomeRamal $link $linkRamal "; $a++; } } //   $head = ''; $foot = "
Ramal Nome Endereço IP Status
Quantidade Total: $a
"; $resultados = $head . $resultados . $foot; if (IsAjax()) { echo $resultados; exit; } else { $jsStartup[] = "var idSet = window.setInterval(\"timedRefresh()\", 3000);"; $smarty->assign('resultados', $resultados); $smarty->assign('option', $option); $smarty->assign('tipoDispositivo', $tipoDispositivo); $smarty->display("monitorRamal.tpl"); } function GetConnection($conStr) { $ret = pg_connect($conStr, PGSQL_CONNECT_FORCE_NEW); if (!$ret) { logTrb('GetConnection', "Conexao com banco de dados", "Erro ao executar conexao no banco", 'ERRO', $conStr); } return $ret; } function GetRamaisNome($tipoDispositivo) { global $servidor; $ret = array(); $conn = !$servidor ? '' : sprintf("host='%s' port='%s' dbname='%s' user='%s' password='%s'", $servidor, '5432', 'pbx', 'contacte', 'ctepgSQL'); $db = GetConnection($conn ? $conn : GetDefStrDb()); $query = ""; if(($tipoDispositivo == 1) || ($tipoDispositivo == 3)){ $query = "select 'R' as tipo, nome, callerid, tipo_table as protocol from pbx_ramais_mesa where tipo_table in('IAX', 'SIP')\n"; } if(($tipoDispositivo == 2) || ($tipoDispositivo == 3)){ if($query){ $query .= "union all\n"; } $query .= "select 'T' as tipo,nome, callerid, tipo as protocol from pbx_troncos where tipo in('SIP', 'IAX2')\n"; } $result = pg_query($db, $query); if (pg_num_rows($result)) { while ($dados = pg_fetch_array($result)) { $ret[$dados["nome"]] = $dados; } } return $ret; } function GetDisplaySip($socket, &$troncosMonitor) { $buffer = ''; $param = ''; $value = ''; $start = false; $startInfo = false; $arInfo = array(); $actionid = rand(000000000, 9999999999); fwrite($socket, "action: sippeers\r\n"); fwrite($socket, "ActionID: " . $actionid . "\r\n\r\n"); while (!feof($socket)) { $buffer = fgets($socket); list($param, $value) = GetExplode($buffer); if(!$start){ $start = (strtoupper($param) == 'ACTIONID') && ($actionid == (int) $value); } if ($start) { /* * Vefirica o inicio de um blolco de informacoes. * * Event: PeerEntry * ActionID: 56dd6666094747 * Channeltype: SIP * ObjectName: PORTABILIDADE * ChanObjectType: peer * IPaddress: 127.0.0.1 * IPport: 5060 * Dynamic: no * AutoForcerport: no * Forcerport: yes * AutoComedia: no * Comedia: yes * VideoSupport: yes * TextSupport: no * ACL: yes * Status: OK (1 ms) * RealtimeDevice: no * Description: * Accountcode: * */ if(!$startInfo){ $startInfo = ($param == 'Event') && ($value == 'PeerEntry'); } /* * Finaliza a leitura de um bloco. */ if ($startInfo && (trim($buffer) == "")) { $troncosMonitor[$arInfo['objectname']] = $arInfo; $startInfo = false; $arInfo = array(); } if ($startInfo) { /* * Le o bloco de informações. */ switch ($param) { case 'ObjectName': list($nome) = explode("/", $value); $arInfo[strtolower($param)] = $nome; break; case 'IPaddress': case 'Channeltype': case 'Status': $arInfo[strtolower($param)] = $value; break; } } } /* * Encerra a leitura. */ if (($param == 'Event') && ($value == "PeerlistComplete")) { break; } } } function GetDisplayIax($socket, &$troncosMonitor) { $buffer = ''; $param = ''; $value = ''; $start = false; $startInfo = false; $arInfo = array(); $actionid = rand(000000000, 9999999999); fwrite($socket, "action: iaxpeers\r\n"); fwrite($socket, "ActionID: " . $actionid . "\r\n\r\n"); while (!feof($socket)) { $buffer = fgets($socket); list($param, $value) = GetExplode($buffer); if(!$start){ $start = (strtoupper($param) == 'ACTIONID') && ($actionid == (int) $value); } if ($start) { /* * Vefirica o inicio de um blolco de informacoes. * * Event: PeerEntry * Channeltype: IAX2 * ObjectName: TROPICAL_PNEUS_TANGARA/SIMPLESIP * ChanObjectType: peer * IPaddress: 10.254.254.205 * IPport: 4569 * Dynamic: no * Trunk: yes * Encryption: no * Status: OK (79 ms) * Description: * */ if(!$startInfo){ $startInfo = (trim($param) == 'Event') && (trim($value) == 'PeerEntry'); } /* * Finaliza a leitura de um bloco. */ if ($startInfo && (trim($buffer) == "")) { $troncosMonitor[$arInfo['objectname']] = $arInfo; $startInfo = false; $arInfo = array(); } if ($startInfo) { /* * Le o bloco de informações. */ switch ($param) { case 'ObjectName': list($nome) = explode("/", $value); $arInfo[strtolower($param)] = $nome; break; case 'IPaddress': case 'Status': case 'Channeltype': $arInfo[strtolower($param)] = $value; break; } } } /* * Encerra a leitura. */ if (($param == 'Event') && ($value == "PeerlistComplete")) { break; } } } function RemoveDuploSpaco($str) { $s = str_replace(' ', ' ', $str); return strstr($s, ' ') === false ? $s : RemoveDuploSpaco($s); } function GetImgTipo($tipo, $status) { if ($tipo != 'T') { return $status ? "" : ""; } return $status ? "" : ""; } function GetExplode($str, $ch = ':'){ $ar = explode($ch, $str); return array(trim($ar[0]), trim($ar[1])); } ?>