<?php
require_once("includes/SQLConnect.php");
require_once("includes/user_data.php");

if(isset($_POST['login'])) {

	$email = $_POST['email'];
	$password = $_POST['password'];
	
	if($email == "")
		$validation[] = 'No Username was entered.';
	if($password == "")
		$validation[] = 'No password was entered.';
	
	if(!isset($validation)){

		require_once('includes/classes/PasswordHash.php');
		
		$hasher = new PasswordHash(8, FALSE);
		$query = sprintf("SELECT ID,password FROM `Users` WHERE email='%s'",mysql_real_escape_string($email));
		
		$user = mysql_fetch_assoc(mysql_query($query));
		$ID = $user['ID'];
		$hash = $user['password'];

		if($hasher->CheckPassword($password, $hash)){
			require_once('includes/classes/CookieManager.php');
			
			$secretKey = 'fdasdKJKS7*32Mnbbhey$@kjhk888';
			$manager = new BigOrNot_CookieManager($secretKey);
			setcookie('ID', $ID, time() + 86400, "/");
			$value = $manager->setCookie('password', $hash, $ID);
			
			
			header("Location: http://".$_SERVER['SERVER_NAME']."/district.php?success=You are logged in.");
		}
		else
			$error = 'Either your username, or more likely, your password is incorrect.';
	}
	unset($t_hasher);
}
include_once("includes/header.php");

?>
	<form method="post" id="login" class="compact">
		<h2>Login</h2>

		<label for="email">Email:</label>
		<?php
		$email = "";
		if(isset($_POST['email']) && strlen($_POST['email'])>0)
			$email = $_POST['email'];
		elseif(isset($_GET['email']) && strlen($_GET['email'])>0)
			$email = $_GET['email'];
		?>
		<input type="text" id="email" name="email" size="30"  tabindex="1" alt="Enter your email address" value="<?=$email;?>" />
		<label for="password">Password:</label>
		<input type="password" id="password" name="password" size="20"  tabindex="2" alt="Enter your password" />

		<input type="submit" class="submit" name="login" value="Login">

	</form>
<?php
include_once("includes/footer.php");
?>
