Skip to content

Commit

Permalink
Small changes to make it work on newer php under fpm
Browse files Browse the repository at this point in the history
  • Loading branch information
asternic committed Oct 8, 2023
1 parent ee18019 commit 3b50990
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
+----------------------------------------------------------------------+
| Autores: Alex Villacís Lasso <[email protected]> |
+----------------------------------------------------------------------+
$Id: paloEndpointScanStatus.class.php, Thu 20 May 2021 08:29:23 AM EDT, [email protected]
$Id: paloEndpointScanStatus.class.php, Sun 08 Oct 2023 06:01:26 PM EDT, [email protected]
*/

/*
Expand Down Expand Up @@ -56,20 +56,20 @@ function __construct()
}

function createEmptyResponse() { return array('endpointchanges' => array()); }
function isEmptyResponse($jsonResponse) { return (count($jsonResponse['endpointchanges']) <= 0); }
function isEmptyResponse($jsonResponse) { return (count($jsonResponse['endpointchanges']) <= 0); }

function findInitialStateDifferences(&$currentClientState, &$jsonResponse)
{
// Se asume que la verificación inicial tiene un archivo de socket
$errno = $errstr = NULL;
$this->_scanSockPath = $currentClientState['scanSocket'];
$this->_scanSock = @fsockopen('unix://'.$this->_scanSockPath, -1, $errno, $errstr);
$this->_scanSock = stream_socket_client('unix://'.$this->_scanSockPath, $errno, $errstr, -1, STREAM_CLIENT_CONNECT);

if (FALSE === $this->_scanSock) {
/* La causa más probable de fallo en abrir es que el escaneo ya
* terminó. Se verifica contra la base de datos por si hay
* diferencias, las cuales se anotan en $jsonResponse. */
$this->_scanSock = NULL;
$this->_scanSock = NULL;
$currentClientState['scanSocket'] = $this->_scanSockPath = NULL;
$this->_buscarCambioGlobalEndpoints($currentClientState['endpoints'], $jsonResponse['endpointchanges']);
$currentClientState['endpoints'] = NULL;
Expand All @@ -78,10 +78,10 @@ function findInitialStateDifferences(&$currentClientState, &$jsonResponse)
} else {
stream_set_blocking($this->_scanSock, 0);

while ($s = fgets($this->_scanSock)) {
while (($s = fgets($this->_scanSock)) !== false) {
/* La línea leída puede ser 'quit' si el escaneo termina, o una
* tupla (insert|update|delete id_endpoint) */
$s = trim($s);
$s = trim($s);
if ($s == 'quit') {
fclose($this->_scanSock);
$this->_scanSock = NULL;
Expand Down Expand Up @@ -136,8 +136,8 @@ function findEventStateDifferences(&$currentClientState, &$jsonResponse)
if (is_null($this->_scanSock)) {
$currentClientState['scanSocket'] = $this->_scanSockPath = NULL;
$this->_buscarCambioGlobalEndpoints($currentClientState['endpoints'], $jsonResponse['endpointchanges']);
$currentClientState['endpoints'] = NULL;
$jsonResponse['endpointchanges'][] = array('quit', NULL);
$currentClientState['endpoints'] = NULL;
$jsonResponse['endpointchanges'][] = array('quit', NULL);
return FALSE;
} else return TRUE;
}
Expand Down
84 changes: 42 additions & 42 deletions modules/endpoint_configurator/libs/paloServerSentEvents.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
Codificación: UTF-8
+----------------------------------------------------------------------+
| Issabel version 1.0 |
| Issabel version 5.0 |
| http://www.issabel.org |
+----------------------------------------------------------------------+
| Copyright (c) 2021 Issabel Foundation |
| Copyright (c) 2023 Issabel Foundation |
| Copyright (c) 2006 Palosanto Solutions S. A. |
+----------------------------------------------------------------------+
| The contents of this file are subject to the General Public License |
Expand All @@ -21,7 +21,7 @@
+----------------------------------------------------------------------+
| Autores: Alex Villacís Lasso <[email protected]> |
+----------------------------------------------------------------------+
$Id: paloServerSentEvents.class.php, Thu 20 May 2021 08:29:54 AM EDT, [email protected]
$Id: paloServerSentEvents.class.php, Sun 08 Oct 2023 06:04:03 PM EDT, [email protected]
*/
require_once 'libs/misc.lib.php';
require_once 'libs/paloSantoJSON.class.php';
Expand All @@ -31,52 +31,52 @@ class paloServerSentEvents
private $_implementation;
private $_module_name;
private $_debug = FALSE;

private function debug($s)
{
if ($this->_debug) {
file_put_contents('/tmp/debug-sse-core.txt', $s, FILE_APPEND);
}
if ($this->_debug) {
file_put_contents('/tmp/debug-sse-core.txt', $s, FILE_APPEND);
}
}

function __construct($module_name, $implClass)
{
$this->_module_name = $module_name;
if (is_object($implClass)) {
$this->_implementation = $implClass;
$this->_implementation = $implClass;
} else {
$this->_implementation = new $implClass;
$this->_implementation = new $implClass;
}
}

function handle()
{
$jsonResponse = array();
ignore_user_abort(true);

ignore_user_abort(true);
set_time_limit(0);

// Estado del lado del cliente
$estadoHash = getParameter('clientstatehash');
if (!is_null($estadoHash)) {
$estadoCliente = isset($_SESSION[$this->_module_name]['estadoCliente'])
? $_SESSION[$this->_module_name]['estadoCliente']
: array();
$estadoCliente = isset($_SESSION[$this->_module_name]['estadoCliente'])
? $_SESSION[$this->_module_name]['estadoCliente']
: array();
} else {
$estadoCliente = getParameter('clientstate');
if (!is_array($estadoCliente)) return;
}

// Modo a funcionar: Long-Polling, o Server-sent Events
$sModoEventos = getParameter('serverevents');
$bSSE = (!is_null($sModoEventos) && $sModoEventos);
$bSSE = (!is_null($sModoEventos) && $sModoEventos);
if ($bSSE) {
Header('Content-Type: text/event-stream');
$this->_printflush("retry: 1\n");
} else {
Header('Content-Type: application/json');
}

// Verificar hash correcto
if (!is_null($estadoHash) && $estadoHash != $_SESSION[$this->_module_name]['estadoClienteHash']) {
$jsonResponse['estadoClienteHash'] = 'mismatch';
Expand All @@ -85,60 +85,60 @@ function handle()
return;
}

$this->debug("Estado inicial: ".print_r($estadoCliente, 1));
$this->debug("Estado inicial: ".print_r($estadoCliente, 1));
$jsonResponse = $this->_implementation->createEmptyResponse();
$bKeepListening = $this->_implementation->findInitialStateDifferences($estadoCliente, $jsonResponse);
if (!$bKeepListening) {
$this->debug("Estado inicial aborta la escucha: ".print_r($estadoCliente, 1));
$this->debug("Respuesta inicial aborta la escucha: ".print_r($jsonResponse, 1));
$this->debug("Respuesta inicial aborta la escucha: ".print_r($jsonResponse, 1));
$jsonResponse['estadoClienteHash'] = self::generarEstadoHash($this->_module_name, $estadoCliente);
$this->_jsonflush($bSSE, $jsonResponse);
} else {
$this->_implementation->setupBeforeEventLoop();
$iTimeoutPoll = $this->_suggestEventTimeout();
do {
$this->_implementation->setupBeforeEventWait();
$this->_implementation->setupBeforeEventWait();

// Se inicia espera larga con el navegador...
$iTimestampInicio = time();
$this->debug("Respuesta antes de while: ".print_r($jsonResponse, 1));
$this->debug("Estado antes de while: ".print_r($estadoCliente, 1));
while (connection_status() == CONNECTION_NORMAL
&& $this->_implementation->isEmptyResponse($jsonResponse)
$this->debug("Respuesta antes de while: ".print_r($jsonResponse, 1));
$this->debug("Estado antes de while: ".print_r($estadoCliente, 1));
while (connection_status() == CONNECTION_NORMAL
&& $this->_implementation->isEmptyResponse($jsonResponse)
&& time() - $iTimestampInicio < $iTimeoutPoll) {
session_commit();

//session_commit();
if (!$this->_implementation->waitForEvents()) {
$jsonResponse['error'] = $this->_implementation->getErrMsg();
$this->_jsonflush($bSSE, $jsonResponse);
$this->_implementation->shutdown();
return;
}
@session_start();
if (isset($_SESSION[$this->_module_name])
//session_start();

if (isset($_SESSION[$this->_module_name])
&& $this->_implementation->checkInvalidatedWait($estadoCliente, $_SESSION[$this->_module_name]['estadoCliente'])) {
$jsonResponse['estadoClienteHash'] = 'invalidated';
$this->_jsonflush($bSSE, $jsonResponse);
$this->_implementation->shutdown();
return;
}

$bKeepListening = $this->_implementation->findEventStateDifferences($estadoCliente, $jsonResponse);
$this->debug("Diferencias encontradas ($bKeepListening): ".print_r($jsonResponse, 1));
$this->debug("Estado modificado: ".print_r($estadoCliente, 1));
$this->debug("Estado modificado: ".print_r($estadoCliente, 1));
}

$jsonResponse['estadoClienteHash'] = self::generarEstadoHash($this->_module_name, $estadoCliente);
$this->_jsonflush($bSSE, $jsonResponse);

$jsonResponse = $this->_implementation->createEmptyResponse();
} while ($bSSE && $bKeepListening && connection_status() == CONNECTION_NORMAL);
}

$this->_implementation->shutdown();
}

private function _suggestEventTimeout()
{
$iTimeoutPoll = 2 * 60;
Expand All @@ -159,23 +159,23 @@ private function _jsonflush($bSSE, $jsonResponse)
$this->_printflush("data: $r\n\n");
else $this->_printflush($r);
}

private function _printflush($s)
{
print $s;
$this->debug($s);
ob_flush();
flush();
}

static function generarEstadoHash($module_name, $estadoCliente)
{
$estadoHash = md5(serialize($estadoCliente));
$_SESSION[$module_name]['estadoCliente'] = $estadoCliente;
$_SESSION[$module_name]['estadoClienteHash'] = $estadoHash;

return $estadoHash;
}

}
?>

0 comments on commit 3b50990

Please sign in to comment.