mirror of
				https://gitlab.com/animath/si/plateforme.git
				synced 2025-11-04 09:02:11 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			155 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			155 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
if (!isset($_SESSION["user_id"]))
 | 
						|
	require_once "server_files/403.php";
 | 
						|
 | 
						|
/** @var User $user */
 | 
						|
$user = $_SESSION["user"];
 | 
						|
 | 
						|
$has_error = false;
 | 
						|
$error_message = null;
 | 
						|
 | 
						|
if (isset($_POST["update_account"])) {
 | 
						|
    $my_account = new MyAccount($_POST);
 | 
						|
    try {
 | 
						|
    	$my_account->makeVerifications();
 | 
						|
    	$my_account->updateAccount();
 | 
						|
	}
 | 
						|
	catch (AssertionError $e) {
 | 
						|
    	$has_error = true;
 | 
						|
    	$error_message = $e->getMessage();
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
if (isset($_POST["update_password"])) {
 | 
						|
	$new_password = new NewPassword($_POST);
 | 
						|
	try {
 | 
						|
		$new_password->makeVerifications();
 | 
						|
		$new_password->updatePassword();
 | 
						|
	}
 | 
						|
	catch (AssertionError $e) {
 | 
						|
		$has_error = true;
 | 
						|
		$error_message = $e->getMessage();
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
class MyAccount
 | 
						|
{
 | 
						|
	public $email;
 | 
						|
	public $surname;
 | 
						|
	public $first_name;
 | 
						|
	public $birth_date;
 | 
						|
	public $gender;
 | 
						|
	public $address;
 | 
						|
	public $postal_code;
 | 
						|
	public $city;
 | 
						|
	public $country;
 | 
						|
	public $phone_number;
 | 
						|
	public $school;
 | 
						|
	public $class;
 | 
						|
	public $responsible_name;
 | 
						|
	public $responsible_phone;
 | 
						|
	public $responsible_email;
 | 
						|
	public $description;
 | 
						|
	private $user;
 | 
						|
 | 
						|
	public function __construct($data)
 | 
						|
	{
 | 
						|
		foreach ($data as $key => $value)
 | 
						|
			$this->$key = htmlspecialchars($value);
 | 
						|
 | 
						|
		$this->user = $_SESSION["user"];
 | 
						|
 | 
						|
		$keys = ["email", "surname", "first_name", "birth_date", "gender", "address", "postal_code", "city", "country", "phone_number",
 | 
						|
			"school", "class", "responsible_name", "responsible_phone", "responsible_email", "description"];
 | 
						|
 | 
						|
		if ($this->user->getRole() != Role::PARTICIPANT)
 | 
						|
			$this->class = SchoolClass::fromName(strtoupper($this->class));
 | 
						|
		else
 | 
						|
		    $this->class = SchoolClass::ADULT;
 | 
						|
 | 
						|
		foreach ($keys as $key)
 | 
						|
			$this->$key = $this->$key != null && $this->$key != "" ? $this->$key : $this->user->$key;
 | 
						|
	}
 | 
						|
 | 
						|
	public function makeVerifications()
 | 
						|
	{
 | 
						|
		global $YEAR;
 | 
						|
 | 
						|
		ensure(filter_var($this->email, FILTER_VALIDATE_EMAIL), "L'adresse e-mail entrée est invalide.");
 | 
						|
		$this->email = strtolower($this->email);
 | 
						|
		ensure($this->email == $this->user->getEmail() || !userExists($this->email), "Un compte existe déjà avec cette adresse e-mail.");
 | 
						|
		ensure(dateWellFormed($this->birth_date), "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.");
 | 
						|
		ensure(strlen($this->phone_number) >= 10, "Le numéro de téléphone est invalide.");
 | 
						|
 | 
						|
		if ($this->user->getRole() == Role::PARTICIPANT) {
 | 
						|
			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.");
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	public function updateAccount()
 | 
						|
	{
 | 
						|
		$this->user->setSurname($this->surname);
 | 
						|
		$this->user->setFirstName($this->first_name);
 | 
						|
		$this->user->setBirthDate($this->birth_date);
 | 
						|
		$this->user->setGender($this->gender);
 | 
						|
		$this->user->setAddress($this->address);
 | 
						|
		$this->user->setPostalCode($this->postal_code);
 | 
						|
		$this->user->setCity($this->city);
 | 
						|
		$this->user->setCountry($this->country);
 | 
						|
		$this->user->setPhoneNumber($this->phone_number);
 | 
						|
		$this->user->setSchool($this->school);
 | 
						|
		$this->user->setClass($this->class);
 | 
						|
		$this->user->setResponsibleName($this->responsible_name);
 | 
						|
		$this->user->setResponsiblePhone($this->responsible_phone);
 | 
						|
		$this->user->setResponsibleEmail($this->responsible_email);
 | 
						|
		$this->user->setDescription($this->description);
 | 
						|
 | 
						|
		if ($this->email != $this->user->getEmail()) {
 | 
						|
			$this->user->setEmail($this->email);
 | 
						|
			$this->user->setConfirmEmailToken(genRandomPhrase(64));
 | 
						|
 | 
						|
			Mailer::sendChangeEmailAddressMail($this->user);
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
class NewPassword
 | 
						|
{
 | 
						|
	private $user;
 | 
						|
	private $old_password;
 | 
						|
	private $new_password;
 | 
						|
	private $confirm_password;
 | 
						|
 | 
						|
	public function __construct($data)
 | 
						|
	{
 | 
						|
		foreach ($data as $key => $value)
 | 
						|
			$this->$key = htmlspecialchars($value);
 | 
						|
 | 
						|
		$this->user = $_SESSION["user"];
 | 
						|
	}
 | 
						|
 | 
						|
	public function makeVerifications()
 | 
						|
	{
 | 
						|
		ensure($this->user->checkPassword($this->old_password), "L'ancien mot de passe est incorrect.");
 | 
						|
		ensure(strlen($this->new_password) >= 8, "Le mot de passe doit comporter au moins 8 caractères.");
 | 
						|
		ensure($this->new_password == $this->confirm_password, "Les deux mots de passe sont différents.");
 | 
						|
	}
 | 
						|
 | 
						|
	public function updatePassword()
 | 
						|
	{
 | 
						|
		$this->user->setPassword($this->new_password);
 | 
						|
 | 
						|
		Mailer::sendChangePasswordMail($this->user);
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
require_once "server_files/views/mon_compte.php";
 |