1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-16 19:44:00 +02:00

Use connection() instead of $connection

This commit is contained in:
Jakub Vrana
2025-03-29 21:16:28 +01:00
parent 168ea5ae6d
commit 712d96b22c
36 changed files with 130 additions and 196 deletions

View File

@@ -521,7 +521,6 @@ class Adminer {
* @return list<string> expressions to join by AND
*/
function selectSearchProcess(array $fields, array $indexes): array {
global $connection;
$return = array();
foreach ($indexes as $i => $index) {
if ($index["type"] == "FULLTEXT" && $_GET["fulltext"][$i] != "") {
@@ -793,7 +792,6 @@ class Adminer {
* @return void prints data
*/
function dumpData(string $table, string $style, string $query): void {
global $connection;
if ($style) {
$max_packet = (JUSH == "sqlite" ? 0 : 1048576); // default, minimum is 1024
$fields = array();
@@ -813,7 +811,7 @@ class Adminer {
}
}
}
$result = $connection->query($query, 1); // 1 - MYSQLI_USE_RESULT //! enum and set as numbers
$result = connection()->query($query, 1); // 1 - MYSQLI_USE_RESULT //! enum and set as numbers
if ($result) {
$insert = "";
$buffer = "";
@@ -872,7 +870,7 @@ class Adminer {
echo $buffer . $suffix;
}
} elseif ($_POST["format"] == "sql") {
echo "-- " . str_replace("\n", " ", $connection->error) . "\n";
echo "-- " . str_replace("\n", " ", connection()->error) . "\n";
}
if ($identity_insert) {
echo "SET IDENTITY_INSERT " . table($table) . " OFF;\n";
@@ -938,7 +936,6 @@ 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 $connection;
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>";
@@ -966,7 +963,7 @@ class Adminer {
} else {
$tables = array();
if ($_GET["ns"] !== "" && !$missing && DB != "") {
$connection->select_db(DB);
connection()->select_db(DB);
$tables = table_status('', true);
}
adminer()->syntaxHighlighting($tables);
@@ -998,7 +995,6 @@ class Adminer {
* @param TableStatus[] $tables
*/
function syntaxHighlighting(array $tables): void {
global $connection;
// this is matched by compile.php
echo script_src("../externals/jush/modules/jush.js");
echo script_src("../externals/jush/modules/jush-textarea.js");
@@ -1019,14 +1015,13 @@ class Adminer {
}
echo "</script>\n";
}
echo script("syntaxHighlighting('" . (is_object($connection) ? preg_replace('~^(\d\.?\d).*~s', '\1', $connection->server_info) : "") . "'"
. ($connection->flavor == 'maria' ? ", 'maria'" : ($connection->flavor == 'cockroach' ? ", 'cockroach'" : "")) . ");"
echo script("syntaxHighlighting('" . (is_object(connection()) ? preg_replace('~^(\d\.?\d).*~s', '\1', connection()->server_info) : "") . "'"
. (connection()->flavor == 'maria' ? ", 'maria'" : (connection()->flavor == 'cockroach' ? ", 'cockroach'" : "")) . ");"
);
}
/** Print databases list in menu */
function databasesPrint(string $missing): void {
global $connection;
$databases = adminer()->databases();
if (DB && $databases && !in_array(DB, $databases)) {
array_unshift($databases, DB);
@@ -1040,7 +1035,7 @@ class Adminer {
);
echo "<input type='submit' value='" . lang('Use') . "'" . ($databases ? " class='hidden'" : "") . ">\n";
if (support("scheme")) {
if ($missing != "db" && DB != "" && $connection->select_db(DB)) {
if ($missing != "db" && DB != "" && connection()->select_db(DB)) {
echo "<br><span>" . lang('Schema') . ":</span> " . html_select("ns", array("" => "") + adminer()->schemas(), $_GET["ns"]) . $db_events;
if ($_GET["ns"] != "") {
set_schema($_GET["ns"]);

View File

@@ -1,8 +1,6 @@
<?php
namespace Adminer;
$connection = '';
$permanent = array();
if ($_COOKIE["adminer_permanent"]) {
foreach (explode(" ", $_COOKIE["adminer_permanent"]) as $val) {
@@ -175,18 +173,18 @@ if (isset($_GET["username"]) && is_string(get_password())) {
auth_error(lang('Connecting to privileged ports is not allowed.'), $permanent);
}
check_invalid_login($permanent);
$connection = connect(adminer()->credentials());
if (is_object($connection)) {
Driver::$instance = new Driver($connection);
if ($connection->flavor) {
Db::$instance = connect(adminer()->credentials());
if (is_object(Db::$instance)) {
Driver::$instance = new Driver(Db::$instance);
if (Db::$instance->flavor) {
save_settings(array("vendor-" . DRIVER . "-" . SERVER => get_driver(DRIVER)));
}
}
}
$login = null;
if (!is_object($connection) || ($login = adminer()->login($_GET["username"], get_password())) !== true) {
$error = (is_string($connection) ? nl_br(h($connection)) : (is_string($login) ? $login : lang('Invalid credentials.')));
if (!is_object(connection()) || ($login = adminer()->login($_GET["username"], get_password())) !== true) {
$error = (is_string(connection()) ? nl_br(h(connection())) : (is_string($login) ? $login : lang('Invalid credentials.')));
auth_error(
$error . (preg_match('~^ | $~', get_password()) ? '<br>' . lang('There is a space in the input password which might be the cause.') : ''),
$permanent

View File

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

View File

@@ -10,7 +10,7 @@ if (isset($_GET["import"])) {
if (
!(DB != ""
? $connection->select_db(DB)
? connection()->select_db(DB)
: isset($_GET["sql"]) || isset($_GET["dump"]) || isset($_GET["database"]) || isset($_GET["processlist"]) || isset($_GET["privileges"]) || isset($_GET["user"]) || isset($_GET["variables"])
|| $_GET["script"] == "connect" || $_GET["script"] == "kill"
)
@@ -42,7 +42,7 @@ if (
echo "<a href='" . h(ME) . "$key='>$val</a>\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('%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

@@ -4,6 +4,8 @@ namespace Adminer;
// this could be interface when "Db extends \mysqli" can have compatible type declarations (PHP 7)
// interfaces can include properties only since PHP 8.4
abstract class SqlDb {
/** @var Db|string */ static $instance = ''; // string means error
public string $extension; // extension name
public string $flavor = ''; // different vendor with the same API, e.g. MariaDB; usually stays empty
public string $server_info; // server version

View File

@@ -206,9 +206,8 @@ function edit_type(string $key, array $field, array $collations, array $foreign_
* @return array{partition_by:string, partition:string, partitions:string, partition_names:list<string>, partition_values:list<string>}
*/
function get_partitions_info(string $table): array {
global $connection;
$from = "FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = " . q(DB) . " AND TABLE_NAME = " . q($table);
$result = $connection->query("SELECT PARTITION_METHOD, PARTITION_EXPRESSION, PARTITION_ORDINAL_POSITION $from ORDER BY PARTITION_ORDINAL_POSITION DESC LIMIT 1");
$result = connection()->query("SELECT PARTITION_METHOD, PARTITION_EXPRESSION, PARTITION_ORDINAL_POSITION $from ORDER BY PARTITION_ORDINAL_POSITION DESC LIMIT 1");
$return = array();
list($return["partition_by"], $return["partition"], $return["partitions"]) = $result->fetch_row();
$partitions = get_key_vals("SELECT PARTITION_NAME, PARTITION_DESCRIPTION $from AND PARTITION_NAME != '' ORDER BY PARTITION_ORDINAL_POSITION");
@@ -543,17 +542,16 @@ function ini_bytes(string $ini): int {
* @return string HTML code
*/
function doc_link(array $paths, string $text = "<sup>?</sup>"): string {
global $connection;
$server_info = $connection->server_info;
$server_info = connection()->server_info;
$version = preg_replace('~^(\d\.?\d).*~s', '\1', $server_info); // two most significant digits
$urls = array(
'sql' => "https://dev.mysql.com/doc/refman/$version/en/",
'sqlite' => "https://www.sqlite.org/",
'pgsql' => "https://www.postgresql.org/docs/" . ($connection->flavor == 'cockroach' ? "current" : $version) . "/",
'pgsql' => "https://www.postgresql.org/docs/" . (connection()->flavor == 'cockroach' ? "current" : $version) . "/",
'mssql' => "https://learn.microsoft.com/en-us/sql/",
'oracle' => "https://www.oracle.com/pls/topic/lookup?ctx=db" . preg_replace('~^.* (\d+)\.(\d+)\.\d+\.\d+\.\d+.*~s', '\1\2', $server_info) . "&id=",
);
if ($connection->flavor == 'maria') {
if (connection()->flavor == 'maria') {
$urls['sql'] = "https://mariadb.com/kb/en/";
$paths['sql'] = (isset($paths['mariadb']) ? $paths['mariadb'] : str_replace(".html", "/", $paths['sql']));
}
@@ -564,8 +562,7 @@ function doc_link(array $paths, string $text = "<sup>?</sup>"): string {
* @return string formatted
*/
function db_size(string $db): string {
global $connection;
if (!$connection->select_db($db)) {
if (!connection()->select_db($db)) {
return "?";
}
$return = 0;
@@ -577,10 +574,9 @@ function db_size(string $db): string {
/** Print SET NAMES if utf8mb4 might be needed */
function set_utf8mb4(string $create): void {
global $connection;
static $set = false;
if (!$set && preg_match('~\butf8mb4~i', $create)) { // possible false positive
$set = true;
echo "SET NAMES " . charset($connection) . ";\n\n";
echo "SET NAMES " . charset(connection()) . ";\n\n";
}
}

View File

@@ -3,11 +3,12 @@ namespace Adminer;
// This file is used both in Adminer and Adminer Editor.
/** Get database connection */
function connection(): Db {
// can be used in customization, $connection is minified
global $connection;
return $connection;
/** Get database connection
* @return Db|string string means error
*/
function connection() {
// can be used in customization, Db::$instance is minified
return Db::$instance;
}
/** Get Adminer object
@@ -33,10 +34,9 @@ function idf_unescape(string $idf): string {
return str_replace($last . $last, $last, substr($idf, 1, -1));
}
/** Shortcut for $connection->quote($string) */
/** Shortcut for connection()->quote($string) */
function q(string $string): string {
global $connection;
return $connection->quote($string);
return connection()->quote($string);
}
/** Escape string to use inside '' */
@@ -96,12 +96,11 @@ function bracket_escape(string $idf, bool $back = false): string {
/** Check if connection has at least the given version
* @param string|float $version required version
* @param string|float $maria_db required MariaDB version
* @param Db $connection2 defaults to $connection
* @param Db $connection2 defaults to connection()
*/
function min_version($version, $maria_db = "", Db $connection2 = null): bool {
global $connection;
if (!$connection2) {
$connection2 = $connection;
$connection2 = connection();
}
$server_info = $connection2->server_info;
if ($maria_db && preg_match('~([\d.]+)-MariaDB~', $server_info, $match)) {
@@ -157,8 +156,7 @@ function get_password() {
* @return string|false false if error
*/
function get_val(string $query, int $field = 0, ?Db $conn = null) {
global $connection;
$conn = (is_object($conn) ? $conn : $connection);
$conn = (is_object($conn) ? $conn : connection());
$result = $conn->query($query);
if (!is_object($result)) {
return false;
@@ -172,9 +170,8 @@ function get_val(string $query, int $field = 0, ?Db $conn = null) {
* @return list<string>
*/
function get_vals(string $query, $column = 0): array {
global $connection;
$return = array();
$result = $connection->query($query);
$result = connection()->query($query);
if (is_object($result)) {
while ($row = $result->fetch_row()) {
$return[] = $row[$column];
@@ -187,9 +184,8 @@ function get_vals(string $query, $column = 0): array {
* @return string[]
*/
function get_key_vals(string $query, Db $connection2 = null, bool $set_keys = true): array {
global $connection;
if (!is_object($connection2)) {
$connection2 = $connection;
$connection2 = connection();
}
$return = array();
$result = $connection2->query($query);
@@ -209,8 +205,7 @@ function get_key_vals(string $query, Db $connection2 = null, bool $set_keys = tr
* @return list<string[]> of associative arrays
*/
function get_rows(string $query, Db $connection2 = null, string $error = "<p class='error'>"): array {
global $connection;
$conn = (is_object($connection2) ? $connection2 : $connection);
$conn = (is_object($connection2) ? $connection2 : connection());
$return = array();
$result = $conn->query($query);
if (is_object($result)) { // can return true
@@ -256,7 +251,6 @@ function escape_key(string $key): string {
* @param Field[] $fields
*/
function where(array $where, array $fields = array()): string {
global $connection;
$return = array();
foreach ((array) $where["where"] as $key => $val) {
$key = bracket_escape($key, true); // true - back
@@ -270,7 +264,7 @@ function where(array $where, array $fields = array()): string {
: " = " . unconvert_field($field, q($val)))))
; //! enum and set
if (JUSH == "sql" && preg_match('~char|text~', $field_type) && preg_match("~[^ -@]~", $val)) { // not just [a-z] to catch non-ASCII characters
$return[] = "$column = " . q($val) . " COLLATE " . charset($connection) . "_bin";
$return[] = "$column = " . q($val) . " COLLATE " . charset(connection()) . "_bin";
}
}
foreach ((array) $where["null"] as $key) {
@@ -430,10 +424,9 @@ function redirect(?string $location, string $message = null): void {
* @param bool $redirect
*/
function query_redirect(string $query, ?string $location, string $message, $redirect = true, bool $execute = true, bool $failed = false, string $time = ""): bool {
global $connection;
if ($execute) {
$start = microtime(true);
$failed = !$connection->query($query);
$failed = !connection()->query($query);
$time = format_time($start);
}
$sql = ($query ? adminer()->messageQuery($query, $time, $failed) : "");
@@ -457,12 +450,11 @@ class Queries {
* @return Result|bool
*/
function queries(string $query) {
global $connection;
if (!Queries::$start) {
Queries::$start = microtime(true);
}
Queries::$queries[] = (preg_match('~;$~', $query) ? "DELIMITER ;;\n$query;\nDELIMITER " : $query) . ";";
return $connection->query($query);
return connection()->query($query);
}
/** Apply command to all array items

View File

@@ -331,13 +331,12 @@ function process_input(array $field) {
* @uses $_POST["tables"]
*/
function search_tables(): void {
global $connection;
$_GET["where"][0]["val"] = $_POST["query"];
$sep = "<ul>\n";
foreach (table_status('', true) as $table => $table_status) {
$name = adminer()->tableName($table_status);
if (isset($table_status["Engine"]) && $name != "" && (!$_POST["tables"] || in_array($table, $_POST["tables"]))) {
$result = $connection->query("SELECT" . limit("1 FROM " . table($table), " WHERE " . implode(" AND ", adminer()->selectSearchProcess(fields($table), array())), 1));
$result = connection()->query("SELECT" . limit("1 FROM " . table($table), " WHERE " . implode(" AND ", adminer()->selectSearchProcess(fields($table), array())), 1));
if (!$result || $result->fetch_row()) {
$print = "<a href='" . h(ME . "select=" . urlencode($table) . "&where[0][op]=" . urlencode($_GET["where"][0]["op"]) . "&where[0][val]=" . urlencode($_GET["where"][0]["val"])) . "'>$name</a>";
echo "$sep<li>" . ($result ? $print : "<p class='error'>$print: " . error()) . "\n";