Develop and Download Open Source Software

Browse Subversion Repository

Contents of /WebModuleUnit1.pas

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (show annotations) (download) (as text)
Wed Sep 21 09:33:28 2016 UTC (7 years, 6 months ago) by yamat0jp
File MIME type: text/x-pascal
File size: 19854 byte(s)
Error occurred while calculating annotation data.
悪意あるスクリプトを埋め込まれないように<,>をエンコードして出力するように書き換えました。
そのため<,>は使用できなくなりました。
1 unit WebModuleUnit1;
2
3
4 interface
5
6 uses System.SysUtils, System.Classes, Web.HTTPApp, FireDAC.Stan.Intf,
7 FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS,
8 FireDAC.Phys.Intf, FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt,
9 FireDAC.UI.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Phys,
10 FireDAC.Phys.SQLite, FireDAC.Phys.SQLiteDef, FireDAC.Stan.ExprFuncs, Data.DB,
11 FireDAC.Comp.Client, FireDAC.Comp.DataSet, Web.HTTPProd, Web.DSProd,
12 AnsiStrings, System.DateUtils, System.Types, Vcl.AppEvnts, System.NetEncoding;
13
14 type
15 TWebModule1 = class(TWebModule)
16 FDTable1: TFDTable;
17 FDConnection1: TFDConnection;
18 FDTable2: TFDTable;
19 FDTable2home: TWideMemoField;
20 FDTable2title: TWideMemoField;
21 FDTable2title2: TWideMemoField;
22 FDTable2pass: TWideMemoField;
23 PageProducer1: TPageProducer;
24 DataSetPageProducer2: TDataSetPageProducer;
25 FDTable1name: TWideMemoField;
26 FDTable1no: TIntegerField;
27 FDTable1date: TWideMemoField;
28 FDTable1sub: TWideMemoField;
29 FDTable1com: TWideMemoField;
30 FDTable1pass: TWideMemoField;
31 adminDS: TDataSetPageProducer;
32 FDTable2ngwords: TWideMemoField;
33 FDTable3: TFDTable;
34 FDTable3link: TIntegerField;
35 FDTable3count: TIntegerField;
36 FDTable1line: TIntegerField;
37 FDTable3line: TIntegerField;
38 FDTable2mente: TBooleanField;
39 procedure WebModule1DefaultHandlerAction(Sender: TObject;
40 Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
41 procedure PageProducer1HTMLTag(Sender: TObject; Tag: TTag;
42 const TagString: string; TagParams: TStrings; var ReplaceText: string);
43 procedure WebModule1registAction(Sender: TObject; Request: TWebRequest;
44 Response: TWebResponse; var Handled: Boolean);
45 procedure DataSetPageProducer2HTMLTag(Sender: TObject; Tag: TTag;
46 const TagString: string; TagParams: TStrings; var ReplaceText: string);
47 procedure WebModule1userdelAction(Sender: TObject; Request: TWebRequest;
48 Response: TWebResponse; var Handled: Boolean);
49 procedure WebModule1adminAction(Sender: TObject; Request: TWebRequest;
50 Response: TWebResponse; var Handled: Boolean);
51 procedure WebModule1admindelAction(Sender: TObject; Request: TWebRequest;
52 Response: TWebResponse; var Handled: Boolean);
53 procedure WebModule1loginAction(Sender: TObject; Request: TWebRequest;
54 Response: TWebResponse; var Handled: Boolean);
55 procedure WebModuleBeforeDispatch(Sender: TObject; Request: TWebRequest;
56 Response: TWebResponse; var Handled: Boolean);
57 procedure WebModule1setupAction(Sender: TObject; Request: TWebRequest;
58 Response: TWebResponse; var Handled: Boolean);
59 procedure WebModule1searchAction(Sender: TObject; Request: TWebRequest;
60 Response: TWebResponse; var Handled: Boolean);
61 procedure WebModuleCreate(Sender: TObject);
62 procedure WebModuleDestroy(Sender: TObject);
63 procedure WebModule1adminsetAction(Sender: TObject; Request: TWebRequest;
64 Response: TWebResponse; var Handled: Boolean);
65 private
66 { private ���� }
67 public
68 { public ���� }
69 user: string;
70 time: Int64;
71 words: TStringList;
72 position: integer;
73 uri: string;
74 er: string;
75 foo: string;
76 function footer(const path: string): string;
77 procedure regist(var com: string);
78 end;
79
80 var
81 WebModuleClass: TComponentClass = TWebModule1;
82
83 implementation
84
85 { %CLASSGROUP 'Vcl.Controls.TControl' }
86
87 {$R *.dfm}
88
89 procedure TWebModule1.DataSetPageProducer2HTMLTag(Sender: TObject; Tag: TTag;
90 const TagString: string; TagParams: TStrings; var ReplaceText: string);
91 var
92 s: TURLEncoding;
93 begin
94 ReplaceText := FDTable1.FieldByName(TagString).AsString;
95 if TagString = 'com' then
96 regist(ReplaceText);
97 end;
98
99 function TWebModule1.footer(const path: string): string;
100 var
101 s1, s2, t1, t2: string;
102 home: string;
103 i, k, count: integer;
104 function linkcontent: string;
105 var
106 j, link: integer;
107 begin
108 result := '';
109 link := FDTable3.FieldByName('link').AsInteger;
110 if position > link div 2 then
111 begin
112 if (FDTable1.RecordCount - 1) div count < position + link div 2 then
113 i := FDTable1.RecordCount div count - link - position
114 else
115 i := -(link div 2)
116 end
117 else if position = -1 then
118 i := -position + FDTable1.RecordCount div count - link
119 else
120 i := -position;
121 for j := 0 to link do
122 begin
123 if FDTable1.RecordCount <= (position + i + j) * count then
124 break;
125 if position + i + j < 0 then
126 continue;
127 if i + j = 0 then
128 result := result + ' ' + IntToStr(position + 1) + ' '
129 else
130 result := result + Format(' <a href="' + home + path +
131 '?pos=%d#article">%d</a> ',
132 [position + i + j, position + 1 + i + j]);
133 end;
134 end;
135
136 begin
137 count := FDTable3.FieldByName('count').AsInteger;
138 home := FDTable2.FieldByName('home').AsString;
139 if position <= -1 then
140 begin
141 s1 := '���V%d�����L�����\��<br><center>Pages : [<b>';
142 s2 := '<<' + linkcontent + '>>] ���V</b><br></center>';
143 result := Format(s1, [count]) + s2;
144 end
145 else
146 begin
147 if position = 0 then
148 t1 := '?pos=0'
149 else
150 t1 := '?pos=' + IntToStr(position - 1);
151 i := (FDTable1.RecordCount - 1) div count;
152 if position > i then
153 position := i;
154 if position = i then
155 begin
156 t2 := '?pos=' + position.ToString + '#article';
157 k := FDTable1.RecordCount;
158 end
159 else
160 begin
161 t2 := '?pos=' + IntToStr(position + 1) + '#article';
162 k := (position + 1) * count;
163 end;
164 s1 := '%d ���� %d ������ %d �������\��<br><center>Page : [<b>';
165 s2 := '<a href="' + home + path + t1 + '#article"><<</a>' + linkcontent +
166 '<a href="' + home + path + t2 + '">>></a>] <a href="' + home + path +
167 '#article">���V</a></b></center>';
168 result := Format(s1, [FDTable1.RecordCount, position * count + 1, k]) + s2;
169 end;
170 end;
171
172 procedure TWebModule1.PageProducer1HTMLTag(Sender: TObject; Tag: TTag;
173 const TagString: string; TagParams: TStrings; var ReplaceText: string);
174 var
175 i, j, count: integer;
176 s: TDataSetPageProducer;
177 begin
178 if TagString = 'home' then
179 ReplaceText := FDTable2.FieldByName('home').AsString;
180 if (TagString = 'title') or (TagString = 'title2') then
181 ReplaceText := FDTable2.FieldByName(TagString).AsString;
182 if TagString = 'username' then
183 ReplaceText := user;
184 if TagString = 'main' then
185 begin
186 if FDTable1.RecordCount = 0 then
187 begin
188 if uri = '/index' then
189 ReplaceText := '<hr>�������e������������.<hr>';
190 end
191 else
192 begin
193 count := FDTable3.FieldByName('count').AsInteger;
194 if uri = '/admin' then
195 s := adminDS
196 else
197 s := DataSetPageProducer2;
198 if position < -1 then
199 position := -1;
200 if (position + 1) * count > FDTable1.RecordCount then
201 position := (FDTable1.RecordCount - 1) div count;
202 if position = -1 then
203 begin
204 if FDTable1.RecordCount < count then
205 j := FDTable1.RecordCount mod count
206 else
207 j := count;
208 end
209 else if FDTable1.RecordCount < (position + 1) * count then
210 j := FDTable1.RecordCount - position * count
211 else
212 j := count;
213 if position = -1 then
214 FDTable1.Last
215 else
216 FDTable1.RecNo := (position + 1) * count;
217 for i := 1 to j do
218 begin
219 ReplaceText := ReplaceText + s.Content;
220 FDTable1.Prior;
221 end;
222 ReplaceText := ReplaceText + '<hr>';
223 end;
224 end;
225 if TagString = 'footer' then
226 if TagParams.Values['refer'] = 'true' then
227 ReplaceText := foo
228 else
229 begin
230 foo := footer(uri);
231 ReplaceText := foo;
232 end;
233 if TagString = 'text' then
234 if FDTable1.Filtered = false then
235 ReplaceText := '����������'
236 else
237 ReplaceText := '�Y������ ' + IntToStr(FDTable1.RecordCount) + ' ��' +
238 '<br><#main>';
239 if TagString = 'pass' then
240 ReplaceText := FDTable2.FieldByName('pass').AsString;
241 if TagString = 'content' then
242 ReplaceText := er;
243 if (TagString = 'mente') and (FDTable2.FieldByName('mente').AsBoolean = True)
244 then
245 ReplaceText := 'checked="checked"';
246 end;
247
248 procedure TWebModule1.regist(var com: string);
249 var
250 i: integer;
251 begin
252 i := 1;
253 while True do
254 begin
255 i := Pos(#$D#$A, com, i);
256 if i = 0 then
257 break;
258 Delete(com, i, Length(#$D#$A));
259 Insert('<br>', com, i);
260 inc(i, 4);
261 end;
262 end;
263
264 procedure TWebModule1.WebModule1adminAction(Sender: TObject;
265 Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
266 var
267 t: string;
268 x: Boolean;
269 procedure something;
270 var
271 s: TResourceStream;
272 begin
273 x := false;
274 s := TResourceStream.Create(HInstance, 'admin', RT_RCDATA);
275 try
276 uri := '/admin';
277 Response.ContentType := 'text/html; charset=utf-8;';
278 Response.Content := PageProducer1.ContentFromStream(s)
279 finally
280 s.Free;
281 end;
282 end;
283 procedure addcookie(day: integer; pass: string);
284 begin
285 with Response.Cookies.Add do
286 begin
287 path := '/';
288 Expires := Now + day;
289 Name := 'psw';
290 Value := AnsiString(pass);
291 end;
292 end;
293
294 begin
295 if Request.Query = 'del' then
296 begin
297 addcookie(-3, '');
298 Response.SendRedirect(FDTable2.FieldByName('home').AsAnsiString + '/index');
299 Exit;
300 end;
301 x := True;
302 t := Request.ContentFields.Values['password'];
303 position := StrToIntDef(Request.QueryFields.Values['pos'], -1);
304 if t <> '' then
305 begin
306 addcookie(14, t);
307 if t = FDTable2.FieldByName('pass').AsString then
308 something;
309 end
310 else if Request.CookieFields.Values['psw'] = FDTable2.FieldByName('pass').AsString
311 then
312 something;
313 if x = True then
314 Response.SendRedirect(FDTable2.FieldByName('home').AsAnsiString + '/login');
315 end;
316
317 procedure TWebModule1.WebModule1admindelAction(Sender: TObject;
318 Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
319 var
320 i: integer;
321 begin
322 for i := 0 to Request.ContentFields.count - 1 do
323 if FDTable1.Locate('no', Request.ContentFields.ValueFromIndex[i], []) = True
324 then
325 FDTable1.Delete;
326 Response.SendRedirect(FDTable2.FieldByName('home').AsAnsiString + '/admin');
327 end;
328
329 procedure TWebModule1.WebModule1adminsetAction(Sender: TObject;
330 Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
331 var
332 s: string;
333 x: Boolean;
334 begin
335 s := Request.ContentFields.Values['pass'];
336 if FDTable2.FieldByName('pass').AsString <> s then
337 begin
338 FDTable2.Edit;
339 FDTable2.FieldByName('pass').AsString := s;
340 FDTable2.Post;
341 with Response.Cookies.Add do
342 begin
343 path := AnsiString(FDTable2.FieldByName('home').AsAnsiString + '/admin');
344 Expires := Now + 14;
345 Name := 'psw';
346 Value := AnsiString(s);
347 end;
348 end;
349 x := Request.ContentFields.Values['mente'] = 'on';
350 if FDTable2.FieldByName('mente').AsBoolean <> x then
351 begin
352 FDTable2.Edit;
353 FDTable2.FieldByName('mente').AsBoolean := x;
354 FDTable2.Post;
355 end;
356 Response.SendRedirect(FDTable2.FieldByName('home').AsAnsiString + '/admin');
357 end;
358
359 procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
360 Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
361 var
362 s: TResourceStream;
363 t: TURLEncoding;
364 begin
365 if FDTable2.FieldByName('mente').AsBoolean = True then
366 begin
367 s := TResourceStream.Create(HInstance, 'mente', RT_RCDATA);
368 try
369 uri := '/mente';
370 Response.ContentType := 'text/html; charset=utf-8;';
371 Response.Content := PageProducer1.ContentFromStream(s);
372 finally
373 s.Free;
374 end;
375 Exit;
376 end;
377 position := StrToIntDef(Request.QueryFields.Values['pos'], -1);
378 s := TResourceStream.Create(HInstance, 'index', RT_RCDATA);
379 t := TURLEncoding.Create;
380 try
381 user := t.Decode(Request.CookieFields.Values['UID']);
382 uri := '/index';
383 Response.ContentType := 'text/html; charset=utf-8;';
384 Response.Content := PageProducer1.ContentFromStream(s);
385 finally
386 s.Free;
387 end;
388 end;
389
390 procedure TWebModule1.WebModule1loginAction(Sender: TObject;
391 Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
392 var
393 s: TResourceStream;
394 begin
395 s := TResourceStream.Create(HInstance, 'login', RT_RCDATA);
396 try
397 uri := '/login';
398 Response.ContentType := 'text/html; charset=utf-8;';
399 Response.Content := PageProducer1.ContentFromStream(s);
400 finally
401 s.Free;
402 end;
403 end;
404
405 procedure TWebModule1.WebModule1registAction(Sender: TObject;
406 Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
407 var
408 na, sub, com, pass: string;
409 no, line: integer;
410 i: integer;
411 s: TResourceStream;
412 t: TURLEncoding;
413 begin
414 with Request.ContentFields do
415 begin
416 na := Values['name'];
417 sub := Values['title'];
418 com := Values['comment'];
419 pass := Values['password'];
420 end;
421 er := '';
422 line := 0;
423 if na = '' then
424 na := '�N������';
425 if sub = '' then
426 sub := '�^�C�g������';
427 if com = '' then
428 er := '�{��������������.'#$D#$A
429 else
430 for i := 1 to Length(com) do
431 if Copy(com, i, i + 2) = #$D#$A then
432 inc(line);
433 for i := 0 to words.count - 1 do
434 if ContainsText(AnsiString(com), AnsiString(words[i])) = True then
435 begin
436 er := er + '���~����������������.';
437 break;
438 end;
439 if FDTable1.RecordCount = 0 then
440 no := 1
441 else
442 begin
443 FDTable1.Last;
444 no := FDTable1.FieldByName('no').AsInteger + 1;
445 if SecondsBetween(Now, StrToDateTime(FDTable1.FieldByName('date').AsString))
446 < time then
447 er := '����������������.';
448 end;
449 if er <> '' then
450 begin
451 Response.ContentType := 'text/html; charset=utf-8;';
452 s := TResourceStream.Create(HInstance, 'regist', RT_RCDATA);
453 try
454 Response.Content := PageProducer1.ContentFromStream(s);
455 finally
456 s.Free;
457 end;
458 Exit;
459 end;
460 t := TURLEncoding.Create;
461 try
462 FDTable1.AppendRecord([na, no, DateTimeToStr(Now), sub,
463 ReplaceText(ReplaceText(com, '<', t.Encode('<')), '>', t.Encode('>')),
464 pass, line]);
465 finally
466 t.Free;
467 end;
468 with Response.Cookies.Add do
469 begin
470 path := '/';
471 Name := 'UID';
472 Value := AnsiString(na);
473 Expires := Now + 1;
474 end;
475 Response.SendRedirect(FDTable2.FieldByName('home').AsAnsiString +
476 '/index#article');
477 end;
478
479 procedure TWebModule1.WebModule1searchAction(Sender: TObject;
480 Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
481 var
482 s: TStringList;
483 t1, t2, t3: string;
484 i, j: integer;
485 m: TResourceStream;
486 begin
487 t1 := Request.ContentFields.Values['filter'];
488 t3 := '';
489 s := TStringList.Create;
490 try
491 s.DelimitedText := Request.ContentFields.Values['word1'];
492 for i := 0 to s.count - 1 do
493 begin
494 t2 := Trim(s[i]);
495 if t2 = '' then
496 continue;
497 if t3 <> '' then
498 t3 := t3 + ' and ';
499 t3 := t3 + t1 + ' LIKE ''%' + t2 + '%''';
500 end;
501 finally
502 s.Free;
503 end;
504 if t3 = '' then
505 begin
506 m := TResourceStream.Create(HInstance, 'search', RT_RCDATA);
507 try
508 Response.ContentType := 'text/html; charset=utf-8;';
509 Response.Content := PageProducer1.ContentFromStream(m);
510 finally
511 m.Free;
512 end;
513 Exit;
514 end;
515 FDTable1.Filter := t3;
516 FDTable1.Filtered := True;
517 m := TResourceStream.Create(HInstance, 'search', RT_RCDATA);
518 try
519 j := 0;
520 for i := 1 to FDTable1.RecordCount - 1 do
521 begin
522 inc(j, FDTable1.FieldByName('line').AsInteger);
523 FDTable1.Next;
524 end;
525 PageProducer1.HTMLDoc.LoadFromStream(m);
526 Response.ContentType := 'text/html; charset=utf-8;';
527 if (t3 <> '') and (j > FDTable3.FieldByName('line').AsInteger) then
528 begin
529 t1 := PageProducer1.Content;
530 i := Pos('<#main>', t1);
531 Delete(t1, i, Length('<#main>'));
532 Response.Content := t1 + Format('�\���s����%d�s��������������.',
533 [FDTable3.FieldByName('line').AsInteger]);
534 end
535 else
536 Response.Content := PageProducer1.ContentFromString
537 (PageProducer1.Content);
538 finally
539 FDTable1.Filtered := false;
540 m.Free;
541 end;
542 end;
543
544 procedure TWebModule1.WebModule1setupAction(Sender: TObject;
545 Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
546 var
547 i: integer;
548 s: string;
549 begin
550 FDTable2.Edit;
551 for i := 0 to Request.ContentFields.count - 1 do
552 begin
553 s := Request.ContentFields.Names[i];
554 if s <> 'home' then
555 FDTable2.FieldByName(s).AsString :=
556 Request.ContentFields.ValueFromIndex[i]
557 else
558 FDTable2.FieldByName(s).AsString := Request.ContentFields.Values[s];
559 end;
560 FDTable2.Post;
561 Handled := false;
562 end;
563
564 procedure TWebModule1.WebModule1userdelAction(Sender: TObject;
565 Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
566 var
567 s: string;
568 i: integer;
569 begin
570 i := StrToIntDef(Request.ContentFields.Values['number'], 0);
571 if (i > 0) and (FDTable1.Locate('no', i, []) = True) then
572 begin
573 s := FDTable1.FieldByName('pass').AsString;
574 if (s <> '') and (s = Request.ContentFields.Values['password']) then
575 FDTable1.Delete;
576 end;
577 Handled := false;
578 end;
579
580 procedure TWebModule1.WebModuleBeforeDispatch(Sender: TObject;
581 Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
582 var
583 m: TResourceStream;
584 begin
585 if FDTable1.Exists = false then
586 FDTable1.CreateTable(false, [tpTable]);
587 if FDTable2.Exists = false then
588 begin
589 FDTable2.CreateTable(false, [tpTable]);
590 FDTable2.Active := True;
591 {
592 if (Request.ServerPort = 80) or (Request.ServerPort = 443) then
593 s := 'http://' + String(Request.Host)
594 else
595 s := 'http://' + String(Request.Host) + ':' +
596 IntToStr(Request.ServerPort);
597 }
598 FDTable2.AppendRecord([Request.ScriptName, 'pbbs clone',
599 '<center><font size=5 face=Verdana color=gray><b>P-BBS CLONE</b></font></center>',
600 'admin', '����,����,����', false]);
601 m := TResourceStream.Create(HInstance, 'setup', RT_RCDATA);
602 try
603 uri := '/setup';
604 Response.ContentType := 'text/html; charset=utf-8;';
605 Response.Content := PageProducer1.ContentFromStream(m);
606 finally
607 m.Free;
608 end;
609 Handled := True;
610 end
611 else
612 FDTable2.Active := True;
613 if FDTable3.Exists = false then
614 begin
615 FDTable3.CreateTable(false, [tpTable]);
616 FDTable3.Active := True;
617 FDTable3.AppendRecord([5, 20, 500]);
618 end
619 else
620 FDTable3.Active := True;
621 words.DelimitedText := FDTable2.FieldByName('ngwords').AsString;
622 FDTable1.Active := True;
623 {
624 t:='';
625 ssl:=TStringList.Create;
626 try
627 ssl.Add('/login');
628 ssl.Add('/admin');
629 if (ssl.IndexOf(Request.PathInfo) > -1)and(StartsText('https',FDTable2.FieldByName('home').AsString) = false) then
630 begin
631 s:=FDTable2.FieldByName('home').AsAnsiString;
632 Delete(s,Length(s),1);
633 t:='https'+Copy(s,5,Length(s))+Request.PathInfo;
634 end;
635 finally
636 ssl.Free;
637 end;
638 if t <> '' then
639 begin
640 Response.SendRedirect(t);
641 Exit;
642 end;
643 }
644 end;
645
646 procedure TWebModule1.WebModuleCreate(Sender: TObject);
647 begin
648 FDConnection1.Open('Database=data.sdb;LockingMode=Normal;DriverID=SQLite');
649 time := 1;
650 words := TStringList.Create;
651 end;
652
653 procedure TWebModule1.WebModuleDestroy(Sender: TObject);
654 begin
655 words.Free;
656 end;
657
658 end.
659

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26