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.
255 lines
8.8 KiB
PHP
255 lines
8.8 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', t('api.post_only'));
|
|
}
|
|
api_create_token();
|
|
return;
|
|
}
|
|
|
|
$user = api_require_user();
|
|
|
|
if ($path === '/api/v1/me') {
|
|
if ($method !== 'GET') {
|
|
abort_api(405, 'method_not_allowed', t('api.get_only'));
|
|
}
|
|
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', t('api.get_or_post'));
|
|
}
|
|
|
|
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', t('api.get_only'));
|
|
}
|
|
$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', t('api.post_only'));
|
|
}
|
|
api_upload($user, (int) $matches[1]);
|
|
return;
|
|
}
|
|
|
|
abort_api(404, 'not_found', t('api.unknown_path'));
|
|
}
|
|
|
|
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', t('api.credentials_required'));
|
|
}
|
|
$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', t('api.change_password_web'));
|
|
}
|
|
$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', t('routine.not_found_short'));
|
|
}
|
|
if (!routine_can_view($routine, $user)) {
|
|
abort_api(403, 'forbidden', t('routine.forbidden'));
|
|
}
|
|
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', t('api.edit_forbidden'));
|
|
}
|
|
routine_set_status($id, 'hidden');
|
|
json_response(['id' => $id, 'status' => 'hidden']);
|
|
}
|
|
abort_api(405, 'method_not_allowed', t('api.get_or_delete'));
|
|
}
|
|
|
|
function api_upload(array $user, ?int $routineId): void
|
|
{
|
|
if (!api_truthy($_POST['rights'] ?? null)) {
|
|
abort_api(422, 'rights_required', t('api.rights_required'));
|
|
}
|
|
if (!routine_upload_quota_ok((int) $user['id'])) {
|
|
abort_api(429, 'upload_limit', t('api.upload_limit'));
|
|
}
|
|
$package = routine_validate_package($_POST);
|
|
if (!$package['ok']) {
|
|
json_response([
|
|
'error' => ['code' => 'validation_failed', 'message' => t('api.invalid_files')],
|
|
'errors' => $package['errors'],
|
|
], 422);
|
|
}
|
|
|
|
if ($routineId === null) {
|
|
$duplicate = routine_duplicate_exists((int) $user['id'], $package);
|
|
if ($duplicate !== null) {
|
|
abort_api(409, 'duplicate', t('api.duplicate', ['id' => $duplicate]));
|
|
}
|
|
$routineId = routine_create((int) $user['id'], $package);
|
|
} else {
|
|
$routine = routine_fetch($routineId);
|
|
if ($routine === null) {
|
|
abort_api(404, 'not_found', t('routine.not_found_short'));
|
|
}
|
|
if (!routine_can_edit($routine, $user)) {
|
|
abort_api(403, 'forbidden', t('api.edit_forbidden'));
|
|
}
|
|
routine_add_revision($routineId, (int) $user['id'], $package);
|
|
}
|
|
$created = routine_fetch($routineId);
|
|
if ($created === null) {
|
|
abort_api(500, 'server_error', t('api.routine_load_failed'));
|
|
}
|
|
json_response(api_routine_payload($created), 201);
|
|
}
|