1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-05 14:17:26 +02:00

Move $drivers to SqlDriver

This commit is contained in:
Jakub Vrana
2025-03-28 22:39:12 +01:00
parent 1f88485a3c
commit aceb4ce7a5
16 changed files with 26 additions and 32 deletions

View File

@@ -7,7 +7,7 @@
namespace Adminer;
$drivers["mssql"] = "MS SQL";
add_driver("mssql", "MS SQL");
if (isset($_GET["mssql"])) {
define('Adminer\DRIVER', "mssql");

View File

@@ -1,7 +1,7 @@
<?php
namespace Adminer;
$drivers = array("server" => "MySQL / MariaDB") + $drivers;
SqlDriver::$drivers = array("server" => "MySQL / MariaDB") + SqlDriver::$drivers;
if (!defined('Adminer\DRIVER')) {
define('Adminer\DRIVER', "server"); // server - backwards compatibility
@@ -366,7 +366,6 @@ if (!defined('Adminer\DRIVER')) {
* @return string|Db string for error
*/
function connect(array $credentials) {
global $drivers;
$connection = new Db;
$error = $connection->attach($credentials[0], $credentials[1], $credentials[2]);
if ($error) {
@@ -378,7 +377,7 @@ if (!defined('Adminer\DRIVER')) {
$connection->set_charset(charset($connection));
$connection->query("SET sql_quote_show_create = 1, autocommit = 1");
$connection->flavor = (preg_match('~MariaDB~', $connection->server_info) ? 'maria' : 'mysql');
$drivers[DRIVER] = ($connection->flavor == 'maria' ? "MariaDB" : "MySQL");
add_driver(DRIVER, ($connection->flavor == 'maria' ? "MariaDB" : "MySQL"));
return $connection;
}

View File

@@ -1,7 +1,7 @@
<?php
namespace Adminer;
$drivers["oracle"] = "Oracle (beta)";
add_driver("oracle", "Oracle (beta)");
if (isset($_GET["oracle"])) {
define('Adminer\DRIVER', "oracle");

View File

@@ -1,7 +1,7 @@
<?php
namespace Adminer;
$drivers["pgsql"] = "PostgreSQL";
add_driver("pgsql", "PostgreSQL");
if (isset($_GET["pgsql"])) {
define('Adminer\DRIVER', "pgsql");
@@ -301,7 +301,6 @@ if (isset($_GET["pgsql"])) {
}
function connect($credentials) {
global $drivers;
$connection = new Db;
$error = $connection->attach($credentials[0], $credentials[1], $credentials[2]);
if ($error) {
@@ -314,7 +313,7 @@ if (isset($_GET["pgsql"])) {
$connection->flavor = (preg_match('~CockroachDB~', $version) ? 'cockroach' : '');
$connection->server_info = preg_replace('~^\D*([\d.]+[-\w]*).*~', '\1', $version);
if ($connection->flavor == 'cockroach') { // we don't use "PostgreSQL / CockroachDB" by default because it's too long
$drivers[DRIVER] = "CockroachDB";
add_driver(DRIVER, "CockroachDB");
}
return $connection;
}

View File

@@ -1,7 +1,7 @@
<?php
namespace Adminer;
$drivers["sqlite"] = "SQLite";
add_driver("sqlite", "SQLite");
if (isset($_GET["sqlite"])) {
define('Adminer\DRIVER', "sqlite");

View File

@@ -16,7 +16,7 @@ if ($_POST && !$error) {
$is_sql = preg_match('~sql~', $_POST["format"]);
if ($is_sql) {
echo "-- Adminer " . VERSION . " " . $drivers[DRIVER] . " " . str_replace("\n", " ", $connection->server_info) . " dump\n\n";
echo "-- Adminer " . VERSION . " " . get_driver(DRIVER) . " " . str_replace("\n", " ", $connection->server_info) . " dump\n\n";
if (JUSH == "sql") {
echo "SET NAMES utf8;
SET time_zone = '+00:00';

View File

@@ -118,10 +118,10 @@ class Adminer {
/** Print login form */
function loginForm(): void {
global $drivers, $adminer;
global $adminer;
echo "<table class='layout'>\n";
// this is matched by compile.php
echo $adminer->loginFormField('driver', '<tr><th>' . lang('System') . '<td>', html_select("auth[driver]", $drivers, DRIVER, "loginDriver(this);"));
echo $adminer->loginFormField('driver', '<tr><th>' . lang('System') . '<td>', html_select("auth[driver]", SqlDriver::$drivers, DRIVER, "loginDriver(this);"));
echo $adminer->loginFormField('server', '<tr><th>' . lang('Server') . '<td>', '<input name="auth[server]" value="' . h(SERVER) . '" title="hostname[:port]" placeholder="localhost" autocapitalize="off">');
// this is matched by compile.php
echo $adminer->loginFormField('username', '<tr><th>' . lang('Username') . '<td>', '<input name="auth[username]" id="username" autofocus value="' . h($_GET["username"]) . '" autocomplete="username" autocapitalize="off">' . script("qs('#username').form['auth[driver]'].onchange();"));
@@ -946,7 +946,7 @@ class Adminer {
* @param string $missing can be "auth" if there is no database connection, "db" if there is no database selected, "ns" with invalid schema
*/
function navigation(string $missing): void {
global $drivers, $connection, $adminer;
global $connection, $adminer;
echo "<h1>" . $adminer->name() . " <span class='version'>" . VERSION;
$new_version = $_COOKIE["adminer_version"];
echo " <a href='https://www.adminer.org/#download'" . target_blank() . " id='version'>" . (version_compare(VERSION, $new_version) < 0 ? h($new_version) : "") . "</a>";
@@ -957,7 +957,7 @@ class Adminer {
$output = "";
foreach ((array) $_SESSION["pwds"] as $vendor => $servers) {
foreach ($servers as $server => $usernames) {
$name = h(get_setting("vendor-$vendor-$server") ?: $drivers[$vendor]);
$name = h(get_setting("vendor-$vendor-$server") ?: get_driver($vendor));
foreach ($usernames as $username => $password) {
if ($password !== null) {
$dbs = $_SESSION["db"][$vendor][$server][$username];

View File

@@ -182,7 +182,7 @@ if (isset($_GET["username"]) && is_string(get_password())) {
if (is_object($connection)) {
$driver = new Driver($connection);
if ($connection->flavor) {
save_settings(array("vendor-" . DRIVER . "-" . SERVER => $drivers[DRIVER]));
save_settings(array("vendor-" . DRIVER . "-" . SERVER => get_driver(DRIVER)));
}
}
}

View File

@@ -39,7 +39,7 @@ if ($_GET["script"] == "version") {
exit;
}
global $adminer, $connection, $driver, $drivers, $translations; // allows including Adminer inside a function
global $adminer, $connection, $driver, $translations; // allows including Adminer inside a function
if (!$_SERVER["REQUEST_URI"]) { // IIS 5 compatibility
$_SERVER["REQUEST_URI"] = $_SERVER["ORIG_PATH_INFO"];

View File

@@ -42,7 +42,7 @@ if (
echo "<a href='" . h(ME) . "$key='>$val</a>\n";
}
}
echo "<p>" . lang('%s version: %s through PHP extension %s', $drivers[DRIVER], "<b>" . h($connection->server_info) . "</b>", "<b>$connection->extension</b>") . "\n";
echo "<p>" . lang('%s version: %s through PHP extension %s', get_driver(DRIVER), "<b>" . h($connection->server_info) . "</b>", "<b>$connection->extension</b>") . "\n";
echo "<p>" . lang('Logged as: %s', "<b>" . h(logged_user()) . "</b>") . "\n";
if (isset($adminer->plugins) && is_array($adminer->plugins)) {
echo "<p>" . lang('Loaded plugins') . ":\n<ul>\n";

View File

@@ -7,7 +7,7 @@ namespace Adminer;
* @param string $title2 used after colon in title and heading, should be HTML escaped
*/
function page_header(string $title, string $error = "", $breadcrumb = array(), string $title2 = ""): void {
global $adminer, $drivers;
global $adminer;
page_headers();
if (is_ajax() && $error) {
page_messages($error);
@@ -88,7 +88,7 @@ const thousandsSeparator = '" . js_escape(lang(',')) . "';")
echo "<div id='content'>\n";
if ($breadcrumb !== null) {
$link = substr(preg_replace('~\b(username|db|ns)=[^&]*&~', '', ME), 0, -1);
echo '<p id="breadcrumb"><a href="' . h($link ?: ".") . '">' . $drivers[DRIVER] . '</a> » ';
echo '<p id="breadcrumb"><a href="' . h($link ?: ".") . '">' . get_driver(DRIVER) . '</a> » ';
$link = substr(preg_replace('~\b(db|ns)=[^&]*&~', '', ME), 0, -1);
$server = $adminer->serverName(SERVER);
$server = ($server != "" ? $server : lang('Server'));

View File

@@ -1,21 +1,18 @@
<?php
namespace Adminer;
$drivers = array();
/** Add a driver */
/** Add or overwrite a driver */
function add_driver(string $id, string $name): void {
global $drivers;
$drivers[$id] = $name;
SqlDriver::$drivers[$id] = $name;
}
/** Get driver name */
function get_driver(string $id): string {
global $drivers;
return $drivers[$id];
return SqlDriver::$drivers[$id];
}
abstract class SqlDriver {
/** @var string[] */ static array $drivers = array();
/** @var list<string> */ static array $extensions = array(); // possible extensions
static string $jush; // JUSH identifier

View File

@@ -391,8 +391,7 @@ function set_session(string $key, $val) {
/** Get authenticated URL */
function auth_url(string $vendor, ?string $server, string $username, string $db = null): string {
global $drivers;
$uri = remove_from_uri(implode("|", array_keys($drivers))
$uri = remove_from_uri(implode("|", array_keys(SqlDriver::$drivers))
. "|username|ext|"
. ($db !== null ? "db|" : "")
. ($vendor == 'mssql' || $vendor == 'pgsql' ? "" : "ns|") // we don't have access to support() here

View File

@@ -320,8 +320,8 @@ if ($vendor) {
$file = preg_replace("((\t*)" . preg_quote('if (support("' . $feature . '")') . ".*?\n\\1\\}( else)?)s", '', $file);
}
}
if ($project != "editor" && count($drivers) == 1) {
$file = str_replace('html_select("auth[driver]", $drivers, DRIVER, "loginDriver(this);")', 'input_hidden("auth[driver]", "' . ($vendor == "mysql" ? "server" : $vendor) . '") . "' . reset($drivers) . '"', $file, $count);
if ($project != "editor" && count(Adminer\SqlDriver::$drivers) == 1) {
$file = str_replace('html_select("auth[driver]", SqlDriver::$drivers, DRIVER, "loginDriver(this);")', 'input_hidden("auth[driver]", "' . ($vendor == "mysql" ? "server" : $vendor) . '") . "' . reset(Adminer\SqlDriver::$drivers) . '"', $file, $count);
if (!$count) {
echo "auth[driver] form field not found\n";
}

View File

@@ -10,7 +10,7 @@
namespace Adminer;
include "../adminer/include/bootstrap.inc.php";
$drivers[DRIVER] = lang('Login');
add_driver(DRIVER, lang('Login'));
if (isset($_GET["select"]) && ($_POST["edit"] || $_POST["clone"]) && !$_POST["save"]) {
$_GET["edit"] = $_GET["select"];

View File

@@ -12,7 +12,7 @@ parameters:
- identifier: includeOnce.fileNotFound # ./adminer-plugins.php
- "~^Function (set_magic_quotes_runtime|mysql_)~" # PHP < 7 functions
- "~an unknown class OCI-?Lob~" # this looks like PHPStan bug
- "~^Variable \\$(adminer|connection|driver|drivers|error|translations) might not be defined~" # declared in bootstrap.inc.php
- "~^Variable \\$(adminer|connection|driver|error|translations) might not be defined~" # declared in bootstrap.inc.php
- "~^Constant LANG not found~" # defined in lang.inc.php
- "~expects int, float given~" # this will work
- "~expects bool~" # truthy values