<?php

function fnWriteAgent($param)
{
	if(!file_exists('lib/log/agents.txt'))
	{
		$fp = fopen('lib/log/agents.txt', 'w');
	}
	else 
	{
		$fp = fopen('lib/log/agents.txt', 'a');
	}
	if($param)
	{
		fputs($fp, $_SERVER['HTTP_USER_AGENT']."\n");
	}
	else 
	{
		fputs($fp, "*** BLOCKED *** ".$_SERVER['HTTP_USER_AGENT']."\n");
	}
	fclose($fp);
}

function fnDetectAgent($part)
{
	// 0 pozadia
	// 1 melodie
	// 2 hry
	// 3 videa
	
	$blocked = array
	(
		0 => true,
		1 => true,
		2 => true,
		3 => true,
	);
	
	if(!$blocked[$part])
	{
		$result = true;
	}
	else 
	{
		if(eregi('Mozilla', $_SERVER['HTTP_USER_AGENT']) || eregi('opera', $_SERVER['HTTP_USER_AGENT']))
		{
			$result = false;
		}
		else 
		{
			$result = true;
		}
	}
	fnWriteAgent($result);
	return $result;
}

function fnWriteLog($type, $message)
{
	global $link;
	$filename="lib/log/".$type;
	if(!file_exists($filename))
	{
		$fp=fOpen($filename, "w");
		$time=date("d.m.Y H:i:s");
		fputs($fp, $time.": ".$message."\n");
		fClose($fp);
		chmod($filename, 0204);
	}
	else 
	{
		//chmod($filename, 0666);
		$fp=fOpen($filename, "a");
		$time=date("d.m.Y H:i:s");
		fputs($fp, $time.": ".$message."\n");
		fClose($fp);
		//chmod($filename, 0600);
	}
	$message=addslashes($message);
	$query="INSERT INTO mobile_logs (type, time, message) VALUES ('".$type."', '".$time."', '".$message."')";
	mysql_query($query, $link);	
}

function fnAnketa()
{	
	global $link;
	if(!session_is_registered("hlasoval_v_ankete"))
	{
		$query="SELECT * FROM mobile_anketa WHERE visible=1 ORDER by zobrazeni ASC";
		$result=mysql_query($query,$link);
		if($result) 
		{
			$row=mysql_fetch_array($result);
			$anketa=$row["id"];
			$zob=$row["zobrazeni"];
			$zob+=1;
			$query="UPDATE mobile_anketa SET zobrazeni=".$zob." WHERE id=".$anketa." limit 1";
			$result=mysql_query($query, $link);
			session_register("anketa_id");
			$_SESSION["anketa_id"]=$anketa;
			$mobile_anketa_string="mobile_anketa_"+$anketa;
			$anketa_cookie="mobile_hlas_anketa"+$anketa;
			if (!isset($_COOKIE[$anketa_cookie]) && !session_is_registered($mobile_anketa_string))
			{	
				$query="SELECT * FROM mobile_anketa WHERE id=$anketa";
				$result=mysql_query($query, $link);
				if($result) 
				{
					$row=mysql_fetch_array($result);
					$spolu=0;
					for($i=1;$i<=10;$i++)
					{
						$spolu+=$row["poc".$i];
					}	
					echo "<span class=info1><b>".$row["otazka"]."</b></span><br><br>";
					$perc=array();
					for($i=1; $i<=10; $i++)
					{
						if($row["odp".$i]<>''){
						echo "<a href='aw.php?odpoved=poc".$i."' class=menu2>".$row["odp".$i]."</a> (".$row["poc".$i].")<br>";
						if($spolu==0){$perc[$i]=0;} else{
						$perc[$i]=round($row["poc".$i]*100/$spolu);}
						echo "<img src='images/pas_ank.gif' height=10 width=".$perc[$i]." alt=''><br>";}
					}
					echo " Spolu:".$spolu."<br>Od ".$row["od"];
				}
			}
		}
	}
	else 
	{	
		if(session_is_registered('hlasoval_v_ankete')) $query="SELECT * FROM mobile_anketa WHERE id=".$_SESSION['hlasoval_v_ankete'];
		else $query="SELECT * FROM mobile_anketa WHERE visible=1 ORDER by zobrazeni ASC";
		$result=mysql_query($query, $link);
		if($result)
		{
			$row=mysql_fetch_array($result);
			$anketa=$row["id"];
			$spolu=0;
			for($i=1;$i<=10;$i++)
			{
				$spolu+=$row["poc".$i];
			}
			echo "<span class=info1><b>".$row["otazka"]."</b></span><br><br>";
			$perc=array();
			for($i=1; $i<=10; $i++)
			{
				if($row["odp".$i]<>''){
				echo "<span class=menu2>".$row["odp".$i]."</span> (".$row["poc".$i].")<br>";
				if($spolu==0){$perc[$i]=0;} else{
				$perc[$i]=round($row["poc".$i]*100/$spolu);}
				echo "<img src='images/pas_ank.gif' height=10 width=".$perc[$i]." alt=''><br>";}
			}
		}	echo " Spolu:".$spolu."<br>Od ".$row["od"];
	}
	session_register("anketa_id");
	$_SESSION["anketa_id"]=$anketa;
	echo "<Br><br>";
	if(session_is_registered("mobile_voted")){echo"<span class=news_date><b>Už si hlasoval!</b></span><br><br>";}
}
	
?>