2019-09-09 23:48:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
$has_error = false;
|
|
|
|
$error_message = null;
|
|
|
|
|
2019-09-24 21:18:32 +00:00
|
|
|
if (isset($_POST["register"])) {
|
2019-09-09 23:48:52 +00:00
|
|
|
$user = new NewUser($_POST);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$user->makeVerifications();
|
|
|
|
$user->register();
|
|
|
|
} catch (AssertionError $e) {
|
|
|
|
$has_error = true;
|
|
|
|
$error_message = $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class NewUser
|
|
|
|
{
|
|
|
|
public $email;
|
|
|
|
public $first_name;
|
|
|
|
public $surname;
|
|
|
|
public $role;
|
|
|
|
public $school;
|
|
|
|
public $class;
|
2019-09-24 21:54:04 +00:00
|
|
|
public $receive_animath_mails;
|
2019-09-10 23:17:05 +00:00
|
|
|
|
2019-09-09 23:48:52 +00:00
|
|
|
public $description;
|
|
|
|
public $confirm_email_token;
|
|
|
|
private $password;
|
|
|
|
private $confirm_password;
|
|
|
|
|
|
|
|
public function __construct($data)
|
|
|
|
{
|
|
|
|
foreach ($data as $key => $value)
|
|
|
|
$this->$key = htmlspecialchars($value);
|
2019-09-24 21:54:04 +00:00
|
|
|
|
|
|
|
$this->receive_animath_mails = $this->receive_animath_mails ? 1 : 0;
|
2019-09-09 23:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function makeVerifications()
|
|
|
|
{
|
2019-09-12 16:47:57 +00:00
|
|
|
global $CONFIG;
|
|
|
|
|
|
|
|
ensure(date("Y-m-d H:i:s") < $CONFIG->getInscriptionDate(), "Les inscriptions sont terminées.");
|
2019-09-09 23:48:52 +00:00
|
|
|
ensure(filter_var($this->email, FILTER_VALIDATE_EMAIL), "L'adresse e-mail entrée est invalide.");
|
|
|
|
$this->email = strtolower($this->email);
|
|
|
|
ensure(!userExists($this->email), "Un compte existe déjà avec cette adresse e-mail.");
|
|
|
|
ensure(strlen($this->password) >= 8, "Le mot de passe doit comporter au moins 8 caractères.");
|
|
|
|
ensure($this->password == $this->confirm_password, "Les deux mots de passe sont différents.");
|
|
|
|
ensure($this->surname != "", "Le nom de famille est obligatoire.");
|
|
|
|
ensure($this->first_name != "", "Le prénom est obligatoire.");
|
|
|
|
$this->role = Role::fromName(strtoupper($this->role));
|
2019-09-24 09:00:44 +00:00
|
|
|
ensure($this->role == Role::PARTICIPANT || $this->role == Role::ENCADRANT, "Vous devez être participant ou encadrant.");
|
2019-09-09 23:48:52 +00:00
|
|
|
|
2019-09-10 23:17:05 +00:00
|
|
|
if ($this->role == Role::PARTICIPANT)
|
2019-09-09 23:48:52 +00:00
|
|
|
$this->class = SchoolClass::fromName(strtoupper($this->class));
|
|
|
|
|
|
|
|
$this->confirm_email_token = genRandomPhrase(64);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function register()
|
|
|
|
{
|
|
|
|
global $DB, $YEAR;
|
|
|
|
|
2019-09-24 09:00:44 +00:00
|
|
|
if (!$DB->query("SELECT `id` FROM `users` WHERE `year` = $YEAR;")->fetch())
|
|
|
|
$this->role = Role::ADMIN;
|
|
|
|
|
2019-09-24 21:54:04 +00:00
|
|
|
$req = $DB->prepare("INSERT INTO `users`(`email`, `pwd_hash`, `confirm_email`, `surname`, `first_name`, `school`, `class`, `role`, `description`, `receive_animath_mails`, `year`)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
|
2019-09-10 23:17:05 +00:00
|
|
|
$req->execute([$this->email, password_hash($this->password, PASSWORD_BCRYPT), $this->confirm_email_token, $this->surname, $this->first_name,
|
2019-09-24 21:54:04 +00:00
|
|
|
$this->school, SchoolClass::getName($this->class), Role::getName($this->role), $this->description, $this->receive_animath_mails, $YEAR]);
|
2019-09-09 23:48:52 +00:00
|
|
|
|
|
|
|
Mailer::sendRegisterMail($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once "server_files/views/inscription.php";
|