Project

General

Profile

Download (6.98 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/* $Id$ */
4
/*
5
	system_firmware_auto.php
6
	part of pfSense (http://www.pfsense.com)
7

    
8
	Copyright (C) 2005 Scott Ullrich
9

    
10
	Based originally on system_firmware.php
11
        (C)2003-2004 Manuel Kasper
12

    
13
	All rights reserved.
14

    
15
	Redistribution and use in source and binary forms, with or without
16
	modification, are permitted provided that the following conditions are met:
17

    
18
	1. Redistributions of source code must retain the above copyright notice,
19
	   this list of conditions and the following disclaimer.
20

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

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

    
37
require("guiconfig.inc");
38

    
39
?>
40
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
41
<html>
42
<head>
43
<title><?=gentitle("System: Firmware: Invoke Auto Upgrade");?></title>
44
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
45
<link href="gui.css" rel="stylesheet" type="text/css">
46
</head>
47

    
48
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
49

    
50
<?php include("fbegin.inc"); ?>
51
<p class="pgtitle">System: Firmware: Invoke Auto Upgrade</p>
52

    
53
<form action="system_firmware_auto.php" method="post">
54
<table width="100%" border="0" cellpadding="6" cellspacing="0">
55
	<tr>
56
	  <td class="tabcont">
57
	      <table width="100%" border="0" cellpadding="6" cellspacing="0">
58
		<tr>
59
		  <td>
60
		      <!-- progress bar -->
61
		      <center>
62
		      <table id="progholder" name="progholder" height='20' border='1' bordercolor='black' width='420' bordercolordark='#000000' bordercolorlight='#000000' style='border-collapse: collapse' colspacing='2' cellpadding='2' cellspacing='2'><tr><td><img border='0' src='progress_bar.gif' width='280' height='23' name='progressbar' id='progressbar'></td></tr></table>
63
		      <br>
64
		      <!-- status box -->
65
		      <textarea border='1' bordercolordark='#000000' bordercolorlight='#000000' style='background-color: #EEEEEE; border-collapse: collapse' cols="60" rows="1" name="status" id="status" wrap="hard">
66
		      Beginning system autoupdate...
67
		      </textarea>
68
		      <!-- command output box -->
69
		      <textarea border='1' bordercolordark='#000000' bordercolorlight='#000000' style='background-color: #EEEEEE; border-collapse: collapse' cols="60" rows="25" name="output" id="output" wrap="hard">
70
		      </textarea>
71
		      </center>
72
		  </td>
73
		</tr>
74
	      </table>
75
	  </td>
76
	</tr>
77
</table>
78
</form>
79
<?php include("fend.inc"); ?>
80
</body>
81
</html>
82

    
83
<?php
84

    
85
/* Define necessary variables. */
86
$platform =		trim(file_get_contents('/etc/platform'));
87
$firmware_version =	trim(file_get_contents('/etc/version'));
88
$kernel_version =	trim(file_get_contents('/etc/version_kernel'));
89
$base_version =		trim(file_get_contents('/etc/version_base'));
90

    
91
update_status("Downloading current version information...");
92
$versions = check_firmware_version();
93

    
94
if($versions != -1) {
95
	if($versions[0] == true) {
96
		if($versions[1] != $firmware_version) $needs_firmware_upgrade = true;
97
		if($versions[2] != $kernel_version) $needs_kernel_upgrade = true;
98
		if($versions[3] != $base_version) $needs_base_version = true;
99
		if(isset($versions[4])) update_output_window($versions[3]); // If we have additional data (a CHANGELOG etc) to display, do so.
100
	} else {
101
		update_status("No updates required.");
102
	}
103
} else {
104
	update_status("Could not retrieve version information.");
105
	exit();
106
}
107

    
108
if($needs_firmware_upgrade == true) {
109
	update_status("Downloading firmware update...");
110
	$status = download_file_with_progress_bar("http://www.pfSense.com/latest.tgz", "/tmp/latest.tgz");
111
	update_output_window("Firmware download complete.");
112
}
113

    
114
if($needs_kernel_upgrade == true) {
115
	update_status("Downloading kernel update...");
116
	$status = download_file_with_progress_bar("http://www.pfSense.com/latest_kernel{$platform}.tgz", "/tmp/latest_kernel.tgz");
117
	update_output_window("Kernel download complete.");
118

    
119
if($needs_base_upgrade == true) {
120
	update_status("Downloading base update...");
121
	$status = download_file_with_progress_bar("http://www.pfSense.com/latest_base.tgz", "/tmp/latest_base.tgz");
122
	update_output_window("Base update complete.");
123
}
124

    
125
/* launch external upgrade helper */
126
$external_upgrade_helper_text = "";
127
if($needs_system_upgrade == true) {
128
	exec_rc_script_async("/etc/rc.firmware pfSense");
129
}
130

    
131
if($needs_kernel_upgrade == true) {
132
	exec_rc_script_async("/etc/rc.firmware pfSense_kernel");
133
}
134

    
135
if($needs_base_upgrade == true) {
136
	exec_rc_script_async("/etc/rc.firmware pfSense_base");
137
}
138

    
139
update_status("pfSense is now upgrading.  The firewall will reboot once the operation is completed.");
140

    
141
echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';\n</script>";
142

    
143
/* end of upgrade script */
144

    
145

    
146

    
147
/*
148
	Helper functions
149
*/
150

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

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

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

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

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

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

    
188
function read_body($ch, $string) {
189
	global $fout, $file_size, $downloaded, $counter;
190
	$length = strlen($string);
191
	$downloaded += intval($length);
192
	$downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
193
	$downloadProgress = 100 - $downloadProgress;
194
	$a = $file_size;
195
	$b = $downloaded;
196
	$c = $downloadProgress;
197
	$text  = "  Download Status\\n";
198
	$text .= "---------------------------------\\n";
199
	$text .= "  File size  : {$a}\\n";
200
	$text .= "  Downloaded : {$b}\\n";
201
	$text .= "  Percent    : {$c}%\\n";
202
	$text .= "---------------------------------\\n";
203
	$counter++;
204
	if($counter > 150) {
205
		update_output_window($text);
206
		update_progress_bar($downloadProgress);
207
		$counter = 0;
208
	}
209
	fwrite($fout, $string);
210
	return $length;
211
}
212

    
213
?>
(95-95/111)