This commit is contained in:
Flatlogic Bot 2026-03-25 16:40:11 +00:00
parent fe7ebfad4d
commit cdb6be6798
3 changed files with 6585 additions and 22 deletions

View File

@ -90,6 +90,11 @@ class MailService
} }
private static function loadConfig(): array private static function loadConfig(): array
{ {
static $cachedConfig = null;
if (is_array($cachedConfig)) {
return $cachedConfig;
}
$configPath = __DIR__ . '/config.php'; $configPath = __DIR__ . '/config.php';
if (!file_exists($configPath)) { if (!file_exists($configPath)) {
throw new \RuntimeException('Mail config not found. Copy mail/config.sample.php to mail/config.php and fill in credentials.'); throw new \RuntimeException('Mail config not found. Copy mail/config.sample.php to mail/config.php and fill in credentials.');
@ -98,7 +103,9 @@ class MailService
if (!is_array($cfg)) { if (!is_array($cfg)) {
throw new \RuntimeException('Invalid mail config format: expected array'); throw new \RuntimeException('Invalid mail config format: expected array');
} }
return $cfg;
$cachedConfig = $cfg;
return $cachedConfig;
} }
// Send a contact message // Send a contact message

View File

@ -2,34 +2,38 @@
// Mail configuration sourced from environment variables. // Mail configuration sourced from environment variables.
// No secrets are stored here; the file just maps env -> config array for MailService. // No secrets are stored here; the file just maps env -> config array for MailService.
function env_val(string $key, $default = null) { if (!function_exists('env_val')) {
$v = getenv($key); function env_val(string $key, $default = null) {
return ($v === false || $v === null || $v === '') ? $default : $v; $v = getenv($key);
return ($v === false || $v === null || $v === '') ? $default : $v;
}
} }
// Fallback: if critical vars are missing from process env, try to parse executor/.env // Fallback: if critical vars are missing from process env, try to parse executor/.env
// This helps in web/Apache contexts where .env is not exported. // This helps in web/Apache contexts where .env is not exported.
// Supports simple KEY=VALUE lines; ignores quotes and comments. // Supports simple KEY=VALUE lines; ignores quotes and comments.
function load_dotenv_if_needed(array $keys): void { if (!function_exists('load_dotenv_if_needed')) {
$missing = array_filter($keys, fn($k) => getenv($k) === false || getenv($k) === ''); function load_dotenv_if_needed(array $keys): void {
if (empty($missing)) return; $missing = array_filter($keys, fn($k) => getenv($k) === false || getenv($k) === '');
static $loaded = false; if (empty($missing)) return;
if ($loaded) return; static $loaded = false;
$envPath = realpath(__DIR__ . '/../../.env'); // executor/.env if ($loaded) return;
if ($envPath && is_readable($envPath)) { $envPath = realpath(__DIR__ . '/../../.env'); // executor/.env
$lines = @file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: []; if ($envPath && is_readable($envPath)) {
foreach ($lines as $line) { $lines = @file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [];
if ($line[0] === '#' || trim($line) === '') continue; foreach ($lines as $line) {
if (!str_contains($line, '=')) continue; if ($line[0] === '#' || trim($line) === '') continue;
[$k, $v] = array_map('trim', explode('=', $line, 2)); if (!str_contains($line, '=')) continue;
// Strip potential surrounding quotes [$k, $v] = array_map('trim', explode('=', $line, 2));
$v = trim($v, "\"' "); // Strip potential surrounding quotes
// Do not override existing env $v = trim($v, "\"' " );
if ($k !== '' && (getenv($k) === false || getenv($k) === '')) { // Do not override existing env
putenv("{$k}={$v}"); if ($k !== '' && (getenv($k) === false || getenv($k) === '')) {
putenv("{$k}={$v}");
}
} }
$loaded = true;
} }
$loaded = true;
} }
} }

File diff suppressed because it is too large Load Diff