mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-07-16 06:26:24 +02:00
refactor: general code base refactor (#2950)
* refactor * fix: bug in previous refactor * chore: exclude phpcompat sniff due to bug in phpcompat * fix: do not leak absolute paths * refactor/fix: batch extensions checking, fix DOS issue
This commit is contained in:
@ -1,8 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Cache with file system
|
||||
*/
|
||||
class FileCache implements CacheInterface
|
||||
{
|
||||
protected $path;
|
||||
@ -11,10 +8,7 @@ class FileCache implements CacheInterface
|
||||
public function __construct()
|
||||
{
|
||||
if (!is_writable(PATH_CACHE)) {
|
||||
returnServerError(
|
||||
'RSS-Bridge does not have write permissions for '
|
||||
. PATH_CACHE . '!'
|
||||
);
|
||||
throw new \Exception('The cache folder is not writeable');
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,20 +17,15 @@ class FileCache implements CacheInterface
|
||||
if (file_exists($this->getCacheFile())) {
|
||||
return unserialize(file_get_contents($this->getCacheFile()));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function saveData($data)
|
||||
{
|
||||
// Notice: We use plain serialize() here to reduce memory footprint on
|
||||
// large input data.
|
||||
$writeStream = file_put_contents($this->getCacheFile(), serialize($data));
|
||||
|
||||
if ($writeStream === false) {
|
||||
throw new \Exception('Cannot write the cache... Do you have the right permissions ?');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -46,7 +35,10 @@ class FileCache implements CacheInterface
|
||||
clearstatcache(false, $cacheFile);
|
||||
if (file_exists($cacheFile)) {
|
||||
$time = filemtime($cacheFile);
|
||||
return ($time !== false) ? $time : null;
|
||||
if ($time !== false) {
|
||||
return $time;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
@ -55,28 +47,25 @@ class FileCache implements CacheInterface
|
||||
public function purgeCache($seconds)
|
||||
{
|
||||
$cachePath = $this->getPath();
|
||||
if (file_exists($cachePath)) {
|
||||
$cacheIterator = new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator($cachePath),
|
||||
RecursiveIteratorIterator::CHILD_FIRST
|
||||
);
|
||||
if (!file_exists($cachePath)) {
|
||||
return;
|
||||
}
|
||||
$cacheIterator = new \RecursiveIteratorIterator(
|
||||
new \RecursiveDirectoryIterator($cachePath),
|
||||
\RecursiveIteratorIterator::CHILD_FIRST
|
||||
);
|
||||
|
||||
foreach ($cacheIterator as $cacheFile) {
|
||||
if (in_array($cacheFile->getBasename(), ['.', '..', '.gitkeep'])) {
|
||||
continue;
|
||||
} elseif ($cacheFile->isFile()) {
|
||||
if (filemtime($cacheFile->getPathname()) < time() - $seconds) {
|
||||
unlink($cacheFile->getPathname());
|
||||
}
|
||||
foreach ($cacheIterator as $cacheFile) {
|
||||
if (in_array($cacheFile->getBasename(), ['.', '..', '.gitkeep'])) {
|
||||
continue;
|
||||
} elseif ($cacheFile->isFile()) {
|
||||
if (filemtime($cacheFile->getPathname()) < time() - $seconds) {
|
||||
unlink($cacheFile->getPathname());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set scope
|
||||
* @return self
|
||||
*/
|
||||
public function setScope($scope)
|
||||
{
|
||||
if (is_null($scope) || !is_string($scope)) {
|
||||
@ -88,10 +77,6 @@ class FileCache implements CacheInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set key
|
||||
* @return self
|
||||
*/
|
||||
public function setKey($key)
|
||||
{
|
||||
if (!empty($key) && is_array($key)) {
|
||||
@ -107,10 +92,6 @@ class FileCache implements CacheInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return cache path (and create if not exist)
|
||||
* @return string Cache path
|
||||
*/
|
||||
private function getPath()
|
||||
{
|
||||
if (is_null($this->path)) {
|
||||
@ -119,26 +100,18 @@ class FileCache implements CacheInterface
|
||||
|
||||
if (!is_dir($this->path)) {
|
||||
if (mkdir($this->path, 0755, true) !== true) {
|
||||
throw new \Exception('Unable to create ' . $this->path);
|
||||
throw new \Exception('mkdir: Unable to create file cache folder');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file name use for cache store
|
||||
* @return string Path to the file cache
|
||||
*/
|
||||
private function getCacheFile()
|
||||
{
|
||||
return $this->getPath() . $this->getCacheName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines file name for store the cache
|
||||
* return string
|
||||
*/
|
||||
private function getCacheName()
|
||||
{
|
||||
if (is_null($this->key)) {
|
||||
|
@ -12,29 +12,33 @@ class MemcachedCache implements CacheInterface
|
||||
public function __construct()
|
||||
{
|
||||
if (!extension_loaded('memcached')) {
|
||||
returnServerError('"memcached" extension not loaded. Please check "php.ini"');
|
||||
throw new \Exception('"memcached" extension not loaded. Please check "php.ini"');
|
||||
}
|
||||
|
||||
$section = 'MemcachedCache';
|
||||
$host = Configuration::getConfig($section, 'host');
|
||||
$port = Configuration::getConfig($section, 'port');
|
||||
|
||||
if (empty($host) && empty($port)) {
|
||||
returnServerError('Configuration for ' . $section . ' missing. Please check your ' . FILE_CONFIG);
|
||||
} elseif (empty($host)) {
|
||||
returnServerError('"host" param is not set for ' . $section . '. Please check your ' . FILE_CONFIG);
|
||||
} elseif (empty($port)) {
|
||||
returnServerError('"port" param is not set for ' . $section . '. Please check your ' . FILE_CONFIG);
|
||||
} elseif (!ctype_digit($port)) {
|
||||
returnServerError('"port" param is invalid for ' . $section . '. Please check your ' . FILE_CONFIG);
|
||||
throw new \Exception('Configuration for ' . $section . ' missing. Please check your ' . FILE_CONFIG);
|
||||
}
|
||||
if (empty($host)) {
|
||||
throw new \Exception('"host" param is not set for ' . $section . '. Please check your ' . FILE_CONFIG);
|
||||
}
|
||||
if (empty($port)) {
|
||||
throw new \Exception('"port" param is not set for ' . $section . '. Please check your ' . FILE_CONFIG);
|
||||
}
|
||||
if (!ctype_digit($port)) {
|
||||
throw new \Exception('"port" param is invalid for ' . $section . '. Please check your ' . FILE_CONFIG);
|
||||
}
|
||||
|
||||
$port = intval($port);
|
||||
|
||||
if ($port < 1 || $port > 65535) {
|
||||
returnServerError('"port" param is invalid for ' . $section . '. Please check your ' . FILE_CONFIG);
|
||||
throw new \Exception('"port" param is invalid for ' . $section . '. Please check your ' . FILE_CONFIG);
|
||||
}
|
||||
|
||||
$conn = new Memcached();
|
||||
$conn = new \Memcached();
|
||||
$conn->addServer($host, $port) or returnServerError('Could not connect to memcached server');
|
||||
$this->conn = $conn;
|
||||
}
|
||||
@ -64,7 +68,7 @@ class MemcachedCache implements CacheInterface
|
||||
$result = $this->conn->set($this->getCacheKey(), $object_to_save, $this->expiration);
|
||||
|
||||
if ($result === false) {
|
||||
returnServerError('Cannot write the cache to memcached server');
|
||||
throw new \Exception('Cannot write the cache to memcached server');
|
||||
}
|
||||
|
||||
$this->time = $time;
|
||||
@ -87,20 +91,12 @@ class MemcachedCache implements CacheInterface
|
||||
$this->expiration = $duration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set scope
|
||||
* @return self
|
||||
*/
|
||||
public function setScope($scope)
|
||||
{
|
||||
$this->scope = $scope;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set key
|
||||
* @return self
|
||||
*/
|
||||
public function setKey($key)
|
||||
{
|
||||
if (!empty($key) && is_array($key)) {
|
||||
@ -119,7 +115,7 @@ class MemcachedCache implements CacheInterface
|
||||
private function getCacheKey()
|
||||
{
|
||||
if (is_null($this->key)) {
|
||||
returnServerError('Call "setKey" first!');
|
||||
throw new \Exception('Call "setKey" first!');
|
||||
}
|
||||
|
||||
return 'rss_bridge_cache_' . hash('md5', $this->scope . $this->key . 'A');
|
||||
|
@ -13,38 +13,32 @@ class SQLiteCache implements CacheInterface
|
||||
public function __construct()
|
||||
{
|
||||
if (!extension_loaded('sqlite3')) {
|
||||
print render('error.html.php', ['message' => '"sqlite3" extension not loaded. Please check "php.ini"']);
|
||||
exit;
|
||||
throw new \Exception('"sqlite3" extension not loaded. Please check "php.ini"');
|
||||
}
|
||||
|
||||
if (!is_writable(PATH_CACHE)) {
|
||||
returnServerError(
|
||||
'RSS-Bridge does not have write permissions for '
|
||||
. PATH_CACHE . '!'
|
||||
);
|
||||
throw new \Exception('The cache folder is not writable');
|
||||
}
|
||||
|
||||
$section = 'SQLiteCache';
|
||||
$file = Configuration::getConfig($section, 'file');
|
||||
if (empty($file)) {
|
||||
$message = sprintf('Configuration for %s missing. Please check your %s', $section, FILE_CONFIG);
|
||||
print render('error.html.php', ['message' => $message]);
|
||||
exit;
|
||||
throw new \Exception(sprintf('Configuration for %s missing.', $section));
|
||||
}
|
||||
|
||||
if (dirname($file) == '.') {
|
||||
$file = PATH_CACHE . $file;
|
||||
} elseif (!is_dir(dirname($file))) {
|
||||
$message = sprintf('Invalid configuration for %s. Please check your %s', $section, FILE_CONFIG);
|
||||
print render('error.html.php', ['message' => $message]);
|
||||
exit;
|
||||
throw new \Exception(sprintf('Invalid configuration for %s', $section));
|
||||
}
|
||||
|
||||
if (!is_file($file)) {
|
||||
$this->db = new SQLite3($file);
|
||||
// The instantiation creates the file
|
||||
$this->db = new \SQLite3($file);
|
||||
$this->db->enableExceptions(true);
|
||||
$this->db->exec("CREATE TABLE storage ('key' BLOB PRIMARY KEY, 'value' BLOB, 'updated' INTEGER)");
|
||||
} else {
|
||||
$this->db = new SQLite3($file);
|
||||
$this->db = new \SQLite3($file);
|
||||
$this->db->enableExceptions(true);
|
||||
}
|
||||
$this->db->busyTimeout(5000);
|
||||
@ -55,8 +49,8 @@ class SQLiteCache implements CacheInterface
|
||||
$Qselect = $this->db->prepare('SELECT value FROM storage WHERE key = :key');
|
||||
$Qselect->bindValue(':key', $this->getCacheKey());
|
||||
$result = $Qselect->execute();
|
||||
if ($result instanceof SQLite3Result) {
|
||||
$data = $result->fetchArray(SQLITE3_ASSOC);
|
||||
if ($result instanceof \SQLite3Result) {
|
||||
$data = $result->fetchArray(\SQLITE3_ASSOC);
|
||||
if (isset($data['value'])) {
|
||||
return unserialize($data['value']);
|
||||
}
|
||||
@ -81,7 +75,7 @@ class SQLiteCache implements CacheInterface
|
||||
$Qselect = $this->db->prepare('SELECT updated FROM storage WHERE key = :key');
|
||||
$Qselect->bindValue(':key', $this->getCacheKey());
|
||||
$result = $Qselect->execute();
|
||||
if ($result instanceof SQLite3Result) {
|
||||
if ($result instanceof \SQLite3Result) {
|
||||
$data = $result->fetchArray(SQLITE3_ASSOC);
|
||||
if (isset($data['updated'])) {
|
||||
return $data['updated'];
|
||||
@ -98,10 +92,6 @@ class SQLiteCache implements CacheInterface
|
||||
$Qdelete->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set scope
|
||||
* @return self
|
||||
*/
|
||||
public function setScope($scope)
|
||||
{
|
||||
if (is_null($scope) || !is_string($scope)) {
|
||||
@ -112,10 +102,6 @@ class SQLiteCache implements CacheInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set key
|
||||
* @return self
|
||||
*/
|
||||
public function setKey($key)
|
||||
{
|
||||
if (!empty($key) && is_array($key)) {
|
||||
@ -131,8 +117,6 @@ class SQLiteCache implements CacheInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
private function getCacheKey()
|
||||
{
|
||||
if (is_null($this->key)) {
|
||||
|
Reference in New Issue
Block a user