From c2e98b94a1ffa6a46090a604e1ee59b6b312aae9 Mon Sep 17 00:00:00 2001 From: Steve Beaver Date: Thu, 10 Aug 2017 13:56:19 -0400 Subject: Revised Netgate Services and Support widget to use AJAX when refreshing the data --- .../netgate_services_and_support.widget.php | 120 +++++++++++++++------ 1 file changed, 85 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/usr/local/www/widgets/widgets/netgate_services_and_support.widget.php b/src/usr/local/www/widgets/widgets/netgate_services_and_support.widget.php index 4a045a1..f1ebf7e 100644 --- a/src/usr/local/www/widgets/widgets/netgate_services_and_support.widget.php +++ b/src/usr/local/www/widgets/widgets/netgate_services_and_support.widget.php @@ -20,26 +20,52 @@ */ /* - This widget transmits the device ID to Netgate's REST API and retrieves the support information. + This widget transmits the Netgate Device ID to Netgate's REST API, and retrieves the support information. The connection is made using HTTPS/TLS. No other data is transmitted. If the widget is not enabled, then no transmission is made + + If the file ontaining the support data exists on the file system and is less than 24 hours old + the file contents are displayed immediately. If not, an AJAX call is made to retrieve fresh information */ +require_once("guiconfig.inc"); + $nocsrf = true; $supportfile = "/var/db/support.json"; $idfile = "/var/db/uniqueid"; $FQDN = "https://ews.netgate.com/support"; $refreshinterval = (24 * 3600); // 24 hours -// Write a dummy support file containg an error message -function nosupportdata() { - global $supportfile; - file_put_contents($supportfile, sprintf(gettext("%sSupport information could not be retrieved%s"), - "{\"summary\":\"
", "
\",\"htmltext\":\"\"}")); +if ($_REQUEST['ajax']) { + + // Retrieve the support data from Netgate.com if the supprt data file does not exist, + // or if it is more than a day old + if (!file_exists($supportfile) || ( time()-filemtime($supportfile) > $refreshinterval)) { + if (file_exists($supportfile)) { + unlink($supportfile); + } + + updateSupport(); + } + + if (file_exists($supportfile)) { + print(file_get_contents($supportfile)); + } + + exit; +} + +// If the widget is called with act=refresh, delete the JSON file and reload the page, thereby forcing the +// widget to get a fresh copy of the support information +if ($_REQUEST['act'] == "refresh") { + + if (file_exists($supportfile)) { + unlink($supportfile); + } - // Make the file {refreshinterval} old so that the widget tries again on the next page load - touch($supportfile, (time() - $refreshinterval)); + header("Location: /"); + exit; } // Poll the Netgate server to obtain the JSON/HTML formatted support information @@ -47,8 +73,6 @@ function nosupportdata() { function updateSupport() { global $g, $supportfile, $idfile, $FQDN; - $success = false; - if (file_exists($idfile)) { if (function_exists('curl_version')) { $post = ['uid' => file_get_contents($idfile), 'language' => '0']; @@ -62,47 +86,34 @@ function updateSupport() { curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post)); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,4); $response = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($status == 200) { file_put_contents($supportfile, $response); - $success = true; } } } - - if (!$success) { - nosupportdata(); - } } -// If the widget is called with act=refresh, delete the JSON file and reload the page, thereby forcing the -// widget to get a fresh copy of the support information -if ($_REQUEST['act'] == "refresh") { - unlink($supportfile); - header("Location: /"); - exit; -} -// Retrieve the support data from Netgate.com if the supprt data file does not exist, -// or if it is more than a day old -if (!file_exists($supportfile) || ( time()-filemtime($supportfile) > $refreshinterval)) { - updateSupport(); -} - -$str = file_get_contents($supportfile); -$json = json_decode($str, true); +$doajax = "yes"; print("
"); -print($json['summary']); -if (strlen($json['htmltext']) > 0) { - print('
'); - print('
'); +if (file_exists($supportfile) && ( time()-filemtime($supportfile) < $refreshinterval)) { + // Print the support data from the file + $str = file_get_contents($supportfile); + $json = json_decode($str, true); + print($json['summary']); print($json['htmltext']); - print('
'); + $doajax = "no"; +} else { + //Print empty
s and request the data by AJAX + print(sprintf(gettext("%sRetrieving support information %s %s"), + "
", "", "
")); } // Print a low-key refresh link @@ -111,3 +122,42 @@ print('
"); ?> + + -- cgit v1.1