folding Persistance\PurgeLimiter into Data\Filesystem

This commit is contained in:
El RIDO 2021-06-07 21:53:42 +02:00
parent f46221e7c3
commit ae486d651b
No known key found for this signature in database
GPG Key ID: 0F5C940A6BD81F92
5 changed files with 94 additions and 46 deletions

View File

@ -253,7 +253,10 @@ class Filesystem extends AbstractData
{
switch ($namespace) {
case 'purge_limiter':
;
return self::_storeString(
self::$_path . DIRECTORY_SEPARATOR . 'purge_limiter.php',
'<?php' . PHP_EOL . '$GLOBALS[\'purge_limiter\'] = ' . $value . ';'
);
break;
case 'salt':
;
@ -261,10 +264,8 @@ class Filesystem extends AbstractData
case 'traffic_limiter':
;
break;
default:
return false;
break;
}
return false;
}
/**
@ -277,7 +278,40 @@ class Filesystem extends AbstractData
*/
public function getValue($namespace, $key = '')
{
switch ($namespace) {
case 'purge_limiter':
$file = self::$_path . DIRECTORY_SEPARATOR . 'purge_limiter.php';
if (is_file($file)) {
require $file;
return $GLOBALS['purge_limiter'];
}
break;
case 'salt':
;
break;
case 'traffic_limiter':
;
break;
}
return '';
}
/**
* get the data
*
* @access public
* @static
* @param string $filename
* @return array|false $data
*/
private static function _get($filename)
{
return Json::decode(
substr(
file_get_contents($filename),
strlen(self::PROTECTION_LINE . PHP_EOL)
)
);
}
/**
@ -427,16 +461,33 @@ class Filesystem extends AbstractData
* @param array $data
* @return bool
*/
private static function _store($filename, $data)
private static function _store($filename, array $data)
{
if (strpos($filename, self::$_path) === 0) {
$filename = substr($filename, strlen(self::$_path));
try {
return self::_storeString(
$filename,
self::PROTECTION_LINE . PHP_EOL . Json::encode($data)
);
} catch (Exception $e) {
return false;
}
}
/**
* store a string
*
* @access public
* @static
* @param string $filename
* @param string $data
* @return bool
*/
private static function _storeString($filename, $data)
{
// Create storage directory if it does not exist.
if (!is_dir(self::$_path)) {
if (!@mkdir(self::$_path, 0700)) {
throw new Exception('unable to create directory ' . self::$_path, 10);
return false;
}
}
$file = self::$_path . DIRECTORY_SEPARATOR . '.htaccess';
@ -454,45 +505,21 @@ class Filesystem extends AbstractData
}
}
try {
$data = self::PROTECTION_LINE . PHP_EOL . Json::encode($data);
} catch (Exception $e) {
return false;
}
$file = self::$_path . DIRECTORY_SEPARATOR . $filename;
$fileCreated = true;
$writtenBytes = 0;
if (!is_file($file)) {
$fileCreated = @touch($file);
if (!is_file($filename)) {
$fileCreated = @touch($filename);
}
if ($fileCreated) {
$writtenBytes = @file_put_contents($file, $data, LOCK_EX);
$writtenBytes = @file_put_contents($filename, $data, LOCK_EX);
}
if ($fileCreated === false || $writtenBytes === false || $writtenBytes < strlen($data)) {
return false;
}
@chmod($file, 0640); // protect file access
@chmod($filename, 0640); // protect file access
return true;
}
/**
* get the data
*
* @access public
* @static
* @param string $filename
* @return array|false $data
*/
private static function _get($filename)
{
return Json::decode(
substr(
file_get_contents($filename),
strlen(self::PROTECTION_LINE . PHP_EOL)
)
);
}
/**
* rename a file, prepending the protection line at the beginning
*

View File

@ -67,6 +67,7 @@ class Model
public function purge()
{
PurgeLimiter::setConfiguration($this->_conf);
PurgeLimiter::setStore($this->_getStore());
if (PurgeLimiter::canPurge()) {
$this->_getStore()->purge($this->_conf->getKey('batchsize', 'purge'));
}

View File

@ -13,6 +13,7 @@
namespace PrivateBin\Persistence;
use Exception;
use PrivateBin\Data\AbstractData;
/**
* AbstractPersistence
@ -30,6 +31,15 @@ abstract class AbstractPersistence
*/
private static $_path = 'data';
/**
* data storage to use to persist something
*
* @access private
* @static
* @var AbstractData
*/
protected static $_store;
/**
* set the path
*
@ -42,6 +52,18 @@ abstract class AbstractPersistence
self::$_path = $path;
}
/**
* set the path
*
* @access public
* @static
* @param AbstractData $store
*/
public static function setStore(AbstractData $store)
{
self::$_store = $store;
}
/**
* get the path
*

View File

@ -71,17 +71,11 @@ class PurgeLimiter extends AbstractPersistence
}
$now = time();
$file = 'purge_limiter.php';
if (self::_exists($file)) {
require self::getPath($file);
$pl = $GLOBALS['purge_limiter'];
if ($pl + self::$_limit >= $now) {
return false;
}
$pl = (int) self::$_store->getValue('purge_limiter');
if ($pl + self::$_limit >= $now) {
return false;
}
$content = '<?php' . PHP_EOL . '$GLOBALS[\'purge_limiter\'] = ' . $now . ';';
self::_store($file, $content);
self::$_store->setValue((string) $now, 'purge_limiter');
return true;
}
}

View File

@ -1,5 +1,6 @@
<?php
use PrivateBin\Data\Filesystem;
use PrivateBin\Persistence\PurgeLimiter;
class PurgeLimiterTest extends PHPUnit_Framework_TestCase
@ -14,6 +15,9 @@ class PurgeLimiterTest extends PHPUnit_Framework_TestCase
mkdir($this->_path);
}
PurgeLimiter::setPath($this->_path);
PurgeLimiter::setStore(
Filesystem::getInstance(array('dir' => $this->_path))
);
}
public function tearDown()