<?php

function get_jin_base_dir($live_version)
{
	$parts = explode('/', dirname(__FILE__));
	$numOfParts = count($parts);
	unset($parts[$numOfParts - 1]);
	
	if ($live_version)
		unset($parts[$numOfParts - 2]);
	
	return join('/', $parts);
}

function get_db_config_filename()
{
	for ($i = 1; $i >= 0; $i--)
	{
		$directory = get_jin_base_dir($i) . '/kit.hostconf/config';
		$files = @scandir($directory);
		
		if (is_array($files))
			foreach ($files as $filename)
				if (is_integer(strpos($filename, 'hostconf_db.conf')))
					return "$directory/$filename";
	}
	
	echo "hostconf_db.conf not found<br>";
	
	return;
}

function connect_to_db()
{
	$filename = get_db_config_filename();
	$file = fopen($filename, 'r');
	$content = fread($file, filesize($filename));
	fclose($file);
	
	preg_match("|host=(\w*)|i", $content, $host);
	preg_match("|database=(\w*)|i", $content, $database);
	
	if (is_integer(strpos($filename, 'comp.pl')))
	{
		preg_match("|dbuser => \'(\w*)\'|i", $content, $dbuser);
		preg_match("|dbpass => \'(.*)\'|i", $content, $dbpass);
	}
	else
	{
		preg_match("|dbuser=(\w*)|i", $content, $dbuser);
		preg_match("|dbpass=(.*)|i", $content, $dbpass);
	}
	
	$db_context = mysql_connect($host[1], $dbuser[1], $dbpass[1]);
	mysql_select_db($database[1], $db_context);
	
	return;
}

function display()
{
	if (! isset($_GET['id']))
		return;
	
	$id = $_GET['id'] + 0;
	$ath = mysql_query("SELECT * FROM jin_backlink WHERE id='$id'");
	$user_link = mysql_fetch_assoc($ath);
	
	if ($user_link === false)
		return;
	
	// We keep in a database IP-address of the last visitor and link ID,  which was shown then.
	
	if ($_SERVER['REMOTE_ADDR'] == $user_link['last_ip'])
	{
		// If IP-address of the current visitor is exactly same that is stored in a database,
		// we simply show the old link,
		// points for such display aren't changed.

		$ath = mysql_query("SELECT * FROM jin_backlink WHERE id='$user_link[last_id]'");
		$displayed_link = mysql_fetch_assoc($ath);
	}
	else
	{
		$ath = mysql_query("SELECT MAX(rating) FROM jin_backlink WHERE id!='$id'");
		$rating = mysql_fetch_row($ath);
		
		if (isset($rating[0]))
		{
			$ath = mysql_query("SELECT * FROM jin_backlink WHERE id!='$id' AND rating='$rating[0]' ORDER BY RAND() LIMIT 1");
			$displayed_link = mysql_fetch_assoc($ath);
		
			if ($displayed_link !== false)
			{
				mysql_query("UPDATE jin_backlink SET rating=rating+1, last_id='$displayed_link[id]', last_ip='$_SERVER[REMOTE_ADDR]' WHERE id='$id'");
				mysql_query("UPDATE jin_backlink SET rating_self=rating_self+1, rating=rating-1 WHERE id='$displayed_link[id]'");
			}
		}
	}
	
	// Show user link, if nothing else is available (test mode)
	
	if (! isset($displayed_link) || $displayed_link === false)
		$displayed_link = $user_link;
		
	$code = strlen($displayed_link['image']) ?
		"<img border=\"0\" src=\"$displayed_link[image]\">" :
		"<b style=\"font-size:120%\">$displayed_link[title]</b><br>" . nl2br($displayed_link['descr']);
	
	echo "document.write('<a target=\"_blank\" style=\"text-decoration: none\" href=\"$displayed_link[url]\">$code</a>');";
	
	return;
}

connect_to_db();
display();
mysql_close();

?>
