]>
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
("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \
%color);
44 # Get CGI request data
45 $cgiparams{'ACTION'} = '';
46 $cgiparams{'VALID'} = '';
48 $cgiparams{'INSPAKS'} = '';
49 $cgiparams{'DELPAKS'} = '';
51 &Header
::getcgihash
(\
%cgiparams);
53 ### Process AJAX/JSON request ###
54 if($cgiparams{'ACTION'} eq 'json-getstatus') {
58 # Read /var/log/messages backwards until a "Pakfire started" header is found,
59 # to capture all messages of the last (i.e. current) Pakfire run
60 my @messages = `tac /var/log/messages | sed -n '/pakfire:/{p;/Pakfire.*started/q}'`;
62 # Test if the log contains an error message (fastest implementation, stops at first match)
63 my $failure = any
{ index($_, 'ERROR') != -1 } @messages;
65 # Collect Pakfire status
67 'running' => &_is_pakfire_busy
() || "0",
68 'running_since' => &General
::age
("$Pakfire::lockfile") || "0s",
69 'reboot' => (-e
"/var/run/need_reboot") || "0",
70 'failure' => $failure || "0"
76 foreach my $key (keys %status) {
77 my $value = $status{$key};
78 print qq{\t"$key": "$value",\n};
81 # Print sanitized messages in reverse order to undo previous "tac"
82 print qq{\t"messages": [\n};
83 for my $index (reverse (0 .. $#messages)) {
84 my $line = $messages[$index];
85 $line =~ s/[[:cntrl:]<>&\\]+//g;
87 print qq{\t\t"$line"};
88 print ",\n" unless $index < 1;
92 # Finalize JSON file & stop
97 ### Start pakfire page ###
98 &Header
::showhttpheaders
();
100 ###--- HTML HEAD ---###
101 my $extraHead = <<END
106 border-style: hidden;
114 #pfmain tr:not(:last-child) > td {
115 padding-bottom: 1.5em;
117 #pfmain tr > td.heading {
120 background-color: $color{'color20'};
126 margin-bottom: 0.8em;
129 /* Pakfire log viewer */
130 section#pflog-header {
137 #pflog-header > div:last-child {
144 #pflog-header span:empty::before {
145 content: "\\200b"; /* zero width space */
151 border-top: 0.5px solid $Header::bordercolour;
159 <script src="/include/pakfire.js"></script>
163 'working': '$Lang::tr{'pakfire working'}',
164 'finished': '$Lang::tr{'pakfire finished'}',
165 'finished error': '$Lang::tr{'pakfire finished error'}',
166 'since': '$Lang::tr{'since'}',
168 'link_return': '<a href="$ENV{'SCRIPT_NAME'}">$Lang::tr{'pakfire return'}</a>',
169 'link_reboot': '<a href="/cgi-bin/shutdown.cgi">$Lang::tr{'needreboot'}</a>'
172 // AJAX auto refresh interval (in ms, default: 1000)
173 //pakfire.refreshInterval = 1000;
175 // Enable returning to main screen (delay in ms)
176 pakfire.setupPageReload(true, 3000);
180 ###--- END HTML HEAD ---###
182 &Header
::openpage
($Lang::tr
{'pakfire configuration'}, 1, $extraHead);
183 &Header
::openbigbox
('100%', 'left', '', $errormessage);
185 # Process Pakfire commands
186 if (($cgiparams{'ACTION'} eq 'install') && (! &_is_pakfire_busy
())) {
187 my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
188 if ("$cgiparams{'FORCE'}" eq "on") {
189 &General
::system_background
("/usr/local/bin/pakfire", "install", "--non-interactive", "--no-colors", @pkgs);
191 &Header
::openbox
("100%", "center", $Lang::tr
{'request'});
192 my @output = &General
::system_output
("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
194 <table><tr><td colspan='2'>$Lang::tr{'pakfire install package'} @pkgs $Lang::tr{'pakfire possible dependency'}
198 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
203 <tr><td colspan='2'>$Lang::tr{'pakfire accept all'}</td></tr>
204 <tr><td colspan='2'> </td></tr>
205 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
206 <input type='hidden' name='INSPAKS' value='$cgiparams{'INSPAKS'}' />
207 <input type='hidden' name='FORCE' value='on' />
208 <input type='hidden' name='ACTION' value='install' />
209 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/go-next.png' />
213 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
214 <input type='hidden' name='ACTION' value='' />
215 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
222 &Header
::closebigbox
();
223 &Header
::closepage
();
226 } elsif (($cgiparams{'ACTION'} eq 'remove') && (! &_is_pakfire_busy
())) {
227 my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
228 if ("$cgiparams{'FORCE'}" eq "on") {
229 &General
::system_background
("/usr/local/bin/pakfire", "remove", "--non-interactive", "--no-colors", @pkgs);
231 &Header
::openbox
("100%", "center", $Lang::tr
{'request'});
232 my @output = &General
::system_output
("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
234 <table><tr><td colspan='2'>$Lang::tr{'pakfire uninstall package'} @pkgs $Lang::tr{'pakfire possible dependency'}
238 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
243 <tr><td colspan='2'>$Lang::tr{'pakfire uninstall all'}</td></tr>
244 <tr><td colspan='2'> </td></tr>
245 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
246 <input type='hidden' name='DELPAKS' value='$cgiparams{'DELPAKS'}' />
247 <input type='hidden' name='FORCE' value='on' />
248 <input type='hidden' name='ACTION' value='remove' />
249 <input type='image' alt='$Lang::tr{'uninstall'}' title='$Lang::tr{'uninstall'}' src='/images/go-next.png' />
253 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
254 <input type='hidden' name='ACTION' value='' />
255 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
262 &Header
::closebigbox
();
263 &Header
::closepage
();
267 } elsif (($cgiparams{'ACTION'} eq 'update') && (! &_is_pakfire_busy
())) {
268 &General
::system_background
("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
269 } elsif (($cgiparams{'ACTION'} eq 'upgrade') && (! &_is_pakfire_busy
())) {
270 &General
::system_background
("/usr/local/bin/pakfire", "upgrade", "-y", "--no-colors");
271 } elsif ($cgiparams{'ACTION'} eq "$Lang::tr{'save'}") {
272 $pakfiresettings{"TREE"} = $cgiparams{"TREE"};
274 # Check for valid input
275 if ($pakfiresettings{"TREE"} !~ m/^(stable|testing|unstable)$/) {
276 $errormessage .= $Lang::tr
{'pakfire invalid tree'};
279 unless ($errormessage) {
280 &General
::writehash
("${General::swroot}/pakfire/settings", \
%pakfiresettings);
283 &General
::system_background
("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
287 &General
::readhash
("${General::swroot}/pakfire/settings", \
%pakfiresettings);
292 $selected{"TREE"} = ();
293 $selected{"TREE"}{"stable"} = "";
294 $selected{"TREE"}{"testing"} = "";
295 $selected{"TREE"}{"unstable"} = "";
296 $selected{"TREE"}{$pakfiresettings{"TREE"}} = "selected";
298 # DPC move error message to top so it is seen!
300 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
301 print "<font class='base'>$errormessage </font>\n";
305 # Show log output while Pakfire is running
306 if(&_is_pakfire_busy
()) {
307 &Header
::openbox
("100%", "center", "Pakfire");
310 <section id="pflog-header">
311 <div><img src="/images/indicator.gif" alt="$Lang::tr{'active'}" title="$Lang::tr{'pagerefresh'}"></div>
313 <span id="pflog-status">$Lang::tr{'pakfire working'}</span><br>
314 <span id="pflog-time"></span><br>
315 <span id="pflog-action"></span>
317 <div><a href="$ENV{'SCRIPT_NAME'}"><img src="/images/view-refresh.png" alt="$Lang::tr{'refresh'}" title="$Lang::tr{'refresh'}"></a></div>
320 <!-- Pakfire log messages -->
321 <pre id="pflog-messages"></pre>
323 // Start automatic log refresh
324 pakfire.running = true;
331 &Header
::closebigbox
();
332 &Header
::closepage
();
336 my $core_release = `cat /opt/pakfire/db/core/mine 2>/dev/null`;
337 chomp($core_release);
338 my $core_update_age = &General
::age
("/opt/pakfire/db/core/mine");
339 my $corelist_update_age = &General
::age
("/opt/pakfire/db/lists/core-list.db");
340 my $server_update_age = &General
::age
("/opt/pakfire/db/lists/server-list.db");
341 my $packages_update_age = &General
::age
("/opt/pakfire/db/lists/packages_list.db");
343 &Header
::openbox
("100%", "center", "Pakfire");
348 if ( -e
"/var/run/need_reboot") {
349 print "\t\t<tr><td colspan='2'><a href='/cgi-bin/shutdown.cgi'>$Lang::tr{'needreboot'}!</a></td></tr>\n";
352 <tr><td class="heading">$Lang::tr{'pakfire system state'}:</td>
353 <td class="heading">$Lang::tr{'available updates'}:</td></tr>
355 <tr><td><strong>$Lang::tr{'pakfire core update level'}: $core_release</strong>
358 $Lang::tr{'pakfire last update'} $core_update_age $Lang::tr{'pakfire ago'}<br>
359 $Lang::tr{'pakfire last serverlist update'} $server_update_age $Lang::tr{'pakfire ago'}<br>
360 $Lang::tr{'pakfire last core list update'} $corelist_update_age $Lang::tr{'pakfire ago'}<br>
361 $Lang::tr{'pakfire last package update'} $packages_update_age $Lang::tr{'pakfire ago'}
363 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
364 <input type='hidden' name='ACTION' value='update' />
365 <input type='submit' value='$Lang::tr{'calamaris refresh list'}' />
369 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
370 <select name="UPDPAKS" class="pflist" size="5" disabled>
373 &Pakfire
::dblist
("upgrade", "forweb");
376 <input type='hidden' name='ACTION' value='upgrade' />
377 <input type='image' alt='$Lang::tr{'upgrade'}' title='$Lang::tr{'upgrade'}' src='/images/document-save.png' />
381 <tr><td class="heading">$Lang::tr{'pakfire available addons'}</td>
382 <td class="heading">$Lang::tr{'pakfire installed addons'}</td></tr>
384 <tr><td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire install description'}</p>
385 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
386 <select name="INSPAKS" class="pflist" size="10" multiple>
389 &Pakfire
::dblist
("notinstalled", "forweb");
392 <input type='hidden' name='ACTION' value='install' />
393 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/list-add.png' />
396 <td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire uninstall description'}</p>
397 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
398 <select name="DELPAKS" class="pflist" size="10" multiple>
401 &Pakfire
::dblist
("installed", "forweb");
404 <input type='hidden' name='ACTION' value='remove' />
405 <input type='image' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' src='/images/list-remove.png' />
413 &Header
::openbox
("100%", "center", "$Lang::tr{'settings'}");
416 <form method='POST' action='$ENV{'SCRIPT_NAME'}'>
419 <td align='left' width='45%'>$Lang::tr{'pakfire tree'}</td>
420 <td width="55%" align="left">
422 <option value="stable" $selected{"TREE"}{"stable"}>$Lang::tr{'pakfire tree stable'}</option>
423 <option value="testing" $selected{"TREE"}{"testing"}>$Lang::tr{'pakfire tree testing'}</option>
424 <option value="unstable" $selected{"TREE"}{"unstable"}>$Lang::tr{'pakfire tree unstable'}</option>
429 <td colspan="2"> </td>
432 <td colspan="2" align="center">
433 <input type="submit" name="ACTION" value="$Lang::tr{'save'}" />
441 &Header
::closebigbox
();
442 &Header
::closepage
();
444 ###--- Internal functions ---###
446 # Check if pakfire is already running (extend test here if necessary)
447 sub _is_pakfire_busy
{
448 # Return immediately if lockfile is present
449 if(-e
"$Pakfire::lockfile") {
453 # Check if a PID of a running pakfire instance is found
454 # (The system backpipe command is safe, because no user input is computed.)
455 my $pakfire_pid = `pidof -s /usr/local/bin/pakfire`;
462 # Pakfire isn't running
467 sub _start_json_output
{
468 print "Cache-Control: no-cache, no-store\n";
469 print "Content-Type: application/json\n";
470 print "\n"; # End of HTTP headers