#!/usr/bin/perl

#デイリーアクセスログ
#Daily Access Log - dailylog.cgi
#Ver 1.56
#Copyright(C) 1999 - 2000 MOG T.Kitazaki
#CGI Pocket http://pocket.727.net/
#<pocket@727.net>

############
#設定箇所
############

#戻りのリンク先の指定
$Home = 'http://www.akinai.ne.jp/~ikoi/';

#タイトル
$Title = '居酒屋いこい「アクセス統計」';

#背景色
$Bgcol = 'white';

#文字色
$Text = '#404040';

#リンク色（未読）
$Link = 'red';

#リンク色（既読）
$Vlink = 'blue';

#リンク色（リンク中）
$Alink = 'lime';

#表示テーブル枠
$Tblbor = 0;

#表示テーブル色上段
$Tblcol1 = '#e8f0ff';

#表示テーブル色中段
$Tblcol2 = '#f0f0f0';

#表示テーブル色詳細表示部
$Tblcol3 = 'ivory';

#壁紙の指定
$Background = '';

#リモホの取得
#0 = ＩＰアドレスで取得
#1 = リモートホスト名で取得
$Getremote = 1;

############
#ここまで
############

#このCGIの名前
$Cgi = 'dailylog.cgi';

#管理者プログラム
$Manage = 'manage.cgi';

#データインデックスファイル
$Index = 'dailylog.dat';

#データディレクトリ
$Dir = 'data';

#リンク用画像ファイル
$Logo = 'logo.gif';

#グラフ表示用画像ファイル
$Gif1 = 'blue.gif';
$Gif2 = 'red.gif';
$Gif3 = 'green.gif';

$Footlogo = '<a href="http://www.akinai.ne.jp/~ikoi/">HOME</a>';

$times = time;

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($times);

$mon++;
$year += 1900;

$Nowdate = $year . '-' . $mon . '-' . $mday;

$imgw = 2;
$imgh = 10;
$cols = 150;

$perlver = $];
if($perlver >= 5){

	use Socket;
	if(not $ENV{'REMOTE_HOST'}){
		$hostaddr = inet_aton($ENV{'REMOTE_ADDR'});
		$Host = gethostbyaddr ($hostaddr, &AF_INET);
		if($Host){
			$Ip = inet_ntoa ($hostaddr);
		}
		else{
			$Ip = $ENV{'REMOTE_ADDR'};
		}
	}
	else{
		$Host = $ENV{'REMOTE_HOST'};
		$Ip = $ENV{'REMOTE_ADDR'};
	}

}
else{
	$Host = $ENV{'REMOTE_HOST'};
	if($Host eq ''){
		$hostaddr = pack('C4', (split(/\./,$ENV{'REMOTE_ADDR'})));
		$Host = gethostbyaddr ($hostaddr, 2);
	}
	$Ip = $ENV{'REMOTE_ADDR'};
}


if($Getremote){
	$HOST = $Host;
}
else{
	$HOST = $Ip;
}

$AGENT = $ENV{'HTTP_USER_AGENT'};

$HOST =~ s/,//g;
$AGENT =~ s/,//g;

if($Background ne ''){
	$Bg = " background=\"$Background\"";
}
else{
	$Bg = '';
}

&Read_Formdata;

if($IN{'mode'} eq 'view'){

	&View;

}
elsif($IN{'mode'} eq 'view2'){

	&View2;

}
elsif($IN{'mode'} eq 'all'){

	&All;

}
else{

	$filename = $Dir . '/' . $Nowdate;

	unless(-e $filename){
		$f = 1;
		open (INDEX,$Index);
		while (<INDEX>){
			chomp $_;
			if($Nowdate eq $_){ $f = 0; last; }
		}
		close (INDEX);

		if($f){
			open (INDEX, ">>$Index");
			print INDEX "$Nowdate\n";
			close (INDEX);
		}
	}

	$logdat = "$hour\,$HOST\,$AGENT\,\n";

	open (LOG,">>$filename"); print LOG $logdat; close (LOG);

	print "Location: $Logo\n\n";

}

exit(0);

#####

sub View{

	if($IN{'date'} eq '' || $IN{'date'} eq $Nowdate){
		$date = $Nowdate;
		$filename = $Dir . '/' . $Nowdate;
		$now = $hour;
	}
	else{
		$date = $IN{'date'};
		$filename = $Dir . '/' . $IN{'date'};
		$now = 23;
	}
	open (INDEX,$Index); @files = <INDEX>;close (INDEX);

	if(!open (LOG,$filename)){
		&Err("指定された日付のデータは記録されていません");
		exit (0);
	}
	@logs = <LOG>; close (LOG);

	$hits = $#logs + 1;

	for ($i = 0; $i <= $#logs; $i++){
		chop($logs[$i]);
		@log = split(/\,/,$logs[$i]);
		$DATAS{$log[0]}++;
		$HOSTS{$log[1]}++;
		$AGENTS{$log[2]}++;
		$hourhosts = $log[0] . '__E__' . $log[1];
		$houragents = $log[0] . '__E__' . $log[2];
		$H{$hourhosts}++;
		$A{$houragents}++;
	}

	print "Content-type: text/html\n\n";

	print <<EOM;
<html>
<head><title>$Title</title></head>

<body bgcolor="$Bgcol" text="$Text" link="$Link" vlink="$Vlink" alink="$Alink"$Bg>
<basefont size="3">
<center>

EOM

	if($IN{'mode_enter'} eq 'host'){
		print <<EOM;
<table border=3>
<tr>
<th>
<font size=4>$IN{'date'} - 時間別（ホスト）アクセス統計</font>
</th>
</tr>

<tr>
<form method="POST" action="$Cgo">
<th>
<input type="hidden" name="mode" value="view">
<input type="hidden" name="fullview" value="fullview">
<input type="hidden" name="date" value="$IN{'date'}">
<input type="submit" value="BACK">
</th>
</form>
</tr> 
</table>

<br>
<br>

<table border="$Tblbor" width="600">
<tr>
<th colspan=3 bgcolor="$Tblcol1">時間別（ホスト）アクセス統計</th>
</tr>
<tr>
<td align="center" width="40%" bgcolor="$Tblcol2">ホスト</td>
<td align="center" width="10%" bgcolor="$Tblcol2">カウント数</td>
<td align="center" width="50%" bgcolor="$Tblcol2">カウント数グラフ</td>
</tr>
EOM

		for ($i = $now; $i >= 0; $i--){

			print <<EOM;
<tr>
<th colspan=3 bgcolor="$Tblcol1">$i:00〜$i:59</th>
</tr>
EOM

			undef %GH;
			while (($hk, $hv)= each %H){
				($hou, $hos) = split (/__E__/,$hk);
				if($hou == $i){ $GH{$hos} += $hv; }
			}
			$ke = values %GH;
			if($ke == 0){

				print <<EOM;
<tr>
<td colspan=3 bgcolor="$Tblcol3" colspan="3">-</td>
</tr>
EOM
			}
			else{
				foreach $hk (sort By_Number4 keys (%GH)){
					print "<tr><td width=40% bgcolor=$Tblcol3>$hk</td><td width=10% align=right bgcolor=$Tblcol3>$GH{$hk}</td><td width=50% bgcolor=$Tblcol3>\n";

					$col = int ($GH{$hk} / $cols);
					$yo  = int ($GH{$hk} % $cols);
					while ($col--){
						$iw = $imgw * $cols;
						print "<img src=\"$Gif2\" width=\"$iw\" height=\"$imgh\"><br>";
					}
					$iw = $imgw * $yo;
					print "<img src=\"$Gif2\" width=\"$iw\" height=\"$imgh\">";
					print "</td></tr>\n";
				}
			}
		}

		print <<EOM;
</table>
EOM

	}
	elsif($IN{'mode_enter'} eq 'agent'){
		print <<EOM;
<table border=3>
<tr>
<th>
<font size=4>$IN{'date'} - 時間別（ブラウザ）アクセス統計</font>
</th>
</tr>
<tr>
<form method="POST" action="$Cgo">
<th>
<input type="hidden" name="mode" value="view">
<input type="hidden" name="fullview" value="fullview">
<input type="hidden" name="date" value="$IN{'date'}">
<input type="submit" value="BACK">
</th>
</form>
</tr> 
</table>

<br>
<br>

<table border="$Tblbor" width="600">
<tr>
<th colspan=3 bgcolor="$Tblcol1">時間別（ブラウザ）アクセス統計</th>
</tr>
<tr>
<td align="center" width="40%" bgcolor="$Tblcol2">ブラウザ名</td>
<td align="center" width="10%" bgcolor="$Tblcol2">カウント数</td>
<td align="center" width="50%" bgcolor="$Tblcol2">カウント数グラフ</td>
</tr>
EOM

		for ($i = $now; $i >= 0; $i--){

			print <<EOM;
<tr>
<th colspan=3 bgcolor="$Tblcol1">$i:00〜$i:59</th>
</tr>
EOM
			undef %GH;
			while (($hk, $hv)= each %A){
				($hou, $hos) = split (/__E__/,$hk);
				if($hou == $i){ $GH{$hos} += $hv; }
			}
			$ke = values %GH;
			if($ke == 0){
				print <<EOM;
<tr>
<td colspan=3 bgcolor="$Tblcol3" colspan="3">-</td>
</tr>
EOM
			}
			else{
				foreach $hk (sort By_Number4 keys (%GH)){
					print "<tr><td width=40% bgcolor=$Tblcol3><font size=-2>$hk</font></td><td width=10% align=right bgcolor=$Tblcol3>$GH{$hk}</td><td width=50% bgcolor=$Tblcol3>\n";

					$col = int ($GH{$hk} / $cols);
					$yo  = int ($GH{$hk} % $cols);
					while ($col--){
						$iw = $imgw * $cols;
						print "<img src=\"$Gif3\" width=\"$iw\" height=\"$imgh\"><br>";
					}
					$iw = $imgw * $yo;
					print "<img src=\"$Gif3\" width=\"$iw\" height=\"$imgh\">";
					print "</td></tr>\n";
				}
			}
		}
		print <<EOM;
</table>
EOM
	}
	else{
		print <<EOM;
<table border=3>
<tr>
<th colspan="5">
<font size=4>$Title</font>
</th>
</tr>

<tr>
<form method="POST" action="$Cgi">
<th>
<input type="hidden" name="mode" value="all">
<input type="submit" value="全アクセス解析">
</th>
</form>
<form method="POST" action="$Cgi">
<th>
EOM
		if($IN{'fullview'} eq 'fullview'){
			print <<EOM;
<input type="hidden" name="mode" value="view">
<input type="hidden" name="date" value="$IN{'date'}">
<input type="submit" value="時間別アクセスのみ表\示">
EOM
		}
		else{
			print <<EOM;
<input type="hidden" name="mode" value="view">
<input type="hidden" name="fullview" value="fullview">
<input type="hidden" name="date" value="$IN{'date'}">
<input type="submit" value="ホスト/ブラウザ解析も表\示">
EOM
		}

		print <<EOM;
</th>
</form>
<form action="$Home">
<th>
<input type="submit" value="BACK">
</th>
</form>
<form method="POST" action="$Manage">
<td>
<input type="password" name="password" size=10>
</td>
<th>
<input type="submit" value="管理者用">
</th>
</form>
</tr> 
</table>

<br>
<br>
EOM

		if($IN{'date'} eq '' || $IN{'date'} eq $Nowdate){
			print <<EOM;
<table border="$Tblbor" width="600">
<tr>
<td align="center" bgcolor="$Tblcol1">
<b>
本日[ $Nowdate ]のアクセス統計
</b>
</td>
</tr>
<tr>
<td align="center" bgcolor="$Tblcol3">
本日はこれまで <b>$hits</b> 件のアクセスがあります
</td>
</tr>
</table>
EOM
		}
		else{
			print <<EOM;
<table border="$Tblbor" width="600">
<tr>
<td align="center" bgcolor="$Tblcol1">
<b>
[ $IN{'date'} ]のアクセス統計
</b>
</td>
</tr>
<tr>
<td align="center" bgcolor="$Tblcol3">
$IN{'date'} は <b>$hits</b> 件のアクセスがありました
</td>
</tr>
</table>
EOM
}

		print <<EOM;
<br>
<br>

<table border="$Tblbor" width="600">
<tr>
<th colspan=3 bgcolor="$Tblcol1">時間別アクセス統計</th>
</tr>
<tr>
<td align="center" width="40%" bgcolor="$Tblcol2">時間</td>
<td align="center" width="10%" bgcolor="$Tblcol2">カウント数</td>
<td align="center" width="50%" bgcolor="$Tblcol2">カウント数グラフ</td>
</tr>
EOM

		for ($i = $now; $i >= 0; $i--){

			if($DATAS{$i} eq '') { $DATAS{$i} = 0; }

			print "<tr><td width=40% bgcolor=$Tblcol3>$i:00〜$i:59</td><td width=10% align=right bgcolor=$Tblcol3>$DATAS{$i}</td><td width=50% bgcolor=$Tblcol3>\n";

			if($DATAS{$i} == 0){ print "<br></td></tr>\n"; }
			else{
				$col = int ($DATAS{$i} / $cols);
				$yo  = int ($DATAS{$i} % $cols);
				while ($col--){
					$iw = $imgw * $cols;
					print "<img src=\"$Gif1\" width=\"$iw\" height=\"$imgh\"><br>";
				}
				$iw = $imgw * $yo;
				print "<img src=\"$Gif1\" width=\"$iw\" height=\"$imgh\">";
			}
		}

		print <<EOM;
</table>

<br>
<br>
EOM

		if($IN{'fullview'} eq 'fullview'){

			print <<EOM;
<table border="$Tblbor" width="600">
<tr>
<th colspan="3" bgcolor="$Tblcol1">ホスト別アクセス統計</th>
</tr>
<tr>
<td align="center" width="40%" bgcolor="$Tblcol2">ホスト</td>
<td align="center" width="10%" bgcolor="$Tblcol2">カウント数</td>
<td align="center" width="50%" bgcolor="$Tblcol2">カウント数グラフ</td>
</tr>
EOM

		foreach (sort By_Number2 keys (%HOSTS)){
			print "<tr><td width=40% bgcolor=$Tblcol3>$_</td><td width=10% bgcolor=$Tblcol3 align=right>$HOSTS{$_}</td><td width=40%  bgcolor=$Tblcol3>\n";

			$col = int ($HOSTS{$_} / $cols);
			$yo  = int ($HOSTS{$_} % $cols);
			while ($col--){
				$iw = $imgw * $cols;
				print "<img src=\"$Gif2\" width=\"$iw\" height=\"$imgh\"><br>";
			}
			$iw = $imgw * $yo;
			print "<img src=\"$Gif2\" width=\"$iw\" height=\"$imgh\">";
			print "</td></tr>\n";
		}

		print <<EOM;
<tr>
<form method="post" action="$Cgi">
<input type="hidden" name="mode" value="view">
<input type="hidden" name="date" value="$date">
<input type="hidden" name="mode_enter" value="host">
<th colspan="3" bgcolor="$Tblcol1">
<input type="submit" value="さらに詳しく（時間別表\示）">
</th>
</form>
</tr>
</table>

<br>
<br>

<table border="$Tblbor" width=600>
<tr>
<th colspan="3" bgcolor="$Tblcol1">ブラウザ別アクセス統計</th>
</tr>
<tr>
<td align="center" width="40%" bgcolor="$Tblcol2">ブラウザ名</td>
<td align="center" width="10%" bgcolor="$Tblcol2">カウント数</td>
<td align="center" width="50%" bgcolor="$Tblcol2">カウント数グラフ</td>
</tr>
EOM

		foreach (sort By_Number3 keys (%AGENTS)){
			print "<tr><td width=40% bgcolor=$Tblcol3><font size=-2>$_</font></td><td width=10% bgcolor=$Tblcol3 align=right>$AGENTS{$_}</td><td width=40%  bgcolor=$Tblcol3>\n";

			$col = int ($AGENTS{$_} / $cols);
			$yo  = int ($AGENTS{$_} % $cols);
			while ($col--){
				$iw = $imgw * $cols;
				print "<img src=\"$Gif3\" width=\"$iw\" height=\"$imgh\"><br>";
			}
			$iw = $imgw * $yo;
			print "<img src=\"$Gif3\" width=\"$iw\" height=\"$imgh\">";
			print "</td></tr>\n";
		}

		print <<EOM;
<tr>
<form method="post" action="$Cgi">
<input type="hidden" name="mode" value="view">
<input type="hidden" name="date" value="$date">
<input type="hidden" name="mode_enter" value="agent">
<th colspan="3" bgcolor="$Tblcol1">
<input type="submit" value="さらに詳しく（時間別表\示）">
</th>
</form>
</tr>
</table>

<br>
<br>
EOM
		}

		if($IN{'date'} ne '' && $IN{'date'} ne $Nowdate){
			print <<EOM;
<table border="$Tblbor">
<tr>
<th colspan=4 bgcolor="$Tblcol1"><a href="$Cgi?mode=view">本日のデータ</a></th>
</tr>
</table>
<br>
<br>
EOM

		}

		print <<EOM;
<table border="$Tblbor" width="600">
<tr>
<th colspan=4 bgcolor="$Tblcol1">昨日までのデータ</th>
</tr>
EOM

		$lop = 0;
		foreach (@files){
			chop ($_);
			if($_ ne $Nowdate){
				if($lop == 0){ print "<tr>\n"; }

				print <<EOM;
<td align=center width=25% bgcolor=$Tblcol3>
<a href="$Cgi?mode=view&date=$_">$_</a>
</td>
EOM
				$lop++;
				if($lop == 4){
					$lop  = 0; print "</tr>\n";
				}
			}
		}

		if($lop != 0){ print "</tr>\n"; }

		print <<EOM;
</table>
EOM

	}

	print <<EOM;
<br>
<br>
$Footlogo
</center>
</body>
</html>
EOM

}

#####

sub All{

	$allcount = 0;

	open (INDEX,$Index); @files = <INDEX>; close (INDEX);

	print "Content-type: text/html\n\n";

	print <<EOM;
<html>
<head><title>$Title</title></head>

<body bgcolor="$Bgcol" text="$Text" link="$Link" vlink="$Vlink" alink="$Alink"$Bg>
<basefont size="3">
<center>

<table border=3>
<tr>

<th>
<font size=+2>全アクセス解析</font>
</th>

<form method="POST" action="$Cgi">
<input type="hidden" name="mode" value="view">
<th>
<input type="submit" value="BACK">
</th>
</form>
EOM

	if($IN{'allmode'} eq ''){
		print <<EOM;
<form method="POST" action="$Cgi">
<th>
<input type="hidden" name="mode" value="all">
<input type="hidden" name="allmode" value="allmode">
<input type="submit" value="ホスト/ブラウザ解析も表\示">
</th>
</form>
EOM

	}

	print <<EOM;
</tr> 
</table>

<br>
<br>

<table border="$Tblbor" width="600">
<tr>
<th colspan=3 bgcolor="$Tblcol1">日別アクセス統計</th>
</tr>

<tr>
<td width="35%" align="center" bgcolor="$Tblcol2">日付</td>
<td width="15%" align="center" bgcolor="$Tblcol2">カウント</td>
<td width="50%" align="center" bgcolor="$Tblcol2">カウントグラフ</td>
</tr>
EOM

	$pos = 0;
	$days = $#files + 1;

	foreach $line (@files){
		chop ($line);
		if($pos == 0){ $start = $line; }
		$pos++;
		$end = $line;
		$Openfile = $Dir . '/' . $line;

		open (DAT,$Openfile); @dat = <DAT>; close (DAT);

		$count = $#dat + 1;
		$count_loop = int ($count / 10);

		print <<EOM;
<tr>
<td width="35%" bgcolor="$Tblcol3">$line</td>
<td width="15%" bgcolor="$Tblcol3">$count</td>
<td width="50%" bgcolor="$Tblcol3">
EOM

	if($count == 0){ print "<br>\n"; }
	else{
		$col = int ($count / $cols);
		$yo  = int ($count % $cols);
		while ($col--){
			$iw = $imgw * $cols;
			print "<img src=\"$Gif1\" width=\"$iw\" height=\"$imgh\"><br>";
		}
		$iw = $imgw * $yo;
		print "<img src=\"$Gif1\" width=\"$iw\" height=\"$imgh\">";
	}

		print <<EOM;
</td>
</tr>
EOM
		foreach $dline (@dat){
			chop ($dline);
			@data = split(/,/,$dline);
			$DATAS{"$data[0]"}++;
			$HOSTS{"$data[1]"}++;
			$AGENTS{"$data[2]"}++;
			$allcount++;
		}
		close (DAT);
	}

	$ave = $allcount / $days;
	@av = split(/\./,$ave);
	$ave = $av[0];

	print <<EOM;
<tr>
<td colspan="4" bgcolor="$Tblcol2" align="center">
$start 〜 $end 間の記録 アクセス $allcount 件 1日平均 $ave 件
</td>
</tr>
</table>

<br>
<br>

<table border="$Tblbor" width="600">
<tr>
<th colspan=4 bgcolor="$Tblcol1">時間別アクセス統計</th>
</tr>

<tr>
<td width="45%" align="center" bgcolor="$Tblcol2">アクセス時間</td>
<td width="15%" align="center" bgcolor="$Tblcol2">カウント</td>
<td width="10%" align="center" bgcolor="$Tblcol2">比率</td>
<td width="30%" align="center" bgcolor="$Tblcol2">比率グラフ</td>
</tr>
EOM

	$now = 23;

	for ($i = $now; $i >= 0; $i--){

		if($DATAS{$i} eq '') { $DATAS{$i} = 0; }
		if($DATAS{$i} != 0){
			if($DATAS{$i} == $allcount){
				$ritu = 100;
			}
			else{
				$ritu = $DATAS{$i}/$allcount;
				($ritu1,$ritu2) = split(/\./,$ritu);
				@tmp = split (//,$ritu2);
				$tmp[0] =~ s/0//;
                if($tmp[2] >= 5){
					if($tmp[1] == 9){
						$tmp[0]++;
						$tmp[1] = 0;
					}
					else{
						$tmp[1]++;
					}
				}
				$ritu = $tmp[0] . $tmp[1];
			}
		}
		else{ $ritu = 0; }

		print "<tr><td width=45% bgcolor=$Tblcol3>$i:00〜$i:59</td><td width=15% align=right bgcolor=$Tblcol3>$DATAS{$i}</td><td width=10% align=right bgcolor=$Tblcol3>$ritu%</td><td width=30% bgcolor=$Tblcol3>\n";

		if($ritu == 0){ print "</br></td></tr>\n"; }
		else{
			$iw = int (($imgw * $ritu) * 1.5);
			print "<img src=\"$Gif1\" width=\"$iw\" height=\"$imgh\">";
			print "</td></tr>\n";
		}
	}

	print <<EOM;
</table>

<br>
<br>
EOM

	if($IN{'allmode'} eq 'allmode'){
		print <<EOM;
<table border="$Tblbor" width="600">
<tr>
<th colspan="4" bgcolor="$Tblcol1">ホスト別アクセス統計</th>
</tr>

<tr>
<td width="45%" align="center" bgcolor="$Tblcol2">ホスト名</td>
<td width="15%" align="center" bgcolor="$Tblcol2">カウント</td>
<td width="10%" align="center" bgcolor="$Tblcol2">比率</td>
<td width="30%" align="center" bgcolor="$Tblcol2">比率グラフ</td>
</tr>
EOM

	foreach (sort By_Number2 keys (%HOSTS)){

			if($HOSTS{$_} == $allcount){
				$ritu = 100;
			}
			else{
				$ritu = $HOSTS{$_}/$allcount;
				($ritu1,$ritu2) = split(/\./,$ritu);
				@tmp = split (//,$ritu2);
				$tmp[0] =~ s/0//;
                if($tmp[2] >= 5){
					if($tmp[1] == 9){
						$tmp[0]++;
						$tmp[1] = 0;
					}
					else{
						$tmp[1]++;
					}
				}
				$ritu = $tmp[0] . $tmp[1];
			}

		print "<tr><td width=45% bgcolor=$Tblcol3>$_</td><td width=15% align=right bgcolor=$Tblcol3>$HOSTS{$_}</td><td width=10% align=right bgcolor=$Tblcol3>$ritu%</td><td width=30% bgcolor=$Tblcol3>\n";
			if($ritu == 0){ print "<br>"; }
			else{
				$iw = int (($imgw * $ritu) * 1.5);
				print "<img src=\"$Gif2\" width=\"$iw\" height=\"$imgh\">";
				print "</td></tr>\n";
			}

		print "</td></tr>\n";

	}

	print <<EOM;
</table>

<br>
<br>

<table border="$Tblbor" width="600">
<tr>
<th colspan=4 bgcolor="$Tblcol1">ブラウザ別アクセス統計</th>
</tr>

<tr>
<td width="45%" align="center" bgcolor="$Tblcol2">ブラウザ名</td>
<td width="15%" align="center" bgcolor="$Tblcol2">カウント</td>
<td width="10%" align="center" bgcolor="$Tblcol2">比率</td>
<td width="30%" align="center" bgcolor="$Tblcol2">比率グラフ</td>
</tr>
EOM

	foreach (sort By_Number3 keys (%AGENTS)){

			if($AGENTS{$_} == $allcount){
				$ritu = 100;
			}
			else{
				$ritu = $AGENTS{$_}/$allcount;
				($ritu1,$ritu2) = split(/\./,$ritu);
				@tmp = split (//,$ritu2);
				$tmp[0] =~ s/0//;
                if($tmp[2] >= 5){
					if($tmp[1] == 9){
						$tmp[0]++;
						$tmp[1] = 0;
					}
					else{
						$tmp[1]++;
					}
				}
				$ritu = $tmp[0] . $tmp[1];
			}

		print "<tr><td width=45% bgcolor=$Tblcol3><font size=-2>$_</font></td><td width=15% align=right bgcolor=$Tblcol3>$AGENTS{$_}</td><td width=10% align=right bgcolor=$Tblcol3>$ritu%</td><td width=30% bgcolor=$Tblcol3>\n";

			if($ritu == 0){ print "<br>"; }
			else{
				$iw = int (($imgw * $ritu) * 1.5);
				print "<img src=\"$Gif3\" width=\"$iw\" height=\"$imgh\">";
				print "</td></tr>\n";
			}

		print "</td></tr>\n";

	}

	print <<EOM;
</table>

<br>
<br>
EOM
	}

	print <<EOM;
$Footlogo
</center>
</body>
</html>
EOM


}

#####

sub Read_Formdata{

	if ($ENV{'REQUEST_METHOD'} eq "POST") {
  		read(STDIN, $buf, $ENV{'CONTENT_LENGTH'});
	}
	else {
  		$buf = $ENV{'QUERY_STRING'};
	}

	@lens = split(/&/,$buf);

	foreach $len(@lens){

		($key, $value) = split(/=/,$len,2);
		$value =~ tr/+/ /;
		$key =~ s/%(..)/pack("c",hex($1))/ge;
		$value =~ s/%(..)/pack("c",hex($1))/ge;

		$IN{$key} = $value;

	}
}

#####

sub By_Number2{

	if($HOSTS{$b} != $HOSTS{$a}){
		$HOSTS{$b} <=> $HOSTS{$a};
	}
	else{
		$HOSTS{$b} cmp $HOSTS{$a};
	}

}

#####

sub By_Number3{

	if($AGENTS{$b} != $AGENTS{$a}){
		$AGENTS{$b} <=> $AGENTS{$a};
	}
	else{
		$AGENTS{$b} cmp $AGENTS{$a};
	}

}

#####

sub By_Number4{

	if($GH{$b} != $GH{$a}){
		$GH{$b} <=> $GH{$a};
	}
	else{
		$GH{$b} cmp $GH{$a};
	}

}

#####

sub Err{

	local ($msg) = @_;

	print "Content-type: text/html\n\n";

	print <<EOM;
<html>
<head>
<title>Error Message</title>
</head>
<body>
<center>
<b>
■ Error Message ■
<hr>
$msg
<hr>
<a href="$Cgi?mode=view">BACK</a>
</b>
</center>
</body>
</html>
EOM

}

#####