- PHP-App unter hub/ für rinkplanner-hub.programmschmiede.de - MariaDB-Schema, Argon2id, Sessions, CSRF und Ratenbegrenzung - Einladungscodes, Admin-Bereich und Kür-Pakete mit Audiodatei - JSON-API v1 mit Bearer-Tokens für die Godot-App - Deployment und Erstinstallation über tools/deploy_hub.sh - Doku und Credentials-Vorlage ergänzt
255 lines
9.1 KiB
PHP
255 lines
9.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/** JSON-API v1 für die Godot-App. Authentifizierung: Bearer-Token. */
|
|
|
|
function api_base_url(): string
|
|
{
|
|
$base = rtrim((string) config('app.public_url', ''), '/');
|
|
if ($base !== '') {
|
|
return $base;
|
|
}
|
|
$scheme = is_https() ? 'https' : 'http';
|
|
return $scheme . '://' . (string) ($_SERVER['HTTP_HOST'] ?? 'localhost');
|
|
}
|
|
|
|
function api_truthy(mixed $value): bool
|
|
{
|
|
return $value === true || $value === 1 || $value === '1' || $value === 'true';
|
|
}
|
|
|
|
function api_routine_payload(array $routine): array
|
|
{
|
|
$base = api_base_url();
|
|
$id = (int) $routine['id'];
|
|
$audio = null;
|
|
if (!empty($routine['audio_path'])) {
|
|
$audio = [
|
|
'name' => $routine['audio_original_name'],
|
|
'bytes' => (int) $routine['audio_bytes'],
|
|
'sha256' => $routine['audio_sha256'],
|
|
'mime' => $routine['audio_mime'],
|
|
'url' => $base . url('/api/v1/routines/' . $id . '/audio'),
|
|
];
|
|
}
|
|
return [
|
|
'id' => $id,
|
|
'title' => $routine['title'],
|
|
'skater_name' => $routine['skater_name'],
|
|
'category' => $routine['category'],
|
|
'description' => $routine['description'],
|
|
'music_title' => $routine['music_title'],
|
|
'music_artist' => $routine['music_artist'],
|
|
'status' => $routine['status'],
|
|
'revision' => $routine['revision'] !== null ? (int) $routine['revision'] : null,
|
|
'download_count' => (int) $routine['download_count'],
|
|
'created_at' => $routine['created_at'],
|
|
'updated_at' => $routine['updated_at'],
|
|
'owner' => [
|
|
'username' => $routine['owner_username'],
|
|
'display_name' => $routine['owner_display_name'],
|
|
],
|
|
'files' => [
|
|
'routine' => [
|
|
'name' => $routine['routine_original_name'],
|
|
'bytes' => (int) $routine['routine_bytes'],
|
|
'sha256' => $routine['routine_sha256'],
|
|
'url' => $base . url('/api/v1/routines/' . $id . '/download'),
|
|
],
|
|
'audio' => $audio,
|
|
],
|
|
'url' => $base . url('/routines/' . $id),
|
|
];
|
|
}
|
|
|
|
function api_routine_summary(array $row): array
|
|
{
|
|
return [
|
|
'id' => (int) $row['id'],
|
|
'title' => $row['title'],
|
|
'skater_name' => $row['skater_name'],
|
|
'category' => $row['category'],
|
|
'music_title' => $row['music_title'],
|
|
'music_artist' => $row['music_artist'],
|
|
'status' => $row['status'],
|
|
'revision' => $row['revision'] !== null ? (int) $row['revision'] : null,
|
|
'routine_bytes' => isset($row['routine_bytes']) ? (int) $row['routine_bytes'] : null,
|
|
'audio_bytes' => isset($row['audio_bytes']) ? (int) $row['audio_bytes'] : null,
|
|
'download_count' => (int) $row['download_count'],
|
|
'updated_at' => $row['updated_at'],
|
|
'owner' => [
|
|
'username' => $row['owner_username'],
|
|
'display_name' => $row['owner_display_name'],
|
|
],
|
|
];
|
|
}
|
|
|
|
function api_handle(string $method, string $path): void
|
|
{
|
|
$path = '/' . trim($path, '/');
|
|
|
|
if ($path === '/api/v1/tokens') {
|
|
if ($method !== 'POST') {
|
|
abort_api(405, 'method_not_allowed', 'Nur POST ist erlaubt.');
|
|
}
|
|
api_create_token();
|
|
return;
|
|
}
|
|
|
|
$user = api_require_user();
|
|
|
|
if ($path === '/api/v1/me') {
|
|
if ($method !== 'GET') {
|
|
abort_api(405, 'method_not_allowed', 'Nur GET ist erlaubt.');
|
|
}
|
|
json_response([
|
|
'id' => (int) $user['id'],
|
|
'username' => $user['username'],
|
|
'display_name' => $user['display_name'],
|
|
'role' => $user['role'],
|
|
'created_at' => $user['created_at'],
|
|
]);
|
|
}
|
|
|
|
if ($path === '/api/v1/routines') {
|
|
if ($method === 'GET') {
|
|
$query = mb_substr(query_string('q'), 0, 100, 'UTF-8');
|
|
$page = max(1, query_int('page', 1));
|
|
$perPage = max(1, min(50, query_int('per_page', 20)));
|
|
$mine = query_string('mine') === '1';
|
|
$result = routines_query($query, $page, $perPage, $mine ? (int) $user['id'] : null, $mine);
|
|
json_response([
|
|
'items' => array_map('api_routine_summary', $result['items']),
|
|
'total' => $result['total'],
|
|
'page' => $result['page'],
|
|
'pages' => $result['pages'],
|
|
]);
|
|
}
|
|
if ($method === 'POST') {
|
|
api_upload($user, null);
|
|
}
|
|
abort_api(405, 'method_not_allowed', 'Nur GET oder POST ist erlaubt.');
|
|
}
|
|
|
|
if (preg_match('#^/api/v1/routines/(\d+)$#', $path, $matches) === 1) {
|
|
api_routine((int) $matches[1], $method, $user);
|
|
return;
|
|
}
|
|
if (preg_match('#^/api/v1/routines/(\d+)/(download|audio)$#', $path, $matches) === 1) {
|
|
if ($method !== 'GET') {
|
|
abort_api(405, 'method_not_allowed', 'Nur GET ist erlaubt.');
|
|
}
|
|
$routine = api_fetch_visible((int) $matches[1], $user);
|
|
routine_send_file($routine, (string) $matches[2], $user, true);
|
|
}
|
|
if (preg_match('#^/api/v1/routines/(\d+)/revisions$#', $path, $matches) === 1) {
|
|
if ($method !== 'POST') {
|
|
abort_api(405, 'method_not_allowed', 'Nur POST ist erlaubt.');
|
|
}
|
|
api_upload($user, (int) $matches[1]);
|
|
return;
|
|
}
|
|
|
|
abort_api(404, 'not_found', 'Unbekannter API-Pfad.');
|
|
}
|
|
|
|
function api_create_token(): void
|
|
{
|
|
$input = json_input();
|
|
$login = trim((string) ($input['username'] ?? $input['login'] ?? ''));
|
|
$password = (string) ($input['password'] ?? '');
|
|
$name = trim((string) ($input['name'] ?? 'App'));
|
|
if ($login === '' || $password === '') {
|
|
abort_api(422, 'invalid_credentials', 'Benutzername und Passwort sind erforderlich.');
|
|
}
|
|
$result = auth_attempt($login, $password);
|
|
if (!$result['ok']) {
|
|
if (($result['retry'] ?? 0) > 0) {
|
|
header('Retry-After: ' . (int) $result['retry']);
|
|
abort_api(429, 'rate_limited', (string) $result['error']);
|
|
}
|
|
abort_api(401, 'invalid_credentials', (string) $result['error']);
|
|
}
|
|
$user = $result['user'];
|
|
if ((int) ($user['must_change_password'] ?? 0) === 1) {
|
|
abort_api(403, 'password_change_required', 'Bitte zuerst im Web das Passwort ändern.');
|
|
}
|
|
$created = token_create((int) $user['id'], $name);
|
|
json_response([
|
|
'token' => $created['token'],
|
|
'prefix' => $created['prefix'],
|
|
'name' => $name !== '' ? $name : 'App',
|
|
'user' => [
|
|
'id' => (int) $user['id'],
|
|
'username' => $user['username'],
|
|
'display_name' => $user['display_name'],
|
|
],
|
|
], 201);
|
|
}
|
|
|
|
function api_fetch_visible(int $id, array $user): array
|
|
{
|
|
$routine = routine_fetch($id);
|
|
if ($routine === null) {
|
|
abort_api(404, 'not_found', 'Kür nicht gefunden.');
|
|
}
|
|
if (!routine_can_view($routine, $user)) {
|
|
abort_api(403, 'forbidden', 'Diese Kür ist nicht freigegeben.');
|
|
}
|
|
return $routine;
|
|
}
|
|
|
|
function api_routine(int $id, string $method, array $user): void
|
|
{
|
|
$routine = api_fetch_visible($id, $user);
|
|
if ($method === 'GET') {
|
|
json_response(api_routine_payload($routine));
|
|
}
|
|
if ($method === 'DELETE') {
|
|
if (!routine_can_edit($routine, $user)) {
|
|
abort_api(403, 'forbidden', 'Nur der Eigentümer oder ein Admin darf die Kür ändern.');
|
|
}
|
|
routine_set_status($id, 'hidden');
|
|
json_response(['id' => $id, 'status' => 'hidden']);
|
|
}
|
|
abort_api(405, 'method_not_allowed', 'Nur GET oder DELETE ist erlaubt.');
|
|
}
|
|
|
|
function api_upload(array $user, ?int $routineId): void
|
|
{
|
|
if (!api_truthy($_POST['rights'] ?? null)) {
|
|
abort_api(422, 'rights_required', 'Bitte bestätigen, dass Kür und Musik geteilt werden dürfen.');
|
|
}
|
|
if (!routine_upload_quota_ok((int) $user['id'])) {
|
|
abort_api(429, 'upload_limit', 'Stündliches Upload-Limit erreicht.');
|
|
}
|
|
$package = routine_validate_package($_POST);
|
|
if (!$package['ok']) {
|
|
json_response([
|
|
'error' => ['code' => 'validation_failed', 'message' => 'Die Dateien sind nicht gültig.'],
|
|
'errors' => $package['errors'],
|
|
], 422);
|
|
}
|
|
|
|
if ($routineId === null) {
|
|
$duplicate = routine_duplicate_exists((int) $user['id'], $package);
|
|
if ($duplicate !== null) {
|
|
abort_api(409, 'duplicate', 'Diese Kür ist bereits im Hub (ID ' . $duplicate . ').');
|
|
}
|
|
$routineId = routine_create((int) $user['id'], $package);
|
|
} else {
|
|
$routine = routine_fetch($routineId);
|
|
if ($routine === null) {
|
|
abort_api(404, 'not_found', 'Kür nicht gefunden.');
|
|
}
|
|
if (!routine_can_edit($routine, $user)) {
|
|
abort_api(403, 'forbidden', 'Nur der Eigentümer oder ein Admin darf die Kür ändern.');
|
|
}
|
|
routine_add_revision($routineId, (int) $user['id'], $package);
|
|
}
|
|
$created = routine_fetch($routineId);
|
|
if ($created === null) {
|
|
abort_api(500, 'server_error', 'Kür konnte nicht geladen werden.');
|
|
}
|
|
json_response(api_routine_payload($created), 201);
|
|
}
|