Skip to content

Commit

Permalink
ncp-web: faster first load by asynchronous call to is_active()
Browse files Browse the repository at this point in the history
  • Loading branch information
nachoparker committed Apr 6, 2018
1 parent 7eecd81 commit b9116e7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 52 deletions.
4 changes: 3 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

[v0.53.23](https://github.com/nextcloud/nextcloudpi/commit/b7ee6cd) (2018-04-05) ncp-web: force reload CSRF tokens every time
[v0.53.24](https://github.com/nextcloud/nextcloudpi/commit/181603a) (2018-04-05) ncp-web: faster first load by asynchronous call to is_active()

[v0.53.23](https://github.com/nextcloud/nextcloudpi/commit/6fa40c5) (2018-04-05) ncp-web: force reload CSRF tokens every time

[v0.53.22](https://github.com/nextcloud/nextcloudpi/commit/6f916a1) (2018-04-05) ncp-web: collapse sidebar menu when clicking in new sections

Expand Down
52 changes: 28 additions & 24 deletions ncp-web/activate/JS.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,33 @@ function errorMsg()
$('#error-box').fill( "Something went wrong. Try refreshing the page" );
}

function launch_nc_passwd()
{
// request
$.request('post', '../ncp-launcher.php', { action: 'launch',
ref : 'nc-passwd',
config: '{ "PASSWORD":"' + $('#ncp-pwd').get('.value') + '",'
+ '"CONFIRM" :"' + $('#ncp-pwd').get('.value') + '"}',
csrf_token: $( '#csrf-token' ).get( '.value' ) }).then(

function success( result )
{
var ret = $.parseJSON( result );
if ( ret.ret == '0' )
{
setTimeout( function(){
$('#loading-gif').hide();
$('#error-box').fill( "ACTIVATION SUCCESSFUL" );
var url = window.location.protocol + '//' + window.location.hostname + ':4443';
if ( !window.open( url, '_blank' ) ) // try to open in a new tab first
window.location.replace( url );
}, 2000 );
} else {
$('#error-box').fill( "nc-passwd error" );
}
} ).error( errorMsg );
}

$(function()
{
// print info page
Expand Down Expand Up @@ -64,30 +91,7 @@ $(function()
if ( ret.ret == '0' ) {
if ( ret.token )
$('#csrf-token').set( { value: ret.token } );

// request
$.request('post', '../ncp-launcher.php', { action: 'launch',
ref : 'nc-passwd',
config: '{ "PASSWORD":"' + $('#ncp-pwd').get('.value') + '",'
+ '"CONFIRM" :"' + $('#ncp-pwd').get('.value') + '"}',
csrf_token: $( '#csrf-token' ).get( '.value' ) }).then(

function success( result )
{
var ret = $.parseJSON( result );
if ( ret.ret == '0' )
{
setTimeout( function(){
$('#loading-gif').hide();
$('#error-box').fill( "ACTIVATION SUCCESSFUL" );
var url = window.location.protocol + '//' + window.location.hostname + ':4443';
if ( !window.open( url, '_blank' ) ) // try to open in a new tab first
window.location.replace( url );
}, 2000 );
} else {
$('#error-box').fill( "nc-passwd error" );
}
} ).error( errorMsg );
launch_nc_passwd();
} else {
$('#error-box').fill( "nc-admin error" );
}
Expand Down
6 changes: 3 additions & 3 deletions ncp-web/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
<div id='overlay' class="hidden"></div>
<div id="app-navigation">
<ul id="ncp-options">
<?php echo print_sidebar($l); ?>
<?php echo print_sidebar($l, false); ?>
</ul>
</div>

Expand Down Expand Up @@ -237,8 +237,8 @@

<?php
include('csrf.php');
echo '<input type="hidden" id="csrf-token" name="csrf-token" value="' . getCSRFToken() . '"/>';
echo '<input type="hidden" id="csrf-token-dash" name="csrf-token-dash" value="' . getCSRFToken() . '"/>';
echo '<input type="hidden" id="csrf-token" name="csrf-token" value="' . getCSRFToken() . '"/>';
echo '<input type="hidden" id="csrf-token-ui" name="csrf-token-ui" value="' . getCSRFToken() . '"/>';
?>
<script src="minified.js"></script>
<script src="ncp.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion ncp-web/ncp-launcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
require( "sidebar.php" );
// return JSON
echo '{ "token": "' . getCSRFToken() . '",'; // Get new token
echo ' "output": ' . json_encode( print_sidebar( $l ) ) . ' , ';
echo ' "output": ' . json_encode( print_sidebar( $l, true ) ) . ' , ';
echo ' "ret": "0" }';
}
}
Expand Down
44 changes: 26 additions & 18 deletions ncp-web/ncp.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,37 @@ function set_sidebar_click_handlers()
});
}

function print_dashboard()
{
$.request('post', 'ncp-launcher.php', { action: 'info',
csrf_token: $( '#csrf-token-ui' ).get( '.value' ) }).then(

function success( result )
{
var ret = $.parseJSON( result );
if ( ret.token )
$('#csrf-token-ui').set( { value: ret.token } );
$('#loading-info-gif').hide();
$('#dashboard-table').ht( ret.table );
$('#dashboard-suggestions').ht( ret.suggestions );
reload_sidebar();
} ).error( errorMsg );
}

function reload_sidebar()
{
// request
$.request('post', 'ncp-launcher.php', { action:'sidebar',
csrf_token: $( '#csrf-token' ).get( '.value' ) }).then(
csrf_token: $( '#csrf-token-ui' ).get( '.value' ) }).then(
function success( result )
{
var ret = $.parseJSON( result );
if ( ret.token )
$('#csrf-token').set( { value: ret.token } );
$('#ncp-options').ht( ret.output );
set_sidebar_click_handlers();
$('#csrf-token-ui').set( { value: ret.token } );
if ( ret.ret && ret.ret == '0' ) {
$('#ncp-options').ht( ret.output );
set_sidebar_click_handlers();
}
}).error( errorMsg );
}

Expand Down Expand Up @@ -336,20 +355,6 @@ $(function()
// click to nextcloud button
$('#nextcloud-btn').set( '@href', window.location.protocol + '//' + window.location.hostname );

// load dashboard info
$.request('post', 'ncp-launcher.php', { action: 'info',
csrf_token: $( '#csrf-token-dash' ).get( '.value' ) }).then(

function success( result )
{
var ret = $.parseJSON( result );
if ( ret.token )
$('#csrf-token-dash').set( { value: ret.token } );
$('#loading-info-gif').hide();
$('#dashboard-table').ht( ret.table );
$('#dashboard-suggestions').ht( ret.suggestions );
} ).error( errorMsg );

// dashboard button
$( '#dashboard-btn' ).on('click', function(e)
{
Expand All @@ -365,6 +370,9 @@ $(function()
close_menu();
switch_to_section( 'nc-config' );
} );

// load dashboard info
print_dashboard();
} );

// License
Expand Down
13 changes: 8 additions & 5 deletions ncp-web/sidebar.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

// fill options with contents from directory

function print_sidebar( $l /* translations l10n object */ )
function print_sidebar( $l /* translations l10n object */, $ticks /* wether to calculate ticks(slow) */ )
{
$modules_path = '/usr/local/etc/nextcloudpi-config.d/';
$files = array_diff(scandir($modules_path), array('.', '..', 'nc-wifi.sh', 'nc-info.sh', 'l10n'));
Expand All @@ -21,10 +21,13 @@ function print_sidebar( $l /* translations l10n object */ )
$txt = file_get_contents($modules_path . $file);

$active = "";
$etc = '/usr/local/etc';
exec("bash -c \"source $etc/library.sh && is_active_script $etc/nextcloudpi-config.d/$script\".sh", $output, $retval);
if ($retval == 0)
$active = "";
if ( $ticks ) {
$etc = '/usr/local/etc';
exec("bash -c \"source $etc/library.sh && is_active_script $etc/nextcloudpi-config.d/$script\".sh", $output, $retval);
if ($retval == 0)
$active = "";
} else if (preg_match('/^ACTIVE_=yes$/m', $txt, $matches))
$active = "";

$ret .= "<li id=\"$script\" class=\"nav-recent\">";
$ret .= "<a href=\"#\"> {$l->__($script, $script)}$active </a>";
Expand Down

0 comments on commit b9116e7

Please sign in to comment.