From 0c466972c2b37f191bd1484b3cf878aeda9fa052 Mon Sep 17 00:00:00 2001 From: Niek Rabelink Date: Mon, 5 Oct 2026 10:39:17 +0200 Subject: [PATCH] fix(env): lock production hostnames to live environment and live database --- config/.env | 17 ++++++--------- config/config.php | 48 ++++++++++++++++++++++++++++++------------ includes/env_badge.php | 6 +----- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/config/.env b/config/.env index c731486..f7c841c 100644 --- a/config/.env +++ b/config/.env @@ -1,17 +1,14 @@ -# ========================================================================= -# FOCIZ CONFIGURATIE — DEVELOPMENT OMGEVING (dev.fociz.nl) +# ========================================================================= +# FOCIZ CONFIGURATIE (Centraal) +# De omgeving en database worden automatisch veilig bepaald op basis van het domein: +# - dev.fociz.nl / localhost => development (focizdev / focizdev_user) +# - www.fociz.nl / fociz.nl => live (fociz / focizlive_user) +# Optionele serverspecifieke overrides kunnen in de root /.env worden geplaatst. # ========================================================================= -# MariaDB Verbinding +# MariaDB Server DB_HOST="192.168.2.33" DB_PORT="3306" -DB_NAME="focizdev" -DB_USER="focizdev_user" -DB_PASS="F0c1z_Dev_Secure_2026!" - -# Omgeving & URL -APP_ENV="development" -APP_URL="https://dev.fociz.nl" # Applicatie Geheime Sleutel (voor tokenversleuteling & OAuth state) APP_SECRET="fociz-maria-db-super-secret-key-998877" diff --git a/config/config.php b/config/config.php index e04cbbf..f6dcfca 100644 --- a/config/config.php +++ b/config/config.php @@ -25,9 +25,9 @@ if (!headers_sent()) { } @ini_set('expose_php', 'off'); -// 1. .env inlezing (controleert zowel config/.env als hoofdmap .env) +// 1. .env inlezing (controleert eerst server root .env en dan config/.env) $envVars = []; -$envLocations = [__DIR__ . '/.env', __DIR__ . '/../.env']; +$envLocations = [__DIR__ . '/../.env', __DIR__ . '/.env']; foreach ($envLocations as $envFile) { if (file_exists($envFile)) { $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); @@ -81,30 +81,52 @@ if (!function_exists('fociz_env')) { define('APP_NAME', 'Fociz'); // Automatische omgevingsdetectie: -// 1. Expliciete .env variabele 'APP_ENV' -// 2. Automatisch 'development' indien hostname 'dev.' of 'localhost' bevat -// 3. Standaard 'live' -$detectedHost = $_SERVER['HTTP_HOST'] ?? ''; +// 1. Hostname/domeinnaam is altijd leidend voor productiebeveiliging +$detectedHost = strtolower($_SERVER['HTTP_HOST'] ?? ''); $isDevHost = ( str_contains($detectedHost, 'dev.') || str_contains($detectedHost, 'localhost') || str_contains($detectedHost, '127.0.0.1') ); -$defaultEnv = $isDevHost ? 'development' : 'live'; -define('APP_ENV', fociz_env('APP_ENV', $defaultEnv)); +$isLiveHost = ( + !empty($detectedHost) && + !$isDevHost && + (str_contains($detectedHost, 'fociz.nl') || str_contains($detectedHost, 'live')) +); + +// Bepaal APP_ENV: op live domeinen (bijv. www.fociz.nl of fociz.nl) ALTIJD 'live' +// zodat een per ongeluk meegekomen dev-configuratie NOOIT productie kan beïnvloeden. +if ($isLiveHost) { + $effectiveEnv = 'live'; +} elseif ($isDevHost) { + $effectiveEnv = 'development'; +} else { + $effectiveEnv = fociz_env('APP_ENV', 'live'); +} +define('APP_ENV', $effectiveEnv); define('APP_SECRET', fociz_env('APP_SECRET', 'fociz-maria-db-super-secret-key-998877')); // ========================================================================= // 3. CENTRALE DATABASE CONFIGURATIE (OMGEVINGSAFHANKELIJK & GIT-VEILIG) // ========================================================================= -// Leest eerst eventuele .env variabelen (die per server uniek en gitignored zijn). -// Indien niet ingesteld via .env, wordt automatisch de juiste database gekozen op basis van APP_ENV. $isDevEnv = (APP_ENV === 'development'); define('DB_HOST', fociz_env('DB_HOST', '192.168.2.33')); define('DB_PORT', fociz_env('DB_PORT', '3306')); -define('DB_NAME', fociz_env('DB_NAME', $isDevEnv ? 'focizdev' : 'fociz')); -define('DB_USER', fociz_env('DB_USER', $isDevEnv ? 'focizdev_user' : 'focizlive_user')); -define('DB_PASS', fociz_env('DB_PASS', $isDevEnv ? 'F0c1z_Dev_Secure_2026!' : 'F0c1z_Live_Secure_2026!')); + +// Veiligheidswaarborg: op het live domein of live omgeving NOOIT per ongeluk de dev database gebruiken +if ($isLiveHost || APP_ENV === 'live') { + $rawDbName = fociz_env('DB_NAME'); + $rawDbUser = fociz_env('DB_USER'); + $rawDbPass = fociz_env('DB_PASS'); + + define('DB_NAME', (!empty($rawDbName) && $rawDbName !== 'focizdev') ? $rawDbName : 'fociz'); + define('DB_USER', (!empty($rawDbUser) && $rawDbUser !== 'focizdev_user') ? $rawDbUser : 'focizlive_user'); + define('DB_PASS', (!empty($rawDbPass) && $rawDbPass !== 'F0c1z_Dev_Secure_2026!') ? $rawDbPass : 'F0c1z_Live_Secure_2026!'); +} else { + define('DB_NAME', fociz_env('DB_NAME', 'focizdev')); + define('DB_USER', fociz_env('DB_USER', 'focizdev_user')); + define('DB_PASS', fociz_env('DB_PASS', 'F0c1z_Dev_Secure_2026!')); +} define('DB_CHARSET', 'utf8mb4'); // Bepaal de dynamische of geconfigureerde applicatie URL diff --git a/includes/env_badge.php b/includes/env_badge.php index f34e2ed..4c6ff3f 100644 --- a/includes/env_badge.php +++ b/includes/env_badge.php @@ -14,13 +14,9 @@ require_once __DIR__ . '/../config/config.php'; -// Standaard waarde voor de main branch: 'live' (kan overschreven worden via APP_ENV) +// Standaard waarde voor de main branch: 'live' (bepaald via centrale config.php) $environment = defined('APP_ENV') ? APP_ENV : 'live'; -if (function_exists('fociz_env') && fociz_env('APP_ENV')) { - $environment = fociz_env('APP_ENV'); -} - $isLive = in_array(strtolower(trim($environment)), ['live', 'production', 'prod', 'main']); ?>