OpenNIT

Quelltext des aktuellen Standes

Fassung 1.1.1

webhooks.php

7 KB
<?php
$this->layout('layouts/main', [
    'page_title'    => $page_title ?? 'Meine Webhooks',
    'current_route' => 'workflows',
    'full_width'    => true,
    'extra_css'     => ['/assets/css/workflows.css'],
]);
$hooks  = $hooks ?? [];
$events = $events ?? [];
$active = 'webhooks';
include __DIR__ . '/_nav.php';
?>
<div class="page-body">
    <?php include __DIR__ . '/_subnav.php'; ?>

    <p class="text-muted">
        <?= $this->t('Persönliche Webhooks benachrichtigen ein externes System (z. B. n8n, Make, Slack-Workflow) über Ereignisse, die <strong>dich</strong> betreffen. Bei jedem Ereignis sendet OpenNIT einen <code>POST</code> mit JSON-Body. Ist ein Secret gesetzt, wird der Body per HMAC-SHA256 signiert (Header <code>X-OpenNIT-Signature: sha256=…</code>).') ?>
    </p>

    <div class="wf-detail-grid">
        <div class="detail-card">
            <div class="detail-card-header"><span class="detail-card-title"><?= $this->te('Eingerichtete Webhooks') ?></span></div>
            <div class="detail-card-body">
                <?php if (empty($hooks)): ?>
                    <div class="empty-state"><i class="fas fa-bolt"></i><p><?= $this->te('Noch keine Webhooks eingerichtet.') ?></p></div>
                <?php else: ?>
                <div class="table-wrapper">
                    <table class="data-table">
                        <thead><tr><th scope="col"><?= $this->te('Bezeichnung / URL') ?></th><th scope="col"><?= $this->te('Ereignisse') ?></th><th scope="col"><?= $this->te('Status') ?></th><th scope="col"><?= $this->te('Letzte Zustellung') ?></th><th scope="col"></th></tr></thead>
                        <tbody>
                        <?php foreach ($hooks as $h): ?>
                            <?php $subs = $h['events_json'] ? (json_decode($h['events_json'], true) ?: []) : []; ?>
                            <tr>
                                <td>
                                    <?php if (!empty($h['label'])): ?><strong><?= $this->e($h['label']) ?></strong><br><?php endif; ?>
                                    <span class="text-muted" style="word-break:break-all;font-size:.82rem;"><?= $this->e($h['url']) ?></span>
                                    <?php if (!empty($h['secret'])): ?> <span class="badge" title="<?= $this->te('HMAC-signiert') ?>"><i class="fas fa-lock"></i> <?= $this->te('signiert') ?></span><?php endif; ?>
                                </td>
                                <td style="font-size:.82rem;">
                                    <?= empty($subs) ? 'Alle' : $this->e(implode(', ', array_map(fn($e) => $events[$e] ?? $e, $subs))) ?>
                                </td>
                                <td>
                                    <?php if ((int)$h['enabled'] === 1): ?>
                                        <span class="status-pill status-pill--success"><?= $this->te('Aktiv') ?></span>
                                    <?php else: ?>
                                        <span class="status-pill"><?= $this->te('Pausiert') ?></span>
                                    <?php endif; ?>
                                    <?php if (!empty($h['last_status'])): ?><br><span class="text-muted" style="font-size:.78rem;"><?= $this->e($h['last_status']) ?></span><?php endif; ?>
                                </td>
                                <td style="font-size:.82rem;"><?= $h['last_delivered_at'] ? $this->date($h['last_delivered_at'], 'd.m.Y H:i') : '<span class="text-muted">–</span>' ?></td>
                                <td style="white-space:nowrap;">
                                    <form method="post" action="/antraege/webhooks/<?= (int)$h['id'] ?>/test" style="display:inline;">
                                        <input type="hidden" name="_csrf" value="<?= $this->csrf() ?>">
                                        <button class="btn btn-xs btn-secondary" type="submit" title="<?= $this->te('Test senden') ?>"><i class="fas fa-paper-plane"></i></button>
                                    </form>
                                    <form method="post" action="/antraege/webhooks/<?= (int)$h['id'] ?>/toggle" style="display:inline;">
                                        <input type="hidden" name="_csrf" value="<?= $this->csrf() ?>">
                                        <button class="btn btn-xs btn-secondary" type="submit" title="<?= (int)$h['enabled'] === 1 ? 'Pausieren' : 'Aktivieren' ?>"><i class="fas fa-<?= (int)$h['enabled'] === 1 ? 'pause' : 'play' ?>"></i></button>
                                    </form>
                                    <form method="post" action="/antraege/webhooks/<?= (int)$h['id'] ?>/delete" style="display:inline;" onsubmit="return confirm('Webhook wirklich entfernen?');">
                                        <input type="hidden" name="_csrf" value="<?= $this->csrf() ?>">
                                        <button class="btn btn-xs btn-danger" type="submit" title="<?= $this->te('Entfernen') ?>"><i class="fas fa-trash"></i></button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <div class="detail-card">
            <div class="detail-card-header"><span class="detail-card-title"><?= $this->te('Neuer Webhook') ?></span></div>
            <div class="detail-card-body">
                <form method="post" action="/antraege/webhooks">
                    <input type="hidden" name="_csrf" value="<?= $this->csrf() ?>">
                    <div class="form-group">
                        <label><?= $this->te('Bezeichnung') ?></label>
                        <input class="form-control form-control-sm" type="text" name="label" placeholder="<?= $this->te('z. B. Mein n8n-Flow') ?>">
                    </div>
                    <div class="form-group">
                        <label><?= $this->te('Ziel-URL') ?> <span class="text-danger">*</span></label>
                        <input class="form-control form-control-sm" type="url" name="url" placeholder="https://…" required>
                    </div>
                    <div class="form-group">
                        <label><?= $this->te('Secret (optional, für HMAC-Signatur)') ?></label>
                        <input class="form-control form-control-sm" type="text" name="secret" placeholder="<?= $this->te('leer = ohne Signatur') ?>">
                    </div>
                    <div class="form-group">
                        <label><?= $this->te('Ereignisse (keins gewählt = alle)') ?></label>
                        <?php foreach ($events as $ev => $label): ?>
                            <label style="display:block;font-weight:normal;"><input type="checkbox" name="events[]" value="<?= $this->e($ev) ?>"> <?= $this->e($label) ?> <code style="font-size:.75rem;"><?= $this->e($ev) ?></code></label>
                        <?php endforeach; ?>
                    </div>
                    <button class="btn btn-primary" type="submit"><i class="fas fa-plus"></i> <?= $this->te('Webhook anlegen') ?></button>
                </form>
            </div>
        </div>
    </div>
</div>