index.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. use App\Auth\LocalAdmin;
  4. use App\Auth\OidcClient;
  5. use App\Auth\SessionGuard;
  6. use App\Controllers\AuthController;
  7. use App\Db\Connection;
  8. use App\Db\Migrator;
  9. use App\Http\Request;
  10. use App\Http\Response;
  11. use App\Http\Router;
  12. use App\Http\View;
  13. use App\Repositories\UserRepository;
  14. use App\Services\AuditLogger;
  15. // Buffer output so a stray warning/notice can't send headers before
  16. // Response::send() gets a chance to set them. send() will flush.
  17. ob_start();
  18. define('APP_ROOT', dirname(__DIR__));
  19. // ---------------------------------------------------------------------------
  20. // Autoload
  21. // ---------------------------------------------------------------------------
  22. $autoload = APP_ROOT . '/vendor/autoload.php';
  23. if (!is_file($autoload)) {
  24. http_response_code(500);
  25. header('Content-Type: text/plain; charset=utf-8');
  26. echo "Composer dependencies are not installed.\n";
  27. echo "Run: composer install (or rebuild the container).\n";
  28. exit;
  29. }
  30. require $autoload;
  31. // ---------------------------------------------------------------------------
  32. // Environment
  33. // ---------------------------------------------------------------------------
  34. if (is_file(APP_ROOT . '/.env')) {
  35. $dotenv = Dotenv\Dotenv::createImmutable(APP_ROOT);
  36. $dotenv->safeLoad();
  37. }
  38. $appEnv = getenv('APP_ENV') ?: 'production';
  39. if ($appEnv !== 'production') {
  40. ini_set('display_errors', '1');
  41. error_reporting(E_ALL);
  42. } else {
  43. ini_set('display_errors', '0');
  44. }
  45. // ---------------------------------------------------------------------------
  46. // Migrations — cheap no-op when already current
  47. // ---------------------------------------------------------------------------
  48. try {
  49. $pdo = Connection::pdo();
  50. (new Migrator($pdo))->migrate();
  51. } catch (\Throwable $e) {
  52. http_response_code(500);
  53. header('Content-Type: text/plain; charset=utf-8');
  54. echo "Database bootstrap failed.\n";
  55. if ($appEnv !== 'production') {
  56. echo $e->getMessage() . "\n";
  57. }
  58. exit;
  59. }
  60. // ---------------------------------------------------------------------------
  61. // Shared services
  62. // ---------------------------------------------------------------------------
  63. $view = new View(APP_ROOT . '/views');
  64. $users = new UserRepository($pdo);
  65. $audit = new AuditLogger($pdo);
  66. $auth = new AuthController($pdo, $users, $audit, $view);
  67. // ---------------------------------------------------------------------------
  68. // Routing
  69. // ---------------------------------------------------------------------------
  70. $router = new Router();
  71. $router->get('/', function (Request $req) use ($view, $pdo, $users, $appEnv): Response {
  72. $currentUser = SessionGuard::currentUser($users);
  73. $schemaVersion = (int) $pdo->query(
  74. 'SELECT COALESCE(MAX(version), 0) FROM schema_version'
  75. )->fetchColumn();
  76. return Response::html($view->render('home', [
  77. 'title' => 'Sprint Planner',
  78. 'currentUser' => $currentUser,
  79. 'schemaVersion' => $schemaVersion,
  80. 'dbPath' => Connection::path(),
  81. 'appEnv' => $appEnv,
  82. 'oidcConfigured' => OidcClient::isConfigured(),
  83. 'localAdminEnabled' => LocalAdmin::isEnabled(),
  84. 'authError' => isset($req->query['auth_error']),
  85. 'csrfToken' => SessionGuard::csrfToken(),
  86. ]));
  87. });
  88. $router->get('/healthz', fn() => Response::text('ok'));
  89. $router->get('/auth/login', $auth->login(...));
  90. $router->get('/auth/callback', $auth->callback(...));
  91. $router->post('/auth/logout', $auth->logout(...));
  92. $router->get('/auth/local', $auth->loginLocalForm(...));
  93. $router->post('/auth/local', $auth->loginLocal(...));
  94. // ---------------------------------------------------------------------------
  95. // Dispatch
  96. // ---------------------------------------------------------------------------
  97. $request = Request::fromGlobals();
  98. $response = $router->dispatch($request);
  99. $response->send();
  100. // Flush the output buffer opened at the top.
  101. if (ob_get_level() > 0) {
  102. @ob_end_flush();
  103. }