home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2003 August / VPR0308.ISO / OLS / DCP0002 / dcp0002.lzh / dcp0002 / dcp.cgi next >
Text File  |  2003-05-25  |  13KB  |  391 lines

  1. #!/usr/bin/perl
  2. #                                                                    +---------+
  3. #  doCNT+ カウンタCGI                                                | dcp.cgi |
  4. #--------------------------------------------------------------------+---------+
  5. # 20030526
  6.  
  7.  
  8. #-------------------------------------------------------------------------------
  9. #---------------------------------------------------------------- 初期環境設定▼
  10. $| = 1;
  11. require './dcpi.pl';
  12. &ini;
  13.  
  14. # 画像連結ルーチン読み込み
  15. require './gifcat.pl';
  16.  
  17. #-------------------------------------------------------------------------------
  18. #------------------------------------------------------------- メインルーチン ▼
  19. # 引数の受取り
  20. %COM = &get_query;
  21.  
  22. # SSIモードの判定
  23. if($COM{'md'} ne 's' && $ENV{'SERVER_PROTOCOL'} eq 'INCLUDED'){
  24.     $COM{'md'} = 's';
  25. }
  26.  
  27. # 昨日・今日のカウント出力処理
  28. if($COM{'count'} eq 'today'){
  29.     &put_today_count;
  30.     exit;
  31. }elsif($COM{'count'} eq 'yesterday'){
  32.     &put_yesterday_count;
  33.     exit;
  34. }
  35.  
  36. # IP or リモートホストによるカウントアップ拒否処理
  37. if($INI{'deny_ip'} ne ''){
  38.     @lines = split(/<>/,$INI{'deny_ip'});
  39.     foreach (@lines){
  40.         if($ENV{'REMOTE_ADDR'} =~ /\Q$_\E/ || $ENV{'REMOTE_HOST'} =~ /\Q$_\E/){
  41.             &put_total_count;
  42.             exit;
  43.         }
  44.     }
  45. }
  46.  
  47. # アクセスタイム取得
  48. $INI{'time'} = time;
  49.  
  50. # クッキーによるカウントアップ防止処理
  51. if($COM{'md'} ne 's' && $INI{'wcnt_cookie'} == 1){
  52.     %COOKIE = &get_cookie($INI{'cookie_name'});
  53.     $i = $INI{'time'} - $COOKIE{'stay'};
  54.     if($i < $INI{'no_cnt_interval'}){
  55.         &put_total_count;
  56.         exit;
  57.     }
  58. }
  59.  
  60. # ログファイルによる2重カウントアップ防止処理
  61. if($INI{'wcnt'} == 1){
  62.     # ログファイル読み込み
  63.     open(RLOG,"<$INI{'wcnt_logfile_path'}") || &error('2重カウント防止ログファイルの読み込みオープンに失敗しました。');
  64.     @wcnt_logs = <RLOG>;
  65.     close(RLOG);
  66.     # ログに一致したらカウンタ出力して終了
  67.     foreach (@wcnt_logs){
  68.         chomp($_);
  69.         my($time,$ip,$ua) = split(/\,/,$_);
  70.         $i = $INI{'time'} - $time;
  71.         if($i < $INI{'no_cnt_interval'} && $ip eq $ENV{'REMOTE_ADDR'} && $ua eq $ENV{'HTTP_USER_AGENT'}){
  72.             &put_total_count;
  73.             exit;
  74.         }
  75.     }
  76.     # ファイルロック処理
  77.     $INI{'lfh'} = &filelock($INI{'fld'},$INI{'lf'},$INI{'lto'},$INI{'lrt'}) || &error('ファイルロック処理がタイムアウトしました。');
  78.     open(WLOG,">$INI{'wcnt_logfile_path'}$INI{'time'}") || &error('2重カウント防止ログファイルの読み込みオープンに失敗しました。');
  79.     print WLOG "$INI{'time'}\,$ENV{'REMOTE_ADDR'}\,$ENV{'HTTP_USER_AGENT'}\n";
  80.     $i = 1;
  81.     foreach (@wcnt_logs){
  82.         if($i >= $INI{'wcnt_log'}){
  83.             last;
  84.         }
  85.         print WLOG "$_\n";
  86.         $i++;
  87.     }
  88.     close(WLOG);
  89.     unlink ($INI{'wcnt_logfile_path'}) || &error("2重カウント防止ログファイルの一時ファイルを削除できませんでした。");
  90.     rename("$INI{'wcnt_logfile_path'}$INI{'time'}","$INI{'wcnt_logfile_path'}") || &error("2重カウント防止ログファイルの一時ファイルをリネームできませんでした。");
  91.     # ファイルロック解除処理
  92.     &unlock($INI{'lfh'});
  93. }
  94.  
  95. # ファイルロック処理
  96. $INI{'lfh'} = &filelock($INI{'fld'},$INI{'lf'},$INI{'lto'},$INI{'lrt'}) || &error('ファイルロック処理がタイムアウトしました。');
  97. # 現トータルカウント取得
  98. open(RCNT,"<$INI{'total_cnt_file_path'}") || &error('トータルカウントファイルの読み込みオープンに失敗しました。');
  99. $count_total = <RCNT>;
  100. close(RCNT);
  101. # 最終カウント時刻ファイル更新
  102. open(RLCD,"<$INI{'lastcntdate_file_path'}") || &error('最終カウント時刻ファイルの読み込みオープンに失敗しました。');
  103. $lastcntdate = <RLCD>;
  104. close(RLCD);
  105. # 本日カウント取得
  106. open(RTD,"<$INI{'today_cnt_file_path'}") || &error('本日カウントファイルの読み込みオープンに失敗しました。');
  107. $today_cnt = <RTD>;
  108. close(RTD);
  109. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($INI{'time'});
  110. if($lastcntdate ne "$year$mon$mday"){
  111.     # 本日・昨日カウンタ更新
  112.     open(WYD,">$INI{'yesterday_cnt_file_path'}") || &error('昨日カウントファイルの書き込みオープンに失敗しました。');
  113.     print WYD $today_cnt;
  114.     close(WYD);
  115.     open(WTD,">$INI{'today_cnt_file_path'}") || &error('本日カウントファイルの書き込みオープンに失敗しました。');
  116.     print WTD "1";
  117.     close(WTD);
  118.     open(WLCD,">$INI{'lastcntdate_file_path'}") || &error('最終カウント時刻ファイルの書き込みオープンに失敗しました。');
  119.     print WLCD "$year$mon$mday";
  120.     close(WLCD);
  121. }else{
  122.     # 本日カウントアップ処理
  123.     $today_cnt++;
  124.     open(WTD,">$INI{'today_cnt_file_path'}") || &error('本日カウントファイルの書き込みオープンに失敗しました。');
  125.     print WTD $today_cnt;
  126.     close(WTD);
  127. }
  128. # トータルカウントアップ処理
  129. $count_total++;
  130. open(WCNT,">$INI{'total_cnt_file_path'}") || &error('トータルカウントファイルの書き込みオープンに失敗しました。');
  131. print WCNT $count_total;
  132. close(WCNT);
  133. # ファイルロック解除処理
  134. &unlock($INI{'lfh'});
  135.  
  136. # クッキー書き込み
  137. if($COM{'md'} ne 's'){
  138.     &put_cookie($INI{'cookie_name'},$INI{'cookie_on_hours'},"stay:$INI{'time'}",$INI{'cookie_path'});
  139. }
  140.  
  141. # トータルカウント出力
  142. &put_total_count;
  143.  
  144. exit;
  145.  
  146.  
  147.  
  148.  
  149.  
  150. #-------------------------------------------------------------------------------
  151. #-------------------------------------------------------- トータルカウンタ出力▼
  152. sub put_total_count {
  153.  
  154.     my($count_total);
  155.     my(@INI_put_count,@INI_put_image);
  156.  
  157.     open(RCNT,"<$INI{'total_cnt_file_path'}") || &error('今日のログファイルの読み込みオープンに失敗しました。');
  158.     $count_total = <RCNT>;
  159.     close(RCNT);
  160.  
  161.     if($COM{'md'} eq 's'){
  162.         print "Content-type: text/html;\n\n";
  163.         if($INI{'cnt_total_keta'} > 0){ $COM{'k'} = $INI{'cnt_total_keta'}; }
  164.         $count_total = "0" x ($COM{'k'} - (length("$count_total"))) . "$count_total";
  165.         if($COM{'p'} ne 't'){
  166.             @INI_put_count = split(//,$count_total);
  167.             foreach (@INI_put_count){
  168.                 print "<img src=\"$INI{'cnt_images_url_path'}$_\.gif\" border=0 alt=\"$_\">";
  169.             }
  170.         }else{
  171.             print "$count_total";
  172.         }
  173.     }else{
  174.         print "Content-type: image/gif\n\n";
  175.         if($INI{'cnt_total_keta'} > 0){ $COM{'k'} = $INI{'cnt_total_keta'}; }
  176.         $count_total = "0" x ($COM{'k'} - (length("$count_total"))) . "$count_total";
  177.         @INI_put_count = split(//,$count_total);
  178.         $INI{'i'} = 0;
  179.         foreach (@INI_put_count){
  180.             $INI_put_image[$INI{'i'}] = "$INI{'cnt_images_path'}$_\.gif";
  181.             $INI{'i'}++;
  182.         }
  183.         undef(@INI_put_count);
  184.         # 連結画像を出力
  185.         binmode(STDOUT);
  186.         print &gifcat'gifcat(@INI_put_image);
  187.     }
  188.  
  189.     return;
  190.  
  191. } # close sub count_total
  192.  
  193.  
  194. #-------------------------------------------------------------------------------
  195. #-------------------------------------------------------- 今日のアクセス数出力▼
  196. sub put_today_count {
  197.  
  198.     my($count_today);
  199.     my(@INI_put_count,@INI_put_image);
  200.  
  201.     open(RCNT,"<$INI{'today_cnt_file_path'}") || &error('今日のログファイルの読み込みオープンに失敗しました。');
  202.     $count_today = <RCNT>;
  203.     close(RCNT);
  204.  
  205.     if($COM{'md'} eq 's'){
  206.         print "Content-type: text/html;\n\n";
  207.         if($INI{'cnt_tyday_keta'} > 0){ $COM{'k'} = $INI{'cnt_tyday_keta'}; }
  208.         $count_today = "0" x ($COM{'k'} - (length("$count_today"))) . "$count_today";
  209.         if($COM{'p'} ne 't'){
  210.             @INI_put_count = split(//,$count_today);
  211.             foreach (@INI_put_count){
  212.                 print "<img src=\"$INI{'cnt_images_url_path'}$_\.gif\" border=0 alt=\"$_\">";
  213.             }
  214.         }else{
  215.             print "$count_today";
  216.         }
  217.     }else{
  218.         print "Content-type: image/gif\n\n";
  219.         if($INI{'cnt_tyday_keta'} > 0){ $COM{'k'} = $INI{'cnt_tyday_keta'}; }
  220.         $count_today = "0" x ($COM{'k'} - (length("$count_today"))) . "$count_today";
  221.         @INI_put_count = split(//,$count_today);
  222.         $INI{'i'} = 0;
  223.         foreach (@INI_put_count){
  224.             $INI_put_image[$INI{'i'}] = "$INI{'cnt_images_path'}$_\.gif";
  225.             $INI{'i'}++;
  226.         }
  227.         undef(@INI_put_count);
  228.         # 連結画像を出力
  229.         binmode(STDOUT);
  230.         print &gifcat'gifcat(@INI_put_image);
  231.     }
  232.  
  233.     return;
  234.  
  235. } # close sub count_today
  236.  
  237.  
  238. #-------------------------------------------------------------------------------
  239. #-------------------------------------------------------- 昨日のアクセス数出力▼
  240. sub put_yesterday_count {
  241.  
  242.     my($count_yesterday);
  243.     my(@INI_put_count,@INI_put_image);
  244.  
  245.     open(RCNT,"<$INI{'yesterday_cnt_file_path'}") || &error('昨日のログファイルの読み込みオープンに失敗しました。');
  246.     $count_yesterday = <RCNT>;
  247.     close(RCNT);
  248.  
  249.     if($COM{'md'} eq 's'){
  250.         print "Content-type: text/html;\n\n";
  251.         if($INI{'cnt_tyday_keta'} > 0){ $COM{'k'} = $INI{'cnt_tyday_keta'}; }
  252.         $count_yesterday = "0" x ($COM{'k'} - (length("$count_yesterday"))) . "$count_yesterday";
  253.         if($COM{'p'} ne 't'){
  254.             @INI_put_count = split(//,$count_yesterday);
  255.             foreach (@INI_put_count){
  256.                 print "<img src=\"$INI{'cnt_images_url_path'}$_\.gif\" border=0 alt=\"$_\">";
  257.             }
  258.         }else{
  259.             print "$count_yesterday";
  260.         }
  261.     }else{
  262.         print "Content-type: image/gif\n\n";
  263.         if($INI{'cnt_tyday_keta'} > 0){ $COM{'k'} = $INI{'cnt_tyday_keta'}; }
  264.         $count_yesterday = "0" x ($COM{'k'} - (length("$count_yesterday"))) . "$count_yesterday";
  265.         @INI_put_count = split(//,$count_yesterday);
  266.         $INI{'i'} = 0;
  267.         foreach (@INI_put_count){
  268.             $INI_put_image[$INI{'i'}] = "$INI{'cnt_images_path'}$_\.gif";
  269.             $INI{'i'}++;
  270.         }
  271.         undef(@INI_put_count);
  272.         # 連結画像を出力
  273.         binmode(STDOUT);
  274.         print &gifcat'gifcat(@INI_put_image);
  275.     }
  276.  
  277.     return;
  278.  
  279. } # close sub count_yesterday
  280.  
  281.  
  282. #-------------------------------------------------------------------------------
  283. #--------------------------------------------------------------- クエリー取得 ▼
  284. sub get_query {
  285.  
  286.     my($buffer,@pairs,$pair,%COM,$name,$value);
  287.  
  288.     $buffer = $ENV{'QUERY_STRING'};
  289.     @pairs = split(/&/,$buffer);
  290.     foreach $pair (@pairs){
  291.         $pair =~ /^([a-zA-Z0-9]+)=(.*)$/;
  292.         $name = $1;
  293.         $value = $2;
  294.         $COM{$name} = $value;
  295.     }
  296.  
  297.     return %COM;
  298.  
  299. } # close sub get_query
  300.  
  301.  
  302. #-------------------------------------------------------------------------------
  303. #---------------------------------------------------------------- クッキー取得▼
  304. sub get_cookie {
  305.  
  306.     my($cookie_name) = @_;
  307.     my($http_cookie,@pairs,$pair,$name,$value,%cookie_data,%COOKIE);
  308.  
  309.     $http_cookie = $ENV{'HTTP_COOKIE'};
  310.  
  311.     @pairs = split(/;/, $http_cookie);
  312.     foreach $pair (@pairs) {
  313.         ($name, $value) = split(/=/, $pair);
  314.         $name =~ s/ //g;
  315.         $COOKIE_DATA{$name} = $value;
  316.     }
  317.  
  318.     @pairs = split(/\,/, $COOKIE_DATA{$cookie_name});
  319.     foreach $pair (@pairs) {
  320.         ($name, $value) = split(/:/, $pair);
  321.         $COOKIE{$name} = $value;
  322.     }
  323.  
  324.     return %COOKIE;
  325.  
  326. } # close sub cookie_get
  327.  
  328.  
  329. #-------------------------------------------------------------------------------
  330. #---------------------------------------------------------------- クッキー送出▼
  331. sub put_cookie {
  332.  
  333.     my($cookie_name,$cookie_on_hours,$cookie_data,$cookie_path) = @_;
  334.     my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst,$month,$youbi,$date_gmt);
  335.  
  336.     $ENV{'TZ'} = "GMT";
  337.     ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time + $cookie_on_hours * 3600); # 3600 = 60 * 60
  338.     $month = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')[$mon];
  339.     $youbi = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')[$wday];
  340.     $date_gmt = sprintf("%s\, %02d\-%s\-%04d %02d:%02d:%02d GMT",$youbi,$mday,$month,$year + 1900,$hour,$min,$sec);
  341.  
  342.     print "Set-Cookie: $cookie_name=$cookie_data; expires=$date_gmt path=$cookie_path\n";
  343.  
  344.     return 1;
  345.  
  346. } # close sub put_cookie
  347.  
  348.  
  349. #-------------------------------------------------------------------------------
  350. #---------------------------------------------------------- ファイルロック処理▼
  351. sub filelock {
  352.  
  353.     my($fld,$lf,$lto,$lrt) = @_;
  354.     my %lfh = ('dir' => "$fld", 'basename' => "$lf", 'timeout' => "$lto", 'trytime' => "$lrt");
  355.     my($i,@filelists);
  356.  
  357.     $lfh{'path'} = $lfh{'dir'} . $lfh{'basename'};
  358.  
  359.     for ($i = 0; $i < $lfh{'trytime'}; $i++, sleep 1) {
  360.         return \%lfh if(rename($lfh{'path'}, $lfh{'current'} = $lfh{'path'} . time));
  361.     }
  362.     opendir(LOCKDIR, $lfh{'dir'});
  363.     @filelists = readdir(LOCKDIR);
  364.     closedir(LOCKDIR);
  365.     foreach (@filelists) {
  366.         if(/^$lfh{'basename'}(\d+)/) {
  367.             return \%lfh if(time - $1 > $lfh{'timeout'} and rename($lfh{'dir'} . $_, $lfh{'current'} = $lfh{'path'} . time));
  368.             last;
  369.         }
  370.     }
  371.     undef;
  372.  
  373. }
  374.  
  375. # ファイルロック解除
  376. sub unlock {
  377.  
  378.     rename($_[0]->{'current'}, $_[0]->{'path'});
  379.  
  380. }
  381.  
  382.  
  383.  
  384.  
  385.  
  386. #                              利用規定は http://www.bayashi.net/st/ds_rule.html
  387. #-------------------------------------------------------------------------------
  388. #               Copyright (c) 2001-CurrentYear bayashi.net. All rights reserved.
  389. #                                                     http://www.bayashi.net/st/
  390. #                                                                 ok@bayashi.net
  391.