renamed tftproot => data

Added strict_types=1
Added NameSpaces
Fixed config::replaceSubdirTreeStructure function

Signed-off-by: Diederik de Groot <ddegroot@talon.nl>
This commit is contained in:
Diederik de Groot
2020-03-16 11:33:02 +01:00
parent 67bf5bdca5
commit c06cac4653
7 changed files with 71 additions and 44 deletions

View File

@@ -22,7 +22,7 @@ cert_pub = NULL
hash = NULL
;[subdirs]
;tftproot = tftpboot
;tftproot = data
;firmware = firmware
;settings = settings
;wallpapers = wallpapers

View File

@@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
namespace SCCP\Config;
include_once("logger.php");
include_once("utils.php");
$base_path = realpath(__DIR__ . DIRECTORY_SEPARATOR . "..");
@@ -24,8 +27,8 @@ class ConfigParser {
hash = NULL
[subdirs]
etc = ../etc
tftproot = tftpboot
etc = etc
data = data
firmware = firmware
settings = settings
wallpapers = wallpapers
@@ -72,7 +75,7 @@ class ConfigParser {
$config = array_merge($default_config, $ini_config);
}
$config['main']['base_path'] = $base_path;
$config['main']['tftproot'] = (!empty($config['main']['tftproot'])) ? $base_path . "tftpboot" : '/tftpboot';
$config['main']['data'] = (!empty($config['main']['data'])) ? $base_path . "data" : DIRECTORY_SEPARATOR . 'data';
$config['subdirs'] = $this->replaceSubdirTreeStructure($config['subdirs']);
$this->config = $config;
}
@@ -85,12 +88,12 @@ class ConfigParser {
private function replaceSubdirTreeStructure($tmpSubdirs) {
$tree_structure = Array(
'etc' => array('parent' => NULL, "strip" => true),
'tftproot' => array('parent' => NULL, "strip" => true),
'settings' => array('parent' => 'tftproot', "strip" => true),
'wallpapers' => array('parent' => 'tftproot', "strip" => false),
'ringtones' => array('parent' => 'tftproot', "strip" => true),
'locales' => array('parent' => 'tftproot', "strip" => true),
'firmware' => array('parent' => 'tftproot', "strip" => true),
'data' => array('parent' => NULL, "strip" => true),
'settings' => array('parent' => 'data', "strip" => true),
'wallpapers' => array('parent' => 'data', "strip" => false),
'ringtones' => array('parent' => 'data', "strip" => true),
'locales' => array('parent' => 'data', "strip" => true),
'firmware' => array('parent' => 'data', "strip" => true),
'languages' => array('parent' => 'locales', "strip" => false),
'countries' => array('parent' => 'locales', "strip" => false),
);
@@ -98,16 +101,10 @@ class ConfigParser {
$subdirs = Array();
foreach ($tree_structure as $key => $value) {
if (!empty($tmpSubdirs[$key])) {
if (substr($tmpSubdirs[$key], 0, 1) !== "/") {
if (!$value['parent']) {
$path = $tmpSubdirs[$key];
} else {
if (is_array($tmpSubdirs[$value['parent']])) {
$path = $tmpSubdirs[$value['parent']]['path'].'/'.$tmpSubdirs[$key];
} else {
$path = $tmpSubdirs[$value['parent']].'/'.$tmpSubdirs[$key];
}
}
if (!$value['parent']) {
$path = $tmpSubdirs[$key];
} else {
$path = $subdirs[$value['parent']]['path'] . DIRECTORY_SEPARATOR . $tmpSubdirs[$key];
}
$subdirs[$key] = array('path' => $path, 'strip' => $value['strip']);
}
@@ -190,7 +187,7 @@ $config = $configParser->getConfiguration();
switch($config['main']['log_type']) {
case 'SYSLOG':
$logger = new Logger_Syslog($config['main']['log_level']);
$logger = new \SCCP\Logger\Logger_Syslog($config['main']['log_level']);
break;
case 'FILE':
if (!isempty($config['main']['log_file'])) {

View File

@@ -1,4 +1,7 @@
<?php
namespace SCCP\Logger;
/* Note about the Logger class:
* The "priority" and "minimum should be one of the constants used for syslog.
* See: http://php.net/manual/en/function.syslog.php

View File

@@ -1,4 +1,7 @@
<?php
namespace SCCP\ResolveCache;
use SCCP\Utils as Utils;
abstract class resolveCache {
abstract protected function addFile($filename, $realpath);
abstract protected function removeFile($filename);
@@ -27,7 +30,7 @@ class fileCache extends resolveCache {
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");
Utils\log_error_and_throw("Could not write to file '".$this->_cache_file."' at Resolver::destruct");
}
}
}
@@ -42,7 +45,7 @@ class fileCache extends resolveCache {
public function addFile($filename, $realpath) {
if ($this->check($filename))
log_error("Duplicate file:$filename"); /* should we prevent this ? */
Utils\log_error("Duplicate file:$filename"); /* should we prevent this ? */
$this->_cache[$filename] = $realpath;
$this->_isDirty =true;
}

View File

@@ -1,8 +1,14 @@
<?php
declare(strict_types=1);
namespace SCCP\Resolve;
include_once("config.php");
include_once("utils.php");
include_once("resolveCache.php");
use SCCP\Utils as Utils;
use SCCP\ResolveCache as ResolveCache;
/* Todo:
✔️ setup logging
✔️ read config.file
@@ -16,6 +22,7 @@ include_once("resolveCache.php");
- Could use some more test-cases, especially error ones
*/
//class ResolveResult extends Enum {
class ResolveResult {
const Ok = 0;
const EmptyRequest = 1;
@@ -33,43 +40,57 @@ class Resolver {
private $config;
function __construct($config) {
$this->config = $config;
$this->cache = new fileCache($this->config['main']['cache_filename']);
$this->cache = new ResolveCache\fileCache($this->config['main']['cache_filename']);
if ($this->cache->isDirty()) {
$this->rebuildCache();
}
}
public function searchForFile($filename) {
$path = "";
foreach($this->config['subdirs'] as $key => $value) {
if ($key === "firmware" || $key === "tftproot" ) {
if ($key === "firmware" || $key === "data" || $key === "etc") {
continue;
}
$path = realpath($this->config['main']['base_path'] . "/" . $value['path'] . "/$filename");
if (file_exists($path)) {
$this->cache->addFile($filename, $path);
return $path;
$path = realpath($this->config['main']['base_path'] . DIRECTORY_SEPARATOR . $value['path'] . DIRECTORY_SEPARATOR . $filename);
if (!$path) {
print("path: '" . $this->config['main']['base_path'] . DIRECTORY_SEPARATOR . $value['path'] . DIRECTORY_SEPARATOR . $filename . "' not found\n");
return ResolveResult::FileNotFound;
}
$this->cache->addFile($filename, $path);
return $path;
}
log_error("File '$filename' does not exist");
Utils\log_error("File '$filename' does not exist");
return ResolveResult::FileNotFound;
}
public function rebuildCache() {
log_debug("Rebuilding Cache, standby...");
Utils\log_debug("Rebuilding Cache, standby...");
foreach($this->config['subdirs'] as $key =>$value) {
if ($key === "tftproot") {
if ($key === "data" || $key === "etc") {
continue;
}
$path = $this->config['main']['base_path'] . "/" . $value['path'] . "/";
$dir_iterator = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
$path = realpath($this->config['main']['base_path'] . DIRECTORY_SEPARATOR . $value['path'] . DIRECTORY_SEPARATOR);
if (!$path) {
print("path: '" . $this->config['main']['base_path'] . DIRECTORY_SEPARATOR . $value['path'] . "' not found\n");
break;
}
$dir_iterator = new \RecursiveDirectoryIterator($path);
$filter = new \RecursiveCallbackFilterIterator($dir_iterator, function ($current, $key, $iterator) {
// Skip hidden files and directories.
if ($current->getFilename()[0] === '.' || $current->getFilename() == "bak") {
return FALSE;
}
return TRUE;
});
$iterator = new \RecursiveIteratorIterator($filter, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
if ($file->isFile()) {
if ($value['strip']) {
$this->cache->addFile($file->getFileName(), $file->getPathname());
} else {
$subdir = basename(dirname($file->getPathname()));
$this->cache->addFile('$subpath/'.$file->getFileName(), $file->getPathname());
$this->cache->addFile($subdir. DIRECTORY_SEPARATOR . $file->getFileName(), $file->getPathname());
}
}
}
@@ -89,14 +110,14 @@ class Resolver {
log_error("Request is not a string");
return ResolveResult::RequestNotAString;
}
log_debug($request . ":" . escapeshellarg($request) . ":" . utf8_urldecode($request) . "\n");
$escaped_request = escapeshellarg(utf8_urldecode($request));
Utils\log_debug($request . ":" . escapeshellarg($request) . ":" . Utils\utf8_urldecode($request) . "\n");
$escaped_request = escapeshellarg(Utils\utf8_urldecode($request));
if ($escaped_request !== "'" . $request . "'") {
log_error("Request '$request' contains invalid characters");
return ResolveResult::RequestContainsInvalidChar;
}
if (strstr($escaped_request, "..")) {
log_error("Request '$request' contains '..'");
Utils\log_error("Request '$request' contains '..'");
return ResolveResult::RequestContainsPathWalk;
}
return ResolveResult::Ok;
@@ -132,11 +153,11 @@ class Resolver {
if(defined('STDIN') ) {
$resolver = new Resolver($config);
$test_cases = Array(
Array('request' => 'jar70sccp.9-4-2ES26.sbn', 'expected' => '/tftpboot/firmware/7970/jar70sccp.9-4-2ES26.sbn'),
Array('request' => 'Russian_Russian_Federation/be-sccp.jar', 'expected' => '/tftpboot/locales/languages/Russian_Russian_Federation/be-sccp.jar'),
Array('request' => 'Spain/g3-tones.xml', 'expected' => '/tftpboot/locales/countries/Spain/g3-tones.xml'),
Array('request' => '320x196x4/Chan-SCCP-b.png', 'expected' => '/tftpboot/wallpapers/320x196x4/Chan-SCCP-b.png'),
Array('request' => 'XMLDefault.cnf.xml', 'expected' => '/tftpboot/settings/XMLDefault.cnf.xml'),
Array('request' => 'jar70sccp.9-4-2ES26.sbn', 'expected' => '/data/firmware/7970/jar70sccp.9-4-2ES26.sbn'),
Array('request' => 'Russian_Russian_Federation/be-sccp.jar', 'expected' => '/data/locales/languages/Russian_Russian_Federation/be-sccp.jar'),
Array('request' => 'Spain/g3-tones.xml', 'expected' => '/data/locales/countries/Spain/g3-tones.xml'),
Array('request' => '320x196x4/Chan-SCCP-b.png', 'expected' => '/data/wallpapers/320x196x4/Chan-SCCP-b.png'),
Array('request' => 'XMLDefault.cnf.xml', 'expected' => '/data/settings/XMLDefault.cnf.xml'),
Array('request' => '../XMLDefault.cnf.xml', 'expected' => ResolveResult::RequestContainsPathWalk),
Array('request' => 'XMLDefault.cnf.xml/../../text.xml', 'expected' => ResolveResult::RequestContainsPathWalk),
);

View File

@@ -2,6 +2,7 @@
//
// Helper functions
//
namespace SCCP\Utils;
function utf8_urldecode($str) {
$str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str));

View File

@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
namespace SCCP\XML;
include_once("config.php");
class XML {