From a424d469b60c0011e53f4ecc6bc0017da04cea34 Mon Sep 17 00:00:00 2001 From: Roy-Orbison Date: Mon, 22 May 2023 12:17:33 +0930 Subject: [PATCH] Enable regular expressions when searching data Allows use of the database-specific regex operator and pattern syntax when searching multiple tables. --- adminer/db.inc.php | 6 +++++- adminer/drivers/mongo.inc.php | 5 ++++- adminer/drivers/mysql.inc.php | 1 + adminer/drivers/pgsql.inc.php | 3 ++- adminer/include/bootstrap.inc.php | 2 ++ 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/adminer/db.inc.php b/adminer/db.inc.php index 6c879fce..6d7f8dd8 100644 --- a/adminer/db.inc.php +++ b/adminer/db.inc.php @@ -59,9 +59,13 @@ if ($adminer->homepage()) { echo ""; echo script("qsl('input').onkeydown = partialArg(bodyKeydown, 'search');", ""); echo " \n"; + if ($adminer->operator_regexp !== null) { + echo "

'; + echo doc_link(array('sql' => 'regexp.html', 'pgsql' => 'functions-matching.html#FUNCTIONS-POSIX-REGEXP')) . "

\n"; + } echo "\n"; if ($_POST["search"] && $_POST["query"] != "") { - $_GET["where"][0]["op"] = "LIKE %%"; + $_GET["where"][0]["op"] = $adminer->operator_regexp === null || empty($_POST['regexp']) ? "LIKE %%" : $adminer->operator_regexp; search_tables(); } } diff --git a/adminer/drivers/mongo.inc.php b/adminer/drivers/mongo.inc.php index 2b89c46a..9c8086c3 100644 --- a/adminer/drivers/mongo.inc.php +++ b/adminer/drivers/mongo.inc.php @@ -206,6 +206,7 @@ if (isset($_GET["mongo"])) { } $operators = array("="); + $operator_regexp = null; } elseif (class_exists('MongoDB\Driver\Manager')) { class Min_DB { @@ -576,6 +577,7 @@ if (isset($_GET["mongo"])) { "(date)<=", ); + $operator_regexp = 'regex'; } function table($idf) { @@ -732,11 +734,12 @@ if (isset($_GET["mongo"])) { } function driver_config() { - global $operators; + global $operators, $operator_regexp; return array( 'possible_drivers' => array("mongo", "mongodb"), 'jush' => "mongo", 'operators' => $operators, + 'operator_regexp' => $operator_regexp, 'functions' => array(), 'grouping' => array(), 'edit_functions' => array(array("json")), diff --git a/adminer/drivers/mysql.inc.php b/adminer/drivers/mysql.inc.php index 7199c2e8..e9596ad0 100644 --- a/adminer/drivers/mysql.inc.php +++ b/adminer/drivers/mysql.inc.php @@ -1161,6 +1161,7 @@ if (!defined("DRIVER")) { '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", "unix_timestamp", "lower", "round", "floor", "ceil", "sec_to_time", "time_to_sec", "upper"), ///< @var array functions used in select + 'operator_regexp' => 'REGEXP', 'grouping' => array("avg", "count", "count distinct", "group_concat", "max", "min", "sum"), ///< @var array grouping functions used in select 'edit_functions' => array( ///< @var array of array("$type|$type2" => "$function/$function2") functions used in editing, [0] - edit and insert, [1] - edit only array( diff --git a/adminer/drivers/pgsql.inc.php b/adminer/drivers/pgsql.inc.php index 198e6429..4adc248e 100644 --- a/adminer/drivers/pgsql.inc.php +++ b/adminer/drivers/pgsql.inc.php @@ -909,7 +909,8 @@ AND typelem = 0" 'types' => $types, 'structured_types' => $structured_types, 'unsigned' => array(), - 'operators' => array("=", "<", ">", "<=", ">=", "!=", "~", "!~", "LIKE", "LIKE %%", "ILIKE", "ILIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL"), // no "SQL" to avoid CSRF + 'operators' => array("=", "<", ">", "<=", ">=", "!=", "~", "~*", "!~", "!~*", "LIKE", "LIKE %%", "ILIKE", "ILIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL"), // no "SQL" to avoid CSRF + 'operator_regexp' => '~*', 'functions' => array("char_length", "lower", "round", "to_hex", "to_timestamp", "upper"), 'grouping' => array("avg", "count", "count distinct", "max", "min", "sum"), 'edit_functions' => array( diff --git a/adminer/include/bootstrap.inc.php b/adminer/include/bootstrap.inc.php index 8eaff399..01a9d0b6 100644 --- a/adminer/include/bootstrap.inc.php +++ b/adminer/include/bootstrap.inc.php @@ -93,11 +93,13 @@ $types = $config['types']; $structured_types = $config['structured_types']; $unsigned = $config['unsigned']; $operators = $config['operators']; +$operator_regexp = isset($config['operator_regexp']) && in_array($config['operator_regexp'], $operators) ? $config['operator_regexp'] : null; $functions = $config['functions']; $grouping = $config['grouping']; $edit_functions = $config['edit_functions']; if ($adminer->operators === null) { $adminer->operators = $operators; + $adminer->operator_regexp = $operator_regexp; } define("SERVER", $_GET[DRIVER]); // read from pgsql=localhost