|
|
|
|
#!/usr/bin/php -q
|
|
|
|
|
<?php
|
|
|
|
|
include 'funcoes/shared.php';
|
|
|
|
|
include 'util/util.php';
|
|
|
|
|
|
|
|
|
|
//$pg_local["host"] = "192.168.115.11";
|
|
|
|
|
$pg_local["host"] = "127.0.0.1";
|
|
|
|
|
$pg_local["port"] = "5432";
|
|
|
|
|
$pg_local["dbname"] = "pbx";
|
|
|
|
|
$pg_local["user"] = "contacte";
|
|
|
|
|
$pg_local["password"] = "ctepgSQL";
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Linha para incluir no crontab.
|
|
|
|
|
*/
|
|
|
|
|
//$pg_remoto["host"] = "177.67.197.147"; //Ip externo
|
|
|
|
|
$pg_remoto["host"] = "192.168.115.12";
|
|
|
|
|
$pg_remoto["port"] = "5432";
|
|
|
|
|
$pg_remoto["dbname"] = "portabilidade";
|
|
|
|
|
$pg_remoto["user"] = "contacte";
|
|
|
|
|
$pg_remoto["password"] = "ctepgSQL";
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$conn = dbConnect($pg_remoto);
|
|
|
|
|
if (!$conn)
|
|
|
|
|
throw new Exception(GetExcept("N<EFBFBD>o foi possivel conectar ao servidor remoto!"));
|
|
|
|
|
|
|
|
|
|
$db = dbConnect($pg_local);
|
|
|
|
|
if (!$db)
|
|
|
|
|
throw new Exception(GetExcept("N<EFBFBD>o foi possivel conectar ao servidor local!"));
|
|
|
|
|
|
|
|
|
|
$result = pg_query($db, 'begin');
|
|
|
|
|
if (!$result)
|
|
|
|
|
throw new Exception(GetExcept("N<EFBFBD>o foi possivel iniciar uma transa<EFBFBD><EFBFBD>o com o banco de dados local!"));
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Importando a tabela operadoras.
|
|
|
|
|
*/
|
|
|
|
|
echo "Carregando registros de operadoras existentes!\n";
|
|
|
|
|
$numOper = 0;
|
|
|
|
|
$query = "select oper_id, oper_nome, oper_spid, oper_licensa, oper_numero from pbx_operadoras";
|
|
|
|
|
$result = pg_query($db, $query);
|
|
|
|
|
if (!$result)
|
|
|
|
|
throw new Exception(GetExcept("N<EFBFBD>o foi possivel listar as operadoras existentes!"));
|
|
|
|
|
|
|
|
|
|
$numOper = pg_num_rows($result);
|
|
|
|
|
if ($numOper) {
|
|
|
|
|
while ($row = pg_fetch_array($result)) {
|
|
|
|
|
$operadoras[] = $row["oper_id"];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
echo "$numOper Operadoaras selecionadas!\n";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
echo "Atualizando a tabela de operadoras a partir do servidor remoto!\n";
|
|
|
|
|
$query = "select oper_id, oper_nome, oper_spid, oper_licensa, oper_numero from routing";
|
|
|
|
|
$result = pg_query($conn, $query);
|
|
|
|
|
if (!$result)
|
|
|
|
|
throw new Exception(GetExcept("N<EFBFBD>o foi possivel listar as operadoras remotas!"));
|
|
|
|
|
|
|
|
|
|
if (pg_num_rows($result)) {
|
|
|
|
|
while ($row = pg_fetch_array($result)) {
|
|
|
|
|
$operId = $row["oper_id"];
|
|
|
|
|
$operNome = trim($row["oper_nome"]);
|
|
|
|
|
$operSpid = trim($row["oper_spid"]);
|
|
|
|
|
$operLicensa = trim($row["oper_licensa"]);
|
|
|
|
|
$operNumero = trim($row["oper_numero"]);
|
|
|
|
|
$query = '';
|
|
|
|
|
|
|
|
|
|
if ($numOper && (array_search($operId, $operadoras) !== false)) {
|
|
|
|
|
$query = "update pbx_operadoras set oper_nome = %s, oper_spid = %s, oper_licensa = %s, oper_numero = %s where oper_id = %s\n";
|
|
|
|
|
$query = sprintf($query, _QuotedStr($operNome), _QuotedStr($operSpid), _QuotedStr($operLicensa), _QuotedStr($operNumero), _QuotedStr($operId));
|
|
|
|
|
$resultAtu = pg_query($db, $query);
|
|
|
|
|
if (!$resultAtu)
|
|
|
|
|
throw new Exception(GetExcept("N<EFBFBD>o foi possivel atualizar a tabela de operadoras!"));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$query = "insert into pbx_operadoras(oper_id, oper_nome, oper_spid, oper_licensa, oper_numero)values(%s,%s,%s,%s,%s)\n";
|
|
|
|
|
$query = sprintf($query, _QuotedStr($operId), _QuotedStr($operNome), _QuotedStr($operSpid), _QuotedStr($operLicensa), _QuotedStr($operNumero));
|
|
|
|
|
$resultAtu = pg_query($db, $query);
|
|
|
|
|
if (!$resultAtu)
|
|
|
|
|
throw new Exception(GetExcept("N<EFBFBD>o foi possivel importar a tabela de operadoras!"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$result = pg_query($db, 'commit');
|
|
|
|
|
if (!$result)
|
|
|
|
|
throw new Exception(GetExcept("N<EFBFBD>o foi possivel encerrar a transa<EFBFBD><EFBFBD>o com o banco de dados local!"));
|
|
|
|
|
echo "Operadoras atualizadas com sucesso!\n";
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
|
echo $ex->getMessage() . "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dbConnect($pg) {
|
|
|
|
|
$conStr = sprintf("host='%s' port='%s' dbname='%s' user='%s' password='%s'", $pg["host"], $pg["port"], $pg["dbname"], $pg["user"], $pg["password"]);
|
|
|
|
|
return @pg_connect($conStr);
|
|
|
|
|
}
|
|
|
|
|
?>
|