1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2024-12-27 09:42:20 +00:00
plateforme-corres2math/server_files/controllers/inscription.php

79 lines
2.5 KiB
PHP
Raw Normal View History

<?php
$has_error = false;
$error_message = null;
if (isset($_POST["register"])) {
$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;
public $receive_animath_mails;
public $description;
public $confirm_email_token;
private $password;
private $confirm_password;
public function __construct($data)
{
foreach ($data as $key => $value)
$this->$key = htmlspecialchars($value);
$this->receive_animath_mails = $this->receive_animath_mails ? 1 : 0;
}
public function makeVerifications()
{
global $CONFIG;
ensure(date("Y-m-d H:i:s") < $CONFIG->getInscriptionDate(), "Les inscriptions sont terminées.");
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));
ensure($this->role == Role::PARTICIPANT || $this->role == Role::ENCADRANT, "Vous devez être participant ou encadrant.");
if ($this->role == Role::PARTICIPANT)
$this->class = SchoolClass::fromName(strtoupper($this->class));
$this->confirm_email_token = genRandomPhrase(64);
}
public function register()
{
global $DB, $YEAR;
if (!$DB->query("SELECT `id` FROM `users` WHERE `year` = $YEAR;")->fetch())
$this->role = Role::ADMIN;
$req = $DB->prepare("INSERT INTO `users`(`email`, `pwd_hash`, `confirm_email`, `surname`, `first_name`, `school`, `class`, `role`, `description`, `receive_animath_mails`, `year`)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
$req->execute([$this->email, password_hash($this->password, PASSWORD_BCRYPT), $this->confirm_email_token, $this->surname, $this->first_name,
$this->school, SchoolClass::getName($this->class), Role::getName($this->role), $this->description, $this->receive_animath_mails, $YEAR]);
Mailer::sendRegisterMail($this);
}
}
require_once "server_files/views/inscription.php";