-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandboxAPI.js
1304 lines (1155 loc) · 28.8 KB
/
sandboxAPI.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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var libpath = require('path'),
http = require("http"),
fs = require('fs-extra'),
url = require("url"),
mime = require('mime'),
sio = require('socket.io'),
YAML = require('js-yaml');
require('./hash.js');
var safePathRE = RegExp('/\//'+(libpath.sep=='/' ? '\/' : '\\')+'/g');
var datapath = '.'+libpath.sep+'data';
var DAL = null;
// default path to data. over written by setup flags
//generate a random id.
function GUID()
{
var S4 = function ()
{
return Math.floor(
Math.random() * 0x10000 /* 65536 */
).toString(16);
};
return (
S4() + S4() + "-" +
S4() + "-" +
S4() + "-" +
S4() + "-" +
S4() + S4() + S4()
);
}
//simple functio to write a response
function respond(response,status,message)
{
response.writeHead(status, {
"Content-Type": "text/plain"
});
response.write(message + "\n");
global.log(message,2);
response.end();
}
//Just serve a simple file
function ServeFile(filename,response,URL, JSONHeader)
{
global.log(filename,2);
var datatype = "binary";
if(JSONHeader)
datatype = "utf8";
fs.readFile(filename, datatype, function (err, file) {
if (err) {
respond(response,500,err);
return;
}
var type = mime.lookup(filename) || "text/json";
response.writeHead(200, {
"Content-Type": !JSONHeader ? type : "text/json"
});
if(datatype == "binary")
response.write(file, "binary");
else
{
var o = {};
o[JSONHeader] = file;
response.write(JSON.stringify(o), "utf8");
}
response.end();
});
}
//get a profile for a user
//url must contain UID for user and password hash
function ServeProfile(UID,response,URL)
{
DAL.getUser(UID,function(user)
{
if(!user)
{
respond(response,401,"user not logged in, or profile not found");
}else
{
user.Password = '';
respond(response,200,JSON.stringify(user));
}
});
}
function GetLoginData(response,URL)
{
if(URL.loginData)
{
var logindata = {username:URL.loginData.UID,admin:URL.loginData.UID==global.adminUID};
logindata.instances = [];
logindata.clients = [];
for(var i in global.instances)
{
for(var j in global.instances[i].clients)
{
if(global.instances[i].clients[j].loginData && global.instances[i].clients[j].loginData.UID == URL.loginData.UID)
{
logindata.instances.push(i);
logindata.clients.push(j);
}
}
}
respond(response,200,JSON.stringify(logindata));
}
else
respond(response,401,JSON.stringify({username:null}));
return;
}
function SessionData()
{
this.sessionId = GUID();
this.UID = '';
this.Password = '';
this.loginTime = new Date();
this.clients = {};
this.setTimeout = function(sec)
{
if(this.timeout) clearTimeout(this.timeout);
this.timeout = setTimeout(function()
{
//if I have no active clients, log me out
if(Object.keys(this.clients).length == 0)
{
global.sessions.splice(global.sessions.indexOf(this),1);
global.log('Removing Session data for ' + this.UID,1);
}
//wait another three minutes and try again
else
this.resetTimeout();
}.bind(this),sec*1000);
}
this.resetTimeout = function()
{
//15 mins
this.setTimeout(900);
}
}
//login to the site
function SiteLogin(response,URL)
{
var UID = URL.query.UID;
var password = URL.query.P;
if(!UID || !password)
{
respond(response,401,'Login Format incorrect');
return;
}
if(URL.loginData)
{
respond(response,401,'Already Logged in');
return;
}
CheckPassword(UID,password,function(ok)
{
global.log("Login "+ ok,2);
if(ok)
{
var session = new SessionData();
session.UID = UID;
session.Password = password;
session.resetTimeout();
global.sessions.push(session);
response.writeHead(200, {
"Content-Type": "text/plain",
"Set-Cookie": "session="+session.sessionId+"; Path=/; HttpOnly;"
});
response.write("Login Successful", "utf8");
global.log('Client Logged in',1);
response.end();
}else
{
respond(response,401,'Password incorrect');
return;
}
});
}
//login to the site
function SiteLogout(response,URL)
{
if(!URL.loginData)
{
respond(response,401,"Client Not Logged In");
return;
}
if(global.sessions.indexOf(URL.loginData) != -1)
{
global.sessions.splice(global.sessions.indexOf(URL.loginData),1);
response.writeHead(200, {
"Content-Type": "text/plain",
"Set-Cookie": "session=; HttpOnly;"
});
response.end();
}else
{
respond(response,401,"Client Not Logged In");
return;
}
return;
}
//Take ownership if a client websocket connection
//must provide a password and name for the user, and the instance and client ids.
//This will associate a user with a reflector connection
//The reflector will not accept incomming messages from an anonymous connection
function InstanceLogin(response,URL)
{
global.log('instance login',2);
if(!URL.loginData)
{
global.log("Client Not Logged In",1);
respond(response,401,"Client Not Logged In");
return;
}
var instance = URL.query.S;
var cid = URL.query.CID;
if(URL.loginData.clients[cid])
{
respond(response,401,"Client already logged into session");
return;
}
if(global.instances[instance] && global.instances[instance].clients[cid])
{
URL.loginData.clients[cid] = instance;
global.instances[instance].clients[cid].loginData = URL.loginData;
if(global.instances[instance].state.findNode('index-vwf').properties['owner'] == undefined)
global.instances[instance].state.findNode('index-vwf').properties['owner'] = URL.loginData.UID;
respond(response,200,"Client Logged Into " + instance);
return;
}else
{
respond(response,200,"Client Or Instance does not exist " + instance);
return;
}
}
function InstanceLogout(response,URL)
{
if(!URL.loginData)
{
respond("Client Not Logged In",401,response);
return;
}
var instance = URL.query.S;
var cid = URL.query.CID;
if(URL.loginData.clients[cid])
{
if(global.instances[URL.loginData.clients[cid]])
{
if(global.instances[URL.loginData.clients[cid]].clients[cid])
{
delete global.instances[URL.loginData.clients[cid]].clients[cid].loginData;
}
}
delete URL.loginData.clients[cid];
respond(response,200,"Client Logged out " + instance);
}else
{
respond(response,200,"Client was not Logged into " + instance);
return;
}
return;
}
function getInventory(URL,response)
{
if(!URL.loginData)
{
respond(response,401,'no login data');
return;
}
DAL.getInventoryDisplayData(URL.loginData.UID,function(inventory)
{
ServeJSON(inventory,response,URL);
});
}
function getInventoryItemAssetData(URL,response)
{
if(!URL.loginData)
{
respond(response,401,'no login data');
return;
}
if(!URL.query.AID)
{
respond(response,500,'no AID in query string');
return;
}
DAL.getInventoryItemAssetData(URL.loginData.UID,URL.query.AID,function(item)
{
ServeJSON(item,response,URL);
});
}
function getInventoryItemMetaData(URL,response)
{
if(!URL.loginData)
{
respond(response,401,'no login data');
return;
}
if(!URL.query.AID)
{
respond(response,500,'no AID in query string');
return;
}
DAL.getInventoryItemMetaData(URL.loginData.UID,URL.query.AID,function(item)
{
ServeJSON(item,response,URL);
});
}
function addInventoryItem(URL,data,response)
{
if(!URL.loginData)
{
respond(response,401,'no login data');
return;
}
DAL.addToInventory(URL.loginData.UID,{title:URL.query.title,uploaded:new Date(),description:'',type:URL.query.type},data,function(id)
{
respond(response,200,id);
});
}
function updateInventoryItemMetadata(URL,data,response)
{
if(!URL.loginData)
{
respond(response,401,'no login data');
return;
}
if(!URL.query.AID)
{
respond(response,500,'no AID in query string');
return;
}
DAL.updateInventoryItemMetadata(URL.loginData.UID,URL.query.AID,JSON.parse(data),function()
{
respond(response,200,'ok');
});
}
function deleteInventoryItem(URL,response)
{
if(!URL.loginData)
{
respond(response,401,'no login data');
return;
}
if(!URL.query.AID)
{
respond(response,500,'no AID in query string');
return;
}
DAL.deleteInventoryItem(URL.loginData.UID,URL.query.AID,function()
{
respond(response,200,'ok');
});
}
function getGlobalInventory(URL,response)
{
DAL.getInventoryDisplayData('___Global___',function(inventory)
{
ServeJSON(inventory,response,URL);
});
}
function getGlobalInventoryItemAssetData(URL,response)
{
if(!URL.query.AID)
{
respond(response,500,'no AID in query string');
return;
}
DAL.getInventoryItemAssetData('___Global___',URL.query.AID,function(item)
{
ServeJSON(item,response,URL);
});
}
function getGlobalInventoryItemMetaData(URL,response)
{
if(!URL.query.AID)
{
respond(response,500,'no AID in query string');
return;
}
DAL.getInventoryItemMetaData('___Global___',URL.query.AID,function(item)
{
ServeJSON(item,response,URL);
});
}
function addGlobalInventoryItem(URL,data,response)
{
if(!URL.loginData)
{
respond(response,401,'no login data');
return;
}
DAL.addToInventory('___Global___',{uploader:URL.loginData.UID,title:URL.query.title,uploaded:new Date(),description:'',type:URL.query.type},data,function(id)
{
respond(response,200,id);
});
}
function deleteGlobalInventoryItem(URL,response)
{
if(!URL.loginData)
{
respond(response,401,'no login data');
return;
}
if(!URL.query.AID)
{
respond(response,500,'no AID in query string');
return;
}
DAL.getInventoryItemMetaData('___Global___',URL.query.AID,function(item)
{
if(item.uploader == URL.loginData.UID)
{
DAL.deleteInventoryItem('___Global___',URL.query.AID,function()
{
respond(response,200,'ok');
});
}else
{
respond(response,401,'you are not the asset owner');
}
});
}
function ServeJSON(jsonobject,response,URL)
{
response.writeHead(200, {
"Content-Type": "text/json"
});
if (jsonobject.constructor != String)
response.write(JSON.stringify(jsonobject), "utf8");
else
response.write(jsonobject, "utf8");
response.end();
}
function SaveProfile(URL,data,response)
{
if(!URL.loginData)
{
respond(response,401,'no login data saving profile ' + filename);
return;
}
DAL.updateUser(URL.loginData.UID,data,function()
{
respond(response,200,'');
return;
});
}
function CreateProfile(URL,data,response)
{
data = JSON.parse(data);
data.Password = Hash(URL.query.P);
DAL.createUser(URL.query.UID,data,function()
{
respond(response,200,'');
return;
});
}
//Read the password from the profile for the UID user, and callback with the match
function CheckPassword(UID,Password, callback)
{
DAL.getUser(UID,function(user)
{
if(!user)
{
callback(false);
return;
}
callback(user.Password == Hash(Password));
return;
});
}
//Check that the UID is the author of the asset
function CheckAuthor(UID,assetFilename, callback)
{
var basedir = datapath + "/GlobalAssets/".replace(safePathRE);
if(!fs.existsSync(assetFilename))
{
callback(false);
return;
}
else
{
fs.readFile(assetFilename, "utf8", function (err, file) {
var asset = JSON.parse(file);
var storedAuthor = asset.Author;
var suppliedAuthor = UID;
global.log(storedAuthor,suppliedAuthor,2);
callback(storedAuthor == suppliedAuthor);
});
return;
}
return;
callback(false);
}
//Check that the UID is the owner of the state
function CheckOwner(UID,stateFilename, callback)
{
var basedir = datapath + "/GlobalAssets/".replace(safePathRE);
if(!fs.existsSync(stateFilename))
{
callback(false);
return;
}
else
{
fs.readFile(stateFilename, "utf8", function (err, file) {
var asset = JSON.parse(file);
var storedOwner = asset[asset.length-1].owner;
var suppliedOwner = UID;
global.log(storedOwner,suppliedOwner,2);
callback(storedOwner == suppliedOwner);
});
return;
}
return;
callback(false);
}
//Save an asset. the POST URL must contain valid name/password and that UID must match the Asset Author
function SaveAsset(URL,filename,data,response)
{
var UID = URL.query.UID || (URL.loginData && URL.loginData.UID);
var P = URL.query.P || (URL.loginData && URL.loginData.Password);
CheckPassword(UID,P,function(e){
//Did no supply a good name password pair
if(!e)
{
respond(response,401,'Incorrect password when saving Asset ' + filename);
return;
}else
{
//the asset is new
if(!fs.existsSync(filename))
{
//Save the asset Author info
global.log('parse asset',2);
var asset = JSON.parse(data);
asset.Author = URL.query.UID;
data = JSON.stringify(asset);
SaveFile(filename,data,response);
global.log('Saved Asset ' + filename,2);
return;
}else
{
//overwriting the asset;
CheckAuthor(UID,filename,function(e){
//trying to overwrite existing file that user is not author of
if(!e)
{
respond(response,401,'Permission denied to overwrite asset ' + filename);
return;
}else
{
//Over writing an asset that the user owns
var asset = JSON.parse(data);
asset.Author = URL.query.UID;
data = JSON.stringify(asset);
SaveFile(filename,data,response);
global.log('Saved Asset ' + filename,2);
return;
}
});
}
}
});
}
//Save an asset. the POST URL must contain valid name/password and that UID must match the Asset Author
function DeleteProfile(URL,filename,response)
{
DAL.deleteUser(URL.loginData,function()
{
respond(response,200,'');
});
}
var deleteFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + libpath.sep + file;
if(fs.statSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
function strBeginsWith(str, prefix) {
return str.match('^' + prefix)==prefix;
}
function strEndsWith(str, suffix) {
return str.match(suffix+"$")==suffix;
}
//Save an asset. the POST URL must contain valid name/password and that UID must match the Asset Author
function CopyState(URL,filename,newname,response)
{
var UID = URL.query.UID || (URL.loginData && URL.loginData.UID);
var P = URL.query.P || (URL.loginData && URL.loginData.Password);
if(!UID || !P)
{
respond(response,401,'No Credentials to copy state to ' + newname);
return;
}
newname = newname.replace(/[\\\/]/g,'_');
var appname = filename.replace(/_[a-zA-Z0-9]*?_$/,'');
var stateID = newname.match(/_([a-zA-Z0-9]*?)_$/)[1];
if(!strBeginsWith(newname,appname) || !strEndsWith(newname,'_') || !stateID || stateID.length != 16)
{
respond(response,401,'Bad new name ' + newname);
return;
}
filename = datapath+"/states/".replace(safePathRE) + filename;
newname = datapath+"/states/".replace(safePathRE) + newname;
CheckPassword(UID,P,function(e){
//Did not supply a good name password pair
if(!e)
{
respond(response,401,'Incorrect password when deleting state ' + filename);
return;
}
else
{
//the asset is new
if(!fs.existsSync(filename))
{
respond(response,401,'cant delete state that does not exist' + filename);
return;
}
else
{
if(fs.existsSync(newname))
{
respond(response,500,'new state name in use' + filename);
return;
}
else
{
fs.copy(filename,newname,function()
{
respond(response,200,"Copied state " + filename + " to " + newname);
});
}
}
}
});
}
//Save an asset. the POST URL must contain valid name/password and that UID must match the Asset Author
function DeleteState(URL,SID,response)
{
if(!URL.loginData)
{
respond(response,401,'Anonymous users cannot delete instances');
return;
}
DAL.getInstance(SID,function(state)
{
if(state.owner != URL.loginData.UID && URL.loginData.UID != global.adminUID)
{
respond(response,401,'User does not have permission to delete instance');
return;
}else
{
DAL.deleteInstance(SID,function()
{
respond(response,200,'deleted instance');
return;
});
}
});
}
function RenameFile(filename,newname,callback,sync)
{
if(!sync)
fs.rename(filename,newname,callback);
else
{
fs.renameSync(filename,newname);
callback();
}
}
//make a directory if the directory does not exist
function MakeDirIfNotExist(dirname,callback)
{
fs.exists(dirname, function(e)
{
if(e)
callback();
else
{
fs.mkdir(dirname,function(){
callback();
});
}
});
}
//hash a string
function hash(str)
{
return require('crypto').createHash('md5').update(str).digest("hex");
}
//no point clogging up the disk with backups if the state does not change.
function CheckHash(filename,data,callback)
{
fs.readFile(filename, "utf8", function (err, file) {
global.log("hash is:"+hash(data) +" "+ hash(file),2);
callback(hash(data) == hash(file));
});
return;
}
//Save an instance. the POST URL must contain valid name/password and that UID must match the Asset Author
function SaveState(URL,id,data,response)
{
if(!URL.loginData)
{
respond(response,401,'No login data when saving state');
return;
}
//not currently checking who saves the state, so long as they are logged in
DAL.saveInstanceState(id,data,function()
{
respond(response,200,'saved ' + id);
return;
});
}
//Save an asset. the POST URL must contain valid name/password and that UID must match the Asset Author
function DeleteAsset(URL,filename,response)
{
var UID = URL.query.UID || (URL.loginData && URL.loginData.UID);
var P = URL.query.P || (URL.loginData && URL.loginData.Password);
CheckPassword(UID,P,function(e){
//Did no supply a good name password pair
if(!e)
{
respond(response,401,'Incorrect password when deleting Asset ' + filename);
return;
}
else
{
//the asset is new
if(!fs.existsSync(filename))
{
respond(response,401,'cant delete asset that does not exist' + filename);
return;
}
else
{
//overwriting the asset;
CheckAuthor(UID,filename,function(e){
//trying to delete existing file that user is not author of
if(!e)
{
respond(response,401,'Permission denied to delete asset ' + filename);
return;
}else
{
fs.unlink(filename);
respond(response,200,'Deleted asset ' + filename);
return;
}
});
}
}
});
}
function SaveFile(filename,data,response,sync)
{
if(!sync)
{
fs.writeFile(filename,data,'binary',function()
{
respond(response,200,'Saved ' + filename);
});
}else
{
fs.writeFileSync(filename,data,'binary');
respond(response,200,'Saved ' + filename);
}
}
function _404(response)
{
response.writeHead(404, {
"Content-Type": "text/plain",
"Access-Control-Allow-Origin": "*"
});
response.write("404 Not Found\n");
response.end();
}
function RecurseDirs(startdir, currentdir, files)
{
for(var i =0; i<files.length; i++)
{
if(fs.statSync(startdir + currentdir + libpath.sep+ files[i]).isDirectory())
{
var o = {};
var newfiles = fs.readdirSync(startdir + currentdir + libpath.sep + files[i]+libpath.sep);
var tdir = currentdir ? currentdir + libpath.sep + files[i] : files[i];
RecurseDirs(startdir,tdir,newfiles);
newfiles.sort(function(a,b){
if(typeof a == "string" && typeof b == "string") return (a<b ? -1 : 1);
if(typeof a == "object" && typeof b == "string") return 1;
if(typeof a == "string" && typeof b == "object") return -1;
return -1;
});
for(var j = 0; j < newfiles.length; j++)
if(typeof newfiles[j] == "string")
newfiles[j] = currentdir + libpath.sep + files[i] + libpath.sep + newfiles[j];
o[currentdir ? currentdir + libpath.sep + files[i] : files[i]] = newfiles;
files[i] = o;
}
}
}
//Generate a random ID for a instance
var ValidIDChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
function makeid()
{
var text = "";
for( var i=0; i < 16; i++ )
text += ValidIDChars.charAt(Math.floor(Math.random() * ValidIDChars.length));
return text;
}
function setStateData(URL,data,response)
{
if(!URL.loginData)
{
respond(response,401,'Anonymous users cannot edit instances');
return;
}
data = JSON.parse(data);
var sid = URL.query.SID;
var statedata = {};
sid = sid.replace(/\//g,'_');
statedata.title = data.title;
statedata.description = data.description;
statedata.lastUpdate = (new Date());
DAL.getInstance(sid,function(state)
{
if(!state)
{
respond(response,401,'State not found. State ' + sid);
return;
}
if(state.owner == URL.loginData.UID || URL.loginData.UID == global.adminUID)
{
DAL.updateInstance(sid,statedata,function()
{
respond(response,200,'Created state ' + sid);
});
}else
{
respond(response,401,'Not authorized to edit state ' + sid);
}
});
}
function createState(URL,data,response)
{
if(!URL.loginData)
{
respond(response,401,'Anonymous users cannot create instances');
return;
}
data = JSON.parse(data);
var statedata = {};
statedata.objects = 0;
statedata.owner = URL.loginData.UID;
statedata.title = data.title;
statedata.description = data.description;
statedata.lastUpdate = (new Date());
var id = '_adl_sandbox_' + makeid() +'_';
DAL.createInstance(id,statedata,function()
{
respond(response,200,'Created state ' + id);
});
}
//Just return the state data, dont serve a response
function getState(SID)
{
SID = SID.replace(/[\\,\/]/g,'_');
var basedir = datapath + libpath.sep;
var statedir = (basedir + 'States/' + SID).replace(safePathRE);
var statefile = statedir + '/state'.replace(safePathRE);
global.log('serve state ' + statedir,2);