2012-05-31 09:56:49 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* OpenSQLManager
|
|
|
|
*
|
|
|
|
* Free Database manager for Open Source Databases
|
|
|
|
*
|
|
|
|
* @package OpenSQLManager
|
|
|
|
* @author Timothy J. Warren
|
|
|
|
* @copyright Copyright (c) 2012
|
|
|
|
* @link https://github.com/aviat4ion/OpenSQLManager
|
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace OpenSQLManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for manipulating datbase connections, and miscellaneous settings
|
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @subpackage Helper Classes
|
|
|
|
*/
|
|
|
|
class Settings {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Settings object represented by the currently loaded JSON file
|
|
|
|
*/
|
|
|
|
private $current;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Singleton instance
|
|
|
|
*/
|
|
|
|
private static $instance;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static method to retreive current instance
|
|
|
|
* of the singleton
|
|
|
|
*
|
|
|
|
* @return Settings
|
|
|
|
*/
|
|
|
|
public static function &get_instance()
|
|
|
|
{
|
|
|
|
if( ! isset(self::$instance))
|
|
|
|
{
|
|
|
|
$name = __CLASS__;
|
|
|
|
self::$instance = new $name();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the settings file - private so it can't be loaded
|
|
|
|
* directly - the settings should be safe!
|
|
|
|
*/
|
|
|
|
private function __construct()
|
|
|
|
{
|
|
|
|
// For testing and use outside of OpenSQLManager,
|
|
|
|
// define a different SETTINGS_DIR
|
|
|
|
if ( ! defined('SETTINGS_DIR'))
|
|
|
|
{
|
|
|
|
define('SETTINGS_DIR', '.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$path = SETTINGS_DIR.'/settings.json';
|
|
|
|
|
|
|
|
if( ! is_file($path))
|
|
|
|
{
|
|
|
|
//Create the file!
|
|
|
|
touch($path);
|
2012-05-31 14:18:37 -04:00
|
|
|
$this->current = new \stdClass();
|
2012-05-31 09:56:49 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->current = json_decode(file_get_contents($path));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the DB object under the settings if it doesn't already exist
|
|
|
|
if( ! isset($this->current->dbs))
|
|
|
|
{
|
2012-05-31 14:18:37 -04:00
|
|
|
$this->current->dbs = new \stdClass();
|
2012-05-31 09:56:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output the settings on destruct
|
|
|
|
*/
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
$file_string = (defined('JSON_PRETTY_PRINT'))
|
|
|
|
? json_encode($this->current, JSON_PRETTY_PRINT)
|
|
|
|
: json_encode($this->current);
|
|
|
|
|
|
|
|
file_put_contents(SETTINGS_DIR . '/settings.json', $file_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Magic method to simplify isset checking for config options
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __get($key)
|
|
|
|
{
|
|
|
|
return (isset($this->current->{$key}) && $key != "dbs")
|
|
|
|
? $this->current->{$key}
|
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Magic method to simplify setting config options
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed
|
|
|
|
*/
|
|
|
|
public function __set($key, $val)
|
|
|
|
{
|
|
|
|
//Don't allow direct db config changes
|
|
|
|
if($key == "dbs")
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->current->{$key} = $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a database connection
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public function add_db($name, $params)
|
|
|
|
{
|
|
|
|
// Return on bad data
|
|
|
|
if (empty($name) || empty($params))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-06-04 16:49:30 -04:00
|
|
|
if ( ! isset($this->current->dbs->{$name}))
|
2012-05-31 09:56:49 -04:00
|
|
|
{
|
2012-06-04 16:49:30 -04:00
|
|
|
$params->name = $name;
|
2012-05-31 09:56:49 -04:00
|
|
|
|
|
|
|
$this->current->dbs->{$name} = array();
|
|
|
|
$this->current->dbs->{$name} = $params;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the json
|
|
|
|
$this->__destruct();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Edit a database connection
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public function edit_db($name, $params)
|
|
|
|
{
|
|
|
|
// Return on bad data
|
|
|
|
if (empty($name) || empty($params))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-06-04 16:49:30 -04:00
|
|
|
|
|
|
|
if (is_object($params))
|
|
|
|
{
|
|
|
|
$params = (array) $params;
|
|
|
|
}
|
2012-05-31 09:56:49 -04:00
|
|
|
|
2012-06-04 16:49:30 -04:00
|
|
|
if (isset($this->current->dbs->{$name}) && ($name === $params->name))
|
2012-05-31 09:56:49 -04:00
|
|
|
{
|
|
|
|
$this->current->dbs->{$name} = $params;
|
|
|
|
}
|
2012-06-04 16:49:30 -04:00
|
|
|
elseif ($name !== $params->name)
|
2012-05-31 09:56:49 -04:00
|
|
|
{
|
|
|
|
unset($this->current->dbs->{$name});
|
|
|
|
|
2012-06-04 16:49:30 -04:00
|
|
|
if ( ! isset($this->current->dbs->{$params->name}))
|
2012-05-31 09:56:49 -04:00
|
|
|
{
|
2012-06-04 16:49:30 -04:00
|
|
|
$this->current->dbs->{$params->name} = $params;
|
2012-05-31 09:56:49 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the json
|
|
|
|
$this->__destruct();
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a database connection
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
public function remove_db($name)
|
|
|
|
{
|
|
|
|
if( ! isset($this->current->dbs->{$name}))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the db name from the object
|
|
|
|
unset($this->current->dbs->{$name});
|
|
|
|
|
|
|
|
// Save the json
|
|
|
|
$this->__destruct();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retreive all db connections
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_dbs()
|
|
|
|
{
|
|
|
|
return $this->current->dbs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// End of settings.php
|