added functions for environment variable management and updated dotenv loading logic
This commit is contained in:
37
common.php
37
common.php
@ -4,6 +4,33 @@
|
|||||||
|
|
||||||
require_once __DIR__ . '/lib/text_sanitize.php';
|
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.
|
* Load KEY=value pairs from .env in the project root.
|
||||||
* Does not override variables already set in the real environment.
|
* 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);
|
$value = substr($value, 1, -1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$existing = getenv($name);
|
if (qdb_env_get($name) !== null) {
|
||||||
if ($existing !== false && $existing !== '') {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
putenv("$name=$value");
|
qdb_env_set($name, $value);
|
||||||
$_ENV[$name] = $value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,8 +76,8 @@ qdb_load_dotenv(__DIR__ . '/.env');
|
|||||||
// --- MASTER-KEY (Server-seitig zum Speichern der DB) ---
|
// --- MASTER-KEY (Server-seitig zum Speichern der DB) ---
|
||||||
// Per ENV 'QDB_MASTER_KEY' (Base64- oder ASCII-String) oder Fallback auf bisherigen Wert.
|
// Per ENV 'QDB_MASTER_KEY' (Base64- oder ASCII-String) oder Fallback auf bisherigen Wert.
|
||||||
function get_master_key_bytes(): string {
|
function get_master_key_bytes(): string {
|
||||||
$env = getenv('QDB_MASTER_KEY');
|
$env = qdb_env_get('QDB_MASTER_KEY');
|
||||||
if (!$env || $env === '') {
|
if ($env === null) {
|
||||||
throw new RuntimeException('QDB_MASTER_KEY environment variable is not set. Cannot proceed without a master key.');
|
throw new RuntimeException('QDB_MASTER_KEY environment variable is not set. Cannot proceed without a master key.');
|
||||||
}
|
}
|
||||||
$b = base64_decode($env, true);
|
$b = base64_decode($env, true);
|
||||||
|
|||||||
Reference in New Issue
Block a user