summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSteve Beaver <sbeaver@netgate.com>2017-08-10 13:56:19 -0400
committerSteve Beaver <sbeaver@netgate.com>2017-08-10 13:57:10 -0400
commitc2e98b94a1ffa6a46090a604e1ee59b6b312aae9 (patch)
treeb3389ff8a234e6e27bea9128f7a43ce0e0d31353 /src
parent1371aa1a054d7816eafa90ffd904257de384d947 (diff)
downloadpfsense-c2e98b94a1ffa6a46090a604e1ee59b6b312aae9.zip
pfsense-c2e98b94a1ffa6a46090a604e1ee59b6b312aae9.tar.gz
Revised Netgate Services and Support widget to use AJAX when refreshing the data
Diffstat (limited to 'src')
-rw-r--r--src/usr/local/www/widgets/widgets/netgate_services_and_support.widget.php120
1 files changed, 85 insertions, 35 deletions
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\":\"<div class=\\\"alert alert-danger\\\">", "</div>\",\"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("<div>");
-print($json['summary']);
-if (strlen($json['htmltext']) > 0) {
- print('<div class="panel-body" style="padding-left:15px; padding-right:15px;">');
- print('<hr style="margin-top:0px">');
+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('</div>');
+ $doajax = "no";
+} else {
+ //Print empty <div>s and request the data by AJAX
+ print(sprintf(gettext("%sRetrieving support information %s %s"),
+ "<div id=\"summary\" class=\"alert alert-warning\">", "<i class=\"fa fa-cog fa-spin\"></i>", "</div><div id=\"htmltxt\"></div>"));
}
// Print a low-key refresh link
@@ -111,3 +122,42 @@ print('<div style="text-align:right;padding-right:15px;"><a href="/widgets/widge
print("</div>");
?>
+
+<script type="text/javascript">
+//<![CDATA[
+ events.push(function(){
+ function fetch_spt_data() {
+
+ $.ajax({
+ type: 'POST',
+ url: "/widgets/widgets/netgate_services_and_support.widget.php",
+ data: {
+ ajax: "ajax"
+ },
+
+ success: function(data){
+ if (data.length > 0) {
+ var obj = JSON.parse(data);
+
+ $('#summary').removeClass("alert");
+ $('#summary').removeClass("alert-warning");
+ $('#summary').html(obj.summary);
+ $('#htmltxt').html(obj.htmltext);
+ }
+ },
+
+ error: function(e){
+ // alert("Error: " + e);
+
+ }
+ });
+ }
+
+ if ("<?=$doajax?>" === "yes") {
+ fetch_spt_data();
+ }
+ });
+
+
+//]]>
+</script>
OpenPOWER on IntegriCloud