]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/pakfire.cgi
pakfire.cgi: Cosmetic fix
[ipfire-2.x.git] / html / cgi-bin / pakfire.cgi
CommitLineData
3ea75603 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
b81c77b9 5# Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
70df8302
MT
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
3ea75603
MT
21
22use strict;
4d70f591 23use List::Util qw(any);
3ea75603
MT
24
25# enable only the following on debugging purpose
cb5e9c6c
CS
26#use warnings;
27#use CGI::Carp 'fatalsToBrowser';
3ea75603 28
986e08d9 29require '/var/ipfire/general-functions.pl';
3ea75603
MT
30require "${General::swroot}/lang.pl";
31require "${General::swroot}/header.pl";
1bd42c89 32require "/opt/pakfire/lib/functions.pl";
3ea75603 33
131f163c 34my %cgiparams=();
3ea75603 35my $errormessage = '';
cb5e9c6c 36my %color = ();
131f163c 37my %pakfiresettings = ();
cb5e9c6c 38my %mainsettings = ();
3ea75603 39
db9ee62e
LAH
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);
3ea75603 43
db9ee62e 44# Get CGI request data
131f163c
MT
45$cgiparams{'ACTION'} = '';
46$cgiparams{'VALID'} = '';
3ea75603 47
131f163c
MT
48$cgiparams{'INSPAKS'} = '';
49$cgiparams{'DELPAKS'} = '';
3ea75603 50
131f163c 51&Header::getcgihash(\%cgiparams);
d40aff35 52
db9ee62e
LAH
53### Process AJAX/JSON request ###
54if($cgiparams{'ACTION'} eq 'json-getstatus') {
55 # Send HTTP headers
56 _start_json_output();
57
4d70f591
LAH
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}'`;
61
62 # Test if the log contains an error message (fastest implementation, stops at first match)
63 my $failure = any{ index($_, 'ERROR') != -1 } @messages;
64
65 # Collect Pakfire status
db9ee62e
LAH
66 my %status = (
67 'running' => &_is_pakfire_busy() || "0",
68 'running_since' => &General::age("$Pakfire::lockfile") || "0s",
4d70f591
LAH
69 'reboot' => (-e "/var/run/need_reboot") || "0",
70 'failure' => $failure || "0"
db9ee62e 71 );
db9ee62e
LAH
72
73 # Start JSON file
74 print "{\n";
75
76 foreach my $key (keys %status) {
77 my $value = $status{$key};
78 print qq{\t"$key": "$value",\n};
79 }
80
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;
86
87 print qq{\t\t"$line"};
88 print ",\n" unless $index < 1;
89 }
90 print "\n\t]\n";
91
92 # Finalize JSON file & stop
93 print "}";
94 exit;
95}
96
97### Start pakfire page ###
98&Header::showhttpheaders();
99
100###--- HTML HEAD ---###
101my $extraHead = <<END
102<style>
219dacef
LAH
103 /* Main screen */
104 table#pfmain {
105 width: 100%;
106 border-style: hidden;
107 table-layout: fixed;
108 }
109
110 #pfmain td {
111 padding: 5px 20px 0;
112 text-align: center;
113 }
114 #pfmain tr:not(:last-child) > td {
115 padding-bottom: 1.5em;
116 }
117 #pfmain tr > td.heading {
118 padding: 0;
119 font-weight: bold;
120 background-color: $color{'color20'};
121 }
122
123 .pflist {
124 width: 100%;
125 text-align: left;
126 margin-bottom: 0.8em;
127 }
128
db9ee62e
LAH
129 /* Pakfire log viewer */
130 section#pflog-header {
131 width: 100%;
132 display: flex;
133 text-align: left;
134 align-items: center;
135 column-gap: 20px;
136 }
137 #pflog-header > div:last-child {
138 margin-left: auto;
139 margin-right: 20px;
140 }
141 #pflog-header span {
142 line-height: 1.3em;
143 }
144 #pflog-header span:empty::before {
145 content: "\\200b"; /* zero width space */
146 }
147
148 pre#pflog-messages {
149 margin-top: 0.7em;
150 padding-top: 0.7em;
151 border-top: 0.5px solid $Header::bordercolour;
3ea75603 152
db9ee62e
LAH
153 text-align: left;
154 min-height: 15em;
155 overflow-x: auto;
156 }
157</style>
158
159<script src="/include/pakfire.js"></script>
160<script>
161 // Translations
162 pakfire.i18n.load({
163 'working': '$Lang::tr{'pakfire working'}',
524bbe32 164 'finished': '$Lang::tr{'pakfire finished'}',
4d70f591
LAH
165 'finished error': '$Lang::tr{'pakfire finished error'}',
166 'since': '$Lang::tr{'since'}',
db9ee62e 167
524bbe32 168 'link_return': '<a href="$ENV{'SCRIPT_NAME'}">$Lang::tr{'pakfire return'}</a>',
db9ee62e
LAH
169 'link_reboot': '<a href="/cgi-bin/shutdown.cgi">$Lang::tr{'needreboot'}</a>'
170 });
4d70f591
LAH
171
172 // AJAX auto refresh interval (in ms, default: 1000)
173 //pakfire.refreshInterval = 1000;
174
175 // Enable returning to main screen (delay in ms)
176 pakfire.setupPageReload(true, 3000);
db9ee62e
LAH
177</script>
178END
179;
180###--- END HTML HEAD ---###
181
182&Header::openpage($Lang::tr{'pakfire configuration'}, 1, $extraHead);
1bd42c89 183&Header::openbigbox('100%', 'left', '', $errormessage);
3ea75603 184
db9ee62e 185# Process Pakfire commands
d255e2d1 186if (($cgiparams{'ACTION'} eq 'install') && (! &_is_pakfire_busy())) {
75ee0279 187 my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
131f163c 188 if ("$cgiparams{'FORCE'}" eq "on") {
75ee0279 189 &General::system_background("/usr/local/bin/pakfire", "install", "--non-interactive", "--no-colors", @pkgs);
5b2a12ff 190 } else {
40228ef4 191 &Header::openbox("100%", "center", $Lang::tr{'request'});
75ee0279 192 my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
5b2a12ff 193 print <<END;
124926ee 194 <table><tr><td colspan='2'>$Lang::tr{'pakfire install package'} @pkgs $Lang::tr{'pakfire possible dependency'}
fee5c6b7 195 <pre>
5b2a12ff
MT
196END
197 foreach (@output) {
fee5c6b7 198 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
5b2a12ff
MT
199 print "$_\n";
200 }
201 print <<END;
219dacef
LAH
202 </pre></td></tr>
203 <tr><td colspan='2'>$Lang::tr{'pakfire accept all'}</td></tr>
204 <tr><td colspan='2'>&nbsp;</td></tr>
5b2a12ff 205 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
131f163c 206 <input type='hidden' name='INSPAKS' value='$cgiparams{'INSPAKS'}' />
5b2a12ff
MT
207 <input type='hidden' name='FORCE' value='on' />
208 <input type='hidden' name='ACTION' value='install' />
f8aa0679 209 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/go-next.png' />
5b2a12ff 210 </form>
219dacef 211 </td>
5b2a12ff
MT
212 <td align='left'>
213 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
214 <input type='hidden' name='ACTION' value='' />
f8aa0679 215 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
5b2a12ff 216 </form>
219dacef
LAH
217 </td>
218 </tr>
5b2a12ff
MT
219 </table>
220END
221 &Header::closebox();
222 &Header::closebigbox();
223 &Header::closepage();
224 exit;
225 }
d255e2d1 226} elsif (($cgiparams{'ACTION'} eq 'remove') && (! &_is_pakfire_busy())) {
75ee0279 227 my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
131f163c 228 if ("$cgiparams{'FORCE'}" eq "on") {
75ee0279 229 &General::system_background("/usr/local/bin/pakfire", "remove", "--non-interactive", "--no-colors", @pkgs);
74693811 230 } else {
40228ef4 231 &Header::openbox("100%", "center", $Lang::tr{'request'});
75ee0279 232 my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
74693811 233 print <<END;
124926ee 234 <table><tr><td colspan='2'>$Lang::tr{'pakfire uninstall package'} @pkgs $Lang::tr{'pakfire possible dependency'}
fee5c6b7 235 <pre>
74693811
CS
236END
237 foreach (@output) {
fee5c6b7 238 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
74693811
CS
239 print "$_\n";
240 }
241 print <<END;
219dacef
LAH
242 </pre></td></tr>
243 <tr><td colspan='2'>$Lang::tr{'pakfire uninstall all'}</td></tr>
244 <tr><td colspan='2'>&nbsp;</td></tr>
74693811 245 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
131f163c 246 <input type='hidden' name='DELPAKS' value='$cgiparams{'DELPAKS'}' />
74693811
CS
247 <input type='hidden' name='FORCE' value='on' />
248 <input type='hidden' name='ACTION' value='remove' />
f8aa0679 249 <input type='image' alt='$Lang::tr{'uninstall'}' title='$Lang::tr{'uninstall'}' src='/images/go-next.png' />
74693811 250 </form>
219dacef 251 </td>
74693811
CS
252 <td align='left'>
253 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
254 <input type='hidden' name='ACTION' value='' />
f8aa0679 255 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
74693811 256 </form>
219dacef
LAH
257 </td>
258 </tr>
74693811
CS
259 </table>
260END
261 &Header::closebox();
262 &Header::closebigbox();
263 &Header::closepage();
264 exit;
265 }
266
d255e2d1 267} elsif (($cgiparams{'ACTION'} eq 'update') && (! &_is_pakfire_busy())) {
bce85943 268 &General::system_background("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
d255e2d1 269} elsif (($cgiparams{'ACTION'} eq 'upgrade') && (! &_is_pakfire_busy())) {
bce85943 270 &General::system_background("/usr/local/bin/pakfire", "upgrade", "-y", "--no-colors");
131f163c 271} elsif ($cgiparams{'ACTION'} eq "$Lang::tr{'save'}") {
f61be862
MT
272 $pakfiresettings{"TREE"} = $cgiparams{"TREE"};
273
274 # Check for valid input
275 if ($pakfiresettings{"TREE"} !~ m/^(stable|testing|unstable)$/) {
276 $errormessage .= $Lang::tr{'pakfire invalid tree'};
277 }
278
279 unless ($errormessage) {
280 &General::writehash("${General::swroot}/pakfire/settings", \%pakfiresettings);
281
282 # Update lists
bce85943 283 &General::system_background("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
f61be862 284 }
3ea75603
MT
285}
286
287&General::readhash("${General::swroot}/pakfire/settings", \%pakfiresettings);
288
3ea75603
MT
289my %selected=();
290my %checked=();
291
f61be862
MT
292$selected{"TREE"} = ();
293$selected{"TREE"}{"stable"} = "";
294$selected{"TREE"}{"testing"} = "";
295$selected{"TREE"}{"unstable"} = "";
296$selected{"TREE"}{$pakfiresettings{"TREE"}} = "selected";
297
3ea75603
MT
298# DPC move error message to top so it is seen!
299if ($errormessage) {
300 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
301 print "<font class='base'>$errormessage&nbsp;</font>\n";
302 &Header::closebox();
5b2a12ff
MT
303}
304
db9ee62e
LAH
305# Show log output while Pakfire is running
306if(&_is_pakfire_busy()) {
307 &Header::openbox("100%", "center", "Pakfire");
308
309 print <<END
310<section id="pflog-header">
311 <div><img src="/images/indicator.gif" alt="$Lang::tr{'active'}" title="$Lang::tr{'pagerefresh'}"></div>
312 <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>
316 </div>
317 <div><a href="$ENV{'SCRIPT_NAME'}"><img src="/images/view-refresh.png" alt="$Lang::tr{'refresh'}" title="$Lang::tr{'refresh'}"></a></div>
318</section>
319
320<!-- Pakfire log messages -->
321<pre id="pflog-messages"></pre>
322<script>
4d70f591 323 // Start automatic log refresh
db9ee62e
LAH
324 pakfire.running = true;
325</script>
326
5b2a12ff 327END
db9ee62e
LAH
328;
329
5b2a12ff
MT
330 &Header::closebox();
331 &Header::closebigbox();
332 &Header::closepage();
333 exit;
334}
3ea75603 335
91a08eac 336my $core_release = `cat /opt/pakfire/db/core/mine 2>/dev/null`;
377560fb
MT
337chomp($core_release);
338my $core_update_age = &General::age("/opt/pakfire/db/core/mine");
339my $corelist_update_age = &General::age("/opt/pakfire/db/lists/core-list.db");
340my $server_update_age = &General::age("/opt/pakfire/db/lists/server-list.db");
341my $packages_update_age = &General::age("/opt/pakfire/db/lists/packages_list.db");
342
1bd42c89
MT
343&Header::openbox("100%", "center", "Pakfire");
344
345print <<END;
219dacef 346 <table id="pfmain">
337305ef 347END
324bb888 348if ( -e "/var/run/need_reboot") {
219dacef 349 print "\t\t<tr><td colspan='2'><a href='/cgi-bin/shutdown.cgi'>$Lang::tr{'needreboot'}!</a></td></tr>\n";
337305ef
JPT
350}
351print <<END;
219dacef
LAH
352 <tr><td class="heading">$Lang::tr{'pakfire system state'}:</td>
353 <td class="heading">$Lang::tr{'available updates'}:</td></tr>
354
355 <tr><td><strong>$Lang::tr{'pakfire core update level'}: $core_release</strong>
356 <hr>
357 <div class="pflist">
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>
1e908471 361 $Lang::tr{'pakfire last package update'} $packages_update_age $Lang::tr{'pakfire ago'}
219dacef
LAH
362 </div>
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'}' />
366 </form>
367 </td>
368 <td>
6666b93d 369 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
219dacef 370 <select name="UPDPAKS" class="pflist" size="5" disabled>
377560fb 371END
219dacef
LAH
372
373 &Pakfire::dblist("upgrade", "forweb");
377560fb
MT
374 print <<END;
375 </select>
377560fb 376 <input type='hidden' name='ACTION' value='upgrade' />
f8aa0679 377 <input type='image' alt='$Lang::tr{'upgrade'}' title='$Lang::tr{'upgrade'}' src='/images/document-save.png' />
377560fb 378 </form>
219dacef
LAH
379 </td>
380 </tr>
381 <tr><td class="heading">$Lang::tr{'pakfire available addons'}</td>
382 <td class="heading">$Lang::tr{'pakfire installed addons'}</td></tr>
fee5c6b7 383
b81c77b9 384 <tr><td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire install description'}</p>
219dacef
LAH
385 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
386 <select name="INSPAKS" class="pflist" size="10" multiple>
1bd42c89 387END
fee5c6b7 388
219dacef
LAH
389 &Pakfire::dblist("notinstalled", "forweb");
390 print <<END;
391 </select>
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' />
394 </form>
395 </td>
b81c77b9 396 <td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire uninstall description'}</p>
219dacef
LAH
397 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
398 <select name="DELPAKS" class="pflist" size="10" multiple>
957363eb 399END
5b2a12ff 400
219dacef
LAH
401 &Pakfire::dblist("installed", "forweb");
402 print <<END;
403 </select>
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' />
406 </form>
407 </td>
408 </tr>
377560fb 409 </table>
4b122800
MT
410END
411
f61be862
MT
412&Header::closebox();
413&Header::openbox("100%", "center", "$Lang::tr{'settings'}");
414
415print <<END;
416 <form method='POST' action='$ENV{'SCRIPT_NAME'}'>
417 <table width='95%'>
418 <tr>
419 <td align='left' width='45%'>$Lang::tr{'pakfire tree'}</td>
420 <td width="55%" align="left">
421 <select name="TREE">
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>
425 </select>
426 </td>
427 </tr>
428 <tr>
429 <td colspan="2">&nbsp;</td>
430 </tr>
431 <tr>
432 <td colspan="2" align="center">
433 <input type="submit" name="ACTION" value="$Lang::tr{'save'}" />
434 </td>
435 </tr>
436 </table>
437 </form>
438END
439
3ea75603 440&Header::closebox();
3ea75603 441&Header::closebigbox();
3ea75603 442&Header::closepage();
d255e2d1
LAH
443
444###--- Internal functions ---###
445
446# Check if pakfire is already running (extend test here if necessary)
447sub _is_pakfire_busy {
4d70f591
LAH
448 # Return immediately if lockfile is present
449 if(-e "$Pakfire::lockfile") {
450 return 1;
451 }
452
453 # Check if a PID of a running pakfire instance is found
d255e2d1
LAH
454 # (The system backpipe command is safe, because no user input is computed.)
455 my $pakfire_pid = `pidof -s /usr/local/bin/pakfire`;
456 chomp($pakfire_pid);
457
4d70f591
LAH
458 if($pakfire_pid) {
459 return 1;
460 }
461
462 # Pakfire isn't running
463 return 0;
d255e2d1 464}
db9ee62e
LAH
465
466# Send HTTP headers
467sub _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
471}