#!/usr/local/bin/perl

###############################################
#   sche31.cgi
#      V1.2 (2004.1.12)
#                     Copyright(C) CGI-design
###############################################

$script = 'sche31.cgi';
$base = './schedata';					#データ格納ディレクトリ
$opfile = "$base/option.txt";			#オプションファイル

@week = ('日','月','火','水','木','金','土');
@mdays = (31,28,31,30,31,30,31,31,30,31,30,31);
@wcolor = ("#ff0000","#000000","#000000","#000000","#000000","#000000","#0000ff");

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<>"; close OUT;
	foreach (1 .. 12) {chmod(0666,"$base/d2004$_.txt");}
}

##### メイン処理 #####
@pair = split(/&/,$ENV{'QUERY_STRING'});
foreach (@pair) {
	($n,$val) = split(/=/);
	$val =~ tr/+/ /;
	$val =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$in{$n} = $val;
}
$mode = $in{'mode'};
$act = $in{'act'};

open (IN,"$opfile") || &error("OPEN ERROR");
($pass,$home) = split(/<>/,<IN>);
close IN;

($sec,$min,$hour,$nowday,$nowmon,$nowyear) = localtime;
$nowyear += 1900;
$nowmon++;

$logyear = $in{'year'};
$logmon = $in{'mon'};
$logday = $in{'day'};
if (!$logyear) {$logyear = $nowyear; $logmon = $nowmon; $logday = $nowday;}
$logfile = "$base/d$logyear$logmon.txt";

&header;
if ($mode eq 'comdsp') {&comdsp;}
elsif ($mode eq 'admin') {&admin;}
else {&main;}

print "</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\"></head><body>\n";
	$head = 1;
}

###
sub main {
	print "<center>";
	if ($home) {print "<a href=\"$home\">[HOME]</a>";}
	print "　<a href=\"$script?mode=admin\">[管理]</a></center>\n";
	&dsp;
	# 次の行は著作権表示ですので削除しないで下さい。#
	print "<hr size=1><center>CGI-design</center>\n";
}

###
sub dsp {
	if (-e $logfile) {
		open (IN, "$logfile") || &error("OPEN ERROR");
		while (<IN>) {
			($day,$com,$holiday) = split(/<>/);
			$com[$day] = $com;
			$holiday[$day] = $holiday;
		}
		close IN;
	}
	print "<center>";
	$mon = $logmon - 1;
	if ($mon < 1) {$mon = 12; $year = $logyear - 1;} else {$year = $logyear;}
	print "<a href=\"$script?mode=$mode&pass=$inpass&year=$year&mon=$mon\"><b>←</b></a> ";
	print "$logyear年$logmon月";
	$mon = $logmon + 1;
	if (12 < $mon) {$mon = 1; $year = $logyear + 1;} else {$year = $logyear;}
	print " <a href=\"$script?mode=$mode&pass=$inpass&year=$year&mon=$mon\"><b>→</b></a></center><hr size=1>\n";

	$mdays = $mdays[$logmon - 1];
	if ($logmon == 2) {$u = $logyear % 4; if (!$u) {$mdays = 29;}}

	$w = &get_date($logyear,$logmon,1);
	for ($k=1; $k<=$mdays; $k++) {
		if ($holiday[$k]) {$wcolor = $wcolor[0];} else {$wcolor = $wcolor[$w];}
		if ($k < 10) {$day = "&nbsp;$k";} else {$day = $k;}
		if ($mode eq 'admin') {print "<a href=\"$script?mode=admin&act=regin&pass=$inpass&year=$logyear&mon=$logmon&day=$k\"><font color=\"$wcolor\"><b>$day</b></font></a>\n";}
		else {print "<font color=\"$wcolor\"><b>$day</b></font>";}

		$com = $com[$k];
		$com =~ s/<br>/ /g;
		if (14 < length($com)) {
			$com = substr($com,0,12);
			$com .= '..';
		}
		if ($logyear == $nowyear && $logmon == $nowmon && $k == $nowday) {$com = "<font color=\"#008040\">$com</font>";}
		print "<a href=\"$script?mode=comdsp&year=$logyear&mon=$logmon&day=$k&w=$w\">$com</a><br>\n";
		$w++;
		if (6 < $w) {$w = 0;}
	}
}

###
sub comdsp {
	print "<a href=\"$script\">[戻る]</a><br>\n";
	open (IN, "$logfile") || &error("OPEN ERROR");
	while (<IN>) {
		($day,$com,$holiday) = split(/<>/);
		if ($logday eq $day) {last;}
	}
	close IN;

	$w = $in{'w'};
	if ($holiday) {$wcolor = $wcolor[0];} else {$wcolor = $wcolor[$w];}
	print "$logyear.$logmon.$logday<font color=\"$wcolor\">($week[$w])<br>\n";
	if ($holiday) {print "[$holiday]<br>";}
	print "</font>$com";
}

###
sub get_date {
	my($y,$m,$d) = @_;
	if( $m < 3 ){$y--; $m+=12;}
	return ($y+int($y/4)-int($y/100)+int($y/400)+int((13*$m+8)/5)+$d)%7;
}

###
sub admin {
	$inpass = $in{'pass'};
	print "<a href=\"$script\">戻る</a>\n";
	if ($inpass eq '') {
		print "<form action=\"$script\">\n";
		print "<input type=hidden name=mode value=\"admin\">\n";
		print "パスワード入力<br><input type=password name=pass size=10 maxlength=8><br>\n";
		print "<input type=submit value=\"認証\"></form>\n";
		print "</body></html>\n";
		exit;
	}
	$mat = &decrypt($inpass,$pass);
	if (!$mat) {&error("パスワードが違います");}

	print "　<a href=\"$script?mode=admin&pass=$inpass\">編集</a>　<a href=\"$script?mode=admin&act=set&pass=$inpass\">設定</a><br>\n";
	if ($act eq 'regin') {&in_form;}
	elsif ($act eq 'wrt') {&regist;}
	elsif ($act eq 'set') {&setup;}
	print "日付クリック<br>\n";
	&dsp;
}

###
sub in_form {
	$flag = 0;
	if (-e $logfile) {
		open (IN, "$logfile") || &error("OPEN ERROR");
		while (<IN>) {
			($day,$com,$holiday) = split(/<>/);
			if ($logday eq $day) {$flag = 1; $com =~ s/<br>/\r/g; last;}
		}
		close IN;
	}
	if (!$flag) {$com=$holiday='';}

	print "<form action=\"$script\">\n";
	print "<input type=hidden name=mode value=\"admin\">\n";
	print "<input type=hidden name=act value=\"wrt\">\n";
	print "<input type=hidden name=year value=\"$logyear\">\n";
	print "<input type=hidden name=mon value=\"$logmon\">\n";
	print "<input type=hidden name=day value=\"$logday\">\n";
	print "<input type=hidden name=pass value=\"$inpass\">\n";
	print "$logyear.$logmon.$logday<br>\n";
	print "休日設定<br><input type=text size=16 name=holiday value=\"$holiday\"><br>\n";
	print "内容<br><textarea cols=16 rows=6 name=com wrap=\"soft\">$com</textarea><br>\n";
	print "<input type=submit value=\"書込む\"></form>\n";
	print "</body></html>\n";
	exit;
}

###
sub regist {
	$in{'com'} =~ s/\r\n|\r|\n/<br>/g;
	if (-e $logfile) {
		$flag = 0;
		open (IN, "$logfile") || &error("OPEN ERROR");
		while (<IN>) {
			($day,$com,$holiday) = split(/<>/);
			if ($logday eq $day) {
				push(@new,"$day<>$in{'com'}<>$in{'holiday'}<>\n");
				$flag = 1;
			}
			else {push(@new,$_);}
		}
		close IN;
		if (!$flag) {push(@new,"$logday<>$in{'com'}<>$in{'holiday'}<>\n");}
		open (OUT,">$logfile") || &error("OPEN ERROR");		print OUT @new;		close OUT;
	} else {
		open (OUT,">$logfile") || &error("OPEN ERROR");
		print OUT "$logday<>$in{'com'}<>$in{'holiday'}<>\n";
		close OUT;
		chmod(0666,$logfile);
	}
}

###
sub setup {
	if ($in{'wrt'} eq 'on') {
		if ($in{'newpass'} ne '') {$pass = &crypt($in{'newpass'});}
		$home = $in{'home'};
		open (OUT, ">$opfile") || &error("OPEN ERROR");		print OUT "$pass<>$home";		close OUT;
	}
	print "<form action=\"$script\">\n";
	print "<input type=hidden name=mode value=\"admin\">\n";
	print "<input type=hidden name=act value=\"set\">\n";
	print "<input type=hidden name=wrt value=\"on\">\n";
	print "<input type=hidden name=pass value=\"$inpass\">\n";
	print "ホームURL<br><input type=text size=16 name=home value=\"$home\"><br>\n";
	print "パスワード変更<br><input type=password name=newpass size=10 maxlength=8><br>\n";
	print "<input type=submit value=\"送信\">\n";
	print "</form></body></html>\n";
	exit;
}

###
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 "<br>ERROR !!<br><font color=red><b>$_[0]</b></font>\n";
	print "</body></html>\n";
	exit;
}
