Skip to content
This repository has been archived by the owner on Mar 30, 2024. It is now read-only.

Commit

Permalink
Mail Spam Protect
Browse files Browse the repository at this point in the history
  • Loading branch information
kimbtech committed Jan 6, 2020
1 parent abba320 commit 6d56e25
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 13 deletions.
4 changes: 2 additions & 2 deletions VERSION
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
latest
1.1.8
1.1
1.2.0
1.2
1
32 changes: 29 additions & 3 deletions core/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@
*/
class Mail {

/**
* The system uses some type of spam protection.
* One can not send too many mails to one address.
*/
const LAST_TIMES = 3; // check times of last x mails to this address
const LAST_SECONDS = 3600; // allow LAST_TIMES mails in last y seconds

private static $templates = array(
'mailAdminNotif',
'mailNewPollNotif',
'mailPollSubm'
);

private $type, $template, $mailHeader;
private $type, $template, $mailHeader, $maillog;

/**
* Creates the Mail (also using a HTML Template)
Expand All @@ -35,6 +42,8 @@ public function __construct( string $mailType ){
}
$this->template = new Template( $this->type );

$this->maillog = new JSONReader('mail');

$this->setUpMailMeta();
}

Expand Down Expand Up @@ -74,9 +83,26 @@ private function setUpMailMeta(){
/**
* Sends the created Mail
* @param $to The destination mail address
* @param $force Force to send the mail (also if many mail send to this address in the last time)
*/
public function sendMail(string $to){
mail(
public function sendMail(string $to, bool $force = false){
if( $this->maillog->isValue( [$to] ) && !$force ){
$lastx = array_slice( $this->maillog->getValue( [$to] ), -self::LAST_TIMES);
if(
count($lastx) == self::LAST_TIMES && $lastx[0] + self::LAST_SECONDS > time()
){
return false;
}
}

if( $this->maillog->isValue( [$to] ) ){
$this->maillog->setValue( [$to, null], time() );
}
else {
$this->maillog->setValue( [$to], array(time()) );
}

return mail(
$to,
LanguageManager::getTranslation($this->type),
$this->template->getOutputString(),
Expand Down
2 changes: 1 addition & 1 deletion core/PollCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private function sendNewPollMail() {
$m->setContent('POLLLINK', URL::generateLink('poll', $this->data['code']['poll'], '' ));
$m->setContent('POLLID', $this->data['code']['poll'] );

$m->sendMail( $to );
$m->sendMail( $to, true );
}
}

Expand Down
16 changes: 12 additions & 4 deletions core/SubmissionQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,20 @@ private function checkForPost(){
}
}
}
$this->mailsend = true;
if( !empty($mailsubs) ){ // found entry?
$this->doMail( $mailsubs, $email );
if( !$this->doMail( $mailsubs, $email ) ){
$this->mailsend = false;

$alert = new Template( 'alert' );
$this->template->includeTemplate($alert);
$alert->setContent( 'ALERTMESSAGE', LanguageManager::getTranslation('MailTimeout') );

return;
}
}
usleep(random_int(200000,800000)); // prevent timing attacks

$this->mailsend = true;

return;
}
}
Expand Down Expand Up @@ -117,7 +125,7 @@ private function doMail( array $d, string $email ){
}
$m->setMultipleContent( "Items", $items );

$m->sendMail($email);
return $m->sendMail($email);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Utilities {
/**
* The system's Version
*/
const SYS_VERSION = 'v1.1.8';
const SYS_VERSION = 'v1.2.0';

/**
* Possible chars for:
Expand Down
3 changes: 2 additions & 1 deletion data/translation_de.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
"mailNewPollNotif": "Neue Umfrage erstellt",
"FillAdditionals": "Bitte alle Felder füllen, nicht als optional gekennzeichnete Felder sind Pflichtfelder.",
"InvalMail": "Die eingegebene E-Mail-Adresse erfüllt nicht die Anforderungen!",
"mailPollSubm": "Liste meiner Eintraege"
"mailPollSubm": "Liste meiner Eintraege",
"MailTimeout": "Mailversand an die Adresse fehlgeschlagen, da zu viele Mails an diese Adresse geschickt wurden. Versuchen Sie es später erneut."
}
3 changes: 2 additions & 1 deletion data/translation_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
"mailNewPollNotif": "A new Poll was created",
"FillAdditionals": "Please fill in all values, fields not marked as optional are required.",
"InvalMail": "The given E-Mail-Address does not fullfil the requirements!",
"mailPollSubm": "List of my entries"
"mailPollSubm": "List of my entries",
"MailTimeout": "Error while sending mail, because too many mails have been send to this address. Please try again later."
}

0 comments on commit 6d56e25

Please sign in to comment.