From aebb62b12ebbb08df2cfa05f7527f63d3fe608a4 Mon Sep 17 00:00:00 2001 From: Abimanyu Ridho Date: Sat, 13 Jun 2026 09:47:48 +0700 Subject: [PATCH] Update Config --- api/config/database.php | 92 +++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 59 deletions(-) diff --git a/api/config/database.php b/api/config/database.php index 6662a19..112c5cc 100644 --- a/api/config/database.php +++ b/api/config/database.php @@ -3,70 +3,44 @@ mysqli_report(MYSQLI_REPORT_OFF); -/** - * Parse and load a .env file into the environment. - * Supports KEY=VALUE format, ignores comments (#) and empty lines. - */ -if (!function_exists('loadEnvFile')) { - function loadEnvFile(string $filePath): void { - if (!is_file($filePath)) { - return; - } - - $lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); - foreach ($lines as $line) { - $line = trim($line); - - // Skip comments and empty lines - if ($line === '' || str_starts_with($line, '#')) { - continue; - } - - // Split on first '=' only - $eqPos = strpos($line, '='); - if ($eqPos === false) { - continue; - } - - $key = trim(substr($line, 0, $eqPos)); - $value = trim(substr($line, $eqPos + 1)); - - // Strip surrounding quotes if present - if ( - strlen($value) >= 2 && - (($value[0] === '"' && $value[-1] === '"') || - ($value[0] === "'" && $value[-1] === "'")) - ) { - $value = substr($value, 1, -1); - } - - // Only set if not already defined by the server/environment - if ($key !== '' && getenv($key) === false) { - putenv("$key=$value"); - $_ENV[$key] = $value; - $_SERVER[$key] = $value; - } - } - } -} - -// Load .env from project root (two levels up from this file) -loadEnvFile(dirname(__DIR__, 2) . '/.env'); - if (!function_exists('loadDatabaseConfig')) { function loadDatabaseConfig(): array { + // Load .env if exists (for local development) + $envFile = dirname(__DIR__, 2) . '/.env'; + if (file_exists($envFile)) { + $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + foreach ($lines as $line) { + if (trim($line) === '' || strpos(trim($line), '#') === 0) continue; + $parts = explode('=', $line, 2); + if (count($parts) === 2) { + $name = trim($parts[0]); + $value = trim($parts[1]); + // Strip quotes if any + if (strlen($value) >= 2 && ($value[0] === '"' || $value[0] === "'") && $value[0] === $value[-1]) { + $value = substr($value, 1, -1); + } + if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) { + putenv(sprintf('%s=%s', $name, $value)); + $_ENV[$name] = $value; + $_SERVER[$name] = $value; + } + } + } + } + + // Use $_SERVER or $_ENV first as Coolify/Docker usually passes them there, fallback to getenv $config = [ - 'host' => getenv('DB_HOST') ?: 'localhost', - 'user' => getenv('DB_USER') ?: 'root', - 'pass' => getenv('DB_PASS') ?: '', - 'name' => getenv('DB_NAME') ?: 'webgis_class', - 'port' => (int)(getenv('DB_PORT') ?: 3306), - 'charset' => getenv('DB_CHARSET') ?: 'utf8mb4', + 'host' => $_SERVER['DB_HOST'] ?? $_ENV['DB_HOST'] ?? getenv('DB_HOST') ?: '127.0.0.1', + 'user' => $_SERVER['DB_USER'] ?? $_ENV['DB_USER'] ?? getenv('DB_USER') ?: 'root', + 'pass' => $_SERVER['DB_PASS'] ?? $_ENV['DB_PASS'] ?? getenv('DB_PASS') ?: '', + 'name' => $_SERVER['DB_NAME'] ?? $_ENV['DB_NAME'] ?? getenv('DB_NAME') ?: 'webgis_class', + 'port' => (int)($_SERVER['DB_PORT'] ?? $_ENV['DB_PORT'] ?? getenv('DB_PORT') ?: 3306), + 'charset' => $_SERVER['DB_CHARSET'] ?? $_ENV['DB_CHARSET'] ?? getenv('DB_CHARSET') ?: 'utf8mb4', ]; - // Allow local override file for development (not committed to git) + // Only allow local override if we are truly in a local environment $localConfigFile = __DIR__ . '/database.local.php'; - if (is_file($localConfigFile)) { + if (is_file($localConfigFile) && ($config['host'] === '127.0.0.1' || $config['host'] === 'localhost')) { $localConfig = require $localConfigFile; if (is_array($localConfig)) { $config = array_merge($config, array_intersect_key($localConfig, $config)); @@ -95,7 +69,7 @@ function getDB(): mysqli { http_response_code(500); echo json_encode([ 'status' => 'error', - 'message' => 'Koneksi database gagal' + 'message' => 'Koneksi database gagal: ' . $conn->connect_error // Add actual error message for debugging ]); exit; }