#!/usr/local/bin/perl
#↑Perlのパスを指定

#----------------------------------------------------------#
#Copyright(C)2000-2002 ohtan. All rights reserved.         #
#CmfCounter-TypeA ver7.00                                  #
#Build :2000/01/10                                         #
#Updata:2003/02/28                                         #
#Home  :Counter Material Factory                           #
#       http://cmf.ohtanz.com/                             #
#Mail  :cmf@ohtanz.com                                     #
#----------------------------------------------------------#
#_______________________[ 設定項目 ]_______________________#

#[1]カウンターのデフォルトの桁数---------------------------
$Beam      = 6;

#[2]連続カウントアップの防止-------------------------------
$Doubleup  = 0;       #(0:OFF/1:同日中の同一IPはカウントしない/2:直前の同一IPはカウントしない/3:一定時間内の同一IPはカウントしない)
$Doublelmt = 60*60*1; #$Doubleupが3の場合の記録しない時間(例：12時間記録しない場合は「60*60*12」30分なら「60*30」)

#[3]不正使用防止機能---------------------------------------
$Prevent   = 0;       #(0:OFF/1:ON)
@Prevurl   = ();      #ONにした場合カウンターを設置するページのURL

#[4]カウンターログの自動生成機能---------------------------
#この機能を有効にした場合はなるべく上記3も有効にして下さい
$Automake  = 1;       #(0:OFF/1:ON)

#[5]ファイルロックのタイプ---------------------------------
$Locktype  = 0;       #(0:flock/1:link/2:rename)

#[6]カウンター画像格納用ディレクトリ-----------------------
$Imagedir  = './images';

#[7]ログ格納用ディレクトリ---------------------------------
$Datadir   = './data';

#[8]アクセスログ機能---------------------------------------
$Accesslog = 1;       #(0:OFF/1:ON)

#[9]アクセスログの背景色-----------------------------------
$Accbgcol  = '#ffffff';

#[10]アクセスログ表示用画像が入っているディレクトリのパス--
$Accimage  = './images';

#[11]アクセスログのバーの長さ------------------------------
$Hourbar   = 1; #時
$Daybar    = 1; #日
$Monthbar  = 5; #月

#[12]海外サーバー用時差調整(最初の数字を1-24時間で指定)----
#(Ex:15時間進める場合15*60*60/15時間遅らせる場合-15*60*60)
$Timediff  = 0;

#[13]特定のHOST/IPのカウント防止---------------------------
#(Ex:HOSTを指定する場合sample.ne.jp/IPを指定する場合192.168.1.1なら192.168を指定
@Denycount = ();

#[14]gifcat.plのパス---------------------------------------
$Libgifcat = './gifcat.pl';

#[15]errgif.plのパス---------------------------------------
$Liberrgif = './errgif.pl';

#________[ 設定終了(以下の変更はPerlの知識が必要) ]________#

#Get form data --------------------------------------------
$Buffer = $ENV{'QUERY_STRING'};

@Bufferlist = split(/&/,$Buffer);

%Bufferhash = ();
foreach (@Bufferlist) {
  ($n,$v) = split(/=/,$_);
   $Bufferhash{$n} = $v;
}

$ModName = $Bufferhash{'mode'};
$CntName = $Bufferhash{'name'};
$Cntbeam = $Bufferhash{'beam'};
$ImgName = $Bufferhash{'img'};

$Nowtime = time + $Timediff;

#Setting of accesslog -------------------------------------
$Acctmp  = "$Datadir/$$\_accs.tmp";
$Acclock = "$Datadir/$CntName\_accs.tmp";
$Accfile = "$Datadir/$CntName\_accs.dat";

#Output of access log -------------------------------------
if ($Accesslog && $ModName eq "acclog") {
    if ($CntName eq "") {
        &AccErr("Counter name is not specified.");
    }
    elsif ($CntName =~ /[^\w\-]/) {
        &AccErr("Counter name is inaccurate.");
    }

    &OutputAcc;
}
#Output of counter ----------------------------------------
elsif ($ModName eq "count") {
    require "$Liberrgif";

    if ($CntName eq "") {
        errgif::getErr(1);
    }
    elsif ($CntName =~ /[^\w\-]/) {
        errgif::getErr(2);
    }
    elsif ($ImgName eq "" || $ImgName =~ /[^a-zA-Z]/) {
        errgif::getErr(3);
    }

    #Setting of counter -----------------------------------
    require "$Libgifcat";

    $HttpReferer = $ENV{'HTTP_REFERER'};
    $Remoteaddr  = $ENV{'REMOTE_ADDR'};
    $Remotehost  = $ENV{'REMOTE_HOST'};

    $Tempfile = "$Datadir/$$.tmp";
    $Lockfile = "$Datadir/$CntName\_lock.tmp";
    $Backfile = "$Datadir/$CntName\_back.dat";
    $Cntfile  = "$Datadir/$CntName\_count.dat";
    $Addrfile = "$Datadir/$CntName\_addr.dat";

    #Check of referer -------------------------------------
    if ($Prevent && !grep($HttpReferer =~ /$_/i,@Prevurl)) {
        errgif::getErr(4);
    }

    #Check of counter log ---------------------------------
    if (!$Automake && !-e $Cntfile) {
        errgif::getErr(2);
    }

    #Check of countup -------------------------------------
    if ($Locktype != 0) {
        if (!&LockCheck) {
            errgif::getErr(5);
        }
    }

    $Addrok = &AddrCheck;
    $Hostok = &HostCheck;

    if ($Addrok && $Hostok) {
        &CountUp($ImgName);
    } else {
        &NoCount($ImgName);
    }
} else {
    &AccErr("There is no specification of mode.");
}

exit;

sub AddrCheck {
    if ($Doubleup == 1 || $Doubleup == 2) {
        $Adddate1 = &GetDate("statd",$Addrfile);
        $Adddate2 = &GetDate("dated");

        if (!-e $Addrfile || $Adddate1 != $Adddate2) {
            if (!open(OUT,">$Addrfile")) {
                errgif::getErr(6);
            }
            print OUT "$Remoteaddr\n";
            close(OUT);

            $Cntok = 1;
        } else {
            if (!open(IN,"<$Addrfile")) {
                errgif::getErr(7);
            }
            @Iplist = <IN>;
            close(IN);

            $Match = 1;
            foreach (@Iplist) {
               $_ =~ s/\r\n|\r|\n//g;

               if ($_ eq $Remoteaddr) {
                   $Match = 0;
                   last;
               }
            }

            if (!$Match) {
                $Cntok = 0;
            } else {
                if ($Doubleup == 1) {
                    if (!open(OUT,">>$Addrfile")) {
                       errgif::getErr(8);
                    }
                } else {
                    if (!open(OUT,">$Addrfile")) {
                        errgif::getErr(8);
                    }
                }
                print OUT "$Remoteaddr\n";
                close(OUT);

                $Cntok = 1;
            }
        }
    }
    elsif ($Doubleup == 3) {
        $LimitDate = $Nowtime - $Doublelmt;

        if (!-e $Addrfile) {
            if (!open(OUT,">$Addrfile")) {
                errgif::getErr(6);
            }
            print OUT "$Remoteaddr<>$Nowtime\n";
            close(OUT);

            $Cntok = 1;
        } else {
            %addrs = ();
            if (!open(IN,"<$Addrfile")) {
                errgif::getErr(7);
            }
            while (<IN>) {
               $_ =~ s/\r\n|\r|\n//g;
              ($cache_addr,$cache_time) = split(/<>/,$_);
               $addrs{$cache_addr} = $cache_time;
            }
            close(IN);

            if ($addrs{$Remoteaddr} eq "" || $addrs{$Remoteaddr} < $LimitDate) {
                $addrs{$Remoteaddr} = $Nowtime;
                $Cntok = 1;
            } else {
                $Cntok = 0;
            }

            if (!open(OUT,">$Addrfile")) {
                errgif::getErr(8);
            }
            eval'flock(OUT,2);';

            foreach (keys(%addrs)) {
               if ($addrs{$_} < $LimitDate) {
                   next;
               } else {
                   print OUT "$_<>$addrs{$_}\n";
               }
            }

            close(OUT);
        }
    } else {
        $Cntok = 1;
    }

    return($Cntok);
}

sub CountUp {
    local($L_name) = @_;

    if (!$Locktype) {
        foreach (1..10) {
           if (!-e $Lockfile) {
               if (!open(LOCK,">$Lockfile")) {
                   errgif::getErr(5);
               }
               eval'flock(LOCK,2);';
               last;
           }
           elsif ($_ == 10) {
               $lapsed_time = (-M $Lockfile) * 86400;

               if ($lapsed_time > 180) {
                   if (!open(LOCK,">$Lockfile")) {
                       errgif::getErr(5);
                   }
                   eval'flock(LOCK,2);';
                   last;
               } else {
                   errgif::getErr(5);
               }
           }
           sleep(1);
        }

        if (!-e $Cntfile) {
            $Cntdata = 0;
        } else {
            $Cntdata = &ReadCnt;
            $Cntback = &CheckBack($Cntdata);

            if ($Cntdata < $Cntback) {
                $Cntdata = $Cntback;
            }
        }

        $Cntdata++;
        $Cntdata .= "\n";

        if (!-e $Cntfile) {
            if (!open(OUT,">$Cntfile")) {
                errgif::getErr(9);
                close(LOCK);
            }
        } else {
            if (!open(OUT,"+<$Cntfile")) {
                errgif::getErr(9);
                close(LOCK);
            }
        }
        seek(OUT,0,0);
        print OUT $Cntdata;
        eval'truncate(OUT,tell(OUT));';
        close(OUT);

        close(LOCK);

        unlink($Lockfile);
    } else {
        if (!-e $Cntfile) {
            $Cntdata = 0;
        } else {
            $Cntdata = &ReadCnt;
            $Cntback = &CheckBack($Cntdata);

            if ($Cntdata < $Cntback) {
                $Cntdata = $Cntback;
            }
        }

        $Cntdata++;
        $Cntdata .= "\n";

        if (!open(OUT,">$Tempfile")) {
            &UnlinkTmp;
            errgif::getErr(9);
        }
        eval'flock(OUT,2);';
        print OUT $Cntdata;
        close(OUT);

        if ($Locktype == 1) {
            &CntLink($Tempfile);
        } else {
            &CntRename($Tempfile);
        }
    }

    &WriteAcc if $Accesslog;
    &PrintBack($Cntdata);

    $Cntdata =~ s/\r\n|\r|\n//g;

    &OutputCnt($Cntdata,$L_name);
}

sub NoCount {
    local($L_name) = @_;

    $Cntdata = &ReadCnt;
    &OutputCnt($Cntdata,$L_name);
}

sub UnlinkTmp {
    $statdate1 = &GetDate("stata",$Lockfile);
    $statdate2 = &GetDate("stata",$Acclock);
    $basedate  = $Nowtime - 10;

    unlink($Tempfile) if -e $Tempfile;
    unlink($Lockfile) if -e $Lockfile && $statdate1 < $basedate;
    unlink($Acctmp)   if -e $Acctmp;
    unlink($Acclock)  if -e $Acclock && $statdate2 < $basedate;
}

sub ReadCnt {
    local($L_cntdata);

    if ($Automake && !-e $Cntfile) {
        if (!open(OUT,">$Cntfile")) {
            errgif::getErr(10);
        }
        print OUT "0\n";
        close(OUT);
    }

    if (!open(IN,"<$Cntfile")) {
        errgif::getErr(10);
    }
    eval'flock(IN,2);';
    $L_cntdata = <IN>;
    close(IN);

    $L_cntdata =~ s/\r\n|\r|\n//g;

    return($L_cntdata);
}

sub CntLink {
    local($L_file) = @_;

    foreach (1..10) {
       if (link($L_file,$Lockfile) == 1) {
           if (!rename($Lockfile,$Cntfile)) {
               &UnlinkTmp;
               errgif::getErr(11);
           }
           chmod(0666,$Cntfile);
           last;
       }
       elsif ($_ == 10) {
           &UnlinkTmp;
           errgif::getErr(5);
       }

       sleep(1);
    }

    unlink($L_file);
}

sub CntRename {
    local($L_file) = @_;

    foreach (1..10) {
       if (!-e $Lockfile) {
           if (!open(OUT,">$Lockfile")) {
               &UnlinkTmp;
               errgif::getErr(11);
           }
           close(OUT);

           if (!rename($L_file,$Cntfile)) {
               &UnlinkTmp;
               errgif::getErr(11);
           }
           chmod(0666,$Cntfile);
           last;
       }
       elsif ($_ == 10) {
           &UnlinkTmp;
           errgif::getErr(5);
       }

       sleep(1);
    }

    unlink($Lockfile);
}

sub CheckBack {
    local($L_cntdata) = @_;

    if (-e $Backfile) {
        if (!open(IN,"<$Backfile")) {
            errgif::getErr(12);
        }
        eval'flock(IN,2);';
        @Backlist = <IN>;
        close(IN);

        $Backcnt = pop(@Backlist);
        $Backcnt =~ s/\r\n|\r|\n//g;
    } else {
        if (!open(OUT,">$Backfile")) {
            errgif::getErr(13);
        }
        close(OUT);

       $Backcnt = $L_cntdata;
    }

    return($Backcnt);
}

sub PrintBack {
    local($L_cntdata) = @_;

    $Backdate1 = &GetDate("statd",$Backfile);
    $Backdate2 = &GetDate("dated");

    if ($Backdate1 < $Backdate2) {
        if (!open(OUT,">$Backfile")) {
            errgif::getErr(14);
        }
    } else {
        if (!open(OUT,">>$Backfile")) {
            errgif::getErr(14);
        }
    }
    eval'flock(OUT,2);';
    print OUT $L_cntdata;
    close(OUT);
}

sub HostCheck {
    if ($Denycount[0] eq "") {
        $Cntok = 1;
    } else {
        if ($Remoteaddr eq $Remotehost || $Remotehost eq "") {
            $Remotehost = gethostbyaddr(pack("C4",split(/\./,$Remoteaddr)),2);

            if ($Remotehost eq "") {
                $Remotehost = $Remoteaddr;
            }
        }

        if (grep($Remoteaddr =~ /$_/i,@Denycount)) {
            $Cntok = 0;
        }
        elsif (grep($Remotehost =~ /$_/i,@Denycount)) {
            $Cntok = 0;
        } else {
            $Cntok = 1;
        }
    }

    return($Cntok);
}

sub LockCheck {
    local($Lockok) = 0;

    foreach (1..10) {
       if (!-e $Lockfile) {
           $Lockok = 1;
           last;
       }
       sleep(1);
    }

    return($Lockok);
}

sub OutputCnt {
    local($L_cntnum,$L_cntn) = @_;

    if (!$Cntbeam || $Cntbeam =~ /[^0-9]/) {
        $Cntbeam = $Beam;
    }

    if ($Cntbeam < length($L_cntnum)) {
        $Digits = $L_cntnum;
    } else {
        $Digits = sprintf("%0$Cntbeam\d",$L_cntnum);
    }

    @Digit = split(//,$Digits);

    $i = 0;
    foreach (@Digit) {
       $Images[$i] = "$Imagedir/$L_cntn$_.gif";
       $i++;
    }

    binmode(STDOUT);
    print "Content-type: image/gif\n\n";
    print &gifcat'gifcat(@Images);
}

sub GetDate {
    local($L_type,$L_file) = @_;

    if ($L_type eq "stata") {
        $Return = (stat($L_file))[9] + $Timediff;
    }
    elsif ($L_type eq "statd") {
        $Return = (localtime((stat($L_file))[9] + $Timediff))[3];
    }
    elsif ($L_type eq "statm") {
        $Return = ((localtime((stat($L_file))[9] + $Timediff))[4]) + 1;
    }
    elsif ($L_type eq "staty") {
        $Return = ((localtime((stat($L_file))[9] + $Timediff))[5]) + 1900;
    }
    elsif ($L_type eq "dateh") {
        $Return = (localtime($Nowtime))[2];
    }
    elsif ($L_type eq "dated") {
        $Return = (localtime($Nowtime))[3];
    }
    elsif ($L_type eq "datem") {
        $Return = ((localtime($Nowtime))[4]) + 1;
    }
    elsif ($L_type eq "datey") {
        $Return = ((localtime($Nowtime))[5]) + 1900;
    }

    return($Return);
}

sub WriteAcc {
    $Acchour   = &GetDate("dateh");
    $Accdate   = &GetDate("dated");
    $Accmonth  = &GetDate("datem");
    $Accyear   = &GetDate("datey");
    $Statdate  = &GetDate("statd",$Accfile);
    $Statmonth = &GetDate("statm",$Accfile);
    $Statyear  = &GetDate("staty",$Accfile);

    %Accdata = ();
    $Acclist = "";

    if (!-e $Accfile) {
        &MakeHour;
        &MakeDate;
        &MakeMonth;
    } else {
        if (!open(IN,"<$Accfile")) {
            errgif::getErr(16);
        }
        $Accnow = <IN>;
        close(IN);

        $Accnow =~ s/\r\n|\r|\n//g;
        @Accall = split(/&/,$Accnow);

        $R_acchour  = "h$Acchour";
        $R_accdate  = "d$Accdate";
        $R_accmonth = "m$Accmonth";

        if ($Accyear != $Statyear) {
            &MakeHour;
            &MakeDate;
            &MakeMonth;
        }
        elsif ($Accmonth != $Statmonth) {
            &MakeHour;
            &MakeDate;

            foreach (@Accall) {
              ($Acckey,$Accval)  = split(/<>/,$_);
               $Accdata{$Acckey} = $Accval;

               if ($R_accmonth eq $Acckey) {
                   $Accdata{$R_accmonth} += 1;
               }

               if ($Acckey =~ /^m/) {
                   $Acclist .= "$Acckey<>$Accdata{$Acckey}\&";
               }
            }
        }
        elsif ($Accdate != $Statdate) {
            &MakeHour;

            foreach (@Accall) {
              ($Acckey,$Accval)  = split(/<>/,$_);
               $Accdata{$Acckey} = $Accval;

               if ($R_accdate eq $Acckey) {
                   $Accdata{$R_accdate} += 1;
               }
               elsif ($R_accmonth eq $Acckey) {
                   $Accdata{$R_accmonth} += 1;
               }

               if ($Acckey !~ /^h/) {
                   $Acclist .= "$Acckey<>$Accdata{$Acckey}\&";
               }
            }
        } else {
            foreach (@Accall) {
              ($Acckey,$Accval)  = split(/<>/,$_);
               $Accdata{$Acckey} = $Accval;

               if ($R_acchour eq $Acckey) {
                   $Accdata{$R_acchour} += 1;
               }
               elsif ($R_accdate eq $Acckey) {
                   $Accdata{$R_accdate} += 1;
               }
               elsif ($R_accmonth eq $Acckey) {
                   $Accdata{$R_accmonth} += 1;
               }

               $Acclist .= "$Acckey<>$Accdata{$Acckey}\&";
            }
        }
    }

    $Acclist .= "\n";

    if (!$Locktype) {
        foreach (1..10) {
           if (!-e $Acclock) {
               if (!open(ALOK,">$Acclock")) {
                   errgif::getErr(15);
               }
               eval'flock(LOCK,2);';
               last;
           }
           elsif ($_ == 10) {
               $lapsed_atime = (-M $Acclock) * 86400;

               if ($lapsed_atime > 180) {
                   if (!open(ALOK,">$Acclock")) {
                       errgif::getErr(15);
                   }
                   eval'flock(ALOK,2);';
                   last;
               } else {
                   errgif::getErr(15);
               }
           }
           sleep(1);
        }

        if (!-e $Accfile) {
            if (!open(OUT,">$Accfile")) {
                errgif::getErr(15);
                close(ALOK);
            }
        } else {
            if (!open(OUT,"+<$Accfile")) {
                errgif::getErr(17);
                close(ALOK);
            }
        }
        seek(OUT,0,0);
        print OUT $Acclist;
        eval'truncate(OUT,tell(OUT));';
        close(OUT);

        close(ALOK);

        unlink($Acclock);
    } else {
        if (!open(OUT,">$Acctmp")) {
            errgif::getErr(17);
        }
        eval'flock(OUT,2);';
        print OUT $Acclist;
        close(OUT);

        if ($Locktype == 1) {
            &AccLink($Acctmp);
        } else {
            &AccRename($Acctmp);
        }
    }
}

sub MakeHour {
    foreach (0..23) {
       if ($Acchour == $_) {
           $Accdata{"h$_"} += 1;
       } else {
           $Accdata{"h$_"} = 0;
       }
       $R_acchour = $Accdata{"h$_"};
       $Acclist  .= "h$_<>$R_acchour\&";
    }
}

sub MakeDate {
    foreach (1..31) {
       if ($Accdate == $_) {
           $Accdata{"d$_"} += 1;
       } else {
           $Accdata{"d$_"} = 0;
       }
       $R_accdate = $Accdata{"d$_"};
       $Acclist .= "d$_<>$R_accdate\&";
    }
}

sub MakeMonth {
    foreach (1..12) {
       if ($Accmonth == $_) {
           $Accdata{"m$_"} += 1;
       } else {
           $Accdata{"m$_"} = 0;
       }
       $R_accmonth = $Accdata{"m$_"};
       $Acclist .= "m$_<>$R_accmonth\&";
    }
}

sub AccLink {
    local($A_file) = @_;

    foreach (1..10) {
       if (link($A_file,$Acclock) == 1) {
           if (!rename($Acclock,$Accfile)) {
               &UnlinkTmp;
               errgif::getErr(17);
           }
           chmod(0666,$Accfile);
           last;
       }
       elsif ($_ == 10) {
           &UnlinkTmp;
           errgif::getErr(5);
       }

       sleep(1);
    }

    unlink($A_file);
}

sub AccRename {
    local($A_file) = @_;

    foreach (1..10) {
       if (!-e $Acclock) {
           if (!open(OUT,">$Acclock")) {
               &UnlinkTmp;
               errgif::getErr(17);
           }
           close(OUT);

           if (!rename($A_file,$Accfile)) {
               &UnlinkTmp;
               errgif::getErr(17);
           }
           chmod(0666,$Accfile);
           last;
       }
       elsif ($_ == 10) {
           &UnlinkTmp;
           errgif::getErr(5);
       }

       sleep(1);
    }

    unlink($Acclock);
}

sub OutputAcc {
    foreach (1..10) {
       if (!-e $Acclock) {
           last;
       }
       elsif ($_ == 10) {
           &AccErr("It is the deadline(budy).");
       }
    }

    if (!open(IN,"<$Accfile")) {
        &AccErr("Reading of access log was not completed.");
    }
    $Accdata = <IN>;
    close(IN);

    $Accdata =~ s/\r\n|\r|\n//g;
    @Acclist = split(/&/,$Accdata);

    %Accdata = (); 

    foreach (@Acclist) {
      ($Acckey,$Accval)  = split(/<>/,$_);
       $Accdata{$Acckey} = $Accval;
    }

    $Acchour    = &GetDate("dateh");
    $Accdate    = &GetDate("dated");
    $Accmonth   = &GetDate("datem");
    $Accyear    = &GetDate("datey");
    $R_acchour  = "h$Acchour";
    $R_accdate  = "d$Accdate";
    $R_accmonth = "m$Accmonth";

    print "Content-type: text/html\n\n";
    print "<html>\n";
    print "<head>\n";
    print "<title>AccessLog($CntName)</title>\n";
    print "<style type=\"text/css\"><!--\n";
    print ".h{color:#c80000;font-size:12px;font-family:MS UI Gothic}\n";
    print ".d{color:#191970;font-size:12px;font-family:MS UI Gothic}\n";
    print ".m{color:#008000;font-size:12px;font-family:MS UI Gothic}\n";
    print "--></style>\n";
    print "</head>\n";
    print "<body bgcolor=\"$Accbgcol\">\n";
    print "<div align=\"center\">\n";
    print "<table width=\"80%\" bgcolor=\"#ffd9d9\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\">\n";
    print "<tr><td colspan=\"2\"><hr size=\"1\" color=\"#c80000\" noshade></td></tr>\n";
    print "<tr><td align=\"center\" colspan=\"2\" class=\"h\"><b>HOUR</b></td></tr>\n";
    print "<tr><td colspan=\"2\"><hr size=\"1\" color=\"#c80000\" noshade></td></tr>\n";

    $h_all = 0;
    $c     = 0;
    foreach (keys(%Accdata)) {
       $h_val = $Accdata{"h$c"};
       $h_key = "h$c";

       if ($c > 23) {
           last;
       } else {
           if ($h_val > 0) {
               $h_wid = $h_val / $Hourbar;
               $h_img = "<img src=\"$Accimage/ach.gif\" width=\"$h_wid\" height=\"10\">";
           } else {
               $h_img = "";
           }

           if ($R_acchour eq $h_key) {
               print "<tr><td align=\"right\" width=\"5%\" class=\"h\" nowrap><b>$c時</b></td><td class=\"h\" nowrap>$h_img $h_val</td></tr>\n";
           } else {
               print "<tr><td align=\"right\" width=\"5%\" class=\"h\" nowrap>$c時</td><td class=\"h\" nowrap>$h_img $h_val</td></tr>\n";
           }
           $h_all += $h_val;
       }
       $c++;
    }

    print "<tr><td colspan=\"2\"><hr size=\"1\" color=\"#c80000\" noshade></td></tr>\n";
    print "<tr><td colspan=\"2\" class=\"h\"><b>$Accdate日の合計 = $h_all</b></td></tr>\n";
    print "<tr><td colspan=\"2\"><hr size=\"1\" color=\"#c80000\" noshade></td></tr>\n";
    print "</table><br><br>\n";
    print "<table width=\"80%\" bgcolor=\"#b9dcff\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\">\n";
    print "<tr><td colspan=\"2\"><hr size=\"1\" color=\"#191970\" noshade></td></tr>\n";
    print "<tr><td align=\"center\" colspan=\"2\" class=\"d\"><b>DAY</b></td></tr>\n";
    print "<tr><td colspan=\"2\"><hr size=\"1\" color=\"#191970\" noshade></td></tr>\n";

    $d_all = 0;
    $c     = 1;
    foreach (keys(%Accdata)) {
       $d_val = $Accdata{"d$c"};
       $d_key = "d$c";

       if ($c > 31) {
           last;
       } else {
           if ($d_val > 0) {
               $d_wid = $d_val / $Daybar;
               $d_img = "<img src=\"$Accimage/acd.gif\" width=\"$d_wid\" height=\"10\">";
           } else {
               $d_img = "";
           }

           if ($R_accdate eq $d_key) {
               print "<tr><td align=\"right\" width=\"5%\" class=\"d\" nowrap><b>$c日</b></td><td class=\"d\" nowrap>$d_img $d_val</td></tr>\n";
           } else {
               print "<tr><td align=\"right\" width=\"5%\" class=\"d\" nowrap>$c日</td><td class=\"d\" nowrap>$d_img $d_val</td></tr>\n";
           }
           $d_all += $d_val;
       }
       $c++;
    }

    print "<tr><td colspan=\"2\"><hr size=\"1\" color=\"#191970\" noshade></td></tr>\n";
    print "<tr><td colspan=\"2\" class=\"d\"><b>$Accmonth月の合計 = $d_all</b></td></tr>\n";
    print "<tr><td colspan=\"2\"><hr size=\"1\" color=\"#191970\" noshade></td></tr>\n";
    print "</table><br><br>\n";
    print "<table width=\"80%\" bgcolor=\"#d0ffd0\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\">\n";
    print "<tr><td colspan=\"2\"><hr size=\"1\" color=\"#008000\" noshade></td></tr>\n";
    print "<tr><td align=\"center\" colspan=\"2\" class=\"m\"><b>MONTH</b></td></tr>\n";
    print "<tr><td colspan=\"2\"><hr size=\"1\" color=\"#008000\" noshade></td></tr>\n";

    $m_all = 0;
    $c     = 1;
    foreach (keys(%Accdata)) {
       $m_val = $Accdata{"m$c"};
       $m_key = "m$c";

       if ($c > 12) {
           last;
       } else {
           if ($m_val > 0) {
               $m_wid = $m_val / $Monthbar;
               $m_img = "<img src=\"$Accimage/acm.gif\" width=\"$m_wid\" height=\"10\">";
           } else {
               $m_img = "";
           }

           if ($R_accmonth eq $m_key) {
               print "<tr><td align=\"right\" width=\"5%\" class=\"m\" nowrap><b>$c月</b></td><td class=\"m\" nowrap>$m_img $m_val</td></tr>\n";
           } else {
               print "<tr><td align=\"right\" width=\"5%\" class=\"m\" nowrap>$c月</td><td class=\"m\" nowrap>$m_img $m_val</td></tr>\n";
           }
           $m_all += $m_val;
       }
       $c++;
    }

    print "<tr><td colspan=\"2\"><hr size=\"1\" color=\"#008000\" noshade></td></tr>\n";
    print "<tr><td colspan=\"2\" class=\"m\"><b>$Accyear年の合計 = $m_all</b></td></tr>\n";
    print "<tr><td colspan=\"2\"><hr size=\"1\" color=\"#008000\" noshade></td></tr>\n";
    print "</table>\n";
    print "</div>\n";
    print "</body>\n";
    print "</html>\n";

    exit;
}

sub AccErr {
    local($ErrorMessage) = @_;

    print "Content-type: text/html\n\n";
    print "<html>\n";
    print "<head>\n";
    print "<style type=\"text/css\"><!--\n";
    print "td {color:#c80000;font-size:12px;font-family:MS UI Gothic}\n";
    print "--></style>\n";
    print "<title>Error!!</title>\n";
    print "</head>\n";
    print "<body bgcolor=\"$Accbgcol\">\n";
    print "<div align=\"center\">\n";
    print "<table border=\"0\" bgcolor=\"#ffffff\" height=\"80\" cellspacing=\"0\" cellpadding=\"2\">\n";
    print "<tr valign=\"middle\" align=\"center\">\n";
    print "    <td nowrap><b>Error!!</b><br><br>$ErrorMessage</td>\n";
    print "</tr>\n";
    print "</table>\n";
    print "</div>\n";
    print "</body>\n";
    print "</html>\n";

    exit;
}
