netgescon-day0/public/adminer.php

98 lines
3.0 KiB
PHP

<?php
/**
* NetGescon Database Visualizer (Adminer Wrapper)
* Direct access to MySQL (256 tables) and PostgreSQL databases
*/
// 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);
}
}
}
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.\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. SESSION SEEDING & AUTO-REDIRECT
if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
$dbName = env_get_val('DB_DATABASE', 'netgescon');
$mySqlUser = env_get_val('DB_USERNAME', 'netgescon_user');
$mySqlPass = env_get_val('DB_PASSWORD', 'NetGescon2024!');
// Pre-authorize session credentials for Adminer
$_SESSION["pwds"]["server"]["127.0.0.1"][$mySqlUser] = $mySqlPass;
$_SESSION["pwds"]["server"]["localhost"][$mySqlUser] = $mySqlPass;
$_SESSION["pwds"]["pgsql"]["127.0.0.1"]["netgescon"] = "netgescon_pass";
$_SESSION["pwds"]["pgsql"]["localhost"]["netgescon"] = "netgescon_pass";
if (empty($_GET) || (isset($_GET['auto']) && $_GET['auto'] === 'mysql')) {
header("Location: adminer.php?server=127.0.0.1&username=" . urlencode($mySqlUser) . "&db=" . urlencode($dbName));
exit;
}
if (isset($_GET['auto']) && $_GET['auto'] === 'pgsql') {
header("Location: adminer.php?pgsql=127.0.0.1&username=netgescon&db=" . urlencode($dbName));
exit;
}
// 3. ADMINER PLUGIN/CUSTOMIZATION
function adminer_object() {
class NetGesconAdminerSecurity extends \Adminer\Adminer {
function name() {
return "NetGescon Database Visualizer";
}
function login($login, $password) {
return true;
}
function database() {
return env_get_val("DB_DATABASE", "netgescon");
}
}
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.');
}