<?php

include 'connection.php';

mysql_connect($server,$benutzername,$passwort);
mysql_select_db($datenbankname);
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");

header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');

if(!isset($_COOKIE[MemberName])) {
	// neues Cookie setzen
	if ($_REQUEST['password'] != '' && $_REQUEST['name']!='' && $_REQUEST['password']!='') {
		$lifetime = time() + (60*60*24);
		$_REQUEST['password'] = md5($_REQUEST['password']);
		$name = mysql_real_escape_string(strtolower($_REQUEST['name']));
		$_REQUEST['name'] = mysql_real_escape_string($_REQUEST['name']);
		$_REQUEST['game'] = mysql_real_escape_string($_REQUEST['game']);
		$sql="SELECT ID FROM members WHERE Game = '".$_REQUEST['game']."' AND lower(Name) = '$name' AND Password = '".$_REQUEST['password']."'";
		$result=mysql_query($sql);
		$anzahl=mysql_num_rows($result);
		$row=mysql_fetch_assoc($result);
		if ($anzahl == 1) {
			setcookie("MemberName", $_REQUEST['name'], $lifetime, '/');
			setcookie("MemberPassword", $_REQUEST['password'], $lifetime, '/');
			setcookie("MemberID", $row['ID'], $lifetime, '/');
			setcookie("Game", $_REQUEST['game'], $lifetime, '/');
			$_COOKIE['MemberName']=$_REQUEST['name'];
			$_COOKIE['MemberPassword']=$_REQUEST['password'];
			$_COOKIE['MemberID']=$row['ID'];
			$_COOKIE['Game']=$_REQUEST['game'];
			printLogin();
		}
		else {
			printError();
		}
	}
	else {
		printError();
	}
}
else {	// bereits drin
	if (!get_magic_quotes_gpc()) {
		$_COOKIE['MemberName']=addslashes($_COOKIE['MemberName']);
		$_COOKIE['MemberID']=addslashes($_COOKIE['MemberID']);
		$_COOKIE['MemberPassword']=addslashes($_COOKIE['MemberPassword']);
		$_COOKIE['Game']=addslashes($_COOKIE['Game']);
	}
	$sql="SELECT * FROM members WHERE Game = '".$_COOKIE['Game']."' AND Name = '".$_COOKIE['MemberName']."' AND ID = ".$_COOKIE['MemberID']." AND Password = '".$_COOKIE['MemberPassword']."'";
	$result=mysql_query($sql);
	$anzahl=mysql_num_rows($result);
	if ($anzahl == 1) {
		printLogin();
	}
	else {
		printError();
	}
}
mysql_close();

function printLogin()
{

	// Hole Texte
	$texts=mysql_query("SELECT * FROM texts WHERE Game='".$_COOKIE['Game']."'");
	while ($rowTexts=mysql_fetch_assoc($texts)) {
		$text[$rowTexts['ID']] = $rowTexts[Text];
	}
	
	// Online Counter
	$timeNow = date("Y-m-d H:i:s",time());
	mysql_query("UPDATE members SET
		     			OnlineSince = '$timeNow'
		      			WHERE ID = '".$_COOKIE['MemberID']."' LIMIT 1");
	$time10 = date("Y-m-d H:i:s",time()-60*10); // Z‰hle alle mit 10 Minuten seit dem letzten Klick
	$total = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM members WHERE OnlineSince >= '$time10'"));
	$onlineCounter = $total[0];


	echo "
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">
<html>
<head>

	<link type=\"text/css\" href=\"../".$_COOKIE['Game']."/images/design.css\" rel=\"stylesheet\" media=\"screen\"/>
	<title>$text[3]</title>
	<link rel=\"shortcut icon\" href=\"favicon.ico\" type=\"image/vnd.microsoft.icon\"/>
	<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"/>
	<script type=\"text/javascript\" src=\"../".$_COOKIE['Game']."/images/jquery-1.7.2.min.js\"></script>
	<script type=\"text/javascript\">
  $(document).ready(function()
  {
    $(\"h2\").toggle
	(
		function()
		{
			$(this).next().next().next().slideDown();
		},
		function()
		{
			$(this).next().next().next().slideUp();		
		}
	);
	$(\".tipgroup\").click(function(e)
	{
		e.preventDefault();
	});
  });
</script>
	";

	if ($_REQUEST['refresh'] == "true") {
		$metaRefresh = "<meta http-equiv=\"refresh\" content=\"300\";URL=frame.php?link=".$_REQUEST['link']."\">"; // Chatbox alle 5 Minuten automatisch refreshen.
	}

	?>

<script> 
<!-- 
var Serverzeit = new Date(); 

function UhrzeitAnzeigen() 
{ 
    if(!document.all && !document.getElementById) 
    { 
        return; 
    } 

    var Stunden = Serverzeit.getHours(); 
    var Minuten = Serverzeit.getMinutes(); 
    var Sekunden = Serverzeit.getSeconds(); 
    Serverzeit.setSeconds(Sekunden+1); 

    if(Stunden <= 9) 
    { 
        Stunden = "0" + Stunden; 
    } 

    if(Minuten <= 9) 
    { 
        Minuten = "0" + Minuten; 
    } 

    if(Sekunden <= 9) 
    { 
        Sekunden = "0" + Sekunden; 
    } 

    Uhrzeitanzeige = Stunden + ":" + Minuten + ":" + Sekunden; 

    if(document.getElementById) 
    { 
        document.getElementById("Uhrzeit").innerHTML = Uhrzeitanzeige 
    } 
    else if(document.all) 
    { 
        Uhrzeit.innerHTML = Uhrzeitanzeige; 
    } 

    setTimeout("UhrzeitAnzeigen()", 1000); 
} 
//--> 
</script>

<?php echo"
	
</head>

<body onLoad=\"UhrzeitAnzeigen();\">
	
	<div id=\"seiten_container\">
	<div id=\"titelbild\"></div>
	<div id=\"menu_container\">	
	<div id=\"menu\">
			<br><br>
			<ul>";
	
$sql="SELECT * FROM links WHERE Game='".$_COOKIE['Game']."' AND Menu = 'loggedIn' AND InUse = 'y' AND Icon != 'statistic.png' ORDER BY Reihenfolge ASC";
$result=mysql_query($sql);
while ($row=mysql_fetch_assoc($result)) {
	if ($row['Linkziel']==="goalkick_admin/index.php" && !isAdmin()) {
		continue;
	}

	if ($row['Intern']=='y') {
		if ($row['Icon']!='') {
			echo "<li><a class=\"important-menu\" href=\"".$row['Linkziel']."\">";
		}
		else
		{
			echo "<li><a href=\"".$row['Linkziel']."\">";
		}
	}
	else {
		echo "<li><a href=\"frame.php?link=extern&ziel=".$row['Linkziel']."\">";
	}
	if ($row['Icon']!='') {
		echo "<img src=\"../".$_COOKIE['Game']."/images/icons/".$row['Icon']."\" border=\"0\">&nbsp;";
	}
	echo $row['Linkname']."</a></li>";
}

echo "</ul>
			<br>
		</div>
		<div id=\"menu\">
			<h1>Statistiken</h1>
			<ul>";
	
$sql="SELECT * FROM links WHERE Game='".$_COOKIE['Game']."' AND Menu = 'loggedIn' AND InUse = 'y' AND Icon = 'statistic.png' ORDER BY Reihenfolge ASC";
$result=mysql_query($sql);
while ($row=mysql_fetch_assoc($result)) {
	echo "<li><a href=\"".$row['Linkziel']."\">";
	if ($row['Icon']!='') {
		echo "<img src=\"../".$_COOKIE['Game']."/images/icons/".$row['Icon']."\" border=\"0\">&nbsp;";
	}
	echo $row['Linkname']."</a></li>";
}

echo "</ul>	
			<p>			
		</div>
		</div>
		<div id=\"inhalt_container\">	
		<div id=\"mittellinie\">	
			Willkommen ".$_COOKIE['MemberName']."!&nbsp;&nbsp;&nbsp;<span id=\"Uhrzeit\"></span>&nbsp;".
			#<a href=\"frame.php?link=rules\">&nbsp;Regeln&nbsp;</a>
			"<a href=\"../".$_COOKIE['Game']."/index.php?link=login&mode=out\">&nbsp;Logout&nbsp;</a>&nbsp;&nbsp;Spieler online: $onlineCounter		
		</div>
				
		<div id=\"inhalt\">";

include '../'.$_COOKIE['Game'].'/configuration/links.php';
if ($_REQUEST['link'] == '') {
	include 'dashboard.php';
}
elseif ($_REQUEST['link'] == 'extern') {
	echo "<iframe marginheight=0 marginwidth=0 frameborder=0 src=\"".$_REQUEST['ziel']."\" width=\"100%\" height=\"500\">
  							<p>Fehler: Ihr Browser kann leider keine eingebetteten Frames anzeigen.</p>
								</iframe>";
}
elseif (in_array($_REQUEST['link'],$permittedLoggedIn)) {
	$link = $_REQUEST['link'].".php"; include $link;
}
else { echo "Fehler. Der Link zu dieser php-Datei ist nicht zugelassen. Falls du der Admin dieser Seite bist, kannst du in goalkick_main/links.php den Dateinamen zu den sicheren Dateien hinzuf¸gen.";
}

echo "		</div>
				</div>
		 		<div id=\"fusszeile\">
					<a href=\"http://www.goalkick.ch\" target=\"new\">&copy; Goalkick 2004 - 2012</a>
				</div>
			</div>
		</body>
		</html>";

}

function isAdmin() {
	$sql="SELECT members.ID FROM members JOIN games ON (members.ID = games.Administrator) WHERE members.Game = '".$_COOKIE['Game']."' AND games.Game = '".$_COOKIE['Game']."' AND members.Name = '".$_COOKIE['MemberName']."' AND members.ID = $_COOKIE[MemberID] AND members.Password = '".$_COOKIE['MemberPassword']."'";
	$result=mysql_query($sql);
	$anzahl=mysql_num_rows($result);
	if ($anzahl == 1) {
		return true;
	}
	else {
		return false;
	}
}

function printError()
{
	include "error.php";
}

?>