102 lines
3.2 KiB
PHP
102 lines
3.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
function da_adminer_sso_auth(): ?array
|
|
{
|
|
$auth = $GLOBALS['da_adminer_sso_auth'] ?? null;
|
|
if (!is_array($auth)) {
|
|
return null;
|
|
}
|
|
|
|
$user = isset($auth['user']) && is_string($auth['user']) ? trim($auth['user']) : '';
|
|
$password = isset($auth['password']) && is_string($auth['password']) ? $auth['password'] : '';
|
|
$database = isset($auth['database']) && is_string($auth['database']) ? $auth['database'] : '';
|
|
if ($user === '' || $password === '') {
|
|
return null;
|
|
}
|
|
|
|
return [
|
|
'user' => $user,
|
|
'password' => $password,
|
|
'database' => $database,
|
|
];
|
|
}
|
|
|
|
function da_adminer_bootstrap_auth(array $auth): void
|
|
{
|
|
$user = isset($auth['user']) && is_string($auth['user']) ? trim($auth['user']) : '';
|
|
$password = isset($auth['password']) && is_string($auth['password']) ? $auth['password'] : '';
|
|
$database = isset($auth['database']) && is_string($auth['database']) ? trim($auth['database']) : '';
|
|
if ($user === '' || $password === '') {
|
|
return;
|
|
}
|
|
|
|
$driver = 'pgsql';
|
|
$server = 'localhost';
|
|
|
|
$_GET[$driver] = $server;
|
|
$_GET['username'] = $user;
|
|
if ($database !== '' && (!isset($_GET['db']) || !is_string($_GET['db']) || $_GET['db'] === '')) {
|
|
$_GET['db'] = $database;
|
|
}
|
|
|
|
if (!isset($_SESSION['pwds']) || !is_array($_SESSION['pwds'])) {
|
|
$_SESSION['pwds'] = [];
|
|
}
|
|
$_SESSION['pwds'][$driver][$server][$user] = $password;
|
|
|
|
if ($database !== '') {
|
|
if (!isset($_SESSION['db']) || !is_array($_SESSION['db'])) {
|
|
$_SESSION['db'] = [];
|
|
}
|
|
$_SESSION['db'][$driver][$server][$user][$database] = true;
|
|
}
|
|
}
|
|
|
|
function adminer_object()
|
|
{
|
|
static $bootstrapped = false;
|
|
if (!$bootstrapped) {
|
|
$auth = da_adminer_sso_auth();
|
|
if ($auth !== null) {
|
|
da_adminer_bootstrap_auth($auth);
|
|
}
|
|
$bootstrapped = true;
|
|
}
|
|
|
|
if (!class_exists('DirectAdminAdminerExtension', false)) {
|
|
class DirectAdminAdminerExtension extends \Adminer\Adminer
|
|
{
|
|
public function credentials()
|
|
{
|
|
$auth = da_adminer_sso_auth();
|
|
if ($auth !== null) {
|
|
return ['localhost', (string)$auth['user'], (string)$auth['password']];
|
|
}
|
|
|
|
$username = isset($_GET['username']) && is_string($_GET['username']) ? $_GET['username'] : '';
|
|
$password = \Adminer\get_password();
|
|
if (!is_string($password)) {
|
|
$password = '';
|
|
}
|
|
|
|
return ['localhost', $username, $password];
|
|
}
|
|
|
|
public function loginFormField($name, $heading, $value)
|
|
{
|
|
if ($name === 'driver') {
|
|
return "<tr style='display:none'><th></th><td><input type='hidden' name='auth[driver]' value='pgsql'></td></tr>\n";
|
|
}
|
|
if ($name === 'server') {
|
|
return "<tr style='display:none'><th></th><td><input type='hidden' name='auth[server]' value='localhost'></td></tr>\n";
|
|
}
|
|
|
|
return parent::loginFormField($name, $heading, $value);
|
|
}
|
|
}
|
|
}
|
|
|
|
return new DirectAdminAdminerExtension();
|
|
}
|