| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- declare(strict_types=1);
- // Shared entry point for every server-side script. Loads config.php, fills in
- // defaults for all MANAGE_* constants, and pulls in the low-level helpers.
- // Deliberately does not start a session: the API endpoints are stateless and
- // only the admin pages call manageStartSession().
- $manageConfigFile = dirname(__DIR__) . "/config.php";
- if (is_file($manageConfigFile)) {
- require_once $manageConfigFile;
- }
- if (!defined("MANAGE_PRODUCT_NAME")) {
- define("MANAGE_PRODUCT_NAME", "Managed Application");
- }
- if (!defined("MANAGE_PACKAGE_PREFIX")) {
- define("MANAGE_PACKAGE_PREFIX", "release");
- }
- if (!defined("MANAGE_PUBLIC_URL")) {
- define("MANAGE_PUBLIC_URL", "");
- }
- if (!defined("MANAGE_STORAGE_DIR")) {
- define("MANAGE_STORAGE_DIR", dirname(__DIR__) . "/storage/");
- }
- if (!defined("MANAGE_BACKUP_RETENTION")) {
- define("MANAGE_BACKUP_RETENTION", 30);
- }
- if (!defined("MANAGE_BACKUP_MAX_UPLOAD_BYTES")) {
- define("MANAGE_BACKUP_MAX_UPLOAD_BYTES", 0);
- }
- if (!defined("MANAGE_S3_ENABLED")) {
- define("MANAGE_S3_ENABLED", false);
- }
- if (!defined("MANAGE_S3_ENDPOINT")) {
- define("MANAGE_S3_ENDPOINT", "");
- }
- if (!defined("MANAGE_S3_REGION")) {
- define("MANAGE_S3_REGION", "");
- }
- if (!defined("MANAGE_S3_BUCKET")) {
- define("MANAGE_S3_BUCKET", "");
- }
- if (!defined("MANAGE_S3_PREFIX")) {
- define("MANAGE_S3_PREFIX", "");
- }
- if (!defined("MANAGE_S3_ACCESS_KEY")) {
- define("MANAGE_S3_ACCESS_KEY", "");
- }
- if (!defined("MANAGE_S3_SECRET_KEY")) {
- define("MANAGE_S3_SECRET_KEY", "");
- }
- if (!defined("MANAGE_S3_PATH_STYLE")) {
- define("MANAGE_S3_PATH_STYLE", false);
- }
- if (!defined("MANAGE_S3_TIMEOUT")) {
- define("MANAGE_S3_TIMEOUT", 120);
- }
- if (!defined("MANAGE_S3_RETENTION")) {
- define("MANAGE_S3_RETENTION", 365);
- }
- if (!defined("MANAGE_LOGIN_RATE_LIMIT_MAX")) {
- define("MANAGE_LOGIN_RATE_LIMIT_MAX", 10);
- }
- if (!defined("MANAGE_LOGIN_RATE_LIMIT_WINDOW")) {
- define("MANAGE_LOGIN_RATE_LIMIT_WINDOW", 900);
- }
- if (!defined("MANAGE_API_RATE_LIMIT_MAX")) {
- define("MANAGE_API_RATE_LIMIT_MAX", 240);
- }
- if (!defined("MANAGE_API_RATE_LIMIT_WINDOW")) {
- define("MANAGE_API_RATE_LIMIT_WINDOW", 300);
- }
- if (!defined("MANAGE_LOG_MAX_BYTES")) {
- define("MANAGE_LOG_MAX_BYTES", 1048576);
- }
- if (!defined("MANAGE_LOG_KEEP_FILES")) {
- define("MANAGE_LOG_KEEP_FILES", 5);
- }
- if (!defined("MANAGE_LOG_MAX_AGE_SECONDS")) {
- define("MANAGE_LOG_MAX_AGE_SECONDS", 2592000);
- }
- // Derived storage paths. Kept as functions rather than constants so a config
- // may override MANAGE_STORAGE_DIR without having to restate every child path.
- function manageStorageDir(): string
- {
- return rtrim((string) MANAGE_STORAGE_DIR, "/\\") . DIRECTORY_SEPARATOR;
- }
- function manageInstancesFile(): string
- {
- return manageStorageDir() . "instances.json";
- }
- function manageSettingsFile(): string
- {
- return manageStorageDir() . "settings.json";
- }
- function manageReleasesDir(): string
- {
- return manageStorageDir() . "releases" . DIRECTORY_SEPARATOR;
- }
- function manageManifestFile(): string
- {
- return manageReleasesDir() . "manifest.json";
- }
- function managePackagesDir(): string
- {
- return manageReleasesDir() . "packages" . DIRECTORY_SEPARATOR;
- }
- function manageBackupsDir(): string
- {
- return manageStorageDir() . "backups" . DIRECTORY_SEPARATOR;
- }
- function manageBackupIndexFile(): string
- {
- return manageBackupsDir() . "index.json";
- }
- function manageLogsDir(): string
- {
- return manageStorageDir() . "logs" . DIRECTORY_SEPARATOR;
- }
- function manageS3LogFile(): string
- {
- return manageLogsDir() . "s3.log";
- }
- function manageProductName(): string
- {
- $name = trim((string) MANAGE_PRODUCT_NAME);
- return $name !== "" ? $name : "Managed Application";
- }
- function manageEscape($value): string
- {
- return htmlspecialchars((string) $value, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
- }
- function manageIsVersion(string $version): bool
- {
- return preg_match('/^v\d+\.\d+\.\d+$/', $version) === 1;
- }
- function manageFormatBytes(int $bytes): string
- {
- if ($bytes >= 1073741824) {
- return number_format($bytes / 1073741824, 2, ",", ".") . " GB";
- }
- if ($bytes >= 1048576) {
- return number_format($bytes / 1048576, 2, ",", ".") . " MB";
- }
- if ($bytes >= 1024) {
- return number_format($bytes / 1024, 1, ",", ".") . " KB";
- }
- return $bytes . " B";
- }
- require_once __DIR__ . "/storage.php";
- require_once __DIR__ . "/log.php";
- require_once __DIR__ . "/ratelimit.php";
|