Project

General

Profile

Download (7.14 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])) $static_text = $versions[4] . '\n'; // If we have additional data (a CHANGELOG etc) to display, do so.
100
		update_output_window($static_text);
101
	} else {
102
		update_status("No updates required.");
103
	}
104
} else {
105
	update_status("Could not retrieve version information.");
106
	exit();
107
}
108

    
109
if($needs_firmware_upgrade == true) {
110
	$static_status = "Downloading firmware update... ";
111
	update_status($static_status);
112
	$status = download_file_with_progress_bar("http://www.pfSense.com/latest.tgz", "/tmp/latest.tgz");
113
	$static_status .= "done. ";
114
}
115

    
116
if($needs_kernel_upgrade == true) {
117
	$static_status .= "Downloading kernel update... ";
118
	update_status($static_status);
119
	$status = download_file_with_progress_bar("http://www.pfSense.com/latest_kernel{$platform}.tgz", "/tmp/latest_kernel.tgz");
120
	$static_status .= "done. ";
121
}
122

    
123
if($needs_base_upgrade == true) {
124
	$static_status .= "Downloading base update... ";
125
	update_status($static_status);
126
	$status = download_file_with_progress_bar("http://www.pfSense.com/latest_base.tgz", "/tmp/latest_base.tgz");
127
	$static_status .= "done. ";
128
	update_status($static_status);
129
}
130

    
131
/* launch external upgrade helper */
132
$external_upgrade_helper_text = "";
133
if($needs_system_upgrade == true) {
134
	exec_rc_script_async("/etc/rc.firmware pfSense");
135
}
136

    
137
if($needs_kernel_upgrade == true) {
138
	exec_rc_script_async("/etc/rc.firmware pfSense_kernel");
139
}
140

    
141
if($needs_base_upgrade == true) {
142
	exec_rc_script_async("/etc/rc.firmware pfSense_base");
143
}
144

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

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

    
149
/* end of upgrade script */
150

    
151

    
152

    
153
/*
154
	Helper functions
155
*/
156

    
157
function download_file_with_progress_bar($url_file, $destination_file) {
158
	global $ch, $fout, $file_size, $downloaded, $counter;
159
	$file_size  = 1;
160
	$downloaded = 1;
161
	/* open destination file */
162
	$fout = fopen($destination_file, "wb");
163

    
164
	/*
165
		Originally by Author: Keyvan Minoukadeh
166
		Modified by Scott Ullrich to return Content-Length size
167
	*/
168

    
169
	$ch = curl_init();
170
	curl_setopt($ch, CURLOPT_URL, $url_file);
171
	curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
172
	curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'read_body');
173
	curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
174

    
175
	curl_exec($ch);
176
	fclose($fout);
177
	return 1;
178

    
179
	if ($error = curl_error($ch)) {
180
	    return -1;
181
	}
182
}
183

    
184
function read_header($ch, $string) {
185
	global $file_size, $ch, $fout;
186
	$length = strlen($string);
187
	ereg("(Content-Length:) (.*)", $string, $regs);
188
	if($regs[2] <> "") {
189
		$file_size = intval($regs[2]);
190
	}
191
	return $length;
192
}
193

    
194
function read_body($ch, $string) {
195
	global $fout, $file_size, $downloaded, $counter;
196
	$length = strlen($string);
197
	$downloaded += intval($length);
198
	$downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
199
	$downloadProgress = 100 - $downloadProgress;
200
	/*
201
	$a = $file_size;
202
	$b = $downloaded;
203
	$c = $downloadProgress;
204
	$text = "  Download Status\\n";
205
	$text .= "---------------------------------\\n";
206
	$text .= "  File size  : {$a}\\n";
207
	$text .= "  Downloaded : {$b}\\n";
208
	$text .= "  Percent    : {$c}%\\n";
209
	$text .= "---------------------------------\\n";
210
	*/
211
	$counter++;
212
	if($counter > 150) {
213
		$tostatus = $static_status . $downloadProgress;
214
		update_status($tostatus);
215
		update_progress_bar($downloadProgress);
216
		$counter = 0;
217
	}
218
	fwrite($fout, $string);
219
	return $length;
220
}
221

    
222
?>
(95-95/111)