##########################################################################################
#
# スパムフィルタープラグイン（学習機能無し版）
#（pcommentプラグインのオーバーライド）
#
##########################################################################################
use strict;

use plugin::pcomment::PComment;

# plugin::pcomment::PCommentのdo_actionメソッドの置き換え
package plugin::pcomment::PComment;

sub do_action {
    my $self = shift;
    my $wiki = shift;
    my $cgi  = $wiki->get_CGI;
    
    my $name      = $cgi->param("name");
    my $message   = $cgi->param("message");
    my $save_page = $cgi->param("save_page");
    my $show_page = $cgi->param("show_page");
    my $head_char = $cgi->param("head_char");
    my $reply     = $cgi->param("reply");

#--------------------------------------------------------------------------------------------------
  if($message){
    unless(&plugin::_ex_spam_filter_light::Install::judgment_text($message)){
      my $time = Util::format_date(time());
      my $file = $wiki->config('log_dir')."/spam_pcomment.txt";
      if(open(SPAM_LOG, ">>$file")){
        print SPAM_LOG "$show_page:$message - $name($time)\n";
        close(SPAM_LOG);
      }
      my $error_message = $wiki->error("投稿された文章はスパムメッセージと判定されました。\n");
      $error_message .= "<p>\n";
      $error_message .= "<div>投稿された文章：</div>\n";
      $error_message .= "<div>" . Util::escapeHTML("$message") . "</div>\n";
      $error_message .= "</p>\n";
      return $error_message;
    }
  }
#--------------------------------------------------------------------------------------------------

    if($name eq ""){
        $name = "名無しさん";
    } else {
        # post_nameというキーでクッキーをセットする
        my $cookie = $cgi->cookie(-name=>'post_name',-value=>$name,-expires=>'+1M');
        print "Set-Cookie: ",$cookie->as_string,"\n";
    }

    if($save_page ne "" && $message ne ""){
        my $content;
        if($wiki->page_exists($save_page)){
            $content = $wiki->get_page($save_page);
            $content =~ s/\n+$//g; #行末の改行は全て除去
        }else{
            $content = "[[$show_page]]\n";
        }
        my $new_content .= $head_char . "$message - $name (".Util::format_date(time()) . ")";

        unless($reply){
            $content .= "\n" if($content);
            $content .= $new_content . "\n";
        }else{
            my @content;
            my $reply_sw = 0; #汚いけど...
            my $lev = 0;
            foreach(split /\n/, $content){
                /^([*+]+)/;
                if($reply_sw == 1 and $lev >= length($1)){
                    push(@content, $head_char x $lev . $new_content);
                    $reply_sw = 2;
                }
                if($reply_sw == 0 and $reply eq Util::md5($_)){
                    $lev = length($1);
                    $reply_sw = 1;
                }
                push(@content, $_);
            }
            if($reply_sw == 1){
                push(@content, $head_char x $lev . $new_content);
            }
            $content = join("\n", @content);
        }

        $wiki->save_page($save_page, $content);

        my $modtime = &Util::load_config_hash($wiki,$Wiki::DefaultStorage::MODTIME_FILE);
        $modtime->{$show_page} = time();
        &Util::save_config_hash($wiki,$Wiki::DefaultStorage::MODTIME_FILE,$modtime);

    }
    # 表示されていたページにリダイレクト
    $wiki->redirect($show_page);
}

1;
