first few implementations of new Setting-class, refs #1325

Signed-off-by: Michael Kaufmann (d00p) <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann (d00p)
2013-12-15 11:47:23 +01:00
parent 4667ccbe43
commit 276d6b30d1
20 changed files with 448 additions and 247 deletions

View File

@@ -32,12 +32,6 @@
*/
class DbManager {
/**
* Settings array
* @var array
*/
private $_settings = null;
/**
* FroxlorLogger object
* @var object
@@ -53,11 +47,9 @@ class DbManager {
/**
* main constructor
*
* @param array $settings
* @param FroxlorLogger $log
*/
public function __construct($settings, &$log = null) {
$this->_settings = $settings;
public function __construct(&$log = null) {
$this->_log = $log;
$this->_setManager();
}
@@ -79,7 +71,7 @@ class DbManager {
Database::needRoot(true);
// check whether we shall create a random username
if (strtoupper($this->_settings['customer']['mysqlprefix']) == 'RANDOM') {
if (strtoupper(Settings::Get('customer.mysqlprefix')) == 'RANDOM') {
// get all usernames from db-manager
$allsqlusers = $this->getManager()->getAllSqlUsers();
// generate random username
@@ -89,7 +81,7 @@ class DbManager {
$username = $loginname . '-' . substr(md5(uniqid(microtime(), 1)), 20, 3);
}
} else {
$username = $loginname . $this->_settings['customer']['mysqlprefix'] . (intval($last_accnumber) + 1);
$username = $loginname . Settings::Get('customer.mysqlprefix') . (intval($last_accnumber) + 1);
}
// now create the database itself
@@ -97,7 +89,7 @@ class DbManager {
$this->_log->logAction(USR_ACTION, LOG_INFO, "created database '" . $username . "'");
// and give permission to the user on every access-host we have
foreach (array_map('trim', explode(',', $this->_settings['system']['mysql_access_host'])) as $mysql_access_host) {
foreach (array_map('trim', explode(',', Settings::Get('system.mysql_access_host'))) as $mysql_access_host) {
$this->getManager()->grantPrivilegesTo($username, $password, $mysql_access_host);
$this->_log->logAction(USR_ACTION, LOG_NOTICE, "grant all privileges for '" . $username . "'@'" . $mysql_access_host . "'");
}
@@ -125,6 +117,6 @@ class DbManager {
*/
private function _setManager() {
// TODO read different dbms from settings later
$this->_manager = new DbManagerMySQL($this->_settings, $this->_log);
$this->_manager = new DbManagerMySQL($this->_log);
}
}

View File

@@ -32,12 +32,6 @@
*/
class DbManagerMySQL {
/**
* Settings array
* @var array
*/
private $_settings = null;
/**
* FroxlorLogger object
* @var object
@@ -47,11 +41,9 @@ class DbManagerMySQL {
/**
* main constructor
*
* @param array $settings
* @param FroxlorLogger $log
*/
public function __construct($settings, &$log = null) {
$this->_settings = $settings;
public function __construct(&$log = null) {
$this->_log = $log;
}

View File

@@ -27,67 +27,67 @@ class paging {
* Userinfo
* @var array
*/
var $userinfo = array();
private $userinfo = array();
/**
* MySQL-Table
* @var string
*/
var $table = '';
private $table = '';
/**
* Fields with description which should be selectable
* @var array
*/
var $fields = array();
private $fields = array();
/**
* Entries per page
* @var int
*/
var $entriesperpage = 0;
private $entriesperpage = 0;
/**
* Number of entries of table
* @var int
*/
var $entries = 0;
private $entries = 0;
/**
* Sortorder, asc or desc
* @var string
*/
var $sortorder = 'asc';
public $sortorder = 'asc';
/**
* Sortfield
* @var string
*/
var $sortfield = '';
public $sortfield = '';
/**
* Searchfield
* @var string
*/
var $searchfield = '';
private $searchfield = '';
/**
* Searchtext
* @var string
*/
var $searchtext = '';
private $searchtext = '';
/**
* Pagenumber
* @var int
*/
var $pageno = 0;
private $pageno = 0;
/**
* Switch natsorting on/off
* @var bool
*/
var $natSorting = false;
private $natSorting = false;
/**
* Class constructor. Loads settings from request or from userdata and saves them to session.
@@ -95,10 +95,19 @@ class paging {
* @param array userinfo
* @param string Name of Table
* @param array Fields, in format array( 'fieldname_in_mysql' => 'field_caption' )
* @param int entries per page
* @param bool Switch natsorting on/off (global, affects all calls of sort)
* @param int *deprecated* entries per page
* @param bool *deprecated* Switch natsorting on/off (global, affects all calls of sort)
* @param int $default_field default sorting-field-index
* @param string $default_order default sorting order 'asc' or 'desc'
*
*/
function paging($userinfo, $table, $fields, $entriesperpage, $natSorting = false) {
public function __construct($userinfo, $table, $fields, $entriesperpage = 0, $natSorting = false, $default_field = 0, $default_order = 'asc') {
// entries per page and natsorting-flag are not
// passed as parameter anymore, because these are
// from the settings anyway
$entriesperpage = Settings::Get('panel.paging');
$natSorting = Settings::Get('panel.natsorting');
$this->userinfo = $userinfo;
@@ -129,7 +138,7 @@ class paging {
$this->sortorder = strtolower($this->userinfo['lastpaging']['sortorder']);
} else {
$this->sortorder = 'asc';
$this->sortorder = $default_order;
}
}
@@ -147,7 +156,7 @@ class paging {
$this->sortfield = $this->userinfo['lastpaging']['sortfield'];
} else {
$fieldnames = array_keys($fields);
$this->sortfield = $fieldnames[0];
$this->sortfield = $fieldnames[$default_field];
}
}
@@ -228,7 +237,7 @@ class paging {
*
* @param int entries
*/
function setEntries($entries) {
public function setEntries($entries) {
$this->entries = $entries;
@@ -245,7 +254,7 @@ class paging {
* @param int number of row
* @return bool to display or not to display, that's the question
*/
function checkDisplay($count) {
public function checkDisplay($count) {
$begin = (intval($this->pageno) - 1) * intval($this->entriesperpage);
$end = (intval($this->pageno) * intval($this->entriesperpage));
return (($count >= $begin && $count < $end) || $this->entriesperpage == 0);
@@ -257,7 +266,7 @@ class paging {
* @param bool should returned condition code start with WHERE (false) or AND (true)?
* @return string the condition code
*/
function getSqlWhere($append = false) {
public function getSqlWhere($append = false) {
if ($this->searchtext != '') {
if ($append == true) {
$condition = ' AND ';
@@ -326,7 +335,7 @@ class paging {
* @param bool Switch natsorting on/off (local, affects just this call)
* @return string the "order by"-code
*/
function getSqlOrderBy($natSorting = null) {
public function getSqlOrderBy($natSorting = null) {
$sortfield = explode('.', $this->sortfield);
foreach ($sortfield as $id => $field) {
@@ -366,7 +375,7 @@ class paging {
*
* @return string always empty
*/
function getSqlLimit() {
public function getSqlLimit() {
/**
* currently not in use
*/
@@ -379,7 +388,7 @@ class paging {
* @param array Language array
* @return string the html sortcode
*/
function getHtmlSortCode($lng, $break = false) {
public function getHtmlSortCode($lng, $break = false) {
$sortcode = '';
$fieldoptions = '';
@@ -405,7 +414,7 @@ class paging {
* @param string If set, only this field will be returned
* @return mixed An array or a string (if field is set) of html code of arrows
*/
function getHtmlArrowCode($baseurl, $field = '') {
public function getHtmlArrowCode($baseurl, $field = '') {
global $theme;
@@ -433,7 +442,7 @@ class paging {
* @param array Language array
* @return string the html searchcode
*/
function getHtmlSearchCode($lng) {
public function getHtmlSearchCode($lng) {
$searchcode = '';
$fieldoptions = '';
@@ -451,7 +460,7 @@ class paging {
* @param string URL to use as base for links
* @return string the html pagingcode
*/
function getHtmlPagingCode($baseurl) {
public function getHtmlPagingCode($baseurl) {
if ($this->entriesperpage == 0) {
return '';
} else {

View File

@@ -0,0 +1,205 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Michael Kaufmann <mkaufmann@nutime.de>
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Classes
*
* @since 0.9.31
*
*/
/**
* Class Settings
*
* Interaction with settings from the db
*
* @copyright (c) the authors
* @author Michael Kaufmann <mkaufmann@nutime.de>
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Classes
*/
class Settings {
/**
* current settings object
*
* @var object
*/
private static $_obj = null;
/**
* settings data
*
* @var array
*/
private static $_data = null;
/**
* changed and unsaved settings data
*
* @var array
*/
private static $_updatedata = null;
/**
* prepared statement for updating the
* settings table
*
* @var PDOStatement
*/
private static $_updstmt = null;
/**
* private constructor, reads in all settings
*/
private function __construct() {
$this->_readSettings();
self::$_updatedata = array();
// prepare statement
self::$_updstmt = Database::prepare("
UPDATE `".TABLE_PANEL_SETTINGS."` SET `value` = :value
WHERE `settinggroup` = :group AND `varname` = :varname
");
}
/**
* Read in all settings from the database
* and set the internal $_data array
*/
private function _readSettings() {
$settings_data = loadConfigArrayDir('actions/admin/settings/');
self::$_data = loadSettings($settings_data);
}
/**
* update a value in the database
*
* @param string $group
* @param string $varname
* @param string $value
*/
private function _storeSetting($group = null, $varname = null, $value = null) {
$upd_data = array(
'group' => $group,
'varname' => $varname,
'value' => $value
);
Database::pexecute(self::$_updstmt, $upd_data);
}
/**
* return a setting-value by its group and varname
*
* @param string $setting a group and a varname separated by a dot (group.varname)
*
* @return mixed
*/
public function pGet($setting = null) {
$sstr = explode(".", $setting);
// no separator - do'h
if (!isset($sstr[1])) {
return null;
}
$result = null;
if (isset(self::$_data[$sstr[0]][$sstr[1]])) {
$result = self::$_data[$sstr[0]][$sstr[1]];
}
return $result;
}
/**
* update a setting / set a new value
*
* @param string $setting a group and a varname separated by a dot (group.varname)
* @param string $value
* @param boolean $instant_save
*/
public function pSet($setting = null, $value = null, $instant_save = true) {
// check whether the setting exists
if (Settings::Get($setting) !== null) {
// set new value in array
$sstr = explode(".", $setting);
self::$_data[$sstr[0]][$sstr[1]] = $value;
// should we store to db instantly?
if ($instant_save) {
$this->_storeSetting($sstr[0], $sstr[1], $value);
} else {
if (!is_array(self::$_data[$sstr[0]])) {
self::$_data[$sstr[0]] = array();
}
self::$_data[$sstr[0]][$sstr[1]] = $value;
}
}
}
/**
* Store all un-saved changes to the database and
* re-read in all settings
*/
public function pFlush() {
if (is_array(self::$_updatedata) && count(self::$_updatedata) > 0) {
// save all un-saved changes to the settings
foreach ($self::$_updatedata as $group => $vargroup) {
foreach ($vargroup as $varname => $value) {
$this->_storeSetting($group, $varname, $value);
}
}
// now empty the array
self::$_updatedata = array();
// re-read in all settings
$this->_readSettings();
}
}
/**
* forget all un-saved changes to settings
*/
public function pStash() {
// empty update array
self::$_updatedata = array();
}
/**
* create new object and return instance
*
* @return object
*/
private static function getInstance() {
// do we got an object already?
if (self::$_obj == null) {
self::$_obj = new self();
}
// return it
return self::$_obj;
}
/**
* let's us interact with the settings-Object by using static
* call like "Settings::function()"
*
* @param string $name
* @param mixed $args
*
* @return mixed
*/
public static function __callStatic($name, $args) {
// as our functions are not static and therefore cannot
// be called statically, we prefix a 'p' to all of
// our public functions so we can use Settings::functionname()
// which looks cooler and is easier to use
$callback = array(self::getInstance(), "p".$name);
$result = call_user_func_array($callback, $args);
return $result;
}
}

View File

@@ -19,7 +19,7 @@
function correctMysqlUsers($mysql_access_host_array) {
global $settings, $log;
global $log;
// get sql-root access data
Database::needRoot(true);
@@ -36,7 +36,7 @@ function correctMysqlUsers($mysql_access_host_array) {
Database::needSqlData();
$sql_root = Database::getSqlData();
$dbm = new DbManager($settings, $log);
$dbm = new DbManager($log);
$users = $dbm->getManager()->getAllSqlUsers(false);
$databases = array(

View File

@@ -21,21 +21,19 @@
* updates the panel.version field
* to the given value (no checks here!)
*
* @param string new-version
* @param string $new_version new-version
*
* @return bool true on success, else false
* @return bool true on success, else false
*/
function updateToVersion($new_version = null) {
global $settings;
if ($new_version !== null && $new_version != '') {
$upd_stmt = Database::prepare("
UPDATE `".TABLE_PANEL_SETTINGS."` SET `value` = :newversion
WHERE `settinggroup` = 'panel' AND `varname` = 'version'"
);
Database::pexecute($upd_stmt, array('newversion' => $new_version));
$settings['panel']['version'] = $new_version;
Settings::Set('panel.version', $new_version);
return true;
}
return false;
@@ -46,13 +44,12 @@ function updateToVersion($new_version = null) {
*
* checks if the panel is froxlor
*
* @return bool true if panel is froxlor, else false
* @return bool true if panel is froxlor, else false
*/
function isFroxlor() {
global $settings;
if (isset($settings['panel']['frontend'])
&& $settings['panel']['frontend'] == 'froxlor'
if (Settings::Get('panel.frontend') !== null
&& Settings::Get('panel.frontend') == 'froxlor'
) {
return true;
}
@@ -65,16 +62,14 @@ function isFroxlor() {
* checks if a given version is the
* current one (and panel is froxlor)
*
* @param string version to check
* @param string $to_check version to check
*
* @return bool true if version to check matches, else false
* @return bool true if version to check matches, else false
*/
function isFroxlorVersion($to_check = null) {
global $settings;
if ($settings['panel']['frontend'] == 'froxlor'
&& $settings['panel']['version'] == $to_check
if (Settings::Get('panel.frontend') == 'froxlor'
&& Settings::Get('panel.version') == $to_check
) {
return true;
}
@@ -82,21 +77,18 @@ function isFroxlorVersion($to_check = null) {
}
/**
* Function isFroxlorVersion
* Function hasUpdates
*
* checks if a given version is the
* current one (and panel is froxlor)
* checks if a given version is not equal the current one
*
* @param string version to check
* @param string $to_check version to check
*
* @return bool true if version to check matches, else false
* @return bool true if version to check does not match, else false
*/
function hasUpdates($to_check = null) {
global $settings;
if (!isset($settings['panel']['version'])
|| $settings['panel']['version'] != $to_check
if (Settings::Get('panel.version') == null
|| Settings::Get('panel.version') != $to_check
) {
return true;
}