#!/usr/local/bin/perl

###############################################
#   bbs20.cgi
#      V2.0 (2004.8.9)
#                     Copyright(C) CGI-design
###############################################

require 'cgi-lib.pl';
require './jcode.pl';
require './mimew.pl';

$adm_mail2 = 'yy-.-y777y-.-yy@docomo.ne.jp';
$adm_mail3 = 'yamazakiya.1596@docomo.ne.jp';
#文字化け投稿が多いので、メールアドレス２・３への送信も当面停止するが、送信スクリプト側のみで停止処置しており、文字宣言はキャンセルしない 2010-03-04
#$adm_mail4 = 'messa2@di.pdx.ne.jp';

$script = 'bbs20.cgi';
$base = './bbsdata';				#データ格納ディレクトリ
$bbsfile = "$base/bbs.txt";			#記事
$nofile = "$base/no.txt";			#記事番号
$opfile = "$base/option.txt";		#オプション
$lockfile = "$base/lock";			#ロック
$cgi_lib'maxdata = 300000;			#入力最大容量（byte）

open (IN,"$opfile") || &error("OPEN ERROR");	$opdata = <IN>;		close IN;
if (!$opdata) {
	$pass = &crypt('cgi');
	chmod(0666,$opfile);	open (OUT,">$opfile") || &error("OPEN ERROR");
	print OUT "$pass<>掲示板<><><><>$base<>$base<><>/usr/lib/sendmail<>#ffffff<>#000000<>#800000<>#c00000<>#0000ff<>#eeeeee<>#fef5da<>#ffffff<>#aaaaaa<>24<>5<>50<>1<>200<>200";
	close OUT;
	chmod(0666,$bbsfile);	chmod(0666,$nofile);
}

###　メイン処理　###
&ReadParse;
while (($n,$val) = each %in) {
	if ($n eq 'img') {next;}
	$val =~ s/&/&amp;/g; $val =~ s/</&lt;/g; $val =~ s/>/&gt;/g; $val =~ s/"/&quot;/g;
	$in{$n} = $val;
}
$mode = $in{'mode'};
$wrt = $in{'wrt'};
$num = $in{'num'};

open (IN, "$opfile") || &error("OPEN ERROR");
($pass,$title,$com_adm,$home,$bg_img,$savedir,$loaddir,$adm_mail,$sendmail,$bg_color,$text_color,$title_color,$sub_color,$name_color,$subbg_color,$new_color,$com_color,$frame_color,$newhour,$page,$logmax,$img_dsp,$max_w,$max_h) = split(/<>/,<IN>);
close IN;
if (!$page) {$page = 5;}

$nowtime = time;
$newtime = $nowtime - $newhour * 3600;

if ($mode eq 'resin') {&resin;}
elsif ($mode eq 'edtin') {&edtin;}
elsif ($mode eq 'admin') {&admin;}
else {&main;}

print "</center></body></html>\n";
exit;

###
sub header {
	print "Content-type: text/html\n\n";
	print "<html><head><META HTTP-EQUIV=\"Content-type\" CONTENT=\"text/html; charset=Shift_JIS\">\n";
	print "<title>$title</title><link rel=\"stylesheet\" type=\"text/css\" href=\"$loaddir/style.css\"></head>\n";
	$head = 1;
}

###
sub main {
	if ($wrt eq 'new') {&newwrt;}
	elsif ($wrt eq 'edt') {&edtwrt;}
	elsif ($wrt eq 'del') {&delwrt;}

	&header;
	print "<BODY bgcolor=\"#FFCC33\" text=\"#663300\" link=\"#3366FF\" vlink=\"#996600\" alink=\"#00CCFF\" background=\"../../bu02_bg.gif\">
<EMBED SRC=\"../../bgm/kentuckynowagaya.mid\" HEIGHT=\"2\" WIDTH=\"2\" AUTOSTART=\"true\" REPEAT=\"true\" LOOP=\"false\" TYPE=\"audio/midi\"><center>\n";
	print "<table width=97%><tr><td width=80 valign=top>";
	if ($home) {print "<a href=\"$home\">[山崎屋に戻る]</a>";}
	print "</td><td align=center><font color=\"$title_color\" size=\"+1\"><b>$title</b></font></td><td width=80></td></tr></table>\n";

	if (!$num) {
		if ($com_adm) {print "<table><tr><td>$com_adm</td></tr></table>\n";}
		&getcook;
		&in_form;
	}
	$back = $num - $page;
	$next = $num + $page;
	$sum = 0;
	$m = -1;
	$start = 1;
	open (IN,"$bbsfile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$rno,$time,$date,$img_type,$img_w,$img_h,$img_big,$name,$mail,$hp,$sub,$com) = split(/<>/);
		$sum++;
		if (!$rno) {$m++;}
		if ($m < $num || $next <= $m) {next;}
		if (!$rno && !$start) {print "</td></tr></table></td></tr></table>\n";}
		&bbs_dsp;
		$start = 0;
	}
	close IN;
	print "</td></tr></table></td></tr></table>\n";

	print "<br><table width=88% cellpadding=0 cellspacing=0><tr>\n";
	if (0 <= $back) {
		print "<td width=55><form action=\"$script\" method=POST>\n";
		print "<input type=hidden name=num value=\"$back\">\n";
		print "<input class=submit type=submit value=\"１ページ戻る\"></td></form>\n";
	}
	if ($next <= $m) {
		print "<td width=55><form action=\"$script\" method=POST>\n";
		print "<input type=hidden name=num value=\"$next\">\n";
		print "<input class=submit type=submit value=\"１ページ進む\"></td></form>\n";
	}
	if ($page <= $m) {
		print "<td><table bgcolor=\"#aaaaaa\" cellspacing=1 cellpadding=2><tr bgcolor=\"#ffffff\" align=center>\n";
		$i = 1;
		for ($k=0; $k<=$m; $k+=$page) {
			if ($k == $num) {print "<td width=30 bgcolor=\"#ffff00\"><font color=\"#000000\">P$i</font></td>\n";}
			else {print "<td width=30><a href=\"$script?num=$k\"><font color=\"#000000\">P$i</font></a></td>\n";}
			$i++;
		}
		print "</tr></table></td>\n";
	}
	print "<td align=right>全 $sum件　　<a href=\"$script?mode=admin\">[管理]</a></td></tr></table>\n";
	# 次の行は著作権表示ですので削除しないで下さい。#
	print "<a href=\"http://merlion.cool.ne.jp/cgi/\" target=\"_blank\"><font size=\"-1\">CGI-design</font></a>\n";
}

###
sub bbs_dsp {
	if (!$rno || $mode eq 'edtin') {
		print "<br><table width=90% bgcolor=\"$frame_color\" cellspacing=1 cellpadding=2><tr align=center><td bgcolor=\"$com_color\">\n";
		if ($rno) {print "<table width=92% cellspacing=0><tr><td>";}
	} else {print "<hr size=1>\n";}

	if ($newtime < $time) {$bc_head = $new_color;} else {$bc_head = $subbg_color;}
	print "<table width=100% bgcolor=\"$bc_head\" cellspacing=3 cellpadding=0><tr>\n";
	print "<td valign=bottom>　<font color=\"$sub_color\"><b>$sub</b></font>　　<font color=\"$name_color\"><b>$name</b></font>　　 $date\n";
	if ($hp) {print "　<a href=\"http://$hp\" target=\"_blank\">HP</a>\n";}
	if ($mail) {print "　<a href=\"mailto:$mail\">Mail</a>";}
	print "</td>\n";

	if (!$mode) {
		if (!$rno) {
			print "<td width=40><form action=\"$script\" method=POST>\n";
			print "<input type=hidden name=mode value=\"resin\">\n";
			print "<input type=hidden name=no value=\"$no\">\n";
			print "<input class=submit type=submit value=\"返信\"></td></form>\n";
		}
		print "<td width=40 align=right><a href=\"$script?mode=edtin&no=$no&num=$num\"><font size=\"-1\">[修正]<br>[削除]</font></a></td>";
	}
	print "</tr></table>\n";
	if (!$rno) {print "<table width=92% cellspacing=0><tr><td>\n";}

	print "<table width=100% cellpadding=8><tr>\n";
	if ($img_type) {
		print "<td width=100 align=center>";
		$imgfile = "$loaddir/$no.$img_type";
		$imgsrc = "<img src=\"$imgfile\" border=0 width=$img_w height=$img_h hspace=5>";
		if ($img_big) {print "<a href=\"$imgfile\" target=\"_blank\">$imgsrc</a>";} else {print $imgsrc;}
		print "</td>\n";
	}
	$com =~ s/([^=^\"]|^)(http\:[\w\.\~\-\/\?\&\+\=\:\@\%\;\#\%]+)/$1<a href=\"$2\" target=\"_blank\">$2<\/a>/g;
	print "<td valign=top>$com</td></tr></table>\n";
}

###
sub in_form {
	print "<br><table cellspacing=1 cellpadding=0>\n";
	print "<form action=\"$script\" method=POST enctype=\"multipart/form-data\">\n";
	if (!$mode) {
		print "<input type=hidden name=wrt value=\"new\">\n";
		&getcook;
		$sub=$com='';
		$submit = '投稿する';
	} elsif ($mode eq 'resin') {
		print "<input type=hidden name=wrt value=\"new\">\n";
		print "<input type=hidden name=rno value=\"$in{'no'}\">\n";
		&getcook;
		$sub=$com='';
		$submit = '返信する';
	} else {
		print "<input type=hidden name=wrt value=\"edt\">\n";
		print "<input type=hidden name=no value=\"$in{'no'}\">\n";
		print "<input type=hidden name=num value=\"$num\">\n";
		print "<input type=hidden name=edtpw value=\"$in{'pwd'}\">\n";
		$com =~ s/<br>/\r/g;
		$pwd = '';
		$submit = '修正する';
	}
	print "<tr><td>お名前</td><td><input class=text type=text name=name size=40 value=\"$name\">　（全角で）</td></tr>\n";
#	print "<tr><td>メール</td><td><input class=text type=text name=mail size=40 value=\"$mail\">　（空欄可）</td></tr>\n";
#	print "<tr><td>Ｈ　Ｐ</td><td><input class=text type=text size=70 name=hp value=\"http://$hp\">　（空欄可）</td></tr>\n";
	print "<tr><td>題　名</td><td><input class=text type=text name=sub size=70 value=\"$sub\"></td></tr>\n";
	print "<tr><td valign=top><br>内　容</td><td><textarea class=text cols=78 rows=8 name=com wrap=\"soft\">$com</textarea></td></tr>\n";
	if ($img_dsp) {print "<tr><td>お写真</td><td><input class=text type=file name=img size=60>　（jpg/gif/png 300KB以内）</td></tr>\n";}
	print "<tr><td>修正キー</td><td><input class=text type=password name=pwd size=10 maxlength=8 value=\"$pwd\">　（投稿した記事を修正削除する際に必要 英数8文字以内　例：abc）</td></tr>\n";
	print "<tr><td></td><td><table width=100% cellspacing=0 cellpadding=2><tr><td><input class=submit type=submit value=\"$submit\"></td></form>\n";
	if ($mode eq 'edtin') {
		print "<td width=40 bgcolor=red align=center>\n";
		print "<form action=\"$script\" method=POST>\n";
		print "<input type=hidden name=wrt value=\"del\">\n";
		print "<input type=hidden name=no value=\"$in{'no'}\">\n";
		print "<input type=hidden name=num value=\"$num\">\n";
		print "<input type=hidden name=edtpw value=\"$in{'pwd'}\">\n";
		print "<input class=submit type=submit value=\"削除\"></td></form>\n";
	}
	print "</tr></table></td></tr></table><br>\n";
}

###
sub newwrt {
	&in_chk;
	$ENV{'TZ'} = "JST-9";
	$times = time;
	($min,$hour,$mday,$mon,$year,$wday) = (localtime($times))[1..6];
#	$week = ('日','月','火','水','木','金','土');
#	$nowdate = sprintf("%04d/%02d/%02d(%s) %02d:%02d",
#			$year+1900,$mon+1,$mday,$week[$wday],$hour,$min);

#	($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime;
	$year += 1900;
	$mon++;
	$week = ('日','月','火','水','木','金','土')[$wday];
	$nowdate = sprintf("$year年$mon月$mday日($week) $hour:%02d",$min);

	if ($in{'pwd'} ne '') {$pwd = &crypt($in{'pwd'});} else {$pwd = '';}
	$addr = $ENV{'REMOTE_ADDR'};

	&lock;
	open (IN,"$nofile") || &error("OPEN ERROR"); 		$no = <IN>; 		close IN;
	$no++;
	open (OUT,">$nofile") || &error("OPEN ERROR");		print OUT $no;		close OUT;
	&img("$savedir/$no",'img');
	$newdata = "$no<>$in{'rno'}<>$nowtime<>$nowdate<>$type<>$width<>$height<>$big<>$in{'name'}<>$in{'mail'}<>$in{'hp'}<>$in{'sub'}<>$in{'com'}<>$pwd<>$addr<>\n";

	if (!$in{'rno'}) {
		$i=$stop=0;
		open (IN,"$bbsfile") || &error("OPEN ERROR");
		while (<IN>) {
			($no,$rno,$time,$date,$img_type) = split(/<>/);
			$i++;
			if ($logmax && $logmax <= $i && !$rno) {$stop = 1;}
			if (!$stop) {push(@new,$_);} elsif ($img_type) {unlink "$savedir/$no.$img_type";}
		}
		close IN;
		unshift(@new,$newdata);
	} else {
		$mat = 0;
		open (IN,"$bbsfile") || &error("OPEN ERROR");
		while (<IN>) {
			($no,$rno) = split(/<>/);
			if ($no eq $in{'rno'}) {$mat = 1; push(@new,$_);}
			elsif ($rno eq $in{'rno'}) {push(@new,$_);}
			else {push(@tmp,$_);}
		}
		close IN;
		if ($mat) {push(@new,$newdata);}
		push(@new,@tmp);
	}
	open (OUT,">$bbsfile") || &error("OPEN ERROR");		print OUT @new;		close OUT;
	&unlock;
	&setcook;
	&send;
}

###
sub in_chk {
	if (!$in{'name'}) {&error("名前を入力して下さい");}
#スパム対策（半角のみの名前は投稿拒否）として以下の３行を追加
		$m,$c;
		($m,$c) = &jcode::getcode(\$in{'name'});
		if(($m and !($c eq 'sjis' or ! $c)) or ! $m) {&error("半角の名前では投稿はできません。名前は全角で入力してください");}
	if (!$in{'com'}) {&error("内容を入力して下さい");}
	$in{'com'} =~ s/\r\n|\r|\n/<br>/g;
	$in{'hp'} =~ s/^http\:\/\///;
}

###
sub resin {
	&header;
	print "<body background=\"$bg_img\" bgcolor=\"$bg_color\" text=\"$text_color\"><center>\n";
	print "<table width=97%><tr><td><a href=\"$script\">[Return]</a></td></tr></table>\n";
	print "<b>*****　返　信　*****</b><br>\n";
	$mat = 0;
	open (IN,"$bbsfile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$rno,$time,$date,$img_type,$img_w,$img_h,$img_big,$name,$mail,$hp,$sub,$com) = split(/<>/);
		if ($no eq $in{'no'} || $rno eq $in{'no'}) {$mat = 1; &bbs_dsp;}
		elsif ($mat) {last;}
	}
	close IN;
	print "</td></tr></table></td></tr></table><br><hr width=90%>\n";
	&in_form;
}

###
sub edtin {
	&header;
	print "<body background=\"$bg_img\" bgcolor=\"$bg_color\" text=\"$text_color\"><center>\n";
	print "<table width=97%><tr><td><a href=\"$script\">[Return]</a></td></tr></table>\n";

	open (IN,"$bbsfile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$rno,$time,$date,$img_type,$img_w,$img_h,$img_big,$name,$mail,$hp,$sub,$com,$pwd,$regaddr) = split(/<>/);
		if ($no eq $in{'no'}) {last;}
	}
	close IN;

	if ($regaddr ne $ENV{'REMOTE_ADDR'}) {
		if ($in{'pwd'} eq '') {
			print "<br><br><br><br><h4>修正キーを入力して下さい</h4>\n";
			print "<form action=\"$script\" method=\"POST\">\n";
			print "<input type=hidden name=mode value=\"edtin\">\n";
			print "<input type=hidden name=no value=\"$in{'no'}\">\n";
			print "<input type=hidden name=num value=\"$num\">\n";
			print "<input type=password name=pwd size=10 maxlength=8>\n";
			print "<input type=submit value=\"入力\"></form>\n";
			print "</center></body></html>\n";
			exit;
		}
		$mat = &decrypt($in{'pwd'},$pass);
		if (!$mat) {
			if ($pwd eq '') {&error("該当の記事に修正キーが設定されていません");}
			$mat = &decrypt($in{'pwd'},$pwd);
			if (!$mat) {&error("修正キーが違います");}
		}
	}
	print "<b>*****　修正・削除　*****</b><br>\n";
	$wcom = $com;
	&bbs_dsp;
	print "</td></tr></table></td></tr></table><br><hr width=90%>\n";
	$com = $wcom;
	&in_form;
}

###
sub in_pwchk {
	open (IN,"$bbsfile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$rno,$time,$date,$img_type,$img_w,$img_h,$img_big,$name,$mail,$hp,$sub,$com,$pwd,$regaddr) = split(/<>/);
		if ($no eq $in{'no'}) {last;}
	}
	close IN;
	if ($regaddr eq $ENV{'REMOTE_ADDR'}) {return;}

	if ($in{'edtpw'} eq '') {&error;}
	$mat = &decrypt($in{'edtpw'},$pass);
	if ($mat) {return;}

	if ($pwd eq '') {&error;}
	$mat = &decrypt($in{'edtpw'},$pwd);
	if (!$mat) {&error;}
}

###
sub edtwrt {
	&in_pwchk;
	&in_chk;
	&img("$savedir/$in{'no'}",'img');

	&lock;
	open (IN, "$bbsfile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$rno,$time,$date,$img_type,$img_w,$img_h,$img_big,$name,$mail,$hp,$sub,$com,$pwd,$regaddr) = split(/<>/);
		if ($no eq $in{'no'}) {
			if ($type) {$img_type = $type; $img_w = $width; $img_h = $height; $img_big = $big;}
			if ($in{'pwd'} ne '') {$pwd = &crypt($in{'pwd'});}
			push(@new,"$no<>$rno<>$time<>$date<>$img_type<>$img_w<>$img_h<>$img_big<>$in{'name'}<>$in{'mail'}<>$in{'hp'}<>$in{'sub'}<>$in{'com'}<>$pwd<>$regaddr<>\n");
		} else {push(@new,$_);}
	}
	close IN;
	open (OUT,">$bbsfile") || &error("OPEN ERROR");		print OUT @new;		close OUT;
	&unlock;
}

###
sub delwrt {
	&in_pwchk;
	&lock;
	open (IN,"$bbsfile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$rno,$time,$date,$img_type) = split(/<>/);
		if ($no eq $in{'no'} || $rno eq $in{'no'}) {if ($img_type) {unlink "$savedir/$no.$img_type";}}
		else {push(@new,$_);}
	}
	close IN;
	open (OUT,">$bbsfile") || &error("OPEN ERROR");		print OUT @new;		close OUT;
	&unlock;
}

###
sub admin {
	&header;
	print "<body><center>\n";
	print "<table width=97%><tr><td><a href=\"$script\">[Return]</a></td></tr></table>\n";
	$inpass = $in{'pass'};
	if ($inpass eq '') {
		print "<br><br><br><br><h4>パスワードを入力して下さい</h4>\n";
		print "<form action=\"$script\" method=\"POST\">\n";
		print "<input type=hidden name=mode value=\"admin\">\n";
		print "<input type=password name=pass size=10 maxlength=8>\n";
		print " <input type=submit value=\" 認証 \"></form>\n";
		print "</center></body></html>\n";
		exit;
	}
	$mat = &decrypt($inpass,$pass);
	if (!$mat) {&error("パスワードが違います");}

	if ($wrt) {
		if ($in{'newpass'} ne '') {$pass = &crypt($in{'newpass'});}
		$title = $in{'title'};
		$com_adm = $in{'com'};	$com_adm =~ s/\r\n|\r|\n/<br>/g;
		$home = $in{'home'};
		$bg_img = $in{'bg_img'};
		$savedir = $in{'savedir'};
		$loaddir = $in{'loaddir'};
		$adm_mail = $in{'adm_mail'};
		$sendmail = $in{'sendmail'};

		$bg_color = $in{'color0'};
		$text_color = $in{'color1'};
		$title_color = $in{'color2'};
		$sub_color = $in{'color3'};
		$name_color = $in{'color4'};
		$subbg_color = $in{'color5'};
		$new_color = $in{'color6'};
		$com_color = $in{'color7'};
		$frame_color = $in{'color8'};

		$newhour = $in{'newhour'};
		$page = $in{'page'};
		$logmax = $in{'logmax'};
		$img_dsp = $in{'img_dsp'};
		$max_w = $in{'max_w'};
		$max_h = $in{'max_h'};

		open (OUT, ">$opfile") || &error("OPEN ERROR");
		print OUT "$pass<>$title<>$com_adm<>$home<>$bg_img<>$savedir<>$loaddir<>$adm_mail<>$sendmail<>$bg_color<>$text_color<>$title_color<>$sub_color<>$name_color<>$subbg_color<>$new_color<>$com_color<>$frame_color<>$newhour<>$page<>$logmax<>$img_dsp<>$max_w<>$max_h";
		close OUT;
	}
	print "下記に入力後、「設定する」を押して下さい。<br>\n";
	print "<form action=\"$script\" method=POST>\n";
	print "<input type=hidden name=mode value=\"admin\">\n";
	print "<input type=hidden name=pass value=\"$inpass\">\n";
	print "<input type=submit name=wrt value=\"設定する\"><br><br>\n";

	print "<table bgcolor=\"#dddddd\" cellspacing=10><tr><td><table cellspacing=1 cellpadding=0>\n";
	print "<tr><td><b>タイトル</b></td><td><input type=text name=title size=40 value=\"$title\"></td></tr>\n";
	$com_adm =~ s/<br>/\r/g;
	print "<tr><td valign=top><b>コメント</b></td><td><textarea cols=68 rows=8 name=com wrap=\"soft\">$com_adm</textarea></td></tr>\n";
	print "<tr><td><b>ホームURL</b></td><td><input type=text size=60 name=home value=\"$home\"></td></tr>\n";
	print "<tr><td><b>壁紙</b></td><td><input type=text size=60 name=bg_img value=\"$bg_img\"></td></tr>\n";
	print "<tr><td><b>画像格納ディレクトリ</b></td><td><input type=text size=60 name=savedir value=\"$savedir\"></td></tr>\n";
	print "<tr><td><b>画像読出ディレクトリ</b></td><td><input type=text size=60 name=loaddir value=\"$loaddir\"></td></tr>\n";
	print "<tr><td><b>受信メールアドレス</b></td><td><input type=text size=40 name=adm_mail value=\"$adm_mail\"></td></tr>\n";
	print "<tr><td><b>sendmail パス</b></td><td><input type=text size=40 name=sendmail value=\"$sendmail\"></td></tr>\n";

	print "<tr><td></td><td><a href=\"$loaddir/color.htm\" target=\"_blank\">カラーコード</a></td></tr>\n";
	@name = ('基本背景色','基本文字色','タイトル色','題名色','名前色','見出し帯色','新記事の見出し帯色','記事背景色','記事枠色');
	@data = ($bg_color,$text_color,$title_color,$sub_color,$name_color,$subbg_color,$new_color,$com_color,$frame_color);
	for (0 .. $#name) {
		print "<tr><td><b>$name[$_]</b></td><td><table cellspacing=0 cellpadding=0><tr>\n";
		print "<td><input type=text name=color$_ size=10 value=\"$data[$_]\"></td>\n";
		print "<td width=5></td><td width=80 bgcolor=\"$data[$_]\"></td></tr></table></td></tr>\n";
	}
	print "<tr><td><b>新記事の表\示時間</b></td><td><input type=text name=newhour size=4 value=\"$newhour\" style=\"text-align: right\">時間</td></tr>\n";
	print "<tr><td><b>記事表\示</b></td><td><input type=text name=page size=4 value=\"$page\" style=\"text-align: right\">件/ページ（親記事数）　　　最大<input type=text name=logmax size=4 value=\"$logmax\" style=\"text-align: right\">件（親/返信記事の合計）</td></tr>\n";
	if ($img_dsp) {$chk = ' checked';} else {$chk = '';}
	print "<tr><td><b>画像表\示</b></td><td><input type=checkbox name=img_dsp value=\"1\"$chk>入力する　　　横max<input type=text name=max_w size=4 value=\"$max_w\" style=\"text-align: right\">px、 縦max<input type=text name=max_h size=4 value=\"$max_h\" style=\"text-align: right\">px</td></tr>\n";
	print "<tr><td><b>パスワード変更</b></td><td><input type=password name=newpass size=10 maxlength=8> （英数8文字以内）</td></tr>\n";
	print "</table></td></tr></table></form>\n";
}

###
sub img {
	$type=$width=$height=$big=$mac='';
	$imgdata = $in{"$_[1]"};
	if (!$imgdata) {return;}

	foreach (@in) {
		if ($_ =~ /$_[1]/ and $_ =~ /Content-Type:(.+)/i) {
			if ($1 =~ /image\/.*jpeg/i) {$type = 'jpg';}
			elsif ($1 =~ /image\/gif/i) {$type = 'gif';}
			elsif ($1 =~ /image\/.*png/i) {$type = 'png';}
		}
		if ($_ =~ /application\/x-macbinary/i) {$mac = 1;}
	}
	if (!$type) {&error("このファイルはアップロードできません。");}

	if ($mac) {
		$leng = substr($imgdata,83,4);
		$leng = unpack("%N",$leng);
		$imgdata = substr($imgdata,128,$leng);
	}
	$img_file = "$_[0].$type";
	open (IMG,">$img_file") || &error("$img_file画像ファイルを作成できません");
	binmode IMG;
	print IMG $imgdata;
	close IMG;
	chmod (0666,$img_file);

	($t,$width,$height) = &getImageSize("$img_file");
	if (!$width || !$height) {&error("ファイルを認識できません。");}

	$big = 0;
	if ($max_w && $max_w < $width) {$rate_w = $max_w / $width; $big = 1;} else {$rate_w = 1;}
	if ($max_h && $max_h < $height) {$rate_h = $max_h / $height; $big = 1;} else {$rate_h = 1;}
	if ($big) {
		if ($rate_w < $rate_h) {$rate = $rate_w;} else {$rate = $rate_h;}
		$width = int($width * $rate);
		$height = int($height * $rate);
	}
}

#=========================================
# Get Image Pixel Size.（出典：stdio-902）
#=========================================
sub getImageSize {
	local($file_name) = @_;
	local($head);

	return if (!open IMG, $file_name);
	binmode IMG;
	read IMG, $head, 8;
	if ($head eq "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a") {
		local($width, $height);
		if (read(IMG, $head, 4) != 4 || read(IMG, $head, 4) != 4 || $head ne 'IHDR') {
			close IMG;
			return "PNG", 0;
		}
		read IMG, $head, 8;
		close IMG;
		$width = unpack "N", substr($head, 0, 4);
		$height = unpack "N", substr($head, 4, 4);
		return "PNG", $width, $height;
	}
	$head = substr $head, 0, 3;
	if ($head eq "\x47\x49\x46") {
		local($head, $width, $height);
		seek IMG, 6, 0;
		read IMG, $head, 4;
		close IMG;
		($width, $height) = unpack "vv", $head;
		return "GIF", $width, $height;
	}
	$head = substr $head, 0, 2;
	if ($head eq "\xff\xd8") {
		local($head, $width, $height, $w1, $w2, $h1, $h2, $l1, $l2, $length);
		seek IMG, 2, 0;
		while (read IMG, $head, 1) {
			last if ($head eq "");
			if ($head eq "\xff") {
				$head = getc IMG;
				if ($head =~ /^[\xc0-\xc3\xc5-\xcf]$/) {
					seek IMG, 3, 1;
					last if (read(IMG, $head, 4) != 4);
					close IMG;
					($h1, $h2, $w1, $w2) = unpack "C4", $head;
					$height = $h1 * 256 + $h2;
					$width  = $w1 * 256 + $w2;
					return "JPG", $width, $height;
				} elsif ($head eq "\xd9" || $head eq "\xda") {
					last;
				} else {
					last if (read(IMG, $head, 2) != 2);
					($l1, $l2) = unpack "CC", $head;
					$length = $l1 * 256 + $l2;
					seek IMG, $length - 2, 1;
				}
			}
		}
		close IMG;
		return "JPG", 0;
	}
	return 0;
}

###
sub send {
	if (!$adm_mail) {return;}
#	$cont = "■投稿日時： $nowdate\n";
	$cont .= "■名前： $in{'name'}\n";
#	$cont .= "■メール： $in{'mail'}\n";
#	$cont .= "■ＨＰ： http://$in{'hp'}\n";
	$cont .= "■題名： $in{'sub'}\n";
	$cont .= "$in{'com'}";
#	$cont .= "■内容： \n$in{'com'}";
	$cont =~ s/&amp;/&/g;
	$cont =~ s/&lt;/</g;
	$cont =~ s/&gt;/>/g;
	$cont =~ s/&quot;/"/g;
	$cont =~ s/\r\n|\r|<br>/\n/g;
	&jcode'convert(*cont,'jis');

	$sub = &mimeencode("山崎屋掲示板に投稿がありました！");
#	$sub = &mimeencode($title);
	$in{'name'} =~ s/ //g;
#	if (!$in{'mail'}) {$in{'mail'} = $adm_mail;}
	$from = &mimeencode("$in{'name'} <$adm_mail>");
	if (!open(MAIL,"| $sendmail -t")) {&error("メールを送信できません");}
	print MAIL "To: $adm_mail\n";
#	print MAIL "Bcc: $adm_mail2\n";
#	print MAIL "Bcc: $adm_mail3\n";
#	print MAIL "Bcc: $adm_mail4\n";
	print MAIL "From: $from\n";
	print MAIL "Subject: $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 "$cont\n";
	close(MAIL);
}

###
sub setcook {
	my($sec,$min,$hour,$mday,$mon,$year,$wday) = gmtime(time+60*24*60*60);
	$ww = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[$wday];
	$month = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon];
	$gmt = sprintf("%s, %02d-%s-%04d %02d:%02d:%02d GMT",$ww,$mday,$month,$year+1900,$hour,$min,$sec);
	$cook = "$in{'name'}<>$in{'mail'}<>$in{'hp'}<>$in{'pwd'}<>";
	print "Set-Cookie: bbs20=$cook; expires=$gmt;\n";
}

###
sub getcook {
	if ($wrt eq 'new') {
		$name = $in{'name'};
		$mail = $in{'mail'};
		$hp = $in{'hp'};
		$pwd = $in{'pwd'};
		return;
	}
	my($n,$val);
	foreach (split(/;\s*/,$ENV{'HTTP_COOKIE'})) {
		($n,$val) = split(/=/);
		if ($n eq 'bbs20') {last;}
		$val = '';
	}
	($name,$mail,$hp,$pwd) = split(/<>/,$val);
}

###
sub lock {
	$retry = 3;
	if (-e $lockfile) {
		$locktime = (stat($lockfile))[9];
		if ($locktime < time - 60) {&unlock;}
	}
	while (!mkdir($lockfile, 0755)) {
		if (--$retry < 0) {&error("busy!");}
		sleep(1);
	}
}
###
sub unlock {rmdir($lockfile);}

###
sub crypt {
	@salt = ('a' .. 'z','A' .. 'Z','0' .. '9');
	srand;
	$salt = "$salt[int(rand($#salt))]$salt[int(rand($#salt))]";
	return crypt($_[0],$salt);
}

###
sub decrypt {
	$salt = $_[1] =~ /^\$1\$(.*)\$/ && $1 || substr($_[1],0,2);
	if (crypt($_[0],$salt) eq $_[1] || crypt($_[0],'$1$' . $salt) eq $_[1]) {return 1;}
	return 0;
}

###
sub error {
	if (!$head) {&header; print "<body><center>\n";}
	print "<br><br><br><br><h3>ERROR !!</h3><font color=red><b>$_[0]</b></font>\n";
	print "</center></body></html>\n";
	exit;
}
