Move throttling out of the Subscriber model to the API 'subscribe' method [MAILPOET-1115]

This commit is contained in:
stoletniy
2017-09-28 16:26:22 +03:00
committed by pavel-mailpoet
parent 8a91eb46e6
commit 9f5fc151b4
4 changed files with 23 additions and 18 deletions

View File

@ -10,10 +10,13 @@ use MailPoet\Form\Util\FieldNameObfuscator;
use MailPoet\Models\Form;
use MailPoet\Models\StatisticsForms;
use MailPoet\Models\Subscriber;
use MailPoet\Util\Helpers;
if(!defined('ABSPATH')) exit;
class Subscribers extends APIEndpoint {
const SUBSCRIPTION_LIMIT_COOLDOWN = 60;
public $permissions = array(
'global' => AccessControl::PERMISSION_MANAGE_SUBSCRIBERS,
'methods' => array('subscribe' => AccessControl::NO_ACCESS_RESTRICTION)
@ -94,6 +97,19 @@ class Subscribers extends APIEndpoint {
$form_fields = $form->getFieldList();
$data = array_intersect_key($data, array_flip($form_fields));
// make sure we don't allow too many subscriptions with the same ip address
$subscription_count = Subscriber::where(
'subscribed_ip',
Helpers::getIP()
)->whereRaw(
'(TIME_TO_SEC(TIMEDIFF(NOW(), created_at)) < ? OR TIME_TO_SEC(TIMEDIFF(NOW(), updated_at)) < ?)',
array(self::SUBSCRIPTION_LIMIT_COOLDOWN, self::SUBSCRIPTION_LIMIT_COOLDOWN)
)->count();
if($subscription_count > 0) {
throw new \Exception(__('You need to wait before subscribing again.', 'mailpoet'));
}
$subscriber = Subscriber::subscribe($data, $segment_ids);
$errors = $subscriber->getErrors();

View File

@ -14,7 +14,6 @@ class Subscriber extends Model {
const STATUS_UNSUBSCRIBED = 'unsubscribed';
const STATUS_UNCONFIRMED = 'unconfirmed';
const STATUS_BOUNCED = 'bounced';
const SUBSCRIPTION_LIMIT_COOLDOWN = 60;
const SUBSCRIBER_TOKEN_LENGTH = 6;
function __construct() {
@ -175,22 +174,7 @@ class Subscriber extends Model {
'signup_confirmation.enabled'
);
$subscriber_data['subscribed_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(
'subscribed_ip',
$subscriber_data['subscribed_ip']
)->whereRaw(
'(TIME_TO_SEC(TIMEDIFF(NOW(), created_at)) < ? OR TIME_TO_SEC(TIMEDIFF(NOW(), updated_at)) < ?)',
array(self::SUBSCRIPTION_LIMIT_COOLDOWN, self::SUBSCRIPTION_LIMIT_COOLDOWN)
)->count();
if($subscription_count > 0) {
throw new \Exception(__('You need to wait before subscribing again.', 'mailpoet'));
}
$subscriber_data['subscribed_ip'] = Helpers::getIP();
$subscriber = self::findOne($subscriber_data['email']);

View File

@ -64,7 +64,7 @@ class Pages {
$subscriber_data = $this->subscriber->getUnconfirmedData();
$this->subscriber->status = Subscriber::STATUS_SUBSCRIBED;
$this->subscriber->confirmed_ip = (!empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : null;
$this->subscriber->confirmed_ip = Helpers::getIP();
$this->subscriber->setExpr('confirmed_at', 'NOW()');
$this->subscriber->unconfirmed_data = null;
$this->subscriber->save();

View File

@ -146,4 +146,9 @@ class Helpers {
return explode(self::DIVIDER, $object);
}
static function getIP() {
return (isset($_SERVER['REMOTE_ADDR']))
? $_SERVER['REMOTE_ADDR']
: null;
}
}