#!/usr/local/bin/perl
#汎用アクセスログ取得CGI V2
#
#漏れIP取得方法としてHTTP_X_FORWARDED_FOR、HTTP_FORWARDED、HTTP_VIA、HTTP_SP_HOST、
#HTTP_CLIENT_IP(16進数変換)の5つをチェックしてIP漏れがある場合はTrueIPとして記録します。
#
#設置方法
#このファイル名をlog.cgiに変更して下さい。
#アクセスログを取りたいHTMLに下記のようなイメージタグを書き込んで下さい。
#
#1×1ピクセルの透明GIFですのでパッと見た目にはわかりません(^^;;;;
#実際にアクセスログを見る時は http://www.〜/log.txt にアクセスすれば見れます。
#
#########初期設定#########
$aclog = "log.txt"; # アクセスログの場所(log.cgiと同じディレクトリの場合はこのまま)
$max = "500"; #最大ログ件数
##########################
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time + 17*60*60);
$month = ($mon + 1);
$year = ($year + 1900);
if ($sec < 10) { $sec = "0$sec"; }
if ($min < 10) { $min = "0$min"; }
if ($hour < 10) { $hour = "0$hour"; }
if ($mday < 10) { $mday = "0$mday"; }
if ($month < 10) { $month = "0$month"; }
$date = "$month/$mday/$year $hour\:$min\:$sec";
$addr = $ENV{'REMOTE_ADDR'};
$host = $ENV{'REMOTE_HOST'};
if (($host eq $addr) || ($host eq '')) {
$host = gethostbyaddr(pack('C4',split(/\./,$addr)),2) || $addr;
}
$via = $ENV{'HTTP_VIA'};
$hua = $ENV{'HTTP_USER_AGENT'};
$sphost = $ENV{'HTTP_SP_HOST'};
$clip = $ENV{'HTTP_CLIENT_IP'};
$hfrom = $ENV{'HTTP_FROM'};
$foward = $ENV{'HTTP_FORWARDED'};
$xfoward = $ENV{'HTTP_X_FORWARDED_FOR'};
$trueip= &getip;
open(LOG,"$aclog") || die "Can't Open User Access Log: $!\n";
@lines=;
close(LOG);
$axs=@lines;
$max2 = ($max - 2);
if ($axs >= $max) {
open(LOG,">$aclog") || die "Can't Open User Access Log: $!\n";
print LOG "[$date] - $host - $addr - $trueip - $via - $hua\n";
foreach $axs (0..$max2) {
print LOG $lines[$axs];
}
}
else {
open(LOG,">$aclog") || die "Can't Open User Access Log: $!\n";
print LOG "[$date] - $host - $addr - $trueip - $via - $hua\n";
foreach $log (@lines) {
print LOG $log;
}
}
close(LOG);
sub getip{
$trueip=$sphost if( $sphost ne "");
$trueip=$via if( $via=~ s/.*\s(\d+)\.(\d+)\.(\d+)\.(\d+)/$1.$2.$3.$4/ );
if( $clip=~ s/^(\d+)\.(\d+)\.(\d+)\.(\d+)(\D*).*/$1.$2.$3.$4/ ){
$trueip=$clip;
}elsif( $clip=~ s/^([\dA-F]{2})([\dA-F]{2})([\dA-F]{2})([\dA-F]{2})/$1$2$3$4/i){
$clip = join('.', hex($1), hex($2), hex($3), hex($4));
$trueip=$clip;
}
$trueip=$foward if( $foward=~ s/.*\s(\d+)\.(\d+)\.(\d+)\.(\d+)/$1.$2.$3.$4/ );
$trueip=$xfoward if( $xfoward=~ s/^(\d+)\.(\d+)\.(\d+)\.(\d+)(\D*).*/$1.$2.$3.$4/ );
$trueip=$hfrom if ($hfrom ne "");
return $trueip;
}#getip END
print "Content-type: image/gif\n\n";
print `/bin/cat log.gif`;
exit(0);