diff --git a/common.php b/common.php index 0d82899..0e377c7 100644 --- a/common.php +++ b/common.php @@ -4,6 +4,33 @@ require_once __DIR__ . '/lib/text_sanitize.php'; +/** + * Read an environment variable (getenv, then $_ENV / $_SERVER). + * PHP-FPM often disables putenv(); .env is still loaded into $_ENV. + */ +function qdb_env_get(string $name): ?string { + $v = getenv($name); + if ($v !== false && $v !== '') { + return $v; + } + if (isset($_ENV[$name]) && $_ENV[$name] !== '') { + return (string)$_ENV[$name]; + } + if (isset($_SERVER[$name]) && $_SERVER[$name] !== '') { + return (string)$_SERVER[$name]; + } + return null; +} + +/** Set an environment variable for the current request (best-effort putenv). */ +function qdb_env_set(string $name, string $value): void { + $_ENV[$name] = $value; + $_SERVER[$name] = $value; + if (function_exists('putenv')) { + @putenv("$name=$value"); + } +} + /** * Load KEY=value pairs from .env in the project root. * Does not override variables already set in the real environment. @@ -37,12 +64,10 @@ function qdb_load_dotenv(string $path): void { $value = substr($value, 1, -1); } } - $existing = getenv($name); - if ($existing !== false && $existing !== '') { + if (qdb_env_get($name) !== null) { continue; } - putenv("$name=$value"); - $_ENV[$name] = $value; + qdb_env_set($name, $value); } } @@ -51,8 +76,8 @@ qdb_load_dotenv(__DIR__ . '/.env'); // --- MASTER-KEY (Server-seitig zum Speichern der DB) --- // Per ENV 'QDB_MASTER_KEY' (Base64- oder ASCII-String) oder Fallback auf bisherigen Wert. function get_master_key_bytes(): string { - $env = getenv('QDB_MASTER_KEY'); - if (!$env || $env === '') { + $env = qdb_env_get('QDB_MASTER_KEY'); + if ($env === null) { throw new RuntimeException('QDB_MASTER_KEY environment variable is not set. Cannot proceed without a master key.'); } $b = base64_decode($env, true);