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