Add settings panel and readonly default
This commit is contained in:
@@ -2,6 +2,39 @@
|
||||
|
||||
const UNAV_DEFAULT_ROOT = '/mnt/user';
|
||||
const UNAV_JOB_DIR = '/tmp/u-navigator/jobs';
|
||||
const UNAV_SETTINGS_FILE = '/boot/config/plugins/u-navigator/settings.json';
|
||||
|
||||
function unav_settings(): array {
|
||||
$defaults = [
|
||||
'readOnly' => true,
|
||||
'debug' => false,
|
||||
];
|
||||
if (!is_file(UNAV_SETTINGS_FILE)) {
|
||||
return $defaults;
|
||||
}
|
||||
$settings = json_decode(file_get_contents(UNAV_SETTINGS_FILE) ?: '{}', true);
|
||||
if (!is_array($settings)) {
|
||||
return $defaults;
|
||||
}
|
||||
return array_merge($defaults, [
|
||||
'readOnly' => filter_var($settings['readOnly'] ?? $defaults['readOnly'], FILTER_VALIDATE_BOOLEAN),
|
||||
'debug' => filter_var($settings['debug'] ?? $defaults['debug'], FILTER_VALIDATE_BOOLEAN),
|
||||
]);
|
||||
}
|
||||
|
||||
function unav_settings_write(array $settings): array {
|
||||
$current = unav_settings();
|
||||
$next = [
|
||||
'readOnly' => filter_var($settings['readOnly'] ?? $current['readOnly'], FILTER_VALIDATE_BOOLEAN),
|
||||
'debug' => filter_var($settings['debug'] ?? $current['debug'], FILTER_VALIDATE_BOOLEAN),
|
||||
];
|
||||
$dir = dirname(UNAV_SETTINGS_FILE);
|
||||
if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) {
|
||||
unav_error(500, 'Could not create settings directory');
|
||||
}
|
||||
file_put_contents(UNAV_SETTINGS_FILE, json_encode($next, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), LOCK_EX);
|
||||
return $next;
|
||||
}
|
||||
|
||||
function unav_roots(): array {
|
||||
$configured = getenv('U_NAV_ROOTS') ?: UNAV_DEFAULT_ROOT;
|
||||
@@ -29,7 +62,14 @@ function unav_roots(): array {
|
||||
}
|
||||
|
||||
function unav_read_only(): bool {
|
||||
return getenv('U_NAV_READ_ONLY') === '1';
|
||||
$env = getenv('U_NAV_READ_ONLY');
|
||||
if ($env === '1') {
|
||||
return true;
|
||||
}
|
||||
if ($env === '0') {
|
||||
return false;
|
||||
}
|
||||
return unav_settings()['readOnly'];
|
||||
}
|
||||
|
||||
function unav_function_available(string $name): bool {
|
||||
|
||||
@@ -1,7 +1,19 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/common.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$data = unav_request_data();
|
||||
$settings = unav_settings_write($data);
|
||||
unav_json([
|
||||
'readOnly' => $settings['readOnly'],
|
||||
'debug' => $settings['debug'],
|
||||
'roots' => unav_roots(),
|
||||
]);
|
||||
}
|
||||
|
||||
$settings = unav_settings();
|
||||
unav_json([
|
||||
'readOnly' => unav_read_only(),
|
||||
'debug' => $settings['debug'],
|
||||
'roots' => unav_roots(),
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user