Web-Push mit VAPID und Erinnerungs-Endpoint
This commit is contained in:
parent
67a1614081
commit
b50267e4f0
24 changed files with 1703 additions and 50 deletions
130
app/pages.php
130
app/pages.php
|
|
@ -57,7 +57,7 @@ function page_handle(string $method, string $path): void
|
|||
|
||||
$user = auth_user();
|
||||
if ($user !== null && $user['must_change_password'] === 1
|
||||
&& !in_array($path, ['/account', '/logout'], true)
|
||||
&& !in_array($path, ['/account', '/logout', '/push/subscribe', '/push/unsubscribe', '/push/test'], true)
|
||||
) {
|
||||
flash('info', 'Bitte zuerst das eigene Passwort ändern.');
|
||||
redirect('/account');
|
||||
|
|
@ -127,6 +127,18 @@ function page_handle(string $method, string $path): void
|
|||
page_reminder_done($user);
|
||||
return;
|
||||
}
|
||||
if ($path === '/push/subscribe') {
|
||||
page_push_subscribe($user);
|
||||
return;
|
||||
}
|
||||
if ($path === '/push/unsubscribe') {
|
||||
page_push_unsubscribe($user);
|
||||
return;
|
||||
}
|
||||
if ($path === '/push/test') {
|
||||
page_push_test($user);
|
||||
return;
|
||||
}
|
||||
if ($path === '/account') {
|
||||
page_account($user, $method);
|
||||
return;
|
||||
|
|
@ -154,13 +166,41 @@ function health_check(): void
|
|||
|
||||
function cron_reminders(): void
|
||||
{
|
||||
header('Content-Type: text/plain; charset=utf-8');
|
||||
header('Cache-Control: no-store');
|
||||
$expected = (string) config('app.cron_token', '');
|
||||
$given = query_string('token');
|
||||
if ($expected === '' || $given === '' || !hash_equals($expected, $given)) {
|
||||
abort_page(403, 'Kein Zugriff.');
|
||||
http_response_code(403);
|
||||
echo "Forbidden\n";
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$push = push_dispatch_due();
|
||||
$mail = reminder_send_due();
|
||||
echo 'Drink it cold – Erinnerungslauf ' . fmt_datetime(utc_now()) . "\n";
|
||||
if (!push_is_configured()) {
|
||||
echo "Push: nicht konfiguriert (VAPID-Schlüssel fehlen).\n";
|
||||
} else {
|
||||
echo sprintf(
|
||||
"Push: %d gesendet, %d fehlgeschlagen, %d abgelaufene Abos entfernt, %d ohne Abo.\n",
|
||||
$push['sent'], $push['failed'], $push['removed'], $push['skipped_no_subscription']
|
||||
);
|
||||
}
|
||||
if (!$mail['enabled']) {
|
||||
echo "E-Mail: deaktiviert.\n";
|
||||
} else {
|
||||
echo sprintf("E-Mail: %d versendet, %d übersprungen.\n", $mail['sent'], $mail['skipped']);
|
||||
foreach ($mail['errors'] as $error) {
|
||||
echo 'E-Mail-Fehler: ' . $error . "\n";
|
||||
}
|
||||
}
|
||||
} catch (Throwable $error) {
|
||||
error_log('Cron-Erinnerungen: ' . $error->getMessage());
|
||||
http_response_code(500);
|
||||
echo "Fehler: Erinnerungslauf konnte nicht abgeschlossen werden.\n";
|
||||
}
|
||||
$result = reminder_send_due();
|
||||
json_response($result);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- Anmeldung */
|
||||
|
|
@ -715,6 +755,7 @@ function page_reminders(array $user): void
|
|||
'isAdmin' => ($user['role'] ?? '') === 'admin',
|
||||
'today' => $today,
|
||||
'mailEnabled' => (bool) config('app.mail_enabled', false),
|
||||
'pushEnabled' => push_is_configured(),
|
||||
], 'Erinnerungen');
|
||||
}
|
||||
|
||||
|
|
@ -785,5 +826,86 @@ function page_account(array $user, string $method): void
|
|||
render_page('account', [
|
||||
'errors' => $errors,
|
||||
'employee' => auth_employee_id($user) > 0 ? employee_find(auth_employee_id($user)) : null,
|
||||
'pushEnabled' => push_is_configured(),
|
||||
'pushEligible' => auth_employee_id($user) > 0,
|
||||
'pushPublicKey' => WebPush::publicKey(),
|
||||
'pushDeviceCount' => push_subscription_count((int) $user['id']),
|
||||
'pushSubscribeUrl' => url('/push/subscribe'),
|
||||
'pushUnsubscribeUrl' => url('/push/unsubscribe'),
|
||||
'pushTestUrl' => url('/push/test'),
|
||||
], 'Mein Konto');
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------- Web-Push */
|
||||
|
||||
function json_body(): array
|
||||
{
|
||||
$raw = file_get_contents('php://input', false, null, 0, 20000);
|
||||
if (is_string($raw) && $raw !== '') {
|
||||
$data = json_decode($raw, true);
|
||||
if (is_array($data)) {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
return $_POST;
|
||||
}
|
||||
|
||||
function page_push_subscribe(array $user): void
|
||||
{
|
||||
require_post();
|
||||
$body = json_body();
|
||||
$endpoint = trim((string) ($body['endpoint'] ?? ''));
|
||||
$keys = is_array($body['keys'] ?? null) ? $body['keys'] : [];
|
||||
$p256dh = trim((string) ($keys['p256dh'] ?? ($body['p256dh'] ?? '')));
|
||||
$auth = trim((string) ($keys['auth'] ?? ($body['auth'] ?? '')));
|
||||
|
||||
if (!WebPush::isValidEndpoint($endpoint) || !WebPush::isValidKeyMaterial($p256dh, $auth)) {
|
||||
json_response(['ok' => false, 'error' => 'Ungültiges Push-Abo.'], 422);
|
||||
}
|
||||
$saved = push_subscription_save(
|
||||
(int) $user['id'],
|
||||
$endpoint,
|
||||
$p256dh,
|
||||
$auth,
|
||||
(string) ($_SERVER['HTTP_USER_AGENT'] ?? '')
|
||||
);
|
||||
if (!$saved) {
|
||||
json_response(['ok' => false, 'error' => 'Abo konnte nicht gespeichert werden.'], 500);
|
||||
}
|
||||
json_response(['ok' => true, 'count' => push_subscription_count((int) $user['id'])]);
|
||||
}
|
||||
|
||||
function page_push_unsubscribe(array $user): void
|
||||
{
|
||||
require_post();
|
||||
$body = json_body();
|
||||
$endpoint = trim((string) ($body['endpoint'] ?? ''));
|
||||
if ($endpoint === '') {
|
||||
json_response(['ok' => false, 'error' => 'Geräte-Endpunkt fehlt.'], 422);
|
||||
}
|
||||
$removed = push_subscription_delete((int) $user['id'], $endpoint);
|
||||
json_response(['ok' => true, 'removed' => $removed, 'count' => push_subscription_count((int) $user['id'])]);
|
||||
}
|
||||
|
||||
function page_push_test(array $user): void
|
||||
{
|
||||
require_post();
|
||||
$body = json_body();
|
||||
$endpoint = trim((string) ($body['endpoint'] ?? ''));
|
||||
$last = (int) ($_SESSION['push_test_at'] ?? 0);
|
||||
if (time() - $last < 5) {
|
||||
json_response(['ok' => false, 'error' => 'Bitte kurz warten, bevor erneut getestet wird.'], 429);
|
||||
}
|
||||
if (!push_subscription_for_user_endpoint((int) $user['id'], $endpoint)) {
|
||||
json_response(['ok' => false, 'error' => 'Dieses Gerät ist nicht für Erinnerungen angemeldet.'], 409);
|
||||
}
|
||||
$_SESSION['push_test_at'] = time();
|
||||
$result = push_send_test((int) $user['id'], $endpoint);
|
||||
if ($result['sent'] > 0) {
|
||||
json_response(['ok' => true]);
|
||||
}
|
||||
if ($result['removed'] > 0) {
|
||||
json_response(['ok' => false, 'error' => 'Das Abo ist abgelaufen. Bitte Erinnerungen aus- und wieder einschalten.'], 410);
|
||||
}
|
||||
json_response(['ok' => false, 'error' => 'Push-Dienst nicht erreichbar.'], 502);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue