aboutsummaryrefslogtreecommitdiff
path: root/caches/SQLiteCache.php
diff options
context:
space:
mode:
authorGravatar Dag <me@dvikan.no> 2022-08-06 22:46:28 +0200
committerGravatar GitHub <noreply@github.com> 2022-08-06 22:46:28 +0200
commit2bbce8ebef8cf4f88392431aabe84a15482dc933 (patch)
tree1f5027ca69b1dfa2364bd9319e8536b86a41e928 /caches/SQLiteCache.php
parentb042412416cc4ecc71c3f9c13239661a0dd588a6 (diff)
downloadrss-bridge-2bbce8ebef8cf4f88392431aabe84a15482dc933.tar.gz
rss-bridge-2bbce8ebef8cf4f88392431aabe84a15482dc933.tar.zst
rss-bridge-2bbce8ebef8cf4f88392431aabe84a15482dc933.zip
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
Diffstat (limited to 'caches/SQLiteCache.php')
-rw-r--r--caches/SQLiteCache.php38
1 files changed, 11 insertions, 27 deletions
diff --git a/caches/SQLiteCache.php b/caches/SQLiteCache.php
index 2f798714..339e4119 100644
--- a/caches/SQLiteCache.php
+++ b/caches/SQLiteCache.php
@@ -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)) {