#!/usr/local/bin/perl

# List for Access Report v2.0 (2001/04/01)
# Copyright(C) Kent Web 2001
# webmaster@kent-web.com
# http://www.kent-web.com/

$ver = 'Access Report v2.0'; # バージョン情報

#--- [注意事項] ------------------------------------------------#
# 1. このスクリプトはフリーソフトです。このスクリプトを使用した	#
#    いかなる損害に対して作者は一切の責任を負いません。		#
# 2. 設置に関する質問はサポート掲示板にお願いいたします。	#
#    直接メールによる質問は一切お受けいたしておりません。	#
#---------------------------------------------------------------#

#============#
#  基本設定  #
#============#

# ロックファイル
$logfile = './report.log';

# スクリプト名
$script = './replist.cgi';

# ホームページタイトル
$title = "旭川";

# リスト一覧からの戻り先 (絶対パスなら http://からのURLで記述）
$home = "../index.html";

# bodyタグ
$body = '<body background="" bgcolor="#EEEEEE" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">';

# 本文中の文字の大きさ（スタイルシート）
$b_size = '10pt';

# タイトル部の文字の大きさ（スタイルシート）
$t_size = '18pt';

# 表示文字色
$font = "#008040";

# グラフ画像（絶対パスなら http://からのURLで記述）
$graph1 = "./graph1.gif"; # 横軸
$graph2 = "./graph2.gif"; # 縦軸

# リスト最低表示件数（これに満たない情報は非表示）
$max_ref = 1;	# リンク元
$max_os  = 2;	# OS情報
$max_brz = 2;	# ブラウザ
$max_hst = 5;	# ホスト名

# 項目部の色指定
$under = "#555555";	# 下地の色
$point = "#F0B30F";	# ポインタ（■）の色
$items = "#FFFFFF";	# 項目文字の色

# パスワード
#  → ここに英数字でパスワードを記述すると「閲覧」には
#     このパスワードが必須となります。
$pass = '';

#============#
#  設定完了  #
#============#

if ($pass ne "") {
	&decode;
	if ($in{'pass'} ne "" && $pass ne "$in{'pass'}") {
		&error("パスワードが違います");
	}
	elsif ($in{'pass'} eq "") {
		&header;
		print "<center><h4>パスワードを入力して下さい</h4>\n";
		print "<form action=\"$script\" method=\"POST\">\n";
		print "<input type=password name=pass size=6>";
		print "<input type=submit value=' 認証 '></form>\n";
		print "</center>\n</body></html>\n";
		exit;
	}
}

$count=0;
open(IN,"$logfile") || &error("Open Error : $logfile");
$top = <IN>;
while (<IN>) {
	$count++;
	($agent,$os,$host,$ref,$hour) = split(/<>/);

	if ($agent) { $AGENT{($agent)[0]}++; $i++; }
	if ($os) { $OS{($os)[0]}++; $j++; }
	if ($host) { $HOST{($host)[0]}++; $k++; }
	if ($ref) { $REF{($ref)[0]}++; $l++; }
	if ($hour ne "") { $HOUR{($hour)[0]}++; $m++; }
}
close(IN);

&header;
print <<"EOM";
<table width="45"><tr><th align=center bgcolor="#B5B5B5">
<a href="$home" style="text-decoration:none;color:black">TOP</a>
</th></tr></table>
<center>
<hr size=2 width="350">
<font color="#008080" size=5><b><span>Access Report</span></b></font>
<hr size=2 width="350">
<P><table><tr><td nowrap>
<UL>
  <LI>以下は <b>$title</b> へのアクセス解析です。
  <LI>解析データは直近 <b>$count</b>件です。
</UL>
</td></tr></table>
</center>
<P><table width="100%" cellpadding=3>
<tr><td bgcolor="$under">
<font color="$point">■</font>
<font color="$items"><b>リンク元の取得</b></font>
</td></th></table>
<P><blockquote>
<LI>リンク元の取得情報（$max_ref件以上 / 取得件数：$l件）
<P>
EOM

print "<table border=1 cellpadding=1 cellspacing=0>\n";
print "<tr><th nowrap>件数</th><th nowrap>リンク元</th><th nowrap>グラフ</th></tr>\n";

$flag=0;
foreach (sort { $REF{$b} <=> $REF{$a} } keys %REF) {

	# 最低表示件数
	if ($REF{$_} < $max_ref) { last; }

	$per = int(($REF{$_}*1000 / $l)+0.5) / 10;
	$per = sprintf("%.1f", $per);
	if (!$flag) {
		$flag=1;
		$bai = $per;

		if ($bai > 30) { $bai = 5; }
		elsif ($bai > 20) { $bai = 7; }
		elsif ($bai > 10) { $bai = 12; }
		else { $bai = 20; }
	}
	$wid = int($per * $bai);
	if ($wid < 1) { $wid = 1; }

	# 文字列長の調整
	if (length($_) > 72) { $url = substr($_,0,68) . "..."; }
	else { $url = $_; }

	print "<tr><th nowrap>$REF{$_}</th>";
	print "<td nowrap><a href=\"$_\" target=\"_top\"><font color=\"$font\">$url</font></a></td>";
	print "<td nowrap><img src=\"$graph1\" width=\"$wid\" height=15> $per\%</td></tr>\n";
}

print "</table></blockquote>\n";

## --- ＯＳ情報
print "<P><table width=\"100%\" cellpadding=3><tr><td bgcolor=\"$under\">\n";
print "<font color=\"$point\">■</font> <font color=\"$items\"><b>訪問者マシンOS</b></font>\n";
print "</td></tr></table>\n";
print "<P><blockquote><LI>訪問者のマシンOS情報 ($max_os件以上)<br>\n";
print "<P><table border=1 cellpadding=3 cellspacing=0>\n";
print "<tr><th nowrap>件数</th><th nowrap>ＯＳ</th><th nowrap>グラフ</th></tr>\n";

foreach (sort { $OS{$b} <=> $OS{$a} } keys %OS) {

	# 最低表示件数
	if ($OS{$_} < $max_os) { last; }

	$per = int(($OS{$_}*1000 / $j)+0.5) / 10;
	$per = sprintf("%.1f", $per);
	$wid = int($per * 3);
	if ($wid < 1) { $wid = 1; }

	print "<tr><th nowrap>$OS{$_}</th><td nowrap><font color=\"$font\"><b>$_</b></font></td>";
	print "<td nowrap><img src=\"$graph1\" width=\"$wid\" height=15> $per\%</td></tr>\n";
}

print "</table></blockquote>\n";

## --- ブラウザ情報
print "<P><table width=\"100%\" cellpadding=3><tr><td bgcolor=\"$under\">\n";
print "<font color=\"$point\">■</font> <font color=\"$items\"><b>ブラウザ</b></font>\n";
print "</td></tr></table>\n";
print "<P><blockquote><LI>訪問者のブラウザ情報 ($max_brz件以上)<br>\n";
print "<P><table border=1 cellpadding=3 cellspacing=0>\n";
print "<tr><th nowrap>件数</th><th nowrap>ブラウザ<th nowrap>グラフ</th></th></tr>\n";

foreach (sort { $AGENT{$b} <=> $AGENT{$a} } keys %AGENT) {

	# 最低表示件数
	if ($AGENT{$_} < $max_brz) { last; }

	$per = int(($AGENT{$_}*1000 / $i)+0.5) / 10;
	$per = sprintf("%.1f", $per);
	$wid = int($per * 3);
	if ($wid < 1) { $wid = 1; }

	print "<tr><th nowrap>$AGENT{$_}</th><td nowrap><font color=\"$font\"><b>$_</b></font></td>";
	print "<td nowrap><img src=\"$graph1\" width=\"$wid\" height=15> $per\%</td></tr>\n";
}

print "</table></blockquote>\n";

## --- ホスト名
print "<P><table width=\"100%\" cellpadding=3><tr><td bgcolor=\"$under\">\n";
print "<font color=\"$point\">■</font> <font color=\"$items\"><b>ホスト名</b></font>\n";
print "</td></tr></table>\n";
print "<P><blockquote><LI>訪問者のホスト名情報 ($max_hst件以上)<br>\n";
print "<P><table border=1 cellpadding=3 cellspacing=0>\n";
print "<tr><th nowrap>件数</th><th nowrap>ホスト名</th><th nowrap>グラフ</th></tr>\n";

foreach (sort { $HOST{$b} <=> $HOST{$a} } keys %HOST) {

	# 最低表示件数
	if ($HOST{$_} < $max_hst) { last; }

	$per = int(($HOST{$_}*1000 / $k)+0.5) / 10;
	$per = sprintf("%.1f", $per);
	$wid = int($per * 15);
	if ($wid < 1) { $wid = 1; }

	print "<tr><th nowrap>$HOST{$_}</th><td nowrap><font color=\"$font\">$_</font></td>";
	print "<td nowrap><img src=\"$graph1\" width=\"$wid\" height=15> $per\%</td></tr>\n";
}

print "</table></blockquote>\n";

## --- 時間帯
print "<P><table width=\"100%\" cellpadding=3><tr><td bgcolor=\"$under\">\n";
print "<font color=\"$point\">■</font> <font color=\"$items\"><b>アクセス時間帯</b></font>\n";
print "</td></tr></table>\n";
print "<P><blockquote><LI>訪問者のアクセス時間帯<br>\n";
print "<P><table border=0 cellpadding=3 cellspacing=0><tr>\n";
#print "<tr><th nowrap>件数</th><th nowrap>時間帯</th><th nowrap>グラフ</th></tr>\n";

print "<th></th>\n";
foreach (0 .. 23) {

	print "<td valign=bottom align=center><small>$HOUR{$_}</small><br clear=all>";
	if ($HOUR{$_}) { print "<img src=\"$graph2\" width=10 height=$HOUR{$_}>"; }
	print "</td>\n";
}

print "</tr><tr bgcolor=\"#B3B3B3\"><th valign=bottom>時間帯：</th>\n";
foreach (0 .. 23) {
	print "<th>$_</th>";
}

print "</tr></table></blockquote>\n";

# 著作権表示（削除不可）
print "<hr><center><small><!-- $ver -->\n";
print "- <a href=\"http://www.kent-web.com/\" target=\"_top\">Access Report</a> -\n";
print "</small></center>\n";
print "</body></html>\n";
exit;

#--------------#
#  エラー処理  #
#--------------#
sub error {
	&header;
	print <<"EOM";
<div align=center>
<hr width="75%">
<h3>ERROR !</h3>
<h4>$_[0]</h4>
<hr width="75%">
</div>
</body></html>
EOM
	exit;
}

#------------#
#  ヘッダー  #
#------------#
sub header {
	print "Content-type: text/html\n\n";
	print <<"EOM";
<html>
<head>
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=Shift_JIS">
<STYLE TYPE="text/css">
<!--
body,tr,td,th { font-size: $b_size }
span { font-size: $t_size }
-->
</STYLE>
<title>Access Report</title></head>
$body
EOM
}

#----------------#
#  デコード処理  #
#----------------#
sub decode {
	if ($ENV{'REQUEST_METHOD'} eq "POST") {
		read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	} else { $buffer = $ENV{'QUERY_STRING'}; }
	@pairs = split(/&/, $buffer);
	foreach (@pairs) {
		($name, $value) = split(/=/);
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		$in{$name} = $value;
	}
}
