Project

General

Profile

Download (3.22 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * copyget.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2024 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 * http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23

    
24
/*
25
	Provides the functionality required to fetch updated copyright information from Netgate
26
*/
27

    
28
require_once("guiconfig.inc");
29

    
30
$copyrightfile = "{$g['cf_conf_path']}/copyright";
31
$tmpfile = "{$g['cf_conf_path']}/copyright.tmp";
32
$idfile = "/var/db/uniqueid";
33
$FQDN = "https://ews.netgate.com/copyright";
34
$refreshinterval = (24 * 3600);	// 24 hours
35

    
36
// Poll the Netgate server to obtain the JSON/HTML formatted support information
37
// and write it to the JSON file
38
function updatecopyright() {
39
	global $g, $copyrightfile, $tmpfile, $idfile, $FQDN;
40

    
41
	if (file_exists($idfile)) {
42
		if (function_exists('curl_version')) {
43
			$post = ['uid' => file_get_contents($idfile), 'language' => '0', 'edition' => 'community', 'version' => '2'];
44
			$url = $FQDN;
45

    
46
			$ch = curl_init();
47
			curl_setopt($ch, CURLOPT_HEADER, 0);
48
			curl_setopt($ch, CURLOPT_VERBOSE, 0);
49
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
50
			curl_setopt($ch, CURLOPT_USERAGENT, g_get('product_label') . '/' . g_get('product_version'));
51
			curl_setopt($ch, CURLOPT_URL, $url);
52
			curl_setopt($ch, CURLOPT_POST, true);
53
			curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
54
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,4);
55
			set_curlproxy($ch);
56

    
57
			$response = curl_exec($ch);
58
			$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
59
			curl_close($ch);
60

    
61
			if ($status == 200) {
62
				// Save to a temporary file
63
				file_put_contents($tmpfile, $response);
64
				// If the file contents are not the same as the existing file, create the trigger to display the new copyright
65
				if (!file_exists($copyrightfile) || !compare_copytext(file_get_contents($copyrightfile), $response)) {
66
					touch("{$g['cf_conf_path']}/copynotice_display");
67
				}
68

    
69
				rename($tmpfile, $copyrightfile);
70
			}
71
		}
72
	}
73
}
74

    
75
// Retrieve the copyright data from Netgate.com if
76
// the support data file does not exist, or
77
// if it is more than a day old and the URL seems resolvable
78
if ((!file_exists($copyrightfile) || (filesize($copyrightfile) == 0) ||
79
    ((time()-filemtime($copyrightfile) > $refreshinterval))) && resolve_address($FQDN)) {
80
	updateCopyright();
81
}
82

    
83
// Compare the copyright text contained in the first HTML <DIV>
84
function compare_copytext ($new, $old) {
85
	if ((strlen($new) > 0) && (strlen($old) > 0)) {
86
		$new_divend = strpos($new, "</div>");
87
		$old_divend = strpos($old, "</div>");
88

    
89
		if ($new_divend && $old_divend) {
90
			return(md5(substr($new, 0, $new_divend)) === md5(substr($old, 0, $old_divend)));
91
		}
92
	}
93

    
94
	return false;
95
}
(12-12/61)