All checks were successful
CI / PHP Lint (push) Successful in 34s
CI / PHP Lint-1 (push) Successful in 39s
CI / Unit Tests & Static Analysis (push) Successful in 38s
CI / PHP Lint (pull_request) Successful in 41s
CI / PHP Lint-1 (pull_request) Successful in 49s
CI / Unit Tests & Static Analysis (pull_request) Successful in 41s
Die Display-Seiten hängen an öffentlichen Links, deshalb bekommen sie Regler, mit denen sich der Missbrauch begrenzen lässt, und ein paar Komfortfunktionen für den Betrieb: - Betriebszeiten je Seite (auch über Mitternacht hinweg); außerhalb zeigt die Seite einen Hinweis statt des Buttons - Optionale PIN von der Rezeption, geprüft mit hash_equals - Wartezeit je Gerät über ein Cookie, zusätzlich zum globalen Cooldown - Feste Sprache je Seite, sonst Browsersprache; Gäste können unten umschalten - Reporting zeigt die Ausgabe pro Display-Seite - Vorschau der Seite direkt in der Administration - Backup exportiert und importiert die neuen Felder mit Migration 0007, 16 neue Sprachschlüssel, 7 zusätzliche Tests (69 Tests, 152 Assertions). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
242 lines
8.3 KiB
PHP
242 lines
8.3 KiB
PHP
<?php
|
||
/**
|
||
* Öffentliche Display-Seiten ("Kiosk").
|
||
*
|
||
* Ein Kiosk gehört zu genau einer Site und ist über einen geheimen Link
|
||
* erreichbar. Gäste holen sich darüber mit einem Klick einen Zugangscode –
|
||
* ohne Anmeldung, aber begrenzt durch Tageslimit und Wartezeit.
|
||
*/
|
||
class Kiosk
|
||
{
|
||
/** Vorgaben für neue Kiosk-Seiten. */
|
||
public const DEFAULT_DAILY_LIMIT = 100;
|
||
public const DEFAULT_COOLDOWN = 20; // Sekunden zwischen zwei Codes
|
||
public const DEFAULT_DISPLAY_SECONDS = 90; // Anzeigedauer des Codes
|
||
|
||
/** Unrat-freier Zufallstoken für den öffentlichen Link. */
|
||
public static function newToken(): string
|
||
{
|
||
return bin2hex(random_bytes(16));
|
||
}
|
||
|
||
/** Token aus einer Anfrage säubern (Länge und Zeichen fest vorgegeben). */
|
||
public static function sanitizeToken(?string $token): string
|
||
{
|
||
$token = strtolower(trim((string)$token));
|
||
|
||
return preg_match('/^[0-9a-f]{32}$/', $token) ? $token : '';
|
||
}
|
||
|
||
/**
|
||
* Kiosk samt Site und Profil laden. Liefert null, wenn der Token nicht
|
||
* passt, der Kiosk deaktiviert ist oder die Site nicht mehr aktiv ist.
|
||
*/
|
||
public static function findByToken($db, string $token): ?array
|
||
{
|
||
$token = self::sanitizeToken($token);
|
||
if ($token === '') {
|
||
return null;
|
||
}
|
||
|
||
$row = $db->fetchOne(
|
||
"SELECT k.*, s.name AS site_name, s.is_active AS site_active,
|
||
t.name AS template_name, t.max_uses AS tpl_max_uses, t.expire_minutes AS tpl_expire_minutes,
|
||
t.qos_rate_max_down, t.qos_rate_max_up, t.qos_usage_quota
|
||
FROM kiosks k
|
||
INNER JOIN sites s ON s.id = k.site_id
|
||
LEFT JOIN voucher_templates t ON t.id = k.template_id
|
||
WHERE k.token = ? AND k.is_active = 1",
|
||
[$token]
|
||
);
|
||
|
||
if (!$row || (int)$row['site_active'] !== 1) {
|
||
return null;
|
||
}
|
||
|
||
return $row;
|
||
}
|
||
|
||
/** Wie viele Codes hat dieser Kiosk heute schon ausgegeben? */
|
||
public static function usedToday($db, int $kioskId): int
|
||
{
|
||
$row = $db->fetchOne(
|
||
"SELECT COUNT(*) AS c FROM vouchers WHERE kiosk_id = ? AND DATE(created_at) = CURDATE()",
|
||
[$kioskId]
|
||
);
|
||
|
||
return (int)($row['c'] ?? 0);
|
||
}
|
||
|
||
/**
|
||
* Liegt der aktuelle Zeitpunkt im Betriebsfenster der Display-Seite?
|
||
* Ohne gesetzte Zeiten gilt: immer offen. Ein Fenster über Mitternacht
|
||
* (z. B. 18:00–02:00) wird unterstützt.
|
||
*/
|
||
public static function withinSchedule(array $kiosk, ?int $now = null): bool
|
||
{
|
||
$from = trim((string)($kiosk['active_from'] ?? ''));
|
||
$to = trim((string)($kiosk['active_to'] ?? ''));
|
||
if ($from === '' || $to === '') {
|
||
return true;
|
||
}
|
||
|
||
$now = $now ?? time();
|
||
$minutes = (int)date('G', $now) * 60 + (int)date('i', $now);
|
||
$start = self::toMinutes($from);
|
||
$end = self::toMinutes($to);
|
||
|
||
if ($start === $end) {
|
||
return true; // 24 Stunden
|
||
}
|
||
if ($start < $end) {
|
||
return $minutes >= $start && $minutes < $end;
|
||
}
|
||
|
||
return $minutes >= $start || $minutes < $end; // über Mitternacht
|
||
}
|
||
|
||
private static function toMinutes(string $time): int
|
||
{
|
||
$parts = explode(':', $time);
|
||
|
||
return ((int)($parts[0] ?? 0)) * 60 + ((int)($parts[1] ?? 0));
|
||
}
|
||
|
||
/**
|
||
* Wartezeit dieses Geräts: das Display merkt sich den letzten Bezug in
|
||
* einem Cookie. Verhindert, dass eine Person am Handy im Takt der
|
||
* globalen Wartezeit das Tageskontingent leerzieht.
|
||
*
|
||
* @param int|null $lastSeen Zeitstempel aus dem Cookie
|
||
* @return int verbleibende Sekunden (0 = frei)
|
||
*/
|
||
public static function deviceCooldownLeft(array $kiosk, ?int $lastSeen, ?int $now = null): int
|
||
{
|
||
$cooldown = max(0, (int)($kiosk['device_cooldown_seconds'] ?? 0));
|
||
if ($cooldown === 0 || !$lastSeen) {
|
||
return 0;
|
||
}
|
||
|
||
$elapsed = ($now ?? time()) - $lastSeen;
|
||
if ($elapsed < 0 || $elapsed >= $cooldown) {
|
||
return 0;
|
||
}
|
||
|
||
return $cooldown - $elapsed;
|
||
}
|
||
|
||
/** Stimmt die eingegebene PIN? Ohne hinterlegte PIN entfällt die Prüfung. */
|
||
public static function pinMatches(array $kiosk, ?string $input): bool
|
||
{
|
||
$pin = trim((string)($kiosk['pin'] ?? ''));
|
||
if ($pin === '') {
|
||
return true;
|
||
}
|
||
|
||
return hash_equals($pin, trim((string)$input));
|
||
}
|
||
|
||
/**
|
||
* Darf gerade ein Code geholt werden?
|
||
*
|
||
* @return array{allowed:bool,reason:string,wait:int}
|
||
* reason: '' | 'closed' | 'cooldown' | 'daily_limit'
|
||
*/
|
||
public static function checkLimits($db, array $kiosk): array
|
||
{
|
||
if (!self::withinSchedule($kiosk)) {
|
||
return ['allowed' => false, 'reason' => 'closed', 'wait' => 0];
|
||
}
|
||
|
||
$cooldown = max(0, (int)$kiosk['cooldown_seconds']);
|
||
if ($cooldown > 0 && !empty($kiosk['last_used_at'])) {
|
||
$elapsed = time() - strtotime((string)$kiosk['last_used_at']);
|
||
if ($elapsed >= 0 && $elapsed < $cooldown) {
|
||
return ['allowed' => false, 'reason' => 'cooldown', 'wait' => $cooldown - $elapsed];
|
||
}
|
||
}
|
||
|
||
$limit = max(0, (int)$kiosk['daily_limit']);
|
||
if ($limit > 0 && self::usedToday($db, (int)$kiosk['id']) >= $limit) {
|
||
return ['allowed' => false, 'reason' => 'daily_limit', 'wait' => 0];
|
||
}
|
||
|
||
return ['allowed' => true, 'reason' => '', 'wait' => 0];
|
||
}
|
||
|
||
/** Nach erfolgreicher Ausgabe den Zeitstempel fortschreiben. */
|
||
public static function markUsed($db, int $kioskId): void
|
||
{
|
||
$db->execute("UPDATE kiosks SET last_used_at = NOW() WHERE id = ?", [$kioskId]);
|
||
}
|
||
|
||
/**
|
||
* Voucher-Eckdaten eines Kiosks: entweder aus dem verknüpften Profil
|
||
* oder aus den globalen Standardwerten.
|
||
*/
|
||
public static function voucherSettings($db, array $kiosk): array
|
||
{
|
||
$maxUses = (int)($kiosk['tpl_max_uses'] ?? 0);
|
||
$expire = (int)($kiosk['tpl_expire_minutes'] ?? 0);
|
||
|
||
if ($maxUses < 1) {
|
||
$maxUses = max(1, (int)$db->getSetting('default_max_uses', 1));
|
||
}
|
||
if ($expire < 1) {
|
||
$expire = max(1, (int)$db->getSetting('default_expire_minutes', 480));
|
||
}
|
||
|
||
return [
|
||
'max_uses' => $maxUses,
|
||
'expire_minutes' => $expire,
|
||
'qos' => [
|
||
'down' => max(0, (int)($kiosk['qos_rate_max_down'] ?? 0)),
|
||
'up' => max(0, (int)($kiosk['qos_rate_max_up'] ?? 0)),
|
||
'quota_mb' => max(0, (int)($kiosk['qos_usage_quota'] ?? 0)),
|
||
],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* Gestaltung einer Display-Seite: eigene Werte, sonst die des Systems.
|
||
*
|
||
* @return array{logo:string,background:string,overlay:float,accent:string,card:string}
|
||
*/
|
||
public static function appearance($db, array $kiosk): array
|
||
{
|
||
$accent = strtolower(trim((string)($kiosk['accent_color'] ?? '')));
|
||
if (!preg_match('/^#[0-9a-f]{6}$/', $accent)) {
|
||
$accent = '';
|
||
}
|
||
|
||
$overlay = (int)($kiosk['bg_overlay'] ?? 45);
|
||
$overlay = max(0, min(90, $overlay));
|
||
|
||
$logo = trim((string)($kiosk['logo_url'] ?? ''));
|
||
if ($logo === '' && $db) {
|
||
$logo = (string)$db->getSetting('logo_url', '');
|
||
}
|
||
|
||
return [
|
||
'logo' => $logo,
|
||
'background' => trim((string)($kiosk['background_url'] ?? '')),
|
||
'overlay' => (float)$overlay / 100, // immer float, auch bei 0
|
||
'accent' => $accent,
|
||
'card' => ($kiosk['card_style'] ?? 'light') === 'dark' ? 'dark' : 'light',
|
||
];
|
||
}
|
||
|
||
/** Öffentliche Adresse eines Kiosks. */
|
||
public static function publicUrl(string $token, string $baseUrl = ''): string
|
||
{
|
||
if ($baseUrl === '') {
|
||
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
|
||
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
|
||
$path = dirname($_SERVER['SCRIPT_NAME'] ?? '/', 2);
|
||
$path = $path === '/' || $path === '\\' ? '' : $path;
|
||
$baseUrl = $protocol . '://' . $host . $path;
|
||
}
|
||
|
||
return rtrim($baseUrl, '/') . '/kiosk.php?k=' . $token;
|
||
}
|
||
}
|