rinkplanner/hub/app/invites.php
torsten 9819311403 feat: Website und Hub zweisprachig (Deutsch/Englisch)
Website (rinkplanner.programmschmiede.de) liegt jetzt im Repo unter web/:
Sprachweiche nach Accept-Language (der User-Agent enthält keine Sprache),
/de/ und /en/ als eigene URLs mit hreflang, Cookie für die manuelle Wahl,
Texte in web/i18n.php, Markup in web/template.php. Deployment über
tools/deploy_web.sh (--sync, --check, --all); /index.html leitet weiter.

Hub: optionale englische Oberfläche über den DE/EN-Umschalter, Cookie
RHUB_LANG, Route /language. Alle Texte (Views, Meldungen, Validierung,
API-messages) stehen in hub/app/i18n_strings.php und laufen über t();
Datum und Zahlen folgen der Sprache. Die API behält ihre code-Felder und
antwortet ohne Cookie weiter deutsch.

deploy_hub.sh --check prüft jetzt Schlüsselparität und fehlende t()-Schlüssel.
Doku: docs/website.md neu, docs/hub.md und AGENTS.md ergänzt.
2026-11-12 22:38:32 +01:00

118 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
const HUB_INVITE_ALPHABET = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
function invite_normalize(string $code): string
{
$code = strtoupper(trim($code));
return preg_replace('/[^A-Z0-9]/', '', $code) ?? '';
}
function invite_hash(string $code): string
{
return hash('sha256', invite_normalize($code));
}
function invite_generate(): string
{
$groups = [];
for ($group = 0; $group < 4; $group++) {
$part = '';
for ($i = 0; $i < 4; $i++) {
$part .= HUB_INVITE_ALPHABET[random_int(0, strlen(HUB_INVITE_ALPHABET) - 1)];
}
$groups[] = $part;
}
return 'RHUB-' . implode('-', $groups);
}
function invite_create(string $note, string $email, int $expiresDays, int $createdBy): string
{
$code = invite_generate();
$expiresAt = null;
if ($expiresDays > 0) {
$expiresAt = utc_after(min($expiresDays, 365) * 86400);
}
$stmt = db()->prepare(
'INSERT INTO invites (code_hash, note, email, created_by, created_at, expires_at)
VALUES (?, ?, ?, ?, ?, ?)'
);
$stmt->execute([
invite_hash($code),
$note !== '' ? mb_substr($note, 0, 190, 'UTF-8') : null,
$email !== '' ? mb_substr($email, 0, 190, 'UTF-8') : null,
$createdBy,
utc_now(),
$expiresAt,
]);
audit('invite.create', 'invite', (int) db()->lastInsertId(), $note, $createdBy);
return $code;
}
function invite_find_valid(string $code): ?array
{
$normalized = invite_normalize($code);
if ($normalized === '') {
return null;
}
$stmt = db()->prepare(
'SELECT * FROM invites WHERE code_hash = ? AND used_by IS NULL AND used_at IS NULL
AND revoked_at IS NULL LIMIT 1'
);
$stmt->execute([invite_hash($normalized)]);
$row = $stmt->fetch();
if (!$row) {
return null;
}
if ($row['expires_at'] !== null && $row['expires_at'] <= utc_now()) {
return null;
}
return $row;
}
function invite_consume(int $inviteId, int $userId): bool
{
$stmt = db()->prepare(
'UPDATE invites SET used_by = ?, used_at = ?
WHERE id = ? AND used_by IS NULL AND used_at IS NULL AND revoked_at IS NULL
AND (expires_at IS NULL OR expires_at > ?)'
);
$stmt->execute([$userId, utc_now(), $inviteId, utc_now()]);
return $stmt->rowCount() === 1;
}
function invite_revoke(int $inviteId): bool
{
$stmt = db()->prepare(
'UPDATE invites SET revoked_at = ? WHERE id = ? AND used_by IS NULL AND revoked_at IS NULL'
);
$stmt->execute([utc_now(), $inviteId]);
return $stmt->rowCount() === 1;
}
function invites_list(): array
{
return db()->query(
"SELECT i.*, c.username AS creator_name, u.username AS used_name
FROM invites i
LEFT JOIN users c ON c.id = i.created_by
LEFT JOIN users u ON u.id = i.used_by
ORDER BY i.created_at DESC, i.id DESC
LIMIT 200"
)->fetchAll();
}
function invite_status(array $invite): string
{
if ($invite['revoked_at'] !== null) {
return 'revoked';
}
if ($invite['used_at'] !== null) {
return 'used';
}
if ($invite['expires_at'] !== null && $invite['expires_at'] <= utc_now()) {
return 'expired';
}
return 'open';
}