#!/usr/bin/perl

use URI::Escape;

#######################################################
#BooCounterPro,v1.26 / this program is value software.#
#(C) BooBooClub 1999-2001                             #
#######################################################

#set data

$loglimit = '2';



#read html
$buffer = $ENV{'QUERY_STRING'};
$buffer = uri_unescape($buffer);

@pairs = split(/&/,$buffer);
foreach $pair (@pairs) {
	($name, $value) = split(/=/,$pair);
	$FORM{$name} = $value;
}
$fig = $FORM{'fig'};
$count = $FORM{'data'};
$image = $FORM{'img'};

#time
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time - (60*60*24*1));
$year = ($year + 1900);
$mon++;
if ($sec < 10) {
	$sec = "0$sec";
}
if ($min < 10) {
	$min = "0$min";
}
if ($hour < 10) {
	$hour = "0$hour";
}
if ($mon < 10) {
	$mon = "0$mon";
}
if ($mday < 10) {
	$mday = "0$mday";
}
#$time = "$year/$mon/$mday $hour:$min:$sec";
$ctime = $wday;


&lock;

sub lock{
	#lock check
	$lockfile = './lock/lockfile';
	$retry = 1;
	until (symlink("$count", "$lockfile")){
		if (++$retry >= 30){
			unlink("$lockfile"); 
			exit 0;
		}
		sleep(1);
	}
}

#count
open(CNT,"$count");
$cnt = <CNT>;
close(CNT);
chomp $cnt;
@cnt = split(/,/, $cnt);
if($ctime ne $cnt[1]){
	if($ctime-1 == $cnt[1] || $ctime+6 == $cnt[1]){
		$cnt[2] = $cnt[3];
		$cnt[3] = 0;
	}
	else{
		$cnt[2] = 0;
		$cnt[3] = 0;
	}
	if($FORM{'log'} eq 'on' && !$FORM{'v'}){
		open (LOG,"$FORM{'ldata'}");
		@log = <LOG>;
		close (LOG);
		$yd = $mday + 0;
		if($yd < 10){
			$yd = "0$yd";
		}
		$nlog = "$year/$mon/$yd : $cnt[2] : $cnt[0]\n";
		push(@log,$nlog);

		if($mon - $loglimit <= 0){
			$fyear = $year - 1;
			$fmon = 12 + ($mon - $loglimit);
		}
		else{
			$fmon = $mon - $loglimit;
		}
		foreach $log(@log){
			@logs = split(/:/,$log);
			$logs[0] =~ s/ //g;
			@logtime = split(/\//,$logs[0]);
			if(($fyear && (($logtime[0] == $fyear && $logtime[1] > $fmon)||$logtime[0] == $year))||!$fyear && $logtime[0] == $year && ($logtime[1] > $fmon)){
				push(@nlog,$log);
			}
		}
		open(CNT,">$FORM{'ldata'}");
		print CNT @nlog;
		close(CNT);
	}
}
if(!$FORM{'v'}){
	$cnt[0]++;
	$cnt[3]++;
	$cnt = "$cnt[0]\,$ctime\,$cnt[2]\,$cnt[3]\,\n";
	open(CNT,">$count");
	print CNT $cnt;
	close(CNT);
	$cnt = $cnt[0];
}
elsif($FORM{'v'} eq 'y'){
	$cnt = $cnt[2];
}
elsif($FORM{'v'} eq 't'){
	$cnt = $cnt[3];
}


#unlock
unlink("$lockfile");



#change_table
%cb = ('b0'=>'0000','b1'=>'0001','b2'=>'0010','b3'=>'0011','b4'=>'0100','b5'=>'0101','b6'=>'0110','b7'=>'0111','b8'=>'1000','b9'=>'1001','ba'=>'1010','bb'=>'1011','bc'=>'1100','bd'=>'1101','be'=>'1110','bf'=>'1111');
%stb = ('b000'=>'0','b001'=>'1','b010'=>'2','b011'=>'3','b100'=>'4','b101'=>'5','b110'=>'6','b111'=>'7');
%ltb = ('b0000'=>'0','b0001'=>'1','b0010'=>'2','b0011'=>'3','b0100'=>'4','b0101'=>'5','b0110'=>'6','b0111'=>'7','b1000'=>'8','b1001'=>'9','b1010'=>'a','b1011'=>'b','b1100'=>'c','b1101'=>'d','b1110'=>'e','b1111'=>'f');

#ready
$imgz = "$image"."0.gif";
open(IMG,"$imgz");
binmode(IMG);
while(<IMG>){
	$img = $img.$_;
}
close(IMG);

$i = 0;
while($i < length($img)){
	$str = substr($img,$i,1);
	$strh = unpack("H",$str).unpack("h",$str);
	push (@btx,$strh);
	$i++;
}

print "Content-type: image/gif\n\n";
binmode(STDOUT);

#output
#gh

print "GIF89a";

$ix = (hex($btx[7])*256) + (hex($btx[6]));
$imgax = sprintf("%lx",(hex($btx[7])*256) + (hex($btx[6])) * $fig);
$imgfx = "0" x (4 - length($imgax)).$imgax;
@imgx = (substr($imgfx,2,2),substr($imgfx,0,2));
foreach $imgx(@imgx){
	print chr(hex("$imgx"));
}

print chr(hex("$btx[8]"));
print chr(hex("$btx[9]"));

$hb1 = $cb{"b".substr($btx[10],0,1)};
$hb2 = $cb{"b".substr($btx[10],1,1)};
print chr(hex("$btx[10]"));
print chr(hex("$btx[11]"));
print chr(hex("00"));

if(substr($hb1,0,1) == 1){
	$sgct = $stb{"b".substr($hb2,1,3)};
	$slg = 3*(2**($sgct+1));
	$hli = 13;
	$hh = $hli + $slg;
	while($hli < $hh){
		print chr(hex("$btx[$hli]"));
		$ogct .= $btx[$hli];
		$hli++;
	}
}

if(($fig - length($cnt)) >= 0){
	$cntl = ($fig - length($cnt));
}
else {
	$mcntl = (length($cnt) - $fig);
	$cnt = substr($cnt,$mcntl,$fig);
}

$ctc = 0;
$dcd = 0;
while($ctc < $fig){
	$ctc++;
	if($ctc <= $cntl){
		&oeimg;
	}
	else{
		@btx = ();
		$img = "";
		
		$eimg = "$image".substr($cnt,$dcd,1).".gif";
		
		$dcd++;
		open(IMG,"$eimg");
		binmode(IMG);
		while(<IMG>){
			$img = $img.$_;
		}
		close(IMG);

		$i = 0;
		while($i < length($img)){
			$str = substr($img,$i,1);
			$strh = unpack("H",$str).unpack("h",$str);
			push (@btx,$strh);
			$i++;
		}

		#gh
		$imgax = sprintf("%lx",(hex($btx[7])*256) + (hex($btx[6])) * $fig);
		$imgfx = "0" x (4 - length($imgax)).$imgax;
		@imgx = (substr($imgfx,2,2),substr($imgfx,0,2));

		$hb1 = $cb{"b".substr($btx[10],0,1)};
		$hb2 = $cb{"b".substr($btx[10],1,1)};

		if(substr($hb1,0,1) == 1){
			$osgct = substr($hb2,1,3);
			$sgct = $stb{"b".substr($hb2,1,3)};
			$slg = 3*(2**($sgct+1));
			$hli = 13;
			$hh = $hli + $slg;
			$egct = '';
			while($hli < $hh){
				$egct .= $btx[$hli];
				$hli++;
			}
			$check = 0;
			if($ogct ne $egct){
				$check = 1;
			}
		}
		&oeimg;
	}
}

#each_image_data

sub oeimg{
	#gce
	if (($btx[$slg+13]) == 21){
		print chr(hex("21"));
		print chr(hex("f9"));
		print chr(hex("04"));
		print chr(hex("$btx[$slg+16]"));
		print chr(hex("$btx[$slg+17]"));
		print chr(hex("$btx[$slg+18]"));
		print chr(hex("$btx[$slg+19]"));
		print chr(hex("00"));
	}
	else{
		print chr(hex("21"));
		print chr(hex("f9"));
		print chr(hex("04"));
		print chr(hex("00"));
		print chr(hex("00"));
		print chr(hex("00"));
		print chr(hex("00"));
		print chr(hex("00"));
	}

	#ib
	print chr(hex("2c"));

	$limgax = sprintf("%lx",(($ctc - 1) * $ix));
	$limgfx = "0" x (4 - length($limgax)).$limgax;
	@limgx = (substr($limgfx,2,2),substr($limgfx,0,2));
	foreach $limgx(@limgx){
		print chr(hex("$limgx"));
	}

	if (($btx[$slg+13]) == 21){
		$hb1 = $cb{"b".substr($btx[$slg+30],0,1)};
		$hb2 = $cb{"b".substr($btx[$slg+30],1,1)};
	}
	else{
		$hb1 = $cb{"b".substr($btx[$slg+22],0,1)};
		$hb2 = $cb{"b".substr($btx[$slg+22],1,1)};
	}
	$slct = $hb1.$hb2;

	if($check == 0 || substr($slct,0,1) == 1){
		$btx = @btx - 1;
		$lmt = 0;
		if (($btx[$slg+13]) == 21){
			$lp = 24;
		}
		else{
			$lp = 16;
		}
		$ri = $slg + $lp;
		while ($lmt == 0){
			if($btx ne $ri){
				print chr(hex("$btx[$ri]"));
			}
			else{
				$lmt++;
			}
			$ri++;
		}
	}
	else{
		if (($btx[$slg+13]) == 21){
			print chr(hex("$btx[$slg+24]"));
			print chr(hex("$btx[$slg+25]"));
			print chr(hex("$btx[$slg+26]"));
			print chr(hex("$btx[$slg+27]"));
			print chr(hex("$btx[$slg+28]"));
			print chr(hex("$btx[$slg+29]"));
		}
		else{
			print chr(hex("$btx[$slg+16]"));
			print chr(hex("$btx[$slg+17]"));
			print chr(hex("$btx[$slg+18]"));
			print chr(hex("$btx[$slg+19]"));
			print chr(hex("$btx[$slg+20]"));
			print chr(hex("$btx[$slg+21]"));
		}
		$slct = "1" . substr($slct,1,4) . "$osgct";
		$slct = $ltb{"b".substr($slct,0,4)}.$ltb{"b".substr($slct,4,4)};
		print chr(hex("$slct"));
		$egct =~ s/(.{2})/$1,/g;
		@egct = split(/,/,$egct);
		foreach $egct(@egct){
			print chr(hex("$egct"));
		}
		$btx = @btx - 1;
		$lmt = 0;
		if (($btx[$slg+13]) == 21){
			$lp = 24;
		}
		else{
			$lp = 16;
		}
		$slc = $stb{"b".substr($hb2,1,3)};
		$sll = 3*(2**($slc+1));
		$ri = $slg + $lp + 7;
		while ($lmt == 0){
			if($btx ne $ri){
				print chr(hex("$btx[$ri]"));
			}
			else{
				$lmt++;
			}
			$ri++;
		}
	}
}

print chr(hex("3b"));
exit 0;

