1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
/*jslint browser: true*/
/*global $, jQuery, alert*/
$(document).ready(function(){
var in_docker = $('#in-docker');
function addNotification( txt, tclass )
{
// limit to 9 notifications
if ( $('#notifications').children().length > 8 )
$('#notifications').children().last().remove();
$('#notifications').prepend( '<div class="notification ' + tclass + '">' + txt + '</div>' );
}
function logOutput( txt )
{
var textarea = $('#output-box');
textarea.val( textarea.val() + txt );
textarea[0].scrollTop = textarea[0].scrollHeight;
}
function showLog()
{
$('#output-wrapper').show();
var textarea = $('#output-box');
textarea[0].scrollTop = textarea[0].scrollHeight;
textarea.animate({ width: "40em" });
$('#overlay').show();
}
// launch an request for launch action to the backend
function launch_action( action /* string */, args /* object */, callback /* callback */ )
{
$('input').prop('disabled', true);
$('button').prop('disabled', true);
addNotification( action, 'gray-bg' ) ;
$.post('../ncp-launcher.php',
{ action:'launch',
ref: action,
config: JSON.stringify( args ),
csrf_token: document.getElementById( 'csrf-token' ).value
}
).fail( errorMsg ).done( callback );
}
function nextOnSuccess( data, nextfunc, failfunc )
{
$('input').prop('disabled', false);
$('button').prop('disabled', false);
var res = JSON.parse( data );
// save next single use token
if ( res.token )
$('#csrf-token').val( res.token );
// remove gray (loading) notification
$('#notifications').children().first().remove();
// continue if ok
var msg = res.ref || res.output || 'error';
if ( res.ret && res.ret == 0 )
{
addNotification( msg, 'green-bg' );
nextfunc();
}
else
{
addNotification( msg, 'orange-bg' );
showLog();
failfunc && failfunc();
}
}
function nextTabOnSuccess( data )
{
nextOnSuccess( data, function(){ $('#rootwizard').bootstrapWizard('next') } );
}
function show_with_animation( elemid )
{
$('#' + elemid).show(500);
$('html, body').animate({
scrollTop: $('#' + elemid).offset().top
}, 2000);
}
// Show error on failed AJAX call
function errorMsg( data )
{
alert('There was an error with the request');
}
function post_ddns_hook( data )
{
nextOnSuccess( data, function(){
launch_action( 'nc-autoupdate-ncp', { "ACTIVE":"yes" },
function( data ){
nextOnSuccess( data, function(){
launch_action( 'dnsmasq', { "ACTIVE":"yes", "DOMAIN":$("#ddns-domain").val() },
// keep this last, because it restarts the httpd server
function( data ){
nextOnSuccess( data, function(){
launch_action( 'letsencrypt', { "DOMAIN":$("#ddns-domain").val() },
nextTabOnSuccess
) } ) }
) } ) }
) } )
}
// Event source to receive process output in real time
if (!!window.EventSource)
var source = new EventSource('../ncp-output.php');
else
$('#config-box-title').val( "Browser not supported" );
source.addEventListener('message', function(e)
{
if ( e.origin != 'https://' + window.location.hostname + ':4443')
{
$('#output-box').val( "Invalid origin" );
return;
}
logOutput( e.data + '\n' );
}, false);
// start wizard clicking logo
$('#ncp-welcome-logo ').on('click', function(){ $('#rootwizard').bootstrapWizard('next'); } );
// This must be first or it breaks
$('#rootwizard').bootstrapWizard({onTabShow: function(tab, navigation, index){
var $total = navigation.find('li').length - 1;
var $current = index;
var $percent = ($current/$total) * 100;
$('#rootwizard').find('.progress-bar').css({width:$percent+'%'});
}});
// This is required or the tabs aren't styled
$('#rootwizard').bootstrapWizard({'tabClass': 'nav nav-pills'});
// Enable Automount step
$('#enable-automount').on('click', function() {
show_with_animation( 'plug-usb-pane' );
});
// Disable Automount step
$('#disable-automount').on('click', function() {
$("#plug-usb-pane").hide();
$('#rootwizard').bootstrapWizard('next');
});
// Enable format-usb step
$('#plugUSB').on('click', function() {
launch_action( 'nc-automount',
{"ACTIVE":"yes"},
function ( data ){
nextOnSuccess( data, function(){ show_with_animation( 'format-usb' ); } );
}
);
});
// Enable nextcloud-data step
$('#format-USB').on('click', function(){
launch_action( 'nc-format-USB',
{"LABEL":"myCloudDrive"},
function ( data ){
nextOnSuccess( data, function(){ show_with_animation( 'nc-datadir-pane' ); } );
}
);
});
// Enable nextcloud data tab on skip format.
$('#skip-format-USB').on('click', function(){
show_with_animation( 'nc-datadir-pane' );
});
// Launch nc-datadir
$('#nc-datadir').on('click', function() {
launch_action( 'nc-datadir', {"DATADIR":"/media/USBdrive/ncdata"}, nextTabOnSuccess );
});
// Enable external access step
$('#enable-external').on('click', function(){
if ( !in_docker )
launch_action( 'fail2ban',
{ "ACTIVE":"yes" },
function ( data ){
nextOnSuccess( data, function(){ show_with_animation( 'forward-ports-pane' ) } );
}
);
else
show_with_animation( 'forward-ports-pane' );
});
// Skip external access step
$('#skip-external').on('click', function(){
$('#forward-ports-manual-pane').hide();
$('#forward-ports-pane' ).hide();
$('#ddns-choose' ).hide();
$("#ddns-account" ).hide();
$("#noip" ).hide();
$("#freedns" ).hide();
$('#rootwizard').bootstrapWizard('next');
});
// Run port forwarding with UPnP step
$('#port-forward-run').on('click', function(){
// Run Port Forwarding and Test Port
launch_action( 'nc-forward-ports',
{"HTTPSPORT":"443","HTTPPORT":"80"},
function ( data ){
nextOnSuccess( data, function(){ show_with_animation( 'ddns-choose' ) } );
}
);
});
// Manual port forwarding
$('#port-forward-manual').on('click', function() {
show_with_animation( 'forward-ports-manual-pane' );
});
// Manual port forwarding done
$('#port-forward-done').on('click', function() {
show_with_animation( 'ddns-choose' );
});
// Skip DDNS setup
$('#ddns-skip').on('click', function(){
$("#domain" ).hide();
$("#noip" ).hide();
$("#freedns").hide();
$('#rootwizard').bootstrapWizard('next');
});
// Show FreeDNS step
$('#ddns-freedns').on('click', function(){
$("#noip" ).hide();
$("#freedns").show();
show_with_animation( 'ddns-account' );
});
// Enable FreeDNS step
$('#ddns-enable-freedns').on('click', function(){
launch_action( 'freeDNS',
{
"ACTIVE":"yes",
"DOMAIN": $("#ddns-domain" ).val(),
"UPDATEHASH": $("#freedns-hash").val(),
"UPDATEINTERVAL": "30"
},
post_ddns_hook
);
// prevent scroll up
return false;
});
// Show noip step
$('#ddns-noip').on('click', function(){
$("#noip" ).show();
$("#freedns").hide();
show_with_animation( 'ddns-account' );
});
// Enable noip step
$('#ddns-enable-noip').on('click', function(){
launch_action( 'no-ip',
{
"ACTIVE":"yes",
"DOMAIN": $("#ddns-domain" ).val(),
"USER": $("#noip-user" ).val(),
"PASS": $("#noip-password").val(),
},
post_ddns_hook
);
// prevent scroll up
return false;
}
);
// show log output
$('#output-btn').on('click', function(){
showLog();
} );
// close log output
$('.output-close').on('click', function(e){
if( e.target.id == 'output-wrapper' )
{
$('#output-box').animate(
{ width: "0em" },
{ complete: function() {
$('#output-wrapper').hide();
$('#overlay').hide();
}
} );
}
} );
// make sure log box starts empty
$('#output-box').val('');
// click to nextcloud button
$('#gotonextcloud').attr('href', window.location.protocol + '//' + window.location.hostname );
});
|