bootstrap.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/zip.php';
  21. require APP_ROOT . '/app/archive.php';
  22. require APP_ROOT . '/app/migrate.php';
  23. require APP_ROOT . '/app/markdown.php';
  24. require APP_ROOT . '/app/partials.php';
  25. /**
  26. * Read a config value by dot path, e.g. config('s3.bucket').
  27. */
  28. function config(string $path, mixed $default = null): mixed
  29. {
  30. $value = $GLOBALS['config'];
  31. foreach (explode('.', $path) as $part) {
  32. if (!is_array($value) || !array_key_exists($part, $value)) {
  33. return $default;
  34. }
  35. $value = $value[$part];
  36. }
  37. return $value;
  38. }
  39. /**
  40. * Prefix a link or asset path with the way back to the site root, so the shared
  41. * public partials work from any depth. Pages in the document root need no
  42. * prefix; pages in a subfolder (gallery/) define SITE_BASE as '../' before
  43. * including this file. Relative rather than absolute, because the app may be
  44. * installed in a subdirectory of the domain.
  45. */
  46. function base(string $path = ''): string
  47. {
  48. return (defined('SITE_BASE') ? SITE_BASE : '') . $path;
  49. }
  50. /** HTML-escape for output. */
  51. function e(?string $s): string
  52. {
  53. return htmlspecialchars($s ?? '', ENT_QUOTES, 'UTF-8');
  54. }
  55. /**
  56. * Byte count as something a client can judge at a glance. Gallery archives run
  57. * to gigabytes, so the size belongs on the download button itself.
  58. */
  59. function human_bytes(int $bytes): string
  60. {
  61. $units = ['B', 'KB', 'MB', 'GB', 'TB'];
  62. $i = 0;
  63. $value = (float)$bytes;
  64. while ($value >= 1024 && $i < count($units) - 1) {
  65. $value /= 1024;
  66. $i++;
  67. }
  68. return ($value >= 10 || $i === 0 ? round($value) : round($value, 1)) . ' ' . $units[$i];
  69. }
  70. /** Start the session with hardened cookie settings (idempotent). */
  71. function session_boot(): void
  72. {
  73. if (session_status() === PHP_SESSION_ACTIVE) {
  74. return;
  75. }
  76. session_set_cookie_params([
  77. 'lifetime' => 0,
  78. 'path' => '/',
  79. 'secure' => !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off',
  80. 'httponly' => true,
  81. 'samesite' => 'Lax',
  82. ]);
  83. session_name('fpsid');
  84. session_start();
  85. }
  86. /** Redirect and stop. */
  87. function redirect(string $url): never
  88. {
  89. header('Location: ' . $url);
  90. exit;
  91. }
  92. /** Send a JSON response and stop (used by admin/api.php). */
  93. function json_response(array $payload, int $status = 200): never
  94. {
  95. http_response_code($status);
  96. header('Content-Type: application/json; charset=utf-8');
  97. echo json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  98. exit;
  99. }