Refactored the filename cache into standalone abstract class which can be implemented using different backends
Signed-off-by: Diederik de Groot <ddegroot@talon.nl>
This commit is contained in:
79
lib/resolveCache.php
Normal file
79
lib/resolveCache.php
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
abstract class resolveCache {
|
||||||
|
abstract protected function addFile($filename, $realpath);
|
||||||
|
abstract protected function removeFile($filename);
|
||||||
|
abstract protected function check($filename);
|
||||||
|
abstract protected function getPath($filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
class fileCache extends resolveCache {
|
||||||
|
private $_isDirty = false;
|
||||||
|
private $_cache = array();
|
||||||
|
private $_cache_file = NULL;
|
||||||
|
|
||||||
|
function __construct($filename) {
|
||||||
|
$this->_cache_file = $filename;
|
||||||
|
if (file_exists($this->_cache_file)) {
|
||||||
|
$this->_cache = unserialize(file_get_contents($this->_cache_file));
|
||||||
|
$this->_isDirty = false;
|
||||||
|
} else {
|
||||||
|
$this->_isDirty = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function __destruct() {
|
||||||
|
if ($this->_isDirty) {
|
||||||
|
if (!is_writable($this->_cache_file)) {
|
||||||
|
log_error_and_throw("Could not write to file '".$this->_cache_file."' at Resolver::destruct");
|
||||||
|
}
|
||||||
|
if (!file_put_contents($this->_cache_file, serialize($this->cache))) {
|
||||||
|
log_error_and_throw("Could not write to file '".$this->_cache_file."' at Resolver::destruct");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isDirty() {
|
||||||
|
return $this->_isDirty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addFile($filename, $realpath) {
|
||||||
|
$this->_cache[$filename] = $realpath;
|
||||||
|
$this->_isDirty =true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeFile($filename) {
|
||||||
|
if (array_key_exists($filename, $this->_cache)) {
|
||||||
|
unset($this->_cache[$filename]);
|
||||||
|
$this->_isDirty = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function check($filename) {
|
||||||
|
return array_key_exists($filename, $this->_cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPath($filename) {
|
||||||
|
if (array_key_exists($filename, $this->_cache)) {
|
||||||
|
return $this->_cache[$filename];
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function printCache() {
|
||||||
|
print_r($this->_cache);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
class sqliteCache extends resolveCache {
|
||||||
|
function __construct() {
|
||||||
|
}
|
||||||
|
function __destruct() {
|
||||||
|
}
|
||||||
|
public function addFile($filename, $realpath);
|
||||||
|
public function removeFile($filename);
|
||||||
|
public function check($filename);
|
||||||
|
public function getPath($filename);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
?>
|
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
include_once("config.php");
|
include_once("config.php");
|
||||||
include_once("utils.php");
|
include_once("utils.php");
|
||||||
|
include_once("resolveCache.php");
|
||||||
|
|
||||||
/* Todo:
|
/* Todo:
|
||||||
✔️ setup logging
|
✔️ setup logging
|
||||||
@@ -28,45 +29,16 @@ class ResolveResult {
|
|||||||
|
|
||||||
class Resolver {
|
class Resolver {
|
||||||
private $isDirty = FALSE;
|
private $isDirty = FALSE;
|
||||||
private $cache = array();
|
private $cache;
|
||||||
private $config;
|
private $config;
|
||||||
//private $logger;
|
|
||||||
function __construct($config) {
|
function __construct($config) {
|
||||||
//global $logger;
|
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
//$this->logger = $logger;
|
$this->cache = new fileCache($this->config['main']['cache_filename']);
|
||||||
if(file_exists($this->config['main']['cache_filename'])) {
|
if ($this->cache->isDirty()) {
|
||||||
$this->cache = unserialize(file_get_contents($config['main']['cache_filename']));
|
$this->rebuildCache();
|
||||||
} else {
|
|
||||||
$this->buildCleanCache();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function __destruct() {
|
|
||||||
// $this->printCache()
|
|
||||||
if ($this->isDirty) {
|
|
||||||
if (!is_writable($this->config['main']['cache_filename'])) {
|
|
||||||
$this->log_error_and_throw("Could not write to file '".$this->config['cache_filename']."' at Resolver::destruct");
|
|
||||||
}
|
|
||||||
if (!file_put_contents($this->config['main']['cache_filename'], serialize($this->cache))) {
|
|
||||||
$this->log_error_and_throw("Could not write to file '".$this->config['cache_filename']."' at Resolver::destruct");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function log_debug($message) {
|
|
||||||
global $logger;
|
|
||||||
$logger->log('LOG_DEBUG', $message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function log_error($message) {
|
|
||||||
global $logger;
|
|
||||||
$logger->log('LOG_ERROR', $message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function log_error_and_throw($message) {
|
|
||||||
$this->log_error($message);
|
|
||||||
throw new Exception($message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function searchForFile($filename) {
|
public function searchForFile($filename) {
|
||||||
foreach($this->config['subdirs'] as $key => $value) {
|
foreach($this->config['subdirs'] as $key => $value) {
|
||||||
@@ -79,11 +51,11 @@ class Resolver {
|
|||||||
return $path;
|
return $path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->log_error("File '$filename' does not exist");
|
log_error("File '$filename' does not exist");
|
||||||
return ResolveResult::FileNotFound;
|
return ResolveResult::FileNotFound;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildCleanCache() {
|
public function rebuildCache() {
|
||||||
foreach($this->config['subdirs'] as $key =>$value) {
|
foreach($this->config['subdirs'] as $key =>$value) {
|
||||||
if ($key === "tftproot") {
|
if ($key === "tftproot") {
|
||||||
continue;
|
continue;
|
||||||
@@ -106,15 +78,13 @@ class Resolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function addFile($requestpath, $truepath) {
|
public function addFile($requestpath, $truepath) {
|
||||||
$this->log_debug("Adding $requestpath");
|
log_debug("Adding $requestpath");
|
||||||
$this->cache[$requestpath] = $truepath;
|
$this->cache->addFile($requestpath, $truepath);
|
||||||
$this->isDirty =TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeFile($requestpath) {
|
public function removeFile($requestpath) {
|
||||||
$this->log_debug("Removing $hash");
|
log_debug("Removing $hash");
|
||||||
unset($this->cache[$requestpath]);
|
$this->cache->removeFile($requestpath, $truepath);
|
||||||
$this->isDirty = TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function validateRequest($request) {
|
public function validateRequest($request) {
|
||||||
@@ -122,21 +92,21 @@ class Resolver {
|
|||||||
/* make sure request only starts with filename or one of $config[$subdir]['locale'] or $config[$subdir]['wallpaper'] */
|
/* make sure request only starts with filename or one of $config[$subdir]['locale'] or $config[$subdir]['wallpaper'] */
|
||||||
/* check uri/url decode */
|
/* check uri/url decode */
|
||||||
if (!$request || empty($request)) {
|
if (!$request || empty($request)) {
|
||||||
$this->log_error("Request is empty");
|
log_error("Request is empty");
|
||||||
return ResolveResult::EmptyRequest;
|
return ResolveResult::EmptyRequest;
|
||||||
}
|
}
|
||||||
if (!is_string($request)) {
|
if (!is_string($request)) {
|
||||||
$this->log_error("Request is not a string");
|
log_error("Request is not a string");
|
||||||
return ResolveResult::RequestNotAString;
|
return ResolveResult::RequestNotAString;
|
||||||
}
|
}
|
||||||
$this->log_debug($request . ":" . escapeshellarg($request) . ":" . utf8_urldecode($request) . "\n");
|
log_debug($request . ":" . escapeshellarg($request) . ":" . utf8_urldecode($request) . "\n");
|
||||||
$escaped_request = escapeshellarg(utf8_urldecode($request));
|
$escaped_request = escapeshellarg(utf8_urldecode($request));
|
||||||
if ($escaped_request !== "'" . $request . "'") {
|
if ($escaped_request !== "'" . $request . "'") {
|
||||||
$this->log_error("Request '$request' contains invalid characters");
|
log_error("Request '$request' contains invalid characters");
|
||||||
return ResolveResult::RequestContainsInvalidChar;
|
return ResolveResult::RequestContainsInvalidChar;
|
||||||
}
|
}
|
||||||
if (strstr($escaped_request, "..")) {
|
if (strstr($escaped_request, "..")) {
|
||||||
$this->log_error("Request '$request' contains '..'");
|
log_error("Request '$request' contains '..'");
|
||||||
return ResolveResult::RequestContainsPathWalk;
|
return ResolveResult::RequestContainsPathWalk;
|
||||||
}
|
}
|
||||||
return ResolveResult::Ok;
|
return ResolveResult::Ok;
|
||||||
@@ -148,18 +118,16 @@ class Resolver {
|
|||||||
if ($result !== ResolveResult::Ok) {
|
if ($result !== ResolveResult::Ok) {
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
if (array_key_exists($request, $this->cache)) {
|
if (($path = $this->cache->getPath($request))) {
|
||||||
if ($path = $this->cache[$request]) {
|
|
||||||
if (!file_exists($path)) {
|
if (!file_exists($path)) {
|
||||||
$this->removeFile($request);
|
$this->removeFile($request);
|
||||||
$this->log_error("File '$request' does not exist on FS");
|
log_error("File '$request' does not exist on FS");
|
||||||
return ResolveResult::FileNotFound;
|
return ResolveResult::FileNotFound;
|
||||||
}
|
}
|
||||||
return $path;
|
return $path;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if ($this->searchForFile($request)) {
|
if ($this->searchForFile($request)) {
|
||||||
$path = $this->cache[$request];
|
$path = $this->cache->getPath($request);
|
||||||
}
|
}
|
||||||
return $path;
|
return $path;
|
||||||
}
|
}
|
||||||
|
@@ -50,4 +50,19 @@ function parse_ini_file_multi($file, $process_sections = false, $scanner_mode =
|
|||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function log_debug($message) {
|
||||||
|
global $logger;
|
||||||
|
$logger->log('LOG_DEBUG', $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
function log_error($message) {
|
||||||
|
global $logger;
|
||||||
|
$logger->log('LOG_ERROR', $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
function log_error_and_throw($message) {
|
||||||
|
log_error($message);
|
||||||
|
throw new Exception($message);
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
|
Reference in New Issue
Block a user