]>
git.ipfire.org Git - 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 '') && (! &_is_pakfire_busy
())) {
100 if(($cgiparams{'ACTION'} eq 'install') && ($cgiparams{'FORCE'} eq 'on')) {
101 my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
102 &General
::system_background
("/usr/local/bin/pakfire", "install", "--non-interactive", "--no-colors", @pkgs);
103 } elsif(($cgiparams{'ACTION'} eq 'remove') && ($cgiparams{'FORCE'} eq 'on')) {
104 my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
105 &General
::system_background
("/usr/local/bin/pakfire", "remove", "--non-interactive", "--no-colors", @pkgs);
106 } elsif($cgiparams{'ACTION'} eq 'update') {
107 &General
::system_background
("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
108 } elsif($cgiparams{'ACTION'} eq 'upgrade') {
109 &General
::system_background
("/usr/local/bin/pakfire", "upgrade", "-y", "--no-colors");
110 } elsif($cgiparams{'ACTION'} eq $Lang::tr
{'save'}) {
111 $pakfiresettings{"TREE"} = $cgiparams{"TREE"};
113 # Check for valid input
114 if ($pakfiresettings{"TREE"} !~ m/^(stable|testing|unstable)$/) {
115 $errormessage .= $Lang::tr
{'pakfire invalid tree'};
118 unless ($errormessage) {
119 &General
::writehash
("${General::swroot}/pakfire/settings", \
%pakfiresettings);
122 &General
::system_background
("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
127 ### Start pakfire page ###
128 &Header
::showhttpheaders
();
130 ###--- HTML HEAD ---###
131 my $extraHead = <<END
136 border-style: hidden;
144 #pfmain tr:not(:last-child) > td {
145 padding-bottom: 1.5em;
147 #pfmain tr > td.heading {
150 background-color: $color{'color20'};
156 margin-bottom: 0.8em;
159 /* Pakfire log viewer */
160 section#pflog-header {
167 #pflog-header > div:last-child {
174 #pflog-header span:empty::before {
175 content: "\\200b"; /* zero width space */
181 border-top: 0.5px solid $Header::bordercolour;
189 <script src="/include/pakfire.js"></script>
193 'working': '$Lang::tr{'pakfire working'}',
194 'finished': '$Lang::tr{'pakfire finished'}',
195 'finished error': '$Lang::tr{'pakfire finished error'}',
196 'since': '$Lang::tr{'since'}',
198 'link_return': '<a href="$ENV{'SCRIPT_NAME'}">$Lang::tr{'pakfire return'}</a>',
199 'link_reboot': '<a href="/cgi-bin/shutdown.cgi">$Lang::tr{'needreboot'}</a>'
202 // AJAX auto refresh interval (in ms, default: 1000)
203 //pakfire.refreshInterval = 1000;
205 // Enable returning to main screen (delay in ms)
206 pakfire.setupPageReload(true, 3000);
210 ###--- END HTML HEAD ---###
212 &Header
::openpage
($Lang::tr
{'pakfire configuration'}, 1, $extraHead);
213 &Header
::openbigbox
('100%', 'left', '', $errormessage);
215 # Process Pakfire commands
216 if (($cgiparams{'ACTION'} eq 'install') && (! &_is_pakfire_busy
())) {
217 my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
218 &Header
::openbox
("100%", "center", $Lang::tr
{'request'});
219 my @output = &General
::system_output
("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
221 <table><tr><td colspan='2'>$Lang::tr{'pakfire install package'} @pkgs $Lang::tr{'pakfire possible dependency'}
225 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
230 <tr><td colspan='2'>$Lang::tr{'pakfire accept all'}</td></tr>
231 <tr><td colspan='2'> </td></tr>
232 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
233 <input type='hidden' name='INSPAKS' value='$cgiparams{'INSPAKS'}' />
234 <input type='hidden' name='FORCE' value='on' />
235 <input type='hidden' name='ACTION' value='install' />
236 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/go-next.png' />
240 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
241 <input type='hidden' name='ACTION' value='' />
242 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
249 &Header
::closebigbox
();
250 &Header
::closepage
();
253 } elsif (($cgiparams{'ACTION'} eq 'remove') && (! &_is_pakfire_busy
())) {
254 my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
255 &Header
::openbox
("100%", "center", $Lang::tr
{'request'});
256 my @output = &General
::system_output
("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
258 <table><tr><td colspan='2'>$Lang::tr{'pakfire uninstall package'} @pkgs $Lang::tr{'pakfire possible dependency'}
262 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
267 <tr><td colspan='2'>$Lang::tr{'pakfire uninstall all'}</td></tr>
268 <tr><td colspan='2'> </td></tr>
269 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
270 <input type='hidden' name='DELPAKS' value='$cgiparams{'DELPAKS'}' />
271 <input type='hidden' name='FORCE' value='on' />
272 <input type='hidden' name='ACTION' value='remove' />
273 <input type='image' alt='$Lang::tr{'uninstall'}' title='$Lang::tr{'uninstall'}' src='/images/go-next.png' />
277 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
278 <input type='hidden' name='ACTION' value='' />
279 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
286 &Header
::closebigbox
();
287 &Header
::closepage
();
295 $selected{"TREE"} = ();
296 $selected{"TREE"}{"stable"} = "";
297 $selected{"TREE"}{"testing"} = "";
298 $selected{"TREE"}{"unstable"} = "";
299 $selected{"TREE"}{$pakfiresettings{"TREE"}} = "selected";
301 # DPC move error message to top so it is seen!
303 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
304 print "<font class='base'>$errormessage </font>\n";
308 # Show log output while Pakfire is running
309 if(&_is_pakfire_busy
()) {
310 &Header
::openbox
("100%", "center", "Pakfire");
313 <section id="pflog-header">
314 <div><img src="/images/indicator.gif" alt="$Lang::tr{'active'}" title="$Lang::tr{'pagerefresh'}"></div>
316 <span id="pflog-status">$Lang::tr{'pakfire working'}</span><br>
317 <span id="pflog-time"></span><br>
318 <span id="pflog-action"></span>
320 <div><a href="$ENV{'SCRIPT_NAME'}"><img src="/images/view-refresh.png" alt="$Lang::tr{'refresh'}" title="$Lang::tr{'refresh'}"></a></div>
323 <!-- Pakfire log messages -->
324 <pre id="pflog-messages"></pre>
326 // Start automatic log refresh
327 pakfire.running = true;
334 &Header
::closebigbox
();
335 &Header
::closepage
();
339 my $core_release = `cat /opt/pakfire/db/core/mine 2>/dev/null`;
340 chomp($core_release);
341 my $core_update_age = &General
::age
("/opt/pakfire/db/core/mine");
342 my $corelist_update_age = &General
::age
("/opt/pakfire/db/lists/core-list.db");
343 my $server_update_age = &General
::age
("/opt/pakfire/db/lists/server-list.db");
344 my $packages_update_age = &General
::age
("/opt/pakfire/db/lists/packages_list.db");
346 &Header
::openbox
("100%", "center", "Pakfire");
351 if ( -e
"/var/run/need_reboot") {
352 print "\t\t<tr><td colspan='2'><a href='/cgi-bin/shutdown.cgi'>$Lang::tr{'needreboot'}!</a></td></tr>\n";
355 <tr><td class="heading">$Lang::tr{'pakfire system state'}:</td>
356 <td class="heading">$Lang::tr{'available updates'}:</td></tr>
358 <tr><td><strong>$Lang::tr{'pakfire core update level'}: $core_release</strong>
361 $Lang::tr{'pakfire last update'} $core_update_age $Lang::tr{'pakfire ago'}<br>
362 $Lang::tr{'pakfire last serverlist update'} $server_update_age $Lang::tr{'pakfire ago'}<br>
363 $Lang::tr{'pakfire last core list update'} $corelist_update_age $Lang::tr{'pakfire ago'}<br>
364 $Lang::tr{'pakfire last package update'} $packages_update_age $Lang::tr{'pakfire ago'}
366 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
367 <input type='hidden' name='ACTION' value='update' />
368 <input type='submit' value='$Lang::tr{'calamaris refresh list'}' />
372 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
373 <select name="UPDPAKS" class="pflist" size="5" disabled>
376 &Pakfire
::dblist
("upgrade", "forweb");
379 <input type='hidden' name='ACTION' value='upgrade' />
380 <input type='image' alt='$Lang::tr{'upgrade'}' title='$Lang::tr{'upgrade'}' src='/images/document-save.png' />
384 <tr><td class="heading">$Lang::tr{'pakfire available addons'}</td>
385 <td class="heading">$Lang::tr{'pakfire installed addons'}</td></tr>
387 <tr><td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire install description'}</p>
388 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
389 <select name="INSPAKS" class="pflist" size="10" multiple>
392 &Pakfire
::dblist
("notinstalled", "forweb");
395 <input type='hidden' name='ACTION' value='install' />
396 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/list-add.png' />
399 <td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire uninstall description'}</p>
400 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
401 <select name="DELPAKS" class="pflist" size="10" multiple>
404 &Pakfire
::dblist
("installed", "forweb");
407 <input type='hidden' name='ACTION' value='remove' />
408 <input type='image' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' src='/images/list-remove.png' />
416 &Header
::openbox
("100%", "center", "$Lang::tr{'settings'}");
419 <form method='POST' action='$ENV{'SCRIPT_NAME'}'>
422 <td align='left' width='45%'>$Lang::tr{'pakfire tree'}</td>
423 <td width="55%" align="left">
425 <option value="stable" $selected{"TREE"}{"stable"}>$Lang::tr{'pakfire tree stable'}</option>
426 <option value="testing" $selected{"TREE"}{"testing"}>$Lang::tr{'pakfire tree testing'}</option>
427 <option value="unstable" $selected{"TREE"}{"unstable"}>$Lang::tr{'pakfire tree unstable'}</option>
432 <td colspan="2"> </td>
435 <td colspan="2" align="center">
436 <input type="submit" name="ACTION" value="$Lang::tr{'save'}" />
444 &Header
::closebigbox
();
445 &Header
::closepage
();
447 ###--- Internal functions ---###
449 # Check if pakfire is already running (extend test here if necessary)
450 sub _is_pakfire_busy
{
451 # Return immediately if lockfile is present
452 if(-e
"$Pakfire::lockfile") {
456 # Check if a PID of a running pakfire instance is found
457 # (The system backpipe command is safe, because no user input is computed.)
458 my $pakfire_pid = `pidof -s /usr/local/bin/pakfire`;
465 # Pakfire isn't running
470 sub _start_json_output
{
471 print "Cache-Control: no-cache, no-store\n";
472 print "Content-Type: application/json\n";
473 print "\n"; # End of HTTP headers