netgescon-day0/public/adminer.php

139 lines
5.3 KiB
PHP

<?php
/**
* NetGescon PostgreSQL & MySQL Secure Web Database Visualizer
*
* SECURE WRAPPER FOR ADMINER
* - Disabled in Production (APP_ENV=production or APP_DEBUG=false)
* - Supports 1-Click Instant Auto-Login via ?auto=mysql or ?auto=pgsql
* - Auto-connects via TCP (127.0.0.1) to avoid Unix Socket Peer Auth failures
*/
// 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. AUTO-LOGIN & DATABASE SELECTION INJECTION
$dbName = env_get_val('DB_DATABASE', 'netgescon');
$mySqlUser = env_get_val('DB_USERNAME', 'netgescon_user');
$mySqlPass = env_get_val('DB_PASSWORD', 'NetGescon2024!');
if (isset($_GET['auto'])) {
if ($_GET['auto'] === 'pgsql') {
$_GET['pgsql'] = '127.0.0.1';
$_GET['username'] = 'netgescon';
$_GET['db'] = $dbName;
$_POST['auth'] = [
'driver' => 'pgsql',
'server' => '127.0.0.1',
'username' => 'netgescon',
'password' => 'netgescon_pass',
'db' => $dbName,
];
} else {
$_GET['server'] = '127.0.0.1';
$_GET['username'] = $mySqlUser;
$_GET['db'] = $dbName;
$_POST['auth'] = [
'driver' => 'server',
'server' => '127.0.0.1',
'username' => $mySqlUser,
'password' => $mySqlPass,
'db' => $dbName,
];
}
}
// 3. ADMINER OBJECT CUSTOMIZATION
function adminer_object() {
class NetGesconAdminerSecurity extends \Adminer\Adminer {
function name() {
return "NetGescon Database Visualizer";
}
function credentials() {
$driver = $_POST["auth"]["driver"] ?? $_GET["driver"] ?? (isset($_GET["pgsql"]) ? "pgsql" : "server");
$dbHost = "127.0.0.1";
if ($driver === "pgsql" || isset($_GET["pgsql"]) || (isset($_GET["auto"]) && $_GET["auto"] === "pgsql")) {
return array($dbHost, "netgescon", "netgescon_pass");
}
$dbUser = env_get_val("DB_USERNAME", "netgescon_user");
$dbPass = env_get_val("DB_PASSWORD", "NetGescon2024!");
return array($dbHost, $dbUser, $dbPass);
}
function database() {
return env_get_val("DB_DATABASE", "netgescon");
}
function login($login, $password) {
return true;
}
function loginForm() {
$mySqlUser = env_get_val("DB_USERNAME", "netgescon_user");
$dbName = env_get_val("DB_DATABASE", "netgescon");
echo '<div style="background:#0f172a; color:#f8fafc; padding:20px; border-radius:12px; margin-bottom:18px; font-family:sans-serif; font-size:13px; line-height:1.6; border:1px solid #334155; box-shadow:0 10px 15px -3px rgba(0,0,0,0.3);">';
echo '<strong style="color:#38bdf8; font-size:16px;">🔒 NetGescon Secure Dual DB Visualizer</strong><br>';
echo 'Clicca su uno dei due link di accesso diretto per aprire l\'elenco tabelle del Database (TCP 127.0.0.1):<br><br>';
echo '<div style="display:flex; gap:12px; flex-wrap:wrap;">';
echo '<a href="adminer.php?auto=mysql" style="background:#0284c7; color:#ffffff; font-weight:bold; border:none; padding:12px 20px; border-radius:8px; text-decoration:none; font-size:13px; shadow:0 4px 6px -1px rgba(0,0,0,0.2);">🐬 APRI TABELLE MYSQL STAGING (' . $mySqlUser . ')</a>';
echo '<a href="adminer.php?auto=pgsql" style="background:#334155; color:#38bdf8; font-weight:bold; border:2px solid #38bdf8; padding:12px 20px; border-radius:8px; text-decoration:none; font-size:13px; shadow:0 4px 6px -1px rgba(0,0,0,0.2);">🐘 APRI TABELLE POSTGRESQL CONSOLIDATO (netgescon)</a>';
echo '</div>';
echo '</div>';
return true;
}
}
return new NetGesconAdminerSecurity;
}
// 4. 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.');
}