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
|
2012-03-28 11:23:08 -04:00
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
2012-02-01 10:51:06 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for manipulating datbase connections, and program settings
|
2012-03-28 11:23:08 -04:00
|
|
|
*
|
2012-02-01 10:51:06 -05:00
|
|
|
* 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-03-28 11:23:08 -04:00
|
|
|
|
2012-02-01 10:51:06 -05:00
|
|
|
/**
|
2012-03-27 10:57:42 -04:00
|
|
|
* Load the settings file - private so it can't be loaded
|
|
|
|
* directly - the settings should be safe!
|
2012-02-01 10:51:06 -05:00
|
|
|
*/
|
2012-03-27 10:57:42 -04:00
|
|
|
private function __construct()
|
2012-02-01 10:51:06 -05:00
|
|
|
{
|
2012-03-28 11:23:08 -04: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-03-27 10:57:42 -04: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
|
2012-03-28 11:23:08 -04:00
|
|
|
*
|
2012-02-01 16:36:55 -05:00
|
|
|
* @param string $key
|
2012-03-28 11:23:08 -04:00
|
|
|
* @return mixed
|
2012-02-01 16:36:55 -05:00
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function __get($key)
|
2012-02-01 16:36:55 -05:00
|
|
|
{
|
2012-03-28 11:23:08 -04:00
|
|
|
return (isset($this->current->{$key}) && $key != "dbs")
|
|
|
|
? $this->current->{$key}
|
2012-02-21 17:00:22 -05:00
|
|
|
: NULL;
|
2012-02-01 16:36:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Magic method to simplify setting config options
|
2012-03-28 11:23:08 -04:00
|
|
|
*
|
2012-02-01 16:36:55 -05:00
|
|
|
* @param string $key
|
2012-03-28 11:23:08 -04:00
|
|
|
* @param mixed
|
2012-02-01 16:36:55 -05:00
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2012-03-28 11:23:08 -04:00
|
|
|
return $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;
|
|
|
|
}
|
2012-03-27 15:47:56 -04:00
|
|
|
|
|
|
|
// Save the json
|
|
|
|
$this->__destruct();
|
2012-02-21 17:00:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2012-03-27 15:47:56 -04:00
|
|
|
|
|
|
|
// Save the json
|
|
|
|
$this->__destruct();
|
2012-02-01 16:36:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a database connection
|
2012-03-28 11:23:08 -04: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-03-27 15:47:56 -04:00
|
|
|
|
|
|
|
// Save the json
|
|
|
|
$this->__destruct();
|
2012-02-01 10:51:06 -05:00
|
|
|
}
|
2012-02-01 16:36:55 -05:00
|
|
|
|
2012-02-02 22:18:50 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-03-28 11:23:08 -04:00
|
|
|
|
2012-02-02 22:18:50 -05:00
|
|
|
/**
|
|
|
|
* Retreive all db connections
|
2012-03-28 11:23:08 -04:00
|
|
|
*
|
|
|
|
* @return array
|
2012-02-02 22:18:50 -05:00
|
|
|
*/
|
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)
|
|
|
|
{
|
2012-03-28 11:23:08 -04:00
|
|
|
return (isset($this->current->dbs->{$name}))
|
|
|
|
? $this->current->dbs->{$name}
|
|
|
|
: FALSE;
|
2012-02-29 14:36:42 -05:00
|
|
|
}
|
|
|
|
|
2012-02-01 10:51:06 -05:00
|
|
|
}
|
|
|
|
// End of settings.php
|