AdminUserSubscription.php
2 months ago
Blacklist.php
1 year ago
Comment.php
2 months ago
Form.php
2 weeks ago
Manage.php
5 days ago
ManageSubscriptionFormRenderer.php
5 days ago
Pages.php
5 days ago
Registration.php
2 months ago
SubscriptionUrlFactory.php
5 days ago
Throttling.php
2 months ago
index.php
3 years ago
Throttling.php
102 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Subscription; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\SubscriberIPEntity; |
| 9 | use MailPoet\Subscribers\SubscriberIPsRepository; |
| 10 | use MailPoet\Util\Helpers; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | |
| 13 | class Throttling { |
| 14 | /** @var SubscriberIPsRepository */ |
| 15 | private $subscriberIPsRepository; |
| 16 | |
| 17 | /** @var WPFunctions */ |
| 18 | private $wp; |
| 19 | |
| 20 | public function __construct( |
| 21 | SubscriberIPsRepository $subscriberIPsRepository, |
| 22 | WPFunctions $wp |
| 23 | ) { |
| 24 | $this->wp = $wp; |
| 25 | $this->subscriberIPsRepository = $subscriberIPsRepository; |
| 26 | } |
| 27 | |
| 28 | public function throttle() { |
| 29 | $subscriptionLimitEnabled = $this->wp->applyFilters('mailpoet_subscription_limit_enabled', true); |
| 30 | |
| 31 | $window = $this->wp->applyFilters('mailpoet_subscription_limit_window', DAY_IN_SECONDS); |
| 32 | $subscriptionLimitWindow = is_numeric($window) ? (int)$window : DAY_IN_SECONDS; |
| 33 | $base = $this->wp->applyFilters('mailpoet_subscription_limit_base', MINUTE_IN_SECONDS); |
| 34 | $subscriptionLimitBase = is_numeric($base) ? (int)$base : MINUTE_IN_SECONDS; |
| 35 | |
| 36 | $subscriberIp = Helpers::getIP(); |
| 37 | |
| 38 | if ($subscriptionLimitEnabled && !$this->isUserExemptFromThrottling()) { |
| 39 | if (!empty($subscriberIp)) { |
| 40 | $subscriptionCount = $this->subscriberIPsRepository->getCountByIPAndCreatedAtAfterTimeInSeconds($subscriberIp, $subscriptionLimitWindow); |
| 41 | if ($subscriptionCount > 0) { |
| 42 | $timeout = $subscriptionLimitBase * pow(2, $subscriptionCount - 1); |
| 43 | // Cap timeout and avoid float numbers |
| 44 | $timeout = min($timeout, $subscriptionLimitWindow); |
| 45 | $existingUser = $this->subscriberIPsRepository->findOneByIPAndCreatedAtAfterTimeInSeconds($subscriberIp, $timeout); |
| 46 | if (!empty($existingUser)) { |
| 47 | return $timeout; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if ($subscriberIp !== null) { |
| 54 | $ip = new SubscriberIPEntity($subscriberIp); |
| 55 | $existingIp = $this->subscriberIPsRepository->findOneBy(['ip' => $ip->getIP(), 'createdAt' => $ip->getCreatedAt()]); |
| 56 | if (!$existingIp) { |
| 57 | $this->subscriberIPsRepository->persist($ip); |
| 58 | $this->subscriberIPsRepository->flush(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | $this->purge(); |
| 63 | |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | public function purge(): void { |
| 68 | $interval = $this->wp->applyFilters('mailpoet_subscription_purge_window', MONTH_IN_SECONDS); |
| 69 | $purgeWindow = is_numeric($interval) ? (int)$interval : MONTH_IN_SECONDS; |
| 70 | $this->subscriberIPsRepository->deleteCreatedAtBeforeTimeInSeconds($purgeWindow); |
| 71 | } |
| 72 | |
| 73 | public function secondsToTimeString($seconds): string { |
| 74 | $hrs = floor($seconds / 3600); |
| 75 | $min = floor($seconds % 3600 / 60); |
| 76 | $sec = $seconds % 3600 % 60; |
| 77 | $result = [ |
| 78 | // translators: %s is the number of hours |
| 79 | 'hours' => $hrs ? sprintf(__('%d hours', 'mailpoet'), $hrs) : '', |
| 80 | // translators: %s is the number of minutes |
| 81 | 'minutes' => $min ? sprintf(__('%d minutes', 'mailpoet'), $min) : '', |
| 82 | // translators: %s is the number of seconds |
| 83 | 'seconds' => $sec ? sprintf(__('%d seconds', 'mailpoet'), $sec) : '', |
| 84 | ]; |
| 85 | return join(' ', array_filter($result)); |
| 86 | } |
| 87 | |
| 88 | private function isUserExemptFromThrottling(): bool { |
| 89 | if (!$this->wp->isUserLoggedIn()) { |
| 90 | return false; |
| 91 | } |
| 92 | $user = $this->wp->wpGetCurrentUser(); |
| 93 | $roles = $this->wp->applyFilters('mailpoet_subscription_throttling_exclude_roles', ['administrator', 'editor']); |
| 94 | if (!is_array($roles)) { |
| 95 | $roles = ['administrator', 'editor']; |
| 96 | } |
| 97 | $roles = array_values(array_filter($roles, 'is_string')); |
| 98 | $userRoles = array_values(array_filter((array)$user->roles, 'is_string')); |
| 99 | return !empty(array_intersect($roles, $userRoles)); |
| 100 | } |
| 101 | } |
| 102 |