Files
nexus/modules/pi_control/pages/commands.php
2026-03-05 00:33:30 +01:00

91 lines
3.1 KiB
PHP

<?php
$pdo = module_fn('pi_control', 'pdo');
module_fn('pi_control', 'ensure_schema');
$table = fn(string $name) => module_fn('pi_control', 'table', $name);
$notice = null;
$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
require_admin();
$label = trim((string)($_POST['label'] ?? ''));
$command = trim((string)($_POST['command'] ?? ''));
$adminOnly = !empty($_POST['admin_only']) ? 1 : 0;
if ($label === '' || $command === '') {
$error = 'Bitte Label und Command angeben.';
} else {
$stmt = $pdo->prepare(
'INSERT INTO ' . $table('commands') . ' (label, command, admin_only) VALUES (:label, :command, :admin_only)'
);
$stmt->execute([
'label' => $label,
'command' => $command,
'admin_only' => $adminOnly,
]);
$notice = 'Befehl gespeichert.';
}
}
$commands = $pdo->query('SELECT * FROM ' . $table('commands') . ' ORDER BY id DESC')->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="card">
<div class="pill">Pi Control</div>
<h1 style="margin-top:.75rem;">Befehle</h1>
<p class="muted">Verwalte vordefinierte SSH-Befehle.</p>
<?php if ($error): ?>
<div class="card" style="margin-top:1rem; border-color:#ffb4a8; background:#fff5f3; color:#7a2114;">
<?= e($error) ?>
</div>
<?php elseif ($notice): ?>
<div class="card" style="margin-top:1rem; border-color:var(--accent-2);">
<?= e($notice) ?>
</div>
<?php endif; ?>
<div class="grid" style="margin-top:1rem;">
<div class="card" style="background:var(--panel-2);">
<strong>Neuer Befehl</strong>
<form method="post" style="display:grid; gap:10px; margin-top:.75rem;">
<input type="text" name="label" placeholder="Label" required>
<textarea name="command" rows="4" placeholder="Command" required></textarea>
<label class="muted" style="display:flex; gap:8px; align-items:center;">
<input type="checkbox" name="admin_only" value="1">
Nur Admin
</label>
<button class="cta-button" type="submit">Speichern</button>
</form>
</div>
<div class="card" style="background:var(--panel-2);">
<strong>Vorhandene Befehle</strong>
<div style="margin-top:.75rem; overflow:auto;">
<table class="table" style="min-width:560px;">
<thead>
<tr>
<th>Label</th>
<th>Command</th>
<th>Admin</th>
</tr>
</thead>
<tbody>
<?php if (!$commands): ?>
<tr><td colspan="3" class="muted">Keine Befehle vorhanden.</td></tr>
<?php endif; ?>
<?php foreach ($commands as $c): ?>
<tr>
<td><?= e($c['label'] ?? '') ?></td>
<td class="muted" style="max-width:360px;">
<code><?= e($c['command'] ?? '') ?></code>
</td>
<td><?= !empty($c['admin_only']) ? 'ja' : 'nein' ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>