http://sv3.mbx.jp/macavity/index.cgi
#!/usr/local/bin/perl
$base = "http://sv3.mbx.jp/macavity/contents";
$ENV{'TZ'} = "JST-9";
$| = 1;

# HTTP_USER_AGENT をチェック
if( $ENV{HTTP_USER_AGENT} =~ /^DoCoMo\/2/i ){ $agent = "foma"; }
elsif( $ENV{HTTP_USER_AGENT} =~ /^DoCoMo/i ){ $agent = "i-mode"; }
elsif( $ENV{HTTP_USER_AGENT} =~ /^J\-PHONE/i ){ $agent = "vodafone"; }
elsif( $ENV{HTTP_USER_AGENT} =~ /^Vodafone/i ){ $agent = "vodafone"; }
elsif( $ENV{HTTP_USER_AGENT} =~ /^MOT\-[CV]/i ){ $agent = "vodafone"; }
elsif( $ENV{HTTP_USER_AGENT} =~ /^KDDI\-/i ){ $agent = "ezweb"; }
elsif( $ENV{HTTP_USER_AGENT} =~ /UP\.Browser/i ){ $agent = "ezweb"; }
elsif( $ENV{HTTP_USER_AGENT} =~ /^PDXGW/i ){ $agent = "edge"; }
elsif( $ENV{HTTP_USER_AGENT} =~ /DDIPOCKET/i ){ $agent = "edge"; }
elsif( $ENV{HTTP_USER_AGENT} =~ /^ASTEL/i ){ $agent = "other"; }
elsif( $ENV{HTTP_USER_AGENT} =~ /^L\-mode/i ){ $agent = "other"; }
else{ $agent = "pc"; }
if( $ENV{HTTP_USER_AGENT} =~ /\,/ ){ $ENV{HTTP_USER_AGENT} = "\"$ENV{HTTP_USER_AGENT}\""; }

# contents からの帰還か
if( $ENV{HTTP_REFERER} =~ /\/contents/ ){ $back = 1 ; }
elsif( $ENV{HTTP_REFERER} =~ /\/bbs/ ){ $back = 1 ; }
else { $back = 0 ; }

# log用の文字列をセット
($sec, $min, $hour, $mday, $mon, $year, $wday) = localtime(time);
$info=sprintf("%04d/%02d/%02d,%02d:%02d:%02d,(%s),%s,%s,%s",
   $year + 1900, $mon + 1, $mday, $hour, $min, $sec,
   $ENV{REMOTE_ADDR}, 
   $ENV{HTTP_USER_AGENT},
   $ENV{SERVER_SOFTWARE},
   $ENV{HTTP_REFERER},
   );

# カウンター値取得 & log の記録
if(open(F,"+< ./index/log/count.txt")){
   $num = '?';
   $i = 0;
   while($i++<5){
      if(flock(F, 6)){
         $num = <F>;
         if( $back == 0 ){
            $num++;
            seek(F, 0, 0);
            print F $num;
         }
         open(LOG, ">> ./index/log/log.csv");
         print LOG "$info\n";
         close(LOG);
         close(F);
         last;
      }else{
         sleep(1);
      }
   }
}
1 while $num =~ s/(\d+)(\d\d\d)/$1,$2/;

# HTML 表示開始
print <<END;
Content-type: text/html

<HTML>
<HEAD>
   <TITLE>WEB SHINSHIBA</TITLE>
   <BASE href="$base/" target="_top">
</HEAD>
END
if($agent eq "pc"){
   print <<END;
<BODY BGCOLOR="#C6E9FD" background="$base/index.gif"><BR>
<FONT COLOR="#006666" SIZE="7"><B><I>しんしば </I></B></FONT>
<FONT SIZE="5">$num points</FONT><BR>
<IMG SRC="$base/nfox3.jpg"><BR>
<FONT SIZE="5">
END
   open(IN, "./index/comment.txt");
   print while (<IN>);
   close(IN);
   print "</FONT><BR>\n";
   open(IN, "./index/pc.txt");
}else{
   print <<END;
<BODY BGCOLOR="#C6E9FD">
<FONT COLOR="#006666">しんしば</FONT>
<BR>$num points<BR>
END
   if($agent eq "foma"){
      print "<IMG SRC=\"$base/nfox3.jpg\"><BR>\n";
   }
   open(IN, "./index/imode.txt");
}
print while (<IN>);
close(IN);
print "</BODY>\n</HTML>";

#終わり