<?php

class MotoIcon
{
	var $mysql = array();
	var $info = array();
	var $link;
	function __construct()
	{
		$this->mysql['host'] = 'localhost';
		$this->mysql['user'] = 'motoicon';
		$this->mysql['database'] = 'motoicon';
		$this->mysql['password'] = 'thTjBrKrVY7TFqZ4';

		$this->info['requesttime'] = time();
		$this->info['useragent'] = $_SERVER['HTTP_USER_AGENT'];
		$this->info['clientip'] = $_SERVER['REMOTE_ADDR'];
		if(isset($_SERVER['HTTP_REFERER']))
		{
			$this->info['referer'] = $_SERVER['HTTP_REFERER'];
		}
		else
		{
		    $this->info['referer'] = '-';
		}
		
		$this->connectDb();
		try
		{
			$this->serve();	
		}
		catch(Exception $e)
		{
			//TODO
		}
	}

	function connectDb()
	{
		$this->link = mysql_connect($this->mysql['host'],$this->mysql['user'],$this->mysql['password']);
		if (!$this->link) {
			die('Could not connect: ' . mysql_error());
		}
	}

	function serve()
	{
		if($this->link)
		{
			date_default_timezone_set('Europe/Amsterdam');
			if(date("d-m") == '08-07' || date("m-Y") == '07-2008')
			{
				$sql = 'INSERT INTO
							hits
						(special, requesttime, clientip, referer, useragent)
					VALUES
					(
					\'yoeri\',
					'.$this->info['requesttime'].',
					"'.mysql_escape_string(htmlspecialchars($this->info['clientip'])).'",
					"'.mysql_escape_string(htmlspecialchars($this->info['referer'])).'",
					"'.mysql_escape_string(htmlspecialchars($this->info['useragent'])).'"
					)';
				
				$icon = "/home/hylke/public_html/GoT/animated-yoeri-icon.gif";
				$this->serveIcon($icon,'gif');
			}
			else
			{
				$sql = 'INSERT INTO
							hits
						(requesttime, clientip, referer, useragent)
					VALUES
					(
					'.$this->info['requesttime'].',
					"'.mysql_escape_string(htmlspecialchars($this->info['clientip'])).'",
					"'.mysql_escape_string(htmlspecialchars($this->info['referer'])).'",
					"'.mysql_escape_string(htmlspecialchars($this->info['useragent'])).'"
					)';
				$icon = '/home/hylke/public_html/GoT/moto-icon.jpg';
				$this->serveIcon($icon,'jpg');
			}
			mysql_select_db($this->mysql['database']);
			$res = mysql_query($sql);

		
		}
		else
		{
			throw new Exception("No database");
		}
	}

	function serveIcon($icon, $type)
	{
		header("Cache-Control: no-store, no-cache, must-revalidate");

		if($type == 'jpg')
		{
			header("Content-Type: image/jpeg");
		
		}
		else if($type == 'gif')
		{
			header('Content-Type: image/gif');
		}		
		header("Content-Length: " . filesize($icon));
		$file = fopen($icon,'rb');
		fpassthru($file);
	}
}

new MotoIcon();
?>
