2018-10-04 21:01:17 +02:00
|
|
|
<?php
|
2019-08-14 21:40:36 +02:00
|
|
|
define('MSZ_CFG_ANY', '');
|
|
|
|
define('MSZ_CFG_STR', 'string');
|
|
|
|
define('MSZ_CFG_INT', 'integer');
|
|
|
|
define('MSZ_CFG_BOOL', 'boolean');
|
|
|
|
define('MSZ_CFG_ARR', 'array');
|
|
|
|
|
|
|
|
define('MSZ_CFG_DEFAULTS', [
|
|
|
|
MSZ_CFG_ANY => null,
|
|
|
|
MSZ_CFG_STR => '',
|
|
|
|
MSZ_CFG_INT => 0,
|
|
|
|
MSZ_CFG_BOOL => false,
|
|
|
|
MSZ_CFG_ARR => [],
|
|
|
|
]);
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
function config_store(?array $append = null): array {
|
2019-05-07 10:08:27 +02:00
|
|
|
static $store = [];
|
|
|
|
|
|
|
|
if(!is_null($append)) {
|
2019-08-14 21:40:36 +02:00
|
|
|
$store = array_merge($store, $append);
|
2019-05-07 10:08:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $store;
|
|
|
|
}
|
2018-10-04 21:01:17 +02:00
|
|
|
|
2019-08-14 21:40:36 +02:00
|
|
|
function config_init(): void {
|
2019-08-28 14:46:12 +02:00
|
|
|
try {
|
2019-09-29 00:38:39 +02:00
|
|
|
$dbconfig = \Misuzu\DB::prepare('SELECT * FROM `msz_config`')->fetchAll();
|
2019-08-28 14:46:12 +02:00
|
|
|
} catch (PDOException $ex) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-14 21:40:36 +02:00
|
|
|
$config = [];
|
|
|
|
|
|
|
|
foreach($dbconfig as $record)
|
|
|
|
$config[$record['config_name']] = unserialize($record['config_value']);
|
2018-10-04 21:01:17 +02:00
|
|
|
|
2019-05-07 10:08:27 +02:00
|
|
|
config_store($config);
|
2018-10-04 21:01:17 +02:00
|
|
|
}
|
|
|
|
|
2019-08-14 21:40:36 +02:00
|
|
|
function config_get(string $key, string $type = MSZ_CFG_ANY, $default = null) {
|
|
|
|
$value = config_store()[$key] ?? null;
|
2018-10-04 21:01:17 +02:00
|
|
|
|
2019-08-14 21:40:36 +02:00
|
|
|
if($type !== MSZ_CFG_ANY && gettype($value) !== $type)
|
|
|
|
$value = null;
|
2018-10-04 22:30:55 +02:00
|
|
|
|
2019-08-14 21:40:36 +02:00
|
|
|
return $value ?? $default ?? MSZ_CFG_DEFAULTS[$type];
|
2018-10-04 22:30:55 +02:00
|
|
|
}
|
|
|
|
|
2019-08-14 21:40:36 +02:00
|
|
|
function config_set(string $key, $value, bool $soft = false): void {
|
|
|
|
config_store([$key => $value]);
|
|
|
|
|
|
|
|
if($soft)
|
|
|
|
return;
|
|
|
|
|
|
|
|
$value = serialize($value);
|
2019-09-29 00:38:39 +02:00
|
|
|
$saveVal = \Misuzu\DB::prepare('
|
2019-08-14 21:40:36 +02:00
|
|
|
INSERT INTO `msz_config`
|
|
|
|
(`config_name`, `config_value`)
|
|
|
|
VALUES
|
|
|
|
(:name, :value_1)
|
|
|
|
ON DUPLICATE KEY UPDATE
|
|
|
|
`config_value` = :value_2
|
|
|
|
');
|
2019-09-29 00:38:39 +02:00
|
|
|
$saveVal->bind('name', $key);
|
|
|
|
$saveVal->bind('value_1', $value);
|
|
|
|
$saveVal->bind('value_2', $value);
|
2019-08-14 21:40:36 +02:00
|
|
|
$saveVal->execute();
|
2018-10-04 21:01:17 +02:00
|
|
|
}
|