#!/usr/bin/perl

#┌─────────────────────────────────
#│  DAY COUNTER-EX MANAGER v3.41 (2002/07/15)
#│  Copyright(C) KENT WEB 2002
#│  webmaster@kent-web.com
#│  http://www.kent-web.com/
#└─────────────────────────────────
$ver = 'DAY COUNTER-EX v3.41';
#┌─────────────────────────────────
#│ 【原作者注意事項中譯】
#│ 1. 這個CGI程式為免費提供使用。有關使用此程式所造成的任何
#│    問題及損害，作者將不負任何賠償責任。
#│ 【中文化作者按】
#│ 1. 本中文化為作者本身網站的需求而設定，如果任何人想要利用此程式
#│    請不要刪除程式檔裡的版權宣告以及注意事項！
#│ 2. 如果有翻譯不週到的地方還請多包含！
#│    
#│    
#│  【中文化作者資訊】
#│ 姓名：郭勝佳  網址：http://www.ksc.idv.tw/　日期：2002/11/23
#└─────────────────────────────────
#============#
#  設定項目  #
#============#

# 資料檔路徑
$logfile = "./dayx.dat";

# 日次紀錄檔路徑
$dayfile = "./day.dat";

# 月次紀錄檔路徑
$monfile = "./mon.dat";

# 返回首頁路徑
$home = "http://sex.ncu.edu.tw/";

# 標題名稱
$title = "訪客人次統計系統";

# 標題文字顏色
$t_color = "#78AEFF";

# 圖表圖檔路徑
$graph1 = "http://anti-censorship.twfriend.net/gif1/blue.gif";
$graph2 = "http://anti-censorship.twfriend.net/gif1/red.gif";

# 月間表格幅度調整
# 一個月四位數大約50~100 五位數約200~500
$mKEY = 1750;

# 日次表格幅度調整
# 一天兩位數左右約1~2  三位數5~10  四位數30~60
$dKEY = 30;

# BODY標籤
$body = '<body bgcolor="#460069" text="#DDFF78" link="#FFD350" vlink="#D2FF78">';

#============#
#  設定結束 #
#============#

# 
open(IN,"$logfile") || &error("Open Error : $logfile");
$data = <IN>;
close(IN);
($day,$yes,$to) = split(/<>/, $data);

# 時間取得
$ENV{'TZ'} = "JST-8";
($mday,$mon,$year,$wday) = (localtime(time))[3..6];
@week = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
$date = sprintf("%02d/%02d (%s) ", $mon+1,$mday,$week[$wday]);
$D_Y  = sprintf("%04d/%02d", $year+1900,$mon+1);

# 
open(IN,"$dayfile") || &error("Open Error : $dayfile");
@dayfile = <IN>;
close(IN);
push(@dayfile,"$date<>$to<>\n");

# 
open(IN,"$monfile") || &error("Open Error : $monfile");
@monfile = <IN>;
close(IN);

$under = pop(@monfile);
if ($under =~ /^(\d+)\/(\d+)<>(\d+)/) { $Y="$1/$2"; $C=$3; }
if ($Y eq $D_Y) {
	$C2 = $C + $to;
	push(@monfile,"$Y<>$C2<>\n");
} else {
	push(@monfile,"$D_Y<>$to<>\n");
}

# 
&header;
print <<"EOM";
<TABLE cellspacing="0" cellpadding="0">
<TR><TD bgcolor="#B4B4B4" width="70" height="20" align="center" nowrap>
<A href="$home" target="_top" style="font-size:10pt; color:#000000; text-decoration:none">返回首頁</A>
</TD></TR></TABLE>
<div align="center">
<hr width="300">
<b style="font-size:12pt; color:$t_color">$title</b>
<hr width="300">
<br>
[<a href="#day">每日來訪人次一覽</a>]
[<a href="#mon">月間訪客人次一覽</a>]
</div>
<br>
<table width="100%">
<tr><td bgcolor="#6D6D6D">　<font color="yellow">■</font>
  <font color="#FFFFFF"><b><a name="day">每日來訪人次一覽</a></b></font>
</td></tr></table>
<br>
<blockquote>
<table border=0 cellpadding=1 cellspacing=0>

<tr>
  <th bgcolor=#C33100> 日期 </th><th bgcolor=#C33100> 訪客數 </th>
  <th bgcolor=#C33100> 圖表 </th>
</tr>

EOM

$flag=0;
$tochu=0;
$i=0;
foreach (@dayfile) {
	$i++;
	chop;
	($m_d,$dcnt) = split(/<>/);
	if ($i == 1 && $m_d =~ /^(\d+)\/(\d+)/) {
		if ($2 != 1) { $tochu=1; }
	}

	# 
	if ($dcnt > 0) { $width = int($dcnt / $dKEY); }
	else { $width=1; }
	if ($width < 1) { $width=1; }

	# 
	$dcnt = &filler($dcnt);

	# 
	$m_d =~ s/Sat/<font color=#00E5FF>Sat<\/font>/;
	$m_d =~ s/Sun/<font color=#FF00FF>Sun<\/font>/;

	print "<tr><td nowrap>$m_d</td><td align=right>   $dcnt   </td>";
	print "<td><img src=\"$graph2\" width=$width height=5></td></tr>\n";
}

print <<"EOM";
</font></font></table></blockquote>
<br><br>
<table width="100%">
<tr><td bgcolor="#6D6D6D">　<font color="yellow">■</font>
<font color="#FFFFFF"><b><a name="mon">月間訪客人次一覽</a></b></font>
</td></tr></table>
<br>
<blockquote>
<table border=0 cellpadding=2 cellspacing=0>
<tr bgcolor="#C33100">
  <th nowrap>年月</th><th nowrap> 月間人數</th>
  <th nowrap> 日平均</th><th>  圖表</th>
</tr>
EOM

$flag=0;
foreach (@monfile) {
	($y_m,$mcnt) = split(/<>/);
	($year,$mon) = split(/\//, $y_m);

	if ($_ eq $monfile[$#monfile]) {
		if ($day == 1) { $avr = ' - '; }
		else {
			if ($tochu) { $waru = @dayfile-1; }
			else { $waru = $day-1; }
			if ($C > 0) {
				$avr = int (($C / $waru) +0.5);
				$avr = &filler($avr);
			} else { $avr = ' - '; }
		}
	} else {
		$lastday = &LastDay($year, $mon);
		$avr = int (($mcnt / $lastday) +0.5);
		$avr = &filler($avr);
	}

	# 
	if ($mcnt > 0) { $width = int($mcnt / $mKEY); }
	else { $width=1; }
	if ($width < 1) { $width=1; }

	# 
	$mcnt = &filler($mcnt);

	if ($year ne $year2) { print "<tr><td colspan=4><hr size=1></td></tr>\n"; }

	print "<tr><th nowrap>$y_m</th><td align=right>$mcnt</td>";
	print "<td align=right>$avr</td>";
	print "<td>  <img src=\"$graph1\" width=$width height=10></td></tr>\n";

	$year2 = $year;
}

## 著作權標示，請勿刪除
print <<"EOM";
</table>
</blockquote>
<hr><div align="right"><span style="font-family:verdana; font-size:8pt">
Copyright (C) <a href="http://www.kent-web.com/" target="_blank">Kent Web</a> 2002~2003﹝中文化：<a href="http://www.ksc.idv.tw/" target="_blank">阿佳</a>﹞<br>
<b>$ver</b></span>
</div>
</body>
</html>
EOM
exit;

#----------------#
#    #
#----------------#
sub LastDay {
	local($year, $mon) = @_;
	local($lastday);

	$lastday = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) [$mon - 1]
	+ ($mon == 2 && (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0));

	$lastday;
}

#--------------#
#    #
#--------------#
sub header {
	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=big5">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
<title>$title</title>
<STYLE TYPE="text/css">
<!--
body,tr,td,th { font-size: 10pt; }
small { font-size: 9pt; }
-->
</STYLE>
</head>
$body
EOM
}

#----------------#
#    #
#----------------#
sub filler {
	local($_) = $_[0];
	1 while s/(.*\d)(\d\d\d)/$1,$2/;
	$_;
}

#--------------#
#    #
#--------------#
sub error {
	&header;
	print <<"EOM";
<div align="center">
<hr width="70%">
<h3>ERROR !</h3>
<h4>$_[0]</h4>
<hr width="70%">
</div>

</html>
EOM
	exit;
}