<?php
/*******************************************************************************\

File Name:	imgcounter.php

Description:	This module increments the visit count for a specified web page
		and outputs a 1 pixel x 1 pixel image of the specified color.

Usage:		In an HTML file, use:
		    <img src="imgcounter.php?filename=FILENAME&color=#HEXCOLOR">
		where
		    FILENAME - the desired filename without the .count extension
		    HEXCOLOR - 6-character hex color, e.g. ffffff.
		e.g.
		    <p><img src="imgcounter.php?filename=home&color=000000">

CAVEAT:		Always provide the FILENAME parameter.  At this time, this script
		does not provide a reliable default value for FILENAME.

\*******************************************************************************/

$filename = trim($_GET['filename']);
$color = (trim($_GET['color']) == "") ? "000000" : trim($_GET['color']);

//$log = fopen("imgcounter.log", "w");

//echo $filename . "<br>\n";
//echo $color . "<br>\n";
//fwrite($log, "filename arg: " $filename . "\n");
//fwrite($log, "color arg   : " $color . "\n");

$count = '0';
$file_last_mod = 0;

// Set up filename whether or not provided

if(strlen($filename)) {
    $filename .= ".count";
}
else {
    $fname_end = strrpos($script_name, ".");
    $fname_end = ($fname_end === false) ? strlen($script_name) : $fname_end - 1;
    $slash_pos = strrpos($script_name, "/");
    $fname_start = ($slash_pos === false) ? 0 : $slash_pos + 1;
    $script_name = (!strlen($filename)) ? $HTTP_SERVER_VARS['SCRIPT_NAME'] : $filename;
    $filename = substr($script_name, $fname_start, $fname_end - $slash_pos) . ".count";
}

//echo $filename . "<br>\n";
//fwrite($log, $filename . "\n");

// open and read file if it exists

if (file_exists($filename)) {
    $file_last_mod = filemtime($filename);
    $count = file_get_contents($filename);
}

// increment count

$count += 1;
//echo $count;	// for testing only
//fwrite($log, $count . "visits\n");

// reopen file and write new count

$handle = fopen($filename, "w");
fwrite($handle, $count);
fclose($handle);

//fwrite($log, "Creating image...\n\n");

// Generate a 1 x 1 pixel image

$im = @imagecreate(1, 1)
    or die("<!-- Cannot Initialize new GD image stream -->");

// Set up color if not provided

if (!strlen(trim($color))) $color = "000000";

// Set image color

$red   = 0 + base_convert(substr($color, 0, 2), 16, 8);
$green = 0 + base_convert(substr($color, 2, 2), 16, 8);
$blue  = 0 + base_convert(substr($color, 4, 2), 16, 8);
$img_color = imagecolorallocate($im, $red, $green, $blue);

// Output the created image according to available GD image support

if (function_exists("imagegif")) {
    header("Content-type: image/gif");
    imagegif($im);
} elseif (function_exists("imagejpeg")) {
    header("Content-type: image/jpeg");
    imagejpeg($im);
} elseif (function_exists("imagepng")) {
    header("Content-type: image/png");
    imagepng($im);
} elseif (function_exists("imagewbmp")) {
    header("Content-type: image/vnd.wap.wbmp");
    imagewbmp($im);
} else {
    die("<!-- No image support in this PHP server -->");
}

// Free associated image memory

//imagedestroy($im);

//fclose($log);
?>
