bootstrap.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. declare(strict_types=1);
  3. // Shared entry point for every server-side script. Loads config.php, fills in
  4. // defaults for all MANAGE_* constants, and pulls in the low-level helpers.
  5. // Deliberately does not start a session: the API endpoints are stateless and
  6. // only the admin pages call manageStartSession().
  7. $manageConfigFile = dirname(__DIR__) . "/config.php";
  8. if (is_file($manageConfigFile)) {
  9. require_once $manageConfigFile;
  10. }
  11. if (!defined("MANAGE_PRODUCT_NAME")) {
  12. define("MANAGE_PRODUCT_NAME", "Managed Application");
  13. }
  14. if (!defined("MANAGE_PACKAGE_PREFIX")) {
  15. define("MANAGE_PACKAGE_PREFIX", "release");
  16. }
  17. if (!defined("MANAGE_PUBLIC_URL")) {
  18. define("MANAGE_PUBLIC_URL", "");
  19. }
  20. if (!defined("MANAGE_STORAGE_DIR")) {
  21. define("MANAGE_STORAGE_DIR", dirname(__DIR__) . "/storage/");
  22. }
  23. if (!defined("MANAGE_BACKUP_RETENTION")) {
  24. define("MANAGE_BACKUP_RETENTION", 30);
  25. }
  26. if (!defined("MANAGE_BACKUP_MAX_UPLOAD_BYTES")) {
  27. define("MANAGE_BACKUP_MAX_UPLOAD_BYTES", 0);
  28. }
  29. if (!defined("MANAGE_S3_ENABLED")) {
  30. define("MANAGE_S3_ENABLED", false);
  31. }
  32. if (!defined("MANAGE_S3_ENDPOINT")) {
  33. define("MANAGE_S3_ENDPOINT", "");
  34. }
  35. if (!defined("MANAGE_S3_REGION")) {
  36. define("MANAGE_S3_REGION", "");
  37. }
  38. if (!defined("MANAGE_S3_BUCKET")) {
  39. define("MANAGE_S3_BUCKET", "");
  40. }
  41. if (!defined("MANAGE_S3_PREFIX")) {
  42. define("MANAGE_S3_PREFIX", "");
  43. }
  44. if (!defined("MANAGE_S3_ACCESS_KEY")) {
  45. define("MANAGE_S3_ACCESS_KEY", "");
  46. }
  47. if (!defined("MANAGE_S3_SECRET_KEY")) {
  48. define("MANAGE_S3_SECRET_KEY", "");
  49. }
  50. if (!defined("MANAGE_S3_PATH_STYLE")) {
  51. define("MANAGE_S3_PATH_STYLE", false);
  52. }
  53. if (!defined("MANAGE_S3_TIMEOUT")) {
  54. define("MANAGE_S3_TIMEOUT", 120);
  55. }
  56. if (!defined("MANAGE_S3_RETENTION")) {
  57. define("MANAGE_S3_RETENTION", 365);
  58. }
  59. if (!defined("MANAGE_LOGIN_RATE_LIMIT_MAX")) {
  60. define("MANAGE_LOGIN_RATE_LIMIT_MAX", 10);
  61. }
  62. if (!defined("MANAGE_LOGIN_RATE_LIMIT_WINDOW")) {
  63. define("MANAGE_LOGIN_RATE_LIMIT_WINDOW", 900);
  64. }
  65. if (!defined("MANAGE_API_RATE_LIMIT_MAX")) {
  66. define("MANAGE_API_RATE_LIMIT_MAX", 240);
  67. }
  68. if (!defined("MANAGE_API_RATE_LIMIT_WINDOW")) {
  69. define("MANAGE_API_RATE_LIMIT_WINDOW", 300);
  70. }
  71. if (!defined("MANAGE_LOG_MAX_BYTES")) {
  72. define("MANAGE_LOG_MAX_BYTES", 1048576);
  73. }
  74. if (!defined("MANAGE_LOG_KEEP_FILES")) {
  75. define("MANAGE_LOG_KEEP_FILES", 5);
  76. }
  77. if (!defined("MANAGE_LOG_MAX_AGE_SECONDS")) {
  78. define("MANAGE_LOG_MAX_AGE_SECONDS", 2592000);
  79. }
  80. // Derived storage paths. Kept as functions rather than constants so a config
  81. // may override MANAGE_STORAGE_DIR without having to restate every child path.
  82. function manageStorageDir(): string
  83. {
  84. return rtrim((string) MANAGE_STORAGE_DIR, "/\\") . DIRECTORY_SEPARATOR;
  85. }
  86. function manageInstancesFile(): string
  87. {
  88. return manageStorageDir() . "instances.json";
  89. }
  90. function manageSettingsFile(): string
  91. {
  92. return manageStorageDir() . "settings.json";
  93. }
  94. function manageReleasesDir(): string
  95. {
  96. return manageStorageDir() . "releases" . DIRECTORY_SEPARATOR;
  97. }
  98. function manageManifestFile(): string
  99. {
  100. return manageReleasesDir() . "manifest.json";
  101. }
  102. function managePackagesDir(): string
  103. {
  104. return manageReleasesDir() . "packages" . DIRECTORY_SEPARATOR;
  105. }
  106. function manageBackupsDir(): string
  107. {
  108. return manageStorageDir() . "backups" . DIRECTORY_SEPARATOR;
  109. }
  110. function manageBackupIndexFile(): string
  111. {
  112. return manageBackupsDir() . "index.json";
  113. }
  114. function manageLogsDir(): string
  115. {
  116. return manageStorageDir() . "logs" . DIRECTORY_SEPARATOR;
  117. }
  118. function manageS3LogFile(): string
  119. {
  120. return manageLogsDir() . "s3.log";
  121. }
  122. function manageProductName(): string
  123. {
  124. $name = trim((string) MANAGE_PRODUCT_NAME);
  125. return $name !== "" ? $name : "Managed Application";
  126. }
  127. function manageEscape($value): string
  128. {
  129. return htmlspecialchars((string) $value, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
  130. }
  131. function manageIsVersion(string $version): bool
  132. {
  133. return preg_match('/^v\d+\.\d+\.\d+$/', $version) === 1;
  134. }
  135. function manageFormatBytes(int $bytes): string
  136. {
  137. if ($bytes >= 1073741824) {
  138. return number_format($bytes / 1073741824, 2, ",", ".") . " GB";
  139. }
  140. if ($bytes >= 1048576) {
  141. return number_format($bytes / 1048576, 2, ",", ".") . " MB";
  142. }
  143. if ($bytes >= 1024) {
  144. return number_format($bytes / 1024, 1, ",", ".") . " KB";
  145. }
  146. return $bytes . " B";
  147. }
  148. require_once __DIR__ . "/storage.php";
  149. require_once __DIR__ . "/log.php";
  150. require_once __DIR__ . "/ratelimit.php";