Project

General

Profile

Download (5.94 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/* $Id$ */
4
/*
5
	system_firmware_auto.php
6
	part of m0n0wall (http://m0n0.ch/wall)
7

    
8
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10

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

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

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

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

    
33
require("guiconfig.inc");
34

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

    
44
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
45

    
46
<?php include("fbegin.inc"); ?>
47
<p class="pgtitle">System: Firmware: Invoke Auto Upgrade</p>
48

    
49
<form action="system_firmware_auto.php" method="post">
50
  <table width="100%" border="0" cellpadding="6" cellspacing="0">
51
	<tr>
52
	  <td class="tabcont">
53
	      <table width="100%" border="0" cellpadding="6" cellspacing="0">
54
		     <tr>
55
		       <td>
56
			   <!-- progress bar -->
57
			   <center>
58
			   <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>
59
			   <br>
60
			   <!-- status box -->
61
			   <textarea cols="60" rows="1" name="status" id="status" wrap="hard">One moment please... This will take a while!</textarea>
62
			   <!-- command output box -->
63
			   <textarea cols="60" rows="25" name="output" id="output" wrap="hard"></textarea>
64
			   </center>
65
		       </td>
66
		     </tr>
67
	      </table>
68
	  </td>
69
	</tr>
70
  </table>
71
</form>
72
<?php include("fend.inc"); ?>
73
</body>
74
</html>
75

    
76
<?php
77

    
78
/* auto upgrade logic starts here */
79

    
80
$needs_base_update 	= false;
81
$needs_pfSense_updates 	= false;
82

    
83
update_status("Downloading current version information...");
84
$status = download_file_with_progress_bar("http://www.pfSense.com/pfSense/version", "/tmp/pfSense_version");
85

    
86
update_status("Downloading current base version information...");
87
$status = download_file_with_progress_bar("http://www.pfSense.com/pfSense/version_base", "/tmp/pfSense_base_version");
88

    
89
$current_installed_pfsense_version = return_filename_as_string("/etc/version");
90
$current_installed_pfsense_base_version = return_filename_as_string("/etc/version_base");
91

    
92
// XXX: logic to deterimine if we need a new version.
93
$needs_system_upgrade 	= true;
94
$needs_base_upgrade 	= false;
95

    
96
if($needs_system_upgrade == true) {
97
	update_status("Downloading updates ...");
98
	$status = download_file_with_progress_bar("http://www.pfSense.com/latest.tgz", "/tmp/latest.tgz");
99
	update_output_window("pfSense download complete.");
100
}
101

    
102
if($needs_base_upgrade == true) {
103
	update_status("Downloading base updates ...");
104
	$status = download_file_with_progress_bar("http://www.pfSense.com/latest_base.tgz", "/tmp/latest_base.tgz");
105
	update_output_window("pfSense base download complete.");
106
}
107

    
108
/* launch external upgrade helper */
109
$external_upgrade_helper_text = "/etc/rc.firmware ";
110
if($needs_system_upgrade == true)
111
	$external_upgrade_helper_text .= "/tmp/latest.tgz /tmp/latest.tgz.md5";
112
if($needs_base_upgrade == true)
113
	$external_upgrade_helper_text .= "/tmp/latest_base.tgz /tmp/latest_base.tgz.md5";
114

    
115
update_output_window("pfSense is now upgrading.  The firewall will reboot once the operation is completed.");
116
exec_rc_script_async("{$external_upgrade_helper_text}");
117

    
118

    
119
function download_file_with_progress_bar($url_file, $destination_file) {
120
	global $ch, $fout, $file_size, $downloaded, $counter;
121
	$file_size  = 1;
122
	$downloaded = 1;
123
	/* open destination file */
124
	$fout = fopen($destination_file, "wb");
125

    
126
	/*
127
		Originally by Author: Keyvan Minoukadeh
128
		Modified by Scott Ullrich to return Content-Length size
129
	*/
130

    
131
	$ch = curl_init();
132
	curl_setopt($ch, CURLOPT_URL, $url_file);
133
	curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
134
	curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'read_body');
135
	curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
136

    
137
	curl_exec($ch);
138
	fclose($fout);
139
	return 1;
140

    
141
	if ($error = curl_error($ch)) {
142
	    return -1;
143
	}
144
}
145

    
146
function read_header($ch, $string) {
147
    global $file_size, $ch, $fout;
148
    $length = strlen($string);
149
    ereg("(Content-Length:) (.*)", $string, $regs);
150
    if($regs[2] <> "") {
151
	    $file_size = intval($regs[2]);
152
    }
153
    return $length;
154
}
155

    
156
function read_body($ch, $string) {
157
	global $fout, $file_size, $downloaded, $counter;
158
	$length = strlen($string);
159
	$downloaded += intval($length);
160
	$downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
161
	$downloadProgress = 100 - $downloadProgress;
162
	$text  = "File size : {$file_size}   ";
163
	$text .= "Downloaded: {$downloaded}  ";
164
	$counter++;
165
	if($counter > 150) {
166
		update_output_window($text);
167
		update_progress_bar($downloadProgress);
168
		$counter = 0;
169
	}
170
	fwrite($fout, $string);
171
	return $length;
172
}
173

    
174
?>
(95-95/111)