summaryrefslogtreecommitdiffstats
path: root/src/usr/local/www/widgets/widgets/netgate_services_and_support.widget.php
blob: 0d8b28b7febcde15c10afc676da8a9507dfba30b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/*
 * netgate_services_and_support.widget.php
 *
 * part of pfSense (https://www.pfsense.org)
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
	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 containing 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


if ($_REQUEST['ajax']) {

	// Retrieve the support data from Netgate.com if the support 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);
    }

    header("Location: /");
	exit;
}

// Poll the Netgate server to obtain the JSON/HTML formatted support information
// and write it to the JSON file
function updateSupport() {
	global $g, $supportfile, $idfile, $FQDN;

	if (file_exists($idfile)) {
		if (function_exists('curl_version')) {
			$post = ['uid' => file_get_contents($idfile), 'language' => '0'];
			$url = $FQDN;

			$ch = curl_init();
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_VERBOSE, 0);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_USERAGENT, $g['product_name'] . '/' . $g['product_version']);
			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);
			}
		}
	}
}


$doajax = "yes";

print("<div>");

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']);
	$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
print('<div style="text-align:right;padding-right:15px;"><a href="/widgets/widgets/netgate_services_and_support.widget.php?act=refresh" usepost><i class="fa fa-refresh"></i></a></div>');

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