2012-02-01 10:51:06 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* OpenSQLManager
|
|
|
|
*
|
|
|
|
* Free Database manager for Open Source Databases
|
|
|
|
*
|
|
|
|
* @author Timothy J. Warren
|
|
|
|
* @copyright Copyright (c) 2012
|
|
|
|
* @link https://github.com/aviat4ion/OpenSQLManager
|
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for manipulating datbase connections, and program settings
|
|
|
|
*
|
|
|
|
* Use JSON for compatibility
|
|
|
|
*/
|
|
|
|
class Settings {
|
|
|
|
|
2012-02-21 17:00:22 -05:00
|
|
|
private $current;
|
2012-02-21 20:15:49 -05:00
|
|
|
private static $instance;
|
|
|
|
|
|
|
|
public static function &get_instance()
|
|
|
|
{
|
|
|
|
if( ! isset(self::$instance))
|
|
|
|
{
|
|
|
|
$name = __CLASS__;
|
|
|
|
self::$instance = new $name();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
}
|
2012-02-01 10:51:06 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the settings file
|
|
|
|
*/
|
2012-02-21 20:15:49 -05:00
|
|
|
protected function __construct()
|
2012-02-01 10:51:06 -05:00
|
|
|
{
|
2012-02-29 16:14:21 -05:00
|
|
|
$path = SETTINGS_DIR.'/settings.json';
|
2012-02-01 10:51:06 -05:00
|
|
|
|
|
|
|
if( ! is_file($path))
|
|
|
|
{
|
|
|
|
//Create the file!
|
|
|
|
touch($path);
|
|
|
|
$this->current = new stdClass();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->current = json_decode(file_get_contents($path));
|
|
|
|
}
|
|
|
|
|
2012-02-01 16:36:55 -05:00
|
|
|
// Add the DB object under the settings if it doesn't already exist
|
|
|
|
if( ! isset($this->current->dbs))
|
|
|
|
{
|
|
|
|
$this->current->dbs = new stdClass();
|
|
|
|
}
|
|
|
|
|
2012-02-01 10:51:06 -05:00
|
|
|
}
|
|
|
|
|
2012-02-21 20:15:49 -05:00
|
|
|
/**
|
|
|
|
* Output the settings on destruct
|
|
|
|
*/
|
2012-02-21 20:19:21 -05:00
|
|
|
public function __destruct()
|
2012-02-21 20:15:49 -05:00
|
|
|
{
|
2012-02-29 16:14:21 -05:00
|
|
|
file_put_contents(SETTINGS_DIR . '/settings.json', json_encode($this->current));
|
2012-02-21 20:15:49 -05:00
|
|
|
}
|
|
|
|
|
2012-02-01 10:51:06 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-02-01 16:36:55 -05:00
|
|
|
/**
|
|
|
|
* Magic method to simplify isset checking for config options
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return $mixed
|
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function __get($key)
|
2012-02-01 16:36:55 -05:00
|
|
|
{
|
2012-02-21 17:00:22 -05:00
|
|
|
return (isset($this->current->{$key}) && $key != "dbs")
|
|
|
|
? $this->current->{$key}
|
|
|
|
: NULL;
|
2012-02-01 16:36:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Magic method to simplify setting config options
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed $val
|
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function __set($key, $val)
|
2012-02-01 16:36:55 -05:00
|
|
|
{
|
|
|
|
//Don't allow direct db config changes
|
|
|
|
if($key == "dbs")
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->current->{$key} = $val;
|
2012-02-01 10:51:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a database connection
|
2012-02-01 16:36:55 -05:00
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param array $params
|
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function add_db($name, $params)
|
2012-02-01 16:36:55 -05:00
|
|
|
{
|
2012-02-21 11:38:45 -05:00
|
|
|
if( ! isset($this->current->dbs->{$name}))
|
2012-02-01 16:36:55 -05:00
|
|
|
{
|
2012-02-06 12:14:39 -05:00
|
|
|
$this->current->dbs->{$name} = array();
|
2012-02-01 16:36:55 -05:00
|
|
|
$this->current->dbs->{$name} = $params;
|
2012-02-21 17:00:22 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Edit a database connection
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public function edit_db($name, $params)
|
|
|
|
{
|
|
|
|
if(isset($this->current->dbs->{$name}))
|
|
|
|
{
|
|
|
|
$this->current->dbs->{$name} = $params;
|
2012-02-01 16:36:55 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a database connection
|
2012-02-01 10:51:06 -05:00
|
|
|
*
|
2012-02-01 16:36:55 -05:00
|
|
|
* @param string $name
|
2012-02-01 10:51:06 -05:00
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function remove_db($name)
|
2012-02-01 10:51:06 -05:00
|
|
|
{
|
2012-02-01 16:36:55 -05:00
|
|
|
if( ! isset($this->current->dbs->{$name}))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the db name from the object
|
|
|
|
unset($this->current->dbs->{$name});
|
2012-02-01 10:51:06 -05:00
|
|
|
}
|
2012-02-01 16:36:55 -05:00
|
|
|
|
2012-02-02 22:18:50 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retreive all db connections
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-02-21 17:00:22 -05:00
|
|
|
public function get_dbs()
|
|
|
|
{
|
|
|
|
return $this->current->dbs;
|
|
|
|
}
|
2012-02-29 14:36:42 -05:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retreive a specific database connection
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function get_db($name)
|
|
|
|
{
|
|
|
|
return (isset($this->current->dbs->{$name})) ? $this->current->dbs->{$name} : FALSE;
|
|
|
|
}
|
|
|
|
|
2012-02-01 10:51:06 -05:00
|
|
|
}
|
|
|
|
// End of settings.php
|