#!/usr/local/bin/perl
# ↑あなたが加入しているプロバイダの「perl」言語が使用できる
# パスを指定します。一般的に「#!/usr/local/bin/perl」で大丈夫
#=======================================================================================
# access2 Version 98.1
# Mercury Cusomise Version
#=======================================================================================
#ログファイル
$datafile = 'log.txt';
#----------------------------------------------------------
#ダミーで使用する画像(gif又はjpg形式)
$imgfile = 'dot.gif';
#----------------------------------------------------------
#あなたの最短のアドレス
#同一ページからのリンクをカウントしないため
#私の場合はhttp://www2q.meshnet.or.jp/~terra/
$homepage = '';
#----------------------------------------------------------
#最大記録数
$max = 1000;
#----------------------------------------------------------
#統計表示数の設定
#時間帯別統計を当日1日にする 'yes' / 'no'
$timeflag = 'no';
#----------------------------------------------------------
#ホスト名
$hostvew = 32;
#----------------------------------------------------------
#ブラウザ 表示しない場合は0にする
$agentvew = 20;
#----------------------------------------------------------
#リンク先 表示しない場合は0にする
$linkvew = 30;
#----------------------------------------------------------
#グラフの最大表示数
$graphvew = 200;
#----------------------------------------------------------
#その他のリンク先(ブックマーク等)の集計 'yes' / 'no'
$nolink = 'yes';
#----------------------------------------------------------
#クッキーを使用して正確な人数をカウントする 'yes' / 'no'
$cookieFlag = 'no';
#----------------------------------------------------------
#クッキーを格納する名前を設定する
$CookieName = 'access2';
#=======================================================================================
# 初期設定が必要なのはここまでです。
#=======================================================================================
#日付と時刻を取得して、すべて2桁に統一する
$ENV{'TZ'} = "JST-9"; #日本時間に設定
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$month = sprintf("%02d",$mon + 1);
$mday = sprintf("%02d",$mday);
#日付時刻のフォーマットを整えます
$youbi = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat') [$wday];
$date_now = "$year年$month月$mday日($youbi)";
#access.cgiより追加分
if ($sec < 10) { $sec = "0$sec"; }
if ($min < 10) { $min = "0$min"; }
if ($hour < 10) { $hour = "0$hour"; }
$date = "$year/$month/$mday($youbi) $hour\:$min\:$sec";
#access.cgiより追加分 ここまで
$QUERY_DATA = $ENV{'QUERY_STRING'};
if ($QUERY_DATA eq 'vew') {
&data_read;
$datacount = @DATA;
foreach $line (@DATA) {
($da,$ho,$ag,$hr,$dm) = split(/\,/,$line);
chop($dm);
$dm += 0;
if (!($hr =~ /^http:\/\//i)) { $hr = ''; }
if ($hr =~ /$homepage/i) { $hr = ''; }
if ($hr eq '') { $hr = 'その他(ブックマーク等)' }
#日付ごとの集計
$DA{$da}++;
#時間ごとの集計
if ($timeflag eq 'yes') {
if ($date_now eq $da) { $seaflag = 1; } else { $seaflag = 0; }
} else { $seaflag = 1; }
if ($seaflag == 1) {
$TI{$dm}++;
}
#ホストごとの集計
$HO{$ho}++;
if ($agentvew != 0) {
#ブラウザごとの集計
$AG{$ag}++;
}
if ($linkvew != 0) {
#リンク先ごとの集計
if ($nolink eq 'yes' || $hr ne 'その他(ブックマーク等)') { $HR{$hr}++; }
}
}
#並べ替え
foreach (keys %DA) {
$dummy = "$_\,$_\,$DA{$_}";
push(@DATE,$dummy);
if ($datemax < $DA{$_}) { $datemax = $DA{$_}; }
}
@DATE = sort(@DATE); @DATE = reverse(@DATE);
($s,$d,$datecount) = split(/\,/,$DATE[0]);
foreach (keys %TI) {
$s = sprintf("%04d",$_);
$dummy = "$s\,$_\,$TI{$_}";
push(@TIME,$dummy);
if ($timemax < $TI{$_}) { $timemax = $TI{$_}; }
}
@TIME = sort(@TIME);
foreach (keys %HO) {
$s = sprintf("%04d",$HO{$_});
$dummy = "$s\,$_\,$HO{$_}";
push(@HOST,$dummy);
if ($hostmax < $HO{$_}) { $hostmax = $HO{$_}; }
}
@HOST = sort(@HOST); @HOST = reverse(@HOST);
if ($agentvew != 0) {
foreach (keys %AG) {
$s = sprintf("%04d",$AG{$_});
$dummy = "$s\,$_\,$AG{$_}";
push(@AGENT,$dummy);
if ($agentmax < $AG{$_}) { $agentmax = $AG{$_}; }
}
@AGENT = sort(@AGENT); @AGENT = reverse(@AGENT);
}
if ($linkvew != 0) {
foreach (keys %HR) {
$s = sprintf("%04d",$HR{$_});
$dummy = "$s\,$_\,$HR{$_}";
push(@HREFLINK,$dummy);
if ($linkmax < $HR{$_}) { $linkmax = $HR{$_}; }
}
@HREFLINK = sort(@HREFLINK); @HREFLINK = reverse(@HREFLINK);
}
#HTMLを生成
print "Content-type: text/html\n\n";
print "
access\n";
print "\n";
print "\n";
print "最新 $max アクセスの統計です
";
print "現在のサンプル数 $datacount アクセスです。";
if ($cookieFlag eq 'yes') { print "
重複アクセスを排除した正確な人数を表\示しています。"; }
print "
\n";
print "\n";
print "◆日別統計\n";
print "\n";
print "日付 | \n";
print "カウント | \n";
print "ポイント | \n";
print "グラフ | \n";
foreach $dummy (@DATE) {
($s,$d,$c) = split(/\,/,$dummy);
if ($d =~ /\d{4}\年\d{2}\月\d{2}\日/) {
if ($datacount == 0) {
$per = 0;
$chrlen = 0;
} else {
$per = $c / $datacount * 100;
if ($datemax != 0) { $chrlen = int($c / $datemax * $graphvew); }
}
print "$d | \n";
print "$c | \n";
print "\n";
printf "%10.2f\n" , $per;
print "\% | \n";
print " | \n";
}
}
print " \n";
print "◆時間別統計\n";
if ($timeflag eq 'yes') {
print "$date_now\n";
}
print "\n";
print "時間 | ";
print "カウント | ";
print "ポイント | ";
print "グラフ | \n";
foreach $dummy (@TIME) {
($s,$d,$c) = split(/\,/,$dummy);
if ($d < 1) { $d = 0; }
if ($c != 0) {
if ($datacount == 0) {
$per = 0;
$chrlen = 0;
} else {
if ($timeflag eq 'yes') {
if ($datecount > 0) { $per = $c / $datecount * 100; }
else { $per = 0; $chrlen = 0; }
} else {
$per = $c / $datacount * 100;
}
if ($timemax != 0) { $chrlen = int($c / $timemax * $graphvew); }
}
print "$d 時 | \n";
print "$c | \n";
print "\n";
printf "%10.2f\n" , $per;
print "\% | \n";
print "";
if ($chrlen < 1) { print " "; }
else {
print "\n";
}
print " | \n";
}
}
print "
| \n";
print "◆ホスト統計\n";
print "\n";
print "ホスト名 | ";
print "カウント | ";
print "ポイント | ";
print "グラフ | \n";
$vew = 1;
foreach $dummy (@HOST) {
($s,$h,$c) = split(/\,/,$dummy);
if ($c != 0) {
if ($datacount == 0) {
$per = 0;
$chrlen = 0;
} else {
$per = $c / $datacount * 100;
if ($hostmax != 0) { $chrlen = int($c / $hostmax * $graphvew); }
}
print "$h | \n";
print "$c | \n";
print "\n";
printf "%10.2f\n" , $per;
print "\% | \n";
print "";
if ($chrlen < 1) { print " "; }
else {
print "\n";
}
print " | \n";
}
$vew++;
if ($vew > $hostvew) { last; }
}
print "
| \n";
print "
\n";
if ($agentvew != 0) {
print "◆ブラウザ統計\n";
print "\n";
print "ブラウザ | ";
print "カウント | ";
print "ポイント | ";
print "グラフ |
\n";
$vew = 1;
foreach $dummy (@AGENT) {
($s,$a,$c) = split(/\,/,$dummy);
if ($c != 0) {
if ($datacount == 0) {
$per = 0;
$chrlen = 0;
} else {
$per = $c / $datacount * 100;
if ($agentmax != 0) { $chrlen = int($c / $agentmax * $graphvew); }
}
print "$a | \n";
print "$c | \n";
print "\n";
printf "%10.2f\n" , $per;
print "\% | \n";
print "";
if ($chrlen < 1) { print " "; }
else {
print "\n";
}
print " |
\n";
}
$vew++;
if ($vew > $agentvew) { last; }
}
print "
\n";
}
if ($linkvew != 0) {
print "◆リンク先統計\n";
print "\n";
print "リンク先 | ";
print "カウント | ";
print "ポイント | ";
print "グラフ |
\n";
$vew = 1;
foreach $dummy (@HREFLINK) {
($s,$a,$c) = split(/\,/,$dummy);
if ($c != 0) {
if ($datacount == 0) {
$per = 0;
$chrlen = 0;
} else {
$per = $c / $datacount * 100;
if ($linkmax != 0) { $chrlen = int($c / $linkmax * $graphvew); }
}
print "";
print "$a";
print " | \n";
print "$c | \n";
print "\n";
printf "%10.2f\n" , $per;
print "\% | \n";
print "";
if ($chrlen < 1) { print " "; }
else {
print "\n";
}
print " |
\n";
}
$vew++;
if ($vew > $linkvew) { last; }
}
print "
\n";
}
print " \n";
print "Access2 by Terra
\n";
print "\n";
exit;
} else {
if ($cookieFlag eq 'yes') {
$ENV{'TZ'} = "GMT";
($c_sec,$c_min,$c_hour,$c_mday,$c_mon,$c_year,$c_wday,$c_yday,$c_isdst) = localtime(time + 3 * 86400);
$cookies = $ENV{'HTTP_COOKIE'};
@pairs = split(/;/,$cookies);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$name =~ s/ //g;
$DUMMY{$name} = $value;
}
@pairs = split(/,/,$DUMMY{$CookieName});
foreach $pair (@pairs) {
($name, $value) = split(/\!/, $pair);
$COOKIE{$name} = $value;
}
}
if ($COOKIE{'date'} ne $date_now) {
&data_read;
$datacount = @DATA;
$hostadd = &domain_name;
#addition from access.cgi
$addr = $ENV{'REMOTE_ADDR'};
$host = $ENV{'REMOTE_HOST'};
if (($host eq $addr) || ($host eq '')) {
$host = gethostbyaddr(pack('C4',split(/\./,$addr)),2) || $addr;
}
#addition from access.cgi
$agent = $ENV{'HTTP_USER_AGENT'};
$agent =~ s/,/./g;
$agent =~ s/\; Yahoo\! JAPAN Version Windows 95\/NT CD\-ROM Edition 1\.0\.//;
$hreflink = $QUERY_DATA;
$value = "[$date]\,$host\,$agent\,$hreflink\,$addr\n";
while ($datacount >= $max) {
pop(@DATA);
$datacount = @DATA;
}
unshift(@DATA,$value);
&data_save;
if ($cookieFlag eq 'yes') {
$c_year = sprintf("%02d",$c_year);
$c_sec = sprintf("%02d",$c_sec);
$c_min = sprintf("%02d",$c_min);
$c_hour = sprintf("%02d",$c_hour);
$c_mday = sprintf("%02d",$c_mday);
$youbi = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday') [$c_wday];
$month = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec') [$c_mon];
$date_gmt = "$youbi, $c_mday\-$month\-$c_year $c_hour:$c_min:$c_sec GMT";
$cook = "date\!$date_now";
print "Set-Cookie: $CookieName=$cook; expires=$date_gmt\n";
}
}
print "Content-type: image/jpeg\n\n";
open(IMG, "$imgfile") || die "Can't open: $dir/$img";
print $_ while ();
close(IMG);
exit;
}
#=======================================================================================
sub domain_name {
local($addr) = $ENV{'REMOTE_ADDR'};
local($_) = gethostbyaddr(pack("C4",split(/\./,$addr)),2);
if ($_ eq '') { $_ = $addr; }
else {
if (/.+\.(.+)\.(.+)\.(.+)$/) { $_ = "\*\.$1\.$2\.$3"; }
elsif (/.+\.(.+)\.(.+)$/) { $_ = "\*\.$1\.$2"; }
elsif (/.+\.(.+)$/) { $_ = "\*\.$1"; }
else { $_ = "on the internet"; }
}
$_;
}
#=======================================================================================
sub data_read {
if (open(DB,"$datafile")) {
@DATA = ;
close(DB);
}
}
#=======================================================================================
sub data_save {
$tmpfile = 'access2.tmp';
foreach (1 .. 10) {
unless (-f $tmpfile) { $tmpflag = 1; last; }
$tmpflag = 0;
sleep(1);
}
if ($tmpflag == 1) {
$tmp_dummy = "$$\.tmp";
if (!open(TMP,">$tmp_dummy")) { &error(bad_tmpfile); }
close(TMP);
chmod 0666,$tmp_dummy;
if (!open(TMP,">$tmp_dummy")) { &error(bad_tmpfile); }
print TMP @DATA;
close(TMP);
foreach (1 .. 10) {
unless (-f $tmpfile) {
if (!open(TMP,">$tmpfile")) { &error(bad_tmpfile); }
close(TMP);
rename($tmp_dummy,$datafile);
unlink $tmpfile;
$tmpflag = 1;
last;
}
$tmpflag = 0;
sleep(1);
}
if (-f $tmp_dummy) { unlink $tmp_dummy; }
}
$tmpflag;
}