Added subscription limit in order to avoid mass subscriptions

This commit is contained in:
Jonathan Labreuille
2016-11-08 17:29:28 +01:00
parent 1220f47d87
commit 1a6b032943
5 changed files with 68 additions and 1 deletions

View File

@@ -14,6 +14,8 @@ class Subscriber extends Model {
const STATUS_UNSUBSCRIBED = 'unsubscribed';
const STATUS_UNCONFIRMED = 'unconfirmed';
const SUBSCRIPTION_LIMIT_COOLDOWN = 60;
function __construct() {
parent::__construct();
@@ -158,9 +160,27 @@ class Subscriber extends Model {
'signup_confirmation.enabled'
);
// set user ip
$subscriber_data['ip'] = (isset($_SERVER['REMOTE_ADDR']))
? $_SERVER['REMOTE_ADDR']
: null;
// make sure we don't allow too many subscriptions with the same ip address
$subscription_count = Subscriber::where('ip', $subscriber_data['ip'])
->whereRaw(
'TIME_TO_SEC(TIMEDIFF(NOW(), created_at)) < ?',
self::SUBSCRIPTION_LIMIT_COOLDOWN
)
->count();
if($subscription_count > 0) {
throw new \Exception(__('You need to wait before subscribing again.', 'mailpoet'));
}
$subscriber = self::findOne($subscriber_data['email']);
if($subscriber === false) {
// create new subscriber
$subscriber = self::createOrUpdate($subscriber_data);
if($subscriber->getErrors() !== false) {