TrackManiaControl/core/Settings/SettingManager.php

493 lines
13 KiB
PHP
Raw Normal View History

<?php
2013-12-09 11:25:30 +01:00
2014-01-27 09:07:43 +01:00
namespace ManiaControl\Settings;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\Callbacks;
use ManiaControl\General\UsageInformationAble;
use ManiaControl\General\UsageInformationTrait;
2014-02-12 21:12:51 +01:00
use ManiaControl\ManiaControl;
2014-04-30 03:45:54 +02:00
use ManiaControl\Plugins\PluginManager;
use ManiaControl\Utils\ClassUtil;
/**
* Class managing ManiaControl Settings and Configurations
2014-05-02 17:50:30 +02:00
*
* @author ManiaControl Team <mail@maniacontrol.com>
2019-01-05 21:02:24 +01:00
* @copyright 2014-2019 ManiaControl Team
2014-05-02 17:50:30 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class SettingManager implements CallbackListener, UsageInformationAble {
use UsageInformationTrait;
/*
* Constants
*/
2014-05-13 14:15:00 +02:00
const TABLE_SETTINGS = 'mc_settings';
const CB_SETTING_CHANGED = 'SettingManager.SettingChanged';
2014-05-02 17:50:30 +02:00
/*
* Private properties
*/
2014-07-25 16:39:07 +02:00
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
2014-07-25 16:39:07 +02:00
/** @var Setting[] $storedSettings */
2014-04-20 19:25:06 +02:00
private $storedSettings = array();
/**
* Construct a new setting manager instance
2014-05-02 17:50:30 +02:00
*
2014-01-05 22:51:21 +01:00
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->initTables();
2014-02-12 21:12:51 +01:00
// Callbacks
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'handleAfterInit');
}
/**
* Initialize the necessary database tables
2014-05-02 17:50:30 +02:00
*
* @return bool
*/
private function initTables() {
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-04-20 19:25:06 +02:00
$settingTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SETTINGS . "` (
`index` INT(11) NOT NULL AUTO_INCREMENT,
`class` VARCHAR(100) NOT NULL,
`setting` VARCHAR(150) NOT NULL,
`type` VARCHAR(50) NOT NULL,
2017-02-04 11:41:31 +01:00
`value` VARCHAR(150) NOT NULL,
`default` VARCHAR(100) NOT NULL,
`set` VARCHAR(100) NOT NULL,
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`index`),
UNIQUE KEY `settingId` (`class`,`setting`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Settings and Configurations' AUTO_INCREMENT=1;";
2014-06-22 18:26:01 +02:00
$result = $mysqli->query($settingTableQuery);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
}
2014-06-22 18:26:01 +02:00
return $result;
}
2014-05-02 17:50:30 +02:00
/**
* Handle After Init Callback
*/
public function handleAfterInit() {
$this->deleteUnusedSettings();
}
/**
* Delete all unused Settings that haven't been initialized during the current Startup
*
* @return bool
*/
private function deleteUnusedSettings() {
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-05-13 14:15:00 +02:00
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
2014-05-02 17:50:30 +02:00
WHERE `changed` < NOW() - INTERVAL 1 HOUR;";
2014-05-13 14:15:00 +02:00
$result = $mysqli->query($settingQuery);
2014-05-02 17:50:30 +02:00
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
2014-05-13 14:15:00 +02:00
if ($result) {
$this->clearStorage();
2014-05-13 14:15:00 +02:00
return true;
2014-05-02 17:50:30 +02:00
}
2014-05-13 14:15:00 +02:00
return false;
2014-05-02 17:50:30 +02:00
}
/**
* Clear the Settings Storage
*/
public function clearStorage() {
$this->storedSettings = array();
}
/**
* @deprecated
* @see SettingManager::getSettingValueByIndex()
*/
public function getSettingByIndex($settingIndex, $defaultValue = null) {
return $this->getSettingValueByIndex($settingIndex, $defaultValue);
}
/**
* Get a Setting Value by its Index
2014-05-02 17:50:30 +02:00
*
2014-05-13 14:15:00 +02:00
* @param int $settingIndex
* @param mixed $defaultValue
* @return mixed
*/
public function getSettingValueByIndex($settingIndex, $defaultValue = null) {
$setting = $this->getSettingObjectByIndex($settingIndex);
if (!$setting) {
return $defaultValue;
}
return $setting->value;
}
/**
* Get a Setting Object by its Index
*
* @param int $settingIndex
2014-05-13 14:15:00 +02:00
* @return Setting
2014-05-02 17:50:30 +02:00
*/
public function getSettingObjectByIndex($settingIndex) {
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-05-13 14:15:00 +02:00
$settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE `index` = {$settingIndex};";
$result = $mysqli->query($settingQuery);
2014-05-02 17:50:30 +02:00
if ($mysqli->error) {
trigger_error($mysqli->error);
2014-05-13 14:15:00 +02:00
return null;
2014-05-02 17:50:30 +02:00
}
2014-05-13 14:15:00 +02:00
if ($result->num_rows <= 0) {
$result->free();
return null;
2014-05-02 17:50:30 +02:00
}
2014-05-13 14:15:00 +02:00
/** @var Setting $setting */
$setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null));
$result->free();
$this->storeSetting($setting);
2013-11-10 15:11:50 +01:00
2014-05-13 14:15:00 +02:00
return $setting;
}
/**
* Store the given Setting
*
* @param Setting $setting
*/
private function storeSetting(Setting $setting) {
$this->storedSettings[$setting->class . $setting->setting] = $setting;
}
2013-11-10 13:23:35 +01:00
/**
2014-05-13 14:15:00 +02:00
* Set a Setting for the given Object
2014-05-02 17:50:30 +02:00
*
2014-05-13 14:15:00 +02:00
* @param mixed $object
* @param string $settingName
2014-05-02 17:50:30 +02:00
* @param mixed $value
* @return bool
2013-11-10 13:23:35 +01:00
*/
2014-05-13 14:15:00 +02:00
public function setSetting($object, $settingName, $value) {
//TODO nowhere used, everywhere saveSettings used, is it depreciated?
$setting = $this->getSettingObject($object, $settingName);
2014-05-13 14:15:00 +02:00
if ($setting) {
$setting->value = $value;
$saved = $this->saveSetting($setting);
if (!$saved) {
return false;
2014-05-13 14:15:00 +02:00
}
} else {
$saved = $this->initSetting($object, $settingName, $value);
if (!$saved) {
return false;
}
$setting = $this->getSettingObject($object, $settingName, $value);
2014-01-05 22:51:21 +01:00
}
2014-05-13 14:15:00 +02:00
$this->storeSetting($setting);
2014-05-13 14:15:00 +02:00
return true;
2014-01-05 22:51:21 +01:00
}
/**
* Get Setting by Name for the given Object
2014-05-02 17:50:30 +02:00
*
* @param mixed $object
* @param string $settingName
2014-05-02 17:50:30 +02:00
* @param mixed $default
2014-05-13 14:15:00 +02:00
* @return Setting
2014-01-05 22:51:21 +01:00
*/
public function getSettingObject($object, $settingName, $default = null) {
2014-05-13 14:15:00 +02:00
$settingClass = ClassUtil::getClass($object);
2014-05-02 17:50:30 +02:00
2014-05-13 14:15:00 +02:00
// Retrieve from Storage if possible
$storedSetting = $this->getStoredSetting($object, $settingName);
if ($storedSetting) {
return $storedSetting;
2014-04-20 19:25:06 +02:00
}
2014-05-02 17:50:30 +02:00
2014-05-13 14:15:00 +02:00
// Fetch setting
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-05-13 14:15:00 +02:00
$settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = '" . $mysqli->escape_string($settingClass) . "'
AND `setting` = '" . $mysqli->escape_string($settingName) . "';";
$result = $mysqli->query($settingQuery);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
2014-05-13 14:15:00 +02:00
if ($result->num_rows <= 0) {
$result->free();
2014-05-13 14:15:00 +02:00
if ($default === null) {
return null;
}
$saved = $this->initSetting($object, $settingName, $default);
if ($saved) {
return $this->getSettingObject($object, $settingName, $default);
} else {
return null;
}
}
2014-05-02 17:50:30 +02:00
2014-05-13 14:15:00 +02:00
/** @var Setting $setting */
$setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null));
2014-05-13 14:15:00 +02:00
$result->free();
$this->storeSetting($setting);
2014-05-13 14:15:00 +02:00
return $setting;
}
2013-11-10 13:23:35 +01:00
/**
* Retrieve a stored Setting
*
* @param mixed $settingClass
* @param string $settingName
* @return Setting
*/
private function getStoredSetting($settingClass, $settingName) {
$settingClass = ClassUtil::getClass($settingClass);
if (isset($this->storedSettings[$settingClass . $settingName])) {
return $this->storedSettings[$settingClass . $settingName];
}
return null;
}
2014-01-05 22:51:21 +01:00
/**
2014-05-13 14:15:00 +02:00
* Initialize a Setting for the given Object
2014-05-02 17:50:30 +02:00
*
* @param mixed $object
2014-01-05 22:51:21 +01:00
* @param string $settingName
2014-05-13 14:15:00 +02:00
* @param mixed $defaultValue
* @return bool
2013-11-10 13:23:35 +01:00
*/
2014-05-13 14:15:00 +02:00
public function initSetting($object, $settingName, $defaultValue) {
$setting = new Setting($object, $settingName, $defaultValue);
return $this->saveSetting($setting, true);
2014-05-13 14:15:00 +02:00
}
2014-05-02 17:50:30 +02:00
2014-05-13 14:15:00 +02:00
/**
* Save the given Setting in the Database
*
* @param Setting $setting
* @param bool $init
2014-05-13 14:15:00 +02:00
* @return bool
*/
public function saveSetting(Setting $setting, $init = false) {
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
if ($init) {
// Init - Keep old value if the default didn't change
$valueUpdateString = '`value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`))';
} else {
// Set - Update value in any case
$valueUpdateString = '`value` = VALUES(`value`)';
}
2014-05-13 14:15:00 +02:00
$settingQuery = "INSERT INTO `" . self::TABLE_SETTINGS . "` (
`class`,
`setting`,
`type`,
`value`,
2014-05-18 21:46:00 +02:00
`default`,
`set`
2014-05-13 14:15:00 +02:00
) VALUES (
2014-06-15 02:13:00 +02:00
?, ?, ?, ?, ?, ?
2014-05-13 14:15:00 +02:00
) ON DUPLICATE KEY UPDATE
`index` = LAST_INSERT_ID(`index`),
`type` = VALUES(`type`),
{$valueUpdateString},
2014-05-13 14:15:00 +02:00
`default` = VALUES(`default`),
2014-05-18 21:46:00 +02:00
`set` = VALUES(`set`),
2014-05-13 14:15:00 +02:00
`changed` = NOW();";
2013-11-10 13:23:35 +01:00
$settingStatement = $mysqli->prepare($settingQuery);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
2013-11-10 13:23:35 +01:00
trigger_error($mysqli->error);
return false;
}
2014-05-18 21:46:00 +02:00
$formattedValue = $setting->getFormattedValue();
$formattedDefault = $setting->getFormattedDefault();
$formattedSet = $setting->getFormattedSet();
$settingStatement->bind_param('ssssss', $setting->class, $setting->setting, $setting->type, $formattedValue, $formattedDefault, $formattedSet);
2014-05-13 14:15:00 +02:00
$settingStatement->execute();
2014-01-27 09:07:43 +01:00
if ($settingStatement->error) {
2013-11-10 13:23:35 +01:00
trigger_error($settingStatement->error);
$settingStatement->close();
2013-11-10 13:23:35 +01:00
return false;
}
$settingStatement->close();
// Trigger Settings Changed Callback
if (!$init) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_SETTING_CHANGED, $setting);
2014-07-04 19:58:59 +02:00
}
2014-05-13 14:15:00 +02:00
return true;
2013-11-10 13:23:35 +01:00
}
2013-11-10 15:11:50 +01:00
/**
* @deprecated
* @see SettingManager::getSettingValue()
*/
public function getSetting($object, $settingName, $default = null) {
return $this->getSettingValue($object, $settingName, $default);
}
2014-05-13 16:03:26 +02:00
/**
* Get the Setting Value directly
*
* @param mixed $object
* @param string $settingName
* @param mixed $default
* @return mixed
*/
public function getSettingValue($object, $settingName, $default = null) {
$setting = $this->getSettingObject($object, $settingName, $default);
2014-05-13 16:03:26 +02:00
if ($setting) {
return $setting->value;
}
return null;
}
2014-05-02 17:50:30 +02:00
/**
2014-05-13 14:15:00 +02:00
* Reset a Setting to its Default Value
2014-05-02 17:50:30 +02:00
*
* @param mixed $object
* @param string $settingName
2013-11-10 15:11:50 +01:00
* @return bool
*/
2014-05-13 14:15:00 +02:00
public function resetSetting($object, $settingName = null) {
if ($object instanceof Setting) {
$className = $object->class;
$settingName = $object->setting;
} else {
$className = ClassUtil::getClass($object);
}
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-05-13 14:15:00 +02:00
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
2013-11-10 15:11:50 +01:00
SET `value` = `default`
2014-05-13 14:15:00 +02:00
WHERE `class` = '" . $mysqli->escape_string($className) . "'
AND `setting` = '" . $mysqli->escape_string($settingName) . "';";
$result = $mysqli->query($settingQuery);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
2013-11-10 15:11:50 +01:00
trigger_error($mysqli->error);
return false;
}
if (isset($this->storedSettings[$className . $settingName])) {
unset($this->storedSettings[$className . $settingName]);
}
2014-05-13 14:15:00 +02:00
return $result;
2013-12-09 10:19:29 +01:00
}
/**
* Delete a Setting
2014-05-02 17:50:30 +02:00
*
* @param mixed $object
* @param string $settingName
2013-12-09 10:19:29 +01:00
* @return bool
*/
2014-05-13 14:15:00 +02:00
public function deleteSetting($object, $settingName = null) {
if ($object instanceof Setting) {
$className = $object->class;
$settingName = $object->setting;
} else {
$className = ClassUtil::getClass($object);
}
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-05-13 14:15:00 +02:00
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = '" . $mysqli->escape_string($className) . "'
AND `setting` = '" . $mysqli->escape_string($settingName) . "';";
$result = $mysqli->query($settingQuery);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
2013-12-09 10:19:29 +01:00
trigger_error($mysqli->error);
return false;
}
2014-05-13 14:15:00 +02:00
if (isset($this->storedSettings[$className . $settingName])) {
unset($this->storedSettings[$className . $settingName]);
}
2014-05-13 14:15:00 +02:00
return $result;
2013-11-10 15:11:50 +01:00
}
2014-01-18 20:05:50 +01:00
/**
* Get all Settings for the given Class
2014-05-02 17:50:30 +02:00
*
* @param mixed $object
2014-05-27 23:00:39 +02:00
* @return Setting[]
2014-01-18 20:05:50 +01:00
*/
public function getSettingsByClass($object) {
2014-05-13 14:15:00 +02:00
$className = ClassUtil::getClass($object);
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-05-13 14:15:00 +02:00
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = '" . $mysqli->escape_string($className) . "'
2014-01-18 20:05:50 +01:00
ORDER BY `setting` ASC;";
2014-05-02 17:50:30 +02:00
$result = $mysqli->query($query);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
2014-01-18 20:05:50 +01:00
trigger_error($mysqli->error);
return null;
}
$settings = array();
while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null))) {
2014-01-18 20:05:50 +01:00
$settings[$setting->index] = $setting;
}
$result->free();
return $settings;
}
/**
2014-05-13 14:15:00 +02:00
* Get all Settings
2014-05-02 17:50:30 +02:00
*
2014-05-27 23:00:39 +02:00
* @return Setting[]
*/
public function getSettings() {
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-05-02 17:50:30 +02:00
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
ORDER BY `class` ASC, `setting` ASC;";
$result = $mysqli->query($query);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
$settings = array();
while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null))) {
$settings[$setting->index] = $setting;
}
$result->free();
return $settings;
}
2014-02-12 21:12:51 +01:00
2014-04-30 03:45:54 +02:00
/**
* Get all Setting Classes
*
* @param bool $hidePluginClasses
2014-05-27 23:00:39 +02:00
* @return string[]
2014-04-30 03:45:54 +02:00
*/
public function getSettingClasses($hidePluginClasses = false) {
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-05-02 17:50:30 +02:00
$query = "SELECT DISTINCT `class` FROM `" . self::TABLE_SETTINGS . "`
2014-04-30 03:45:54 +02:00
ORDER BY `class` ASC;";
$result = $mysqli->query($query);
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
$settingClasses = array();
2014-05-13 14:15:00 +02:00
while ($row = $result->fetch_object()) {
if (!$hidePluginClasses || !PluginManager::isPluginClass($row->class)) {
array_push($settingClasses, $row->class);
2014-04-30 03:45:54 +02:00
}
}
$result->free();
return $settingClasses;
}
}