<?php session_start();
include ("./api.class.php");
if (isset ( $_POST ["anmelden"] )) {
	$API = new MediaDBAPI ();
	if ($API->APIinit($_POST["api-key"])) {
		if($API->webapp){
			// Session Start bzw. Cookie setzen!!
			$_SESSION['api-key'] = $_POST["api-key"];
			if(isset($_POST['angemeldetbleiben'])){
				setcookie ("api-key", $_POST["api-key"], time() + 3*24*3600);
				setcookie ("api-secret", md5($_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR']), time() + 3*24*3600);
			}
		}else {
			$FehlerArray = $API->error ( 1007 );
			$ErrorBox = '<div class="alert alert-danger">' . PHP_EOL . '<strong>Fehler ' . $FehlerArray ["FehlerID"] . '!</strong> ' . $FehlerArray ["FehlerText"] . PHP_EOL . '</div>' . PHP_EOL;
		}
		
	} else {
		$FehlerArray = $API->error ( 1002 );
		$ErrorBox = '<div class="alert alert-danger">' . PHP_EOL . '<strong>Fehler ' . $FehlerArray ["FehlerID"] . '!</strong> ' . $FehlerArray ["FehlerText"] . PHP_EOL . '</div>' . PHP_EOL;
	}
}
if(isset($_SESSION['api-key']) OR (isset($_COOKIE['api-key']) AND isset($_COOKIE['api-secret']))){
	$dologin = false;
	if(isset($_SESSION['api-key'])){ // Login aus Session verhanden?
		$dologin = true;
	}
	// Login in Cookie vorhanden? 
	else if((md5($_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR']) == $_COOKIE['api-secret']) AND isset($_COOKIE['api-key'])){
		$dologin = true;
		$_SESSION['api-key'] = $_COOKIE['api-key'];
	}
	
	if($dologin){
		$API = new MediaDBAPI ();
		if ($API->APIinit($_SESSION['api-key'])) {
			header('location: webapp/');
		}
	}
}
?><!DOCTYPE html>
<html lang="de">
<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
	<meta name="description" content="MediaDB Webfrontend">
	<meta name="author" content="">
	<title>MediaDB-Anmeldung</title>
	<link href="webapp/css/bootstrap.css" rel="stylesheet">
	
	<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
	<link href="files/ie10-viewport-bug-workaround.css" rel="stylesheet">
	<!-- Custom styles for this template -->
	<link href="files/starter-template.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
	<!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
	<nav class="navbar navbar-inverse navbar-fixed-top">
		<div class="container">
			<div class="navbar-header">
				<button type="button" class="navbar-toggle collapsed"
					data-toggle="collapse" data-target="#navbar" aria-expanded="false"
					aria-controls="navbar">
					<span class="sr-only">Toggle navigation</span> <span
						class="icon-bar"></span> <span class="icon-bar"></span> <span
						class="icon-bar"></span>
				</button>
				<a class="navbar-brand" href="#">MediaDB</a>
			</div>
			<div id="navbar" class="collapse navbar-collapse">
				<ul class="nav navbar-nav">
					<li <?php echo (!isset($_GET["action"])?'class="active"':'')?>><a href="index.php">Anmeldung</a></li>
					<li <?php echo (($_GET["action"] == "app")?'class="active"':'')?>><a href="index.php?action=app">App</a></li>
            		<!--	<li><a href="#contact">Contact</a></li>-->
				</ul>
			</div>
			<!--/.nav-collapse -->
		</div>
	</nav>

	<div class="container">
		<div class="starter-template">
			<?php if($_GET["action"] == "app")
				{
				?>
					<h1>MediaDB Viewer - Android App</h1>
					<h2>Aktuelle Version: <?php echo file_get_contents("app/Version.txt")?></h2>
					<a href="app/MediaDBViewer.apk">Download!</a><br>
					
					<pre><?php echo file_get_contents("app/Changelog.txt")?></pre>
					
				<?php }else if(isset($_GET["action"]))
				{
			?>
			Fehler!! 404
			<?php 
				} else {?>
			<h1>Anmeldung</h1>
			<?php echo $ErrorBox;?>
			
			<form role="form" method="post" action="">
				<div class="form-group">
					<label for="email">API-Schlüssel:</label> <input type="text"
						maxlength="10" class="form-control" id="api-key" name="api-key"
						<?php echo 'value="'.$_POST['api-key'].'"'; ?>>
				</div>
				<div class="checkbox">
					<label><input type="checkbox" name="angemeldetbleiben"
						<?php echo (isset($_POST['angemeldetbleiben'])?"checked":"");?>>
						Länger angemeldet bleiben (via Cookies)</label>
				</div>
				<button type="submit" class="btn btn-default" name="anmelden">Anmelden</button>
			</form>
			<?php }?>
		</div>
	</div>
	<!-- /.container -->

	<!-- Bootstrap core JavaScript
    ================================================== -->
	<!-- Placed at the end of the document so the pages load faster -->
	<script src="files/jquery.js" type="text/javascript"></script>
	<!--<script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>-->
	<script src="files/bootstrap.js" type="text/javascript"></script>
	<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
	<script src="files/ie10-viewport-bug-workaround.js"
		type="text/javascript"></script>


</body>
</html>