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 {
|
|
|
|
|
|
|
|
protected $current;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the settings file
|
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function __construct()
|
2012-02-01 10:51:06 -05:00
|
|
|
{
|
|
|
|
$path = BASE_DIR.'/settings.json';
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the settings file on close, just to be safe
|
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function __destruct()
|
2012-02-01 10:51:06 -05:00
|
|
|
{
|
2012-02-01 16:36:55 -05:00
|
|
|
file_put_contents(BASE_DIR.'/settings.json', json_encode($this->current));
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
|
|
|
return (isset($this->current->{$key})) ? $this->current->{$key} : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
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 11:45:42 -05:00
|
|
|
public function get_dbs()
|
2012-02-02 22:18:50 -05:00
|
|
|
{
|
|
|
|
return $this->current->dbs;
|
|
|
|
}
|
2012-02-01 10:51:06 -05:00
|
|
|
}
|
|
|
|
// End of settings.php
|