From 69d7d76dc51b2ffe82575026ecf6e3f9e7d8b4e3 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Thu, 6 Mar 2025 13:48:13 +0100 Subject: [PATCH] Move operators, functions and grouping to Driver --- adminer/drivers/mongo.inc.php | 49 +++++++++++++++---------------- adminer/drivers/mssql.inc.php | 7 +++-- adminer/drivers/mysql.inc.php | 9 +++--- adminer/drivers/oracle.inc.php | 7 +++-- adminer/drivers/pgsql.inc.php | 7 +++-- adminer/drivers/sqlite.inc.php | 7 +++-- adminer/include/adminer.inc.php | 12 ++++---- adminer/include/auth.inc.php | 3 ++ adminer/include/bootstrap.inc.php | 8 +---- adminer/include/driver.inc.php | 3 ++ plugins/drivers/clickhouse.php | 6 ++-- plugins/drivers/elastic.php | 4 +-- plugins/drivers/elastic5.php | 4 +-- plugins/drivers/firebird.php | 4 +-- plugins/drivers/simpledb.php | 6 ++-- 15 files changed, 66 insertions(+), 70 deletions(-) diff --git a/adminer/drivers/mongo.inc.php b/adminer/drivers/mongo.inc.php index d1e0b1e3..65faee34 100644 --- a/adminer/drivers/mongo.inc.php +++ b/adminer/drivers/mongo.inc.php @@ -115,6 +115,29 @@ if (isset($_GET["mongo"])) { class Driver extends SqlDriver { var $editFunctions = array(array("json")); + + var $operators = array( + "=", + "!=", + ">", + "<", + ">=", + "<=", + "regex", + "(f)=", + "(f)!=", + "(f)>", + "(f)<", + "(f)>=", + "(f)<=", + "(date)=", + "(date)!=", + "(date)>", + "(date)<", + "(date)>=", + "(date)<=", + ); + public $primary = "_id"; function select($table, $select, $where, $group, $order = array(), $limit = 1, $page = 0, $print = false) { @@ -349,28 +372,6 @@ if (isset($_GET["mongo"])) { } return $data; } - - $operators = array( - "=", - "!=", - ">", - "<", - ">=", - "<=", - "regex", - "(f)=", - "(f)!=", - "(f)>", - "(f)<", - "(f)>=", - "(f)<=", - "(date)=", - "(date)!=", - "(date)>", - "(date)<", - "(date)>=", - "(date)<=", - ); } function table($idf) { @@ -532,13 +533,9 @@ if (isset($_GET["mongo"])) { } function driver_config() { - global $operators; return array( 'possible_drivers' => array("mongodb"), 'jush' => "mongo", - 'operators' => $operators, - 'functions' => array(), - 'grouping' => array(), ); } } diff --git a/adminer/drivers/mssql.inc.php b/adminer/drivers/mssql.inc.php index d993094d..472e7912 100644 --- a/adminer/drivers/mssql.inc.php +++ b/adminer/drivers/mssql.inc.php @@ -195,6 +195,10 @@ if (isset($_GET["mssql"])) { ) ); + var $operators = array("=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL"); + var $functions = array("len", "lower", "round", "upper"); + var $grouping = array("avg", "count", "count distinct", "max", "min", "sum"); + function __construct($connection) { parent::__construct($connection); $this->types = array( //! use sys.types @@ -707,9 +711,6 @@ WHERE sys1.xtype = 'TR' AND sys2.name = " . q($table)) as $row 'possible_drivers' => array("SQLSRV", "PDO_SQLSRV", "PDO_DBLIB"), 'jush' => "mssql", 'unsigned' => array(), - 'operators' => array("=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL"), - 'functions' => array("len", "lower", "round", "upper"), - 'grouping' => array("avg", "count", "count distinct", "max", "min", "sum"), ); } } diff --git a/adminer/drivers/mysql.inc.php b/adminer/drivers/mysql.inc.php index c3399ab9..d5492825 100644 --- a/adminer/drivers/mysql.inc.php +++ b/adminer/drivers/mysql.inc.php @@ -281,6 +281,10 @@ if (!defined("DRIVER")) { class Driver extends SqlDriver { + var $operators = array("=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%", "REGEXP", "IN", "FIND_IN_SET", "IS NULL", "NOT LIKE", "NOT REGEXP", "NOT IN", "IS NOT NULL", "SQL"); + var $functions = array("char_length", "date", "from_unixtime", "lower", "round", "floor", "ceil", "sec_to_time", "time_to_sec", "upper"); + var $grouping = array("avg", "count", "count distinct", "group_concat", "max", "min", "sum"); + function __construct($connection) { parent::__construct($connection); $this->types = array( @@ -1193,16 +1197,13 @@ if (!defined("DRIVER")) { } /** Get driver config - * @return array ['possible_drivers' => , 'jush' => , 'unsigned' => , 'operators' => , 'functions' => , 'grouping' => ] + * @return array ['possible_drivers' => , 'jush' => , 'unsigned' => ] */ function driver_config() { return array( 'possible_drivers' => array("MySQLi", "MySQL", "PDO_MySQL"), 'jush' => "sql", ///< @var string JUSH identifier 'unsigned' => array("unsigned", "zerofill", "unsigned zerofill"), ///< @var array number variants - 'operators' => array("=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%", "REGEXP", "IN", "FIND_IN_SET", "IS NULL", "NOT LIKE", "NOT REGEXP", "NOT IN", "IS NOT NULL", "SQL"), ///< @var array operators used in select - 'functions' => array("char_length", "date", "from_unixtime", "lower", "round", "floor", "ceil", "sec_to_time", "time_to_sec", "upper"), ///< @var array functions used in select - 'grouping' => array("avg", "count", "count distinct", "group_concat", "max", "min", "sum"), ///< @var array grouping functions used in select ); } } diff --git a/adminer/drivers/oracle.inc.php b/adminer/drivers/oracle.inc.php index af33c6c6..0e11e878 100644 --- a/adminer/drivers/oracle.inc.php +++ b/adminer/drivers/oracle.inc.php @@ -152,6 +152,10 @@ if (isset($_GET["oracle"])) { ) ); + var $operators = array("=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL", "SQL"); + var $functions = array("length", "lower", "round", "upper"); + var $grouping = array("avg", "count", "count distinct", "max", "min", "sum"); + function __construct($connection) { parent::__construct($connection); $this->types = array( @@ -546,9 +550,6 @@ ORDER BY PROCESS 'possible_drivers' => array("OCI8", "PDO_OCI"), 'jush' => "oracle", 'unsigned' => array(), - 'operators' => array("=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL", "SQL"), - 'functions' => array("length", "lower", "round", "upper"), - 'grouping' => array("avg", "count", "count distinct", "max", "min", "sum"), ); } } diff --git a/adminer/drivers/pgsql.inc.php b/adminer/drivers/pgsql.inc.php index 180c6baf..c08bce14 100644 --- a/adminer/drivers/pgsql.inc.php +++ b/adminer/drivers/pgsql.inc.php @@ -196,6 +196,10 @@ if (isset($_GET["pgsql"])) { class Driver extends SqlDriver { + var $operators = array("=", "<", ">", "<=", ">=", "!=", "~", "!~", "LIKE", "LIKE %%", "ILIKE", "ILIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL"); // no "SQL" to avoid CSRF + var $functions = array("char_length", "lower", "round", "to_hex", "to_timestamp", "upper"); + var $grouping = array("avg", "count", "count distinct", "max", "min", "sum"); + function __construct($connection) { parent::__construct($connection); $this->types = array( //! arrays @@ -937,9 +941,6 @@ AND typelem = 0" 'possible_drivers' => array("PgSQL", "PDO_PgSQL"), 'jush' => "pgsql", 'unsigned' => array(), - 'operators' => array("=", "<", ">", "<=", ">=", "!=", "~", "!~", "LIKE", "LIKE %%", "ILIKE", "ILIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL"), // no "SQL" to avoid CSRF - 'functions' => array("char_length", "lower", "round", "to_hex", "to_timestamp", "upper"), - 'grouping' => array("avg", "count", "count distinct", "max", "min", "sum"), ); } } diff --git a/adminer/drivers/sqlite.inc.php b/adminer/drivers/sqlite.inc.php index 0a01b6b4..d428fbd3 100644 --- a/adminer/drivers/sqlite.inc.php +++ b/adminer/drivers/sqlite.inc.php @@ -139,6 +139,10 @@ if (isset($_GET["sqlite"])) { ) ); + var $operators = array("=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL", "SQL"); // REGEXP can be user defined function + var $functions = array("hex", "length", "lower", "round", "unixepoch", "upper"); + var $grouping = array("avg", "count", "count distinct", "group_concat", "max", "min", "sum"); + function structuredTypes() { return array_keys($this->types[0]); } @@ -739,9 +743,6 @@ if (isset($_GET["sqlite"])) { 'possible_drivers' => array("SQLite3", "PDO_SQLite"), 'jush' => "sqlite", 'unsigned' => array(), - 'operators' => array("=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL", "SQL"), // REGEXP can be user defined function - 'functions' => array("hex", "length", "lower", "round", "unixepoch", "upper"), - 'grouping' => array("avg", "count", "count distinct", "group_concat", "max", "min", "sum"), ); } } diff --git a/adminer/include/adminer.inc.php b/adminer/include/adminer.inc.php index c90c8a3c..f7de73a3 100644 --- a/adminer/include/adminer.inc.php +++ b/adminer/include/adminer.inc.php @@ -361,7 +361,7 @@ class Adminer { * @return null */ function selectColumnsPrint($select, $columns) { - global $functions, $grouping; + global $driver; print_fieldset("select", lang('Select'), $select); $i = 0; $select[""] = array(); @@ -373,8 +373,8 @@ class Adminer { $val["col"], ($key !== "" ? "selectFieldChange" : "selectAddRow") ); - echo "
" . ($functions || $grouping ? "" + echo "
" . ($driver->functions || $driver->grouping ? "" . on_help("getTarget(event).value && getTarget(event).value.replace(/ |\$/, '(') + ')'", 1) . script("qsl('select').onchange = function () { helpClose();" . ($key !== "" ? "" : " qsl('select, input', this.parentNode).onchange();") . " };", "") . "($column)" : $column) . "
\n"; @@ -518,13 +518,13 @@ class Adminer { * @return array [[select_expressions], [group_expressions]] */ function selectColumnsProcess($columns, $indexes) { - global $functions, $grouping; + global $driver; $select = array(); // select expressions, empty for * $group = array(); // expressions without aggregation - will be used for GROUP BY if an aggregation function is used foreach ((array) $_GET["columns"] as $key => $val) { - if ($val["fun"] == "count" || ($val["col"] != "" && (!$val["fun"] || in_array($val["fun"], $functions) || in_array($val["fun"], $grouping)))) { + if ($val["fun"] == "count" || ($val["col"] != "" && (!$val["fun"] || in_array($val["fun"], $driver->functions) || in_array($val["fun"], $driver->grouping)))) { $select[$key] = apply_sql_function($val["fun"], ($val["col"] != "" ? idf_escape($val["col"]) : "*")); - if (!in_array($val["fun"], $grouping)) { + if (!in_array($val["fun"], $driver->grouping)) { $group[] = $select[$key]; } } diff --git a/adminer/include/auth.inc.php b/adminer/include/auth.inc.php index 7097cff3..b345c9fe 100644 --- a/adminer/include/auth.inc.php +++ b/adminer/include/auth.inc.php @@ -165,6 +165,9 @@ if (isset($_GET["username"]) && is_string(get_password())) { check_invalid_login(); $connection = connect(); $driver = new Driver($connection); + if ($adminer->operators === null) { + $adminer->operators = $driver->operators; + } } $login = null; diff --git a/adminer/include/bootstrap.inc.php b/adminer/include/bootstrap.inc.php index 2472dcd8..a0ac55df 100644 --- a/adminer/include/bootstrap.inc.php +++ b/adminer/include/bootstrap.inc.php @@ -35,7 +35,7 @@ if ($_GET["script"] == "version") { exit; } -global $adminer, $connection, $driver, $drivers, $enum_length, $error, $functions, $grouping, $HTTPS, $inout, $jush, $LANG, $langs, $on_actions, $permanent, $has_token, $token, $translations, $unsigned, $VERSION; // allows including Adminer inside a function +global $adminer, $connection, $driver, $drivers, $enum_length, $error, $HTTPS, $inout, $jush, $LANG, $langs, $on_actions, $permanent, $has_token, $token, $translations, $unsigned, $VERSION; // allows including Adminer inside a function if (!$_SERVER["REQUEST_URI"]) { // IIS 5 compatibility $_SERVER["REQUEST_URI"] = $_SERVER["ORIG_PATH_INFO"]; @@ -83,12 +83,6 @@ $config = driver_config(); $possible_drivers = $config['possible_drivers']; $jush = $config['jush']; $unsigned = $config['unsigned']; -$operators = $config['operators']; -$functions = $config['functions']; -$grouping = $config['grouping']; -if ($adminer->operators === null) { - $adminer->operators = $operators; -} define("SERVER", $_GET[DRIVER]); // read from pgsql=localhost define("DB", $_GET["db"]); // for the sake of speed and size diff --git a/adminer/include/driver.inc.php b/adminer/include/driver.inc.php index 2f575499..3aaf801b 100644 --- a/adminer/include/driver.inc.php +++ b/adminer/include/driver.inc.php @@ -26,6 +26,9 @@ abstract class SqlDriver { var $_conn; protected $types = array(); ///< @var array [$description => [$type => $maximum_unsigned_length, ...], ...] var $editFunctions = array(); ///< @var array of ["$type|$type2" => "$function/$function2"] functions used in editing, [0] - edit and insert, [1] - edit only + var $operators = array(); ///< @var array operators used in select + var $functions = array(); ///< @var array functions used in select + var $grouping = array(); ///< @var array grouping functions used in select /** Create object for performing database operations * @param Db diff --git a/plugins/drivers/clickhouse.php b/plugins/drivers/clickhouse.php index d58579c1..2ed7c76d 100644 --- a/plugins/drivers/clickhouse.php +++ b/plugins/drivers/clickhouse.php @@ -131,6 +131,9 @@ if (isset($_GET["clickhouse"])) { class Driver extends SqlDriver { + var $operators = array("=", "<", ">", "<=", ">=", "!=", "~", "!~", "LIKE", "LIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL", "SQL"); + var $grouping = array("avg", "count", "count distinct", "max", "min", "sum"); + function __construct($connection) { parent::__construct($connection); $this->types = array( //! arrays @@ -382,9 +385,6 @@ if (isset($_GET["clickhouse"])) { return array( 'jush' => "clickhouse", 'unsigned' => array(), - 'operators' => array("=", "<", ">", "<=", ">=", "!=", "~", "!~", "LIKE", "LIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL", "SQL"), - 'functions' => array(), - 'grouping' => array("avg", "count", "count distinct", "max", "min", "sum"), ); } } diff --git a/plugins/drivers/elastic.php b/plugins/drivers/elastic.php index 0286bf8f..0b20d63f 100644 --- a/plugins/drivers/elastic.php +++ b/plugins/drivers/elastic.php @@ -123,6 +123,7 @@ if (isset($_GET["elastic"])) { class Driver extends SqlDriver { var $editFunctions = array(array("json")); + var $operators = array("=", "must", "should", "must_not"); function __construct($connection) { parent::__construct($connection); @@ -568,9 +569,6 @@ if (isset($_GET["elastic"])) { return array( 'possible_drivers' => array("json + allow_url_fopen"), 'jush' => "elastic", - 'operators' => array("=", "must", "should", "must_not"), - 'functions' => array(), - 'grouping' => array(), ); } } diff --git a/plugins/drivers/elastic5.php b/plugins/drivers/elastic5.php index c0766346..f204b725 100644 --- a/plugins/drivers/elastic5.php +++ b/plugins/drivers/elastic5.php @@ -114,6 +114,7 @@ if (isset($_GET["elastic5"])) { class Driver extends SqlDriver { var $editFunctions = array(array("json")); + var $operators = array("=", "must", "should", "must_not"); function __construct($connection) { parent::__construct($connection); @@ -534,9 +535,6 @@ if (isset($_GET["elastic5"])) { return array( 'possible_drivers' => array("json + allow_url_fopen"), 'jush' => "elastic", - 'operators' => array("=", "must", "should", "must_not"), - 'functions' => array(), - 'grouping' => array(), ); } } diff --git a/plugins/drivers/firebird.php b/plugins/drivers/firebird.php index c947e079..6320fd3f 100644 --- a/plugins/drivers/firebird.php +++ b/plugins/drivers/firebird.php @@ -115,6 +115,7 @@ if (isset($_GET["firebird"])) { class Driver extends SqlDriver { + var $operators = array("="); } @@ -305,9 +306,6 @@ ORDER BY RDB$INDEX_SEGMENTS.RDB$FIELD_POSITION'; return array( 'possible_drivers' => array("interbase"), 'jush' => "firebird", - 'operators' => array("="), - 'functions' => array(), - 'grouping' => array(), ); } } diff --git a/plugins/drivers/simpledb.php b/plugins/drivers/simpledb.php index 4b6bfc3d..b04539ef 100644 --- a/plugins/drivers/simpledb.php +++ b/plugins/drivers/simpledb.php @@ -118,6 +118,9 @@ if (isset($_GET["simpledb"])) { class Driver extends SqlDriver { + var $operators = array("=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%", "IN", "IS NULL", "NOT LIKE", "IS NOT NULL"); + var $grouping = array("count"); + public $primary = "itemName()"; function _chunkRequest($ids, $action, $params, $expand = array()) { @@ -467,9 +470,6 @@ if (isset($_GET["simpledb"])) { return array( 'possible_drivers' => array("SimpleXML + allow_url_fopen"), 'jush' => "simpledb", - 'operators' => array("=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%", "IN", "IS NULL", "NOT LIKE", "IS NOT NULL"), - 'functions' => array(), - 'grouping' => array("count"), ); } }