BlueGit

projects-timfalken-com verhalenboek

Files

verhalenboek / web / api.php

api.php

feat: toon API-sleutels opnieuw en leg authenticatie uit
timfa project_87242033_bot_b167a9be22027ad5a4b431d0ad8426c6 · pushed by timfa · 6 weeks ago
16b40fa3 History
api.php 5.2 KiB Download Raw
1<?php
2declare(strict_types=1);
3require __DIR__.'/lib/bootstrap.php';
4
5try {
6 $method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
7 $route = apiRoute();
8 $actor = authenticate();
9 if ($method !== 'GET' && $method !== 'HEAD') requireCsrf($actor);
10 $readerId = $actor['kind'] === 'user' ? $actor['userId'] : null;
11
12 if ($route === '/me') {
13 if ($method !== 'GET') fail('Deze methode is hier niet toegestaan.', 405);
14 jsonOut([
15 'user' => publicUser($actor['userId']),
16 'csrf' => $actor['kind'] === 'user' ? ($_SESSION['csrf'] ?? null) : null,
17 'local' => localDev(),
18 'auth' => $actor['kind'],
19 ]);
20 }
21 if ($route === '/me/settings') {
22 if ($method !== 'PUT') fail('Deze methode is hier niet toegestaan.', 405);
23 requireReader($actor);
24 jsonOut(['user' => saveSettings($actor['userId'], readJson())]);
25 }
26 if ($route === '/me/export') {
27 if ($method !== 'GET') fail('Deze methode is hier niet toegestaan.', 405);
28 requireReader($actor);
29 header('Content-Disposition: attachment; filename="verhalenboek-gegevens.json"');
30 jsonOut(exportUser($actor['userId']));
31 }
32 if ($route === '/session/switch') {
33 if ($method !== 'POST') fail('Deze methode is hier niet toegestaan.', 405);
34 requireReader($actor);
35 if (!localDev()) fail('Niet beschikbaar.', 403);
36 $in = readJson();
37 if (!in_array($in['user'] ?? '', ['test_user_1', 'test_user_2'], true)) throw new AppException('Onbekende testgebruiker.');
38 loginUser('local:'.$in['user'], $in['user']);
39 jsonOut(['ok' => true, 'user' => publicUser($_SESSION['user']), 'csrf' => $_SESSION['csrf']]);
40 }
41 if ($route === '/session/logout') {
42 if ($method !== 'POST') fail('Deze methode is hier niet toegestaan.', 405);
43 requireReader($actor);
44 if (!localDev() && isset($_SESSION['token'])) {
45 try { authRequest('/api/logout', [], (string)$_SESSION['token']); } catch (Throwable $e) { /* lokaal alsnog afmelden */ }
46 }
47 $_SESSION = [];
48 session_destroy();
49 jsonOut(['ok' => true]);
50 }
51 if ($route === '/keys') {
52 requireAdminSession($actor);
53 if ($method === 'GET') jsonOut(['keys' => listApiKeys()]);
54 if ($method === 'POST') {
55 $in = readJson();
56 jsonOut(['key' => createApiKey($actor['userId'], $in['name'] ?? null)], 201);
57 }
58 fail('Deze methode is hier niet toegestaan.', 405);
59 }
60 if (preg_match('#^/keys/(\d+)$#', $route, $match)) {
61 requireAdminSession($actor);
62 if ($method === 'GET') jsonOut(['key' => revealApiKey((int)$match[1])]);
63 if ($method === 'DELETE') jsonOut(['key' => revokeApiKey((int)$match[1])]);
64 fail('Deze methode is hier niet toegestaan.', 405);
65 }
66 if ($route === '/days') {
67 if ($method === 'GET') jsonOut(['days' => listDays($readerId)]);
68 if ($method === 'POST') {
69 requireWriter($actor);
70 $in = readJson();
71 if (!is_string($in['date'] ?? null)) throw new AppException('De datum moet de vorm JJJJ-MM-DD hebben.');
72 jsonOut(createDay($in['date'], $in['title'] ?? null, $in['stories'] ?? null), 201);
73 }
74 fail('Deze methode is hier niet toegestaan.', 405);
75 }
76 if ($route === '/days/latest') {
77 if ($method !== 'GET') fail('Deze methode is hier niet toegestaan.', 405);
78 jsonOut(latestWritten());
79 }
80 if ($route === '/days/gaps') {
81 if ($method !== 'GET') fail('Deze methode is hier niet toegestaan.', 405);
82 jsonOut(dayGaps());
83 }
84 if (preg_match('#^/days/(\d{4}-\d{2}-\d{2})$#', $route, $match)) {
85 $date = $match[1];
86 if ($method === 'GET') jsonOut(loadDay($date, $readerId));
87 if ($method === 'PUT') {
88 requireWriter($actor);
89 jsonOut(updateDay($date, readJson()));
90 }
91 if ($method === 'DELETE') {
92 requireWriter($actor);
93 jsonOut(['ok' => true, 'date' => deleteDay($date)]);
94 }
95 fail('Deze methode is hier niet toegestaan.', 405);
96 }
97 if (preg_match('#^/days/(\d{4}-\d{2}-\d{2})/read$#', $route, $match)) {
98 requireReader($actor);
99 if ($method === 'POST') jsonOut(markRead($actor['userId'], $match[1], true));
100 if ($method === 'DELETE') jsonOut(markRead($actor['userId'], $match[1], false));
101 fail('Deze methode is hier niet toegestaan.', 405);
102 }
103 if (preg_match('#^/days/(\d{4}-\d{2}-\d{2})/stories$#', $route, $match)) {
104 if ($method !== 'POST') fail('Deze methode is hier niet toegestaan.', 405);
105 requireWriter($actor);
106 jsonOut(addStory($match[1], readJson()), 201);
107 }
108 if (preg_match('#^/days/(\d{4}-\d{2}-\d{2})/stories/([123])$#', $route, $match)) {
109 requireWriter($actor);
110 if ($method === 'PUT') jsonOut(updateStory($match[1], (int)$match[2], readJson()));
111 if ($method === 'DELETE') jsonOut(deleteStory($match[1], (int)$match[2]));
112 fail('Deze methode is hier niet toegestaan.', 405);
113 }
114 fail('Onbekend pad.', 404);
115} catch (AppException $e) {
116 fail($e->getMessage(), $e->status, $e->auth);
117} catch (Throwable $e) {
118 error_log($e->getMessage());
119 fail('Er ging iets mis.', 500);
120}