2015-03-08 04:57:30 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Configuration Management
|
|
|
|
*/
|
|
|
|
|
2015-03-25 09:59:10 +00:00
|
|
|
namespace Sakura;
|
2015-03-08 04:57:30 +00:00
|
|
|
|
|
|
|
class Configuration {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-04-01 15:06:42 +00:00
|
|
|
// Configuration data
|
2015-03-08 04:57:30 +00:00
|
|
|
public static $_LCNF;
|
|
|
|
public static $_DCNF;
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-04-01 15:06:42 +00:00
|
|
|
// Initialise configuration, does not contain database initialisation because explained below
|
2015-03-08 04:57:30 +00:00
|
|
|
public static function init($local) {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-04-01 15:06:42 +00:00
|
|
|
// Check if $local is an array and then store it in $_LCNF
|
2015-03-08 04:57:30 +00:00
|
|
|
if(is_array($local))
|
|
|
|
self::$_LCNF = $local;
|
2015-04-01 15:06:42 +00:00
|
|
|
else // Otherwise trigger an error
|
|
|
|
trigger_error('Failed to load local configuration!', E_USER_ERROR);
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
}
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-04-01 15:06:42 +00:00
|
|
|
/*
|
|
|
|
* Initialise Database configuration values.
|
|
|
|
* Different from init as that is called before the database connection is initially
|
|
|
|
* established.
|
|
|
|
*/
|
2015-03-08 04:57:30 +00:00
|
|
|
public static function initDB() {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-04-01 15:14:09 +00:00
|
|
|
// Get config table from the database
|
2015-03-08 04:57:30 +00:00
|
|
|
$_DATA = Database::fetch('config', true);
|
2015-04-01 15:14:09 +00:00
|
|
|
|
|
|
|
// Create variable to temporarily store values in
|
2015-03-08 04:57:30 +00:00
|
|
|
$_DBCN = array();
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-04-01 15:14:09 +00:00
|
|
|
foreach($_DATA as $_CONF) // Properly sort the values
|
2015-03-08 04:57:30 +00:00
|
|
|
$_DBCN[$_CONF[0]] = $_CONF[1];
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-04-01 15:14:09 +00:00
|
|
|
// Assign the temporary array to the static one
|
2015-03-08 04:57:30 +00:00
|
|
|
self::$_DCNF = $_DBCN;
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
}
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
// Get values from the configuration on the file system
|
|
|
|
public static function getLocalConfig($key, $subkey = null) {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-04-01 15:06:42 +00:00
|
|
|
// Check if the key that we're looking for exists
|
|
|
|
if(array_key_exists($key, self::$_LCNF)) { // If we also have a subkey we check
|
|
|
|
if($subkey && array_key_exists($subkey, $key)) // if that exists, else we just return
|
|
|
|
return self::$_LCNF[$key][$subkey]; // the default value.
|
2015-03-08 04:57:30 +00:00
|
|
|
else
|
|
|
|
return self::$_LCNF[$key];
|
2015-04-01 15:06:42 +00:00
|
|
|
} else // If it doesn't exist trigger an error to avoid explosions
|
2015-03-29 16:25:18 +00:00
|
|
|
trigger_error('Unable to get local configuration value!', E_USER_ERROR);
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dynamically set local configuration values, does not update the configuration file
|
|
|
|
public static function setLocalConfig($key, $subkey, $value) {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-04-01 15:06:42 +00:00
|
|
|
// Check if we also do a subkey
|
2015-03-08 04:57:30 +00:00
|
|
|
if($subkey) {
|
2015-04-01 15:06:42 +00:00
|
|
|
|
|
|
|
// If we do we make sure that the parent key is an array
|
2015-03-21 14:12:51 +00:00
|
|
|
if(!isset(self::$_LCNF[$key]))
|
2015-03-08 04:57:30 +00:00
|
|
|
self::$_LCNF[$key] = array();
|
2015-04-01 15:06:42 +00:00
|
|
|
|
|
|
|
// And then assign the value
|
2015-03-08 04:57:30 +00:00
|
|
|
self::$_LCNF[$key][$subkey] = $value;
|
2015-04-01 15:06:42 +00:00
|
|
|
|
|
|
|
} else // Otherwise we just straight up assign it
|
2015-03-08 04:57:30 +00:00
|
|
|
self::$_LCNF[$key] = $value;
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
}
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
// Get values from the configuration in the database
|
|
|
|
public static function getConfig($key) {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-04-01 15:06:42 +00:00
|
|
|
// Check if the key that we're looking for exists
|
2015-03-08 04:57:30 +00:00
|
|
|
if(array_key_exists($key, self::$_DCNF))
|
2015-04-01 15:06:42 +00:00
|
|
|
return self::$_DCNF[$key]; // Then return the value
|
|
|
|
else // If it doesn't exist trigger an error to avoid explosions
|
2015-03-29 16:25:18 +00:00
|
|
|
trigger_error('Unable to get configuration value!', E_USER_ERROR);
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
}
|
2015-04-01 15:14:09 +00:00
|
|
|
/*
|
2015-04-01 15:06:42 +00:00
|
|
|
// Parse .cfg files, mainly/only used for templates
|
2015-04-01 15:08:38 +00:00
|
|
|
public static function parseCfg($data) {
|
2015-04-01 15:06:42 +00:00
|
|
|
|
|
|
|
// Create storage variable
|
|
|
|
$out = array();
|
|
|
|
|
|
|
|
// Remove comments and empty lines
|
|
|
|
$data = preg_replace('/#.*?\r\n/im', null, $data);
|
|
|
|
$data = preg_replace('/^\r\n/im', null, $data);
|
|
|
|
|
|
|
|
// Break line breaks up into array values
|
|
|
|
$data = explode("\r\n", $data);
|
|
|
|
|
|
|
|
foreach($data as $var) {
|
|
|
|
|
|
|
|
// Remove whitespace between key, equals sign and value
|
|
|
|
$var = preg_replace('/[\s+]=[\s+]/i', '=', $var);
|
|
|
|
|
|
|
|
// Then break this up
|
|
|
|
$var = explode('=', $var);
|
|
|
|
|
|
|
|
// And assign the value with the key to the output variable
|
|
|
|
$out[$var[0]] = $var[1];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the output variable
|
|
|
|
return $out;
|
|
|
|
|
2015-04-01 15:14:09 +00:00
|
|
|
}*/
|
2015-04-01 15:06:42 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
}
|