<?php
	header ("Content-type: image/png");
	$rootPath = ""; require( $rootPath . "include/common.inc.php" );
	
	$num = 0; $numToday = 0; $counterId = getInt("id"); $canIncrease = false; $exists = false;
	
	/* if ip not in db use cookie */
	$sqlQuery = "SELECT COUNT(*) FROM counter_ip WHERE IP = '" . $_SERVER["REMOTE_ADDR"] . "' AND ID=" . $counterId . ";";
	if ( (int) $db->getValue( $sqlQuery ) > 0 )
	{
		$exists = true;
		$dt = @$_COOKIE["golitsyno_counter_date"];
		if ( $dt == "" )
		{
			$canIncrease = true;
		}
	}
	else
		$canIncrease = true;
	
#	echo $HTTP_COOKIE_VARS["golitsyno_counter_date"] . "<br>";
	
	if ( $canIncrease )
		$db->execute("UPDATE counter SET VISITOR=VISITOR+1, VISITOR_ALL=VISITOR_ALL+1 WHERE ID=" . $counterId . ";");
	
	$res = $db->getValues("SELECT VISITOR_ALL, VISITOR FROM counter WHERE ID=" . $counterId . ";");
	$num = (int) $res["VISITOR_ALL"];
	$numToday = (int) $res["VISITOR"];
	
	setcookie( "golitsyno_counter_date", date( mktime() ) );
#	setcookie( "golitsyno_counter_date", "" );
	if ( ! $exists ) $db->execute( "INSERT INTO counter_ip ( ID, IP, DT ) VALUES ( " . $counterId . ", '" . $_SERVER["REMOTE_ADDR"] . "', NOW() );" );
	
	$size = getimagesize ( "images/btn/0.gif" );
	$imBg = imagecreatefromgif( "images/btn/btn.gif" );
	
	$digits = array();
	$digits[0] = imagecreatefromgif( "images/btn/0.gif" );
	$digits[1] = imagecreatefromgif( "images/btn/1.gif" );
	$digits[2] = imagecreatefromgif( "images/btn/2.gif" );
	$digits[3] = imagecreatefromgif( "images/btn/3.gif" );
	$digits[4] = imagecreatefromgif( "images/btn/4.gif" );
	$digits[5] = imagecreatefromgif( "images/btn/5.gif" );
	$digits[6] = imagecreatefromgif( "images/btn/6.gif" );
	$digits[7] = imagecreatefromgif( "images/btn/7.gif" );
	$digits[8] = imagecreatefromgif( "images/btn/8.gif" );
	$digits[9] = imagecreatefromgif( "images/btn/9.gif" );
	
	/* Out of all of the visitors. */
	$im = imagecreate ( 88, 31 ); $x = 85 - (int) ( strlen( $num ) * $size[0] );
	for ( $i=0; $i < strlen( $num ); $i++ )
	{
		$digit = (int)substr( $num, $i, 1 );
		imagecopy ( $imBg, $digits[ $digit ], $x, 6, 0, 0, $size[0], $size[1] );
		$x = $x + $size[0];
	}
	
	/* Out of the visitors today. */
	$x = 85 - (int) ( strlen( $numToday ) * $size[0] );
	for ( $i=0; $i < strlen( $numToday ); $i++ )
	{
		$digit = (int)substr( $numToday, $i, 1 );
		imagecopy ( $imBg, $digits[ $digit ], $x, 18, 0, 0, $size[0], $size[1] );
		$x = $x + $size[0];
	}
	
	imagepng( $imBg );
	imagedestroy( $imBg );
?>