forked from rejetto/hfs2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scriptLib.pas
2644 lines (2350 loc) · 69.3 KB
/
scriptLib.pas
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
{
Copyright (C) 2002-2020 Massimo Melina (www.rejetto.com)
This file is part of HFS ~ HTTP File Server.
HFS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
HFS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with HFS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
}
unit scriptLib;
interface
uses main, classesLib, iniFiles, types;
type
TmacroData = record
cd: TconnData;
tpl: Ttpl;
folder, f: Tfile;
afterTheList, archiveAvailable, hideExt, breaking: boolean;
aliases, tempVars: THashedStringList;
table: TStringDynArray;
logTS: boolean;
end;
var
defaultAlias: THashedStringList;
staticVars : THashedStringList; // these scripting variables are held for the whole run-time
eventScripts: Ttpl;
function tryApplyMacrosAndSymbols(var txt:string; var md:TmacroData; removeQuotings:boolean=true):boolean;
function macroQuote(s:string):string;
function runScript(script:string; table:TstringDynArray=NIL; tpl_:Ttpl=NIL; f:Tfile=NIL; folder:Tfile=NIL; cd:TconnData=NIL):string;
function runEventScript(event:string; table:TStringDynArray=NIL; cd:TconnData=NIL):string;
procedure resetLog();
implementation
uses windows, utilLib, trayLib, parserLib, graphics, classes, sysutils, StrUtils,
hslib, comctrls, math, controls, forms, clipbrd, MMsystem;
const
HEADER = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style>'
+#13'dt, dd { margin:0; padding:0.2em 0.5em; white-space:pre; display:block; font-family:monospace; } dt { background:#dfd; } dd { background:#fdd; }'
+#13'</style></head><body>';
var
stopOnMacroRename: boolean; // this ugly global var is used to avoid endless recursion on a renaming rename event. this method won't work on a multithreaded system, but i opted for it because otherwise the changes would have been big.
cachedTpls: TcachedTpls;
function macrosLog(textIn, textOut:string; ts:boolean=FALSE):boolean;
var
s: string;
begin
s:='';
if ts then
s:='<hr>'+dateTimeToStr(now())+CRLF;
s:=s+#13'<dt>'+htmlEncode(textIn)+'</dt><dd>'+htmlEncode(textOut)+'</dd>';
if sizeOfFile(MACROS_LOG_FILE) = 0 then
s:=HEADER+s;
result:=appendTextFile(MACROS_LOG_FILE, s);
end; // macrosLog
procedure resetLog();
begin saveFile(MACROS_LOG_FILE, '') end;
function expandLinkedAccounts(account:Paccount):TStringDynArray;
var
i: integer;
begin
result:=NIL;
if account = NIL then exit;
i:=0;
result:=account.link;
while i < length(result) do
begin
account:=getAccount(result[i], TRUE);
inc(i);
if (account = NIL) or not account.enabled then continue;
addUniqueArray(result, account.link);
end;
end; // expandLinkedAccounts
function encodeMarkers(s:string):string;
var
i: integer;
t: string;
begin
for i:=0 to length(MARKERS)-1 do
begin
t:=MARKERS[i];
replace(t, '&#'+intToStr(charToUnicode(t[1]))+';', 1,1);
s:=replaceStr(s, MARKERS[i], t);
end;
result:=s;
end; // encodeMarkers
function noMacrosAllowed(s:string):string;
// prevent hack attempts
var
i: integer;
begin
i:=1;
enforceNUL(s);
repeat
i:=findMacroMarker(s, i);
if i = 0 then break;
replace(s, '&#'+intToStr(charToUnicode(s[i]))+';', i,i);
until false;
s:=reReplace(s,'%([-a-z0-9]+%)','%$1', 'mi');
result:=s;
end; // noMacrosAllowed
function isMacroQuoted(s:string):boolean;
begin result:=ansiStartsStr(MARKER_QUOTE, s) and ansiEndsStr(MARKER_UNQUOTE, s) end;
function macroQuote(s:string):string;
var
t: string;
begin
enforceNUL(s);
if not anyMacroMarkerIn(s) then
begin
result:=s;
exit;
end;
// an UNQUOTE would invalidate our quoting, so let's encode any of it
t:=MARKER_UNQUOTE;
replace(t, '&#'+intToStr(charToUnicode(t[1]))+';', 1,1);
result:=MARKER_QUOTE+replaceStr(s, MARKER_UNQUOTE, t)+MARKER_UNQUOTE
end; // macroQuote
function macroDequote(s:string):string;
begin
result:=s;
s:=trim(s);
if isMacroQuoted(s) then
result:=copy(s, length(MARKER_QUOTE)+1, length(s)-length(MARKER_QUOTE)-length(MARKER_UNQUOTE) );
end; // macroDequote
function cbMacros(fullMacro:string; pars:Tstrings; cbData:pointer):string;
var
md: ^TmacroData;
name, p: string;
unnamedPars: integer; // this is a guessing of the number of unnamed parameters. just guessing because there's no true distinction between a parameter "value" named "key", and parameter "key=value"
procedure macroError(msg:string);
begin result:='<div class=macroerror>macro error: '+name+nonEmptyConcat('<br>',msg)+'</div>' end;
procedure deprecatedMacro(what:string=''; instead:string='');
begin mainfrm.add2log('WARNING, deprecated macro: '+first(what, name)+nonEmptyConcat(' - Use instead: ',instead), NIL, clRed) end;
procedure unsatisfied(b:boolean=TRUE);
begin
if b then
macroError('cannot be used here')
end;
function satisfied(p:pointer):boolean;
begin
result:=assigned(p);
unsatisfied(not result);
end;
function parEx(idx:integer; name:string=''; doTrim:boolean=TRUE):string; overload;
var
i: integer;
begin
result:='';
if name > '' then
begin
i:=pars.IndexOfName(name);
if i >= 0 then
begin
result:=pars.valueFromIndex[i];
if doTrim then result:=trim(result);
exit;
end;
end;
if (idx < 0) // no numeric index accept
or (idx >= pars.count) // invalid index
or (name > '') and (pars.names[idx] > '') and not anycharIn(' '#13#10, pars.names[idx]) // this numerical index was already taken by a valid mnemonic name
then
raise Exception.create('invalid parameter index');
result:=pars[idx];
if doTrim then result:=trim(result);
end; // parEx
function parEx(name:string; doTrim:boolean=TRUE):string; overload;
begin result:=parEx(-1, name, doTrim) end;
function par(idx:integer; name:string=''; doTrim:boolean=TRUE):string; overload;
begin
try result:=parEx(idx, name, doTrim)
except result:='' end
end;
function par(name:string=''; doTrim:boolean=TRUE; defval:string=''):string; overload;
begin
try result:=parEx(-1, name, doTrim)
except result:=defval end
end;
function parI(idx:integer):int64; overload;
begin result:=strToInt64(par(idx)) end;
function parI(idx:integer; def:int64):int64; overload;
begin result:=strToInt64Def(par(idx), def) end;
function parI(name:string; def:int64):int64; overload;
begin result:=strToInt64Def(par(name), def) end;
function parF(idx:integer):extended; overload;
begin result:=strToFloat(par(idx)) end;
function parF(idx:integer; def:extended):extended; overload;
begin result:=strToFloatDef(par(idx), def) end;
function parF(name:string; def:extended):extended; overload;
begin result:=strToFloatDef(par(name), def) end;
// note this function works on N parameters
function parExist(names: array of string):boolean;
var
i: integer;
begin
result:=FALSE;
for i:=0 to length(names)-1 do
if pars.indexOfName(names[i]) < 0 then
exit;
result:=TRUE;
end; // parExist
procedure trueIf(condition:boolean);
begin if condition then result:='1' else result:='' end;
// this is for cases where normally we want a "clean" output. User can still detect outcome by using macro "length".
// Reason for having this instead of using in place a simple "result:=if_(cond, ' ')" is to evidence our purpose. It's not faster or cleaner, it's more semantic.
procedure spaceIf(condition:boolean);
begin if condition then result:=' ' else result:='' end;
function isFalse(s:string):boolean;
begin result:=(s='') or (strToFloatDef(s,1) = 0) end;
function isTrue(s:string):boolean; inline;
begin result:=not isFalse(s) end;
function getVarSpace(var varname:string):THashedStringList;
begin
varname:=trim(varname);
if ansiStartsStr(G_VAR_PREFIX, varname) then
begin
result:=staticVars;
delete(varname,1,length(G_VAR_PREFIX));
end
else if assigned(md.cd) then
result:=md.cd.vars
else if assigned(md.tempVars) then
result:=md.tempVars
else
raise Exception.create('no namespace available');
end; // getVarSpace
function getVar(varname:string):string; overload;
begin result:=getVarSpace(varname).values[varname] end;
// if par with name exists, then it's a var name, otherwise it's a constant value at specified index
function parVar(parname:string; idx:integer):string; overload;
begin
if parExist([parname]) then
result:=getVar(par(parname))
else
result:=pars[idx];
end; // parVar
function setVar(varname, value:string; space:THashedStringList=NIL):boolean;
var
o: Tobject;
i: integer;
begin
result:=FALSE;
if space = NIL then
space:=getVarSpace(varname);
if not satisfied(space) then exit;
i:=space.indexOfName(varname);
if i < 0 then
if value = '' then exit(TRUE) // all is good the way it is
else i:=space.add(varname+'='+value)
else
if value > '' then // in case of empty value, there's no need to assign, because we are going to delete it (after we cleared the bound object)
space.valueFromIndex[i]:=value;
assert(i >= 0, 'setVar: i<0');
// the previous hash object linked to this data is not valid anymore, and must be freed
o:=space.objects[i];
freeAndNIL(o);
if value = '' then
space.delete(i)
else
space.objects[i]:=NIL;
result:=TRUE;
end; // setVar
// we wrap pos() to switch between case sensitivity
function pos_(caseSensitive:boolean; ss, s:string; ofs:integer=1):integer;
begin
if caseSensitive then result:=posEx(ss,s,ofs)
else result:=ipos(ss,s,ofs)
end; // pos_
procedure allLogic(isAnd:boolean); // when not "isAnd", then it isOr ;-)
var
i: integer;
begin
// AND will return first FALSE value, or having none, the last TRUE value.
// OR will return last TRUE value, or having none, last value.
result:='';
for i:=0 to pars.count-1 do
begin
result:=par(i);
if isAnd xor isTrue(result) then exit;
end;
end; // allLogic
procedure substring();
var
i, j: integer;
s: string;
what2inc: integer;
caseSens: boolean;
begin
result:='';
// input what to be included in the result
s:=par('include');
try what2inc:=strToInt(s)
except // we also support the following values
if s = 'none' then what2inc:=0
else if s = 'both' then what2inc:=3
else if s = '1+2' then what2inc:=3
else what2inc:=1; // by default we include only the first delimiter
end;
caseSens:=isTrue(par('case'));
// find the delimiters
s:=macroDequote(par(2));
if pars[0] = '' then i:=1
else i:=pos_(caseSens, pars[0], s); // we don' trim this, so you can use blank-space as delimiter
if i = 0 then exit;
j:=pos_(caseSens, pars[1], s, i+1);
if j = 0 then j:=length(s)+1;
// apply what2inc
if what2inc and 1 = 0 then
inc(i, length(pars[0]));
if what2inc and 2 > 0 then
inc(j, length(pars[1]));
// end of the story
result:=macroQuote(copy(s, i, j-i));
end; // substring
procedure switch();
var
what, sep: string;
i, j: integer;
a: TStringDynArray;
begin
what:=par(0);
sep:=first(pars[1], ' '); // we don' trim this, so you can use blank-space as separator
i:=2;
while i < pars.count do
begin
if i = pars.count-1 then
begin
result:=macroDequote(par(i));
exit;
end;
a:=split(sep, par(i));
for j:=0 to length(a)-1 do
if sameText(a[j], what) then
begin
result:=macroDequote(par(i+1));
exit;
end;
inc(i, 2);
end;
result:='';
end; // switch
procedure cut();
var
from, upTo, l: integer;
s, v: string;
begin
v:=par('var');
if v = '' then
s:=par(2,'what')
else
s:=getVar(v);
l:=length(s);
from:=strToIntDef(par(0,'from'), 1);
if from < 0 then from:=l+from+1;
try upTo:=strToInt(parEx('to'))
except
upTo:=strToIntDef(par(1,'size'), 0);
if upTo = 0 then
upTo:=l
else if upTo > 0 then
upTo:=from+upTo-1
else
upTo:=l+upTo;
end;
result:=substr(s, from, upTo);
try setVar(parEx('remainder'), substr(s,1,from-1)+substr(s,upTo+1));
except end;
if v = '' then exit;
setVar(v, result);
result:='';
end; // cut
procedure minOrMax();
var
i: integer;
r, v: real;
min: boolean;
begin
min:=name='min';
r:=parF(0);
for i:=1 to pars.Count-1 do
begin
v:=parF(i);
if (v < r) and min
or (v > r) and not min then
r:=v;
end;
result:=floatToStr(r);
end; // minOrMax
procedure getUri();
var
i, ex, eq: integer;
vars: Tstrings;
s: string;
begin
if not satisfied(md.cd) then
exit;
try
result:=md.cd.conn.request.url;
if pars.count < 2 then exit;
s:=result;
result:=chop('?', s);
vars:=TstringList.create();
try
vars.delimiter:='&';
vars.quoteChar:=#0;
vars.delimitedText:=s;
if pars.count > 1 then
for i:=1 to pars.count-1 do
begin
s:=par(i);
if s = '' then continue;
eq:=pos('=', s);
if eq = 0 then
begin
if vars.indexOf(s) < 0 then
vars.add(pars[i]);
continue;
end;
ex:=vars.indexOfName(chop(eq,s));
if ex < 0 then
if s = '' then
continue // the parameter didn't exist, and we are trying to empty it
else
vars.add(par(i)) // didn't exist, put the whole
else
if s = '' then
vars.delete(ex) // exists, but we are trying to empty it
else
vars.valueFromIndex[ex]:=s; // exists, change the value
end;
if vars.count = 0 then exit;
for i:=vars.Count-1 downto 0 do
if vars[i] = '' then
vars.delete(i);
result:=result+'?'+vars.delimitedText;
finally vars.free end;
finally result:=macroQuote(result) end;
end; // getUri
procedure section(ofs:integer);
var
t: Ttpl;
s: string;
begin
if not satisfied(md.tpl) then exit;
s:=par(ofs);
if (par('file') = '') and ((s = '') or (pos('=',s) > 0)) then
begin // current template
result:='';
t:=md.tpl;
ofs:=parI('back', 0);
while ofs > 0 do
begin
dec(ofs);
t:=t.over;
if t = NIL then exit;
end;
try result:=t[p] except end;
exit;
end;
// template in other file
t:=Ttpl.create;
try
t.fullText:=loadTextFile(par(ofs, 'file'));
result:=t[p];
finally t.free end;
// templates outside hfs folder get quoted for security reasons
if anyCharIn('\/', par(ofs)) then
result:=macroQuote(result);
end; // section
function urlVar(k:string):string;
var
s: string;
begin
if not satisfied(md.cd) then exit;
s:=md.cd.urlvars.values[k];
if (s = '') and (md.cd.urlvars.indexOf(k) >= 0) then s:='1';
try
result:=noMacrosAllowed(s);
setVar(parEx('var'), result); // if no var is specified, it will break here, and result will have the value
result:='';
except end;
end; // urlVar
function maybeUrlvar(k:string):string;
begin
if (k = '') or (k[1] <> '?') then result:=k
else result:=urlvar(copy(k,2,MAXINT));
end; // maybeUrlvar
function compare(op,p1,p2:string):boolean;
var
r1,r2: double;
c: integer;
begin
try
r1:=StrToFloat(p1);
r2:=StrToFloat(p2);
c:=compare_(r1,r2)
except
c:=ansiCompareText(p1,p2);
end;
if op = '=' then result:= c=0
else if op = '>' then result:= c>0
else if op = '<' then result:= c<0
else if op = '>=' then result:= c>=0
else if op = '<=' then result:= c<=0
else if (op = '<>') or (op = '!=') then result:= c<>0
else result:=FALSE;
end; // compare
procedure infixOperators(ops:array of string);
var
i, j: integer;
s: string;
begin
if pars.count > 0 then exit;
for i:=0 to length(ops)-1 do
begin
j:=pos(ops[i], name);
if j = 0 then continue;
s:=trim(chop(j, length(ops[i]), name));
trueIf(compare(ops[i], maybeUrlvar(s), maybeUrlvar(trim(name))));
exit;
end;
end; // infixOperators
procedure call(code:string; ofs:integer=0);
var
i: integer;
begin
result:=code;
if pars.count=0 then
exit;
for i:=ofs to pars.Count-1 do
result:=replaceStr(result, format('$%d',[i-ofs+1]), pars[i]);
for i:=pars.count to pars.count+5 do
result:=replaceStr(result, format('$%d',[i-ofs+1]), '');
end; // call
procedure breadcrumbs();
var
e, d: string;
ae, ad: TstringDynArray;
i: integer;
fld: Tfile;
freeIt: boolean;
begin
freeIt:=FALSE;
if md.f = NIL then
fld:=md.folder
else
begin
fld:=md.f.parent;
if md.f.isTemp() then
begin
e:=extractFilePath(md.f.resource);
if length(e) > 3 then
e:=excludeTrailingPathDelimiter(e);
if e <> fld.resource then
begin
fld:=Tfile.createTemp(e);
fld.node:=md.f.node;
freeIt:=TRUE;
end
end;
end;
if not satisfied(fld) then exit;
e:=htmlEncode(encodeMarkers(fld.url(TRUE)));
d:=htmlEncode(encodeMarkers(fld.getFolder()+fld.name+'/'));
ae:=split('/', e);
ad:=split('/', d);
p:=macroDequote(p);
result:='';
e:='';
i:=length(ae)-1;
if ae[i] = '' then
dec(i);
for i:=parI('from',0) to i do
begin
e:=e+ae[i]+'/';
result:=result+xtpl(p, [
'%bread-url%', e,
'%bread-name%', ad[i],
'%bread-idx%', intToStr(i)
]);
end;
if freeIt then
freeAndNIL(fld);
end; // breadcrumbs
procedure inc_(v:integer=+1);
begin
try
setVar(p, intToStr(strToIntDef(getVar(p),0)+v*parI(1,1)));
result:='';
except
end;
end; // inc_
procedure convert();
var
dst, s: string;
c: ansichar;
begin
dst:=par(1);
s:=par(2);
if sameText(p, 'ansi') and sameText(dst, 'utf-8') then
result:=string(ansiToUTF8(ansistring(s)))
else if sameText(p, 'utf-8') then
if sameText(dst, 'ansi') then
result:=utf8ToAnsi(ansistring(s))
else if dst='dec' then
begin
result:='';
for c in UTF8encode(s) do
result:=result+intToStr(ord(c))+',';
setLength(result, length(result)-1);
end
else if dst='hex' then
begin
result:='';
for c in UTF8encode(s) do
result:=result+intToHex(ord(c));
end;
if isFalse(par('macros')) then
result:=noMacrosAllowed(result);
end; // convert
procedure encodeuri();
var
i: integer;
cs: Tcharset;
begin
result:='';
try cs:=[#0..#255]-strToCharset(parEx('only'));
except
cs:=['a'..'z','A'..'Z','0'..'9',',','/','#','&','?',':','$','@','=','+']
-strToCharset(par('add'))+strToCharset(par('not'));
end;
for i:=1 to length(p) do
if charInSet(p[i], cs) then
result:=result+p[i]
else
result:=result+'%'+intToHex(ord(p[i]),2)
end; // encodeuri
procedure addFolder();
var
parent: Ttreenode;
f, old: Tfile;
fn, name: string;
// extract the path from "name", if any, and assign it to "parent"
function validateAndExtractParent():boolean;
var
i: integer;
parentF: Tfile;
begin
result:=TRUE;
i:=lastDelimiter('/',name);
if i = 0 then exit;
result:=FALSE;
parentf:=mainfrm.findFilebyURL(chop(i+1, 0, name), NIL, FALSE);
if parentf = NIL then exit;
parent:=parentf.node; // ok, this is where we'll add the folder
result:=TRUE;
end; // validateAndExtractParent
begin
result:='';
if not stringExists(p, ['real','virtual']) then exit;
parent:=NIL;
if assigned(md.folder) then
parent:=md.folder.node;
if p = 'virtual' then
begin
name:=par(1);
if not validateAndExtractParent() then exit;
f:=Tfile.createVirtualFolder(name);
end
else
begin
fn:=uri2diskMaybe(par(1));
if not isAbsolutePath(fn) and assigned(md.folder) then
fn:=expandFileName(md.folder.resource+'\'+fn);
if not directoryExists(fn) then exit; // the real folder must exists on disk
// is a name specified in the third parameter, or should we deduce it from the disk path?
name:=par(2);
if (name = '') or containsStr(name,'=') then
name:=extractFileName(fn);
if not validateAndExtractParent() then exit;
f:=Tfile.create(fn);
f.name:=name;
end;
if not validFilename(f.name) then
begin
f.free;
exit;
end;
old:=mainfrm.findFilebyURL(f.name, nodeToFile(parent), FALSE);
if assigned(old) then
if not old.isRoot()
and (not parExist(['overwrite']) or isTrue(par('overwrite'))) then
try old.node.delete() except end // delete existing one
else
begin
f.free;
exit;
end;
if mainfrm.addFile(f, parent, TRUE) = NIL then
f.free
else
spaceIf(TRUE)
end; // addFolder
procedure setItem();
var
f: Tfile;
act: TfileAction;
function get(prefix:string):TStringDynArray;
begin
result:=onlyExistentAccounts(split(';', parEx(prefix+FILEACTION2STR[act])));
uniqueStrings(result);
end;
procedure setAttr(a:TfileAttribute; parName:string);
begin
try
if isTrue(parEx(parname)) then
include(f.flags, a)
else
exclude(f.flags, a);
except end;
end; // setAttr
begin
result:='';
f:=mainfrm.findFileByURL(p, md.folder);
if f = NIL then exit; // doesn't exist
try f.setDynamicComment(macroDequote(parEx('comment'))) except end;
try
f.name:=parEx('name');
if assigned(f.node) then
f.node.text:=f.name;
except end;
try f.resource:=parEx('resource') except end;
try f.diffTpl:=parEx('diff template') except end;
try f.filesFilter:=parEx('files filter') except end;
try f.foldersFilter:=parEx('folders filter') except end;
// following commands make no sense on temporary items
if freeIfTemp(f) then exit;
setAttr(FA_HIDDEN, 'hide');
setAttr(FA_HIDDENTREE, 'hide tree');
setAttr(FA_DONT_LOG, 'no log');
setAttr(FA_ARCHIVABLE, 'archivable');
setAttr(FA_BROWSABLE, 'browsable');
setAttr(FA_DL_FORBIDDEN, 'download forbidden');
if f.isFolder() then
try f.dontCountAsDownloadMask:=parEx('not as download') except end
else
setAttr(FA_DONT_COUNT_AS_DL, 'not as download');
for act:=low(act) to high(act) do
begin
try f.accounts[act]:=get('') except end;
try addUniqueArray(f.accounts[act], get('add ')) except end;
try removeArray(f.accounts[act], get('remove ')) except end;
end;
VFSmodified:=TRUE;
mainfrm.filesBox.repaint();
end; // setItem
function getItemIcon(f:Tfile):string;
begin
if f = NIL then
result:=''
else if (f.icon >= 0) or (mainfrm.useSystemIconsChk.checked and f.isFile()) then
result:='/~img'+intToStr(f.getSystemIcon())
else if f.isFile() then
result:='/~img_file'
else if f.isFolder() then
if FA_UNIT in f.flags then
result:=format('/~img%d', [f.getIconForTreeview()])
else
result:='/~img_folder'
else if f.isLink() then
result:='/~img_link'
else
result:='';
end; // getItemIcon
procedure deleteItem();
var
f: Tfile;
begin
f:=mainfrm.findFileByURL(p);
spaceIf(assigned(f)); // so you can know if something really has been deleted
if f = NIL then exit; // doesn't exist
mainFrm.remove(f.node);
VFSmodified:=TRUE;
end; // deleteItem
procedure getItem();
var
f: Tfile;
act: TfileAction;
w: string;
function getAttr(name:string; a:TfileAttribute):boolean;
begin
result:= w = name;
if result then
trueIf(a in f.flags);
end; // setAttr
begin
result:='';
f:=mainfrm.findFileByURL(p, md.folder);
if f = NIL then exit; // doesn't exist
try
w:=par(1);
if w = 'exists' then
result:='1'
else if w = 'comment' then
result:=f.getDynamicComment()
else if w = 'resource' then
result:=f.resource
else if w = 'icon' then
result:=getItemIcon(f)
else if getAttr('hide', FA_HIDDEN)
or getAttr('hide tree', FA_HIDDENTREE)
or getAttr('no log', FA_DONT_LOG) then
exit
else if w = 'not as download' then
if f.isFolder() then
result:=f.dontCountAsDownloadMask
else
trueIf(FA_DONT_COUNT_AS_DL in f.flags);
for act:=low(act) to high(act) do
if compareText(w, FILEACTION2STR[act]) = 0 then
begin
result:=join(';', f.accounts[act]);
exit;
end;
finally freeIfTemp(f) end;
end; // getItem
procedure foreach();
var
i, e: integer;
s, code: string;
begin
e:=pars.count-2; // 3 parameters minimum (the check is outside)
code:=macroDequote(par(pars.count-1));
with TfastStringAppend.create do
try
for i:=1 to e do
begin
setVar(p, par(i));
s:=code;
applyMacrosAndSymbols(s, cbMacros, cbData);
append(s);
end;
result:=reset();
finally free end;
end; // foreach
procedure forLine();
var
lines: TStringList;
line, code, run: string;
i: integer;
begin
code:=macroDequote(par(pars.count-1));
lines:=TStringList.create();
with TfastStringAppend.create do
try
lines.text:= getVar(par('var'));
for line in lines do
begin
i:=pos('=',line);
if i > 0 then
begin
setVar('line-key', Copy(line, 1, i-1));
setVar('line-value', Copy(line, i+1, MAXINT));
end;
setVar('line', line);
run:=code;
applyMacrosAndSymbols(run, cbMacros, cbData);
append(run);
end;
result:=reset();
finally
Free;
lines.Free;
end;
end; //forLine
procedure for_();
var
b, e, i, d: integer;
s, code: string;
begin
try
b:=strToInt(par(1));
e:=strToInt(par(2));
try
d:=strToInt(par(3));
code:=par(4);
except
d:=1;
code:=par(3);
end;
if d = 0 then exit;
if (e < b) and (d > 0) then d:=-d; // we care