PABX da Simples IP
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.
 
 
 
 
 
 

202 lines
5.6 KiB

<?php
function GetSocket($host, $port, $login, $pass, &$erro)
{
$port = empty($port ) ? "5038" : $port;
$errno = 0;
$errstr = "";
$erro = "";
$tentaConexao = 0;
$timeout = 10;
// $socket = fsockopen("192.168.80.13","5038", $errno, $errstr, $timeout);
$socket = @fsockopen($host, $port, $errno, $errstr, $timeout);
if(!$socket)
{
$erro = "Não foi possível estabelecer uma conexão com o servidor! Erro: Nº: $errno Msg:$errstr ";
return false;
}
else
{
fwrite($socket, "action: login\r\n");
fwrite($socket, "username: $login\r\n");
fwrite($socket, "secret: $pass\r\n");
fwrite($socket, "Events: off\r\n");
$actionid=rand(000000000,9999999999);
fwrite($socket, "actionid: ".$actionid."\r\n\r\n");
usleep(500);
while (!feof($socket))
{
$buffer=fgets($socket);
if(stristr($buffer,"Authentication accepted"))
{
$erro = "";
return $socket;
}
elseif(stristr($buffer,"Authentication failed"))
{
$erro = "Não foi possível se logar-se no servidor!";
return false;
}
else if($tentaConexao == MAX_CONECT_SOCKET)
{
$erro = "Não foi possível se logar-se no servidor, o número máximo de tentavas foi excedido!";
return false;
}
$tentaConexao++;
usleep(500000);
}
}
}
function GetDataSocket($socket, $comando, $evento, &$dataInfo)
{
/*
* O array "chaves" determina os valores que vão ser recuperados,
* o índice "0"($chaves[0]) é o valor que representa o início
* do bloco de informações ex.:
* $chaves = array("Agents", "Agent");
* Primeira linha do bloco = "Event: Agents"
* $chave[0] = "Agents"; //Início de bloco
* Segunda linha do bloco = Agent: 1000
* $chave[1] = "Agent"; //retorna o valor "1000"
*/
$buffer = "";
$evt_fim = trim($evento);
$linhas = 0;
$actionid=rand(000000000,9999999999);
$actionid="actionid: ".$actionid."\r\n";
$cmd .= $comando . $actionid."\r\n";
fwrite($socket, $cmd);
usleep(500);
$idx = -1;
while(!feof($socket))
{
$buffer = trim(fgets($socket));
if(stristr($buffer,$evt_fim))
{
return true;
}
else if(!empty($buffer))
{
$dataInfo[] = $buffer;
}
//Garante que o script nao vai rodar infinitamente
if(trim($buffer) == "")
{
$linhas++;
}
if($linhas > 30)
{
return false;
}
}
return false;
}
function GetInfoDataSocket($source,$chaves)
{
$buffer = "";
$chave = "";
$value = "";
$ch = array();
$dataInfo = array();
foreach($source as $buffer)
{
$ch = split(':', $buffer);
$chave = trim($ch[0]);
$value = trim($ch[1]);
if($value == $chaves[0])
$idx++;
if($chaves && array_search($chave, $chaves))
$dataInfo[$idx][$chave] = $value;
}
return $dataInfo;
}
function GetSatatusAgt($status)
{
if($status == "")
return "Ocupado";
else if($status == "Livre")
return "";
else if($status == "Pausa")
return "";
}
/*
function GetInfoSoket($socket, $comando, $evento, $chaves)
{
*
* O array "chaves" determina os valores que vão ser recuperados,
* o índice "0"($chaves[0]) é o valor que representa o início
* do bloco de informações ex.:
* $chaves = array("Agents", "Agent");
* Primeira linha do bloco = "Event: Agents"
* $chave[0] = "Agents"; //Início de bloco
* Segunda linha do bloco = Agent: 1000
* $chave[1] = "Agent"; //retorna o valor "1000"
*
$buffer = "";
$data = "";
$evt_fim = trim($evento);
$linhas = 0;
$actionid=rand(000000000,9999999999);
$actionid="actionid: ".$actionid."\r\n";
$cmd .= $comando . $actionid."\r\n";
fwrite($socket, $cmd);
usleep(500);
$dataInlfo = array();
$idx = -1;
while(!feof($socket))
{
$buffer = trim(fgets($socket));
$data .= $buffer . "<br>";
$ch = split(':', $buffer);
$chave = trim($ch[0]);
$value = trim($ch[1]);
if($value == $chaves[0])
{
$idx++;
}
if($chaves && array_search($chave, $chaves))
{
$dataInlfo[$idx][$chave] = $value;
}
if(stristr($buffer,$evt_fim))
{
return $dataInlfo;
}
*
* Garante que o script nao vai rodar infinitamente
* se nao encontrar o evento de finalização
*
if(trim($buffer) == "")
{
$linhas++;
}
if($linhas > 30)
{
return $dataInlfo;
}
}
return $data;
}
*/
?>