<?php
namespace Misuzu;

use Misuzu\AuditLog;
use Misuzu\Config;
use Misuzu\Config\CfgTools;
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;
}

$sName = (string)filter_input(INPUT_GET, 'name');
if(!CfgTools::validateName($sName) || !$cfg->hasValue($sName))
    throw new \Exception("Config value does not exist.");

if($_SERVER['REQUEST_METHOD'] === 'POST') {
    if(!CSRF::validateRequest())
        throw new \Exception("Request verification failed.");

    AuditLog::create($_SERVER['REMOTE_ADDR'], AuditLog::CONFIG_DELETE, [$sName]);
    $cfg->removeValue($sName);
    url_redirect('manage-general-settings');
} else {
    $sValue = $cfg->getValue($sName);

    Template::render('manage.general.setting-delete', [
        'conf_var' => [
            'name' => $sName,
            'type' => CfgTools::type($sValue),
            'value' => $sValue,
        ],
    ]);
}
