BlueGit

projects-timfalken-com verhalenboek

Files

verhalenboek / web / api.php

api.php

feat: voeg Verhalenboek toe met dagen, leesvoortgang en schrijf-API
timfa project_87242033_bot_b167a9be22027ad5a4b431d0ad8426c6 · pushed by timfa · 6 weeks ago
799da657 History
api.php 5.1 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 if ($method !== 'DELETE') fail('Deze methode is hier niet toegestaan.', 405);
62 requireAdminSession($actor);
63 jsonOut(['key' => revokeApiKey((int)$match[1])]);
64 }
65 if ($route === '/days') {
66 if ($method === 'GET') jsonOut(['days' => listDays($readerId)]);
67 if ($method === 'POST') {
68 requireWriter($actor);
69 $in = readJson();
70 if (!is_string($in['date'] ?? null)) throw new AppException('De datum moet de vorm JJJJ-MM-DD hebben.');
71 jsonOut(createDay($in['date'], $in['title'] ?? null, $in['stories'] ?? null), 201);
72 }
73 fail('Deze methode is hier niet toegestaan.', 405);
74 }
75 if ($route === '/days/latest') {
76 if ($method !== 'GET') fail('Deze methode is hier niet toegestaan.', 405);
77 jsonOut(latestWritten());
78 }
79 if ($route === '/days/gaps') {
80 if ($method !== 'GET') fail('Deze methode is hier niet toegestaan.', 405);
81 jsonOut(dayGaps());
82 }
83 if (preg_match('#^/days/(\d{4}-\d{2}-\d{2})$#', $route, $match)) {
84 $date = $match[1];
85 if ($method === 'GET') jsonOut(loadDay($date, $readerId));
86 if ($method === 'PUT') {
87 requireWriter($actor);
88 jsonOut(updateDay($date, readJson()));
89 }
90 if ($method === 'DELETE') {
91 requireWriter($actor);
92 jsonOut(['ok' => true, 'date' => deleteDay($date)]);
93 }
94 fail('Deze methode is hier niet toegestaan.', 405);
95 }
96 if (preg_match('#^/days/(\d{4}-\d{2}-\d{2})/read$#', $route, $match)) {
97 requireReader($actor);
98 if ($method === 'POST') jsonOut(markRead($actor['userId'], $match[1], true));
99 if ($method === 'DELETE') jsonOut(markRead($actor['userId'], $match[1], false));
100 fail('Deze methode is hier niet toegestaan.', 405);
101 }
102 if (preg_match('#^/days/(\d{4}-\d{2}-\d{2})/stories$#', $route, $match)) {
103 if ($method !== 'POST') fail('Deze methode is hier niet toegestaan.', 405);
104 requireWriter($actor);
105 jsonOut(addStory($match[1], readJson()), 201);
106 }
107 if (preg_match('#^/days/(\d{4}-\d{2}-\d{2})/stories/([123])$#', $route, $match)) {
108 requireWriter($actor);
109 if ($method === 'PUT') jsonOut(updateStory($match[1], (int)$match[2], readJson()));
110 if ($method === 'DELETE') jsonOut(deleteStory($match[1], (int)$match[2]));
111 fail('Deze methode is hier niet toegestaan.', 405);
112 }
113 fail('Onbekend pad.', 404);
114} catch (AppException $e) {
115 fail($e->getMessage(), $e->status);
116} catch (Throwable $e) {
117 error_log($e->getMessage());
118 fail('Er ging iets mis.', 500);
119}