2013-11-10 12:51:41 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class managing settings and configurations
|
|
|
|
*
|
|
|
|
* @author steeffeen & kremsy
|
|
|
|
*/
|
|
|
|
class SettingManager {
|
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
const TABLE_SETTINGS = 'mc_settings';
|
2013-11-10 13:23:35 +01:00
|
|
|
const TYPE_STRING = 'string';
|
|
|
|
const TYPE_INT = 'int';
|
|
|
|
const TYPE_REAL = 'real';
|
|
|
|
const TYPE_BOOL = 'bool';
|
|
|
|
const TYPE_ARRAY = 'array';
|
2013-11-10 12:51:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Private properties
|
|
|
|
*/
|
|
|
|
private $maniaControl = null;
|
2013-11-10 13:23:35 +01:00
|
|
|
private $arrayDelimiter = ';;';
|
2013-11-10 12:51:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct setting manager
|
|
|
|
*
|
|
|
|
* @param ManiaControl $maniaControl
|
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
|
|
|
|
$this->initTables();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize necessary database tables
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function initTables() {
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2013-11-10 13:23:35 +01:00
|
|
|
$defaultType = "'" . self::TYPE_STRING . "'";
|
|
|
|
$typeSet = $defaultType;
|
|
|
|
$typeSet .= ",'" . self::TYPE_INT . "'";
|
|
|
|
$typeSet .= ",'" . self::TYPE_REAL . "'";
|
|
|
|
$typeSet .= ",'" . self::TYPE_BOOL . "'";
|
|
|
|
$typeSet .= ",'" . self::TYPE_ARRAY . "'";
|
2013-11-10 12:51:41 +01:00
|
|
|
$settingTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SETTINGS . "` (
|
|
|
|
`index` int(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
`class` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
|
|
|
|
`setting` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
|
2013-11-10 13:23:35 +01:00
|
|
|
`type` set({$typeSet}) COLLATE utf8_unicode_ci NOT NULL DEFAULT '{$defaultType}',
|
|
|
|
`value` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
|
|
|
|
`default` varchar(100) COLLATE utf8_unicode_ci 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;";
|
|
|
|
$settingTableStatement = $mysqli->prepare($settingTableQuery);
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$settingTableStatement->execute();
|
|
|
|
if ($settingTableStatement->error) {
|
|
|
|
trigger_error($settingTableStatement->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$settingTableStatement->close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get type for given parameter
|
|
|
|
*
|
|
|
|
* @param mixed $param
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getType($param) {
|
|
|
|
if (is_int($param)) {
|
2013-11-10 13:23:35 +01:00
|
|
|
return self::TYPE_INT;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
|
|
|
if (is_real($param)) {
|
2013-11-10 13:23:35 +01:00
|
|
|
return self::TYPE_REAL;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
|
|
|
if (is_bool($param)) {
|
2013-11-10 13:23:35 +01:00
|
|
|
return self::TYPE_BOOL;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
|
|
|
if (is_string($param)) {
|
2013-11-10 13:23:35 +01:00
|
|
|
return self::TYPE_STRING;
|
|
|
|
}
|
|
|
|
if (is_array($param)) {
|
|
|
|
return self::TYPE_ARRAY;
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
|
|
|
trigger_error('Unsupported setting type. ' . print_r($param, true));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cast a setting to the given type
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param mixed $value
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
private function castSetting($type, $value) {
|
2013-11-10 13:23:35 +01:00
|
|
|
if ($type === self::TYPE_INT) {
|
2013-11-10 12:51:41 +01:00
|
|
|
return (int) $value;
|
|
|
|
}
|
2013-11-10 13:23:35 +01:00
|
|
|
if ($type === self::TYPE_REAL) {
|
2013-11-10 12:51:41 +01:00
|
|
|
return (real) $value;
|
|
|
|
}
|
2013-11-10 13:23:35 +01:00
|
|
|
if ($type === self::TYPE_BOOL) {
|
2013-11-10 12:51:41 +01:00
|
|
|
return (bool) $value;
|
|
|
|
}
|
2013-11-10 13:23:35 +01:00
|
|
|
if ($type === self::TYPE_STRING) {
|
2013-11-10 12:51:41 +01:00
|
|
|
return (string) $value;
|
|
|
|
}
|
2013-11-10 13:23:35 +01:00
|
|
|
if ($type === self::TYPE_ARRAY) {
|
|
|
|
return explode($this->arrayDelimiter, $value);
|
|
|
|
}
|
2013-11-10 12:51:41 +01:00
|
|
|
trigger_error('Unsupported setting type. ' . print_r($param, true));
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2013-11-10 13:23:35 +01:00
|
|
|
/**
|
|
|
|
* Format a setting for saving it to the database
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
* @param string $type
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
private function formatSetting($value, $type = null) {
|
|
|
|
if ($type === null) {
|
|
|
|
$type = $this->getType($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type === self::TYPE_ARRAY) {
|
|
|
|
return implode($this->arrayDelimiter, $value);
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2013-11-10 12:51:41 +01:00
|
|
|
/**
|
|
|
|
* Initialize a setting for given object
|
|
|
|
*
|
|
|
|
* @param object $object
|
|
|
|
* @param string $settingName
|
|
|
|
* @param mixed $default
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function initSetting($object, $settingName, $default) {
|
|
|
|
if ($default === null || is_object($default)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$className = $object;
|
|
|
|
if (is_object($object)) {
|
|
|
|
$className = get_class($object);
|
|
|
|
}
|
|
|
|
$type = $this->getType($default);
|
2013-11-10 13:23:35 +01:00
|
|
|
$default = $this->formatSetting($default, $type);
|
2013-11-10 12:51:41 +01:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
$settingQuery = "INSERT INTO `" . self::TABLE_SETTINGS . "` (
|
|
|
|
`class`,
|
|
|
|
`setting`,
|
|
|
|
`type`,
|
|
|
|
`value`,
|
|
|
|
`default`
|
|
|
|
) VALUES (
|
|
|
|
?, ?, ?,
|
|
|
|
@value := ?,
|
|
|
|
@value
|
|
|
|
);";
|
|
|
|
$settingStatement = $mysqli->prepare($settingQuery);
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$settingStatement->bind_param('ssss', $className, $settingName, $type, $default);
|
|
|
|
$settingStatement->execute();
|
|
|
|
if ($settingStatement->error) {
|
|
|
|
trigger_error($settingStatement->error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$settingStatement->close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get setting by name for given object
|
|
|
|
*
|
|
|
|
* @param object $object
|
|
|
|
* @param string $settingName
|
|
|
|
* @param mixed $default
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getSetting($object, $settingName, $default = null) {
|
|
|
|
$className = $object;
|
|
|
|
if (is_object($object)) {
|
|
|
|
$className = get_class($object);
|
|
|
|
}
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
$settingQuery = "SELECT `type`, `value` FROM `" . self::TABLE_SETTINGS . "`
|
|
|
|
WHERE `class` = ?
|
|
|
|
AND `setting` = ?;";
|
|
|
|
$settingStatement = $mysqli->prepare($settingQuery);
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$settingStatement->bind_param('ss', $className, $settingName);
|
|
|
|
$settingStatement->execute();
|
|
|
|
if ($settingStatement->error) {
|
|
|
|
trigger_error($settingStatement->error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$settingStatement->store_result();
|
|
|
|
if ($settingStatement->num_rows <= 0) {
|
|
|
|
$this->initSetting($object, $settingName, $default);
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
$settingStatement->bind_result($type, $value);
|
|
|
|
$settingStatement->fetch();
|
|
|
|
$settingStatement->free_result();
|
|
|
|
$settingStatement->close();
|
|
|
|
$setting = $this->castSetting($type, $value);
|
|
|
|
return $setting;
|
|
|
|
}
|
2013-11-10 13:23:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a setting for the given object
|
|
|
|
*
|
|
|
|
* @param object $object
|
|
|
|
* @param string $settingName
|
|
|
|
* @param mixed $value
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function setSetting($object, $settingName, $value) {
|
|
|
|
$className = $object;
|
|
|
|
if (is_object($object)) {
|
|
|
|
$className = get_class($object);
|
|
|
|
}
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
|
|
|
|
SET `value` = ?
|
|
|
|
WHERE `class` = ?
|
|
|
|
AND `setting` = ?;";
|
|
|
|
$settingStatement = $mysqli->prepare($settingQuery);
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$value = $this->formatSetting($value);
|
|
|
|
$settingStatement->bind_param('sss', $value, $className, $settingName);
|
|
|
|
$settingStatement->execute();
|
|
|
|
if ($settingStatement->error) {
|
|
|
|
trigger_error($settingStatement->error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$settingStatement->close();
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-10 12:51:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|