1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-15 11:04:02 +02:00

Return Db from connection()

It's not a real type declaration because compile.php passes stdClass here.
This commit is contained in:
Jakub Vrana
2025-03-30 08:18:25 +02:00
parent 7ee6f4f7ac
commit a93e4cb694
8 changed files with 29 additions and 32 deletions

View File

@@ -167,24 +167,27 @@ if (isset($_GET["username"]) && !class_exists('Adminer\Db')) {
exit;
}
$connection = '';
if (isset($_GET["username"]) && is_string(get_password())) {
list($host, $port) = explode(":", SERVER, 2);
if (preg_match('~^\s*([-+]?\d+)~', $port, $match) && ($match[1] < 1024 || $match[1] > 65535)) { // is_numeric('80#') would still connect to port 80
auth_error(lang('Connecting to privileged ports is not allowed.'), $permanent);
}
check_invalid_login($permanent);
Db::$instance = connect(adminer()->credentials());
if (is_object(Db::$instance)) {
Driver::$instance = new Driver(Db::$instance);
if (Db::$instance->flavor) {
$credentials = adminer()->credentials();
$connection = Driver::connect($credentials[0], $credentials[1], $credentials[2]);
if (is_object($connection)) {
Db::$instance = $connection;
Driver::$instance = new Driver($connection);
if ($connection->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

@@ -4,7 +4,7 @@ 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
/** @var Db */ static $instance;
public string $extension; // extension name
public string $flavor = ''; // different vendor with the same API, e.g. MariaDB; usually stays empty

View File

@@ -5,7 +5,6 @@ namespace Adminer;
/** Print select result
* @param Result $result
* @param Db|string $connection2
* @param string[] $orgtables
* @param int|numeric-string $limit
* @return string[] $orgtables

View File

@@ -4,12 +4,12 @@ namespace Adminer;
// This file is used both in Adminer and Adminer Editor.
/** Get database connection
* @param Db|string $connection2 custom connection to use instead of the default
* @return Db|string string means error
* @param ?Db $connection2 custom connection to use instead of the default
* @return Db
*/
function connection($connection2 = null) {
function connection(Db $connection2 = null) {
// can be used in customization, Db::$instance is minified
return (is_object($connection2) ? $connection2 : Db::$instance);
return ($connection2 ?: Db::$instance);
}
/** Get Adminer object
@@ -26,10 +26,10 @@ function driver(): Driver {
/** Connect to the database
* @param array{?string, string, string} $credentials [$server, $username, $password]
* @return Db|string string for error
*/
function connect(array $credentials) {
return driver()->connect($credentials[0], $credentials[1], $credentials[2]);
function connect(array $credentials): ?Db {
$return = driver()->connect($credentials[0], $credentials[1], $credentials[2]);
return (is_object($return) ? $return : null);
}
/** Unescape database identifier
@@ -105,9 +105,8 @@ 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|string $connection2 defaults to connection()
*/
function min_version($version, $maria_db = "", $connection2 = null): bool {
function min_version($version, $maria_db = "", Db $connection2 = null): bool {
$connection2 = connection($connection2);
$server_info = $connection2->server_info;
if ($maria_db && preg_match('~([\d.]+)-MariaDB~', $server_info, $match)) {
@@ -188,10 +187,9 @@ function get_vals(string $query, $column = 0): array {
}
/** Get keys from first column and values from second
* @param Db|string $connection2
* @return string[]
*/
function get_key_vals(string $query, $connection2 = null, bool $set_keys = true): array {
function get_key_vals(string $query, Db $connection2 = null, bool $set_keys = true): array {
$connection2 = connection($connection2);
$return = array();
$result = $connection2->query($query);
@@ -208,10 +206,9 @@ function get_key_vals(string $query, $connection2 = null, bool $set_keys = true)
}
/** Get all rows of result
* @param Db|string $connection2
* @return list<string[]> of associative arrays
*/
function get_rows(string $query, $connection2 = null, string $error = "<p class='error'>"): array {
function get_rows(string $query, Db $connection2 = null, string $error = "<p class='error'>"): array {
$conn = connection($connection2);
$return = array();
$result = $conn->query($query);
@@ -219,7 +216,7 @@ function get_rows(string $query, $connection2 = null, string $error = "<p class=
while ($row = $result->fetch_assoc()) {
$return[] = $row;
}
} elseif (!$result && $connection2 === null && $error && (defined('Adminer\PAGE_HEADER') || $error == "-- ")) {
} elseif (!$result && !$connection2 && $error && (defined('Adminer\PAGE_HEADER') || $error == "-- ")) {
echo $error . error() . "\n";
}
return $return;
@@ -833,7 +830,7 @@ function slow_query(string $query): array {
$connection2 = null;
if (!$slow_query && support("kill")) {
$connection2 = connect(adminer()->credentials());
if (is_object($connection2) && ($db == "" || $connection2->select_db($db))) {
if ($connection2 && ($db == "" || $connection2->select_db($db))) {
$kill = get_val(connection_id(), 0, $connection2); // MySQL and MySQLi can use thread_id but it's not in PDO_MySQL
echo script("const timeout = setTimeout(() => { ajax('" . js_escape(ME) . "script=kill', function () {}, 'kill=$kill&token=" . get_token() . "'); }, 1000 * $timeout);");
}