plateforme-corres2math/server_files/services/mail.php

257 lines
11 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
class Mailer
{
private static function sendMail($email, $subject, $content, $from = "contact")
{
global $MAIL_DOMAIN, $URL_BASE, $YEAR;
$content = preg_replace("#{URL_BASE}#", $URL_BASE, $content);
$content = preg_replace("#{YEAR}#", $YEAR . "- " . ($YEAR + 1), $content);
$headers = "From: \"Contact Corres2Math\" <" . $from . "@" . $MAIL_DOMAIN . ">\r\n";
$headers .= "Reply-To: \"Contact corres2Math\" <contact@" . $MAIL_DOMAIN . ">\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
ensure(mail($email, $subject, $content, $headers), "Un problème est survenu dans l'envoi d'un mail. Veuiller contacter votre administrateur.");
}
private static function broadcastToTeam(Team $team, $subject, $content, $from = "contact")
{
$content = preg_replace("#{TEAM_NAME}#", $team->getName(), $content);
$content = preg_replace("#{TRIGRAM}#", $team->getTrigram(), $content);
$encadrant = User::fromId($team->getEncadrantId());
if ($encadrant != null) {
$c = preg_replace("#{FIRST_NAME}#", $encadrant->getFirstName(), $content);
$c = preg_replace("#{SURNAME}#", $encadrant->getSurname(), $c);
self::sendMail($encadrant->getEmail(), $subject, $c, $from);
}
foreach ($team->getParticipants() as $participant_id) {
$participant = User::fromId($participant_id);
if ($participant == null)
continue;
$c = preg_replace("#{FIRST_NAME}#", $participant->getFirstName(), $content);
$c = preg_replace("#{SURNAME}#", $participant->getSurname(), $c);
self::sendMail($participant->getEmail(), $subject, $c, $from);
}
}
private static function broadcastToAdmins($subject, $content, $from = "contact")
{
/** @var User $admin */
foreach (User::getAdmins() as $admin) {
if (!$admin->doReceiveAnimathMails())
continue;
$c = preg_replace("#{FIRST_NAME}#", $admin->getFirstName(), $content);
$c = preg_replace("#{SURNAME}#", $admin->getSurname(), $c);
self::sendMail($admin->getEmail(), $subject, $c, $from);
}
}
private static function getTemplate($name)
{
global $LOCAL_PATH;
return file_get_contents("$LOCAL_PATH/server_files/services/mail_templates/$name.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);
self::sendMail($new_user->email, "Inscription aux Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
}
public static function sendConfirmEmail(User $user)
{
global $YEAR;
$content = self::getTemplate("confirm_email");
$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 Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
}
public static function sendChangeEmailAddressMail(User $user)
{
global $YEAR;
$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 Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
}
public static function sendForgottenPasswordProcedureMail(User $user)
{
global $YEAR;
$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é Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
}
public static function sendChangePasswordMail(User $user)
{
global $YEAR;
$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é Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
}
public static function sendAddAdminMail(NewAdmin $new_admin)
{
global $YEAR;
$content = self::getTemplate("add_admin");
$content = preg_replace("#{FIRST_NAME}#", $new_admin->first_name, $content);
$content = preg_replace("#{SURNAME}#", $new_admin->surname, $content);
$content = preg_replace("#{PASSWORD}#", $new_admin->password, $content);
self::sendMail($new_admin->email, "Ajout d'un administrateur Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
}
public static function sendAddTeamMail(User $user, Team $team)
{
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);
$content = preg_replace("#{TRIGRAM}#", $team->getTrigram(), $content);
$content = preg_replace("#{ACCESS_CODE}#", $team->getAccessCode(), $content);
if ($team->getProblem() > 0)
$content = preg_replace("#{PROBLEM}#", "pour le problème" . $team->getProblem(), $content);
else
$content = preg_replace("#{PROBLEM}#", "", $content);
self::sendMail($user->getEmail(), "Ajout d'une équipe Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
}
public static function sendJoinTeamMail(User $user, Team $team)
{
global $YEAR;
$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);
if ($team->getProblem() > 0)
$content = preg_replace("#{PROBLEM}#", "pour le problème" . $team->getProblem(), $content);
else
$content = preg_replace("#{PROBLEM}#", "", $content);
self::sendMail($user->getEmail(), "Équipe rejointe Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
}
public static function sendRequestValidationMail(Team $team)
{
global $YEAR;
$content = self::getTemplate("request_validation");
$content = preg_replace("#{TEAM_NAME}#", $team->getName(), $content);
$content = preg_replace("#{TRIGRAM}#", $team->getTrigram(), $content);
$content = preg_replace("#{PROBLEM}#", $team->getProblem(), $content);
$content = preg_replace("#{ACCESS_CODE}#", $team->getAccessCode(), $content);
self::broadcastToAdmins("Demande de validation Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
}
public static function sendValidateTeam($team, $message)
{
global $YEAR;
$content = self::getTemplate("validate_team");
if (strlen($message) > 0)
$message = " L'équipe d'organisation vous transmet le message suivant :\n\n" . $message;
$message = preg_replace("#\n#", "<br/>\n", $message);
$content = preg_replace("#{MESSAGE}#", $message, $content);
self::broadcastToTeam($team, "Équipe validée Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
}
public static function sendUnvalidateTeam($team, $message)
{
global $YEAR;
$content = self::getTemplate("unvalidate_team");
if (strlen($message) > 0)
$message = " L'équipe d'organisation vous transmet le message suivant :\n\n" . $message;
$message = preg_replace("#\n#", "<br/>\n", $message);
$content = preg_replace("#{MESSAGE}#", $message, $content);
self::broadcastToTeam($team, "Équipe non validée Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
}
public static function sendNewVideo(NewVideo $video, Team $team)
{
global $YEAR;
$content = self::getTemplate("new_video");
$content = preg_replace("#{TEAM_NAME}#", $team->getName(), $content);
$content = preg_replace("#{TRIGRAM}#", $team->getTrigram(), $content);
$content = preg_replace("#{PROBLEM}#", $team->getProblem(), $content);
$content = preg_replace("#{VIDEO_LINK}#", $video->link, $content);
self::broadcastToAdmins("Nouvelle vidéo Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
}
public static function sendNewAnswer(NewAnswer $video, Team $team)
{
global $YEAR;
$content = self::getTemplate("new_answer");
$content = preg_replace("#{TEAM_NAME}#", $team->getName(), $content);
$content = preg_replace("#{TRIGRAM}#", $team->getTrigram(), $content);
$dest = Team::fromId($team->getVideoTeamIds()[$video->team - 1]);
$content = preg_replace("#{DEST_TEAM_NAME}#", $dest->getName(), $content);
$content = preg_replace("#{DEST_TRIGRAM}#", $dest->getTrigram(), $content);
$content = preg_replace("#{PROBLEM}#", $team->getProblem(), $content);
$content = preg_replace("#{VIDEO_LINK}#", $video->link, $content);
self::broadcastToAdmins("Nouvelle vidéo de réponse Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
}
public static function validateVideo(Video $video, $message)
{
global $YEAR;
$team = Team::fromId($video->getTeam());
$template = "video_";
$template .= $video->getValidation() == Video::ACCEPTED ? "accepted" : "rejected";
$template .= $video->getReason() == Reason::SOLUTION ? "_solution" : "_answer";
$content = self::getTemplate($template);
$content = preg_replace("#{TRIGRAM}#", $team->getTrigram(), $content);
$content = preg_replace("#{PROBLEM}#", $team->getProblem(), $content);
$content = preg_replace("#{VIDEO_LINK}#", $video->getLink(), $content);
if (strlen($message) > 0)
$message = " L'équipe d'organisation vous transmet le message suivant :\n\n" . $message . "\n\n";
$message = preg_replace("#\n#", "<br/>\n", $message);
$content = preg_replace("#{MESSAGE}#", $message, $content);
self::broadcastToTeam($team, ($video->getValidation() == Video::REJECTED ? "Vidéo refusée " : "Vidéo acceptée ")
. $team->getTrigram() . " Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
}
}