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.
 
 
 
 
 
 

266 lines
7.7 KiB

<?php
function Malloc() {
/*
* Calculates necessary memory for MEMORY_SEGMENT_COUNT simultaneous processes.
* Os primeiros MEMORY_SEGMENT_COUNT são destinados a controlar os processos 0 livre 1 ocupado.
*/
return MEMORY_SEGMENT_COUNT + (MEMORY_SEGMENT_COUNT * MEMORY_SEGMENT_WITH);
}
function CreateSegment($shmKey) {
/*
* 00000000000000000000000000000000000000000000000000000000000000|000000000000000000|00000000000|00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
* |_Ctrl_MEMORY_SEGMENT_COUNTb_||________Uid_32b________________|__Telephone_18b___|_Queue_11b_|_______________________________Return data of client__128b_____________________________________________________________________|
*/
/*
* Apaga a informação.
*/
for ($i = 0; $i < Malloc(); $i++)
$segment .= ' ';
ShmWrite($shmKey, $segment);
/*
* Marca o seguimento de controle com PROCESSESS_WAIT que representa
* o processo livre. O seguimento de controle tem o comprimento igual
* a MEMORY_SEGMENT_COUNT e sinaliza o status do processo, normalmente
* 0(PROCESSESS_WAIT) livre; 1(PROCESSESS_START) iniciado geralmente
* marcado por serverAgi.php; 2(PROCESSESS_READ) significa que os dados ja
* foram integrados marcado por agiIntegra.php
*/
$ctrlProc = '';
for ($i = 0; $i < MEMORY_SEGMENT_COUNT; $i++) {
$ctrlProc .= PROCESSESS_WAIT;
}
ShmWrite($shmKey, $ctrlProc);
}
function CopySegment($shmKey, $idxSegment) {
$data = ShmRead($shmKey, GetOffSet($idxSegment), MEMORY_SEGMENT_WITH);
if ($data) {
return explode('|', $data);
}
return false;
}
function ClearSegment($shmKey, $idxSegment) {
$data = str_repeat(' ', MEMORY_SEGMENT_WITH);
ShmWrite($shmKey, $data, GetOffSet($idxSegment));
}
function GetOffSet($idxSegment) {
return MEMORY_SEGMENT_COUNT + ($idxSegment * MEMORY_SEGMENT_WITH);
}
function GetActiveProcesses($shmKey, $st) {
$data = ShmRead($shmKey, 0, MEMORY_SEGMENT_COUNT);
if ($data) {
$stProc = str_split($data);
$countProc = count($stProc);
$ret = array();
for ($i = 0; $i < $countProc; $i++) {
if ($stProc[$i] == $st)
$ret[] = $i;
}
return $ret;
}
return false;
}
function GetStatusProcesses($shmKey, $idProc) {
$data = trim(ShmRead($shmKey, $idProc, 1));
if ($data != '') {
return $data;
}
return false;
}
function GetParamConnecionDB() {
$connPG = $GLOBALS['connPG'];
$query = "select itg_id, itg_type, db_type, db_host, db_port, db_database, db_user, db_password, db_command_type, db_command, db_result, db_command_abd from pbx_integracao_ativa where itg_id = 0";
$result = pg_query($connPG, $query);
if ($result and pg_num_rows($result)) {
$dados = pg_fetch_array($result);
$GLOBALS["dbType"] = $dados["db_type"];
$GLOBALS["dbHhost"] = $dados["db_host"];
$GLOBALS["dbPort"] = $dados["db_port"];
$GLOBALS["dbDataBase"] = $dados["db_database"];
$GLOBALS["dbUserid"] = $dados["db_user"];
$GLOBALS["dbPassword"] = $dados["db_password"];
$GLOBALS["dbCommandType"] = $dados["db_command_type"];
$GLOBALS["dbCommand"] = $dados["db_command"];
$GLOBALS["dbResult"] = $dados["db_result"];
$GLOBALS["dbCommandAbd"] = $dados["db_command_abd"];
return true;
}
return false;
}
function GetFreeProcesses($shmKey) {
/*
* Esta funcao le todo o segmento definido para controle dos processos por MEMORY_SEGMENT_COUNT.
* E procura por PROCESSESS_WAIT que indica que o bloco não esta sendo utilizado, retornando o
* id correspondente ao processo.
*/
$data = ShmRead($shmKey, 0, MEMORY_SEGMENT_COUNT);
if ($data) {
$stProc = str_split($data);
$countProc = count($stProc);
for ($i = 0; $i < $countProc; $i++) {
if ($stProc[$i] == PROCESSESS_WAIT)
return $i;
}
}
return -1;
}
function GetParamQuery($params) {
// 4|#|ura @ 2|#|fone@5||uid
$listParam = array();
$params = explode('@', $params);
foreach ($params as $param) {
$paramValue = explode('|', $param);
$listParam[] = $paramValue[2];
}
return $listParam;
}
function GetQueryIntegracao($cmd, $params) {
// 4|#|ura@2|#|fone@5||uid
$params = explode('@', $params);
foreach ($params as $param) {
$paramValue = explode('|', $param);
$id = $paramValue[0];
$def = ($paramValue[1] != '#') ? $paramValue[1] : '0';
$nome = "@" . $paramValue[2];
$valueParam = GetValueParam($id, $def);
if ($valueParam !== 'NULL') {
$valueParam = QuotedStr($valueParam);
}
$cmd = str_replace($nome, $valueParam, $cmd);
}
return $cmd;
}
function GetValueParam($idParam, $value = 0) {
/*
* 1 - Uniqueid
* 2 - Numero Telefone
* 3 - Uniqueid Transbordo
* 4 - Ura
* 5 - Custom
*/
global $paramValue;
if ($idParam == 5) {
return $value;
}
return $paramValue[$idParam];
/*
switch ($idParam)
{
case 1: return $GLOBALS['uid'];
case 2: return $GLOBALS['numero'];
case 3: return $GLOBALS['uidOld'];
case 4: return $GLOBALS['ura'];
default : return $value;
}
*/
}
function GetCommand($query, &$cmd) {
/*
* Extrai multiplos comandos sql de uma mesma query e os retorna em um array, com
* exceção do primeiro que é retornado parametro $cmd.
*/
$cmds = explode('#', $query);
$cmd = trim($cmds[0]) . "\n";
$numCmd = count($cmds);
if ($numCmd > 1) {
$ar = array();
for ($i = 1; $i < $numCmd; $i++) {
$str = trim($cmds[$i]);
if ($str)
$ar[] = $str;
}
return count($ar) ? $ar : array();
}
return array();
}
function GetValuesIntegracaoAtv($paramMapa, $vArgv) {
$paramValue = array();
/*
* Associa os valores
*/
foreach ($paramMapa as $key => $keyArgv) {
$paramValue[$key] = $vArgv[$keyArgv];
}
return $paramValue;
}
function ExecuteCustom($idMetodo, &$nomeMetodo) {
try {
$patScript = "/var/lib/asterisk/scripts/integracao/custom/";
/*
* Esta função retorna informações sobre o tipo de integração.
*/
$row = GetInfoMetodo($idMetodo);
/*
* Se o metodo informado não for custom, segue o fluxo normal.
*/
$tipo = strtoupper($row["itgp_prefix"]);
if ($tipo != 'CUSTOM') {
return false;
}
/*
* Verifica se o script informado existe, caso não exista registra no log e encerra.
*/
$return = explode(' ', $row["itgm_comando"]);
$script = trim($return[0]);
if (!file_exists($patScript . $script)) {
__logStr("ExecuteCustom", "O arquivo informado não existe! Nome: $script", "serverAgi.php", true);
return true;
}
/*
* Executa o script customizado.
*/
$nomeMetodo = sprintf("%s%s", $patScript, $script);
/*
* Registra a execução do script.
*/
__logStr("ExecuteCustom", $nomeMetodo, "serverAgi.php", false);
/*
* Este script é executado em outro processo para permitir o contro de timeout das querys,
* assim o processo é encerrado caso não retorne no tempo máximo definido para timeout.
*/
return true;
} catch (Exception $ex) {
/*
* Devolve a execução para integraçao ativa.
*/
__logStr("ExecuteCustom", $ex->getMessage(), "serverAgi.php", true);
return false;
}
}
?>