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

use plugin::bbsx::BBSXHandler;

# plugin::bbsx::BBSXHandlerのdo_actionメソッドの置き換え
package plugin::bbsx::BBSXHandler;

sub do_action {
	my $self = shift;
	my $wiki = shift;
	my $cgi  = $wiki->get_CGI;

	my $bbsname = $cgi->param("bbsname");
	my $name    = $cgi->param("name");
	my $subject = $cgi->param("subject");
	my $message = $cgi->param("message");
	my $password = $cgi->param("password");
	my $page    = $cgi->param("page");
	my $option  = $cgi->param("option");
	my $filename = $cgi->param("file");
	my $uploadable = $cgi->param("uploadable");

	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($subject eq ""){
		$subject = "無題";
	}

	if($bbsname eq ""){
		return $wiki->error("パラメータが不正です。");
	}

	if($uploadable eq 'yes'){
		if($message eq "" and $filename eq ""){
		return $wiki->error("本文か添付ファイルを指定してください。");
		}
	} else {
		if($message eq ""){
		return $wiki->error("本文を入力してください。");
		}
	}

	if($password eq ""){
		return $wiki->error("削除キーを入力してください。");
	}

	my $pagename = $self->get_page_name($wiki,$bbsname);

	my $refpart='';

	if($filename ne ""){
		$filename =~ s/\\/\//g;
		$filename = substr($filename,rindex($filename,"/")+1);
		&Jcode::convert(\$filename,'euc');

		my $hundle   = $cgi->upload("file");
		my $filecont;
		while(<$hundle>){ $filecont = $filecont.$_; }
		my $uploadfileNP = &Util::url_encode($pagename).".".&Util::url_encode($filename);
		my $uploadfile = $wiki->config('attach_dir')."/".$uploadfileNP;
		open(DATA,">$uploadfile") or die $!;
		binmode(DATA);
		print DATA $filecont;
		close(DATA);
	        # ログの記録
	        &plugin::attach::AttachHandler::write_log($wiki,"UPLOAD",$pagename,$filename);

		# 添付ファイルの表示形式判定

		my ($is_image,$infostring,$width,$height) = $self->getfileinfo($wiki,$uploadfile);
		if($is_image){
			$refpart = "{{bbsxfile $filename,show=1,page=$pagename,width=$width,height=$height,border=0}}\n";
		} else {
			my ($size) = (stat($uploadfile))[7];
			$refpart = "{{bbsxfile $filename,show=0,page=$pagename,size=$size}}\n";
		}
		if($plugin::bbsx::BBSXHandler::WRITEINFO){
			$refpart .= "$infostring\n";
		}

	}

	my $addr = $ENV{'REMOTE_ADDR'};
	my $content = "!![[$subject|$pagename]] - $name (".&Util::format_date(time()).")\n";

	if($refpart ne ""){
		$content .= "$refpart";
	}
	if($message ne ""){
		$content .= "\n$message\n";
	}

#--------------------------------------------------------------------------------------------------
  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_bbsx.txt";
      if(open(SPAM_LOG, ">>$file")){
        print SPAM_LOG "-" x 70 . "\n";
        print SPAM_LOG "$bbsname:$subject - $name ($time)\n";
        print SPAM_LOG "-" x 70 . "\n";
        print SPAM_LOG "$message\n";
        close(SPAM_LOG);
      }
      my $error_message = $wiki->error("投稿された文章はスパムメッセージと判定されました。\n");
      $subject = Util::escapeHTML($subject);
      $message = Util::escapeHTML($message);
      $message =~ s/\n/<br>/g;
      $error_message .= "<p>\n";
      $error_message .= "<div>投稿された文章：</div>\n";
      $error_message .= "<div>＜件名＞</div>\n";
      $error_message .= "<div>$subject</div>\n";
      $error_message .= "<div>＜本文＞</div>\n";
      $error_message .= "<div>$message</div>\n";
      $error_message .= "</p>\n";
      return $error_message;
    }
  }
#--------------------------------------------------------------------------------------------------

	$content .= "//IP=$addr\n";
	# パスワード記録
	$content .= "{{bbsxpassword $pagename," . crypt($password, 'aa') . "}}\n";

	# no_commentオプション
	if($option eq "no_comment"){

	# reverse_commentオプション
	} elsif($option eq "reverse_comment"){
		$content .= "{{bbsxcomment reverse}}\n";
	# デフォルト
	} else {
		$content .= "{{bbsxcomment}}\n";
	}
	$wiki->save_page($pagename,$content);

	# 元のページにリダイレクト
	$wiki->redirect($bbsname);
}

use plugin::bbsx::BBSXCommentHandler;

# plugin::bbsx::BBSXCommentHandlerのdo_actionメソッドの置き換え
package plugin::bbsx::BBSXCommentHandler;

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 $count   = $cgi->param("count");
	my $page    = $cgi->param("page");
	my $option  = $cgi->param("option");

	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($message){
    unless(&plugin::_ex_spam_filter_light::Install::judgment_text($message)){
      my $time = Util::format_date(time());
      my $file = $wiki->config('log_dir')."/spam_bbsx_comment.txt";
      if(open(SPAM_LOG, ">>$file")){
        print SPAM_LOG "$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($page ne "" && $message ne "" && $count ne ""){

		my @lines = split(/\n/,$wiki->get_page($page));
		my $flag       = 0;
		my $form_count = 1;
		my $content    = "";

		my $addr = $ENV{'REMOTE_ADDR'};

		# 新着順の場合
		if($option eq "reverse"){
			foreach(@lines){
				if(/^{{bbsxpassword\s*.*}}$/){
					# パスワード行を削除
					next;
				}
				$content = $content.$_."\n";
				if(/^{{bbsxcomment\s*.*}}$/ && $flag==0){
					if($form_count==$count){
						$content = $content."*$message - $name (".Util::format_date(time()).")\n";
						$flag = 1;
						$content = $content."//IP=$addr\n";
					} else {
						$form_count++;
					}
				}

			}
		# 投稿順の場合
		} else {
			foreach(@lines){
				if(/^{{bbsxpassword\s*.*}}$/){
					# パスワード行を削除
					next;
				}
				if(/^{{bbsxcomment\s*.*}}$/ && $flag==0){
					if($form_count==$count){
						$content = $content."*$message - $name (".Util::format_date(time()).")\n";
						$flag = 1;
						$content = $content."//IP=$addr\n";
					} else {
						$form_count++;
					}
				}
				$content = $content.$_."\n";
			}
		}
		if($flag==1){
			$wiki->save_page($page,$content);
		}
	}

	my $bbsname = &plugin::bbsx::BBSXCommon::get_bbsname($wiki,$page);
	# 元のページにリダイレクト
	$wiki->redirect($bbsname);
}

1;
