';
return [
$title,
$export,
];
}
/**
* Get HTML for display Add userfieldset
*
* @param string $db the database
* @param string $table the table name
*
* @return string html output
*/
public function getAddUserHtmlFieldset($db = '', $table = '')
{
if (! $this->dbi->isCreateUser()) {
return '';
}
$rel_params = [];
$url_params = ['adduser' => 1];
if (! empty($db)) {
$url_params['dbname']
= $rel_params['checkprivsdb']
= $db;
}
if (! empty($table)) {
$url_params['tablename']
= $rel_params['checkprivstable']
= $table;
}
return $this->template->render('server/privileges/add_user_fieldset', [
'url_params' => $url_params,
'rel_params' => $rel_params,
]);
}
/**
* Get HTML snippet for display user overview page
*
* @param string $themeImagePath a image source link
* @param string $text_dir text directory
*
* @return string
*/
public function getHtmlForUserOverview($themeImagePath, $text_dir)
{
$password_column = 'Password';
$server_type = Util::getServerType();
$serverVersion = $this->dbi->getVersion();
if (($server_type === 'MySQL' || $server_type === 'Percona Server')
&& $serverVersion >= 50706
) {
$password_column = 'authentication_string';
}
// $sql_query is for the initial-filtered,
// $sql_query_all is for counting the total no. of users
$sql_query = $sql_query_all = 'SELECT *,' .
' IF(`' . $password_column . "` = _latin1 '', 'N', 'Y') AS 'Password'" .
' FROM `mysql`.`user`';
$sql_query .= (isset($_GET['initial'])
? $this->rangeOfUsers($_GET['initial'])
: '');
$sql_query .= ' ORDER BY `User` ASC, `Host` ASC;';
$sql_query_all .= ' ;';
$res = $this->dbi->tryQuery(
$sql_query,
DatabaseInterface::CONNECT_USER,
DatabaseInterface::QUERY_STORE
);
$res_all = $this->dbi->tryQuery(
$sql_query_all,
DatabaseInterface::CONNECT_USER,
DatabaseInterface::QUERY_STORE
);
$errorMessages = '';
if (! $res) {
// the query failed! This may have two reasons:
// - the user does not have enough privileges
// - the privilege tables use a structure of an earlier version.
// so let's try a more simple query
$this->dbi->freeResult($res);
$this->dbi->freeResult($res_all);
$sql_query = 'SELECT * FROM `mysql`.`user`';
$res = $this->dbi->tryQuery(
$sql_query,
DatabaseInterface::CONNECT_USER,
DatabaseInterface::QUERY_STORE
);
if (! $res) {
$errorMessages .= $this->getHtmlForViewUsersError();
$errorMessages .= $this->getAddUserHtmlFieldset();
} else {
// This message is hardcoded because I will replace it by
// a automatic repair feature soon.
$raw = 'Your privilege table structure seems to be older than'
. ' this MySQL version!
'
. 'Please run the
mysql_upgrade command'
. ' that should be included in your MySQL server distribution'
. ' to solve this problem!';
$errorMessages .= Message::rawError($raw)->getDisplay();
}
$this->dbi->freeResult($res);
} else {
$db_rights = $this->getDbRightsForUserOverview();
// for all initials, even non A-Z
$array_initials = [];
foreach ($db_rights as $right) {
foreach ($right as $account) {
if (empty($account['User']) && $account['Host'] === 'localhost') {
$emptyUserNotice = Message::notice(
__(
'A user account allowing any user from localhost to '
. 'connect is present. This will prevent other users '
. 'from connecting if the host part of their account '
. 'allows a connection from any (%) host.'
)
. MySQLDocumentation::show('problems-connecting')
)->getDisplay();
break 2;
}
}
}
/**
* Displays the initials
* Also not necessary if there is less than 20 privileges
*/
if ($this->dbi->numRows($res_all) > 20) {
$initials = $this->getHtmlForInitials($array_initials);
}
/**
* Display the user overview
* (if less than 50 users, display them immediately)
*/
if (isset($_GET['initial'])
|| isset($_GET['showall'])
|| $this->dbi->numRows($res) < 50
) {
$usersOverview = $this->getUsersOverview(
$res,
$db_rights,
$themeImagePath,
$text_dir
);
}
$response = Response::getInstance();
if (! $response->isAjax()
|| ! empty($_REQUEST['ajax_page_request'])
) {
if ($GLOBALS['is_reload_priv']) {
$flushnote = new Message(
__(
'Note: phpMyAdmin gets the users’ privileges directly '
. 'from MySQL’s privilege tables. The content of these '
. 'tables may differ from the privileges the server uses, '
. 'if they have been changed manually. In this case, '
. 'you should %sreload the privileges%s before you continue.'
),
Message::NOTICE
);
$flushnote->addParamHtml(
'
'
);
$flushnote->addParamHtml('');
} else {
$flushnote = new Message(
__(
'Note: phpMyAdmin gets the users’ privileges directly '
. 'from MySQL’s privilege tables. The content of these '
. 'tables may differ from the privileges the server uses, '
. 'if they have been changed manually. In this case, '
. 'the privileges have to be reloaded but currently, you '
. 'don\'t have the RELOAD privilege.'
)
. MySQLDocumentation::show(
'privileges-provided',
false,
null,
null,
'priv_reload'
),
Message::NOTICE
);
}
$flushNotice = $flushnote->getDisplay();
}
}
return $this->template->render('server/privileges/user_overview', [
'error_messages' => $errorMessages,
'empty_user_notice' => $emptyUserNotice ?? '',
'initials' => $initials ?? '',
'users_overview' => $usersOverview ?? '',
'is_createuser' => $this->dbi->isCreateUser(),
'flush_notice' => $flushNotice ?? '',
]);
}
/**
* Get HTML snippet for display user properties
*
* @param bool $dbname_is_wildcard whether database name is wildcard or not
* @param string $url_dbname url database name that urlencode() string
* @param string $username username
* @param string $hostname host name
* @param string|array $dbname database name
* @param string $tablename table name
*
* @return string
*/
public function getHtmlForUserProperties(
$dbname_is_wildcard,
$url_dbname,
$username,
$hostname,
$dbname,
$tablename
) {
global $cfg;
$sql = "SELECT '1' FROM `mysql`.`user`"
. " WHERE `User` = '" . $this->dbi->escapeString($username) . "'"
. " AND `Host` = '" . $this->dbi->escapeString($hostname) . "';";
$user_does_not_exists = (bool) ! $this->dbi->fetchValue($sql);
$loginInformationFields = '';
if ($user_does_not_exists) {
$loginInformationFields = $this->getHtmlForLoginInformationFields();
}
$_params = [
'username' => $username,
'hostname' => $hostname,
];
if (! is_array($dbname) && strlen($dbname) > 0) {
$_params['dbname'] = $dbname;
if (strlen($tablename) > 0) {
$_params['tablename'] = $tablename;
}
} else {
$_params['dbname'] = $dbname;
}
$privilegesTable = $this->getHtmlToDisplayPrivilegesTable(
// If $dbname is an array, pass any one db as all have same privs.
Core::ifSetOr($dbname, is_array($dbname) ? $dbname[0] : '*', 'length'),
Core::ifSetOr($tablename, '*', 'length')
);
$tableSpecificRights = '';
if (! is_array($dbname) && strlen($tablename) === 0
&& empty($dbname_is_wildcard)
) {
// no table name was given, display all table specific rights
// but only if $dbname contains no wildcards
if (strlen($dbname) === 0) {
$tableSpecificRights .= $this->getHtmlForAllTableSpecificRights(
$username,
$hostname,
'database'
);
} else {
// unescape wildcards in dbname at table level
$unescaped_db = Util::unescapeMysqlWildcards($dbname);
$tableSpecificRights .= $this->getHtmlForAllTableSpecificRights(
$username,
$hostname,
'table',
$unescaped_db
);
$tableSpecificRights .= $this->getHtmlForAllTableSpecificRights(
$username,
$hostname,
'routine',
$unescaped_db
);
}
}
$databaseUrl = Util::getScriptNameForOption(
$cfg['DefaultTabDatabase'],
'database'
);
$databaseUrlTitle = Util::getTitleForTarget(
$cfg['DefaultTabDatabase']
);
$tableUrl = Util::getScriptNameForOption(
$cfg['DefaultTabTable'],
'table'
);
$tableUrlTitle = Util::getTitleForTarget(
$cfg['DefaultTabTable']
);
$changePassword = '';
$userGroup = '';
$changeLoginInfoFields = '';
if (! is_array($dbname) && strlen($dbname) === 0 && ! $user_does_not_exists) {
//change login information
$changePassword = $this->getFormForChangePassword($username, $hostname, true);
$userGroup = $this->getUserGroupForUser($username);
$changeLoginInfoFields = $this->getHtmlForLoginInformationFields('change', $username, $hostname);
}
return $this->template->render('server/privileges/user_properties', [
'user_does_not_exists' => $user_does_not_exists,
'login_information_fields' => $loginInformationFields,
'params' => $_params,
'privileges_table' => $privilegesTable,
'table_specific_rights' => $tableSpecificRights,
'change_password' => $changePassword,
'database' => $dbname,
'dbname' => $url_dbname,
'username' => $username,
'hostname' => $hostname,
'is_databases' => $dbname_is_wildcard || is_array($dbname) && count($dbname) > 1,
'is_wildcard' => $dbname_is_wildcard,
'table' => $tablename,
'current_user' => $this->dbi->getCurrentUser(),
'user_group' => $userGroup,
'change_login_info_fields' => $changeLoginInfoFields,
'database_url' => $databaseUrl,
'database_url_title' => $databaseUrlTitle,
'table_url' => $tableUrl,
'table_url_title' => $tableUrlTitle,
]);
}
/**
* Get queries for Table privileges to change or copy user
*
* @param string $user_host_condition user host condition to
* select relevant table privileges
* @param array $queries queries array
* @param string $username username
* @param string $hostname host name
*
* @return array
*/
public function getTablePrivsQueriesForChangeOrCopyUser(
$user_host_condition,
array $queries,
$username,
$hostname
) {
$res = $this->dbi->query(
'SELECT `Db`, `Table_name`, `Table_priv` FROM `mysql`.`tables_priv`'
. $user_host_condition,
DatabaseInterface::CONNECT_USER,
DatabaseInterface::QUERY_STORE
);
while ($row = $this->dbi->fetchAssoc($res)) {
$res2 = $this->dbi->query(
'SELECT `Column_name`, `Column_priv`'
. ' FROM `mysql`.`columns_priv`'
. ' WHERE `User`'
. ' = \'' . $this->dbi->escapeString($_POST['old_username']) . "'"
. ' AND `Host`'
. ' = \'' . $this->dbi->escapeString($_POST['old_username']) . '\''
. ' AND `Db`'
. ' = \'' . $this->dbi->escapeString($row['Db']) . "'"
. ' AND `Table_name`'
. ' = \'' . $this->dbi->escapeString($row['Table_name']) . "'"
. ';',
DatabaseInterface::CONNECT_USER,
DatabaseInterface::QUERY_STORE
);
$tmp_privs1 = $this->extractPrivInfo($row);
$tmp_privs2 = [
'Select' => [],
'Insert' => [],
'Update' => [],
'References' => [],
];
while ($row2 = $this->dbi->fetchAssoc($res2)) {
$tmp_array = explode(',', $row2['Column_priv']);
if (in_array('Select', $tmp_array)) {
$tmp_privs2['Select'][] = $row2['Column_name'];
}
if (in_array('Insert', $tmp_array)) {
$tmp_privs2['Insert'][] = $row2['Column_name'];
}
if (in_array('Update', $tmp_array)) {
$tmp_privs2['Update'][] = $row2['Column_name'];
}
if (! in_array('References', $tmp_array)) {
continue;
}
$tmp_privs2['References'][] = $row2['Column_name'];
}
if (count($tmp_privs2['Select']) > 0 && ! in_array('SELECT', $tmp_privs1)) {
$tmp_privs1[] = 'SELECT (`' . implode('`, `', $tmp_privs2['Select']) . '`)';
}
if (count($tmp_privs2['Insert']) > 0 && ! in_array('INSERT', $tmp_privs1)) {
$tmp_privs1[] = 'INSERT (`' . implode('`, `', $tmp_privs2['Insert']) . '`)';
}
if (count($tmp_privs2['Update']) > 0 && ! in_array('UPDATE', $tmp_privs1)) {
$tmp_privs1[] = 'UPDATE (`' . implode('`, `', $tmp_privs2['Update']) . '`)';
}
if (count($tmp_privs2['References']) > 0
&& ! in_array('REFERENCES', $tmp_privs1)
) {
$tmp_privs1[]
= 'REFERENCES (`' . implode('`, `', $tmp_privs2['References']) . '`)';
}
$queries[] = 'GRANT ' . implode(', ', $tmp_privs1)
. ' ON ' . Util::backquote($row['Db']) . '.'
. Util::backquote($row['Table_name'])
. ' TO \'' . $this->dbi->escapeString($username)
. '\'@\'' . $this->dbi->escapeString($hostname) . '\''
. (in_array('Grant', explode(',', $row['Table_priv']))
? ' WITH GRANT OPTION;'
: ';');
}
return $queries;
}
/**
* Get queries for database specific privileges for change or copy user
*
* @param array $queries queries array with string
* @param string $username username
* @param string $hostname host name
*
* @return array
*/
public function getDbSpecificPrivsQueriesForChangeOrCopyUser(
array $queries,
$username,
$hostname
) {
$user_host_condition = ' WHERE `User`'
. ' = \'' . $this->dbi->escapeString($_POST['old_username']) . "'"
. ' AND `Host`'
. ' = \'' . $this->dbi->escapeString($_POST['old_hostname']) . '\';';
$res = $this->dbi->query(
'SELECT * FROM `mysql`.`db`' . $user_host_condition
);
while ($row = $this->dbi->fetchAssoc($res)) {
$queries[] = 'GRANT ' . implode(', ', $this->extractPrivInfo($row))
. ' ON ' . Util::backquote($row['Db']) . '.*'
. ' TO \'' . $this->dbi->escapeString($username)
. '\'@\'' . $this->dbi->escapeString($hostname) . '\''
. ($row['Grant_priv'] === 'Y' ? ' WITH GRANT OPTION;' : ';');
}
$this->dbi->freeResult($res);
$queries = $this->getTablePrivsQueriesForChangeOrCopyUser(
$user_host_condition,
$queries,
$username,
$hostname
);
return $queries;
}
/**
* Prepares queries for adding users and
* also create database and return query and message
*
* @param bool $_error whether user create or not
* @param string $real_sql_query SQL query for add a user
* @param string $sql_query SQL query to be displayed
* @param string $username username
* @param string $hostname host name
* @param string $dbname database name
* @param string $alter_real_sql_query SQL query for ALTER USER
* @param string $alter_sql_query SQL query for ALTER USER to be displayed
*
* @return array
*/
public function addUserAndCreateDatabase(
$_error,
$real_sql_query,
$sql_query,
$username,
$hostname,
$dbname,
$alter_real_sql_query,
$alter_sql_query
): array {
if ($_error || (! empty($real_sql_query)
&& ! $this->dbi->tryQuery($real_sql_query))
) {
$_POST['createdb-1'] = $_POST['createdb-2']
= $_POST['createdb-3'] = null;
$message = Message::rawError((string) $this->dbi->getError());
} elseif ($alter_real_sql_query !== '' && ! $this->dbi->tryQuery($alter_real_sql_query)) {
$_POST['createdb-1'] = $_POST['createdb-2']
= $_POST['createdb-3'] = null;
$message = Message::rawError((string) $this->dbi->getError());
} else {
$sql_query .= $alter_sql_query;
$message = Message::success(__('You have added a new user.'));
}
if (isset($_POST['createdb-1'])) {
// Create database with same name and grant all privileges
$q = 'CREATE DATABASE IF NOT EXISTS '
. Util::backquote(
$this->dbi->escapeString($username)
) . ';';
$sql_query .= $q;
if (! $this->dbi->tryQuery($q)) {
$message = Message::rawError((string) $this->dbi->getError());
}
/**
* Reload the navigation
*/
$GLOBALS['reload'] = true;
$GLOBALS['db'] = $username;
$q = 'GRANT ALL PRIVILEGES ON '
. Util::backquote(
Util::escapeMysqlWildcards(
$this->dbi->escapeString($username)
)
) . '.* TO \''
. $this->dbi->escapeString($username)
. '\'@\'' . $this->dbi->escapeString($hostname) . '\';';
$sql_query .= $q;
if (! $this->dbi->tryQuery($q)) {
$message = Message::rawError((string) $this->dbi->getError());
}
}
if (isset($_POST['createdb-2'])) {
// Grant all privileges on wildcard name (username\_%)
$q = 'GRANT ALL PRIVILEGES ON '
. Util::backquote(
Util::escapeMysqlWildcards(
$this->dbi->escapeString($username)
) . '\_%'
) . '.* TO \''
. $this->dbi->escapeString($username)
. '\'@\'' . $this->dbi->escapeString($hostname) . '\';';
$sql_query .= $q;
if (! $this->dbi->tryQuery($q)) {
$message = Message::rawError((string) $this->dbi->getError());
}
}
if (isset($_POST['createdb-3'])) {
// Grant all privileges on the specified database to the new user
$q = 'GRANT ALL PRIVILEGES ON '
. Util::backquote(
$this->dbi->escapeString($dbname)
) . '.* TO \''
. $this->dbi->escapeString($username)
. '\'@\'' . $this->dbi->escapeString($hostname) . '\';';
$sql_query .= $q;
if (! $this->dbi->tryQuery($q)) {
$message = Message::rawError((string) $this->dbi->getError());
}
}
return [
$sql_query,
$message,
];
}
/**
* Get the hashed string for password
*
* @param string $password password
*
* @return string
*/
public function getHashedPassword($password)
{
$password = $this->dbi->escapeString($password);
$result = $this->dbi->fetchSingleRow(
"SELECT PASSWORD('" . $password . "') AS `password`;"
);
return $result['password'];
}
/**
* Check if MariaDB's 'simple_password_check'
* OR 'cracklib_password_check' is ACTIVE
*
* @return bool if at least one of the plugins is ACTIVE
*/
public function checkIfMariaDBPwdCheckPluginActive()
{
$serverVersion = $this->dbi->getVersion();
if (! (Util::getServerType() === 'MariaDB' && $serverVersion >= 100002)) {
return false;
}
$result = $this->dbi->tryQuery(
'SHOW PLUGINS SONAME LIKE \'%_password_check%\''
);
/* Plugins are not working, for example directory does not exists */
if ($result === false) {
return false;
}
while ($row = $this->dbi->fetchAssoc($result)) {
if ($row['Status'] === 'ACTIVE') {
return true;
}
}
return false;
}
/**
* Get SQL queries for Display and Add user
*
* @param string $username username
* @param string $hostname host name
* @param string $password password
*
* @return array ($create_user_real, $create_user_show, $real_sql_query, $sql_query
* $password_set_real, $password_set_show, $alter_real_sql_query, $alter_sql_query)
*/
public function getSqlQueriesForDisplayAndAddUser($username, $hostname, $password)
{
$slashedUsername = $this->dbi->escapeString($username);
$slashedHostname = $this->dbi->escapeString($hostname);
$slashedPassword = $this->dbi->escapeString($password);
$serverType = Util::getServerType();
$serverVersion = $this->dbi->getVersion();
$create_user_stmt = sprintf(
'CREATE USER \'%s\'@\'%s\'',
$slashedUsername,
$slashedHostname
);
$isMariaDBPwdPluginActive = $this->checkIfMariaDBPwdCheckPluginActive();
// See https://github.com/phpmyadmin/phpmyadmin/pull/11560#issuecomment-147158219
// for details regarding details of syntax usage for various versions
// 'IDENTIFIED WITH auth_plugin'
// is supported by MySQL 5.5.7+
if (($serverType === 'MySQL' || $serverType === 'Percona Server')
&& $serverVersion >= 50507
&& isset($_POST['authentication_plugin'])
) {
$create_user_stmt .= ' IDENTIFIED WITH '
. $_POST['authentication_plugin'];
}
// 'IDENTIFIED VIA auth_plugin'
// is supported by MariaDB 5.2+
if ($serverType === 'MariaDB'
&& $serverVersion >= 50200
&& isset($_POST['authentication_plugin'])
&& ! $isMariaDBPwdPluginActive
) {
$create_user_stmt .= ' IDENTIFIED VIA '
. $_POST['authentication_plugin'];
}
$create_user_real = $create_user_stmt;
$create_user_show = $create_user_stmt;
$password_set_stmt = 'SET PASSWORD FOR \'%s\'@\'%s\' = \'%s\'';
$password_set_show = sprintf(
$password_set_stmt,
$slashedUsername,
$slashedHostname,
'***'
);
$sql_query_stmt = sprintf(
'GRANT %s ON *.* TO \'%s\'@\'%s\'',
implode(', ', $this->extractPrivInfo()),
$slashedUsername,
$slashedHostname
);
$real_sql_query = $sql_query = $sql_query_stmt;
// Set the proper hashing method
if (isset($_POST['authentication_plugin'])) {
$this->setProperPasswordHashing(
$_POST['authentication_plugin']
);
}
// Use 'CREATE USER ... WITH ... AS ..' syntax for
// newer MySQL versions
// and 'CREATE USER ... VIA .. USING ..' syntax for
// newer MariaDB versions
if ((($serverType == 'MySQL' || $serverType == 'Percona Server')
&& $serverVersion >= 50706)
|| ($serverType == 'MariaDB'
&& $serverVersion >= 50200)
) {
$password_set_real = null;
// Required for binding '%' with '%s'
$create_user_stmt = str_replace(
'%',
'%%',
$create_user_stmt
);
// MariaDB uses 'USING' whereas MySQL uses 'AS'
// but MariaDB with validation plugin needs cleartext password
if ($serverType == 'MariaDB'
&& ! $isMariaDBPwdPluginActive
) {
$create_user_stmt .= ' USING \'%s\'';
} elseif ($serverType == 'MariaDB') {
$create_user_stmt .= ' IDENTIFIED BY \'%s\'';
} elseif (($serverType == 'MySQL' || $serverType == 'Percona Server') && $serverVersion >= 80011) {
if (mb_strpos($create_user_stmt, 'IDENTIFIED') === false) {
// Maybe the authentication_plugin was not posted and then a part is missing
$create_user_stmt .= ' IDENTIFIED BY \'%s\'';
} else {
$create_user_stmt .= ' BY \'%s\'';
}
} else {
$create_user_stmt .= ' AS \'%s\'';
}
if ($_POST['pred_password'] === 'keep') {
$create_user_real = sprintf(
$create_user_stmt,
$slashedPassword
);
$create_user_show = sprintf(
$create_user_stmt,
'***'
);
} elseif ($_POST['pred_password'] === 'none') {
$create_user_real = sprintf(
$create_user_stmt,
null
);
$create_user_show = sprintf(
$create_user_stmt,
'***'
);
} else {
if (! (($serverType === 'MariaDB' && $isMariaDBPwdPluginActive)
|| ($serverType === 'MySQL' || $serverType === 'Percona Server') && $serverVersion >= 80011)
) {
$hashedPassword = $this->getHashedPassword($_POST['pma_pw']);
} else {
// MariaDB with validation plugin needs cleartext password
$hashedPassword = $_POST['pma_pw'];
}
$create_user_real = sprintf(
$create_user_stmt,
$hashedPassword
);
$create_user_show = sprintf(
$create_user_stmt,
'***'
);
}
} else {
// Use 'SET PASSWORD' syntax for pre-5.7.6 MySQL versions
// and pre-5.2.0 MariaDB versions
if ($_POST['pred_password'] === 'keep') {
$password_set_real = sprintf(
$password_set_stmt,
$slashedUsername,
$slashedHostname,
$slashedPassword
);
} elseif ($_POST['pred_password'] === 'none') {
$password_set_real = sprintf(
$password_set_stmt,
$slashedUsername,
$slashedHostname,
null
);
} else {
$hashedPassword = $this->getHashedPassword($_POST['pma_pw']);
$password_set_real = sprintf(
$password_set_stmt,
$slashedUsername,
$slashedHostname,
$hashedPassword
);
}
}
$alter_real_sql_query = '';
$alter_sql_query = '';
if (($serverType === 'MySQL' || $serverType === 'Percona Server') && $serverVersion >= 80011) {
$sql_query_stmt = '';
if ((isset($_POST['Grant_priv']) && $_POST['Grant_priv'] === 'Y')
|| (isset($GLOBALS['Grant_priv']) && $GLOBALS['Grant_priv'] === 'Y')
) {
$sql_query_stmt = ' WITH GRANT OPTION';
}
$real_sql_query .= $sql_query_stmt;
$sql_query .= $sql_query_stmt;
$alter_sql_query_stmt = sprintf(
'ALTER USER \'%s\'@\'%s\'',
$slashedUsername,
$slashedHostname
);
$alter_real_sql_query = $alter_sql_query_stmt;
$alter_sql_query = $alter_sql_query_stmt;
}
// add REQUIRE clause
$require_clause = $this->getRequireClause();
$with_clause = $this->getWithClauseForAddUserAndUpdatePrivs();
if (($serverType === 'MySQL' || $serverType === 'Percona Server') && $serverVersion >= 80011) {
$alter_real_sql_query .= $require_clause;
$alter_sql_query .= $require_clause;
$alter_real_sql_query .= $with_clause;
$alter_sql_query .= $with_clause;
} else {
$real_sql_query .= $require_clause;
$sql_query .= $require_clause;
$real_sql_query .= $with_clause;
$sql_query .= $with_clause;
}
if ($alter_real_sql_query !== '') {
$alter_real_sql_query .= ';';
$alter_sql_query .= ';';
}
$create_user_real .= ';';
$create_user_show .= ';';
$real_sql_query .= ';';
$sql_query .= ';';
// No Global GRANT_OPTION privilege
if (! $this->dbi->isGrantUser()) {
$real_sql_query = '';
$sql_query = '';
}
// Use 'SET PASSWORD' for pre-5.7.6 MySQL versions
// and pre-5.2.0 MariaDB
if (($serverType === 'MySQL'
&& $serverVersion >= 50706)
|| ($serverType === 'MariaDB'
&& $serverVersion >= 50200)
) {
$password_set_real = null;
$password_set_show = null;
} else {
if ($password_set_real !== null) {
$password_set_real .= ';';
}
$password_set_show .= ';';
}
return [
$create_user_real,
$create_user_show,
$real_sql_query,
$sql_query,
$password_set_real,
$password_set_show,
$alter_real_sql_query,
$alter_sql_query,
];
}
/**
* Returns the type ('PROCEDURE' or 'FUNCTION') of the routine
*
* @param string $dbname database
* @param string $routineName routine
*
* @return string type
*/
public function getRoutineType($dbname, $routineName)
{
$routineData = $this->dbi->getRoutines($dbname);
foreach ($routineData as $routine) {
if ($routine['name'] === $routineName) {
return $routine['type'];
}
}
return '';
}
/**
* @param string $username User name
* @param string $hostname Host name
* @param string $database Database name
* @param string $routine Routine name
*
* @return array
*/
private function getRoutinePrivileges(
string $username,
string $hostname,
string $database,
string $routine
): array {
$sql = 'SELECT `Proc_priv`'
. ' FROM `mysql`.`procs_priv`'
. " WHERE `User` = '" . $this->dbi->escapeString($username) . "'"
. " AND `Host` = '" . $this->dbi->escapeString($hostname) . "'"
. " AND `Db` = '"
. $this->dbi->escapeString(Util::unescapeMysqlWildcards($database)) . "'"
. " AND `Routine_name` LIKE '" . $this->dbi->escapeString($routine) . "';";
$privileges = $this->dbi->fetchValue($sql);
if ($privileges === false) {
$privileges = '';
}
return $this->parseProcPriv($privileges);
}
public function getFormForChangePassword(string $username, string $hostname, bool $editOthers): string
{
global $route;
$isPrivileges = $route === '/server/privileges';
$serverType = Util::getServerType();
$serverVersion = $this->dbi->getVersion();
$origAuthPlugin = $this->getCurrentAuthenticationPlugin(
'change',
$username,
$hostname
);
$isNew = ($serverType === 'MySQL' && $serverVersion >= 50507)
|| ($serverType === 'MariaDB' && $serverVersion >= 50200);
$hasMoreAuthPlugins = ($serverType === 'MySQL' && $serverVersion >= 50706)
|| ($this->dbi->isSuperUser() && $editOthers);
$activeAuthPlugins = ['mysql_native_password' => __('Native MySQL authentication')];
if ($isNew && $hasMoreAuthPlugins) {
$activeAuthPlugins = $this->getActiveAuthPlugins();
if (isset($activeAuthPlugins['mysql_old_password'])) {
unset($activeAuthPlugins['mysql_old_password']);
}
}
return $this->template->render('server/privileges/change_password', [
'username' => $username,
'hostname' => $hostname,
'is_privileges' => $isPrivileges,
'is_new' => $isNew,
'has_more_auth_plugins' => $hasMoreAuthPlugins,
'active_auth_plugins' => $activeAuthPlugins,
'orig_auth_plugin' => $origAuthPlugin,
]);
}
}