Added /lib directory
Moved /tftpboot/index.cfg -> /config.ini Moved /tftpboot/resolver.php -> /lib/resolver.php Added /lib/config.php - include /lib/config.php in resolver.php and index.php Changed $config array Signed-off-by: Diederik de Groot <ddegroot@talon.nl>
This commit is contained in:
59
lib/config.php
Normal file
59
lib/config.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
$base_path = !empty($_SERVER['DOCUMENT_ROOT']) ? realpath($_SERVER['DOCUMENT_ROOT'] . "/../"): realpath(getcwd()."/../");
|
||||
$base_config = Array(
|
||||
'main' => Array(
|
||||
'debug' => 1,
|
||||
'default_language' => 'English_United_States',
|
||||
),
|
||||
'subdirs' => Array(
|
||||
'tftproot' => 'tftpboot',
|
||||
'firmware' => 'firmware',
|
||||
'settings' => 'settings',
|
||||
'wallpapers' => 'wallpapers',
|
||||
'ringtones' => 'ringtones',
|
||||
'locales' => 'locales',
|
||||
'countries' => 'countries',
|
||||
'languages' => 'languages',
|
||||
)
|
||||
);
|
||||
$tree_base = Array(
|
||||
'settings' => array('path' => 'tftproot', "strip" => 1),
|
||||
'wallpapers' => array('path' => 'tftproot', "strip" => 0),
|
||||
'ringtones' => array('path' => 'tftproot', "strip" => 1),
|
||||
'locales' => array('path' => 'tftproot', "strip" => 1),
|
||||
'firmware' => array('path' => 'tftproot', "strip" => 1),
|
||||
'languages' => array('path' => 'locales', "strip" => 0),
|
||||
'countries' => array('path' => 'locales', "strip" => 0),
|
||||
'default_language' => array('path' => 'locales', "strip" => 1),
|
||||
);
|
||||
|
||||
# Merge config
|
||||
$ini_array = parse_ini_file('../config.ini', TRUE, INI_SCANNER_TYPED);
|
||||
if (!empty($ini_array)) {
|
||||
$config = array_merge($base_config, $ini_array);
|
||||
}
|
||||
|
||||
# rewrite config['subdirs'] paths using tree_base data
|
||||
# Not sure if this is a good way
|
||||
foreach ($tree_base as $key => $value) {
|
||||
if (!empty($config['subdirs'][$key])) {
|
||||
if (substr($config['subdirs'][$key], 0, 1) !== "/") {
|
||||
$path = $config['subdirs'][$value['path']].'/'.$config['subdirs'][$key];
|
||||
$config['subdirs'][$key] = $path;
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($tree_base as $key => $value) {
|
||||
if (!empty($config['subdirs'][$key])) {
|
||||
$config['subdirs'][$key] = array('path' => $config['subdirs'][$key], 'strip' => $value['strip']);
|
||||
}
|
||||
}
|
||||
|
||||
$config['main']['base_path'] = $base_path;
|
||||
print_r($config['main']['base_path']);
|
||||
$config['main']['tftproot'] = (!empty($config['main']['tftproot'])) ? $base_path . "tftpboot" : '/tftpboot';
|
||||
|
||||
# Fixup debug
|
||||
$print_debug = (!empty($config['main']['debug'])) ? $config['main']['debug'] : 'off';
|
||||
$print_debug = ($print_debug == 1) ? 'on' : $print_debug;
|
||||
?>
|
114
lib/resolver.php
Executable file
114
lib/resolver.php
Executable file
@@ -0,0 +1,114 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
include_once("config.php");
|
||||
|
||||
/* Todo:
|
||||
- setup logging
|
||||
- read config.file
|
||||
- improve error handling
|
||||
- secure urlencoding/urldecoding
|
||||
- don't allow browsing
|
||||
- check source ip-range
|
||||
- check HTTPHeader for known BrowserTypes
|
||||
*/
|
||||
|
||||
class Resolver {
|
||||
private $isDirty = FALSE;
|
||||
private $cache = array();
|
||||
private $config;
|
||||
function __construct($config) {
|
||||
$this->config = $config;
|
||||
if(file_exists($this->config['main']['cache_filename'])) {
|
||||
$this->cache = unserialize(file_get_contents($config['main']['cache_filename']));
|
||||
} else {
|
||||
$this->buildCleanCache();
|
||||
}
|
||||
}
|
||||
function __destruct() {
|
||||
//print_r($this->cache);
|
||||
if ($this->isDirty) {
|
||||
if (!file_put_contents($this->config['main']['cache_filename'], serialize($this->cache))) {
|
||||
throw new Exception("Could not write to file '".$this->config['cache_filename']."' at Resolver::destruct");
|
||||
}
|
||||
}
|
||||
}
|
||||
function searchForFile($filename) {
|
||||
foreach($this->config['subdirs'] as $key => $value) {
|
||||
if ($key === "firmware" || $key === "tftproot" ) {
|
||||
continue;
|
||||
}
|
||||
$path = realpath($this->config['main']['base_path'] . "/" . $value['path'] . "/$filename");
|
||||
if (file_exists($path)) {
|
||||
$this-> addFile($filename, $path);
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
throw new Exception("File '$filename' does not exist");
|
||||
}
|
||||
function buildCleanCache() {
|
||||
foreach($this->config['subdirs'] as $key =>$value) {
|
||||
if ($key === "tftproot") {
|
||||
continue;
|
||||
}
|
||||
$path = $this->config['main']['base_path'] . "/" . $value['path'] . "/";
|
||||
$dir_iterator = new RecursiveDirectoryIterator($path);
|
||||
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
|
||||
foreach ($iterator as $file) {
|
||||
if ($file->isFile()) {
|
||||
if ($value['strip'] === 1) {
|
||||
$this->addFile($file->getFileName(), $file->getPathname());
|
||||
} else {
|
||||
$subdir = basename(dirname($file->getPathname()));
|
||||
$this->addFile('$subpath/'.$file->getFileName(), $file->getPathname());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->isDirty = TRUE;
|
||||
}
|
||||
function addFile($hash, $path) {
|
||||
//echo 'Adding $hash\n';
|
||||
$this->cache[$hash] = $path;
|
||||
$this->isDirty =TRUE;
|
||||
}
|
||||
function removeFile($hash) {
|
||||
//echo 'Removing $hash\n';
|
||||
unset($this->cache[$hash]);
|
||||
$this->isDirty = TRUE;
|
||||
}
|
||||
function resolve($request) /* canthrow */ {
|
||||
$path = '';
|
||||
if (array_key_exists($request, $this->cache)) {
|
||||
if ($path = $this->cache[$request]) {
|
||||
if (!file_exists($path)) {
|
||||
$this->removeFile($request);
|
||||
throw new Exception("File '$request' does not exist on FS");
|
||||
}
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
if ($this->searchForFile($request)) {
|
||||
return $this->cache[$request];
|
||||
}
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
|
||||
$resolver = new Resolver($config);
|
||||
try {
|
||||
print($resolver->resolve("jar70sccp.9-4-2ES26.sbn")."\n");
|
||||
print($resolver->resolve("Russian_Russian_Federation/be-sccp.jar")."\n");
|
||||
print($resolver->resolve("Spain/g3-tones.xml")."\n");
|
||||
print($resolver->resolve("320x196x4/Chan-SCCP-b.png")."\n");
|
||||
} catch (Exception $e) {
|
||||
print($e . "\n");
|
||||
}
|
||||
try {
|
||||
print($resolver->resolve("XMLDefault.cnf.xml")."\n");
|
||||
} catch (Exception $e) {
|
||||
print($e . "\n");
|
||||
}
|
||||
|
||||
unset($resolver);
|
||||
#unlink($CACHEFILE_NAME);
|
||||
?>
|
Reference in New Issue
Block a user