mirror of
https://github.com/PrivateBin/PrivateBin.git
synced 2024-03-22 13:10:41 +08:00
folding Persistance\TrafficLimiter into Data\Filesystem
This commit is contained in:
parent
3429d293d3
commit
b5a6ce323e
|
@ -198,6 +198,7 @@ class Controller
|
|||
try {
|
||||
// Ensure last paste from visitors IP address was more than configured amount of seconds ago.
|
||||
TrafficLimiter::setConfiguration($this->_conf);
|
||||
TrafficLimiter::setStore($this->_model->getStore());
|
||||
if (!TrafficLimiter::canPass()) {
|
||||
$this->_return_message(
|
||||
1, I18n::_(
|
||||
|
|
|
@ -130,6 +130,16 @@ abstract class AbstractData
|
|||
*/
|
||||
abstract public function existsComment($pasteid, $parentid, $commentid);
|
||||
|
||||
/**
|
||||
* Purge outdated entries.
|
||||
*
|
||||
* @access public
|
||||
* @param string $namespace
|
||||
* @param int $time
|
||||
* @return void
|
||||
*/
|
||||
abstract public function purgeValues($namespace, $time);
|
||||
|
||||
/**
|
||||
* Save a value.
|
||||
*
|
||||
|
|
|
@ -423,6 +423,23 @@ class Database extends AbstractData
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge outdated entries.
|
||||
*
|
||||
* @access public
|
||||
* @param string $namespace
|
||||
* @param int $time
|
||||
* @return void
|
||||
*/
|
||||
public function purgeValues($namespace, $time)
|
||||
{
|
||||
switch ($namespace) {
|
||||
case 'traffic_limiter':
|
||||
;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a value.
|
||||
*
|
||||
|
|
|
@ -38,6 +38,15 @@ class Filesystem extends AbstractData
|
|||
*/
|
||||
private static $_path = 'data';
|
||||
|
||||
/**
|
||||
* cache for the traffic limiter
|
||||
*
|
||||
* @access private
|
||||
* @static
|
||||
* @var array
|
||||
*/
|
||||
private static $_traffic_limiter_cache = array();
|
||||
|
||||
/**
|
||||
* get instance of singleton
|
||||
*
|
||||
|
@ -240,6 +249,27 @@ class Filesystem extends AbstractData
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge outdated entries.
|
||||
*
|
||||
* @access public
|
||||
* @param string $namespace
|
||||
* @param int $time
|
||||
* @return void
|
||||
*/
|
||||
public function purgeValues($namespace, $time)
|
||||
{
|
||||
switch ($namespace) {
|
||||
case 'traffic_limiter':
|
||||
foreach (self::$_traffic_limiter_cache as $key => $last_access) {
|
||||
if ($last_access <= $time) {
|
||||
unset(self::$_traffic_limiter_cache[$key]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a value.
|
||||
*
|
||||
|
@ -262,7 +292,11 @@ class Filesystem extends AbstractData
|
|||
;
|
||||
break;
|
||||
case 'traffic_limiter':
|
||||
;
|
||||
self::$_traffic_limiter_cache[$key] = $value;
|
||||
return self::_storeString(
|
||||
self::$_path . DIRECTORY_SEPARATOR . 'traffic_limiter.php',
|
||||
'<?php' . PHP_EOL . '$GLOBALS[\'purge_limiter\'] = ' . var_export(self::$_traffic_limiter_cache, true) . ';'
|
||||
);
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
|
@ -290,7 +324,14 @@ class Filesystem extends AbstractData
|
|||
;
|
||||
break;
|
||||
case 'traffic_limiter':
|
||||
;
|
||||
$file = self::$_path . DIRECTORY_SEPARATOR . 'traffic_limiter.php';
|
||||
if (is_file($file)) {
|
||||
require $file;
|
||||
self::$_traffic_limiter_cache = $GLOBALS['traffic_limiter'];
|
||||
if (array_key_exists($key, self::$_traffic_limiter_cache)) {
|
||||
return self::$_traffic_limiter_cache[$key];
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return '';
|
||||
|
|
|
@ -217,6 +217,23 @@ class GoogleCloudStorage extends AbstractData
|
|||
return $o->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge outdated entries.
|
||||
*
|
||||
* @access public
|
||||
* @param string $namespace
|
||||
* @param int $time
|
||||
* @return void
|
||||
*/
|
||||
public function purgeValues($namespace, $time)
|
||||
{
|
||||
switch ($namespace) {
|
||||
case 'traffic_limiter':
|
||||
;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the simplest thing that could possibly work.
|
||||
* will be to tested for runtime performance.
|
||||
|
|
|
@ -54,7 +54,7 @@ class Model
|
|||
*/
|
||||
public function getPaste($pasteId = null)
|
||||
{
|
||||
$paste = new Paste($this->_conf, $this->_getStore());
|
||||
$paste = new Paste($this->_conf, $this->getStore());
|
||||
if ($pasteId !== null) {
|
||||
$paste->setId($pasteId);
|
||||
}
|
||||
|
@ -67,9 +67,9 @@ class Model
|
|||
public function purge()
|
||||
{
|
||||
PurgeLimiter::setConfiguration($this->_conf);
|
||||
PurgeLimiter::setStore($this->_getStore());
|
||||
PurgeLimiter::setStore($this->getStore());
|
||||
if (PurgeLimiter::canPurge()) {
|
||||
$this->_getStore()->purge($this->_conf->getKey('batchsize', 'purge'));
|
||||
$this->getStore()->purge($this->_conf->getKey('batchsize', 'purge'));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -78,7 +78,7 @@ class Model
|
|||
*
|
||||
* @return Data\AbstractData
|
||||
*/
|
||||
private function _getStore()
|
||||
public function getStore()
|
||||
{
|
||||
if ($this->_store === null) {
|
||||
$this->_store = forward_static_call(
|
||||
|
|
|
@ -169,35 +169,18 @@ class TrafficLimiter extends AbstractPersistence
|
|||
}
|
||||
}
|
||||
|
||||
$file = 'traffic_limiter.php';
|
||||
if (self::_exists($file)) {
|
||||
require self::getPath($file);
|
||||
$tl = $GLOBALS['traffic_limiter'];
|
||||
} else {
|
||||
$tl = array();
|
||||
}
|
||||
|
||||
// purge file of expired hashes to keep it small
|
||||
$now = time();
|
||||
foreach ($tl as $key => $time) {
|
||||
if ($time + self::$_limit < $now) {
|
||||
unset($tl[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// this hash is used as an array key, hence a shorter algo is used
|
||||
$hash = self::getHash('sha256');
|
||||
if (array_key_exists($hash, $tl) && ($tl[$hash] + self::$_limit >= $now)) {
|
||||
$now = time();
|
||||
$tl = self::$_store->getValue('traffic_limiter', $hash);
|
||||
self::$_store->purgeValues('traffic_limiter', $now - self::$_limit);
|
||||
if ($tl > 0 && ($tl + self::$_limit >= $now)) {
|
||||
$result = false;
|
||||
} else {
|
||||
$tl[$hash] = time();
|
||||
$tl = time();
|
||||
$result = true;
|
||||
}
|
||||
self::_store(
|
||||
$file,
|
||||
'<?php' . PHP_EOL .
|
||||
'$GLOBALS[\'traffic_limiter\'] = ' . var_export($tl, true) . ';'
|
||||
);
|
||||
self::$_store->setValue((string) $tl, 'traffic_limiter');
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ class ControllerTest extends PHPUnit_Framework_TestCase
|
|||
/* Setup Routine */
|
||||
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
|
||||
$this->_data = Filesystem::getInstance(array('dir' => $this->_path));
|
||||
TrafficLimiter::setStore($this->_data);
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user