#!/usr/bin/perl

#┌────────────────────────
#│ Diary Note
#│ diary.cgi - 2008/03/01
#│ Copyright (c) KentWeb
#│ http://www.kent-web.com/
#└────────────────────────

# 外部ファイル取り込み
require './init.cgi';
require $holiday;

&parse_form;
if ($mode eq "read") { &read; }
elsif ($mode eq "popup") { &popup; }
elsif ($mode eq "check") { &check; }
&topmenu;

#-------------------------------------------------
#  TOP画面
#-------------------------------------------------
sub topmenu {
	&header;
	print <<EOM;
<div align="center">
<table width="620" cellpadding="4" border="0">
<tr>
  <td class="tbl_1" colspan="13"> &nbsp;&nbsp; <b>$title</b></td>
</tr>
</table>
<table width="620" border="0">
<tr>
  <td align="right" height="35"><a href="$home" onMouseover="image1.src='$imgurl/home2.gif'" onMouseout="image1.src='$imgurl/home1.gif'" target="_top"><img src="$imgurl/home1.gif" name="image1" border="0" alt="Homeに戻る"></a></td>
</tr>
</table>
<table width="620" cellpadding="4" border="0">
EOM

	# index読み取り
	open(IN,"$logdir/index.dat") || &error("Open Error: index.dat");
	while (<IN>) {
		my @f = split(/,/);

		print qq|<tr><td height="35" align="center" class="year">$f[0]</td>|;
		foreach $i (1 .. 12) {

			print qq|<td align="center">|;

			if ($f[$i]) {
				print "<a href=\"$script?mode=read&y=$f[0]&m=$i\">$i月</a>";
			} else {
				print "$i月";
			}

			print "</td>";
		}
		print "</tr>\n";
	}
	close(IN);

	print <<EOM;
</table>
<p>
<table width="620" cellpadding="4" border="0">
<tr>
  <td colspan="13" class="tbl_3" align="center">$message</td>
</tr>
</table>
<br><br><br>
<!-- $ver -->
<span style="font-size:10px; font-family:Verdana,Helvetica,Arial;">
- <a href="http://www.kent-web.com/" target="_top">DiaryNote</a> -
</span>
</div>
</body>
</html>
EOM
	exit;
}

#-------------------------------------------------
#  内容閲覧
#-------------------------------------------------
sub read {
	# 汚染チェック
	$in{'y'} =~ s/\D//g;
	$in{'m'} =~ s/\D//g;

	my $data = sprintf("%04d%02d", $in{'y'},$in{'m'});
	my $sub_width = $log_width + 30;

	&header('js');
	print <<EOM;
<div align="center">
<Table cellpadding="3"><Tr><Td align="center" valign="top">
<table width="$sub_width" cellpadding="0" border="0">
<tr>
  <td class="tbl_1m" height="27"> &nbsp;&nbsp; Diary $in{'y'}. $in{'m'}</td>
</tr></table>
<table width="$log_width" border="0">
<tr>
  <td align="right" height="35"><a href="$script?" onMouseover="image2.src='$imgurl/back2.gif'" onMouseout="image2.src='$imgurl/back1.gif'"><img src="$imgurl/back1.gif" name="image2" border="0" alt="メニューに戻る"></a></td>
</tr>
</table>
EOM

	# ログ展開
	my $i = 0;
	local(%cal);
	open(IN,"$logdir/$data.txt");
	while (<IN>) {
		$i++;
		local($no,$d,$w,$sub,$msg,$t[0],$w[0],$h[0],$t[1],$w[1],$h[1],$t[2],$w[2],$h[2]) = split(/<>/);

		$cal{$d} = $d;

		next if ($i < $page + 1);
		next if ($i > $page + $pagelog);

		# 自動リンク
		$msg = &auto_link($msg) if ($autolink);

		my $day = $d;
		$day =~ s/^0//;

		print qq|<table width="$log_width"><tr><td class="log" align="$layout">|;
		print qq|$in{'m'}月$day日 ($week[$w]) &nbsp;<b>$sub</b><br><br>$msg\n|;

		my $f;
		foreach $i (0 .. 2) {
			if ($t[$i]) { $f++; } else { next; }
			my $j = $i + 1;
			print "<p>\n" if ($f == 1);

			my $wh;
			if ($w[$i] && $h[$i]) { $wh = "width=$w[$i] height=$h[$i]"; }
			print qq|<a href="$imgurl/$no-$j$t[$i]" target="_blank">|;
			print qq|<img src="$imgurl/$no-$j$t[$i]" border="0" alt="$no-$j$t[$i]" $wh></a>\n|;
		}
		print "</p>" if ($f);

		print "</td></tr></table><br><br>\n";
	}
	close(IN);

	# 繰越数定義
	my $next = $page + $pagelog;
	my $back = $page - $pagelog;

	my $flag = 0;
	print qq|<table border="0"><tr>\n|;
	if ($back >= 0) {
		$flag++;
		print "<td valign=top><form action=\"$script\">\n";
		print "<input type=hidden name=page value=\"$back\">\n";
		print "<input type=hidden name=mode value=\"read\">\n";
		print "<input type=hidden name=y value=\"$in{'y'}\">\n";
		print "<input type=hidden name=m value=\"$in{'m'}\">\n";
		print "<input type=submit value=\"前の$pagelog件\"></form></td>\n";
	}
	if ($next < $i) {
		$flag++;
		print "<td valign=top><form action=\"$script\">\n";
		print "<input type=hidden name=page value=\"$next\">\n";
		print "<input type=hidden name=mode value=\"read\">\n";
		print "<input type=hidden name=y value=\"$in{'y'}\">\n";
		print "<input type=hidden name=m value=\"$in{'m'}\">\n";
		print "<input type=submit value=\"次の$pagelog件\"></form></td>\n";
	}

	# ページ移動ボタン表示
	if ($flag) {
		print qq|<td width="7"></td><td class="page" valign="top">\n|;
		local($x,$y) = (1,0);
		while ($i > 0) {
			if ($page == $y) {
				print qq!| <b style="color:red">$x</b>\n!;
			} else {
				print qq!| <a href="$script?mode=read&page=$y&y=$in{'y'}&m=$in{'m'}">$x</a>\n!;
			}
			$x++;
			$y += $pagelog;
			$i -= $pagelog;
		}
		print "|</td>";
	} else {
		print "<td></td>";
	}

	print <<EOM;
</tr></table>
</Td>
<Td width="30"></Td>
<Td valign="top">
EOM

	&calen;

	print <<EOM;
</Td>
</Tr></Table>
</div>
</body>
</html>
EOM
	exit;
}

#-------------------------------------------------
#  カレンダ
#-------------------------------------------------
sub calen {
	# 本日を求める
	$ENV{'TZ'} = "JST-9";
	my ($mday,$mon,$year) = (localtime(time))[3..5];
	my $today = sprintf("%04d-%s-%s", $year+1900,$mon+1,$mday);

	# 翌月・前月を求める
	my $next_y = $in{'y'};
	my $next_m = $in{'m'}+1;
	if ($next_m > 12) {
		$next_y++;
		$next_m -= 12;
	}
	my $back_y = $in{'y'};
	my $back_m = $in{'m'}-1;
	if ($back_m < 1) {
		$back_y--;
		$back_m += 12;
	}

	# 当月の末日を求める
	my $last = (31,28,31,30,31,30,31,31,30,31,30,31) [$in{'m'}-1]
	+ ($in{'m'} == 2 && (($in{'y'} % 4 == 0 && $in{'y'} % 100 != 0) ||
	$in{'y'} % 400 == 0));

	# 当月１日の週を求める
	my $week = &getweek(1, $in{'y'}, $in{'m'});

	# 日の表示色を定義
	my @col = ($sunCol,$norCol,$norCol,$norCol,$norCol,$norCol,$satCol,$holCol);

	# 表示テーブル開始
	print <<EOM;
<table border="0" cellspacing="0" cellpadding="2">
<tr>
  <td colspan="7" align="center" class="cal"><b>$in{'y'}/$in{'m'}</b></td>
</tr>
<tr>
EOM

	# 週項目を記述
	foreach $i (0 .. 6) {
		print qq|<td align="center" style="font-size:$calSize">$calwk[$i]</td>|;
	}
	print "</tr>\n<tr>";

	# 月初のブランクを記述
	foreach $i (0 .. 6) {
		last if ($i == $week);

		print "<td>&nbsp;</td>";
	}

	# 日テーブルをループする
	my $flag = 0;
	foreach my $i (1 .. $last) {

		# 週を認識
		if ($week > 0 && $week % 7 == 0) {
			print "</tr><tr>";
			$week = 0;
		}

		# 祝日チェック
		if (&holiday::holiday($in{'y'}, $in{'m'}, $i)) {
			$w = 7;
		} else {
			$w = $week;
		}

		if ($today eq "$in{'y'}-$in{'m'}-$i") {
			print qq|<td align="right" class="cal" bgcolor="$todCol">|;
		} else {
			print qq|<td align="right" class="cal">|;
		}

		# 記事有り
		my $j = sprintf("%02d", $i);
		if (defined($cal{$j})) {
			print "<a href=\"$script?mode=popup&y=$in{'y'}&m=$in{'m'}&d=$i\" target=\"popwin\" onclick=\"popup(0)\"><font color=\"$col[$w]\"><b>$i</b></font></a>";
		} else {
			print "<font color=\"$col[$w]\">$i</font>";
		}
		print "</td>";
		$week++;
	}

	# 月末のブランクを記述
	while ( $week < 7 ) {
		print "<td>&nbsp;</td>";
		$week++;
	}

	# 月替えボタン
	print <<EOM;
</tr>
<tr>
  <td align="center" colspan="7"><br>
<a href="$script?mode=read&y=$back_y&m=$back_m"><img src="$imgurl/left.gif" alt="前月" border="0"></a>
&nbsp;&nbsp;&nbsp;
<a href="$script?mode=read&y=$next_y&m=$next_m"><img src="$imgurl/right.gif" alt="翌月" border="0"></a>
  </td>
</tr>
</table>
EOM
}

#-------------------------------------------------
#  ポップアップ画面
#-------------------------------------------------
sub popup {
	# 汚染チェック
	$in{'y'} =~ s/\D//g;
	$in{'m'} =~ s/\D//g;
	$in{'d'} =~ s/\D//g;

	my $data = sprintf("%04d%02d", $in{'y'},$in{'m'});

	my @view;
	open(IN,"$logdir/$data.txt");
	while (<IN>) {
		local($no,$d,$w,$sub,$msg,$t[0],$w[0],$h[0],$t[1],$w[1],$h[1],$t[2],$w[2],$h[2]) = split(/<>/);

		if ($in{'d'} == $d) {
			push(@view,$_);
		}
	}
	close(IN);

	if (@view == 0) { &error("該当記事なし"); }

	# 当月１日の週を求める
	my $week = &getweek($in{'d'}, $in{'y'}, $in{'m'});

	&header;
	print qq|<div align="center">\n|;

	# 表示開始
	foreach (@view) {
		local($no,$d,$w,$sub,$msg,$t[0],$w[0],$h[0],$t[1],$w[1],$h[1],$t[2],$w[2],$h[2]) = split(/<>/);

		print qq|<p></p><table width="$log_width"><tr>\n|;
		print qq|<td class="log" align="$layout">\n|;
		print qq|$in{'m'}月$in{'d'}日 ($week[$week]) &nbsp;|;
		print qq|<b>$sub</b><p>$msg</p>\n|;

		my $f;
		foreach $i (0 .. 2) {
			if ($t[$i]) { $f++; } else { next; }
			my $j = $i + 1;
			print "<p>\n" if ($f == 1);

			if ($w[$i] && $h[$i]) { $wh = "width=$w[$i] height=$h[$i]"; }
			print qq|<a href="$imgurl/$no-$j$t[$i]" target="_blank">|;
			print qq|<img src="$imgurl/$no-$j$t[$i]" border="0" alt="$no-$j$t[$i]" $wh></a>\n|;
		}
		print "</p>" if ($f);

		print qq|</td></tr>\n</table>\n|;
	}

	print <<EOM;
<form>
<input type="button" value=" 閉じる " onclick="window.close()">
</form>
</div>
</body>
</html>
EOM
	exit;
}

#-------------------------------------------------
#  フォームデコード
#-------------------------------------------------
sub parse_form {
	my $buf;
	if ($ENV{'REQUEST_METHOD'} eq "POST") {
		read(STDIN, $buf, $ENV{'CONTENT_LENGTH'});
	} else {
		$buf = $ENV{'QUERY_STRING'};
	}

	undef(%in);
	foreach ( split(/&/, $buf) ) {
		my ($key, $val) = split(/=/);
		$val =~ tr/+/ /;
		$val =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("H2", $1)/eg;

		# タグ処理
		$val =~ s/&/&amp;/g;
		$val =~ s/"/&quot;/g;
		$val =~ s/</&lt;/g;
		$val =~ s/>/&gt;/g;
		$val =~ s/\0//g;

		$in{$key} .= "\0" if (defined($in{$key}));
		$in{$key} .= $val;
	}
	$page = $in{'page'};
	$mode = $in{'mode'};

	$headflag = 0;
}

#-------------------------------------------------
#  HTMLヘッダ
#-------------------------------------------------
sub header {
	local($js) = @_;
	if ($headflag) { return; }

	print "Content-type: text/html\n\n";
	print <<EOM;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja">
<head>
<meta http-equiv="content-type" content="text/html; charset=shift_jis">
<meta http-equiv="content-style-type" content="text/css">
<style type="text/css">
<!--
body,td,th { font-size: $b_size; font-family: $b_face; }
a:hover { color: #DD0000; }
.cal    { font-size: $calSize; font-family: $calFace; }
.tbl_1  { background-color: $tb1_col; color: $tb2_col; }
.tbl_1m {
	background-color: $tb1_col;
	color: $tb2_col;
	font-size: 14px;
	font-family: Verdana,Helvetica,Arial;
	font-weight: bold;
}
.tbl_2  { background-color: $tb2_col; color: $tb1_col; }
.tbl_3  {
	padding: 15px;
	background-color: $tb3_col;
	color: $tb1_col;
	line-height: 150%;
	letter-spacing: 3px;
	border-top: 1px solid $tb1_col;
	border-left: 1px solid $tb1_col;
	border-right: 1px solid $tb1_col;
	border-bottom: 1px solid $tb1_col;
}
.year {
	font-size: 14px;
	font-family: Verdana,Helvetica,Arial;
	font-weight: bold;
}
.log   {
	padding: 15px;
	background-color: $tb2_col;
	color: $tb1_col;
	border-top: 1px solid $tb1_col;
	border-left: 1px solid $tb1_col;
	border-right: 1px solid $tb1_col;
	border-bottom: 1px solid $tb1_col;
}
.page { font-family: Verdana,Helvetica,Arial; }
-->
</style>
<title>$title</title>
EOM

	# JavaScriptヘッダー
	if ($js eq "js") {
		print "<meta http-equiv=\"content-script-type\" content=\"text/javascript\">\n";
		print "<script type=\"text/javascript\">\n<!--\n";
		print "function popup(no) {\n";
		print "window.open(\"\",\"popwin\",\"toolbar=0,menubar=0,scrollbars=1,width=$pop_w,height=$pop_h\");\n}\n//-->\n</script>\n";
	}
	print "</head>";

	if ($bg_img) {
		print "<body background=\"$bg_img\" bgcolor=\"$bg_col\" text=\"$tx_col\" link=\"$lk_col\" vlink=\"$vl_col\" alink=\"$al_col\">\n";
	} else {
		print "<body bgcolor=\"$bg_col\" text=\"$tx_col\" link=\"$lk_col\" vlink=\"$vl_col\" alink=\"$al_col\">\n";
	}

	$headflag=1;
}

#-------------------------------------------------
#  自動リンク
#-------------------------------------------------
sub auto_link {
	local($_) = @_;

	s/([^=^\"]|^)(https?\:[\w\.\~\-\/\?\&\=\@\;\#\:\%]+)/$1<a href=\"$2\" target=\"_blank\">$2<\/a>/g;
	$_;
}

#-------------------------------------------------
#  エラー処理
#-------------------------------------------------
sub error {
	&header;
	print <<EOM;
<div align="center">
<table border="0" cellpadding="18" cellspacing="0" width="450">
<tr><td class="log" align="center">
<h3>ERROR !</h3>
<font color="#dd0000">$_[0]</font>
<p>
<form>
<input type="button" value="前画面に戻る" onclick="history.back()">
</form>
</td></tr>
</table>
</div>
</body>
</html>
EOM
	exit;
}

#-------------------------------------------------
#  簡易チェック
#-------------------------------------------------
sub check {
	&header;
	print <<EOM;
<h3>Check Mode</h3>
<ul>
EOM

	my %dir = (
		$logdir => 'ログディレクトリ',
		$imgdir => '画像ディレクトリ',
	);
	foreach ( keys(%dir) ) {
		# パス
		if (-d $_) {
			print "<li>$dir{$_}パス: OK!\n";

			# パーミッション
			if (-r $_ && -w $_ && -x $_) {
				print "<li>$dir{$_}パーミッション: OK!\n";
			} else {
				print "<li>$dir{$_}パーミッション: NG → $_\n";
			}
		} else {
			print "<li>$dir{$_}パス: NG → $_\n";
		}
	}

	my %log = (
		'index.dat' => 'indexファイル',
		'num.dat' => 'numファイル',
		'pwd.cgi' => 'pwdファイル',
	);
	foreach ( keys(%log) ) {
		if (-e "$logdir/$_") {
			print "<li>$log{$_}パス: OK!\n";

			# パーミッション
			if (-r "$logdir/$_" && -w "$logdir/$_") {
				print "<li>$log{$_}パーミッション: OK!\n";
			} else {
				print "<li>$log{$_}パーミッション: NG → $_\n";
			}
		} else {
			print "<li>$log{$_}: NG → $_\n";
		}
	}

	print <<EOM;
<li>バージョン: $ver
</ul>
</body>
</html>
EOM
	exit;
}

