]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/pakfire.cgi
8516b07b1d6054bb1dc6654ddfc14e81aa82d9b3
[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-2011 Michael Tremer & Christian Schmidt #
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 /* Pakfire log viewer */
104 section#pflog-header {
105 width: 100%;
106 display: flex;
107 text-align: left;
108 align-items: center;
109 column-gap: 20px;
110 }
111 #pflog-header > div:last-child {
112 margin-left: auto;
113 margin-right: 20px;
114 }
115 #pflog-header span {
116 line-height: 1.3em;
117 }
118 #pflog-header span:empty::before {
119 content: "\\200b"; /* zero width space */
120 }
121
122 pre#pflog-messages {
123 margin-top: 0.7em;
124 padding-top: 0.7em;
125 border-top: 0.5px solid $Header::bordercolour;
126
127 text-align: left;
128 min-height: 15em;
129 overflow-x: auto;
130 }
131 </style>
132
133 <script src="/include/pakfire.js"></script>
134 <script>
135 // Translations
136 pakfire.i18n.load({
137 'working': '$Lang::tr{'pakfire working'}',
138 'finished': '$Lang::tr{'pakfire finished'}',
139 'finished error': '$Lang::tr{'pakfire finished error'}',
140 'since': '$Lang::tr{'since'}',
141
142 'link_return': '<a href="$ENV{'SCRIPT_NAME'}">$Lang::tr{'pakfire return'}</a>',
143 'link_reboot': '<a href="/cgi-bin/shutdown.cgi">$Lang::tr{'needreboot'}</a>'
144 });
145
146 // AJAX auto refresh interval (in ms, default: 1000)
147 //pakfire.refreshInterval = 1000;
148
149 // Enable returning to main screen (delay in ms)
150 pakfire.setupPageReload(true, 3000);
151 </script>
152 END
153 ;
154 ###--- END HTML HEAD ---###
155
156 &Header::openpage($Lang::tr{'pakfire configuration'}, 1, $extraHead);
157 &Header::openbigbox('100%', 'left', '', $errormessage);
158
159 # Process Pakfire commands
160 if (($cgiparams{'ACTION'} eq 'install') && (! &_is_pakfire_busy())) {
161 my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
162 if ("$cgiparams{'FORCE'}" eq "on") {
163 &General::system_background("/usr/local/bin/pakfire", "install", "--non-interactive", "--no-colors", @pkgs);
164 } else {
165 &Header::openbox("100%", "center", $Lang::tr{'request'});
166 my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
167 print <<END;
168 <table><tr><td colspan='2'>$Lang::tr{'pakfire install package'} @pkgs $Lang::tr{'pakfire possible dependency'}
169 <pre>
170 END
171 foreach (@output) {
172 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
173 print "$_\n";
174 }
175 print <<END;
176 </pre>
177 <tr><td colspan='2'>$Lang::tr{'pakfire accept all'}
178 <tr><td colspan='2'>&nbsp;
179 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
180 <input type='hidden' name='INSPAKS' value='$cgiparams{'INSPAKS'}' />
181 <input type='hidden' name='FORCE' value='on' />
182 <input type='hidden' name='ACTION' value='install' />
183 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/go-next.png' />
184 </form>
185 <td align='left'>
186 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
187 <input type='hidden' name='ACTION' value='' />
188 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
189 </form>
190 </table>
191 END
192 &Header::closebox();
193 &Header::closebigbox();
194 &Header::closepage();
195 exit;
196 }
197 } elsif (($cgiparams{'ACTION'} eq 'remove') && (! &_is_pakfire_busy())) {
198 my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
199 if ("$cgiparams{'FORCE'}" eq "on") {
200 &General::system_background("/usr/local/bin/pakfire", "remove", "--non-interactive", "--no-colors", @pkgs);
201 } else {
202 &Header::openbox("100%", "center", $Lang::tr{'request'});
203 my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
204 print <<END;
205 <table><tr><td colspan='2'>$Lang::tr{'pakfire uninstall package'} @pkgs $Lang::tr{'pakfire possible dependency'}
206 <pre>
207 END
208 foreach (@output) {
209 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
210 print "$_\n";
211 }
212 print <<END;
213 </pre>
214 <tr><td colspan='2'>$Lang::tr{'pakfire uninstall all'}
215 <tr><td colspan='2'>&nbsp;
216 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
217 <input type='hidden' name='DELPAKS' value='$cgiparams{'DELPAKS'}' />
218 <input type='hidden' name='FORCE' value='on' />
219 <input type='hidden' name='ACTION' value='remove' />
220 <input type='image' alt='$Lang::tr{'uninstall'}' title='$Lang::tr{'uninstall'}' src='/images/go-next.png' />
221 </form>
222 <td align='left'>
223 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
224 <input type='hidden' name='ACTION' value='' />
225 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
226 </form>
227 </table>
228 END
229 &Header::closebox();
230 &Header::closebigbox();
231 &Header::closepage();
232 exit;
233 }
234
235 } elsif (($cgiparams{'ACTION'} eq 'update') && (! &_is_pakfire_busy())) {
236 &General::system_background("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
237 } elsif (($cgiparams{'ACTION'} eq 'upgrade') && (! &_is_pakfire_busy())) {
238 &General::system_background("/usr/local/bin/pakfire", "upgrade", "-y", "--no-colors");
239 } elsif ($cgiparams{'ACTION'} eq "$Lang::tr{'save'}") {
240 $pakfiresettings{"TREE"} = $cgiparams{"TREE"};
241
242 # Check for valid input
243 if ($pakfiresettings{"TREE"} !~ m/^(stable|testing|unstable)$/) {
244 $errormessage .= $Lang::tr{'pakfire invalid tree'};
245 }
246
247 unless ($errormessage) {
248 &General::writehash("${General::swroot}/pakfire/settings", \%pakfiresettings);
249
250 # Update lists
251 &General::system_background("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
252 }
253 }
254
255 &General::readhash("${General::swroot}/pakfire/settings", \%pakfiresettings);
256
257 my %selected=();
258 my %checked=();
259
260 $selected{"TREE"} = ();
261 $selected{"TREE"}{"stable"} = "";
262 $selected{"TREE"}{"testing"} = "";
263 $selected{"TREE"}{"unstable"} = "";
264 $selected{"TREE"}{$pakfiresettings{"TREE"}} = "selected";
265
266 # DPC move error message to top so it is seen!
267 if ($errormessage) {
268 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
269 print "<font class='base'>$errormessage&nbsp;</font>\n";
270 &Header::closebox();
271 }
272
273 # Show log output while Pakfire is running
274 if(&_is_pakfire_busy()) {
275 &Header::openbox("100%", "center", "Pakfire");
276
277 print <<END
278 <section id="pflog-header">
279 <div><img src="/images/indicator.gif" alt="$Lang::tr{'active'}" title="$Lang::tr{'pagerefresh'}"></div>
280 <div>
281 <span id="pflog-status">$Lang::tr{'pakfire working'}</span><br>
282 <span id="pflog-time"></span><br>
283 <span id="pflog-action"></span>
284 </div>
285 <div><a href="$ENV{'SCRIPT_NAME'}"><img src="/images/view-refresh.png" alt="$Lang::tr{'refresh'}" title="$Lang::tr{'refresh'}"></a></div>
286 </section>
287
288 <!-- Pakfire log messages -->
289 <pre id="pflog-messages"></pre>
290 <script>
291 // Start automatic log refresh
292 pakfire.running = true;
293 </script>
294
295 END
296 ;
297
298 &Header::closebox();
299 &Header::closebigbox();
300 &Header::closepage();
301 exit;
302 }
303
304 my $core_release = `cat /opt/pakfire/db/core/mine 2>/dev/null`;
305 chomp($core_release);
306 my $core_update_age = &General::age("/opt/pakfire/db/core/mine");
307 my $corelist_update_age = &General::age("/opt/pakfire/db/lists/core-list.db");
308 my $server_update_age = &General::age("/opt/pakfire/db/lists/server-list.db");
309 my $packages_update_age = &General::age("/opt/pakfire/db/lists/packages_list.db");
310
311 &Header::openbox("100%", "center", "Pakfire");
312
313 print <<END;
314 <table width='95%' cellpadding='5'>
315 END
316 if ( -e "/var/run/need_reboot") {
317 print "<tr><td align='center' colspan='2'><font color='red'>$Lang::tr{'needreboot'}!</font></td></tr>";
318 print "<tr><td colspan='2'>&nbsp;</font></td></tr>"
319 }
320 print <<END;
321 <tr><td width="50%" bgcolor='$color{'color20'}' align="center"><b>$Lang::tr{'pakfire system state'}:</b>
322
323 <td width="50%" bgcolor='$color{'color20'}' align="center"><b>$Lang::tr{'available updates'}:</b></tr>
324
325 <tr><td align="center">$Lang::tr{'pakfire core update level'}: $core_release<hr />
326 $Lang::tr{'pakfire last update'} $core_update_age $Lang::tr{'pakfire ago'}<br />
327 $Lang::tr{'pakfire last serverlist update'} $server_update_age $Lang::tr{'pakfire ago'}<br />
328 $Lang::tr{'pakfire last core list update'} $corelist_update_age $Lang::tr{'pakfire ago'}<br />
329 $Lang::tr{'pakfire last package update'} $packages_update_age $Lang::tr{'pakfire ago'}
330 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
331 <input type='hidden' name='ACTION' value='update' /><br />
332 <input type='submit' value='$Lang::tr{'calamaris refresh list'}' /><br />
333 </form>
334 <br />
335 <td align="center">
336 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
337 <select name="UPDPAKS" size="5" disabled>
338 END
339 &Pakfire::dblist("upgrade", "forweb");
340 print <<END;
341 </select>
342 <br />
343 <input type='hidden' name='ACTION' value='upgrade' />
344 <input type='image' alt='$Lang::tr{'upgrade'}' title='$Lang::tr{'upgrade'}' src='/images/document-save.png' />
345 </form>
346
347 <tr><td colspan="2"><!-- Just an empty line -->&nbsp;
348 <tr><td bgcolor='$color{'color20'}' align="center"><b>$Lang::tr{'pakfire available addons'}</b>
349 <td bgcolor='$color{'color20'}' align="center"><b>$Lang::tr{'pakfire installed addons'}</b>
350 <tr><td style="padding:5px 10px 20px 20px" align="center">
351 <p>$Lang::tr{'pakfire install description'}</p>
352 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
353 <select name="INSPAKS" size="10" multiple>
354 END
355 &Pakfire::dblist("notinstalled", "forweb");
356
357 print <<END;
358 </select>
359 <br />
360 <input type='hidden' name='ACTION' value='install' />
361 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/list-add.png' />
362 </form>
363
364 <td style="padding:5px 10px 20px 20px" align="center">
365 <p>$Lang::tr{'pakfire uninstall description'}</p>
366 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
367 <select name="DELPAKS" size="10" multiple>
368 END
369
370 &Pakfire::dblist("installed", "forweb");
371
372 print <<END;
373 </select>
374 <br />
375 <input type='hidden' name='ACTION' value='remove' />
376 <input type='image' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' src='/images/list-remove.png' />
377 </form>
378 </table>
379 END
380
381 &Header::closebox();
382 &Header::openbox("100%", "center", "$Lang::tr{'settings'}");
383
384 print <<END;
385 <form method='POST' action='$ENV{'SCRIPT_NAME'}'>
386 <table width='95%'>
387 <tr>
388 <td align='left' width='45%'>$Lang::tr{'pakfire tree'}</td>
389 <td width="55%" align="left">
390 <select name="TREE">
391 <option value="stable" $selected{"TREE"}{"stable"}>$Lang::tr{'pakfire tree stable'}</option>
392 <option value="testing" $selected{"TREE"}{"testing"}>$Lang::tr{'pakfire tree testing'}</option>
393 <option value="unstable" $selected{"TREE"}{"unstable"}>$Lang::tr{'pakfire tree unstable'}</option>
394 </select>
395 </td>
396 </tr>
397 <tr>
398 <td colspan="2">&nbsp;</td>
399 </tr>
400 <tr>
401 <td colspan="2" align="center">
402 <input type="submit" name="ACTION" value="$Lang::tr{'save'}" />
403 </td>
404 </tr>
405 </table>
406 </form>
407 END
408
409 &Header::closebox();
410 &Header::closebigbox();
411 &Header::closepage();
412
413 ###--- Internal functions ---###
414
415 # Check if pakfire is already running (extend test here if necessary)
416 sub _is_pakfire_busy {
417 # Return immediately if lockfile is present
418 if(-e "$Pakfire::lockfile") {
419 return 1;
420 }
421
422 # Check if a PID of a running pakfire instance is found
423 # (The system backpipe command is safe, because no user input is computed.)
424 my $pakfire_pid = `pidof -s /usr/local/bin/pakfire`;
425 chomp($pakfire_pid);
426
427 if($pakfire_pid) {
428 return 1;
429 }
430
431 # Pakfire isn't running
432 return 0;
433 }
434
435 # Send HTTP headers
436 sub _start_json_output {
437 print "Cache-Control: no-cache, no-store\n";
438 print "Content-Type: application/json\n";
439 print "\n"; # End of HTTP headers
440 }