2.11.1.2 release
[phpmyadmin/arisferyanto.git] / server_privileges.php
blobd806a44135a5d7b73f6d5b6fb5e3813fda03301d
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>' . "\n"
187 . $spaces . ' <select id="select_' . $name . '_priv" name="' . $name_for_select . '[]" multiple="multiple">' . "\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')
753 .' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted')
754 .' AND ' . PMA_convert_using('Host')
755 .' = ' . PMA_convert_using($hostname, 'quoted') . ';',
756 null, PMA_DBI_QUERY_STORE);
757 if (PMA_DBI_num_rows($res) == 1) {
758 PMA_DBI_free_result($res);
759 $message = sprintf($GLOBALS['strUserAlreadyExists'], '[i]\'' . $username . '\'@\'' . $hostname . '\'[/i]');
760 $adduser = 1;
761 } else {
762 PMA_DBI_free_result($res);
764 if (50002 <= PMA_MYSQL_INT_VERSION) {
765 // MySQL 5 requires CREATE USER before any GRANT on this user can done
766 $create_user_real = 'CREATE USER \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\'';
769 $real_sql_query =
770 'GRANT ' . join(', ', PMA_extractPrivInfo()) . ' ON *.* TO \''
771 . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\'';
772 if ($pred_password != 'none' && $pred_password != 'keep') {
773 $pma_pw_hidden = str_repeat('*', strlen($pma_pw));
774 $sql_query = $real_sql_query . ' IDENTIFIED BY \'' . $pma_pw_hidden . '\'';
775 $real_sql_query .= ' IDENTIFIED BY \'' . PMA_sqlAddslashes($pma_pw) . '\'';
776 if (isset($create_user_real)) {
777 $create_user_show = $create_user_real . ' IDENTIFIED BY \'' . $pma_pw_hidden . '\'';
778 $create_user_real .= ' IDENTIFIED BY \'' . PMA_sqlAddslashes($pma_pw) . '\'';
780 } else {
781 if ($pred_password == 'keep' && !empty($password)) {
782 $real_sql_query .= ' IDENTIFIED BY PASSWORD \'' . $password . '\'';
783 if (isset($create_user_real)) {
784 $create_user_real .= ' IDENTIFIED BY PASSWORD \'' . $password . '\'';
787 $sql_query = $real_sql_query;
788 if (isset($create_user_real)) {
789 $create_user_show = $create_user_real;
793 * @todo similar code appears twice in this script
795 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)))) {
796 $real_sql_query .= 'WITH';
797 $sql_query .= 'WITH';
798 if (isset($Grant_priv) && $Grant_priv == 'Y') {
799 $real_sql_query .= ' GRANT OPTION';
800 $sql_query .= ' GRANT OPTION';
802 if (PMA_MYSQL_INT_VERSION >= 40002) {
803 if (isset($max_questions)) {
804 // avoid negative values
805 $max_questions = max(0, (int)$max_questions);
806 $real_sql_query .= ' MAX_QUERIES_PER_HOUR ' . $max_questions;
807 $sql_query .= ' MAX_QUERIES_PER_HOUR ' . $max_questions;
809 if (isset($max_connections)) {
810 $max_connections = max(0, (int)$max_connections);
811 $real_sql_query .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections;
812 $sql_query .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections;
814 if (isset($max_updates)) {
815 $max_updates = max(0, (int)$max_updates);
816 $real_sql_query .= ' MAX_UPDATES_PER_HOUR ' . $max_updates;
817 $sql_query .= ' MAX_UPDATES_PER_HOUR ' . $max_updates;
820 if (PMA_MYSQL_INT_VERSION >= 50003) {
821 if (isset($max_user_connections)) {
822 $max_user_connections = max(0, (int)$max_user_connections);
823 $real_sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections;
824 $sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections;
828 if (isset($create_user_real)) {
829 $create_user_real .= ';';
830 $create_user_show .= ';';
832 $real_sql_query .= ';';
833 $sql_query .= ';';
834 if (empty($change_copy)) {
835 if (isset($create_user_real)) {
836 PMA_DBI_try_query($create_user_real) or PMA_mysqlDie(PMA_DBI_getError(), $create_user_show);
837 $sql_query = $create_user_show . $sql_query;
839 PMA_DBI_try_query($real_sql_query) or PMA_mysqlDie(PMA_DBI_getError(), $sql_query);
840 $message = $GLOBALS['strAddUserMessage'];
842 /* Create database for new user */
843 if (isset($createdb) && $createdb > 0) {
844 if ($createdb == 1) {
845 $q = 'CREATE DATABASE IF NOT EXISTS ' . PMA_backquote(PMA_sqlAddslashes($username)) . ';';
846 $sql_query .= $q;
847 PMA_DBI_try_query($q) or PMA_mysqlDie(PMA_DBI_getError(), $sql_query);
848 $GLOBALS['reload'] = TRUE;
849 PMA_reloadNavigation();
851 $q = 'GRANT ALL PRIVILEGES ON ' . PMA_backquote(PMA_sqlAddslashes($username)) . '.* TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\';';
852 $sql_query .= $q;
853 PMA_DBI_try_query($q) or PMA_mysqlDie(PMA_DBI_getError(), $sql_query);
854 } elseif ($createdb == 2) {
855 $q = 'GRANT ALL PRIVILEGES ON ' . PMA_backquote(PMA_sqlAddslashes($username) . '\_%') . '.* TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\';';
856 $sql_query .= $q;
857 PMA_DBI_try_query($q) or PMA_mysqlDie(PMA_DBI_getError(), $sql_query);
860 } else {
861 if (isset($create_user_real)) {
862 $queries[] = $create_user_real;
864 $queries[] = $real_sql_query;
865 // we put the query containing the hidden password in
866 // $queries_for_display, at the same position occupied
867 // by the real query in $queries
868 $tmp_count = count($queries);
869 if (isset($create_user_real)) {
870 $queries_for_display[$tmp_count - 2] = $create_user_show;
872 $queries_for_display[$tmp_count - 1] = $sql_query;
874 unset($res, $real_sql_query);
880 * Changes / copies a user, part III
882 if (!empty($change_copy)) {
883 $user_host_condition =
884 ' WHERE ' . PMA_convert_using('User')
885 .' = ' . PMA_convert_using(PMA_sqlAddslashes($old_username), 'quoted')
886 .' AND ' . PMA_convert_using('Host')
887 .' = ' . PMA_convert_using($old_hostname, 'quoted') . ';';
888 $res = PMA_DBI_query('SELECT * FROM `mysql`.`db`' . $user_host_condition);
889 while ($row = PMA_DBI_fetch_assoc($res)) {
890 $queries[] =
891 'GRANT ' . join(', ', PMA_extractPrivInfo($row))
892 .' ON `' . $row['Db'] . '`.*'
893 .' TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\''
894 . ($row['Grant_priv'] == 'Y' ? ' WITH GRANT OPTION;' : ';');
896 PMA_DBI_free_result($res);
897 $res = PMA_DBI_query(
898 'SELECT `Db`, `Table_name`, `Table_priv`'
899 .' FROM `mysql`.`tables_priv`' . $user_host_condition,
900 $GLOBALS['userlink'], PMA_DBI_QUERY_STORE);
901 while ($row = PMA_DBI_fetch_assoc($res)) {
903 $res2 = PMA_DBI_QUERY(
904 'SELECT `Column_name`, `Column_priv`'
905 .' FROM `mysql`.`columns_priv`'
906 .' WHERE ' . PMA_convert_using('User')
907 .' = ' . PMA_convert_using(PMA_sqlAddslashes($old_username), 'quoted')
908 .' AND ' . PMA_convert_using('`Host`')
909 .' = ' . PMA_convert_using($old_hostname, 'quoted')
910 .' AND ' . PMA_convert_using('`Db`')
911 .' = ' . PMA_convert_using($row['Db'], 'quoted')
912 .' AND ' . PMA_convert_using('`Table_name`')
913 .' = ' . PMA_convert_using($row['Table_name'], 'quoted')
914 .';',
915 null, PMA_DBI_QUERY_STORE);
917 $tmp_privs1 = PMA_extractPrivInfo($row);
918 $tmp_privs2 = array(
919 'Select' => array(),
920 'Insert' => array(),
921 'Update' => array(),
922 'References' => array()
925 while ($row2 = PMA_DBI_fetch_assoc($res2)) {
926 $tmp_array = explode(',', $row2['Column_priv']);
927 if (in_array('Select', $tmp_array)) {
928 $tmp_privs2['Select'][] = $row2['Column_name'];
930 if (in_array('Insert', $tmp_array)) {
931 $tmp_privs2['Insert'][] = $row2['Column_name'];
933 if (in_array('Update', $tmp_array)) {
934 $tmp_privs2['Update'][] = $row2['Column_name'];
936 if (in_array('References', $tmp_array)) {
937 $tmp_privs2['References'][] = $row2['Column_name'];
939 unset($tmp_array);
941 if (count($tmp_privs2['Select']) > 0 && !in_array('SELECT', $tmp_privs1)) {
942 $tmp_privs1[] = 'SELECT (`' . join('`, `', $tmp_privs2['Select']) . '`)';
944 if (count($tmp_privs2['Insert']) > 0 && !in_array('INSERT', $tmp_privs1)) {
945 $tmp_privs1[] = 'INSERT (`' . join('`, `', $tmp_privs2['Insert']) . '`)';
947 if (count($tmp_privs2['Update']) > 0 && !in_array('UPDATE', $tmp_privs1)) {
948 $tmp_privs1[] = 'UPDATE (`' . join('`, `', $tmp_privs2['Update']) . '`)';
950 if (count($tmp_privs2['References']) > 0 && !in_array('REFERENCES', $tmp_privs1)) {
951 $tmp_privs1[] = 'REFERENCES (`' . join('`, `', $tmp_privs2['References']) . '`)';
953 unset($tmp_privs2);
954 $queries[] =
955 'GRANT ' . join(', ', $tmp_privs1)
956 . ' ON `' . $row['Db'] . '`.`' . $row['Table_name']
957 . '` TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\''
958 . (in_array('Grant', explode(',', $row['Table_priv'])) ? ' WITH GRANT OPTION;' : ';');
964 * Updates privileges
966 if (!empty($update_privs)) {
967 // escaping a wildcard character in a GRANT is only accepted at the global
968 // or database level, not at table level; this is why I remove
969 // the escaping character
970 // Note: in the phpMyAdmin list of Database-specific privileges,
971 // we will have for example
972 // test\_db SELECT (this one is for privileges on a db level)
973 // test_db USAGE (this one is for table-specific privileges)
975 // It looks curious but reflects the way MySQL works
977 if (! isset($dbname) || ! strlen($dbname)) {
978 $db_and_table = '*.*';
979 } else {
980 if (isset($tablename) && strlen($tablename)) {
981 $db_and_table = PMA_backquote(PMA_unescape_mysql_wildcards($dbname)) . '.';
982 $db_and_table .= PMA_backquote($tablename);
983 } else {
984 $db_and_table = PMA_backquote($dbname) . '.';
985 $db_and_table .= '*';
989 $sql_query0 =
990 'REVOKE ALL PRIVILEGES ON ' . $db_and_table
991 . ' FROM \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\';';
992 if (!isset($Grant_priv) || $Grant_priv != 'Y') {
993 $sql_query1 =
994 'REVOKE GRANT OPTION ON ' . $db_and_table
995 . ' FROM \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\';';
997 $sql_query2 =
998 'GRANT ' . join(', ', PMA_extractPrivInfo())
999 . ' ON ' . $db_and_table
1000 . ' TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\'';
1003 * @todo similar code appears twice in this script
1005 if ((isset($Grant_priv) && $Grant_priv == 'Y')
1006 || ((! isset($dbname) || ! strlen($dbname)) && PMA_MYSQL_INT_VERSION >= 40002
1007 && (isset($max_questions) || isset($max_connections)
1008 || isset($max_updates) || isset($max_user_connections))))
1010 $sql_query2 .= 'WITH';
1011 if (isset($Grant_priv) && $Grant_priv == 'Y') {
1012 $sql_query2 .= ' GRANT OPTION';
1014 if (PMA_MYSQL_INT_VERSION >= 40002) {
1015 if (isset($max_questions)) {
1016 $max_questions = max(0, (int)$max_questions);
1017 $sql_query2 .= ' MAX_QUERIES_PER_HOUR ' . $max_questions;
1019 if (isset($max_connections)) {
1020 $max_connections = max(0, (int)$max_connections);
1021 $sql_query2 .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections;
1023 if (isset($max_updates)) {
1024 $max_updates = max(0, (int)$max_updates);
1025 $sql_query2 .= ' MAX_UPDATES_PER_HOUR ' . $max_updates;
1028 if (PMA_MYSQL_INT_VERSION >= 50003) {
1029 if (isset($max_user_connections)) {
1030 $max_user_connections = max(0, (int)$max_user_connections);
1031 $sql_query2 .= ' MAX_USER_CONNECTIONS ' . $max_user_connections;
1035 $sql_query2 .= ';';
1036 if (!PMA_DBI_try_query($sql_query0)) { // this query may fail, but this does not matter :o)
1037 // a case when it can fail is when the admin does not have all
1038 // privileges: he can't do a REVOKE ALL PRIVILEGES !
1039 // so at least we display the error
1040 echo PMA_DBI_getError();
1041 unset($sql_query0);
1043 if (isset($sql_query1) && !PMA_DBI_try_query($sql_query1)) { // this one may fail, too...
1044 unset($sql_query1);
1046 PMA_DBI_query($sql_query2);
1047 $sql_query = (isset($sql_query0) ? $sql_query0 . ' ' : '')
1048 . (isset($sql_query1) ? $sql_query1 . ' ' : '')
1049 . $sql_query2;
1050 $message = sprintf($GLOBALS['strUpdatePrivMessage'], '\'' . $username . '\'@\'' . $hostname . '\'');
1055 * Revokes Privileges
1057 if (!empty($revokeall)) {
1059 if (! isset($dbname) || ! strlen($dbname)) {
1060 $db_and_table = '*.*';
1061 } else {
1062 if (! isset($tablename) || ! strlen($tablename)) {
1063 $db_and_table = PMA_backquote($dbname) . '.';
1064 $db_and_table .= '*';
1065 } else {
1066 $db_and_table = PMA_backquote(PMA_unescape_mysql_wildcards($dbname)) . '.';
1067 $db_and_table .= PMA_backquote($tablename);
1071 $sql_query0 =
1072 'REVOKE ALL PRIVILEGES ON ' . $db_and_table
1073 . ' FROM \'' . $username . '\'@\'' . $hostname . '\';';
1074 $sql_query1 =
1075 'REVOKE GRANT OPTION ON ' . $db_and_table
1076 . ' FROM \'' . $username . '\'@\'' . $hostname . '\';';
1077 PMA_DBI_query($sql_query0);
1078 if (!PMA_DBI_try_query($sql_query1)) { // this one may fail, too...
1079 unset($sql_query1);
1081 $sql_query = $sql_query0 . (isset($sql_query1) ? ' ' . $sql_query1 : '');
1082 $message = sprintf($GLOBALS['strRevokeMessage'], '\'' . $username . '\'@\'' . $hostname . '\'');
1083 if (! isset($tablename) || ! strlen($tablename)) {
1084 unset($dbname);
1085 } else {
1086 unset($tablename);
1092 * Updates the password
1094 if (!empty($change_pw)) {
1095 // similar logic in user_password.php
1096 $message = '';
1098 if ($nopass == 0 && isset($pma_pw) && isset($pma_pw2)) {
1099 if ($pma_pw != $pma_pw2) {
1100 $message = $strPasswordNotSame;
1102 if (empty($pma_pw) || empty($pma_pw2)) {
1103 $message = $strPasswordEmpty;
1105 } // end if
1107 // here $nopass could be == 1
1108 if (empty($message)) {
1110 $hashing_function = (PMA_MYSQL_INT_VERSION >= 40102 && !empty($pw_hash) && $pw_hash == 'old' ? 'OLD_' : '')
1111 . 'PASSWORD';
1113 // in $sql_query which will be displayed, hide the password
1114 $sql_query = 'SET PASSWORD FOR \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\' = ' . (($pma_pw == '') ? '\'\'' : $hashing_function . '(\'' . preg_replace('@.@s', '*', $pma_pw) . '\')');
1115 $local_query = 'SET PASSWORD FOR \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\' = ' . (($pma_pw == '') ? '\'\'' : $hashing_function . '(\'' . PMA_sqlAddslashes($pma_pw) . '\')');
1116 PMA_DBI_try_query($local_query) or PMA_mysqlDie(PMA_DBI_getError(), $sql_query, FALSE, $err_url);
1117 $message = sprintf($GLOBALS['strPasswordChanged'], '\'' . $username . '\'@\'' . $hostname . '\'');
1123 * Deletes users
1124 * (Changes / copies a user, part IV)
1126 $user_host_separator = chr(27);
1128 if (!empty($delete) || (!empty($change_copy) && $mode < 4)) {
1129 if (!empty($change_copy)) {
1130 $selected_usr = array($old_username . $user_host_separator . $old_hostname);
1131 } else {
1132 $queries = array();
1134 for ($i = 0; isset($selected_usr[$i]); $i++) {
1135 list($this_user, $this_host) = explode($user_host_separator, $selected_usr[$i]);
1136 $queries[] = '# ' . sprintf($GLOBALS['strDeleting'], '\'' . $this_user . '\'@\'' . $this_host . '\'') . ' ...';
1137 if (PMA_MYSQL_INT_VERSION >= 50002) {
1138 $queries[] = 'DROP USER \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1139 } else {
1140 if ($mode == 2) {
1141 // The SHOW GRANTS query may fail if the user has not been loaded
1142 // into memory
1143 $res = PMA_DBI_try_query('SHOW GRANTS FOR \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';');
1144 if ($res) {
1145 $queries[] = 'REVOKE ALL PRIVILEGES ON *.* FROM \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1146 while ($row = PMA_DBI_fetch_row($res)) {
1147 $this_table = substr($row[0], (strpos($row[0], 'ON') + 3), (strpos($row[0], ' TO ') - strpos($row[0], 'ON') - 3));
1148 if ($this_table != '*.*') {
1149 $queries[] = 'REVOKE ALL PRIVILEGES ON ' . $this_table . ' FROM \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1151 if (strpos($row[0], 'WITH GRANT OPTION')) {
1152 $queries[] = 'REVOKE GRANT OPTION ON ' . $this_table . ' FROM \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1155 unset($this_table);
1157 PMA_DBI_free_result($res);
1159 unset($res);
1161 if (PMA_MYSQL_INT_VERSION >= 40101) {
1162 if (PMA_MYSQL_INT_VERSION < 50002) {
1163 $queries[] = 'REVOKE GRANT OPTION ON *.* FROM \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1165 $queries[] = 'DROP USER \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1166 } else {
1167 $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') . ';';
1169 if ($mode != 2) {
1170 // If we REVOKE the table grants, we should not need to modify the
1171 // `mysql`.`db`, `mysql`.`tables_priv` and `mysql`.`columns_priv` tables manually...
1172 $user_host_condition =
1173 ' WHERE ' . PMA_convert_using('User')
1174 . ' = ' . PMA_convert_using(PMA_sqlAddslashes($this_user), 'quoted')
1175 . ' AND ' . PMA_convert_using('Host')
1176 . ' = ' . PMA_convert_using($this_host, 'quoted') . ';';
1177 $queries[] = 'DELETE FROM `mysql`.`db`' . $user_host_condition;
1178 $queries[] = 'DELETE FROM `mysql`.`tables_priv`' . $user_host_condition;
1179 $queries[] = 'DELETE FROM `mysql`.`columns_priv`' . $user_host_condition;
1182 if (!empty($drop_users_db)) {
1183 $queries[] = 'DROP DATABASE IF EXISTS ' . PMA_backquote($this_user) . ';';
1184 $GLOBALS['reload'] = TRUE;
1185 PMA_reloadNavigation();
1188 if (empty($change_copy)) {
1189 if (empty($queries)) {
1190 $show_error_header = TRUE;
1191 $message = $GLOBALS['strDeleteNoUsersSelected'];
1192 } else {
1193 if ($mode == 3) {
1194 $queries[] = '# ' . $GLOBALS['strReloadingThePrivileges'] . ' ...';
1195 $queries[] = 'FLUSH PRIVILEGES;';
1197 foreach ($queries as $sql_query) {
1198 if ($sql_query{0} != '#') {
1199 PMA_DBI_query($sql_query, $GLOBALS['userlink']);
1202 $sql_query = join("\n", $queries);
1203 $message = $GLOBALS['strUsersDeleted'];
1205 unset($queries);
1211 * Changes / copies a user, part V
1213 if (!empty($change_copy)) {
1214 $tmp_count = 0;
1215 foreach ($queries as $sql_query) {
1216 if ($sql_query{0} != '#') {
1217 PMA_DBI_query($sql_query);
1219 // when there is a query containing a hidden password, take it
1220 // instead of the real query sent
1221 if (isset($queries_for_display[$tmp_count])) {
1222 $queries[$tmp_count] = $queries_for_display[$tmp_count];
1224 $tmp_count++;
1226 $message = $GLOBALS['strSuccess'];
1227 $sql_query = join("\n", $queries);
1232 * Reloads the privilege tables into memory
1234 if (!empty($flush_privileges)) {
1235 $sql_query = 'FLUSH PRIVILEGES;';
1236 PMA_DBI_query($sql_query);
1237 $message = $GLOBALS['strPrivilegesReloaded'];
1242 * Displays the links
1244 if (isset($viewing_mode) && $viewing_mode == 'db') {
1245 $db = $checkprivs;
1246 $url_query .= '&amp;goto=db_operations.php';
1248 // Gets the database structure
1249 $sub_part = '_structure';
1250 require './libraries/db_info.inc.php';
1251 echo "\n";
1252 } else {
1253 require './libraries/server_links.inc.php';
1258 * defines some standard links
1260 $link_edit = '<a href="server_privileges.php?' . $GLOBALS['url_query']
1261 .'&amp;username=%s'
1262 .'&amp;hostname=%s'
1263 .'&amp;dbname=%s'
1264 .'&amp;tablename=%s">';
1265 if ($GLOBALS['cfg']['PropertiesIconic']) {
1266 $link_edit .= '<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_usredit.png" width="16" height="16" alt="' . $GLOBALS['strEditPrivileges'] . '" title="' . $GLOBALS['strEditPrivileges'] . '" />';
1267 } else {
1268 $link_edit .= $GLOBALS['strEditPrivileges'];
1270 $link_edit .= '</a>';
1272 $link_revoke = '<a href="server_privileges.php?' . $GLOBALS['url_query']
1273 .'&amp;username=%s'
1274 .'&amp;hostname=%s'
1275 .'&amp;dbname=%s'
1276 .'&amp;tablename=%s'
1277 .'&amp;revokeall=1">';
1278 if ($GLOBALS['cfg']['PropertiesIconic']) {
1279 $link_revoke .= '<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_usrdrop.png" width="16" height="16" alt="' . $GLOBALS['strRevoke'] . '" title="' . $GLOBALS['strRevoke'] . '" />';
1280 } else {
1281 $link_revoke .= $GLOBALS['strRevoke'];
1283 $link_revoke .= '</a>';
1286 * Displays the page
1288 if (empty($adduser) && (! isset($checkprivs) || ! strlen($checkprivs))) {
1289 if (! isset($username)) {
1290 // No username is given --> display the overview
1291 echo '<h2>' . "\n"
1292 . ($GLOBALS['cfg']['MainPageIconic'] ? '<img class="icon" src="'. $GLOBALS['pmaThemeImage'] . 'b_usrlist.png" alt="" />' : '')
1293 . $GLOBALS['strUserOverview'] . "\n"
1294 . '</h2>' . "\n";
1296 $sql_query =
1297 'SELECT `User`,' .
1298 ' `Host`,' .
1299 ' IF(`Password` = ' . (PMA_MYSQL_INT_VERSION >= 40100 ? '_latin1 ' : '') . '\'\', \'N\', \'Y\') AS \'Password\',' .
1300 ' `Select_priv`,' .
1301 ' `Insert_priv`,' .
1302 ' `Update_priv`,' .
1303 ' `Delete_priv`,' .
1304 ' `Index_priv`,' .
1305 ' `Alter_priv`,' .
1306 ' `Create_priv`,' .
1307 ' `Drop_priv`,' .
1308 ' `Grant_priv`,' .
1309 ' `References_priv`,' .
1310 ' `Reload_priv`,' .
1311 ' `Shutdown_priv`,' .
1312 ' `Process_priv`,' .
1313 ' `File_priv`';
1315 if (PMA_MYSQL_INT_VERSION >= 40002) {
1316 $sql_query .= ', `Show_db_priv`, `Super_priv`, `Create_tmp_table_priv`, `Lock_tables_priv`, `Execute_priv`, `Repl_slave_priv`, `Repl_client_priv`';
1319 if (PMA_MYSQL_INT_VERSION >= 50001) {
1320 $sql_query .= ', `Create_view_priv`, `Show_view_priv`';
1323 if (PMA_MYSQL_INT_VERSION >= 50003) {
1324 $sql_query .= ', `Create_user_priv`, `Create_routine_priv`, `Alter_routine_priv`';
1327 $sql_query .= ' FROM `mysql`.`user`';
1329 $sql_query .= (isset($initial) ? PMA_RangeOfUsers($initial) : '');
1331 $sql_query .= ' ORDER BY `User` ASC, `Host` ASC;';
1332 $res = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_STORE);
1334 if (! $res) {
1335 // the query failed! This may have two reasons:
1336 // - the user does not have enough privileges
1337 // - the privilege tables use a structure of an earlier version.
1338 // so let's try a more simple query
1340 $sql_query = 'SELECT * FROM `mysql`.`user`';
1341 $res = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_STORE);
1343 if (!$res) {
1344 echo '<i>' . $GLOBALS['strNoPrivileges'] . '</i>' . "\n";
1345 PMA_DBI_free_result($res);
1346 unset($res);
1347 } else {
1348 // rabus: This message is hardcoded because I will replace it by
1349 // a automatic repair feature soon.
1350 echo '<div class="warning">' . "\n"
1351 . ' Warning: Your privilege table structure seems to be older than this MySQL version!<br />' . "\n"
1352 . ' Please run the script <tt>mysql_fix_privilege_tables</tt> that should be included in your MySQL server distribution to solve this problem!' . "\n"
1353 . '</div><br />' . "\n";
1355 } else {
1357 // we also want users not in table `user` but in other table
1358 $tables = PMA_DBI_fetch_result('SHOW TABLES FROM `mysql`;');
1360 $tables_to_search_for_users = array(
1361 'user', 'db', 'tables_priv', 'columns_priv', 'procs_priv',
1364 $db_rights_sqls = array();
1365 foreach ($tables_to_search_for_users as $table_search_in) {
1366 if (in_array($table_search_in, $tables)) {
1367 $db_rights_sqls[] = 'SELECT DISTINCT `User`, `Host` FROM `mysql`.`' . $table_search_in . '` ' . (isset($initial) ? PMA_RangeOfUsers($initial) : '');
1371 $user_defaults = array(
1372 'User' => '',
1373 'Host' => '%',
1374 'Password' => '?',
1375 'Grant_priv' => 'N',
1376 'privs' => array('USAGE'),
1379 // for all initials, even non A-Z
1380 $array_initials = array();
1381 // for the rights
1382 $db_rights = array();
1384 // do not use UNION DISTINCT, as it's not allowed before
1385 // MySQL 4.0.17, and because "it does nothing" (cf manual)
1386 if (PMA_MYSQL_INT_VERSION >= 40000) {
1387 $db_rights_sql = '(' . implode(') UNION (', $db_rights_sqls) . ')'
1388 .' ORDER BY `User` ASC, `Host` ASC';
1390 $db_rights_result = PMA_DBI_query($db_rights_sql);
1392 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1393 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1394 $db_rights[$db_rights_row['User']][$db_rights_row['Host']] =
1395 $db_rights_row;
1397 } else {
1398 foreach ($db_rights_sqls as $db_rights_sql) {
1399 $db_rights_result = PMA_DBI_query($db_rights_sql);
1401 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1402 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1403 $db_rights[$db_rights_row['User']][$db_rights_row['Host']] =
1404 $db_rights_row;
1408 PMA_DBI_free_result($db_rights_result);
1409 unset($db_rights_sql, $db_rights_sqls, $db_rights_result, $db_rights_row);
1410 ksort($db_rights);
1413 * Displays the initials
1416 // initialize to FALSE the letters A-Z
1417 for ($letter_counter = 1; $letter_counter < 27; $letter_counter++) {
1418 if (! isset($array_initials[chr($letter_counter + 64)])) {
1419 $array_initials[chr($letter_counter + 64)] = FALSE;
1423 $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);
1424 while (list($tmp_initial) = PMA_DBI_fetch_row($initials)) {
1425 $array_initials[$tmp_initial] = TRUE;
1428 // Display the initials, which can be any characters, not
1429 // just letters. For letters A-Z, we add the non-used letters
1430 // as greyed out.
1432 uksort($array_initials, "strnatcasecmp");
1434 echo '<table cellspacing="5"><tr>';
1435 foreach ($array_initials as $tmp_initial => $initial_was_found) {
1436 if ($initial_was_found) {
1437 echo '<td><a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;initial=' . urlencode($tmp_initial) . '">' . $tmp_initial . '</a></td>' . "\n";
1438 } else {
1439 echo '<td>' . $tmp_initial . '</td>';
1442 echo '<td><a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;showall=1">[' . $GLOBALS['strShowAll'] . ']</a></td>' . "\n";
1443 echo '</tr></table>';
1446 * Display the user overview
1447 * (if less than 50 users, display them immediately)
1450 if (isset($initial) || isset($showall) || PMA_DBI_num_rows($res) < 50) {
1452 while ($row = PMA_DBI_fetch_assoc($res)) {
1453 $row['privs'] = PMA_extractPrivInfo($row, true);
1454 $db_rights[$row['User']][$row['Host']] = $row;
1456 @PMA_DBI_free_result($res);
1457 unset($res);
1459 echo '<form name="usersForm" id="usersForm" action="server_privileges.php" method="post">' . "\n"
1460 . PMA_generate_common_hidden_inputs('', '', 1)
1461 . ' <table id="tableuserrights" class="data">' . "\n"
1462 . ' <thead>' . "\n"
1463 . ' <tr><td></td>' . "\n"
1464 . ' <th>' . $GLOBALS['strUser'] . '</th>' . "\n"
1465 . ' <th>' . $GLOBALS['strHost'] . '</th>' . "\n"
1466 . ' <th>' . $GLOBALS['strPassword'] . '</th>' . "\n"
1467 . ' <th>' . $GLOBALS['strGlobalPrivileges'] . ' '
1468 . PMA_showHint($GLOBALS['strEnglishPrivileges']) . '</th>' . "\n"
1469 . ' <th>' . $GLOBALS['strGrantOption'] . '</th>' . "\n"
1470 . ' ' . ($GLOBALS['cfg']['PropertiesIconic'] ? '<td></td>' : '<th>' . $GLOBALS['strAction'] . '</th>') . "\n";
1471 echo ' </tr>' . "\n";
1472 echo ' </thead>' . "\n";
1473 echo ' <tbody>' . "\n";
1474 $odd_row = true;
1475 $index_checkbox = -1;
1476 foreach ($db_rights as $user) {
1477 $index_checkbox++;
1478 ksort($user);
1479 foreach ($user as $host) {
1480 $index_checkbox++;
1481 echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n"
1482 . ' <td><input type="checkbox" name="selected_usr[]" id="checkbox_sel_users_' . $index_checkbox . '" value="' . str_replace(chr(27), '&#27;', htmlentities($host['User'] . $user_host_separator . $host['Host'])) . '"' . (empty($GLOBALS['checkall']) ? '' : ' checked="checked"') . ' /></td>' . "\n"
1483 . ' <td><label for="checkbox_sel_users_' . $index_checkbox . '">' . (empty($host['User']) ? '<span style="color: #FF0000">' . $GLOBALS['strAny'] . '</span>' : htmlspecialchars($host['User'])) . '</label></td>' . "\n"
1484 . ' <td>' . htmlspecialchars($host['Host']) . '</td>' . "\n";
1485 echo ' <td>';
1486 switch ($host['Password']) {
1487 case 'Y':
1488 echo $GLOBALS['strYes'];
1489 break;
1490 case 'N':
1491 echo '<span style="color: #FF0000">' . $GLOBALS['strNo'] . '</span>';
1492 break;
1493 // this happens if this is a definition not coming from mysql.user
1494 default:
1495 echo '--'; // in future version, replace by "not present"
1496 break;
1497 } // end switch
1498 echo '</td>' . "\n"
1499 . ' <td><tt>' . "\n"
1500 . ' ' . implode(',' . "\n" . ' ', $host['privs']) . "\n"
1501 . ' </tt></td>' . "\n"
1502 . ' <td>' . ($host['Grant_priv'] == 'Y' ? $GLOBALS['strYes'] : $GLOBALS['strNo']) . '</td>' . "\n"
1503 . ' <td align="center">';
1504 printf($link_edit, urlencode($host['User']),
1505 urlencode($host['Host']), '', '');
1506 echo '</td>' . "\n"
1507 . ' </tr>' . "\n";
1508 $odd_row = ! $odd_row;
1512 unset($user, $host, $odd_row);
1513 echo ' </tbody></table>' . "\n"
1514 .'<img class="selectallarrow"'
1515 .' src="' . $pmaThemeImage . 'arrow_' . $text_dir . '.png"'
1516 .' width="38" height="22"'
1517 .' alt="' . $GLOBALS['strWithChecked'] . '" />' . "\n"
1518 .'<a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;checkall=1"'
1519 .' onclick="if (markAllRows(\'usersForm\')) return false;">'
1520 . $GLOBALS['strCheckAll'] . '</a>' . "\n"
1521 .'/' . "\n"
1522 .'<a href="server_privileges.php?' . $GLOBALS['url_query'] . '"'
1523 .' onclick="if (unMarkAllRows(\'usersForm\')) return false;">'
1524 . $GLOBALS['strUncheckAll'] . '</a>' . "\n";
1526 // add/delete user fieldset
1527 echo ' <fieldset id="fieldset_add_user">' . "\n"
1528 . ' <a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;adduser=1">' . "\n"
1529 . ($GLOBALS['cfg']['PropertiesIconic'] ? ' <img class="icon" src="' . $pmaThemeImage . 'b_usradd.png" width="16" height="16" alt="" />' . "\n" : '')
1530 . ' ' . $GLOBALS['strAddUser'] . '</a>' . "\n"
1531 . ' </fieldset>' . "\n"
1532 . ' <fieldset id="fieldset_delete_user">'
1533 . ' <legend>' . "\n"
1534 . ($GLOBALS['cfg']['PropertiesIconic'] ? ' <img class="icon" src="' . $pmaThemeImage . 'b_usrdrop.png" width="16" height="16" alt="" />' . "\n" : '')
1535 . ' ' . $GLOBALS['strRemoveSelectedUsers'] . '' . "\n"
1536 . ' </legend>' . "\n";
1538 // before MySQL 4.1.1, we offer some choices for the delete
1539 // mode, but for 4.1.1+, it will be done with REVOKEing the
1540 // privileges then a DROP USER (even no REVOKE at all
1541 // for MySQL 5), so no need to offer so many options
1542 if (PMA_MYSQL_INT_VERSION < 40101) {
1543 echo ' <input type="radio" title="' . $GLOBALS['strJustDelete'] . ' ' . $GLOBALS['strJustDeleteDescr'] . '" name="mode" id="radio_mode_1" value="1" checked="checked" />' . "\n"
1544 . ' <label for="radio_mode_1" title="' . $GLOBALS['strJustDelete'] . ' ' . $GLOBALS['strJustDeleteDescr'] . '">' . "\n"
1545 . ' ' . $GLOBALS['strJustDelete'] . "\n"
1546 . ' </label><br />' . "\n"
1547 . ' <input type="radio" title="' . $GLOBALS['strRevokeAndDelete'] . ' ' . $GLOBALS['strRevokeAndDeleteDescr'] . '" name="mode" id="radio_mode_2" value="2" />' . "\n"
1548 . ' <label for="radio_mode_2" title="' . $GLOBALS['strRevokeAndDelete'] . ' ' . $GLOBALS['strRevokeAndDeleteDescr'] . '">' . "\n"
1549 . ' ' . $GLOBALS['strRevokeAndDelete'] . "\n"
1550 . ' </label><br />' . "\n"
1551 . ' <input type="radio" title="' . $GLOBALS['strDeleteAndFlush'] . ' ' . $GLOBALS['strDeleteAndFlushDescr'] . '" name="mode" id="radio_mode_3" value="3" />' . "\n"
1552 . ' <label for="radio_mode_3" title="' . $GLOBALS['strDeleteAndFlush'] . ' ' . $GLOBALS['strDeleteAndFlushDescr'] . '">' . "\n"
1553 . ' ' . $GLOBALS['strDeleteAndFlush'] . "\n"
1554 . ' </label><br />' . "\n";
1555 } else {
1556 echo ' <input type="hidden" name="mode" value="2" />' . "\n"
1557 . '(' . $GLOBALS['strRevokeAndDelete'] . ')<br />' . "\n";
1560 echo ' <input type="checkbox" title="' . $GLOBALS['strDropUsersDb'] . '" name="drop_users_db" id="checkbox_drop_users_db" />' . "\n"
1561 . ' <label for="checkbox_drop_users_db" title="' . $GLOBALS['strDropUsersDb'] . '">' . "\n"
1562 . ' ' . $GLOBALS['strDropUsersDb'] . "\n"
1563 . ' </label>' . "\n"
1564 . ' </fieldset>' . "\n"
1565 . ' <fieldset id="fieldset_delete_user_footer" class="tblFooters">' . "\n"
1566 . ' <input type="submit" name="delete" value="' . $GLOBALS['strGo'] . '" id="buttonGo" />' . "\n"
1567 . ' </fieldset>' . "\n";
1568 } else {
1570 unset ($row);
1571 echo ' <fieldset id="fieldset_add_user">' . "\n"
1572 . ' <a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;adduser=1">' . "\n"
1573 . ($GLOBALS['cfg']['PropertiesIconic'] ? ' <img class="icon" src="' . $pmaThemeImage . 'b_usradd.png" width="16" height="16" alt="" />' . "\n" : '')
1574 . ' ' . $GLOBALS['strAddUser'] . '</a>' . "\n"
1575 . ' </fieldset>' . "\n";
1576 } // end if (display overview)
1577 echo '</form>' . "\n"
1578 . '<div class="warning">' . "\n"
1579 . ' ' . sprintf($GLOBALS['strFlushPrivilegesNote'], '<a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;flush_privileges=1">', '</a>') . "\n"
1580 . '</div>' . "\n";
1584 } else {
1586 // A user was selected -> display the user's properties
1588 echo '<h2>' . "\n"
1589 . ($GLOBALS['cfg']['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_usredit.png" width="16" height="16" alt="" />' : '')
1590 . $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";
1591 if (isset($dbname) && strlen($dbname)) {
1592 if ($dbname_is_wildcard) {
1593 echo ' - ' . $GLOBALS['strDatabases'];
1594 } else {
1595 echo ' - ' . $GLOBALS['strDatabase'];
1597 $url_dbname = urlencode(str_replace('\_', '_', $dbname));
1598 echo ' <i><a href="' . $GLOBALS['cfg']['DefaultTabDatabase'] . '?' . $GLOBALS['url_query'] . '&amp;db=' . $url_dbname . '&amp;reload=1">' . htmlspecialchars($dbname) . '</a></i>' . "\n";
1599 if (isset($tablename) && strlen($tablename)) {
1600 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";
1602 unset($url_dbname);
1604 echo ' : ' . $GLOBALS['strEditPrivileges'] . '</h2>' . "\n";
1605 $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);
1606 $user_does_not_exists = (PMA_DBI_num_rows($res) < 1);
1607 PMA_DBI_free_result($res);
1608 unset($res);
1609 if ($user_does_not_exists) {
1610 echo $GLOBALS['strUserNotFound'];
1611 PMA_displayLoginInformationFields();
1612 //require_once './libraries/footer.inc.php';
1614 echo '<form name="usersForm" id="usersForm" action="server_privileges.php" method="post">' . "\n"
1615 . PMA_generate_common_hidden_inputs('', '', 3)
1616 . '<input type="hidden" name="username" value="' . htmlspecialchars($username) . '" />' . "\n"
1617 . '<input type="hidden" name="hostname" value="' . htmlspecialchars($hostname) . '" />' . "\n";
1618 if (isset($dbname) && strlen($dbname)) {
1619 echo '<input type="hidden" name="dbname" value="' . htmlspecialchars($dbname) . '" />' . "\n";
1620 if (isset($tablename) && strlen($tablename)) {
1621 echo ' <input type="hidden" name="tablename" value="' . htmlspecialchars($tablename) . '" />' . "\n";
1624 PMA_displayPrivTable(((! isset($dbname) || ! strlen($dbname)) ? '*' : $dbname),
1625 (((! isset($dbname) || ! strlen($dbname)) || (! isset($tablename) || ! strlen($tablename))) ? '*' : $tablename),
1626 TRUE, 3);
1627 echo '</form>' . "\n";
1629 if ((! isset($tablename) || ! strlen($tablename)) && empty($dbname_is_wildcard)) {
1631 // no table name was given, display all table specific rights
1632 // but only if $dbname contains no wildcards
1634 // table header
1635 echo '<form action="server_privileges.php" method="post">' . "\n"
1636 . PMA_generate_common_hidden_inputs('', '', 6)
1637 . '<input type="hidden" name="username" value="' . htmlspecialchars($username) . '" />' . "\n"
1638 . '<input type="hidden" name="hostname" value="' . htmlspecialchars($hostname) . '" />' . "\n"
1639 . '<fieldset>' . "\n"
1640 . '<legend>' . ((! isset($dbname) || ! strlen($dbname)) ? $GLOBALS['strDbPrivileges'] : $GLOBALS['strTblPrivileges']) . '</legend>' . "\n"
1641 . '<table class="data">' . "\n"
1642 . '<thead>' . "\n"
1643 . '<tr><th>' . ((! isset($dbname) || ! strlen($dbname)) ? $GLOBALS['strDatabase'] : $GLOBALS['strTable']) . '</th>' . "\n"
1644 . ' <th>' . $GLOBALS['strPrivileges'] . '</th>' . "\n"
1645 . ' <th>' . $GLOBALS['strGrantOption'] . '</th>' . "\n"
1646 . ' <th>' . ((! isset($dbname) || ! strlen($dbname)) ? $GLOBALS['strTblPrivileges'] : $GLOBALS['strColumnPrivileges']) . '</th>' . "\n"
1647 . ' <th colspan="2">' . $GLOBALS['strAction'] . '</th>' . "\n"
1648 . '</tr>' . "\n"
1649 . '</thead>' . "\n"
1650 . '<tbody>' . "\n";
1652 $user_host_condition =
1653 ' WHERE ' . PMA_convert_using('`User`')
1654 . ' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted')
1655 . ' AND ' . PMA_convert_using('`Host`')
1656 . ' = ' . PMA_convert_using($hostname, 'quoted');
1658 // table body
1659 // get data
1661 // we also want privielgs for this user not in table `db` but in other table
1662 $tables = PMA_DBI_fetch_result('SHOW TABLES FROM `mysql`;');
1663 if ((! isset($dbname) || ! strlen($dbname))) {
1665 // no db name given, so we want all privs for the given user
1667 $tables_to_search_for_users = array(
1668 'tables_priv', 'columns_priv',
1671 $db_rights_sqls = array();
1672 foreach ($tables_to_search_for_users as $table_search_in) {
1673 if (in_array($table_search_in, $tables)) {
1674 $db_rights_sqls[] = '
1675 SELECT DISTINCT `Db`
1676 FROM `mysql`.`' . $table_search_in . '`
1677 ' . $user_host_condition;
1681 $user_defaults = array(
1682 'Db' => '',
1683 'Grant_priv' => 'N',
1684 'privs' => array('USAGE'),
1685 'Table_privs' => true,
1688 // for the rights
1689 $db_rights = array();
1691 if (PMA_MYSQL_INT_VERSION >= 40000) {
1692 $db_rights_sql = '(' . implode(') UNION (', $db_rights_sqls) . ')'
1693 .' ORDER BY `Db` ASC';
1695 $db_rights_result = PMA_DBI_query($db_rights_sql);
1697 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1698 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1699 // only Db names in the table `mysql`.`db` uses wildcards
1700 // as we are in the db specific rights display we want
1701 // all db names escaped, also from other sources
1702 $db_rights_row['Db'] = PMA_escape_mysql_wildcards(
1703 $db_rights_row['Db']);
1704 $db_rights[$db_rights_row['Db']] = $db_rights_row;
1706 } else {
1707 foreach ($db_rights_sqls as $db_rights_sql) {
1708 $db_rights_result = PMA_DBI_query($db_rights_sql);
1710 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1711 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1712 $db_rights[$db_rights_row['Db']] = $db_rights_row;
1716 PMA_DBI_free_result($db_rights_result);
1717 unset($db_rights_sql, $db_rights_sqls, $db_rights_result, $db_rights_row);
1719 $sql_query = 'SELECT * FROM `mysql`.`db`' . $user_host_condition . ' ORDER BY `Db` ASC';
1720 $res = PMA_DBI_query($sql_query);
1721 $sql_query = '';
1723 while ($row = PMA_DBI_fetch_assoc($res)) {
1724 if (isset($db_rights[$row['Db']])) {
1725 $db_rights[$row['Db']] = array_merge($db_rights[$row['Db']], $row);
1726 } else {
1727 $db_rights[$row['Db']] = $row;
1729 // there are db specific rights for this user
1730 // so we can drop this db rights
1731 $db_rights[$row['Db']]['can_delete'] = true;
1733 PMA_DBI_free_result($res);
1734 unset($row, $res);
1736 } else {
1738 // db name was given,
1739 // so we want all user specific rights for this db
1741 $user_host_condition .=
1742 ' AND ' . PMA_convert_using('`Db`')
1743 .' LIKE ' . PMA_convert_using($dbname, 'quoted');
1745 $tables_to_search_for_users = array(
1746 'columns_priv',
1749 $db_rights_sqls = array();
1750 foreach ($tables_to_search_for_users as $table_search_in) {
1751 if (in_array($table_search_in, $tables)) {
1752 $db_rights_sqls[] = '
1753 SELECT DISTINCT `Table_name`
1754 FROM `mysql`.`' . $table_search_in . '`
1755 ' . $user_host_condition;
1759 $user_defaults = array(
1760 'Table_name' => '',
1761 'Grant_priv' => 'N',
1762 'privs' => array('USAGE'),
1763 'Column_priv' => true,
1766 // for the rights
1767 $db_rights = array();
1769 if (PMA_MYSQL_INT_VERSION >= 40000) {
1770 $db_rights_sql = '(' . implode(') UNION (', $db_rights_sqls) . ')'
1771 .' ORDER BY `Table_name` ASC';
1773 $db_rights_result = PMA_DBI_query($db_rights_sql);
1775 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1776 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1777 $db_rights[$db_rights_row['Table_name']] = $db_rights_row;
1779 } else {
1780 foreach ($db_rights_sqls as $db_rights_sql) {
1781 $db_rights_result = PMA_DBI_query($db_rights_sql);
1783 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1784 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1785 $db_rights[$db_rights_row['Table_name']] = $db_rights_row;
1789 PMA_DBI_free_result($db_rights_result);
1790 unset($db_rights_sql, $db_rights_sqls, $db_rights_result, $db_rights_row);
1792 $sql_query =
1793 'SELECT `Table_name`,'
1794 .' `Table_priv`,'
1795 .' IF(`Column_priv` = ' . (PMA_MYSQL_INT_VERSION >= 40100 ? '_latin1 ' : '') . ' \'\', 0, 1)'
1796 .' AS \'Column_priv\''
1797 .' FROM `mysql`.`tables_priv`'
1798 . $user_host_condition
1799 .' ORDER BY `Table_name` ASC;';
1800 $res = PMA_DBI_query($sql_query);
1801 $sql_query = '';
1803 while ($row = PMA_DBI_fetch_assoc($res)) {
1804 if (isset($db_rights[$row['Table_name']])) {
1805 $db_rights[$row['Table_name']] = array_merge($db_rights[$row['Table_name']], $row);
1806 } else {
1807 $db_rights[$row['Table_name']] = $row;
1810 PMA_DBI_free_result($res);
1811 unset($row, $res);
1813 ksort($db_rights);
1815 // display rows
1816 if (count($db_rights) < 1) {
1817 echo '<tr class="odd">' . "\n"
1818 . ' <td colspan="6"><center><i>' . $GLOBALS['strNone'] . '</i></center></td>' . "\n"
1819 . '</tr>' . "\n";
1820 } else {
1821 $odd_row = true;
1822 $found_rows = array();
1823 //while ($row = PMA_DBI_fetch_assoc($res)) {
1824 foreach ($db_rights as $row) {
1825 $found_rows[] = (! isset($dbname) || ! strlen($dbname)) ? $row['Db'] : $row['Table_name'];
1827 echo '<tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n"
1828 . ' <td>' . htmlspecialchars((! isset($dbname) || ! strlen($dbname)) ? $row['Db'] : $row['Table_name']) . '</td>' . "\n"
1829 . ' <td><tt>' . "\n"
1830 . ' ' . join(',' . "\n" . ' ', PMA_extractPrivInfo($row, TRUE)) . "\n"
1831 . ' </tt></td>' . "\n"
1832 . ' <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"
1833 . ' <td>';
1834 if (! empty($row['Table_privs']) || ! empty ($row['Column_priv'])) {
1835 echo $GLOBALS['strYes'];
1836 } else {
1837 echo $GLOBALS['strNo'];
1839 echo '</td>' . "\n"
1840 . ' <td>';
1841 printf($link_edit, urlencode($username),
1842 urlencode($hostname),
1843 urlencode((! isset($dbname) || ! strlen($dbname)) ? $row['Db'] : $dbname),
1844 urlencode((! isset($dbname) || ! strlen($dbname)) ? '' : $row['Table_name']));
1845 echo '</td>' . "\n"
1846 . ' <td>';
1847 if (! empty($row['can_delete']) || isset($row['Table_name']) && strlen($row['Table_name'])) {
1848 printf($link_revoke, urlencode($username),
1849 urlencode($hostname),
1850 urlencode((! isset($dbname) || ! strlen($dbname)) ? $row['Db'] : $dbname),
1851 urlencode((! isset($dbname) || ! strlen($dbname)) ? '' : $row['Table_name']));
1853 echo '</td>' . "\n"
1854 . '</tr>' . "\n";
1855 $odd_row = ! $odd_row;
1856 } // end while
1858 unset($row);
1859 echo '</tbody>' . "\n"
1860 . '</table>' . "\n";
1862 if (! isset($dbname) || ! strlen($dbname)) {
1864 // no database name was give, display select db
1866 if (! empty($found_rows)) {
1867 $pred_db_array = array_diff(
1868 PMA_DBI_fetch_result('SHOW DATABASES;'),
1869 $found_rows);
1870 } else {
1871 $pred_db_array =PMA_DBI_fetch_result('SHOW DATABASES;');
1874 echo ' <label for="text_dbname">' . $GLOBALS['strAddPrivilegesOnDb'] . ':</label>' . "\n";
1875 if (!empty($pred_db_array)) {
1876 echo ' <select name="pred_dbname" onchange="this.form.submit();">' . "\n"
1877 . ' <option value="" selected="selected">' . $GLOBALS['strUseTextField'] . ':</option>' . "\n";
1878 foreach ($pred_db_array as $current_db) {
1879 $current_db = PMA_escape_mysql_wildcards($current_db);
1880 echo ' <option value="' . htmlspecialchars($current_db) . '">'
1881 . htmlspecialchars($current_db) . '</option>' . "\n";
1883 echo ' </select>' . "\n";
1885 echo ' <input type="text" id="text_dbname" name="dbname" />' . "\n"
1886 .PMA_showHint($GLOBALS['strEscapeWildcards']);
1887 } else {
1888 echo ' <input type="hidden" name="dbname" value="' . htmlspecialchars($dbname) . '"/>' . "\n"
1889 . ' <label for="text_tablename">' . $GLOBALS['strAddPrivilegesOnTbl'] . ':</label>' . "\n";
1890 if ($res = @PMA_DBI_try_query('SHOW TABLES FROM ' . PMA_backquote($dbname) . ';', null, PMA_DBI_QUERY_STORE)) {
1891 $pred_tbl_array = array();
1892 while ($row = PMA_DBI_fetch_row($res)) {
1893 if (!isset($found_rows) || !in_array($row[0], $found_rows)) {
1894 $pred_tbl_array[] = $row[0];
1897 PMA_DBI_free_result($res);
1898 unset($res, $row);
1899 if (!empty($pred_tbl_array)) {
1900 echo ' <select name="pred_tablename" onchange="this.form.submit();">' . "\n"
1901 . ' <option value="" selected="selected">' . $GLOBALS['strUseTextField'] . ':</option>' . "\n";
1902 foreach ($pred_tbl_array as $current_table) {
1903 echo ' <option value="' . htmlspecialchars($current_table) . '">' . htmlspecialchars($current_table) . '</option>' . "\n";
1905 echo ' </select>' . "\n";
1907 } else {
1908 unset($res);
1910 echo ' <input type="text" id="text_tablename" name="tablename" />' . "\n";
1912 echo '</fieldset>' . "\n";
1913 echo '<fieldset class="tblFooters">' . "\n"
1914 . ' <input type="submit" value="' . $GLOBALS['strGo'] . '" />'
1915 . '</fieldset>' . "\n"
1916 . '</form>' . "\n";
1919 if ((! isset($dbname) || ! strlen($dbname)) && ! $user_does_not_exists) {
1920 require_once './libraries/display_change_password.lib.php';
1922 echo '<form action="server_privileges.php" method="post" onsubmit="return checkPassword(this);">' . "\n"
1923 . PMA_generate_common_hidden_inputs('', '', 3)
1924 . '<input type="hidden" name="old_username" value="' . htmlspecialchars($username) . '" />' . "\n"
1925 . '<input type="hidden" name="old_hostname" value="' . htmlspecialchars($hostname) . '" />' . "\n"
1926 . '<fieldset id="fieldset_change_copy_user">' . "\n"
1927 . ' <legend>' . $GLOBALS['strChangeCopyUser'] . '</legend>' . "\n";
1928 PMA_displayLoginInformationFields('change', 3);
1929 echo ' <fieldset>' . "\n"
1930 . ' <legend>' . $GLOBALS['strChangeCopyMode'] . '</legend>' . "\n"
1931 . ' <input type="radio" name="mode" value="4" id="radio_mode_4" checked="checked" /><label for="radio_mode_4">' . "\n"
1932 . ' ' . $GLOBALS['strChangeCopyModeCopy'] . "\n"
1933 . ' </label><br />' . "\n"
1934 . ' <input type="radio" name="mode" value="1" id="radio_mode_1" /><label for="radio_mode_1">' . "\n"
1935 . ' ' . $GLOBALS['strChangeCopyModeJustDelete'] . "\n"
1936 . ' </label><br />' . "\n"
1937 . ' <input type="radio" name="mode" value="2" id="radio_mode_2" /><label for="radio_mode_2">' . "\n"
1938 . ' ' . $GLOBALS['strChangeCopyModeRevoke'] . "\n"
1939 . ' </label><br />' . "\n"
1940 . ' <input type="radio" name="mode" value="3" id="radio_mode_3" /><label for="radio_mode_3">' . "\n"
1941 . ' ' . $GLOBALS['strChangeCopyModeDeleteAndReload'] . "\n"
1942 . ' </label>' . "\n"
1943 . ' </fieldset>' . "\n"
1944 . '</fieldset>' . "\n"
1945 . '<fieldset id="fieldset_change_copy_user_footer" class="tblFooters">' . "\n"
1946 . ' <input type="submit" name="change_copy" value="' . $GLOBALS['strGo'] . '" />' . "\n"
1947 . '</fieldset>' . "\n"
1948 . '</form>' . "\n";
1951 } elseif (!empty($adduser)) {
1952 // Add a new user
1953 $GLOBALS['url_query'] .= '&amp;adduser=1';
1954 echo '<h2>' . "\n"
1955 . ($GLOBALS['cfg']['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_usradd.png" width="16" height="16" alt="" />' : '')
1956 . ' ' . $GLOBALS['strAddUser'] . "\n"
1957 . '</h2>' . "\n"
1958 . '<form name="usersForm" id="usersForm" action="server_privileges.php" method="post" onsubmit="return checkAddUser(this);">' . "\n"
1959 . PMA_generate_common_hidden_inputs('', '', 1);
1960 PMA_displayLoginInformationFields('new', 2);
1961 echo '<fieldset id="fieldset_add_user_database">' . "\n"
1962 . '<legend>' . $GLOBALS['strCreateUserDatabase'] . '</legend>' . "\n"
1963 . ' <div class="item">' . "\n"
1964 . ' <input type="radio" name="createdb" value="0" id="radio_createdb_0" checked="checked" />' . "\n"
1965 . ' <label for="radio_createdb_0">' . $GLOBALS['strCreateUserDatabaseNone'] . '</label>' . "\n"
1966 . ' </div>' . "\n"
1967 . ' <div class="item">' . "\n"
1968 . ' <input type="radio" name="createdb" value="1" id="radio_createdb_1" />' . "\n"
1969 . ' <label for="radio_createdb_1">' . $GLOBALS['strCreateUserDatabaseName'] . '</label>' . "\n"
1970 . ' </div>' . "\n"
1971 . ' <div class="item">' . "\n"
1972 . ' <input type="radio" name="createdb" value="2" id="radio_createdb_2" />' . "\n"
1973 . ' <label for="radio_createdb_2">' . $GLOBALS['strCreateUserDatabaseWildcard'] . '</label>' . "\n"
1974 . ' </div>' . "\n"
1975 . '</fieldset>' . "\n";
1976 PMA_displayPrivTable('*', '*', FALSE, 1);
1977 echo ' <fieldset id="fieldset_add_user_footer" class="tblFooters">' . "\n"
1978 . ' <input type="submit" name="adduser_submit" value="' . $GLOBALS['strGo'] . '" />' . "\n"
1979 . ' </fieldset>' . "\n"
1980 . '</form>' . "\n";
1981 } else {
1982 // check the privileges for a particular database.
1983 echo '<table id="tablespecificuserrights" class="data">' . "\n"
1984 . '<caption class="tblHeaders">' . "\n"
1985 . ($GLOBALS['cfg']['PropertiesIconic'] ? ' <img class="icon" src="' . $pmaThemeImage . 'b_usrcheck.png" width="16" height="16" alt="" />' . "\n" : '')
1986 . ' ' . sprintf($GLOBALS['strUsersHavingAccessToDb'], '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase'] . '?' . PMA_generate_common_url($checkprivs) . '">' . htmlspecialchars($checkprivs) . '</a>') . "\n"
1987 . '</caption>' . "\n"
1988 . '<thead>' . "\n"
1989 . ' <tr><th>' . $GLOBALS['strUser'] . '</th>' . "\n"
1990 . ' <th>' . $GLOBALS['strHost'] . '</th>' . "\n"
1991 . ' <th>' . $GLOBALS['strType'] . '</th>' . "\n"
1992 . ' <th>' . $GLOBALS['strPrivileges'] . '</th>' . "\n"
1993 . ' <th>' . $GLOBALS['strGrantOption'] . '</th>' . "\n"
1994 . ' <th>' . $GLOBALS['strAction'] . '</th>' . "\n"
1995 . ' </tr>' . "\n"
1996 . '</thead>' . "\n"
1997 . '<tbody>' . "\n";
1998 $odd_row = TRUE;
1999 unset($row);
2000 unset($row1);
2001 unset($row2);
2002 // now, we build the table...
2003 if (PMA_MYSQL_INT_VERSION >= 40000) {
2004 // Starting with MySQL 4.0.0, we may use UNION SELECTs and this makes
2005 // the job much easier here!
2007 $no = PMA_convert_using('N', 'quoted');
2009 $list_of_privileges =
2010 PMA_convert_using('Select_priv') . ' AS Select_priv, '
2011 . PMA_convert_using('Insert_priv') . ' AS Insert_priv, '
2012 . PMA_convert_using('Update_priv') . ' AS Update_priv, '
2013 . PMA_convert_using('Delete_priv') . ' AS Delete_priv, '
2014 . PMA_convert_using('Create_priv') . ' AS Create_priv, '
2015 . PMA_convert_using('Drop_priv') . ' AS Drop_priv, '
2016 . PMA_convert_using('Grant_priv') . ' AS Grant_priv, '
2017 . PMA_convert_using('References_priv') . ' AS References_priv';
2019 $list_of_compared_privileges =
2020 PMA_convert_using('Select_priv') . ' = ' . $no
2021 . ' AND ' . PMA_convert_using('Insert_priv') . ' = ' . $no
2022 . ' AND ' . PMA_convert_using('Update_priv') . ' = ' . $no
2023 . ' AND ' . PMA_convert_using('Delete_priv') . ' = ' . $no
2024 . ' AND ' . PMA_convert_using('Create_priv') . ' = ' . $no
2025 . ' AND ' . PMA_convert_using('Drop_priv') . ' = ' . $no
2026 . ' AND ' . PMA_convert_using('Grant_priv') . ' = ' . $no
2027 . ' AND ' . PMA_convert_using('References_priv') . ' = ' . $no;
2029 $sql_query =
2030 '(SELECT ' . PMA_convert_using('`User`') . ' AS `User`, '
2031 . PMA_convert_using('`Host`') . ' AS `Host`, '
2032 . PMA_convert_using('`Db`') . ' AS `Db`, '
2033 . $list_of_privileges
2034 .' FROM `mysql`.`db`'
2035 .' WHERE ' . PMA_convert_using($checkprivs, 'quoted')
2036 .' LIKE ' . PMA_convert_using('`Db`')
2037 .' AND NOT (' . $list_of_compared_privileges. ')) '
2038 .'UNION '
2039 .'(SELECT ' . PMA_convert_using('`User`') . ' AS `User`, '
2040 . PMA_convert_using('`Host`') . ' AS `Host`, '
2041 . PMA_convert_using('*', 'quoted') .' AS `Db`, '
2042 . $list_of_privileges
2043 .' FROM `mysql`.`user` '
2044 .' WHERE NOT (' . $list_of_compared_privileges . ')) '
2045 .' ORDER BY `User` ASC,'
2046 .' `Host` ASC,'
2047 .' `Db` ASC;';
2048 $res = PMA_DBI_query($sql_query);
2049 $row = PMA_DBI_fetch_assoc($res);
2050 if ($row) {
2051 $found = TRUE;
2053 } else {
2054 // With MySQL 3, we need 2 seperate queries here.
2055 $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;';
2056 $res1 = PMA_DBI_query($sql_query);
2057 $row1 = PMA_DBI_fetch_assoc($res1);
2058 $sql_query =
2059 'SELECT * FROM `mysql`.`db`'
2060 .' WHERE \'' . $checkprivs . '\''
2061 .' LIKE `Db`'
2062 .' AND NOT (`Select_priv` = \'N\''
2063 .' AND `Insert_priv` = \'N\''
2064 .' AND `Update_priv` = \'N\''
2065 .' AND `Delete_priv` = \'N\''
2066 .' AND `Create_priv` = \'N\''
2067 .' AND `Drop_priv` = \'N\''
2068 .' AND `Grant_priv` = \'N\''
2069 .' AND `References_priv` = \'N\')'
2070 .' ORDER BY `User` ASC, `Host` ASC;';
2071 $res2 = PMA_DBI_query($sql_query);
2072 $row2 = PMA_DBI_fetch_assoc($res2);
2073 if ($row1 || $row2) {
2074 $found = TRUE;
2076 } // end if (PMA_MYSQL_INT_VERSION >= 40000) ... else ...
2077 if ($found) {
2078 while (TRUE) {
2079 // prepare the current user
2080 if (PMA_MYSQL_INT_VERSION >= 40000) {
2081 $current_privileges = array();
2082 $current_user = $row['User'];
2083 $current_host = $row['Host'];
2084 while ($row && $current_user == $row['User'] && $current_host == $row['Host']) {
2085 $current_privileges[] = $row;
2086 $row = PMA_DBI_fetch_assoc($res);
2088 } else {
2089 $current_privileges = array();
2090 if ($row1 && (!$row2 || ($row1['User'] < $row2['User'] || ($row1['User'] == $row2['User'] && $row1['Host'] <= $row2['Host'])))) {
2091 $current_user = $row1['User'];
2092 $current_host = $row1['Host'];
2093 $current_privileges = array($row1);
2094 $row1 = PMA_DBI_fetch_assoc($res1);
2095 } else {
2096 $current_user = $row2['User'];
2097 $current_host = $row2['Host'];
2098 $current_privileges = array();
2100 while ($row2 && $current_user == $row2['User'] && $current_host == $row2['Host']) {
2101 $current_privileges[] = $row2;
2102 $row2 = PMA_DBI_fetch_assoc($res2);
2105 echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n"
2106 . ' <td';
2107 if (count($current_privileges) > 1) {
2108 echo ' rowspan="' . count($current_privileges) . '"';
2110 echo '>' . (empty($current_user) ? '<span style="color: #FF0000">' . $GLOBALS['strAny'] . '</span>' : htmlspecialchars($current_user)) . "\n"
2111 . ' </td>' . "\n"
2112 . ' <td';
2113 if (count($current_privileges) > 1) {
2114 echo ' rowspan="' . count($current_privileges) . '"';
2116 echo '>' . htmlspecialchars($current_host) . '</td>' . "\n";
2117 foreach ($current_privileges as $current) {
2118 echo ' <td>' . "\n"
2119 . ' ';
2120 if (!isset($current['Db']) || $current['Db'] == '*') {
2121 echo $GLOBALS['strGlobal'];
2122 } elseif ($current['Db'] == PMA_escape_mysql_wildcards($checkprivs)) {
2123 echo $GLOBALS['strDbSpecific'];
2124 } else {
2125 echo $GLOBALS['strWildcard'], ': <tt>' . htmlspecialchars($current['Db']) . '</tt>';
2127 echo "\n"
2128 . ' </td>' . "\n"
2129 . ' <td>' . "\n"
2130 . ' <tt>' . "\n"
2131 . ' ' . join(',' . "\n" . ' ', PMA_extractPrivInfo($current, TRUE)) . "\n"
2132 . ' </tt>' . "\n"
2133 . ' </td>' . "\n"
2134 . ' <td>' . "\n"
2135 . ' ' . ($current['Grant_priv'] == 'Y' ? $GLOBALS['strYes'] : $GLOBALS['strNo']) . "\n"
2136 . ' </td>' . "\n"
2137 . ' <td>' . "\n";
2138 printf($link_edit, urlencode($current_user),
2139 urlencode($current_host),
2140 urlencode(! isset($current['Db']) || $current['Db'] == '*' ? '' : $current['Db']),
2141 '');
2142 echo '</td>' . "\n"
2143 . ' </tr>' . "\n";
2145 if (empty($row) && empty($row1) && empty($row2)) {
2146 break;
2148 $odd_row = ! $odd_row;
2150 } else {
2151 echo ' <tr class="odd">' . "\n"
2152 . ' <td colspan="6">' . "\n"
2153 . ' ' . $GLOBALS['strNoUsersFound'] . "\n"
2154 . ' </td>' . "\n"
2155 . ' </tr>' . "\n";
2157 echo '</tbody>' . "\n"
2158 . '</table>' . "\n";
2159 } // end if (empty($adduser) && empty($checkprivs)) ... elseif ... else ...
2163 * Displays the footer
2165 echo "\n\n";
2166 require_once './libraries/footer.inc.php';