folding Persistance\ServerSalt into Data\Filesystem

This commit is contained in:
El RIDO 2021-06-08 22:01:29 +02:00
parent b5a6ce323e
commit 7901ec74a7
No known key found for this signature in database
GPG key ID: 0F5C940A6BD81F92
12 changed files with 80 additions and 196 deletions

View file

@ -12,7 +12,6 @@
namespace PrivateBin\Persistence;
use Exception;
use PrivateBin\Data\AbstractData;
/**
@ -22,15 +21,6 @@ use PrivateBin\Data\AbstractData;
*/
abstract class AbstractPersistence
{
/**
* path in which to persist something
*
* @access private
* @static
* @var string
*/
private static $_path = 'data';
/**
* data storage to use to persist something
*
@ -40,18 +30,6 @@ abstract class AbstractPersistence
*/
protected static $_store;
/**
* set the path
*
* @access public
* @static
* @param string $path
*/
public static function setPath($path)
{
self::$_path = $path;
}
/**
* set the path
*
@ -63,95 +41,4 @@ abstract class AbstractPersistence
{
self::$_store = $store;
}
/**
* get the path
*
* @access public
* @static
* @param string $filename
* @return string
*/
public static function getPath($filename = null)
{
if (strlen($filename)) {
return self::$_path . DIRECTORY_SEPARATOR . $filename;
} else {
return self::$_path;
}
}
/**
* checks if the file exists
*
* @access protected
* @static
* @param string $filename
* @return bool
*/
protected static function _exists($filename)
{
self::_initialize();
return is_file(self::$_path . DIRECTORY_SEPARATOR . $filename);
}
/**
* prepares path for storage
*
* @access protected
* @static
* @throws Exception
*/
protected static function _initialize()
{
// 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);
}
}
$file = self::$_path . DIRECTORY_SEPARATOR . '.htaccess';
if (!is_file($file)) {
$writtenBytes = 0;
if ($fileCreated = @touch($file)) {
$writtenBytes = @file_put_contents(
$file,
'Require all denied' . PHP_EOL,
LOCK_EX
);
}
if ($fileCreated === false || $writtenBytes === false || $writtenBytes < 19) {
throw new Exception('unable to write to file ' . $file, 11);
}
}
}
/**
* store the data
*
* @access protected
* @static
* @param string $filename
* @param string $data
* @throws Exception
* @return string
*/
protected static function _store($filename, $data)
{
self::_initialize();
$file = self::$_path . DIRECTORY_SEPARATOR . $filename;
$fileCreated = true;
$writtenBytes = 0;
if (!is_file($file)) {
$fileCreated = @touch($file);
}
if ($fileCreated) {
$writtenBytes = @file_put_contents($file, $data, LOCK_EX);
}
if ($fileCreated === false || $writtenBytes === false || $writtenBytes < strlen($data)) {
throw new Exception('unable to write to file ' . $file, 13);
}
@chmod($file, 0640); // protect file access
return $file;
}
}

View file

@ -13,6 +13,7 @@
namespace PrivateBin\Persistence;
use Exception;
use PrivateBin\Data\AbstractData;
/**
* ServerSalt
@ -71,20 +72,12 @@ class ServerSalt extends AbstractPersistence
return self::$_salt;
}
if (self::_exists(self::$_file)) {
if (is_readable(self::getPath(self::$_file))) {
$items = explode('|', file_get_contents(self::getPath(self::$_file)));
}
if (!isset($items) || !is_array($items) || count($items) != 3) {
throw new Exception('unable to read file ' . self::getPath(self::$_file), 20);
}
self::$_salt = $items[1];
$salt = self::$_store->getValue('salt');
if ($salt) {
self::$_salt = $salt;
} else {
self::$_salt = self::generate();
self::_store(
self::$_file,
'<?php # |' . self::$_salt . '|'
);
self::$_store->setValue(self::$_salt, 'salt');
}
return self::$_salt;
}
@ -94,11 +87,11 @@ class ServerSalt extends AbstractPersistence
*
* @access public
* @static
* @param string $path
* @param AbstractData $store
*/
public static function setPath($path)
public static function setStore(AbstractData $store)
{
self::$_salt = '';
parent::setPath($path);
parent::setStore($store);
}
}

View file

@ -172,7 +172,7 @@ class TrafficLimiter extends AbstractPersistence
// this hash is used as an array key, hence a shorter algo is used
$hash = self::getHash('sha256');
$now = time();
$tl = self::$_store->getValue('traffic_limiter', $hash);
$tl = (int) self::$_store->getValue('traffic_limiter', $hash);
self::$_store->purgeValues('traffic_limiter', $now - self::$_limit);
if ($tl > 0 && ($tl + self::$_limit >= $now)) {
$result = false;
@ -180,7 +180,7 @@ class TrafficLimiter extends AbstractPersistence
$tl = time();
$result = true;
}
self::$_store->setValue((string) $tl, 'traffic_limiter');
self::$_store->setValue((string) $tl, 'traffic_limiter', $hash);
return $result;
}
}