2019-08-21 20:56:46 +00:00
|
|
|
<?php
|
|
|
|
|
2019-09-08 22:41:52 +00:00
|
|
|
$has_error = false;
|
|
|
|
$error_message = null;
|
2019-09-08 10:45:48 +00:00
|
|
|
|
2019-08-21 20:56:46 +00:00
|
|
|
if (isset($_POST["submitted"]) && !isset($_SESSION["user_id"])) {
|
2019-09-08 22:41:52 +00:00
|
|
|
$logging_in_user = new LoggingInUser($_POST);
|
|
|
|
try {
|
|
|
|
$logging_in_user->makeVerifications();
|
|
|
|
$logging_in_user->login();
|
|
|
|
} catch (AssertionError $e) {
|
|
|
|
$has_error = true;
|
|
|
|
$error_message = $e->getMessage();
|
|
|
|
}
|
2019-08-21 20:56:46 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 14:39:57 +00:00
|
|
|
if (isset($_POST["forgotten_password"]) && !isset($_SESSION["user_id"])) {
|
2019-09-08 22:41:52 +00:00
|
|
|
$recuperate_account = new RecuperateAccount($_POST);
|
|
|
|
try {
|
|
|
|
$recuperate_account->makeVerifications();
|
|
|
|
$recuperate_account->recuperateAccount();
|
|
|
|
} catch (AssertionError $e) {
|
|
|
|
$has_error = true;
|
|
|
|
$error_message = $e->getMessage();
|
|
|
|
}
|
2019-09-02 14:39:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_GET["reset_password"]) && isset($_GET["token"]) && !isset($_SESSION["user_id"])) {
|
2019-09-08 22:41:52 +00:00
|
|
|
$reset_password = new ResetPassword($_GET, $_POST);
|
|
|
|
try {
|
|
|
|
$reset_password->makeVerifications();
|
|
|
|
if (isset($_POST["password"]))
|
|
|
|
$reset_password->resetPassword();
|
|
|
|
} catch (AssertionError $e) {
|
|
|
|
$has_error = true;
|
|
|
|
$error_message = $e->getMessage();
|
|
|
|
}
|
2019-09-02 14:39:57 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 22:41:52 +00:00
|
|
|
if (isset($_GET["confirmation-mail"]) && !isset($_SESSION["user_id"]))
|
|
|
|
sendConfirmEmail();
|
2019-09-02 15:29:27 +00:00
|
|
|
|
2019-09-08 22:41:52 +00:00
|
|
|
class LoggingInUser
|
|
|
|
{
|
|
|
|
public $email;
|
|
|
|
/** @var User $user */
|
|
|
|
public $user;
|
|
|
|
private $password;
|
2019-08-21 20:56:46 +00:00
|
|
|
|
2019-09-08 22:41:52 +00:00
|
|
|
public function __construct($data)
|
|
|
|
{
|
|
|
|
foreach ($data as $key => $value)
|
|
|
|
$this->$key = htmlspecialchars($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function makeVerifications()
|
|
|
|
{
|
|
|
|
global $URL_BASE;
|
|
|
|
|
|
|
|
ensure(filter_var($this->email, FILTER_VALIDATE_EMAIL), "L'adresse email est invalide.");
|
|
|
|
$this->user = User::fromEmail($this->email);
|
|
|
|
ensure($this->user != null, "Le compte n'existe pas.");
|
|
|
|
ensure($this->user->checkPassword($this->password), "Le mot de passe est incorrect.");
|
|
|
|
if ($this->user->getConfirmEmailToken() != null) {
|
|
|
|
$_SESSION["confirm_email"] = $this->email;
|
|
|
|
throw new AssertionError("L'adresse mail n'a pas été validée. Veuillez vérifier votre boîte mail (surtout vos spams). "
|
|
|
|
. "<a href=\"$URL_BASE/connexion/confirmation-mail\">Cliquez ici pour renvoyer le mail de confirmation</a>.");
|
|
|
|
}
|
|
|
|
}
|
2019-08-21 20:56:46 +00:00
|
|
|
|
2019-09-08 22:41:52 +00:00
|
|
|
public function login()
|
|
|
|
{
|
|
|
|
$_SESSION["user_id"] = $this->user->getId();
|
|
|
|
loadUserValues();
|
|
|
|
}
|
|
|
|
}
|
2019-08-21 20:56:46 +00:00
|
|
|
|
2019-09-08 22:41:52 +00:00
|
|
|
class RecuperateAccount
|
|
|
|
{
|
|
|
|
public $email;
|
|
|
|
/** @var User $user */
|
|
|
|
public $user;
|
2019-08-21 20:56:46 +00:00
|
|
|
|
2019-09-08 22:41:52 +00:00
|
|
|
public function __construct($data)
|
|
|
|
{
|
|
|
|
foreach ($data as $key => $value)
|
|
|
|
$this->$key = htmlspecialchars($value);
|
2019-09-02 15:29:27 +00:00
|
|
|
}
|
2019-08-21 20:56:46 +00:00
|
|
|
|
2019-09-08 22:41:52 +00:00
|
|
|
public function makeVerifications()
|
|
|
|
{
|
|
|
|
ensure(filter_var($this->email, FILTER_VALIDATE_EMAIL), "L'adresse email est invalide.");
|
|
|
|
$this->user = User::fromEmail($this->email);
|
|
|
|
ensure($this->user != null, "Le compte n'existe pas.");
|
|
|
|
}
|
2019-08-21 20:56:46 +00:00
|
|
|
|
2019-09-08 22:41:52 +00:00
|
|
|
public function recuperateAccount()
|
|
|
|
{
|
|
|
|
$token = genRandomPhrase(64);
|
|
|
|
$this->user->setForgottenPasswordToken($token);
|
|
|
|
Mailer::sendForgottenPasswordProcedureMail($this->user);
|
|
|
|
}
|
2019-08-21 20:56:46 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 22:41:52 +00:00
|
|
|
class ResetPassword
|
|
|
|
{
|
|
|
|
public $token;
|
|
|
|
/** @var User $user */
|
|
|
|
public $user;
|
|
|
|
private $password;
|
|
|
|
private $confirm_password;
|
|
|
|
|
|
|
|
public function __construct($data, $data2)
|
|
|
|
{
|
|
|
|
foreach ($data as $key => $value)
|
|
|
|
$this->$key = htmlspecialchars($value);
|
|
|
|
foreach ($data2 as $key => $value)
|
|
|
|
$this->$key = htmlspecialchars($value);
|
|
|
|
}
|
2019-09-06 23:33:05 +00:00
|
|
|
|
2019-09-08 22:41:52 +00:00
|
|
|
public function makeVerifications()
|
|
|
|
{
|
|
|
|
global $DB;
|
|
|
|
$data = $DB->query("SELECT `id` FROM `users` WHERE `forgotten_password` = '" . $this->token . "';")->fetch();
|
|
|
|
ensure($data !== false, "Il n'y a pas de compte à récupérer avec ce jeton.");
|
|
|
|
$this->user = User::fromId($data["id"]);
|
2019-09-08 10:45:48 +00:00
|
|
|
|
2019-09-08 22:41:52 +00:00
|
|
|
if ($this->password == null)
|
|
|
|
return;
|
2019-09-02 14:39:57 +00:00
|
|
|
|
2019-09-08 22:41:52 +00:00
|
|
|
ensure($this->password == $this->confirm_password, "Les deux mots de passe sont différents.");
|
|
|
|
ensure(strlen($this->password) >= 8, "Le mot de passe doit comporter au moins 8 caractères.");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function resetPassword()
|
|
|
|
{
|
|
|
|
$this->user->setForgottenPasswordToken(null);
|
|
|
|
$this->user->setPassword($this->password);
|
|
|
|
|
|
|
|
Mailer::sendChangePasswordMail($this->user);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-02 14:39:57 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 22:41:52 +00:00
|
|
|
function sendConfirmEmail()
|
|
|
|
{
|
2019-09-08 10:45:48 +00:00
|
|
|
global $URL_BASE;
|
2019-09-08 22:41:52 +00:00
|
|
|
|
|
|
|
$email = htmlspecialchars($_SESSION["confirm_email"]);
|
|
|
|
|
|
|
|
if (!isset($email)) {
|
|
|
|
header("Location: $URL_BASE/connexion");
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = User::fromEmail($email);
|
|
|
|
|
|
|
|
if ($user === null) {
|
|
|
|
unset($_SESSION["confirm_email"]);
|
2019-09-02 15:29:27 +00:00
|
|
|
header("Location: $URL_BASE/connexion");
|
|
|
|
exit();
|
2019-09-08 22:41:52 +00:00
|
|
|
}
|
2019-09-08 10:45:48 +00:00
|
|
|
|
|
|
|
Mailer::sendConfirmEmail($user);
|
2019-09-08 22:41:52 +00:00
|
|
|
|
|
|
|
return false;
|
2019-09-02 15:29:27 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 11:42:36 +00:00
|
|
|
require_once "server_files/views/connexion.php";
|