]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/backup.cgi
Some Fixes for the CGIs not spamming apache logfile.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / backup.cgi
CommitLineData
cf29614f 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2007 Michael Tremer & Christian Schmidt #
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###############################################################################
cf29614f
CS
21
22use strict;
23# enable only the following on debugging purpose
36dfbfcf
CS
24#use warnings;
25#use CGI::Carp 'fatalsToBrowser';
8e8bbd9d 26use File::Copy;
cf29614f
CS
27
28require '/var/ipfire/general-functions.pl';
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
31
32my %color = ();
33my %mainsettings = ();
8e8bbd9d 34my %cgiparams=();
cf29614f
CS
35my %checked = ();
36my $message = "";
37my $errormessage = "";
cf29614f 38
8e8bbd9d 39$a = new CGI;
cf29614f
CS
40
41&General::readhash("${General::swroot}/main/settings", \%mainsettings);
42&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
43
8e8bbd9d
CS
44$cgiparams{'ACTION'} = '';
45$cgiparams{'FILE'} = '';
46$cgiparams{'UPLOAD'} = '';
47$cgiparams{'BACKUPLOGS'} = '';
69addbb8 48
8e8bbd9d 49&Header::getcgihash(\%cgiparams);
cf29614f 50
a609bfb0
CS
51############################################################################################################################
52################################################ Workaround for Directories ################################################
53
2a1f6aa7 54system("/usr/local/bin/backupctrl makedirs >/dev/null 2>&1 ") unless ( -e '/var/ipfire/backup/addons/backup') ;
a609bfb0 55
cf29614f 56############################################################################################################################
69addbb8 57############################################## System calls ohne Http Header ###############################################
cf29614f 58
b90a7e56
DG
59# Replace slashes from filename
60$cgiparams{'FILE'} =~ s/\///;
69addbb8
CS
61
62if ( $cgiparams{'ACTION'} eq "download" )
8e8bbd9d 63{
901a50cf
CS
64 open(DLFILE, "</var/ipfire/backup/$cgiparams{'FILE'}") or die "Unable to open $cgiparams{'FILE'}: $!";
65 my @fileholder = <DLFILE>;
66 print "Content-Type:application/x-download\n";
67 print "Content-Disposition:attachment;filename=$cgiparams{'FILE'}\n\n";
68 print @fileholder;
69 exit (0);
8e8bbd9d 70}
b90a7e56
DG
71if ( $cgiparams{'ACTION'} eq "downloadiso" )
72{
73 open(DLFILE, "</var/tmp/backupiso/$cgiparams{'FILE'}") or die "Unable to open $cgiparams{'FILE'}: $!";
74 my @fileholder = <DLFILE>;
75 print "Content-Type:application/x-download\n";
76 print "Content-Disposition:attachment;filename=$cgiparams{'FILE'}\n\n";
77 print @fileholder;
78 exit (0);
79}
5ad5a6bc
MT
80if ( $cgiparams{'ACTION'} eq "downloadaddon" )
81{
82 open(DLFILE, "</var/ipfire/backup/addons/backup/$cgiparams{'FILE'}") or die "Unable to open $cgiparams{'FILE'}: $!";
83 my @fileholder = <DLFILE>;
84 print "Content-Type:application/x-download\n";
85 print "Content-Disposition:attachment;filename=$cgiparams{'FILE'}\n\n";
86 print @fileholder;
87 exit (0);
88}
8e8bbd9d
CS
89elsif ( $cgiparams{'ACTION'} eq "restore" )
90{
901a50cf
CS
91 my $upload = $a->param("UPLOAD");
92 open UPLOADFILE, ">/tmp/restore.ipf";
93 binmode $upload;
94 while ( <$upload> ) {
95 print UPLOADFILE;
96 }
97 close UPLOADFILE;
98 system("/usr/local/bin/backupctrl restore >/dev/null 2>&1");
8e8bbd9d 99}
5ad5a6bc
MT
100elsif ( $cgiparams{'ACTION'} eq "restoreaddon" )
101{
d2b476e8
CS
102 chomp($cgiparams{'UPLOAD'});
103 # we need to fix cause IE7 gives the full path and FF only the filename
104 my @temp = split(/\\/,$cgiparams{'UPLOAD'});
5ad5a6bc 105 my $upload = $a->param("UPLOAD");
d2b476e8 106 open UPLOADFILE, ">/tmp/".$temp[$#temp];
5ad5a6bc
MT
107 binmode $upload;
108 while ( <$upload> ) {
109 print UPLOADFILE;
110 }
111 close UPLOADFILE;
d2b476e8 112 system("/usr/local/bin/backupctrl restoreaddon ".$temp[$#temp]." >/dev/null 2>&1");
5ad5a6bc 113}
cf29614f 114
8e8bbd9d 115&Header::showhttpheaders();
69addbb8
CS
116
117sub 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();}
118
8e8bbd9d
CS
119&Header::openpage($Lang::tr{'backup'}, 1, "");
120&Header::openbigbox('100%', 'left', '', $errormessage);
121
69addbb8
CS
122############################################################################################################################
123################################################### Default System calls ###################################################
124
125if ( $cgiparams{'ACTION'} eq "backup" )
126{
f31d3c1f
DG
127 if ( $cgiparams{'BACKUPLOGS'} eq "include" ) {
128 system("/usr/local/bin/backupctrl include >/dev/null 2>&1");
129 } elsif ( $cgiparams{'BACKUPLOGS'} eq "exclude" ) {
130 system("/usr/local/bin/backupctrl exclude >/dev/null 2>&1");
131 } elsif ( $cgiparams{'BACKUPLOGS'} eq "iso" ) {
132 system("/usr/local/bin/backupctrl iso >/dev/null 2>&1");
133 }
901a50cf
CS
134}
135if ( $cgiparams{'ACTION'} eq "addonbackup" )
136{
5ad5a6bc 137 system("/usr/local/bin/backupctrl addonbackup $cgiparams{'ADDON'} >/dev/null 2>&1");
69addbb8
CS
138}
139elsif ( $cgiparams{'ACTION'} eq "delete" )
140{
901a50cf 141 system("/usr/local/bin/backupctrl $cgiparams{'FILE'} >/dev/null 2>&1");
69addbb8
CS
142}
143
144############################################################################################################################
901a50cf 145############################################ Backups des Systems erstellen #################################################
69addbb8 146
8e8bbd9d
CS
147if ( $message ne "" ){
148 &Header::openbox('100%','left',$Lang::tr{'error messages'});
149 print "<font color='red'>$message</font>\n";
150 &Header::closebox();
151}
cf29614f 152
901a50cf 153my @backups = `cd /var/ipfire/backup/ && ls *.ipf 2>/dev/null`;
b90a7e56 154my @backupisos = `cd /var/tmp/backupiso/ && ls *.iso 2>/dev/null`;
901a50cf 155
cf29614f
CS
156&Header::openbox('100%', 'center', $Lang::tr{'backup'});
157
158print <<END
159<form method='post' action='$ENV{'SCRIPT_NAME'}'>
160<table width='95%' cellspacing='0'>
f31d3c1f
DG
161<tr>
162 <td align='left' width='40%'>$Lang::tr{'logs'}</td>
163 <td align='left'>
164 <input type='radio' name='BACKUPLOGS' value='include'/> $Lang::tr{'include logfiles'}<br/>
165 <input type='radio' name='BACKUPLOGS' value='exclude' checked='checked'/> $Lang::tr{'exclude logfiles'}<br/>
166 <input type='radio' name='BACKUPLOGS' value='iso' /> $Lang::tr{'generate iso'}
167 </td>
168</tr>
901a50cf
CS
169<tr><td align='center' colspan='2'>
170 <input type='hidden' name='ACTION' value='backup' />
171 <input type='image' alt='$Lang::tr{'backup'}' title='$Lang::tr{'backup'}' src='/images/document-save.png' />
172</td></tr>
cf29614f
CS
173</table>
174</form>
175END
176;
177&Header::closebox();
178
901a50cf
CS
179############################################################################################################################
180############################################ Backups des Systems downloaden ################################################
181
cf29614f
CS
182&Header::openbox('100%', 'center', $Lang::tr{'backups'});
183
184print <<END
cf29614f
CS
185<table width='95%' cellspacing='0'>
186END
187;
188foreach (@backups){
8e8bbd9d 189chomp($_);
69addbb8 190my $Datei = "/var/ipfire/backup/".$_;
8e8bbd9d 191my @Info = stat($Datei);
5504342a
JPT
192my $Size = $Info[7] / 1024 / 1024;
193$Size = sprintf("%0.2f", $Size);
194print "<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>";
69addbb8 195print "<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>";
cf29614f 196}
b90a7e56
DG
197foreach (@backupisos){
198chomp($_);
199my $Datei = "/var/tmp/backupiso/".$_;
200my @Info = stat($Datei);
5504342a
JPT
201my $Size = $Info[7] / 1024 / 1024;
202$Size = sprintf("%0.2f", $Size);
203print "<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>";
b90a7e56
DG
204print "<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>";
205}
901a50cf 206print <<END
cf29614f 207</table>
8e8bbd9d
CS
208END
209;
210&Header::closebox();
211
901a50cf
CS
212############################################################################################################################
213############################################# Backups von Addons erstellen #################################################
214
215&Header::openbox('100%', 'center', 'addons');
216
217my @addonincluds = `ls /var/ipfire/backup/addons/includes/ 2>/dev/null`;
98ab7f50
CS
218my @addons = `ls /var/ipfire/backup/addons/backup/ 2>/dev/null`;
219my %addons;
220
221foreach (@addons){
222 my $addon=substr($_,0,length($_)-5);
223 $addons{$addon}='';
224}
901a50cf
CS
225
226print "<table width='95%' cellspacing='0'>";
227foreach (@addonincluds){
228chomp($_);
98ab7f50 229delete $addons{$_};
901a50cf
CS
230my $Datei = "/var/ipfire/backup/addons/backup/".$_.".ipf";
231my @Info = stat($Datei);
232my $Size = $Info[7] / 1024;
c35ac552 233
901a50cf 234if ( -e $Datei ){
c35ac552
JPT
235 if ($Size < 1) {
236 $Size = sprintf("%.2f", $Size);
237 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size KB $Lang::tr{'date'} ".localtime($Info[9])."</td>";
238 } else {
239 $Size = sprintf("%2d", $Size);
240 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size KB $Lang::tr{'date'} ".localtime($Info[9])."</td>";
241
242 }
243
901a50cf
CS
244print <<END
245 <td align='right' width='5'>
246 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
5ad5a6bc
MT
247 <input type='hidden' name='ACTION' value='downloadaddon' />
248 <input type='hidden' name='FILE' value='$_.ipf' />
901a50cf
CS
249 <input type='image' alt='$Lang::tr{'download'}' title='$Lang::tr{'download'}' src='/images/package-x-generic.png' />
250 </form>
251 </td>
252 <td align='right' width='5'>
253 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
254 <input type='hidden' name='ACTION' value='delete' />
363a19ee 255 <input type='hidden' name='FILE' value='addons//backup/$_.ipf' />
901a50cf
CS
256 <input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' />
257 </form>
258 </td>
259END
260;
261}
262else{
b8e5a9c5 263 print "<tr><td align='center'>$Lang::tr{'backup from'} $_ </td><td width='5' align='right'></td><td width='5' align='right'></td>";
901a50cf
CS
264}
265print <<END
266 <td align='right' width='5'>
267 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
268 <input type='hidden' name='ACTION' value='addonbackup' />
269 <input type='hidden' name='ADDON' value='$_' />
270 <input type='image' alt='$Lang::tr{'backup'}' title='$Lang::tr{'backup'}' src='/images/document-save.png' />
271 </form>
272 </td></tr>
273END
274;
275}
b8e5a9c5
CS
276foreach (keys(%addons)){
277chomp($_);
98ab7f50 278my $Datei = "/var/ipfire/backup/addons/backup/".$_.".ipf";
b8e5a9c5
CS
279my @Info = stat($Datei);
280my $Size = $Info[7] / 1024;
281$Size = sprintf("%2d", $Size);
98ab7f50 282print "<tr><td align='center'>$Lang::tr{'backup from'} $_ $Lang::tr{'size'} $Size KB $Lang::tr{'date'} ".localtime($Info[9])."</td>";
b8e5a9c5
CS
283print <<END
284 <td align='right' width='5'>
285 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
286 <input type='hidden' name='ACTION' value='downloadaddon' />
98ab7f50 287 <input type='hidden' name='FILE' value='$_.ipf' />
b8e5a9c5
CS
288 <input type='image' alt='$Lang::tr{'download'}' title='$Lang::tr{'download'}' src='/images/package-x-generic.png' />
289 </form>
290 </td>
291 <td align='right' width='5'>
292 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
293 <input type='hidden' name='ACTION' value='delete' />
41708968 294 <input type='hidden' name='FILE' value='addons//backup/$_.ipf' />
b8e5a9c5
CS
295 <input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' />
296 </form>
297 </td>
298 <td align='right' width='5'></td></tr>
299END
300;
301}
302
901a50cf
CS
303print "</table>";
304&Header::closebox();
5ad5a6bc
MT
305
306############################################################################################################################
307####################################### Backups des Systems wiederherstellen ###############################################
308
309&Header::openbox('100%', 'center', $Lang::tr{'restore'});
310
311print <<END
312<table width='95%' cellspacing='0'>
313<tr><td align='center' colspan='2'><font color='red'><br />$Lang::tr{'backupwarning'}</font><br /><br /></td></tr>
d2b476e8
CS
314<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>
315<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>
5ad5a6bc
MT
316</table>
317END
318;
319&Header::closebox();
cf29614f
CS
320&Header::closebigbox();
321&Header::closepage();