misuzu/public-legacy/manage/general/setting.php

93 lines
3 KiB
PHP
Raw Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
2024-10-05 02:40:29 +00:00
use Index\Config\Db\DbConfig;
2022-09-13 13:14:49 +00:00
2024-12-02 02:28:08 +00:00
if(!isset($msz) || !($msz instanceof \Misuzu\MisuzuContext))
die('Script must be called through the Misuzu route dispatcher.');
if(!$msz->authInfo->getPerms('global')->check(Perm::G_CONFIG_MANAGE))
Template::throwError(403);
2022-09-13 13:14:49 +00:00
2023-07-18 21:48:44 +00:00
$isNew = true;
2022-09-13 13:14:49 +00:00
$sName = (string)filter_input(INPUT_GET, 'name');
$sType = (string)filter_input(INPUT_GET, 'type');
2023-07-18 21:48:44 +00:00
$sValue = null;
2024-12-02 02:28:08 +00:00
$loadValueInfo = fn() => $msz->config->getValueInfo($sName);
2022-09-13 13:14:49 +00:00
2023-07-18 21:48:44 +00:00
if(!empty($sName)) {
$sInfo = $loadValueInfo();
if($sInfo !== null) {
$isNew = false;
$sName = $sInfo->getName();
$sType = $sInfo->getType();
$sValue = $sInfo->getValue();
2022-09-13 13:14:49 +00:00
}
2023-07-18 21:48:44 +00:00
}
2022-09-13 13:14:49 +00:00
2023-07-18 21:48:44 +00:00
while($_SERVER['REQUEST_METHOD'] === 'POST' && CSRF::validateRequest()) {
if($isNew) {
$sName = trim((string)filter_input(INPUT_POST, 'conf_name'));
if(!DbConfig::validateName($sName)) {
echo 'Name contains invalid characters.';
break;
}
2022-09-13 13:14:49 +00:00
2023-07-18 21:48:44 +00:00
$sType = trim((string)filter_input(INPUT_POST, 'conf_type'));
if(!in_array($sType, ['string', 'int', 'float', 'bool', 'array'])) {
echo 'Invalid type specified.';
break;
}
2022-09-13 13:14:49 +00:00
}
if($sType === 'array') {
2024-12-02 02:28:08 +00:00
$applyFunc = $msz->config->setArray(...);
2023-07-18 21:48:44 +00:00
$sValue = [];
$sRaw = filter_input(INPUT_POST, 'conf_value', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
foreach($sRaw as $rValue) {
if(strpos($rValue, ':') === 1) {
$rType = $rValue[0];
$rValue = substr($rValue, 2);
$sValue[] = match($rType) {
's' => $rValue,
'i' => (int)$rValue,
'd' => (float)$rValue,
'f' => (float)$rValue,
'b' => $rValue !== 'false' && $rValue !== '0' && $rValue !== '',
default => '',
};
} else
$sValue[] = $rValue;
2022-09-13 13:14:49 +00:00
}
2023-07-18 21:48:44 +00:00
} elseif($sType === 'bool') {
2022-09-13 13:14:49 +00:00
$sValue = !empty($_POST['conf_value']);
2024-12-02 02:28:08 +00:00
$applyFunc = $msz->config->setBoolean(...);
2022-09-13 13:14:49 +00:00
} else {
2023-07-18 21:48:44 +00:00
$sValue = filter_input(INPUT_POST, 'conf_value');
if($sType === 'int') {
2024-12-02 02:28:08 +00:00
$applyFunc = $msz->config->setInteger(...);
$sValue = (int)$sValue;
2023-07-18 21:48:44 +00:00
} elseif($sType === 'float') {
2024-12-02 02:28:08 +00:00
$applyFunc = $msz->config->setFloat(...);
2023-07-18 21:48:44 +00:00
$sValue = (float)$sValue;
} else
2024-12-02 02:28:08 +00:00
$applyFunc = $msz->config->setString(...);
2022-09-13 13:14:49 +00:00
}
2023-07-18 21:48:44 +00:00
$msz->createAuditLog($isNew ? 'CONFIG_CREATE' : 'CONFIG_UPDATE', [$sName]);
$applyFunc($sName, $sValue);
Tools::redirect($msz->urls->format('manage-general-settings'));
2022-09-13 13:14:49 +00:00
return;
}
2023-07-18 21:48:44 +00:00
if($sType === 'array' && !empty($sValue))
foreach($sValue as $key => $value)
$sValue[$key] = gettype($value)[0] . ':' . $value;
2022-09-13 13:14:49 +00:00
Template::render('manage.general.setting', [
2023-07-18 21:48:44 +00:00
'config_new' => $isNew,
'config_name' => $sName,
'config_type' => $sType,
'config_value' => $sValue,
2022-09-13 13:14:49 +00:00
]);