#!/usr/local/bin/perl5

# MAG BOARD v1.1 (2002/05)
# Copyright(C) Kent Web 2002
# webmaster@kent-web.com
# http://www.kent-web.com/

$ver = 'MagBoard v1.1';

#---------------------------------------------------------------#
# [ 注意事項 ]							#
# ・このスクリプトを使用したいかなる損害も作者は一切の責任を	#
#   負いません。						#
# ・掲示板下部にある「著作権表示」は、削除・改変を行なうことは	#
#   できません。						#
#---------------------------------------------------------------#

#============#
#  基本設定  #
#============#

# ライブラリ取り込み
require './jcode.pl';

# スクリプト
#  → 絶対パスなら http://から記述
$script = "./magbbs.cgi";

# ログファイル
#  → 絶対パスなら / から始まるサーバパスで記述
$logfile = "./magbbs.log";

# 設定ファイル
#  → 絶対パスなら / から始まるサーバパスで記述
$setfile = "./magbbs.ini";

# 会員ファイル
#  → 絶対パスなら / から始まるサーバパスで記述
$memfile = "./magdata.cgi";

# 管理パスワード
$pass = 'clip';

# method形式 (POST/GET)
$method = 'POST';

# ホスト名取得方式
#  0 : $ENV{'REMOTE_HOST'}変数で取得
#  1 : gethostbyaddr関数で取得
$gethostbyaddr = 0;

# ファイルロック形式
#  0 : no
#  1 : yes
# → 設定する場合は、ロックファイルを生成するディレクトリ
#    のパーミッションは原則として「777」に設定する。
$lockkey = 1;

# ロックファイル
$lockfile = "./lock/magbbs.lock";

# sendmailパス
$sendmail = '/usr/lib/sendmail';

#============#
#  設定完了  #
#============#

&decode;
&set_file;
if ($mode eq "regist") { &regist; }
elsif ($mode eq "form") { &form; }
elsif ($mode eq "log_mente") { &log_mente; }
elsif ($mode eq "mem_mente") { &mem_mente; }
elsif ($mode eq "member") { &member; }
elsif ($mode eq "join_in") { &join_in; }
elsif ($mode eq "join_out") { &join_out; }
elsif ($mode eq "howto") { &howto; }
elsif ($mode eq "find") { &find; }
elsif ($mode eq "mente_form") { &mente_form; }
elsif ($mode eq "edit") { &edit; }
&html;

#--------------------#
#  設定ファイル認識  #
#--------------------#
sub set_file {
	open(IN,"$setfile") || &error("Open Error : $setfile");
	$data = <IN>;
	close(IN);

	($title,$t_color,$t_size,$ImgT,$bg,$bc,$tx,$lk,$vl,$al,$face,$b_size,$color1,$color2,$color3,$max,$mem_max,$home,$p_log,$deny,$mailto,$sub_message,$mail_head,$signature) = split(/<>/, $data);

	# アクセス制限
	&get_host;
	local($flag)=0;
	@deny = split(/\s+/, $deny);
	foreach (@deny) {
		$_ =~ s/\*/\.\*/g;
		if ($host =~ /$_/i) { $flag=1; last; }
	}
	if ($flag) { &error("アクセスを許可されていません"); }

	# ポイント数
	$t_size .= 'pt';
	$s_size = ($b_size - 1) . 'pt';
	$b_size .= 'pt';
}

#------------#
#  記事表示  #
#------------#
sub html {
	open(IN,"$logfile") || &error("Open Error : $logfile");
	@lines = <IN>;
	close(IN);

	&header;
	print <<"EOM";
<table border=1 cellpadding=0 cellspacing=1><tr>
  <td>
    <form action="$home"><input type=submit value="DA7 Home">
  </td></form>
  <td>
    <form action="$script" method=$method>
    <input type=hidden name=mode value="member">
    <input type=submit value="メール登録・解除">
  </td></form>
  <td>
    <form action="$script" method=$method>
    <input type=hidden name=mode value="howto">
    <input type=submit value="利用方法">
  </td></form>
  <td>
    <form action="$script" method=$method>
    <input type=hidden name=mode value="find">
    <input type=submit value="キーワード検索">
  </td></form>
</tr></table>
<P><center>
EOM
	# タイトル部
	if ($ImgT) { print "<img src=\"$ImgT\" alt=\"$title\">\n"; }
	else {
		print "<font size=6 color=\"$t_color\"><b style=\"font-size: $t_size\">$title</b></font>\n";
	}

	print "<br><table><tr><td>$sub_message</td></tr></table></center>\n";

	# ページログ表示調整
	if ($in{'page'} eq "") { $in{'page'} = 0; }
	$start = $in{'page'};
	$end   = $in{'page'} + $p_log - 1;
	if ($#lines < $end) { $end = $#lines; }

	foreach ($start .. $end) {
		($no,$date,$sub,$msg,$url,$img) = split(/<>/, $lines[$_]);

		# URLリンク
		&auto_link($msg);

		print "<hr><table width='100%' cellpadding=3>\n";
		print "<tr><td bgcolor=\"$color1\">　<font color=\"$color2\">■</font>　";
		print "<font color=\"$color3\"><b>$sub</b></font></td></tr></table>\n";
		print "&nbsp;&nbsp;&nbsp;&nbsp;Date : $date<br>\n";
		print "<blockquote>$msg\n";

		if ($url) {
			print "<P><a href=\"$url\" target=\"_blank\">$url</a>\n";
		}
		print "</blockquote><P>\n";
	}

	$next_page = $in{'page'} + $p_log;
	$back_page = $in{'page'} - $p_log;

	print "<hr><table border=0 width='100%'><tr>\n";
	if ($back_page >= 0) {
		print "<td><form action=\"$script\" method=$method>\n";
		print "<input type=hidden name=page value=\"$back_page\">\n";
		print "<input type=submit value=\"前ページ\"></td></form>\n";
	}
	if ($next_page < $#lines) {
		print "<td><form action=\"$script\" method=$method>\n";
		print "<input type=hidden name=page value=\"$next_page\">\n";
		print "<input type=submit value=\"次ページ\"></td></form>\n";
	}

	print "<td width='90%' align=right>\n";
	print "<form action=\"$script\" method=$method>\n";
	print "<select name=mode>\n";
	print "<option value=\"form\">登録\n";
	print "<option value=\"log_mente\">ログ\n";
	print "<option value=\"mem_mente\">会員\n";
	print "<option value=\"edit\">設定\n";
	print "</select>\n";
	print "<input type=password name=pass size=8>\n";
	print "<input type=submit value=\"管理用\"></td></form>\n";
	print "</tr></table>\n";
	print "<!-- $ver --><small><center>\n";
	print "- <a href='http://www.kent-web.com/' target='_top'>Mag Board</a> -\n";
	print "</small></center>\n";
	print "</body></html>\n";
	exit;
}

#----------------#
#  記事投稿処理  #
#----------------#
sub regist {
	if ($in{'pass'} ne "$pass") { &error("パスワードが違います"); }

	&get_time;

	open(IN,"$logfile") || &error("Open Error : $logfile");
	@lines = <IN>;
	close(IN);

	($no,$dmy) = split(/<>/, $lines[0]);
	$no++;

	while ($max-1 < @lines) { pop(@lines); }
	unshift(@lines,"$no<>$in{'date'}<>$in{'sub'}<>$in{'message'}<>$in{'url'}<>$img_key<>\n");

	open(OUT,">$logfile") || &error("Write Error : $logfile");
	print OUT @lines;
	close(OUT);

	$mail_head =~ s/\r/\n/g;
	$signature =~ s/\r/\n/g;

	$mail_body .= "$mail_head\n\n---\n";
	$mail_body .= "送信日 : $Date\n";
	$mail_body .= "題　名 : $in{'sub'}\n";
	$mail_body .= "リンク : $in{'url'}\n" if ($in{'url'});
	$mail_body .= "\n$in{'message'}\n\n$signature";

	# メール用題名を定義
	$mail_sub  = $in{'sub'};

	# メール本文の改行処理
	$mail_body =~ s/<br>/\n/g;

	# メール本文／題名を JISコード変換
	&jcode'convert(*mail_body,'jis');
	&jcode'convert(*mail_sub,'jis');

	# バッファをフラッシュ：メール送信に備える
	$|=1;

	# メール送信
	open(IN,"$memfile") || &error("Open Error : $memfile");
	while (<IN>) {
		($num,$date,$email,$ip) = split(/<>/);

		&SendMail("$email", "$mailto");
	}
	close(IN);
}

#------------------#
#  メール送信処理  #
#------------------#
sub SendMail {
	local($to, $from) = @_;

	open(MAIL,"| $sendmail -t") || &error("メール送信に失敗しました");
	print MAIL "To: $to\n";
	print MAIL "Bcc: $bcc\n" if ($bcc);
	print MAIL "Errors-To: $mailto\n";
	print MAIL "From: $from\n";
	print MAIL "Subject: $mail_sub\n";
	print MAIL "MIME-Version: 1.0\n";
	print MAIL "Content-type: text/plain; charset=ISO-2022-JP\n";
	print MAIL "Content-Transfer-Encoding: 7bit\n";
	print MAIL "X-Mailer: $ver\n\n";
	print MAIL "$mail_body\n";
	close(MAIL);
}

#----------------#
#  投稿フォーム  #
#----------------#
sub form {
	if ($in{'pass'} ne "$pass") { &error("パスワードが違います"); }

	&get_time;

	&header;
	print <<"EOM";
[<a href="javascript:history.back()">もどる</a>]
<P>
<LI>必要事項を記入し「投稿する」を押して下さい<br>
<form action="$script" method=$method>
<input type=hidden name=mode value="regist">
<input type=hidden name=pass value="$in{'pass'}">
<table>
<tr>
  <td><b>投稿日</b></td>
  <td><input type=text name=date size=30 value="$Date"></td>
</tr>
<tr>
  <td><b>タイトル</b></td>
  <td><input type=text name=sub size=30></td>
</tr>
<tr>
  <td><b>ＵＲＬ</b></td>
  <td><input type=text name=url size=50 value="http://"></td>
</tr>
<tr>
  <td colspan=2><b>メッセージ</b><br>
  <textarea name=message cols=55 rows=6 wrap=soft></textarea></td>
</tr>
<tr>
  <td colspan=2>
    <input type=submit value="投稿する"><input type=reset value="リセット">
  </td>
</tr>
</table>
</form>
</body></html>
EOM
	exit;
}

#--------------------#
#  管理者ログメンテ  #
#--------------------#
sub log_mente {
	if ($in{'pass'} ne "$pass") { &error("パスワードが違います"); }

	open(IN,"$logfile") || &error("Open Error : $logfile");
	@lines = <IN>;
	close(IN);

	# 削除処理
	if ($in{'action'} eq "delete") {
		$flag=1;
		@new=();
		foreach (@lines) {
			($no,$dmy) = split(/<>/);

			if ($in{'no'} eq "$no") { $flag=1; }
			else { push(@new,$_); }
		}
		if (!$flag) { &error("削除記事が見つかりません"); }

		open(OUT,">$logfile") || &error("Write Error : $logfile");
		print OUT @new;
		close(OUT);

		@lines = @new;
	}

	&header;
	print <<"EOM";
[<a href="$script?">戻る</a>]
<table width="100%"><tr><th bgcolor="#008080">
  <font color="#FFFFFF">管理モード</font>
</th></tr></table>
<P>
<table border=1>
<tr><th>削除</th><th>題名</th><th>投稿日</th><th>コメント</th><th>ログ修正</th></tr>
EOM

	foreach (@lines) {
		($no,$date,$sub,$msg,$url,$img) = split(/<>/);

		$msg =~ s/<br>//g;
		if (length($msg) > 50) { $msg=substr($msg,0,48); $msg .= '..'; }

		print "<tr><th><form action=\"$script\" method=$method><input type=hidden name=pass value=\"$in{'pass'}\"><input type=hidden name=mode value=\"log_mente\"><input type=hidden name=action value=\"delete\"><input type=hidden name=no value=\"$no\"><input type=submit value=\"削除\"></th></form>\n";
		print "<td>$sub</td><td>$date</td><td>$msg</td>\n";
		print "<th><form action=\"$script\" method=$method><input type=hidden name=pass value=\"$in{'pass'}\"><input type=hidden name=mode value=\"mente_form\"><input type=hidden name=no value=\"$no\"><input type=submit value=\"修正\"></th></form></tr>\n";
	}
	print "</table></center>\n";
	print "</body></html>\n";
	exit;
}

#------------------------#
#  管理者会員情報メンテ  #
#------------------------#
sub mem_mente {
	if ($in{'pass'} ne "$pass") { &error("パスワードが違います"); }

	open(IN,"$memfile") || &error("Open Error : $memfile");
	@lines = <IN>;
	close(IN);

	if ($DEL[0]) {
		@new=();
		foreach (@lines) {
			($no,$email) = split(/<>/);
			$flag=0;
			foreach $x (@DEL) {
				if ($no == $x) { $flag=1; last; }
			}
			if (!$flag) { push(@new,$_); }
		}
		open(OUT,">$memfile") || &error("Write Error : $memfile");
		print OUT @new;
		close(OUT);

		@lines = @new;
	}
	$total = @lines;

	&header;
	print <<"EOM";
[<a href="$script?">戻る</a>]
<table width="100%"><tr><th bgcolor="#008080">
  <font color="#FFFFFF">管理モード</font>
</th></tr></table>
<P>
<center>
<form action="$script" method=$method>
<input type=hidden name=pass value="$in{'pass'}">
<input type=hidden name=mode value="mem_mente">
アドレス検索：<input type=text name=word size=35><input type=submit value="検索">
</form></center>
EOM
	# 検索処理
	if ($in{'word'}) {

		print "<DL>\n";
		print "<form action=\"$script\" method=$method>\n";
		print "<input type=hidden name=pass value=\"$in{'pass'}\">\n";
		print "<input type=hidden name=mode value=\"mem_mente\">\n";
		print "<input type=submit value=\"削除する\">";
		print "<input type=reset value=\"リセット\">\n<P>\n";

		open(IN,"$memfile") || &error("Write Error : $memfile");
		while (<IN>) {
			if ($_ =~ /$in{'word'}/i) {
				($num,$date,$email,$ip) = split(/<>/);
				print "<DT><input type=checkbox name=del value=$num>\n";
				print "$email [$ip]\n";
			}
		}
		close(IN);

		print "</form>\n";
		print "</DL>\n";
		print "</body></html>\n";
		exit;
	}

	print <<"EOM";
<hr>
登録件数：<b>$total</b>名<br>
<OL>
<form action="$script" method=$method>
<input type=hidden name=pass value="$in{'pass'}">
<input type=hidden name=mode value="mem_mente">

<input type=hidden name=action value="delete">
<input type=submit value="削除する"><input type=reset value="リセット">
<P>
EOM

	foreach (@lines) {
		($num,$date,$email,$ip) = split(/<>/);
		print "<LI><input type=checkbox name=del value=\"$num\"> <font color=blue>$email</font> [$ip]\n";
	}

	print "</OL>\n</form>\n";
	print "</body></html>\n";
	exit;
}

#------------------#
#  メンテフォーム  #
#------------------#
sub mente_form {
	if ($in{'pass'} ne "$pass") { &error("パスワードが違います"); }

	open(IN,"$logfile") || &error("Open Error : $logfile");
	@lines = <IN>;
	close(IN);

	# メンテ処理
	if ($in{'action'} eq "retouch") {
		@new=();
		foreach (@lines) {
			($no,$date,$sub,$msg,$url,$img) = split(/<>/);
			if ($in{'no'} eq "$no") {
				$_ = "$no<>$date<>$in{'sub'}<>$in{'message'}<>$in{'url'}<>$img<>\n";
			}
			push(@new,$_);
		}
		open(OUT,">$logfile") || &error("Write Error : $logfile");
		print OUT @new;
		close(OUT);

		&html;
	}

	foreach (@lines) {
		($no,$date,$sub,$msg,$url,$img) = split(/<>/);
		if ($in{'no'} eq "$no") { last; }
	}

	if ($url eq "") { $url = "http://"; }
	$msg =~ s/<br>/\r/g;

	&header;
	print <<"EOM";
<center>
<h4>以下のフォームから記事の修正を行います</h4>
<form action="$script" method=$method>
<input type=hidden name=pass value="$in{'pass'}">
<input type=hidden name=no value="$in{'no'}">
<input type=hidden name=mode value="mente_form">
<input type=hidden name=action value="retouch">
<table>
<tr>
  <td><b>タイトル</b></td>
  <td><input type=text name=sub size=30 value="$sub"></td>
</tr>
<tr>
  <td><b>ＵＲＬ</b></td>
  <td><input type=text name=url size=50 value="$url"></td>
</tr>
<tr>
  <td colspan=2><b>コメント</b><br>
    <textarea name=message cols=55 rows=6 wrap=soft>$msg</textarea>
  </td>
</tr>
</table>
<input type=submit value="メンテする"><input type=reset value="リセット">
</form>
</body></html>
EOM
	exit;
}

#--------------------------#
#   メール登録・解除画面   #
#--------------------------#
sub member {
	open(IN,"$memfile") || &error("Open Error : $memfile");
	@lines = <IN>;
	close(IN);

	&header;
	print <<"EOM";
[<a href="javascript:history.back()">戻る</a>]
<table width="100%"><tr><th bgcolor="#008080">
  <font color="#FFFFFF">メールアドレス登録・解除</font>
</th></tr></table>
<P><blockquote>
<h3>講読の登録・解除・変更</h3>
<P><font color="$t_color">▼メールマガジンの「登録」</font>
<br><br>
EOM

	# 登録件数をチェック
	if (@lines >= $mem_max) {
		print "<b>- 現在メールアドレスの登録はできません -</b>\n";
	}
	else {
		print "<form action=\"$script\" method=\"$method\">\n";
		print "<input type=hidden name=mode value=\"join_in\">\n";
		print "<small>メールアドレス（半角）：</small><br>\n";
		print "<input type=text name=email size=40>\n";
		print "<input type=submit value=\"登録\"></form>\n";
	}

	print <<"EOM";
<P><font color="$t_color">▼メールマガジンの「解除」</font><br><br>
<form action="$script" method="$method">
<input type=hidden name=mode value="join_out">
<small>メールアドレス（半角）：</small><br>
<input type=text name=email size=40>
<input type=submit value="解除">
</form>
<br><br><br>
*1 : 登録したアドレスを「変更」する場合は、一旦「解除」した後、新規に「登録」して下さい。<br>
*2 : セキュリティ上、同一ホストからの連続登録はできません。
</blockquote>
</center>
</body><html>
EOM
	exit;
}

#----------------#
#   メール登録   #
#----------------#
sub join_in {
	if ($in{'email'} =~ /\,/)
		{ &error("メールアドレスにコンマが含まれています"); }
	if ($in{'email'} !~ /(.+)\@(.+)\.(.+)/)
		{ &error("メールアドレスの入力が不正です"); }

	# 小文字に統一
	$in{'email'} =~ tr/[A-Z]/[a-z]/;

	# ロック開始
	if ($lockkey) { &lock; }

	# 会員ファイルを読み込み
	open(IN,"$memfile") || &error("Open Error : $memfile");
	@lines = <IN>;
	close(IN);

	# 登録件数をチェック
	if (@lines >= $mem_max) { &error("現在登録受付を中止しています"); }

	# ホスト名を取得
	&get_host;

	# ホストチェック
	($num,$date,$email,$ip) = split(/<>/, $lines[0]);
	if ($host eq "$ip") { &error("同一ホストからの連続投稿はできません"); }

	# 二重登録をチェック
	$flag=0;
	foreach (@lines) {
		($no,$date,$email,$ip) = split(/<>/);

		if ($in{'email'} eq "$email") { $flag=1; last; }
	}
	if ($flag) { &error("ご指定のメールアドレスは既に登録済です"); }

	# 会員Noを採番し、時間を取得
	$num++;
	&get_time('join');

	# 改行コード処理
	$mail_head =~ s/\r/\n/g;
	$signature =~ s/\r/\n/g;

	# メール送信
	$mail_body = <<"EOM";
$mail_head

---
$title へメールアドレスの登録をありがとうございます。
内容をご確認下さい。

■送信日時：$Date
■ホスト名：$ENV{'REMOTE_HOST'}
■ブラウザ：$ENV{'HTTP_USER_AGENT'}

■登録メールアドレス：$in{'email'}

$signature
EOM

	$mail_sub  = "メールマガジン登録のご確認";

	# メール本文／題名を JISコード変換
	&jcode'convert(*mail_body,'jis');
	&jcode'convert(*mail_sub,'jis');

	&SendMail("$in{'email'}", "$mailto");

	# ファイル更新
	unshift(@lines,"$num<>$Date<>$in{'email'}<>$host<>\n");
	open(OUT,">$memfile") || &error("Write Error : $memfile");
	print OUT @lines;
	close(OUT);

	# ロック解除
	if ($lockkey) { &unlock; }

	# 完了メッセージ
	&header;
	print <<"EOM";
<center>
<hr width="400">
<h3>登録は正常に終了しました</h3>
<P>登録されたアドレス：<font color="$t_color">$in{'email'}</font>
<P>[<a href="$home">トップに戻る</a>]
<P><hr width="400">
</center>
</body></html>
EOM
	exit;
}


#----------------#
#   メール解除   #
#----------------#
sub join_out {
	if ($in{'email'} !~ /(.+)\@(.+)\.(.+)/) {
		&error("メールアドレスの入力が不正です");
	}

	# ロック開始
	if ($lockkey) { &lock; }

	# 会員ファイルを読み込み
	open(IN,"$memfile") || &error("Open Error : $memfile");
	@lines = <IN>;
	close(IN);

	# ファイルから該当メールをマッチングし排除
	$flag=0;
	@new=();
	foreach (@lines) {
		($no,$date,$email,$ip) = split(/<>/);

		if ($in{'email'} eq "$email") { $flag=1; }
		else { push(@new,$_); }
	}
	if (!$flag) { &error("ご指定のメールアドレスは見当たりませんでした"); }

	# ファイル更新
	open(OUT,">$memfile") || &error("Write Error : $memfile");
	print OUT @new;
	close(OUT);

	# ロック解除
	if ($lockkey) { &unlock; }

	# 完了メッセージ
	&header;
	print <<"EOM";
<center>
<hr width="400">
<h3>解除は正常に終了しました</h3>
<P>解除されたアドレス：<font color="$t_color">$in{'email'}</font>
<P>[<a href="$home">トップに戻る</a>]
<P><hr width="400">
</center>
</body></html>
EOM
	exit;
}

#------------------#
#  キーワード検索  #
#------------------#
sub find {
	&header;
	print <<"EOM";
[<a href="$script?">戻る</a>]
<table width="100%"><tr><th bgcolor="#008080">
  <font color="#FFFFFF">キーワード検索</font>
</th></tr></table>
<P><center>
<table cellpadding=5 cellspacing=0>
<tr><td nowrap>
  <UL>
  <LI>検索したい<b>キーワード</b>を入力し、検索条件を選択して「検索ボタン」を押してください。
  <LI>キーワードは「半角スペース」で区切って複数指定することができます。
  </OL>
</td></tr></table>
<P><form action="$script" method="$method">
<input type=hidden name=mode value="find">
<table border=1 cellspacing=0 cellpadding=5>
<tr>
  <th>キーワード</td>
  <td><input type=text name=word size=30 value="$in{'word'}"></th>
</tr>
<tr>
  <th>検索条件</th>
  <td>
    <input type=radio name=cond value="and" checked>AND
    <input type=radio name=cond value="or">OR
  </td>
</tr>
<tr>
  <th colspan=2>
    <input type=submit value="検索する"><input type=reset value="リセット">
  </th>
</tr>
</table>
</form>
</center>
EOM

	# ワード検索の実行と結果表示
	if ($in{'word'}) {

		# 入力内容を整理
		$cond = $in{'cond'};
		$word = $in{'word'};
		$word =~ s/　/ /g;
		@pairs = split(/\s+/, $word);

		# 検索処理
		print "<OL>\n";
		open(IN,"$logfile") || &error("Open Error : $logfile");
		$i=0;
		while ($_ = <IN>) {
			$flag = 0;
			foreach $pair (@pairs) {
				if (index($_,$pair) >= 0) {
					$flag=1;
					if ($cond eq 'or') { last; }
				}
				else {
					if ($cond eq 'and') { $flag=0; last; }
				}
			}
			if ($flag == 0) { next; }


			# 結果を表示
			$i++;
			($no,$date,$sub,$msg,$url,$img) = split(/<>/);

			if ($url) { $url = "<a href=\"$url\" target='_blank'>$url</a>"; }

			print "<hr><LI><font color=\"$color1\"><b>$sub</b></font><br>\n";
			print "&nbsp; Date：$date<br>\n";
			print "<blockquote>$msg<P>$url</blockquote>\n";
		}
		close(IN);

		print "<hr>検索結果は <b>$i</b>件です。\n";
		print "</OL>\n";
	}
	print "</body></html>\n";
	exit;
}

#----------------#
#  利用規定画面  #
#----------------#
sub howto {
	&header;
	print <<"EOM";
[<a href="javascript:history.back()">戻る</a>]
<table width="100%"><tr><th bgcolor="#008080">
  <font color="#FFFFFF">利用方法</font>
</th></tr></table>
<P><center>
<table cellpadding=10 border=1><tr><td bgcolor="#FFFFFF">
<br>
<OL>
  <LI>この掲示板は<b>「メールマガジン」</b>対応です。<br><br>
  <LI>メールアドレスを登録しておくと、この掲示板に更新があると自動で<b>メール通知</b>します。<br><br>
  <LI>メールアドレスの<b>登録・解除・変更</b>は <a href="$script?mode=member">専用画面</a> から自由に行なうことができます。<br><br>
  <LI>過去の記事（$max件）は <a href="$script?mode=find">検索画面</a> から<b>キーワード検索</b>することができます。
</OL>
</td></tr></table>
</center>
</body></html>
EOM
	exit;
}

#--------------------#
#  設定ファイル編集  #
#--------------------#
sub edit {
	if ($in{'pass'} ne "$pass") { &error("パスワードが違います"); }

	# 実行
	if ($in{'action'} eq "edit") {

		if ($in{'ImgT'} eq "http://") { $in{'ImgT'} = ""; }
		if ($in{'bg'} eq "http://") { $in{'bg'} = ""; }
		if ($in{'mailto'} ne "") {
			if ($in{'mailto'} =~ /\,/) {
				&error("メールアドレスに「コンマ」が含まれています");
			}
			if ($in{'mailto'} !~ /.+\@.+\..+/) {
				&error("メールアドレスの入力が不正です");
			}
		}
		$in{'deny'} =~ s/　/ /g;

		$in{'sub_message'} =~ s/&lt;/</g;
		$in{'sub_message'} =~ s/&gt;/>/g;
		$in{'mail_head'} =~ s/&lt;/</g;
		$in{'mail_head'} =~ s/&gt;/>/g;
		$in{'signature'} =~ s/&lt;/</g;
		$in{'signature'} =~ s/&gt;/>/g;

		open(OUT,">$setfile") || &error("Write Error : $setfile");
		print OUT "$in{'title'}<>$in{'t_color'}<>$in{'t_size'}<>$in{'ImgT'}<>$in{'bg'}<>$in{'bc'}<>$in{'tx'}<>$in{'lk'}<>$in{'vl'}<>$in{'al'}<>$in{'face'}<>$in{'b_size'}<>$in{'color1'}<>$in{'color2'}<>$in{'color3'}<>$in{'max'}<>$in{'mem_max'}<>$in{'home'}<>$in{'p_log'}<>$in{'deny'}<>$in{'mailto'}<>$in{'sub_message'}<>$in{'mail_head'}<>$in{'signature'}<>";
		close(OUT);

		# 完了メッセージ
		&header;
		print "<center><h3>修正は正常に完了しました</h3>\n";
		print "<form action=\"$script\">\n";
		print "<input type=submit value='掲示板に戻る'></form>\n";
		print "</center>\n</body></html>\n";
		exit;
	}

	$t_size =~s /pt$//g;
	$b_size2 = $b_size;
	$b_size2 =~s /pt$//g;
	if ($ImgT eq "") { $ImgT = "http://"; }
	if ($bg eq "") { $bg = "http://"; }

	&header;
	print <<"EOM";
[<a href="$script?">戻る</a>]
<table width="100%"><tr><th bgcolor="#008080">
<font color="#FFFFFF">設定ファイル編集</font>
</th></tr></table>
<center>
<P>- 変更する部分のみ修正してください -
<form action="$script" method="$method">
<input type=hidden name=mode value="edit">
<input type=hidden name=action value="edit">
<input type=hidden name=pass value="$in{'pass'}">
<table>
<tr><td colspan=2><hr></td></tr>
<tr>
  <td>タイトル名</td>
  <td><input type=text name=title size=25 value="$title"></td>
</tr>
<tr>
  <td>タイトル色</td>
  <td><input type=text name=t_color size=25 value="$t_color">
      <font color="$t_color">■</font>
  </td>
</tr>
<tr>
  <td>タイトルサイズ</td>
  <td><input type=text name=t_size size=4 value="$t_size">ポイント</td>
</tr>
<tr>
  <td>タイトル画像</td>
  <td><input type=text name=ImgT size=35 value="$ImgT">（任意）</td>
</tr>
<tr><td colspan=2><hr></td></tr>
<tr>
  <td>壁紙</td>
  <td><input type=text name=bg size=35 value="$bg">（任意）</td>
</tr>
<tr>
  <td>背景色</td>
  <td><input type=text name=bc size=25 value="$bc">
      <font color="$bc">■</font>
  </td>

</tr>
<tr>
  <td>文字色</td>
  <td><input type=text name=tx size=25 value="$tx">
      <font color="$tx">■</font>
  </td>
</tr>
<tr>
  <td>リンク色１</td>
  <td><input type=text name=lk size=25 value="$lk">
      <font color="$lk">■</font>　（未訪問）
  </td>
</tr>
<tr>
  <td>リンク色２</td>
  <td><input type=text name=vl size=25 value="$vl">
      <font color="$vl">■</font>　（訪問済）
  </td>
</tr>
<tr>
  <td>リンク色３</td>
  <td><input type=text name=al size=25 value="$al">
      <font color="$al">■</font>　（訪問中）
  </td>
</tr>
<tr><td colspan=2><hr></td></tr>
<tr>
  <td>文字サイズ</td>
  <td><input type=text name=b_size size=4 value="$b_size2">ポイント</td>
</tr>
<tr>
  <td>文字タイプ</td>
  <td><input type=text name=face size=25 value="$face"></td>
</tr>
<tr>
  <td>題名部色1</td>
  <td><input type=text name=color1 size=25 value="$color1">
      <font color="$color1">■</font>（下地の色）
  </td>
</tr>
<tr>
  <td>題名部色2</td>
  <td><input type=text name=color2 size=25 value="$color2">
      <font color="$color2">■</font>（■の色）
  </td>
</tr>
<tr>
  <td>題名部色3</td>
  <td><input type=text name=color3 size=25 value="$color3">
      <font color="$color3">■</font>（題名文字の色）
  </td>
</tr>
<tr><td colspan=2><hr></td></tr>
<tr>
  <td>最大記事数</td>
  <td><input type=text name=max size=4 value="$max"></td>
</tr>
<tr>
  <td>最大会員数</td>
  <td>
	<input type=text name=mem_max size=4 value="$mem_max">
	（これを超えると会員登録を休止します）
  </td>
</tr>
<tr>
  <td>ページ記事数</td>
  <td><input type=text name=p_log size=4 value="$p_log"></td>
</tr>
<tr>
  <td>戻り先</td>
  <td><input type=text name=home size=35 value="$home"></td>
</tr>
<tr><td colspan=2><hr></td></tr>
<tr>
  <td valign=top>アクセス制限</td>
  <td valign=top><input type=text name=deny size=35 value="$deny"><br>
・アクセス拒否するホスト名を記述<br>
・半角スペースで区切って複数指定可<br>
・ワイルドカード (*) 使用可<br>
  </td>
</tr>
<tr><td colspan=2><hr></td></tr>
<tr>
  <td>管理者E-mail</td>
  <td><input type=text name=mailto size=25 value=\"$mailto\"></td>
</tr>
<tr><td colspan=2><hr></td></tr>
<tr>
  <td colspan=2>一口コメント（タイトル下に表\示）<br>
  <textarea name=sub_message cols=50 rows=5 nowrap>$sub_message</textarea></td>
</tr>
<tr>
  <td colspan=2>メールヘッダ（配信メールの本文先頭に付加）<br>
  <textarea name=mail_head cols=50 rows=5 nowrap>$mail_head</textarea></td>
</tr>
<tr>
  <td colspan=2>メール署名（配信メールの本文末尾に付加）<br>
  <textarea name=signature cols=50 rows=5 nowrap>$signature</textarea></td>
</tr>
<tr><td colspan=2><hr></td></tr>
</table>
<input type=submit value='送信する'><input type=reset value='リセット'>
</form>
</center>
</body></html>
EOM
	exit;
}

#----------------#
#  デコード処理  #
#----------------#
sub decode {
	if ($ENV{'REQUEST_METHOD'} eq "POST") {
		read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	} else { $buffer = $ENV{'QUERY_STRING'}; }

	@pairs = split(/&/, $buffer);
	foreach $pair (@pairs) {
		($name, $value) = split(/=/, $pair);
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

		# S-JISコード変換
		&jcode'convert(*value,'sjis');

		# タグ処理
		$value =~ s/</&lt;/g;
		$value =~ s/>/&gt;/g;

		# 改行処理
		if ($name eq "message") {
			$value =~ s/\r\n/<br>/g;
			$value =~ s/\r/<br>/g;
			$value =~ s/\n/<br>/g;
		} elsif ($name eq "sub_message" || $name eq "mail_head" || $name eq "signature") {
			$value =~ s/\r\n/\r/g;
			$value =~ s/\n/\r/g;
		} else {
			$value =~ s/\r//g;
			$value =~ s/\n//g;
		}

		# 削除情報
		if ($name eq 'del') { push(@DEL,$value); }

		$in{$name} = $value;
	}
	if ($in{'url'} eq "http://") { $in{'url'} = ''; }
	$mode = $in{'mode'};
}

#----------------#
#   エラー処理   #
#----------------#
sub error {
	if ($lockkey) { &unlock; }

	&header if (!$head_flag);
	print <<"EOM";
<center>
<hr width="400">
<h3>ERROR !</h3>
<P><font color=red>$_[0]</font>
<P><hr width="400">
</center>
</body></html>
EOM
	exit;
}

#--------------#
#  HTMLヘッダ  #
#--------------#
sub header {
	$head_flag = 1;
	print "Content-type: text/html\n\n";
	print <<"EOM";
<html>
<head>
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=Shift_JIS">
<STYLE TYPE="text/css">
<!--
body,tr,td,th { font-size: $b_size; font-family: "$face" }
small { font-size: $s_size }
a:hover { color: $al; }
-->
</STYLE>
<title>$title</title></head>
<body background="$bg" bgcolor="$bc" text="$tx" link="$lk" vlink="$vl" alink="$al">
EOM
}

#----------------#
#   時間を取得   #
#----------------#
sub get_time {
	$ENV{'TZ'} = "JST-9";
	($sec,$min,$hour,$mday,$mon,$year,$wday,$dmy,$dmy) = localtime(time);

	# 日時のフォーマット
	@week = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');

	if (!$_[0]) {
		$Date = sprintf("%04d/%02d/%02d (%s)",
				$year+1900,$mon+1,$mday,$week[$wday]);
	} else {
		$Date = sprintf("%04d/%02d/%02d(%s) %02d:%02d",
				$year+1900,$mon+1,$mday,$week[$wday],$hour,$min);
	}
}

#----------------#
#  ホスト名取得  #
#----------------#
sub get_host {
	$host = $ENV{'REMOTE_HOST'};
	$addr = $ENV{'REMOTE_ADDR'};

	if ($gethostbyaddr) {
		if ($host eq "" || $host eq "$addr") {
			$host = gethostbyaddr(pack("C4", split(/\./, $addr)), 2);
		}
	}
	if ($host eq "") { $host = $addr; }
}

#--------------#
#  ロック処理  #
#--------------#
sub lock {
	if (-e $lockfile) {
		# 3分以上古いロックは削除する
		($mtime) = (stat($lockfile))[9];
		if ($mtime < time - 180) { &unlock; }
	}
	local($retry) = 5;
	while (!mkdir($lockfile, 0755)) {
		if (--$retry <= 0) { &error('Lock is busy'); }
		sleep(1);
	}
}

#--------------#
#  ロック解除  #
#--------------#
sub unlock {
	rmdir($lockfile);
}

#--------------#
#  自動リンク  #
#--------------#
sub auto_link {
	$_[0] =~ s/([^=^\"]|^)(http\:[\w\.\~\-\/\?\&\=\@\;\#\:\%]+)/$1<a href=\"$2\" target='_top'>$2<\/a>/g;
}