misuzu/public/manage/general/setting.php

119 lines
3.2 KiB
PHP
Raw Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
use Misuzu\Config;
2023-01-01 20:23:53 +00:00
use Misuzu\Config\CfgTools;
use Misuzu\Config\CfgType;
2022-09-13 13:14:49 +00:00
use Misuzu\Users\User;
require_once '../../../misuzu.php';
if(!User::hasCurrent()
|| !perms_check_user(MSZ_PERMS_GENERAL, User::getCurrent()->getId(), MSZ_PERM_GENERAL_MANAGE_CONFIG)) {
echo render_error(403);
return;
}
$sVar = [
'name' => '',
'type' => '',
'value' => null,
'new' => true,
];
$sName = (string)filter_input(INPUT_GET, 'name');
if(!empty($sName)) {
2023-01-01 20:23:53 +00:00
if(!CfgTools::validateName($sName))
2022-09-13 13:14:49 +00:00
throw new \Exception("Config key name has invalid format.");
$sVar['name'] = $sName;
}
$sType = (string)filter_input(INPUT_GET, 'type');
if(!empty($sType)) {
2023-01-01 20:23:53 +00:00
if(!CfgTools::isValidType($sType))
2022-09-13 13:14:49 +00:00
throw new \Exception("Specified type is invalid.");
$sVar['type'] = $sType;
2023-01-01 20:23:53 +00:00
$sVar['value'] = CfgTools::default($sType);
2022-09-13 13:14:49 +00:00
}
if($_SERVER['REQUEST_METHOD'] === 'POST') {
if(!CSRF::validateRequest())
throw new \Exception("Request verification failed.");
if(empty($sName)) {
$sName = (string)filter_input(INPUT_POST, 'conf_name');
2023-01-01 20:23:53 +00:00
if(empty($sName) || !CfgTools::validateName($sName))
2022-09-13 13:14:49 +00:00
throw new \Exception("Config key name has invalid format.");
$sVar['name'] = $sName;
}
$sLogAction = AuditLog::CONFIG_CREATE;
2023-01-01 20:23:53 +00:00
if($cfg->hasValue($sName)) {
$sType = CfgTools::type($cfg->getValue($sName));
2022-09-13 13:14:49 +00:00
$sVar['new'] = false;
$sLogAction = AuditLog::CONFIG_UPDATE;
} elseif(empty($sType)) {
$sType = (string)filter_input(INPUT_POST, 'conf_type');
2023-01-01 20:23:53 +00:00
if(empty($sType) || !CfgTools::isValidType($sType))
2022-09-13 13:14:49 +00:00
throw new \Exception("Specified type is invalid.");
}
$sVar['type'] = $sType;
2023-01-01 20:23:53 +00:00
$sValue = CfgTools::default($sType);
2022-09-13 13:14:49 +00:00
if($sType === 'array') {
if(!empty($_POST['conf_value']) && is_array($_POST['conf_value'])) {
foreach($_POST['conf_value'] as $fv) {
2023-01-01 20:23:53 +00:00
$fv = (string)$fv;
2022-09-13 13:14:49 +00:00
if(str_starts_with($fv, 's:')) {
$fv = substr($fv, 2);
} elseif(str_starts_with($fv, 'i:')) {
$fv = (int)substr($fv, 2);
2022-09-13 13:14:49 +00:00
} elseif(str_starts_with($fv, 'b:')) {
$fv = strtolower(substr($fv, 2));
$fv = $fv !== 'false' && $fv !== '0' && $fv !== '';
}
$sValue[] = $fv;
}
}
} elseif($sType === 'boolean') {
$sValue = !empty($_POST['conf_value']);
} else {
$sValue = (string)filter_input(INPUT_POST, 'conf_value');
if($sType === 'integer')
$sValue = (int)$sValue;
2022-09-13 13:14:49 +00:00
}
$sVar['value'] = $sValue;
AuditLog::create($sLogAction, [$sName]);
2023-01-01 20:23:53 +00:00
$cfg->setValue($sName, $sValue);
2022-09-13 13:14:49 +00:00
url_redirect('manage-general-settings');
return;
}
2023-01-01 20:23:53 +00:00
if($cfg->hasValue($sName)) {
2022-09-13 13:14:49 +00:00
$sVar['new'] = false;
2023-01-01 20:23:53 +00:00
$sValue = $cfg->getValue($sName);
$sVar['type'] = $sType = CfgTools::type($sValue);
2022-09-13 13:14:49 +00:00
2023-01-01 20:23:53 +00:00
if($sType === CfgType::T_ARR)
2022-09-13 13:14:49 +00:00
foreach($sValue as $fk => $fv)
$sValue[$fk] = ['integer' => 'i', 'string' => 's', 'boolean' => 'b'][gettype($fv)] . ':' . $fv;
$sVar['value'] = $sValue;
}
Template::render('manage.general.setting', [
'conf_var' => $sVar,
]);