Fix HTTP 500 on live after login, unify database config in config.php, sanitize env example, and harden pentest security
This commit is contained in:
+85
-46
@@ -1,44 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* Fociz Applicatie- en Databaseconfiguratie
|
||||
* Laadt automatisch .env (of .env.example indien .env niet bestaat)
|
||||
* Fociz Centrale Applicatie- en Databaseconfiguratie
|
||||
* Alle configuratie staat overzichtelijk en veilig in dit ene bestand.
|
||||
*/
|
||||
|
||||
// Blokkeer directe HTTP toegang tot dit configuratiebestand (Pen Test Beveiliging)
|
||||
if (basename($_SERVER['SCRIPT_FILENAME'] ?? '') === 'config.php') {
|
||||
http_response_code(403);
|
||||
exit('Toegang geweigerd.');
|
||||
}
|
||||
|
||||
// Stuur direct strenge beveiligingsheaders mee op elk verzoek (Pen Test Beveiliging)
|
||||
if (!headers_sent()) {
|
||||
header('X-Content-Type-Options: nosniff');
|
||||
header('X-Frame-Options: SAMEORIGIN');
|
||||
header('X-XSS-Protection: 1; mode=block');
|
||||
header('Referrer-Policy: strict-origin-when-cross-origin');
|
||||
header('Permissions-Policy: camera=(), microphone=(), geolocation=()');
|
||||
header_remove('X-Powered-By');
|
||||
}
|
||||
@ini_set('expose_php', 'off');
|
||||
|
||||
// 1. Optionele .env inlezing uit de hoofdmap (uitsluitend .env, GEEN .env.example fallback)
|
||||
$envVars = [];
|
||||
|
||||
// 1. Zoek naar .env of val terug op config/.env.example
|
||||
$envCandidates = [
|
||||
__DIR__ . '/../.env',
|
||||
__DIR__ . '/.env.example',
|
||||
__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) {
|
||||
$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\"'");
|
||||
|
||||
// Sla op in array en zet in globale PHP variabelen
|
||||
$envVars[$name] = $value;
|
||||
$_ENV[$name] = $value;
|
||||
$_SERVER[$name] = $value;
|
||||
@putenv("{$name}={$value}");
|
||||
}
|
||||
$envFile = __DIR__ . '/../.env';
|
||||
if (file_exists($envFile)) {
|
||||
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
if ($lines !== false) {
|
||||
foreach ($lines as $line) {
|
||||
$line = trim($line);
|
||||
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, " \t\n\r\0\x0B\"'");
|
||||
$envVars[$name] = $value;
|
||||
$_ENV[$name] = $value;
|
||||
$_SERVER[$name] = $value;
|
||||
@putenv("{$name}={$value}");
|
||||
}
|
||||
}
|
||||
break; // Stop zodra we het eerste geldige bestand hebben ingeladen
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +68,9 @@ if (!function_exists('fociz_env')) {
|
||||
}
|
||||
}
|
||||
|
||||
// MariaDB Verbindingsgegevens (uit .env of .env.example)
|
||||
// =========================================================================
|
||||
// 2. CENTRALE DATABASE CONFIGURATIE (DE ENIGE BRON VAN WAARHEID)
|
||||
// =========================================================================
|
||||
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', 'fociz'));
|
||||
@@ -73,15 +78,22 @@ define('DB_USER', fociz_env('DB_USER', 'focizlive_user'));
|
||||
define('DB_PASS', fociz_env('DB_PASS', 'F0c1z_Live_Secure_2026!'));
|
||||
define('DB_CHARSET', 'utf8mb4');
|
||||
|
||||
// =========================================================================
|
||||
// 3. APPLICATIE INSTELLINGEN & OMGEVING
|
||||
// =========================================================================
|
||||
define('APP_NAME', 'Fociz');
|
||||
define('APP_ENV', fociz_env('APP_ENV', 'live')); // 'live' of 'development'
|
||||
define('APP_SECRET', fociz_env('APP_SECRET', 'fociz-maria-db-super-secret-key-998877'));
|
||||
|
||||
// Bepaal de dynamische of geconfigureerde applicatie URL
|
||||
if (!function_exists('fociz_detect_app_url')) {
|
||||
function fociz_detect_app_url(): string {
|
||||
$envUrl = fociz_env('APP_URL');
|
||||
if (!empty($envUrl) && !str_contains($envUrl, 'uw-domein.nl') && !str_contains($envUrl, 'www.fociz.nl')) {
|
||||
if (!empty($envUrl) && !str_contains($envUrl, 'uw-domein.nl')) {
|
||||
return rtrim($envUrl, '/');
|
||||
}
|
||||
|
||||
// Dynamische detectie op basis van het huidige domein (werkt direct op dev.fociz.nl én fociz.nl)
|
||||
// Dynamische detectie op basis van het huidige domein (werkt direct op www.fociz.nl én dev.fociz.nl)
|
||||
if (!empty($_SERVER['HTTP_HOST'])) {
|
||||
$isHttps = (
|
||||
(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ||
|
||||
@@ -93,22 +105,49 @@ if (!function_exists('fociz_detect_app_url')) {
|
||||
return $scheme . $_SERVER['HTTP_HOST'];
|
||||
}
|
||||
|
||||
// Standaard fallback per omgeving
|
||||
return (defined('APP_ENV') && APP_ENV === 'live') ? 'https://fociz.nl' : 'https://dev.fociz.nl';
|
||||
return (defined('APP_ENV') && APP_ENV === 'live') ? 'https://www.fociz.nl' : 'https://dev.fociz.nl';
|
||||
}
|
||||
}
|
||||
|
||||
// Applicatie instellingen
|
||||
define('APP_NAME', 'Fociz');
|
||||
define('APP_ENV', fociz_env('APP_ENV', 'live')); // 'development' of 'live'
|
||||
define('APP_URL', fociz_detect_app_url());
|
||||
define('APP_SECRET', fociz_env('APP_SECRET', 'fociz-maria-db-super-secret-key-998877'));
|
||||
|
||||
// Tijdzone
|
||||
date_default_timezone_set('Europe/Amsterdam');
|
||||
|
||||
// Foutrapportage: in development tonen we fouten direct op het scherm
|
||||
if (APP_ENV !== 'live') {
|
||||
// =========================================================================
|
||||
// 4. VEILIGE FOUTAFHANDELING & PEN TEST BESCHERMING
|
||||
// =========================================================================
|
||||
if (APP_ENV === 'live') {
|
||||
ini_set('display_errors', '0');
|
||||
ini_set('display_startup_errors', '0');
|
||||
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
|
||||
|
||||
// Registreer een nette noodfoutpagina voor onverhoopte fatale PHP fouten
|
||||
register_shutdown_function(function() {
|
||||
$error = error_get_last();
|
||||
if ($error && in_array($error['type'], [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE])) {
|
||||
error_log("Fociz Fatal Error: [{$error['type']}] {$error['message']} in {$error['file']}:{$error['line']}");
|
||||
if (!headers_sent()) {
|
||||
http_response_code(500);
|
||||
}
|
||||
if (isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'ADMIN') {
|
||||
echo "<div style='font-family: monospace; background: #0c0f17; color: #ef4444; padding: 25px; margin: 20px; border-radius: 12px; border: 1px solid #dc2626;'>
|
||||
<h3>⚠️ Fociz Systeemfout (Alleen zichtbaar voor Beheerder)</h3>
|
||||
<p><strong>Fout:</strong> " . htmlspecialchars($error['message']) . "</p>
|
||||
<p><strong>Bestand:</strong> " . htmlspecialchars($error['file']) . " op regel " . $error['line'] . "</p>
|
||||
</div>";
|
||||
} else {
|
||||
echo "<div style='font-family: sans-serif; background: #0c0f17; color: #f8fafc; min-height: 100vh; display: flex; align-items: center; justify-content: center;'>
|
||||
<div style='max-width: 480px; text-align: center; padding: 40px;'>
|
||||
<div style='width: 48px; height: 48px; margin: 0 auto 16px; border-radius: 12px; background: rgba(99, 102, 241, 0.1); border: 1px solid rgba(99, 102, 241, 0.3); display: flex; align-items: center; justify-content: center; color: #818cf8; font-weight: bold;'>F</div>
|
||||
<h2 style='font-size: 20px; margin-bottom: 8px;'>Er is iets misgegaan</h2>
|
||||
<p style='color: #94a3b8; font-size: 14px; line-height: 1.5;'>De pagina kon niet worden geladen. Probeer de pagina te vernieuwen of log opnieuw in.</p>
|
||||
<a href='/auth/login.php' style='display: inline-block; margin-top: 20px; padding: 10px 20px; border-radius: 8px; background: #4f46e5; color: white; text-decoration: none; font-size: 13px; font-weight: 600;'>Naar Inloggen</a>
|
||||
</div>
|
||||
</div>";
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
ini_set('display_errors', '1');
|
||||
ini_set('display_startup_errors', '1');
|
||||
error_reporting(E_ALL);
|
||||
|
||||
Reference in New Issue
Block a user