Fix plugin routing, missing includes, and make SCRIPTS_DIR configurable

- 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)
This commit is contained in:
Gmer4Lfe
2026-05-23 16:48:52 -04:00
parent 4ab3ddbc47
commit 7b7a3a9e9e
6 changed files with 63 additions and 1 deletions
@@ -0,0 +1,24 @@
<?php
header('Content-Type: application/json');
$body = json_decode(file_get_contents('php://input'), true);
$scriptsDir = trim($body['scripts_dir'] ?? '');
if (!$scriptsDir) {
echo json_encode(['ok' => false, 'error' => 'scripts_dir is required']);
exit;
}
if (!is_dir($scriptsDir)) {
echo json_encode(['ok' => false, 'error' => 'Directory does not exist']);
exit;
}
$cfgFile = '/boot/config/plugins/varaverk/varaverk.cfg';
$cfgDir = dirname($cfgFile);
if (!is_dir($cfgDir)) mkdir($cfgDir, 0755, true);
$content = 'SCRIPTS_DIR="' . addslashes($scriptsDir) . '"' . "\n";
$ok = file_put_contents($cfgFile, $content) !== false;
echo json_encode(['ok' => $ok, 'error' => $ok ? null : 'Failed to write cfg file']);
@@ -2,8 +2,12 @@
// Config file parser and writer.
// Reads master.conf and the appropriate host*.conf based on running host.
define('SCRIPTS_DIR', '/mnt/user/appdata/unraid_scripts');
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') ?: '');
@@ -2,6 +2,8 @@
// Docs — markdown file discovery, $VAR substitution, and rendering.
// Requires parsedown or similar. Falls back to <pre> if not available.
require_once __DIR__ . '/config.php';
define('PARSEDOWN_PATH', '/usr/local/emhttp/plugins/varaverk/lib/Parsedown.php');
function vv_docs_tree(): array {
@@ -2,6 +2,8 @@
// Scheduler — manages schedule.json and /etc/cron.d/varaverk.
// schedule.json is per-host, never synced.
require_once __DIR__ . '/config.php';
define('SCHEDULE_FILE', '/boot/config/plugins/varaverk/schedule.json');
define('CRON_FILE', '/etc/cron.d/varaverk');
define('CRON_USER', 'root');
@@ -3,10 +3,25 @@ require_once dirname(__DIR__) . '/include/config.php';
$files = vv_get_conf_files();
$active = $_GET['conf'] ?? ($files[0] ?? '');
if (!in_array($active, $files)) $active = $files[0] ?? '';
$currentScriptsDir = SCRIPTS_DIR;
?>
<div id="vv-config">
<!-- Plugin Settings -->
<div class="vv-card vv-wide" style="margin-bottom:16px;">
<h3>Plugin Settings</h3>
<div class="vv-job-row" style="max-width:700px;gap:8px;">
<label style="color:#aaa;font-size:13px;white-space:nowrap;">Scripts directory</label>
<input type="text" id="vv-scripts-dir" value="<?= htmlspecialchars($currentScriptsDir) ?>"
style="flex:1;background:#111;border:1px solid #444;color:#ddd;padding:4px 8px;
border-radius:4px;font-family:monospace;font-size:13px;">
<button type="button" onclick="vvSaveSettings()" style="padding:4px 14px;background:#4caf50;
border:none;color:#fff;border-radius:4px;cursor:pointer;font-size:13px;">Save</button>
<span id="vv-settings-status" style="font-size:13px;color:#aaa;"></span>
</div>
</div>
<!-- File selector -->
<div id="vv-conf-tabs">
<?php foreach ($files as $f): ?>
@@ -35,6 +50,21 @@ if (!in_array($active, $files)) $active = $files[0] ?? '';
</div>
<script>
function vvSaveSettings() {
const dir = document.getElementById('vv-scripts-dir').value.trim();
const status = document.getElementById('vv-settings-status');
if (!dir) { status.textContent = '✗ Path required'; return; }
status.textContent = 'Saving...';
fetch('/plugins/varaverk/api/settings.php', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({scripts_dir: dir})
})
.then(r => r.json())
.then(d => { status.textContent = d.ok ? '✓ Saved — reload to apply' : '✗ ' + (d.error ?? 'Error'); })
.catch(() => { status.textContent = '✗ Request failed'; });
}
function vvSaveConf() {
const form = document.getElementById('vv-conf-form');
const file = form.querySelector('[name=file]').value;