1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-12-25 06:22:22 +00:00

Optimisation de l'envoi de mails

This commit is contained in:
Yohann 2019-09-08 12:45:48 +02:00 committed by galaxyoyo
parent bd73e82cb0
commit 5771a15a32
16 changed files with 241 additions and 116 deletions

View File

@ -59,7 +59,7 @@ class NewTeam {
$_SESSION["team"] = Team::fromTrigram($this->trigram);
$_SESSION["user"]->setTeamId($_SESSION["team"]->getId());
sendAddTeamMail($_SESSION["user"], $_SESSION["team"], $this->tournament);
Mailer::sendAddTeamMail($_SESSION["user"], $_SESSION["team"], $this->tournament);
}
}

View File

@ -53,7 +53,7 @@ class NewOrganizer {
VALUES (?, ?, ?, ?, ?, ?);");
$req->execute([$this->email, password_hash($this->password, PASSWORD_BCRYPT), $this->surname, $this->first_name, $this->admin ? "ADMIN" : "ORGANIZER", $YEAR]);
sendAddOrganizerMail($this);
Mailer::sendAddOrganizerMail($this);
}
}

View File

@ -93,10 +93,11 @@ class NewTournament {
$this->tournament = Tournament::fromName($this->name);
/** @var User $organizer */
foreach ($this->organizers as $organizer) {
$req = $DB->prepare("INSERT INTO `organizers`(`organizer`, `tournament`) VALUES(?, ?);");
$req->execute([$organizer->getId(), $this->tournament->getId()]);
sendAddOrganizerForTournamentMail($organizer, $this->tournament);
Mailer::sendAddOrganizerForTournamentMail($organizer, $this->tournament);
}
}
}

View File

@ -1,5 +1,7 @@
<?php
// TODO Arranger tout ça
if (isset($_POST["submitted"]) && !isset($_SESSION["user_id"])) {
$error_message = login();
}
@ -9,7 +11,7 @@ if (isset($_POST["forgotten_password"]) && !isset($_SESSION["user_id"])) {
}
if (isset($_GET["reset_password"]) && isset($_GET["token"]) && !isset($_SESSION["user_id"])) {
$reset_data = $DB->query("SELECT `id`, `email` FROM `users` WHERE `forgotten_password` = '" . htmlspecialchars($_GET["token"]) . "';")->fetch();
$reset_data = $DB->query("SELECT `id` FROM `users` WHERE `forgotten_password` = '" . htmlspecialchars($_GET["token"]) . "';")->fetch();
if ($reset_data === FALSE) {
header("Location: $URL_BASE/connexion");
exit();
@ -34,7 +36,7 @@ function login() {
$password = htmlspecialchars($_POST["password"]);
$user = User::fromEmail($email);
if ($user === FALSE)
if ($user === null)
return "Le compte n'existe pas.";
if ($user->getConfirmEmailToken() !== NULL) {
@ -52,8 +54,6 @@ function login() {
}
function recuperateAccount() {
global $MAIL_ADDRESS, $URL_BASE;
$email = htmlspecialchars($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
@ -67,21 +67,15 @@ function recuperateAccount() {
$user->setForgottenPasswordToken($token);
$msg = "Bonjour,\r\n\r\n"
. "Vous avez indiqué avoir oublié votre mot de passe. Veuillez cliquer ici pour le réinitialiser : $URL_BASE/connexion/reinitialiser_mdp/$token\r\n\r\n"
. "Si vous n'êtes pas à l'origine de cette manipulation, vous pouvez ignorer ce message.\r\n\r\n"
. "Cordialement,\r\n\r\n"
. "Le comité national d'organisation du TFJM².";
mail("$email", "Mot de passe oublié - TFJM²", $msg, "From: $MAIL_ADDRESS\r\n");
Mailer::sendForgottenPasswordProcedureMail($user);
return false;
}
function resetPassword() {
global $DB, $MAIL_ADDRESS, $reset_data;
global $reset_data;
$id = $reset_data["id"];
$email = $reset_data["email"];
$password = htmlspecialchars($_POST["password"]);
$confirm = htmlspecialchars($_POST["confirm_password"]);
@ -91,20 +85,17 @@ function resetPassword() {
if ($password != $confirm)
return "Les deux mots de passe sont différents.";
$hash = password_hash($password, PASSWORD_BCRYPT);
$user = User::fromId($id);
$user->setForgottenPasswordToken(null);
$user->setPassword($password);
$DB->prepare("UPDATE `users` SET `pwd_hash` = ?, `forgotten_password` = NULL WHERE `id` = ?;")->execute([$hash, $id]);
$msg = "Bonjour,\r\n\r\nNous vous informons que votre mot de passe vient d'être modifié. "
. "Si vous n'êtes pas à l'origine de cette manipulation, veuillez immédiatement vérifier vos accès à votre boîte mail et changer votre mot de passe sur la plateforme d'inscription.\r\n\r\n"
. "Cordialement,\r\n\r\nLe comité national d'organisation du TFJM²";
mail($email, "Mot de passe modifié TFJM²", $msg, "From: $MAIL_ADDRESS\r\n");
Mailer::sendChangePasswordMail($user);
return false;
}
function sendConfirmEmail() {
global $URL_BASE, $MAIL_ADDRESS, $YEAR;
global $URL_BASE;
$email = htmlspecialchars($_SESSION["confirm_email"]);
@ -121,11 +112,7 @@ function sendConfirmEmail() {
exit();
}
$confirm_email_uid = $user->getConfirmEmailToken();
$msg = "Bonjour,\r\n\r\nPour confirmer votre adresse mail, cliquez ici : $URL_BASE/confirmer_mail/$confirm_email_uid\r\n\r\n"
. "Cordialement,\r\n\r\nLe comité national d'organisation du TFJM²";
mail($email, "Confirmation d'adresse mail TFJM² $YEAR", $msg, "From: $MAIL_ADDRESS\r\n");
Mailer::sendConfirmEmail($user);
return false;
}

View File

@ -86,7 +86,7 @@ class NewUser
$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]);
sendRegisterMail($this);
Mailer::sendRegisterMail($this);
}
}

View File

@ -14,7 +14,7 @@ if (isset($_POST["submitted"])) {
function updateAccount()
{
global $URL_BASE, $MAIL_ADDRESS, $user;
global $user;
$surname = htmlspecialchars($_POST["surname"]);
if (isset($surname) && $surname != "")
@ -93,10 +93,10 @@ function updateAccount()
$email = htmlspecialchars($_POST["email"]);
if (isset($email) && $email != "" && filter_var($email, FILTER_VALIDATE_EMAIL)) {
$confirm_email_token = uniqid();
$user->setEmail($email);
$user->setConfirmEmailToken($confirm_email_token);
$msg = "Vous venez de changer votre adresse mail. Veuillez désormais confirmer votre adresse mail en cliquant ici : $URL_BASE/confirmer_mail/$confirm_email_token";
mail($email, "Changement d'adresse mail - TFJM²", $msg, "From: $MAIL_ADDRESS\r\n");
Mailer::sendChangeEmailAddressMail($user);
}
return false;

View File

@ -8,7 +8,7 @@ if (isset($_POST["submitted"])) {
}
function joinTeam() {
global $YEAR, $MAIL_ADDRESS, $access_code;
global $access_code;
$access_code = htmlspecialchars($_POST["access_code"]);
@ -42,10 +42,7 @@ function joinTeam() {
$_SESSION["team"] = $team;
$tournament = $_SESSION["tournament"] = Tournament::fromId($team->getTournamentId());
$msg = "Bonjour " . $user->getFirstName() . " " . $user->getSurname() . ",\r\n\r\n";
$msg .= "Vous venez de rejoindre l'équipe « " . $team->getName() . " » (" . $team->getTrigram() . ") pour le TFJM² de " . $tournament->getId() . " et nous vous en remercions.\r\n\r\n";
$msg .= "Cordialement,\r\n\r\nL'organisation du TFJM² $YEAR";
mail($_SESSION["email"], "Équipe rejointe TFJM² $YEAR", $msg, "From: $MAIL_ADDRESS\r\n");
Mailer::sendJoinTeamMail($user, $team, $tournament);
return false;
}

View File

@ -1,6 +1,8 @@
<?php
function sendMail($email, $subject, $content, $from = "contact")
class Mailer
{
private static function sendMail($email, $subject, $content, $from = "contact")
{
global $MAIL_DOMAIN, $URL_BASE, $YEAR;
@ -14,31 +16,70 @@ function sendMail($email, $subject, $content, $from = "contact")
mail($email, $subject, $content, $headers);
}
/**
* @param NewUser
*/
function sendRegisterMail($new_user)
private static function getTemplate($name)
{
global $LOCAL_PATH, $YEAR;
global $LOCAL_PATH;
return file_get_contents("$LOCAL_PATH/server_files/services/mail_templates/$name.html");
}
$content = file_get_contents("$LOCAL_PATH/server_files/services/mail_templates/register.html");
public static function sendRegisterMail(NewUser $new_user)
{
global $YEAR;
$content = self::getTemplate("register");
$content = preg_replace("#{FIRST_NAME}#", $new_user->first_name, $content);
$content = preg_replace("#{SURNAME}#", $new_user->surname, $content);
$content = preg_replace("#{TOKEN}#", $new_user->confirm_email_token, $content);
sendMail($new_user->email, "Inscription au TFJM² $YEAR", $content);
self::sendMail($new_user->email, "Inscription au TFJM² $YEAR", $content);
}
/**
* @param $user User
* @param $team Team
* @param $tournament Tournament
*/
function sendAddTeamMail($user, $team, $tournament)
public static function sendConfirmEmail(User $user)
{
global $LOCAL_PATH, $YEAR;
global $YEAR;
$content = file_get_contents("$LOCAL_PATH/server_files/services/mail_templates/add_team.html");
$content = self::getTemplate("register");
$content = preg_replace("#{FIRST_NAME}#", $user->getFirstName(), $content);
$content = preg_replace("#{SURNAME}#", $user->getSurname(), $content);
$content = preg_replace("#{TOKEN}#", $user->getConfirmEmailToken(), $content);
self::sendMail($user->getEmail(), "Confirmation d'adresse e-mail TFJM² $YEAR", $content);
}
public static function sendChangeEmailAddressMail(User $user)
{
$content = self::getTemplate("change_email_address");
$content = preg_replace("#{FIRST_NAME}#", $user->getFirstName(), $content);
$content = preg_replace("#{SURNAME}#", $user->getSurname(), $content);
$content = preg_replace("#{TOKEN}#", $user->getConfirmEmailToken(), $content);
self::sendMail($user->getEmail(), "Changement d'adresse e-mail TFJM²", $content);
}
public static function sendForgottenPasswordProcedureMail(User $user)
{
$content = self::getTemplate("forgotten_password");
$content = preg_replace("#{FIRST_NAME}#", $user->getFirstName(), $content);
$content = preg_replace("#{SURNAME}#", $user->getSurname(), $content);
$content = preg_replace("#{TOKEN}#", $user->getForgottenPasswordToken(), $content);
self::sendMail($user->getEmail(), "Mot de passe oublié TFJM²", $content);
}
public static function sendChangePasswordMail(User $user)
{
$content = self::getTemplate("change_password");
$content = preg_replace("#{FIRST_NAME}#", $user->getFirstName(), $content);
$content = preg_replace("#{SURNAME}#", $user->getSurname(), $content);
self::sendMail($user->getEmail(), "Mot de passe changé TFJM²", $content);
}
public static function sendAddTeamMail(User $user, Team $team, Tournament $tournament)
{
global $YEAR;
$content = self::getTemplate("add_team");
$content = preg_replace("#{FIRST_NAME}#", $user->getFirstName(), $content);
$content = preg_replace("#{SURNAME}#", $user->getSurname(), $content);
$content = preg_replace("#{TEAM_NAME}#", $team->getName(), $content);
@ -46,36 +87,44 @@ function sendAddTeamMail($user, $team, $tournament)
$content = preg_replace("#{TOURNAMENT_NAME}#", $tournament->getName(), $content);
$content = preg_replace("#{ACCESS_CODE}#", $team->getAccessCode(), $content);
sendMail($user->getEmail(), "Ajout d'une équipe TFJM² $YEAR", $content);
self::sendMail($user->getEmail(), "Ajout d'une équipe TFJM² $YEAR", $content);
}
/**
* @param NewUser
*/
function sendAddOrganizerMail($new_orga)
public static function sendJoinTeamMail(User $user, Team $team, Tournament $tournament)
{
global $LOCAL_PATH, $YEAR;
global $YEAR;
$content = file_get_contents("$LOCAL_PATH/server_files/services/mail_templates/add_organizer.html");
$content = self::getTemplate("join_team");
$content = preg_replace("#{FIRST_NAME}#", $user->getFirstName(), $content);
$content = preg_replace("#{SURNAME}#", $user->getSurname(), $content);
$content = preg_replace("#{TEAM_NAME}#", $team->getName(), $content);
$content = preg_replace("#{TRIGRAM}#", $team->getTrigram(), $content);
$content = preg_replace("#{TOURNAMENT_NAME}#", $tournament->getName(), $content);
self::sendMail($user->getEmail(), "Équipe rejointe TFJM² $YEAR", $content);
}
public static function sendAddOrganizerMail(NewOrganizer $new_orga)
{
global $YEAR;
$content = self::getTemplate("add_organizer");
$content = preg_replace("#{FIRST_NAME}#", $new_orga->first_name, $content);
$content = preg_replace("#{SURNAME}#", $new_orga->surname, $content);
$content = preg_replace("#{PASSWORD}#", $new_orga->password, $content);
sendMail($new_orga->email, "Ajout d'un organisateur -- TFJM² $YEAR", $content);
self::sendMail($new_orga->email, "Ajout d'un organisateur TFJM² $YEAR", $content);
}
/**
* @param $organizer User
* @param $tournament Tournament
*/
function sendAddOrganizerForTournamentMail($organizer, $tournament)
public static function sendAddOrganizerForTournamentMail(User $organizer, Tournament $tournament)
{
global $LOCAL_PATH, $YEAR;
global $YEAR;
$content = file_get_contents("$LOCAL_PATH/server_files/services/mail_templates/add_organizer_for_tournament.html");
$content = self::getTemplate("add_organizer_for_tournament");
$content = preg_replace("#{FIRST_NAME}#", $organizer->getFirstName(), $content);
$content = preg_replace("#{SURNAME}#", $organizer->getSurname(), $content);
$content = preg_replace("#{TOURNAMENT_NAME}#", $tournament->getName(), $content);
sendMail($organizer->getEmail(), "Ajout d'un organisateur pour le tournoi " . $tournament->getName() . "-- TFJM² $YEAR", $content);
self::sendMail($organizer->getEmail(), "Ajout d'un organisateur pour le tournoi " . $tournament->getName() . " TFJM² $YEAR", $content);
}
}

View File

@ -1,8 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<!--suppress HtmlUnknownTarget -->
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Organisateur du tournoi de {TOURNAMENT_NAME} -- TFJM²</title>
<title>Organisateur du tournoi de {TOURNAMENT_NAME} TFJM²</title>
</head>
<body>
Bonjour {FIRST_NAME} {SURNAME},<br />

View File

@ -8,7 +8,8 @@
Bonjour {FIRST_NAME} {SURNAME},<br />
<br />
Vous venez de créer l'équipe « {TEAM_NAME} » ({TRIGRAM}) pour le TFJM<sup>2</sup> de {TOURNAMENT_NAME} et nous vous en remercions.<br />
"Afin de permettre aux autres membres de votre équipe de vous rejoindre, veuillez leur transmettre le code d'accès : {ACCESS_CODE}<br />
Afin de permettre aux autres membres de votre équipe de vous rejoindre, veuillez leur transmettre le code d'accès :
{ACCESS_CODE}<br/>
<br />
Le comité national d'organisation du TFJM<sup>2</sup>
</body>

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<!--suppress HtmlUnknownTarget -->
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Changement d'adresse e-mail TFJM²</title>
</head>
<body>
Bonjour {FIRST_NAME} {SURNAME},<br/>
<br/>
Vous venez de changer votre adresse e-mail. Veuillez désormais la confirmer en cliquant ici : <a
href="{URL_BASE}/confirmer_mail/{TOKEN}">{URL_BASE}/confirmer_mail/{TOKEN}</a><br/>
<br/>
Le comité national d'organisation du TFJM<sup>2</sup>
</body>
</html>

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Mot de passe changé TFJM²</title>
</head>
<body>
Bonjour {FIRST_NAME} {SURNAME},<br/>
<br/>
Nous vous informons que votre mot de passe vient d'être modifié. Si vous n'êtes pas à l'origine de cette manipulation,
veuillez immédiatement vérifier vos accès à votre boîte mail et changer votre mot de passe sur la plateforme
d'inscription.<br/>
<br/>
Cordialement,<br/>
<br/>
Le comité national d'organisation du TFJM<sup>2</sup>
</body>
</html>

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<!--suppress HtmlUnknownTarget -->
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Inscription au TFJM² {YEAR}</title>
</head>
<body>
Bonjour {FIRST_NAME} {SURNAME},<br/>
<br/>
Vous êtes inscrit au TFJM<sup>2</sup> {YEAR} et nous vous en remercions.<br/>
Pour valider votre adresse e-mail, veuillez cliquer sur le lien : <a href="{URL_BASE}/confirmer_mail/{TOKEN}">{URL_BASE}/confirmer_mail/{TOKEN}</a><br/>
<br/>
Cordialement,<br/>
<br/>
Le comité national d'organisation du TFJM<sup>2</sup>
</body>
</html>

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Mot de passe oublié TFJM²</title>
</head>
<body>
Bonjour,<br/>
<br/>
Vous avez indiqué avoir oublié votre mot de passe. Veuillez cliquer ici pour le réinitialiser :
$URL_BASE/connexion/reinitialiser_mdp/{TOKEN}<br/>
<br/>
Si vous n'êtes pas à l'origine de cette manipulation, vous pouvez ignorer ce message.<br/>
<br/>
Cordialement,<br/>
<br/>
Le comité national d'organisation du TFJM<sup>2</sup>
</body>
</html>

View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Équipe rejointe TFJM² {YEAR}</title>
</head>
<body>
Bonjour {FIRST_NAME} {SURNAME},<br/>
<br/>
Vous venez de rejoindre l'équipe « {TEAM_NAME} » ({TRIGRAM}) pour le TFJM² de {TOURNAMENT_NAME} et nous vous en
remercions.<br/>
<br/>
Cordialement,<br/>
<br/>
Le comité national d'organisation du TFJM<sup>2</sup>
</body>
</html>

View File

@ -1,4 +1,5 @@
<!DOCTYPE html>
<!--suppress HtmlUnknownTarget -->
<html lang="fr">
<head>
<meta charset="UTF-8">