This repository has been archived by the owner on May 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
common.pas
255 lines (225 loc) · 4.85 KB
/
common.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
unit common;
interface
uses
SysUtils,Windows;
const
dictfile = 'dict.txt';
tempFile = 'temp.txt';
backupFile = 'dict.bak';
showTemplateFile = 'show.txt';
addTemplateFile = 'add.txt';
indexTemplateFile = 'index.txt';
maintTemplateFile = 'maint.txt';
userFile = 'user.txt';
CGIPost : boolean = false;
QueryString : string = '';
function getenv(s:string):string;
function Replace(s,src,dst:string):string;
function HexToInt(s:string; var value:integer):boolean;
function GetParse(s:string; parsechar:char; index:integer):string;
function GetParseCount(s:string; parsechar:char):integer;
function Translate(s,src,dst:string):string;
function getValue(s:string):string;
function isValue(s:string):boolean;
function GetUserPass(username:string):string;
procedure debug(s:string);
procedure normalizeQueryString(var s:string);
procedure normalizeword(var s:string);
procedure outFile(afile:string);
implementation
procedure outFile;
var
s:string;
F:Textfile;
begin
Assign(F,afile);
Reset(F);
while not Eof(F) do begin
readlN(f,s);
writeln(s);
end;
CloseFile(F);
end;
procedure Init;
var
si:TStartupInfo;
len:longword;
begin
QueryString := getenv('QUERY_STRING');
if QueryString = '' then begin
len := StrToIntDef(getenv('CONTENT_LENGTH'),0);
if len > 0 then begin
GetStartupInfo(si);
setlength(QueryString,len);
CGIPost := ReadFile(si.hStdInput,QueryString[1],len,len,NIL);
end;
end;
normalizeQueryString(QueryString);
writeln('Content-type: text/html'#13#10);
end;
function getValue;
var
n:integer;
sub:string;
begin
s := LowerCase(s);
Result := '';
for n:=1 to GetParseCount(QueryString,'&') do begin
sub := getParse(QueryString,'&',n);
if LowerCase(GetParse(sub,'=',1)) = s then begin
Result := GetParse(sub,'=',2);
exit;
end;
end;
end;
function isValue;
var
n:integer;
sub:string;
begin
s := LowerCase(s);
Result := false;
for n:=1 to GetParseCount(QueryString,'&') do begin
sub := getParse(QueryString,'&',n);
if LowerCase(GetParse(sub,'&',1)) = s then begin
Result := true;
exit;
end;
end;
end;
function GetUserPass;
var
F:TextFile;
s:string;
begin
AssignFile(F,userFile);
Reset(F);
if IOResult = 0 then while not Eof(F) do begin
Readln(F,s);
if s <> '' then if s[1] = '~' then begin
if Trim(copy(s,2,length(s))) = username then begin
Readln(F,s);
Result := Trim(s);
CloseFile(F);
exit;
end;
end;
end;
CloseFile(F);
Result := '';
end;
procedure normalizeword(var s:string);
var
n:integer;
begin
s := Translate(s,'$ðüþiöçÐÜÞÝÖÇ','sgusiocGUSiOC');
for n:=1 to length(s) do
if not (s[n] in ['0'..'9','A'..'Z','a'..'z']) then s[n] := ' ';
s := Lowercase(Trim(s));
end;
procedure normalizeQueryString(var s:string);
var
n:integer;
b:integer;
begin
repeat
n := pos('%',s);
if n = 0 then break;
if HexToInt(copy(s,n+1,2),b) then begin
Delete(s,n,3);
Insert(char(b),s,n);
end else s[n] := #255;
until false;
s := Replace(s,'<','<');
s := Replace(s,'>','>');
s := Translate(LowerCase(Trim(s)),'+'#255,' %');
end;
function Translate;
var
n,b:integer;
begin
for n:=1 to length(s) do begin
b := pos(s[n],src);
if b > 0 then s[n] := dst[b];
end;
Result := s;
end;
function GetParseCount(s:string; parsechar:char):integer;
var
n:integer;
begin
Result := 1;
for n:=1 to length(s) do if s[n]=parsechar then inc(Result);
end;
function GetParse(s:string; parsechar:char; index:integer):string;
var
n,count:integer;
begin
count := 0;
Result := '';
repeat
n := pos(parsechar,s);
if n > 0 then begin
inc(count);
if count=index then begin
Result := Trim(copy(s,1,n-1));
exit;
end;
s := copy(s,n+1,length(s));
if s = '' then exit;
end;
until n=0;
if count+1 = index then Result := Trim(s);
end;
function HexToInt;
var
n,t:integer;
c:char;
begin
value := 0;
t := 0;
for n:=length(s) downto 1 do begin
c := upcase(s[n]);
case c of
'0'..'9' : t := byte(c)-48;
'A'..'F' : t := (byte(c)-65)+10;
else begin
Result := false;
exit;
end;
end; {case}
value := value or (t shl ((length(s)-n)*4));
end;
Result := true;
end;
procedure debug(s:string);
begin
writeln(s+'<br>');
flush(output);
end;
function Replace(s,src,dst:string):string;
var
b:byte;
begin
repeat
b := pos(src,s);
if b > 0 then begin
Delete(s,b,length(src));
if dst <> '' then Insert(dst,s,b);
end;
until b = 0;
Result := s;
end;
function getenv(s:string):string;
var
ar:array[1..32768] of char;
begin
FillChar(ar,SizeOf(ar),0);
GetEnvironmentVariable(PChar(s),@ar,SizeOf(ar));
Result := Trim(StrPas(@ar));
end;
initialization
begin
Init;
end;
end.