plateforme-tfjm2/server_files/controllers/inscription.php

100 lines
3.7 KiB
PHP
Raw Normal View History

2019-09-06 11:48:50 +00:00
<?php
$has_error = false;
$error_message = null;
2019-12-26 21:30:42 +00:00
if (isset($_POST["register"])) {
$user = new NewUser($_POST);
try {
$user->makeVerifications();
$user->register();
} catch (AssertionError $e) {
$has_error = true;
$error_message = $e->getMessage();
}
2019-09-06 11:48:50 +00:00
}
class NewUser
{
public $email;
public $first_name;
public $surname;
public $birth_date;
public $gender;
public $address = "";
public $postal_code;
public $city = "";
public $country;
public $phone_number;
public $role;
public $school;
public $class;
public $responsible_name;
public $responsible_phone;
public $responsible_email;
public $description;
public $confirm_email_token;
private $password;
private $confirm_password;
public function __construct($data)
{
foreach ($data as $key => $value)
$this->$key = htmlspecialchars($value);
}
public function makeVerifications()
{
global $YEAR;
ensure(filter_var($this->email, FILTER_VALIDATE_EMAIL), "L'adresse e-mail entrée est invalide.");
2019-09-07 15:26:30 +00:00
$this->email = strtolower($this->email);
2019-09-08 20:54:57 +00:00
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.");
ensure(dateWellFormed($this->birth_date), "La date de naissance est invalide.");
ensure($this->birth_date < $YEAR . "-01-01", "Vous devez être né.");
2020-02-21 22:26:47 +00:00
ensure($this->gender == "M" || $this->gender == "F", "Merci de spécifier un genre.");
ensure(preg_match("#^[0-9]{4}[0-9]?$#", $this->postal_code) && intval($this->postal_code) >= 01000 && intval($this->postal_code) <= 95999, "Le code postal est invalide.");
if ($this->country == "")
$this->country = "France";
2020-01-16 21:04:29 +00:00
ensure(strlen($this->phone_number) >= 10 && strlen($this->phone_number) <= 20, "Le numéro de téléphone est invalide.");
$this->role = Role::fromName(strtoupper($this->role));
if ($this->role == Role::PARTICIPANT) {
$this->class = SchoolClass::fromName(strtoupper($this->class));
if ($this->birth_date > strval($YEAR - 18) . "04-01") {
ensure($this->responsible_name != "", "Veuillez spécifier un responsable légal.");
ensure(strlen($this->responsible_phone) >= 10, "Veuillez rentrer le numéro de téléphone de votre responsable légal.");
ensure(filter_var($this->responsible_email, FILTER_VALIDATE_EMAIL), "Veuillez spécifier un responsable légal.");
}
}
else {
$this->class = SchoolClass::ADULT;
}
if (count(User::getAllUsers()) == 0)
2020-01-18 16:37:24 +00:00
$this->role = Role::ADMIN;
$this->confirm_email_token = genRandomPhrase(64);
}
public function register()
{
global $DB, $YEAR;
$req = $DB->prepare("INSERT INTO `users`(`email`, `pwd_hash`, `confirm_email`, `surname`, `first_name`, `birth_date`, `gender`,
`address`, `postal_code`, `city`, `country`, `phone_number`, `school`, `class`, `role`, `description`, `responsible_name`, `responsible_phone`, `responsible_email`, `year`)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
$req->execute([$this->email, password_hash($this->password, PASSWORD_BCRYPT), $this->confirm_email_token, $this->surname, $this->first_name, $this->birth_date, $this->gender, $this->address,
$this->postal_code, $this->city, $this->country, $this->phone_number, $this->school, SchoolClass::getName($this->class), Role::getName($this->role), $this->description, $this->responsible_name, $this->responsible_phone, $this->responsible_email, $YEAR]);
2019-09-06 11:48:50 +00:00
2019-09-08 10:45:48 +00:00
Mailer::sendRegisterMail($this);
}
2019-09-06 11:48:50 +00:00
}
require_once "server_files/views/inscription.php";