index.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <?php
  2. declare(strict_types=1);
  3. require_once dirname(__DIR__) . '/src/bootstrap.php';
  4. header('Content-Type: text/html; charset=UTF-8');
  5. $auth = app_admin_auth();
  6. $auth->start();
  7. $configRepository = app_config_repository();
  8. $message = null;
  9. $messageType = 'success';
  10. $rawConfigText = null;
  11. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  12. $action = $_POST['action'] ?? '';
  13. if ($action === 'login') {
  14. $success = $auth->login(
  15. trim((string) ($_POST['username'] ?? '')),
  16. (string) ($_POST['password'] ?? '')
  17. );
  18. if ($success) {
  19. app_redirect(app_url('/admin/'));
  20. }
  21. $message = 'Login fehlgeschlagen. Bitte Zugangsdaten prüfen.';
  22. $messageType = 'error';
  23. }
  24. if ($action === 'save_config' && $auth->isAuthenticated()) {
  25. try {
  26. $config = $configRepository->getConfig();
  27. $newPassword = trim((string) ($_POST['admin_password'] ?? ''));
  28. $config['app'] = [
  29. 'name' => trim((string) ($_POST['app_name'] ?? 'Getränkeautomat Monitor')),
  30. 'timezone' => trim((string) ($_POST['timezone'] ?? 'Europe/Berlin')),
  31. 'dashboard_refresh_seconds' => max(5, (int) ($_POST['dashboard_refresh_seconds'] ?? 15)),
  32. 'default_from_email' => trim((string) ($_POST['default_from_email'] ?? 'monitor@example.local')),
  33. 'base_path' => app_normalize_base_path((string) ($_POST['base_path'] ?? '')),
  34. ];
  35. $config['api'] = [
  36. 'bearer_token' => trim((string) ($_POST['bearer_token'] ?? 'change-me-token')),
  37. ];
  38. $config['admin'] = [
  39. 'username' => trim((string) ($_POST['admin_username'] ?? 'admin')),
  40. 'password_hash' => $newPassword !== ''
  41. ? password_hash($newPassword, PASSWORD_BCRYPT)
  42. : (string) ($config['admin']['password_hash'] ?? ''),
  43. ];
  44. $config['alerts'] = [
  45. 'webhooks' => normalizeWebhooks($_POST['webhooks'] ?? []),
  46. 'emails' => normalizeEmails($_POST['emails'] ?? []),
  47. ];
  48. $config['machines'] = normalizeMachines($_POST['machines'] ?? []);
  49. $configRepository->saveConfig($config);
  50. $message = 'Konfiguration gespeichert.';
  51. $messageType = 'success';
  52. } catch (Throwable $exception) {
  53. $message = 'Konfiguration konnte nicht gespeichert werden: ' . $exception->getMessage();
  54. $messageType = 'error';
  55. }
  56. }
  57. if ($action === 'save_raw_config' && $auth->isAuthenticated()) {
  58. $rawConfigText = (string) ($_POST['raw_config'] ?? '');
  59. try {
  60. $decoded = json_decode($rawConfigText, true, 512, JSON_THROW_ON_ERROR);
  61. if (!is_array($decoded) || array_is_list($decoded)) {
  62. throw new JsonException('Die JSON-Config muss ein JSON-Objekt sein.');
  63. }
  64. $configRepository->saveConfig($decoded);
  65. $message = 'JSON-Config gespeichert.';
  66. $messageType = 'success';
  67. $rawConfigText = encodeConfigForEditor($decoded);
  68. } catch (Throwable $exception) {
  69. $message = 'JSON-Config ist ungültig: ' . $exception->getMessage();
  70. $messageType = 'error';
  71. }
  72. }
  73. }
  74. $config = $configRepository->getConfig();
  75. $rawConfigText ??= encodeConfigForEditor($config);
  76. if (!$auth->isAuthenticated()) {
  77. renderLogin($message, $messageType);
  78. exit;
  79. }
  80. renderAdmin($config, $rawConfigText, $message, $messageType);
  81. function renderLogin(?string $message, string $messageType): void
  82. {
  83. ?>
  84. <!DOCTYPE html>
  85. <html lang="de">
  86. <head>
  87. <meta charset="UTF-8">
  88. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  89. <title>Adminpanel Login</title>
  90. <link rel="stylesheet" href="<?= htmlspecialchars(app_url('/styles.css'), ENT_QUOTES) ?>">
  91. </head>
  92. <body>
  93. <main class="auth-page">
  94. <section class="auth-card">
  95. <p class="eyebrow">Adminbereich</p>
  96. <h1>Konfiguration sichern</h1>
  97. <p>Logge dich mit den statischen Zugangsdaten aus der JSON-Config ein.</p>
  98. <?php if ($message !== null): ?>
  99. <div class="message message--<?= htmlspecialchars($messageType, ENT_QUOTES) ?>"><?= htmlspecialchars($message, ENT_QUOTES) ?></div>
  100. <?php endif; ?>
  101. <form method="post">
  102. <input type="hidden" name="action" value="login">
  103. <label>
  104. Benutzername
  105. <input type="text" name="username" required>
  106. </label>
  107. <label>
  108. Passwort
  109. <input type="password" name="password" required>
  110. </label>
  111. <button class="button button--primary" type="submit">Einloggen</button>
  112. <a class="button button--ghost" href="<?= htmlspecialchars(app_url('/'), ENT_QUOTES) ?>">Zurück zum Dashboard</a>
  113. </form>
  114. </section>
  115. </main>
  116. </body>
  117. </html>
  118. <?php
  119. }
  120. function renderAdmin(array $config, string $rawConfigText, ?string $message, string $messageType): void
  121. {
  122. ?>
  123. <!DOCTYPE html>
  124. <html lang="de">
  125. <head>
  126. <meta charset="UTF-8">
  127. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  128. <title>Adminpanel</title>
  129. <link rel="stylesheet" href="<?= htmlspecialchars(app_url('/styles.css'), ENT_QUOTES) ?>">
  130. </head>
  131. <body>
  132. <main class="admin-page">
  133. <section class="admin-shell">
  134. <div class="admin-header">
  135. <div>
  136. <p class="eyebrow">Adminpanel</p>
  137. <h1>Konfiguration der Automaten</h1>
  138. <p>Hier werden API-Token, Zugangsdaten, Fächer und Alarmwege direkt in der JSON-Config gepflegt.</p>
  139. </div>
  140. <div class="inline-actions">
  141. <a class="button button--ghost" href="<?= htmlspecialchars(app_url('/'), ENT_QUOTES) ?>">Dashboard</a>
  142. <a class="button button--secondary" href="<?= htmlspecialchars(app_url('/admin/logout.php'), ENT_QUOTES) ?>">Logout</a>
  143. </div>
  144. </div>
  145. <?php if ($message !== null): ?>
  146. <div class="message message--<?= htmlspecialchars($messageType, ENT_QUOTES) ?>"><?= htmlspecialchars($message, ENT_QUOTES) ?></div>
  147. <?php endif; ?>
  148. <section class="form-section">
  149. <div class="panel__header">
  150. <div>
  151. <h2>JSON-Config direkt bearbeiten</h2>
  152. <p>Die komplette Konfiguration kann hier als Klartext angezeigt und direkt als JSON gespeichert werden.</p>
  153. </div>
  154. </div>
  155. <form method="post" class="admin-form">
  156. <input type="hidden" name="action" value="save_raw_config">
  157. <label>
  158. Konfiguration als JSON
  159. <textarea class="config-textarea" name="raw_config" spellcheck="false"><?= htmlspecialchars($rawConfigText, ENT_QUOTES) ?></textarea>
  160. </label>
  161. <div class="section-actions">
  162. <button class="button button--primary" type="submit">JSON speichern</button>
  163. </div>
  164. </form>
  165. </section>
  166. <form method="post" class="admin-form" id="admin-form">
  167. <input type="hidden" name="action" value="save_config">
  168. <section class="form-section">
  169. <h2>Allgemein</h2>
  170. <div class="field-grid">
  171. <label>
  172. App-Name
  173. <input type="text" name="app_name" value="<?= htmlspecialchars((string) ($config['app']['name'] ?? ''), ENT_QUOTES) ?>" required>
  174. </label>
  175. <label>
  176. Zeitzone
  177. <input type="text" name="timezone" value="<?= htmlspecialchars((string) ($config['app']['timezone'] ?? 'Europe/Berlin'), ENT_QUOTES) ?>" required>
  178. </label>
  179. <label>
  180. Auto-Refresh Sekunden
  181. <input type="number" min="5" name="dashboard_refresh_seconds" value="<?= (int) ($config['app']['dashboard_refresh_seconds'] ?? 15) ?>" required>
  182. </label>
  183. <label>
  184. Absender für Email
  185. <input type="email" name="default_from_email" value="<?= htmlspecialchars((string) ($config['app']['default_from_email'] ?? ''), ENT_QUOTES) ?>">
  186. </label>
  187. <label>
  188. Basis-Pfad
  189. <input type="text" name="base_path" value="<?= htmlspecialchars((string) ($config['app']['base_path'] ?? ''), ENT_QUOTES) ?>" placeholder="/auswertung">
  190. </label>
  191. </div>
  192. <p class="field-help">Leer lassen für den Domain-Root. Für Unterordner z. B. <code>/auswertung</code> eintragen.</p>
  193. </section>
  194. <section class="admin-grid">
  195. <section class="form-section">
  196. <h2>API</h2>
  197. <label>
  198. Bearer-Token
  199. <input type="text" name="bearer_token" value="<?= htmlspecialchars((string) ($config['api']['bearer_token'] ?? ''), ENT_QUOTES) ?>" required>
  200. </label>
  201. <p class="field-help">ESP32-Clients senden dieses Token im Header <code>Authorization: Bearer ...</code>.</p>
  202. </section>
  203. <section class="form-section">
  204. <h2>Adminzugang</h2>
  205. <div class="field-grid">
  206. <label>
  207. Benutzername
  208. <input type="text" name="admin_username" value="<?= htmlspecialchars((string) ($config['admin']['username'] ?? ''), ENT_QUOTES) ?>" required>
  209. </label>
  210. <label>
  211. Neues Passwort
  212. <input type="password" name="admin_password" placeholder="Leer lassen, um es nicht zu ändern">
  213. </label>
  214. </div>
  215. </section>
  216. </section>
  217. <section class="form-section">
  218. <div class="panel__header">
  219. <div>
  220. <h2>Webhooks</h2>
  221. <p>Mehrere Ziele sind möglich. Header können als JSON hinterlegt werden.</p>
  222. <p>Webhooks sind Alert-Ziele, wenn eine Alarmstufe bei einem Fach erreicht wurde</p>
  223. </div>
  224. <button class="button button--secondary" type="button" data-add="webhook">Webhook hinzufügen</button>
  225. </div>
  226. <div class="repeat-stack" id="webhook-stack">
  227. <?php foreach (($config['alerts']['webhooks'] ?? []) as $index => $webhook): ?>
  228. <?= renderWebhookRow((int) $index, $webhook) ?>
  229. <?php endforeach; ?>
  230. </div>
  231. </section>
  232. <section class="form-section">
  233. <div class="panel__header">
  234. <div>
  235. <h2>Email-Empfänger</h2>
  236. <p>Emails werden über die Serverkonfiguration von <code>mail()</code> versendet.</p>
  237. </div>
  238. <button class="button button--secondary" type="button" data-add="email">Email-Empfänger hinzufügen</button>
  239. </div>
  240. <div class="repeat-stack" id="email-stack">
  241. <?php foreach (($config['alerts']['emails'] ?? []) as $index => $email): ?>
  242. <?= renderEmailRow((int) $index, $email) ?>
  243. <?php endforeach; ?>
  244. </div>
  245. </section>
  246. <section class="form-section">
  247. <div class="panel__header">
  248. <div>
  249. <h2>Automaten und Fächer</h2>
  250. <p>Jeder Automat enthält beliebig viele Fächer, die jeweils genau einem Sensor zugeordnet sind.</p>
  251. </div>
  252. <button class="button button--secondary" type="button" data-add="machine">Automat hinzufügen</button>
  253. </div>
  254. <div class="repeat-stack" id="machine-stack">
  255. <?php foreach (($config['machines'] ?? []) as $machineIndex => $machine): ?>
  256. <?= renderMachineRow((int) $machineIndex, $machine) ?>
  257. <?php endforeach; ?>
  258. </div>
  259. </section>
  260. <div class="section-actions">
  261. <button class="button button--primary" type="submit">Konfiguration speichern</button>
  262. </div>
  263. </form>
  264. </section>
  265. </main>
  266. <template id="tpl-webhook"><?= renderWebhookRow('__INDEX__', []) ?></template>
  267. <template id="tpl-email"><?= renderEmailRow('__INDEX__', []) ?></template>
  268. <template id="tpl-machine"><?= renderMachineRow('__MACHINE__', []) ?></template>
  269. <template id="tpl-slot"><?= renderSlotRow('__MACHINE__', '__SLOT__', []) ?></template>
  270. <script>
  271. const webhookStack = document.getElementById('webhook-stack');
  272. const emailStack = document.getElementById('email-stack');
  273. const machineStack = document.getElementById('machine-stack');
  274. const renderTemplate = (id, replacements) => {
  275. let html = document.getElementById(id).innerHTML;
  276. Object.entries(replacements).forEach(([needle, value]) => {
  277. html = html.replaceAll(needle, value);
  278. });
  279. return html;
  280. };
  281. const countChildren = (node, selector) => node.querySelectorAll(selector).length;
  282. document.addEventListener('click', (event) => {
  283. const addType = event.target.getAttribute('data-add');
  284. const removeType = event.target.getAttribute('data-remove');
  285. if (addType === 'webhook') {
  286. const index = countChildren(webhookStack, '[data-webhook-row]');
  287. webhookStack.insertAdjacentHTML('beforeend', renderTemplate('tpl-webhook', { '__INDEX__': index }));
  288. }
  289. if (addType === 'email') {
  290. const index = countChildren(emailStack, '[data-email-row]');
  291. emailStack.insertAdjacentHTML('beforeend', renderTemplate('tpl-email', { '__INDEX__': index }));
  292. }
  293. if (addType === 'machine') {
  294. const index = countChildren(machineStack, '[data-machine-row]');
  295. machineStack.insertAdjacentHTML('beforeend', renderTemplate('tpl-machine', { '__MACHINE__': index, '__SLOT__': 0 }));
  296. }
  297. if (addType === 'slot') {
  298. const machineIndex = event.target.getAttribute('data-machine-index');
  299. const container = document.querySelector(`[data-slot-stack="${machineIndex}"]`);
  300. const slotIndex = countChildren(container, '[data-slot-row]');
  301. container.insertAdjacentHTML('beforeend', renderTemplate('tpl-slot', {
  302. '__MACHINE__': machineIndex,
  303. '__SLOT__': slotIndex
  304. }));
  305. }
  306. if (removeType) {
  307. const row = event.target.closest(`[data-${removeType}-row]`);
  308. if (row) {
  309. row.remove();
  310. }
  311. }
  312. });
  313. </script>
  314. </body>
  315. </html>
  316. <?php
  317. }
  318. function renderWebhookRow(int|string $index, array $webhook): string
  319. {
  320. $headers = $webhook['headers'] ?? [];
  321. $headerJson = json_encode($headers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  322. ob_start();
  323. ?>
  324. <div class="repeat-card" data-webhook-row>
  325. <div class="panel__header">
  326. <h4>Webhook</h4>
  327. <button class="button button--danger" type="button" data-remove="webhook">Entfernen</button>
  328. </div>
  329. <div class="field-grid">
  330. <label>
  331. ID
  332. <input type="text" name="webhooks[<?= $index ?>][id]" value="<?= htmlspecialchars((string) ($webhook['id'] ?? ''), ENT_QUOTES) ?>">
  333. </label>
  334. <label>
  335. Name
  336. <input type="text" name="webhooks[<?= $index ?>][name]" value="<?= htmlspecialchars((string) ($webhook['name'] ?? ''), ENT_QUOTES) ?>">
  337. </label>
  338. <label>
  339. URL
  340. <input type="url" name="webhooks[<?= $index ?>][url]" value="<?= htmlspecialchars((string) ($webhook['url'] ?? ''), ENT_QUOTES) ?>">
  341. </label>
  342. <label>
  343. Aktiv
  344. <select name="webhooks[<?= $index ?>][enabled]">
  345. <option value="1" <?= !empty($webhook['enabled']) ? 'selected' : '' ?>>Ja</option>
  346. <option value="0" <?= empty($webhook['enabled']) ? 'selected' : '' ?>>Nein</option>
  347. </select>
  348. </label>
  349. </div>
  350. <label>
  351. Header als JSON-Objekt
  352. <textarea name="webhooks[<?= $index ?>][headers_json]"><?= htmlspecialchars((string) $headerJson, ENT_QUOTES) ?></textarea>
  353. </label>
  354. </div>
  355. <?php
  356. return (string) ob_get_clean();
  357. }
  358. function renderEmailRow(int|string $index, array $email): string
  359. {
  360. ob_start();
  361. ?>
  362. <div class="repeat-card" data-email-row>
  363. <div class="panel__header">
  364. <h4>Email-Empfänger</h4>
  365. <button class="button button--danger" type="button" data-remove="email">Entfernen</button>
  366. </div>
  367. <div class="field-grid">
  368. <label>
  369. ID
  370. <input type="text" name="emails[<?= $index ?>][id]" value="<?= htmlspecialchars((string) ($email['id'] ?? ''), ENT_QUOTES) ?>">
  371. </label>
  372. <label>
  373. Name
  374. <input type="text" name="emails[<?= $index ?>][name]" value="<?= htmlspecialchars((string) ($email['name'] ?? ''), ENT_QUOTES) ?>">
  375. </label>
  376. <label>
  377. Adresse
  378. <input type="email" name="emails[<?= $index ?>][address]" value="<?= htmlspecialchars((string) ($email['address'] ?? ''), ENT_QUOTES) ?>">
  379. </label>
  380. <label>
  381. Aktiv
  382. <select name="emails[<?= $index ?>][enabled]">
  383. <option value="1" <?= !empty($email['enabled']) ? 'selected' : '' ?>>Ja</option>
  384. <option value="0" <?= empty($email['enabled']) ? 'selected' : '' ?>>Nein</option>
  385. </select>
  386. </label>
  387. </div>
  388. </div>
  389. <?php
  390. return (string) ob_get_clean();
  391. }
  392. function renderMachineRow(int|string $machineIndex, array $machine): string
  393. {
  394. ob_start();
  395. ?>
  396. <div class="repeat-card" data-machine-row>
  397. <div class="panel__header">
  398. <div>
  399. <h4>Automat</h4>
  400. <p>Fächer können direkt darunter verwaltet werden.</p>
  401. </div>
  402. <div class="inline-actions">
  403. <button class="button button--secondary" type="button" data-add="slot" data-machine-index="<?= htmlspecialchars((string) $machineIndex, ENT_QUOTES) ?>">Fach hinzufügen</button>
  404. <button class="button button--danger" type="button" data-remove="machine">Automat entfernen</button>
  405. </div>
  406. </div>
  407. <div class="field-grid">
  408. <label>
  409. ID
  410. <input type="text" name="machines[<?= $machineIndex ?>][id]" value="<?= htmlspecialchars((string) ($machine['id'] ?? ''), ENT_QUOTES) ?>">
  411. </label>
  412. <label>
  413. Name
  414. <input type="text" name="machines[<?= $machineIndex ?>][name]" value="<?= htmlspecialchars((string) ($machine['name'] ?? ''), ENT_QUOTES) ?>">
  415. </label>
  416. <label>
  417. Standort
  418. <input type="text" name="machines[<?= $machineIndex ?>][location]" value="<?= htmlspecialchars((string) ($machine['location'] ?? ''), ENT_QUOTES) ?>">
  419. </label>
  420. </div>
  421. <div class="repeat-stack" data-slot-stack="<?= htmlspecialchars((string) $machineIndex, ENT_QUOTES) ?>">
  422. <?php foreach (($machine['slots'] ?? []) as $slotIndex => $slot): ?>
  423. <?= renderSlotRow($machineIndex, (int) $slotIndex, $slot) ?>
  424. <?php endforeach; ?>
  425. </div>
  426. </div>
  427. <?php
  428. return (string) ob_get_clean();
  429. }
  430. function renderSlotRow(int|string $machineIndex, int|string $slotIndex, array $slot): string
  431. {
  432. $webhookIds = implode(',', $slot['webhook_ids'] ?? []);
  433. $emailIds = implode(',', $slot['email_ids'] ?? []);
  434. ob_start();
  435. ?>
  436. <div class="slot-editor" data-slot-row>
  437. <div class="panel__header">
  438. <h5>Fach</h5>
  439. <button class="button button--danger" type="button" data-remove="slot">Fach entfernen</button>
  440. </div>
  441. <div class="field-grid">
  442. <label>
  443. Sensor-ID
  444. <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][sensor_id]" value="<?= htmlspecialchars((string) ($slot['sensor_id'] ?? ''), ENT_QUOTES) ?>">
  445. </label>
  446. <label>
  447. Label
  448. <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][label]" value="<?= htmlspecialchars((string) ($slot['label'] ?? ''), ENT_QUOTES) ?>">
  449. </label>
  450. <label>
  451. Produktname
  452. <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][product_name]" value="<?= htmlspecialchars((string) ($slot['product_name'] ?? ''), ENT_QUOTES) ?>">
  453. </label>
  454. <label>
  455. Voll-Distanz in mm
  456. <input type="number" step="0.01" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][full_distance_mm]" value="<?= htmlspecialchars((string) ($slot['full_distance_mm'] ?? ''), ENT_QUOTES) ?>">
  457. </label>
  458. <label>
  459. Leer-Distanz in mm
  460. <input type="number" step="0.01" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][empty_distance_mm]" value="<?= htmlspecialchars((string) ($slot['empty_distance_mm'] ?? ''), ENT_QUOTES) ?>">
  461. </label>
  462. <label>
  463. Distanz pro Flasche
  464. <input type="number" step="0.01" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][distance_per_unit]" value="<?= htmlspecialchars((string) ($slot['distance_per_unit'] ?? ''), ENT_QUOTES) ?>">
  465. </label>
  466. <label>
  467. Alarm unter Bestand
  468. <input type="number" step="1" min="0" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][alert_below_units]" value="<?= htmlspecialchars((string) ($slot['alert_below_units'] ?? ''), ENT_QUOTES) ?>">
  469. </label>
  470. <label>
  471. Webhook-IDs
  472. <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][webhook_ids]" value="<?= htmlspecialchars((string) $webhookIds, ENT_QUOTES) ?>" placeholder="ops,lager">
  473. </label>
  474. <label>
  475. Email-IDs
  476. <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][email_ids]" value="<?= htmlspecialchars((string) $emailIds, ENT_QUOTES) ?>" placeholder="lager,service">
  477. </label>
  478. </div>
  479. </div>
  480. <?php
  481. return (string) ob_get_clean();
  482. }
  483. function normalizeWebhooks(array $rows): array
  484. {
  485. $items = [];
  486. foreach ($rows as $row) {
  487. $id = trim((string) ($row['id'] ?? ''));
  488. $url = trim((string) ($row['url'] ?? ''));
  489. if ($id === '' && $url === '') {
  490. continue;
  491. }
  492. $headers = json_decode((string) ($row['headers_json'] ?? '{}'), true);
  493. $items[] = [
  494. 'id' => $id,
  495. 'name' => trim((string) ($row['name'] ?? '')),
  496. 'url' => $url,
  497. 'enabled' => (string) ($row['enabled'] ?? '1') === '1',
  498. 'headers' => is_array($headers) ? $headers : [],
  499. ];
  500. }
  501. return array_values($items);
  502. }
  503. function normalizeEmails(array $rows): array
  504. {
  505. $items = [];
  506. foreach ($rows as $row) {
  507. $id = trim((string) ($row['id'] ?? ''));
  508. $address = trim((string) ($row['address'] ?? ''));
  509. if ($id === '' && $address === '') {
  510. continue;
  511. }
  512. $items[] = [
  513. 'id' => $id,
  514. 'name' => trim((string) ($row['name'] ?? '')),
  515. 'address' => $address,
  516. 'enabled' => (string) ($row['enabled'] ?? '1') === '1',
  517. ];
  518. }
  519. return array_values($items);
  520. }
  521. function normalizeMachines(array $rows): array
  522. {
  523. $items = [];
  524. foreach ($rows as $row) {
  525. $id = trim((string) ($row['id'] ?? ''));
  526. $name = trim((string) ($row['name'] ?? ''));
  527. if ($id === '' && $name === '') {
  528. continue;
  529. }
  530. $slots = [];
  531. foreach (($row['slots'] ?? []) as $slot) {
  532. $sensorId = trim((string) ($slot['sensor_id'] ?? ''));
  533. if ($sensorId === '') {
  534. continue;
  535. }
  536. $slots[] = [
  537. 'sensor_id' => $sensorId,
  538. 'label' => trim((string) ($slot['label'] ?? $sensorId)),
  539. 'product_name' => trim((string) ($slot['product_name'] ?? '')),
  540. 'full_distance_mm' => (float) ($slot['full_distance_mm'] ?? 0),
  541. 'empty_distance_mm' => (float) ($slot['empty_distance_mm'] ?? 0),
  542. 'distance_per_unit' => max(0.01, (float) ($slot['distance_per_unit'] ?? 1)),
  543. 'alert_below_units' => max(0, (int) ($slot['alert_below_units'] ?? 0)),
  544. 'webhook_ids' => parseIdList((string) ($slot['webhook_ids'] ?? '')),
  545. 'email_ids' => parseIdList((string) ($slot['email_ids'] ?? '')),
  546. ];
  547. }
  548. $items[] = [
  549. 'id' => $id,
  550. 'name' => $name,
  551. 'location' => trim((string) ($row['location'] ?? '')),
  552. 'slots' => $slots,
  553. ];
  554. }
  555. return array_values($items);
  556. }
  557. function parseIdList(string $value): array
  558. {
  559. $parts = array_map('trim', explode(',', $value));
  560. return array_values(array_filter($parts, static fn (string $item): bool => $item !== ''));
  561. }
  562. function encodeConfigForEditor(array $config): string
  563. {
  564. $json = json_encode(
  565. $config,
  566. JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
  567. );
  568. return $json === false ? '{}' : $json;
  569. }