This repository has been archived by the owner on Sep 5, 2019. It is now read-only.
/
server_script.js
708 lines (612 loc) · 17.7 KB
/
server_script.js
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/*
*
* UJS Manager Unite Application
* Rafal Chlodnicki, 2009
*
*/
var
SHARED_DIR = opera.io.filesystem.mountSystemDirectory('shared'),
PUBLIC_DIR = opera.io.filesystem.mountSystemDirectory('application'),
SERVICE_DOAP = 'http://unite.opera.com/service/doap/401/',
/* DON'T FORGET TO UPDATE FOR EVERY RELEASE !!! */
SERVICE_VERSION = '2.0',
service_path = 'http://' + opera.io.webserver.hostName + opera.io.webserver.currentServicePath,
static_files = [],
data = { scripts: getAllUserScripts(null, true) };
sharePublicHtml();
opera.io.webserver.addEventListener( '_request', handleRequest, false );
function handleRequest( event )
{
var response = event.connection.response;
var request = event.connection.request;
if ( isPublicFile(request.uri) )
{
response.closeAndRedispatch();
return;
}
// this block is before isOwner check because we want user to be able to install script from
// normal pages. We could redirect to admin page but that would cause frightening unite redirect
// warning page. Instead, we redirect from script file to public service address and then to
// admin service address without causing warnings. Admin page is where all the magic happens
// and remote users can't connect to it anyway.
if ( !event.connection.isOwner )
{
if ( request.bodyItems['install_script'] )
{
if ( !request.bodyItems['unique_id']
|| request.bodyItems['unique_id'][0] != getPref('unique_id') )
{
response.write( "This won't work. Bad unique id." );
response.close();
return;
}
var tpldata = {
admin_url : 'http://admin.' + request.host + opera.io.webserver.currentServicePath,
install_url : request.bodyItems['install_script'][0],
script_body : request.bodyItems['script_body'][0],
ask_overwrite : false,
old_header : null,
new_header : null
};
// extract file name from path
var filename = tpldata.install_url.match(/.+\/([^/?]+)/);
if ( filename )
{
filename = filename[1];
// check if file already exists and ask for overwrite if yes
var existing_body = readFile(filename);
if ( existing_body !== null )
{
tpldata.ask_overwrite = true;
tpldata.old_header = getUserScriptHeader(existing_body)||{'<missing>':''};
tpldata.new_header = getUserScriptHeader(tpldata.script_body)||{'<missing>':''};
}
}
else
{
response.write( "Error. UJS Manager couldn't extract filename from path." );
response.close();
return;
}
var template = new Markuper( 'templates/dialog.html', tpldata );
response.write( template.parse().html() );
response.close();
return;
}
var tpldata = {
msg : $('<p>UJS Manager is made to be only accessible from Opera running this service.</p>' +
'<p>If you are the owner of this service, go to <a href="http://admin.' + request.host + opera.io.webserver.currentServicePath + '" onclick="location.replace(this.href);return false;">Admin section</a>.</p>' +
'<p>If you want to download UJS Manager, go to <a href="http://unite.opera.com/application/401/">download page</a>.</p>')
};
var template = new Markuper( 'templates/remote.html', tpldata );
response.write( template.parse().html() );
response.close();
return;
}
if ( request.bodyItems['install_script'] )
{
var
script_uri = unescape(request.bodyItems.install_script[0]),
script_body = request.bodyItems.script_body[0],
overwrite = ( request.bodyItems.overwrite ? true : false ),
filename = script_uri.match(/.+\/([^/?]+)/);
// we are pretty sure that this above will match as it did in install dialog already
filename = filename[1];
// install file if there is none already
widget.showNotification(
createFile(filename, script_body, overwrite) ?
( overwrite ? 'User script "' + filename + '" updated': 'User script "' + filename + '" installed' )
: 'Error installing "' + filename + '"!'
);
// redirect to admin part
response.setStatusCode('303');
response.setResponseHeader('Location', 'http://' + request.host + opera.io.webserver.currentServicePath);
response.close();
return;
}
// handle xhr calls with json
if ( request.bodyItems['action'] )
{
var resp = handleXHRQuery(request.bodyItems);
response.write(JSON.stringify(resp));
response.close();
return;
}
var
dir_query = null,
istopdir = true;
if ( request.queryItems['dir'] )
{
dir_query = request.queryItems['dir'][0];
istopdir = false;
}
data = {
scripts : getAllUserScripts(dir_query, istopdir),
version : SERVICE_VERSION,
newversion : updater.checkUpdate()
}
var template = new Markuper( 'templates/tpl.html', data );
response.write( template.parse().html() );
response.close();
}
function handleXHRQuery(query)
{
var
action = query['action'][0],
q_filename = query['filename'] ? query['filename'][0] : null;
switch (action)
{
case 'toggle':
var
newpath = null,
enabled = null,
File = SHARED_DIR.resolve( q_filename );
if ( File && File.exists )
{
if ( File.path.match(/\.js\.xx$/i) )
{
newpath = File.path.replace(/\.js\.xx$/i, '.js');
enabled = true;
}
else if ( File.path.match(/\.js$/i) )
{
newpath = File.path.replace(/\.js$/i, '.js.xx');
enabled = false;
}
else
{
return {error: "Error. Script with file name " + q_filename + " is not user script file!"};
}
// make sure new path does not overwrite any other file
if ( newpath && !SHARED_DIR.resolve(newpath).exists )
{
File = File.moveTo(newpath);
}
else
{
return {
error: "Error. Script with file name " + newpath.replace(/.*\/([^\/]+)$/, '$1') + " already exists!",
enabled: !enabled
};
}
return {
result: unescape(File.name),
enabled: enabled
};
}
break;
case 'getsettings':
return getScriptSettings(q_filename);
break;
case 'changesetting':
var q_exactmatch = query['exactmatch'][0];
var q_name = query['name'][0];
var q_value = query['value'][0];
return changeScriptSetting(q_filename, q_exactmatch, q_name, q_value);
break;
case 'delete':
var result = deleteFile(q_filename);
return result||{error: 'Deleting file failed!'};
case 'readtxt':
return readFile(q_filename)||{error: "Wasn't able to read file or file empty!"}
case 'writetxt':
var File = null;
if (File=writeFile(q_filename, query['data'][0], query['can_overwrite']))
return getUserScript(File);
else
return {error: "Wasn't able to write file. Possibly file already exists!"};
case 'remindmelater':
updater.remindMeLater();
return true;
default:
return {error: 'Option not implemented!'};
}
return false;
}
function sharePublicHtml()
{
var public_dir = PUBLIC_DIR.resolve('/public_html/');
public_dir.refresh();
for ( var i=0,file; file=public_dir[i]; i++ )
{
if ( file.isFile )
{
var path = file.path, match = null;
if ( match = file.path.match(/(\/[^\/]+)$/) )
{
path = match[1];
}
static_files.push( path );
}
}
}
function isPublicFile(uri)
{
for ( var i=0; i<static_files.length; i++ )
{
var item = static_files[i];
if ( uri.indexOf(item)>-1 )
return true;
}
return false;
}
/**
* Reads all user scripts in directory
* @returns array of script objects
*/
function getAllUserScripts(requested_dir, istopdir)
{
if ( requested_dir && SHARED_DIR.resolve(requested_dir).exists )
{
SHARED_DIR = SHARED_DIR.resolve(requested_dir);
}
else
{
SHARED_DIR = opera.io.filesystem.mountSystemDirectory('shared');
}
SHARED_DIR.refresh();
var
scripts = [],
ujs_installer = false;
// add link to parent directory
if ( SHARED_DIR.parent.exists )
{
scripts.push
({
prettyname : '..',
filename : SHARED_DIR.parent.path.replace('mountpoint:/', ''),
isdirectory : true
});
}
var obj = null;
for ( var i = 0, file; file = SHARED_DIR[i]; i++ )
{
// special handling for directories
if ( file.isDirectory )
{
scripts.push
({
prettyname : unescape(file.name),
filename : file.path.replace('mountpoint:/', ''),
isdirectory : true
});
continue;
}
// skip if no js extension (or disabled - .xx)
if ( !(/\.js(\.xx|)$/i.test(file.name)) ) continue;
obj = getUserScript(file);
scripts.push( obj );
// check for user js installer
if ( obj.prettyname == 'UJS Manager - script installer' )
ujs_installer = obj;
}
// only top directory should get ujs installer script
if ( istopdir )
{
// copy user js installer if not found or older
var scr_installer = getUserScript( PUBLIC_DIR.resolve('/js/ujs_manager_installer.js') );
if ( !ujs_installer
|| getPropOfArrayItem('version', scr_installer.header) > getPropOfArrayItem('version', ujs_installer.header)
|| getPropOfArrayItem('servicepath', ujs_installer.header) != service_path
|| getPropOfArrayItem('uniqueid', ujs_installer.header) != getPref('unique_id') )
{
// add user js installer to list because it wasn't there when reading dir
if ( !ujs_installer )
scripts.push( scr_installer );
// insert current service path to ujs installer
scr_installer.filecontent = scr_installer.filecontent.replace(
/\{\{service_path\}\}/g, service_path);
// generate unique id (for script installation)
var unique_id = Math.random();
scr_installer.filecontent = scr_installer.filecontent.replace(
/\{\{unique_id\}\}/g, unique_id);
savePref('unique_id', unique_id);
writeFile('ujs_manager_installer.js', scr_installer.filecontent, true);
}
}
// sort scripts by name
scripts.sort(
function(a, b)
{
// put directories on top
if ( a.isdirectory != b.isdirectory )
{
return (a.isdirectory?-1:1);
}
return (a.prettyname.toLowerCase() < b.prettyname.toLowerCase()?-1:1);
}
);
return scripts;
}
/**
* Creates object from user script file
* @returns script object
*/
function getUserScript(File)
{
var obj = {
prettyname : unescape(File.name.replace(/\.xx$/i, '')),
filename : File.name,
isenabled : ( ( /\.js$/i.test(File.name) ) ? true : false ),
isdirectory : false,
hassettings : false,
filecontent : readFile(File.path),
header : null
};
if ( obj.filecontent )
{
// if at last one setting found in file then add edit button
if ( obj.filecontent.match(/\/\*@[^@]+@(bool|int|string)@\*\/.+\/\*@\*\//g) )
{
obj.hassettings = true;
}
// read script header
obj.header = getUserScriptHeader(obj.filecontent);
if ( obj.header )
{
// try to get pretty name
obj.prettyname = getPropOfArrayItem('name', obj.header)||obj.prettyname;
}
}
return obj;
}
/**
* creates key-value object from user script header
* @return object of key-value pairs
*/
function getUserScriptHeader(content)
{
var
start = content.indexOf('// ==UserScript=='),
end = content.indexOf('// ==/UserScript=='),
desc = null;
if ( start!=-1 && end!=-1 )
{
// add length of header start string
start += 18
desc = content.substr(start, end-start);
// remove trailing/ending spaces
desc = desc.replace(/(^\s+|\s+$)/, '');
// remove some stuph
desc = desc.replace(/^\/\/\s+/gm, '');
}
if ( !desc )
return null;
var
lines = desc.split('\n'),
line,
rgx_match = null,
line_rgx = /^@(.+?)\s+(.+)/,
data = [];
for ( var i=0; i<lines.length; i++ )
{
line = lines[i];
// skip empty lines
if ( !line ) continue;
rgx_match = line.match(line_rgx);
if ( rgx_match && rgx_match.length > 1 )
{
var name = rgx_match[1], val = rgx_match[2];
data.push( { key: name, value: trim(val) } );
}
}
return (data?data:null);
}
/**
* gathers all settings from user script by looking for matching patters
* @return array of objects with specific keys
*/
function getScriptSettings(filename)
{
var content = readFile(filename);
if ( !content ) return false;
// matches all options of kind /*@opname@optype@*/opvalue/*@*/
var matches = content.match(/\/\*@[^@]+@(bool|int|string|regexp)@\*\/.*\/\*@\*\//g);
if ( !matches ) return false;
// matches individual types of given option
var
ret = [],
optionmatch = /^\/\*@([^@]+)@([^@]+)@\*\/(.*)\/\*@\*\/$/;
/*
* option object:
* name,
* type,
* value,
* exactmatch
*/
for ( var i=0; i<matches.length; i++ )
{
var match = matches[i].match(optionmatch);
ret.push({
name : match[1],
type : match[2],
value : match[3],
exactmatch : matches[i]
});
}
return ret;
}
function changeScriptSetting(filename, exactmatch, name, value)
{
var content = readFile(filename);
var newval = exactmatch.replace(/\*\/.*\/\*@\*\/$/, '*/'+value+'/*@*/');
content = content.replace(exactmatch, newval);
if ( writeFile(filename, content, true) )
{
return {
filename : filename,
name : name,
value : value,
exactmatch: newval
};
}
return {};
}
function readFile(f)
{
// accepts file object or file path
var
Stream = null,
str;
if ( typeof f == 'string' )
{
f = SHARED_DIR.resolve(f);
}
if ( f && f.exists )
{
Stream = f.open(f, opera.io.filemode.READ)
str = Stream.read( Stream.bytesAvailable );
Stream.close();
return str;
}
return null;
}
function writeFile(path, content, can_overwrite)
{
var File = null;
if ( path && content && (File=SHARED_DIR.resolve(path)) )
{
if (File.exists && !can_overwrite)
return false;
var stream = SHARED_DIR.open(path, opera.io.filemode.WRITE);
stream.write(content);
stream.close();
return File;
}
return false;
}
function createFile(path, content, overwrite)
{
return writeFile(path, content, overwrite);
}
function deleteFile(filename)
{
var File = null;
if ( filename && (File=SHARED_DIR.resolve(filename))
&& File.exists && File.isFile )
{
return SHARED_DIR.deleteFile(File);
}
return false;
}
/**
* downloads file using synchronous XHR
*/
function downloadScript(uri)
{
var xhr = new XMLHttpRequest();
xhr.open('GET', uri, false);
xhr.setRequestHeader('Cache-Control', 'no-cache');
try
{
xhr.send();
}
catch(e)
{
// will throw for local files for example
return false;
}
// little validation to make sure it's javascript
if ( xhr.getResponseHeader('Content-type').indexOf('javascript') == -1 )
return false;
if ( !xhr.responseText )
return false;
// extract file name from path
var filename = uri.match(/.+\/([^/?]+)/);
if ( filename )
filename = filename[1];
else
return false;
// create user script file
return createFile(filename, xhr.responseText);
}
function savePref(key, val)
{
return widget.setPreferenceForKey(encodeURIComponent(val), key);
}
function getPref(name)
{
return decodeURIComponent(widget.preferenceForKey(name))||null;
}
/**
* update checking
*/
var updater = new function()
{
var latest_ver = getPref('latestVer');
var last_check = getPref('lastCheck');
this.checkUpdate = function(force)
{
// if there is saved pref with higher version then
// don't check. we know about new version already
if ( latest_ver && latest_ver > SERVICE_VERSION )
{
return true;
}
// get curent time (ms)
var cur_date = new Date().getTime();
// don't proceed if we checked for new version in last 3 days
if ( last_check && !force )
{
var days_since_check = (cur_date-last_check)/(1000*60*60*24);
if ( days_since_check < 3 ) return false;
}
// update last check data
savePref('lastCheck', cur_date);
var req = new XMLHttpRequest();
req.onreadystatechange = function()
{
if ( req.readyState == 4 )
{
var
doap = req.responseXML,
remoteVersion = null;
if ( !doap ) return false;
var nsresolve = function(ns)
{
switch(ns)
{
case 'rdf': return 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
case 'doap': return 'http://usefulinc.com/ns/doap#';
}
}
// compare with current version
if ( remoteVersion = doap.selectSingleNode('//rdf:RDF/doap:Project/doap:release/doap:Version/doap:revision', nsresolve) )
{
remoteVersion = remoteVersion.text;
if ( latest_ver && parseFloat(remoteVersion) > latest_ver )
{
savePref('latestVer', remoteVersion);
return true;
}
else
{
savePref('latestVer', remoteVersion);
}
}
return false;
}
}
req.open('GET', SERVICE_DOAP, false);
req.setRequestHeader('Cache-Control', 'no-cache');
req.send();
};
this.remindMeLater = function()
{
savePref('latestVer', latest_ver=SERVICE_VERSION);
savePref('lastCheck', last_check=new Date().getTime());
}
}
function getPropOfArrayItem(prop, arr)
{
for (var i=0; i<arr.length; i++)
{
if (arr[i].key==prop)
return arr[i].value;
}
return null;
}
function trim(str)
{
return str.replace(/^[\s\-]+|\s+$/, '');
}