2013-11-10 12:51:41 +01:00
|
|
|
<?php
|
2013-12-09 11:25:30 +01:00
|
|
|
|
2014-01-27 09:07:43 +01:00
|
|
|
namespace ManiaControl\Settings;
|
|
|
|
|
2014-02-12 21:07:54 +01:00
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
2014-05-24 16:39:12 +02:00
|
|
|
use ManiaControl\Callbacks\Callbacks;
|
2017-03-26 19:44:55 +02:00
|
|
|
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;
|
2014-05-13 14:32:30 +02:00
|
|
|
use ManiaControl\Utils\ClassUtil;
|
2013-11-10 12:51:41 +01:00
|
|
|
|
|
|
|
/**
|
2014-07-25 16:28:47 +02:00
|
|
|
* Class managing ManiaControl Settings and Configurations
|
2014-05-02 17:50:30 +02:00
|
|
|
*
|
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2020-01-22 10:39:35 +01:00
|
|
|
* @copyright 2014-2020 ManiaControl Team
|
2014-05-02 17:50:30 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2013-11-10 12:51:41 +01:00
|
|
|
*/
|
2017-03-26 19:44:55 +02:00
|
|
|
class SettingManager implements CallbackListener, UsageInformationAble {
|
|
|
|
use UsageInformationTrait;
|
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2013-11-10 12:51:41 +01:00
|
|
|
* Constants
|
|
|
|
*/
|
2022-04-10 22:11:28 +02:00
|
|
|
const TABLE_SETTINGS = 'mc_settings';
|
|
|
|
const CB_SETTING_CHANGED = 'SettingManager.SettingChanged';
|
|
|
|
|
|
|
|
const SETTING_ALLOW_UNLINK_SERVER = 'Allow to unlink settings with multiple servers';
|
2014-05-02 17:50:30 +02:00
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-07-25 16:28:47 +02:00
|
|
|
* Private properties
|
2013-11-10 12:51:41 +01:00
|
|
|
*/
|
2014-07-25 16:39:07 +02:00
|
|
|
/** @var ManiaControl $maniaControl */
|
2013-11-10 12:51:41 +01:00
|
|
|
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();
|
2013-11-10 12:51:41 +01:00
|
|
|
|
|
|
|
/**
|
2014-07-25 16:28:47 +02:00
|
|
|
* 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
|
2013-11-10 12:51:41 +01:00
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
$this->initTables();
|
2014-02-12 21:12:51 +01:00
|
|
|
|
2014-07-25 16:28:47 +02:00
|
|
|
// Callbacks
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'handleAfterInit');
|
2022-04-10 22:11:28 +02:00
|
|
|
|
|
|
|
$this->initSetting($this, self::SETTING_ALLOW_UNLINK_SERVER, false);
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-25 16:28:47 +02:00
|
|
|
* Initialize the necessary database tables
|
2014-05-02 17:50:30 +02:00
|
|
|
*
|
2013-11-10 12:51:41 +01: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 . "` (
|
2014-05-14 00:28:53 +02:00
|
|
|
`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,
|
2014-05-14 00:28:53 +02:00
|
|
|
`default` VARCHAR(100) NOT NULL,
|
|
|
|
`set` VARCHAR(100) NOT NULL,
|
2013-11-10 12:51:41 +01:00
|
|
|
`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) {
|
2013-11-10 12:51:41 +01:00
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
}
|
2020-05-19 22:07:18 +02:00
|
|
|
|
|
|
|
$mysqli->query("ALTER TABLE `" . self::TABLE_SETTINGS . "` ADD `description` VARCHAR(500) DEFAULT NULL;");
|
|
|
|
if ($mysqli->error) {
|
|
|
|
// If not Duplicate
|
|
|
|
if ($mysqli->errno !== 1060) {
|
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2020-08-02 10:32:27 +02:00
|
|
|
|
|
|
|
// Grow the size limit for plugins settings
|
2021-06-30 16:19:31 +02:00
|
|
|
$mysqli->query("ALTER TABLE `" . self::TABLE_SETTINGS . "` MODIFY `value` VARCHAR(1000);");
|
2020-08-02 10:32:27 +02:00
|
|
|
if ($mysqli->error) {
|
|
|
|
// If not Duplicate
|
|
|
|
if ($mysqli->errno !== 1060) {
|
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2022-03-17 21:19:31 +01:00
|
|
|
|
|
|
|
// Add priority value
|
|
|
|
$mysqli->query("ALTER TABLE `" . self::TABLE_SETTINGS . "` ADD `priority` INT(5) DEFAULT 100;");
|
|
|
|
if ($mysqli->error) {
|
|
|
|
// If not Duplicate
|
|
|
|
if ($mysqli->errno !== 1060) {
|
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2022-04-10 22:11:28 +02:00
|
|
|
|
|
|
|
// Add link status
|
|
|
|
$mysqli->query("ALTER TABLE `" . self::TABLE_SETTINGS . "` ADD COLUMN `linked` TINYINT(1) DEFAULT 1 AFTER `type`,
|
|
|
|
ADD COLUMN `serverIndex` INT(11) DEFAULT 0 AFTER `linked`,
|
|
|
|
DROP INDEX `settingId`,
|
|
|
|
ADD UNIQUE KEY `settingId` (`class`,`setting`,`serverIndex`);");
|
|
|
|
if ($mysqli->error) {
|
|
|
|
// If not Duplicate
|
|
|
|
if ($mysqli->errno !== 1060) {
|
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2014-06-22 18:26:01 +02:00
|
|
|
return $result;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
|
|
|
|
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();
|
2022-04-27 00:19:41 +02:00
|
|
|
$settingStatement = $mysqli->prepare("DELETE FROM `" . self::TABLE_SETTINGS . "`
|
|
|
|
WHERE ((`linked` = 1 AND `serverIndex` = ?) OR `linked` = 0) AND `changed` < NOW() - INTERVAL 1 HOUR;");
|
2014-05-02 17:50:30 +02:00
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error);
|
2022-04-27 00:19:41 +02:00
|
|
|
return null;
|
2014-05-02 17:50:30 +02:00
|
|
|
}
|
2022-04-27 00:19:41 +02:00
|
|
|
|
|
|
|
$serverInfo = $this->maniaControl->getServer();
|
|
|
|
if ($serverInfo === null) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
$serverIndex = $serverInfo->index;
|
|
|
|
}
|
|
|
|
$settingStatement->bind_param('i', $serverIndex);
|
|
|
|
if (!$settingStatement->execute()) {
|
|
|
|
trigger_error('Error executing MySQL query: ' . $settingStatement->error);
|
|
|
|
}
|
|
|
|
$result = $settingStatement->get_result();
|
2014-05-13 14:15:00 +02:00
|
|
|
if ($result) {
|
2014-05-18 23:35:39 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-18 23:35:39 +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
|
2014-05-18 23:35:39 +02:00
|
|
|
* @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
|
|
|
*/
|
2014-05-18 23:35:39 +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) {
|
2014-05-18 23:35:39 +02:00
|
|
|
$result->free();
|
|
|
|
return null;
|
2014-05-02 17:50:30 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 14:15:00 +02:00
|
|
|
/** @var Setting $setting */
|
2014-05-14 00:28:53 +02:00
|
|
|
$setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null));
|
2014-05-18 23:35:39 +02:00
|
|
|
$result->free();
|
|
|
|
|
|
|
|
$this->storeSetting($setting);
|
2013-11-10 15:11:50 +01:00
|
|
|
|
2014-05-13 14:15:00 +02:00
|
|
|
return $setting;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
|
|
|
|
2014-05-18 23:35:39 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
2020-05-19 22:07:18 +02:00
|
|
|
* @param mixed $object
|
|
|
|
* @param string $settingName
|
|
|
|
* @param mixed $value
|
|
|
|
* @param string|null $description
|
2014-05-18 23:35:39 +02:00
|
|
|
* @return bool
|
2013-11-10 13:23:35 +01:00
|
|
|
*/
|
2020-05-19 22:07:18 +02:00
|
|
|
public function setSetting($object, $settingName, $value, $description = null) {
|
2014-07-04 18:48:39 +02:00
|
|
|
//TODO nowhere used, everywhere saveSettings used, is it depreciated?
|
2014-05-13 22:54:26 +02:00
|
|
|
$setting = $this->getSettingObject($object, $settingName);
|
2014-05-13 14:15:00 +02:00
|
|
|
if ($setting) {
|
|
|
|
$setting->value = $value;
|
2014-05-18 23:35:39 +02:00
|
|
|
$saved = $this->saveSetting($setting);
|
|
|
|
if (!$saved) {
|
|
|
|
return false;
|
2014-05-13 14:15:00 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-19 22:07:18 +02:00
|
|
|
$saved = $this->initSetting($object, $settingName, $value, $description);
|
2014-05-18 23:35:39 +02:00
|
|
|
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
|
|
|
|
2014-05-18 23:35:39 +02:00
|
|
|
$this->storeSetting($setting);
|
2014-05-13 14:15:00 +02:00
|
|
|
|
2014-05-18 23:35:39 +02:00
|
|
|
return true;
|
2014-01-05 22:51:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Get Setting by Name for the given Object
|
2014-05-02 17:50:30 +02:00
|
|
|
*
|
|
|
|
* @param mixed $object
|
2014-02-12 21:07:54 +01:00
|
|
|
* @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
|
|
|
*/
|
2014-05-13 22:54:26 +02: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
|
2014-05-18 23:35:39 +02:00
|
|
|
$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();
|
2022-04-10 22:11:28 +02:00
|
|
|
$settingStatement = $mysqli->prepare("SELECT * FROM `" . self::TABLE_SETTINGS . "`
|
|
|
|
WHERE `class` = ?
|
|
|
|
AND `setting` = ?
|
|
|
|
AND (`serverIndex` = ? OR `serverIndex` = 0) ORDER BY `serverIndex` DESC ;");
|
2014-01-27 09:07:43 +01:00
|
|
|
if ($mysqli->error) {
|
2013-11-10 12:51:41 +01:00
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-10 22:11:28 +02:00
|
|
|
|
|
|
|
$serverInfo = $this->maniaControl->getServer();
|
|
|
|
if ($serverInfo == null) {
|
|
|
|
$serverIndex = "";
|
|
|
|
} else {
|
|
|
|
$serverIndex = $serverInfo->index;
|
|
|
|
}
|
|
|
|
$settingStatement->bind_param('ssi', $settingClass, $settingName, $serverIndex);
|
|
|
|
if (!$settingStatement->execute()) {
|
|
|
|
trigger_error('Error executing MySQL query: ' . $settingStatement->error);
|
|
|
|
}
|
|
|
|
$result = $settingStatement->get_result();
|
2014-05-13 14:15:00 +02:00
|
|
|
if ($result->num_rows <= 0) {
|
2014-05-18 23:35:39 +02:00
|
|
|
$result->free();
|
2014-05-13 14:15:00 +02:00
|
|
|
if ($default === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-05-18 23:35:39 +02:00
|
|
|
$saved = $this->initSetting($object, $settingName, $default);
|
|
|
|
if ($saved) {
|
|
|
|
return $this->getSettingObject($object, $settingName, $default);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
2014-05-02 17:50:30 +02:00
|
|
|
|
2014-05-13 14:15:00 +02:00
|
|
|
/** @var Setting $setting */
|
2014-05-14 00:28:53 +02:00
|
|
|
$setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null));
|
2014-05-13 14:15:00 +02:00
|
|
|
$result->free();
|
|
|
|
|
2014-05-18 23:35:39 +02:00
|
|
|
$this->storeSetting($setting);
|
2014-05-13 14:15:00 +02:00
|
|
|
|
2013-11-10 12:51:41 +01:00
|
|
|
return $setting;
|
|
|
|
}
|
2013-11-10 13:23:35 +01:00
|
|
|
|
2014-05-18 23:35:39 +02: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
|
|
|
*
|
2020-05-19 22:07:18 +02:00
|
|
|
* @param mixed $object
|
|
|
|
* @param string $settingName
|
|
|
|
* @param mixed $defaultValue
|
|
|
|
* @param string|null $description
|
2014-05-18 23:35:39 +02:00
|
|
|
* @return bool
|
2013-11-10 13:23:35 +01:00
|
|
|
*/
|
2022-03-17 21:19:31 +01:00
|
|
|
public function initSetting($object, $settingName, $defaultValue, $description = null, $priority = 100) {
|
|
|
|
$setting = new Setting($object, $settingName, $defaultValue, $description, $priority);
|
2014-05-18 23:35:39 +02:00
|
|
|
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
|
2014-05-18 23:35:39 +02:00
|
|
|
* @param bool $init
|
2014-05-13 14:15:00 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-05-18 23:35:39 +02:00
|
|
|
public function saveSetting(Setting $setting, $init = false) {
|
2014-08-13 11:05:52 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2014-05-18 23:35:39 +02:00
|
|
|
if ($init) {
|
2022-04-10 22:11:28 +02:00
|
|
|
$existingsetting = $this->getSettingObject($setting->class, $setting->setting);
|
|
|
|
if ($existingsetting !== null && !$existingsetting->linked){
|
|
|
|
$setting->linked = false;
|
|
|
|
}
|
|
|
|
|
2014-05-18 23:35:39 +02:00
|
|
|
// Init - Keep old value if the default didn't change
|
2022-04-10 22:11:28 +02:00
|
|
|
$valueUpdateString = '`value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)),
|
|
|
|
`serverIndex` = IF(`serverIndex` IS NULL, 0, `serverIndex`)';
|
2014-05-18 23:35:39 +02:00
|
|
|
} else {
|
|
|
|
// Set - Update value in any case
|
2022-04-10 22:11:28 +02:00
|
|
|
$valueUpdateString = '`value` = VALUES(`value`), `serverIndex` = VALUES(`serverIndex`)';
|
2014-05-18 23:35:39 +02:00
|
|
|
}
|
2014-05-13 14:15:00 +02:00
|
|
|
$settingQuery = "INSERT INTO `" . self::TABLE_SETTINGS . "` (
|
|
|
|
`class`,
|
|
|
|
`setting`,
|
|
|
|
`type`,
|
2020-05-19 22:07:18 +02:00
|
|
|
`description`,
|
2014-05-13 14:15:00 +02:00
|
|
|
`value`,
|
2022-04-10 22:11:28 +02:00
|
|
|
`serverIndex`,
|
|
|
|
`linked`,
|
2014-05-18 21:46:00 +02:00
|
|
|
`default`,
|
2022-03-17 21:19:31 +01:00
|
|
|
`set`,
|
|
|
|
`priority`
|
2014-05-13 14:15:00 +02:00
|
|
|
) VALUES (
|
2022-04-10 22:11:28 +02:00
|
|
|
?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
2014-05-13 14:15:00 +02:00
|
|
|
) ON DUPLICATE KEY UPDATE
|
|
|
|
`index` = LAST_INSERT_ID(`index`),
|
|
|
|
`type` = VALUES(`type`),
|
2014-05-18 23:35:39 +02:00
|
|
|
{$valueUpdateString},
|
2022-04-10 22:11:28 +02:00
|
|
|
`linked` = VALUES(`linked`),
|
2014-05-13 14:15:00 +02:00
|
|
|
`default` = VALUES(`default`),
|
2014-05-18 21:46:00 +02:00
|
|
|
`set` = VALUES(`set`),
|
2020-05-19 22:07:18 +02:00
|
|
|
`description` = VALUES(`description`),
|
2022-03-17 21:19:31 +01:00
|
|
|
`priority` = VALUES(`priority`),
|
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();
|
2022-04-10 22:11:28 +02:00
|
|
|
$serverInfo = $this->maniaControl->getServer();
|
|
|
|
|
|
|
|
if ($setting->linked || $serverInfo == null || $serverInfo->index == null) {
|
|
|
|
$serverIndex = 0;
|
|
|
|
} else {
|
|
|
|
$serverIndex = $serverInfo->index;
|
|
|
|
}
|
2020-05-19 22:07:18 +02:00
|
|
|
$settingStatement->bind_param(
|
2022-04-10 22:11:28 +02:00
|
|
|
'sssssiissi',
|
2020-05-19 22:07:18 +02:00
|
|
|
$setting->class,
|
|
|
|
$setting->setting,
|
|
|
|
$setting->type,
|
|
|
|
$setting->description,
|
|
|
|
$formattedValue,
|
2022-04-10 22:11:28 +02:00
|
|
|
$serverIndex,
|
|
|
|
$setting->linked,
|
2020-05-19 22:07:18 +02:00
|
|
|
$formattedDefault,
|
2022-03-17 21:19:31 +01:00
|
|
|
$formattedSet,
|
|
|
|
$setting->priority
|
2020-05-19 22:07:18 +02:00
|
|
|
);
|
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);
|
2013-11-12 16:52:23 +01:00
|
|
|
$settingStatement->close();
|
2013-11-10 13:23:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$settingStatement->close();
|
2014-07-04 18:48:39 +02:00
|
|
|
|
|
|
|
// Trigger Settings Changed Callback
|
2014-07-25 16:28:47 +02:00
|
|
|
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
|
|
|
|
2014-05-13 22:54:26 +02: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) {
|
2014-05-13 22:54:26 +02:00
|
|
|
$setting = $this->getSettingObject($object, $settingName, $default);
|
2014-05-13 16:03:26 +02:00
|
|
|
if ($setting) {
|
|
|
|
return $setting->value;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-10 22:11:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* setSettingUnlinked
|
|
|
|
*
|
|
|
|
* @param mixed $object
|
|
|
|
* @param string $settingName
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setSettingUnlinked($object, $settingName = "") {
|
|
|
|
if ($object instanceof Setting) {
|
|
|
|
$settingClass = $object->class;
|
|
|
|
$settingName = $object->setting;
|
|
|
|
} else {
|
|
|
|
$settingClass = ClassUtil::getClass($object);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch setting
|
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
|
|
|
$settingStatement = $mysqli->prepare("UPDATE `" . self::TABLE_SETTINGS . "`
|
|
|
|
SET `linked` = 0, `changed` = NOW()
|
|
|
|
WHERE `class` = ?
|
|
|
|
AND `setting` = ?");
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$settingStatement->bind_param('ss', $settingClass, $settingName);
|
|
|
|
if (!$settingStatement->execute()) {
|
|
|
|
trigger_error('Error executing MySQL query: ' . $settingStatement->error);
|
|
|
|
}
|
|
|
|
$result = $settingStatement->get_result();
|
|
|
|
|
|
|
|
if (isset($this->storedSettings[$settingClass . $settingName])) {
|
|
|
|
unset($this->storedSettings[$settingClass . $settingName]);
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2014-05-13 16:03:26 +02:00
|
|
|
|
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
|
2013-12-09 13:45:58 +01:00
|
|
|
* @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) {
|
2022-04-10 22:11:28 +02:00
|
|
|
$settingClass = $object->class;
|
2014-05-13 14:15:00 +02:00
|
|
|
$settingName = $object->setting;
|
|
|
|
} else {
|
2022-04-10 22:11:28 +02:00
|
|
|
$settingClass = ClassUtil::getClass($object);
|
2014-05-13 14:15:00 +02:00
|
|
|
}
|
2014-08-13 11:05:52 +02:00
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
2022-04-10 22:11:28 +02:00
|
|
|
$settingStatement = $mysqli->prepare("UPDATE `" . self::TABLE_SETTINGS . "`
|
2013-11-10 15:11:50 +01:00
|
|
|
SET `value` = `default`
|
2022-04-10 22:11:28 +02:00
|
|
|
WHERE `class` = ?
|
|
|
|
AND `setting` = ?
|
|
|
|
AND (`serverIndex` = ? OR `serverIndex` = 0) ORDER BY `serverIndex` DESC LIMIT 1;"); // TODO : by server if linked or not
|
2014-01-27 09:07:43 +01:00
|
|
|
if ($mysqli->error) {
|
2013-11-10 15:11:50 +01:00
|
|
|
trigger_error($mysqli->error);
|
2022-04-10 22:11:28 +02:00
|
|
|
return null;
|
2013-11-10 15:11:50 +01:00
|
|
|
}
|
2022-04-10 22:11:28 +02:00
|
|
|
|
|
|
|
$serverInfo = $this->maniaControl->getServer();
|
|
|
|
if ($serverInfo == null) {
|
|
|
|
$serverIndex = 0;
|
|
|
|
} else {
|
|
|
|
$serverIndex = $serverInfo->index;
|
|
|
|
}
|
|
|
|
$settingStatement->bind_param('ssi', $settingClass, $settingName, $serverIndex);
|
|
|
|
if (!$settingStatement->execute()) {
|
|
|
|
trigger_error('Error executing MySQL query: ' . $settingStatement->error);
|
|
|
|
}
|
|
|
|
$result = $settingStatement->get_result();
|
|
|
|
|
|
|
|
if (isset($this->storedSettings[$settingClass . $settingName])) {
|
|
|
|
unset($this->storedSettings[$settingClass . $settingName]);
|
2014-04-30 02:27:17 +02:00
|
|
|
}
|
2022-04-10 22:11:28 +02:00
|
|
|
|
2014-05-13 14:15:00 +02:00
|
|
|
return $result;
|
2013-12-09 10:19:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Delete a Setting
|
2014-05-02 17:50:30 +02:00
|
|
|
*
|
|
|
|
* @param mixed $object
|
2013-12-09 13:45:58 +01:00
|
|
|
* @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
|
|
|
|
2014-04-30 02:27:17 +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
|
|
|
}
|
2013-11-28 04:48:04 +01:00
|
|
|
|
2022-04-10 22:11:28 +02:00
|
|
|
/**
|
|
|
|
* Delete unlinked settings
|
|
|
|
*
|
|
|
|
* @param mixed $object
|
|
|
|
* @param string $settingName
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function deleteSettingUnlinked($object, $settingName = null) {
|
|
|
|
if ($object instanceof Setting) {
|
|
|
|
$settingClass = $object->class;
|
|
|
|
$settingName = $object->setting;
|
|
|
|
} else {
|
|
|
|
$settingClass = ClassUtil::getClass($object);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch setting
|
|
|
|
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
|
|
|
$settingStatement = $mysqli->prepare("DELETE FROM `" . self::TABLE_SETTINGS . "`
|
|
|
|
WHERE `class` = ?
|
|
|
|
AND `setting` = ?
|
|
|
|
AND `serverIndex` != 0");
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$settingStatement->bind_param('ss', $settingClass, $settingName);
|
|
|
|
if (!$settingStatement->execute()) {
|
|
|
|
trigger_error('Error executing MySQL query: ' . $settingStatement->error);
|
|
|
|
}
|
|
|
|
$result = $settingStatement->get_result();
|
|
|
|
|
|
|
|
if (isset($this->storedSettings[$settingClass . $settingName])) {
|
|
|
|
unset($this->storedSettings[$settingClass . $settingName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2014-01-18 20:05:50 +01:00
|
|
|
/**
|
2014-02-12 21:07:54 +01:00
|
|
|
* Get all Settings for the given Class
|
2014-05-02 17:50:30 +02:00
|
|
|
*
|
2014-02-12 21:07:54 +01:00
|
|
|
* @param mixed $object
|
2014-05-27 23:00:39 +02:00
|
|
|
* @return Setting[]
|
2014-01-18 20:05:50 +01:00
|
|
|
*/
|
2014-02-12 21:07:54 +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();
|
2022-04-10 22:11:28 +02:00
|
|
|
// LIMIT is required to keep unlinked setting
|
|
|
|
$settingStatement = $mysqli->prepare("SELECT * FROM (SELECT * FROM `" . self::TABLE_SETTINGS . "`
|
|
|
|
WHERE class = ? AND (`serverIndex` = ? OR `serverIndex` = 0)
|
2022-04-13 10:46:42 +02:00
|
|
|
ORDER BY `serverIndex` DESC
|
2022-04-10 22:11:28 +02:00
|
|
|
LIMIT 9999999)
|
2022-04-13 10:46:42 +02:00
|
|
|
as t GROUP BY `setting` ORDER BY `priority` ASC, `setting`;");
|
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;
|
|
|
|
}
|
2022-04-10 22:11:28 +02:00
|
|
|
$serverInfo = $this->maniaControl->getServer();
|
|
|
|
if ($serverInfo == null) {
|
|
|
|
$serverIndex = 0;
|
|
|
|
} else {
|
|
|
|
$serverIndex = $serverInfo->index;
|
|
|
|
}
|
|
|
|
$settingStatement->bind_param('si', $className, $serverIndex);
|
|
|
|
if (!$settingStatement->execute()) {
|
|
|
|
trigger_error('Error executing MySQL query: ' . $settingStatement->error);
|
|
|
|
}
|
|
|
|
$result = $settingStatement->get_result();
|
2014-01-18 20:05:50 +01:00
|
|
|
$settings = array();
|
2014-05-14 00:28:53 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-11-28 04:48:04 +01:00
|
|
|
/**
|
2014-05-13 14:15:00 +02:00
|
|
|
* Get all Settings
|
2022-04-10 22:11:28 +02:00
|
|
|
* CAREFUL: could have multiple time the same setting if in unlinked mode
|
2014-05-02 17:50:30 +02:00
|
|
|
*
|
2014-05-27 23:00:39 +02:00
|
|
|
* @return Setting[]
|
2013-11-28 04:48:04 +01:00
|
|
|
*/
|
2022-04-10 22:11:28 +02:00
|
|
|
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 . "`
|
2022-03-17 21:19:31 +01:00
|
|
|
ORDER BY `class` ASC, `priority` ASC, `setting` ASC;";
|
2013-11-28 04:48:04 +01:00
|
|
|
$result = $mysqli->query($query);
|
2014-01-27 09:07:43 +01:00
|
|
|
if ($mysqli->error) {
|
2013-11-28 04:48:04 +01:00
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$settings = array();
|
2014-05-14 00:28:53 +02:00
|
|
|
while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null))) {
|
2014-01-05 21:48:49 +01:00
|
|
|
$settings[$setting->index] = $setting;
|
2013-11-28 04:48:04 +01:00
|
|
|
}
|
|
|
|
$result->free();
|
|
|
|
return $settings;
|
|
|
|
}
|
2014-02-12 21:12:51 +01:00
|
|
|
|
2014-04-30 03:45:54 +02:00
|
|
|
/**
|
|
|
|
* Get all Setting Classes
|
2014-05-02 16:13:45 +02:00
|
|
|
*
|
|
|
|
* @param bool $hidePluginClasses
|
2014-05-27 23:00:39 +02:00
|
|
|
* @return string[]
|
2014-04-30 03:45:54 +02:00
|
|
|
*/
|
2014-04-30 05:27:23 +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;
|
|
|
|
}
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|