Skip to content

Commit b41fad0

Browse files
committed
ncp-web: add NCP logs section
Signed-off-by: nachoparker <nacho@ownyourbits.com>
1 parent 85cb1f0 commit b41fad0

File tree

9 files changed

+214
-12
lines changed

9 files changed

+214
-12
lines changed

build/docker/docker-compose-ncpdev.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ services:
88
- "443:443"
99
- "4443:4443"
1010
volumes:
11-
- ./ncp-web:/var/www/ncp-web
12-
- ./etc:/usr/local/etc
13-
- ./changelog.md:/changelog.md
11+
- ../../ncp-web:/var/www/ncp-web
12+
- ../../etc:/usr/local/etc
13+
- ../../changelog.md:/changelog.md
1414
- ncdata:/data
1515
container_name: nextcloudpi
1616

changelog.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11

2-
<<<<<<< HEAD
3-
[v1.40.5](https://github.com/nextcloud/nextcloudpi/commit/6c3cb37) (2021-10-11) letsencrypt: keep original cert name
2+
[v1.41.0](https://github.com/nextcloud/nextcloudpi/commit/33d8167) (2021-10-13) ncp-web: add NCP logs section
43

5-
[v1.40.4 ](https://github.com/nextcloud/nextcloudpi/commit/de104a5) (2021-10-06) Make templating safer and more verbose (#1343)
6-
=======
7-
[v1.40.5](https://github.com/nextcloud/nextcloudpi/commit/f7c393f) (2021-10-11) add architecture and kernel info to ncp-info (#1360)
4+
[v1.40.10](https://github.com/nextcloud/nextcloudpi/commit/8bb5795) (2021-10-12) nc-import-ncp: run activated apps upon import
5+
6+
[v1.40.9 ](https://github.com/nextcloud/nextcloudpi/commit/a4998cd) (2021-10-12) letsencrypt: favor most recent cert if there are many
7+
8+
[v1.40.8 ](https://github.com/nextcloud/nextcloudpi/commit/9bddfad) (2021-10-12) fix HPB with dynamic IP
9+
10+
[v1.40.7 ](https://github.com/nextcloud/nextcloudpi/commit/4e790e4) (2021-10-12) letsencrypt: fix template generation
811

9-
[v1.40.4](https://github.com/nextcloud/nextcloudpi/commit/de104a5) (2021-10-06) Make templating safer and more verbose (#1343)
10-
>>>>>>> b532589 (add architecture and kernel info to ncp-info (#1360))
12+
[v1.40.6 ](https://github.com/nextcloud/nextcloudpi/commit/b64859e) (2021-10-11) add architecture and kernel info to ncp-info (#1360)
13+
14+
[v1.40.5](https://github.com/nextcloud/nextcloudpi/commit/cc3f336) (2021-10-11) letsencrypt: keep original cert name
15+
16+
[v1.40.4 ](https://github.com/nextcloud/nextcloudpi/commit/de104a5) (2021-10-06) Make templating safer and more verbose (#1343)
1117

1218
[v1.40.3 ](https://github.com/nextcloud/nextcloudpi/commit/8a6c1c0) (2021-10-06) ncp-check-nc-version: dont notify the same version more than once
1319

ncp-web/css/ncp.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,9 @@ select {
11531153
.icon-backups {
11541154
background-image: url('../img/defaults-white.svg');
11551155
}
1156+
.icon-logs {
1157+
background-image: url('../img/logs.svg');
1158+
}
11561159
.icon-config {
11571160
background-image: url('../img/settings-white.svg');
11581161
}
@@ -1238,6 +1241,10 @@ a#versionlink:hover {
12381241
cursor:pointer;
12391242
}
12401243

1244+
#log-download-btn-wrapper {
1245+
text-align: center
1246+
}
1247+
12411248
.dialog {
12421249
display:block;
12431250
background: white;
@@ -1375,6 +1382,14 @@ a#versionlink:hover {
13751382
overflow: auto;
13761383
}
13771384

1385+
#logs-details-box {
1386+
background-color: #fbfbfb;
1387+
border: solid 1px lightgray;
1388+
color: #565656;
1389+
overflow: auto;
1390+
height: 80vh;
1391+
}
1392+
13781393
#language-selection {
13791394
border: none;
13801395
}

ncp-web/download_logs.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
///
3+
// NextCloudPi Web Panel backend
4+
//
5+
// Copyleft 2019 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
6+
// GPL licensed (see end of file) * Use at your own risk!
7+
//
8+
// More at https://nextcloudpi.com
9+
///
10+
11+
include ('csrf.php');
12+
session_start();
13+
14+
// CSRF check
15+
$token = isset($_REQUEST['token']) ? $_REQUEST['token'] : '';
16+
if ( empty($token) || !validateCSRFToken($token) )
17+
exit('Unauthorized download');
18+
19+
$file = '/var/log/ncp.log';
20+
21+
if (!file_exists($file)
22+
die('File not found');
23+
24+
if (!is_readable($file))
25+
die('NCP does not have read permissions on this file');
26+
27+
function filesize_compat($file)
28+
{
29+
if(PHP_INT_SIZE === 4) # workaround for 32-bit architectures
30+
return trim(shell_exec("stat -c%s " . escapeshellarg($file)));
31+
else
32+
return filesize($file);
33+
}
34+
35+
$size = filesize_compat($file);
36+
37+
$mime_type = 'text/plain';
38+
39+
ob_start();
40+
ob_clean();
41+
header('Content-Description: File Transfer');
42+
header('Content-Type: ' . $mime_type);
43+
header("Content-Transfer-Encoding: Binary");
44+
header("Content-disposition: attachment; filename=\"" . basename($file) . "\"");
45+
header('Content-Length: ' . $size);
46+
header('Expires: 0');
47+
header('Cache-Control: must-revalidate');
48+
header('Pragma: public');
49+
50+
$chunksize = 8 * (1024 * 1024);
51+
52+
if($size > $chunksize || PHP_INT_SIZE === 4) # always chunk for 32-bit architectures
53+
{
54+
$handle = fopen($file, 'rb') or die("Error opening file");
55+
56+
while (!feof($handle))
57+
{
58+
$buffer = fread($handle, $chunksize);
59+
echo $buffer;
60+
61+
ob_flush();
62+
flush();
63+
}
64+
65+
fclose($handle);
66+
}
67+
else
68+
readfile($file);
69+
70+
ob_flush();
71+
flush();
72+
73+
exit();
74+
75+
?>

ncp-web/img/logs.svg

Lines changed: 38 additions & 0 deletions
Loading

ncp-web/index.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@
190190
<div class="icon-config"></div>
191191
</div>
192192
</div>
193+
<div id="logs-btn" title="<?php echo $l->__("Logs"); ?>">
194+
<div class="expand">
195+
<div class="icon-logs"></div>
196+
</div>
197+
</div>
193198
<a href="wizard" title="<?php echo $l->__("NextCloudPi Wizard"); ?>">
194199
<div class="wizard-btn">
195200
<div class="expand">
@@ -266,8 +271,16 @@
266271
}
267272
echo '</table>';
268273
?>
274+
</div>
269275
</div>
270-
</div>
276+
<div id="logs-wrapper" class="content-box <?php if(!array_key_exists('app',$_GET) || (array_key_exists('app',$_GET) && $_GET['app'] != 'logs')) echo 'hidden';?>">
277+
<h2 class="text-title"><?php echo $l->__("NextCloudPi logs"); ?></h2>
278+
<div id="logs-content" class="table-wrapper">
279+
<div id="logs-details-box" class="outputbox"><?php echo str_replace(array("\r\n", "\n", "\r"), '<br/>', file_get_contents('/var/log/ncp.log')) ?></div>
280+
<div id="log-download-btn-wrapper"><input id="log-download-btn" type="button" value="Download"/></div>
281+
</div>
282+
</div>
283+
271284
</div>
272285

273286
<div id="poweroff-dialog" class='dialog primary hidden'>
@@ -283,6 +296,7 @@
283296
include('csrf.php');
284297
echo '<input type="hidden" id="csrf-token" name="csrf-token" value="' . getCSRFToken() . '"/>';
285298
echo '<input type="hidden" id="csrf-token-ui" name="csrf-token-ui" value="' . getCSRFToken() . '"/>';
299+
echo '<input type="hidden" id="csrf-token-dl" name="csrf-token-dl" value="' . getCSRFToken() . '"/>';
286300
echo '<input type="hidden" id="csrf-token-cfg" name="csrf-token-cfg" value="' . getCSRFToken() . '"/>';
287301
?>
288302
<script src="js/minified.js"></script>

ncp-web/js/ncp.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ window.onpopstate = function(event) {
2424
switch_to_section('nc-config');
2525
else if (selectedID == 'dashboard')
2626
switch_to_section('dashboard');
27+
else if (selectedID == 'logs')
28+
switch_to_section('logs');
2729
else
2830
click_app($('#' + selectedID));
2931
};
@@ -38,6 +40,7 @@ function switch_to_section(section)
3840
// TODO unify repeating code
3941
$( '#config-wrapper > div' ).hide();
4042
$( '#dashboard-wrapper' ).hide();
43+
$( '#logs-wrapper' ).hide();
4144
$( '#nc-config-wrapper' ).hide();
4245
$( '#backups-wrapper' ).hide();
4346
$( '#' + section + '-wrapper' ).show();
@@ -228,14 +231,31 @@ function dialog_action(button)
228231
clicked_dialog_action(clicked_dialog_button);
229232
}
230233

234+
function refresh_dl_token()
235+
{
236+
$.request('post', 'ncp-launcher.php', { action:'next-dl',
237+
csrf_token: $( '#csrf-token' ).get( '.value' ) }).then(
238+
function success( result )
239+
{
240+
var ret = $.parseJSON( result );
241+
if ( ret.token )
242+
$('#csrf-token').set( { value: ret.token } );
243+
if ( ret.token_dl )
244+
$('#csrf-token-dl').set( { value: ret.token_dl } );
245+
}
246+
).error( errorMsg )
247+
}
248+
231249
// backups
232250
function set_backup_handlers()
233251
{
234252
$( '.download-bkp' ).on('click', function(e)
235253
{
236254
var tr = this.up().up();
237255
var path = tr.get('.id');
238-
window.location.replace('download.php?bkp=' + encodeURIComponent(path) + '&token=' + encodeURIComponent(tr.next().get('.value')));
256+
var token_dl = $('#csrf-token-dl').get('.value');
257+
window.location.replace('download.php?bkp=' + encodeURIComponent(path) + '&token=' + encodeURIComponent(token_dl));
258+
refresh_dl_token();
239259
});
240260
$( '.delete-bkp' ).on('click', function(e)
241261
{
@@ -365,6 +385,12 @@ $(function()
365385
if (location.search)
366386
selectedID = location.search.split('=')[1];
367387

388+
// scroll down logs box by default
389+
var logs_box_l = $('#logs-details-box');
390+
var logs_box = logs_box_l[0];
391+
logs_box.scrollTop = logs_box.scrollHeight;
392+
//$('#logs-details-box').scrollTop = $('#logs-details-box').scrollHeight;
393+
368394
// Event source to receive process output in real time
369395
if (!!window.EventSource)
370396
var source = new EventSource('ncp-output.php');
@@ -687,6 +713,24 @@ $(function()
687713
history.pushState(null, selectedID, "?app=backups");
688714
} );
689715

716+
// logs button
717+
$( '#logs-btn' ).on('click', function(e)
718+
{
719+
if ( lock ) return;
720+
close_menu();
721+
switch_to_section( 'logs' );
722+
history.pushState(null, selectedID, "?app=logs");
723+
} );
724+
725+
// log download button
726+
$( '#log-download-btn' ).on('click', function(e)
727+
{
728+
var token_dl = $('#csrf-token-dl').get('.value');
729+
var token = $('#csrf-token').get('.value');
730+
window.location.replace('download_logs.php?token=' + encodeURIComponent(token_dl));
731+
refresh_dl_token();
732+
} );
733+
690734
// language selection
691735
var langold = $( '#language-selection' ).get( '.value' );
692736
$( '#language-selection' ).on( 'change', function(e)

ncp-web/ncp-launcher.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@
176176
echo ' "ret": "' . $ret . '" }';
177177
}
178178

179+
//
180+
// dl-token
181+
//
182+
else if ( $_POST['action'] == "next-dl" )
183+
{
184+
echo '{ "token": "' . getCSRFToken() . '",'; // Get new token
185+
echo '"token_dl": "' . getCSRFToken() . '",'; // Get new download token
186+
echo ' "ret": "' . $ret . '" }';
187+
}
188+
179189
//
180190
// path field
181191
//
File renamed without changes.

0 commit comments

Comments
 (0)