2019-09-06 11:48:50 +00:00
< ? php
2019-09-07 13:51:16 +00:00
$has_error = false ;
$error_message = null ;
2019-09-06 11:48:50 +00:00
if ( isset ( $_POST [ " submitted " ])) {
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é. " );
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 ();
}
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`,
2019-09-06 11:48:50 +00:00
`address` , `postal_code` , `city` , `country` , `phone_number` , `school` , `class` , `role` , `description` , `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 ,
$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
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 " ;