#!/usr/bin/perl

#┌─────────────────────────────────
#│ Web Forum v5
#│ wforum.cgi - 2007/02/14
#│ Copyright (c) KentWeb
#│ webmaster@kent-web.com
#│ http://www.kent-web.com/
#│ 
#│ Web Forum Antispan Version Modified by isso.
#│ http://swanbay-web.hp.infoseek.co.jp/index.html
#└─────────────────────────────────

# 外部ファイル取込み
require './init.cgi';
require $jcodepl;
require $foldpl;
require $cgi_lib;

#-------------------------------------------------
# 設定チェック
#-------------------------------------------------
if(!$writevalue || !$postvalue) {
	&error("wf_init.cgiファイルが正しく転送されていないか設定値が正しくありません。"); }
if($writevalue eq $postvalue) {
	&error("\$writevalueと\$postvalueの文字は同じにしないでください"); }

# ログファイル
unless(-e "$logfile") {
	&error("ログファイル $logfile がありません(wf_log.cgi → log.cgiにファイル名変更)。");
}

# 過去ログデータファイル
if($pastkey) {
	unless(-d "$pastdir") {
		mkdir ($pastdir, 0707) || die "$pastdir ディレクトリを作成できません : $!";
	}
	unless(-e "$nofile") {
		&error("過去ログデータファイル $nofile  がありません。");
	}
}

# 画像用ディレクトリ
if($img){
	unless(-d "$imgdir") {
		mkdir ($imgdir, 0707) || die "$imgdir ディレクトリを作成できません : $!";
	}
}

# Webmail認証用ディレクトリ
if($webmail){
	unless(-d "$mailchk") {
		mkdir ($mailchk, 0707) || die "ディレクトリを作成できません : $!";
	}
}

# カラーデータファイル
if($boardmode) {
	unless(-e "$colorfile") {
		open(OUT,">>$colorfile");
		print OUT "0";
		close(OUT);
	}
	unless(-e "$colordata") {
		&error("カラーデータファイル $colordata がありません。");
	}
}

# 表示モード設定
if ($boardmode && -s "$colordata") { &read_color; }

# 投稿キー暗号用パスワードチェック
if ($regist_key) {
	if (!$pcp_passwd) {
		&error("投稿キー暗号用パスワード \$pcp_passwd が設定されていません。");
	}
}

#-------------------------------------------------
# 基本処理を定義
#-------------------------------------------------
&agent;
&decode;
&axsCheck;

# メンテナンスモードで過去ログのhtmlファイル作成用(ファイル作成後削除可)
if ($mode eq "allread" && $in{'pastlog'} && $in{'act'} eq 'past') { require $allreadpl; &allread; }

if (-e "maintenance") { &mainte; }
elsif ($mode eq "msgview") { &msgview; }
elsif ($mode eq "allread") { require $allreadpl; &allread; }
elsif ($mode eq "find") { require $searchpl; &find; }
elsif ($mode eq "past") { require $pastlogpl; &past_view; }
elsif ($mode eq "fullsize") { require $messagepl; &fullsize; }
elsif ($mode eq "exif") { require $messagepl; &exif; }
elsif ($mode eq "newpost") { require $formpl; &newpost; }
elsif ($mode eq "check") { require $checkpl; &check; }
elsif ($webmail && $mode eq "writemail") { require $webmailpl; &writemail; }
elsif ($webmail && $mode eq "sendmail") { require $webmailpl; &sendmail; }
elsif ($mode eq "note") { require $howtopl; &note; }
elsif ($mode eq "sage") { require $howtopl; &sage; }
elsif ($mode eq "noscript") { require $howtopl; &noscript; }
# 携帯対応改造
elsif ($keitai ne 'p' && $mode eq "knew") { &keitai_new_list; }
elsif ($keitai ne 'p' && $mode eq "klist") { &keitai_list_view; }
elsif ($keitai ne 'p' && $mode eq "klview") { &keitai_log_list; }
elsif ($keitai ne 'p' && $mode eq "kaview") { &keitai_msg_view; }
elsif ($keitai ne 'p' && $mode eq "kmsgview") { &keitai_msg_view; }
elsif ($keitai ne 'p' && $mode eq "k_admin") { &keitai_new_list; }
elsif ($keitai ne 'p' && $mode eq "dele" && $in{'no'}) { &k_dele; }
&list_view;


#-------------------------------------------------
#  リスト表示
#-------------------------------------------------
sub list_view {

	# 携帯モード
	if (-e "$kscript" && $keitai ne 'p') {
		if ($list_ini eq "tree") { &keitai_list_view; } else { &keitai_new_list; }
	}

	&header;

	# 表示形式設定
	if (!$in{'list'}) { $in{'list'} = $list_ini; }

	if ($fpage) {$fpage = $page;}
	if ($phlist && !$fpage) { print "$pheader\n"; }

	print "<div align=\"center\">\n";

	# タイトル部
	if ($t_img) {
		print "<img src=\"$t_img\" alt=\"$title\" width=\"$t_w\" height=\"$t_h\">\n";
	} else {
		print "<B style=\"font-size:$t_point;color:$t_color\">$title</B>\n";
	}

	if ( (-s $spamlogfile) > $spamlog_maxfile ) {
		print "<br>\n<br>\n<b style=\"color:#FF0000\">",
		"$postmodeログのファイルサイズが大きくなりました。<br>",
		"至急、管理モードから$postmodeログを削除して下さい。</b><br>\n<br>\n";
	}

	# メニュー部
	if ($boardmode) {
		print "<table border=\"0\" width=\"500\"><tr>\n";

		print "<form action=\"$home\" method=\"get\">\n<td nowrap>\n",
		"<input type=\"submit\" value=\"もどる\" class=\"menu\">\n</td>\n</form>\n";

		# 新規投稿表示
		if ($in{'list'} eq "tree") {
			print "<form action=\"$script#msg\" method=\"post\">\n<td nowrap>\n",
			"<input type=\"hidden\" name=\"mode\" value=\"$in{'list'}\">\n",
			"<input type=\"hidden\" name=\"list\" value=\"tree\">\n",
			"<input type=\"submit\" value=\"新規投稿\" class=\"menu\">\n</td>\n</form>\n";
		} elsif ($in{'list'} eq 'newsort' && !$form_newsort) {
			print "<form action=\"$script#msg\" method=\"post\">\n<td nowrap>\n",
			"<input type=\"hidden\" name=\"mode\" value=\"newpost\">\n",
			"<input type=\"hidden\" name=\"list\" value=\"$in{'list'}\">\n",
			"<input type=\"submit\" value=\"新規投稿\" class=\"menu\">\n</td>\n</form>\n";
		} elsif ($in{'list'} eq 'new' && !$form_new) {
			print "<form action=\"$script#msg\" method=\"post\">\n<td nowrap>\n",
			"<input type=\"hidden\" name=\"mode\" value=\"newpost\">\n",
			"<input type=\"hidden\" name=\"list\" value=\"$in{'list'}\">\n",
			"<input type=\"submit\" value=\"新規投稿\" class=\"menu\">\n</td>\n</form>\n";
		} else {
			print "<form action=\"$script#msg\" method=\"post\">\n<td nowrap>\n",
			"<input type=\"hidden\" name=\"list\" value=\"$in{'list'}\">\n",
			"<input type=\"submit\" value=\"新規投稿\" class=\"menu\">\n</td>\n</form>\n";
		}

		print "<form action=\"$script\" method=\"post\">\n<td nowrap>\n",
		"<input type=\"hidden\" name=\"list\" value=\"new\">\n",
		"<input type=\"submit\" value=\"新着順タイトル表\示\" class=\"menu\">\n</td>\n</form>\n";

		print "<form action=\"$script\" method=\"post\">\n<td nowrap>\n",
		"<input type=\"hidden\" name=\"list\" value=\"tree\">\n",
		"<input type=\"submit\" value=\"ツリー表\示\" class=\"menu\">\n</td>\n</form>\n";

		print "<form action=\"$script\" method=\"post\">\n<td nowrap>\n",
		"<input type=\"hidden\" name=\"list\" value=\"newsort\">\n",
		"<input type=\"submit\" value=\"新着順記事\" class=\"menu\">\n</td>\n</form>\n";

		print "<form action=\"$script\" method=\"post\">\n<td nowrap>\n",
		"<input type=\"hidden\" name=\"mode\" value=\"note\">\n",
		"<input type=\"submit\" value=\"留意事項\" class=\"menu\">\n</td>\n</form>\n";

		print "<form action=\"$script\" method=\"post\">\n<td nowrap>\n",
		"<input type=\"hidden\" name=\"mode\" value=\"find\">\n",
			"<input type=\"hidden\" name=\"list\" value=\"$in{'list'}\">\n",
		"<input type=\"submit\" value=\"ワード検索\" class=\"menu\">\n</td>\n</form>\n";

		print "<form action=\"$script\" method=\"post\">\n<td nowrap>\n",
		"<input type=\"hidden\" name=\"mode\" value=\"past\">\n",
		"<input type=\"hidden\" name=\"list\" value=\"$in{'list'}\">\n",
		"<input type=\"submit\" value=\"過去ログ\" class=\"menu\">\n</td>\n</form>\n" if ($pastkey);

		print "<form action=\"$admin\" method=\"post\"><td nowrap>\n",
		"<input type=\"submit\" value=\"管理用\" class=\"menu\">\n</td>\n</form>\n";

		print "</tr></table>\n";
		print "<table border=\"0\"><tr><td>\n";

	} else {

		print "<hr width=\"90%\">\n",
		"[<a href=\"$home\" target=\"_top\">もどる</a>]\n";

		if ($in{'list'} eq "tree") {print "[<a href=\"$script?list=tree#msg\">新規投稿</a>]\n";
		} elsif ($in{'list'} eq 'newsort' && !$form_newsort) {
			print "[<a href=\"$script?list=$in{'list'}&mode=newpost#msg\">新規投稿</a>]\n";
		} elsif ($in{'list'} eq 'new' && !$form_new) {
			print "[<a href=\"$script?list=$in{'list'}&mode=newpost#msg\">新規投稿</a>]\n";
		} else {print "[<a href=\"$script?list=$in{'list'}#msg\">新規投稿</a>]\n";}

		print "[<a href=\"$script?list=new\">新規順タイトル表\示</a>]\n",
		"[<a href=\"$script?list=tree\">ツリー表\示</a>]\n",
		"[<a href=\"$script?list=newsort\">新着順記事</a>]\n";

		print "[<a href=\"$script?mode=note\">留意事項</a>]\n",
		"[<a href=\"$script?mode=find&list=$in{'list'}\">ワード検索</a>]\n";

		print "[<a href=\"$script?mode=past&list=$in{'list'}\">過去ログ</a>]\n" if ($pastkey);
		print "[<a href=\"$admin\">管理用</a>]\n",
		"<hr width=\"90%\"><table><tr><td>\n";
	 }

	if ($in{'list'} =~ /new/ ) {
		print "<li>以下は新規投稿順のリスト(投稿記事)表\示です。</li>\n";
	} else {
		print "<li>ツリー先頭部の $treehead をクリックすると関連記事を一括表\示します。</li>\n";
	}

	# 掲示板の注意書き表示
	foreach (0 .. $#attention) {
		print "<li>$attention[$_]</li>\n";
	}

	print "</td></tr></table></div>\n";

	# ログを開く
	if (!$page) { if ($alltitle && $in{'list'} eq "tree") { &SubjectList; } }
	if ($in{'list'} eq "new") { require $newsort; &ListNewOpen; }
	elsif ($in{'list'} eq "newsort") { require $newsort; &newsort; }
	else { &ListTreeOpen; }

	# 著作権表示（削除しないで下さい）
	print "<P><!-- $ver -->\n<div align=\"center\" style=\"font-size:10px; font-family:Verdana,Helvetica,Arial\">",
	"- <a href=\"&#104;&#116;&#116;&#112;&#58;&#47;&#47;&#119;&#119;&#119;&#46;&#107;&#101;&#110;&#116;&#45;&#119;&#101;&#98;&#46;&#99;&#111;&#109;&#47;\" target=\"_top\">&#x57;&#x65;&#x62;&#x20;&#x46;&#x6f;&#x72;&#x75;&#x6d;</a> -\n",
	"<br>\n- <a href=\"&#104;&#116;&#116;&#112;&#58;&#47;&#47;&#115;&#119;&#97;&#110;&#98;&#97;&#121;&#45;&#119;&#101;&#98;&#46;&#104;&#112;&#46;&#105;&#110;&#102;&#111;&#115;&#101;&#101;&#107;&#46;&#99;&#111;&#46;&#106;&#112;&#47;&#105;&#110;&#100;&#101;&#120;&#46;&#104;&#116;&#109;&#108;\" target=\"_top\">&#x41;&#x6e;&#x74;&#x69;&#x73;&#x70;&#x61;&#x6d;&#x20;&#x56;&#x65;&#x72;&#x73;&#x69;&#x6f;&#x6e;</a> -<br>\n";
	print "</div>\n</body></html>\n";
	exit;
}

#-------------------------------------------------
#  一覧表示
#-------------------------------------------------
sub SubjectList {
	local($j,$k,$i0,%i,%oyasub,%no,%reno,%name,%date,%sub,$co);

	print "<div align=\"center\">\n",
	"<table border=\"0\" bgcolor=\"$allt_col\" width=\"90%\" cellspacing=\"10\" cellpadding=\"0\" class=\"alltree\">\n",
	"<tr><td>\n";

	open(IN,"$logfile") || &error("ログファイル $logfile がありません。");
	$top = <IN>;
	@i=();
	$j = 1;
	$i0 = 0;
	while (<IN>) {
		local($no,$reno,$lx,$sub,$email,$url,$name,$date) = split(/<>/);
		if ($reno eq "no") {$oyano{$j}=$no; $oyasub{$j}=$sub; $i0++; $date{$i0}=$date; $name{$i0}=$name; $j++; }
		else { $date{$i0}=$date; $name{$i0}=$name; $i{$j}++; }
	}
	close(IN);

	if ($alltitle && $j > $alltitle) {$j = ($alltitle+1);}
	for ($k = 1;$k<$j;$k++){
		$co = sprintf("%01d",$i{$k+1});
		print "<A HREF=\"$script?no=$oyano{$k}&mode=allread#$oyano{$k}\" ",
		"title=\"最新投稿:$date{$k} (投稿者:$name{$k})\">$k:&nbsp;$oyasub{$k}&nbsp;($co)</A>",
		"&nbsp;<font color=\"$no_color\">/</font>\n";
	}
	print "</td></tr></table></div>\n";
}

#-------------------------------------------------
#  リストツリー表示
#-------------------------------------------------
sub ListTreeOpen {
	local($no,$reno,$lx,$sub,$email,$url,$name,$dat,$msg,$t,
	$h,$pw,$w,$oya,$sml,$res,$color,$img,$wi,$hi,$id,$uni);

	$in{'list'} = "tree";

	# 時間取得
	$time = time;

	if ($mode ne "past") {
		if ($boardmode) {
			print "<div align=\"center\">\n",
			"<table border=\"0\" bgcolor=\"$allt_col\" width=\"90%\" cellspacing=\"10\" cellpadding=\"0\"  class=\"list\">\n<tr>\n<td>\n<P>\n"; }
		else { print "<hr width=\"90%\">\n<DL>\n"; }
	}
	print "<UL>\n";

	# ページ区切り処理
#	$p_tree *= 4;
	$i = 0;
	$x = 0;

	open(IN,"$logfile") || &error("ログファイル $logfile がありません。");
	if ($mode ne "past") { $top = <IN>; }
	while (<IN>) {
		chomp;
		($no,$reno,$lx,$sub,$email,$url,$name,$dat,$msg,$t,
			$h,$pw,$w,$oya,$sml,$res,$color,$img,$wi,$hi,$id,$uni) = split(/<>/);
		if ($reno == 0) { $i++; }
		if ($i < $page + 1) { next; }
		if ($i > $page + $p_tree) { next; }

		while ($x > $lx) { print "\n"; $x--; }
		while ($x < $lx) { print "\n"; $x++; }

		if ($reno == 0) { while ($x > 0) { print "\n"; $x--; } }

		# 所定時間以内の投稿は[NEWマーク]表示
		if ($time - $t > $new_time * 3600) { $newsign = ""; }
		else { $newsign = $newmark; }

		# 文字色変更
		if ( $color && $nam_color ) { $name ="<font color=$color>$name</font>";
		} else { $name ="<font color=$te>$name</font>"; }

		# 記事タイトル長調整
		$sub = &cut_subject($sub);

		$id =~ s/\n//g;
		$id =~ s/\r//g;

		# 過去記事
		if ($mode eq "past") {
			if ($lx ne "0") {
				print "&nbsp;&nbsp;" x $lx,
				"&nbsp;&nbsp;$c_mark&nbsp;&nbsp;";
		} else { print "<P><DT><A HREF=\"$script?pastlog=$in{'pastlog'}&no=$oya&act=past&mode=allread\">$treehead</A>&nbsp;-&nbsp;";}
			print "<a href=\"$script?pastlog=$in{'pastlog'}&no=$oya&act=past&mode=allread#$no\">$sub</a> - <b>$name</b>";
			if ( $id && $idmode ) { print " <small>[ID:<font color=\"$idc\">$id</font>]</small> "; }
			print " $dat <font color=\"$no_color\">No\.$no</font> $newsign<br>\n";
		# 削除記事
		} elsif ($pw eq 'DEL') {
			if ($lx == 0) {
				print "<P><DT><a href=\"$script?no=$no&mode=allread\">$treehead</a> - ";
				print "$sub - <b>$name</b> $dat <font color=\"$no_color\">No\.$no</font><br>\n";
			} else {
				print "&nbsp;&nbsp;" x $lx;
				print "&nbsp;&nbsp;$c_mark&nbsp;&nbsp;$sub - <b>$name</b> $dat <font color=\"$no_color\">No\.$no</font><br>\n";
			}
		# レス記事
		} elsif ($lx != 0) {
			print "&nbsp;&nbsp;" x $lx;
			print "&nbsp;&nbsp;$c_mark&nbsp;&nbsp;<a href=\"$script?no=$no&reno=$reno&oya=$oya&mode=msgview\">$sub</a> - <b>$name</b>";
			if ( $id && $idmode ) { print " <small>[ID:<font color=\"$idc\">$id</font>]</small> "; }
			print " $dat <font color=\"$no_color\">No\.$no</font> $newsign<br>\n";

		# 親記事
		} else {
			print "<P><DT><a href=\"$script?no=$no&mode=allread\">$treehead</a> - ";
			print "<a href=\"$script?no=$no&reno=$reno&oya=$oya&mode=msgview\">$sub</a> - <b>$name</b>";
			if ( $id && $idmode ) { print " <small>[ID:<font color=\"$idc\">$id</font>]</small> "; }
			print " $dat <font color=\"$no_color\">No\.$no</font> $newsign<br>\n";
		}

	}
	close(IN);
	while ($x > 0) { print "\n"; $x--; }
	print "\n";
	if ($mode ne "past") { print "</DL>\n"; }

	# ページ移動フォーム
	&move_list;

	# メッセージ投稿フォ−ムを表示
	if ($mode ne "past") {
		require $formpl;
		&msg_form;
		if (!$boardmode) { print "<hr width=\"90%\">\n"; }
		&edt_form;
	} else {
		if (!$boardmode) { print "<hr width=\"90%\">\n"; }
	}
}

#-------------------------------------------------
#  メッセージ内容表示
#-------------------------------------------------
sub msgview {
	local($no,$re,$lx,$sub,$eml,$url,$nam,$dat,$msg,$lt,$ho,$pw,$wrap,$oya,$sml,$sml,$res,$color,
	$img,$wi,$hi,$id,$uni,$v_dat,$v_nam,$v_eml,$v_url,$v_msg,$v_tim,$v_sub,$v_wrp,$v_sml,$date,
	$v_sml,$v_res,$v_color,$v_img,$v_wi,$v_hi,$v_id,$v_uni,$flag,$oflag);

	local($cname, $cmail, $curl, $cpwd, $cpv, $csmail, $ccolor, $caikotoba) = &get_cookie;
	require $messagepl;

	$flag = 0; $oflag = 0;
	@new = ();
	open(IN,"$logfile") || &error("ログファイル $logfile がありません。");
	$top = <IN>;
	while (<IN>) {
		chomp;
		($no,$re,$lx,$sub,$eml,$url,$nam,$dat,$msg,$lt,$ho,$pw,$wrap,$oya,
		$sml,$res,$col,$img,$wi,$hi,$id,$uni) = split(/<>/);
		if ($in{'oya'} == $no) { $oflag = 1; }
		if ($in{'oya'} == $oya) { push(@new,$_); }
		elsif ($flag && $in{'oya'} != $oya) { last; }
		if ($in{'no'} == $no) {
			$flag = 1;
			($v_exiflink,$v_exinf) = &exiflink($img,$no);
			$v_no  = $no;
			$v_re  = $re;
			$v_dat = $dat;
			$v_nam = $nam;
			$v_eml = $eml;
			$v_url = $url;
			$v_msg = $msg;
			$v_tim = $lt;
			$v_ho  = $ho;
			$v_sub = $sub;
			$v_wrp = $wrap;
			$v_oya = $oya;
			$v_sml = $sml;
			$v_col = $col;
			$v_img = $img;
			$v_wi  = $wi;
			$v_hi  = $hi;
			$v_id  = $id;
		}
	}
	close(IN);

	if (!$flag || !$oflag)  { &error("指定記事が存在しません。"); }

	# コメント
	$res_msg = "\n&gt; $v_msg";
	$res_msg =~ s/<br>/\r&gt; /g;

	# コメントタイトル
	$res_sub = $v_sub;
	$res_sub =~ s/<([^>]|\n)*>//g;

	if ($res_sub =~ /^Re(.*)/i) { $res_sub = "Re$1";
	}
	else { $res_sub = "Re: $res_sub"; }

	$e_nam = $v_nam;

	# HTMLを出力
	&header;

	if ($fpage) {$fpage = $page;}
	if ($phview && !$fpage) { print "$pheader\n"; }

	print "<div align=\"center\">\n";
	if ($t_img) {
		print "<img src=\"$t_img\" alt=\"$title\" width=\"$t_w\" height=\"$t_h\">\n";
	} else {
		print "<B style=\"font-size:$t_point;color:$t_color\">$title</B>\n";
	}

	# メニュー部
	if ($boardmode) {
		print "<table border=\"0\" width=\"500\"><tr>\n";

		print "<form action=\"$script\" method=\"post\">\n<td nowrap>\n",
		"<input type=\"hidden\" name=\"list\" value=\"$in{'list'}\">\n",
		"<input type=\"submit\" value=\"リストにもどる\" class=\"menu\">\n</td>\n</form>\n";

		print "<form action=\"$script\" method=\"post\">\n<td nowrap>\n",
		"<input type=\"hidden\" name=\"list\" value=\"new\">\n",
		"<input type=\"submit\" value=\"新着順タイトル表\示\" class=\"menu\">\n</td>\n</form>\n";

		print "<form action=\"$script\" method=\"post\">\n<td nowrap>\n",
		"<input type=\"hidden\" name=\"list\" value=\"tree\">\n",
		"<input type=\"submit\" value=\"ツリー表\示\" class=\"menu\">\n</td>\n</form>\n";

		print "<form action=\"$script\" method=\"post\">\n<td nowrap>\n",
		"<input type=\"hidden\" name=\"list\" value=\"newsort\">\n",
		"<input type=\"submit\" value=\"新着順記事\" class=\"menu\">\n</td>\n</form>\n";

		print "<form action=\"$script\" method=\"post\">\n<td nowrap>\n",
		"<input type=\"hidden\" name=\"mode\" value=\"note\">\n",
		"<input type=\"submit\" value=\"留意事項\" class=\"menu\">\n</td>\n</form>\n";

		print "<form action=\"$script\" method=\"post\">\n<td nowrap>\n",
		"<input type=\"hidden\" name=\"mode\" value=\"find\">\n",
			"<input type=\"hidden\" name=\"list\" value=\"$in{'list'}\">\n",
		"<input type=\"submit\" value=\"ワード検索\" class=\"menu\">\n</td>\n</form>\n";

		print "<form action=\"$script\" method=\"post\">\n<td nowrap>\n",
		"<input type=\"hidden\" name=\"mode\" value=\"past\">\n",
		"<input type=\"hidden\" name=\"list\" value=\"$in{'list'}\">\n",
		"<input type=\"submit\" value=\"過去ログ\" class=\"menu\">\n</td>\n</form>\n" if ($pastkey);

		print "<form action=\"$admin\" method=\"post\"><td nowrap>\n",
		"<input type=\"submit\" value=\"管理用\" class=\"menu\">\n</td>\n</form>\n";

		print "</tr></table>\n</div>\n";
#		print "<table border=\"1\"><tr><td>\n";

	} else {

		print "<hr width=\"90%\">\n",
		"[<a href=\"$script?list=new\">新規順タイトル表\示</a>]\n",
		"[<a href=\"$script?list=tree\">ツリー表\示</a>]\n",
		"[<a href=\"$script?list=newsort\">新着順記事</a>]\n",
		"[<a href=\"$script?mode=note\">留意事項</a>]\n",
		"[<a href=\"$script?list=$in{'list'}&mode=find\">ワード検索</a>]\n";
		print "[<a href=\"$script?mode=past\">過去ログ</a>]\n" if ($pastkey);
		print "[<a href=\"$admin\">管理用</a>]<hr width=\"90%\"></div>\n";

	}

	# 投稿者引用符
	if ($grt) { $v_renam = &greeting($v_re,$v_nam,$v_msg); }

	# 文字色変更
	if (!$v_col) { $v_col = $te; }

	#投稿者メールアドレス
	if ( $v_col && $nam_color ) { $v_nam ="<font color=$v_col>$v_nam</font>";
	} else { $v_nam ="<font color=$te>$v_nam</font>"; }

	#メッセージ文字色の変更
	if ( $msg_color ) { $v_msg = "<font color=\"$v_col\">$v_msg</font>"; }

	# 引用部色変更
	if ($refcol) {
		$v_msg =~ s/([\>]|^)(&gt;[^<]*)/$1<font color=\"$refcol\">$2<\/font>/g;
	}

	# 自動リンク
	if ($autolink) { &auto_link($v_msg); }

	# PREタグ
	if ($v_wrp eq 'pre') {
		$v_msg =~ s/<br>/\n/g;
		$v_msg = "<pre>$v_msg</pre>";
	}

	# 投稿日時
	$date = &get_time($v_tim);

	$md = "&no=$v_oya&mode=allread#$v_no";
	$am = $md;

	# 投稿者情報表示
	if ($boardmode) {
		($author,$author2) = &author_new($v_sub,$v_eml,$v_sml,$webmail,$v_nam,$v_no,$page,
			$md,$am,$in{'list'},$date,$v_url,$v_id,$v_ho,$act);
	} else {
		if ($AuthorType) { &author_wf($v_sub,$v_eml,$v_sml,$webmail,$v_nam,$v_no,$page,
					$md,$am,$in{'list'},$date,$v_url,$v_id,$v_ho,$act);
		} else { &author_std($v_sub,$v_eml,$v_sml,$webmail,$v_nam,$v_no,$page,
					$md,$am,$in{'list'},$date,$v_url,$v_id,$v_ho,$act);
		}
	}
	if ($boardmode) {
		print "<P><div align=\"center\">",
		"<table width=\"90%\" cellpadding=\"$cellpadding\" cellspacing=\"$cellspacing\" border=\"$border\" class=\"thread\">\n<tr><td valign=\"top\">\n";
	}
	print $author;
	if ($boardmode) {print "</td></tr>\n<tr><td>\n";}

	$v_msg = "$v_renam <P>$v_msg</P>";
	print "<div style=\"margin-left:$margin_left; margin-top:5px; margin-right:$margin_right;\">\n";
	if ($v_img) {
		&prev_img($v_img,$v_no,$v_sub,$v_wi,$v_hi,$v_exiflink,$v_exinf,$v_msg);
	} else  { print "$v_msg\n"; }
	print "</div>\n";

	if ($boardmode) {
		print "</td></tr>\n<tr><td>\n";
		print $author2;

		# 投稿者編集フォーム
		if ($mode ne "past" && $in{'action'} ne "past") {
			if ($cname) {
				if ($cname =~ /\Q$e_nam\E/i || $e_nam =~ /\Q$cname\E/i) {
				print "<form action=\"$regist\" method=\"POST\">\n<div align=right>\n",
				"<span style=\"color:$btx_col\">投稿者</span> <select name=mode>\n",
				"<option value=usr_edt>修正\n",
				"<option value=usr_del>削除</select>\n",
				"<input type=hidden name=no value=\"$v_no\">\n&nbsp;",
				"<span style=\"color:$btx_col\">暗証キー</span> ",
				"<input type=password name=pwd size=8 maxlength=8 value=\"$cpwd\">\n",
				"<input type=submit value=\"送信\"></div>\n</form>\n";
				}
			}
		}
		print "</td></tr></table>\n</div>\n</P>\n<br>\n";
	}

	if (@new > 1) {
		if (!$boardmode) { print "<hr width=\"95%\">\n"; }
		print "<b style=\"text-indent:18\">- 関連一覧ツリー</b>\n",
		"（$treehead をクリックするとツリー全体を一括表\示します）<br>\n";

		$x = 0;
		print "<UL>\n";
		foreach (@new) {
			($no,$re,$lx,$sub,$eml,$url,$nam,$dat,$msg,$lt,
				$ho,$pw,$wrap,$oya,$sml,$res,$color,$img,$wi,$hi,$id,$uni) = split(/<>/);
			while ($x > $lx) { print "\n"; $x--; }
			while ($x < $lx) { print "\n"; $x++; }

			$id =~ s/\n//g;
			$id =~ s/\r//g;

			# 記事タイトル長調整
			$sub = &cut_subject($sub);

			# 文字色変更
			if ( $color && $nam_color ) { $nam ="<font color=$color>$nam</font>";
			} else { $nam ="<font color=$te>$nam</font>"; }

			if ($in{'no'} == $no) {
				$pno = "<font color=\"$scolor\"><B>No\.$no</B></font>\n";
				$sub = "<font color=\"$scolor\"><B>$sub</B></font>\n";
				$dat = "<font color=\"$scolor\"><B>$dat</B></font>\n";
			} else {
				$pno = "<font color=\"$no_color\">No\.$no</font>\n";
				$sub = "<a href=\"$script?no=$no&reno=$re&oya=$oya&mode=msgview\">$sub</a>";
			}

			if ($lx != 0) {
				print "&nbsp;&nbsp;" x $lx;
				print "&nbsp;&nbsp;$c_mark&nbsp;&nbsp;$sub - <b>$nam</b>";
			if ( $id && $idmode ) { print " <small>[ID:<font color=\"$idc\">$id</font>]</small> "; }
			print " $dat $pno<br>";
			} else {
				print "<P><DT><a href=\"$script?list=$in{'list'}&no=$no&mode=allread&page=$page\">",
				"$treehead</a> - $sub - <b>$nam</b>";
			if ( $id && $idmode ) { print " <small>[ID:<font color=\"$idc\">$id</font>]</small> "; }
			print " $dat $pno<br>";
			}
		}
		while ($x > 0) { print "\n"; $x--; }
		print "</UL>\n";
	}

	# 返信フォーム
	require $formpl;
	&msg_form;
	if (!$boardmode) {
		print "<hr width=\"90%\">\n";
		&edt_form;
	}
	print "</body>\n</html>\n";
	exit;
}

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

#-------------------------------------------------
#  記事タイトル長調整
#-------------------------------------------------
sub cut_subject {
	# 制限長に満たないものは戻す
	if (length($_[0]) <= $sub_length) { return $_[0]; }

	# カット処理
	($_[0], $folded) = &fold($_[0], $sub_length);
	$_[0] .= '..';

	return $_[0];
}

#-------------------------------------------------
#  ページ移動フォーム
#-------------------------------------------------
sub move_list {
	local($x,$y,$next,$back);

	$in{'list'} ||= 'tree';

	$next = $page + $p_tree;
	$back = $page - $p_tree;
	print "<p><table><tr>\n";
	if ($back >= 0) {
		print "<td><form action=\"$script\" method=\"get\">\n";
		print "<input type=\"hidden\" name=\"pastlog\" value=\"$in{'pastlog'}\">\n" if ($in{'pastlog'} ne "");
		print "<input type=\"hidden\" name=\"page\" value=\"$back\">\n";
		print "<input type=\"hidden\" name=\"mode\" value=\"past\">\n" if ($mode eq 'past');
		print "<input type=\"hidden\" name=\"list\" value=\"$in{'list'}\">\n";
		print "<input type=\"submit\" value=\"前ページ\"></td></form>\n";
	}
	if ($next < $i) {
		print "<td><form action=\"$script\" method=\"get\">\n";
		print "<input type=\"hidden\" name=\"pastlog\" value=\"$in{'pastlog'}\">\n" if ($in{'pastlog'} ne "");
		print "<input type=\"hidden\" name=\"page\" value=\"$next\">\n";
		print "<input type=\"hidden\" name=\"mode\" value=\"$mode\">\n" if ($mode eq 'past');
		print "<input type=\"hidden\" name=\"list\" value=\"$in{'list'}\">\n";
		print "<input type=\"submit\" value=\"次ページ\"></td></form>\n";
	}
	print "<td width=\"10\"></td><td class=\"num\">";

	# ページ移動ボタン表示
	if ($pnew || $in{'list'} ne "new") {
		$x = 1;
		$y = 0;
		$z = 0;
		while ($i > 0) {
			if ($page eq $y) {
				$z++;
				print "| <b style=\"color:red\">$x</b>\n";
			} elsif ($in{'pastlog'} ne "") {
				$z++;
				print "| <a href=\"$script?page=$y&list=$in{'list'}&mode=past&pastlog=$in{'pastlog'}\">$x</a>\n";
			} else {
				$z++;
				print "| <a href=\"$script?page=$y&list=$in{'list'}\">$x</a>\n";
			}
			$x++;
			$y += $p_tree;
			$i -= $p_tree;
		}
		print "|" if ($z);
	}
	print "</td></tr></table>\n";
}

#-------------------------------------------------
#  URLエンコード
#-------------------------------------------------
sub url_enc {
	local($_) = @_;

	s/(\W)/'%' . unpack('H2', $1)/eg;
	s/\s/+/g;
	$_;
}

#-------------------------------------------------
#  メンテナンス中
#-------------------------------------------------
sub mainte {
	&header;
	print "<div align=\"center\"><br><br>\n",
	"<br><br><big>メンテナンス中です</big>\n",
	"<div align=\"center\"><hr width=\"95%\">\n",
	"$Maintenance<br><hr width=\"95%\">\n",
	"<form action=\"$admin\" method=\"POST\">\n",
	"<input type=\"password\" name=\"pass\" size=\"8\" value=\"\">\n",
	"<input type=\"submit\" value=\" 管理モード \"></form>\n",
	"<script language=\"javascript\">\n",
	"<!--\n",
	"self.document.forms[0].pass.focus();\n",
	"//-->\n",
	"</script>\n",
	"</body></html>\n";
	exit;
}

__END__

