Skip to content

Commit

Permalink
Add option to send events to a configurable remote event listener
Browse files Browse the repository at this point in the history
A new configuration option "remote_events_url" is declared. When defined
on each new event, AgilityContest try to send json encoded data to provided URL.
Is up to the listener, enqueue, parse and handle received events
  • Loading branch information
jonsito committed Sep 17, 2021
1 parent 119a223 commit d7662ac
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
7 changes: 6 additions & 1 deletion agility/console/dialogs/dlg_configuracion.inc
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,9 @@ $config =Config::getInstance();
<input type="hidden" name="register_events" value="0"/> <!-- trick for checkboxes -->
<input type="checkbox" name="register_events" id="register_events" value="1"/><br />

<label for="remote_events_url"> <?php _e('Remote events listener URL'); ?> </label>
<input id="remote_events_url" name="remote_events_url" type="text" size="48" value=""/>

<label for="event_printer"><?php _e('Printer device name to write tablet reports'); ?></label>
<input id="event_printer" name="event_printer" type="text" size="10" value=""/> <?php _e('Paper size'); ?>:
<label for="wide_printer58" style="width:70px;">
Expand Down Expand Up @@ -617,6 +620,7 @@ $config =Config::getInstance();
$('#email_realm').textbox();
$('#email_workstation').textbox();
$('#ip_address').textbox().textbox('setValue','<?php $a=new networkInterfaces(); echo implode(" - ",$a->get_interfaces());?>');
$('#remote_events_url').textbox();
$('#event_printer').textbox();
$('#backup_dir').textbox();

Expand All @@ -628,7 +632,8 @@ $config =Config::getInstance();
textField:'Name'
});

addTooltip($('#event_printer').linkbutton(),'<?php _e("Enter printer name/device or leave empty to disable"); ?>');
addTooltip($('#remote_events_url').textbox('textbox'),'<?php _e("Enter (optional) URL to send events to (or leave empty)"); ?>');
addTooltip($('#event_printer').textbox('textbox'),'<?php _e("Enter printer name/device or leave empty to disable"); ?>');
addTooltip($('#config-resetBtn').linkbutton(),'<?php _e("Cancel changes. Restore information from server"); ?>');
addTooltip($('#config-defaultBtn').linkbutton(),'<?php _e("Factory reset. Configure with default data"); ?>');
addTooltip($('#config-okBtn').linkbutton(),'<?php _e("Accept changes. Save as new configuration"); ?>');
Expand Down
2 changes: 2 additions & 0 deletions agility/server/auth/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
/** logging **/
define('AC_DEBUG_LEVEL','0');
define('AC_REGISTER_EVENTS',"0"); // write events into file
define('AC_REMOTE_EVENTS_URL',""); // URL to send events outside server. format: http(s)://{host}:{port}/{baseurl}
define('AC_CONSOLE_EVENTS',"0"); // track and show events in cosole
define('AC_CONSOLE_NEWS',"1"); // ask server for news on startup
define('AC_RESET_EVENTS',"1"); // clear event registry on login
Expand Down Expand Up @@ -196,6 +197,7 @@
// version, logging y depuracion
'debug_level' => array( 'i', false, AC_DEBUG_LEVEL),
'register_events' => array( 'b', false, AC_REGISTER_EVENTS),
'remote_events_url'=> array( 's', false, AC_REMOTE_EVENTS_URL),
'reset_events' => array( 'b', false, AC_RESET_EVENTS),
'console_events' => array( 'b', false, AC_CONSOLE_EVENTS),
'console_news' => array( 'i', false, AC_CONSOLE_NEWS),
Expand Down
54 changes: 50 additions & 4 deletions agility/server/database/classes/Eventos.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ function putEvent(&$data) {
$data['TimeStamp']=$timestamp;
$data['ID']=$this->conn->insert_id;
$stmt->close();

// and save content to event file
// if saving event file is requested, handle it
$flag=$this->myConfig->getEnv("register_events");
$str=json_encode($data);
if (boolval($flag)) {
Expand All @@ -234,8 +233,14 @@ function putEvent(&$data) {
// just overwrite event file with last event
file_put_contents($this->sessionFile,$str."\n",LOCK_EX);
}
// if printer is enabled, send every "accept" events
if ($data['Type']=='aceptar') {
// if a remote server to send events is declared, try also to send data
$revents_url=$this->myConfig->getEnv("remote_events_url");
if (! is_null($revents_url) && trim($revents_url) != "") {
$this->uploadEvent($revents_url,$str);
}
// if raw printer is enabled, send every "accept" events
$printerName=$this->myConfig->getEnv("event_printer");
if ( (! is_null($printerName)) && (trim($printerName) != "") && ($data['Type']=='aceptar') ) {
$p=new RawPrinter();
$p->rawprinter_Print($data);
}
Expand All @@ -244,6 +249,47 @@ function putEvent(&$data) {
return "";
}

/**
* Send event to server as json data. Ignore response
* and receive answer
* @param {string} $host
* @param {int} $port
* @throws Exception
*/
function uploadEvent($url,$data) {
// first of all, check internet conectivity
if (isNetworkAlive()<0) {
throw new Exception("updater::SendJSONRequest(): No internet access available");
}
// do not verify cert on localhost
$checkcert=true;
if (strpos($url,"localhost")!=false) $checkcert=false;
if (strpos($url,"127.0.0.1")!=false) $checkcert=false;
$hdata=array(
"Operation" => 'putEvent'
);
$pdata=array(
'Data' => $data
);
// prepare and execute json request
$curl = curl_init($url."?".http_build_query($hdata) );
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // allow server redirection
curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); // try to fix some slowness issues in windozes
curl_setopt($curl, CURLOPT_POSTREDIR, 1); // do not change from post to get on "301 redirect"
curl_setopt($curl, CURLOPT_POSTFIELDS, $pdata );
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $checkcert); // set to false when using "localhost" url
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT, 5); // wait 5 secs to attemp connect

$json_response = @curl_exec($curl); // supress stdout warning
if ( curl_error($curl) ) {
$this->myLogger->error("event::uploadEvent() call to URL $url failed: " . curl_error($curl) );
}
// close curl stream
curl_close($curl);
}

/**
* send 'reconfig' event to every sessions
*/
Expand Down

0 comments on commit d7662ac

Please sign in to comment.