bootstrap.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Application bootstrap. Every public entry script includes this first.
  4. */
  5. declare(strict_types=1);
  6. define('APP_ROOT', dirname(__DIR__));
  7. define('DATA_DIR', APP_ROOT . '/data');
  8. define('MEDIA_DIR', APP_ROOT . '/media');
  9. define('CONFIG_DIR', APP_ROOT . '/config');
  10. if (!is_file(CONFIG_DIR . '/config.php')) {
  11. http_response_code(500);
  12. exit('Missing config/config.php — copy config/config.sample.php and adjust it.');
  13. }
  14. $GLOBALS['config'] = require CONFIG_DIR . '/config.php';
  15. date_default_timezone_set(config('site.timezone', 'UTC'));
  16. require APP_ROOT . '/app/storage.php';
  17. require APP_ROOT . '/app/csrf.php';
  18. require APP_ROOT . '/app/auth.php';
  19. require APP_ROOT . '/app/s3.php';
  20. require APP_ROOT . '/app/markdown.php';
  21. require APP_ROOT . '/app/partials.php';
  22. /**
  23. * Read a config value by dot path, e.g. config('s3.bucket').
  24. */
  25. function config(string $path, mixed $default = null): mixed
  26. {
  27. $value = $GLOBALS['config'];
  28. foreach (explode('.', $path) as $part) {
  29. if (!is_array($value) || !array_key_exists($part, $value)) {
  30. return $default;
  31. }
  32. $value = $value[$part];
  33. }
  34. return $value;
  35. }
  36. /**
  37. * Prefix a link or asset path with the way back to the site root, so the shared
  38. * public partials work from any depth. Pages in the document root need no
  39. * prefix; pages in a subfolder (gallery/) define SITE_BASE as '../' before
  40. * including this file. Relative rather than absolute, because the app may be
  41. * installed in a subdirectory of the domain.
  42. */
  43. function base(string $path = ''): string
  44. {
  45. return (defined('SITE_BASE') ? SITE_BASE : '') . $path;
  46. }
  47. /** HTML-escape for output. */
  48. function e(?string $s): string
  49. {
  50. return htmlspecialchars($s ?? '', ENT_QUOTES, 'UTF-8');
  51. }
  52. /** Start the session with hardened cookie settings (idempotent). */
  53. function session_boot(): void
  54. {
  55. if (session_status() === PHP_SESSION_ACTIVE) {
  56. return;
  57. }
  58. session_set_cookie_params([
  59. 'lifetime' => 0,
  60. 'path' => '/',
  61. 'secure' => !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off',
  62. 'httponly' => true,
  63. 'samesite' => 'Lax',
  64. ]);
  65. session_name('fpsid');
  66. session_start();
  67. }
  68. /** Redirect and stop. */
  69. function redirect(string $url): never
  70. {
  71. header('Location: ' . $url);
  72. exit;
  73. }
  74. /** Send a JSON response and stop (used by admin/api.php). */
  75. function json_response(array $payload, int $status = 200): never
  76. {
  77. http_response_code($status);
  78. header('Content-Type: application/json; charset=utf-8');
  79. echo json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  80. exit;
  81. }