MailService now manage the anti-flood system
This commit is contained in:
parent
92d84f3193
commit
be155aa2a1
|
@ -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 . '<br/><br/>' . __('Mail', 'Thanks for your trust.') . '<br/>' . NOMAPPLICATION . '<hr/>' . __('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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue