diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..5d98719 --- /dev/null +++ b/.env.example @@ -0,0 +1,6 @@ +DB_HOST= +DB_USER=root +DB_PASS= +DB_NAME=webgis_class +DB_PORT=3306 +DB_CHARSET=utf8mb4 diff --git a/.gitignore b/.gitignore index 1ea6781..0c4beec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ .env -.env.* api/config/database.local.php *.sql !database.sql diff --git a/api/config/database.php b/api/config/database.php index 816c251..6662a19 100644 --- a/api/config/database.php +++ b/api/config/database.php @@ -3,16 +3,68 @@ 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 { $config = [ - 'host' => getenv('DB_HOST') ?: 'localhost', - 'user' => getenv('DB_USER') ?: 'root', - 'pass' => getenv('DB_PASS') ?: '', - 'name' => getenv('DB_NAME') ?: 'webgis', + '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', ]; + // Allow local override file for development (not committed to git) $localConfigFile = __DIR__ . '/database.local.php'; if (is_file($localConfigFile)) { $localConfig = require $localConfigFile; @@ -27,16 +79,17 @@ if (!function_exists('loadDatabaseConfig')) { $dbConfig = loadDatabaseConfig(); -define('DB_HOST', $dbConfig['host']); -define('DB_USER', $dbConfig['user']); -define('DB_PASS', $dbConfig['pass']); -define('DB_NAME', $dbConfig['name']); +define('DB_HOST', $dbConfig['host']); +define('DB_USER', $dbConfig['user']); +define('DB_PASS', $dbConfig['pass']); +define('DB_NAME', $dbConfig['name']); +define('DB_PORT', $dbConfig['port']); define('DB_CHARSET', $dbConfig['charset']); function getDB(): mysqli { static $conn = null; if ($conn === null) { - $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); + $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT); if ($conn->connect_error) { error_log('DB connection failed: ' . $conn->connect_error); http_response_code(500);