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:
Diederik de Groot
2020-02-15 23:20:46 +01:00
parent caa3827bc6
commit ad87422c60
5 changed files with 120 additions and 89 deletions

15
config.ini Normal file
View File

@@ -0,0 +1,15 @@
[main]
debug = on ; The output in the browser window for more information
cache_filename = "/tmp/provision_sccp_resolver.cache"
default_language = English_United_States
;log = provision.log
[subdirs]
tftproot = tftpboot
firmware = firmware
settings = settings
wallpapers = wallpapers
ringtones = ringtones
locales = locales
countries = countries
languages = languages

59
lib/config.php Normal file
View 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;
?>

View File

@@ -1,6 +1,6 @@
#!/usr/bin/php #!/usr/bin/php
<?php <?php
$CACHEFILE_NAME="/tmp/provision_sccp_resolver.cache"; include_once("config.php");
/* Todo: /* Todo:
- setup logging - setup logging
@@ -15,9 +15,11 @@ $CACHEFILE_NAME="/tmp/provision_sccp_resolver.cache";
class Resolver { class Resolver {
private $isDirty = FALSE; private $isDirty = FALSE;
private $cache = array(); private $cache = array();
function __construct() { private $config;
if(file_exists($GLOBALS["CACHEFILE_NAME"])) { function __construct($config) {
$this->cache = unserialize(file_get_contents($GLOBALS["CACHEFILE_NAME"])); $this->config = $config;
if(file_exists($this->config['main']['cache_filename'])) {
$this->cache = unserialize(file_get_contents($config['main']['cache_filename']));
} else { } else {
$this->buildCleanCache(); $this->buildCleanCache();
} }
@@ -25,58 +27,57 @@ class Resolver {
function __destruct() { function __destruct() {
//print_r($this->cache); //print_r($this->cache);
if ($this->isDirty) { if ($this->isDirty) {
if (!file_put_contents($GLOBALS["CACHEFILE_NAME"], serialize($this->cache))) { if (!file_put_contents($this->config['main']['cache_filename'], serialize($this->cache))) {
throw new Exception("Could not write to file ".$GLOBALS["CACHEFILE_NAME"]." at Resolver::destruct"); throw new Exception("Could not write to file '".$this->config['cache_filename']."' at Resolver::destruct");
} }
} }
} }
function searchForFile($filename) { function searchForFile($filename) {
foreach(["settings","wallpapers","ringtones","locales/countries","locales/languages"] as $subdir) { foreach($this->config['subdirs'] as $key => $value) {
$path = "$subdir/$filename"; if ($key === "firmware" || $key === "tftproot" ) {
continue;
}
$path = realpath($this->config['main']['base_path'] . "/" . $value['path'] . "/$filename");
if (file_exists($path)) { if (file_exists($path)) {
$this-> addFile($filename, $path); $this-> addFile($filename, $path);
return $path; return $path;
} }
} }
throw new Exception("File '$request' does not exist"); throw new Exception("File '$filename' does not exist");
} }
function buildCleanCache() { function buildCleanCache() {
// Intelligently walk tree foreach($this->config['subdirs'] as $key =>$value) {
$currentdir=getcwd(); if ($key === "tftproot") {
//foreach(["firmware","ringtones","settings"] as $subdir) { continue;
foreach(["firmware","ringtones"] as $subdir) { }
$dir_iterator = new RecursiveDirectoryIterator("$currentdir/$subdir"); $path = $this->config['main']['base_path'] . "/" . $value['path'] . "/";
$dir_iterator = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST); $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) { foreach ($iterator as $file) {
if ($file->isFile()) { if ($file->isFile()) {
if ($value['strip'] === 1) {
$this->addFile($file->getFileName(), $file->getPathname()); $this->addFile($file->getFileName(), $file->getPathname());
} else {
$subdir = basename(dirname($file->getPathname()));
$this->addFile('$subpath/'.$file->getFileName(), $file->getPathname());
} }
} }
} }
foreach(["locales/languages", "locales/countries", "wallpapers"] as $subdir) {
$dir_iterator = new RecursiveDirectoryIterator("$currentdir/$subdir");
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
if ($file->isFile()) {
$path = basename(dirname($file->getPathname()));
$this->addFile("$path/".$file->getFileName(), $file->getPathname());
}
}
} }
$this->isDirty = TRUE; $this->isDirty = TRUE;
} }
function addFile($hash, $path) { function addFile($hash, $path) {
//echo "Rdding $hash\n"; //echo 'Adding $hash\n';
$this->cache[$hash] = $path; $this->cache[$hash] = $path;
$this->isDirty =TRUE; $this->isDirty =TRUE;
} }
function removeFile($hash) { function removeFile($hash) {
//echo "Removing $hash\n"; //echo 'Removing $hash\n';
unset($this->cache[$hash]); unset($this->cache[$hash]);
$this->isDirty = TRUE; $this->isDirty = TRUE;
} }
function resolve($request) /* canthrow */ { function resolve($request) /* canthrow */ {
$path = ""; $path = '';
if (array_key_exists($request, $this->cache)) { if (array_key_exists($request, $this->cache)) {
if ($path = $this->cache[$request]) { if ($path = $this->cache[$request]) {
if (!file_exists($path)) { if (!file_exists($path)) {
@@ -93,7 +94,7 @@ class Resolver {
} }
} }
$resolver = new Resolver(); $resolver = new Resolver($config);
try { try {
print($resolver->resolve("jar70sccp.9-4-2ES26.sbn")."\n"); print($resolver->resolve("jar70sccp.9-4-2ES26.sbn")."\n");
print($resolver->resolve("Russian_Russian_Federation/be-sccp.jar")."\n"); print($resolver->resolve("Russian_Russian_Federation/be-sccp.jar")."\n");

View File

@@ -1,13 +0,0 @@
[main]
debug = on ; The output in the browser window for more information
tftproot = /tftpboot
;default_language = English_United_States
firmware = firmware
settings = settings
wallpapers = wallpapers
ringtones = ringtones
locales = locales
countries = countries
languages = languages
;log = /tftpboot/provision.log

View File

@@ -2,38 +2,7 @@
// //
// Written by Alex / github.com/PhantomVI // Written by Alex / github.com/PhantomVI
// //
include_once("../lib/config.php");
$base_path = realpath($_SERVER['DOCUMENT_ROOT']);
$base_config = Array(
'debug' => 1,
'tftproot' => $base_path,
'default_language' => 'English_United_States',
'firmware' => 'firmware', 'settings' => 'settings', 'wallpapers' => 'wallpapers', 'ringtones' => 'ringtones',
'locales' => 'locales', 'countries' => 'countries', 'languages' => 'languages'
);
$base_tree = Array('settings' => 'tftproot', 'wallpapers' => 'tftproot', 'ringtones'=>'tftproot',
'locales' => 'tftproot', 'firmware' => 'tftproot', 'languages' => 'locales',
'countries' => 'locales', 'default_language' => 'locales');
# Merge config
$ini_array = parse_ini_file('index.cnf');
if (!empty($ini_array)) {
$config = array_merge($base_config, $ini_array);
}
foreach ($base_tree as $key => $value) {
if (!empty($config[$key])) {
if (substr($config[$key],0,1) != "/") {
$config[$key] = $config[$value].'/'.$config[$key];
}
}
}
#$config['tftproot'] = (!empty($config['tftproot'])) ? $config['tftproot'] : '/tftpboot';
# Fixup debug
$print_debug = (!empty($config['debug'])) ? $config['debug'] : 'off';
$print_debug = ($print_debug == 1) ? 'on' : $print_debug;
# Parse request # Parse request
$request = $_REQUEST; $request = $_REQUEST;
@@ -78,16 +47,16 @@ if (!empty($req_file)) {
} }
if (file_exists($config['tftproot'].'/'.$req_file_name)) // prevent "/../...//" browsing - (eliminate back door) if (file_exists($config['main']['tftproot'].'/'.$req_file_name)) // prevent "/../...//" browsing - (eliminate back door)
{ {
$req_file_full_path = $config['tftproot'].'/'.$req_file_name; $req_file_full_path = $config['main']['tftproot'].'/'.$req_file_name;
} }
else else
{ {
$tmp_file = explode('.', $req_file_name); $tmp_file = explode('.', $req_file_name);
if (strpos_array($req_file_name, $fw_suffix,'any') !== FALSE) { // Firmware file was requested if (strpos_array($req_file_name, $fw_suffix,'any') !== FALSE) { // Firmware file was requested
$firmware_list = find_all_files($config['firmware']); $firmware_list = find_all_files($config['subdirs']['firmware']);
$pos2 = strpos_array($firmware_list, $req_file_name, 'any'); // case unsensitive $pos2 = strpos_array($firmware_list, $req_file_name, 'any'); // case unsensitive
if ($pos2 !== FALSE) { // Request Firmware if ($pos2 !== FALSE) { // Request Firmware
$req_file_full_path = $firmware_list[$pos2]; $req_file_full_path = $firmware_list[$pos2];
@@ -100,55 +69,55 @@ if (!empty($req_file)) {
//if (strpos_array($req_file_name, $settings_suffix, 'any') !== FALSE) { // Request Settings //if (strpos_array($req_file_name, $settings_suffix, 'any') !== FALSE) { // Request Settings
if (strpos(strtolower($req_file_name), '.cnf.xml') !== FALSE) { // Request Settings if (strpos(strtolower($req_file_name), '.cnf.xml') !== FALSE) { // Request Settings
$tmp_file = $config['settings'].'/'.$req_file_name; $tmp_file = $config['subdirs']['settings'].'/'.$req_file_name;
} }
else if (strpos(strtolower($req_file), 'desktops/') !== FALSE) { // Request Wallpapers else if (strpos(strtolower($req_file), 'desktops/') !== FALSE) { // Request Wallpapers
$tmp_file = $config['wallpapers'].'/'. $req_data_ar[$req_data_len-1].'/'. $req_file_name; $tmp_file = $config['subdirs']['wallpapers'].'/'. $req_data_ar[$req_data_len-1].'/'. $req_file_name;
} }
else if (strpos_array($ringtones_list, $req_file_name, 'any') !== FALSE) { // Request RingTones else if (strpos_array($ringtones_list, $req_file_name, 'any') !== FALSE) { // Request RingTones
$tmp_file = $config['ringtones'].'/'.$req_file_name; $tmp_file = $config['subdirs']['ringtones'].'/'.$req_file_name;
if (!file_exists($tmp_file)) { if (!file_exists($tmp_file)) {
$tmp_file = $config['ringtones'].'/ringlist.xml'; $tmp_file = $config['subdirs']['ringtones'].'/ringlist.xml';
} }
} }
else if(strpos_array($req_file_name, $ringtones_suffix,'any') !== FALSE) { // Firmware file was requested else if(strpos_array($req_file_name, $ringtones_suffix,'any') !== FALSE) { // Firmware file was requested
$tmp_file = $config['ringtones'].'/'.$req_file_name; $tmp_file = $config['subdirs']['ringtones'].'/'.$req_file_name;
} }
else if (strpos_array($req_file, $locale_list, 'any') !== FALSE) { // Request Languages else if (strpos_array($req_file, $locale_list, 'any') !== FALSE) { // Request Languages
if (!empty($req_data_ar[$req_data_len-1])) { if (!empty($req_data_ar[$req_data_len-1])) {
$tmp_file = $config['languages'].'/'. $req_data_ar[$req_data_len-1].'/'. $req_file_name; $tmp_file = $config['subdirs']['languages'].'/'. $req_data_ar[$req_data_len-1].'/'. $req_file_name;
} else { } else {
$tmp_file = $config['default_language'].'/'. $req_file_name; $tmp_file = $config['main']['default_language'].'/'. $req_file_name;
} }
} }
/* /*
else if (strpos(strtolower($req_file), '-tones.xml') !== FALSE) { // Request Countries else if (strpos(strtolower($req_file), '-tones.xml') !== FALSE) { // Request Countries
$tmp_file = $config['countries'].'/'. $req_data_ar[$req_data_len-1].'/'. $req_data_ar[$req_data_len]; $tmp_file = $config['subdirs']['countries'].'/'. $req_data_ar[$req_data_len-1].'/'. $req_data_ar[$req_data_len];
} }
else if (strpos(strtolower($req_file), '-dictionary.') !== FALSE) { // Request Countries else if (strpos(strtolower($req_file), '-dictionary.') !== FALSE) { // Request Countries
$tmp_file = $config['languages'].'/'. $req_data_ar[$req_data_len-1].'/'. $req_data_ar[$req_data_len]; $tmp_file = $config['subdirs']['languages'].'/'. $req_data_ar[$req_data_len-1].'/'. $req_data_ar[$req_data_len];
} }
else if (strpos_array($req_file, $locale_list, 'any') !== FALSE) { // Request Languages else if (strpos_array($req_file, $locale_list, 'any') !== FALSE) { // Request Languages
$tmp_file = $config['languages'].'/'. $req_data_ar[$req_data_len-1].'/'. $req_data_ar[$req_data_len]; $tmp_file = $config['subdirs']['languages'].'/'. $req_data_ar[$req_data_len-1].'/'. $req_data_ar[$req_data_len];
} }
else if (strpos(strtolower($req_file), '-dictionary.jar') !== FALSE) { // Request Countries else if (strpos(strtolower($req_file), '-dictionary.jar') !== FALSE) { // Request Countries
$tmp_file = $config['languages'].'/'. $req_data_ar[$req_data_len-1].'/'. $req_data_ar[$req_data_len]; $tmp_file = $config['subdirs']['languages'].'/'. $req_data_ar[$req_data_len-1].'/'. $req_data_ar[$req_data_len];
} }
*/ */
if ($print_debug == 'on'){ print_r('<br>File : '. $orig_req_file_name. ' not found.<br>');} if ($print_debug == 'on'){ print_r('<br>File : '. $orig_req_file_name. ' not found.<br>');}
if (empty($tmp_file)) { if (empty($tmp_file)) {
if (!empty($config['log'])) { to_log(array('GET ('.$req_file.'):'.$orig_req_file_name, 'no match found'),'E',$config['log']); } if (!empty($config['main']['log'])) { to_log(array('GET ('.$req_file.'):'.$orig_req_file_name, 'no match found'),'E',$config['log']); }
header("HTTP/1.0 404 Not Found"); header("HTTP/1.0 404 Not Found");
die('ERROR: no match found.'); die('ERROR: no match found.');
} }
$req_file_full_path = $tmp_file; $req_file_full_path = $tmp_file;
} }
} }
if (!empty($config['log'])) { to_log(array('GET ('.$req_file.') :'.$orig_req_file_name, 'Remap :'.$req_file_full_path),'i',$config['log']); } if (!empty($config['main']['log'])) { to_log(array('GET ('.$req_file.') :'.$orig_req_file_name, 'Remap :'.$req_file_full_path),'i',$config['main']['log']); }
if (!empty($req_file_full_path)) { if (!empty($req_file_full_path)) {
if ($signed) { if ($signed) {