plateforme-tfjm2/server_files/controllers/inscription.php

99 lines
4.0 KiB
PHP
Raw Normal View History

2019-09-06 11:48:50 +00:00
<?php
$has_error = false;
$error_message = null;
2019-09-06 11:48:50 +00:00
if (isset($_POST["submitted"])) {
$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 = null;
public $first_name = null;
public $surname = null;
public $birth_date = null;
public $gender = null;
public $address = "";
public $postal_code = null;
public $city = "";
public $country = null;
public $phone_number = null;
public $role = null;
public $school = null;
public $class = null;
public $responsible_name = null;
public $responsible_phone = null;
public $responsible_email = null;
public $description = null;
public $confirm_email_token = null;
private $password = null;
private $confirm_password = null;
public function __construct($data)
{
foreach ($data as $key => $value)
$this->$key = htmlspecialchars($value);
}
public function makeVerifications()
{
global $DB, $YEAR;
ensure(filter_var($this->email, FILTER_VALIDATE_EMAIL), "L'adresse e-mail entrée est invalide.");
ensure(!$DB->query("SELECT `email` FROM `users` WHERE `email` = '" . $this->email . "' AND `year` = '$YEAR';")->fetch(), "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(date_parse_from_format("yyyy-mm-dd", $this->birth_date) !== false, "La date de naissance est invalide.");
ensure($this->birth_date < $YEAR . "-01-01", "Vous devez être né.");
ensure($this->gender == "M" || $this->gender == "F", "Le sexe indiqué est invalide.");
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";
ensure(strlen($this->phone_number) >= 10, "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.");
}
}
$this->confirm_email_token = uniqid();
throw new AssertionError("erreur");
}
public function register()
{
global $DB, $YEAR, $URL_BASE, $MAIL_ADDRESS;
$req = $DB->prepare("INSERT INTO `users`(`email`, `pwd_hash`, `confirm_email`, `surname`, `first_name`, `birth_date`, `gender`,
2019-09-06 11:48:50 +00:00
`address`, `postal_code`, `city`, `country`, `phone_number`, `school`, `class`, `role`, `description`, `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, $YEAR]);
2019-09-06 11:48:50 +00:00
// TODO Mieux gérer l'envoi des mails avec une classe à part
2019-09-06 11:48:50 +00:00
$msg = "Merci pour votre inscription au TFJM² $YEAR ! Veuillez désormais confirmer votre adresse mail en cliquant ici : $URL_BASE/confirmer_mail/" . $this->confirm_email_token;
mail($this->email, "Inscription au TFJM² $YEAR", $msg, "From: $MAIL_ADDRESS\r\n");
}
2019-09-06 11:48:50 +00:00
}
require_once "server_files/views/inscription.php";