#!/usr/bin/perl

###############################################
#   inf11.cgi
#      V1.0 (2005.12.17)
#                     Copyright(C) CGI-design
###############################################

$script = 'inf11.cgi';
$base = './infdata';				#データ格納ディレクトリ
$inffile = "$base/inf.txt";			#記事
$nofile = "$base/no.txt";			#記事番号
$opfile = "$base/option.txt";

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<>情報<>280<>#ffffff,#000000,#c00000";
	close OUT;
	chmod(0666,$inffile);	chmod(0666,$nofile);
}

### メイン処理 ###
if ($ENV{'REQUEST_METHOD'} eq "POST") {read(STDIN,$in,$ENV{'CONTENT_LENGTH'});} else {$in = $ENV{'QUERY_STRING'};}
%in = ();
foreach (split(/&/,$in)) {
	($n,$val) = split(/=/);
	$val =~ tr/+/ /;
	$val =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$val =~ s/\r\n|\r|\n/<br>/g;
	if (defined($in{$n})) {$in{$n} .= "\0$val";} else {$in{$n} = $val;}
}
$mode = $in{'mode'};

open (IN,"$opfile") || &error("OPEN ERROR");
($pass,$title,$dspw,$colors) = split(/<>/,<IN>);
close IN;
($bg_color,$text_color,$title_color) = split(/,/,$colors);

if ($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=\"$base/style.css\"></head>\n";
	$head = 1;
}

###
sub main {
	&header;
	print "<body bgcolor=\"$bg_color\" text=\"$text_color\" link=\"$text_color\" vlink=\"$text_color\" alink=\"$text_color\" style=\"scrollbar-arrow-color:$text_color; scrollbar-face-color:$bg_color; scrollbar-track-color:$bg_color; scrollbar-3dlight-color:$bg_color; scrollbar-highlight-color:$bg_color; scrollbar-shadow-color:$bg_color; scrollbar-darkshadow-color:$bg_color;\">\n";
	print "<center><font color=\"$title_color\"><b>$title</b></font></center>\n";
	&dsp;
	print "<a href=\"$script?mode=admin\" target=\"_blank\">[編集]</a><center>\n";
	#次の行は著作権表示ですので削除しないで下さい。#
	print "<a href=\"http://cgi-design.net\" target=\"_blank\">CGI-design</a>\n";
}

###
sub dsp {
	open (IN,"$inffile") || &error("OPEN ERROR");
	while (<IN>) {
		($no,$year,$mon,$day,$com) = split(/<>/);
		$com =~ s/([^=^\"]|^)(http\:[\w\.\~\-\/\?\&\+\=\:\@\%\;\#\%]+)/$1<a href=\"$2\" target=\"_blank\">$2<\/a>/g;
		print "<table width=100% cellspacing=0 cellpadding=0><tr><td><b>$year.$mon.$day</b></td>";
		if ($mode eq 'admin') {print "<td align=right><input type=submit name=$no value=\"修正\"></td>";}
		print "</tr></table>$com<hr size=1 color=\"$title_color\">\n";
	}
	close IN;
}

###
sub admin {
	&header;
	print "<body><center>\n";
	$inpass = $in{'pass'};
	if ($inpass eq '') {
		print "<table width=97%><tr><td><a href=\"$script\">[Return]</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>Return</b></font></a></td>\n";
	print "<td align=right><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 value=\"新規/修正\">\n";
	print "<input type=submit name=set value=\"基本設定\"></td></form><td width=10></td></tr></table><br>\n";

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

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

	&in_form;
	print "<hr width=450>修正、削除する場合は「修正」をクリックして下さい。\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=edt value=\"1\">\n";
	print "<table width=$dspw><tr><td>\n";
	&dsp;
	print "</td></tr></table></form>\n";
}

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

	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";
	if ($in{'edt'}) {
		open (IN,"$inffile") || &error("OPEN ERROR");
		while (<IN>) {
			($no,$year,$mon,$day,$com) = split(/<>/);
			if ($in{$no}) {last;}
		}
		close IN;
		print "<input type=hidden name=no value=\"$no\">\n";
		$com =~ s/<br>/\r/g;
	} else {
		$year = $nowyear;
		$mon = $nowmon;
		$day = $nowday;
		$com = '';
	}
	print "<table bgcolor=\"#e6e4ce\" cellspacing=8><tr><td><table cellspacing=2 cellpadding=0>\n";
	print "<tr><td>日付&nbsp;</td><td><select name=year>\n";
	for (2008 .. $nowyear+1) {
		if ($_ eq $year) {$sel = ' selected';} else {$sel = '';}
		print "<option value=\"$_\"$sel>$_</option>\n";
	}
	print "</select>年 <select name=mon>\n";
	for (1 .. 12) {
		if ($_ eq $mon) {$sel = ' selected';} else {$sel = '';}
		print "<option value=\"$_\"$sel>$_</option>\n";
	}
	print "</select>月 <select name=day>\n";
	for (1 .. 31) {
		if ($_ eq $day) {$sel = ' selected';} else {$sel = '';}
		print "<option value=\"$_\"$sel>$_</option>\n";
	}
	print "</select>日</td></tr>\n";
	print "<tr><td valign=top><br>内容</td><td><textarea cols=50 rows=6 name=com style=\"ime-mode:active;\">$com</textarea></td></tr>\n";
	print "<tr><td></td><td>";
	if ($in{'edt'}) {
		print "<table width=100%><tr><td><input type=submit name=edtwrt value=\"修正する\"></td>\n";
		print "<td width=40 bgcolor=red align=center><input type=submit name=delwrt value=\"削除\"></td></tr></table>\n";
	} else {print "<input type=submit name=newwrt value=\"新規登録\">";}
	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;
	$newdata = "$no<>$in{'year'}<>$in{'mon'}<>$in{'day'}<>$in{'com'}<>\n";
	&logwrt;
}

###
sub logwrt {
	$newdate = sprintf("$in{'year'}%02d%02d",$in{'mon'},$in{'day'});
	$flag = 0;
	@new = ();
	open (IN,"$inffile") || &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,">$inffile") || &error("OPEN ERROR");		print OUT @new;		close OUT;
}

###
sub edtwrt {
	$newdata = "$in{'no'}<>$in{'year'}<>$in{'mon'}<>$in{'day'}<>$in{'com'}<>\n";
	&logwrt;
}

###
sub delwrt {
	@new = ();
	open (IN,"$inffile") || &error("OPEN ERROR");
	while (<IN>) {
		($no) = split(/<>/);
		if ($no ne $in{'no'}) {push(@new,$_);}
	}
	close IN;
	open (OUT,">$inffile") || &error("OPEN ERROR");		print OUT @new;		close OUT;
}

###
sub setup {
	if ($in{'wrt'}) {
		if ($in{'newpass'} ne '') {$pass = &crypt($in{'newpass'});}
		$title = $in{'title'};
		$dspw = $in{'dspw'};
		$colors = $in{'colors'};	$colors =~ s/\0/,/g;

		open (OUT,">$opfile") || &error("OPEN ERROR");
		print OUT "$pass<>$title<>$dspw<>$colors";
		close OUT;
	}
	print "下記に入力後、「設定する」を押して下さい。\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=40 name=title value=\"$title\"></td></tr>\n";
	print "<tr><td></td><td><a href=\"$base/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 size=10 name=colors 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=dspw value=\"$dspw\" 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 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;
}
