| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569 |
- <?php
- declare(strict_types=1);
- require_once dirname(__DIR__, 2) . '/src/bootstrap.php';
- $auth = app_admin_auth();
- $auth->start();
- $configRepository = app_config_repository();
- $message = null;
- $messageType = 'success';
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $action = $_POST['action'] ?? '';
- if ($action === 'login') {
- $success = $auth->login(
- trim((string) ($_POST['username'] ?? '')),
- (string) ($_POST['password'] ?? '')
- );
- if ($success) {
- app_redirect(app_url('/admin/'));
- }
- $message = 'Login fehlgeschlagen. Bitte Zugangsdaten pruefen.';
- $messageType = 'error';
- }
- if ($action === 'save_config' && $auth->isAuthenticated()) {
- $config = $configRepository->getConfig();
- $newPassword = trim((string) ($_POST['admin_password'] ?? ''));
- $config['app'] = [
- 'name' => trim((string) ($_POST['app_name'] ?? 'Getraenkeautomat Monitor')),
- 'timezone' => trim((string) ($_POST['timezone'] ?? 'Europe/Berlin')),
- 'dashboard_refresh_seconds' => max(5, (int) ($_POST['dashboard_refresh_seconds'] ?? 15)),
- 'default_from_email' => trim((string) ($_POST['default_from_email'] ?? 'monitor@example.local')),
- 'base_path' => app_normalize_base_path((string) ($_POST['base_path'] ?? '')),
- ];
- $config['api'] = [
- 'bearer_token' => trim((string) ($_POST['bearer_token'] ?? 'change-me-token')),
- ];
- $config['admin'] = [
- 'username' => trim((string) ($_POST['admin_username'] ?? 'admin')),
- 'password_hash' => $newPassword !== ''
- ? password_hash($newPassword, PASSWORD_BCRYPT)
- : (string) ($config['admin']['password_hash'] ?? ''),
- ];
- $config['alerts'] = [
- 'webhooks' => normalizeWebhooks($_POST['webhooks'] ?? []),
- 'emails' => normalizeEmails($_POST['emails'] ?? []),
- ];
- $config['machines'] = normalizeMachines($_POST['machines'] ?? []);
- $configRepository->saveConfig($config);
- $message = 'Konfiguration gespeichert.';
- $messageType = 'success';
- }
- }
- $config = $configRepository->getConfig();
- if (!$auth->isAuthenticated()) {
- renderLogin($message, $messageType);
- exit;
- }
- renderAdmin($config, $message, $messageType);
- function renderLogin(?string $message, string $messageType): void
- {
- ?>
- <!DOCTYPE html>
- <html lang="de">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Adminpanel Login</title>
- <link rel="stylesheet" href="<?= htmlspecialchars(app_url('/styles.css'), ENT_QUOTES) ?>">
- </head>
- <body>
- <main class="auth-page">
- <section class="auth-card">
- <p class="eyebrow">Adminbereich</p>
- <h1>Konfiguration sichern</h1>
- <p>Logge dich mit den statischen Zugangsdaten aus der JSON-Config ein.</p>
- <?php if ($message !== null): ?>
- <div class="message message--<?= htmlspecialchars($messageType, ENT_QUOTES) ?>"><?= htmlspecialchars($message, ENT_QUOTES) ?></div>
- <?php endif; ?>
- <form method="post">
- <input type="hidden" name="action" value="login">
- <label>
- Benutzername
- <input type="text" name="username" required>
- </label>
- <label>
- Passwort
- <input type="password" name="password" required>
- </label>
- <button class="button button--primary" type="submit">Einloggen</button>
- <a class="button button--ghost" href="<?= htmlspecialchars(app_url('/'), ENT_QUOTES) ?>">Zurueck zum Dashboard</a>
- </form>
- </section>
- </main>
- </body>
- </html>
- <?php
- }
- function renderAdmin(array $config, ?string $message, string $messageType): void
- {
- ?>
- <!DOCTYPE html>
- <html lang="de">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Adminpanel</title>
- <link rel="stylesheet" href="<?= htmlspecialchars(app_url('/styles.css'), ENT_QUOTES) ?>">
- </head>
- <body>
- <main class="admin-page">
- <section class="admin-shell">
- <div class="admin-header">
- <div>
- <p class="eyebrow">Adminpanel</p>
- <h1>Konfiguration der Automaten</h1>
- <p>Hier werden API-Token, Zugangsdaten, Faecher und Alarmwege direkt in der JSON-Config gepflegt.</p>
- </div>
- <div class="inline-actions">
- <a class="button button--ghost" href="<?= htmlspecialchars(app_url('/'), ENT_QUOTES) ?>">Dashboard</a>
- <a class="button button--secondary" href="<?= htmlspecialchars(app_url('/admin/logout.php'), ENT_QUOTES) ?>">Logout</a>
- </div>
- </div>
- <?php if ($message !== null): ?>
- <div class="message message--<?= htmlspecialchars($messageType, ENT_QUOTES) ?>"><?= htmlspecialchars($message, ENT_QUOTES) ?></div>
- <?php endif; ?>
- <form method="post" class="admin-form" id="admin-form">
- <input type="hidden" name="action" value="save_config">
- <section class="form-section">
- <h2>Allgemein</h2>
- <div class="field-grid">
- <label>
- App-Name
- <input type="text" name="app_name" value="<?= htmlspecialchars((string) ($config['app']['name'] ?? ''), ENT_QUOTES) ?>" required>
- </label>
- <label>
- Zeitzone
- <input type="text" name="timezone" value="<?= htmlspecialchars((string) ($config['app']['timezone'] ?? 'Europe/Berlin'), ENT_QUOTES) ?>" required>
- </label>
- <label>
- Auto-Refresh Sekunden
- <input type="number" min="5" name="dashboard_refresh_seconds" value="<?= (int) ($config['app']['dashboard_refresh_seconds'] ?? 15) ?>" required>
- </label>
- <label>
- Absender fuer Email
- <input type="email" name="default_from_email" value="<?= htmlspecialchars((string) ($config['app']['default_from_email'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Basis-Pfad
- <input type="text" name="base_path" value="<?= htmlspecialchars((string) ($config['app']['base_path'] ?? ''), ENT_QUOTES) ?>" placeholder="/auswertung">
- </label>
- </div>
- <p class="field-help">Leer lassen fuer den Domain-Root. Fuer Unterordner z. B. <code>/auswertung</code> eintragen.</p>
- </section>
- <section class="admin-grid">
- <section class="form-section">
- <h2>API</h2>
- <label>
- Bearer-Token
- <input type="text" name="bearer_token" value="<?= htmlspecialchars((string) ($config['api']['bearer_token'] ?? ''), ENT_QUOTES) ?>" required>
- </label>
- <p class="field-help">ESP32-Clients senden dieses Token im Header <code>Authorization: Bearer ...</code>.</p>
- </section>
- <section class="form-section">
- <h2>Adminzugang</h2>
- <div class="field-grid">
- <label>
- Benutzername
- <input type="text" name="admin_username" value="<?= htmlspecialchars((string) ($config['admin']['username'] ?? ''), ENT_QUOTES) ?>" required>
- </label>
- <label>
- Neues Passwort
- <input type="password" name="admin_password" placeholder="Leer lassen, um es nicht zu aendern">
- </label>
- </div>
- </section>
- </section>
- <section class="form-section">
- <div class="panel__header">
- <div>
- <h2>Webhooks</h2>
- <p>Mehrere Ziele sind moeglich. Header koennen als JSON hinterlegt werden.</p>
- </div>
- <button class="button button--secondary" type="button" data-add="webhook">Webhook hinzufuegen</button>
- </div>
- <div class="repeat-stack" id="webhook-stack">
- <?php foreach (($config['alerts']['webhooks'] ?? []) as $index => $webhook): ?>
- <?= renderWebhookRow((int) $index, $webhook) ?>
- <?php endforeach; ?>
- </div>
- </section>
- <section class="form-section">
- <div class="panel__header">
- <div>
- <h2>Email-Empfaenger</h2>
- <p>Emails werden ueber die Serverkonfiguration von <code>mail()</code> versendet.</p>
- </div>
- <button class="button button--secondary" type="button" data-add="email">Email-Empfaenger hinzufuegen</button>
- </div>
- <div class="repeat-stack" id="email-stack">
- <?php foreach (($config['alerts']['emails'] ?? []) as $index => $email): ?>
- <?= renderEmailRow((int) $index, $email) ?>
- <?php endforeach; ?>
- </div>
- </section>
- <section class="form-section">
- <div class="panel__header">
- <div>
- <h2>Automaten und Faecher</h2>
- <p>Jeder Automat enthaelt beliebig viele Faecher, die jeweils genau einem Sensor zugeordnet sind.</p>
- </div>
- <button class="button button--secondary" type="button" data-add="machine">Automat hinzufuegen</button>
- </div>
- <div class="repeat-stack" id="machine-stack">
- <?php foreach (($config['machines'] ?? []) as $machineIndex => $machine): ?>
- <?= renderMachineRow((int) $machineIndex, $machine) ?>
- <?php endforeach; ?>
- </div>
- </section>
- <div class="section-actions">
- <button class="button button--primary" type="submit">Konfiguration speichern</button>
- </div>
- </form>
- </section>
- </main>
- <template id="tpl-webhook"><?= renderWebhookRow('__INDEX__', []) ?></template>
- <template id="tpl-email"><?= renderEmailRow('__INDEX__', []) ?></template>
- <template id="tpl-machine"><?= renderMachineRow('__MACHINE__', []) ?></template>
- <template id="tpl-slot"><?= renderSlotRow('__MACHINE__', '__SLOT__', []) ?></template>
- <script>
- const webhookStack = document.getElementById('webhook-stack');
- const emailStack = document.getElementById('email-stack');
- const machineStack = document.getElementById('machine-stack');
- const renderTemplate = (id, replacements) => {
- let html = document.getElementById(id).innerHTML;
- Object.entries(replacements).forEach(([needle, value]) => {
- html = html.replaceAll(needle, value);
- });
- return html;
- };
- const countChildren = (node, selector) => node.querySelectorAll(selector).length;
- document.addEventListener('click', (event) => {
- const addType = event.target.getAttribute('data-add');
- const removeType = event.target.getAttribute('data-remove');
- if (addType === 'webhook') {
- const index = countChildren(webhookStack, '[data-webhook-row]');
- webhookStack.insertAdjacentHTML('beforeend', renderTemplate('tpl-webhook', { '__INDEX__': index }));
- }
- if (addType === 'email') {
- const index = countChildren(emailStack, '[data-email-row]');
- emailStack.insertAdjacentHTML('beforeend', renderTemplate('tpl-email', { '__INDEX__': index }));
- }
- if (addType === 'machine') {
- const index = countChildren(machineStack, '[data-machine-row]');
- machineStack.insertAdjacentHTML('beforeend', renderTemplate('tpl-machine', { '__MACHINE__': index, '__SLOT__': 0 }));
- }
- if (addType === 'slot') {
- const machineIndex = event.target.getAttribute('data-machine-index');
- const container = document.querySelector(`[data-slot-stack="${machineIndex}"]`);
- const slotIndex = countChildren(container, '[data-slot-row]');
- container.insertAdjacentHTML('beforeend', renderTemplate('tpl-slot', {
- '__MACHINE__': machineIndex,
- '__SLOT__': slotIndex
- }));
- }
- if (removeType) {
- const row = event.target.closest(`[data-${removeType}-row]`);
- if (row) {
- row.remove();
- }
- }
- });
- </script>
- </body>
- </html>
- <?php
- }
- function renderWebhookRow(int|string $index, array $webhook): string
- {
- $headers = $webhook['headers'] ?? [];
- $headerJson = json_encode($headers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
- ob_start();
- ?>
- <div class="repeat-card" data-webhook-row>
- <div class="panel__header">
- <h4>Webhook</h4>
- <button class="button button--danger" type="button" data-remove="webhook">Entfernen</button>
- </div>
- <div class="field-grid">
- <label>
- ID
- <input type="text" name="webhooks[<?= $index ?>][id]" value="<?= htmlspecialchars((string) ($webhook['id'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Name
- <input type="text" name="webhooks[<?= $index ?>][name]" value="<?= htmlspecialchars((string) ($webhook['name'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- URL
- <input type="url" name="webhooks[<?= $index ?>][url]" value="<?= htmlspecialchars((string) ($webhook['url'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Aktiv
- <select name="webhooks[<?= $index ?>][enabled]">
- <option value="1" <?= !empty($webhook['enabled']) ? 'selected' : '' ?>>Ja</option>
- <option value="0" <?= empty($webhook['enabled']) ? 'selected' : '' ?>>Nein</option>
- </select>
- </label>
- </div>
- <label>
- Header als JSON-Objekt
- <textarea name="webhooks[<?= $index ?>][headers_json]"><?= htmlspecialchars((string) $headerJson, ENT_QUOTES) ?></textarea>
- </label>
- </div>
- <?php
- return (string) ob_get_clean();
- }
- function renderEmailRow(int|string $index, array $email): string
- {
- ob_start();
- ?>
- <div class="repeat-card" data-email-row>
- <div class="panel__header">
- <h4>Email-Empfaenger</h4>
- <button class="button button--danger" type="button" data-remove="email">Entfernen</button>
- </div>
- <div class="field-grid">
- <label>
- ID
- <input type="text" name="emails[<?= $index ?>][id]" value="<?= htmlspecialchars((string) ($email['id'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Name
- <input type="text" name="emails[<?= $index ?>][name]" value="<?= htmlspecialchars((string) ($email['name'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Adresse
- <input type="email" name="emails[<?= $index ?>][address]" value="<?= htmlspecialchars((string) ($email['address'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Aktiv
- <select name="emails[<?= $index ?>][enabled]">
- <option value="1" <?= !empty($email['enabled']) ? 'selected' : '' ?>>Ja</option>
- <option value="0" <?= empty($email['enabled']) ? 'selected' : '' ?>>Nein</option>
- </select>
- </label>
- </div>
- </div>
- <?php
- return (string) ob_get_clean();
- }
- function renderMachineRow(int|string $machineIndex, array $machine): string
- {
- ob_start();
- ?>
- <div class="repeat-card" data-machine-row>
- <div class="panel__header">
- <div>
- <h4>Automat</h4>
- <p>Faecher koennen direkt darunter verwaltet werden.</p>
- </div>
- <div class="inline-actions">
- <button class="button button--secondary" type="button" data-add="slot" data-machine-index="<?= htmlspecialchars((string) $machineIndex, ENT_QUOTES) ?>">Fach hinzufuegen</button>
- <button class="button button--danger" type="button" data-remove="machine">Automat entfernen</button>
- </div>
- </div>
- <div class="field-grid">
- <label>
- ID
- <input type="text" name="machines[<?= $machineIndex ?>][id]" value="<?= htmlspecialchars((string) ($machine['id'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Name
- <input type="text" name="machines[<?= $machineIndex ?>][name]" value="<?= htmlspecialchars((string) ($machine['name'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Standort
- <input type="text" name="machines[<?= $machineIndex ?>][location]" value="<?= htmlspecialchars((string) ($machine['location'] ?? ''), ENT_QUOTES) ?>">
- </label>
- </div>
- <div class="repeat-stack" data-slot-stack="<?= htmlspecialchars((string) $machineIndex, ENT_QUOTES) ?>">
- <?php foreach (($machine['slots'] ?? []) as $slotIndex => $slot): ?>
- <?= renderSlotRow($machineIndex, (int) $slotIndex, $slot) ?>
- <?php endforeach; ?>
- </div>
- </div>
- <?php
- return (string) ob_get_clean();
- }
- function renderSlotRow(int|string $machineIndex, int|string $slotIndex, array $slot): string
- {
- $webhookIds = implode(',', $slot['webhook_ids'] ?? []);
- $emailIds = implode(',', $slot['email_ids'] ?? []);
- ob_start();
- ?>
- <div class="slot-editor" data-slot-row>
- <div class="panel__header">
- <h5>Fach</h5>
- <button class="button button--danger" type="button" data-remove="slot">Fach entfernen</button>
- </div>
- <div class="field-grid">
- <label>
- Sensor-ID
- <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][sensor_id]" value="<?= htmlspecialchars((string) ($slot['sensor_id'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Label
- <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][label]" value="<?= htmlspecialchars((string) ($slot['label'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Produktname
- <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][product_name]" value="<?= htmlspecialchars((string) ($slot['product_name'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Voll-Distanz in mm
- <input type="number" step="0.01" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][full_distance_mm]" value="<?= htmlspecialchars((string) ($slot['full_distance_mm'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Leer-Distanz in mm
- <input type="number" step="0.01" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][empty_distance_mm]" value="<?= htmlspecialchars((string) ($slot['empty_distance_mm'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Distanz pro Flasche
- <input type="number" step="0.01" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][distance_per_unit]" value="<?= htmlspecialchars((string) ($slot['distance_per_unit'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Alarm unter Bestand
- <input type="number" step="1" min="0" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][alert_below_units]" value="<?= htmlspecialchars((string) ($slot['alert_below_units'] ?? ''), ENT_QUOTES) ?>">
- </label>
- <label>
- Webhook-IDs
- <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][webhook_ids]" value="<?= htmlspecialchars((string) $webhookIds, ENT_QUOTES) ?>" placeholder="ops,lager">
- </label>
- <label>
- Email-IDs
- <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][email_ids]" value="<?= htmlspecialchars((string) $emailIds, ENT_QUOTES) ?>" placeholder="lager,service">
- </label>
- </div>
- </div>
- <?php
- return (string) ob_get_clean();
- }
- function normalizeWebhooks(array $rows): array
- {
- $items = [];
- foreach ($rows as $row) {
- $id = trim((string) ($row['id'] ?? ''));
- $url = trim((string) ($row['url'] ?? ''));
- if ($id === '' && $url === '') {
- continue;
- }
- $headers = json_decode((string) ($row['headers_json'] ?? '{}'), true);
- $items[] = [
- 'id' => $id,
- 'name' => trim((string) ($row['name'] ?? '')),
- 'url' => $url,
- 'enabled' => (string) ($row['enabled'] ?? '1') === '1',
- 'headers' => is_array($headers) ? $headers : [],
- ];
- }
- return array_values($items);
- }
- function normalizeEmails(array $rows): array
- {
- $items = [];
- foreach ($rows as $row) {
- $id = trim((string) ($row['id'] ?? ''));
- $address = trim((string) ($row['address'] ?? ''));
- if ($id === '' && $address === '') {
- continue;
- }
- $items[] = [
- 'id' => $id,
- 'name' => trim((string) ($row['name'] ?? '')),
- 'address' => $address,
- 'enabled' => (string) ($row['enabled'] ?? '1') === '1',
- ];
- }
- return array_values($items);
- }
- function normalizeMachines(array $rows): array
- {
- $items = [];
- foreach ($rows as $row) {
- $id = trim((string) ($row['id'] ?? ''));
- $name = trim((string) ($row['name'] ?? ''));
- if ($id === '' && $name === '') {
- continue;
- }
- $slots = [];
- foreach (($row['slots'] ?? []) as $slot) {
- $sensorId = trim((string) ($slot['sensor_id'] ?? ''));
- if ($sensorId === '') {
- continue;
- }
- $slots[] = [
- 'sensor_id' => $sensorId,
- 'label' => trim((string) ($slot['label'] ?? $sensorId)),
- 'product_name' => trim((string) ($slot['product_name'] ?? '')),
- 'full_distance_mm' => (float) ($slot['full_distance_mm'] ?? 0),
- 'empty_distance_mm' => (float) ($slot['empty_distance_mm'] ?? 0),
- 'distance_per_unit' => max(0.01, (float) ($slot['distance_per_unit'] ?? 1)),
- 'alert_below_units' => max(0, (int) ($slot['alert_below_units'] ?? 0)),
- 'webhook_ids' => parseIdList((string) ($slot['webhook_ids'] ?? '')),
- 'email_ids' => parseIdList((string) ($slot['email_ids'] ?? '')),
- ];
- }
- $items[] = [
- 'id' => $id,
- 'name' => $name,
- 'location' => trim((string) ($row['location'] ?? '')),
- 'slots' => $slots,
- ];
- }
- return array_values($items);
- }
- function parseIdList(string $value): array
- {
- $parts = array_map('trim', explode(',', $value));
- return array_values(array_filter($parts, static fn (string $item): bool => $item !== ''));
- }
|