<?php
// Error reporting:
error_reporting(E_ALL^E_NOTICE);

if(!$_GET['file']) error('Missing parameter!');
if($_GET['file']{0}=='.') error('Invalid file!');
if(is_array($_GET['file'])) error('Invalid file!');

if(file_exists($_GET['file']))
{
	/* If the visitor is not a search engine, count the download: */
	if(!is_bot()){

		if(!file_exists($_GET['file']."-numdownloads.txt")){
			$filehandlew = fopen($_GET['file']."-numdownloads.txt", 'w+');
			fclose($filehandlew);
		}
		
		$filehandle = fopen($_GET['file']."-numdownloads.txt", 'r+');
		
		$numberofdownloads = fgets($filehandle);
		if(!(is_numeric($numberofdownloads)))$numberofdownloads=0;
		
		$numberofdownloads++;
		
		//set pointer back to beginning
		rewind($filehandle);

		//delete everything in the file.
		ftruncate($filehandle, filesize($file_name));

		//write the number of downloads back
		fwrite($filehandle, $numberofdownloads);
		
		fclose($filehandle);
	}

	header("Location: http://www.tyrovc.com/barbarianandnt/".$_GET['file']);
	exit;
}
else error("This file does not exist!");

/* Helper functions: */

function error($str)
{
	die($str);
}

function is_bot()
{
	/* This function will check whether the visitor is a search engine robot */

	$botlist = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi",
	"looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory",
	"Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot",
	"crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp",
	"msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz",
	"Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot",
	"Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot",
	"Butterfly","Twitturls","Me.dium","Twiceler","bingbot","MJ12bot");

	foreach($botlist as $bot)
	{
		if(strpos($_SERVER['HTTP_USER_AGENT'],$bot)!==false)
		return true;	// Is a bot
	}

	return false;	// Not a bot
}
?>