From b8dfe1a6076cd3db3c6c7fe3ee82ac6604c9bc0e Mon Sep 17 00:00:00 2001 From: galaxyoyo Date: Sat, 7 Sep 2019 15:51:16 +0200 Subject: [PATCH] Restructuration de la page d'inscription --- dispatcher.php | 3 +- server_files/classes/SchoolClass.php | 41 ++++ server_files/classes/User.php | 4 +- server_files/controllers/inscription.php | 185 ++++++-------- server_files/model.php | 5 + server_files/views/inscription.php | 294 +++++++++++++---------- server_files/views/mon_compte.php | 6 +- 7 files changed, 291 insertions(+), 247 deletions(-) create mode 100644 server_files/classes/SchoolClass.php diff --git a/dispatcher.php b/dispatcher.php index 68f1947..9ad9e69 100644 --- a/dispatcher.php +++ b/dispatcher.php @@ -2,12 +2,13 @@ require_once "server_files/config.php"; -require_once "server_files/model.php"; require_once "server_files/classes/Role.php"; +require_once "server_files/classes/SchoolClass.php"; require_once "server_files/classes/Team.php"; require_once "server_files/classes/Tournament.php"; require_once "server_files/classes/User.php"; require_once "server_files/classes/ValidationStatus.php"; +require_once "server_files/model.php"; loadUserValues(); diff --git a/server_files/classes/SchoolClass.php b/server_files/classes/SchoolClass.php new file mode 100644 index 0000000..cde1604 --- /dev/null +++ b/server_files/classes/SchoolClass.php @@ -0,0 +1,41 @@ +country = $data["country"]; $this->phone_number = $data["phone_number"]; $this->school = $data["school"]; - $this->class = $data["class"]; + $this->class = SchoolClass::fromName($data["class"]); $this->responsible_name = $data["responsible_name"]; $this->responsible_phone = $data["responsible_phone"]; $this->responsible_email = $data["responsible_email"]; @@ -250,7 +250,7 @@ class User { global $DB; $this->class = $class; - $DB->prepare("UPDATE `users` SET `class` = ? WHERE `id` = ?;")->execute([$class, $this->getId()]); + $DB->prepare("UPDATE `users` SET `class` = ? WHERE `id` = ?;")->execute([SchoolClass::getName($class), $this->getId()]); } public function getResponsibleName() diff --git a/server_files/controllers/inscription.php b/server_files/controllers/inscription.php index 5b39e6a..aba712f 100644 --- a/server_files/controllers/inscription.php +++ b/server_files/controllers/inscription.php @@ -1,129 +1,98 @@ makeVerifications(); + $user->register(); + } catch (AssertionError $e) { + $has_error = true; + $error_message = $e->getMessage(); + } } -function register() { - global $DB, $YEAR, $URL_BASE, $MAIL_ADDRESS; - global $email, $firstname, $surname, $birth_date, $gender, $address, $postal_code, $city, $country, $phone_number, $role, $school, $class, $responsible_name, $responsible_phone, $responsible_email; +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; - $email = strtolower(htmlspecialchars($_POST["email"])); + public function __construct($data) + { - if (!filter_var($email, FILTER_VALIDATE_EMAIL)) - return "L'email entrée est invalide."; + foreach ($data as $key => $value) + $this->$key = htmlspecialchars($value); + } - $result = $DB->query("SELECT `email` FROM `users` WHERE `email` = '" . $email . "' AND `year` = '$YEAR';"); - if ($result->fetch()) - return "Un compte existe déjà avec cette adresse e-mail."; + public function makeVerifications() + { + global $DB, $YEAR; - $password = htmlspecialchars($_POST["password"]); - if (strlen($password) < 8) - return "Le mot de passe doit comporter au moins 8 caractères."; - if ($password != $_POST["confirm_password"]) - return "Les deux mots de passe sont différents."; + 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)); - $password = password_hash($password, PASSWORD_BCRYPT); + 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."); + } + } - $surname = strtoupper(htmlspecialchars($_POST["surname"])); - if (!isset($surname) || $surname == "") - return "Le nom de famille est obligatoire."; + $this->confirm_email_token = uniqid(); - $firstname = htmlspecialchars($_POST["firstname"]); - if (!isset($surname) || $surname == "") - return "Le prénom est obligatoire."; + throw new AssertionError("erreur"); + } - $birth_date = date_parse_from_format("yyyy-mm-dd", htmlspecialchars($_POST["birth_date"])); + public function register() + { + global $DB, $YEAR, $URL_BASE, $MAIL_ADDRESS; - if ($birth_date === FALSE) - return "La date de naissance est invalide."; - - if (htmlspecialchars($_POST["birth_date"]) >= $YEAR . "-01-01") - return "Vous devez avoir un âge strictement positif. Date de naissance rentrée : " . htmlspecialchars($_POST["birth_date"]); - - $gender = htmlspecialchars($_POST["gender"]); - - if (!isset($gender) || ($gender != "M" && $gender != "F")) - return "Le sexe indiqué est invalide."; - - $address = htmlspecialchars($_POST["address"]); - - if (!isset($address)) - $address = ""; - - try { - $postal_code = intval($_POST["postal_code"]); - if ($postal_code < 1000 || $postal_code > 95999) - return "Le code postal est invalide."; - } - catch (Exception $ex) { - return "Le code postal n'est pas un nombre valide."; - } - - $city = htmlspecialchars($_POST["city"]); - - if (!isset($city)) - $city = ""; - - $country = htmlspecialchars($_POST["country"]); - - if (!isset($country)) - $country = "France"; - - $phone_number = htmlspecialchars($_POST["phone_number"]); - - if (!isset($phone_number) || $phone_number == "") - return "Vous devez renseigner un numéro de téléphone."; - - $role = htmlspecialchars($_POST["role"]); - - if (!isset($role) || ($role != "participant" && $role != "encadrant")) - return "Le rôle entré n'est pas valide."; - - $role = strtoupper($role); - - $school = htmlspecialchars($_POST["school"]); - $class = strtoupper(htmlspecialchars($_POST["class"])); - $responsible_name = htmlspecialchars($_POST["responsible_name"]); - $responsible_phone = htmlspecialchars($_POST["responsible_phone"]); - $responsible_email = htmlspecialchars($_POST["responsible_email"]); - - if ($role == "ENCADRANT") { - $school = NULL; - $class = NULL; - $responsible_name = NULL; - $responsible_phone = NULL; - $responsible_email = NULL; - } - else { - if (!isset($class) && $class != "TERMINALE" && $class != "PREMIERE" && $class != "SECONDE") - return "La classe spécifiée est invalide. Merci de ne pas créer vos propres requêtes."; - - if ((!isset($responsible_name) || $responsible_name == "") && $birth_date > strval($YEAR - 18) . "-05-01") - return "Veuillez spécifier un nom de responsable légal."; - - if ((!isset($responsible_phone) || $responsible_phone == "") && (!isset($responsible_email) || !filter_var($responsible_email, FILTER_VALIDATE_EMAIL)) - && $birth_date > strval($YEAR - 18) . "-05-01") - return "Veuillez préciser au moins le numéro de téléphone ou l'addresse e-mail de votre responsable légal."; - } - - $description = $_POST["description"]; - - if ($role == "PARTICIPANT") - $description = NULL; - - $confirm_email_uid = uniqid(); - - $req = $DB->prepare("INSERT INTO `users`(`email`, `pwd_hash`, `confirm_email`, `surname`, `first_name`, `birth_date`, `gender`, + $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`, `year`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"); - $req->execute([$email, $password, $confirm_email_uid, $surname, $firstname, $_POST["birth_date"], $gender, $address, $postal_code, - $city, $country, $phone_number, $school, $class, $role, $description, $YEAR]); + $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]); - $msg = "Merci pour votre inscription au TFJM² $YEAR ! Veuillez désormais confirmer votre adresse mail en cliquant ici : $URL_BASE/confirmer_mail/$confirm_email_uid"; - mail($email, "Inscription au TFJM² $YEAR", $msg, "From: $MAIL_ADDRESS\r\n"); + // TODO Mieux gérer l'envoi des mails avec une classe à part - return false; + $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"); + } } require_once "server_files/views/inscription.php"; diff --git a/server_files/model.php b/server_files/model.php index 717b938..6e5eef3 100644 --- a/server_files/model.php +++ b/server_files/model.php @@ -94,4 +94,9 @@ function quitTeam() { $_SESSION["team"] = null; unset($_SESSION["team"]); +} + +function ensure($bool, $error_msg = "") { + if (!$bool) + throw new AssertionError($error_msg); } \ No newline at end of file diff --git a/server_files/views/inscription.php b/server_files/views/inscription.php index 1f34347..7abb639 100644 --- a/server_files/views/inscription.php +++ b/server_files/views/inscription.php @@ -1,149 +1,177 @@ Erreur : " . $error_message . ""; +if ($has_error) + echo "

Erreur : " . $error_message . "

"; ?> - Votre inscription est validée ! Merci désormais de confirmer votre boîte mail pour valider votre adresse. + Votre inscription est validée ! Merci désormais de confirmer votre boîte mail pour valider votre adresse. -

Vous êtes déjà connecté !

+

Vous êtes déjà connecté !

-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
" required />
" required />
" required />
" required />
/> - />
" />
" min="1000" max="95999" required />
" />
" required />
" />
" />
" />
" />
" />
-
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
/> + />
" required/>
+
+ +
+ +
+
+
- + selectRole(); + diff --git a/server_files/views/mon_compte.php b/server_files/views/mon_compte.php index 615f70c..872d46d 100644 --- a/server_files/views/mon_compte.php +++ b/server_files/views/mon_compte.php @@ -98,9 +98,9 @@ if (isset($error_message) && $error_message === FALSE) {