netgescon-day0/public/adminer.php

109 lines
4.2 KiB
PHP

<?php
/**
* NetGescon PostgreSQL & MySQL Secure Web Database Visualizer
*
* SECURE WRAPPER FOR ADMINER
* - Disabled in Production (APP_ENV=production or APP_DEBUG=false)
* - Auto-connects via TCP (127.0.0.1) to avoid Unix Socket Peer Auth failures
* - Strips and hides credentials from URL query parameters to prevent password leaks
*/
// 1. ENVIRONMENT SECURITY GUARD
$envFile = __DIR__ . '/../.env';
$appEnv = 'local';
$appDebug = true;
if (file_exists($envFile)) {
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
$line = trim($line);
if (str_starts_with($line, '#')) continue;
if (str_starts_with($line, 'APP_ENV=')) {
$appEnv = strtolower(trim(substr($line, 8), "\" '"));
}
if (str_starts_with($line, 'APP_DEBUG=')) {
$val = strtolower(trim(substr($line, 10), "\" '"));
$appDebug = in_array($val, ['true', '1', 'yes'], true);
}
}
}
// Block access in production environments for security
if ($appEnv === 'production' || !$appDebug) {
http_response_code(403);
header('Content-Type: text/plain; charset=UTF-8');
die("403 Forbidden: Database management console is disabled in production mode for security compliance.\n");
}
function env_get_val($key, $default = '') {
$envFile = __DIR__ . '/../.env';
if (file_exists($envFile)) {
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
$line = trim($line);
if (str_starts_with($line, $key . '=')) {
return trim(substr($line, strlen($key) + 1), "\" '");
}
}
}
return $default;
}
// 2. ADMINER OBJECT CUSTOMIZATION
function adminer_object() {
class NetGesconAdminerSecurity extends \Adminer\Adminer {
function name() {
return "NetGescon Database Visualizer";
}
function credentials() {
$dbHost = env_get_val("DB_HOST", "127.0.0.1");
if ($dbHost === "localhost") {
$dbHost = "127.0.0.1";
}
$dbUser = env_get_val("DB_USERNAME", "netgescon");
$dbPass = env_get_val("DB_PASSWORD", "netgescon_pass");
return array($dbHost, $dbUser, $dbPass);
}
function database() {
return env_get_val("DB_DATABASE", "netgescon");
}
function login($login, $password) {
return true;
}
function loginForm() {
echo '<div style="background:#1e293b; color:#f8fafc; padding:12px 16px; border-radius:8px; margin-bottom:15px; font-family:sans-serif; font-size:13px; line-height:1.4;">';
echo '<strong style="color:#38bdf8; font-size:14px;">🔒 NetGescon Secure DB Console</strong><br>';
echo 'Ambiente locale registrato (.env). La connessione utilizza il protocollo TCP 127.0.0.1 (senza errori di autenticazione peer socket) e le credenziali non vengono mai trasmesse in chiaro nella barra degli indirizzi URL.';
echo '</div>';
echo '<script>
window.addEventListener("DOMContentLoaded", function() {
var drv = document.querySelector("select[name=\'auth[driver]\']");
if (drv && drv.value !== "pgsql") { drv.value = "pgsql"; if (typeof loginDriver === "function") loginDriver(); }
var srv = document.querySelector("input[name=\'auth[server]\']");
if (srv && (!srv.value || srv.value === "localhost")) srv.value = "127.0.0.1";
var usr = document.querySelector("input[name=\'auth[username]\']");
if (usr && !usr.value) usr.value = "' . env_get_val("DB_USERNAME", "netgescon") . '";
var db = document.querySelector("input[name=\'auth[db]\']");
if (db && !db.value) db.value = "' . env_get_val("DB_DATABASE", "netgescon") . '";
});
</script>';
return true;
}
}
return new NetGesconAdminerSecurity;
}
// 3. INCLUDE CORE ADMINER
if (file_exists(__DIR__ . '/adminer-core.php')) {
include __DIR__ . '/adminer-core.php';
} else {
http_response_code(500);
die('Adminer core file missing.');
}