version 0.8.0
This commit is contained in:
parent
f22007003a
commit
e5ca9ef194
95 changed files with 5601 additions and 4042 deletions
159
app/Controllers/Api/AssetController.php
Normal file
159
app/Controllers/Api/AssetController.php
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Globale, admin-verwaltete Spiel-Assets: Sounds (Audio), Reaktionen
|
||||
* (Emoji ODER Bild) und Sticker (Bild). Lesen = eingeloggt; Anlegen/Löschen = Admin.
|
||||
*/
|
||||
final class AssetController
|
||||
{
|
||||
/** Alle Assets fürs Spiel + die Admin-Verwaltung. */
|
||||
public function assets(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
if (empty($_SESSION['user_id'])) {
|
||||
respond(['error' => 'Bitte erst einloggen.'], 401);
|
||||
}
|
||||
respond([
|
||||
'ok' => true,
|
||||
'sounds' => $pdo->query('SELECT id, label, file FROM gtl_sounds ORDER BY id ASC')->fetchAll(),
|
||||
'reactions' => $pdo->query('SELECT id, emoji, image FROM gtl_reactions ORDER BY id ASC')->fetchAll(),
|
||||
'stickers' => $pdo->query('SELECT id, label, file FROM gtl_stickers ORDER BY id ASC')->fetchAll(),
|
||||
]);
|
||||
}
|
||||
|
||||
// ── Sounds ────────────────────────────────────────────────
|
||||
public function sound_create(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
AuthService::requireAdmin($pdo);
|
||||
$b = input();
|
||||
$label = trim((string) ($b['label'] ?? ''));
|
||||
if ($label === '') respond(['error' => 'Label erforderlich.'], 400);
|
||||
$fn = $this->saveAudio((string) ($b['fileBase64'] ?? ''));
|
||||
$pdo->prepare('INSERT INTO gtl_sounds (label, file, created_at) VALUES (?,?,?)')
|
||||
->execute([mb_substr($label, 0, 60), '/uploads/sounds/' . $fn, time()]);
|
||||
respond(['ok' => true]);
|
||||
}
|
||||
|
||||
public function sound_delete(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
AuthService::requireAdmin($pdo);
|
||||
$id = (int) (input()['id'] ?? 0);
|
||||
$st = $pdo->prepare('SELECT file FROM gtl_sounds WHERE id=?');
|
||||
$st->execute([$id]);
|
||||
$this->unlinkUpload((string) ($st->fetchColumn() ?: ''));
|
||||
$pdo->prepare('DELETE FROM gtl_sounds WHERE id=?')->execute([$id]);
|
||||
respond(['ok' => true]);
|
||||
}
|
||||
|
||||
// ── Reaktionen (Emoji ODER Bild) ──────────────────────────
|
||||
public function reaction_create(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
AuthService::requireAdmin($pdo);
|
||||
$b = input();
|
||||
$emoji = trim((string) ($b['emoji'] ?? ''));
|
||||
$b64 = (string) ($b['imageBase64'] ?? '');
|
||||
if ($emoji !== '') {
|
||||
$pdo->prepare('INSERT INTO gtl_reactions (emoji, created_at) VALUES (?,?)')
|
||||
->execute([mb_substr($emoji, 0, 8), time()]);
|
||||
} elseif ($b64 !== '') {
|
||||
$fn = $this->saveImage($b64, 'reactions');
|
||||
$pdo->prepare('INSERT INTO gtl_reactions (image, created_at) VALUES (?,?)')
|
||||
->execute(['/uploads/reactions/' . $fn, time()]);
|
||||
} else {
|
||||
respond(['error' => 'Emoji oder Bild angeben.'], 400);
|
||||
}
|
||||
respond(['ok' => true]);
|
||||
}
|
||||
|
||||
public function reaction_delete(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
AuthService::requireAdmin($pdo);
|
||||
$id = (int) (input()['id'] ?? 0);
|
||||
$st = $pdo->prepare('SELECT image FROM gtl_reactions WHERE id=?');
|
||||
$st->execute([$id]);
|
||||
$this->unlinkUpload((string) ($st->fetchColumn() ?: ''));
|
||||
$pdo->prepare('DELETE FROM gtl_reactions WHERE id=?')->execute([$id]);
|
||||
respond(['ok' => true]);
|
||||
}
|
||||
|
||||
// ── Sticker (Bild) ────────────────────────────────────────
|
||||
public function sticker_create(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
AuthService::requireAdmin($pdo);
|
||||
$b = input();
|
||||
$label = trim((string) ($b['label'] ?? ''));
|
||||
if ($label === '') respond(['error' => 'Label erforderlich.'], 400);
|
||||
$fn = $this->saveImage((string) ($b['imageBase64'] ?? ''), 'stickers');
|
||||
$pdo->prepare('INSERT INTO gtl_stickers (label, file, created_at) VALUES (?,?,?)')
|
||||
->execute([mb_substr($label, 0, 60), '/uploads/stickers/' . $fn, time()]);
|
||||
respond(['ok' => true]);
|
||||
}
|
||||
|
||||
public function sticker_delete(): void
|
||||
{
|
||||
$pdo = Database::pdo();
|
||||
AuthService::requireAdmin($pdo);
|
||||
$id = (int) (input()['id'] ?? 0);
|
||||
$st = $pdo->prepare('SELECT file FROM gtl_stickers WHERE id=?');
|
||||
$st->execute([$id]);
|
||||
$this->unlinkUpload((string) ($st->fetchColumn() ?: ''));
|
||||
$pdo->prepare('DELETE FROM gtl_stickers WHERE id=?')->execute([$id]);
|
||||
respond(['ok' => true]);
|
||||
}
|
||||
|
||||
// ── Helfer ────────────────────────────────────────────────
|
||||
private function saveImage(string $b64, string $subdir): string
|
||||
{
|
||||
if (!preg_match('#^data:image/(png|gif|jpeg|webp);base64,#', $b64, $m)) {
|
||||
respond(['error' => 'Nur PNG, GIF, JPEG oder WebP erlaubt.'], 400);
|
||||
}
|
||||
$ext = $m[1] === 'jpeg' ? 'jpg' : $m[1];
|
||||
$raw = base64_decode(preg_replace('#^data:image/[^;]+;base64,#', '', $b64) ?? '');
|
||||
if ($raw === false || strlen($raw) > 800 * 1024) {
|
||||
respond(['error' => 'Bild zu groß (max 800 KB).'], 400);
|
||||
}
|
||||
$tmp = tempnam(sys_get_temp_dir(), 'gtl_');
|
||||
file_put_contents($tmp, $raw);
|
||||
$info = @getimagesize($tmp);
|
||||
unlink($tmp);
|
||||
if (!$info) respond(['error' => 'Ungültige Bilddatei.'], 400);
|
||||
|
||||
$dir = BSB_UPLOADS . '/' . $subdir;
|
||||
if (!is_dir($dir)) @mkdir($dir, 0755, true);
|
||||
$fn = bin2hex(random_bytes(6)) . '.' . $ext;
|
||||
file_put_contents($dir . '/' . $fn, $raw);
|
||||
return $fn;
|
||||
}
|
||||
|
||||
private function saveAudio(string $b64): string
|
||||
{
|
||||
if (!preg_match('#^data:audio/(mpeg|mp3|ogg|wav|x-wav);base64,#', $b64, $m)) {
|
||||
respond(['error' => 'Nur MP3, OGG oder WAV erlaubt.'], 400);
|
||||
}
|
||||
$ext = ($m[1] === 'mpeg') ? 'mp3' : (($m[1] === 'x-wav') ? 'wav' : $m[1]);
|
||||
$raw = base64_decode(preg_replace('#^data:audio/[^;]+;base64,#', '', $b64) ?? '');
|
||||
if ($raw === false || strlen($raw) > 1500 * 1024) {
|
||||
respond(['error' => 'Audio zu groß (max 1,5 MB).'], 400);
|
||||
}
|
||||
$dir = BSB_UPLOADS . '/sounds';
|
||||
if (!is_dir($dir)) @mkdir($dir, 0755, true);
|
||||
$fn = bin2hex(random_bytes(6)) . '.' . $ext;
|
||||
file_put_contents($dir . '/' . $fn, $raw);
|
||||
return $fn;
|
||||
}
|
||||
|
||||
/** Löscht eine hochgeladene Datei (nur innerhalb /uploads/, nie die Seed-Statics). */
|
||||
private function unlinkUpload(string $path): void
|
||||
{
|
||||
if ($path !== '' && str_starts_with($path, '/uploads/')) {
|
||||
$abs = BSB_BASE . '/public' . $path;
|
||||
if (is_file($abs)) @unlink($abs);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue