From 3d8063221d382b2ae83d58aaa729465e0c6faad0 Mon Sep 17 00:00:00 2001 From: nachoparker Date: Thu, 17 Aug 2017 22:02:54 +0200 Subject: ncp-web: poweroff button --- ncp-web/index.php | 7 +++- ncp-web/loading-small.gif | Bin 1771 -> 1772 bytes ncp-web/ncp-launcher.php | 15 ++++++-- ncp-web/ncp.css | 33 ++++++++++++++++++ ncp-web/ncp.js | 12 +++++++ ncp-web/poweroff.svg | 85 ++++++++++++++++++++++++++++++++++++++++++++++ nextcloudpi.sh | 2 +- update.sh | 3 ++ 8 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 ncp-web/poweroff.svg diff --git a/ncp-web/index.php b/ncp-web/index.php index 85bea26d..65ceff78 100644 --- a/ncp-web/index.php +++ b/ncp-web/index.php @@ -64,9 +64,14 @@

NextCloudPi

+
- +
+
+
+
+
diff --git a/ncp-web/loading-small.gif b/ncp-web/loading-small.gif index 83587a63..2d04fd0d 100644 Binary files a/ncp-web/loading-small.gif and b/ncp-web/loading-small.gif differ diff --git a/ncp-web/ncp-launcher.php b/ncp-web/ncp-launcher.php index f410640b..032b01ef 100644 --- a/ncp-web/ncp-launcher.php +++ b/ncp-web/ncp-launcher.php @@ -12,10 +12,10 @@ include ('csrf.php'); session_start(); -if ( !$_POST['ref'] ) exit( '{ "output": "Invalid request" }' ); - if ( $_POST['action'] == "cfgreq" ) { + if ( !$_POST['ref'] ) exit( '{ "output": "Invalid request" }' ); + //CSFR check $token = isset($_POST['csrf_token']) ? $_POST['csrf_token'] : ''; if ( empty($token) || !validateCSRFToken($token) ) @@ -64,6 +64,8 @@ if ( $_POST['action'] == "cfgreq" ) else if ( $_POST['action'] == "launch" && $_POST['config'] ) { + if ( !$_POST['ref'] ) exit( '{ "output": "Invalid request" }' ); + // CSRF check $token = isset($_POST['csrf_token']) ? $_POST['csrf_token'] : ''; if ( empty($token) || !validateCSRFToken($token) ) @@ -100,6 +102,15 @@ else if ( $_POST['action'] == "launch" && $_POST['config'] ) echo json_encode( shell_exec( 'bash -c "sudo /home/www/ncp-launcher.sh ' . $file . '"' ) ) . ' }'; } +else if ( $_POST['action'] == "poweroff" ) +{ + // CSRF check + $token = isset($_POST['csrf_token']) ? $_POST['csrf_token'] : ''; + if ( empty($token) || !validateCSRFToken($token) ) + exit( '{ "output": "Unauthorized request. Try reloading the page" }' ); + shell_exec( 'bash -c "( sleep 2 && sudo halt ) 2>/dev/null >/dev/null &"' ); +} + // License // // This script is free software; you can redistribute it and/or modify it diff --git a/ncp-web/ncp.css b/ncp-web/ncp.css index 253a0f71..012721dc 100644 --- a/ncp-web/ncp.css +++ b/ncp-web/ncp.css @@ -1084,3 +1084,36 @@ select { .icon-close { background-image:url('close.svg') } + +#power { + display: inline-block; + color: rgba(255, 255, 255, 0.7); + cursor: pointer; + flex: 0 0 auto; +} + +#expand { + position: relative; + display: flex; + align-items: center; + padding: 7px 20px 6px 10px; + cursor: pointer; +} + +#expand * { + cursor: pointer; +} + +#expand:hover img,#expand:focus img,#expand:active img,#expand:hover #expandDisplayName,#expand:focus #expandDisplayName,#expand:active #expandDisplayName { + -ms-filter:'progid:DXImageTransform.Microsoft.Alpha(Opacity=100)'; + opacity:1 +} + +.icon-power-white { + background-image: url('poweroff.svg'); +} + +#expand #expandDisplayName { + padding: 8px; + opacity: 0.6; +} diff --git a/ncp-web/ncp.js b/ncp-web/ncp.js index 5d92bed0..b200c8b0 100644 --- a/ncp-web/ncp.js +++ b/ncp-web/ncp.js @@ -81,6 +81,18 @@ $(function() confLock = false; }).error( errorMsg ); }); + + // Power-off button + $( '#poweroff' ).on('click', function(e) + { + // request + $.request('post', 'ncp-launcher.php', { action:'poweroff', + csrf_token: $( '#csrf-token' ).get( '.value' ) }).then( + function success( result ) + { + $('#config-box-title').fill( "Shutting down..." ); + }).error( errorMsg ); + } ); }); // License diff --git a/ncp-web/poweroff.svg b/ncp-web/poweroff.svg new file mode 100644 index 00000000..6b25d0b9 --- /dev/null +++ b/ncp-web/poweroff.svg @@ -0,0 +1,85 @@ + + + +image/svg+xml \ No newline at end of file diff --git a/nextcloudpi.sh b/nextcloudpi.sh index b509878d..ef066106 100644 --- a/nextcloudpi.sh +++ b/nextcloudpi.sh @@ -63,7 +63,7 @@ cd $DIR launch_script $1 EOF chmod 700 /home/www/ncp-launcher.sh - echo "www-data ALL = NOPASSWD: /home/www/ncp-launcher.sh" >> /etc/sudoers + echo "www-data ALL = NOPASSWD: /home/www/ncp-launcher.sh , /sbin/halt" >> /etc/sudoers # NEXTCLOUDPI MOTD ########################################## diff --git a/update.sh b/update.sh index c05e22e5..7f23a0f4 100755 --- a/update.sh +++ b/update.sh @@ -114,6 +114,9 @@ cat $LATEST > $NOTIFIED EOF chmod +x /usr/local/bin/ncp-notify-update +# fix permissions for ncp-web: shutdown button +sed -i 's|www-data.*|www-data ALL = NOPASSWD: /home/www/ncp-launcher.sh , /sbin/halt|' /etc/sudoers + # License # # This script is free software; you can redistribute it and/or modify it -- cgit v1.2.3