-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebUtils.pas
More file actions
768 lines (689 loc) · 25.4 KB
/
webUtils.pas
File metadata and controls
768 lines (689 loc) · 25.4 KB
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
unit webUtils;
{$MODE OBJFPC} {$H+} {$J-}
interface
uses
EventLog;
const
// tanda elemen html
LE = LineEnding; // baris baru asli
Po = '<p>'; // buka paragraf
Pc = '</p>'; // tutup paragraf
BR = '<br/>'; // baris baru
HR = '<hr/>'; // garis horisontal
DIVc = '</div>'; // tutup div
FORMc = '</form>'; // tutup form
TABLEc = '</table>'; // tutup tabel
// nama berkas catatan
LOG_FILE = '/home/cabox/cgi.log';
// alias konstanta sbg pasangan fungsi
paraClose = '</p>';
divClose = '</div>';
formClose = '</form>';
tableClose = '</table>';
var
log : TEventLog; // pencatatan
inputIndex : integer = 0; // penghitung antarmuka masukan
webInputs : string = ''; // data input http
webQueries : string = ''; // data query http
webCookies : string = ''; // data kue http
allWebInput: string = ''; // gabungan seluruh data masukan
webHasInput: boolean = false; // status ketersediaan masukan
type
TArrayOfString = array of string;
// penyusun bagian utama html
procedure writeHeader(const aTitle: string; const loadCSS: string = ''; const loadJS: string = '');
procedure writeFooter(const loadCSS: string = ''; const loadJS: string = '');
procedure writeTitle(const aTitle: string; const aLevel: integer = 1; const aClass: string = '');
procedure writeScript(const aScript: string);
// penyusun elemen html
function spacer(const aClass: string = 'spacer'; const aID: string = ''): string;
function separator(const aClass: string = 'separator'; const aID: string = ''): string;
function block(const aText: string; const aClass: string = ''; const aID: string = ''): string;
function span(const aText: string; const aClass: string = ''; const aID: string = ''): string;
function paraOpen(const aClass: string = ''; const aID: string = ''): string;
function divOpen(const aClass: string = ''; const aID: string = ''): string;
function listOpen(const isOrdered: boolean; const aClass: string = ''; const aID: string = ''): string;
function listItem(const aText: string): string;
function listClose: string;
function tableOpen(const headers: TArrayOfString; const cols: integer = 0; const aClass: string = ''; const aID: string = ''): string;
function tableRow(const cells: TArrayOfString; const cols: integer = 0): string;
// mengubah isian masukan dgn kode javascript
function jsFillValue(const aID: string; const aValue: string): string;
function jsFillValue(const aID: string; const aValue: integer): string;
function jsFillValue(const aID: string; const aValue: double): string;
function jsFillValue(const aID: string; const aValue: boolean): string;
function jsFillMemo(const aID: string; const aValue: string): string;
//function jsFillVar(const aID: string; const aValue: string): string;
//function jsFillOption(const aID: string; const aValue: integer): string;
//function jsFillSelect(const aID: string; const aValue: integer): string;
// penyusun antarmuka masukan
function formOpen(const action: string; const isPost: boolean = false): string;
function putSpace(const aText: string = ''): string;
function putLabel(const aLabel: string; const forID: string = '_'): string;
function putVar(const aKey, aValue: string): string;
function inputVar(const aValue: string): string;
function inputText(const aValue: string = ''): string;
function inputNumber(const aValue: integer = -1): string;
function inputFloat(const aValue: double = -1): string;
function inputBool(const aCaption: string; const aValue: boolean = false): string;
function inputMemo(const aValue: string = ''): string;
function inputOption(const options: TArrayOfString; const checked: integer = -1; const newLine: boolean = false): string;
function inputSelect(const items: TArrayOfString; const selected: integer = -1; const isMultiple: boolean = false): string;
function inputButton(const aCaption: string; const action: string = ''; const isReset: boolean = false): string;
// fungsi untuk membaca data masukan web
function readWebInput: string;
function readWebQuery: string;
function readWebCookie: string;
function readAllWebInput(const includeCookies: boolean = false): string;
// membaca data masukan dan menyusun antarmuka masukan
procedure webRead(var str: string; const newLine: boolean = false);
procedure webRead(var int: integer; const newLine: boolean = false);
procedure webRead(var float: double; const newLine: boolean = false);
procedure webRead(var bool: boolean; const aCaption: string; const newLine: boolean = false);
procedure webReadLn(var str: string);
procedure webReadLn(var int: integer);
procedure webReadLn(var float: double);
procedure webReadLn(var bool: boolean; const aCaption: string);
procedure webReadVar(var aValue: string);
procedure webReadMemo(var str: string; const newLine: boolean = true);
procedure webReadOption(var checked: integer; const options: TArrayOfString; const newLine: boolean = true);
procedure webReadSelect(var selected: integer; const items: TArrayOfString; const newLine: boolean = true);
procedure webReadButton(var clicked: boolean; const aCaption: string; const newLine: boolean = false;
const action: string = ''; const isReset: boolean = false);
// fungsi bantuan untuk baca/tulis data http/html
function createCookie(const aName, aValue: string; const aDuration: integer = 0): string;
function htmlEncode(const aText: string): string;
function httpDecode(const encodedText: string): string;
// fungsi bantuan olah data
function generateGUID: string;
function strings(const arrayOfString: array of string): TArrayOfString;
function switch(const condition: boolean; const ifTrue, ifFalse: string): string;
function hasKey(const aKey, ofString: string; const delimiter: string = '='): boolean;
function getValue(const aKey, ofString: string; const delimiter: string = '&'; const equalizer: string = '='): string;
implementation
uses
SysUtils, StrUtils, DateUtils;
var
start: TDateTime; // untuk awal pewaktuan
isLogged: boolean = false; // pengaturan pencatatan unit
listOrdered: boolean = true; // penanda jenis list html yg sedang dibuat
// membuat guid
function generateGUID: string;
var
guid: TGUID;
begin
repeat sleep(1) until createGUID(guid) = 0;
result := guidToString(guid);
end;
// memudahkan membuat deret string
function strings(const arrayOfString: array of string): TArrayOfString;
var
i: integer;
begin
setLength(result,high(arrayOfString)+1);
for i := 0 to high(arrayOfString) do result[i] := arrayOfString[i];
end;
// uji kondisi 2 teks
function switch(const condition: boolean; const ifTrue, ifFalse: string): string;
begin
if condition then result := ifTrue else result := ifFalse;
end;
// mengambil kata kunci dari teks key=value
function hasKey(const aKey, ofString: string; const delimiter: string = '='): boolean;
begin
result := pos(lowerCase(aKey)+delimiter, lowerCase(ofString)) > 0;
end;
// mengambil nilai dari teks key=value
function getValue(const aKey, ofString: string; const delimiter: string = '&'; const equalizer: string = '='): string;
var
pStart, pStop: integer;
s: string;
begin
s := lowerCase(ofString);
// cari tanda kesamaan
pStart := pos(lowerCase(aKey)+equalizer, s);
if pStart > 0 then
begin
// cari tanda pemisah
pStop := posEx(delimiter, s, pStart);
pStart := pStart + length(aKey) + 1;
// jika tak ada pemisah, ambil sampai akhir
if pStop = 0 then pStop := length(s) + 1;
result := copy(ofString, pStart, pStop-pStart);
end
else result := '';
end;
// menyusun kue http; harus dipanggil sebelum writeHeader()
function createCookie(const aName, aValue: string; const aDuration: integer = 0): string;
var
utc: TDateTime;
begin
result := 'set-cookie: '+aName+'='+aValue+'; SameSite=Strict; HTTPOnly';
if aDuration > 0 then
begin
utc := incMinute(now,GetLocalTimeOffset + aDuration);
result += formatDateTime('; "Expires="ddd, dd mmm yyyy hh:nn:ss "GMT"',utc);
end;
end;
// menerjemahkan teks normal ke teks html (berkode &)
function htmlEncode(const aText: string): string;
begin
result := aText;
// penggantian tanda & harus dilakukan paling awal
result := stringReplace(result,'&','&' ,[rfReplaceAll]);
result := stringReplace(result,'"','"',[rfReplaceAll]);
result := stringReplace(result,'<','<' ,[rfReplaceAll]);
result := stringReplace(result,'>','>' ,[rfReplaceAll]);
end;
// menerjemahkan teks http (berkode %) ke teks normal
function httpDecode(const encodedText: string): string;
var
b: byte;
c: word;
l,p: integer;
s,r,res: string;
begin
// ganti seluruh tanda + menjadi spasi
res := stringReplace(encodedText,'+',' ',[rfReplaceAll]);
l := length(res);
p := 1;
// cari tanda % satu per satu
repeat
p := posEx('%', res, p);
if p > 0 then
begin
s := copy(res, p+1, 2);
val('$'+s,b,c); // % dlm hexa
// ganti angka % menjadi huruf yg sesuai
if c = 0 then
begin
r := chr(b); // hexa ke huruf
res := stringReplace(res,'%'+s,r,[rfIgnoreCase]);
end;
p += 1;
end;
until (p = 0) or (p > l);
result := res;
end;
// menulis kepala html
procedure writeHeader(const aTitle: string; const loadCSS: string = ''; const loadJS: string = '');
begin
start := now; // awal pewaktuan
writeln('content-type: text/html;');
writeln; // penting!
writeln('<!DOCTYPE html>');
writeln('<html><head>');
writeln('<meta charset="utf-8">');
writeln('<meta name="viewport" content="width=device-width,initial-scale=1">');
if loadCSS <> '' then writeln('<link rel="stylesheet" href="',loadCSS,'">');
if loadJS <> '' then writeln('<script src="',loadJS,'"></script>');
writeln('<title>',aTitle,'</title>');
writeln('</head><body>');
writeln('<!-- isi laman mulai dari sini -->');
if isLogged then log.debug('#'+{$I %LINE%}+': [webUtils.writeHeader] done.');
end;
// menulis kaki html
procedure writeFooter(const loadCSS: string = ''; const loadJS: string = '');
var
elapse: integer;
begin
if loadCSS <> '' then writeln('<link rel="stylesheet" href="',loadCSS,'">');
if loadJS <> '' then writeln('<script src="',loadJS,'"></script>');
elapse := trunc(milliSecondSpan(start,now)); // akhir pewaktuan
writeln('<!-- isi laman berhenti di sini -->');
writeln('<p align="right"><small>This page is served in ',elapse,' ms.</small></p>');
writeln('</body></html>');
if isLogged then log.debug('#'+{$I %LINE%}+': [webUtils.writeFooter] done.');
end;
// menulis judul laman
procedure writeTitle(const aTitle:string; const aLevel:integer=1; const aClass:string='');
begin
if aClass <> '' then
writeln('<h',aLevel,' class="',aClass,'">',aTitle,'</h',aLevel,'>')
else
writeln('<h',aLevel,'>',aTitle,'</h',aLevel,'>');
if isLogged then log.debug('#'+{$I %LINE%}+': [webUtils.writeTitle] done.');
end;
// menyisipkan kode javascript ke dokumen html
procedure writeScript(const aScript: string);
begin
writeln('<script>',LE,aScript,'</script>',LE);
end;
// baca data dari masukan baku (std-in)
function readWebInput: string;
var
ch: char;
begin
result := '';
while not EOF(input) do
begin
read(ch);
result += ch;
end;
end;
// baca data query dari variabel sistem
function readWebQuery: string;
begin
result := GetEnvironmentVariable('QUERY_STRING');
end;
// baca data kue dari variabel sistem
function readWebCookie: string;
begin
result := GetEnvironmentVariable('HTTP_COOKIE');
end;
// baca dan gabungkan seluruh data masukan web
function readAllWebInput(const includeCookies: boolean = false): string;
begin
webInputs := readWebInput;
webQueries := readWebQuery;
webHasInput := (webQueries <> '') or (webInputs <> '');
result := switch(webQueries='', switch(webInputs='','',webInputs),
switch(webInputs='', webQueries, webQueries+'&'+webInputs));
if includeCookies then
begin
webCookies := readWebCookie;
result := switch(webCookies='', switch(result='','',result),
switch(result='', webCookies, webCookies+'&'+result));
end;
webHasInput := (result <> '');
end;
// membuat garis pemisah vertical
function spacer(const aClass: string = 'spacer'; const aID: string = ''): string;
begin
result := '<span'+switch(aClass='','',' class="'+aClass+'"')+
switch(aID='','',' id="'+aID+'"')+'> │ </span>';
end;
// membuat garis pemisah horisontal
function separator(const aClass: string = 'separator'; const aID: string = ''): string;
begin
result := '<hr'+switch(aClass='','',' class="'+aClass+'"')+
switch(aID='','',' id="'+aID+'"')+'/>';
end;
// membuat tanda kutipan
function block(const aText: string; const aClass: string = ''; const aID: string = ''): string;
begin
result := '<blockquote'+
switch(aClass='','',' class="'+aClass+'"')+
switch(aID='','',' id="'+aID+'"')+
'>'+aText+'</blockquote>';
end;
// membuat tanda span
function span(const aText: string; const aClass: string = ''; const aID: string = ''): string;
begin
result := '<span'+
switch(aClass='','',' class="'+aClass+'"')+
switch(aID='','',' id="'+aID+'"')+
'>'+aText+'</span>';
end;
// membuat bukaan paragraf
function paraOpen(const aClass: string = ''; const aID: string = ''): string;
begin
result := '<p'+switch(aClass='','',' class="'+aClass+'"')+
switch(aID='','',' id="'+aID+'"')+'>';
end;
// menulis bukaan tanda div
function divOpen(const aClass: string = ''; const aID: string = ''): string;
begin
result := '<div'+switch(aClass='','',' class="'+aClass+'"')+
switch(aID='','',' id="'+aID+'"')+'>';
end;
// membuat bukaan daftar
function listOpen(const isOrdered: boolean; const aClass: string = ''; const aID: string = ''): string;
begin
listOrdered := isOrdered;
result := switch(listOrdered,'<ol','<ul')+
switch(aClass='','',' class="'+aClass+'"')+
switch(aID='','',' id="'+aID+'"')+'>';
end;
// membuat item daftar
function listItem(const aText: string): string;
begin
result := '<li>'+aText+'</li>';
end;
// membuat tutupan daftar
function listClose: string;
begin
result := switch(listOrdered,'</ol>','</ul>');
end;
// membuat bukaan tabel
function tableOpen(const headers: TArrayOfString; const cols: integer = 0;
const aClass: string = ''; const aID: string = ''): string;
var
i: integer;
begin
result := '<table'+
switch(aClass='','',' class="'+aClass+'"')+
switch(aID='','',' id="'+aID+'"')+'>'+LE;
if length(headers) > 0 then
begin
result += '<tr>';
for i := 0 to high(headers) do
if (cols > 0) and (i = high(headers)) then
result += '<th colspan="'+intToStr(cols-high(headers))+'">'+headers[i]+'</th>'
else
result += '<th>'+headers[i]+'</th>';
result += '</tr>';
end;
end;
// membuat baris tabel
function tableRow(const cells: TArrayOfString; const cols: integer = 0): string;
var
i: integer;
begin
result := '';
if length(cells) > 0 then
begin
result := '<tr>';
for i := 0 to high(cells) do
if (cols > 0) and (i = high(cells)) then
result += '<td colspan="'+intToStr(cols-high(cells))+'">'+cells[i]+'</td>'
else
result += '<td>'+cells[i]+'</td>';
result += '</tr>';
end;
end;
// mengubah isian masukan dgn kode javascript [string]
function jsFillValue(const aID: string; const aValue: string): string;
begin
result := 'document.getElementById("'+aID+'").value = "'+aValue+'";'+LE;
end;
// mengubah isian masukan dgn kode javascript [integer]
function jsFillValue(const aID: string; const aValue: integer): string;
begin
result := 'document.getElementById("'+aID+'").value = '+intToStr(aValue)+';'+LE;
end;
// mengubah isian masukan dgn kode javascript [double]
function jsFillValue(const aID: string; const aValue: double): string;
begin
result := 'document.getElementById("'+aID+'").value = '+floatToStr(aValue)+';'+LE;
end;
// mengubah isian masukan dgn kode javascript [boolean]
function jsFillValue(const aID: string; const aValue: boolean): string;
begin
result := 'document.getElementById("'+aID+'").checked = '+lowerCase(boolToStr(aValue,true))+';'+LE;
end;
// mengubah isian masukan dgn kode javascript [memo]
function jsFillMemo(const aID: string; const aValue: string): string;
begin
result := 'document.getElementById("'+aID+'").innerHTML = "'+aValue+'";'+LE;
end;
// membuat bukaan form
function formOpen(const action: string; const isPost: boolean = false): string;
begin
result := '<form action="'+action+'"'+
' id="'+ExtractFilename(ParamStr(0))+'"'+
' method="'+switch(isPost,'post','get')+'">';
end;
// membuat ruang kosong sebelum masukan
function putSpace(const aText: string = ''): string;
begin
result := '<span class="input">'+aText+'</span>';
end;
// membuat teks keterangan masukan
function putLabel(const aLabel: string; const forID: string = '_'): string;
begin
result := '<label class="input"';
if forID = '_' then
result += ' for="input_'+intToStr(inputIndex+1)+'">'
else if forID <> '' then
result += ' for="'+forID+'">'
else
result += '>';
result += aLabel+'</label>';
end;
// membuat masukan tersembunyi dgn nama berbeda
function putVar(const aKey, aValue: string): string;
begin
result := '<input type="hidden" name="'+aKey+'" value="'+aValue+'"/>';
end;
// membuat masukan tersembunyi
function inputVar(const aValue: string): string;
begin
inputIndex := inputIndex+1;
result := '<input type="hidden" name="input_'+intToStr(inputIndex)+'" value="'+aValue+'"/>';
end;
// membuat masukan teks
function inputText(const aValue: string = ''): string;
begin
inputIndex := inputIndex+1;
result := '<input type="text"'+
' id="input_'+intToStr(inputIndex)+'"'+
' name="input_'+intToStr(inputIndex)+'"'+
switch(aValue='','',' value="'+aValue+'"')+
'/>';
end;
// membuat masukan bilangan bulat
function inputNumber(const aValue: integer = -1): string;
begin
inputIndex := inputIndex+1;
result := '<input type="number"'+
' id="input_'+intToStr(inputIndex)+'"'+
' name="input_'+intToStr(inputIndex)+'"'+
switch(intToStr(aValue)='-1','',' value="'+intToStr(aValue)+'"')+
'/>';
end;
// membuat masukan bilangan pecahan
function inputFloat(const aValue: double = -1): string;
begin
inputIndex := inputIndex+1;
result := '<input type="number"'+
' id="input_'+intToStr(inputIndex)+'"'+
' name="input_'+intToStr(inputIndex)+'"'+
switch(floatToStr(aValue)='-1','',' value="'+floatToStr(aValue)+'"')+
' step="any"/>';
end;
// membuat masukan logika
function inputBool(const aCaption: string; const aValue: boolean = false): string;
begin
inputIndex := inputIndex+1;
result := '<label><input type="checkbox"'+
' id="input_'+intToStr(inputIndex)+'"'+
' name="input_'+intToStr(inputIndex)+'"'+
' value="true"'+
switch(aValue,' checked','')+
'/> '+aCaption+' </label>';
end;
// membuat masukan opsi
function inputOption(const options: TArrayOfString; const checked: integer = -1;
const newLine: boolean = false): string;
var
i: integer;
begin
inputIndex := inputIndex+1;
result := '';
for i := 0 to high(options) do
begin
result += '<label>'+
'<input type="radio"'+
' id="input_'+intToStr(inputIndex)+'"'+
' name="input_'+intToStr(inputIndex)+'"'+
' value="option_'+intToStr(i)+'"'+
switch(i=checked,' checked','')+
'/> '+options[i]+' </label>';
if i < high(options) then
if newLine then
result += BR+LE+'<span class="input"></span>'
else
result += LE+' <font color="lightgray"> │ </font>';
end;
end;
// membuat masukan pilihan
function inputSelect(const items: TArrayOfString; const selected: integer = -1;
const isMultiple: boolean = false): string;
var
i: integer;
begin
inputIndex := inputIndex+1;
result := '<select'+
' id="input_'+intToStr(inputIndex)+'"'+
' name="input_'+intToStr(inputIndex)+'"'+
switch(isMultiple,' multiple>','>')+LE;
for i := 0 to high(items) do
result += '<option value="item_'+intToStr(i)+'"'+
switch(i=selected,' selected','')+'>'+
items[i]+'</option>'+LE;
result += '</select>';
end;
// membuat masukan memo
function inputMemo(const aValue: string = ''): string;
begin
inputIndex := inputIndex+1;
result := '<textarea'+
' id="input_'+intToStr(inputIndex)+'"'+
' name="input_'+intToStr(inputIndex)+'">'+
aValue+'</textarea>';
end;
// membuat masukan tombol
function inputButton(const aCaption: string; const action: string = ''; const isReset: boolean = false): string;
begin
inputIndex := inputIndex+1;
result := '<button'+
' type="'+switch(isReset,'reset','submit')+'"'+
' id="input_'+intToStr(inputIndex)+'"'+
' name="input_'+intToStr(inputIndex)+'"'+
switch(action='','',' formaction="'+action+'"')+
' value="clicked">'+aCaption+
'</button>';
end;
// gabungkan antarmuka masukan dan pembacaan data masukan [string]
procedure webRead(var str: string; const newLine: boolean = false);
var
s: string;
begin
// baca data masukan
s := getValue('input_'+intToStr(inputIndex+1),allWebInput);
if s <> '' then str := s;
// tampilan antarmuka masukan
write(inputText(htmlEncode(httpDecode(str))));
if newLine then writeln(BR) else writeln;
end;
// gabungkan antarmuka masukan dan pembacaan data masukan [integer]
procedure webRead(var int: integer; const newLine: boolean = false);
var
s: string;
begin
// baca data masukan
s := getValue('input_'+intToStr(inputIndex+1),allWebInput);
if s <> '' then int := strToInt(s);
// tampilan antarmuka masukan
write(inputNumber(int));
if newLine then writeln(BR) else writeln;
end;
// gabungkan antarmuka masukan dan pembacaan data masukan [float]
procedure webRead(var float: double; const newLine: boolean = false);
var
s: string;
begin
// baca data masukan
s := getValue('input_'+intToStr(inputIndex+1),allWebInput);
if s <> '' then float := strToFloat(s);
// tampilan antarmuka masukan
write(inputFloat(float));
if newLine then writeln(BR) else writeln;
end;
// gabungkan antarmuka masukan dan pembacaan data masukan [boolean]
procedure webRead(var bool: boolean; const aCaption: string; const newLine: boolean = false);
var
s: string;
begin
// baca data masukan
s := getValue('input_'+intToStr(inputIndex+1),allWebInput);
if webHasInput then bool := (s = 'true');
// tampilan antarmuka masukan
write(inputBool(aCaption,bool));
if newLine then writeln(BR) else writeln;
end;
// akhiran ln untuk membuat baris baru setelah antarmuka masukan
procedure webReadLn(var str: string);
begin
webRead(str,true);
end;
procedure webReadLn(var int: integer);
begin
webRead(int,true);
end;
procedure webReadLn(var bool: boolean; const aCaption: string);
begin
webRead(bool,aCaption,true);
end;
procedure webReadLn(var float: double);
begin
webRead(float,true);
end;
// membaca data masukan variabel html
procedure webReadVar(var aValue: string);
var
s: string;
begin
s := getValue('input_'+intToStr(inputIndex+1),allWebInput);
if s <> '' then aValue := s;
writeln(inputVar(htmlEncode(httpDecode(aValue))));
end;
// gabungkan antarmuka masukan dan pembacaan data masukan [memo]
procedure webReadMemo(var str: string; const newLine: boolean = true);
var
s: string;
begin
s := getValue('input_'+intToStr(inputIndex+1),allWebInput);
if s <> '' then str := s;
write(inputMemo(htmlEncode(httpDecode(str))));
if newLine then writeln(BR) else writeln;
end;
// gabungkan antarmuka masukan dan pembacaan data masukan [radio]
procedure webReadOption(var checked: integer; const options: TArrayOfString; const newLine: boolean = true);
var
s: string;
p: integer;
begin
s := getValue('input_'+intToStr(inputIndex+1),allWebInput);
if s <> '' then
begin
p := pos('_',s);
if p > 0 then checked := strToInt(copy(s,p+1,length(s)));
end;
write(inputOption(options,checked,newLine));
if newLine then writeln(BR) else writeln;
end;
// gabungkan antarmuka masukan dan pembacaan data masukan [combo]
procedure webReadSelect(var selected: integer; const items: TArrayOfString; const newLine: boolean = true);
var
s: string;
p: integer;
begin
s := getValue('input_'+intToStr(inputIndex+1),allWebInput);
if s <> '' then
begin
p := pos('_',s);
if p > 0 then selected := strToInt(copy(s,p+1,length(s)));
end;
write(inputSelect(items,selected));
if newLine then writeln(BR) else writeln;
end;
// gabungkan antarmuka masukan dan pembacaan data masukan [button]
procedure webReadButton(var clicked: boolean; const aCaption: string; const newLine: boolean = false;
const action: string = ''; const isReset: boolean = false);
begin
clicked := (getValue('input_'+intToStr(inputIndex+1),allWebInput) = 'clicked');
write(inputButton(aCaption,action,isReset));
if newLine then writeln(BR) else writeln;
end;
(*** awalan dan akhiran unit ***)
initialization
// memastikan berkas catatan tersedia
if fileExists(LOG_FILE) then
begin
log := TEventLog.Create(nil);
log.logType := ltFile;
log.fileName := LOG_FILE;
log.identification := ExtractFilename(ParamStr(0))+':';
log.appendContent := true;
log.active := true;
log.debug('#'+{$I %LINE%}+': [webUtils.initialization] done.');
end;
finalization
if fileExists(LOG_FILE) then
begin
log.debug('#'+{$I %LINE%}+': [webUtils.finalization] done.');
log.free;
end;
end.