Project

General

Profile

Download (3.73 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-2021 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, $config;
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['product_label'] . '/' . $g['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

    
56
			if (!empty($config['system']['proxyurl'])) {
57
				curl_setopt($ch, CURLOPT_PROXY, $config['system']['proxyurl']);
58
				if (!empty($config['system']['proxyport'])) {
59
					curl_setopt($ch, CURLOPT_PROXYPORT, $config['system']['proxyport']);
60
				}
61
				if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
62
					@curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY | CURLAUTH_ANYSAFE);
63
					curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$config['system']['proxyuser']}:{$config['system']['proxypass']}");
64
				}
65
			}
66

    
67
			$response = curl_exec($ch);
68
			$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
69
			curl_close($ch);
70

    
71
			if ($status == 200) {
72
				// Save to a temporary file
73
				file_put_contents($tmpfile, $response);
74
				// If the file contents are not the same as the existing file, create the trigger to display the new copyright
75
				if (!file_exists($copyrightfile) || !compare_copytext(file_get_contents($copyrightfile), $response)) {
76
					touch("{$g['cf_conf_path']}/copynotice_display");
77
				}
78

    
79
				rename($tmpfile, $copyrightfile);
80
			}
81
		}
82
	}
83
}
84

    
85
// Retrieve the copyright data from Netgate.com if
86
// the support data file does not exist, or
87
// if it is more than a day old and the URL seems resolvable
88
if (!file_exists($copyrightfile) || (filesize($copyrightfile) == 0) ||
89
    ((time()-filemtime($copyrightfile) > $refreshinterval) && is_url_hostname_resolvable($FQDN))) {
90
	updateCopyright();
91
}
92

    
93
// Compare the copyright text contained in the first HTML <DIV>
94
function compare_copytext ($new, $old) {
95
	if ((strlen($new) > 0) && (strlen($old) > 0)) {
96
		$new_divend = strpos($new, "</div>");
97
		$old_divend = strpos($old, "</div>");
98

    
99
		if ($new_divend && $old_divend) {
100
			return(md5(substr($new, 0, $new_divend)) === md5(substr($old, 0, $old_divend)));
101
		}
102
	}
103

    
104
	return false;
105
}
(12-12/61)