Ensure there is no false array entry inside the backup listing.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / backup.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2005-2010 IPFire Team #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23 # enable only the following on debugging purpose
24 #use warnings;
25 #use CGI::Carp 'fatalsToBrowser';
26 use File::Copy;
27
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31
32 my %color = ();
33 my %mainsettings = ();
34 my %cgiparams=();
35 my %checked = ();
36 my $message = "";
37 my $errormessage = "";
38 my @backups = "";
39 my @backupisos = "";
40
41 $a = new CGI;
42
43 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
44 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
45
46 $cgiparams{'ACTION'} = '';
47 $cgiparams{'FILE'} = '';
48 $cgiparams{'UPLOAD'} = '';
49 $cgiparams{'BACKUPLOGS'} = '';
50
51 &Header::getcgihash(\%cgiparams);
52
53 ############################################################################################################################
54 ################################################ Workaround for Directories ################################################
55
56 system("/usr/local/bin/backupctrl makedirs >/dev/null 2>&1 ") unless ( -e '/var/ipfire/backup/addons/backup') ;
57
58 ############################################################################################################################
59 ############################################## System calls ohne Http Header ###############################################
60
61 # Replace slashes from filename
62 $cgiparams{'FILE'} =~ s/\///;
63
64 if ( $cgiparams{'ACTION'} eq "download" )
65 {
66 open(DLFILE, "</var/ipfire/backup/$cgiparams{'FILE'}") or die "Unable to open $cgiparams{'FILE'}: $!";
67 my @fileholder = <DLFILE>;
68 print "Content-Type:application/x-download\n";
69 print "Content-Disposition:attachment;filename=$cgiparams{'FILE'}\n\n";
70 print @fileholder;
71 exit (0);
72 }
73 if ( $cgiparams{'ACTION'} eq "downloadiso" )
74 {
75 open(DLFILE, "</var/tmp/backupiso/$cgiparams{'FILE'}") or die "Unable to open $cgiparams{'FILE'}: $!";
76 my @fileholder = <DLFILE>;
77 print "Content-Type:application/x-download\n";
78 print "Content-Disposition:attachment;filename=$cgiparams{'FILE'}\n\n";
79 print @fileholder;
80 exit (0);
81 }
82 if ( $cgiparams{'ACTION'} eq "downloadaddon" )
83 {
84 open(DLFILE, "</var/ipfire/backup/addons/backup/$cgiparams{'FILE'}") or die "Unable to open $cgiparams{'FILE'}: $!";
85 my @fileholder = <DLFILE>;
86 print "Content-Type:application/x-download\n";
87 print "Content-Disposition:attachment;filename=$cgiparams{'FILE'}\n\n";
88 print @fileholder;
89 exit (0);
90 }
91 elsif ( $cgiparams{'ACTION'} eq "restore" )
92 {
93 my $upload = $a->param("UPLOAD");
94 open UPLOADFILE, ">/tmp/restore.ipf";
95 binmode $upload;
96 while ( <$upload> ) {
97 print UPLOADFILE;
98 }
99 close UPLOADFILE;
100 system("/usr/local/bin/backupctrl restore >/dev/null 2>&1");
101 }
102 elsif ( $cgiparams{'ACTION'} eq "restoreaddon" )
103 {
104 chomp($cgiparams{'UPLOAD'});
105 # we need to fix cause IE7 gives the full path and FF only the filename
106 my @temp = split(/\\/,$cgiparams{'UPLOAD'});
107 my $upload = $a->param("UPLOAD");
108 open UPLOADFILE, ">/tmp/".$temp[$#temp];
109 binmode $upload;
110 while ( <$upload> ) {
111 print UPLOADFILE;
112 }
113 close UPLOADFILE;
114 system("/usr/local/bin/backupctrl restoreaddon ".$temp[$#temp]." >/dev/null 2>&1");
115 }
116
117 &Header::showhttpheaders();
118
119 sub refreshpage{&Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='1;'>" );print "<center><img src='/images/clock.gif' alt='' /><br/><font color='red'>$Lang::tr{'pagerefresh'}</font></center>";&Header::closebox();}
120
121 &Header::openpage($Lang::tr{'backup'}, 1, "");
122 &Header::openbigbox('100%', 'left', '', $errormessage);
123
124 ############################################################################################################################
125 ################################################### Default System calls ###################################################
126
127 if ( $cgiparams{'ACTION'} eq "backup" )
128 {
129 if ( $cgiparams{'BACKUPLOGS'} eq "include" ) {
130 system("/usr/local/bin/backupctrl include >/dev/null 2>&1");
131 } elsif ( $cgiparams{'BACKUPLOGS'} eq "exclude" ) {
132 system("/usr/local/bin/backupctrl exclude >/dev/null 2>&1");
133 } elsif ( $cgiparams{'BACKUPLOGS'} eq "iso" ) {
134 system("/usr/local/bin/backupctrl iso >/dev/null 2>&1");
135 }
136 }
137 if ( $cgiparams{'ACTION'} eq "addonbackup" )
138 {
139 system("/usr/local/bin/backupctrl addonbackup $cgiparams{'ADDON'} >/dev/null 2>&1");
140 }
141 elsif ( $cgiparams{'ACTION'} eq "delete" )
142 {
143 system("/usr/local/bin/backupctrl $cgiparams{'FILE'} >/dev/null 2>&1");
144 }
145
146 ############################################################################################################################
147 ############################################ Backups des Systems erstellen #################################################
148
149 if ( $message ne "" ){
150 &Header::openbox('100%','left',$Lang::tr{'error messages'});
151 print "<font color='red'>$message</font>\n";
152 &Header::closebox();
153 }
154
155 if ( -e "/var/ipfire/backup/" ){
156 @backups = `cd /var/ipfire/backup/ && ls *.ipf 2>/dev/null`;
157 }
158
159 if ( -e "/var/tmp/backupiso/" ){
160 @backupisos = `cd /var/tmp/backupiso/ && ls *.iso 2>/dev/null`;
161 }
162
163 &Header::openbox('100%', 'center', $Lang::tr{'backup'});
164
165 print <<END
166 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
167 <table width='95%' cellspacing='0'>
168 <tr>
169 <td align='left' width='40%'>$Lang::tr{'logs'}</td>
170 <td align='left'>
171 <input type='radio' name='BACKUPLOGS' value='include'/> $Lang::tr{'include logfiles'}<br/>
172 <input type='radio' name='BACKUPLOGS' value='exclude' checked='checked'/> $Lang::tr{'exclude logfiles'}<br/>
173 <input type='radio' name='BACKUPLOGS' value='iso' /> $Lang::tr{'generate iso'}
174 </td>
175 </tr>
176 <tr><td align='center' colspan='2'>
177 <input type='hidden' name='ACTION' value='backup' />
178 <input type='image' alt='$Lang::tr{'backup'}' title='$Lang::tr{'backup'}' src='/images/document-save.png' />
179 </td></tr>
180 </table>
181 </form>
182 END
183 ;
184 &Header::closebox();
185
186 ############################################################################################################################
187 ############################################ Backups des Systems downloaden ################################################
188
189 &Header::openbox('100%', 'center', $Lang::tr{'backups'});
190
191 print <<END
192 <table width='95%' cellspacing='0'>
193 END
194 ;
195 foreach (@backups){
196 if ( $_ !~ /ipf$/){next;}
197 chomp($_);
198 my $Datei = "/var/ipfire/backup/".$_;
199 my @Info = stat($Datei);
200 my $Size = $Info[7] / 1024 / 1024;
201 $Size = sprintf("%0.2f", $Size);
202 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size MB</td><td width='5'><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='download' /><input type='hidden' name='FILE' value='$_' /><input type='image' alt='$Lang::tr{'download'}' title='$Lang::tr{'download'}' src='/images/package-x-generic.png' /></form></td>";
203 print "<td width='5'><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='delete' /><input type='hidden' name='FILE' value='$_' /><input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' /></form></td></tr>";
204 }
205 foreach (@backupisos){
206 if ( $_ !~ /iso$/){next;}
207 chomp($_);
208 my $Datei = "/var/tmp/backupiso/".$_;
209 my @Info = stat($Datei);
210 my $Size = $Info[7] / 1024 / 1024;
211 $Size = sprintf("%0.2f", $Size);
212 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size MB</td><td width='5'><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='downloadiso' /><input type='hidden' name='FILE' value='$_' /><input type='image' alt='$Lang::tr{'download'}' title='$Lang::tr{'download'}' src='/images/package-x-generic.png' /></form></td>";
213 print "<td width='5'><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='delete' /><input type='hidden' name='FILE' value='$_' /><input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' /></form></td></tr>";
214 }
215 print <<END
216 </table>
217 END
218 ;
219 &Header::closebox();
220
221 ############################################################################################################################
222 ############################################# Backups von Addons erstellen #################################################
223
224 &Header::openbox('100%', 'center', 'addons');
225
226 my @addonincluds = `ls /var/ipfire/backup/addons/includes/ 2>/dev/null`;
227 my @addons = `ls /var/ipfire/backup/addons/backup/ 2>/dev/null`;
228 my %addons;
229
230 foreach (@addons){
231 my $addon=substr($_,0,length($_)-5);
232 $addons{$addon}='';
233 }
234
235 print "<table width='95%' cellspacing='0'>";
236 foreach (@addonincluds){
237 chomp($_);
238 delete $addons{$_};
239 my $Datei = "/var/ipfire/backup/addons/backup/".$_.".ipf";
240 my @Info = stat($Datei);
241 my $Size = $Info[7] / 1024;
242
243 if ( -e $Datei ){
244 if ($Size < 1) {
245 $Size = sprintf("%.2f", $Size);
246 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size KB $Lang::tr{'date'} ".localtime($Info[9])."</td>";
247 } else {
248 $Size = sprintf("%2d", $Size);
249 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size KB $Lang::tr{'date'} ".localtime($Info[9])."</td>";
250
251 }
252
253 print <<END
254 <td align='right' width='5'>
255 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
256 <input type='hidden' name='ACTION' value='downloadaddon' />
257 <input type='hidden' name='FILE' value='$_.ipf' />
258 <input type='image' alt='$Lang::tr{'download'}' title='$Lang::tr{'download'}' src='/images/package-x-generic.png' />
259 </form>
260 </td>
261 <td align='right' width='5'>
262 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
263 <input type='hidden' name='ACTION' value='delete' />
264 <input type='hidden' name='FILE' value='addons//backup/$_.ipf' />
265 <input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' />
266 </form>
267 </td>
268 END
269 ;
270 }
271 else{
272 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ </td><td width='5' align='right'></td><td width='5' align='right'></td>";
273 }
274 print <<END
275 <td align='right' width='5'>
276 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
277 <input type='hidden' name='ACTION' value='addonbackup' />
278 <input type='hidden' name='ADDON' value='$_' />
279 <input type='image' alt='$Lang::tr{'backup'}' title='$Lang::tr{'backup'}' src='/images/document-save.png' />
280 </form>
281 </td></tr>
282 END
283 ;
284 }
285 foreach (keys(%addons)){
286 chomp($_);
287 my $Datei = "/var/ipfire/backup/addons/backup/".$_.".ipf";
288 my @Info = stat($Datei);
289 my $Size = $Info[7] / 1024;
290 $Size = sprintf("%2d", $Size);
291 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size KB $Lang::tr{'date'} ".localtime($Info[9])."</td>";
292 print <<END
293 <td align='right' width='5'>
294 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
295 <input type='hidden' name='ACTION' value='downloadaddon' />
296 <input type='hidden' name='FILE' value='$_.ipf' />
297 <input type='image' alt='$Lang::tr{'download'}' title='$Lang::tr{'download'}' src='/images/package-x-generic.png' />
298 </form>
299 </td>
300 <td align='right' width='5'>
301 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
302 <input type='hidden' name='ACTION' value='delete' />
303 <input type='hidden' name='FILE' value='addons//backup/$_.ipf' />
304 <input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' />
305 </form>
306 </td>
307 <td align='right' width='5'></td></tr>
308 END
309 ;
310 }
311
312 print "</table>";
313 &Header::closebox();
314
315 ############################################################################################################################
316 ####################################### Backups des Systems wiederherstellen ###############################################
317
318 &Header::openbox('100%', 'center', $Lang::tr{'restore'});
319
320 print <<END
321 <table width='95%' cellspacing='0'>
322 <tr><td align='center' colspan='2'><font color='red'><br />$Lang::tr{'backupwarning'}</font><br /><br /></td></tr>
323 <tr><td align='left'>$Lang::tr{'backup'}</td><td align='left'><form method='post' enctype='multipart/form-data' action='$ENV{'SCRIPT_NAME'}'><input type="file" size='50' name="UPLOAD" /><input type='hidden' name='ACTION' value='restore' /><input type='hidden' name='FILE' /><input type='image' alt='$Lang::tr{'restore'}' title='$Lang::tr{'restore'}' src='/images/media-floppy.png' /></form></td></tr>
324 <tr><td align='left'>$Lang::tr{'backupaddon'}</td><td align='left'><form method='post' enctype='multipart/form-data' action='$ENV{'SCRIPT_NAME'}'><input type="file" size='50' name="UPLOAD" /><input type='hidden' name='ACTION' value='restoreaddon' /><input type='hidden' name='FILE' /><input type='image' alt='$Lang::tr{'restore'}' title='$Lang::tr{'restore'}' src='/images/media-floppy.png' /></form></td></tr>
325 </table>
326 END
327 ;
328 &Header::closebox();
329 &Header::closebigbox();
330 &Header::closepage();