';
$html_output .= PMA_Util::getWithSelected(
$GLOBALS['pmaThemeImage'], $GLOBALS['text_dir'], "usersForm"
);
$html_output .= PMA_Util::getButtonOrImage(
'submit_mult', 'mult_submit', 'submit_mult_export',
__('Export'), 'b_tblexport.png', 'export'
);
$html_output .= '';
$html_output .= '';
} else {
$html_output .= PMA_getHtmlForViewUsersError();
}
// Offer to create a new user for the current database
$html_output .= PMA_getAddUserHtmlFieldset($db, $table);
return $html_output;
}
/**
* gets privilege map
*
* @param string $db the database
*
* @return array $privMap the privilege map
*/
function PMA_getPrivMap($db)
{
list($listOfPrivs, $listOfComparedPrivs)
= PMA_getListOfPrivilegesAndComparedPrivileges();
$sql_query
= "("
. " SELECT " . $listOfPrivs . ", '*' AS `Db`, 'g' AS `Type`"
. " FROM `mysql`.`user`"
. " WHERE NOT (" . $listOfComparedPrivs . ")"
. ")"
. " UNION "
. "("
. " SELECT " . $listOfPrivs . ", `Db`, 'd' AS `Type`"
. " FROM `mysql`.`db`"
. " WHERE '" . PMA_Util::sqlAddSlashes($db) . "' LIKE `Db`"
. " AND NOT (" . $listOfComparedPrivs . ")"
. ")"
. " ORDER BY `User` ASC, `Host` ASC, `Db` ASC;";
$res = $GLOBALS['dbi']->query($sql_query);
$privMap = array();
PMA_mergePrivMapFromResult($privMap, $res);
return $privMap;
}
/**
* merge privilege map and rows from resultset
*
* @param array &$privMap the privilege map reference
* @param object $result the resultset of query
*
* @return void
*/
function PMA_mergePrivMapFromResult(&$privMap, $result)
{
while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
$user = $row['User'];
$host = $row['Host'];
if (! isset($privMap[$user])) {
$privMap[$user] = array();
}
if (! isset($privMap[$user][$host])) {
$privMap[$user][$host] = array();
}
$privMap[$user][$host][] = $row;
}
}
/**
* Get HTML snippet for privileges table head
*
* @return string $html_output
*/
function PMA_getHtmlForPrivsTableHead()
{
return '
'
. ''
. ' | '
. '' . __('User name') . ' | '
. '' . __('Host name') . ' | '
. '' . __('Type') . ' | '
. '' . __('Privileges') . ' | '
. '' . __('Grant') . ' | '
. '' . __('Action') . ' | '
. '
'
. '';
}
/**
* Get HTML error for View Users form
* For non superusers such as grant/create users
*
* @return string $html_output
*/
function PMA_getHtmlForViewUsersError()
{
return PMA_Message::error(
__('Not enough privilege to view users.')
)->getDisplay();
}
/**
* Get HTML snippet for table body of specific database or table privileges
*
* @param array $privMap privilege map
* @param string $db database
*
* @return string $html_output
*/
function PMA_getHtmlTableBodyForSpecificDbOrTablePrivs($privMap, $db)
{
$html_output = '
';
$index_checkbox = 0;
$odd_row = true;
if (empty($privMap)) {
$html_output .= ''
. '| '
. __('No user found.')
. ' | '
. '
'
. '';
return $html_output;
}
foreach ($privMap as $current_user => $val) {
foreach ($val as $current_host => $current_privileges) {
$nbPrivileges = count($current_privileges);
$html_output .= '
';
$value = htmlspecialchars($current_user . '' . $current_host);
$html_output .= '| 1) {
$html_output .= ' rowspan="' . $nbPrivileges . '"';
}
$html_output .= '>';
$html_output .= ' | ' . "\n";
// user
$html_output .= ' 1) {
$html_output .= ' rowspan="' . $nbPrivileges . '"';
}
$html_output .= '>';
if (empty($current_user)) {
$html_output .= ''
. __('Any') . '';
} else {
$html_output .= htmlspecialchars($current_user);
}
$html_output .= ' | ';
// host
$html_output .= ' 1) {
$html_output .= ' rowspan="' . $nbPrivileges . '"';
}
$html_output .= '>';
$html_output .= htmlspecialchars($current_host);
$html_output .= ' | ';
$html_output .= PMA_getHtmlListOfPrivs(
$db, $current_privileges, $current_user,
$current_host, $odd_row
);
$odd_row = ! $odd_row;
}
}
$html_output .= '';
return $html_output;
}
/**
* Get HTML to display privileges
*
* @param string $db Database name
* @param array $current_privileges List of privileges
* @param string $current_user Current user
* @param string $current_host Current host
* @param boolean $odd_row Current row is odd
*
* @return string HTML to display privileges
*/
function PMA_getHtmlListOfPrivs(
$db, $current_privileges, $current_user,
$current_host, $odd_row
) {
$nbPrivileges = count($current_privileges);
$html_output = null;
for ($i = 0; $i < $nbPrivileges; $i++) {
$current = $current_privileges[$i];
// type
$html_output .= '';
if ($current['Type'] == 'g') {
$html_output .= __('global');
} elseif ($current['Type'] == 'd') {
if ($current['Db'] == PMA_Util::escapeMysqlWildcards($db)) {
$html_output .= __('database-specific');
} else {
$html_output .= __('wildcard') . ': '
. ''
. htmlspecialchars($current['Db'])
. '';
}
} elseif ($current['Type'] == 't') {
$html_output .= __('table-specific');
}
$html_output .= ' | ';
// privileges
$html_output .= '';
if (isset($current['Table_name'])) {
$privList = explode(',', $current['Table_priv']);
$privs = array();
$grantsArr = PMA_getTableGrantsArray();
foreach ($grantsArr as $grant) {
$privs[$grant[0]] = 'N';
foreach ($privList as $priv) {
if ($grant[0] == $priv) {
$privs[$grant[0]] = 'Y';
}
}
}
$html_output .= ''
. join(
',',
PMA_extractPrivInfo($privs, true, true)
)
. '';
} else {
$html_output .= ''
. join(
',',
PMA_extractPrivInfo($current, true, false)
)
. '';
}
$html_output .= ' | ';
// grant
$html_output .= '';
$containsGrant = false;
if (isset($current['Table_name'])) {
$privList = explode(',', $current['Table_priv']);
foreach ($privList as $priv) {
if ($priv == 'Grant') {
$containsGrant = true;
}
}
} else {
$containsGrant = $current['Grant_priv'] == 'Y';
}
$html_output .= ($containsGrant ? __('Yes') : __('No'));
$html_output .= ' | ';
// action
$html_output .= '';
if ($GLOBALS['is_grantuser']) {
$specific_db = (isset($current['Db']) && $current['Db'] != '*')
? $current['Db'] : '';
$specific_table = (isset($current['Table_name'])
&& $current['Table_name'] != '*')
? $current['Table_name'] : '';
$html_output .= PMA_getUserLink(
'edit',
$current_user,
$current_host,
$specific_db,
$specific_table
);
}
$html_output .= ' | ';
$html_output .= '
';
if (($i + 1) < $nbPrivileges) {
$html_output .= '
';
}
}
return $html_output;
}
/**
* Returns edit, revoke or export link for a user.
*
* @param string $linktype The link type (edit | revoke | export)
* @param string $username User name
* @param string $hostname Host name
* @param string $dbname Database name
* @param string $tablename Table name
* @param string $initial Initial value
*
* @return string HTML code with link
*/
function PMA_getUserLink(
$linktype, $username, $hostname, $dbname = '', $tablename = '', $initial = ''
) {
$html = ' $username,
'hostname' => $hostname
);
switch($linktype) {
case 'edit':
$params['dbname'] = $dbname;
$params['tablename'] = $tablename;
break;
case 'revoke':
$params['dbname'] = $dbname;
$params['tablename'] = $tablename;
$params['revokeall'] = 1;
break;
case 'export':
$params['initial'] = $initial;
$params['export'] = 1;
break;
}
$html .= ' href="server_privileges.php'
. PMA_URL_getCommon($params)
. '">';
switch($linktype) {
case 'edit':
$html .= PMA_Util::getIcon('b_usredit.png', __('Edit privileges'));
break;
case 'revoke':
$html .= PMA_Util::getIcon('b_usrdrop.png', __('Revoke'));
break;
case 'export':
$html .= PMA_Util::getIcon('b_tblexport.png', __('Export'));
break;
}
$html .= '';
return $html;
}
/**
* Returns user group edit link
*
* @param string $username User name
*
* @return string HTML code with link
*/
function PMA_getUserGroupEditLink($username)
{
return ''
. PMA_Util::getIcon('b_usrlist.png', __('Edit user group'))
. '';
}
/**
* Returns number of defined user groups
*
* @return integer $user_group_count
*/
function PMA_getUserGroupCount()
{
$cfgRelation = PMA_getRelationsParam();
$user_group_table = PMA_Util::backquote($cfgRelation['db'])
. '.' . PMA_Util::backquote($cfgRelation['usergroups']);
$sql_query = 'SELECT COUNT(*) FROM ' . $user_group_table;
$user_group_count = $GLOBALS['dbi']->fetchValue(
$sql_query, 0, 0, $GLOBALS['controllink']
);
return $user_group_count;
}
/**
* This function return the extra data array for the ajax behavior
*
* @param string $password password
* @param string $sql_query sql query
* @param string $hostname hostname
* @param string $username username
*
* @return array $extra_data
*/
function PMA_getExtraDataForAjaxBehavior(
$password, $sql_query, $hostname, $username
) {
if (isset($GLOBALS['dbname'])) {
//if (preg_match('/\\\\(?:_|%)/i', $dbname)) {
if (preg_match('/(?' . "\n"
. '| '
. ' | ' . "\n"
. ' | ' . "\n"
. '' . htmlspecialchars($hostname) . ' | ' . "\n";
$new_user_string .= '';
if (! empty($password) || isset($_POST['pma_pw'])) {
$new_user_string .= __('Yes');
} else {
$new_user_string .= ''
. __('No')
. '';
};
$new_user_string .= ' | ' . "\n";
$new_user_string .= ''
. '' . join(', ', PMA_extractPrivInfo(null, true)) . ''
. ' | '; //Fill in privileges here
// if $cfg['Servers'][$i]['users'] and $cfg['Servers'][$i]['usergroups'] are
// enabled
$cfgRelation = PMA_getRelationsParam();
if (isset($cfgRelation['users']) && isset($cfgRelation['usergroups'])) {
$new_user_string .= ' | ';
}
$new_user_string .= '';
if ((isset($_POST['Grant_priv']) && $_POST['Grant_priv'] == 'Y')) {
$new_user_string .= __('Yes');
} else {
$new_user_string .= __('No');
}
$new_user_string .=' | ';
if ($GLOBALS['is_grantuser']) {
$new_user_string .= ''
. PMA_getUserLink('edit', $username, $hostname)
. ' | ' . "\n";
}
if ($cfgRelation['menuswork'] && $user_group_count > 0) {
$new_user_string .= ''
. PMA_getUserGroupEditLink($username)
. ' | ' . "\n";
}
$new_user_string .= ''
. PMA_getUserLink(
'export',
$username,
$hostname,
'',
'',
isset($_GET['initial']) ? $_GET['initial'] : ''
)
. ' | ' . "\n";
$new_user_string .= '
';
$extra_data['new_user_string'] = $new_user_string;
/**
* Generate the string for this alphabet's initial, to update the user
* pagination
*/
$new_user_initial = /*overload*/mb_strtoupper(
/*overload*/mb_substr($username, 0, 1)
);
$newUserInitialString = '
'
. $new_user_initial . '';
$extra_data['new_user_initial'] = $new_user_initial;
$extra_data['new_user_initial_string'] = $newUserInitialString;
}
if (isset($_POST['update_privs'])) {
$extra_data['db_specific_privs'] = false;
$extra_data['db_wildcard_privs'] = false;
if (isset($dbname_is_wildcard)) {
$extra_data['db_specific_privs'] = ! $dbname_is_wildcard;
$extra_data['db_wildcard_privs'] = $dbname_is_wildcard;
}
$new_privileges = join(', ', PMA_extractPrivInfo(null, true));
$extra_data['new_privileges'] = $new_privileges;
}
if (isset($_REQUEST['validate_username'])) {
$sql_query = "SELECT * FROM `mysql`.`user` WHERE `User` = '"
. $_REQUEST['username'] . "';";
$res = $GLOBALS['dbi']->query($sql_query);
$row = $GLOBALS['dbi']->fetchRow($res);
if (empty($row)) {
$extra_data['user_exists'] = false;
} else {
$extra_data['user_exists'] = true;
}
}
return $extra_data;
}
/**
* Get the HTML snippet for change user login information
*
* @param string $username username
* @param string $hostname host name
*
* @return string HTML snippet
*/
function PMA_getChangeLoginInformationHtmlForm($username, $hostname)
{
$choices = array(
'4' => __('… keep the old one.'),
'1' => __('… delete the old one from the user tables.'),
'2' => __(
'… revoke all active privileges from '
. 'the old one and delete it afterwards.'
),
'3' => __(
'… delete the old one from the user tables '
. 'and reload the privileges afterwards.'
)
);
$html_output = '' . "\n";
return $html_output;
}
/**
* Provide a line with links to the relevant database and table
*
* @param string $url_dbname url database name that urlencode() string
* @param string $dbname database name
* @param string $tablename table name
*
* @return string HTML snippet
*/
function PMA_getLinkToDbAndTable($url_dbname, $dbname, $tablename)
{
$html_output = '[ ' . __('Database')
. '
'
. htmlspecialchars($dbname) . ': '
. PMA_Util::getTitleForTarget(
$GLOBALS['cfg']['DefaultTabDatabase']
)
. " ]\n";
if (/*overload*/mb_strlen($tablename)) {
$html_output .= ' [ ' . __('Table') . '
' . htmlspecialchars($tablename) . ': '
. PMA_Util::getTitleForTarget(
$GLOBALS['cfg']['DefaultTabTable']
)
. " ]\n";
}
return $html_output;
}
/**
* no db name given, so we want all privs for the given user
* db name was given, so we want all user specific rights for this db
* So this function returns user rights as an array
*
* @param array $tables tables
* @param string $user_host_condition a where clause that contained user's host
* condition
* @param string $dbname database name
*
* @return array $db_rights database rights
*/
function PMA_getUserSpecificRights($tables, $user_host_condition, $dbname)
{
if (!/*overload*/mb_strlen($dbname)) {
$tables_to_search_for_users = array(
'tables_priv', 'columns_priv',
);
$dbOrTableName = 'Db';
} else {
$user_host_condition .=
' AND `Db`'
. ' LIKE \''
. PMA_Util::sqlAddSlashes($dbname, true) . "'";
$tables_to_search_for_users = array('columns_priv',);
$dbOrTableName = 'Table_name';
}
$db_rights_sqls = array();
foreach ($tables_to_search_for_users as $table_search_in) {
if (in_array($table_search_in, $tables)) {
$db_rights_sqls[] = '
SELECT DISTINCT `' . $dbOrTableName . '`
FROM `mysql`.' . PMA_Util::backquote($table_search_in)
. $user_host_condition;
}
}
$user_defaults = array(
$dbOrTableName => '',
'Grant_priv' => 'N',
'privs' => array('USAGE'),
'Column_priv' => true,
);
// for the rights
$db_rights = array();
$db_rights_sql = '(' . implode(') UNION (', $db_rights_sqls) . ')'
. ' ORDER BY `' . $dbOrTableName . '` ASC';
$db_rights_result = $GLOBALS['dbi']->query($db_rights_sql);
while ($db_rights_row = $GLOBALS['dbi']->fetchAssoc($db_rights_result)) {
$db_rights_row = array_merge($user_defaults, $db_rights_row);
if (!/*overload*/mb_strlen($dbname)) {
// only Db names in the table `mysql`.`db` uses wildcards
// as we are in the db specific rights display we want
// all db names escaped, also from other sources
$db_rights_row['Db'] = PMA_Util::escapeMysqlWildcards(
$db_rights_row['Db']
);
}
$db_rights[$db_rights_row[$dbOrTableName]] = $db_rights_row;
}
$GLOBALS['dbi']->freeResult($db_rights_result);
if (!/*overload*/mb_strlen($dbname)) {
$sql_query = 'SELECT * FROM `mysql`.`db`'
. $user_host_condition . ' ORDER BY `Db` ASC';
} else {
$sql_query = 'SELECT `Table_name`,'
. ' `Table_priv`,'
. ' IF(`Column_priv` = _latin1 \'\', 0, 1)'
. ' AS \'Column_priv\''
. ' FROM `mysql`.`tables_priv`'
. $user_host_condition
. ' ORDER BY `Table_name` ASC;';
}
$result = $GLOBALS['dbi']->query($sql_query);
while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
if (isset($db_rights[$row[$dbOrTableName]])) {
$db_rights[$row[$dbOrTableName]]
= array_merge($db_rights[$row[$dbOrTableName]], $row);
} else {
$db_rights[$row[$dbOrTableName]] = $row;
}
if (!/*overload*/mb_strlen($dbname)) {
// there are db specific rights for this user
// so we can drop this db rights
$db_rights[$row['Db']]['can_delete'] = true;
}
}
$GLOBALS['dbi']->freeResult($result);
return $db_rights;
}
/**
* Display user rights in table rows(Table specific or database specific privs)
*
* @param array $db_rights user's database rights array
* @param string $dbname database name
* @param string $hostname host name
* @param string $username username
*
* @return array $found_rows, $html_output
*/
function PMA_getHtmlForUserRights($db_rights, $dbname,
$hostname, $username
) {
$html_output = '';
$found_rows = array();
// display rows
if (count($db_rights) < 1) {
$html_output .= '
' . "\n"
. '| ' . __('None') . ' | ' . "\n"
. '
' . "\n";
return array($found_rows, $html_output);
}
$odd_row = true;
//while ($row = $GLOBALS['dbi']->fetchAssoc($res)) {
foreach ($db_rights as $row) {
$dbNameLength = /*overload*/mb_strlen($dbname);
$found_rows[] = (!$dbNameLength)
? $row['Db']
: $row['Table_name'];
$html_output .= '
' . "\n"
. '| '
. htmlspecialchars(
(!$dbNameLength)
? $row['Db']
: $row['Table_name']
)
. ' | ' . "\n"
. '' . "\n"
. ' '
. join(
',' . "\n" . ' ',
PMA_extractPrivInfo($row, true)
) . "\n"
. ' | ' . "\n"
. ''
. ((((!$dbNameLength) && $row['Grant_priv'] == 'Y')
|| ($dbNameLength
&& in_array('Grant', explode(',', $row['Table_priv']))))
? __('Yes')
: __('No'))
. ' | ' . "\n"
. '';
if (!empty($row['Table_privs']) || !empty($row['Column_priv'])) {
$html_output .= __('Yes');
} else {
$html_output .= __('No');
}
$html_output .= ' | ';
$html_output .= '';
if ($GLOBALS['is_grantuser']) {
$html_output .= PMA_getUserLink(
'edit',
$username,
$hostname,
(!$dbNameLength) ? $row['Db'] : $dbname,
(!$dbNameLength) ? '' : $row['Table_name']
);
}
$html_output .= ' | ';
$html_output .= '';
if (! empty($row['can_delete'])
|| isset($row['Table_name'])
&& /*overload*/mb_strlen($row['Table_name'])
) {
$html_output .= PMA_getUserLink(
'revoke',
$username,
$hostname,
(!$dbNameLength) ? $row['Db'] : $dbname,
(!$dbNameLength) ? '' : $row['Table_name']
);
}
$html_output .= ' | ' . "\n"
. '
' . "\n";
$odd_row = ! $odd_row;
} // end while
return array($found_rows, $html_output);
}
/**
* Get a HTML table for display user's tabel specific or database specific rights
*
* @param string $username username
* @param string $hostname host name
* @param string $dbname database name
*
* @return array $html_output, $found_rows
*/
function PMA_getHtmlForAllTableSpecificRights(
$username, $hostname, $dbname
) {
// table header
$html_output = PMA_URL_getHiddenInputs('', '')
. '
' . "\n"
. '
' . "\n"
. '