#!/usr/bin/perl

# Counter Address
$C_A = 'http://170.208.6.70/cgi-bin/counter.cgi';

# Data
$cmd = $ENV{'QUERY_STRING'};
@pairs = split(/&/,$cmd);

foreach $pair (@pairs) {
    ($name,$value) = split(/=/,$pair);
    $FORM{$name} = $value;
}

# Date
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = $year % 100;
$mon  = $mon + 1;

if($FORM{'mode'} eq 'text'){
    &tc;
}
elsif($FORM{'mode'} eq 'graphic'){
    require "gifcat.pl";
    &gc;
}
elsif($FORM{'mode'} eq 'log'){
    &log;
}
elsif($FORM{'mode'} eq 'analysis'){
    &analysis;
}

exit;

# TextCounter
sub tc
{
    if(!open(FILE, "+< log/$FORM{'for'}/count")){ return; }
    flock(FILE, 2);
    @count = <FILE>;
    chop($count[0]);
    $addr = $ENV{'REMOTE_ADDR'};
    if($count[0] eq $addr){
	close(FILE);
	print "Content-type:text/html\n\n";
	print $count[1];
	return;
    }
    # Count
    $count[0] = $addr;
    $count[1] = $count[1] + 1;
    chop($count[2]);
    @week = split(/:/, $count[2]);
    $week[$wday]++;
    $week[7]++;
    $count[2] = join(':', @week);
    chop($count[3]);
    @dt = split(/:/, $count[3]);
    $dt[$hour]++;
    $dt[24]++;
    $count[3] = join(':', @dt);
    truncate(FILE, 0);
    seek(FILE, 0, 0);
    $host = gethostbyaddr(pack("c4", split(/\./, $addr)), 2);
    $buffer = sprintf("%02d\t%02d\t%02d\t%s\t%s\t%s\t%s", $mday, $hour, $min, $addr, $host, $ENV{'HTTP_REFERER'}, $ENV{'HTTP_USER_AGENT'});
    print FILE "$count[0]\n$count[1]\n$count[2]\n$count[3]\n";
    print FILE "$year\t$mon\t$buffer\n";
    for($i = 4; $i < 13; $i++){
	print FILE "$count[$i]";
    }
    close(FILE);
    print "Content-type:text/html\n\n";
    print $count[1];

    # access log
    $file = sprintf("log/%s/%02d%02d", $FORM{'for'}, $year, $mon);
    if(!open(FILE, ">> $file")){ return; }
    print FILE "$count[1]\t$buffer\n";
    close(FILE);
}

# GraphicsCounter
sub gc
{
    if(!open(FILE, "+< log/$FORM{'for'}/count")){ return; }
    flock(FILE, 2);
    @count = <FILE>;
    chop($count[0]);
    $addr = $ENV{'REMOTE_ADDR'};
    $cntstr = sprintf("%0*d", $FORM{'length'}, $count[1]);
    
    if($count[0] eq $addr){
	close(FILE);
	printf("Content-type: image/gif\n\n");
	for ($i = 0; $i < length($cntstr); $i++) {
	    $n = substr($cntstr, $i, 1);
	    push(@files, "number/$n.gif");
	}
	binmode(STDOUT);
	print &gifcat'gifcat(@files);
	return;
    }
    # Count
    $count[0] = $addr;
    $count[1] = $count[1] + 1;
    chop($count[2]);
    @week = split(/:/, $count[2]);
    $week[$wday]++;
    $week[7]++;
    $count[2] = join(':', @week);
    chop($count[3]);
    @dt = split(/:/, $count[3]);
    $dt[$hour]++;
    $dt[24]++;
    $count[3] = join(':', @dt);
    truncate(FILE, 0);
    seek(FILE, 0, 0);
    $host = gethostbyaddr(pack("c4", split(/\./, $addr)), 2);
    $buffer = sprintf("%02d\t%02d\t%02d\t%s\t%s\t%s\t%s", $mday, $hour, $min, $addr, $host, $ENV{'HTTP_REFERER'}, $ENV{'HTTP_USER_AGENT'});
    print FILE "$count[0]\n$count[1]\n$count[2]\n$count[3]\n";
    print FILE "$year\t$mon\t$buffer\n";
    for($i = 4; $i < 13; $i++){
	print FILE "$count[$i]";
    }
    close(FILE);

    printf("Content-type: image/gif\n\n");
    for ($i = 0; $i < length($cntstr); $i++) {
	$n = substr($cntstr, $i, 1);
	push(@files, "number/$n.gif");
    }
    require "gifcat.pl";
    binmode(STDOUT);
    print &gifcat'gifcat(@files);

    # access log
    $host = gethostbyaddr(pack("c4", split(/\./, $addr)), 2);
    $buffer = sprintf("%d\t%02d\t%02d\t%02d\t%s\t%s\t%s\t%s", $count[1], $mday, $hour, $min, $addr, $host, $ENV{'HTTP_REFERER'}, $ENV{'HTTP_USER_AGENT'});
    $file = sprintf("log/%s/%02d%02d", $FORM{'for'}, $year, $mon);
    if(!open(FILE, ">> $file")){ return; }
    print FILE "$buffer\n";
    close(FILE);
}

# print log
sub log
{
    if($FORM{'year'} ne ''){
	$year = $FORM{'year'};
    }
    if($FORM{'mon'} ne ''){
	$mon = $FORM{'mon'};
    }
    $buffer = sprintf("[%s %02d' %d]", $FORM{'for'}, $year, $mon);
    $file = sprintf("log/%s/%02d%02d", $FORM{'for'}, $year, $mon);
    if(!open(FILE, "$file")){
	print "Content-type:text/html\n\n";
	print "<html><title>access log$buffer</title>";
	print "<body bgcolor=aliceblue>no logs</body></html>";
	return;
    }
    print "Content-type:text/html\n\n";
    print "<html><title>access log$buffer</title>\n";
    print "<body bgcolor=aliceblue>\n<table border=1>\n";
    print "<tr><td>count</td><td>day</td><td>hour</td><td>min</td><td>IP</td><td>host</td><td>refer</td><td>agent</td></tr>\n";
    foreach(<FILE>){
	chop($_);
	$_ =~ s/\t/<\/td><td>/g;
	print "<tr><td>$_</td></tr>\n";
    }
    close(FILE);
    print "</table></body></html>"
}

sub analysis
{
}
