#!/usr/bin/perl # # SmoothWall CGIs # # This code is distributed under the terms of the GPL # # (c) The SmoothWall Team # # $Id: updates.cgi,v 1.9.2.22 2005/12/01 20:41:53 franck78 Exp $ # use LWP::UserAgent; use File::Copy; use strict; # enable only the following on debugging purpose #use warnings; #use CGI::Carp 'fatalsToBrowser'; require 'CONFIG_ROOT/general-functions.pl'; require "${General::swroot}/lang.pl"; require "${General::swroot}/header.pl"; #workaround to suppress a warning when a variable is used only once my @dummy = ( $General::version ); undef (@dummy); my $warnmessage=''; my $errormessage=''; my @av=(''); my @pf=(''); &Header::showhttpheaders(); my %uploadsettings=(); $uploadsettings{'ACTION'} = ''; &Header::getcgihash(\%uploadsettings, {'wantfile' => 1, 'filevar' => 'FH'}); if ($uploadsettings{'ACTION'} eq $Lang::tr{'upload'}) { # This code do not serve a lot because $General::version cannot change while the module is loaded. So no change # can appear. More, this call should be called 'after' update is done ! # my $return = &downloadlist(); # if ($return && $return->is_success) { # if (open(LIST, ">${General::swroot}/patches/available")){ # flock LIST, 2; # my @this = split(/----START LIST----\n/,$return->content); # print LIST $this[1]; # close(LIST); # } else { # $errormessage = $Lang::tr{'could not open available updates file'}; # } # } else { # if (open(LIST, "<${General::swroot}/patches/available")) { # my @list = ; # close(LIST); # } # $warnmessage = $Lang::tr{'could not download the available updates list'}; # } if (copy ($uploadsettings{'FH'}, "/var/patches/patch-$$.tar.gz.gpg") != 1) { $errormessage = $!; } else { my $exitcode = system("/usr/local/bin/installpackage $$ > /dev/null") >> 8; if ($exitcode == 0) { #Hack to get correct version displayed after update open (XX,"perl -e \"require'${General::swroot}/general-functions.pl';print \\\$General::version\"|"); $General::version=; close (XX); &General::log("$Lang::tr{'the following update was successfully installed'} ($General::version)"); } elsif($exitcode == 2) { $errormessage = "$Lang::tr{'could not create directory'}"; } elsif($exitcode == 3) { $errormessage = "$Lang::tr{'this is not an authorised update'}"; } elsif($exitcode == 4) { $errormessage = "$Lang::tr{'this is not a valid archive'}"; } elsif($exitcode == 5) { $errormessage = "$Lang::tr{'could not open update information file'}"; } elsif($exitcode == 6) { $errormessage = "$Lang::tr{'could not open installed updates file'}"; } elsif($exitcode == 7) { $errormessage = "$Lang::tr{'this update is already installed'}"; } elsif($exitcode == 11) { $errormessage = "$Lang::tr{'not enough disk space'}"; } else { $errormessage = "$Lang::tr{'package failed to install'}"; } } } elsif ($uploadsettings{'ACTION'} eq $Lang::tr{'refresh update list'}) { my $return = &downloadlist(); if ($return && $return->is_success) { if (open(LIST, ">${General::swroot}/patches/available")) { flock LIST, 2; my @this = split(/----START LIST----\n/,$return->content); print LIST $this[1]; close(LIST); &General::log($Lang::tr{'successfully refreshed updates list'}); } else { $errormessage = $Lang::tr{'could not open available updates file'}; } } else { $errormessage = $Lang::tr{'could not download the available updates list'}; } } elsif ($uploadsettings{'ACTION'} eq "$Lang::tr{'clear cache'} (squid)") { system('/usr/local/bin/restartsquid','-f'); } if (!open(AV, "<${General::swroot}/patches/available")) { $errormessage = $Lang::tr{'could not open available updates file'}; } else { @av = ; close(AV); } if (!open (PF, "<${General::swroot}/patches/installed")) { $errormessage = $Lang::tr{'could not open installed updates file'}; } else { @pf = ; close (PF); #substract installed patch from list displayed (AV list may not be updated) foreach my $P (@pf) { $P =~ /^(...)/; my $order=$1; my $idx=0; foreach my $A (@av) { $A =~ /^(...)/; if ($1 eq $order) { # match splice (@av,$idx,1); last; } $idx++; } } } &Header::openpage($Lang::tr{'updates'}, 1, ''); &Header::openbigbox('100%', 'left', 'download.png', $errormessage); if ($errormessage) { &Header::openbox('100%', 'left', $Lang::tr{'error messages'}); print $errormessage; print " "; &Header::closebox(); } if ($warnmessage) { &Header::openbox('100%', 'LEFT', "$Lang::tr{'warning messages'}:"); print "$warnmessage \n"; print " \n"; &Header::closebox(); } &Header::openbox('100%', 'left', $Lang::tr{'available updates'}); if ( defined $av[0] ) { print $Lang::tr{'there are updates available'}; print qq| |; foreach (@av) { my @temp = split(/\|/,$_); print ""; } print "
$Lang::tr{'id'} $Lang::tr{'title'} $Lang::tr{'description'} $Lang::tr{'released'}  
$temp[0]$temp[1]$temp[2]$temp[3]$Lang::tr{'info'}
"; } else { print $Lang::tr{'all updates installed'}; } print qq|

$Lang::tr{'to install an update'}
$Lang::tr{'upload update file'}
|; print "$Lang::tr{'disk usage'}"; open (XX,'df -h / /var/log|'); my @df=; close (XX); print ""; map ( $_ =~ s/ +/"; print ""; print ""; print "
/g,@df); # tablify each line! print "
$df[0]
$df[1]
$df[2]
"; print "\n
"; print "\n\n"; print "\n\t"; print "\n\t"; print "\n
 
\n"; print ""; &Header::closebox(); &Header::openbox('100%', 'LEFT', $Lang::tr{'installed updates'}); print qq| |; foreach my $pf (@pf) { next if $pf =~ m/^#/; my @temp = split(/\|/,$pf); #??? @av = grep(!/^$temp[0]/, @av); print ""; } close(PF); print "
$Lang::tr{'id'} $Lang::tr{'title'} $Lang::tr{'description'} $Lang::tr{'released'} $Lang::tr{'installed'}
" . join("",@temp) . "
"; &Header::closebox(); &Header::closebigbox(); &Header::closepage(); sub downloadlist { unless (-e "${General::swroot}/red/active") { return 0; } my $downloader = LWP::UserAgent->new; $downloader->timeout(5); my %proxysettings=(); &General::readhash("${General::swroot}/proxy/settings", \%proxysettings); if ($_=$proxysettings{'UPSTREAM_PROXY'}) { my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/); if ($proxysettings{'UPSTREAM_USER'}) { $downloader->proxy("http","http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$peer:$peerport/"); } else { $downloader->proxy("http","http://$peer:$peerport/"); } } return $downloader->get("http://www.ipcop.org/patches/${General::version}", 'Cache-Control', 'no-cache'); }