cache table information
[phpmyadmin.git] / server_privileges.php
blob686461d0840e83e836cca4b8955edebea21c2ddc
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 $GLOBALS['js_include'][] = 'server_privileges.js';
17 $GLOBALS['js_include'][] = 'functions.js';
18 require './libraries/server_common.inc.php';
21 /**
22 * Checks if a dropdown box has been used for selecting a database / table
24 if (PMA_isValid($_REQUEST['pred_tablename'])) {
25 $tablename = $_REQUEST['pred_tablename'];
26 unset($pred_tablename);
27 } elseif (PMA_isValid($_REQUEST['tablename'])) {
28 $tablename = $_REQUEST['tablename'];
29 } else {
30 unset($tablename);
33 if (PMA_isValid($_REQUEST['pred_dbname'])) {
34 $dbname = $_REQUEST['pred_dbname'];
35 unset($pred_dbname);
36 } elseif (PMA_isValid($_REQUEST['dbname'])) {
37 $dbname = $_REQUEST['dbname'];
38 } else {
39 unset($dbname);
40 unset($tablename);
43 if (isset($dbname)) {
44 $db_and_table = PMA_backquote(PMA_unescape_mysql_wildcards($dbname)) . '.';
45 if (isset($tablename)) {
46 $db_and_table .= PMA_backquote($tablename);
47 } else {
48 $db_and_table .= '*';
50 } else {
51 $db_and_table = '*.*';
54 // check if given $dbanem is a wildcard or not
55 if (isset($dbname)) {
56 //if (preg_match('/\\\\(?:_|%)/i', $dbname)) {
57 if (preg_match('/(?<!\\\\)(?:_|%)/i', $dbname)) {
58 $dbname_is_wildcard = true;
59 } else {
60 $dbname_is_wildcard = false;
64 /**
65 * Checks if the user is allowed to do what he tries to...
67 if (!$is_superuser) {
68 require './libraries/server_links.inc.php';
69 echo '<h2>' . "\n"
70 . PMA_getIcon('b_usrlist.png')
71 . $GLOBALS['strPrivileges'] . "\n"
72 . '</h2>' . "\n";
73 PMA_Message::error('strNoPrivileges')->display();
74 require_once './libraries/footer.inc.php';
77 /**
78 * Generates a condition on the user name
80 * @param string the user's initial
81 * @return string the generated condition
83 function PMA_RangeOfUsers($initial = '')
85 // strtolower() is used because the User field
86 // might be BINARY, so LIKE would be case sensitive
87 if (!empty($initial)) {
88 $ret = " WHERE `User` LIKE '" . $initial . "%'"
89 . " OR `User` LIKE '" . strtolower($initial) . "%'";
90 } else {
91 $ret = '';
93 return $ret;
94 } // end function
96 /**
97 * Extracts the privilege information of a priv table row
99 * @param array $row the row
100 * @param boolean $enableHTML add <dfn> tag with tooltips
102 * @global ressource $user_link the database connection
104 * @return array
106 function PMA_extractPrivInfo($row = '', $enableHTML = FALSE)
108 $grants = array(
109 array('Select_priv', 'SELECT', $GLOBALS['strPrivDescSelect']),
110 array('Insert_priv', 'INSERT', $GLOBALS['strPrivDescInsert']),
111 array('Update_priv', 'UPDATE', $GLOBALS['strPrivDescUpdate']),
112 array('Delete_priv', 'DELETE', $GLOBALS['strPrivDescDelete']),
113 array('Create_priv', 'CREATE', $GLOBALS['strPrivDescCreateDb']),
114 array('Drop_priv', 'DROP', $GLOBALS['strPrivDescDropDb']),
115 array('Reload_priv', 'RELOAD', $GLOBALS['strPrivDescReload']),
116 array('Shutdown_priv', 'SHUTDOWN', $GLOBALS['strPrivDescShutdown']),
117 array('Process_priv', 'PROCESS', $GLOBALS['strPrivDescProcess']),
118 array('File_priv', 'FILE', $GLOBALS['strPrivDescFile']),
119 array('References_priv', 'REFERENCES', $GLOBALS['strPrivDescReferences']),
120 array('Index_priv', 'INDEX', $GLOBALS['strPrivDescIndex']),
121 array('Alter_priv', 'ALTER', $GLOBALS['strPrivDescAlter']),
122 array('Show_db_priv', 'SHOW DATABASES', $GLOBALS['strPrivDescShowDb']),
123 array('Super_priv', 'SUPER', $GLOBALS['strPrivDescSuper']),
124 array('Create_tmp_table_priv', 'CREATE TEMPORARY TABLES', $GLOBALS['strPrivDescCreateTmpTable']),
125 array('Lock_tables_priv', 'LOCK TABLES', $GLOBALS['strPrivDescLockTables']),
126 array('Repl_slave_priv', 'REPLICATION SLAVE', $GLOBALS['strPrivDescReplSlave']),
127 array('Repl_client_priv', 'REPLICATION CLIENT', $GLOBALS['strPrivDescReplClient']),
128 array('Create_view_priv', 'CREATE VIEW', $GLOBALS['strPrivDescCreateView']),
129 array('Event_priv', 'EVENT', $GLOBALS['strPrivDescEvent']),
130 array('Trigger_priv', 'TRIGGER', $GLOBALS['strPrivDescTrigger']),
131 // for table privs:
132 array('Create View_priv', 'CREATE VIEW', $GLOBALS['strPrivDescCreateView']),
133 array('Show_view_priv', 'SHOW VIEW', $GLOBALS['strPrivDescShowView']),
134 // for table privs:
135 array('Show view_priv', 'SHOW VIEW', $GLOBALS['strPrivDescShowView']),
136 array('Create_routine_priv', 'CREATE ROUTINE', $GLOBALS['strPrivDescCreateRoutine']),
137 array('Alter_routine_priv', 'ALTER ROUTINE', $GLOBALS['strPrivDescAlterRoutine']),
138 array('Create_user_priv', 'CREATE USER', $GLOBALS['strPrivDescCreateUser']),
139 array('Execute_priv', 'EXECUTE', $GLOBALS['strPrivDescExecute5']),
142 if (!empty($row) && isset($row['Table_priv'])) {
143 $row1 = PMA_DBI_fetch_single_row(
144 'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';',
145 'ASSOC', $GLOBALS['userlink']);
146 $av_grants = explode('\',\'', substr($row1['Type'], 5, strlen($row1['Type']) - 7));
147 unset($row1);
148 $users_grants = explode(',', $row['Table_priv']);
149 foreach ($av_grants as $current_grant) {
150 $row[$current_grant . '_priv'] = in_array($current_grant, $users_grants) ? 'Y' : 'N';
152 unset($current_grant);
153 unset($av_grants);
154 unset($users_grants);
156 $privs = array();
157 $allPrivileges = TRUE;
158 foreach ($grants as $current_grant) {
159 if ((!empty($row) && isset($row[$current_grant[0]]))
160 || (empty($row) && isset($GLOBALS[$current_grant[0]]))) {
161 if ((!empty($row) && $row[$current_grant[0]] == 'Y')
162 || (empty($row)
163 && ($GLOBALS[$current_grant[0]] == 'Y'
164 || (is_array($GLOBALS[$current_grant[0]])
165 && count($GLOBALS[$current_grant[0]]) == $GLOBALS['column_count']
166 && empty($GLOBALS[$current_grant[0] . '_none'])))))
168 if ($enableHTML) {
169 $privs[] = '<dfn title="' . $current_grant[2] . '">' . $current_grant[1] . '</dfn>';
170 } else {
171 $privs[] = $current_grant[1];
173 } elseif (!empty($GLOBALS[$current_grant[0]])
174 && is_array($GLOBALS[$current_grant[0]])
175 && empty($GLOBALS[$current_grant[0] . '_none'])) {
176 if ($enableHTML) {
177 $priv_string = '<dfn title="' . $current_grant[2] . '">' . $current_grant[1] . '</dfn>';
178 } else {
179 $priv_string = $current_grant[1];
181 $privs[] = $priv_string . ' (`' . join('`, `', $GLOBALS[$current_grant[0]]) . '`)';
182 } else {
183 $allPrivileges = FALSE;
187 if (empty($privs)) {
188 if ($enableHTML) {
189 $privs[] = '<dfn title="' . $GLOBALS['strPrivDescUsage'] . '">USAGE</dfn>';
190 } else {
191 $privs[] = 'USAGE';
193 } elseif ($allPrivileges && (!isset($GLOBALS['grant_count']) || count($privs) == $GLOBALS['grant_count'])) {
194 if ($enableHTML) {
195 $privs = array('<dfn title="' . $GLOBALS['strPrivDescAllPrivileges'] . '">ALL PRIVILEGES</dfn>');
196 } else {
197 $privs = array('ALL PRIVILEGES');
200 return $privs;
201 } // end of the 'PMA_extractPrivInfo()' function
204 * Displays on which column(s) a table-specific privilege is granted
206 function PMA_display_column_privs($columns, $row, $name_for_select,
207 $priv_for_header, $name, $name_for_dfn, $name_for_current)
209 echo ' <div class="item" id="div_item_' . $name . '">' . "\n"
210 . ' <label for="select_' . $name . '_priv">' . "\n"
211 . ' <tt><dfn title="' . $name_for_dfn . '">'
212 . $priv_for_header . '</dfn></tt>' . "\n"
213 . ' </label><br />' . "\n"
214 . ' <select id="select_' . $name . '_priv" name="'
215 . $name_for_select . '[]" multiple="multiple" size="8">' . "\n";
217 foreach ($columns as $current_column => $current_column_privileges) {
218 echo ' <option value="' . htmlspecialchars($current_column) . '"';
219 if ($row[$name_for_select] == 'Y' || $current_column_privileges[$name_for_current]) {
220 echo ' selected="selected"';
222 echo '>' . htmlspecialchars($current_column) . '</option>' . "\n";
225 echo ' </select>' . "\n"
226 . ' <i>' . $GLOBALS['strOr'] . '</i>' . "\n"
227 . ' <label for="checkbox_' . $name_for_select
228 . '_none"><input type="checkbox"'
229 . (empty($GLOBALS['checkall']) ? '' : ' checked="checked"')
230 . ' name="' . $name_for_select . '_none" id="checkbox_'
231 . $name_for_select . '_none" title="' . $GLOBALS['strNone'] . '" />'
232 . $GLOBALS['strNone'] . '</label>' . "\n"
233 . ' </div>' . "\n";
234 } // end function
238 * Displays the privileges form table
240 * @param string $db the database
241 * @param string $table the table
242 * @param boolean $submit wheather to display the submit button or not
243 * @global array $cfg the phpMyAdmin configuration
244 * @global ressource $user_link the database connection
246 * @return void
248 function PMA_displayPrivTable($db = '*', $table = '*', $submit = TRUE)
250 if ($db == '*') {
251 $table = '*';
254 if (isset($GLOBALS['username'])) {
255 $username = $GLOBALS['username'];
256 $hostname = $GLOBALS['hostname'];
257 if ($db == '*') {
258 $sql_query =
259 "SELECT * FROM `mysql`.`user`"
260 ." WHERE `User` = '" . PMA_sqlAddslashes($username) . "'"
261 ." AND `Host` = '" . PMA_sqlAddslashes($hostname) . "';";
262 } elseif ($table == '*') {
263 $sql_query =
264 "SELECT * FROM `mysql`.`db`"
265 ." WHERE `User` = '" . PMA_sqlAddslashes($username) . "'"
266 ." AND `Host` = '" . PMA_sqlAddslashes($hostname) . "'"
267 ." AND '" . PMA_unescape_mysql_wildcards($db) . "'"
268 ." LIKE `Db`;";
269 } else {
270 $sql_query =
271 "SELECT `Table_priv`"
272 ." FROM `mysql`.`tables_priv`"
273 ." WHERE `User` = '" . PMA_sqlAddslashes($username) . "'"
274 ." AND `Host` = '" . PMA_sqlAddslashes($hostname) . "'"
275 ." AND `Db` = '" . PMA_unescape_mysql_wildcards($db) . "'"
276 ." AND `Table_name` = '" . PMA_sqlAddslashes($table) . "';";
278 $row = PMA_DBI_fetch_single_row($sql_query);
280 if (empty($row)) {
281 if ($table == '*') {
282 if ($db == '*') {
283 $sql_query = 'SHOW COLUMNS FROM `mysql`.`user`;';
284 } elseif ($table == '*') {
285 $sql_query = 'SHOW COLUMNS FROM `mysql`.`db`;';
287 $res = PMA_DBI_query($sql_query);
288 while ($row1 = PMA_DBI_fetch_row($res)) {
289 if (substr($row1[0], 0, 4) == 'max_') {
290 $row[$row1[0]] = 0;
291 } else {
292 $row[$row1[0]] = 'N';
295 PMA_DBI_free_result($res);
296 } else {
297 $row = array('Table_priv' => '');
300 if (isset($row['Table_priv'])) {
301 $row1 = PMA_DBI_fetch_single_row(
302 'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';',
303 'ASSOC', $GLOBALS['userlink']);
304 // note: in MySQL 5.0.3 we get "Create View', 'Show view';
305 // the View for Create is spelled with uppercase V
306 // the view for Show is spelled with lowercase v
307 // and there is a space between the words
309 $av_grants = explode('\',\'', substr($row1['Type'], strpos($row1['Type'], '(') + 2, strpos($row1['Type'], ')') - strpos($row1['Type'], '(') - 3));
310 unset($row1);
311 $users_grants = explode(',', $row['Table_priv']);
313 foreach ($av_grants as $current_grant) {
314 $row[$current_grant . '_priv'] = in_array($current_grant, $users_grants) ? 'Y' : 'N';
316 unset($row['Table_priv'], $current_grant, $av_grants, $users_grants);
318 // get collumns
319 $res = PMA_DBI_try_query('SHOW COLUMNS FROM `' . PMA_unescape_mysql_wildcards($db) . '`.`' . $table . '`;');
320 $columns = array();
321 if ($res) {
322 while ($row1 = PMA_DBI_fetch_row($res)) {
323 $columns[$row1[0]] = array(
324 'Select' => FALSE,
325 'Insert' => FALSE,
326 'Update' => FALSE,
327 'References' => FALSE
330 PMA_DBI_free_result($res);
332 unset($res, $row1);
334 // t a b l e - s p e c i f i c p r i v i l e g e s
335 if (! empty($columns)) {
336 $res = PMA_DBI_query(
337 'SELECT `Column_name`, `Column_priv`'
338 .' FROM `mysql`.`columns_priv`'
339 .' WHERE `User`'
340 .' = \'' . PMA_sqlAddslashes($username) . "'"
341 .' AND `Host`'
342 .' = \'' . PMA_sqlAddslashes($hostname) . "'"
343 .' AND `Db`'
344 .' = \'' . PMA_unescape_mysql_wildcards($db) . "'"
345 .' AND `Table_name`'
346 .' = \'' . PMA_sqlAddslashes($table) . '\';');
348 while ($row1 = PMA_DBI_fetch_row($res)) {
349 $row1[1] = explode(',', $row1[1]);
350 foreach ($row1[1] as $current) {
351 $columns[$row1[0]][$current] = TRUE;
354 PMA_DBI_free_result($res);
355 unset($res, $row1, $current);
357 echo '<input type="hidden" name="grant_count" value="' . count($row) . '" />' . "\n"
358 . '<input type="hidden" name="column_count" value="' . count($columns) . '" />' . "\n"
359 . '<fieldset id="fieldset_user_priv">' . "\n"
360 . ' <legend>' . $GLOBALS['strTblPrivileges']
361 . PMA_showHint($GLOBALS['strEnglishPrivileges'])
362 . '</legend>' . "\n";
366 // privs that are attached to a specific column
367 PMA_display_column_privs($columns, $row, 'Select_priv',
368 'SELECT', 'select', $GLOBALS['strPrivDescSelect'], 'Select');
370 PMA_display_column_privs($columns, $row, 'Insert_priv',
371 'INSERT', 'insert', $GLOBALS['strPrivDescInsert'], 'Insert');
373 PMA_display_column_privs($columns, $row, 'Update_priv',
374 'UPDATE', 'update', $GLOBALS['strPrivDescUpdate'], 'Update');
376 PMA_display_column_privs($columns, $row, 'References_priv',
377 'REFERENCES', 'references', $GLOBALS['strPrivDescReferences'], 'References');
379 // privs that are not attached to a specific column
381 echo ' <div class="item">' . "\n";
382 foreach ($row as $current_grant => $current_grant_value) {
383 if (in_array(substr($current_grant, 0, (strlen($current_grant) - 5)),
384 array('Select', 'Insert', 'Update', 'References'))) {
385 continue;
387 // make a substitution to match the messages variables;
388 // also we must substitute the grant we get, because we can't generate
389 // a form variable containing blanks (those would get changed to
390 // an underscore when receiving the POST)
391 if ($current_grant == 'Create View_priv') {
392 $tmp_current_grant = 'CreateView_priv';
393 $current_grant = 'Create_view_priv';
394 } elseif ($current_grant == 'Show view_priv') {
395 $tmp_current_grant = 'ShowView_priv';
396 $current_grant = 'Show_view_priv';
397 } else {
398 $tmp_current_grant = $current_grant;
401 echo ' <div class="item">' . "\n"
402 . ' <input type="checkbox"'
403 . (empty($GLOBALS['checkall']) ? '' : ' checked="checked"')
404 . ' name="' . $current_grant . '" id="checkbox_' . $current_grant
405 . '" value="Y" '
406 . ($current_grant_value == 'Y' ? 'checked="checked" ' : '')
407 . 'title="';
409 echo (isset($GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))])
410 ? $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))]
411 : $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5)) . 'Tbl']) . '"/>' . "\n";
413 echo ' <label for="checkbox_' . $current_grant
414 . '"><tt><dfn title="'
415 . (isset($GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))])
416 ? $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))]
417 : $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5)) . 'Tbl'])
418 . '">' . strtoupper(substr($current_grant, 0, strlen($current_grant) - 5)) . '</dfn></tt></label>' . "\n"
419 . ' </div>' . "\n";
420 } // end foreach ()
422 echo ' </div>' . "\n";
423 // for Safari 2.0.2
424 echo ' <div class="clearfloat"></div>' . "\n";
426 } else {
428 // g l o b a l o r d b - s p e c i f i c
430 // d a t a
431 $privTable[0] = array(
432 array('Select', 'SELECT', $GLOBALS['strPrivDescSelect']),
433 array('Insert', 'INSERT', $GLOBALS['strPrivDescInsert']),
434 array('Update', 'UPDATE', $GLOBALS['strPrivDescUpdate']),
435 array('Delete', 'DELETE', $GLOBALS['strPrivDescDelete'])
437 if ($db == '*') {
438 $privTable[0][] = array('File', 'FILE', $GLOBALS['strPrivDescFile']);
441 // s t r u c t u r e
442 $privTable[1] = array(
443 array('Create', 'CREATE', ($table == '*' ? $GLOBALS['strPrivDescCreateDb'] : $GLOBALS['strPrivDescCreateTbl'])),
444 array('Alter', 'ALTER', $GLOBALS['strPrivDescAlter']),
445 array('Index', 'INDEX', $GLOBALS['strPrivDescIndex']),
446 array('Drop', 'DROP', ($table == '*' ? $GLOBALS['strPrivDescDropDb'] : $GLOBALS['strPrivDescDropTbl'])),
447 array('Create_tmp_table', 'CREATE TEMPORARY TABLES', $GLOBALS['strPrivDescCreateTmpTable']),
448 array('Show_view', 'SHOW VIEW', $GLOBALS['strPrivDescShowView']),
449 array('Create_routine', 'CREATE ROUTINE', $GLOBALS['strPrivDescCreateRoutine']),
450 array('Alter_routine', 'ALTER ROUTINE', $GLOBALS['strPrivDescAlterRoutine']),
451 array('Execute', 'EXECUTE', $GLOBALS['strPrivDescExecute5']),
453 // this one is for a db-specific priv: Create_view_priv
454 if (isset($row['Create_view_priv'])) {
455 $privTable[1][] = array('Create_view', 'CREATE VIEW', $GLOBALS['strPrivDescCreateView']);
457 // this one is for a table-specific priv: Create View_priv
458 if (isset($row['Create View_priv'])) {
459 $privTable[1][] = array('Create View', 'CREATE VIEW', $GLOBALS['strPrivDescCreateView']);
461 if (isset($row['Event_priv'])) {
462 // MySQL 5.1.6
463 $privTable[1][] = array('Event', 'EVENT', $GLOBALS['strPrivDescEvent']);
464 $privTable[1][] = array('Trigger', 'TRIGGER', $GLOBALS['strPrivDescTrigger']);
467 // a d m i n i s t r a t i o n
468 $privTable[2] = array(
469 array('Grant', 'GRANT', $GLOBALS['strPrivDescGrant']),
471 if ($db == '*') {
472 $privTable[2][] = array('Super', 'SUPER', $GLOBALS['strPrivDescSuper']);
473 $privTable[2][] = array('Process', 'PROCESS', $GLOBALS['strPrivDescProcess']);
474 $privTable[2][] = array('Reload', 'RELOAD', $GLOBALS['strPrivDescReload']);
475 $privTable[2][] = array('Shutdown', 'SHUTDOWN', $GLOBALS['strPrivDescShutdown']);
476 $privTable[2][] = array('Show_db', 'SHOW DATABASES', $GLOBALS['strPrivDescShowDb']);
478 $privTable[2][] = array('Lock_tables', 'LOCK TABLES', $GLOBALS['strPrivDescLockTables']);
479 $privTable[2][] = array('References', 'REFERENCES', $GLOBALS['strPrivDescReferences']);
480 if ($db == '*') {
481 $privTable[2][] = array('Repl_client', 'REPLICATION CLIENT', $GLOBALS['strPrivDescReplClient']);
482 $privTable[2][] = array('Repl_slave', 'REPLICATION SLAVE', $GLOBALS['strPrivDescReplSlave']);
483 $privTable[2][] = array('Create_user', 'CREATE USER', $GLOBALS['strPrivDescCreateUser']);
485 echo '<input type="hidden" name="grant_count" value="'
486 . (count($privTable[0]) + count($privTable[1]) + count($privTable[2]) - (isset($row['Grant_priv']) ? 1 : 0))
487 . '" />' . "\n"
488 . '<fieldset id="fieldset_user_global_rights">' . "\n"
489 . ' <legend>' . "\n"
490 . ' '
491 . ($db == '*'
492 ? $GLOBALS['strGlobalPrivileges']
493 : ($table == '*'
494 ? $GLOBALS['strDbPrivileges']
495 : $GLOBALS['strTblPrivileges'])) . "\n"
496 . ' (<a href="server_privileges.php?'
497 . $GLOBALS['url_query'] . '&amp;checkall=1" onclick="setCheckboxes(\'usersForm\', true); return false;">'
498 . $GLOBALS['strCheckAll'] . '</a> /' . "\n"
499 . ' <a href="server_privileges.php?'
500 . $GLOBALS['url_query'] . '" onclick="setCheckboxes(\'usersForm\', false); return false;">'
501 . $GLOBALS['strUncheckAll'] . '</a>)' . "\n"
502 . ' </legend>' . "\n"
503 . ' <p><small><i>' . $GLOBALS['strEnglishPrivileges'] . '</i></small></p>' . "\n"
504 . ' <fieldset>' . "\n"
505 . ' <legend>' . $GLOBALS['strData'] . '</legend>' . "\n";
506 foreach ($privTable[0] as $priv)
508 echo ' <div class="item">' . "\n"
509 . ' <input type="checkbox"'
510 . (empty($GLOBALS['checkall']) ? '' : ' checked="checked"')
511 . ' name="' . $priv[0] . '_priv" id="checkbox_' . $priv[0]
512 . '_priv" value="Y" '
513 . ($row[$priv[0] . '_priv'] == 'Y' ? 'checked="checked" ' : '')
514 . 'title="' . $priv[2] . '"/>' . "\n"
515 . ' <label for="checkbox_' . $priv[0]
516 . '_priv"><tt><dfn title="' . $priv[2] . '">' . $priv[1]
517 . '</dfn></tt></label>' . "\n"
518 . ' </div>' . "\n";
520 echo ' </fieldset>' . "\n"
521 . ' <fieldset>' . "\n"
522 . ' <legend>' . $GLOBALS['strStructure'] . '</legend>' . "\n";
523 foreach ($privTable[1] as $priv)
525 echo ' <div class="item">' . "\n"
526 . ' <input type="checkbox"'
527 . (empty($GLOBALS['checkall']) ? '' : ' checked="checked"')
528 . ' name="' . $priv[0] . '_priv" id="checkbox_' . $priv[0]
529 . '_priv" value="Y" '
530 . ($row[$priv[0] . '_priv'] == 'Y' ? 'checked="checked" ' : '')
531 . 'title="' . $priv[2] . '"/>' . "\n"
532 . ' <label for="checkbox_' . $priv[0]
533 . '_priv"><tt><dfn title="' . $priv[2] . '">' . $priv[1]
534 . '</dfn></tt></label>' . "\n"
535 . ' </div>' . "\n";
537 echo ' </fieldset>' . "\n"
538 . ' <fieldset>' . "\n"
539 . ' <legend>' . $GLOBALS['strAdministration'] . '</legend>' . "\n";
540 foreach ($privTable[2] as $priv)
542 echo ' <div class="item">' . "\n"
543 . ' <input type="checkbox"'
544 . (empty($GLOBALS['checkall']) ? '' : ' checked="checked"')
545 . ' name="' . $priv[0] . '_priv" id="checkbox_' . $priv[0]
546 . '_priv" value="Y" '
547 . ($row[$priv[0] . '_priv'] == 'Y' ? 'checked="checked" ' : '')
548 . 'title="' . $priv[2] . '"/>' . "\n"
549 . ' <label for="checkbox_' . $priv[0]
550 . '_priv"><tt><dfn title="' . $priv[2] . '">' . $priv[1]
551 . '</dfn></tt></label>' . "\n"
552 . ' </div>' . "\n";
555 echo ' </fieldset>' . "\n";
556 // The "Resource limits" box is not displayed for db-specific privs
557 if ($db == '*') {
558 echo ' <fieldset>' . "\n"
559 . ' <legend>' . $GLOBALS['strResourceLimits'] . '</legend>' . "\n"
560 . ' <p><small><i>' . $GLOBALS['strZeroRemovesTheLimit'] . '</i></small></p>' . "\n"
561 . ' <div class="item">' . "\n"
562 . ' <label for="text_max_questions"><tt><dfn title="'
563 . $GLOBALS['strPrivDescMaxQuestions'] . '">MAX QUERIES PER HOUR</dfn></tt></label>' . "\n"
564 . ' <input type="text" name="max_questions" id="text_max_questions" value="'
565 . $row['max_questions'] . '" size="11" maxlength="11" title="' . $GLOBALS['strPrivDescMaxQuestions'] . '" />' . "\n"
566 . ' </div>' . "\n"
567 . ' <div class="item">' . "\n"
568 . ' <label for="text_max_updates"><tt><dfn title="'
569 . $GLOBALS['strPrivDescMaxUpdates'] . '">MAX UPDATES PER HOUR</dfn></tt></label>' . "\n"
570 . ' <input type="text" name="max_updates" id="text_max_updates" value="'
571 . $row['max_updates'] . '" size="11" maxlength="11" title="' . $GLOBALS['strPrivDescMaxUpdates'] . '" />' . "\n"
572 . ' </div>' . "\n"
573 . ' <div class="item">' . "\n"
574 . ' <label for="text_max_connections"><tt><dfn title="'
575 . $GLOBALS['strPrivDescMaxConnections'] . '">MAX CONNECTIONS PER HOUR</dfn></tt></label>' . "\n"
576 . ' <input type="text" name="max_connections" id="text_max_connections" value="'
577 . $row['max_connections'] . '" size="11" maxlength="11" title="' . $GLOBALS['strPrivDescMaxConnections'] . '" />' . "\n"
578 . ' </div>' . "\n"
579 . ' <div class="item">' . "\n"
580 . ' <label for="text_max_user_connections"><tt><dfn title="'
581 . $GLOBALS['strPrivDescMaxUserConnections'] . '">MAX USER_CONNECTIONS</dfn></tt></label>' . "\n"
582 . ' <input type="text" name="max_user_connections" id="text_max_user_connections" value="'
583 . $row['max_user_connections'] . '" size="11" maxlength="11" title="' . $GLOBALS['strPrivDescMaxUserConnections'] . '" />' . "\n"
584 . ' </div>' . "\n"
585 . ' </fieldset>' . "\n";
587 // for Safari 2.0.2
588 echo ' <div class="clearfloat"></div>' . "\n";
590 echo '</fieldset>' . "\n";
591 if ($submit) {
592 echo '<fieldset id="fieldset_user_privtable_footer" class="tblFooters">' . "\n"
593 . ' <input type="submit" name="update_privs" value="' . $GLOBALS['strGo'] . '" />' . "\n"
594 . '</fieldset>' . "\n";
596 } // end of the 'PMA_displayPrivTable()' function
600 * Displays the fields used by the "new user" form as well as the
601 * "change login information / copy user" form.
603 * @param string $mode are we creating a new user or are we just
604 * changing one? (allowed values: 'new', 'change')
605 * @global array $cfg the phpMyAdmin configuration
606 * @global ressource $user_link the database connection
608 * @return void
610 function PMA_displayLoginInformationFields($mode = 'new')
612 // Get user/host name lengths
613 $fields_info = PMA_DBI_get_fields('mysql', 'user');
614 $username_length = 16;
615 $hostname_length = 41;
616 foreach ($fields_info as $key => $val) {
617 if ($val['Field'] == 'User') {
618 strtok($val['Type'], '()');
619 $v = strtok('()');
620 if (is_int($v)) {
621 $username_length = $v;
623 } elseif ($val['Field'] == 'Host') {
624 strtok($val['Type'], '()');
625 $v = strtok('()');
626 if (is_int($v)) {
627 $hostname_length = $v;
631 unset($fields_info);
633 if (isset($GLOBALS['username']) && strlen($GLOBALS['username']) === 0) {
634 $GLOBALS['pred_username'] = 'any';
636 echo '<fieldset id="fieldset_add_user_login">' . "\n"
637 . '<legend>' . $GLOBALS['strLoginInformation'] . '</legend>' . "\n"
638 . '<div class="item">' . "\n"
639 . '<label for="select_pred_username">' . "\n"
640 . ' ' . $GLOBALS['strUserName'] . ':' . "\n"
641 . '</label>' . "\n"
642 . '<span class="options">' . "\n"
643 . ' <select name="pred_username" id="select_pred_username" title="' . $GLOBALS['strUserName'] . '"' . "\n"
644 . ' onchange="if (this.value == \'any\') { username.value = \'\'; } else if (this.value == \'userdefined\') { username.focus(); username.select(); }">' . "\n"
645 . ' <option value="any"' . ((isset($GLOBALS['pred_username']) && $GLOBALS['pred_username'] == 'any') ? ' selected="selected"' : '') . '>' . $GLOBALS['strAnyUser'] . '</option>' . "\n"
646 . ' <option value="userdefined"' . ((!isset($GLOBALS['pred_username']) || $GLOBALS['pred_username'] == 'userdefined') ? ' selected="selected"' : '') . '>' . $GLOBALS['strUseTextField'] . ':</option>' . "\n"
647 . ' </select>' . "\n"
648 . '</span>' . "\n"
649 . '<input type="text" name="username" maxlength="'
650 . $username_length . '" title="' . $GLOBALS['strUserName'] . '"'
651 . (empty($GLOBALS['username'])
652 ? ''
653 : ' value="' . (isset($GLOBALS['new_username'])
654 ? $GLOBALS['new_username']
655 : $GLOBALS['username']) . '"')
656 . ' onchange="pred_username.value = \'userdefined\';" />' . "\n"
657 . '</div>' . "\n"
658 . '<div class="item">' . "\n"
659 . '<label for="select_pred_hostname">' . "\n"
660 . ' ' . $GLOBALS['strHost'] . ':' . "\n"
661 . '</label>' . "\n"
662 . '<span class="options">' . "\n"
663 . ' <select name="pred_hostname" id="select_pred_hostname" title="' . $GLOBALS['strHost'] . '"' . "\n";
664 $_current_user = PMA_DBI_fetch_value('SELECT USER();');
665 if (! empty($_current_user)) {
666 $thishost = str_replace("'", '', substr($_current_user, (strrpos($_current_user, '@') + 1)));
667 if ($thishost == 'localhost' || $thishost == '127.0.0.1') {
668 unset($thishost);
671 echo ' onchange="if (this.value == \'any\') { hostname.value = \'%\'; } else if (this.value == \'localhost\') { hostname.value = \'localhost\'; } '
672 . (empty($thishost) ? '' : 'else if (this.value == \'thishost\') { hostname.value = \'' . addslashes(htmlspecialchars($thishost)) . '\'; } ')
673 . 'else if (this.value == \'hosttable\') { hostname.value = \'\'; } else if (this.value == \'userdefined\') { hostname.focus(); hostname.select(); }">' . "\n";
674 unset($_current_user);
676 // when we start editing a user, $GLOBALS['pred_hostname'] is not defined
677 if (!isset($GLOBALS['pred_hostname']) && isset($GLOBALS['hostname'])) {
678 switch (strtolower($GLOBALS['hostname'])) {
679 case 'localhost':
680 case '127.0.0.1':
681 $GLOBALS['pred_hostname'] = 'localhost';
682 break;
683 case '%':
684 $GLOBALS['pred_hostname'] = 'any';
685 break;
686 default:
687 $GLOBALS['pred_hostname'] = 'userdefined';
688 break;
691 echo ' <option value="any"'
692 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'any')
693 ? ' selected="selected"' : '') . '>' . $GLOBALS['strAnyHost']
694 . '</option>' . "\n"
695 . ' <option value="localhost"'
696 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'localhost')
697 ? ' selected="selected"' : '') . '>' . $GLOBALS['strLocalhost']
698 . '</option>' . "\n";
699 if (!empty($thishost)) {
700 echo ' <option value="thishost"'
701 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'thishost')
702 ? ' selected="selected"' : '') . '>' . $GLOBALS['strThisHost']
703 . '</option>' . "\n";
705 unset($thishost);
706 echo ' <option value="hosttable"'
707 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'hosttable')
708 ? ' selected="selected"' : '') . '>' . $GLOBALS['strUseHostTable']
709 . '</option>' . "\n"
710 . ' <option value="userdefined"'
711 . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'userdefined')
712 ? ' selected="selected"' : '')
713 . '>' . $GLOBALS['strUseTextField'] . ':</option>' . "\n"
714 . ' </select>' . "\n"
715 . '</span>' . "\n"
716 . '<input type="text" name="hostname" maxlength="'
717 . $hostname_length . '" value="'
718 . (isset($GLOBALS['hostname']) ? $GLOBALS['hostname'] : '')
719 . '" title="' . $GLOBALS['strHost']
720 . '" onchange="pred_hostname.value = \'userdefined\';" />' . "\n"
721 . '</div>' . "\n"
722 . '<div class="item">' . "\n"
723 . '<label for="select_pred_password">' . "\n"
724 . ' ' . $GLOBALS['strPassword'] . ':' . "\n"
725 . '</label>' . "\n"
726 . '<span class="options">' . "\n"
727 . ' <select name="pred_password" id="select_pred_password" title="'
728 . $GLOBALS['strPassword'] . '"' . "\n"
729 . ' onchange="if (this.value == \'none\') { pma_pw.value = \'\'; pma_pw2.value = \'\'; } else if (this.value == \'userdefined\') { pma_pw.focus(); pma_pw.select(); }">' . "\n"
730 . ($mode == 'change' ? ' <option value="keep" selected="selected">' . $GLOBALS['strKeepPass'] . '</option>' . "\n" : '')
731 . ' <option value="none"';
732 if (isset($GLOBALS['username']) && $mode != 'change') {
733 echo ' selected="selected"';
735 echo '>' . $GLOBALS['strNoPassword'] . '</option>' . "\n"
736 . ' <option value="userdefined"' . (isset($GLOBALS['username']) ? '' : ' selected="selected"') . '>' . $GLOBALS['strUseTextField'] . ':</option>' . "\n"
737 . ' </select>' . "\n"
738 . '</span>' . "\n"
739 . '<input type="password" id="text_pma_pw" name="pma_pw" title="' . $GLOBALS['strPassword'] . '" onchange="pred_password.value = \'userdefined\';" />' . "\n"
740 . '</div>' . "\n"
741 . '<div class="item">' . "\n"
742 . '<label for="text_pma_pw2">' . "\n"
743 . ' ' . $GLOBALS['strReType'] . ':' . "\n"
744 . '</label>' . "\n"
745 . '<span class="options">&nbsp;</span>' . "\n"
746 . '<input type="password" name="pma_pw2" id="text_pma_pw2" title="' . $GLOBALS['strReType'] . '" onchange="pred_password.value = \'userdefined\';" />' . "\n"
747 . '</div>' . "\n"
748 . '<div class="item">' . "\n"
749 . '<label for="button_generate_password">' . "\n"
750 . ' ' . $GLOBALS['strGeneratePassword'] . ':' . "\n"
751 . '</label>' . "\n"
752 . '<span class="options">' . "\n"
753 . ' <input type="button" id="button_generate_password" value="' . $GLOBALS['strGenerate'] . '" onclick="suggestPassword()" />' . "\n"
754 . ' <input type="button" id="button_copy_password" value="' . $GLOBALS['strCopy'] . '" onclick="suggestPasswordCopy(this.form)" />' . "\n"
755 . '</span>' . "\n"
756 . '<input type="text" name="generated_pw" id="generated_pw" />' . "\n"
757 . '</div>' . "\n"
758 . '</fieldset>' . "\n";
759 } // end of the 'PMA_displayUserAndHostFields()' function
763 * Changes / copies a user, part I
765 if (isset($_REQUEST['change_copy'])) {
766 $user_host_condition =
767 ' WHERE `User`'
768 .' = \'' . PMA_sqlAddslashes($old_username) . "'"
769 .' AND `Host`'
770 .' = \'' . $old_hostname . '\';';
771 $row = PMA_DBI_fetch_single_row('SELECT * FROM `mysql`.`user` ' . $user_host_condition);
772 if (! $row) {
773 PMA_Message::notice('strNoUsersFound')->display();
774 unset($_REQUEST['change_copy']);
775 } else {
776 extract($row, EXTR_OVERWRITE);
777 // Recent MySQL versions have the field "Password" in mysql.user,
778 // so the previous extract creates $Password but this script
779 // uses $password
780 if (!isset($password) && isset($Password)) {
781 $password = $Password;
783 $queries = array();
789 * Adds a user
790 * (Changes / copies a user, part II)
792 if (isset($_REQUEST['adduser_submit']) || isset($_REQUEST['change_copy'])) {
793 $sql_query = '';
794 if ($pred_username == 'any') {
795 $username = '';
797 switch ($pred_hostname) {
798 case 'any':
799 $hostname = '%';
800 break;
801 case 'localhost':
802 $hostname = 'localhost';
803 break;
804 case 'hosttable':
805 $hostname = '';
806 break;
807 case 'thishost':
808 $_user_name = PMA_DBI_fetch_value('SELECT USER()');
809 $hostname = substr($_user_name, (strrpos($_user_name, '@') + 1));
810 unset($_user_name);
811 break;
813 $sql = "SELECT '1' FROM `mysql`.`user`"
814 . " WHERE `User` = '" . PMA_sqlAddslashes($username) . "'"
815 . " AND `Host` = '" . PMA_sqlAddslashes($hostname) . "';";
816 if (PMA_DBI_fetch_value($sql) == 1) {
817 $message = PMA_Message::error('strUserAlreadyExists');
818 $message->addParam('[i]\'' . $username . '\'@\'' . $hostname . '\'[/i]');
819 $_REQUEST['adduser'] = true;
820 } else {
822 $create_user_real = 'CREATE USER \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\'';
824 $real_sql_query =
825 'GRANT ' . join(', ', PMA_extractPrivInfo()) . ' ON *.* TO \''
826 . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\'';
827 if ($pred_password != 'none' && $pred_password != 'keep') {
828 $sql_query = $real_sql_query . ' IDENTIFIED BY \'***\'';
829 $real_sql_query .= ' IDENTIFIED BY \'' . PMA_sqlAddslashes($pma_pw) . '\'';
830 if (isset($create_user_real)) {
831 $create_user_show = $create_user_real . ' IDENTIFIED BY \'***\'';
832 $create_user_real .= ' IDENTIFIED BY \'' . PMA_sqlAddslashes($pma_pw) . '\'';
834 } else {
835 if ($pred_password == 'keep' && !empty($password)) {
836 $real_sql_query .= ' IDENTIFIED BY PASSWORD \'' . $password . '\'';
837 if (isset($create_user_real)) {
838 $create_user_real .= ' IDENTIFIED BY PASSWORD \'' . $password . '\'';
841 $sql_query = $real_sql_query;
842 if (isset($create_user_real)) {
843 $create_user_show = $create_user_real;
847 * @todo similar code appears twice in this script
849 if ((isset($Grant_priv) && $Grant_priv == 'Y')
850 || (isset($max_questions) || isset($max_connections)
851 || isset($max_updates) || isset($max_user_connections))) {
852 $real_sql_query .= ' WITH';
853 $sql_query .= ' WITH';
854 if (isset($Grant_priv) && $Grant_priv == 'Y') {
855 $real_sql_query .= ' GRANT OPTION';
856 $sql_query .= ' GRANT OPTION';
858 if (isset($max_questions)) {
859 // avoid negative values
860 $max_questions = max(0, (int)$max_questions);
861 $real_sql_query .= ' MAX_QUERIES_PER_HOUR ' . $max_questions;
862 $sql_query .= ' MAX_QUERIES_PER_HOUR ' . $max_questions;
864 if (isset($max_connections)) {
865 $max_connections = max(0, (int)$max_connections);
866 $real_sql_query .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections;
867 $sql_query .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections;
869 if (isset($max_updates)) {
870 $max_updates = max(0, (int)$max_updates);
871 $real_sql_query .= ' MAX_UPDATES_PER_HOUR ' . $max_updates;
872 $sql_query .= ' MAX_UPDATES_PER_HOUR ' . $max_updates;
874 if (isset($max_user_connections)) {
875 $max_user_connections = max(0, (int)$max_user_connections);
876 $real_sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections;
877 $sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections;
880 if (isset($create_user_real)) {
881 $create_user_real .= ';';
882 $create_user_show .= ';';
884 $real_sql_query .= ';';
885 $sql_query .= ';';
886 if (empty($_REQUEST['change_copy'])) {
887 $_error = false;
889 if (isset($create_user_real)) {
890 if (! PMA_DBI_try_query($create_user_real)) {
891 $_error = true;
893 $sql_query = $create_user_show . $sql_query;
896 if ($_error || ! PMA_DBI_try_query($real_sql_query)) {
897 $_REQUEST['createdb'] = false;
898 $message = PMA_Message::rawError(PMA_DBI_getError());
899 } else {
900 $message = PMA_Message::success('strAddUserMessage');
903 switch (PMA_ifSetOr($_REQUEST['createdb'], '0')) {
904 case '1' :
905 $q = 'CREATE DATABASE IF NOT EXISTS '
906 . PMA_backquote(PMA_sqlAddslashes($username)) . ';';
907 $sql_query .= $q;
908 if (! PMA_DBI_try_query($q)) {
909 $message = PMA_Message::rawError(PMA_DBI_getError());
910 break;
912 $GLOBALS['reload'] = TRUE;
913 PMA_reloadNavigation();
915 $q = 'GRANT ALL PRIVILEGES ON '
916 . PMA_backquote(PMA_sqlAddslashes($username)) . '.* TO \''
917 . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\';';
918 $sql_query .= $q;
919 if (! PMA_DBI_try_query($q)) {
920 $message = PMA_Message::rawError(PMA_DBI_getError());
922 break;
923 case '2' :
924 $q = 'GRANT ALL PRIVILEGES ON '
925 . PMA_backquote(PMA_sqlAddslashes($username) . '\_%') . '.* TO \''
926 . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\';';
927 $sql_query .= $q;
928 if (! PMA_DBI_try_query($q)) {
929 $message = PMA_Message::rawError(PMA_DBI_getError());
931 break;
932 case '0' :
933 default :
934 break;
936 } else {
937 if (isset($create_user_real)) {
938 $queries[] = $create_user_real;
940 $queries[] = $real_sql_query;
941 // we put the query containing the hidden password in
942 // $queries_for_display, at the same position occupied
943 // by the real query in $queries
944 $tmp_count = count($queries);
945 if (isset($create_user_real)) {
946 $queries_for_display[$tmp_count - 2] = $create_user_show;
948 $queries_for_display[$tmp_count - 1] = $sql_query;
950 unset($res, $real_sql_query);
956 * Changes / copies a user, part III
958 if (isset($_REQUEST['change_copy'])) {
959 $user_host_condition =
960 ' WHERE `User`'
961 .' = \'' . PMA_sqlAddslashes($old_username) . "'"
962 .' AND `Host`'
963 .' = \'' . $old_hostname . '\';';
964 $res = PMA_DBI_query('SELECT * FROM `mysql`.`db`' . $user_host_condition);
965 while ($row = PMA_DBI_fetch_assoc($res)) {
966 $queries[] =
967 'GRANT ' . join(', ', PMA_extractPrivInfo($row))
968 .' ON `' . $row['Db'] . '`.*'
969 .' TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\''
970 . ($row['Grant_priv'] == 'Y' ? ' WITH GRANT OPTION;' : ';');
972 PMA_DBI_free_result($res);
973 $res = PMA_DBI_query(
974 'SELECT `Db`, `Table_name`, `Table_priv`'
975 .' FROM `mysql`.`tables_priv`' . $user_host_condition,
976 $GLOBALS['userlink'], PMA_DBI_QUERY_STORE);
977 while ($row = PMA_DBI_fetch_assoc($res)) {
979 $res2 = PMA_DBI_QUERY(
980 'SELECT `Column_name`, `Column_priv`'
981 .' FROM `mysql`.`columns_priv`'
982 .' WHERE `User`'
983 .' = \'' . PMA_sqlAddslashes($old_username) . "'"
984 .' AND `Host`'
985 .' = \'' . $old_hostname . '\''
986 .' AND `Db`'
987 .' = \'' . $row['Db'] . "'"
988 .' AND `Table_name`'
989 .' = \'' . $row['Table_name'] . "'"
990 .';',
991 null, PMA_DBI_QUERY_STORE);
993 $tmp_privs1 = PMA_extractPrivInfo($row);
994 $tmp_privs2 = array(
995 'Select' => array(),
996 'Insert' => array(),
997 'Update' => array(),
998 'References' => array()
1001 while ($row2 = PMA_DBI_fetch_assoc($res2)) {
1002 $tmp_array = explode(',', $row2['Column_priv']);
1003 if (in_array('Select', $tmp_array)) {
1004 $tmp_privs2['Select'][] = $row2['Column_name'];
1006 if (in_array('Insert', $tmp_array)) {
1007 $tmp_privs2['Insert'][] = $row2['Column_name'];
1009 if (in_array('Update', $tmp_array)) {
1010 $tmp_privs2['Update'][] = $row2['Column_name'];
1012 if (in_array('References', $tmp_array)) {
1013 $tmp_privs2['References'][] = $row2['Column_name'];
1015 unset($tmp_array);
1017 if (count($tmp_privs2['Select']) > 0 && !in_array('SELECT', $tmp_privs1)) {
1018 $tmp_privs1[] = 'SELECT (`' . join('`, `', $tmp_privs2['Select']) . '`)';
1020 if (count($tmp_privs2['Insert']) > 0 && !in_array('INSERT', $tmp_privs1)) {
1021 $tmp_privs1[] = 'INSERT (`' . join('`, `', $tmp_privs2['Insert']) . '`)';
1023 if (count($tmp_privs2['Update']) > 0 && !in_array('UPDATE', $tmp_privs1)) {
1024 $tmp_privs1[] = 'UPDATE (`' . join('`, `', $tmp_privs2['Update']) . '`)';
1026 if (count($tmp_privs2['References']) > 0 && !in_array('REFERENCES', $tmp_privs1)) {
1027 $tmp_privs1[] = 'REFERENCES (`' . join('`, `', $tmp_privs2['References']) . '`)';
1029 unset($tmp_privs2);
1030 $queries[] =
1031 'GRANT ' . join(', ', $tmp_privs1)
1032 . ' ON `' . $row['Db'] . '`.`' . $row['Table_name']
1033 . '` TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\''
1034 . (in_array('Grant', explode(',', $row['Table_priv'])) ? ' WITH GRANT OPTION;' : ';');
1040 * Updates privileges
1042 if (!empty($update_privs)) {
1043 // escaping a wildcard character in a GRANT is only accepted at the global
1044 // or database level, not at table level; this is why I remove
1045 // the escaping character
1046 // Note: in the phpMyAdmin list of Database-specific privileges,
1047 // we will have for example
1048 // test\_db SELECT (this one is for privileges on a db level)
1049 // test_db USAGE (this one is for table-specific privileges)
1051 // It looks curious but reflects the way MySQL works
1053 $sql_query0 =
1054 'REVOKE ALL PRIVILEGES ON ' . $db_and_table
1055 . ' FROM \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\';';
1056 if (!isset($Grant_priv) || $Grant_priv != 'Y') {
1057 $sql_query1 =
1058 'REVOKE GRANT OPTION ON ' . $db_and_table
1059 . ' FROM \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\';';
1060 } else {
1061 $sql_query1 = '';
1063 $sql_query2 =
1064 'GRANT ' . join(', ', PMA_extractPrivInfo())
1065 . ' ON ' . $db_and_table
1066 . ' TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\'';
1069 * @todo similar code appears twice in this script
1071 if ((isset($Grant_priv) && $Grant_priv == 'Y')
1072 || (! isset($dbname)
1073 && (isset($max_questions) || isset($max_connections)
1074 || isset($max_updates) || isset($max_user_connections))))
1076 $sql_query2 .= 'WITH';
1077 if (isset($Grant_priv) && $Grant_priv == 'Y') {
1078 $sql_query2 .= ' GRANT OPTION';
1080 if (isset($max_questions)) {
1081 $max_questions = max(0, (int)$max_questions);
1082 $sql_query2 .= ' MAX_QUERIES_PER_HOUR ' . $max_questions;
1084 if (isset($max_connections)) {
1085 $max_connections = max(0, (int)$max_connections);
1086 $sql_query2 .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections;
1088 if (isset($max_updates)) {
1089 $max_updates = max(0, (int)$max_updates);
1090 $sql_query2 .= ' MAX_UPDATES_PER_HOUR ' . $max_updates;
1092 if (isset($max_user_connections)) {
1093 $max_user_connections = max(0, (int)$max_user_connections);
1094 $sql_query2 .= ' MAX_USER_CONNECTIONS ' . $max_user_connections;
1097 $sql_query2 .= ';';
1098 if (!PMA_DBI_try_query($sql_query0)) {
1099 // this query may fail, but this does not matter :o)
1100 // a case when it can fail is when the admin does not have all
1101 // privileges: he can't do a REVOKE ALL PRIVILEGES !
1102 // so at least we display the error
1103 echo PMA_DBI_getError();
1104 $sql_query0 = '';
1106 if (isset($sql_query1) && !PMA_DBI_try_query($sql_query1)) {
1107 // this one may fail, too...
1108 $sql_query1 = '';
1110 PMA_DBI_query($sql_query2);
1111 $sql_query = $sql_query0 . ' ' . $sql_query1 . ' ' . $sql_query2;
1112 $message = PMA_Message::success('strUpdatePrivMessage');
1113 $message->addParam('\'' . $username . '\'@\'' . $hostname . '\'');
1118 * Revokes Privileges
1120 if (isset($_REQUEST['revokeall'])) {
1122 $sql_query0 =
1123 'REVOKE ALL PRIVILEGES ON ' . $db_and_table
1124 . ' FROM \'' . $username . '\'@\'' . $hostname . '\';';
1125 $sql_query1 =
1126 'REVOKE GRANT OPTION ON ' . $db_and_table
1127 . ' FROM \'' . $username . '\'@\'' . $hostname . '\';';
1128 PMA_DBI_query($sql_query0);
1129 if (!PMA_DBI_try_query($sql_query1)) {
1130 // this one may fail, too...
1131 $sql_query1 = '';
1133 $sql_query = $sql_query0 . ' ' . $sql_query1;
1134 $message = PMA_Message::success('strRevokeMessage');
1135 $message->addParam('\'' . $username . '\'@\'' . $hostname . '\'');
1136 if (! isset($tablename)) {
1137 unset($dbname);
1138 } else {
1139 unset($tablename);
1145 * Updates the password
1147 if (isset($_REQUEST['change_pw'])) {
1148 // similar logic in user_password.php
1149 $message = '';
1151 if ($nopass == 0 && isset($pma_pw) && isset($pma_pw2)) {
1152 if ($pma_pw != $pma_pw2) {
1153 $message = PMA_Message::error('strPasswordNotSame');
1154 } elseif (empty($pma_pw) || empty($pma_pw2)) {
1155 $message = PMA_Message::error('strPasswordEmpty');
1157 } // end if
1159 // here $nopass could be == 1
1160 if (empty($message)) {
1162 $hashing_function = (!empty($pw_hash) && $pw_hash == 'old' ? 'OLD_' : '')
1163 . 'PASSWORD';
1165 // in $sql_query which will be displayed, hide the password
1166 $sql_query = 'SET PASSWORD FOR \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\' = ' . (($pma_pw == '') ? '\'\'' : $hashing_function . '(\'' . preg_replace('@.@s', '*', $pma_pw) . '\')');
1167 $local_query = 'SET PASSWORD FOR \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\' = ' . (($pma_pw == '') ? '\'\'' : $hashing_function . '(\'' . PMA_sqlAddslashes($pma_pw) . '\')');
1168 PMA_DBI_try_query($local_query)
1169 or PMA_mysqlDie(PMA_DBI_getError(), $sql_query, FALSE, $err_url);
1170 $message = PMA_Message::success('strPasswordEmpty');
1171 $message->addParam('\'' . $username . '\'@\'' . $hostname . '\'');
1177 * Deletes users
1178 * (Changes / copies a user, part IV)
1180 $user_host_separator = chr(27);
1182 if (isset($_REQUEST['delete']) || (isset($_REQUEST['change_copy']) && $_REQUEST['mode'] < 4)) {
1183 if (isset($_REQUEST['change_copy'])) {
1184 $selected_usr = array($old_username . $user_host_separator . $old_hostname);
1185 } else {
1186 $selected_usr = $_REQUEST['selected_usr'];
1187 $queries = array();
1189 foreach ($selected_usr as $each_user) {
1190 list($this_user, $this_host) = explode($user_host_separator, $each_user);
1191 $queries[] = '# ' . sprintf($GLOBALS['strDeleting'], '\'' . $this_user . '\'@\'' . $this_host . '\'') . ' ...';
1192 $queries[] = 'DROP USER \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1194 if (isset($_REQUEST['drop_users_db'])) {
1195 $queries[] = 'DROP DATABASE IF EXISTS ' . PMA_backquote($this_user) . ';';
1196 $GLOBALS['reload'] = TRUE;
1197 PMA_reloadNavigation();
1200 if (empty($_REQUEST['change_copy'])) {
1201 if (empty($queries)) {
1202 $message = PMA_Message::error('strDeleteNoUsersSelected');
1203 } else {
1204 if ($_REQUEST['mode'] == 3) {
1205 $queries[] = '# ' . $GLOBALS['strReloadingThePrivileges'] . ' ...';
1206 $queries[] = 'FLUSH PRIVILEGES;';
1208 $drop_user_error = '';
1209 foreach ($queries as $sql_query) {
1210 if ($sql_query{0} != '#') {
1211 if (! PMA_DBI_try_query($sql_query, $GLOBALS['userlink'])) {
1212 $drop_user_error .= PMA_DBI_getError() . "\n";
1216 $sql_query = join("\n", $queries);
1217 if (! empty($drop_user_error)) {
1218 $message = PMA_Message::rawError($drop_user_error);
1219 } else {
1220 $message = PMA_Message::success('strUsersDeleted');
1223 unset($queries);
1229 * Changes / copies a user, part V
1231 if (isset($_REQUEST['change_copy'])) {
1232 $tmp_count = 0;
1233 foreach ($queries as $sql_query) {
1234 if ($sql_query{0} != '#') {
1235 PMA_DBI_query($sql_query);
1237 // when there is a query containing a hidden password, take it
1238 // instead of the real query sent
1239 if (isset($queries_for_display[$tmp_count])) {
1240 $queries[$tmp_count] = $queries_for_display[$tmp_count];
1242 $tmp_count++;
1244 $message = PMA_Message::success();
1245 $sql_query = join("\n", $queries);
1250 * Reloads the privilege tables into memory
1252 if (isset($_REQUEST['flush_privileges'])) {
1253 $sql_query = 'FLUSH PRIVILEGES;';
1254 PMA_DBI_query($sql_query);
1255 $message = PMA_Message::success('strPrivilegesReloaded');
1260 * Displays the links
1262 if (isset($viewing_mode) && $viewing_mode == 'db') {
1263 $db = $checkprivs;
1264 $url_query .= '&amp;goto=db_operations.php';
1266 // Gets the database structure
1267 $sub_part = '_structure';
1268 require './libraries/db_info.inc.php';
1269 echo "\n";
1270 } else {
1271 require './libraries/server_links.inc.php';
1276 * defines some standard links
1278 $link_edit = '<a href="server_privileges.php?' . $GLOBALS['url_query']
1279 . '&amp;username=%s'
1280 . '&amp;hostname=%s'
1281 . '&amp;dbname=%s'
1282 . '&amp;tablename=%s">'
1283 . PMA_getIcon('b_usredit.png', $GLOBALS['strEditPrivileges'])
1284 . '</a>';
1286 $link_revoke = '<a href="server_privileges.php?' . $GLOBALS['url_query']
1287 . '&amp;username=%s'
1288 . '&amp;hostname=%s'
1289 . '&amp;dbname=%s'
1290 . '&amp;tablename=%s'
1291 . '&amp;revokeall=1">'
1292 . PMA_getIcon('b_usrdrop.png', $GLOBALS['strRevoke'])
1293 . '</a>';
1296 * Displays the page
1298 if (empty($_REQUEST['adduser']) && (! isset($checkprivs) || ! strlen($checkprivs))) {
1299 if (! isset($username)) {
1300 // No username is given --> display the overview
1301 echo '<h2>' . "\n"
1302 . PMA_getIcon('b_usrlist.png')
1303 . $GLOBALS['strUserOverview'] . "\n"
1304 . '</h2>' . "\n";
1306 $sql_query =
1307 'SELECT *,' .
1308 " IF(`Password` = _latin1 '', 'N', 'Y') AS 'Password'" .
1309 ' FROM `mysql`.`user`';
1311 $sql_query .= (isset($initial) ? PMA_RangeOfUsers($initial) : '');
1313 $sql_query .= ' ORDER BY `User` ASC, `Host` ASC;';
1314 $res = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_STORE);
1316 if (! $res) {
1317 // the query failed! This may have two reasons:
1318 // - the user does not have enough privileges
1319 // - the privilege tables use a structure of an earlier version.
1320 // so let's try a more simple query
1322 $sql_query = 'SELECT * FROM `mysql`.`user`';
1323 $res = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_STORE);
1325 if (!$res) {
1326 PMA_Message::error('strNoPrivileges')->display();
1327 PMA_DBI_free_result($res);
1328 unset($res);
1329 } else {
1330 // rabus: This message is hardcoded because I will replace it by
1331 // a automatic repair feature soon.
1332 $raw = 'Your privilege table structure seems to be older than'
1333 . ' this MySQL version!<br />'
1334 . 'Please run the script <tt>mysql_fix_privilege_tables</tt>'
1335 . ' that should be included in your MySQL server distribution'
1336 . ' to solve this problem!';
1337 PMA_Message::rawError($raw)->display();
1339 } else {
1341 // we also want users not in table `user` but in other table
1342 $tables = PMA_DBI_fetch_result('SHOW TABLES FROM `mysql`;');
1344 $tables_to_search_for_users = array(
1345 'user', 'db', 'tables_priv', 'columns_priv', 'procs_priv',
1348 $db_rights_sqls = array();
1349 foreach ($tables_to_search_for_users as $table_search_in) {
1350 if (in_array($table_search_in, $tables)) {
1351 $db_rights_sqls[] = 'SELECT DISTINCT `User`, `Host` FROM `mysql`.`' . $table_search_in . '` ' . (isset($initial) ? PMA_RangeOfUsers($initial) : '');
1355 $user_defaults = array(
1356 'User' => '',
1357 'Host' => '%',
1358 'Password' => '?',
1359 'Grant_priv' => 'N',
1360 'privs' => array('USAGE'),
1363 // for all initials, even non A-Z
1364 $array_initials = array();
1365 // for the rights
1366 $db_rights = array();
1368 $db_rights_sql = '(' . implode(') UNION (', $db_rights_sqls) . ')'
1369 .' ORDER BY `User` ASC, `Host` ASC';
1371 $db_rights_result = PMA_DBI_query($db_rights_sql);
1373 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1374 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1375 $db_rights[$db_rights_row['User']][$db_rights_row['Host']] =
1376 $db_rights_row;
1378 PMA_DBI_free_result($db_rights_result);
1379 unset($db_rights_sql, $db_rights_sqls, $db_rights_result, $db_rights_row);
1380 ksort($db_rights);
1383 * Displays the initials
1386 // initialize to FALSE the letters A-Z
1387 for ($letter_counter = 1; $letter_counter < 27; $letter_counter++) {
1388 if (! isset($array_initials[chr($letter_counter + 64)])) {
1389 $array_initials[chr($letter_counter + 64)] = FALSE;
1393 $initials = PMA_DBI_try_query('SELECT DISTINCT UPPER(LEFT(`User`,1)) FROM `user` ORDER BY `User` ASC', null, PMA_DBI_QUERY_STORE);
1394 while (list($tmp_initial) = PMA_DBI_fetch_row($initials)) {
1395 $array_initials[$tmp_initial] = TRUE;
1398 // Display the initials, which can be any characters, not
1399 // just letters. For letters A-Z, we add the non-used letters
1400 // as greyed out.
1402 uksort($array_initials, "strnatcasecmp");
1404 echo '<table cellspacing="5"><tr>';
1405 foreach ($array_initials as $tmp_initial => $initial_was_found) {
1406 if ($initial_was_found) {
1407 echo '<td><a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;initial=' . urlencode($tmp_initial) . '">' . $tmp_initial . '</a></td>' . "\n";
1408 } else {
1409 echo '<td>' . $tmp_initial . '</td>';
1412 echo '<td><a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;showall=1">[' . $GLOBALS['strShowAll'] . ']</a></td>' . "\n";
1413 echo '</tr></table>';
1416 * Display the user overview
1417 * (if less than 50 users, display them immediately)
1420 if (isset($initial) || isset($showall) || PMA_DBI_num_rows($res) < 50) {
1422 while ($row = PMA_DBI_fetch_assoc($res)) {
1423 $row['privs'] = PMA_extractPrivInfo($row, true);
1424 $db_rights[$row['User']][$row['Host']] = $row;
1426 @PMA_DBI_free_result($res);
1427 unset($res);
1429 echo '<form name="usersForm" id="usersForm" action="server_privileges.php" method="post">' . "\n"
1430 . PMA_generate_common_hidden_inputs('', '')
1431 . ' <table id="tableuserrights" class="data">' . "\n"
1432 . ' <thead>' . "\n"
1433 . ' <tr><td></td>' . "\n"
1434 . ' <th>' . $GLOBALS['strUser'] . '</th>' . "\n"
1435 . ' <th>' . $GLOBALS['strHost'] . '</th>' . "\n"
1436 . ' <th>' . $GLOBALS['strPassword'] . '</th>' . "\n"
1437 . ' <th>' . $GLOBALS['strGlobalPrivileges'] . ' '
1438 . PMA_showHint($GLOBALS['strEnglishPrivileges']) . '</th>' . "\n"
1439 . ' <th>' . $GLOBALS['strGrantOption'] . '</th>' . "\n"
1440 . ' ' . ($GLOBALS['cfg']['PropertiesIconic'] ? '<td></td>' : '<th>' . $GLOBALS['strAction'] . '</th>') . "\n";
1441 echo ' </tr>' . "\n";
1442 echo ' </thead>' . "\n";
1443 echo ' <tbody>' . "\n";
1444 $odd_row = true;
1445 $index_checkbox = -1;
1446 foreach ($db_rights as $user) {
1447 $index_checkbox++;
1448 ksort($user);
1449 foreach ($user as $host) {
1450 $index_checkbox++;
1451 echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n"
1452 . ' <td><input type="checkbox" name="selected_usr[]" id="checkbox_sel_users_'
1453 . $index_checkbox . '" value="'
1454 . str_replace(chr(27), '&#27;', htmlspecialchars($host['User'] . $user_host_separator . $host['Host']))
1455 . '"'
1456 . (empty($GLOBALS['checkall']) ? '' : ' checked="checked"')
1457 . ' /></td>' . "\n"
1458 . ' <td><label for="checkbox_sel_users_' . $index_checkbox . '">' . (empty($host['User']) ? '<span style="color: #FF0000">' . $GLOBALS['strAny'] . '</span>' : htmlspecialchars($host['User'])) . '</label></td>' . "\n"
1459 . ' <td>' . htmlspecialchars($host['Host']) . '</td>' . "\n";
1460 echo ' <td>';
1461 switch ($host['Password']) {
1462 case 'Y':
1463 echo $GLOBALS['strYes'];
1464 break;
1465 case 'N':
1466 echo '<span style="color: #FF0000">' . $GLOBALS['strNo'] . '</span>';
1467 break;
1468 // this happens if this is a definition not coming from mysql.user
1469 default:
1470 echo '--'; // in future version, replace by "not present"
1471 break;
1472 } // end switch
1473 echo '</td>' . "\n"
1474 . ' <td><tt>' . "\n"
1475 . ' ' . implode(',' . "\n" . ' ', $host['privs']) . "\n"
1476 . ' </tt></td>' . "\n"
1477 . ' <td>' . ($host['Grant_priv'] == 'Y' ? $GLOBALS['strYes'] : $GLOBALS['strNo']) . '</td>' . "\n"
1478 . ' <td align="center">';
1479 printf($link_edit, urlencode($host['User']),
1480 urlencode($host['Host']), '', '');
1481 echo '</td>' . "\n"
1482 . ' </tr>' . "\n";
1483 $odd_row = ! $odd_row;
1487 unset($user, $host, $odd_row);
1488 echo ' </tbody></table>' . "\n"
1489 .'<img class="selectallarrow"'
1490 .' src="' . $pmaThemeImage . 'arrow_' . $text_dir . '.png"'
1491 .' width="38" height="22"'
1492 .' alt="' . $GLOBALS['strWithChecked'] . '" />' . "\n"
1493 .'<a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;checkall=1"'
1494 .' onclick="if (markAllRows(\'usersForm\')) return false;">'
1495 . $GLOBALS['strCheckAll'] . '</a>' . "\n"
1496 .'/' . "\n"
1497 .'<a href="server_privileges.php?' . $GLOBALS['url_query'] . '"'
1498 .' onclick="if (unMarkAllRows(\'usersForm\')) return false;">'
1499 . $GLOBALS['strUncheckAll'] . '</a>' . "\n";
1501 // add/delete user fieldset
1502 echo ' <fieldset id="fieldset_add_user">' . "\n"
1503 . ' <a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;adduser=1">' . "\n"
1504 . PMA_getIcon('b_usradd.png')
1505 . ' ' . $GLOBALS['strAddUser'] . '</a>' . "\n"
1506 . ' </fieldset>' . "\n"
1507 . ' <fieldset id="fieldset_delete_user">'
1508 . ' <legend>' . "\n"
1509 . PMA_getIcon('b_usrdrop.png')
1510 . ' ' . $GLOBALS['strRemoveSelectedUsers'] . '' . "\n"
1511 . ' </legend>' . "\n"
1512 . ' <input type="hidden" name="mode" value="2" />' . "\n"
1513 . '(' . $GLOBALS['strRevokeAndDelete'] . ')<br />' . "\n"
1514 . ' <input type="checkbox" title="' . $GLOBALS['strDropUsersDb'] . '" name="drop_users_db" id="checkbox_drop_users_db" />' . "\n"
1515 . ' <label for="checkbox_drop_users_db" title="' . $GLOBALS['strDropUsersDb'] . '">' . "\n"
1516 . ' ' . $GLOBALS['strDropUsersDb'] . "\n"
1517 . ' </label>' . "\n"
1518 . ' </fieldset>' . "\n"
1519 . ' <fieldset id="fieldset_delete_user_footer" class="tblFooters">' . "\n"
1520 . ' <input type="submit" name="delete" value="' . $GLOBALS['strGo'] . '" id="buttonGo" />' . "\n"
1521 . ' </fieldset>' . "\n";
1522 } else {
1524 unset ($row);
1525 echo ' <fieldset id="fieldset_add_user">' . "\n"
1526 . ' <a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;adduser=1">' . "\n"
1527 . PMA_getIcon('b_usradd.png')
1528 . ' ' . $GLOBALS['strAddUser'] . '</a>' . "\n"
1529 . ' </fieldset>' . "\n";
1530 } // end if (display overview)
1531 echo '</form>' . "\n";
1532 $flushnote = new PMA_Message('strFlushPrivilegesNote', PMA_Message::NOTICE);
1533 $flushnote->addParam('<a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;flush_privileges=1">', false);
1534 $flushnote->addParam('</a>', false);
1535 $flushnote->display();
1539 } else {
1541 // A user was selected -> display the user's properties
1543 echo '<h2>' . "\n"
1544 . PMA_getIcon('b_usredit.png')
1545 . $GLOBALS['strUser'] . ' <i><a href="server_privileges.php?'
1546 . $GLOBALS['url_query'] . '&amp;username=' . urlencode($username)
1547 . '&amp;hostname=' . urlencode($hostname) . '">\''
1548 . htmlspecialchars($username) . '\'@\'' . htmlspecialchars($hostname)
1549 . '\'</a></i>' . "\n";
1550 if (isset($dbname)) {
1551 if ($dbname_is_wildcard) {
1552 echo ' - ' . $GLOBALS['strDatabases'];
1553 } else {
1554 echo ' - ' . $GLOBALS['strDatabase'];
1556 $url_dbname = urlencode(str_replace('\_', '_', $dbname));
1557 echo ' <i><a href="' . $GLOBALS['cfg']['DefaultTabDatabase'] . '?'
1558 . $GLOBALS['url_query'] . '&amp;db=' . $url_dbname . '&amp;reload=1">'
1559 . htmlspecialchars($dbname) . '</a></i>' . "\n";
1560 if (isset($tablename)) {
1561 echo ' - ' . $GLOBALS['strTable'] . ' <i><a href="'
1562 . $GLOBALS['cfg']['DefaultTabTable'] . '?' . $GLOBALS['url_query']
1563 . '&amp;db=' . $url_dbname . '&amp;table=' . urlencode($tablename)
1564 . '&amp;reload=1">' . htmlspecialchars($tablename) . '</a></i>'
1565 . "\n";
1567 unset($url_dbname);
1569 echo ' : ' . $GLOBALS['strEditPrivileges'] . '</h2>' . "\n";
1571 $sql = "SELECT '1' FROM `mysql`.`user`"
1572 . " WHERE `User` = '" . PMA_sqlAddslashes($username) . "'"
1573 . " AND `Host` = '" . PMA_sqlAddslashes($hostname) . "';";
1574 $user_does_not_exists = (bool) ! PMA_DBI_fetch_value($sql);
1575 unset($sql);
1576 if ($user_does_not_exists) {
1577 PMA_Message::warning('strUserNotFound')->display();
1578 PMA_displayLoginInformationFields();
1579 //require_once './libraries/footer.inc.php';
1582 echo '<form name="usersForm" id="usersForm" action="server_privileges.php" method="post">' . "\n";
1583 $_params = array(
1584 'username' => $username,
1585 'hostname' => $hostname,
1587 if (isset($dbname)) {
1588 $_params['dbname'] = $dbname;
1589 if (isset($tablename)) {
1590 $_params['tablename'] = $tablename;
1593 echo PMA_generate_common_hidden_inputs($_params);
1595 PMA_displayPrivTable(PMA_ifSetOr($dbname, '*', 'length'),
1596 PMA_ifSetOr($tablename, '*', 'length'));
1598 echo '</form>' . "\n";
1600 if (! isset($tablename) && empty($dbname_is_wildcard)) {
1602 // no table name was given, display all table specific rights
1603 // but only if $dbname contains no wildcards
1605 // table header
1606 echo '<form action="server_privileges.php" method="post">' . "\n"
1607 . PMA_generate_common_hidden_inputs('', '')
1608 . '<input type="hidden" name="username" value="' . htmlspecialchars($username) . '" />' . "\n"
1609 . '<input type="hidden" name="hostname" value="' . htmlspecialchars($hostname) . '" />' . "\n"
1610 . '<fieldset>' . "\n"
1611 . '<legend>' . (! isset($dbname) ? $GLOBALS['strDbPrivileges'] : $GLOBALS['strTblPrivileges']) . '</legend>' . "\n"
1612 . '<table class="data">' . "\n"
1613 . '<thead>' . "\n"
1614 . '<tr><th>' . (! isset($dbname) ? $GLOBALS['strDatabase'] : $GLOBALS['strTable']) . '</th>' . "\n"
1615 . ' <th>' . $GLOBALS['strPrivileges'] . '</th>' . "\n"
1616 . ' <th>' . $GLOBALS['strGrantOption'] . '</th>' . "\n"
1617 . ' <th>' . (! isset($dbname) ? $GLOBALS['strTblPrivileges'] : $GLOBALS['strColumnPrivileges']) . '</th>' . "\n"
1618 . ' <th colspan="2">' . $GLOBALS['strAction'] . '</th>' . "\n"
1619 . '</tr>' . "\n"
1620 . '</thead>' . "\n"
1621 . '<tbody>' . "\n";
1623 $user_host_condition =
1624 ' WHERE `User`'
1625 . ' = \'' . PMA_sqlAddslashes($username) . "'"
1626 . ' AND `Host`'
1627 . ' = \'' . PMA_sqlAddslashes($hostname) . "'";
1629 // table body
1630 // get data
1632 // we also want privielgs for this user not in table `db` but in other table
1633 $tables = PMA_DBI_fetch_result('SHOW TABLES FROM `mysql`;');
1634 if (! isset($dbname)) {
1636 // no db name given, so we want all privs for the given user
1638 $tables_to_search_for_users = array(
1639 'tables_priv', 'columns_priv',
1642 $db_rights_sqls = array();
1643 foreach ($tables_to_search_for_users as $table_search_in) {
1644 if (in_array($table_search_in, $tables)) {
1645 $db_rights_sqls[] = '
1646 SELECT DISTINCT `Db`
1647 FROM `mysql`.`' . $table_search_in . '`
1648 ' . $user_host_condition;
1652 $user_defaults = array(
1653 'Db' => '',
1654 'Grant_priv' => 'N',
1655 'privs' => array('USAGE'),
1656 'Table_privs' => true,
1659 // for the rights
1660 $db_rights = array();
1662 $db_rights_sql = '(' . implode(') UNION (', $db_rights_sqls) . ')'
1663 .' ORDER BY `Db` ASC';
1665 $db_rights_result = PMA_DBI_query($db_rights_sql);
1667 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1668 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1669 // only Db names in the table `mysql`.`db` uses wildcards
1670 // as we are in the db specific rights display we want
1671 // all db names escaped, also from other sources
1672 $db_rights_row['Db'] = PMA_escape_mysql_wildcards(
1673 $db_rights_row['Db']);
1674 $db_rights[$db_rights_row['Db']] = $db_rights_row;
1677 PMA_DBI_free_result($db_rights_result);
1678 unset($db_rights_sql, $db_rights_sqls, $db_rights_result, $db_rights_row);
1680 $sql_query = 'SELECT * FROM `mysql`.`db`' . $user_host_condition . ' ORDER BY `Db` ASC';
1681 $res = PMA_DBI_query($sql_query);
1682 $sql_query = '';
1684 while ($row = PMA_DBI_fetch_assoc($res)) {
1685 if (isset($db_rights[$row['Db']])) {
1686 $db_rights[$row['Db']] = array_merge($db_rights[$row['Db']], $row);
1687 } else {
1688 $db_rights[$row['Db']] = $row;
1690 // there are db specific rights for this user
1691 // so we can drop this db rights
1692 $db_rights[$row['Db']]['can_delete'] = true;
1694 PMA_DBI_free_result($res);
1695 unset($row, $res);
1697 } else {
1699 // db name was given,
1700 // so we want all user specific rights for this db
1702 $user_host_condition .=
1703 ' AND `Db`'
1704 .' LIKE \'' . $dbname . "'";
1706 $tables_to_search_for_users = array(
1707 'columns_priv',
1710 $db_rights_sqls = array();
1711 foreach ($tables_to_search_for_users as $table_search_in) {
1712 if (in_array($table_search_in, $tables)) {
1713 $db_rights_sqls[] = '
1714 SELECT DISTINCT `Table_name`
1715 FROM `mysql`.`' . $table_search_in . '`
1716 ' . $user_host_condition;
1720 $user_defaults = array(
1721 'Table_name' => '',
1722 'Grant_priv' => 'N',
1723 'privs' => array('USAGE'),
1724 'Column_priv' => true,
1727 // for the rights
1728 $db_rights = array();
1730 $db_rights_sql = '(' . implode(') UNION (', $db_rights_sqls) . ')'
1731 .' ORDER BY `Table_name` ASC';
1733 $db_rights_result = PMA_DBI_query($db_rights_sql);
1735 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1736 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1737 $db_rights[$db_rights_row['Table_name']] = $db_rights_row;
1739 PMA_DBI_free_result($db_rights_result);
1740 unset($db_rights_sql, $db_rights_sqls, $db_rights_result, $db_rights_row);
1742 $sql_query =
1743 'SELECT `Table_name`,'
1744 .' `Table_priv`,'
1745 .' IF(`Column_priv` = _latin1 \'\', 0, 1)'
1746 .' AS \'Column_priv\''
1747 .' FROM `mysql`.`tables_priv`'
1748 . $user_host_condition
1749 .' ORDER BY `Table_name` ASC;';
1750 $res = PMA_DBI_query($sql_query);
1751 $sql_query = '';
1753 while ($row = PMA_DBI_fetch_assoc($res)) {
1754 if (isset($db_rights[$row['Table_name']])) {
1755 $db_rights[$row['Table_name']] = array_merge($db_rights[$row['Table_name']], $row);
1756 } else {
1757 $db_rights[$row['Table_name']] = $row;
1760 PMA_DBI_free_result($res);
1761 unset($row, $res);
1763 ksort($db_rights);
1765 // display rows
1766 if (count($db_rights) < 1) {
1767 echo '<tr class="odd">' . "\n"
1768 . ' <td colspan="6"><center><i>' . $GLOBALS['strNone'] . '</i></center></td>' . "\n"
1769 . '</tr>' . "\n";
1770 } else {
1771 $odd_row = true;
1772 $found_rows = array();
1773 //while ($row = PMA_DBI_fetch_assoc($res)) {
1774 foreach ($db_rights as $row) {
1775 $found_rows[] = (! isset($dbname)) ? $row['Db'] : $row['Table_name'];
1777 echo '<tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n"
1778 . ' <td>' . htmlspecialchars((! isset($dbname)) ? $row['Db'] : $row['Table_name']) . '</td>' . "\n"
1779 . ' <td><tt>' . "\n"
1780 . ' ' . join(',' . "\n" . ' ', PMA_extractPrivInfo($row, TRUE)) . "\n"
1781 . ' </tt></td>' . "\n"
1782 . ' <td>' . ((((! isset($dbname)) && $row['Grant_priv'] == 'Y') || (isset($dbname) && in_array('Grant', explode(',', $row['Table_priv'])))) ? $GLOBALS['strYes'] : $GLOBALS['strNo']) . '</td>' . "\n"
1783 . ' <td>';
1784 if (! empty($row['Table_privs']) || ! empty ($row['Column_priv'])) {
1785 echo $GLOBALS['strYes'];
1786 } else {
1787 echo $GLOBALS['strNo'];
1789 echo '</td>' . "\n"
1790 . ' <td>';
1791 printf($link_edit, urlencode($username),
1792 urlencode($hostname),
1793 urlencode((! isset($dbname)) ? $row['Db'] : $dbname),
1794 urlencode((! isset($dbname)) ? '' : $row['Table_name']));
1795 echo '</td>' . "\n"
1796 . ' <td>';
1797 if (! empty($row['can_delete']) || isset($row['Table_name']) && strlen($row['Table_name'])) {
1798 printf($link_revoke, urlencode($username),
1799 urlencode($hostname),
1800 urlencode((! isset($dbname)) ? $row['Db'] : $dbname),
1801 urlencode((! isset($dbname)) ? '' : $row['Table_name']));
1803 echo '</td>' . "\n"
1804 . '</tr>' . "\n";
1805 $odd_row = ! $odd_row;
1806 } // end while
1808 unset($row);
1809 echo '</tbody>' . "\n"
1810 . '</table>' . "\n";
1812 if (! isset($dbname)) {
1814 // no database name was give, display select db
1816 if (! empty($found_rows)) {
1817 $pred_db_array = array_diff(
1818 PMA_DBI_fetch_result('SHOW DATABASES;'),
1819 $found_rows);
1820 } else {
1821 $pred_db_array =PMA_DBI_fetch_result('SHOW DATABASES;');
1824 echo ' <label for="text_dbname">' . $GLOBALS['strAddPrivilegesOnDb'] . ':</label>' . "\n";
1825 if (!empty($pred_db_array)) {
1826 echo ' <select name="pred_dbname" onchange="this.form.submit();">' . "\n"
1827 . ' <option value="" selected="selected">' . $GLOBALS['strUseTextField'] . ':</option>' . "\n";
1828 foreach ($pred_db_array as $current_db) {
1829 $current_db = PMA_escape_mysql_wildcards($current_db);
1830 echo ' <option value="' . htmlspecialchars($current_db) . '">'
1831 . htmlspecialchars($current_db) . '</option>' . "\n";
1833 echo ' </select>' . "\n";
1835 echo ' <input type="text" id="text_dbname" name="dbname" />' . "\n"
1836 . PMA_showHint($GLOBALS['strEscapeWildcards']);
1837 } else {
1838 echo ' <input type="hidden" name="dbname" value="' . htmlspecialchars($dbname) . '"/>' . "\n"
1839 . ' <label for="text_tablename">' . $GLOBALS['strAddPrivilegesOnTbl'] . ':</label>' . "\n";
1840 if ($res = @PMA_DBI_try_query('SHOW TABLES FROM ' . PMA_backquote($dbname) . ';', null, PMA_DBI_QUERY_STORE)) {
1841 $pred_tbl_array = array();
1842 while ($row = PMA_DBI_fetch_row($res)) {
1843 if (!isset($found_rows) || !in_array($row[0], $found_rows)) {
1844 $pred_tbl_array[] = $row[0];
1847 PMA_DBI_free_result($res);
1848 unset($res, $row);
1849 if (!empty($pred_tbl_array)) {
1850 echo ' <select name="pred_tablename" onchange="this.form.submit();">' . "\n"
1851 . ' <option value="" selected="selected">' . $GLOBALS['strUseTextField'] . ':</option>' . "\n";
1852 foreach ($pred_tbl_array as $current_table) {
1853 echo ' <option value="' . htmlspecialchars($current_table) . '">' . htmlspecialchars($current_table) . '</option>' . "\n";
1855 echo ' </select>' . "\n";
1857 } else {
1858 unset($res);
1860 echo ' <input type="text" id="text_tablename" name="tablename" />' . "\n";
1862 echo '</fieldset>' . "\n";
1863 echo '<fieldset class="tblFooters">' . "\n"
1864 . ' <input type="submit" value="' . $GLOBALS['strGo'] . '" />'
1865 . '</fieldset>' . "\n"
1866 . '</form>' . "\n";
1869 if (! isset($dbname) && ! $user_does_not_exists) {
1870 require_once './libraries/display_change_password.lib.php';
1872 echo '<form action="server_privileges.php" method="post" onsubmit="return checkPassword(this);">' . "\n"
1873 . PMA_generate_common_hidden_inputs('', '')
1874 . '<input type="hidden" name="old_username" value="' . htmlspecialchars($username) . '" />' . "\n"
1875 . '<input type="hidden" name="old_hostname" value="' . htmlspecialchars($hostname) . '" />' . "\n"
1876 . '<fieldset id="fieldset_change_copy_user">' . "\n"
1877 . ' <legend>' . $GLOBALS['strChangeCopyUser'] . '</legend>' . "\n";
1878 PMA_displayLoginInformationFields('change');
1879 echo ' <fieldset>' . "\n"
1880 . ' <legend>' . $GLOBALS['strChangeCopyMode'] . '</legend>' . "\n";
1881 $choices = array(
1882 '4' => $GLOBALS['strChangeCopyModeCopy'],
1883 '1' => $GLOBALS['strChangeCopyModeJustDelete'],
1884 '2' => $GLOBALS['strChangeCopyModeRevoke'],
1885 '3' => $GLOBALS['strChangeCopyModeDeleteAndReload']);
1886 PMA_generate_html_radio('mode', $choices, '4', true);
1887 unset($choices);
1889 echo ' </fieldset>' . "\n"
1890 . '</fieldset>' . "\n"
1891 . '<fieldset id="fieldset_change_copy_user_footer" class="tblFooters">' . "\n"
1892 . ' <input type="submit" name="change_copy" value="' . $GLOBALS['strGo'] . '" />' . "\n"
1893 . '</fieldset>' . "\n"
1894 . '</form>' . "\n";
1897 } elseif (isset($_REQUEST['adduser'])) {
1898 // Add a new user
1899 $GLOBALS['url_query'] .= '&amp;adduser=1';
1900 echo '<h2>' . "\n"
1901 . PMA_getIcon('b_usradd.png') . $GLOBALS['strAddUser'] . "\n"
1902 . '</h2>' . "\n"
1903 . '<form name="usersForm" id="usersForm" action="server_privileges.php" method="post" onsubmit="return checkAddUser(this);">' . "\n"
1904 . PMA_generate_common_hidden_inputs('', '');
1905 PMA_displayLoginInformationFields('new');
1906 echo '<fieldset id="fieldset_add_user_database">' . "\n"
1907 . '<legend>' . $GLOBALS['strCreateUserDatabase'] . '</legend>' . "\n";
1909 $choices = array(
1910 '0' => $GLOBALS['strCreateUserDatabaseNone'],
1911 '1' => $GLOBALS['strCreateUserDatabaseName'],
1912 '2' => $GLOBALS['strCreateUserDatabaseWildcard']);
1913 PMA_generate_html_radio('createdb', $choices, '0', true);
1914 unset($choices);
1916 echo '</fieldset>' . "\n";
1917 PMA_displayPrivTable('*', '*', FALSE);
1918 echo ' <fieldset id="fieldset_add_user_footer" class="tblFooters">' . "\n"
1919 . ' <input type="submit" name="adduser_submit" value="' . $GLOBALS['strGo'] . '" />' . "\n"
1920 . ' </fieldset>' . "\n"
1921 . '</form>' . "\n";
1922 } else {
1923 // check the privileges for a particular database.
1924 echo '<table id="tablespecificuserrights" class="data">' . "\n"
1925 . '<caption class="tblHeaders">' . "\n"
1926 . PMA_getIcon('b_usrcheck.png')
1927 . ' ' . sprintf($GLOBALS['strUsersHavingAccessToDb'], '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase'] . '?' . PMA_generate_common_url($checkprivs) . '">' . htmlspecialchars($checkprivs) . '</a>') . "\n"
1928 . '</caption>' . "\n"
1929 . '<thead>' . "\n"
1930 . ' <tr><th>' . $GLOBALS['strUser'] . '</th>' . "\n"
1931 . ' <th>' . $GLOBALS['strHost'] . '</th>' . "\n"
1932 . ' <th>' . $GLOBALS['strType'] . '</th>' . "\n"
1933 . ' <th>' . $GLOBALS['strPrivileges'] . '</th>' . "\n"
1934 . ' <th>' . $GLOBALS['strGrantOption'] . '</th>' . "\n"
1935 . ' <th>' . $GLOBALS['strAction'] . '</th>' . "\n"
1936 . ' </tr>' . "\n"
1937 . '</thead>' . "\n"
1938 . '<tbody>' . "\n";
1939 $odd_row = TRUE;
1940 unset($row, $row1, $row2);
1942 // now, we build the table...
1943 $list_of_privileges =
1944 '`User`, '
1945 . '`Host`, '
1946 . '`Select_priv`, '
1947 . '`Insert_priv`, '
1948 . '`Update_priv`, '
1949 . '`Delete_priv`, '
1950 . '`Create_priv`, '
1951 . '`Drop_priv`, '
1952 . '`Grant_priv`, '
1953 . '`Index_priv`, '
1954 . '`Alter_priv`, '
1955 . '`References_priv`, '
1956 . '`Create_tmp_table_priv`, '
1957 . '`Lock_tables_priv`, '
1958 . '`Create_view_priv`, '
1959 . '`Show_view_priv`, '
1960 . '`Create_routine_priv`, '
1961 . '`Alter_routine_priv`, '
1962 . '`Execute_priv`, '
1963 . '`Event_priv`, '
1964 . '`Trigger_priv`';
1966 $list_of_compared_privileges =
1967 '`Select_priv` = \'N\''
1968 . ' AND `Insert_priv` = \'N\''
1969 . ' AND `Update_priv` = \'N\''
1970 . ' AND `Delete_priv` = \'N\''
1971 . ' AND `Create_priv` = \'N\''
1972 . ' AND `Drop_priv` = \'N\''
1973 . ' AND `Grant_priv` = \'N\''
1974 . ' AND `References_priv` = \'N\''
1975 . ' AND `Create_tmp_table_priv` = \'N\''
1976 . ' AND `Lock_tables_priv` = \'N\''
1977 . ' AND `Create_view_priv` = \'N\''
1978 . ' AND `Show_view_priv` = \'N\''
1979 . ' AND `Create_routine_priv` = \'N\''
1980 . ' AND `Alter_routine_priv` = \'N\''
1981 . ' AND `Execute_priv` = \'N\''
1982 . ' AND `Event_priv` = \'N\''
1983 . ' AND `Trigger_priv` = \'N\'';
1985 $sql_query =
1986 '(SELECT ' . $list_of_privileges . ', `Db`'
1987 .' FROM `mysql`.`db`'
1988 .' WHERE \'' . PMA_sqlAddslashes($checkprivs) . "'"
1989 .' LIKE `Db`'
1990 .' AND NOT (' . $list_of_compared_privileges. ')) '
1991 .'UNION '
1992 .'(SELECT ' . $list_of_privileges . ', \'*\' AS `Db`'
1993 .' FROM `mysql`.`user` '
1994 .' WHERE NOT (' . $list_of_compared_privileges . ')) '
1995 .' ORDER BY `User` ASC,'
1996 .' `Host` ASC,'
1997 .' `Db` ASC;';
1998 $res = PMA_DBI_query($sql_query);
1999 $row = PMA_DBI_fetch_assoc($res);
2000 if ($row) {
2001 $found = TRUE;
2004 if ($found) {
2005 while (TRUE) {
2006 // prepare the current user
2007 $current_privileges = array();
2008 $current_user = $row['User'];
2009 $current_host = $row['Host'];
2010 while ($row && $current_user == $row['User'] && $current_host == $row['Host']) {
2011 $current_privileges[] = $row;
2012 $row = PMA_DBI_fetch_assoc($res);
2014 echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n"
2015 . ' <td';
2016 if (count($current_privileges) > 1) {
2017 echo ' rowspan="' . count($current_privileges) . '"';
2019 echo '>' . (empty($current_user) ? '<span style="color: #FF0000">' . $GLOBALS['strAny'] . '</span>' : htmlspecialchars($current_user)) . "\n"
2020 . ' </td>' . "\n"
2021 . ' <td';
2022 if (count($current_privileges) > 1) {
2023 echo ' rowspan="' . count($current_privileges) . '"';
2025 echo '>' . htmlspecialchars($current_host) . '</td>' . "\n";
2026 foreach ($current_privileges as $current) {
2027 echo ' <td>' . "\n"
2028 . ' ';
2029 if (!isset($current['Db']) || $current['Db'] == '*') {
2030 echo $GLOBALS['strGlobal'];
2031 } elseif ($current['Db'] == PMA_escape_mysql_wildcards($checkprivs)) {
2032 echo $GLOBALS['strDbSpecific'];
2033 } else {
2034 echo $GLOBALS['strWildcard'], ': <tt>' . htmlspecialchars($current['Db']) . '</tt>';
2036 echo "\n"
2037 . ' </td>' . "\n"
2038 . ' <td>' . "\n"
2039 . ' <tt>' . "\n"
2040 . ' ' . join(',' . "\n" . ' ', PMA_extractPrivInfo($current, TRUE)) . "\n"
2041 . ' </tt>' . "\n"
2042 . ' </td>' . "\n"
2043 . ' <td>' . "\n"
2044 . ' ' . ($current['Grant_priv'] == 'Y' ? $GLOBALS['strYes'] : $GLOBALS['strNo']) . "\n"
2045 . ' </td>' . "\n"
2046 . ' <td>' . "\n";
2047 printf($link_edit, urlencode($current_user),
2048 urlencode($current_host),
2049 urlencode(! isset($current['Db']) || $current['Db'] == '*' ? '' : $current['Db']),
2050 '');
2051 echo '</td>' . "\n"
2052 . ' </tr>' . "\n";
2054 if (empty($row) && empty($row1) && empty($row2)) {
2055 break;
2057 $odd_row = ! $odd_row;
2059 } else {
2060 echo ' <tr class="odd">' . "\n"
2061 . ' <td colspan="6">' . "\n"
2062 . ' ' . $GLOBALS['strNoUsersFound'] . "\n"
2063 . ' </td>' . "\n"
2064 . ' </tr>' . "\n";
2066 echo '</tbody>' . "\n"
2067 . '</table>' . "\n";
2068 } // end if (empty($_REQUEST['adduser']) && empty($checkprivs)) ... elseif ... else ...
2072 * Displays the footer
2074 echo "\n\n";
2075 require_once './libraries/footer.inc.php';