92 lines
3 KiB
PHP
92 lines
3 KiB
PHP
<?php
|
|
namespace Misuzu;
|
|
|
|
use Index\Config\Db\DbConfig;
|
|
|
|
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);
|
|
|
|
$isNew = true;
|
|
$sName = (string)filter_input(INPUT_GET, 'name');
|
|
$sType = (string)filter_input(INPUT_GET, 'type');
|
|
$sValue = null;
|
|
$loadValueInfo = fn() => $msz->config->getValueInfo($sName);
|
|
|
|
if(!empty($sName)) {
|
|
$sInfo = $loadValueInfo();
|
|
if($sInfo !== null) {
|
|
$isNew = false;
|
|
$sName = $sInfo->getName();
|
|
$sType = $sInfo->getType();
|
|
$sValue = $sInfo->getValue();
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
$sType = trim((string)filter_input(INPUT_POST, 'conf_type'));
|
|
if(!in_array($sType, ['string', 'int', 'float', 'bool', 'array'])) {
|
|
echo 'Invalid type specified.';
|
|
break;
|
|
}
|
|
}
|
|
|
|
if($sType === 'array') {
|
|
$applyFunc = $msz->config->setArray(...);
|
|
$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;
|
|
}
|
|
} elseif($sType === 'bool') {
|
|
$sValue = !empty($_POST['conf_value']);
|
|
$applyFunc = $msz->config->setBoolean(...);
|
|
} else {
|
|
$sValue = filter_input(INPUT_POST, 'conf_value');
|
|
if($sType === 'int') {
|
|
$applyFunc = $msz->config->setInteger(...);
|
|
$sValue = (int)$sValue;
|
|
} elseif($sType === 'float') {
|
|
$applyFunc = $msz->config->setFloat(...);
|
|
$sValue = (float)$sValue;
|
|
} else
|
|
$applyFunc = $msz->config->setString(...);
|
|
}
|
|
|
|
$msz->createAuditLog($isNew ? 'CONFIG_CREATE' : 'CONFIG_UPDATE', [$sName]);
|
|
$applyFunc($sName, $sValue);
|
|
Tools::redirect($msz->urls->format('manage-general-settings'));
|
|
return;
|
|
}
|
|
|
|
if($sType === 'array' && !empty($sValue))
|
|
foreach($sValue as $key => $value)
|
|
$sValue[$key] = gettype($value)[0] . ':' . $value;
|
|
|
|
Template::render('manage.general.setting', [
|
|
'config_new' => $isNew,
|
|
'config_name' => $sName,
|
|
'config_type' => $sType,
|
|
'config_value' => $sValue,
|
|
]);
|