Neuer Helfer app/pushover.php meldet Selbstregistrierungen an den Betreiber. Der Aufruf sitzt in register.php statt in saas_register_tenant_owner(), damit die check-*-Skripte keine Pushes ausloesen, und wertet das Ergebnis nicht aus: ein fehlgeschlagener Push darf die Registrierung nicht abbrechen. Ohne PUSHOVER_TOKEN/PUSHOVER_USER ist die Funktion still deaktiviert. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
96 lines
3.4 KiB
PHP
96 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/bootstrap.php';
|
|
|
|
/**
|
|
* Betreiber-Benachrichtigungen ueber Pushover. Bewusst als reine
|
|
* "fire and forget"-Meldung ausgelegt: schlaegt der Versand fehl, darf das
|
|
* den ausloesenden Vorgang (z. B. eine Registrierung) niemals abbrechen -
|
|
* der Kunde haette sonst kein Konto, nur weil der Push nicht rausging.
|
|
*
|
|
* Konfiguration ueber PUSHOVER_TOKEN (App-Token) und PUSHOVER_USER
|
|
* (User- oder Group-Key) in env.local.php. Fehlt eines davon, ist die
|
|
* Funktion still deaktiviert - lokal und auf Staging passiert dann nichts.
|
|
*/
|
|
function pushover_is_configured(): bool
|
|
{
|
|
return (string)app_env('PUSHOVER_TOKEN', '') !== ''
|
|
&& (string)app_env('PUSHOVER_USER', '') !== '';
|
|
}
|
|
|
|
/**
|
|
* @return array{ok: bool, error: ?string}
|
|
*/
|
|
function pushover_notify(string $title, string $message, ?string $url = null, ?string $urlTitle = null): array
|
|
{
|
|
if (!pushover_is_configured()) {
|
|
return ['ok' => false, 'error' => 'Pushover ist nicht konfiguriert.'];
|
|
}
|
|
|
|
$params = [
|
|
'token' => (string)app_env('PUSHOVER_TOKEN', ''),
|
|
'user' => (string)app_env('PUSHOVER_USER', ''),
|
|
// Pushover kappt laengere Werte serverseitig; wir kuerzen selbst,
|
|
// damit die Meldung nicht mitten im Wort abbricht.
|
|
'title' => mb_substr($title, 0, 250),
|
|
'message' => mb_substr($message, 0, 1024),
|
|
];
|
|
if ($url !== null && $url !== '') {
|
|
$params['url'] = $url;
|
|
$params['url_title'] = $urlTitle ?? $url;
|
|
}
|
|
|
|
$context = stream_context_create([
|
|
'http' => [
|
|
'method' => 'POST',
|
|
'header' => 'Content-Type: application/x-www-form-urlencoded',
|
|
'content' => http_build_query($params),
|
|
'timeout' => 10,
|
|
'ignore_errors' => true,
|
|
],
|
|
]);
|
|
|
|
$responseBody = @file_get_contents('https://api.pushover.net/1/messages.json', false, $context);
|
|
$status = 0;
|
|
foreach ($http_response_header ?? [] as $header) {
|
|
if (preg_match('~^HTTP/\S+\s+(\d{3})~', $header, $m) === 1) {
|
|
$status = (int)$m[1];
|
|
}
|
|
}
|
|
|
|
if ($responseBody === false) {
|
|
error_log('Pushover: keine Antwort von api.pushover.net');
|
|
return ['ok' => false, 'error' => 'Keine Antwort von Pushover.'];
|
|
}
|
|
|
|
$data = json_decode($responseBody, true);
|
|
if (!is_array($data) || $status >= 400 || (int)($data['status'] ?? 0) !== 1) {
|
|
$errors = is_array($data['errors'] ?? null) ? implode('; ', $data['errors']) : 'HTTP ' . $status;
|
|
error_log('Pushover fehlgeschlagen: ' . $errors);
|
|
return ['ok' => false, 'error' => $errors];
|
|
}
|
|
|
|
return ['ok' => true, 'error' => null];
|
|
}
|
|
|
|
/**
|
|
* Meldung ueber eine abgeschlossene Selbstregistrierung. Wird nur aus
|
|
* register.php aufgerufen, nicht aus saas_register_tenant_owner() selbst -
|
|
* sonst wuerden die check-*-Skripte in scripts/ bei jedem Testlauf Pushes
|
|
* ausloesen.
|
|
*/
|
|
function pushover_notify_new_tenant(string $tenantName, string $tenantSlug, string $email, string $customerType): array
|
|
{
|
|
$type = $customerType === 'b2c' ? 'Privatkunde' : 'Geschäftskunde';
|
|
$baseUrl = rtrim((string)app_env('APP_BASE_URL', ''), '/');
|
|
|
|
return pushover_notify(
|
|
'Neue Registrierung: ' . $tenantName,
|
|
sprintf("%s (%s)\nKürzel: %s\nE-Mail: %s", $tenantName, $type, $tenantSlug, $email),
|
|
$baseUrl !== '' ? $baseUrl . '/backoffice.php' : null,
|
|
'Back-Office öffnen'
|
|
);
|
|
}
|