fix: support loading both .env and .env.example with reliable env parser and APP_ENV support
This commit is contained in:
@@ -5,6 +5,9 @@ DB_NAME="focizdev"
|
||||
DB_USER="focizdev_user"
|
||||
DB_PASS="F0c1z_Dev_Secure_2026!"
|
||||
|
||||
# Omgeving: 'development' (knippert rood) of 'live' (knippert groen)
|
||||
APP_ENV="development"
|
||||
|
||||
# Applicatie URL (voor uitnodigingslinks)
|
||||
APP_URL="https://dev.fociz.nl"
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
# MariaDB Verbinding
|
||||
DB_HOST="127.0.0.1"
|
||||
DB_PORT="3306"
|
||||
DB_NAME="fociz"
|
||||
DB_USER="root"
|
||||
DB_PASS=""
|
||||
|
||||
# Omgeving: 'development' (knippert rood) of 'live' (knippert groen)
|
||||
APP_ENV="development"
|
||||
|
||||
# Applicatie URL (voor uitnodigingslinks)
|
||||
APP_URL="https://uw-domein.nl"
|
||||
|
||||
# Applicatie Geheime Sleutel (willekeurige tekenreeks)
|
||||
APP_SECRET="fociz-maria-db-super-secret-key-998877"
|
||||
+57
-15
@@ -1,40 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* Fociz Applicatie- en Databaseconfiguratie
|
||||
* Laadt automatisch .env (of .env.example indien .env niet bestaat)
|
||||
*/
|
||||
|
||||
// Automatisch eenvoudige .env ondersteuning indien aanwezig
|
||||
$envFile = __DIR__ . '/../.env';
|
||||
if (file_exists($envFile)) {
|
||||
$envVars = [];
|
||||
|
||||
// 1. Zoek naar .env of val terug op .env.example
|
||||
$envCandidates = [
|
||||
__DIR__ . '/../.env',
|
||||
__DIR__ . '/../.env.example',
|
||||
];
|
||||
|
||||
foreach ($envCandidates as $envFile) {
|
||||
if (file_exists($envFile)) {
|
||||
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
if ($lines !== false) {
|
||||
foreach ($lines as $line) {
|
||||
if (str_starts_with(trim($line), '#')) continue;
|
||||
$line = trim($line);
|
||||
// Negeer lege regels en commentaarregels (# of //)
|
||||
if ($line === '' || str_starts_with($line, '#') || str_starts_with($line, '//')) {
|
||||
continue;
|
||||
}
|
||||
if (strpos($line, '=') !== false) {
|
||||
list($name, $value) = explode('=', $line, 2);
|
||||
$name = trim($name);
|
||||
$value = trim($value);
|
||||
// Verwijder omlijstende aanhalingstekens (" of ')
|
||||
$value = trim($value, " \t\n\r\0\x0B\"'");
|
||||
if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) {
|
||||
putenv(sprintf('%s=%s', $name, $value));
|
||||
|
||||
// Sla op in array en zet in globale PHP variabelen
|
||||
$envVars[$name] = $value;
|
||||
$_ENV[$name] = $value;
|
||||
$_SERVER[$name] = $value;
|
||||
@putenv("{$name}={$value}");
|
||||
}
|
||||
}
|
||||
}
|
||||
break; // Stop zodra we het eerste geldige bestand hebben ingeladen
|
||||
}
|
||||
}
|
||||
|
||||
// MariaDB Verbindingsgegevens (pas deze aan naar uw server instellingen of gebruik .env)
|
||||
define('DB_HOST', getenv('DB_HOST') ?: '127.0.0.1');
|
||||
define('DB_PORT', getenv('DB_PORT') ?: '3306');
|
||||
define('DB_NAME', getenv('DB_NAME') ?: 'focizdev');
|
||||
define('DB_USER', getenv('DB_USER') ?: 'focizdev_user');
|
||||
define('DB_PASS', getenv('DB_PASS') !== false ? getenv('DB_PASS') : 'F0c1z_Dev_Secure_2026!');
|
||||
/**
|
||||
* Veilige helper om een omgevingsvariabele op te vragen
|
||||
*/
|
||||
if (!function_exists('fociz_env')) {
|
||||
function fociz_env(string $key, $default = null) {
|
||||
global $envVars;
|
||||
if (isset($envVars[$key]) && $envVars[$key] !== '') {
|
||||
return $envVars[$key];
|
||||
}
|
||||
if (isset($_ENV[$key]) && $_ENV[$key] !== '') {
|
||||
return $_ENV[$key];
|
||||
}
|
||||
if (isset($_SERVER[$key]) && $_SERVER[$key] !== '') {
|
||||
return $_SERVER[$key];
|
||||
}
|
||||
$val = @getenv($key);
|
||||
if ($val !== false && $val !== '') {
|
||||
return $val;
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
// MariaDB Verbindingsgegevens (uit .env of .env.example)
|
||||
define('DB_HOST', fociz_env('DB_HOST', '127.0.0.1'));
|
||||
define('DB_PORT', fociz_env('DB_PORT', '3306'));
|
||||
define('DB_NAME', fociz_env('DB_NAME', 'fociz'));
|
||||
define('DB_USER', fociz_env('DB_USER', 'root'));
|
||||
define('DB_PASS', fociz_env('DB_PASS', ''));
|
||||
define('DB_CHARSET', 'utf8mb4');
|
||||
|
||||
// Applicatie instellingen
|
||||
define('APP_NAME', 'Fociz');
|
||||
define('APP_URL', getenv('APP_URL') ?: ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://') . ($_SERVER['HTTP_HOST'] ?? 'localhost')));
|
||||
define('APP_SECRET', getenv('APP_SECRET') ?: 'fociz-maria-db-super-secret-key-998877');
|
||||
define('APP_URL', fociz_env('APP_URL', ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://') . ($_SERVER['HTTP_HOST'] ?? 'localhost'))));
|
||||
define('APP_SECRET', fociz_env('APP_SECRET', 'fociz-maria-db-super-secret-key-998877'));
|
||||
define('APP_ENV', fociz_env('APP_ENV', 'development')); // 'development' of 'live'
|
||||
|
||||
// Tijdzone
|
||||
date_default_timezone_set('Europe/Amsterdam');
|
||||
|
||||
|
||||
+8
-13
@@ -9,23 +9,19 @@
|
||||
* - OP DE DEV OMGEVING: $environment = 'development'; (Rood met 'Development' + radar ping)
|
||||
* - OP DE MAIN OMGEVING: $environment = 'live'; (Groen met 'Live' + radar ping)
|
||||
*
|
||||
* U kunt de waarde hieronder aanpassen, of instellen via APP_ENV in .env.
|
||||
* U kunt dit hieronder aanpassen, of instellen via APP_ENV in .env / .env.example!
|
||||
*/
|
||||
|
||||
// Bepaal de omgeving: 'development' of 'live' (of 'main')
|
||||
$environment = 'development'; // <-- ZET DIT OP DE MAIN SERVER OP 'live'
|
||||
require_once __DIR__ . '/../config/config.php';
|
||||
|
||||
// Optionele fallback als het in .env gedefinieerd is
|
||||
if (getenv('APP_ENV')) {
|
||||
$envVal = strtolower(trim(getenv('APP_ENV')));
|
||||
if (in_array($envVal, ['live', 'production', 'prod', 'main'])) {
|
||||
$environment = 'live';
|
||||
} elseif (in_array($envVal, ['dev', 'development', 'staging', 'test'])) {
|
||||
$environment = 'development';
|
||||
}
|
||||
// Standaard waarde (wordt overschreven als APP_ENV in .env staat)
|
||||
$environment = defined('APP_ENV') ? APP_ENV : 'development';
|
||||
|
||||
if (function_exists('fociz_env') && fociz_env('APP_ENV')) {
|
||||
$environment = fociz_env('APP_ENV');
|
||||
}
|
||||
|
||||
$isLive = in_array(strtolower($environment), ['live', 'production', 'prod', 'main']);
|
||||
$isLive = in_array(strtolower(trim($environment)), ['live', 'production', 'prod', 'main']);
|
||||
?>
|
||||
|
||||
<!-- FLASHY OMGEVINGSINDICATOR LINKS BOVENIN -->
|
||||
@@ -66,4 +62,3 @@ $isLive = in_array(strtolower($environment), ['live', 'production', 'prod', 'mai
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user