#!/usr/local/bin/perl #--------------------------------------------------------- # cntsx counter # Copyright (c) 2000-2001 ZERONET # Japan Mie Matsusaka # http://www.zeronet.ne.jp/ # # 2000/07/01 1.0初版 #--------------------------------------------------------- # 画像連結ライブラリ require './gifcat.pl'; # #--------------------------------------------------------- # 初期設定 #--------------------------------------------------------- #(0=no 1=yes) $lock_check = 1; $id_check = 1; $userdir = "./cntdat"; $lockdir = "./cntdat"; #--------------------------------------------------------- # main #--------------------------------------------------------- # デコード処理 $buffer = $ENV{'QUERY_STRING'}; if ($buffer) { @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; $value =~ s/\r//g; $value =~ s/\n//g; $value =~ s/\t//g; $FORM{$name} = $value; } $refurl = $FORM{'refurl'}; $gif = $FORM{'gif'}; $keta = $FORM{'keta'}; $id = $FORM{'id'}; } # 指定がない場合はgif1を使用 if ($gif eq "") { $gifdir = "./gif1/";} elsif ($gif eq "gif1") { $gifdir = "./gif1/";} elsif ($gif eq "gif2") { $gifdir = "./gif2/";} elsif ($gif eq "gifx") { $gifdir = "./gifx/";} elsif ($gif eq "1") { $gifdir = "./gif1/";} elsif ($gif eq "2") { $gifdir = "./gif2/";} elsif ($gif eq "x") { $gifdir = "./gifx/";} else {$gifdir = "/home/homepage/public_html/cntgif/$gif/";} # gif画像のあるディレクトリのパス # 指定がない場合は6桁とする if ($keta eq "") { $keta ="6";} # 指定がない場合はindexとする if ($id eq "") { $id ="index";} &get_time; ($nen100,$gatu100,$niti100) = split(/-/,$todaydate); # カウンター記録ファイルを定義 $datfile = "$userdir\/$id\.$nen100$gatu100"; $filename = $datfile;&fcheck; $logfile = "$userdir\/$id\.log"; $filename = $logfile;&fcheck; $alogfile = "$userdir\/analyze\.log"; $filename = $alogfile;&fcheck; $filename = $datfile;&fcheck; $dayfile = "$userdir\/$id\.day"; $filename = $dayfile;&fcheck; $monfile = "$userdir\/$id\.mon"; $filename = $monfile;&fcheck; $lockfile = "$lockdir\/$id\.lock"; # IPアドレスを取得 $addr = $ENV{'REMOTE_ADDR'}; # ロック開始 if ($lock_check == 1) { &file_lock; } # 記録ファイルから読み込み open(IN,"$logfile") || &error(1); $data = ; close(IN); # 記録ファイルを分解 ($count,$today,$yes,$monc,$add_check,$yobi2,$todaydate,$ip,$newpass,$home,$accmax,$daymax,$monmax) = split(/<>/, $data); ($years,$month,$day) = split(/\//, $todaydate); if ($accmax eq "") {$accmax="100";} if ($daymax eq "") {$daymax="100";} if ($monmax eq "") {$monmax="36";} # ログにIPがある場合は二重アクセスをチェック $flag_add=0; if ($ip) { $flag_add = 1;} if ($add_check == 1) { if ($flag_add && $ip eq "$addr") { $flag_add = 2; } } if ($flag_add != 2) { # カウントアップ $count++; $dflag = 2; if ($month < 1) { $month = $mon; } if ($day < 1) { $day = $mday; } # 取得した日月とデータファイルの日月を比較(同じ月度内) if ($month == $mon) { $monc++; #当月カウントアップ if ($day == $mday) { $dflag = 0; $today++; } else { $day1 = $day; $day1++; if ($day1 = $mday) { $dflag = 1; } #翌日の場合 else { $dflag = 2; } #同じ月度でも2日以上経過している場合 } } # 取得した日月とデータファイルの日月を比較(異なる月度) else{ &moncount1; #月次集計データ更新 $mon1 = $month; $mon1++; if ($mon1== $mon) { if ($mday == 1) { if ($month == 2) { if ($day == 28) { $dflag = 1; }#翌日の場合 } if ($month == 4 || $month == 6 || $month == 9 || $month == 11) { if ($day == 30) { $dflag = 1; }#翌日の場合 } if ($month == 1 || $month == 3 || $month == 5 || $month == 7 || $month == 8 || $month == 10 || $month == 12) { if ($day == 31) { $dflag = 1; }#翌日の場合 } } } } if ($dflag == 1 ) { &newcount1 } #翌日処理へ(本日を昨日へ代入・本日を1から始める) if ($dflag == 2 ) { &newcount2 } #2日以上経過処理へ(昨日に0を代入・本日を1から始める) # 記録ファイルを更新する open(OUT,">$logfile") || &error(3); print OUT "$count<>$today<>$yes<>$monc<>$add_check<>$yobi2<>$date1<>$addr<>$newpass<>$home<>$accmax<>$daymax<>$monmax<>"; close(OUT); # アクセスログを取得 if (-e $datfile) { open(DAT,"$datfile") || &error(5); @lines = ; close(DAT); # 記録ログの配列を整える # IPアドレス取得 if ($refurl) {$referer = $refurl;} else {$referer = $ENV{'HTTP_REFERER'};} $agent = $ENV{'HTTP_USER_AGENT'}; $host = $ENV{'REMOTE_HOST'}; $addr = $ENV{'REMOTE_ADDR'}; if ($host eq "" || $host eq "$addr") { $host = gethostbyaddr(pack("C4", split(/\./, $addr)), 2); } if ($host eq "") { $host = $addr; } $line = "$count<>$date<>$agent<>$referer<>$addr<>$host<>\n"; # 最大ログ数処理 if (@lines > $accmax) { until(@lines < $accmax) { shift(@lines); } push(@lines,$line); open(DAT,">$datfile") || &error(6); print DAT @lines; close(DAT); } else { open(DAT,">>$datfile") || &error(7); print DAT $line; close(DAT); } } } # ロック解除 if (-e $lockfile) { unlink($lockfile); } # GIF画像を出力 &count_view; exit; #************************************** # sub #************************************** # GIF画像連結出力処理 #************************************** sub count_view { while (length($count) < $keta) { $count = '0' . $count; } $length = length($count); @GIF=(); foreach (0 .. $length-1) { $n = substr($count,$_,1); push(@GIF,"$gifdir$n\.gif"); } print "Content-type: image/gif\n\n"; binmode(STDOUT); print &gifcat'gifcat(@GIF); } #************************************** # ロックファイル : symlink関数 #************************************** sub file_lock { # 既にロックファイルが存在(新規に作成できない)している場合(処理中)は1秒待つ(sleep). # これを3回まで繰り返す local($retry) = 3; while (!symlink(".", $lockfile)) { if (--$retry <= 0) { &error(4); } sleep(1); } } #************************************** # エラー処理 #************************************** sub error { # ロック中にエラーが生じた場合は、作成したロックファイルを削除をして # エラーメッセージを返し、処理を終了する. $err = $_[0]; if (-e $lockfile) { unlink($lockfile); } print "ERROR($err)"; exit; } #************************************** # 日時の取得 #************************************** sub get_time { $ENV{'TZ'} = "JST-9"; ($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime(time); @week = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat'); # 日時のフォーマット $todaydate = sprintf("%02d-%02d-%02d", $year-100,$mon+1,$mday); $date = sprintf("%04d/%02d/%02d(%s) %02d:%02d:%02d", $year+1900,$mon+1,$mday,$week[$wday],$hour,$min,$sec); $date1 = sprintf("%04d/%02d/%02d(%s) ", $year+1900,$mon+1,$mday,$week[$wday]); $years = sprintf("%04d",$year+1900); $mon++; } #************************************** # 翌日処理 #************************************** # 本日($today)を昨日($yes)に置き換えて本日($today)の新カウント開始 sub newcount1{ # 記録ファイルから読み込み if (-e $dayfile) { open(DAT,"$dayfile") || &error(5); @lines = ; close(DAT); } $line = "$today<>$todaydate<>\n"; # 最大ログ数処理 if (@lines > $daymax) { until(@lines < $daymax) { shift(@lines); } push(@lines,$line); open(DAT,">$dayfile") || &error(6); print DAT @lines; close(DAT); } else { open(DAT,">>$dayfile") || &error(7); print DAT $line; close(DAT); } $yes = $today; $today = 1; } #************************************** # 最後のカウントから2日以上経過処理 #************************************** # 昨日($yes)を0に置き換えて本日($today)の新カウント開始 sub newcount2{ # 日次記録ファイルから読み込み if (-e $dayfile) { open(DAT,"$dayfile") || &error(5); @lines = ; close(DAT); } $line = "$today<>$todaydate<>\n"; # 最大ログ数処理 if (@lines > $daymax) { until(@lines < $daymax) { shift(@lines); } push(@lines,$line); open(DAT,">$dayfile") || &error(6); print DAT @lines; close(DAT); } else { open(DAT,">>$dayfile") || &error(7); print DAT $line; close(DAT); } $yes = 0; $today = 1; } #************************************** # 月次集計ファイル更新処理 #************************************** # カウント記録ログ内の当月カウント(monc)を月次集計ファイルに記録して # 当月カウント(monc)を1から始める sub moncount1{ # 月次集計ファイルから読み込み if (-e $monfile) { open(DAT,"$monfile") || &error(5); @lines = ; close(DAT); } # ログの配列を整える $line = "$monc<>$month<>$years<>\n"; # 最大ログ数処理 if (@lines > $monmax) { until(@lines < $monmax) { shift(@lines); } push(@lines,$line); open(DAT,">$monfile") || &error(6); print DAT @lines; close(DAT); } else { open(DAT,">>$monfile") || &error(7); print DAT $line; close(DAT); } $monc = 1; } #************************************** # 記録ファイルの存在をチェック #************************************** sub fcheck{ unless(-e $filename) { open(OUT,">$filename") || &error(2); print OUT "0"; close(OUT); chmod (0666,"$filename"); } }