Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/wordfenc.../modules/login-se.../classes/model
File: tokenbucket.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace WordfenceLS;
[2] Fix | Delete
[3] Fix | Delete
class Model_TokenBucket {
[4] Fix | Delete
/* Constants to map from tokens per unit to tokens per second */
[5] Fix | Delete
const MICROSECOND = 0.000001;
[6] Fix | Delete
const MILLISECOND = 0.001;
[7] Fix | Delete
const SECOND = 1;
[8] Fix | Delete
const MINUTE = 60;
[9] Fix | Delete
const HOUR = 3600;
[10] Fix | Delete
const DAY = 86400;
[11] Fix | Delete
const WEEK = 604800;
[12] Fix | Delete
const MONTH = 2629743.83;
[13] Fix | Delete
const YEAR = 31556926;
[14] Fix | Delete
[15] Fix | Delete
const BACKING_REDIS = 'redis';
[16] Fix | Delete
const BACKING_WP_OPTIONS = 'wpoptions';
[17] Fix | Delete
[18] Fix | Delete
private $_identifier;
[19] Fix | Delete
private $_bucketSize;
[20] Fix | Delete
private $_tokensPerSecond;
[21] Fix | Delete
[22] Fix | Delete
private $_backing;
[23] Fix | Delete
private $_redis;
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Model_TokenBucket constructor.
[27] Fix | Delete
*
[28] Fix | Delete
* @param string $identifier The identifier for the bucket record in the database
[29] Fix | Delete
* @param int $bucketSize The maximum capacity of the bucket.
[30] Fix | Delete
* @param double $tokensPerSecond The number of tokens per second added to the bucket.
[31] Fix | Delete
* @param string $backing The backing storage to use.
[32] Fix | Delete
*/
[33] Fix | Delete
public function __construct($identifier, $bucketSize, $tokensPerSecond, $backing = self::BACKING_WP_OPTIONS) {
[34] Fix | Delete
$this->_identifier = $identifier;
[35] Fix | Delete
$this->_bucketSize = $bucketSize;
[36] Fix | Delete
$this->_tokensPerSecond = $tokensPerSecond;
[37] Fix | Delete
$this->_backing = $backing;
[38] Fix | Delete
[39] Fix | Delete
if ($backing == self::BACKING_REDIS) {
[40] Fix | Delete
$this->_redis = new \Redis();
[41] Fix | Delete
$this->_redis->pconnect('127.0.0.1');
[42] Fix | Delete
}
[43] Fix | Delete
}
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* Attempts to acquire a lock for the bucket.
[47] Fix | Delete
*
[48] Fix | Delete
* @param int $timeout
[49] Fix | Delete
* @return bool Whether or not the lock was acquired.
[50] Fix | Delete
*/
[51] Fix | Delete
private function _lock($timeout = 30) {
[52] Fix | Delete
if ($this->_backing == self::BACKING_WP_OPTIONS) {
[53] Fix | Delete
$start = microtime(true);
[54] Fix | Delete
while (!$this->_wp_options_create_lock($this->_identifier)) {
[55] Fix | Delete
if (microtime(true) - $start > $timeout) {
[56] Fix | Delete
return false;
[57] Fix | Delete
}
[58] Fix | Delete
usleep(5000); // 5 ms
[59] Fix | Delete
}
[60] Fix | Delete
return true;
[61] Fix | Delete
}
[62] Fix | Delete
else if ($this->_backing == self::BACKING_REDIS) {
[63] Fix | Delete
if ($this->_redis === false) {
[64] Fix | Delete
return false;
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
$start = microtime(true);
[68] Fix | Delete
while (!$this->_redis->setnx('lock:' . $this->_identifier, '1')) {
[69] Fix | Delete
if (microtime(true) - $start > $timeout) {
[70] Fix | Delete
return false;
[71] Fix | Delete
}
[72] Fix | Delete
usleep(5000); // 5 ms
[73] Fix | Delete
}
[74] Fix | Delete
$this->_redis->expire('lock:' . $this->_identifier, 30);
[75] Fix | Delete
return true;
[76] Fix | Delete
}
[77] Fix | Delete
return false;
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
private function _unlock() {
[81] Fix | Delete
if ($this->_backing == self::BACKING_WP_OPTIONS) {
[82] Fix | Delete
$this->_wp_options_release_lock($this->_identifier);
[83] Fix | Delete
}
[84] Fix | Delete
else if ($this->_backing == self::BACKING_REDIS) {
[85] Fix | Delete
if ($this->_redis === false) {
[86] Fix | Delete
return;
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
$this->_redis->del('lock:' . $this->_identifier);
[90] Fix | Delete
}
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
private function _wp_options_create_lock($name, $timeout = null) { //Our own version of WP_Upgrader::create_lock
[94] Fix | Delete
global $wpdb;
[95] Fix | Delete
[96] Fix | Delete
if (!$timeout) {
[97] Fix | Delete
$timeout = 3600;
[98] Fix | Delete
}
[99] Fix | Delete
[100] Fix | Delete
$lock_option = 'wfls_' . $name . '.lock';
[101] Fix | Delete
$lock_result = $wpdb->query($wpdb->prepare("INSERT IGNORE INTO `{$wpdb->options}` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no')", $lock_option, time()));
[102] Fix | Delete
[103] Fix | Delete
if (!$lock_result) {
[104] Fix | Delete
$lock_result = get_option($lock_option);
[105] Fix | Delete
if (!$lock_result) {
[106] Fix | Delete
return false;
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
if ($lock_result > (time() - $timeout)) {
[110] Fix | Delete
return false;
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
$this->_wp_options_release_lock($name);
[114] Fix | Delete
return $this->_wp_options_create_lock($name, $timeout);
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
return true;
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
private function _wp_options_release_lock($name) {
[121] Fix | Delete
return delete_option('wfls_' . $name . '.lock');
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
/**
[125] Fix | Delete
* Atomically checks the available token count, creating the initial record if needed, and updates the available token count if the requested number of tokens is available.
[126] Fix | Delete
*
[127] Fix | Delete
* @param int $tokenCount
[128] Fix | Delete
* @return bool Whether or not there were enough tokens to satisfy the request.
[129] Fix | Delete
*/
[130] Fix | Delete
public function consume($tokenCount = 1) {
[131] Fix | Delete
if (!$this->_lock()) { return false; }
[132] Fix | Delete
[133] Fix | Delete
if ($this->_backing == self::BACKING_WP_OPTIONS) {
[134] Fix | Delete
$record = get_transient('wflsbucket:' . $this->_identifier);
[135] Fix | Delete
}
[136] Fix | Delete
else if ($this->_backing == self::BACKING_REDIS) {
[137] Fix | Delete
$record = $this->_redis->get('bucket:' . $this->_identifier);
[138] Fix | Delete
}
[139] Fix | Delete
else {
[140] Fix | Delete
$this->_unlock();
[141] Fix | Delete
return false;
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
if ($record === false) {
[145] Fix | Delete
if ($tokenCount > $this->_bucketSize) {
[146] Fix | Delete
$this->_unlock();
[147] Fix | Delete
return false;
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
$this->_bootstrap($this->_bucketSize - $tokenCount);
[151] Fix | Delete
$this->_unlock();
[152] Fix | Delete
return true;
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
$tokens = min($this->_secondsToTokens(microtime(true) - (float) $record), $this->_bucketSize);
[156] Fix | Delete
if ($tokenCount > $tokens) {
[157] Fix | Delete
$this->_unlock();
[158] Fix | Delete
return false;
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
if ($this->_backing == self::BACKING_WP_OPTIONS) {
[162] Fix | Delete
set_transient('wflsbucket:' . $this->_identifier, (string) (microtime(true) - $this->_tokensToSeconds($tokens - $tokenCount)), ceil($this->_tokensToSeconds($this->_bucketSize)));
[163] Fix | Delete
}
[164] Fix | Delete
else if ($this->_backing == self::BACKING_REDIS) {
[165] Fix | Delete
$this->_redis->set('bucket:' . $this->_identifier, (string) (microtime(true) - $this->_tokensToSeconds($tokens - $tokenCount)));
[166] Fix | Delete
}
[167] Fix | Delete
[168] Fix | Delete
$this->_unlock();
[169] Fix | Delete
return true;
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
public function reset() {
[173] Fix | Delete
if (!$this->_lock()) { return false; }
[174] Fix | Delete
[175] Fix | Delete
if ($this->_backing == self::BACKING_WP_OPTIONS) {
[176] Fix | Delete
delete_transient('wflsbucket:' . $this->_identifier);
[177] Fix | Delete
}
[178] Fix | Delete
else if ($this->_backing == self::BACKING_REDIS) {
[179] Fix | Delete
$this->_redis->del('bucket:' . $this->_identifier);
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
$this->_unlock();
[183] Fix | Delete
}
[184] Fix | Delete
[185] Fix | Delete
/**
[186] Fix | Delete
* Creates an initial record with the given number of tokens.
[187] Fix | Delete
*
[188] Fix | Delete
* @param int $initialTokens
[189] Fix | Delete
*/
[190] Fix | Delete
protected function _bootstrap($initialTokens) {
[191] Fix | Delete
$microtime = microtime(true) - $this->_tokensToSeconds($initialTokens);
[192] Fix | Delete
if ($this->_backing == self::BACKING_WP_OPTIONS) {
[193] Fix | Delete
set_transient('wflsbucket:' . $this->_identifier, (string) $microtime, ceil($this->_tokensToSeconds($this->_bucketSize)));
[194] Fix | Delete
}
[195] Fix | Delete
else if ($this->_backing == self::BACKING_REDIS) {
[196] Fix | Delete
$this->_redis->set('bucket:' . $this->_identifier, (string) $microtime);
[197] Fix | Delete
}
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
protected function _tokensToSeconds($tokens) {
[201] Fix | Delete
return $tokens / $this->_tokensPerSecond;
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
protected function _secondsToTokens($seconds) {
[205] Fix | Delete
return (int) $seconds * $this->_tokensPerSecond;
[206] Fix | Delete
}
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function