From c136a747055bd73141517692c4d5e88ef284e7a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Wed, 6 Apr 2016 09:45:43 +0200 Subject: [PATCH] Cache current user information for DBI lifetime MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We query this information quite often, so it makes sense to fetch it only once from the MySQL server. Signed-off-by: Michal Čihař --- libraries/DatabaseInterface.php | 19 ++++++++++++++----- libraries/Util.php | 12 ++++-------- libraries/check_user_privileges.lib.php | 4 ++-- libraries/server_privileges.lib.php | 7 +------ user_password.php | 4 +--- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/libraries/DatabaseInterface.php b/libraries/DatabaseInterface.php index 85f0839dbb..900126026d 100644 --- a/libraries/DatabaseInterface.php +++ b/libraries/DatabaseInterface.php @@ -48,6 +48,11 @@ class DatabaseInterface */ private $_table_cache; + /** + * @var array Current user and host cache + */ + private $_current_user; + /** * Constructor * @@ -57,6 +62,7 @@ class DatabaseInterface { $this->_extension = $ext; $this->_table_cache = array(); + $this->_current_user = array(); } /** @@ -2138,12 +2144,12 @@ class DatabaseInterface if ($type === 'super') { $query = 'SELECT 1 FROM mysql.user LIMIT 1'; } elseif ($type === 'create') { - list($user, $host) = $this->_getCurrentUserAndHost(); + list($user, $host) = $this->getCurrentUserAndHost(); $query = "SELECT 1 FROM `INFORMATION_SCHEMA`.`USER_PRIVILEGES` " . "WHERE `PRIVILEGE_TYPE` = 'CREATE USER' AND " . "'''" . $user . "''@''" . $host . "''' LIKE `GRANTEE` LIMIT 1"; } elseif ($type === 'grant') { - list($user, $host) = $this->_getCurrentUserAndHost(); + list($user, $host) = $this->getCurrentUserAndHost(); $query = "SELECT 1 FROM (" . "SELECT `GRANTEE`, `IS_GRANTABLE` FROM " . "`INFORMATION_SCHEMA`.`COLUMN_PRIVILEGES` UNION " @@ -2207,10 +2213,13 @@ class DatabaseInterface * * @return array array of username and hostname */ - private function _getCurrentUserAndHost() + public function getCurrentUserAndHost() { - $user = $GLOBALS['dbi']->fetchValue("SELECT CURRENT_USER();"); - return explode("@", $user); + if (count($this->_current_user) == 0) { + $user = $GLOBALS['dbi']->fetchValue("SELECT CURRENT_USER();"); + $this->_current_user = explode("@", $user); + } + return $this->_current_user; } /** diff --git a/libraries/Util.php b/libraries/Util.php index 3c7ab5091a..4806972a4c 100644 --- a/libraries/Util.php +++ b/libraries/Util.php @@ -4036,20 +4036,16 @@ class Util { // Get the username for the current user in the format // required to use in the information schema database. - $user = $GLOBALS['dbi']->fetchValue("SELECT CURRENT_USER();"); - if ($user === false) { - return false; - } + list($user, $host) = $GLOBALS['dbi']->getCurrentUserAndHost(); - if ($user == '@') { // MySQL is started with --skip-grant-tables + if ($user === '') { // MySQL is started with --skip-grant-tables return true; } - $user = explode('@', $user); $username = "''"; - $username .= str_replace("'", "''", $user[0]); + $username .= str_replace("'", "''", $user); $username .= "''@''"; - $username .= str_replace("'", "''", $user[1]); + $username .= str_replace("'", "''", $host); $username .= "''"; // Prepare the query diff --git a/libraries/check_user_privileges.lib.php b/libraries/check_user_privileges.lib.php index 1d21159394..7c1f13dd63 100644 --- a/libraries/check_user_privileges.lib.php +++ b/libraries/check_user_privileges.lib.php @@ -306,8 +306,8 @@ function PMA_analyseShowGrant() PMA\libraries\Util::cacheSet('db_priv', $GLOBALS['db_priv']); } // end function -$user = $GLOBALS['dbi']->fetchValue("SELECT CURRENT_USER();"); -if ($user == '@') { // MySQL is started with --skip-grant-tables +list($username, $hostname) = $GLOBALS['dbi']->getCurrentUserAndHost(); +if ($username === '') { // MySQL is started with --skip-grant-tables $GLOBALS['is_create_db_priv'] = true; $GLOBALS['is_reload_priv'] = true; $GLOBALS['db_to_create'] = ''; diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 50c39843e6..ff32942d3a 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -1966,12 +1966,7 @@ function PMA_getCurrentAuthenticationPlugin( $authentication_plugin = $row['plugin']; } } elseif ($mode == 'change') { - $row = $GLOBALS['dbi']->fetchSingleRow( - 'SELECT CURRENT_USER() as user;' - ); - if (isset($row) && $row) { - list($username, $hostname) = explode('@', $row['user']); - } + list($username, $hostname) = $GLOBALS['dbi']->getCurrentUserAndHost(); $row = $GLOBALS['dbi']->fetchSingleRow( 'SELECT `plugin` FROM `mysql`.`user` WHERE ' diff --git a/user_password.php b/user_password.php index b6d9445459..e9764e86f5 100644 --- a/user_password.php +++ b/user_password.php @@ -141,9 +141,7 @@ function PMA_changePassword($password, $message, $change_password_message) $hashing_function = PMA_changePassHashingFunction(); - $row = $GLOBALS['dbi']->fetchSingleRow('SELECT CURRENT_USER() as user'); - $curr_user = $row['user']; - list($username, $hostname) = explode('@', $curr_user); + list($username, $hostname) = $GLOBALS['dbi']->getCurrentUserAndHost(); $serverType = PMA\libraries\Util::getServerType();