2019-09-06 11:48:50 +00:00
< ? php
2019-09-07 13:51:16 +00:00
$has_error = false ;
$error_message = null ;
2019-12-26 21:30:42 +00:00
if ( isset ( $_POST [ " register " ])) {
2019-09-07 13:51:16 +00:00
$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
}
2019-09-07 13:51:16 +00:00
class NewUser
{
2019-09-07 14:37:00 +00:00
public $email ;
public $first_name ;
public $surname ;
public $birth_date ;
public $gender ;
2019-09-07 13:51:16 +00:00
public $address = " " ;
2019-09-07 14:37:00 +00:00
public $postal_code ;
2019-09-07 13:51:16 +00:00
public $city = " " ;
2019-09-07 14:37:00 +00:00
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 ;
2019-09-07 13:51:16 +00:00
public function __construct ( $data )
{
foreach ( $data as $key => $value )
$this -> $key = htmlspecialchars ( $value );
}
public function makeVerifications ()
{
2019-09-07 14:37:00 +00:00
global $YEAR ;
2019-09-07 13:51:16 +00:00
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. " );
2019-09-07 13:51:16 +00:00
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. " );
2019-09-07 14:37:00 +00:00
ensure ( dateWellFormed ( $this -> birth_date ), " La date de naissance est invalide. " );
2019-09-07 13:51:16 +00:00
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. " );
2019-09-07 13:51:16 +00:00
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. " );
2019-09-07 13:51:16 +00:00
$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. " );
}
}
2020-01-24 10:13:11 +00:00
else {
$this -> class = SchoolClass :: ADULT ;
}
2019-09-07 13:51:16 +00:00
2020-01-22 20:11:54 +00:00
if ( count ( User :: getAllUsers ()) == 0 )
2020-01-18 16:37:24 +00:00
$this -> role = Role :: ADMIN ;
2019-09-08 22:41:52 +00:00
$this -> confirm_email_token = genRandomPhrase ( 64 );
2019-09-07 13:51:16 +00:00
}
public function register ()
{
2019-09-07 14:37:00 +00:00
global $DB , $YEAR ;
2019-09-07 13:51:16 +00:00
$req = $DB -> prepare ( " INSERT INTO `users`(`email`, `pwd_hash`, `confirm_email`, `surname`, `first_name`, `birth_date`, `gender`,
2020-02-18 15:10:18 +00:00
`address` , `postal_code` , `city` , `country` , `phone_number` , `school` , `class` , `role` , `description` , `responsible_name` , `responsible_phone` , `responsible_email` , `year` )
VALUES ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ); " );
2019-09-07 13:51:16 +00:00
$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 ,
2020-02-18 15:10:18 +00:00
$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-07 13:51:16 +00:00
}
2019-09-06 11:48:50 +00:00
}
2019-09-07 11:42:36 +00:00
require_once " server_files/views/inscription.php " ;