unit Unit2;

interface

uses
  SysUtils, Classes, HTTPApp, StrUtils, HTTPProd, DSProd, OleServer, ZConnection, ZAbstractRODataset,
  ZAbstractDataset, ZAbstractTable, ZSqlMonitor, DB, ZDataset, DBTables;

type
  TWebModule2 = class(TWebModule)
    DataSetPageProducer1: TDataSetPageProducer;
    PageProducer2: TPageProducer;
    DataSetPageProducer2: TDataSetPageProducer;
    PageProducer1: TPageProducer;
    PageProducer3: TPageProducer;
    stopitems: TPageProducer;
    ZTable1: TZTable;
    ZConnection1: TZConnection;
    ZTable2: TZTable;
    ZTable3: TZTable;
    ZConnection2: TZConnection;
    Log: TZTable;
    ZQuery1: TZQuery;
    ZQuery2: TZQuery;
    PageProducer4: TPageProducer;
    Session1: TSession;
    procedure WebModuleCreate(Sender: TObject);
    procedure WebModuleDestroy(Sender: TObject);
    procedure WebModuleBeforeDispatch(Sender: TObject;
      Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
    procedure WebModule2indexAction(Sender: TObject;
      Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
    procedure WebModuleAfterDispatch(Sender: TObject; Request: TWebRequest;
      Response: TWebResponse; var Handled: Boolean);
    procedure PageProducer2HTMLTag(Sender: TObject; Tag: TTag;
      const TagString: String; TagParams: TStrings;
      var ReplaceText: String);
    procedure WebModule2RegistAction(Sender: TObject;
      Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
    procedure DataSetPageProducer1HTMLTag(Sender: TObject; Tag: TTag;
      const TagString: String; TagParams: TStrings;
      var ReplaceText: String);
    procedure WebModule2UsrdelAction(Sender: TObject;
      Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
    procedure PageProducer1HTMLTag(Sender: TObject; Tag: TTag;
      const TagString: String; TagParams: TStrings;
      var ReplaceText: String);
    procedure PageProducer3HTMLTag(Sender: TObject; Tag: TTag;
      const TagString: String; TagParams: TStrings;
      var ReplaceText: String);
    procedure WebModule2admdelAction(Sender: TObject; Request: TWebRequest;
      Response: TWebResponse; var Handled: Boolean);
    procedure WebModule2errorAction(Sender: TObject; Request: TWebRequest;
      Response: TWebResponse; var Handled: Boolean);                                                                        
    procedure WebModule2viewAction(Sender: TObject; Request: TWebRequest;
      Response: TWebResponse; var Handled: Boolean);
    procedure WebModule2startAction(Sender: TObject; Request: TWebRequest;
      Response: TWebResponse; var Handled: Boolean);
    procedure stopitemsHTMLTag(Sender: TObject; Tag: TTag;
      const TagString: String; TagParams: TStrings;
      var ReplaceText: String);
    procedure WebModule2stopAction(Sender: TObject; Request: TWebRequest;
      Response: TWebResponse; var Handled: Boolean);
    procedure WebModule2adminAction(Sender: TObject; Request: TWebRequest;
      Response: TWebResponse; var Handled: Boolean);
    procedure PageProducer4HTMLTag(Sender: TObject; Tag: TTag;
      const TagString: String; TagParams: TStrings;
      var ReplaceText: String);
  private
    { Private declarations }
    head, body, foot, page: string;
    pos: integer;
    htmlw: Boolean;
    html: TStringList;
    r_name: string;
    home: string;
    page_def: integer;
    maxn, maxs, maxv, maxw: integer;
    maxline: integer;
    w_regist: Int64;
    autolink, tag: Boolean;
    mudai: string;
    re_color: string;
    no_hosts, no_words: TStringList;
    l_count: integer;
    admin_log: Boolean;
    loginlist: TStringList;
    function LinkContent(path: string): string;
    function footer(path: string): string;
    function contentstring(dataset: TDataSetPageProducer): string;
    procedure createview;
  public
    { Public declarations }
  end;

const
    GAIBU: Boolean = true;

var
  WebModule2: TWebModule2;

implementation

uses WebReq, DateUtils;

{$R *.DFM}

procedure TWebModule2.WebModuleCreate(Sender: TObject);
begin
//ここで初期化するとデバッグモードと設置段階とで挙動が異なるので好ましくない
//ローカル変数以外宣言してはいけない
//* <body>タグ */
        body := '<body bgcolor="#ddf2ed" text="#444444" link="#0000AA">';
//* 一ページあたりの表示記事数 */
        page_def := 10;

        l_count:=5;
        pos:=-1;
//* 文字数制限（名前、題名、本文）全角だとこの半分です */
        maxn  := 40;
        maxs  := 40;
        maxv  := 1500;

        maxw:=160;
//* 本文の改行数制限 */
        maxline := 25;
//* 同一ホストからの連続投稿を制限
//  --> 秒数を記述するとその時間以上を経過しないと連続投稿できない*/
        w_regist := 5;
//* 文中で自動リンクするかどうか（yes=1 no=0）*/
        autolink := true;
//* HTMLタグを有効にするか（yes=1 no=0)*/
        tag := true;
//* タイトル無しで投稿された場合 */
        mudai := '(無題)';
//* ＞がついた時の色 */
        re_color := '#225588';


        htmlw:=true;
//* 外部書き込み禁止にする?(する=1,しない=0) */
//define("GAIBU", 0);

        admin_log:=false;
        no_hosts:=TStringList.Create;
        no_words:=TStringList.Create;
        loginlist:=TStringList.Create;
        loginlist.CommaText:='/admin2,/admdel,/stop';
        html:=TStringList.Create;
        ZTable1.FilterOptions:=[foCaseInSensitive];
end;

procedure TWebModule2.WebModuleDestroy(Sender: TObject);
begin
        no_hosts.Free;
        no_words.Free;
        loginlist.Free;
        html.Free;
end;

procedure TWebModule2.WebModuleBeforeDispatch(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  i: integer;
  s, t: string;
begin
  with ZQuery1 do
  begin
        ExecSQL;
        SQL.Clear;
        SQL.Add('CREATE TABLE if not exists pass(week integer, pass text);');
        ExecSQL;
        ZTable3.Active:=true;
  if ZTable3.IsEmpty = true then
  begin
    for i:=1 to 7 do
    begin
        ZTable3.AppendRecord([i,'full']);
    end;
  end;
        SQL.Clear;
        SQL.Add('CREATE TABLE if not exists information(home text, title1 text, title2 text,');
        SQL.Add(' no_word text, no_host text);');
        ExecSQL;
        SQL.Clear;
        SQL.Add('SELECT home, title1, no_host FROM information;');
        Active:=true;
        t:='http://'+Request.Host+Request.InternalScriptName+'/';
  if IsEmpty = true then
  begin
    if Request.InternalPathInfo <> '/start' then
    begin
        Response.Content:='<form method="POST" action="'+t+'start">home : '+t+
          '<br>title1 : <input type=text name="title1" value="P-BBS" /><br>title2 : <textarea name="title2" col=5 row=10>'+
          '<font size=5 face=Verdana color=gray><b>P-BBS</b></font></textarea><br><input type=submit value="決定" /></form>';
        Handled:=true;
        Exit;
    end;
        s:='';
  end else
  begin
        home:=FieldByName('home').AsString;
        s:=FieldByName('title1').AsString;
        no_hosts.CommaText:=FieldByName('no_host').AsString;
  end;
        Active:=false;
  end;
        ZTable1.Active:=true;
        head:='<html><head><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=Shift_JIS"><title>'+s+'</title></head>';
        foot:='[ <a href='+home+'>ホーム</a> ] [ <a href='+home+'admin>管理</a> ]'+
          '<br><br><small><!-- P-BBS v1.232 -->- <a href="http://php.s3.to" target="_top">P-BBS</a> -</small></body></html>';
  if loginlist.IndexOf(Request.InternalPathInfo) = -1 then
  begin

    if Request.CookieFields.Values['admin_pass'] <> '' then
    begin
      if admin_log = true then
      begin
                ZQuery2.ExecSQL;
                Log.Active:=true;
                Log.AppendRecord(['LogOut',DateTimeToStr(Now)]);
                Log.Active:=false;
      end;
      with Response.Cookies.Add do
      begin
        Name:='admin_pass';
        Value:='';
//        Secure:=true;
      end;
    end;

  end else
        if t = home then
  begin
        t:=Request.CookieFields.Values['admin_pass'];
    if (t = '')or(ZTable3.Lookup('week',DayOfWeek(Now),'pass') <> t) then
    begin
        Response.Content:='ログインしてください';
        Handled:=true;
    end;
  end else
  begin
        Response.Content:='不正アクセス information テーブルのhomeカラムを見直してください';
        handled:=true;
  end;
  for i:=0 to no_hosts.Count-1 do
  begin
    if AnsiContainsText(t,no_hosts[i]) = true then
    begin
        Response.StatusCode:=204;
        Handled:=true;
        break;
    end;
  end;
  if Request.InternalPathInfo <> 'search' then
  begin
        ZTable1.Filtered:=false;
        ZTable1.SortedFields:='';
  end;
end;

procedure TWebModule2.WebModule2indexAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
        pos:=StrToIntDef(Request.QueryFields.Values['pos'],-1);
  if (htmlw = true)and(pos = -1) then
  begin
    if FileExists('index.html') = false then
    begin
        html.Text:=PageProducer2.Content;
        html.SaveToFile('index.html');
    end else
    begin
        html.LoadFromFile('index.html');
    end;
        Response.Content:=html.Text;
  end else
  begin
        r_name:=Request.CookieFields.Values['name'];
        Response.Content:=PageProducer2.Content;
  end;
end;

procedure TWebModule2.WebModuleAfterDispatch(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
        ZTable1.Active:=false;
        ZTable3.Active:=false;
end;

procedure TWebModule2.PageProducer2HTMLTag(Sender: TObject; Tag: TTag;
  const TagString: String; TagParams: TStrings; var ReplaceText: String);
begin
  if TagString = 'page_def' then
  begin
        ReplaceText:=IntToStr(page_def);
  end;
  if TagString = 'home' then
  begin
        ReplaceText:=home;
  end;
  if TagString = 'main' then
  begin
        ReplaceText:=contentstring(DataSetPageProducer2);
  end;
  if TagString = 'title2' then
  begin
    with ZQuery1 do
    begin
        Active:=false;
        SQL.Clear;
        SQL.Add('SELECT title2 FROM information;');
        Active:=true;
        ReplaceText:=FieldByName('title2').AsString;
    end;
  end;
  if TagString = 'r_name' then
  begin
        ReplaceText:=r_name;
  end;
  if TagString = 'foot' then
  begin
        ReplaceText:=footer('');
  end;
end;

procedure TWebModule2.WebModule2RegistAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  i: integer;
  s: string;
  l_name, pass, sub, com: string;
  x: TDateTime;
  y: Int64;
  procedure linecount;
  var
    j: integer;
    t: WideString;
    temp: TStringList;
  begin
        temp:=TStringList.Create;
    try
        temp.Text:=com;
      if temp.Count > maxline then
      begin
                s:=s+'行数が長すぎます<br>';
      end else
      begin
                com:='';
        for j:=0 to temp.Count-1 do
        begin
                t:=temp[j];
          while Length(t) > maxw do
          begin
                com:=com+'<br>'+Copy(t,1,maxw);
                Delete(t,1,maxw);
          end;
                com:=com+'<br>'+t;
        end;
      end;
    finally
        temp.Free;
    end;
  end;
  procedure cookie;
  begin
    with Response.Cookies.Add do
    begin
        Name:='name';
        Value:=Request.Authorization;
    if Value = '' then
    begin
        Value:=Request.ContentFields.Values['name'];
    end;
        Expires:=Now+20;
    end;
  end;
begin
        s:='';
  if Request.Method <> 'POST' then
  begin
        s:=s+'不正な投稿をしないでください<br>';
  end;
  if (GAIBU = false)and(AnsiContainsStr('http://'+Request.Host+Request.InternalScriptName+Request.InternalPathInfo,home) = false) then
  begin
        s:=s+'外部から書き込みできません<br>';
  end;
        l_name:=Request.ContentFields.Values['name'];
        sub:=Request.ContentFields.Values['sub'];
        com:=Request.ContentFields.Values['com'];
        pass:=Request.ContentFields.Values['password'];
  if l_name = '' then
  begin
        l_name:='no name';
  end else
        if Length(name) > maxn then
  begin
        s:=s+'名前が長すぎます<br>';
  end;
  if sub = '' then
  begin
        sub:=mudai;
  end else
        if Length(sub) > maxs then
  begin
        s:=s+'タイトルが長すぎます<br>';
  end;
  if com = '' then
  begin
        s:=s+'本文が書き込まれていません<br>';
  end else
        if Length(com) > maxv then
  begin
        s:=s+'本文が長すぎます<br>';
  end;
  with ZQuery1 do
  begin
        Active:=false;
        SQL.Clear;
        SQL.Add('SELECT no_word FROM information;');
        Active:=true;
        no_words.CommaText:=FieldByName('no_word').AsString;
  end;
  for i:=0 to no_words.Count-1 do
  begin
    if (AnsiContainsText(com,no_words[i]) = true)or(AnsiContainsText(sub,no_words[i]) = true)or
      (AnsiContainsText(l_name,no_words[i]) = true) then
    begin
        s:=s+'使用できない言葉が含まれています<br>';
    end;
  end;
        ZTable1.Last;
        x:=Now;
        y:=SecondsBetween(x,ZTable1.FieldByName('date').AsFloat);
  if (ZTable1.FieldByName('com').AsString = com)and
    (ZTable1.FieldByName('name').AsString = name) then
  begin
        Response.SendRedirect(home);
        Exit;
  end;
  if y < w_regist then
  begin
        s:=s+'混み合っています　残り'+IntToStr(w_regist-y)+'秒<br>';
  end;
        linecount;
        cookie;
  if s <> '' then
  begin
        Response.Content:=head+s+'ブラウザの戻るで移動してください。</body></html>';
  end else
  begin
    if Request.ContentFields.Values['property'] = 'view' then
    begin
        createview;
      with ZTable2 do
      begin
        TableName:='view';
        Active:=true;
      if IsEmpty = true then
      begin
                i:=1;
      end else
      begin
                i:=FieldByName('no').AsInteger+1;
      end;
        AppendRecord([pass,i,Now,l_name,sub,com]);
        Active:=false;
      end;
        Response.Content:='<table border=1><tr><td>名前</td><td>'+l_name+'</td></tr><tr><td>タイトル</td><td>'+
          sub+'</td></tr><tr><td>コメント</td><td>'+com+'</td></tr></table><form method=post action='+home+
          'view?'+IntToStr(i)+'><input type=submit value="決定">書き直す場合はブラウザの戻る';
        Exit;
    end;
    with ZTable1 do
    begin
        Last;
    if IsEmpty = true then
    begin
        i:=0
    end else
    begin
        i:=FieldByName('no').AsInteger+1;
    end;
        AppendRecord([pass,i,Now,l_name,sub,com]);
    end;
        pos:=-1;
    if htmlw = true then
    begin
        html.Text:=PageProducer2.Content;
        html.SaveToFile('index.html');
    end;
        Response.SendRedirect(home);
  end;
end;

procedure TWebModule2.DataSetPageProducer1HTMLTag(Sender: TObject;
  Tag: TTag; const TagString: String; TagParams: TStrings;
  var ReplaceText: String);
begin
  if TagString = 'home' then
  begin
        ReplaceText:=home;
  end else
        if TagString = 'now' then
  begin
        ReplaceText:=DateTimeToStr(ZTable1.FieldByName('date').AsFloat);
  end else
  begin
        ReplaceText:=ZTable1.FieldByName(TagString).AsString;
  end;
end;

procedure TWebModule2.WebModule2UsrdelAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  s, t: string;
  no, pwd: string;
begin
        no:=Request.ContentFields.Values['no'];
        pwd:=Request.ContentFields.Values['pwd'];
  if (no = '')or(pwd = '') then
  begin
        t:='削除noまたは削除キーが入力もれです';
  end;
  if ZTable1.Locate('no',no,[]) = false then
  begin
        t:='該当記事が見当たりません';
  end else
  begin
        s:=ZTable1.FieldByName('pwd').AsString;
    if s = '' then
    begin
        t:='該当記事には削除キーが設定されていません)';
    end else
        if s <> pwd then
    begin
        t:='削除キーが違います';
    end else
    begin
        ZTable1.Delete;
    end;
  end;
  if t = '' then
  begin
        Response.SendRedirect(home);
  end else
  begin
        Response.Content:=head+t+'</body></html>';
  end;
end;

procedure TWebModule2.PageProducer1HTMLTag(Sender: TObject; Tag: TTag;
  const TagString: String; TagParams: TStrings; var ReplaceText: String);
begin
  if TagString = 'home' then
  begin
        ReplaceText:=home;
  end;
  if TagString = 'main' then
  begin
        pos:=StrToIntDef(Request.QueryFields.Values['pos'],-1);
        ReplaceText:=contentstring(DataSetPageProducer1);
  end;
  if TagString = 'footer' then
  begin
        ReplaceText:=footer('admin2');
  end;
  if TagString = 'pos' then
  begin
        ReplaceText:=Request.QueryFields.Values['pos'];
  end;
end;

procedure TWebModule2.PageProducer3HTMLTag(Sender: TObject; Tag: TTag;
  const TagString: String; TagParams: TStrings; var ReplaceText: String);
begin
  if TagString = 'home' then
  begin
        ReplaceText:=home;
  end;
end;

procedure TWebModule2.WebModule2admdelAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  i, j: integer;
  s: string;
begin
  for i:=0 to Request.ContentFields.Count-1 do
  begin
        s:=Request.ContentFields.Values[Request.ContentFields.Names[i]];
    while ZTable1.Locate('no',StrToInt(s),[]) = true do
    begin
        ZTable1.Delete;
    end;
  end;
  if htmlw = true then
  begin
        j:=pos;
        pos:=-1;
        html.Text:=PageProducer2.Content;
        html.SaveToFile('index.html');
        pos:=j;
  end;
        Response.SendRedirect(home+'admin2?'+Request.Query);
end;

function TWebModule2.LinkContent(path: string): string;
var
  i, j: integer;
begin
        result:='';
  if pos > l_count div 2 then
  begin
        j:=-(l_count div 2);
  end else
  begin
        j:=-pos;
  end;
  for i:=0 to l_count-1 do
  begin
    if ZTable1.RecordCount <= (pos+i+j)*page_def then
    begin
        break;
    end;
    if i+j = 0 then
    begin
        result:=result+'　'+IntToStr(pos)+'　';
    end else
    begin
        result:=result+Format('　<a href="'+home+path+'?pos=%d">%d</a>　',[pos+i+j,pos+i+j]);
    end;
  end;
end;

procedure TWebModule2.WebModule2errorAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
        Response.SendRedirect(home);
end;

function TWebModule2.footer(path: string): string;
var
  s, t, u: string;
  i: integer;
begin
  if pos <= -1 then
  begin
        s:='<hr size=1>最新%d件の記事を表示<br><center>Pages:[<b>';
        page:='<<'+LinkContent(path)+'>>]　最新<br></center>';
        result:=Format(s,[page_def])+page+foot;
  end else
  begin
    if pos = 0 then
    begin
        t:='?pos=0';
        u:='?pos='+IntToStr(pos+1);
    end else
    begin
        t:='?pos='+IntToStr(pos-1);
    end;
        i:=(ZTable1.RecordCount-1) div page_def;
    if pos > i then
    begin
        pos:=i;
    end;
    if pos = i then
    begin
        u:='?pos='+IntToStr(pos);
    end else
    begin
        u:='?pos='+IntToStr(pos+1);
    end;
        page:='<a href="'+home+path+t+'"><<</a>'+LinkContent(path)+
          '<a href="'+home+path+u+'">>></a>]　<a href="'+home+path+'">最新</a></b></center>';
        s:='<hr size=1>%d　件中<br> %d 番目から %d 番目の記事を表示<br><center>Page:[<b>';
        result:=Format(s,[ZTable1.RecordCount,pos*page_def,(pos+1)*page_def-1])+page+foot;
  end;
end;

procedure TWebModule2.WebModule2viewAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  i: integer;
  p, n, d, s, c: string;
begin
        i:=StrToIntDef(Request.QueryFields.Values['pos'],0);
  if i > 0 then
  begin
        createview;
    with ZTable2 do
    begin
        TableName:='view';
        Active:=true;
    if Locate('no',i,[]) = true then
    begin
        p:=FieldByName('pass').AsString;
        n:=FieldByName('name').AsString;
        d:=FieldByName('date').AsString;
        s:=FieldByName('sub').AsString;
        c:=FieldByName('com').AsString;
      with ZTable1 do
      begin
      if Eof and Bof then
      begin
                i:=0;
      end else
      begin
                Last;
                i:=FieldByName('no').AsInteger+1;
      end;
        AppendRecord([p,i,d,n,s,c]);
      end;
        Delete;
        pos:=-1;
      if htmlw = true then
      begin
                html.Text:=PageProducer2.Content;
                html.SaveToFile('index.html');
      end;
    end;
        Active:=false;
    end;
  end;
        Response.SendRedirect(home);
end;

function TWebModule2.contentstring(dataset: TDataSetPageProducer): string;
var
  i: integer;
begin
  with ZTable1 do
  begin
  if (pos < -1)or((pos+1)*page_def > RecordCount) then
  begin
    if pos < -1 then
    begin
        pos:=0;
    end else
    begin
        pos:=(RecordCount-1) div page_def;
    end;
  end;
  if pos = -1 then
  begin
    if RecordCount > page_def then
    begin
        RecNo:=RecordCount-page_def+1;
    end else
    begin
        First;
    end;
  end else
  begin
        RecNo:=pos*page_def+1;
  end;
        i:=0;
  while (Eof = false)and(i < page_def) do
  begin
        result:=dataset.Content+result;
        Next;
        inc(i);
  end;
  end;
end;

procedure TWebModule2.WebModule2startAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  h, t1, t2, c1, c2: string;
  x: Boolean;
begin
        h:='http://'+Request.Host+Request.InternalScriptName+'/';
        t1:=Request.ContentFields.Values['title1'];
        t2:=Request.ContentFields.Values['title2'];
        c1:='死ね,馬鹿,novapublic';
        c2:='kantei.go.jp,anonymizer.com,pt$,ph$,my$,th$,rr.com';
  with ZTable2 do
  begin
        TableName:='information';
        Active:=true;
        x:=IsEmpty;
    if x = true then
    begin
        AppendRecord([h,t1,t2,c1,c2]);
    end;
        Active:=false;
  end;
  if x = true then
  begin
        Response.SendRedirect(h);
  end else
  begin
        Response.Content:='error';
  end;
end;

procedure TWebModule2.createview;
begin
  with ZQuery1 do
  begin
        SQL.Clear;
        SQL.Add('CREATE TABLE if not exists view(pass text, no integer, date real, name text, sub text, com text);');
        ExecSQL;
  end;
end;

procedure TWebModule2.stopitemsHTMLTag(Sender: TObject; Tag: TTag;
  const TagString: String; TagParams: TStrings; var ReplaceText: String);
begin
  if TagString = 'home' then
  begin
        ReplaceText:=home;
  end;
  if TagString = 'no_word' then
  begin
    with ZQuery1 do
    begin
        Active:=false;
        SQL.Clear;
        SQL.Add('SELECT no_word FROM information;');
        Active:=true;
        no_words.CommaText:=FieldByName('no_word').AsString;
    end;
        ReplaceText:=no_words.Text;
  end;
  if TagString = 'no_host' then
  begin
        ReplaceText:=no_hosts.Text;
  end;
end;

procedure TWebModule2.WebModule2stopAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  i: integer;
begin
  if Request.MethodType = mtPost then
  begin
        no_words.Text:=Request.ContentFields.Values['word'];
        no_hosts.Text:=Request.ContentFields.Values['host'];
    for i:=0 to no_words.Count-1 do
    begin
        no_words[i]:=Trim(no_words[i]);
    end;
    for i:=0 to no_hosts.Count-1 do
    begin
        no_hosts[i]:=Trim(no_hosts[i]);
    end;
    with ZTable2 do
    begin
        TableName:='information';
        Active:=true;
        Edit;
        FieldByName('no_word').AsString:=no_words.CommaText;
        FieldByName('no_host').AsString:=no_hosts.CommaText;
        Post;
        Active:=false;
    end;
        Response.SendRedirect(home+'admin2');
  end else
  begin
        Response.Content:=stopitems.Content;
  end;
end;

procedure TWebModule2.WebModule2adminAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  pwd: string;
begin
  if Request.MethodType = mtPost then
  begin
    if (Request.ContentFields.IndexOfName('apass') = -1)or(Request.MethodType <> mtPost) then
    begin
        pwd:=Request.CookieFields.Values['admin_pass'];
    end else
    begin
        pwd:=Request.ContentFields.Values['apass'];
    end;
    if (admin_log = true)and(Request.ContentFields.Values['apass'] <> '') then
    begin
        ZQuery2.ExecSQL;
        Log.Active:=true;
        Log.AppendRecord(['LogIn',DateTimeToStr(Now)]);
        Log.Active:=false;
    end;
    with Response.Cookies.Add do
    begin
        Name:='admin_pass';
        Value:=pwd;
//        Secure:=true;
    end;
        Response.SendRedirect(home+'admin2');
  end else
  begin
        Response.Content:=PageProducer3.Content;
  end;
end;

procedure TWebModule2.PageProducer4HTMLTag(Sender: TObject; Tag: TTag;
  const TagString: String; TagParams: TStrings; var ReplaceText: String);
var
  s: string;
begin
        s:=Request.QueryFields.Values['search'];
  if TagString = 'str' then
  begin
        ReplaceText:=s;
  end;
  if TagString = 'home' then
  begin
        ReplaceText:=home;
  end;
  if (TagString = 'main')and(s <> '') then
  begin
        s:='*'+s+'*';
        ZTable1.Filter:='(sub LIKE '+QuotedStr(s)+') or (com LIKE '+QuotedStr(s)+')';
        ZTable1.Filtered:=true;
    if Request.QueryFields.Values['sort'] = 'name' then
    begin
        ZTable1.SortedFields:='name';
    end else
    begin
        ZTable1.SortedFields:='';
    end;
        ReplaceText:=contentstring(DataSetPageProducer2);
  end;
  if TagString = 'foot' then
  begin
        ReplaceText:=footer('search?'+Request.Query);
  end;
end;

initialization
  if WebRequestHandler <> nil then
    WebRequestHandler.WebModuleClass := TWebModule2;

end.
