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) { switch ($namespace) {
case 'purge_limiter': case 'purge_limiter':
; return self::_storeString(
self::$_path . DIRECTORY_SEPARATOR . 'purge_limiter.php',
'<?php' . PHP_EOL . '$GLOBALS[\'purge_limiter\'] = ' . $value . ';'
);
break; break;
case 'salt': case 'salt':
; ;
@ -261,10 +264,8 @@ class Filesystem extends AbstractData
case 'traffic_limiter': case 'traffic_limiter':
; ;
break; break;
default:
return false;
break;
} }
return false;
} }
/** /**
@ -277,7 +278,40 @@ class Filesystem extends AbstractData
*/ */
public function getValue($namespace, $key = '') 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 * @param array $data
* @return bool * @return bool
*/ */
private static function _store($filename, $data) private static function _store($filename, array $data)
{ {
if (strpos($filename, self::$_path) === 0) { try {
$filename = substr($filename, strlen(self::$_path)); 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. // Create storage directory if it does not exist.
if (!is_dir(self::$_path)) { if (!is_dir(self::$_path)) {
if (!@mkdir(self::$_path, 0700)) { if (!@mkdir(self::$_path, 0700)) {
throw new Exception('unable to create directory ' . self::$_path, 10); return false;
} }
} }
$file = self::$_path . DIRECTORY_SEPARATOR . '.htaccess'; $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; $fileCreated = true;
$writtenBytes = 0; $writtenBytes = 0;
if (!is_file($file)) { if (!is_file($filename)) {
$fileCreated = @touch($file); $fileCreated = @touch($filename);
} }
if ($fileCreated) { 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)) { if ($fileCreated === false || $writtenBytes === false || $writtenBytes < strlen($data)) {
return false; return false;
} }
@chmod($file, 0640); // protect file access @chmod($filename, 0640); // protect file access
return true; 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 * rename a file, prepending the protection line at the beginning
* *

View file

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

View file

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

View file

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

View file

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