]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/pakfire.cgi
Merge branch 'next' into temp-c165-development
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / pakfire.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
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 ###############################################################################
21
22 use strict;
23 use List::Util qw(any);
24
25 # enable only the following on debugging purpose
26 #use warnings;
27 #use CGI::Carp 'fatalsToBrowser';
28
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";
33
34 my %cgiparams=();
35 my $errormessage = '';
36 my %color = ();
37 my %pakfiresettings = ();
38 my %mainsettings = ();
39
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);
43
44 # Get CGI request data
45 $cgiparams{'ACTION'} = '';
46 $cgiparams{'VALID'} = '';
47
48 $cgiparams{'INSPAKS'} = '';
49 $cgiparams{'DELPAKS'} = '';
50
51 &Header::getcgihash(\%cgiparams);
52
53 ### Process AJAX/JSON request ###
54 if($cgiparams{'ACTION'} eq 'json-getstatus') {
55 # Send HTTP headers
56 _start_json_output();
57
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
66 my %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"
71 );
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 ---###
101 my $extraHead = <<END
102 <style>
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
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;
152
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'}',
164 'finished': '$Lang::tr{'pakfire finished'}',
165 'finished error': '$Lang::tr{'pakfire finished error'}',
166 'since': '$Lang::tr{'since'}',
167
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>'
170 });
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);
177 </script>
178 END
179 ;
180 ###--- END HTML HEAD ---###
181
182 &Header::openpage($Lang::tr{'pakfire configuration'}, 1, $extraHead);
183 &Header::openbigbox('100%', 'left', '', $errormessage);
184
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);
190 } else {
191 &Header::openbox("100%", "center", $Lang::tr{'request'});
192 my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
193 print <<END;
194 <table><tr><td colspan='2'>$Lang::tr{'pakfire install package'} @pkgs $Lang::tr{'pakfire possible dependency'}
195 <pre>
196 END
197 foreach (@output) {
198 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
199 print "$_\n";
200 }
201 print <<END;
202 </pre></td></tr>
203 <tr><td colspan='2'>$Lang::tr{'pakfire accept all'}</td></tr>
204 <tr><td colspan='2'>&nbsp;</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' />
210 </form>
211 </td>
212 <td align='left'>
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' />
216 </form>
217 </td>
218 </tr>
219 </table>
220 END
221 &Header::closebox();
222 &Header::closebigbox();
223 &Header::closepage();
224 exit;
225 }
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);
230 } else {
231 &Header::openbox("100%", "center", $Lang::tr{'request'});
232 my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
233 print <<END;
234 <table><tr><td colspan='2'>$Lang::tr{'pakfire uninstall package'} @pkgs $Lang::tr{'pakfire possible dependency'}
235 <pre>
236 END
237 foreach (@output) {
238 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
239 print "$_\n";
240 }
241 print <<END;
242 </pre></td></tr>
243 <tr><td colspan='2'>$Lang::tr{'pakfire uninstall all'}</td></tr>
244 <tr><td colspan='2'>&nbsp;</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' />
250 </form>
251 </td>
252 <td align='left'>
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' />
256 </form>
257 </td>
258 </tr>
259 </table>
260 END
261 &Header::closebox();
262 &Header::closebigbox();
263 &Header::closepage();
264 exit;
265 }
266
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"};
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
283 &General::system_background("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
284 }
285 }
286
287 &General::readhash("${General::swroot}/pakfire/settings", \%pakfiresettings);
288
289 my %selected=();
290 my %checked=();
291
292 $selected{"TREE"} = ();
293 $selected{"TREE"}{"stable"} = "";
294 $selected{"TREE"}{"testing"} = "";
295 $selected{"TREE"}{"unstable"} = "";
296 $selected{"TREE"}{$pakfiresettings{"TREE"}} = "selected";
297
298 # DPC move error message to top so it is seen!
299 if ($errormessage) {
300 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
301 print "<font class='base'>$errormessage&nbsp;</font>\n";
302 &Header::closebox();
303 }
304
305 # Show log output while Pakfire is running
306 if(&_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>
323 // Start automatic log refresh
324 pakfire.running = true;
325 </script>
326
327 END
328 ;
329
330 &Header::closebox();
331 &Header::closebigbox();
332 &Header::closepage();
333 exit;
334 }
335
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");
342
343 &Header::openbox("100%", "center", "Pakfire");
344
345 print <<END;
346 <table id="pfmain">
347 END
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";
350 }
351 print <<END;
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>
361 $Lang::tr{'pakfire last package update'} $packages_update_age $Lang::tr{'pakfire ago'}
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>
369 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
370 <select name="UPDPAKS" class="pflist" size="5" disabled>
371 END
372
373 &Pakfire::dblist("upgrade", "forweb");
374 print <<END;
375 </select>
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' />
378 </form>
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>
383
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>
387 END
388
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>
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>
399 END
400
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>
409 </table>
410 END
411
412 &Header::closebox();
413 &Header::openbox("100%", "center", "$Lang::tr{'settings'}");
414
415 print <<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>
438 END
439
440 &Header::closebox();
441 &Header::closebigbox();
442 &Header::closepage();
443
444 ###--- Internal functions ---###
445
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") {
450 return 1;
451 }
452
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`;
456 chomp($pakfire_pid);
457
458 if($pakfire_pid) {
459 return 1;
460 }
461
462 # Pakfire isn't running
463 return 0;
464 }
465
466 # Send HTTP headers
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
471 }