Another Acknowledgements page update (fix for previous commit)
[openemr.git] / phpmyadmin / server_privileges.php
blob23d174b986d10d12f398cbb771cb92793e8e21d2
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 */
8 /**
11 require_once './libraries/common.inc.php';
13 /**
14 * Does the common work
16 $js_to_run = 'server_privileges.js';
17 require './libraries/server_common.inc.php';
20 /**
21 * Checks if a dropdown box has been used for selecting a database / table
23 if (isset($pred_dbname) && strlen($pred_dbname)) {
24 $dbname = $pred_dbname;
25 unset($pred_dbname);
27 if (isset($pred_tablename) && strlen($pred_tablename)) {
28 $tablename = $pred_tablename;
29 unset($pred_tablename);
32 // check if given $dbanem is a wildcard or not
33 if (isset($dbname)) {
34 //if (preg_match('/\\\\(?:_|%)/i', $dbname)) {
35 if (preg_match('/(?<!\\\\)(?:_|%)/i', $dbname)) {
36 $dbname_is_wildcard = true;
37 } else {
38 $dbname_is_wildcard = false;
42 /**
43 * Checks if the user is allowed to do what he tries to...
45 if (!$is_superuser) {
46 require './libraries/server_links.inc.php';
47 echo '<h2>' . "\n"
48 . ($GLOBALS['cfg']['MainPageIconic'] ? '<img class="icon" src="'. $GLOBALS['pmaThemeImage'] . 'b_usrlist.png" alt="" />' : '')
49 . $GLOBALS['strPrivileges'] . "\n"
50 . '</h2>' . "\n"
51 . $GLOBALS['strNoPrivileges'] . "\n";
52 require_once './libraries/footer.inc.php';
55 /**
56 * Generates a condition on the user name
58 * @param string the user's initial
59 * @return string the generated condition
61 function PMA_RangeOfUsers($initial = '') {
62 // strtolower() is used because the User field
63 // might be BINARY, so LIKE would be case sensitive
64 if (!empty($initial)) {
65 $ret = " WHERE " . PMA_convert_using('User')
66 . " LIKE " . PMA_convert_using($initial . '%', 'quoted')
67 . " OR ". PMA_convert_using('User')
68 . " LIKE " . PMA_convert_using(strtolower($initial) . '%', 'quoted');
69 } else {
70 $ret = '';
72 return $ret;
73 } // end function
75 /**
76 * Extracts the privilege information of a priv table row
78 * @param array $row the row
79 * @param boolean $enableHTML add <dfn> tag with tooltips
81 * @global ressource $user_link the database connection
83 * @return array
85 function PMA_extractPrivInfo($row = '', $enableHTML = FALSE)
87 $grants = array(
88 array('Select_priv', 'SELECT', $GLOBALS['strPrivDescSelect']),
89 array('Insert_priv', 'INSERT', $GLOBALS['strPrivDescInsert']),
90 array('Update_priv', 'UPDATE', $GLOBALS['strPrivDescUpdate']),
91 array('Delete_priv', 'DELETE', $GLOBALS['strPrivDescDelete']),
92 array('Create_priv', 'CREATE', $GLOBALS['strPrivDescCreateDb']),
93 array('Drop_priv', 'DROP', $GLOBALS['strPrivDescDropDb']),
94 array('Reload_priv', 'RELOAD', $GLOBALS['strPrivDescReload']),
95 array('Shutdown_priv', 'SHUTDOWN', $GLOBALS['strPrivDescShutdown']),
96 array('Process_priv', 'PROCESS', $GLOBALS['strPrivDescProcess' . ((!empty($row) && isset($row['Super_priv'])) || (empty($row) && isset($GLOBALS['Super_priv'])) ? '4' : '3')]),
97 array('File_priv', 'FILE', $GLOBALS['strPrivDescFile']),
98 array('References_priv', 'REFERENCES', $GLOBALS['strPrivDescReferences']),
99 array('Index_priv', 'INDEX', $GLOBALS['strPrivDescIndex']),
100 array('Alter_priv', 'ALTER', $GLOBALS['strPrivDescAlter']),
101 array('Show_db_priv', 'SHOW DATABASES', $GLOBALS['strPrivDescShowDb']),
102 array('Super_priv', 'SUPER', $GLOBALS['strPrivDescSuper']),
103 array('Create_tmp_table_priv', 'CREATE TEMPORARY TABLES', $GLOBALS['strPrivDescCreateTmpTable']),
104 array('Lock_tables_priv', 'LOCK TABLES', $GLOBALS['strPrivDescLockTables']),
105 array('Repl_slave_priv', 'REPLICATION SLAVE', $GLOBALS['strPrivDescReplSlave']),
106 array('Repl_client_priv', 'REPLICATION CLIENT', $GLOBALS['strPrivDescReplClient']),
107 array('Create_view_priv', 'CREATE VIEW', $GLOBALS['strPrivDescCreateView']),
108 // for table privs:
109 array('Create View_priv', 'CREATE VIEW', $GLOBALS['strPrivDescCreateView']),
110 array('Show_view_priv', 'SHOW VIEW', $GLOBALS['strPrivDescShowView']),
111 // for table privs:
112 array('Show view_priv', 'SHOW VIEW', $GLOBALS['strPrivDescShowView']),
113 array('Create_routine_priv', 'CREATE ROUTINE', $GLOBALS['strPrivDescCreateRoutine']),
114 array('Alter_routine_priv', 'ALTER ROUTINE', $GLOBALS['strPrivDescAlterRoutine']),
115 array('Create_user_priv', 'CREATE USER', $GLOBALS['strPrivDescCreateUser'])
117 if (PMA_MYSQL_INT_VERSION >= 40002 && PMA_MYSQL_INT_VERSION <50003) {
118 $grants[] = array('Execute_priv', 'EXECUTE', $GLOBALS['strPrivDescExecute']);
119 } else {
120 $grants[] = array('Execute_priv', 'EXECUTE', $GLOBALS['strPrivDescExecute5']);
123 if (!empty($row) && isset($row['Table_priv'])) {
124 $res = PMA_DBI_query(
125 'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';',
126 $GLOBALS['userlink']);
127 $row1 = PMA_DBI_fetch_assoc($res);
128 PMA_DBI_free_result($res);
129 $av_grants = explode ('\',\'', substr($row1['Type'], 5, strlen($row1['Type']) - 7));
130 unset($row1);
131 $users_grants = explode(',', $row['Table_priv']);
132 foreach ($av_grants as $current_grant) {
133 $row[$current_grant . '_priv'] = in_array($current_grant, $users_grants) ? 'Y' : 'N';
135 unset($current_grant);
136 unset($av_grants);
137 unset($users_grants);
139 $privs = array();
140 $allPrivileges = TRUE;
141 foreach ($grants as $current_grant) {
142 if ((!empty($row) && isset($row[$current_grant[0]])) || (empty($row) && isset($GLOBALS[$current_grant[0]]))) {
143 if ((!empty($row) && $row[$current_grant[0]] == 'Y') || (empty($row) && ($GLOBALS[$current_grant[0]] == 'Y' || (is_array($GLOBALS[$current_grant[0]]) && count($GLOBALS[$current_grant[0]]) == $GLOBALS['column_count'] && empty($GLOBALS[$current_grant[0] . '_none']))))) {
144 if ($enableHTML) {
145 $privs[] = '<dfn title="' . $current_grant[2] . '">' . $current_grant[1] . '</dfn>';
146 } else {
147 $privs[] = $current_grant[1];
149 } elseif (!empty($GLOBALS[$current_grant[0]]) && is_array($GLOBALS[$current_grant[0]]) && empty($GLOBALS[$current_grant[0] . '_none'])) {
150 if ($enableHTML) {
151 $priv_string = '<dfn title="' . $current_grant[2] . '">' . $current_grant[1] . '</dfn>';
152 } else {
153 $priv_string = $current_grant[1];
155 $privs[] = $priv_string . ' (`' . join('`, `', $GLOBALS[$current_grant[0]]) . '`)';
156 } else {
157 $allPrivileges = FALSE;
161 if (empty($privs)) {
162 if ($enableHTML) {
163 $privs[] = '<dfn title="' . $GLOBALS['strPrivDescUsage'] . '">USAGE</dfn>';
164 } else {
165 $privs[] = 'USAGE';
167 } elseif ($allPrivileges && (!isset($GLOBALS['grant_count']) || count($privs) == $GLOBALS['grant_count'])) {
168 if ($enableHTML) {
169 $privs = array('<dfn title="' . $GLOBALS['strPrivDescAllPrivileges'] . '">ALL PRIVILEGES</dfn>');
170 } else {
171 $privs = array('ALL PRIVILEGES');
174 return $privs;
175 } // end of the 'PMA_extractPrivInfo()' function
179 * Displays on which column(s) a table-specific privilege is granted
181 function PMA_display_column_privs($spaces, $columns, $row, $name_for_select, $priv_for_header, $name, $name_for_dfn, $name_for_current) {
183 echo $spaces . ' <div class="item" id="div_item_' . $name . '">' . "\n"
184 . $spaces . ' <label for="select_' . $name . '_priv">' . "\n"
185 . $spaces . ' <tt><dfn title="' . $name_for_dfn . '">' . $priv_for_header . '</dfn></tt>' . "\n"
186 . $spaces . ' </label><br />' . "\n"
187 . $spaces . ' <select id="select_' . $name . '_priv" name="' . $name_for_select . '[]" multiple="multiple" size="8">' . "\n";
189 foreach ($columns as $current_column => $current_column_privileges) {
190 echo $spaces . ' <option value="' . htmlspecialchars($current_column) . '"';
191 if ($row[$name_for_select] == 'Y' || $current_column_privileges[$name_for_current]) {
192 echo ' selected="selected"';
194 echo '>' . htmlspecialchars($current_column) . '</option>' . "\n";
197 echo $spaces . ' </select>' . "\n"
198 . $spaces . ' <i>' . $GLOBALS['strOr'] . '</i>' . "\n"
199 . $spaces . ' <label for="checkbox_' . $name_for_select . '_none"><input type="checkbox"' . (empty($GLOBALS['checkall']) ? '' : ' checked="checked"') . ' name="' . $name_for_select . '_none" id="checkbox_' . $name_for_select . '_none" title="' . $GLOBALS['strNone'] . '" />'
200 . $GLOBALS['strNone'] . '</label>' . "\n"
201 . $spaces . ' </div>' . "\n";
202 } // end function
205 * Displays the privileges form table
207 * @param string $db the database
208 * @param string $table the table
209 * @param boolean $submit wheather to display the submit button or not
210 * @param int $indent the indenting level of the code
212 * @global array $cfg the phpMyAdmin configuration
213 * @global ressource $user_link the database connection
215 * @return void
217 function PMA_displayPrivTable($db = '*', $table = '*', $submit = TRUE, $indent = 0)
219 if ($db == '*') {
220 $table = '*';
222 $spaces = str_repeat(' ', $indent);
224 if (isset($GLOBALS['username'])) {
225 $username = $GLOBALS['username'];
226 $hostname = $GLOBALS['hostname'];
227 if ($db == '*') {
228 $sql_query =
229 'SELECT * FROM `mysql`.`user`'
230 .' WHERE ' . PMA_convert_using('User')
231 .' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted')
232 .' AND ' . PMA_convert_using('Host')
233 .' = ' . PMA_convert_using($hostname, 'quoted') . ';';
234 } elseif ($table == '*') {
235 $sql_query =
236 'SELECT * FROM `mysql`.`db`'
237 .' WHERE ' . PMA_convert_using('`User`')
238 .' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted')
239 .' AND ' . PMA_convert_using('`Host`')
240 .' = ' . PMA_convert_using($hostname, 'quoted')
241 .' AND ' . PMA_convert_using(PMA_unescape_mysql_wildcards($db), 'quoted')
242 .' LIKE ' . PMA_convert_using('`Db`') . ';';
243 } else {
244 $sql_query =
245 'SELECT `Table_priv`'
246 .' FROM `mysql`.`tables_priv`'
247 .' WHERE ' . PMA_convert_using('`User`')
248 .' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted')
249 .' AND ' .PMA_convert_using('`Host`')
250 .' = ' . PMA_convert_using($hostname, 'quoted')
251 .' AND ' .PMA_convert_using('`Db`')
252 .' = ' . PMA_convert_using(PMA_unescape_mysql_wildcards($db), 'quoted')
253 .' AND ' . PMA_convert_using('`Table_name`')
254 .' = ' . PMA_convert_using($table, 'quoted') . ';';
256 $res = PMA_DBI_query($sql_query);
257 $row = PMA_DBI_fetch_assoc($res);
258 PMA_DBI_free_result($res);
260 if (empty($row)) {
261 if ($table == '*') {
262 if ($db == '*') {
263 $sql_query = 'SHOW COLUMNS FROM `mysql`.`user`;';
264 } elseif ($table == '*') {
265 $sql_query = 'SHOW COLUMNS FROM `mysql`.`db`;';
267 $res = PMA_DBI_query($sql_query);
268 while ($row1 = PMA_DBI_fetch_row($res)) {
269 if (substr($row1[0], 0, 4) == 'max_') {
270 $row[$row1[0]] = 0;
271 } else {
272 $row[$row1[0]] = 'N';
275 PMA_DBI_free_result($res);
276 } else {
277 $row = array('Table_priv' => '');
280 if (isset($row['Table_priv'])) {
281 $res = PMA_DBI_query(
282 'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';',
283 $GLOBALS['userlink']);
284 // note: in MySQL 5.0.3 we get "Create View', 'Show view';
285 // the View for Create is spelled with uppercase V
286 // the view for Show is spelled with lowercase v
287 // and there is a space between the words
289 $row1 = PMA_DBI_fetch_assoc($res);
290 PMA_DBI_free_result($res);
291 $av_grants = explode ('\',\'', substr($row1['Type'], strpos($row1['Type'], '(') + 2, strpos($row1['Type'], ')') - strpos($row1['Type'], '(') - 3));
292 unset($res, $row1);
293 $users_grants = explode(',', $row['Table_priv']);
295 foreach ($av_grants as $current_grant) {
296 $row[$current_grant . '_priv'] = in_array($current_grant, $users_grants) ? 'Y' : 'N';
298 unset($row['Table_priv'], $current_grant, $av_grants, $users_grants);
300 // get collumns
301 $res = PMA_DBI_try_query('SHOW COLUMNS FROM `' . PMA_unescape_mysql_wildcards($db) . '`.`' . $table . '`;');
302 $columns = array();
303 if ($res) {
304 while ($row1 = PMA_DBI_fetch_row($res)) {
305 $columns[$row1[0]] = array(
306 'Select' => FALSE,
307 'Insert' => FALSE,
308 'Update' => FALSE,
309 'References' => FALSE
312 PMA_DBI_free_result($res);
314 unset($res, $row1);
316 // t a b l e - s p e c i f i c p r i v i l e g e s
317 if (! empty($columns)) {
318 $res = PMA_DBI_query(
319 'SELECT `Column_name`, `Column_priv`'
320 .' FROM `mysql`.`columns_priv`'
321 .' WHERE ' . PMA_convert_using('`User`')
322 .' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted')
323 .' AND ' . PMA_convert_using('`Host`')
324 .' = ' . PMA_convert_using($hostname, 'quoted')
325 .' AND ' . PMA_convert_using('`Db`')
326 .' = ' . PMA_convert_using(PMA_unescape_mysql_wildcards($db), 'quoted')
327 .' AND ' . PMA_convert_using('`Table_name`')
328 .' = ' . PMA_convert_using($table, 'quoted') . ';');
330 while ($row1 = PMA_DBI_fetch_row($res)) {
331 $row1[1] = explode(',', $row1[1]);
332 foreach ($row1[1] as $current) {
333 $columns[$row1[0]][$current] = TRUE;
336 PMA_DBI_free_result($res);
337 unset($res, $row1, $current);
339 echo $spaces . '<input type="hidden" name="grant_count" value="' . count($row) . '" />' . "\n"
340 . $spaces . '<input type="hidden" name="column_count" value="' . count($columns) . '" />' . "\n"
341 . $spaces . '<fieldset id="fieldset_user_priv">' . "\n"
342 . $spaces . ' <legend>' . $GLOBALS['strTblPrivileges'] . '</legend>' . "\n"
343 . $spaces . ' <p><small><i>' . $GLOBALS['strEnglishPrivileges'] . '</i></small></p>' . "\n";
346 // privs that are attached to a specific column
347 PMA_display_column_privs($spaces, $columns, $row, 'Select_priv', 'SELECT', 'select', $GLOBALS['strPrivDescSelect'], 'Select');
349 PMA_display_column_privs($spaces, $columns, $row, 'Insert_priv', 'INSERT', 'insert', $GLOBALS['strPrivDescInsert'], 'Insert');
351 PMA_display_column_privs($spaces, $columns, $row, 'Update_priv', 'UPDATE', 'update', $GLOBALS['strPrivDescUpdate'], 'Update');
353 PMA_display_column_privs($spaces, $columns, $row, 'References_priv', 'REFERENCES', 'references', $GLOBALS['strPrivDescReferences'], 'References');
355 // privs that are not attached to a specific column
357 echo $spaces . ' <div class="item">' . "\n";
358 foreach ($row as $current_grant => $current_grant_value) {
359 if (in_array(substr($current_grant, 0, (strlen($current_grant) - 5)), array('Select', 'Insert', 'Update', 'References'))) {
360 continue;
362 // make a substitution to match the messages variables;
363 // also we must substitute the grant we get, because we can't generate
364 // a form variable containing blanks (those would get changed to
365 // an underscore when receiving the POST)
366 if ($current_grant == 'Create View_priv') {
367 $tmp_current_grant = 'CreateView_priv';
368 $current_grant = 'Create_view_priv';
369 } elseif ($current_grant == 'Show view_priv') {
370 $tmp_current_grant = 'ShowView_priv';
371 $current_grant = 'Show_view_priv';
372 } else {
373 $tmp_current_grant = $current_grant;
376 echo $spaces . ' <div class="item">' . "\n"
377 . $spaces . ' <input type="checkbox"' . (empty($GLOBALS['checkall']) ? '' : ' checked="checked"') . ' name="' . $current_grant . '" id="checkbox_' . $current_grant . '" value="Y" ' . ($current_grant_value == 'Y' ? 'checked="checked" ' : '') . 'title="';
379 echo (isset($GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))]) ? $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))] : $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5)) . 'Tbl']) . '"/>' . "\n";
381 echo $spaces . ' <label for="checkbox_' . $current_grant . '"><tt><dfn title="' . (isset($GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))]) ? $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))] : $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5)) . 'Tbl']) . '">' . strtoupper(substr($current_grant, 0, strlen($current_grant) - 5)) . '</dfn></tt></label>' . "\n"
382 . $spaces . ' </div>' . "\n";
383 } // end foreach ()
385 echo $spaces . ' </div>' . "\n";
386 // for Safari 2.0.2
387 echo $spaces . ' <div class="clearfloat"></div>' . "\n";
389 } else {
391 // g l o b a l o r d b - s p e c i f i c
393 // d a t a
394 $privTable[0] = array(
395 array('Select', 'SELECT', $GLOBALS['strPrivDescSelect']),
396 array('Insert', 'INSERT', $GLOBALS['strPrivDescInsert']),
397 array('Update', 'UPDATE', $GLOBALS['strPrivDescUpdate']),
398 array('Delete', 'DELETE', $GLOBALS['strPrivDescDelete'])
400 if ($db == '*') {
401 $privTable[0][] = array('File', 'FILE', $GLOBALS['strPrivDescFile']);
404 // s t r u c t u r e
405 $privTable[1] = array(
406 array('Create', 'CREATE', ($table == '*' ? $GLOBALS['strPrivDescCreateDb'] : $GLOBALS['strPrivDescCreateTbl'])),
407 array('Alter', 'ALTER', $GLOBALS['strPrivDescAlter']),
408 array('Index', 'INDEX', $GLOBALS['strPrivDescIndex']),
409 array('Drop', 'DROP', ($table == '*' ? $GLOBALS['strPrivDescDropDb'] : $GLOBALS['strPrivDescDropTbl']))
411 if (isset($row['Create_tmp_table_priv'])) {
412 $privTable[1][] = array('Create_tmp_table', 'CREATE TEMPORARY TABLES', $GLOBALS['strPrivDescCreateTmpTable']);
414 // this one is for a db-specific priv: Create_view_priv
415 if (isset($row['Create_view_priv'])) {
416 $privTable[1][] = array('Create_view', 'CREATE VIEW', $GLOBALS['strPrivDescCreateView']);
418 // this one is for a table-specific priv: Create View_priv
419 if (isset($row['Create View_priv'])) {
420 $privTable[1][] = array('Create View', 'CREATE VIEW', $GLOBALS['strPrivDescCreateView']);
422 if (isset($row['Show_view_priv'])) {
423 $privTable[1][] = array('Show_view', 'SHOW VIEW', $GLOBALS['strPrivDescShowView']);
425 if (isset($row['Create_routine_priv'])) {
426 $privTable[1][] = array('Create_routine', 'CREATE ROUTINE', $GLOBALS['strPrivDescCreateRoutine']);
428 if (isset($row['Alter_routine_priv'])) {
429 $privTable[1][] = array('Alter_routine', 'ALTER ROUTINE', $GLOBALS['strPrivDescAlterRoutine']);
431 if (isset($row['Execute_priv'])) {
432 if (PMA_MYSQL_INT_VERSION >= 40002 && PMA_MYSQL_INT_VERSION <50003) {
433 $privTable[1][] = array('Execute', 'EXECUTE', $GLOBALS['strPrivDescExecute']);
434 } else {
435 $privTable[1][] = array('Execute', 'EXECUTE', $GLOBALS['strPrivDescExecute5']);
439 // a d m i n i s t r a t i o n
440 $privTable[2] = array();
441 if (isset($row['Grant_priv'])) {
442 $privTable[2][] = array('Grant', 'GRANT', $GLOBALS['strPrivDescGrant']);
444 if ($db == '*') {
445 if (isset($row['Super_priv'])) {
446 $privTable[2][] = array('Super', 'SUPER', $GLOBALS['strPrivDescSuper']);
447 $privTable[2][] = array('Process', 'PROCESS', $GLOBALS['strPrivDescProcess4']);
448 } else {
449 $privTable[2][] = array('Process', 'PROCESS', $GLOBALS['strPrivDescProcess3']);
451 $privTable[2][] = array('Reload', 'RELOAD', $GLOBALS['strPrivDescReload']);
452 $privTable[2][] = array('Shutdown', 'SHUTDOWN', $GLOBALS['strPrivDescShutdown']);
453 if (isset($row['Show_db_priv'])) {
454 $privTable[2][] = array('Show_db', 'SHOW DATABASES', $GLOBALS['strPrivDescShowDb']);
457 if (isset($row['Lock_tables_priv'])) {
458 $privTable[2][] = array('Lock_tables', 'LOCK TABLES', $GLOBALS['strPrivDescLockTables']);
460 $privTable[2][] = array('References', 'REFERENCES', $GLOBALS['strPrivDescReferences']);
461 if ($db == '*') {
462 //if (isset($row['Execute_priv'])) {
463 // $privTable[2][] = array('Execute', 'EXECUTE', $GLOBALS['strPrivDescExecute']);
465 if (isset($row['Repl_client_priv'])) {
466 $privTable[2][] = array('Repl_client', 'REPLICATION CLIENT', $GLOBALS['strPrivDescReplClient']);
468 if (isset($row['Repl_slave_priv'])) {
469 $privTable[2][] = array('Repl_slave', 'REPLICATION SLAVE', $GLOBALS['strPrivDescReplSlave']);
471 if (isset($row['Create_user_priv'])) {
472 $privTable[2][] = array('Create_user', 'CREATE USER', $GLOBALS['strPrivDescCreateUser']);
475 echo $spaces . '<input type="hidden" name="grant_count" value="' . (count($privTable[0]) + count($privTable[1]) + count($privTable[2]) - (isset($row['Grant_priv']) ? 1 : 0)) . '" />' . "\n"
476 . $spaces . '<fieldset id="fieldset_user_global_rights">' . "\n"
477 . $spaces . ' <legend>' . "\n"
478 . $spaces . ' ' . ($db == '*' ? $GLOBALS['strGlobalPrivileges'] : ($table == '*' ? $GLOBALS['strDbPrivileges'] : $GLOBALS['strTblPrivileges'])) . "\n"
479 . $spaces . ' (<a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;checkall=1" onclick="setCheckboxes(\'usersForm\', true); return false;">' . $GLOBALS['strCheckAll'] . '</a> /' . "\n"
480 . $spaces . ' <a href="server_privileges.php?' . $GLOBALS['url_query'] . '" onclick="setCheckboxes(\'usersForm\', false); return false;">' . $GLOBALS['strUncheckAll'] . '</a>)' . "\n"
481 . $spaces . ' </legend>' . "\n"
482 . $spaces . ' <p><small><i>' . $GLOBALS['strEnglishPrivileges'] . '</i></small></p>' . "\n"
483 . $spaces . ' <fieldset>' . "\n"
484 . $spaces . ' <legend>' . $GLOBALS['strData'] . '</legend>' . "\n";
485 foreach ($privTable[0] as $priv)
487 echo $spaces . ' <div class="item">' . "\n"
488 . $spaces . ' <input type="checkbox"' . (empty($GLOBALS['checkall']) ? '' : ' checked="checked"') . ' name="' . $priv[0] . '_priv" id="checkbox_' . $priv[0] . '_priv" value="Y" ' . ($row[$priv[0] . '_priv'] == 'Y' ? 'checked="checked" ' : '') . 'title="' . $priv[2] . '"/>' . "\n"
489 . $spaces . ' <label for="checkbox_' . $priv[0] . '_priv"><tt><dfn title="' . $priv[2] . '">' . $priv[1] . '</dfn></tt></label>' . "\n"
490 . $spaces . ' </div>' . "\n";
492 echo $spaces . ' </fieldset>' . "\n"
493 . $spaces . ' <fieldset>' . "\n"
494 . $spaces . ' <legend>' . $GLOBALS['strStructure'] . '</legend>' . "\n";
495 foreach ($privTable[1] as $priv)
497 echo $spaces . ' <div class="item">' . "\n"
498 . $spaces . ' <input type="checkbox"' . (empty($GLOBALS['checkall']) ? '' : ' checked="checked"') . ' name="' . $priv[0] . '_priv" id="checkbox_' . $priv[0] . '_priv" value="Y" ' . ($row[$priv[0] . '_priv'] == 'Y' ? 'checked="checked" ' : '') . 'title="' . $priv[2] . '"/>' . "\n"
499 . $spaces . ' <label for="checkbox_' . $priv[0] . '_priv"><tt><dfn title="' . $priv[2] . '">' . $priv[1] . '</dfn></tt></label>' . "\n"
500 . $spaces . ' </div>' . "\n";
502 echo $spaces . ' </fieldset>' . "\n"
503 . $spaces . ' <fieldset>' . "\n"
504 . $spaces . ' <legend>' . $GLOBALS['strAdministration'] . '</legend>' . "\n";
505 foreach ($privTable[2] as $priv)
507 echo $spaces . ' <div class="item">' . "\n"
508 . $spaces . ' <input type="checkbox"' . (empty($GLOBALS['checkall']) ? '' : ' checked="checked"') . ' name="' . $priv[0] . '_priv" id="checkbox_' . $priv[0] . '_priv" value="Y" ' . ($row[$priv[0] . '_priv'] == 'Y' ? 'checked="checked" ' : '') . 'title="' . $priv[2] . '"/>' . "\n"
509 . $spaces . ' <label for="checkbox_' . $priv[0] . '_priv"><tt><dfn title="' . $priv[2] . '">' . $priv[1] . '</dfn></tt></label>' . "\n"
510 . $spaces . ' </div>' . "\n";
513 echo $spaces . ' </fieldset>' . "\n";
514 // The "Resource limits" box is not displayed for db-specific privs
515 if ($db == '*' && PMA_MYSQL_INT_VERSION >= 40002) {
516 echo $spaces . ' <fieldset>' . "\n"
517 . $spaces . ' <legend>' . $GLOBALS['strResourceLimits'] . '</legend>' . "\n"
518 . $spaces . ' <p><small><i>' . $GLOBALS['strZeroRemovesTheLimit'] . '</i></small></p>' . "\n"
519 . $spaces . ' <div class="item">' . "\n"
520 . $spaces . ' <label for="text_max_questions"><tt><dfn title="' . $GLOBALS['strPrivDescMaxQuestions'] . '">MAX QUERIES PER HOUR</dfn></tt></label>' . "\n"
521 . $spaces . ' <input type="text" name="max_questions" id="text_max_questions" value="' . $row['max_questions'] . '" size="11" maxlength="11" title="' . $GLOBALS['strPrivDescMaxQuestions'] . '" />' . "\n"
522 . $spaces . ' </div>' . "\n"
523 . $spaces . ' <div class="item">' . "\n"
524 . $spaces . ' <label for="text_max_updates"><tt><dfn title="' . $GLOBALS['strPrivDescMaxUpdates'] . '">MAX UPDATES PER HOUR</dfn></tt></label>' . "\n"
525 . $spaces . ' <input type="text" name="max_updates" id="text_max_updates" value="' . $row['max_updates'] . '" size="11" maxlength="11" title="' . $GLOBALS['strPrivDescMaxUpdates'] . '" />' . "\n"
526 . $spaces . ' </div>' . "\n"
527 . $spaces . ' <div class="item">' . "\n"
528 . $spaces . ' <label for="text_max_connections"><tt><dfn title="' . $GLOBALS['strPrivDescMaxConnections'] . '">MAX CONNECTIONS PER HOUR</dfn></tt></label>' . "\n"
529 . $spaces . ' <input type="text" name="max_connections" id="text_max_connections" value="' . $row['max_connections'] . '" size="11" maxlength="11" title="' . $GLOBALS['strPrivDescMaxConnections'] . '" />' . "\n"
530 . $spaces . ' </div>' . "\n";
532 if (PMA_MYSQL_INT_VERSION >= 50003) {
533 echo $spaces . ' <div class="item">' . "\n"
534 . $spaces . ' <label for="text_max_user_connections"><tt><dfn title="' . $GLOBALS['strPrivDescMaxUserConnections'] . '">MAX USER_CONNECTIONS</dfn></tt></label>' . "\n"
535 . $spaces . ' <input type="text" name="max_user_connections" id="text_max_user_connections" value="' . $row['max_user_connections'] . '" size="11" maxlength="11" title="' . $GLOBALS['strPrivDescMaxUserConnections'] . '" />' . "\n"
536 . $spaces . ' </div>' . "\n";
538 echo $spaces . ' </fieldset>' . "\n";
540 // for Safari 2.0.2
541 echo $spaces . ' <div class="clearfloat"></div>' . "\n";
543 echo $spaces . '</fieldset>' . "\n";
544 if ($submit) {
545 echo $spaces . '<fieldset id="fieldset_user_privtable_footer" class="tblFooters">' . "\n"
546 . $spaces . ' <input type="submit" name="update_privs" value="' . $GLOBALS['strGo'] . '" />' . "\n"
547 . $spaces . '</fieldset>' . "\n";
549 } // end of the 'PMA_displayPrivTable()' function
553 * Displays the fields used by the "new user" form as well as the
554 * "change login information / copy user" form.
556 * @param string $mode are we creating a new user or are we just
557 * changing one? (allowed values: 'new', 'change')
558 * @param int $indent the indenting level of the code
560 * @global array $cfg the phpMyAdmin configuration
561 * @global ressource $user_link the database connection
563 * @return void
565 function PMA_displayLoginInformationFields($mode = 'new', $indent = 0) {
566 $spaces = str_repeat(' ', $indent);
568 // Get user/host name lengths
569 $fields_info = PMA_DBI_get_fields('mysql', 'user');
570 $username_length = 16;
571 $hostname_length = 41;
572 foreach ($fields_info as $key => $val) {
573 if ($val['Field'] == 'User') {
574 strtok($val['Type'], '()');
575 $v = strtok('()');
576 if (is_int($v)) {
577 $username_length = $v;
579 } elseif ($val['Field'] == 'Host') {
580 strtok($val['Type'], '()');
581 $v = strtok('()');
582 if (is_int($v)) {
583 $hostname_length = $v;
587 unset($fields_info);
589 if (isset($GLOBALS['username']) && strlen($GLOBALS['username']) === 0) {
590 $GLOBALS['pred_username'] = 'any';
592 echo $spaces . '<fieldset id="fieldset_add_user_login">' . "\n"
593 . $spaces . '<legend>' . $GLOBALS['strLoginInformation'] . '</legend>' . "\n"
594 . $spaces . '<div class="item">' . "\n"
595 . $spaces . '<label for="select_pred_username">' . "\n"
596 . $spaces . ' ' . $GLOBALS['strUserName'] . ':' . "\n"
597 . $spaces . '</label>' . "\n"
598 . $spaces . '<span class="options">' . "\n"
599 . $spaces . ' <select name="pred_username" id="select_pred_username" title="' . $GLOBALS['strUserName'] . '"' . "\n"
600 . $spaces . ' onchange="if (this.value == \'any\') { username.value = \'\'; } else if (this.value == \'userdefined\') { username.focus(); username.select(); }">' . "\n"
601 . $spaces . ' <option value="any"' . ((isset($GLOBALS['pred_username']) && $GLOBALS['pred_username'] == 'any') ? ' selected="selected"' : '') . '>' . $GLOBALS['strAnyUser'] . '</option>' . "\n"
602 . $spaces . ' <option value="userdefined"' . ((!isset($GLOBALS['pred_username']) || $GLOBALS['pred_username'] == 'userdefined') ? ' selected="selected"' : '') . '>' . $GLOBALS['strUseTextField'] . ':</option>' . "\n"
603 . $spaces . ' </select>' . "\n"
604 . $spaces . '</span>' . "\n"
605 . $spaces . '<input type="text" name="username" maxlength="' . $username_length . '" title="' . $GLOBALS['strUserName'] . '"' . (empty($GLOBALS['username']) ? '' : ' value="' . (isset($GLOBALS['new_username']) ? $GLOBALS['new_username'] : $GLOBALS['username']) . '"') . ' onchange="pred_username.value = \'userdefined\';" />' . "\n"
606 . $spaces . '</div>' . "\n"
607 . $spaces . '<div class="item">' . "\n"
608 . $spaces . '<label for="select_pred_hostname">' . "\n"
609 . $spaces . ' ' . $GLOBALS['strHost'] . ':' . "\n"
610 . $spaces . '</label>' . "\n"
611 . $spaces . '<span class="options">' . "\n"
612 . $spaces . ' <select name="pred_hostname" id="select_pred_hostname" title="' . $GLOBALS['strHost'] . '"' . "\n";
613 $res = PMA_DBI_query('SELECT USER();');
614 $row = PMA_DBI_fetch_row($res);
615 PMA_DBI_free_result($res);
616 unset($res);
617 if (!empty($row[0])) {
618 $thishost = str_replace("'", '', substr($row[0], (strrpos($row[0], '@') + 1)));
619 if ($thishost == 'localhost' || $thishost == '127.0.0.1') {
620 unset($thishost);
623 echo $spaces . ' onchange="if (this.value == \'any\') { hostname.value = \'%\'; } else if (this.value == \'localhost\') { hostname.value = \'localhost\'; } '
624 . (empty($thishost) ? '' : 'else if (this.value == \'thishost\') { hostname.value = \'' . addslashes(htmlspecialchars($thishost)) . '\'; } ')
625 . 'else if (this.value == \'hosttable\') { hostname.value = \'\'; } else if (this.value == \'userdefined\') { hostname.focus(); hostname.select(); }">' . "\n";
626 unset($row);
628 // when we start editing a user, $GLOBALS['pred_hostname'] is not defined
629 if (!isset($GLOBALS['pred_hostname']) && isset($GLOBALS['hostname'])) {
630 switch (strtolower($GLOBALS['hostname'])) {
631 case 'localhost':
632 case '127.0.0.1':
633 $GLOBALS['pred_hostname'] = 'localhost';
634 break;
635 case '%':
636 $GLOBALS['pred_hostname'] = 'any';
637 break;
638 default:
639 $GLOBALS['pred_hostname'] = 'userdefined';
640 break;
643 echo $spaces . ' <option value="any"' . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'any') ? ' selected="selected"' : '') . '>' . $GLOBALS['strAnyHost'] . '</option>' . "\n"
644 . $spaces . ' <option value="localhost"' . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'localhost') ? ' selected="selected"' : '') . '>' . $GLOBALS['strLocalhost'] . '</option>' . "\n";
645 if (!empty($thishost)) {
646 echo $spaces . ' <option value="thishost"' . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'thishost') ? ' selected="selected"' : '') . '>' . $GLOBALS['strThisHost'] . '</option>' . "\n";
648 unset($thishost);
649 echo $spaces . ' <option value="hosttable"' . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'hosttable') ? ' selected="selected"' : '') . '>' . $GLOBALS['strUseHostTable'] . '</option>' . "\n"
650 . $spaces . ' <option value="userdefined"' . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'userdefined') ? ' selected="selected"' : '') . '>' . $GLOBALS['strUseTextField'] . ':</option>' . "\n"
651 . $spaces . ' </select>' . "\n"
652 . $spaces . '</span>' . "\n"
653 . $spaces . '<input type="text" name="hostname" maxlength="' . $hostname_length . '" value="' . (isset($GLOBALS['hostname']) ? $GLOBALS['hostname'] : '') . '" title="' . $GLOBALS['strHost'] . '" onchange="pred_hostname.value = \'userdefined\';" />' . "\n"
654 . $spaces . '</div>' . "\n"
655 . $spaces . '<div class="item">' . "\n"
656 . $spaces . '<label for="select_pred_password">' . "\n"
657 . $spaces . ' ' . $GLOBALS['strPassword'] . ':' . "\n"
658 . $spaces . '</label>' . "\n"
659 . $spaces . '<span class="options">' . "\n"
660 . $spaces . ' <select name="pred_password" id="select_pred_password" title="' . $GLOBALS['strPassword'] . '"' . "\n"
661 . $spaces . ' onchange="if (this.value == \'none\') { pma_pw.value = \'\'; pma_pw2.value = \'\'; } else if (this.value == \'userdefined\') { pma_pw.focus(); pma_pw.select(); }">' . "\n"
662 . ($mode == 'change' ? $spaces . ' <option value="keep" selected="selected">' . $GLOBALS['strKeepPass'] . '</option>' . "\n" : '')
663 . $spaces . ' <option value="none"';
664 if (isset($GLOBALS['username']) && $mode != 'change') {
665 echo ' selected="selected"';
667 echo $spaces . '>' . $GLOBALS['strNoPassword'] . '</option>' . "\n"
668 . $spaces . ' <option value="userdefined"' . (isset($GLOBALS['username']) ? '' : ' selected="selected"') . '>' . $GLOBALS['strUseTextField'] . ':</option>' . "\n"
669 . $spaces . ' </select>' . "\n"
670 . $spaces . '</span>' . "\n"
671 . $spaces . '<input type="password" id="text_pma_pw" name="pma_pw" title="' . $GLOBALS['strPassword'] . '" onchange="pred_password.value = \'userdefined\';" />' . "\n"
672 . $spaces . '</div>' . "\n"
673 . $spaces . '<div class="item">' . "\n"
674 . $spaces . '<label for="text_pma_pw2">' . "\n"
675 . $spaces . ' ' . $GLOBALS['strReType'] . ':' . "\n"
676 . $spaces . '</label>' . "\n"
677 . $spaces . '<span class="options">&nbsp;</span>' . "\n"
678 . $spaces . '<input type="password" name="pma_pw2" id="text_pma_pw2" title="' . $GLOBALS['strReType'] . '" onchange="pred_password.value = \'userdefined\';" />' . "\n"
679 . $spaces . '</div>' . "\n"
680 . $spaces . '<div class="item">' . "\n"
681 . $spaces . '<label for="button_generate_password">' . "\n"
682 . $spaces . ' ' . $GLOBALS['strGeneratePassword'] . ':' . "\n"
683 . $spaces . '</label>' . "\n"
684 . $spaces . '<span class="options">' . "\n"
685 . $spaces . ' <input type="button" id="button_generate_password" value="' . $GLOBALS['strGenerate'] . '" onclick="suggestPassword()" />' . "\n"
686 . $spaces . ' <input type="button" id="button_copy_password" value="' . $GLOBALS['strCopy'] . '" onclick="suggestPasswordCopy(this.form)" />' . "\n"
687 . $spaces . '</span>' . "\n"
688 . $spaces . '<input type="text" name="generated_pw" id="generated_pw" />' . "\n"
689 . $spaces . '</div>' . "\n"
690 . $spaces . '</fieldset>' . "\n";
691 } // end of the 'PMA_displayUserAndHostFields()' function
695 * Changes / copies a user, part I
697 if (!empty($change_copy)) {
698 $user_host_condition =
699 ' WHERE ' . PMA_convert_using('User')
700 .' = ' . PMA_convert_using(PMA_sqlAddslashes($old_username), 'quoted')
701 .' AND ' . PMA_convert_using('Host')
702 .' = ' . PMA_convert_using($old_hostname, 'quoted') . ';';
703 $res = PMA_DBI_query('SELECT * FROM `mysql`.`user` ' . $user_host_condition);
704 if (!$res) {
705 $message = $GLOBALS['strNoUsersFound'];
706 unset($change_copy);
707 } else {
708 $row = PMA_DBI_fetch_assoc($res);
709 extract($row, EXTR_OVERWRITE);
710 // Recent MySQL versions have the field "Password" in mysql.user,
711 // so the previous extract creates $Password but this script
712 // uses $password
713 if (!isset($password) && isset($Password)) {
714 $password=$Password;
716 PMA_DBI_free_result($res);
717 $queries = array();
723 * Adds a user
724 * (Changes / copies a user, part II)
726 if (!empty($adduser_submit) || !empty($change_copy)) {
727 $sql_query = '';
728 if ($pred_username == 'any') {
729 $username = '';
731 switch ($pred_hostname) {
732 case 'any':
733 $hostname = '%';
734 break;
735 case 'localhost':
736 $hostname = 'localhost';
737 break;
738 case 'hosttable':
739 $hostname = '';
740 break;
741 case 'thishost':
742 $res = PMA_DBI_query('SELECT USER();');
743 $row = PMA_DBI_fetch_row($res);
744 PMA_DBI_free_result($res);
745 unset($res);
746 $hostname = substr($row[0], (strrpos($row[0], '@') + 1));
747 unset($row);
748 break;
750 $res = PMA_DBI_query(
751 'SELECT \'foo\' FROM `mysql`.`user`'
752 .' WHERE ' . PMA_convert_using('User', 'unquoted', true)
753 .' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted', true)
754 .' AND ' . PMA_convert_using('Host', 'unquoted', true)
755 .' = ' . PMA_convert_using($hostname, 'quoted', true) . ';',
756 null, PMA_DBI_QUERY_STORE);
758 if (PMA_DBI_num_rows($res) == 1) {
759 PMA_DBI_free_result($res);
760 $message = sprintf($GLOBALS['strUserAlreadyExists'], '[i]\'' . $username . '\'@\'' . $hostname . '\'[/i]');
761 $adduser = 1;
762 } else {
763 PMA_DBI_free_result($res);
765 if (50002 <= PMA_MYSQL_INT_VERSION) {
766 // MySQL 5 requires CREATE USER before any GRANT on this user can done
767 $create_user_real = 'CREATE USER \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\'';
770 $real_sql_query =
771 'GRANT ' . join(', ', PMA_extractPrivInfo()) . ' ON *.* TO \''
772 . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\'';
773 if ($pred_password != 'none' && $pred_password != 'keep') {
774 $pma_pw_hidden = str_repeat('*', strlen($pma_pw));
775 $sql_query = $real_sql_query . ' IDENTIFIED BY \'' . $pma_pw_hidden . '\'';
776 $real_sql_query .= ' IDENTIFIED BY \'' . PMA_sqlAddslashes($pma_pw) . '\'';
777 if (isset($create_user_real)) {
778 $create_user_show = $create_user_real . ' IDENTIFIED BY \'' . $pma_pw_hidden . '\'';
779 $create_user_real .= ' IDENTIFIED BY \'' . PMA_sqlAddslashes($pma_pw) . '\'';
781 } else {
782 if ($pred_password == 'keep' && !empty($password)) {
783 $real_sql_query .= ' IDENTIFIED BY PASSWORD \'' . $password . '\'';
784 if (isset($create_user_real)) {
785 $create_user_real .= ' IDENTIFIED BY PASSWORD \'' . $password . '\'';
788 $sql_query = $real_sql_query;
789 if (isset($create_user_real)) {
790 $create_user_show = $create_user_real;
794 * @todo similar code appears twice in this script
796 if ((isset($Grant_priv) && $Grant_priv == 'Y') || (PMA_MYSQL_INT_VERSION >= 40002 && (isset($max_questions) || isset($max_connections) || isset($max_updates) || isset($max_user_connections)))) {
797 $real_sql_query .= 'WITH';
798 $sql_query .= 'WITH';
799 if (isset($Grant_priv) && $Grant_priv == 'Y') {
800 $real_sql_query .= ' GRANT OPTION';
801 $sql_query .= ' GRANT OPTION';
803 if (PMA_MYSQL_INT_VERSION >= 40002) {
804 if (isset($max_questions)) {
805 // avoid negative values
806 $max_questions = max(0, (int)$max_questions);
807 $real_sql_query .= ' MAX_QUERIES_PER_HOUR ' . $max_questions;
808 $sql_query .= ' MAX_QUERIES_PER_HOUR ' . $max_questions;
810 if (isset($max_connections)) {
811 $max_connections = max(0, (int)$max_connections);
812 $real_sql_query .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections;
813 $sql_query .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections;
815 if (isset($max_updates)) {
816 $max_updates = max(0, (int)$max_updates);
817 $real_sql_query .= ' MAX_UPDATES_PER_HOUR ' . $max_updates;
818 $sql_query .= ' MAX_UPDATES_PER_HOUR ' . $max_updates;
821 if (PMA_MYSQL_INT_VERSION >= 50003) {
822 if (isset($max_user_connections)) {
823 $max_user_connections = max(0, (int)$max_user_connections);
824 $real_sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections;
825 $sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections;
829 if (isset($create_user_real)) {
830 $create_user_real .= ';';
831 $create_user_show .= ';';
833 $real_sql_query .= ';';
834 $sql_query .= ';';
835 if (empty($change_copy)) {
836 if (isset($create_user_real)) {
837 PMA_DBI_try_query($create_user_real) or PMA_mysqlDie(PMA_DBI_getError(), $create_user_show);
838 $sql_query = $create_user_show . $sql_query;
840 PMA_DBI_try_query($real_sql_query) or PMA_mysqlDie(PMA_DBI_getError(), $sql_query);
841 $message = $GLOBALS['strAddUserMessage'];
843 /* Create database for new user */
844 if (isset($createdb) && $createdb > 0) {
845 if ($createdb == 1) {
846 $q = 'CREATE DATABASE IF NOT EXISTS ' . PMA_backquote(PMA_sqlAddslashes($username)) . ';';
847 $sql_query .= $q;
848 PMA_DBI_try_query($q) or PMA_mysqlDie(PMA_DBI_getError(), $sql_query);
849 $GLOBALS['reload'] = TRUE;
850 PMA_reloadNavigation();
852 $q = 'GRANT ALL PRIVILEGES ON ' . PMA_backquote(PMA_sqlAddslashes($username)) . '.* TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\';';
853 $sql_query .= $q;
854 PMA_DBI_try_query($q) or PMA_mysqlDie(PMA_DBI_getError(), $sql_query);
855 } elseif ($createdb == 2) {
856 $q = 'GRANT ALL PRIVILEGES ON ' . PMA_backquote(PMA_sqlAddslashes($username) . '\_%') . '.* TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\';';
857 $sql_query .= $q;
858 PMA_DBI_try_query($q) or PMA_mysqlDie(PMA_DBI_getError(), $sql_query);
861 } else {
862 if (isset($create_user_real)) {
863 $queries[] = $create_user_real;
865 $queries[] = $real_sql_query;
866 // we put the query containing the hidden password in
867 // $queries_for_display, at the same position occupied
868 // by the real query in $queries
869 $tmp_count = count($queries);
870 if (isset($create_user_real)) {
871 $queries_for_display[$tmp_count - 2] = $create_user_show;
873 $queries_for_display[$tmp_count - 1] = $sql_query;
875 unset($res, $real_sql_query);
881 * Changes / copies a user, part III
883 if (!empty($change_copy)) {
884 $user_host_condition =
885 ' WHERE ' . PMA_convert_using('User')
886 .' = ' . PMA_convert_using(PMA_sqlAddslashes($old_username), 'quoted')
887 .' AND ' . PMA_convert_using('Host')
888 .' = ' . PMA_convert_using($old_hostname, 'quoted') . ';';
889 $res = PMA_DBI_query('SELECT * FROM `mysql`.`db`' . $user_host_condition);
890 while ($row = PMA_DBI_fetch_assoc($res)) {
891 $queries[] =
892 'GRANT ' . join(', ', PMA_extractPrivInfo($row))
893 .' ON `' . $row['Db'] . '`.*'
894 .' TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\''
895 . ($row['Grant_priv'] == 'Y' ? ' WITH GRANT OPTION;' : ';');
897 PMA_DBI_free_result($res);
898 $res = PMA_DBI_query(
899 'SELECT `Db`, `Table_name`, `Table_priv`'
900 .' FROM `mysql`.`tables_priv`' . $user_host_condition,
901 $GLOBALS['userlink'], PMA_DBI_QUERY_STORE);
902 while ($row = PMA_DBI_fetch_assoc($res)) {
904 $res2 = PMA_DBI_QUERY(
905 'SELECT `Column_name`, `Column_priv`'
906 .' FROM `mysql`.`columns_priv`'
907 .' WHERE ' . PMA_convert_using('User')
908 .' = ' . PMA_convert_using(PMA_sqlAddslashes($old_username), 'quoted')
909 .' AND ' . PMA_convert_using('`Host`')
910 .' = ' . PMA_convert_using($old_hostname, 'quoted')
911 .' AND ' . PMA_convert_using('`Db`')
912 .' = ' . PMA_convert_using($row['Db'], 'quoted')
913 .' AND ' . PMA_convert_using('`Table_name`')
914 .' = ' . PMA_convert_using($row['Table_name'], 'quoted')
915 .';',
916 null, PMA_DBI_QUERY_STORE);
918 $tmp_privs1 = PMA_extractPrivInfo($row);
919 $tmp_privs2 = array(
920 'Select' => array(),
921 'Insert' => array(),
922 'Update' => array(),
923 'References' => array()
926 while ($row2 = PMA_DBI_fetch_assoc($res2)) {
927 $tmp_array = explode(',', $row2['Column_priv']);
928 if (in_array('Select', $tmp_array)) {
929 $tmp_privs2['Select'][] = $row2['Column_name'];
931 if (in_array('Insert', $tmp_array)) {
932 $tmp_privs2['Insert'][] = $row2['Column_name'];
934 if (in_array('Update', $tmp_array)) {
935 $tmp_privs2['Update'][] = $row2['Column_name'];
937 if (in_array('References', $tmp_array)) {
938 $tmp_privs2['References'][] = $row2['Column_name'];
940 unset($tmp_array);
942 if (count($tmp_privs2['Select']) > 0 && !in_array('SELECT', $tmp_privs1)) {
943 $tmp_privs1[] = 'SELECT (`' . join('`, `', $tmp_privs2['Select']) . '`)';
945 if (count($tmp_privs2['Insert']) > 0 && !in_array('INSERT', $tmp_privs1)) {
946 $tmp_privs1[] = 'INSERT (`' . join('`, `', $tmp_privs2['Insert']) . '`)';
948 if (count($tmp_privs2['Update']) > 0 && !in_array('UPDATE', $tmp_privs1)) {
949 $tmp_privs1[] = 'UPDATE (`' . join('`, `', $tmp_privs2['Update']) . '`)';
951 if (count($tmp_privs2['References']) > 0 && !in_array('REFERENCES', $tmp_privs1)) {
952 $tmp_privs1[] = 'REFERENCES (`' . join('`, `', $tmp_privs2['References']) . '`)';
954 unset($tmp_privs2);
955 $queries[] =
956 'GRANT ' . join(', ', $tmp_privs1)
957 . ' ON `' . $row['Db'] . '`.`' . $row['Table_name']
958 . '` TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\''
959 . (in_array('Grant', explode(',', $row['Table_priv'])) ? ' WITH GRANT OPTION;' : ';');
965 * Updates privileges
967 if (!empty($update_privs)) {
968 // escaping a wildcard character in a GRANT is only accepted at the global
969 // or database level, not at table level; this is why I remove
970 // the escaping character
971 // Note: in the phpMyAdmin list of Database-specific privileges,
972 // we will have for example
973 // test\_db SELECT (this one is for privileges on a db level)
974 // test_db USAGE (this one is for table-specific privileges)
976 // It looks curious but reflects the way MySQL works
978 if (! isset($dbname) || ! strlen($dbname)) {
979 $db_and_table = '*.*';
980 } else {
981 if (isset($tablename) && strlen($tablename)) {
982 $db_and_table = PMA_backquote(PMA_unescape_mysql_wildcards($dbname)) . '.';
983 $db_and_table .= PMA_backquote($tablename);
984 } else {
985 $db_and_table = PMA_backquote($dbname) . '.';
986 $db_and_table .= '*';
990 $sql_query0 =
991 'REVOKE ALL PRIVILEGES ON ' . $db_and_table
992 . ' FROM \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\';';
993 if (!isset($Grant_priv) || $Grant_priv != 'Y') {
994 $sql_query1 =
995 'REVOKE GRANT OPTION ON ' . $db_and_table
996 . ' FROM \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\';';
998 $sql_query2 =
999 'GRANT ' . join(', ', PMA_extractPrivInfo())
1000 . ' ON ' . $db_and_table
1001 . ' TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\'';
1004 * @todo similar code appears twice in this script
1006 if ((isset($Grant_priv) && $Grant_priv == 'Y')
1007 || ((! isset($dbname) || ! strlen($dbname)) && PMA_MYSQL_INT_VERSION >= 40002
1008 && (isset($max_questions) || isset($max_connections)
1009 || isset($max_updates) || isset($max_user_connections))))
1011 $sql_query2 .= 'WITH';
1012 if (isset($Grant_priv) && $Grant_priv == 'Y') {
1013 $sql_query2 .= ' GRANT OPTION';
1015 if (PMA_MYSQL_INT_VERSION >= 40002) {
1016 if (isset($max_questions)) {
1017 $max_questions = max(0, (int)$max_questions);
1018 $sql_query2 .= ' MAX_QUERIES_PER_HOUR ' . $max_questions;
1020 if (isset($max_connections)) {
1021 $max_connections = max(0, (int)$max_connections);
1022 $sql_query2 .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections;
1024 if (isset($max_updates)) {
1025 $max_updates = max(0, (int)$max_updates);
1026 $sql_query2 .= ' MAX_UPDATES_PER_HOUR ' . $max_updates;
1029 if (PMA_MYSQL_INT_VERSION >= 50003) {
1030 if (isset($max_user_connections)) {
1031 $max_user_connections = max(0, (int)$max_user_connections);
1032 $sql_query2 .= ' MAX_USER_CONNECTIONS ' . $max_user_connections;
1036 $sql_query2 .= ';';
1037 if (!PMA_DBI_try_query($sql_query0)) { // this query may fail, but this does not matter :o)
1038 // a case when it can fail is when the admin does not have all
1039 // privileges: he can't do a REVOKE ALL PRIVILEGES !
1040 // so at least we display the error
1041 echo PMA_DBI_getError();
1042 unset($sql_query0);
1044 if (isset($sql_query1) && !PMA_DBI_try_query($sql_query1)) { // this one may fail, too...
1045 unset($sql_query1);
1047 PMA_DBI_query($sql_query2);
1048 $sql_query = (isset($sql_query0) ? $sql_query0 . ' ' : '')
1049 . (isset($sql_query1) ? $sql_query1 . ' ' : '')
1050 . $sql_query2;
1051 $message = sprintf($GLOBALS['strUpdatePrivMessage'], '\'' . $username . '\'@\'' . $hostname . '\'');
1056 * Revokes Privileges
1058 if (!empty($revokeall)) {
1060 if (! isset($dbname) || ! strlen($dbname)) {
1061 $db_and_table = '*.*';
1062 } else {
1063 if (! isset($tablename) || ! strlen($tablename)) {
1064 $db_and_table = PMA_backquote($dbname) . '.';
1065 $db_and_table .= '*';
1066 } else {
1067 $db_and_table = PMA_backquote(PMA_unescape_mysql_wildcards($dbname)) . '.';
1068 $db_and_table .= PMA_backquote($tablename);
1072 $sql_query0 =
1073 'REVOKE ALL PRIVILEGES ON ' . $db_and_table
1074 . ' FROM \'' . $username . '\'@\'' . $hostname . '\';';
1075 $sql_query1 =
1076 'REVOKE GRANT OPTION ON ' . $db_and_table
1077 . ' FROM \'' . $username . '\'@\'' . $hostname . '\';';
1078 PMA_DBI_query($sql_query0);
1079 if (!PMA_DBI_try_query($sql_query1)) { // this one may fail, too...
1080 unset($sql_query1);
1082 $sql_query = $sql_query0 . (isset($sql_query1) ? ' ' . $sql_query1 : '');
1083 $message = sprintf($GLOBALS['strRevokeMessage'], '\'' . $username . '\'@\'' . $hostname . '\'');
1084 if (! isset($tablename) || ! strlen($tablename)) {
1085 unset($dbname);
1086 } else {
1087 unset($tablename);
1093 * Updates the password
1095 if (!empty($change_pw)) {
1096 // similar logic in user_password.php
1097 $message = '';
1099 if ($nopass == 0 && isset($pma_pw) && isset($pma_pw2)) {
1100 if ($pma_pw != $pma_pw2) {
1101 $message = $strPasswordNotSame;
1103 if (empty($pma_pw) || empty($pma_pw2)) {
1104 $message = $strPasswordEmpty;
1106 } // end if
1108 // here $nopass could be == 1
1109 if (empty($message)) {
1111 $hashing_function = (PMA_MYSQL_INT_VERSION >= 40102 && !empty($pw_hash) && $pw_hash == 'old' ? 'OLD_' : '')
1112 . 'PASSWORD';
1114 // in $sql_query which will be displayed, hide the password
1115 $sql_query = 'SET PASSWORD FOR \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\' = ' . (($pma_pw == '') ? '\'\'' : $hashing_function . '(\'' . preg_replace('@.@s', '*', $pma_pw) . '\')');
1116 $local_query = 'SET PASSWORD FOR \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\' = ' . (($pma_pw == '') ? '\'\'' : $hashing_function . '(\'' . PMA_sqlAddslashes($pma_pw) . '\')');
1117 PMA_DBI_try_query($local_query) or PMA_mysqlDie(PMA_DBI_getError(), $sql_query, FALSE, $err_url);
1118 $message = sprintf($GLOBALS['strPasswordChanged'], '\'' . $username . '\'@\'' . $hostname . '\'');
1124 * Deletes users
1125 * (Changes / copies a user, part IV)
1127 $user_host_separator = chr(27);
1129 if (!empty($delete) || (!empty($change_copy) && $mode < 4)) {
1130 if (!empty($change_copy)) {
1131 $selected_usr = array($old_username . $user_host_separator . $old_hostname);
1132 } else {
1133 $queries = array();
1135 for ($i = 0; isset($selected_usr[$i]); $i++) {
1136 list($this_user, $this_host) = explode($user_host_separator, $selected_usr[$i]);
1137 $queries[] = '# ' . sprintf($GLOBALS['strDeleting'], '\'' . $this_user . '\'@\'' . $this_host . '\'') . ' ...';
1138 if (PMA_MYSQL_INT_VERSION >= 50002) {
1139 $queries[] = 'DROP USER \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1140 } else {
1141 if ($mode == 2) {
1142 // The SHOW GRANTS query may fail if the user has not been loaded
1143 // into memory
1144 $res = PMA_DBI_try_query('SHOW GRANTS FOR \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';');
1145 if ($res) {
1146 $queries[] = 'REVOKE ALL PRIVILEGES ON *.* FROM \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1147 while ($row = PMA_DBI_fetch_row($res)) {
1148 $this_table = substr($row[0], (strpos($row[0], 'ON') + 3), (strpos($row[0], ' TO ') - strpos($row[0], 'ON') - 3));
1149 if ($this_table != '*.*') {
1150 $queries[] = 'REVOKE ALL PRIVILEGES ON ' . $this_table . ' FROM \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1152 if (strpos($row[0], 'WITH GRANT OPTION')) {
1153 $queries[] = 'REVOKE GRANT OPTION ON ' . $this_table . ' FROM \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1156 unset($this_table);
1158 PMA_DBI_free_result($res);
1160 unset($res);
1162 if (PMA_MYSQL_INT_VERSION >= 40101) {
1163 if (PMA_MYSQL_INT_VERSION < 50002) {
1164 $queries[] = 'REVOKE GRANT OPTION ON *.* FROM \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1166 $queries[] = 'DROP USER \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1167 } else {
1168 $queries[] = 'DELETE FROM `mysql`.`user` WHERE ' . PMA_convert_using('User') . ' = ' . PMA_convert_using(PMA_sqlAddslashes($this_user), 'quoted') . ' AND ' . PMA_convert_using('Host') . ' = ' . PMA_convert_using($this_host, 'quoted') . ';';
1170 if ($mode != 2) {
1171 // If we REVOKE the table grants, we should not need to modify the
1172 // `mysql`.`db`, `mysql`.`tables_priv` and `mysql`.`columns_priv` tables manually...
1173 $user_host_condition =
1174 ' WHERE ' . PMA_convert_using('User')
1175 . ' = ' . PMA_convert_using(PMA_sqlAddslashes($this_user), 'quoted')
1176 . ' AND ' . PMA_convert_using('Host')
1177 . ' = ' . PMA_convert_using($this_host, 'quoted') . ';';
1178 $queries[] = 'DELETE FROM `mysql`.`db`' . $user_host_condition;
1179 $queries[] = 'DELETE FROM `mysql`.`tables_priv`' . $user_host_condition;
1180 $queries[] = 'DELETE FROM `mysql`.`columns_priv`' . $user_host_condition;
1183 if (!empty($drop_users_db)) {
1184 $queries[] = 'DROP DATABASE IF EXISTS ' . PMA_backquote($this_user) . ';';
1185 $GLOBALS['reload'] = TRUE;
1186 PMA_reloadNavigation();
1189 if (empty($change_copy)) {
1190 if (empty($queries)) {
1191 $show_error_header = TRUE;
1192 $message = $GLOBALS['strDeleteNoUsersSelected'];
1193 } else {
1194 if ($mode == 3) {
1195 $queries[] = '# ' . $GLOBALS['strReloadingThePrivileges'] . ' ...';
1196 $queries[] = 'FLUSH PRIVILEGES;';
1198 foreach ($queries as $sql_query) {
1199 if ($sql_query{0} != '#') {
1200 PMA_DBI_query($sql_query, $GLOBALS['userlink']);
1203 $sql_query = join("\n", $queries);
1204 $message = $GLOBALS['strUsersDeleted'];
1206 unset($queries);
1212 * Changes / copies a user, part V
1214 if (!empty($change_copy)) {
1215 $tmp_count = 0;
1216 foreach ($queries as $sql_query) {
1217 if ($sql_query{0} != '#') {
1218 PMA_DBI_query($sql_query);
1220 // when there is a query containing a hidden password, take it
1221 // instead of the real query sent
1222 if (isset($queries_for_display[$tmp_count])) {
1223 $queries[$tmp_count] = $queries_for_display[$tmp_count];
1225 $tmp_count++;
1227 $message = $GLOBALS['strSuccess'];
1228 $sql_query = join("\n", $queries);
1233 * Reloads the privilege tables into memory
1235 if (!empty($flush_privileges)) {
1236 $sql_query = 'FLUSH PRIVILEGES;';
1237 PMA_DBI_query($sql_query);
1238 $message = $GLOBALS['strPrivilegesReloaded'];
1243 * Displays the links
1245 if (isset($viewing_mode) && $viewing_mode == 'db') {
1246 $db = $checkprivs;
1247 $url_query .= '&amp;goto=db_operations.php';
1249 // Gets the database structure
1250 $sub_part = '_structure';
1251 require './libraries/db_info.inc.php';
1252 echo "\n";
1253 } else {
1254 require './libraries/server_links.inc.php';
1259 * defines some standard links
1261 $link_edit = '<a href="server_privileges.php?' . $GLOBALS['url_query']
1262 .'&amp;username=%s'
1263 .'&amp;hostname=%s'
1264 .'&amp;dbname=%s'
1265 .'&amp;tablename=%s">';
1266 if ($GLOBALS['cfg']['PropertiesIconic']) {
1267 $link_edit .= '<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_usredit.png" width="16" height="16" alt="' . $GLOBALS['strEditPrivileges'] . '" title="' . $GLOBALS['strEditPrivileges'] . '" />';
1268 } else {
1269 $link_edit .= $GLOBALS['strEditPrivileges'];
1271 $link_edit .= '</a>';
1273 $link_revoke = '<a href="server_privileges.php?' . $GLOBALS['url_query']
1274 .'&amp;username=%s'
1275 .'&amp;hostname=%s'
1276 .'&amp;dbname=%s'
1277 .'&amp;tablename=%s'
1278 .'&amp;revokeall=1">';
1279 if ($GLOBALS['cfg']['PropertiesIconic']) {
1280 $link_revoke .= '<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_usrdrop.png" width="16" height="16" alt="' . $GLOBALS['strRevoke'] . '" title="' . $GLOBALS['strRevoke'] . '" />';
1281 } else {
1282 $link_revoke .= $GLOBALS['strRevoke'];
1284 $link_revoke .= '</a>';
1287 * Displays the page
1289 if (empty($adduser) && (! isset($checkprivs) || ! strlen($checkprivs))) {
1290 if (! isset($username)) {
1291 // No username is given --> display the overview
1292 echo '<h2>' . "\n"
1293 . ($GLOBALS['cfg']['MainPageIconic'] ? '<img class="icon" src="'. $GLOBALS['pmaThemeImage'] . 'b_usrlist.png" alt="" />' : '')
1294 . $GLOBALS['strUserOverview'] . "\n"
1295 . '</h2>' . "\n";
1297 $sql_query =
1298 'SELECT `User`,' .
1299 ' `Host`,' .
1300 ' IF(`Password` = ' . (PMA_MYSQL_INT_VERSION >= 40100 ? '_latin1 ' : '') . '\'\', \'N\', \'Y\') AS \'Password\',' .
1301 ' `Select_priv`,' .
1302 ' `Insert_priv`,' .
1303 ' `Update_priv`,' .
1304 ' `Delete_priv`,' .
1305 ' `Index_priv`,' .
1306 ' `Alter_priv`,' .
1307 ' `Create_priv`,' .
1308 ' `Drop_priv`,' .
1309 ' `Grant_priv`,' .
1310 ' `References_priv`,' .
1311 ' `Reload_priv`,' .
1312 ' `Shutdown_priv`,' .
1313 ' `Process_priv`,' .
1314 ' `File_priv`';
1316 if (PMA_MYSQL_INT_VERSION >= 40002) {
1317 $sql_query .= ', `Show_db_priv`, `Super_priv`, `Create_tmp_table_priv`, `Lock_tables_priv`, `Execute_priv`, `Repl_slave_priv`, `Repl_client_priv`';
1320 if (PMA_MYSQL_INT_VERSION >= 50001) {
1321 $sql_query .= ', `Create_view_priv`, `Show_view_priv`';
1324 if (PMA_MYSQL_INT_VERSION >= 50003) {
1325 $sql_query .= ', `Create_user_priv`, `Create_routine_priv`, `Alter_routine_priv`';
1328 $sql_query .= ' FROM `mysql`.`user`';
1330 $sql_query .= (isset($initial) ? PMA_RangeOfUsers($initial) : '');
1332 $sql_query .= ' ORDER BY `User` ASC, `Host` ASC;';
1333 $res = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_STORE);
1335 if (! $res) {
1336 // the query failed! This may have two reasons:
1337 // - the user does not have enough privileges
1338 // - the privilege tables use a structure of an earlier version.
1339 // so let's try a more simple query
1341 $sql_query = 'SELECT * FROM `mysql`.`user`';
1342 $res = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_STORE);
1344 if (!$res) {
1345 echo '<i>' . $GLOBALS['strNoPrivileges'] . '</i>' . "\n";
1346 PMA_DBI_free_result($res);
1347 unset($res);
1348 } else {
1349 // rabus: This message is hardcoded because I will replace it by
1350 // a automatic repair feature soon.
1351 echo '<div class="warning">' . "\n"
1352 . ' Warning: Your privilege table structure seems to be older than this MySQL version!<br />' . "\n"
1353 . ' Please run the script <tt>mysql_fix_privilege_tables</tt> that should be included in your MySQL server distribution to solve this problem!' . "\n"
1354 . '</div><br />' . "\n";
1356 } else {
1358 // we also want users not in table `user` but in other table
1359 $tables = PMA_DBI_fetch_result('SHOW TABLES FROM `mysql`;');
1361 $tables_to_search_for_users = array(
1362 'user', 'db', 'tables_priv', 'columns_priv', 'procs_priv',
1365 $db_rights_sqls = array();
1366 foreach ($tables_to_search_for_users as $table_search_in) {
1367 if (in_array($table_search_in, $tables)) {
1368 $db_rights_sqls[] = 'SELECT DISTINCT `User`, `Host` FROM `mysql`.`' . $table_search_in . '` ' . (isset($initial) ? PMA_RangeOfUsers($initial) : '');
1372 $user_defaults = array(
1373 'User' => '',
1374 'Host' => '%',
1375 'Password' => '?',
1376 'Grant_priv' => 'N',
1377 'privs' => array('USAGE'),
1380 // for all initials, even non A-Z
1381 $array_initials = array();
1382 // for the rights
1383 $db_rights = array();
1385 // do not use UNION DISTINCT, as it's not allowed before
1386 // MySQL 4.0.17, and because "it does nothing" (cf manual)
1387 if (PMA_MYSQL_INT_VERSION >= 40000) {
1388 $db_rights_sql = '(' . implode(') UNION (', $db_rights_sqls) . ')'
1389 .' ORDER BY `User` ASC, `Host` ASC';
1391 $db_rights_result = PMA_DBI_query($db_rights_sql);
1393 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1394 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1395 $db_rights[$db_rights_row['User']][$db_rights_row['Host']] =
1396 $db_rights_row;
1398 } else {
1399 foreach ($db_rights_sqls as $db_rights_sql) {
1400 $db_rights_result = PMA_DBI_query($db_rights_sql);
1402 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1403 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1404 $db_rights[$db_rights_row['User']][$db_rights_row['Host']] =
1405 $db_rights_row;
1409 PMA_DBI_free_result($db_rights_result);
1410 unset($db_rights_sql, $db_rights_sqls, $db_rights_result, $db_rights_row);
1411 ksort($db_rights);
1414 * Displays the initials
1417 // initialize to FALSE the letters A-Z
1418 for ($letter_counter = 1; $letter_counter < 27; $letter_counter++) {
1419 if (! isset($array_initials[chr($letter_counter + 64)])) {
1420 $array_initials[chr($letter_counter + 64)] = FALSE;
1424 $initials = PMA_DBI_try_query('SELECT DISTINCT UPPER(LEFT(' . PMA_convert_using('User') . ',1)) FROM `user` ORDER BY `User` ASC', null, PMA_DBI_QUERY_STORE);
1425 while (list($tmp_initial) = PMA_DBI_fetch_row($initials)) {
1426 $array_initials[$tmp_initial] = TRUE;
1429 // Display the initials, which can be any characters, not
1430 // just letters. For letters A-Z, we add the non-used letters
1431 // as greyed out.
1433 uksort($array_initials, "strnatcasecmp");
1435 echo '<table cellspacing="5"><tr>';
1436 foreach ($array_initials as $tmp_initial => $initial_was_found) {
1437 if ($initial_was_found) {
1438 echo '<td><a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;initial=' . urlencode($tmp_initial) . '">' . $tmp_initial . '</a></td>' . "\n";
1439 } else {
1440 echo '<td>' . $tmp_initial . '</td>';
1443 echo '<td><a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;showall=1">[' . $GLOBALS['strShowAll'] . ']</a></td>' . "\n";
1444 echo '</tr></table>';
1447 * Display the user overview
1448 * (if less than 50 users, display them immediately)
1451 if (isset($initial) || isset($showall) || PMA_DBI_num_rows($res) < 50) {
1453 while ($row = PMA_DBI_fetch_assoc($res)) {
1454 $row['privs'] = PMA_extractPrivInfo($row, true);
1455 $db_rights[$row['User']][$row['Host']] = $row;
1457 @PMA_DBI_free_result($res);
1458 unset($res);
1460 echo '<form name="usersForm" id="usersForm" action="server_privileges.php" method="post">' . "\n"
1461 . PMA_generate_common_hidden_inputs('', '', 1)
1462 . ' <table id="tableuserrights" class="data">' . "\n"
1463 . ' <thead>' . "\n"
1464 . ' <tr><td></td>' . "\n"
1465 . ' <th>' . $GLOBALS['strUser'] . '</th>' . "\n"
1466 . ' <th>' . $GLOBALS['strHost'] . '</th>' . "\n"
1467 . ' <th>' . $GLOBALS['strPassword'] . '</th>' . "\n"
1468 . ' <th>' . $GLOBALS['strGlobalPrivileges'] . ' '
1469 . PMA_showHint($GLOBALS['strEnglishPrivileges']) . '</th>' . "\n"
1470 . ' <th>' . $GLOBALS['strGrantOption'] . '</th>' . "\n"
1471 . ' ' . ($GLOBALS['cfg']['PropertiesIconic'] ? '<td></td>' : '<th>' . $GLOBALS['strAction'] . '</th>') . "\n";
1472 echo ' </tr>' . "\n";
1473 echo ' </thead>' . "\n";
1474 echo ' <tbody>' . "\n";
1475 $odd_row = true;
1476 $index_checkbox = -1;
1477 foreach ($db_rights as $user) {
1478 $index_checkbox++;
1479 ksort($user);
1480 foreach ($user as $host) {
1481 $index_checkbox++;
1482 echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n"
1483 . ' <td><input type="checkbox" name="selected_usr[]" id="checkbox_sel_users_' . $index_checkbox . '" value="' . str_replace(chr(27), '&#27;', htmlspecialchars($host['User'] . $user_host_separator . $host['Host'])) . '"' . (empty($GLOBALS['checkall']) ? '' : ' checked="checked"') . ' /></td>' . "\n"
1484 . ' <td><label for="checkbox_sel_users_' . $index_checkbox . '">' . (empty($host['User']) ? '<span style="color: #FF0000">' . $GLOBALS['strAny'] . '</span>' : htmlspecialchars($host['User'])) . '</label></td>' . "\n"
1485 . ' <td>' . htmlspecialchars($host['Host']) . '</td>' . "\n";
1486 echo ' <td>';
1487 switch ($host['Password']) {
1488 case 'Y':
1489 echo $GLOBALS['strYes'];
1490 break;
1491 case 'N':
1492 echo '<span style="color: #FF0000">' . $GLOBALS['strNo'] . '</span>';
1493 break;
1494 // this happens if this is a definition not coming from mysql.user
1495 default:
1496 echo '--'; // in future version, replace by "not present"
1497 break;
1498 } // end switch
1499 echo '</td>' . "\n"
1500 . ' <td><tt>' . "\n"
1501 . ' ' . implode(',' . "\n" . ' ', $host['privs']) . "\n"
1502 . ' </tt></td>' . "\n"
1503 . ' <td>' . ($host['Grant_priv'] == 'Y' ? $GLOBALS['strYes'] : $GLOBALS['strNo']) . '</td>' . "\n"
1504 . ' <td align="center">';
1505 printf($link_edit, urlencode($host['User']),
1506 urlencode($host['Host']), '', '');
1507 echo '</td>' . "\n"
1508 . ' </tr>' . "\n";
1509 $odd_row = ! $odd_row;
1513 unset($user, $host, $odd_row);
1514 echo ' </tbody></table>' . "\n"
1515 .'<img class="selectallarrow"'
1516 .' src="' . $pmaThemeImage . 'arrow_' . $text_dir . '.png"'
1517 .' width="38" height="22"'
1518 .' alt="' . $GLOBALS['strWithChecked'] . '" />' . "\n"
1519 .'<a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;checkall=1"'
1520 .' onclick="if (markAllRows(\'usersForm\')) return false;">'
1521 . $GLOBALS['strCheckAll'] . '</a>' . "\n"
1522 .'/' . "\n"
1523 .'<a href="server_privileges.php?' . $GLOBALS['url_query'] . '"'
1524 .' onclick="if (unMarkAllRows(\'usersForm\')) return false;">'
1525 . $GLOBALS['strUncheckAll'] . '</a>' . "\n";
1527 // add/delete user fieldset
1528 echo ' <fieldset id="fieldset_add_user">' . "\n"
1529 . ' <a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;adduser=1">' . "\n"
1530 . ($GLOBALS['cfg']['PropertiesIconic'] ? ' <img class="icon" src="' . $pmaThemeImage . 'b_usradd.png" width="16" height="16" alt="" />' . "\n" : '')
1531 . ' ' . $GLOBALS['strAddUser'] . '</a>' . "\n"
1532 . ' </fieldset>' . "\n"
1533 . ' <fieldset id="fieldset_delete_user">'
1534 . ' <legend>' . "\n"
1535 . ($GLOBALS['cfg']['PropertiesIconic'] ? ' <img class="icon" src="' . $pmaThemeImage . 'b_usrdrop.png" width="16" height="16" alt="" />' . "\n" : '')
1536 . ' ' . $GLOBALS['strRemoveSelectedUsers'] . '' . "\n"
1537 . ' </legend>' . "\n";
1539 // before MySQL 4.1.1, we offer some choices for the delete
1540 // mode, but for 4.1.1+, it will be done with REVOKEing the
1541 // privileges then a DROP USER (even no REVOKE at all
1542 // for MySQL 5), so no need to offer so many options
1543 if (PMA_MYSQL_INT_VERSION < 40101) {
1544 echo ' <input type="radio" title="' . $GLOBALS['strJustDelete'] . ' ' . $GLOBALS['strJustDeleteDescr'] . '" name="mode" id="radio_mode_1" value="1" checked="checked" />' . "\n"
1545 . ' <label for="radio_mode_1" title="' . $GLOBALS['strJustDelete'] . ' ' . $GLOBALS['strJustDeleteDescr'] . '">' . "\n"
1546 . ' ' . $GLOBALS['strJustDelete'] . "\n"
1547 . ' </label><br />' . "\n"
1548 . ' <input type="radio" title="' . $GLOBALS['strRevokeAndDelete'] . ' ' . $GLOBALS['strRevokeAndDeleteDescr'] . '" name="mode" id="radio_mode_2" value="2" />' . "\n"
1549 . ' <label for="radio_mode_2" title="' . $GLOBALS['strRevokeAndDelete'] . ' ' . $GLOBALS['strRevokeAndDeleteDescr'] . '">' . "\n"
1550 . ' ' . $GLOBALS['strRevokeAndDelete'] . "\n"
1551 . ' </label><br />' . "\n"
1552 . ' <input type="radio" title="' . $GLOBALS['strDeleteAndFlush'] . ' ' . $GLOBALS['strDeleteAndFlushDescr'] . '" name="mode" id="radio_mode_3" value="3" />' . "\n"
1553 . ' <label for="radio_mode_3" title="' . $GLOBALS['strDeleteAndFlush'] . ' ' . $GLOBALS['strDeleteAndFlushDescr'] . '">' . "\n"
1554 . ' ' . $GLOBALS['strDeleteAndFlush'] . "\n"
1555 . ' </label><br />' . "\n";
1556 } else {
1557 echo ' <input type="hidden" name="mode" value="2" />' . "\n"
1558 . '(' . $GLOBALS['strRevokeAndDelete'] . ')<br />' . "\n";
1561 echo ' <input type="checkbox" title="' . $GLOBALS['strDropUsersDb'] . '" name="drop_users_db" id="checkbox_drop_users_db" />' . "\n"
1562 . ' <label for="checkbox_drop_users_db" title="' . $GLOBALS['strDropUsersDb'] . '">' . "\n"
1563 . ' ' . $GLOBALS['strDropUsersDb'] . "\n"
1564 . ' </label>' . "\n"
1565 . ' </fieldset>' . "\n"
1566 . ' <fieldset id="fieldset_delete_user_footer" class="tblFooters">' . "\n"
1567 . ' <input type="submit" name="delete" value="' . $GLOBALS['strGo'] . '" id="buttonGo" />' . "\n"
1568 . ' </fieldset>' . "\n";
1569 } else {
1571 unset ($row);
1572 echo ' <fieldset id="fieldset_add_user">' . "\n"
1573 . ' <a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;adduser=1">' . "\n"
1574 . ($GLOBALS['cfg']['PropertiesIconic'] ? ' <img class="icon" src="' . $pmaThemeImage . 'b_usradd.png" width="16" height="16" alt="" />' . "\n" : '')
1575 . ' ' . $GLOBALS['strAddUser'] . '</a>' . "\n"
1576 . ' </fieldset>' . "\n";
1577 } // end if (display overview)
1578 echo '</form>' . "\n"
1579 . '<div class="warning">' . "\n"
1580 . ' ' . sprintf($GLOBALS['strFlushPrivilegesNote'], '<a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;flush_privileges=1">', '</a>') . "\n"
1581 . '</div>' . "\n";
1585 } else {
1587 // A user was selected -> display the user's properties
1589 echo '<h2>' . "\n"
1590 . ($GLOBALS['cfg']['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_usredit.png" width="16" height="16" alt="" />' : '')
1591 . $GLOBALS['strUser'] . ' <i><a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;username=' . urlencode($username) . '&amp;hostname=' . urlencode($hostname) . '">\'' . htmlspecialchars($username) . '\'@\'' . htmlspecialchars($hostname) . '\'</a></i>' . "\n";
1592 if (isset($dbname) && strlen($dbname)) {
1593 if ($dbname_is_wildcard) {
1594 echo ' - ' . $GLOBALS['strDatabases'];
1595 } else {
1596 echo ' - ' . $GLOBALS['strDatabase'];
1598 $url_dbname = urlencode(str_replace('\_', '_', $dbname));
1599 echo ' <i><a href="' . $GLOBALS['cfg']['DefaultTabDatabase'] . '?' . $GLOBALS['url_query'] . '&amp;db=' . $url_dbname . '&amp;reload=1">' . htmlspecialchars($dbname) . '</a></i>' . "\n";
1600 if (isset($tablename) && strlen($tablename)) {
1601 echo ' - ' . $GLOBALS['strTable'] . ' <i><a href="' . $GLOBALS['cfg']['DefaultTabTable'] . '?' . $GLOBALS['url_query'] . '&amp;db=' . $url_dbname . '&amp;table=' . urlencode($tablename) . '&amp;reload=1">' . htmlspecialchars($tablename) . '</a></i>' . "\n";
1603 unset($url_dbname);
1605 echo ' : ' . $GLOBALS['strEditPrivileges'] . '</h2>' . "\n";
1606 $res = PMA_DBI_query('SELECT \'foo\' FROM `mysql`.`user` WHERE ' . PMA_convert_using('User') . ' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted') . ' AND ' . PMA_convert_using('Host') . ' = ' . PMA_convert_using($hostname, 'quoted') . ';', null, PMA_DBI_QUERY_STORE);
1607 $user_does_not_exists = (PMA_DBI_num_rows($res) < 1);
1608 PMA_DBI_free_result($res);
1609 unset($res);
1610 if ($user_does_not_exists) {
1611 echo $GLOBALS['strUserNotFound'];
1612 PMA_displayLoginInformationFields();
1613 //require_once './libraries/footer.inc.php';
1615 echo '<form name="usersForm" id="usersForm" action="server_privileges.php" method="post">' . "\n"
1616 . PMA_generate_common_hidden_inputs('', '', 3)
1617 . '<input type="hidden" name="username" value="' . htmlspecialchars($username) . '" />' . "\n"
1618 . '<input type="hidden" name="hostname" value="' . htmlspecialchars($hostname) . '" />' . "\n";
1619 if (isset($dbname) && strlen($dbname)) {
1620 echo '<input type="hidden" name="dbname" value="' . htmlspecialchars($dbname) . '" />' . "\n";
1621 if (isset($tablename) && strlen($tablename)) {
1622 echo ' <input type="hidden" name="tablename" value="' . htmlspecialchars($tablename) . '" />' . "\n";
1625 PMA_displayPrivTable(((! isset($dbname) || ! strlen($dbname)) ? '*' : $dbname),
1626 (((! isset($dbname) || ! strlen($dbname)) || (! isset($tablename) || ! strlen($tablename))) ? '*' : $tablename),
1627 TRUE, 3);
1628 echo '</form>' . "\n";
1630 if ((! isset($tablename) || ! strlen($tablename)) && empty($dbname_is_wildcard)) {
1632 // no table name was given, display all table specific rights
1633 // but only if $dbname contains no wildcards
1635 // table header
1636 echo '<form action="server_privileges.php" method="post">' . "\n"
1637 . PMA_generate_common_hidden_inputs('', '', 6)
1638 . '<input type="hidden" name="username" value="' . htmlspecialchars($username) . '" />' . "\n"
1639 . '<input type="hidden" name="hostname" value="' . htmlspecialchars($hostname) . '" />' . "\n"
1640 . '<fieldset>' . "\n"
1641 . '<legend>' . ((! isset($dbname) || ! strlen($dbname)) ? $GLOBALS['strDbPrivileges'] : $GLOBALS['strTblPrivileges']) . '</legend>' . "\n"
1642 . '<table class="data">' . "\n"
1643 . '<thead>' . "\n"
1644 . '<tr><th>' . ((! isset($dbname) || ! strlen($dbname)) ? $GLOBALS['strDatabase'] : $GLOBALS['strTable']) . '</th>' . "\n"
1645 . ' <th>' . $GLOBALS['strPrivileges'] . '</th>' . "\n"
1646 . ' <th>' . $GLOBALS['strGrantOption'] . '</th>' . "\n"
1647 . ' <th>' . ((! isset($dbname) || ! strlen($dbname)) ? $GLOBALS['strTblPrivileges'] : $GLOBALS['strColumnPrivileges']) . '</th>' . "\n"
1648 . ' <th colspan="2">' . $GLOBALS['strAction'] . '</th>' . "\n"
1649 . '</tr>' . "\n"
1650 . '</thead>' . "\n"
1651 . '<tbody>' . "\n";
1653 $user_host_condition =
1654 ' WHERE ' . PMA_convert_using('`User`')
1655 . ' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted')
1656 . ' AND ' . PMA_convert_using('`Host`')
1657 . ' = ' . PMA_convert_using($hostname, 'quoted');
1659 // table body
1660 // get data
1662 // we also want privielgs for this user not in table `db` but in other table
1663 $tables = PMA_DBI_fetch_result('SHOW TABLES FROM `mysql`;');
1664 if ((! isset($dbname) || ! strlen($dbname))) {
1666 // no db name given, so we want all privs for the given user
1668 $tables_to_search_for_users = array(
1669 'tables_priv', 'columns_priv',
1672 $db_rights_sqls = array();
1673 foreach ($tables_to_search_for_users as $table_search_in) {
1674 if (in_array($table_search_in, $tables)) {
1675 $db_rights_sqls[] = '
1676 SELECT DISTINCT `Db`
1677 FROM `mysql`.`' . $table_search_in . '`
1678 ' . $user_host_condition;
1682 $user_defaults = array(
1683 'Db' => '',
1684 'Grant_priv' => 'N',
1685 'privs' => array('USAGE'),
1686 'Table_privs' => true,
1689 // for the rights
1690 $db_rights = array();
1692 if (PMA_MYSQL_INT_VERSION >= 40000) {
1693 $db_rights_sql = '(' . implode(') UNION (', $db_rights_sqls) . ')'
1694 .' ORDER BY `Db` ASC';
1696 $db_rights_result = PMA_DBI_query($db_rights_sql);
1698 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1699 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1700 // only Db names in the table `mysql`.`db` uses wildcards
1701 // as we are in the db specific rights display we want
1702 // all db names escaped, also from other sources
1703 $db_rights_row['Db'] = PMA_escape_mysql_wildcards(
1704 $db_rights_row['Db']);
1705 $db_rights[$db_rights_row['Db']] = $db_rights_row;
1707 } else {
1708 foreach ($db_rights_sqls as $db_rights_sql) {
1709 $db_rights_result = PMA_DBI_query($db_rights_sql);
1711 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1712 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1713 $db_rights[$db_rights_row['Db']] = $db_rights_row;
1717 PMA_DBI_free_result($db_rights_result);
1718 unset($db_rights_sql, $db_rights_sqls, $db_rights_result, $db_rights_row);
1720 $sql_query = 'SELECT * FROM `mysql`.`db`' . $user_host_condition . ' ORDER BY `Db` ASC';
1721 $res = PMA_DBI_query($sql_query);
1722 $sql_query = '';
1724 while ($row = PMA_DBI_fetch_assoc($res)) {
1725 if (isset($db_rights[$row['Db']])) {
1726 $db_rights[$row['Db']] = array_merge($db_rights[$row['Db']], $row);
1727 } else {
1728 $db_rights[$row['Db']] = $row;
1730 // there are db specific rights for this user
1731 // so we can drop this db rights
1732 $db_rights[$row['Db']]['can_delete'] = true;
1734 PMA_DBI_free_result($res);
1735 unset($row, $res);
1737 } else {
1739 // db name was given,
1740 // so we want all user specific rights for this db
1742 $user_host_condition .=
1743 ' AND ' . PMA_convert_using('`Db`')
1744 .' LIKE ' . PMA_convert_using($dbname, 'quoted');
1746 $tables_to_search_for_users = array(
1747 'columns_priv',
1750 $db_rights_sqls = array();
1751 foreach ($tables_to_search_for_users as $table_search_in) {
1752 if (in_array($table_search_in, $tables)) {
1753 $db_rights_sqls[] = '
1754 SELECT DISTINCT `Table_name`
1755 FROM `mysql`.`' . $table_search_in . '`
1756 ' . $user_host_condition;
1760 $user_defaults = array(
1761 'Table_name' => '',
1762 'Grant_priv' => 'N',
1763 'privs' => array('USAGE'),
1764 'Column_priv' => true,
1767 // for the rights
1768 $db_rights = array();
1770 if (PMA_MYSQL_INT_VERSION >= 40000) {
1771 $db_rights_sql = '(' . implode(') UNION (', $db_rights_sqls) . ')'
1772 .' ORDER BY `Table_name` ASC';
1774 $db_rights_result = PMA_DBI_query($db_rights_sql);
1776 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1777 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1778 $db_rights[$db_rights_row['Table_name']] = $db_rights_row;
1780 } else {
1781 foreach ($db_rights_sqls as $db_rights_sql) {
1782 $db_rights_result = PMA_DBI_query($db_rights_sql);
1784 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1785 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1786 $db_rights[$db_rights_row['Table_name']] = $db_rights_row;
1790 PMA_DBI_free_result($db_rights_result);
1791 unset($db_rights_sql, $db_rights_sqls, $db_rights_result, $db_rights_row);
1793 $sql_query =
1794 'SELECT `Table_name`,'
1795 .' `Table_priv`,'
1796 .' IF(`Column_priv` = ' . (PMA_MYSQL_INT_VERSION >= 40100 ? '_latin1 ' : '') . ' \'\', 0, 1)'
1797 .' AS \'Column_priv\''
1798 .' FROM `mysql`.`tables_priv`'
1799 . $user_host_condition
1800 .' ORDER BY `Table_name` ASC;';
1801 $res = PMA_DBI_query($sql_query);
1802 $sql_query = '';
1804 while ($row = PMA_DBI_fetch_assoc($res)) {
1805 if (isset($db_rights[$row['Table_name']])) {
1806 $db_rights[$row['Table_name']] = array_merge($db_rights[$row['Table_name']], $row);
1807 } else {
1808 $db_rights[$row['Table_name']] = $row;
1811 PMA_DBI_free_result($res);
1812 unset($row, $res);
1814 ksort($db_rights);
1816 // display rows
1817 if (count($db_rights) < 1) {
1818 echo '<tr class="odd">' . "\n"
1819 . ' <td colspan="6"><center><i>' . $GLOBALS['strNone'] . '</i></center></td>' . "\n"
1820 . '</tr>' . "\n";
1821 } else {
1822 $odd_row = true;
1823 $found_rows = array();
1824 //while ($row = PMA_DBI_fetch_assoc($res)) {
1825 foreach ($db_rights as $row) {
1826 $found_rows[] = (! isset($dbname) || ! strlen($dbname)) ? $row['Db'] : $row['Table_name'];
1828 echo '<tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n"
1829 . ' <td>' . htmlspecialchars((! isset($dbname) || ! strlen($dbname)) ? $row['Db'] : $row['Table_name']) . '</td>' . "\n"
1830 . ' <td><tt>' . "\n"
1831 . ' ' . join(',' . "\n" . ' ', PMA_extractPrivInfo($row, TRUE)) . "\n"
1832 . ' </tt></td>' . "\n"
1833 . ' <td>' . ((((! isset($dbname) || ! strlen($dbname)) && $row['Grant_priv'] == 'Y') || (isset($dbname) && strlen($dbname) && in_array('Grant', explode(',', $row['Table_priv'])))) ? $GLOBALS['strYes'] : $GLOBALS['strNo']) . '</td>' . "\n"
1834 . ' <td>';
1835 if (! empty($row['Table_privs']) || ! empty ($row['Column_priv'])) {
1836 echo $GLOBALS['strYes'];
1837 } else {
1838 echo $GLOBALS['strNo'];
1840 echo '</td>' . "\n"
1841 . ' <td>';
1842 printf($link_edit, urlencode($username),
1843 urlencode($hostname),
1844 urlencode((! isset($dbname) || ! strlen($dbname)) ? $row['Db'] : $dbname),
1845 urlencode((! isset($dbname) || ! strlen($dbname)) ? '' : $row['Table_name']));
1846 echo '</td>' . "\n"
1847 . ' <td>';
1848 if (! empty($row['can_delete']) || isset($row['Table_name']) && strlen($row['Table_name'])) {
1849 printf($link_revoke, urlencode($username),
1850 urlencode($hostname),
1851 urlencode((! isset($dbname) || ! strlen($dbname)) ? $row['Db'] : $dbname),
1852 urlencode((! isset($dbname) || ! strlen($dbname)) ? '' : $row['Table_name']));
1854 echo '</td>' . "\n"
1855 . '</tr>' . "\n";
1856 $odd_row = ! $odd_row;
1857 } // end while
1859 unset($row);
1860 echo '</tbody>' . "\n"
1861 . '</table>' . "\n";
1863 if (! isset($dbname) || ! strlen($dbname)) {
1865 // no database name was give, display select db
1867 if (! empty($found_rows)) {
1868 $pred_db_array = array_diff(
1869 PMA_DBI_fetch_result('SHOW DATABASES;'),
1870 $found_rows);
1871 } else {
1872 $pred_db_array =PMA_DBI_fetch_result('SHOW DATABASES;');
1875 echo ' <label for="text_dbname">' . $GLOBALS['strAddPrivilegesOnDb'] . ':</label>' . "\n";
1876 if (!empty($pred_db_array)) {
1877 echo ' <select name="pred_dbname" onchange="this.form.submit();">' . "\n"
1878 . ' <option value="" selected="selected">' . $GLOBALS['strUseTextField'] . ':</option>' . "\n";
1879 foreach ($pred_db_array as $current_db) {
1880 $current_db = PMA_escape_mysql_wildcards($current_db);
1881 echo ' <option value="' . htmlspecialchars($current_db) . '">'
1882 . htmlspecialchars($current_db) . '</option>' . "\n";
1884 echo ' </select>' . "\n";
1886 echo ' <input type="text" id="text_dbname" name="dbname" />' . "\n"
1887 .PMA_showHint($GLOBALS['strEscapeWildcards']);
1888 } else {
1889 echo ' <input type="hidden" name="dbname" value="' . htmlspecialchars($dbname) . '"/>' . "\n"
1890 . ' <label for="text_tablename">' . $GLOBALS['strAddPrivilegesOnTbl'] . ':</label>' . "\n";
1891 if ($res = @PMA_DBI_try_query('SHOW TABLES FROM ' . PMA_backquote($dbname) . ';', null, PMA_DBI_QUERY_STORE)) {
1892 $pred_tbl_array = array();
1893 while ($row = PMA_DBI_fetch_row($res)) {
1894 if (!isset($found_rows) || !in_array($row[0], $found_rows)) {
1895 $pred_tbl_array[] = $row[0];
1898 PMA_DBI_free_result($res);
1899 unset($res, $row);
1900 if (!empty($pred_tbl_array)) {
1901 echo ' <select name="pred_tablename" onchange="this.form.submit();">' . "\n"
1902 . ' <option value="" selected="selected">' . $GLOBALS['strUseTextField'] . ':</option>' . "\n";
1903 foreach ($pred_tbl_array as $current_table) {
1904 echo ' <option value="' . htmlspecialchars($current_table) . '">' . htmlspecialchars($current_table) . '</option>' . "\n";
1906 echo ' </select>' . "\n";
1908 } else {
1909 unset($res);
1911 echo ' <input type="text" id="text_tablename" name="tablename" />' . "\n";
1913 echo '</fieldset>' . "\n";
1914 echo '<fieldset class="tblFooters">' . "\n"
1915 . ' <input type="submit" value="' . $GLOBALS['strGo'] . '" />'
1916 . '</fieldset>' . "\n"
1917 . '</form>' . "\n";
1920 if ((! isset($dbname) || ! strlen($dbname)) && ! $user_does_not_exists) {
1921 require_once './libraries/display_change_password.lib.php';
1923 echo '<form action="server_privileges.php" method="post" onsubmit="return checkPassword(this);">' . "\n"
1924 . PMA_generate_common_hidden_inputs('', '', 3)
1925 . '<input type="hidden" name="old_username" value="' . htmlspecialchars($username) . '" />' . "\n"
1926 . '<input type="hidden" name="old_hostname" value="' . htmlspecialchars($hostname) . '" />' . "\n"
1927 . '<fieldset id="fieldset_change_copy_user">' . "\n"
1928 . ' <legend>' . $GLOBALS['strChangeCopyUser'] . '</legend>' . "\n";
1929 PMA_displayLoginInformationFields('change', 3);
1930 echo ' <fieldset>' . "\n"
1931 . ' <legend>' . $GLOBALS['strChangeCopyMode'] . '</legend>' . "\n"
1932 . ' <input type="radio" name="mode" value="4" id="radio_mode_4" checked="checked" /><label for="radio_mode_4">' . "\n"
1933 . ' ' . $GLOBALS['strChangeCopyModeCopy'] . "\n"
1934 . ' </label><br />' . "\n"
1935 . ' <input type="radio" name="mode" value="1" id="radio_mode_1" /><label for="radio_mode_1">' . "\n"
1936 . ' ' . $GLOBALS['strChangeCopyModeJustDelete'] . "\n"
1937 . ' </label><br />' . "\n"
1938 . ' <input type="radio" name="mode" value="2" id="radio_mode_2" /><label for="radio_mode_2">' . "\n"
1939 . ' ' . $GLOBALS['strChangeCopyModeRevoke'] . "\n"
1940 . ' </label><br />' . "\n"
1941 . ' <input type="radio" name="mode" value="3" id="radio_mode_3" /><label for="radio_mode_3">' . "\n"
1942 . ' ' . $GLOBALS['strChangeCopyModeDeleteAndReload'] . "\n"
1943 . ' </label>' . "\n"
1944 . ' </fieldset>' . "\n"
1945 . '</fieldset>' . "\n"
1946 . '<fieldset id="fieldset_change_copy_user_footer" class="tblFooters">' . "\n"
1947 . ' <input type="submit" name="change_copy" value="' . $GLOBALS['strGo'] . '" />' . "\n"
1948 . '</fieldset>' . "\n"
1949 . '</form>' . "\n";
1952 } elseif (!empty($adduser)) {
1953 // Add a new user
1954 $GLOBALS['url_query'] .= '&amp;adduser=1';
1955 echo '<h2>' . "\n"
1956 . ($GLOBALS['cfg']['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_usradd.png" width="16" height="16" alt="" />' : '')
1957 . ' ' . $GLOBALS['strAddUser'] . "\n"
1958 . '</h2>' . "\n"
1959 . '<form name="usersForm" id="usersForm" action="server_privileges.php" method="post" onsubmit="return checkAddUser(this);">' . "\n"
1960 . PMA_generate_common_hidden_inputs('', '', 1);
1961 PMA_displayLoginInformationFields('new', 2);
1962 echo '<fieldset id="fieldset_add_user_database">' . "\n"
1963 . '<legend>' . $GLOBALS['strCreateUserDatabase'] . '</legend>' . "\n"
1964 . ' <div class="item">' . "\n"
1965 . ' <input type="radio" name="createdb" value="0" id="radio_createdb_0" checked="checked" />' . "\n"
1966 . ' <label for="radio_createdb_0">' . $GLOBALS['strCreateUserDatabaseNone'] . '</label>' . "\n"
1967 . ' </div>' . "\n"
1968 . ' <div class="item">' . "\n"
1969 . ' <input type="radio" name="createdb" value="1" id="radio_createdb_1" />' . "\n"
1970 . ' <label for="radio_createdb_1">' . $GLOBALS['strCreateUserDatabaseName'] . '</label>' . "\n"
1971 . ' </div>' . "\n"
1972 . ' <div class="item">' . "\n"
1973 . ' <input type="radio" name="createdb" value="2" id="radio_createdb_2" />' . "\n"
1974 . ' <label for="radio_createdb_2">' . $GLOBALS['strCreateUserDatabaseWildcard'] . '</label>' . "\n"
1975 . ' </div>' . "\n"
1976 . '</fieldset>' . "\n";
1977 PMA_displayPrivTable('*', '*', FALSE, 1);
1978 echo ' <fieldset id="fieldset_add_user_footer" class="tblFooters">' . "\n"
1979 . ' <input type="submit" name="adduser_submit" value="' . $GLOBALS['strGo'] . '" />' . "\n"
1980 . ' </fieldset>' . "\n"
1981 . '</form>' . "\n";
1982 } else {
1983 // check the privileges for a particular database.
1984 echo '<table id="tablespecificuserrights" class="data">' . "\n"
1985 . '<caption class="tblHeaders">' . "\n"
1986 . ($GLOBALS['cfg']['PropertiesIconic'] ? ' <img class="icon" src="' . $pmaThemeImage . 'b_usrcheck.png" width="16" height="16" alt="" />' . "\n" : '')
1987 . ' ' . sprintf($GLOBALS['strUsersHavingAccessToDb'], '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase'] . '?' . PMA_generate_common_url($checkprivs) . '">' . htmlspecialchars($checkprivs) . '</a>') . "\n"
1988 . '</caption>' . "\n"
1989 . '<thead>' . "\n"
1990 . ' <tr><th>' . $GLOBALS['strUser'] . '</th>' . "\n"
1991 . ' <th>' . $GLOBALS['strHost'] . '</th>' . "\n"
1992 . ' <th>' . $GLOBALS['strType'] . '</th>' . "\n"
1993 . ' <th>' . $GLOBALS['strPrivileges'] . '</th>' . "\n"
1994 . ' <th>' . $GLOBALS['strGrantOption'] . '</th>' . "\n"
1995 . ' <th>' . $GLOBALS['strAction'] . '</th>' . "\n"
1996 . ' </tr>' . "\n"
1997 . '</thead>' . "\n"
1998 . '<tbody>' . "\n";
1999 $odd_row = TRUE;
2000 unset($row);
2001 unset($row1);
2002 unset($row2);
2003 // now, we build the table...
2004 if (PMA_MYSQL_INT_VERSION >= 40000) {
2005 // Starting with MySQL 4.0.0, we may use UNION SELECTs and this makes
2006 // the job much easier here!
2008 $no = PMA_convert_using('N', 'quoted');
2010 $list_of_privileges =
2011 PMA_convert_using('Select_priv') . ' AS Select_priv, '
2012 . PMA_convert_using('Insert_priv') . ' AS Insert_priv, '
2013 . PMA_convert_using('Update_priv') . ' AS Update_priv, '
2014 . PMA_convert_using('Delete_priv') . ' AS Delete_priv, '
2015 . PMA_convert_using('Create_priv') . ' AS Create_priv, '
2016 . PMA_convert_using('Drop_priv') . ' AS Drop_priv, '
2017 . PMA_convert_using('Grant_priv') . ' AS Grant_priv, '
2018 . PMA_convert_using('References_priv') . ' AS References_priv';
2020 $list_of_compared_privileges =
2021 PMA_convert_using('Select_priv') . ' = ' . $no
2022 . ' AND ' . PMA_convert_using('Insert_priv') . ' = ' . $no
2023 . ' AND ' . PMA_convert_using('Update_priv') . ' = ' . $no
2024 . ' AND ' . PMA_convert_using('Delete_priv') . ' = ' . $no
2025 . ' AND ' . PMA_convert_using('Create_priv') . ' = ' . $no
2026 . ' AND ' . PMA_convert_using('Drop_priv') . ' = ' . $no
2027 . ' AND ' . PMA_convert_using('Grant_priv') . ' = ' . $no
2028 . ' AND ' . PMA_convert_using('References_priv') . ' = ' . $no;
2030 $sql_query =
2031 '(SELECT ' . PMA_convert_using('`User`') . ' AS `User`, '
2032 . PMA_convert_using('`Host`') . ' AS `Host`, '
2033 . PMA_convert_using('`Db`') . ' AS `Db`, '
2034 . $list_of_privileges
2035 .' FROM `mysql`.`db`'
2036 .' WHERE ' . PMA_convert_using(PMA_sqlAddslashes($checkprivs), 'quoted')
2037 .' LIKE ' . PMA_convert_using('`Db`')
2038 .' AND NOT (' . $list_of_compared_privileges. ')) '
2039 .'UNION '
2040 .'(SELECT ' . PMA_convert_using('`User`') . ' AS `User`, '
2041 . PMA_convert_using('`Host`') . ' AS `Host`, '
2042 . PMA_convert_using('*', 'quoted') .' AS `Db`, '
2043 . $list_of_privileges
2044 .' FROM `mysql`.`user` '
2045 .' WHERE NOT (' . $list_of_compared_privileges . ')) '
2046 .' ORDER BY `User` ASC,'
2047 .' `Host` ASC,'
2048 .' `Db` ASC;';
2049 $res = PMA_DBI_query($sql_query);
2050 $row = PMA_DBI_fetch_assoc($res);
2051 if ($row) {
2052 $found = TRUE;
2054 } else {
2055 // With MySQL 3, we need 2 seperate queries here.
2056 $sql_query = 'SELECT * FROM `mysql`.`user` WHERE NOT (`Select_priv` = \'N\' AND `Insert_priv` = \'N\' AND `Update_priv` = \'N\' AND `Delete_priv` = \'N\' AND `Create_priv` = \'N\' AND `Drop_priv` = \'N\' AND `Grant_priv` = \'N\' AND `References_priv` = \'N\') ORDER BY `User` ASC, `Host` ASC;';
2057 $res1 = PMA_DBI_query($sql_query);
2058 $row1 = PMA_DBI_fetch_assoc($res1);
2059 $sql_query =
2060 'SELECT * FROM `mysql`.`db`'
2061 .' WHERE \'' . $checkprivs . '\''
2062 .' LIKE `Db`'
2063 .' AND NOT (`Select_priv` = \'N\''
2064 .' AND `Insert_priv` = \'N\''
2065 .' AND `Update_priv` = \'N\''
2066 .' AND `Delete_priv` = \'N\''
2067 .' AND `Create_priv` = \'N\''
2068 .' AND `Drop_priv` = \'N\''
2069 .' AND `Grant_priv` = \'N\''
2070 .' AND `References_priv` = \'N\')'
2071 .' ORDER BY `User` ASC, `Host` ASC;';
2072 $res2 = PMA_DBI_query($sql_query);
2073 $row2 = PMA_DBI_fetch_assoc($res2);
2074 if ($row1 || $row2) {
2075 $found = TRUE;
2077 } // end if (PMA_MYSQL_INT_VERSION >= 40000) ... else ...
2078 if ($found) {
2079 while (TRUE) {
2080 // prepare the current user
2081 if (PMA_MYSQL_INT_VERSION >= 40000) {
2082 $current_privileges = array();
2083 $current_user = $row['User'];
2084 $current_host = $row['Host'];
2085 while ($row && $current_user == $row['User'] && $current_host == $row['Host']) {
2086 $current_privileges[] = $row;
2087 $row = PMA_DBI_fetch_assoc($res);
2089 } else {
2090 $current_privileges = array();
2091 if ($row1 && (!$row2 || ($row1['User'] < $row2['User'] || ($row1['User'] == $row2['User'] && $row1['Host'] <= $row2['Host'])))) {
2092 $current_user = $row1['User'];
2093 $current_host = $row1['Host'];
2094 $current_privileges = array($row1);
2095 $row1 = PMA_DBI_fetch_assoc($res1);
2096 } else {
2097 $current_user = $row2['User'];
2098 $current_host = $row2['Host'];
2099 $current_privileges = array();
2101 while ($row2 && $current_user == $row2['User'] && $current_host == $row2['Host']) {
2102 $current_privileges[] = $row2;
2103 $row2 = PMA_DBI_fetch_assoc($res2);
2106 echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n"
2107 . ' <td';
2108 if (count($current_privileges) > 1) {
2109 echo ' rowspan="' . count($current_privileges) . '"';
2111 echo '>' . (empty($current_user) ? '<span style="color: #FF0000">' . $GLOBALS['strAny'] . '</span>' : htmlspecialchars($current_user)) . "\n"
2112 . ' </td>' . "\n"
2113 . ' <td';
2114 if (count($current_privileges) > 1) {
2115 echo ' rowspan="' . count($current_privileges) . '"';
2117 echo '>' . htmlspecialchars($current_host) . '</td>' . "\n";
2118 foreach ($current_privileges as $current) {
2119 echo ' <td>' . "\n"
2120 . ' ';
2121 if (!isset($current['Db']) || $current['Db'] == '*') {
2122 echo $GLOBALS['strGlobal'];
2123 } elseif ($current['Db'] == PMA_escape_mysql_wildcards($checkprivs)) {
2124 echo $GLOBALS['strDbSpecific'];
2125 } else {
2126 echo $GLOBALS['strWildcard'], ': <tt>' . htmlspecialchars($current['Db']) . '</tt>';
2128 echo "\n"
2129 . ' </td>' . "\n"
2130 . ' <td>' . "\n"
2131 . ' <tt>' . "\n"
2132 . ' ' . join(',' . "\n" . ' ', PMA_extractPrivInfo($current, TRUE)) . "\n"
2133 . ' </tt>' . "\n"
2134 . ' </td>' . "\n"
2135 . ' <td>' . "\n"
2136 . ' ' . ($current['Grant_priv'] == 'Y' ? $GLOBALS['strYes'] : $GLOBALS['strNo']) . "\n"
2137 . ' </td>' . "\n"
2138 . ' <td>' . "\n";
2139 printf($link_edit, urlencode($current_user),
2140 urlencode($current_host),
2141 urlencode(! isset($current['Db']) || $current['Db'] == '*' ? '' : $current['Db']),
2142 '');
2143 echo '</td>' . "\n"
2144 . ' </tr>' . "\n";
2146 if (empty($row) && empty($row1) && empty($row2)) {
2147 break;
2149 $odd_row = ! $odd_row;
2151 } else {
2152 echo ' <tr class="odd">' . "\n"
2153 . ' <td colspan="6">' . "\n"
2154 . ' ' . $GLOBALS['strNoUsersFound'] . "\n"
2155 . ' </td>' . "\n"
2156 . ' </tr>' . "\n";
2158 echo '</tbody>' . "\n"
2159 . '</table>' . "\n";
2160 } // end if (empty($adduser) && empty($checkprivs)) ... elseif ... else ...
2164 * Displays the footer
2166 echo "\n\n";
2167 require_once './libraries/footer.inc.php';