<?php

Class User {

	private $username 	= "";
	private $userID		= NULL;
	private $password 	= "";
	private $email 		= "";
	private $loggedIn	= false;

	public function __construct($username) {
		if($username != NULL) {
			$this->username = $username;
		}

	}

	public function setLoggedIn($loggedIn) {
		$this->loggedIn = $loggedIn;
	}

	public function getLoggedIn() {
		return $this->loggedIn;
	}

	public function setUserID($userID) {
		$this->userID = $userID;
	}

	public function getUserID() {
		return $this->userID;
	}

	public function login($username, $password) {
		global $db;
		global $CONFIG;

		$this->username = $username;
		$this->password = $password;

		$user = $db->selectQuery("SELECT * FROM `users` WHERE `username` = '" . $username . "'");

		if(sizeof($user) > 1) {
			die('Something really went wrong.');
		}

		if(sizeof($user) < 1) {
			die('No such user.');
		}

		if($user[0]->password === md5($password)) {
			$_SESSION['loggedIn'] = true;
			$_SESSION['username'] = $this->username;
			$this->loggedIn = true;
			header("Location: " . $CONFIG['url']);
		} else {
			return false;
		}

	}

	public function loginByUsername($username) {
		global $db;

		$user = $db->selectQuery("SELECT * FROM `users` WHERE `username` = '" . $username . "'");

		if(sizeof($user) > 1) {
			die('Something really went wrong.');
		}

		if(sizeof($user) < 1) {
			die('No such user.');
		}

		$_SESSION['loggedIn'] = true;
		$this->userID = $user[0]->id;
		$this->loggedIn = true;
	}

	public function logout() {
		global $CONFIG;
		
		$this->username = "";
		$this->password = "";
		$this->email = "";

		$_SESSION['loggedIn'] = false;
		$this->loggedIn	= false;
		unset($_SESSION['username']);
		session_destroy();
		header("Location: " . $CONFIG['url']);
	}

}

?>
