pakfire.cgi: Separate command processing and HTML generation

Move most of the command execution away from the HTML output.
This makes it easier to modify or extend individual commands.

Also load Pakfire settings earlier to ensure that they are
available during command execution.

Signed-off-by: Leo-Andres Hofmann <hofmann@leo-andres.de>
Acked-by: Peter Müller <peter.muelle@ipfire.org>
This commit is contained in:
Leo-Andres Hofmann
2022-05-08 14:09:46 +02:00
committed by Peter Müller
parent c7105c6e66
commit cd521e78b8

View File

@@ -39,11 +39,12 @@ my %mainsettings = ();
# Load general settings
&General::readhash("${General::swroot}/main/settings", \%mainsettings);
&General::readhash("${General::swroot}/pakfire/settings", \%pakfiresettings);
&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
# Get CGI request data
$cgiparams{'ACTION'} = '';
$cgiparams{'VALID'} = '';
$cgiparams{'FORCE'} = '';
$cgiparams{'INSPAKS'} = '';
$cgiparams{'DELPAKS'} = '';
@@ -94,6 +95,35 @@ if($cgiparams{'ACTION'} eq 'json-getstatus') {
exit;
}
### Process Pakfire install/update commands ###
if(($cgiparams{'ACTION'} ne '') && (! &_is_pakfire_busy())) {
if(($cgiparams{'ACTION'} eq 'install') && ($cgiparams{'FORCE'} eq 'on')) {
my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
&General::system_background("/usr/local/bin/pakfire", "install", "--non-interactive", "--no-colors", @pkgs);
} elsif(($cgiparams{'ACTION'} eq 'remove') && ($cgiparams{'FORCE'} eq 'on')) {
my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
&General::system_background("/usr/local/bin/pakfire", "remove", "--non-interactive", "--no-colors", @pkgs);
} elsif($cgiparams{'ACTION'} eq 'update') {
&General::system_background("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
} elsif($cgiparams{'ACTION'} eq 'upgrade') {
&General::system_background("/usr/local/bin/pakfire", "upgrade", "-y", "--no-colors");
} elsif($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
$pakfiresettings{"TREE"} = $cgiparams{"TREE"};
# Check for valid input
if ($pakfiresettings{"TREE"} !~ m/^(stable|testing|unstable)$/) {
$errormessage .= $Lang::tr{'pakfire invalid tree'};
}
unless ($errormessage) {
&General::writehash("${General::swroot}/pakfire/settings", \%pakfiresettings);
# Update lists
&General::system_background("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
}
}
}
### Start pakfire page ###
&Header::showhttpheaders();
@@ -185,9 +215,6 @@ END
# Process Pakfire commands
if (($cgiparams{'ACTION'} eq 'install') && (! &_is_pakfire_busy())) {
my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
if ("$cgiparams{'FORCE'}" eq "on") {
&General::system_background("/usr/local/bin/pakfire", "install", "--non-interactive", "--no-colors", @pkgs);
} else {
&Header::openbox("100%", "center", $Lang::tr{'request'});
my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
print <<END;
@@ -222,12 +249,9 @@ END
&Header::closebigbox();
&Header::closepage();
exit;
}
} elsif (($cgiparams{'ACTION'} eq 'remove') && (! &_is_pakfire_busy())) {
my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
if ("$cgiparams{'FORCE'}" eq "on") {
&General::system_background("/usr/local/bin/pakfire", "remove", "--non-interactive", "--no-colors", @pkgs);
} else {
&Header::openbox("100%", "center", $Lang::tr{'request'});
my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
print <<END;
@@ -262,30 +286,9 @@ END
&Header::closebigbox();
&Header::closepage();
exit;
}
} elsif (($cgiparams{'ACTION'} eq 'update') && (! &_is_pakfire_busy())) {
&General::system_background("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
} elsif (($cgiparams{'ACTION'} eq 'upgrade') && (! &_is_pakfire_busy())) {
&General::system_background("/usr/local/bin/pakfire", "upgrade", "-y", "--no-colors");
} elsif ($cgiparams{'ACTION'} eq "$Lang::tr{'save'}") {
$pakfiresettings{"TREE"} = $cgiparams{"TREE"};
# Check for valid input
if ($pakfiresettings{"TREE"} !~ m/^(stable|testing|unstable)$/) {
$errormessage .= $Lang::tr{'pakfire invalid tree'};
}
unless ($errormessage) {
&General::writehash("${General::swroot}/pakfire/settings", \%pakfiresettings);
# Update lists
&General::system_background("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
}
}
&General::readhash("${General::swroot}/pakfire/settings", \%pakfiresettings);
my %selected=();
my %checked=();