| Server IP : 38.190.208.107 / Your IP : 216.73.217.46 Web Server : nginx/1.28.1 System : Linux ht2026040333114 6.1.0-10-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.37-1 (2023-07-03) x86_64 User : root ( 0) PHP Version : 8.2.28 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv,eval,assert,passthru,system,exec,shell_exec,popen,proc_open MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /www/wwwroot/wp.5/wp-content/plugins/woocommerce-payments/includes/ |
Upload File : |
<?php
/**
* Class Session_Rate_Limiter
*
* @package WooCommerce\Payments
*/
namespace WCPay;
defined( 'ABSPATH' ) || exit; // block direct access.
/**
* A wrapper class for keeping track of events in registries, and to trigger a rate limiter after a threshold.
*/
class Session_Rate_Limiter {
/**
* Key used in the session to store card declined transactions.
*
* @type string
*/
const SESSION_KEY_DECLINED_CARD_REGISTRY = 'wcpay_card_declined_registry';
/**
* Key used to store the registry in the session
*
* @var string
*/
protected $key;
/**
* Number of elements in the registry needed to enable the rate limiter
*
* @var int
*/
protected $threshold;
/**
* Number of seconds the limiter is enabled for after the threshold is reached
*
* @var int
*/
protected $delay;
/**
* Session_Rate_Limiter constructor.
*
* @param string $key - Key for the registry.
* @param int $threshold - Number of elements in the registry before enabling the limiter.
* @param int $delay - Number of seconds the limiter will be in use after threshold is reached.
*/
public function __construct(
$key,
$threshold,
$delay
) {
$this->key = $key;
$this->threshold = $threshold;
$this->delay = $delay;
}
/**
* Saves an event in an specified registry using a key.
* If the number of events in the registry match the threshold,
* a new rate limiter is enabled with the given delay.
*
* The registry of declined card attemps is cleaned after a new rate limiter is enabled.
*/
public function bump() {
if ( ! isset( WC()->session ) ) {
return;
}
$registry = WC()->session->get( $this->key ) ?? [];
$registry[] = time();
WC()->session->set( $this->key, $registry );
}
/**
* Checks if the rate limiter is enabled.
*
* Returns a boolean.
*
* @return bool The rate limiter is in use.
*/
public function is_limited(): bool {
if ( ! isset( WC()->session ) ) {
return false;
}
if ( 'yes' === get_option( 'wcpay_session_rate_limiter_disabled_' . $this->key ) ) {
return false;
}
$registry = WC()->session->get( $this->key ) ?? [];
if ( ( is_countable( $registry ) ? count( $registry ) : 0 ) >= $this->threshold ) {
$start_time_limiter = end( $registry );
$next_try_allowed_at = $start_time_limiter + $this->delay;
$is_limited = time() <= $next_try_allowed_at;
if ( ! $is_limited ) {
WC()->session->set( $this->key, [] );
}
return $is_limited;
}
return false;
}
}