#!/usr/bin/perl

#┌─────────────────────────────────
#│ List for Access Report v3.01 (2004/07/27)
#│ Copyright (c) KentWeb
#│ webmaster@kent-web.com
#│ http://www.kent-web.com/
#└─────────────────────────────────
$ver = 'Access Report v3.01';
#┌─────────────────────────────────
#│ [注意事項]
#│ 1. このスクリプトはフリーソフトです。このスクリプトを使用した
#│    いかなる損害に対して作者は一切の責任を負いません。
#│ 2. 設置に関する質問はサポート掲示板にお願いいたします。
#│    直接メールによる質問は一切お受けいたしておりません。
#└─────────────────────────────────

#-------------------------------------------------
#  基本設定
#-------------------------------------------------

# ログファイル
$logfile = './replog.cgi';

# スクリプトURL
$script = './replist.cgi';

# ホームページタイトル
$title = "人生の音色";

# リスト一覧からの戻り先 (絶対パスなら http://からのURLで記述）
$home = "http://www5f.biglobe.ne.jp/~neiro/";

# bodyタグ
$body = '<body bgcolor="#F0F0F0" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">';

# 本文中の文字の大きさ（スタイルシート）
$b_size = '10pt';

# タイトル部の文字の大きさ（スタイルシート）
$t_size = '18pt';

# タイトル文字の色
$t_color = "#008080";

# 表示文字色
$font = "#008040";

# グラフ画像（絶対パスなら http://からのURLで記述）
$graph1 = "http://www5f.biglobe.ne.jp/~neiro/cgi/neiro/accese/graph1.gif"; # 横軸
$graph2 = "http://www5f.biglobe.ne.jp/~neiro/cgi/neiro/accese/graph2.gif"; # 縦軸

# リスト最低表示件数（これに満たない情報は非表示）
$max_ref = 2;	# リンク元
$max_os  = 2;	# OS情報
$max_brz = 2;	# ブラウザ
$max_hst = 5;	# ホスト名

# 項目部の色指定
$under = "#555555";	# 下地の色
$point = "#FFFF00";	# ポインタ（■）の色
$items = "#FFFFFF";	# 項目文字の色

# パスワード
#  → ここに英数字でパスワードを記述すると「閲覧」には
#     このパスワードが必須となります。
$pass = 'tamotsu416';

#-------------------------------------------------
#  設定完了
#-------------------------------------------------

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=8>\n";
		print "<input type=submit value=' 認証 '></form>\n";
		print "</center>\n</body></html>\n";
		exit;
	}
}
local($top);
$count=0;
$i=0; $j=0; $k=0; $l=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]}++; }
}
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">
<b style="font-size:$t_size; color:$t_color">Access Report</b>
<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></tr></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\">　";
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\">　";
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><th nowrap>グラフ</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\">　";
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\">　";
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 <<"EOM";
</tr></table>
</blockquote>
<hr><!-- $ver -->
<div align="center" style="font-size:10px;font-family:Verdana">
- <a href="http://www.kent-web.com/" target="_top">Access Report</a> -
</div>
</body>
</html>
EOM
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";
<!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,tr,td,th { font-size: $b_size; font-family:"MS UI Gothic" }
span { font-size: $t_size }
-->
</STYLE>
<title>Access Report</title></head>
$body
EOM
}

#-------------------------------------------------
#  デコード処理
#-------------------------------------------------
sub decode {
	local($buf, $key, $val);

	if ($ENV{'REQUEST_METHOD'} eq "POST") {
		read(STDIN, $buf, $ENV{'CONTENT_LENGTH'});
	} else {
		$buf = $ENV{'QUERY_STRING'};
	}
	foreach ( split(/&/, $buf) ) {
		($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/\r|\n|\0//g;

		$in{$key} = $val;
	}
}

__END__

