Project

General

Profile

Download (7.4 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/* $Id$ */
4
/*
5
	system_firmware_auto.php
6
	Copyright (C) 2005 Scott Ullrich
7

    
8
        Based originally on system_firmware.php
9
        (C)2003-2004 Manuel Kasper
10
	All rights reserved.
11

    
12
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14

    
15
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17

    
18
	2. Redistributions in binary form must reproduce the above copyright
19
	   notice, this list of conditions and the following disclaimer in the
20
	   documentation and/or other materials provided with the distribution.
21

    
22
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
*/
33

    
34
require("guiconfig.inc");
35

    
36
$curcfg = $config['system']['firmware'];
37

    
38
if(isset($curcfg['alturl']['enable'])
39
	$updater_url = "{$config['system']['firmware']['alturl']['firmwareurl']}";
40

    
41
$pgtitle = array("Diagnostics","Firmware","Auto Update");
42
include("head.inc");
43

    
44
?>
45

    
46
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
47
<link href="gui.css" rel="stylesheet" type="text/css">
48
</head>
49

    
50
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
51

    
52
<?php include("fbegin.inc"); ?>
53

    
54
<form action="system_firmware_auto.php" method="post">
55
<table width="100%" border="0" cellpadding="6" cellspacing="0">
56
	<tr>
57
		<td>
58
<?php
59
	$tab_array = array();
60
	$tab_array[0] = array("Manual Update", true, "system_firmware.php");
61
	$tab_array[1] = array("Auto Update", false, "system_firmware_check.php");
62
	$tab_array[2] = array("Updater Settings", false, "system_firmware_settings.php");
63
	display_top_tabs($tab_array);
64
?>
65
		</td>
66
	</tr>
67
	<tr>
68
	  <td class="tabcont">
69
	      <table width="100%" border="0" cellpadding="6" cellspacing="0">
70
			  <tr>
71
			    <td class="tabcont">
72
					<table width="100%" border="0" cellpadding="6" cellspacing="0">
73
						<tr>
74
							<td>
75
								<center>
76
								<table height='15' width='420' border='0' colspacing='0' cellpadding='0' cellspacing='0'>
77

    
78
								<tr>
79
									<td background="./themes/the_wall/images/misc/bar_left.gif" height='15' width='5'>
80
									</td>
81
									<td>
82
									<table id="progholder" name="progholder" height='15' width='410' border='0' colspacing='0' cellpadding='0' cellspacing='0'>
83
										<td background="./themes/the_wall/images/misc/bar_gray.gif" valign="top" align="left">
84
											<img src='./themes/the_wall/images/misc/bar_blue.gif' width='0' height='15' name='progressbar' id='progressbar'>
85
										</td>
86
									</table>
87
								</td>
88
								<td background="./themes/the_wall/images/misc/bar_right.gif" height='15' width='5'>
89
								</td>
90
							</tr>
91
						</table>
92
						<br>
93
						<!-- status box -->
94
						<textarea cols="60" rows="1" name="status" id="status" wrap="hard">Beginning package installation.</textarea>
95
						<!-- command output box -->
96
						<textarea cols="60" rows="25" name="output" id="output" wrap="hard"></textarea>
97
					</center>
98
					</td>
99
				</tr>
100
	      </table>
101
		</table>
102
	  </td>
103
	</tr>
104
</table>
105
</form>
106
<?php include("fend.inc"); ?>
107
</body>
108
</html>
109

    
110
<?php
111

    
112
update_status("Downloading current version information...");
113
$latest_version = download_file_with_progress_bar("{$updater_url}/version", "/tmp/{$g['product_name']}_version");
114

    
115
$current_installed_pfsense_version = str_replace("\n", "", file_get_contents("/etc/version"));
116
$latest_version = str_replace("\n", "", file_get_contents("/tmp/{$g['product_name']}_version"));
117

    
118
if($current_installed_pfsense_version <> $latest_version) 
119
	$needs_system_upgrade = true;
120

    
121
if($needs_system_upgrade == true) {
122
	update_status("Downloading updates ...");
123
	$status = download_file_with_progress_bar("{$updater_url}/latest.tgz", "/tmp/latest.tgz");	
124
	$status = download_file_with_progress_bar("{$updater_url}/latest.tgz.sha256", "/tmp/latest.tgz.sha256");
125
	update_output_window("{$g['product_name']} download complete.");
126
}
127

    
128
/* launch external upgrade helper */
129
$external_upgrade_helper_text = "/etc/rc.firmware pfSenseupgrade ";
130
if($needs_system_upgrade == true)
131
	$external_upgrade_helper_text .= "/tmp/latest.tgz";
132

    
133
$downloaded_latest_tgz_sha256 = str_replace("\n", "", `sha256 /tmp/latest.tgz  | awk '{ print $4 }'`);
134
$upgrade_latest_tgz_sha256 = str_replace("\n", "", `cat /tmp/latest.tgz.sha256 | awk '{ print $4 }'`);
135

    
136
if($downloaded_latest_tgz_sha256 <> $upgrade_latest_tgz_sha256) {
137
	update_status("Downloading complete but sha256 does not match.");
138
	update_output_window("Auto upgrade aborted.  \n\nDownloaded SHA256: $downloaded_latest_tgz_sha256 \n\nNeeded SHA256: $upgrade_latest_tgz_sha256");	
139
} else {
140
	update_status("Downloading complete.");
141
	update_output_window("{$g['product_name']} is now upgrading.\\n\\nThe firewall will reboot once the operation is completed.");
142
	echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';\n</script>";
143
	exec_rc_script_async("{$external_upgrade_helper_text}");
144
}
145

    
146
/*
147
	Helper functions
148
*/
149

    
150
function download_file_with_progress_bar($url_file, $destination_file) {
151
	global $ch, $fout, $file_size, $downloaded, $counter;
152
	$file_size  = 1;
153
	$downloaded = 1;
154
	/* open destination file */
155
	$fout = fopen($destination_file, "wb");
156

    
157
	/*
158
		Originally by Author: Keyvan Minoukadeh
159
		Modified by Scott Ullrich to return Content-Length size
160
	*/
161

    
162
	$ch = curl_init();
163
	curl_setopt($ch, CURLOPT_URL, $url_file);
164
	curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
165
	curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'read_body');
166
	curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
167

    
168
	curl_exec($ch);
169
	fclose($fout);
170
	return 1;
171

    
172
	if ($error = curl_error($ch)) {
173
	    return -1;
174
	}
175
}
176

    
177
function read_header($ch, $string) {
178
	global $file_size, $ch, $fout;
179
	$length = strlen($string);
180
	ereg("(Content-Length:) (.*)", $string, $regs);
181
	if($regs[2] <> "") {
182
		$file_size = intval($regs[2]);
183
	}
184
	return $length;
185
}
186

    
187
function read_body($ch, $string) {
188
	global $fout, $file_size, $downloaded, $counter, $version, $latest_version, $current_installed_pfsense_version;
189
	$length = strlen($string);
190
	$downloaded += intval($length);
191
	$downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
192
	$downloadProgress = 100 - $downloadProgress;
193
	$a = $file_size;
194
	$b = $downloaded;
195
	$c = $downloadProgress;
196
	$text  = "  Auto Update Download Status\\n";
197
	$text .= "---------------------------------\\n";
198
	$text .= "  Latest Version  : {$latest_version}\\n";
199
	$text .= "  Current Version : {$current_installed_pfsense_version}\\n";
200
	$text .= "  File size       : {$a}\\n";
201
	$text .= "  Downloaded      : {$b}\\n";
202
	$text .= "  Percent         : {$c}%\\n";
203
	$text .= "---------------------------------\\n";
204
	$counter++;
205
	if($counter > 150) {
206
		update_output_window($text);
207
		update_progress_bar($downloadProgress);
208
		$counter = 0;
209
	}
210
	fwrite($fout, $string);
211
	echo "<script language='javascript'>'>document.progressbar.style.width=\"$c%\";</script>\n";
212
	return $length;
213
}
214

    
215
?>
(149-149/193)