#!/usr/bin/perl

###############################################
#   book.cgi
#      V1.0 (2013.4.17)
#                     Copyright(C) CGI-design
#  本文ポイントを16ポイントに変更（###変更の前の行の$fsize pxを16pxに変更）
###############################################

require './cgi-lib.pl';

$script = 'book.cgi';
$base = './bookdata';				#データ格納ディレクトリ
$bookfile = "$base/book.txt";		#記事
$nofile = "$base/no.txt";			#記事番号
$catfile = "$base/cat.txt";			#評価項目
$cnofile = "$base/cno.txt";			#評価項目番号
$estfile = "$base/est.txt";			#5段階評価
$opfile = "$base/option.txt";
$cgi_lib'maxdata = 1000000;			#入力最大容量（byte）

@degsub = ('専門的','やや専門的','一般向け','中学生','小学生');
@figsub = ('図版主体','図・文半々','文章主体');

open (IN,"$opfile") || &error("OPEN ERROR");	$opdata = <IN>;		close IN;
if (!$opdata) {
	$pass = &crypt('cgi');
	open(OUT,">$opfile") || &error("OPEN ERROR");
	print OUT "$pass<><><><>annai.html<>$base<>$base<>#f0f8ff,#000000,#ffffff,#4169e1,#005100,#D4D4A6<>50<>16<>200<>400";
	close OUT;
}

###　メイン処理　###
&ReadParse;
while (($n,$val) = each %in) {
	if ($n =~ /^img\d*$/) {next;}
	$val =~ s/&/&amp;/g;	$val =~ s/</&lt;/g;		$val =~ s/>/&gt;/g;		$val =~ s/"/&quot;/g;	$val =~ s/\r\n|\r|\n/<br>/g;
	$val =~ s/\0/,/g;
	$in{$n} = $val;
}
$mode = $in{'mode'};
$num = $in{'num'};

open (IN,"$opfile") || &error("OPEN ERROR");
($pass,$title,$com_adm,$home,$guide,$savedir,$loaddir,$colors,$page,$fsize,$max_wh,$menuw) = split(/<>/,<IN>);
close IN;
($bg_color,$text_color,$title_color,$titlebg_color,$sub_color,$mbg_color) = split(/,/,$colors);
if ($page == 0) {$page = 50;}

open (IN,"$estfile") || &error("OPEN ERROR");		@estsub = split(/,/,<IN>);		close IN;

if ($mode eq 'main') {&main;}
elsif ($mode eq 'menu') {&menu;}
elsif ($mode eq 'search') {&search;}
elsif ($mode eq 'admin') {&admin;}
else {&frame;}
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 frame {
	&header;
	print "<frameset cols=\"$menuw,*\" border=0 frameborder=0>\n";
	print "<frame src=\"$script?mode=menu&ssea=$in{'ssea'}&sub=$in{'sub'}&csea=$in{'csea'}&com=$in{'com'}&esea=$in{'esea'}&cno=$in{'cno'}&eno=$in{'eno'}\" scrolling=\"auto\">\n";
	print "<frame src=\"$script?mode=main&ssea=$in{'ssea'}&sub=$in{'sub'}&csea=$in{'csea'}&com=$in{'com'}&esea=$in{'esea'}&cno=$in{'cno'}&eno=$in{'eno'}\" name=\"book\" scrolling=\"auto\">\n";
	print "</frameset>\n";
	print "</html>\n";
}

###
sub menu {
	&header;
	print "<body bgcolor=\"$mbg_color\" text=\"$text_color\">\n";
	if ($in{'ssea'} || $in{'csea'} || $in{'esea'}) {
		print "<a href=\"$script\" target=\"_parent\">トップ画面に戻る</a>";
	} else {
		print "<a href=\"$home\" target=\"_parent\">福島原発行動隊 HOME</a>";
	}
	print "<br><a href=\"$script?mode=search&sub=$in{'sub'}&com=$in{'com'}&cno=$in{'cno'}&eno=$in{'eno'}\" target=\"_parent\">検索ページ</a><br>\n";
	print "<a href=\"$guide\" target=\"_parent\">執筆者紹介など</a><br><br>\n";

	&seaset;
	$back = $num - $page;
	$next = $num + $page;
	$m = -1;
	@data = ();
	open (IN,"$bookfile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$year,$mon,$day,$img,$sub,$deg,$fig,$est,$com) = split(/<>/);
		&seafind;
		if (!$find) {next;}
		$m++;
		if ($num <= $m && $m < $next) {push(@data,$_);}
	}
	close IN;
	print "教材リスト　　全 ",$m+1,"件\n";

	foreach (@data) {
		($no,$year,$mon,$day,$img,$sub) = split(/<>/);
		print "<div style=\"border-bottom:1px dotted $text_color; margin-top:5px;\"><a href=\"$script?mode=main&no=$no\" target=\"book\">■$sub</a></div>\n";
	}
	print "<center><div style=\"margin-top:8px;\"></div>\n";
	if (0 <= $back) {print "<a href=\"$script?mode=menu&num=$back&ssea=$in{'ssea'}&sub=$in{'sub'}&csea=$in{'csea'}&com=$in{'com'}&esea=$in{'esea'}&cno=$in{'cno'}&eno=$in{'eno'}\">≪前画面</a> ";}
	if ($page <= $m) {
		$i = 1;
		for ($k=0; $k<=$m; $k+=$page) {
			if ($k == $num) {print "$i. ";} else {print "<a href=\"$script?mode=menu&num=$k&ssea=$in{'ssea'}&sub=$in{'sub'}&csea=$in{'csea'}&com=$in{'com'}&esea=$in{'esea'}&cno=$in{'cno'}&eno=$in{'eno'}\">$i.</a> ";}
			$i++;
		}
	}
	if ($next <= $m) {print "<a href=\"$script?mode=menu&num=$next&ssea=$in{'ssea'}&sub=$in{'sub'}&csea=$in{'csea'}&com=$in{'com'}&esea=$in{'esea'}&cno=$in{'cno'}&eno=$in{'eno'}\">次画面≫</a>";}
	print "</center><br><a href=\"$script?mode=admin\" target=\"_parent\">[管理]</a><script type=\"text/javascript\">
<!--
document.write('<img src=\"http://svcf.jp/book/acs6/acs6.cgi?'+document.referrer+'\" width=1 height=1>');
//-->
</script>\n";
	print "</body></html>\n";
}

###
sub seaset {
	$in{'sub'} =~ s/　/ /g;
	@sub = split(/\s+/,$in{'sub'});
	$in{'com'} =~ s/　/ /g;
	@com = split(/\s+/,$in{'com'});
}

###
sub seafind {
	$find = 1;
	if ($in{'ssea'}) {
		$find = 0;
		foreach $word (@sub) {if (0 <= index($sub,$word)) {$find = 1;} else {$find = 0; last;}}
	} elsif ($in{'csea'}) {
		$find = 0;
		foreach $word (@com) {if (0 <= index($com,$word)) {$find = 1;} else {$find = 0; last;}}
	} elsif ($in{'esea'}) {
		$find = $mat = 0;
		foreach (split(/:/,$est)) {
			($cno,$eno) = split(/-/);
			if ($cno eq $in{'cno'}) {$mat = 1; last;}
		}
		if (!$mat) {return;}
		foreach (split(/,/,$in{'eno'})) {
			if ($eno eq $_) {$find = 1; last;}
		}
	}
}

###
sub main {
	&header;
	print "<body bgcolor=\"$bg_color\" text=\"$text_color\" leftmargin=0 marginwidth=0 topmargin=0 marginheight=0><center>\n";
	print "<table width=100% bgcolor=\"$titlebg_color\"><tr><td align=center>\n";
	print "<table width=600><tr><td><font color=\"$title_color\"><span style=\"font-size: 20pt;\">$title</span><br><br>$com_adm</font></td></tr></table>\n";
	print "</td></tr></table>\n";

	&seaset;
	open (IN,"$bookfile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$year,$mon,$day,$img,$sub,$deg,$fig,$est,$com) = split(/<>/);
		&seafind;
		if (!$find) {next;}
		if (!$in{'no'} || $no eq $in{'no'}) {last;}
	}
	close IN;
	if ($find) {&dsp;} else {print "<br><br><br><br>記事は見つかりませんでした。\n";}
	print "</center></body></html>\n";
}

###
sub dsp {
	@img = split(/!:/,$img);
	$com =~ s/([^=^\"]|^)(https?\:[\w\.\~\-\/\?\&\+\=\:\@\%\;\#\%]+)/$1<a href="$2" target="_blank">$2<\/a>/g;

	print "<div align=right style=\"width: 600px; margin-top: 5px;\"><a href=\"#\" onclick=\"window.print();\">このページを印刷する</a></div>\n";
	print "<table width=600><tr><td><font color=\"$sub_color\" style=\"font-size: 16pt;\"><b>$sub</b></font><br><br>\n";
	if (@img) {
		$k = 0;
		for ($i=0; $i<$#img; $i+=4) {
			if ($img[$i]) {
				$imgfile = "$loaddir/$no-$k.$img[$i]";
				$imgsrc = "<img src=\"$imgfile\" width=$img[$i+1] height=$img[$i+2] border=0 hspace=5>";
				if ($img[$i+3]) {print "<a href=\"$imgfile\" target=\"_blank\">$imgsrc</a>";} else {print $imgsrc;}
			}
			$k++;
		}
		print "<br><br>\n";
	}
	print "<table bgcolor=\"#ffffff\" bordercolor=\"#000000\" border=1 cellspacing=0 cellpadding=2 style=\"border-collapse: collapse;\">\n";
	print "<tr align=center><td width=200 rowspan=2 align=left>内容の程度</td>";
	foreach (@degsub) {print "<td width=72 bgcolor=\"#eeeeee\">$_</td>";}
	print "</tr><tr>\n";
	for (0 .. $#degsub) {
		if ($_ eq $deg) {print "<td align=center>●</td>";} else {print "<td>　</td>";}
	}
	print "</tr></table><div style=\"margin-top:1px;\"></div>\n";

	print "<table bgcolor=\"#ffffff\" bordercolor=\"#000000\" border=1 cellspacing=0 cellpadding=2 style=\"border-collapse: collapse;\">\n";
	print "<tr align=center><td width=200 rowspan=2 align=left>図版類の使用程度<br>(写真・イラスト・表\)</td>";
	foreach (@figsub) {print "<td width=72 bgcolor=\"#eeeeee\">$_</td>";}
	print "</tr><tr>\n";
	for (0 .. $#figsub) {
		if ($_ eq $fig) {print "<td align=center>●</td>";} else {print "<td>　</td>";}
	}
	print "</tr></table><br>\n";

	%est = ();
	foreach (split(/:/,$est)) {
		($cno,$eno) = split(/-/);
		$est{$cno} = $eno;
	}
	print "<table bgcolor=\"#ffffff\" bordercolor=\"#000000\" border=1 cellspacing=0 cellpadding=2 style=\"border-collapse: collapse;\">\n";
	print "<tr align=center><td width=200>内容項目　　＼　　評価</td>";
	foreach (@estsub) {print "<td width=72 bgcolor=\"#eeeeee\" style=\"font-size: 12px;\">$_</td>";}
	print "</tr>\n";

	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$cat) = split(/<>/);
		print "<tr><td>$cat</td>";
		for (0 .. 4) {
			if ($_ eq $est{$cno}) {print "<td align=center>●</td>";} else {print "<td></td>";}
		}
		print "</tr>\n";
	}
	close IN;
	print "</table><br><div style=\"font-size: 16px;\">$com</div></td></tr></table>\n";
}

###変更 上の行のfont-sizeを$fsize pxから16pxに変更
sub search {
	&header;
	print "<body bgcolor=\"$bg_color\" text=\"$text_color\">\n";
	print "<a href=\"$script\">トップ画面に戻る</a><center>\n";
	print "<table><tr><td><div style=\"font-size: 20pt;\">検索ページ</div>\n";

	print "<form action=\"$script\" method=POST>\n";
	print "<table>\n";
	print "<tr valign=bottom><td><b style=\"font-size: 16px;\">題名検索</b><br><input type=text size=30 name=sub value=\"$in{'sub'}\" style=\"ime-mode:active;\">　<input type=submit name=ssea value=\"送信\"></td><td width=30></td><td><div style=\"padding: 3px 10px; border: solid 1px #000000;\">書籍・資料のタイトルで検索します</div></td></tr>\n";
	print "<tr valign=bottom><td><b style=\"font-size: 16px;\">全文検索</b><br><input type=text size=30 name=com value=\"$in{'com'}\" style=\"ime-mode:active;\">　<input type=submit name=csea value=\"送信\"></td><td width=30></td><td><div style=\"padding: 3px 10px; border: solid 1px #000000;\">著者名、出版社名等で検索します　</div></td></tr>\n";
	print "</table><br>\n";

	print "<table width=100% cellspacing=10 style=\"border: solid 1px #000000;\"><tr valign=top>\n";
	print "<td width=270><b style=\"font-size: 16px;\">内容項目検索</b><br><select name=cno>\n";
	print "<option value=\"\">内容項目を選ぶ</option>\n";
	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$cat) = split(/<>/);
		if ($cno eq $in{'cno'}) {$sel = ' selected';} else {$sel = '';}
		print "<option value=\"$cno\"$sel>$cat</option>\n";
	}
	close IN;
	print "</select><br><br><br><div style=\"width: 240px; padding: 3px 7px; border: solid 1px #000000;\">内容項目を選択して、ご希望の選択肢<br>にチェックを入れてください</div></td>\n";
	print "<td><b style=\"font-size: 16px;\">選択肢</b><br>\n";

	@eno = ();
	foreach (split(/,/,$in{'eno'})) {$eno[$_] = 1;}
	for (0 .. $#estsub) {
		if ($eno[$_]) {$chk = ' checked';} else {$chk = '';}
		print "<input type=checkbox name=eno value=\"$_\"$chk>$estsub[$_]<br>\n";
	}
	print "</td><td><br><input type=submit name=esea value=\"送信\"></td></tr></table>\n";

	print "</form></td></tr></table>\n";
	print "</center></body></html>\n";
}

###
sub admin {
	&header;
	print "<body><center>\n";
	$inpass = $in{'pass'};
	if ($inpass eq '') {
		print "<table width=97%><tr><td><a href=\"$script\">戻る</a></td></tr></table>\n";
		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 size=10 maxlength=8 name=pass>\n";
		print "<input type=submit value=\" 認証 \"></form>\n";
		print "</center></body></html>\n";
		exit;
	}
	$mat = &decrypt($inpass,$pass);
	if (!$mat) {&error("パスワードが違います");}

	print "<table width=95% bgcolor=\"#8c4600\"><tr><td>　<a href=\"$script\"><font color=\"#ffffff\"><b>ログアウト</b></font></a></td>\n";
	print "<form action=\"$script\" method=POST><td align=right>\n";
	print "<input type=hidden name=mode value=\"admin\">\n";
	print "<input type=hidden name=pass value=\"$inpass\">\n";
	print "<input type=submit value=\"記事編集\">\n";
	print "<input type=submit name=cat value=\"評価項目\">\n";
	print "<input type=submit name=set value=\"基本設定\"></td></form><td width=10></td></tr></table><br>\n";

	if ($in{'cat'}) {&catset;}
	elsif ($in{'set'}) {&setup;}
	else {&edtin;}

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

###
sub edtin {
	if ($in{'edt'}) {&in_form; return;}
	if ($in{'newwrt'}) {&newwrt;}
	elsif ($in{'edtwrt'}) {&edtwrt;}
	elsif ($in{'delwrt'}) {&delwrt;}

	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=hidden name=edt value=\"1\">\n";
	print "<input type=hidden name=num value=\"$num\">\n";
	print "<input type=submit name=new value=\"新規登録\"><br><br>\n";
	print "記事を修正、削除する場合は「修正」を押して下さい。\n";

	$back = $num - $page;
	$next = $num + $page;
	$m = -1;
	print "<table><tr><td>";
	open (IN,"$bookfile") || &error("OPEN ERROR");
	while (<IN>) {
		$m++;
		if ($m < $num) {next;}
		if ($next <= $m) {last;}
		($no,$year,$mon,$day,$img,$sub) = split(/<>/);
		$date = sprintf("$year.%02d.%02d",$mon,$day);
		print "<input type=submit name=$no value=\"修正\"> $date　<a href=\"$script?mode=main&no=$no\" target=\"_blank\">$sub</a><br>\n";
	}
	close IN;
	print "</td></form></tr></table>\n";

	print "<table><tr>";
	if (0 <= $back) {
		print "<form action=\"$script\" method=POST><td>\n";
		print "<input type=hidden name=mode value=\"admin\">\n";
		print "<input type=hidden name=pass value=\"$inpass\">\n";
		print "<input type=hidden name=num value=\"$back\">\n";
		print "<input type=submit value=\"前画面\"></td></form>\n";
	}
	if ($next <= $m) {
		print "<form action=\"$script\" method=POST><td>\n";
		print "<input type=hidden name=mode value=\"admin\">\n";
		print "<input type=hidden name=pass value=\"$inpass\">\n";
		print "<input type=hidden name=num value=\"$next\">\n";
		print "<input type=submit value=\"次画面\"></td></form>\n";
	}
	print "</tr></table>\n";
}

###
sub in_form {
	($sec,$min,$hour,$nowday,$nowmon,$nowyear) = localtime;
	$nowyear += 1900;
	$nowmon++;

	print "<form action=\"$script\" method=POST enctype=\"multipart/form-data\">\n";
	print "<input type=hidden name=mode value=\"admin\">\n";
	print "<input type=hidden name=pass value=\"$inpass\">\n";
	if ($in{'new'}) {
		$year = $nowyear;
		$mon = $nowmon;
		$day = $nowday;
		@img = ();
		$sub = $deg = $fig = $est = $com = '';
	} else {
		open (IN,"$bookfile") || &error("OPEN ERROR");
		while (<IN>) {
			($no,$year,$mon,$day,$img,$sub,$deg,$fig,$est,$com) = split(/<>/);
			if ($in{$no}) {last;}
		}
		close IN;
		print "<input type=hidden name=no value=\"$no\">\n";
		print "<input type=hidden name=num value=\"$num\">\n";
		@img = split(/!:/,$img);
		$com =~ s/<br>/\r/g;	$com =~ s/&/&amp;/g;	$com =~ s/</&lt;/g;		$com =~ s/>/&gt;/g;		$com =~ s/"/&quot;/g;
	}
	print "<table bgcolor=\"#e6e4ce\" cellspacing=10><tr><td><table cellspacing=1 cellpadding=0>\n";
	print "<tr><td>発行日&nbsp;</td><td>\n";
	print "<input type=text size=7 name=year value=\"$year\" style=\"text-align:right; ime-mode:disabled;\">年 ";
	print "<input type=text size=4 name=mon value=\"$mon\" style=\"text-align:right; ime-mode:disabled;\">月 ";
	print "<input type=text size=4 name=day value=\"$day\" style=\"text-align:right; ime-mode:disabled;\">日</td></tr>\n";
	print "<tr><td>題名</td><td><input type=text size=70 name=sub value=\"$sub\" style=\"ime-mode:active;\"></td></tr>\n";

	print "<tr><td>内容の程度</td><td>";
	for (0 .. $#degsub) {
		if ($_ eq $deg) {$chk = ' checked';} else {$chk = '';}
		print "<input type=radio name=deg value=\"$_\"$chk>$degsub[$_]　";
	}
	print "</td></tr>\n";

	print "<tr><td>図版類の使用程度&nbsp;</td><td>";
	for (0 .. $#figsub) {
		if ($_ eq $fig) {$chk = ' checked';} else {$chk = '';}
		print "<input type=radio name=fig value=\"$_\"$chk>$figsub[$_]　";
	}
	print "</td></tr>\n";

	print "<tr><td valign=top><br>評価</td><td><table bgcolor=\"#ffffff\" bordercolor=\"#000000\" border=1 cellspacing=0 cellpadding=2 style=\"border-collapse: collapse;\">\n";
	print "<tr align=center><td>内容項目・評価</td>";
	foreach (@estsub) {print "<td width=80>$_</td>";}
	print "</tr>\n";

	%est = ();
	foreach (split(/:/,$est)) {
		($cno,$eno) = split(/-/);
		$est{$cno} = $eno;
	}
	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$cat) = split(/<>/);
		print "<tr align=center><td align=left>$cat</td>";
		for (0 .. 4) {
			if ($_ eq $est{$cno}) {$chk = ' checked';} else {$chk = '';}
			print "<td><input type=radio name=est$cno value=\"$_\"$chk></td>\n";
		}
		print "</tr>\n";
	}
	close IN;
	print "</table><br></td></tr>\n";

	print "<tr><td valign=top><br>内容</td><td><textarea cols=80 rows=20 name=com style=\"ime-mode:active;\">$com</textarea></td></tr>\n";
	for (0 .. 1) {
		print "<tr><td>画像",$_+1,"</td><td><input type=file size=60 name=img$_>";
		if ($img[4*$_]) {print " <input type=checkbox name=del$_ value=\"1\">削除";}
		print "</td></tr>\n";
	}
	print "<tr><td></td><td>\n";
	if ($in{'new'}) {
		print "<input type=submit name=newwrt value=\"新規登録\">";
	} else {
		print "<table width=100%><tr><td><input type=submit name=edtwrt value=\"修正する\"></td>\n";
		print "<td width=40 bgcolor=red><input type=submit name=delwrt value=\"削除\"></td></tr></table>\n";
	}
	print "</td></tr></table></td></tr></table></form>\n";
}

###
sub newwrt {
	open (IN,"$nofile") || &error("OPEN ERROR"); 		$no = <IN>; 		close IN;
	$no++;
	open (OUT,">$nofile") || &error("OPEN ERROR");		print OUT $no;		close OUT;

	$img = '';
	for (0 .. 1) {
		&img("$savedir/$no-$_","img$_");
		$img .= "$type!:$width!:$height!:$big!:";
	}
	$in{'no'} = $no;
	&logwrt;

	($sec,$min,$hour) = localtime;
	open (OUT,">$base/log$hour.txt") || &error("OPEN ERROR");	print OUT @new;		close OUT;
}

###
sub logwrt {
	$in{'com'} =~ s/&amp;/&/g;		$in{'com'} =~ s/&lt;/</g;		$in{'com'} =~ s/&gt;/>/g;		$in{'com'} =~ s/&quot;/"/g;
	$est = '';
	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno) = split(/<>/);
		$est .= "$cno-$in{\"est$cno\"}:";
	}
	close IN;

	$newdata = "$in{'no'}<>$in{'year'}<>$in{'mon'}<>$in{'day'}<>$img<>$in{'sub'}<>$in{'deg'}<>$in{'fig'}<>$est<>$in{'com'}<>\n";
	$newdate = sprintf("$in{'year'}%02d%02d",$in{'mon'},$in{'day'});
	$flag = 0;
	@new = ();
	open (IN,"$bookfile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$year,$mon,$day) = split(/<>/);
		if ($no eq $in{'no'}) {next;}
		if (!$flag) {
			$date = sprintf("$year%02d%02d",$mon,$day);
			if ($date <= $newdate) {push(@new,$newdata); $flag = 1;}
		}
		push(@new,$_);
	}
	close IN;
	if (!$flag) {push(@new,$newdata);}
	open (OUT,">$bookfile") || &error("OPEN ERROR");		print OUT @new;		close OUT;
}

###
sub edtwrt {
	open (IN,"$bookfile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$year,$mon,$day,$img) = split(/<>/);
		if ($no eq $in{'no'}) {last;}
	}
	close IN;

	@img = split(/!:/,$img);
	$img = '';
	for $n (0 .. 1) {
		if ($in{"del$n"}) {
			unlink "$savedir/$no-$n.$img[4*$n]";
			$img .= "!:!:!:!:";
		} else {
			&img("$savedir/$no-$n","img$n");
			if ($type) {
				if ($type ne $img[4*$n]) {unlink "$savedir/$no-$n.$img[4*$n]";}
				$img .= "$type!:$width!:$height!:$big!:";
			} else {
				$img .= "$img[4*$n]!:$img[4*$n+1]!:$img[4*$n+2]!:$img[4*$n+3]!:";
			}
		}
	}
	&logwrt;
}

###
sub delwrt {
	@new = ();
	open (IN,"$bookfile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$year,$mon,$day,$img) = split(/<>/);
		if ($no eq $in{'no'}) {
			@img = split(/!:/,$img);
			for (0 .. 1) {
				if ($img[4*$_]) {unlink "$savedir/$no-$_.$img[4*$_]";}
			}
		} else {push(@new,$_);}
	}
	close IN;
	open (OUT,">$bookfile") || &error("OPEN ERROR");		print OUT @new;		close OUT;
}

###
sub catset {
	if ($in{'wrt'}) {
		@new = ();
		%num = ();
		open (IN,"$catfile") || &error("OPEN ERROR");		@cat = <IN>;		close IN;
		for (0 .. $#cat) {$num{$_} = $in{"num$_"};}
		foreach (sort {$num{$a} <=> $num{$b}} keys(%num)) {
			($cno) = split(/<>/,$cat[$_]);
			if (!$in{"del$cno"}) {push(@new,"$cno<>$in{\"cat$cno\"}<>\n");}
		}
		open (OUT,">$catfile") || &error("OPEN ERROR");		print OUT @new;		close OUT;

		open (OUT,">$estfile") || &error("OPEN ERROR");		print OUT $in{'estsub'};	close OUT;
		@estsub = split(/,/,$in{'estsub'});
	} elsif ($in{'add'}) {
		open (IN,"$cnofile") || &error("OPEN ERROR");		$cno = <IN>;		close IN;
		$cno++;
		open (OUT,">$cnofile") || &error("OPEN ERROR");		print OUT $cno;		close OUT;
		open (OUT,">>$catfile") || &error("OPEN ERROR");	print OUT "$cno<><>\n";	close OUT;
	}
	print "下記に入力後、「設定する」を押して下さい。<br><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=hidden name=cat value=\"1\">\n";
	print "<input type=submit name=wrt value=\"設定する\">　<input type=submit name=add value=\"項目新設\"><br><br>\n";

	print "<table bgcolor=\"#ddffff\" bordercolor=\"#cdcdcd\" border=1 cellspacing=0 cellpadding=2 style=\"border-collapse: collapse;\">\n";
	print "<tr bgcolor=\"#e6edff\" align=center><td width=50>表\示順</td><td width=290>評価項目</td><td>削除</td></tr>\n";
	$k = 0;
	open (IN,"$catfile") || &error("OPEN ERROR");
	while (<IN>) {
		($cno,$cat) = split(/<>/);
		print "<tr align=center><td><input type=text size=4 name=num$k value=\"",$k+1,"\" style=\"text-align:right; ime-mode:disabled;\"></td>\n";
		print "<td><input type=text size=50 name=cat$cno value=\"$cat\" style=\"ime-mode:active;\"></td>\n";
		print "<td bgcolor=red><input type=checkbox name=del$cno value=\"1\"></td></tr>\n";
		$k++;
	}
	close IN;
	print "</table><br>\n";

	print "<table bgcolor=\"#ddffff\" bordercolor=\"#cdcdcd\" border=1 cellspacing=0 cellpadding=2 style=\"border-collapse: collapse;\">\n";
	print "<tr bgcolor=\"#e6edff\"><td width=140>　　５段階評価</td></tr>\n";
	for (0 .. 4) {print "<tr align=center><td><input type=text name=estsub size=20 value=\"$estsub[$_]\" style=\"ime-mode:active;\"></td></tr>\n";}
	print "</table></form>\n";
}

###
sub setup {
	if ($in{'wrt'}) {
		if ($in{'newpass'} ne '') {$pass = &crypt($in{'newpass'});}
		$title = $in{'title'};
		$com_adm = $in{'com_adm'};
		$home = $in{'home'};			$guide = $in{'guide'};
		$savedir = $in{'savedir'};		$loaddir = $in{'loaddir'};
		$colors = $in{'colors'};
		$page = $in{'page'};			$fsize = $in{'fsize'};
		$max_wh = $in{'max_wh'};		$menuw = $in{'menuw'};

		open (OUT,">$opfile") || &error("OPEN ERROR");
		print OUT "$pass<>$title<>$com_adm<>$home<>$guide<>$savedir<>$loaddir<>$colors<>$page<>$fsize<>$max_wh<>$menuw";
		close OUT;
	}
	print "下記に入力後、「設定する」を押して下さい。<br><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=hidden name=set value=\"1\">\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 size=60 name=title value=\"$title\"></td></tr>\n";
	$com_adm =~ s/<br>/\r/g;
	print "<tr><td valign=top><br><b>コメント</b></td><td><textarea cols=60 rows=6 name=com_adm>$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>ご利用案内URL</b></td><td><input type=text size=60 name=guide value=\"$guide\"></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></td><td><a href=\"$loaddir/color.htm\" target=\"_blank\">カラーコード</a></td></tr>\n";
	@name = ('基本背景色','基本文字色','タイトル色','タイトル背景色','題名色','左フレーム背景色');
	@colors = split(/,/,$colors);
	for (0 .. $#name) {
		print "<tr><td><b>$name[$_]</b></td><td><table cellspacing=0 cellpadding=0><tr>\n";
		print "<td><input type=text name=colors size=10 value=\"$colors[$_]\" style=\"ime-mode:inactive;\"></td>\n";
		print "<td width=5></td><td width=80 bgcolor=\"$colors[$_]\"></td></tr></table></td></tr>\n";
	}
	print "<tr><td><b>記事表\示</b></td><td><input type=text size=4 name=page value=\"$page\" style=\"text-align:right; ime-mode:disabled;\">件/ページ　　内容文字サイズ <input type=text size=4 name=fsize value=\"$fsize\" style=\"text-align:right; ime-mode:disabled;\">px</td></tr>\n";
	print "<tr><td><b>画像表\示</b></td><td><input type=text size=4 name=max_wh value=\"$max_wh\" style=\"text-align:right; ime-mode:disabled;\">px</td></tr>\n";
	print "<tr><td><b>左フレーム幅</b></td><td><input type=text size=4 name=menuw value=\"$menuw\" style=\"text-align:right; ime-mode:disabled;\">px</td></tr>\n";
	print "<tr><td><b>パスワード変更</b></td><td><input type=password size=10 maxlength=8 name=newpass> （英数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("ファイルを認識できません");}

	if ($max_wh && ($max_wh < $width || $max_wh < $height)) {
		if ($height < $width) {$height = int($height * $max_wh / $width); $width = $max_wh;}
		else {$width = int($width * $max_wh / $height); $height = $max_wh;}
		$big = 1;
	}
}

#=========================================
# 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 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;
}
