<?php

include_once './includes/authentification/Login.inc.php';
include_once './includes/utils/InputSanitizer.inc.php';
include_once './pages/PageInterface.inc.php';

/**
 * This class represent the login page of the site.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
class LoginPage implements PageInterface {

    private $title;

    private $content;

    public function  __construct() {

        if (key_exists("username", $_POST)) {

            $username = $_POST['username'];
            $password = $_POST['password'];

            $username = InputSanitizer::Sanitize($username);
            $password = InputSanitizer::Sanitize($password);

            $this->title = Login::authentificate($username, $password);
            $this->content = $this->title;

        } else if ((key_exists("type", $_GET)) && ($_GET['type'] == 'logout')) {
            Login::deauthentificate();

            $this->title = Localization::userLogout;

            $loginLabel = Localization::userLoginLink;

            $this->content = Localization::userLogoutMessage . "<br>";
            $this->content.= "<a href=./?page=Login>$loginLabel</a>";

        } else {

            $this->title = "Login page";

            $this->content = <<<FORM
<form name="form1" method="post" action="./?page=Login">
    <div>
    <label for="username">Username</label>
    <input name="username" type="text" id="username"><br>
    <label for="password">Password</label>
    <input name="password" type="password" id="password"><br>
    <input type="submit" name="Submit" value="Login">
    </div>
</form>
FORM;
        }
    }

    public function getTitle() {
        return $this->title;
    }

    public function getContent() {
        return $this->content;
    }

    public static function getStaticTitle() {
        return Localization::userLoginLink;
    }
}
?>
