2019-09-09 23:48:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if (!isset($_SESSION["user_id"]))
|
|
|
|
require_once "server_files/403.php";
|
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = $_SESSION["user"];
|
|
|
|
|
|
|
|
$has_error = false;
|
|
|
|
$error_message = null;
|
|
|
|
|
2019-09-24 21:44:38 +00:00
|
|
|
if (isset($_POST["update_account"])) {
|
2019-09-09 23:48:52 +00:00
|
|
|
$my_account = new MyAccount($_POST);
|
|
|
|
try {
|
|
|
|
$my_account->makeVerifications();
|
|
|
|
$my_account->updateAccount();
|
|
|
|
}
|
|
|
|
catch (AssertionError $e) {
|
|
|
|
$has_error = true;
|
|
|
|
$error_message = $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 21:44:38 +00:00
|
|
|
if (isset($_POST["update_password"])) {
|
2019-09-09 23:48:52 +00:00
|
|
|
$new_password = new NewPassword($_POST);
|
|
|
|
try {
|
|
|
|
$new_password->makeVerifications();
|
|
|
|
$new_password->updatePassword();
|
|
|
|
}
|
|
|
|
catch (AssertionError $e) {
|
|
|
|
$has_error = true;
|
|
|
|
$error_message = $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyAccount
|
|
|
|
{
|
|
|
|
public $email;
|
|
|
|
public $surname;
|
|
|
|
public $first_name;
|
|
|
|
public $school;
|
|
|
|
public $class;
|
|
|
|
public $description;
|
2019-09-18 22:31:53 +00:00
|
|
|
public $receive_animath_mails;
|
2019-09-16 22:04:45 +00:00
|
|
|
/** @var User */
|
2019-09-09 23:48:52 +00:00
|
|
|
private $user;
|
|
|
|
|
|
|
|
public function __construct($data)
|
|
|
|
{
|
|
|
|
foreach ($data as $key => $value)
|
|
|
|
$this->$key = htmlspecialchars($value);
|
|
|
|
|
|
|
|
$this->user = $_SESSION["user"];
|
|
|
|
|
2019-09-10 23:17:05 +00:00
|
|
|
$keys = ["email", "surname", "first_name", "school", "class", "description"];
|
2019-09-09 23:48:52 +00:00
|
|
|
|
|
|
|
if ($this->user->getRole() == Role::PARTICIPANT)
|
|
|
|
$this->class = SchoolClass::fromName($this->class);
|
|
|
|
|
|
|
|
foreach ($keys as $key)
|
|
|
|
$this->$key = $this->$key != null && $this->$key != "" ? $this->$key : $this->user->$key;
|
2019-09-24 21:44:38 +00:00
|
|
|
|
|
|
|
$this->receive_animath_mails = $this->receive_animath_mails == "on";
|
2019-09-09 23:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function makeVerifications()
|
|
|
|
{
|
|
|
|
ensure(filter_var($this->email, FILTER_VALIDATE_EMAIL), "L'adresse e-mail entrée est invalide.");
|
|
|
|
$this->email = strtolower($this->email);
|
|
|
|
ensure($this->email == $this->user->getEmail() || !userExists($this->email), "Un compte existe déjà avec cette adresse e-mail.");
|
2019-09-18 22:31:53 +00:00
|
|
|
$this->receive_animath_mails = $this->receive_animath_mails != false;
|
2019-09-09 23:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function updateAccount()
|
|
|
|
{
|
|
|
|
$this->user->setSurname($this->surname);
|
|
|
|
$this->user->setFirstName($this->first_name);
|
|
|
|
$this->user->setSchool($this->school);
|
|
|
|
$this->user->setClass($this->class);
|
|
|
|
$this->user->setDescription($this->description);
|
2019-09-18 22:31:53 +00:00
|
|
|
$this->user->setReceiveAnimathMails($this->receive_animath_mails);
|
2019-09-09 23:48:52 +00:00
|
|
|
|
|
|
|
if ($this->email != $this->user->getEmail()) {
|
|
|
|
$this->user->setEmail($this->email);
|
|
|
|
$this->user->setConfirmEmailToken(genRandomPhrase(64));
|
|
|
|
|
|
|
|
Mailer::sendChangeEmailAddressMail($this->user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class NewPassword
|
|
|
|
{
|
|
|
|
private $user;
|
|
|
|
private $old_password;
|
|
|
|
private $new_password;
|
|
|
|
private $confirm_password;
|
|
|
|
|
|
|
|
public function __construct($data)
|
|
|
|
{
|
|
|
|
foreach ($data as $key => $value)
|
|
|
|
$this->$key = htmlspecialchars($value);
|
|
|
|
|
|
|
|
$this->user = $_SESSION["user"];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function makeVerifications()
|
|
|
|
{
|
|
|
|
ensure($this->user->checkPassword($this->old_password), "L'ancien mot de passe est incorrect.");
|
|
|
|
ensure(strlen($this->new_password) >= 8, "Le mot de passe doit comporter au moins 8 caractères.");
|
|
|
|
ensure($this->new_password == $this->confirm_password, "Les deux mots de passe sont différents.");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updatePassword()
|
|
|
|
{
|
|
|
|
$this->user->setPassword($this->new_password);
|
|
|
|
|
|
|
|
Mailer::sendChangePasswordMail($this->user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once "server_files/views/mon_compte.php";
|