- Rename varaverk.page → Varaverk.page: unRAID nginx only routes URLs starting with a capital letter (~^/[A-Z].*), lowercase caused 404 - Add require_once config.php to include/scheduler.php and include/docs.php: both used SCRIPTS_DIR constant without including the file that defines it - Replace hardcoded SCRIPTS_DIR with cfg-file-backed setting: reads from /boot/config/plugins/varaverk/varaverk.cfg, falls back to default on first run - Add Plugin Settings card to Config tab with scripts path field - Add api/settings.php to write the cfg file (validates directory exists)
77 lines
2.5 KiB
PHP
77 lines
2.5 KiB
PHP
<?php
|
|
// Config file parser and writer.
|
|
// Reads master.conf and the appropriate host*.conf based on running host.
|
|
|
|
define('PLUGIN_CFG', '/boot/config/plugins/varaverk/varaverk.cfg');
|
|
|
|
$_vv_cfg = @parse_ini_file(PLUGIN_CFG) ?: [];
|
|
define('SCRIPTS_DIR', $_vv_cfg['SCRIPTS_DIR'] ?? '/mnt/user/appdata/unraid_scripts');
|
|
define('CONF_DIR', SCRIPTS_DIR . '/Configurations');
|
|
unset($_vv_cfg);
|
|
|
|
function vv_get_hostname(): string {
|
|
return trim(shell_exec('hostname -s') ?: '');
|
|
}
|
|
|
|
function vv_detect_host(): string {
|
|
// Reads master.conf to find HOST1/HOST2 hostnames, returns 'host1', 'host2', or 'unknown'
|
|
$master = vv_read_conf_raw('master.conf');
|
|
preg_match('/^\s*HOST1_NAME\s*=\s*["\']?(\S+?)["\']?\s*$/m', $master, $m1);
|
|
preg_match('/^\s*HOST2_NAME\s*=\s*["\']?(\S+?)["\']?\s*$/m', $master, $m2);
|
|
$hostname = vv_get_hostname();
|
|
if (!empty($m1[1]) && strcasecmp($hostname, $m1[1]) === 0) return 'host1';
|
|
if (!empty($m2[1]) && strcasecmp($hostname, $m2[1]) === 0) return 'host2';
|
|
return 'unknown';
|
|
}
|
|
|
|
function vv_is_owner(): bool {
|
|
return vv_detect_host() === 'host1';
|
|
}
|
|
|
|
function vv_read_conf_raw(string $filename): string {
|
|
$path = CONF_DIR . '/' . $filename;
|
|
return file_exists($path) ? file_get_contents($path) : '';
|
|
}
|
|
|
|
function vv_write_conf_raw(string $filename, string $content): bool {
|
|
$path = CONF_DIR . '/' . $filename;
|
|
return file_put_contents($path, $content) !== false;
|
|
}
|
|
|
|
function vv_get_conf_files(): array {
|
|
// Returns conf files this host is allowed to view/edit
|
|
$host = vv_detect_host();
|
|
$files = [];
|
|
if ($host === 'host1') {
|
|
$files[] = 'master.conf';
|
|
$files[] = 'host1.conf';
|
|
} elseif ($host === 'host2') {
|
|
$files[] = 'host2.conf';
|
|
} else {
|
|
// Unknown host — show all for dev/debug
|
|
foreach (glob(CONF_DIR . '/*.conf') as $f) {
|
|
$files[] = basename($f);
|
|
}
|
|
}
|
|
return $files;
|
|
}
|
|
|
|
// Parse conf into key=>value map for $VAR substitution in docs
|
|
function vv_conf_vars(): array {
|
|
$vars = [];
|
|
$files = ['master.conf'];
|
|
$host = vv_detect_host();
|
|
if ($host === 'host1') $files[] = 'host1.conf';
|
|
if ($host === 'host2') $files[] = 'host2.conf';
|
|
|
|
foreach ($files as $f) {
|
|
$raw = vv_read_conf_raw($f);
|
|
// Match: VAR_NAME="value" or VAR_NAME=value (no quotes)
|
|
preg_match_all('/^\s*([A-Z0-9_]+)\s*=\s*["\']?([^"\'#\n]*?)["\']?\s*(?:#.*)?$/m', $raw, $m);
|
|
foreach ($m[1] as $i => $key) {
|
|
$vars[$key] = trim($m[2][$i]);
|
|
}
|
|
}
|
|
return $vars;
|
|
}
|