bootstrap.php 3.2 KB

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