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;
|
private $smtp_allowed;
|
||||||
|
|
||||||
|
const DELAY_BEFORE_RESEND = 300;
|
||||||
|
|
||||||
|
const MAILSERVICE_KEY = 'mailservice';
|
||||||
|
|
||||||
function __construct($smtp_allowed) {
|
function __construct($smtp_allowed) {
|
||||||
$this->smtp_allowed = $smtp_allowed;
|
$this->smtp_allowed = $smtp_allowed;
|
||||||
}
|
}
|
||||||
|
@ -13,10 +17,12 @@ class MailService {
|
||||||
return filter_var($email, FILTER_VALIDATE_EMAIL);
|
return filter_var($email, FILTER_VALIDATE_EMAIL);
|
||||||
}
|
}
|
||||||
|
|
||||||
function send($to, $subject, $body, $param = '') {
|
function send($to, $subject, $body, $param = '', $msgKey = null) {
|
||||||
if ($this->smtp_allowed == true) {
|
if ($this->smtp_allowed == true && $this->canSendMsg($msgKey)) {
|
||||||
mb_internal_encoding('UTF-8');
|
mb_internal_encoding('UTF-8');
|
||||||
|
|
||||||
|
// Build headers
|
||||||
|
|
||||||
$subject = mb_encode_mimeheader(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'), 'UTF-8', 'B', "\n", 9);
|
$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);
|
$encoded_app = mb_encode_mimeheader(NOMAPPLICATION, 'UTF-8', 'B', "\n", 6);
|
||||||
|
@ -39,11 +45,30 @@ class MailService {
|
||||||
$headers .= "Auto-Submitted:auto-generated\n";
|
$headers .= "Auto-Submitted:auto-generated\n";
|
||||||
$headers .= 'Return-Path: <>';
|
$headers .= 'Return-Path: <>';
|
||||||
|
|
||||||
|
// Build body
|
||||||
|
|
||||||
$body = $body . '<br/><br/>' . __('Mail', 'Thanks for your trust.') . '<br/>' . NOMAPPLICATION . '<hr/>' . __('Mail', 'FOOTER');
|
$body = $body . '<br/><br/>' . __('Mail', 'Thanks for your trust.') . '<br/>' . NOMAPPLICATION . '<hr/>' . __('Mail', 'FOOTER');
|
||||||
|
|
||||||
|
// Send mail
|
||||||
|
|
||||||
mail($to, $subject, $body, $headers, $param);
|
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;
|
use Framadate\FramaTestCase;
|
||||||
|
|
||||||
class MailServiceUnitTest extends FramaTestCase {
|
class MailServiceUnitTest extends FramaTestCase {
|
||||||
|
const MSG_KEY = '666';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
function cconstruct() {
|
function should_send_a_2nd_mail_after_a_good_interval() {
|
||||||
$this->assertEquals('a', 'a');
|
// 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'] = [];
|
$_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;
|
$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";
|
$message .= Utils::getUrlSondage($poll->admin_id, true) . "\n\n";
|
||||||
|
|
||||||
$mailService->send($poll->admin_mail, $subject, $message);
|
$messageTypeKey = $type . '-' . $poll->id;
|
||||||
|
$mailService->send($messageTypeKey, $poll->admin_mail, $subject, $message);
|
||||||
$_SESSION['mail_sent'][$poll->id] = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user