| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | require __DIR__.'/lib/bootstrap.php'; |
| 4 | |
| 5 | try { |
| 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 ($route === '/users') { |
| 61 | requireAdminSession($actor, 'Alleen een beheerder mag gebruikers beheren.', 'users'); |
| 62 | if ($method !== 'GET') fail('Deze methode is hier niet toegestaan.', 405); |
| 63 | jsonOut(['users' => listUsers()]); |
| 64 | } |
| 65 | if (preg_match('#^/users/([^/]+)$#', $route, $match)) { |
| 66 | requireAdminSession($actor, 'Alleen een beheerder mag gebruikers beheren.', 'users'); |
| 67 | if ($method !== 'PUT') fail('Deze methode is hier niet toegestaan.', 405); |
| 68 | $in = readJson(); |
| 69 | if (!is_bool($in['admin'] ?? null)) throw new AppException('Zeg of deze gebruiker beheerder moet zijn.'); |
| 70 | jsonOut(['user' => setUserRole($actor['userId'], rawurldecode($match[1]), $in['admin'])]); |
| 71 | } |
| 72 | if (preg_match('#^/keys/(\d+)$#', $route, $match)) { |
| 73 | requireAdminSession($actor); |
| 74 | if ($method === 'GET') jsonOut(['key' => revealApiKey((int)$match[1])]); |
| 75 | if ($method === 'DELETE') jsonOut(['key' => revokeApiKey((int)$match[1])]); |
| 76 | fail('Deze methode is hier niet toegestaan.', 405); |
| 77 | } |
| 78 | if ($route === '/days') { |
| 79 | if ($method === 'GET') jsonOut(['days' => listDays($readerId)]); |
| 80 | if ($method === 'POST') { |
| 81 | requireWriter($actor); |
| 82 | $in = readJson(); |
| 83 | if (!is_string($in['date'] ?? null)) throw new AppException('De datum moet de vorm JJJJ-MM-DD hebben.'); |
| 84 | jsonOut(createDay($in['date'], $in['title'] ?? null, $in['stories'] ?? null), 201); |
| 85 | } |
| 86 | fail('Deze methode is hier niet toegestaan.', 405); |
| 87 | } |
| 88 | if ($route === '/days/latest') { |
| 89 | if ($method !== 'GET') fail('Deze methode is hier niet toegestaan.', 405); |
| 90 | jsonOut(latestWritten()); |
| 91 | } |
| 92 | if ($route === '/days/gaps') { |
| 93 | if ($method !== 'GET') fail('Deze methode is hier niet toegestaan.', 405); |
| 94 | jsonOut(dayGaps()); |
| 95 | } |
| 96 | if (preg_match('#^/days/(\d{4}-\d{2}-\d{2})$#', $route, $match)) { |
| 97 | $date = $match[1]; |
| 98 | if ($method === 'GET') jsonOut(loadDay($date, $readerId)); |
| 99 | if ($method === 'PUT') { |
| 100 | requireWriter($actor); |
| 101 | jsonOut(updateDay($date, readJson())); |
| 102 | } |
| 103 | if ($method === 'DELETE') { |
| 104 | requireWriter($actor); |
| 105 | jsonOut(['ok' => true, 'date' => deleteDay($date)]); |
| 106 | } |
| 107 | fail('Deze methode is hier niet toegestaan.', 405); |
| 108 | } |
| 109 | if (preg_match('#^/days/(\d{4}-\d{2}-\d{2})/read$#', $route, $match)) { |
| 110 | requireReader($actor); |
| 111 | if ($method === 'POST') jsonOut(markRead($actor['userId'], $match[1], true)); |
| 112 | if ($method === 'DELETE') jsonOut(markRead($actor['userId'], $match[1], false)); |
| 113 | fail('Deze methode is hier niet toegestaan.', 405); |
| 114 | } |
| 115 | if (preg_match('#^/days/(\d{4}-\d{2}-\d{2})/stories$#', $route, $match)) { |
| 116 | if ($method !== 'POST') fail('Deze methode is hier niet toegestaan.', 405); |
| 117 | requireWriter($actor); |
| 118 | jsonOut(addStory($match[1], readJson()), 201); |
| 119 | } |
| 120 | if (preg_match('#^/days/(\d{4}-\d{2}-\d{2})/stories/([123])$#', $route, $match)) { |
| 121 | requireWriter($actor); |
| 122 | if ($method === 'PUT') jsonOut(updateStory($match[1], (int)$match[2], readJson())); |
| 123 | if ($method === 'DELETE') jsonOut(deleteStory($match[1], (int)$match[2])); |
| 124 | fail('Deze methode is hier niet toegestaan.', 405); |
| 125 | } |
| 126 | fail('Onbekend pad.', 404); |
| 127 | } catch (AppException $e) { |
| 128 | fail($e->getMessage(), $e->status, $e->auth); |
| 129 | } catch (Throwable $e) { |
| 130 | error_log($e->getMessage()); |
| 131 | fail('Er ging iets mis.', 500); |
| 132 | } |
projects-timfalken-com verhalenboek