From fc4206cce87c9d3cf2f2019d072e335b5e7a3b3d Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Fri, 15 May 2026 21:59:06 +0300 Subject: [PATCH] Update: 2026-05-15 21:59:06 --- backend/config/db.php | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/backend/config/db.php b/backend/config/db.php index 13b5c14..d185f8b 100644 --- a/backend/config/db.php +++ b/backend/config/db.php @@ -23,27 +23,34 @@ $envData = []; foreach ($searchPaths as $path) { if (!empty($path) && file_exists($path . '/.env')) { try { - // First try native parsing (bulletproof) - $parsed = parse_ini_file($path . '/.env', false, INI_SCANNER_RAW); - if ($parsed !== false) { - foreach ($parsed as $key => $value) { - $_ENV[$key] = trim($value); - $_SERVER[$key] = trim($value); - putenv("$key=" . trim($value)); + // Manual parsing to handle comments and raw strings gracefully + $lines = file($path . '/.env', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + $parsed = true; + foreach ($lines as $line) { + $line = trim($line); + if (empty($line) || strpos($line, '#') === 0 || strpos($line, ';') === 0) continue; + + if (strpos($line, '=') !== false) { + list($key, $value) = explode('=', $line, 2); + $key = trim($key); + $value = trim($value, " \t\n\r\0\x0B\"'"); // remove quotes + $_ENV[$key] = $value; + $_SERVER[$key] = $value; + putenv("$key=$value"); } - $envLoaded = true; - break; - } - - // Fallback to Dotenv library - if (class_exists('Dotenv\Dotenv')) { - $dotenv = Dotenv\Dotenv::createImmutable($path); - $dotenv->load(); - $envLoaded = true; - break; } + $envLoaded = true; + break; } catch (Exception $e) { - // Try next path + // Fallback to Dotenv library if manual parsing somehow fails + if (class_exists('Dotenv\Dotenv')) { + try { + $dotenv = Dotenv\Dotenv::createImmutable($path); + $dotenv->load(); + $envLoaded = true; + break; + } catch (Exception $inner) {} + } } } }