home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 2003 August
/
VPR0308.ISO
/
OLS
/
DCP0002
/
dcp0002.lzh
/
dcp0002
/
dcp.cgi
next >
Wrap
Text File
|
2003-05-25
|
13KB
|
391 lines
#!/usr/bin/perl
# +---------+
# doCNT+ カウンタCGI | dcp.cgi |
#--------------------------------------------------------------------+---------+
# 20030526
#-------------------------------------------------------------------------------
#---------------------------------------------------------------- 初期環境設定▼
$| = 1;
require './dcpi.pl';
&ini;
# 画像連結ルーチン読み込み
require './gifcat.pl';
#-------------------------------------------------------------------------------
#------------------------------------------------------------- メインルーチン ▼
# 引数の受取り
%COM = &get_query;
# SSIモードの判定
if($COM{'md'} ne 's' && $ENV{'SERVER_PROTOCOL'} eq 'INCLUDED'){
$COM{'md'} = 's';
}
# 昨日・今日のカウント出力処理
if($COM{'count'} eq 'today'){
&put_today_count;
exit;
}elsif($COM{'count'} eq 'yesterday'){
&put_yesterday_count;
exit;
}
# IP or リモートホストによるカウントアップ拒否処理
if($INI{'deny_ip'} ne ''){
@lines = split(/<>/,$INI{'deny_ip'});
foreach (@lines){
if($ENV{'REMOTE_ADDR'} =~ /\Q$_\E/ || $ENV{'REMOTE_HOST'} =~ /\Q$_\E/){
&put_total_count;
exit;
}
}
}
# アクセスタイム取得
$INI{'time'} = time;
# クッキーによるカウントアップ防止処理
if($COM{'md'} ne 's' && $INI{'wcnt_cookie'} == 1){
%COOKIE = &get_cookie($INI{'cookie_name'});
$i = $INI{'time'} - $COOKIE{'stay'};
if($i < $INI{'no_cnt_interval'}){
&put_total_count;
exit;
}
}
# ログファイルによる2重カウントアップ防止処理
if($INI{'wcnt'} == 1){
# ログファイル読み込み
open(RLOG,"<$INI{'wcnt_logfile_path'}") || &error('2重カウント防止ログファイルの読み込みオープンに失敗しました。');
@wcnt_logs = <RLOG>;
close(RLOG);
# ログに一致したらカウンタ出力して終了
foreach (@wcnt_logs){
chomp($_);
my($time,$ip,$ua) = split(/\,/,$_);
$i = $INI{'time'} - $time;
if($i < $INI{'no_cnt_interval'} && $ip eq $ENV{'REMOTE_ADDR'} && $ua eq $ENV{'HTTP_USER_AGENT'}){
&put_total_count;
exit;
}
}
# ファイルロック処理
$INI{'lfh'} = &filelock($INI{'fld'},$INI{'lf'},$INI{'lto'},$INI{'lrt'}) || &error('ファイルロック処理がタイムアウトしました。');
open(WLOG,">$INI{'wcnt_logfile_path'}$INI{'time'}") || &error('2重カウント防止ログファイルの読み込みオープンに失敗しました。');
print WLOG "$INI{'time'}\,$ENV{'REMOTE_ADDR'}\,$ENV{'HTTP_USER_AGENT'}\n";
$i = 1;
foreach (@wcnt_logs){
if($i >= $INI{'wcnt_log'}){
last;
}
print WLOG "$_\n";
$i++;
}
close(WLOG);
unlink ($INI{'wcnt_logfile_path'}) || &error("2重カウント防止ログファイルの一時ファイルを削除できませんでした。");
rename("$INI{'wcnt_logfile_path'}$INI{'time'}","$INI{'wcnt_logfile_path'}") || &error("2重カウント防止ログファイルの一時ファイルをリネームできませんでした。");
# ファイルロック解除処理
&unlock($INI{'lfh'});
}
# ファイルロック処理
$INI{'lfh'} = &filelock($INI{'fld'},$INI{'lf'},$INI{'lto'},$INI{'lrt'}) || &error('ファイルロック処理がタイムアウトしました。');
# 現トータルカウント取得
open(RCNT,"<$INI{'total_cnt_file_path'}") || &error('トータルカウントファイルの読み込みオープンに失敗しました。');
$count_total = <RCNT>;
close(RCNT);
# 最終カウント時刻ファイル更新
open(RLCD,"<$INI{'lastcntdate_file_path'}") || &error('最終カウント時刻ファイルの読み込みオープンに失敗しました。');
$lastcntdate = <RLCD>;
close(RLCD);
# 本日カウント取得
open(RTD,"<$INI{'today_cnt_file_path'}") || &error('本日カウントファイルの読み込みオープンに失敗しました。');
$today_cnt = <RTD>;
close(RTD);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($INI{'time'});
if($lastcntdate ne "$year$mon$mday"){
# 本日・昨日カウンタ更新
open(WYD,">$INI{'yesterday_cnt_file_path'}") || &error('昨日カウントファイルの書き込みオープンに失敗しました。');
print WYD $today_cnt;
close(WYD);
open(WTD,">$INI{'today_cnt_file_path'}") || &error('本日カウントファイルの書き込みオープンに失敗しました。');
print WTD "1";
close(WTD);
open(WLCD,">$INI{'lastcntdate_file_path'}") || &error('最終カウント時刻ファイルの書き込みオープンに失敗しました。');
print WLCD "$year$mon$mday";
close(WLCD);
}else{
# 本日カウントアップ処理
$today_cnt++;
open(WTD,">$INI{'today_cnt_file_path'}") || &error('本日カウントファイルの書き込みオープンに失敗しました。');
print WTD $today_cnt;
close(WTD);
}
# トータルカウントアップ処理
$count_total++;
open(WCNT,">$INI{'total_cnt_file_path'}") || &error('トータルカウントファイルの書き込みオープンに失敗しました。');
print WCNT $count_total;
close(WCNT);
# ファイルロック解除処理
&unlock($INI{'lfh'});
# クッキー書き込み
if($COM{'md'} ne 's'){
&put_cookie($INI{'cookie_name'},$INI{'cookie_on_hours'},"stay:$INI{'time'}",$INI{'cookie_path'});
}
# トータルカウント出力
&put_total_count;
exit;
#-------------------------------------------------------------------------------
#-------------------------------------------------------- トータルカウンタ出力▼
sub put_total_count {
my($count_total);
my(@INI_put_count,@INI_put_image);
open(RCNT,"<$INI{'total_cnt_file_path'}") || &error('今日のログファイルの読み込みオープンに失敗しました。');
$count_total = <RCNT>;
close(RCNT);
if($COM{'md'} eq 's'){
print "Content-type: text/html;\n\n";
if($INI{'cnt_total_keta'} > 0){ $COM{'k'} = $INI{'cnt_total_keta'}; }
$count_total = "0" x ($COM{'k'} - (length("$count_total"))) . "$count_total";
if($COM{'p'} ne 't'){
@INI_put_count = split(//,$count_total);
foreach (@INI_put_count){
print "<img src=\"$INI{'cnt_images_url_path'}$_\.gif\" border=0 alt=\"$_\">";
}
}else{
print "$count_total";
}
}else{
print "Content-type: image/gif\n\n";
if($INI{'cnt_total_keta'} > 0){ $COM{'k'} = $INI{'cnt_total_keta'}; }
$count_total = "0" x ($COM{'k'} - (length("$count_total"))) . "$count_total";
@INI_put_count = split(//,$count_total);
$INI{'i'} = 0;
foreach (@INI_put_count){
$INI_put_image[$INI{'i'}] = "$INI{'cnt_images_path'}$_\.gif";
$INI{'i'}++;
}
undef(@INI_put_count);
# 連結画像を出力
binmode(STDOUT);
print &gifcat'gifcat(@INI_put_image);
}
return;
} # close sub count_total
#-------------------------------------------------------------------------------
#-------------------------------------------------------- 今日のアクセス数出力▼
sub put_today_count {
my($count_today);
my(@INI_put_count,@INI_put_image);
open(RCNT,"<$INI{'today_cnt_file_path'}") || &error('今日のログファイルの読み込みオープンに失敗しました。');
$count_today = <RCNT>;
close(RCNT);
if($COM{'md'} eq 's'){
print "Content-type: text/html;\n\n";
if($INI{'cnt_tyday_keta'} > 0){ $COM{'k'} = $INI{'cnt_tyday_keta'}; }
$count_today = "0" x ($COM{'k'} - (length("$count_today"))) . "$count_today";
if($COM{'p'} ne 't'){
@INI_put_count = split(//,$count_today);
foreach (@INI_put_count){
print "<img src=\"$INI{'cnt_images_url_path'}$_\.gif\" border=0 alt=\"$_\">";
}
}else{
print "$count_today";
}
}else{
print "Content-type: image/gif\n\n";
if($INI{'cnt_tyday_keta'} > 0){ $COM{'k'} = $INI{'cnt_tyday_keta'}; }
$count_today = "0" x ($COM{'k'} - (length("$count_today"))) . "$count_today";
@INI_put_count = split(//,$count_today);
$INI{'i'} = 0;
foreach (@INI_put_count){
$INI_put_image[$INI{'i'}] = "$INI{'cnt_images_path'}$_\.gif";
$INI{'i'}++;
}
undef(@INI_put_count);
# 連結画像を出力
binmode(STDOUT);
print &gifcat'gifcat(@INI_put_image);
}
return;
} # close sub count_today
#-------------------------------------------------------------------------------
#-------------------------------------------------------- 昨日のアクセス数出力▼
sub put_yesterday_count {
my($count_yesterday);
my(@INI_put_count,@INI_put_image);
open(RCNT,"<$INI{'yesterday_cnt_file_path'}") || &error('昨日のログファイルの読み込みオープンに失敗しました。');
$count_yesterday = <RCNT>;
close(RCNT);
if($COM{'md'} eq 's'){
print "Content-type: text/html;\n\n";
if($INI{'cnt_tyday_keta'} > 0){ $COM{'k'} = $INI{'cnt_tyday_keta'}; }
$count_yesterday = "0" x ($COM{'k'} - (length("$count_yesterday"))) . "$count_yesterday";
if($COM{'p'} ne 't'){
@INI_put_count = split(//,$count_yesterday);
foreach (@INI_put_count){
print "<img src=\"$INI{'cnt_images_url_path'}$_\.gif\" border=0 alt=\"$_\">";
}
}else{
print "$count_yesterday";
}
}else{
print "Content-type: image/gif\n\n";
if($INI{'cnt_tyday_keta'} > 0){ $COM{'k'} = $INI{'cnt_tyday_keta'}; }
$count_yesterday = "0" x ($COM{'k'} - (length("$count_yesterday"))) . "$count_yesterday";
@INI_put_count = split(//,$count_yesterday);
$INI{'i'} = 0;
foreach (@INI_put_count){
$INI_put_image[$INI{'i'}] = "$INI{'cnt_images_path'}$_\.gif";
$INI{'i'}++;
}
undef(@INI_put_count);
# 連結画像を出力
binmode(STDOUT);
print &gifcat'gifcat(@INI_put_image);
}
return;
} # close sub count_yesterday
#-------------------------------------------------------------------------------
#--------------------------------------------------------------- クエリー取得 ▼
sub get_query {
my($buffer,@pairs,$pair,%COM,$name,$value);
$buffer = $ENV{'QUERY_STRING'};
@pairs = split(/&/,$buffer);
foreach $pair (@pairs){
$pair =~ /^([a-zA-Z0-9]+)=(.*)$/;
$name = $1;
$value = $2;
$COM{$name} = $value;
}
return %COM;
} # close sub get_query
#-------------------------------------------------------------------------------
#---------------------------------------------------------------- クッキー取得▼
sub get_cookie {
my($cookie_name) = @_;
my($http_cookie,@pairs,$pair,$name,$value,%cookie_data,%COOKIE);
$http_cookie = $ENV{'HTTP_COOKIE'};
@pairs = split(/;/, $http_cookie);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$name =~ s/ //g;
$COOKIE_DATA{$name} = $value;
}
@pairs = split(/\,/, $COOKIE_DATA{$cookie_name});
foreach $pair (@pairs) {
($name, $value) = split(/:/, $pair);
$COOKIE{$name} = $value;
}
return %COOKIE;
} # close sub cookie_get
#-------------------------------------------------------------------------------
#---------------------------------------------------------------- クッキー送出▼
sub put_cookie {
my($cookie_name,$cookie_on_hours,$cookie_data,$cookie_path) = @_;
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst,$month,$youbi,$date_gmt);
$ENV{'TZ'} = "GMT";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time + $cookie_on_hours * 3600); # 3600 = 60 * 60
$month = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')[$mon];
$youbi = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')[$wday];
$date_gmt = sprintf("%s\, %02d\-%s\-%04d %02d:%02d:%02d GMT",$youbi,$mday,$month,$year + 1900,$hour,$min,$sec);
print "Set-Cookie: $cookie_name=$cookie_data; expires=$date_gmt path=$cookie_path\n";
return 1;
} # close sub put_cookie
#-------------------------------------------------------------------------------
#---------------------------------------------------------- ファイルロック処理▼
sub filelock {
my($fld,$lf,$lto,$lrt) = @_;
my %lfh = ('dir' => "$fld", 'basename' => "$lf", 'timeout' => "$lto", 'trytime' => "$lrt");
my($i,@filelists);
$lfh{'path'} = $lfh{'dir'} . $lfh{'basename'};
for ($i = 0; $i < $lfh{'trytime'}; $i++, sleep 1) {
return \%lfh if(rename($lfh{'path'}, $lfh{'current'} = $lfh{'path'} . time));
}
opendir(LOCKDIR, $lfh{'dir'});
@filelists = readdir(LOCKDIR);
closedir(LOCKDIR);
foreach (@filelists) {
if(/^$lfh{'basename'}(\d+)/) {
return \%lfh if(time - $1 > $lfh{'timeout'} and rename($lfh{'dir'} . $_, $lfh{'current'} = $lfh{'path'} . time));
last;
}
}
undef;
}
# ファイルロック解除
sub unlock {
rename($_[0]->{'current'}, $_[0]->{'path'});
}
# 利用規定は http://www.bayashi.net/st/ds_rule.html
#-------------------------------------------------------------------------------
# Copyright (c) 2001-CurrentYear bayashi.net. All rights reserved.
# http://www.bayashi.net/st/
# ok@bayashi.net