From be155aa2a1e11e847a77892683a4c6ca5126b020 Mon Sep 17 00:00:00 2001 From: Olivier PEREZ Date: Fri, 29 May 2015 17:06:03 +0200 Subject: [PATCH] MailService now manage the anti-flood system --- .../Framadate/Services/MailService.php | 29 +++++++++++++++++-- .../Services/MailServiceUnitTest.php | 28 ++++++++++++++++-- studs.php | 7 ++--- 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/app/classes/Framadate/Services/MailService.php b/app/classes/Framadate/Services/MailService.php index 2b7dd66..51d343d 100644 --- a/app/classes/Framadate/Services/MailService.php +++ b/app/classes/Framadate/Services/MailService.php @@ -5,6 +5,10 @@ class MailService { private $smtp_allowed; + const DELAY_BEFORE_RESEND = 300; + + const MAILSERVICE_KEY = 'mailservice'; + function __construct($smtp_allowed) { $this->smtp_allowed = $smtp_allowed; } @@ -13,10 +17,12 @@ class MailService { return filter_var($email, FILTER_VALIDATE_EMAIL); } - function send($to, $subject, $body, $param = '') { - if ($this->smtp_allowed == true) { + function send($to, $subject, $body, $param = '', $msgKey = null) { + if ($this->smtp_allowed == true && $this->canSendMsg($msgKey)) { mb_internal_encoding('UTF-8'); + // Build headers + $subject = mb_encode_mimeheader(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'), 'UTF-8', 'B', "\n", 9); $encoded_app = mb_encode_mimeheader(NOMAPPLICATION, 'UTF-8', 'B', "\n", 6); @@ -39,11 +45,30 @@ class MailService { $headers .= "Auto-Submitted:auto-generated\n"; $headers .= 'Return-Path: <>'; + // Build body + $body = $body . '

' . __('Mail', 'Thanks for your trust.') . '
' . NOMAPPLICATION . '
' . __('Mail', 'FOOTER'); + // Send mail + mail($to, $subject, $body, $headers, $param); + + // Set date before resend in sessions + + $_SESSION[self::MAILSERVICE_KEY][$msgKey] = time() + self::DELAY_BEFORE_RESEND; } } + function canSendMsg($msgKey) { + if ($msgKey == null) { + return true; + } + + if (!isset($_SESSION[self::MAILSERVICE_KEY])) { + $_SESSION[self::MAILSERVICE_KEY] = []; + } + return !isset($_SESSION[self::MAILSERVICE_KEY][$msgKey]) || $_SESSION[self::MAILSERVICE_KEY][$msgKey] < time(); + } + } \ No newline at end of file diff --git a/app/tests/Framadate/Services/MailServiceUnitTest.php b/app/tests/Framadate/Services/MailServiceUnitTest.php index 70e99c6..fe22ebe 100644 --- a/app/tests/Framadate/Services/MailServiceUnitTest.php +++ b/app/tests/Framadate/Services/MailServiceUnitTest.php @@ -4,12 +4,36 @@ namespace Framadate\Services; use Framadate\FramaTestCase; class MailServiceUnitTest extends FramaTestCase { + const MSG_KEY = '666'; /** * @test */ - function cconstruct() { - $this->assertEquals('a', 'a'); + function should_send_a_2nd_mail_after_a_good_interval() { + // Given + $mailService = new MailService(true); + $_SESSION[MailService::MAILSERVICE_KEY] = [self::MSG_KEY => time() - 500]; + + // When + $canSendMsg = $mailService->canSendMsg(self::MSG_KEY); + + // Then + $this->assertEquals(true, $canSendMsg); + } + + /** + * @test + */ + function should_not_send_2_mails_in_a_short_interval() { + // Given + $mailService = new MailService(true); + $_SESSION[MailService::MAILSERVICE_KEY] = [self::MSG_KEY => time() + 500]; + + // When + $canSendMsg = $mailService->canSendMsg(self::MSG_KEY); + + // Then + $this->assertEquals(false, $canSendMsg); } } diff --git a/studs.php b/studs.php index 0bcf460..bab6141 100644 --- a/studs.php +++ b/studs.php @@ -64,7 +64,7 @@ function sendUpdateNotification($poll, $mailService, $name, $type) { $_SESSION['mail_sent'] = []; } - if ($poll->receiveNewVotes && (!isset($_SESSION['mail_sent'][$poll->id]) || $_SESSION['mail_sent'][$poll->id] !== true)) { + if ($poll->receiveNewVotes) { $subject = '[' . NOMAPPLICATION . '] ' . __('Mail', 'Poll\'s participation') . ' : ' . $poll->title; @@ -82,9 +82,8 @@ function sendUpdateNotification($poll, $mailService, $name, $type) { } $message .= Utils::getUrlSondage($poll->admin_id, true) . "\n\n"; - $mailService->send($poll->admin_mail, $subject, $message); - - $_SESSION['mail_sent'][$poll->id] = true; + $messageTypeKey = $type . '-' . $poll->id; + $mailService->send($messageTypeKey, $poll->admin_mail, $subject, $message); } }