BastionGuard/webui/config.php
specialworld83 f0f913a209 Release 2.0
2026-07-15 10:52:22 +02:00

100 lines
6 KiB
PHP

<?php
include 'auth.php';
bg_require_admin();
require_once __DIR__ . '/includes/config_manager.php';
$page_title = 'Configurations - BastionGuard WebUI';
$active_page = 'config.php';
$catalog = bg_config_catalog();
$scan = bg_config_scan_dir();
$selected = $_GET['key'] ?? 'app_config';
if (!isset($catalog[$selected])) $selected = array_key_first($catalog);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
csrf_validate();
$action = $_POST['action'] ?? '';
if ($action === 'ensure') {
$res = bg_config_ensure_all(bg_detect_desktop_user());
bg_flash(($res['code'] ?? 1) === 0 ? 'success' : 'warning', trim($res['output']) ?: 'Operation completed.');
bg_redirect('config.php');
} elseif ($action === 'save') {
$key = $_POST['key'] ?? '';
$content = (string)($_POST['content'] ?? '');
$res = bg_config_save_key($key, $content);
bg_flash(($res['code'] ?? 1) === 0 ? 'success' : 'danger', trim($res['output']) ?: 'Save completed.');
bg_redirect('config.php?key=' . rawurlencode($key));
} elseif ($action === 'reset_default') {
$key = $_POST['key'] ?? '';
if (isset($catalog[$key])) {
$res = bg_config_save_key($key, bg_config_default_string($catalog[$key]));
bg_flash(($res['code'] ?? 1) === 0 ? 'success' : 'danger', trim($res['output']) ?: 'Default created.');
bg_redirect('config.php?key=' . rawurlencode($key));
}
}
}
$def = $catalog[$selected];
$read = bg_config_read($def);
$content = $read['exists'] && $read['readable'] ? $read['raw'] : bg_config_default_string($def);
include 'includes/header.php'; include 'includes/sidebar.php';
?>
<div class="page-title">
<div>
<h1>BastionGuard configurations</h1>
<p>Reads the files actually used by the source and can automatically create missing ones when the WebUI is installed server-side.</p>
</div>
<form method="post" class="d-flex gap-2"><?php csrf_field(); ?><input type="hidden" name="action" value="ensure"><button class="btn btn-primary"><i class="bi bi-file-earmark-plus"></i> Create missing configurations</button></form>
</div>
<div class="alert alert-light border">
<div><strong>Target user:</strong> <code><?= bg_e(bg_detect_desktop_user() ?: 'non impostato') ?></code></div>
<div><strong>Real folder read:</strong> <code><?= bg_e($scan['dir']) ?></code> <?= $scan['exists'] ? '<span class="badge text-bg-success">present</span>' : '<span class="badge text-bg-warning">missing</span>' ?></div>
<div class="small-muted mt-1">To write to <code>/etc</code> or to the user home from Apache/PHP-FPM, install the helpers: <code>sudo bash scripts/install-webui-helpers.sh &quot;$USER&quot;</code></div>
</div>
<div class="row g-3">
<div class="col-lg-4 col-xl-3">
<div class="card mb-3"><div class="card-body"><h6>Present files</h6>
<?php if(!$scan['exists']): ?><p class="text-muted mb-0">Directory .config/BastionGuard not present.</p><?php elseif(empty($scan['items'])): ?><p class="text-muted mb-0">Empty directory.</p><?php else: ?>
<div class="list-scroll"><table class="table table-sm mb-0"><tbody><?php foreach($scan['items'] as $it): ?><tr><td class="text-break"><code><?= bg_e($it['name']) ?></code></td><td><?= bg_e($it['type']) ?></td><td><?= $it['readable'] ? 'read' : 'no' ?></td></tr><?php endforeach; ?></tbody></table></div><?php endif; ?>
</div></div>
<div class="card"><div class="list-group list-group-flush">
<?php foreach($catalog as $key=>$c): $r = bg_config_read($c); ?>
<a class="list-group-item list-group-item-action <?= $key===$selected?'active':'' ?>" href="config.php?key=<?= bg_e(rawurlencode($key)) ?>">
<div class="d-flex justify-content-between gap-2">
<strong><?= bg_e($c['label']) ?></strong>
<?php if(!$r['exists']): ?><span class="badge text-bg-warning">missing</span><?php elseif($r['error']): ?><span class="badge text-bg-danger">error</span><?php else: ?><span class="badge text-bg-success">ok</span><?php endif; ?>
</div>
<div class="small <?= $key===$selected?'text-white-50':'text-muted' ?> text-break"><?= bg_e($c['path']) ?></div>
</a>
<?php endforeach; ?>
</div></div>
</div>
<div class="col-lg-8 col-xl-9">
<div class="card"><div class="card-body">
<div class="d-flex align-items-start justify-content-between gap-3 mb-3">
<div>
<h5 class="mb-1"><?= bg_e($def['label']) ?></h5>
<p class="small-muted mb-0"><?= bg_e($def['description']) ?></p>
<div class="mt-2"><span class="badge text-bg-secondary"><?= bg_e($def['scope']) ?></span> <span class="badge text-bg-info"><?= bg_e($def['type']) ?></span></div>
</div>
<div class="text-end small-muted"><code><?= bg_e($def['path']) ?></code></div>
</div>
<?php if(!$read['exists']): ?><div class="alert alert-warning">Missing file: the default content will be shown.</div><?php endif; ?>
<?php if($read['error']): ?><div class="alert alert-danger"><?= bg_e($read['error']) ?></div><?php endif; ?>
<?php if(($def['type'] ?? '') === 'directory'): ?>
<div class="alert alert-info">This item is a directory. The WebUI reads and displays it, but does not modify it.</div>
<?php bg_render_log($content, 'Empty directory.'); ?>
<?php else: ?>
<form method="post">
<?php csrf_field(); ?><input type="hidden" name="action" value="save"><input type="hidden" name="key" value="<?= bg_e($selected) ?>">
<textarea class="form-control" name="content" rows="22" spellcheck="false"><?= bg_e($content) ?></textarea>
<div class="d-flex gap-2 mt-3">
<button class="btn btn-success"><i class="bi bi-save"></i> Save configuration</button>
<button class="btn btn-outline-warning" name="action" value="reset_default" onclick="return confirm('Overwrite with the default?')"><i class="bi bi-arrow-counterclockwise"></i> Restore default</button>
</div>
</form>
<?php endif; ?>
</div></div>
</div>
</div>
<?php include 'includes/footer.php'; ?>