From 7ee6f4f7acede52f4857780247c2cb2264937b2a Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Sun, 30 Mar 2025 07:51:47 +0200 Subject: [PATCH] Move connect() to Driver --- adminer/drivers/mssql.inc.php | 15 +++++++------ adminer/drivers/mysql.inc.php | 35 +++++++++++++------------------ adminer/drivers/oracle.inc.php | 5 ----- adminer/drivers/pgsql.inc.php | 35 +++++++++++++++---------------- adminer/drivers/sqlite.inc.php | 16 +++++++------- adminer/include/driver.inc.php | 8 +++++++ adminer/include/functions.inc.php | 8 +++++++ plugins/drivers/clickhouse.php | 16 +++++++------- plugins/drivers/elastic.php | 24 +++++++++------------ plugins/drivers/firebird.php | 5 ----- plugins/drivers/imap.php | 5 ----- plugins/drivers/mongo.php | 16 +++++++------- plugins/drivers/simpledb.php | 21 +++++++++---------- 13 files changed, 96 insertions(+), 113 deletions(-) diff --git a/adminer/drivers/mssql.inc.php b/adminer/drivers/mssql.inc.php index db305a9c..58c1141d 100644 --- a/adminer/drivers/mssql.inc.php +++ b/adminer/drivers/mssql.inc.php @@ -213,6 +213,13 @@ if (isset($_GET["mssql"])) { public array $generated = array("PERSISTED", "VIRTUAL"); public string $onActions = "NO ACTION|CASCADE|SET NULL|SET DEFAULT"; + static function connect(?string $server, string $username, string $password) { + if ($server == "") { + $server = "localhost:1433"; + } + return parent::connect($server, $username, $password); + } + function __construct(Db $connection) { parent::__construct($connection); $this->types = array( //! use sys.types @@ -289,14 +296,6 @@ if (isset($_GET["mssql"])) { return ($_GET["ns"] != "" ? idf_escape($_GET["ns"]) . "." : "") . idf_escape($idf); } - function connect($credentials) { - $connection = new Db; - if ($credentials[0] == "") { - $credentials[0] = "localhost:1433"; - } - return ($connection->attach($credentials[0], $credentials[1], $credentials[2]) ?: $connection); - } - function get_databases($flush) { return get_vals("SELECT name FROM sys.databases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb')"); } diff --git a/adminer/drivers/mysql.inc.php b/adminer/drivers/mysql.inc.php index ece0ee45..9203a0ae 100644 --- a/adminer/drivers/mysql.inc.php +++ b/adminer/drivers/mysql.inc.php @@ -210,6 +210,21 @@ if (!defined('Adminer\DRIVER')) { /** @var list */ public array $functions = array("char_length", "date", "from_unixtime", "lower", "round", "floor", "ceil", "sec_to_time", "time_to_sec", "upper"); /** @var list */ public array $grouping = array("avg", "count", "count distinct", "group_concat", "max", "min", "sum"); + static function connect(?string $server, string $username, string $password) { + $connection = parent::connect($server, $username, $password); + if (is_string($connection)) { + if (function_exists('iconv') && !is_utf8($connection) && strlen($s = iconv("windows-1250", "utf-8", $connection)) > strlen($connection)) { // windows-1250 - most common Windows encoding + $connection = $s; + } + return $connection; + } + $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'); + add_driver(DRIVER, ($connection->flavor == 'maria' ? "MariaDB" : "MySQL")); + return $connection; + } + function __construct(Db $connection) { parent::__construct($connection); $this->types = array( @@ -351,26 +366,6 @@ if (!defined('Adminer\DRIVER')) { return idf_escape($idf); } - /** Connect to the database - * @param array{?string, string, string} $credentials [$server, $username, $password] - * @return string|Db string for error - */ - function connect(array $credentials) { - $connection = new Db; - $error = $connection->attach($credentials[0], $credentials[1], $credentials[2]); - if ($error) { - if (function_exists('iconv') && !is_utf8($error) && strlen($s = iconv("windows-1250", "utf-8", $error)) > strlen($error)) { // windows-1250 - most common Windows encoding - $error = $s; - } - return $error; - } - $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'); - add_driver(DRIVER, ($connection->flavor == 'maria' ? "MariaDB" : "MySQL")); - return $connection; - } - /** Get cached list of databases * @return list */ diff --git a/adminer/drivers/oracle.inc.php b/adminer/drivers/oracle.inc.php index 78700d00..88bbfb5d 100644 --- a/adminer/drivers/oracle.inc.php +++ b/adminer/drivers/oracle.inc.php @@ -188,11 +188,6 @@ if (isset($_GET["oracle"])) { return idf_escape($idf); } - function connect($credentials) { - $connection = new Db; - return ($connection->attach($credentials[0], $credentials[1], $credentials[2]) ?: $connection); - } - function get_databases($flush) { return get_vals( "SELECT DISTINCT tablespace_name FROM ( diff --git a/adminer/drivers/pgsql.inc.php b/adminer/drivers/pgsql.inc.php index a274d966..bc536d49 100644 --- a/adminer/drivers/pgsql.inc.php +++ b/adminer/drivers/pgsql.inc.php @@ -171,6 +171,23 @@ if (isset($_GET["pgsql"])) { public array $functions = array("char_length", "lower", "round", "to_hex", "to_timestamp", "upper"); public array $grouping = array("avg", "count", "count distinct", "max", "min", "sum"); + static function connect(?string $server, string $username, string $password) { + $connection = parent::connect($server, $username, $password); + if (is_string($connection)) { + return $connection; + } + if (min_version(9, 0, $connection)) { + $connection->query("SET application_name = 'Adminer'"); + } + $version = get_val("SELECT version()", 0, $connection); + $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 + add_driver(DRIVER, "CockroachDB"); + } + return $connection; + } + function __construct(Db $connection) { parent::__construct($connection); $this->types = array( //! arrays @@ -295,24 +312,6 @@ if (isset($_GET["pgsql"])) { return idf_escape($idf); } - function connect($credentials) { - $connection = new Db; - $error = $connection->attach($credentials[0], $credentials[1], $credentials[2]); - if ($error) { - return $error; - } - if (min_version(9, 0, $connection)) { - $connection->query("SET application_name = 'Adminer'"); - } - $version = get_val("SELECT version()", 0, $connection); - $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 - add_driver(DRIVER, "CockroachDB"); - } - return $connection; - } - function get_databases($flush) { return get_vals("SELECT datname FROM pg_database WHERE datallowconn = TRUE AND has_database_privilege(datname, 'CONNECT') diff --git a/adminer/drivers/sqlite.inc.php b/adminer/drivers/sqlite.inc.php index dd8e5798..5a2ac298 100644 --- a/adminer/drivers/sqlite.inc.php +++ b/adminer/drivers/sqlite.inc.php @@ -122,6 +122,13 @@ if (isset($_GET["sqlite"])) { public array $functions = array("hex", "length", "lower", "round", "unixepoch", "upper"); public array $grouping = array("avg", "count", "count distinct", "group_concat", "max", "min", "sum"); + static function connect(?string $server, string $username, string $password) { + if ($password != "") { + return lang('Database does not support password.'); + } + return parent::connect(":memory:", "", ""); + } + function __construct(Db $connection) { parent::__construct($connection); if (min_version(3.31, 0, $connection)) { @@ -166,15 +173,6 @@ if (isset($_GET["sqlite"])) { return idf_escape($idf); } - function connect($credentials) { - list(, , $password) = $credentials; - if ($password != "") { - return lang('Database does not support password.'); - } - $connection = new Db; - return ($connection->attach(":memory:", "", "") ?: $connection); - } - function get_databases($flush) { return array(); } diff --git a/adminer/include/driver.inc.php b/adminer/include/driver.inc.php index 59e477fc..cc9e10da 100644 --- a/adminer/include/driver.inc.php +++ b/adminer/include/driver.inc.php @@ -30,6 +30,14 @@ abstract class SqlDriver { public string $enumLength = "'(?:''|[^'\\\\]|\\\\.)*'"; // regular expression for parsing enum lengths /** @var list */ public array $generated = array(); // allowed types of generated columns + /** Connect to the database + * @return Db|string string for error + */ + static function connect(?string $server, string $username, string $password) { + $connection = new Db; + return ($connection->attach($server, $username, $password) ?: $connection); + } + /** Create object for performing database operations */ function __construct(Db $connection) { $this->conn = $connection; diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index 406b215c..29ac368a 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -24,6 +24,14 @@ function driver(): Driver { return Driver::$instance; } +/** 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]); +} + /** Unescape database identifier * @param string $idf text inside `` */ diff --git a/plugins/drivers/clickhouse.php b/plugins/drivers/clickhouse.php index 8a1adbcd..ed760560 100644 --- a/plugins/drivers/clickhouse.php +++ b/plugins/drivers/clickhouse.php @@ -123,6 +123,13 @@ if (isset($_GET["clickhouse"])) { public array $operators = array("=", "<", ">", "<=", ">=", "!=", "~", "!~", "LIKE", "LIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL", "SQL"); public array $grouping = array("avg", "count", "count distinct", "max", "min", "sum"); + static function connect(?string $server, string $username, string $password) { + if (!preg_match('~^(https?://)?[-a-z\d.]+(:\d+)?$~', $server)) { + return lang('Invalid server.'); + } + return parent::connect($server, $username, $password); + } + function __construct(Db $connection) { parent::__construct($connection); $this->types = array( //! arrays @@ -224,15 +231,6 @@ if (isset($_GET["clickhouse"])) { return apply_queries("DROP TABLE", $tables); } - function connect($credentials) { - $connection = new Db; - list($server, $username, $password) = $credentials; - if (!preg_match('~^(https?://)?[-a-z\d.]+(:\d+)?$~', $server)) { - return lang('Invalid server.'); - } - return ($connection->attach($server, $username, $password) ?: $connection); - } - function get_databases($flush) { $result = get_rows('SHOW DATABASES'); diff --git a/plugins/drivers/elastic.php b/plugins/drivers/elastic.php index bd224d78..c7db74ef 100644 --- a/plugins/drivers/elastic.php +++ b/plugins/drivers/elastic.php @@ -110,6 +110,16 @@ if (isset($_GET["elastic"])) { public array $insertFunctions = array("json"); public array $operators = array("=", "must", "should", "must_not"); + static function connect(?string $server, string $username, string $password) { + if (!preg_match('~^(https?://)?[-a-z\d.]+(:\d+)?$~', $server)) { + return lang('Invalid server.'); + } + if ($password != "" && is_object(parent::connect($server, $username, ""))) { + return lang('Database does not support password.'); + } + return parent::connect($server, $username, $password); + } + function __construct(Db $connection) { parent::__construct($connection); $this->types = array( @@ -283,20 +293,6 @@ if (isset($_GET["elastic"])) { } } - function connect($credentials) { - $connection = new Db; - - list($server, $username, $password) = $credentials; - if (!preg_match('~^(https?://)?[-a-z\d.]+(:\d+)?$~', $server)) { - return lang('Invalid server.'); - } - if ($password != "" && $connection->attach($server, $username, "")) { - return lang('Database does not support password.'); - } - - return ($connection->attach($server, $username, $password) ?: $connection); - } - function support($feature) { return preg_match("~table|columns~", $feature); } diff --git a/plugins/drivers/firebird.php b/plugins/drivers/firebird.php index 66336003..c4a6ca65 100644 --- a/plugins/drivers/firebird.php +++ b/plugins/drivers/firebird.php @@ -101,11 +101,6 @@ if (isset($_GET["firebird"])) { return idf_escape($idf); } - function connect($credentials) { - $connection = new Db; - return ($connection->attach($credentials[0], $credentials[1], $credentials[2]) ?: $connection); - } - function get_databases($flush) { return array("domain"); } diff --git a/plugins/drivers/imap.php b/plugins/drivers/imap.php index e43cfd53..31b18fbf 100644 --- a/plugins/drivers/imap.php +++ b/plugins/drivers/imap.php @@ -270,11 +270,6 @@ if (isset($_GET["imap"])) { return connection()->expunge(); } - function connect($credentials) { - $connection = new Db; - return ($connection->attach($credentials[0], $credentials[1], $credentials[2]) ?: $connection); - } - function support($feature) { return false; } diff --git a/plugins/drivers/mongo.php b/plugins/drivers/mongo.php index a78e8165..5a77ed11 100644 --- a/plugins/drivers/mongo.php +++ b/plugins/drivers/mongo.php @@ -323,6 +323,13 @@ if (isset($_GET["mongo"])) { public $primary = "_id"; + static function connect(?string $server, string $username, string $password) { + if ($server == "") { + $server = "localhost:27017"; + } + return parent::connect($server, $username, $password); + } + function select(string $table, array $select, array $where, array $group, array $order = array(), $limit = 1, ?int $page = 0, bool $print = false) { $select = ($select == array("*") ? array() @@ -430,15 +437,6 @@ if (isset($_GET["mongo"])) { return $credentials[1]; } - function connect($credentials) { - $connection = new Db; - list($server, $username, $password) = $credentials; - if ($server == "") { - $server = "localhost:27017"; - } - return ($connection->attach($server, $username, $password) ?: $connection); - } - function alter_indexes($table, $alter) { foreach ($alter as $val) { list($type, $name, $set) = $val; diff --git a/plugins/drivers/simpledb.php b/plugins/drivers/simpledb.php index f3f593b2..4bcc0f12 100644 --- a/plugins/drivers/simpledb.php +++ b/plugins/drivers/simpledb.php @@ -119,6 +119,16 @@ if (isset($_GET["simpledb"])) { public $primary = "itemName()"; + static function connect(?string $server, string $username, string $password) { + if (!preg_match('~^(https?://)?[-a-z\d.]+(:\d+)?$~', $server)) { + return lang('Invalid server.'); + } + if ($password != "") { + return lang('Database does not support password.'); + } + return parent::connect($server, $username, $password); + } + private function chunkRequest($ids, $action, $params, $expand = array()) { foreach (array_chunk($ids, 25) as $chunk) { $params2 = $params; @@ -242,17 +252,6 @@ if (isset($_GET["simpledb"])) { - function connect($credentials) { - list($host, , $password) = $credentials; - if (!preg_match('~^(https?://)?[-a-z\d.]+(:\d+)?$~', $host)) { - return lang('Invalid server.'); - } - if ($password != "") { - return lang('Database does not support password.'); - } - return new Db; - } - function support($feature) { return preg_match('~sql~', $feature); }