]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/pakfire.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
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. #
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. #
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/>. #
20 ###############################################################################
23 use List
::Util
qw(any);
25 # enable only the following on debugging purpose
27 #use CGI::Carp 'fatalsToBrowser';
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
32 require "/opt/pakfire/lib/functions.pl";
35 my $errormessage = '';
37 my %pakfiresettings = ();
38 my %mainsettings = ();
40 # Load general settings
41 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
42 &General
::readhash
("${General::swroot}/pakfire/settings", \
%pakfiresettings);
43 &General
::readhash
("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \
%color);
45 # Get CGI request data
46 $cgiparams{'ACTION'} = '';
47 $cgiparams{'FORCE'} = '';
49 $cgiparams{'INSPAKS'} = '';
50 $cgiparams{'DELPAKS'} = '';
52 &Header
::getcgihash
(\
%cgiparams);
54 ### Process AJAX/JSON request ###
55 if($cgiparams{'ACTION'} eq 'json-getstatus') {
59 # Read /var/log/messages backwards until a "Pakfire started" header is found,
60 # to capture all messages of the last (i.e. current) Pakfire run
61 my @messages = `tac /var/log/messages | sed -n '/pakfire:/{p;/Pakfire.*started/q}'`;
63 # Test if the log contains an error message (fastest implementation, stops at first match)
64 my $failure = any
{ index($_, 'ERROR') != -1 } @messages;
66 # Collect Pakfire status
68 'running' => &_is_pakfire_busy
() || "0",
69 'running_since' => &General
::age
("$Pakfire::lockfile") || "0s",
70 'reboot' => (-e
"/var/run/need_reboot") || "0",
71 'failure' => $failure || "0"
77 foreach my $key (keys %status) {
78 my $value = $status{$key};
79 print qq{\t"$key": "$value",\n};
82 # Print sanitized messages in reverse order to undo previous "tac"
83 print qq{\t"messages": [\n};
84 for my $index (reverse (0 .. $#messages)) {
85 my $line = $messages[$index];
86 $line =~ s/[[:cntrl:]<>&\\]+//g;
88 print qq{\t\t"$line"};
89 print ",\n" unless $index < 1;
93 # Finalize JSON file & stop
98 ### Process Pakfire install/update commands ###
99 if($cgiparams{'ACTION'} ne '') {
100 if(&_is_pakfire_busy
()) {
101 $errormessage = $Lang::tr
{'pakfire already busy'};
102 } elsif(($cgiparams{'ACTION'} eq 'install') && ($cgiparams{'FORCE'} eq 'on')) {
103 my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
104 &General
::system_background
("/usr/local/bin/pakfire", "install", "--non-interactive", "--no-colors", @pkgs);
105 } elsif(($cgiparams{'ACTION'} eq 'remove') && ($cgiparams{'FORCE'} eq 'on')) {
106 my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
107 &General
::system_background
("/usr/local/bin/pakfire", "remove", "--non-interactive", "--no-colors", @pkgs);
108 } elsif($cgiparams{'ACTION'} eq 'update') {
109 &General
::system_background
("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
110 } elsif($cgiparams{'ACTION'} eq 'upgrade') {
111 &General
::system_background
("/usr/local/bin/pakfire", "upgrade", "-y", "--no-colors");
112 } elsif($cgiparams{'ACTION'} eq $Lang::tr
{'save'}) {
113 $pakfiresettings{"TREE"} = $cgiparams{"TREE"};
115 # Check for valid input
116 if ($pakfiresettings{"TREE"} !~ m/^(stable|testing|unstable)$/) {
117 $errormessage .= $Lang::tr
{'pakfire invalid tree'};
120 unless ($errormessage) {
121 &General
::writehash
("${General::swroot}/pakfire/settings", \
%pakfiresettings);
124 &General
::system_background
("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
129 ### Start pakfire page ###
130 &Header
::showhttpheaders
();
132 ###--- HTML HEAD ---###
133 my $extraHead = <<END
138 border-style: hidden;
146 #pfmain tr:not(:last-child) > td {
147 padding-bottom: 1.5em;
149 #pfmain tr > td.heading {
152 background-color: $color{'color20'};
158 margin-bottom: 0.8em;
161 /* Pakfire log viewer */
162 section#pflog-header {
169 #pflog-header > div:last-child {
176 #pflog-header span:empty::before {
177 content: "\\200b"; /* zero width space */
183 border-top: 0.5px solid $Header::bordercolour;
191 <script src="/include/pakfire.js"></script>
195 'working': '$Lang::tr{'pakfire working'}',
196 'finished': '$Lang::tr{'pakfire finished'}',
197 'finished error': '$Lang::tr{'pakfire finished error'}',
198 'since': '$Lang::tr{'since'}',
200 'link_return': '<a href="$ENV{'SCRIPT_NAME'}">$Lang::tr{'pakfire return'}</a>',
201 'link_reboot': '<a href="/cgi-bin/shutdown.cgi">$Lang::tr{'needreboot'}</a>'
204 // AJAX auto refresh interval (in ms, default: 1000)
205 //pakfire.refreshInterval = 1000;
207 // Enable returning to main screen (delay in ms)
208 pakfire.setupPageReload(true, 3000);
212 ###--- END HTML HEAD ---###
214 &Header
::openpage
($Lang::tr
{'pakfire configuration'}, 1, $extraHead);
215 &Header
::openbigbox
('100%', 'left', '', $errormessage);
219 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
220 print "<font class='base'>$errormessage </font>\n";
224 # Show log output while Pakfire is running
225 if(&_is_pakfire_busy
()) {
226 &Header
::openbox
("100%", "center", "Pakfire");
229 <section id="pflog-header">
230 <div><img src="/images/indicator.gif" alt="$Lang::tr{'active'}" title="$Lang::tr{'pagerefresh'}"></div>
232 <span id="pflog-status">$Lang::tr{'pakfire working'}</span><br>
233 <span id="pflog-time"></span><br>
234 <span id="pflog-action"></span>
236 <div><a href="$ENV{'SCRIPT_NAME'}"><img src="/images/view-refresh.png" alt="$Lang::tr{'refresh'}" title="$Lang::tr{'refresh'}"></a></div>
239 <!-- Pakfire log messages -->
240 <pre id="pflog-messages"></pre>
242 // Start automatic log refresh
243 pakfire.running = true;
250 &Header
::closebigbox
();
251 &Header
::closepage
();
255 # Show Pakfire install/remove dependencies and confirm form
256 if (($cgiparams{'ACTION'} eq 'install') && (! &_is_pakfire_busy
())) {
257 &Header
::openbox
("100%", "center", $Lang::tr
{'request'});
259 my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
260 my @output = &General
::system_output
("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
262 <table><tr><td colspan='2'>$Lang::tr{'pakfire install package'} @pkgs $Lang::tr{'pakfire possible dependency'}
266 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
271 <tr><td colspan='2'>$Lang::tr{'pakfire accept all'}</td></tr>
272 <tr><td colspan='2'> </td></tr>
273 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
274 <input type='hidden' name='INSPAKS' value='$cgiparams{'INSPAKS'}' />
275 <input type='hidden' name='FORCE' value='on' />
276 <input type='hidden' name='ACTION' value='install' />
277 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/go-next.png' />
281 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
282 <input type='hidden' name='ACTION' value='' />
283 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
290 &Header
::closebigbox
();
291 &Header
::closepage
();
294 } elsif (($cgiparams{'ACTION'} eq 'remove') && (! &_is_pakfire_busy
())) {
295 &Header
::openbox
("100%", "center", $Lang::tr
{'request'});
297 my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
298 my @output = &General
::system_output
("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
300 <table><tr><td colspan='2'>$Lang::tr{'pakfire uninstall package'} @pkgs $Lang::tr{'pakfire possible dependency'}
304 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
309 <tr><td colspan='2'>$Lang::tr{'pakfire uninstall all'}</td></tr>
310 <tr><td colspan='2'> </td></tr>
311 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
312 <input type='hidden' name='DELPAKS' value='$cgiparams{'DELPAKS'}' />
313 <input type='hidden' name='FORCE' value='on' />
314 <input type='hidden' name='ACTION' value='remove' />
315 <input type='image' alt='$Lang::tr{'uninstall'}' title='$Lang::tr{'uninstall'}' src='/images/go-next.png' />
319 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
320 <input type='hidden' name='ACTION' value='' />
321 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
328 &Header
::closebigbox
();
329 &Header
::closepage
();
333 # Show Pakfire main page
337 $selected{"TREE"} = ();
338 $selected{"TREE"}{"stable"} = "";
339 $selected{"TREE"}{"testing"} = "";
340 $selected{"TREE"}{"unstable"} = "";
341 $selected{"TREE"}{$pakfiresettings{"TREE"}} = "selected";
343 my $core_release = `cat /opt/pakfire/db/core/mine 2>/dev/null`;
344 chomp($core_release);
345 my $core_update_age = &General
::age
("/opt/pakfire/db/core/mine");
346 my $corelist_update_age = &General
::age
("/opt/pakfire/db/lists/core-list.db");
347 my $server_update_age = &General
::age
("/opt/pakfire/db/lists/server-list.db");
348 my $packages_update_age = &General
::age
("/opt/pakfire/db/lists/packages_list.db");
350 &Header
::openbox
("100%", "center", "Pakfire");
355 if ( -e
"/var/run/need_reboot") {
356 print "\t\t<tr><td colspan='2'><a href='/cgi-bin/shutdown.cgi'>$Lang::tr{'needreboot'}!</a></td></tr>\n";
359 <tr><td class="heading">$Lang::tr{'pakfire system state'}:</td>
360 <td class="heading">$Lang::tr{'available updates'}:</td></tr>
362 <tr><td><strong>$Lang::tr{'pakfire core update level'}: $core_release</strong>
365 $Lang::tr{'pakfire last update'} $core_update_age $Lang::tr{'pakfire ago'}<br>
366 $Lang::tr{'pakfire last serverlist update'} $server_update_age $Lang::tr{'pakfire ago'}<br>
367 $Lang::tr{'pakfire last core list update'} $corelist_update_age $Lang::tr{'pakfire ago'}<br>
368 $Lang::tr{'pakfire last package update'} $packages_update_age $Lang::tr{'pakfire ago'}
370 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
371 <input type='hidden' name='ACTION' value='update' />
372 <input type='submit' value='$Lang::tr{'calamaris refresh list'}' />
376 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
377 <select name="UPDPAKS" class="pflist" size="5" disabled>
380 &Pakfire
::dblist
("upgrade", "forweb");
383 <input type='hidden' name='ACTION' value='upgrade' />
384 <input type='image' alt='$Lang::tr{'upgrade'}' title='$Lang::tr{'upgrade'}' src='/images/document-save.png' />
388 <tr><td class="heading">$Lang::tr{'pakfire available addons'}</td>
389 <td class="heading">$Lang::tr{'pakfire installed addons'}</td></tr>
391 <tr><td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire install description'}</p>
392 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
393 <select name="INSPAKS" class="pflist" size="10" multiple>
396 &Pakfire
::dblist
("notinstalled", "forweb");
399 <input type='hidden' name='ACTION' value='install' />
400 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/list-add.png' />
403 <td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire uninstall description'}</p>
404 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
405 <select name="DELPAKS" class="pflist" size="10" multiple>
408 &Pakfire
::dblist
("installed", "forweb");
411 <input type='hidden' name='ACTION' value='remove' />
412 <input type='image' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' src='/images/list-remove.png' />
420 &Header
::openbox
("100%", "center", "$Lang::tr{'settings'}");
423 <form method='POST' action='$ENV{'SCRIPT_NAME'}'>
426 <td align='left' width='45%'>$Lang::tr{'pakfire tree'}</td>
427 <td width="55%" align="left">
429 <option value="stable" $selected{"TREE"}{"stable"}>$Lang::tr{'pakfire tree stable'}</option>
430 <option value="testing" $selected{"TREE"}{"testing"}>$Lang::tr{'pakfire tree testing'}</option>
431 <option value="unstable" $selected{"TREE"}{"unstable"}>$Lang::tr{'pakfire tree unstable'}</option>
436 <td colspan="2"> </td>
439 <td colspan="2" align="center">
440 <input type="submit" name="ACTION" value="$Lang::tr{'save'}" />
448 &Header
::closebigbox
();
449 &Header
::closepage
();
451 ###--- Internal functions ---###
453 # Check if pakfire is already running (extend test here if necessary)
454 sub _is_pakfire_busy
{
455 # Return immediately if lockfile is present
456 if(-e
"$Pakfire::lockfile") {
460 # Check if a PID of a running pakfire instance is found
461 # (The system backpipe command is safe, because no user input is computed.)
462 my $pakfire_pid = `pidof -s /usr/local/bin/pakfire`;
469 # Pakfire isn't running
474 sub _start_json_output
{
475 print "Cache-Control: no-cache, no-store\n";
476 print "Content-Type: application/json\n";
477 print "\n"; # End of HTTP headers