previous fix was incompatible with mysql extension
[phpmyadmin/crack.git] / server_privileges.php
blob9efd8f1d23c3d5a2f4e8127347f508c747c1618c
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 if ($nopass == 1) {
1096 $sql_query = 'SET PASSWORD FOR \'' . $username . '\'@\'' . $hostname . '\' = \'\';';
1097 PMA_DBI_query($sql_query);
1098 $message = sprintf($GLOBALS['strPasswordChanged'], '\'' . $username . '\'@\'' . $hostname . '\'');
1099 } elseif (empty($pma_pw) || empty($pma_pw2)) {
1100 $message = $GLOBALS['strPasswordEmpty'];
1101 } elseif ($pma_pw != $pma_pw2) {
1102 $message = $GLOBALS['strPasswordNotSame'];
1103 } else {
1104 $hidden_pw = '';
1105 for ($i = 0; $i < strlen($pma_pw); $i++) {
1106 $hidden_pw .= '*';
1108 $local_query = 'SET PASSWORD FOR \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\' = PASSWORD(\'' . PMA_sqlAddslashes($pma_pw) . '\')';
1109 $sql_query = 'SET PASSWORD FOR \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\' = PASSWORD(\'' . $hidden_pw . '\')';
1110 PMA_DBI_try_query($local_query) or PMA_mysqlDie(PMA_DBI_getError(), $sql_query);
1111 $message = sprintf($GLOBALS['strPasswordChanged'], '\'' . $username . '\'@\'' . $hostname . '\'');
1117 * Deletes users
1118 * (Changes / copies a user, part IV)
1120 $user_host_separator = chr(27);
1122 if (!empty($delete) || (!empty($change_copy) && $mode < 4)) {
1123 if (!empty($change_copy)) {
1124 $selected_usr = array($old_username . $user_host_separator . $old_hostname);
1125 } else {
1126 $queries = array();
1128 for ($i = 0; isset($selected_usr[$i]); $i++) {
1129 list($this_user, $this_host) = explode($user_host_separator, $selected_usr[$i]);
1130 $queries[] = '# ' . sprintf($GLOBALS['strDeleting'], '\'' . $this_user . '\'@\'' . $this_host . '\'') . ' ...';
1131 if (PMA_MYSQL_INT_VERSION >= 50002) {
1132 $queries[] = 'DROP USER \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1133 } else {
1134 if ($mode == 2) {
1135 // The SHOW GRANTS query may fail if the user has not been loaded
1136 // into memory
1137 $res = PMA_DBI_try_query('SHOW GRANTS FOR \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';');
1138 if ($res) {
1139 $queries[] = 'REVOKE ALL PRIVILEGES ON *.* FROM \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1140 while ($row = PMA_DBI_fetch_row($res)) {
1141 $this_table = substr($row[0], (strpos($row[0], 'ON') + 3), (strpos($row[0], ' TO ') - strpos($row[0], 'ON') - 3));
1142 if ($this_table != '*.*') {
1143 $queries[] = 'REVOKE ALL PRIVILEGES ON ' . $this_table . ' FROM \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1145 if (strpos($row[0], 'WITH GRANT OPTION')) {
1146 $queries[] = 'REVOKE GRANT OPTION ON ' . $this_table . ' FROM \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1149 unset($this_table);
1151 PMA_DBI_free_result($res);
1153 unset($res);
1155 if (PMA_MYSQL_INT_VERSION >= 40101) {
1156 if (PMA_MYSQL_INT_VERSION < 50002) {
1157 $queries[] = 'REVOKE GRANT OPTION ON *.* FROM \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1159 $queries[] = 'DROP USER \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
1160 } else {
1161 $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') . ';';
1163 if ($mode != 2) {
1164 // If we REVOKE the table grants, we should not need to modify the
1165 // `mysql`.`db`, `mysql`.`tables_priv` and `mysql`.`columns_priv` tables manually...
1166 $user_host_condition =
1167 ' WHERE ' . PMA_convert_using('User')
1168 . ' = ' . PMA_convert_using(PMA_sqlAddslashes($this_user), 'quoted')
1169 . ' AND ' . PMA_convert_using('Host')
1170 . ' = ' . PMA_convert_using($this_host, 'quoted') . ';';
1171 $queries[] = 'DELETE FROM `mysql`.`db`' . $user_host_condition;
1172 $queries[] = 'DELETE FROM `mysql`.`tables_priv`' . $user_host_condition;
1173 $queries[] = 'DELETE FROM `mysql`.`columns_priv`' . $user_host_condition;
1176 if (!empty($drop_users_db)) {
1177 $queries[] = 'DROP DATABASE IF EXISTS ' . PMA_backquote($this_user) . ';';
1178 $GLOBALS['reload'] = TRUE;
1179 PMA_reloadNavigation();
1182 if (empty($change_copy)) {
1183 if (empty($queries)) {
1184 $show_error_header = TRUE;
1185 $message = $GLOBALS['strDeleteNoUsersSelected'];
1186 } else {
1187 if ($mode == 3) {
1188 $queries[] = '# ' . $GLOBALS['strReloadingThePrivileges'] . ' ...';
1189 $queries[] = 'FLUSH PRIVILEGES;';
1191 foreach ($queries as $sql_query) {
1192 if ($sql_query{0} != '#') {
1193 PMA_DBI_query($sql_query, $GLOBALS['userlink']);
1196 $sql_query = join("\n", $queries);
1197 $message = $GLOBALS['strUsersDeleted'];
1199 unset($queries);
1205 * Changes / copies a user, part V
1207 if (!empty($change_copy)) {
1208 $tmp_count = 0;
1209 foreach ($queries as $sql_query) {
1210 if ($sql_query{0} != '#') {
1211 PMA_DBI_query($sql_query);
1213 // when there is a query containing a hidden password, take it
1214 // instead of the real query sent
1215 if (isset($queries_for_display[$tmp_count])) {
1216 $queries[$tmp_count] = $queries_for_display[$tmp_count];
1218 $tmp_count++;
1220 $message = $GLOBALS['strSuccess'];
1221 $sql_query = join("\n", $queries);
1226 * Reloads the privilege tables into memory
1228 if (!empty($flush_privileges)) {
1229 $sql_query = 'FLUSH PRIVILEGES;';
1230 PMA_DBI_query($sql_query);
1231 $message = $GLOBALS['strPrivilegesReloaded'];
1236 * Displays the links
1238 if (isset($viewing_mode) && $viewing_mode == 'db') {
1239 $db = $checkprivs;
1240 $url_query .= '&amp;goto=db_operations.php';
1242 // Gets the database structure
1243 $sub_part = '_structure';
1244 require './libraries/db_info.inc.php';
1245 echo "\n";
1246 } else {
1247 require './libraries/server_links.inc.php';
1252 * defines some standard links
1254 $link_edit = '<a href="server_privileges.php?' . $GLOBALS['url_query']
1255 .'&amp;username=%s'
1256 .'&amp;hostname=%s'
1257 .'&amp;dbname=%s'
1258 .'&amp;tablename=%s">';
1259 if ($GLOBALS['cfg']['PropertiesIconic']) {
1260 $link_edit .= '<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_usredit.png" width="16" height="16" alt="' . $GLOBALS['strEditPrivileges'] . '" title="' . $GLOBALS['strEditPrivileges'] . '" />';
1261 } else {
1262 $link_edit .= $GLOBALS['strEditPrivileges'];
1264 $link_edit .= '</a>';
1266 $link_revoke = '<a href="server_privileges.php?' . $GLOBALS['url_query']
1267 .'&amp;username=%s'
1268 .'&amp;hostname=%s'
1269 .'&amp;dbname=%s'
1270 .'&amp;tablename=%s'
1271 .'&amp;revokeall=1">';
1272 if ($GLOBALS['cfg']['PropertiesIconic']) {
1273 $link_revoke .= '<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_usrdrop.png" width="16" height="16" alt="' . $GLOBALS['strRevoke'] . '" title="' . $GLOBALS['strRevoke'] . '" />';
1274 } else {
1275 $link_revoke .= $GLOBALS['strRevoke'];
1277 $link_revoke .= '</a>';
1280 * Displays the page
1282 if (empty($adduser) && (! isset($checkprivs) || ! strlen($checkprivs))) {
1283 if (! isset($username)) {
1284 // No username is given --> display the overview
1285 echo '<h2>' . "\n"
1286 . ($GLOBALS['cfg']['MainPageIconic'] ? '<img class="icon" src="'. $GLOBALS['pmaThemeImage'] . 'b_usrlist.png" alt="" />' : '')
1287 . $GLOBALS['strUserOverview'] . "\n"
1288 . '</h2>' . "\n";
1290 $sql_query =
1291 'SELECT `User`,' .
1292 ' `Host`,' .
1293 ' IF(`Password` = ' . (PMA_MYSQL_INT_VERSION >= 40100 ? '_latin1 ' : '') . '\'\', \'N\', \'Y\') AS \'Password\',' .
1294 ' `Select_priv`,' .
1295 ' `Insert_priv`,' .
1296 ' `Update_priv`,' .
1297 ' `Delete_priv`,' .
1298 ' `Index_priv`,' .
1299 ' `Alter_priv`,' .
1300 ' `Create_priv`,' .
1301 ' `Drop_priv`,' .
1302 ' `Grant_priv`,' .
1303 ' `References_priv`,' .
1304 ' `Reload_priv`,' .
1305 ' `Shutdown_priv`,' .
1306 ' `Process_priv`,' .
1307 ' `File_priv`';
1309 if (PMA_MYSQL_INT_VERSION >= 40002) {
1310 $sql_query .= ', `Show_db_priv`, `Super_priv`, `Create_tmp_table_priv`, `Lock_tables_priv`, `Execute_priv`, `Repl_slave_priv`, `Repl_client_priv`';
1313 if (PMA_MYSQL_INT_VERSION >= 50001) {
1314 $sql_query .= ', `Create_view_priv`, `Show_view_priv`';
1317 if (PMA_MYSQL_INT_VERSION >= 50003) {
1318 $sql_query .= ', `Create_user_priv`, `Create_routine_priv`, `Alter_routine_priv`';
1321 $sql_query .= ' FROM `mysql`.`user`';
1323 $sql_query .= (isset($initial) ? PMA_RangeOfUsers($initial) : '');
1325 $sql_query .= ' ORDER BY `User` ASC, `Host` ASC;';
1326 $res = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_STORE);
1328 if (! $res) {
1329 // the query failed! This may have two reasons:
1330 // - the user does not have enough privileges
1331 // - the privilege tables use a structure of an earlier version.
1332 // so let's try a more simple query
1334 $sql_query = 'SELECT * FROM `mysql`.`user`';
1335 $res = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_STORE);
1337 if (!$res) {
1338 echo '<i>' . $GLOBALS['strNoPrivileges'] . '</i>' . "\n";
1339 PMA_DBI_free_result($res);
1340 unset($res);
1341 } else {
1342 // rabus: This message is hardcoded because I will replace it by
1343 // a automatic repair feature soon.
1344 echo '<div class="warning">' . "\n"
1345 . ' Warning: Your privilege table structure seems to be older than this MySQL version!<br />' . "\n"
1346 . ' Please run the script <tt>mysql_fix_privilege_tables</tt> that should be included in your MySQL server distribution to solve this problem!' . "\n"
1347 . '</div><br />' . "\n";
1349 } else {
1351 // we also want users not in table `user` but in other table
1352 $tables = PMA_DBI_fetch_result('SHOW TABLES FROM `mysql`;');
1354 $tables_to_search_for_users = array(
1355 'user', 'db', 'tables_priv', 'columns_priv', 'procs_priv',
1358 $db_rights_sqls = array();
1359 foreach ($tables_to_search_for_users as $table_search_in) {
1360 if (in_array($table_search_in, $tables)) {
1361 $db_rights_sqls[] = 'SELECT DISTINCT `User`, `Host` FROM `mysql`.`' . $table_search_in . '` ' . (isset($initial) ? PMA_RangeOfUsers($initial) : '');
1365 $user_defaults = array(
1366 'User' => '',
1367 'Host' => '%',
1368 'Password' => '?',
1369 'Grant_priv' => 'N',
1370 'privs' => array('USAGE'),
1373 // for all initials, even non A-Z
1374 $array_initials = array();
1375 // for the rights
1376 $db_rights = array();
1378 // do not use UNION DISTINCT, as it's not allowed before
1379 // MySQL 4.0.17, and because "it does nothing" (cf manual)
1380 if (PMA_MYSQL_INT_VERSION >= 40000) {
1381 $db_rights_sql = '(' . implode(') UNION (', $db_rights_sqls) . ')'
1382 .' ORDER BY `User` ASC, `Host` ASC';
1384 $db_rights_result = PMA_DBI_query($db_rights_sql);
1386 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1387 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1388 $db_rights[$db_rights_row['User']][$db_rights_row['Host']] =
1389 $db_rights_row;
1391 } else {
1392 foreach ($db_rights_sqls as $db_rights_sql) {
1393 $db_rights_result = PMA_DBI_query($db_rights_sql);
1395 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1396 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1397 $db_rights[$db_rights_row['User']][$db_rights_row['Host']] =
1398 $db_rights_row;
1402 PMA_DBI_free_result($db_rights_result);
1403 unset($db_rights_sql, $db_rights_sqls, $db_rights_result, $db_rights_row);
1404 ksort($db_rights);
1407 * Displays the initials
1410 // initialize to FALSE the letters A-Z
1411 for ($letter_counter = 1; $letter_counter < 27; $letter_counter++) {
1412 if (! isset($array_initials[chr($letter_counter + 64)])) {
1413 $array_initials[chr($letter_counter + 64)] = FALSE;
1417 $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);
1418 while (list($tmp_initial) = PMA_DBI_fetch_row($initials)) {
1419 $array_initials[$tmp_initial] = TRUE;
1422 // Display the initials, which can be any characters, not
1423 // just letters. For letters A-Z, we add the non-used letters
1424 // as greyed out.
1426 uksort($array_initials, "strnatcasecmp");
1428 echo '<table cellspacing="5"><tr>';
1429 foreach ($array_initials as $tmp_initial => $initial_was_found) {
1430 if ($initial_was_found) {
1431 echo '<td><a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;initial=' . urlencode($tmp_initial) . '">' . $tmp_initial . '</a></td>' . "\n";
1432 } else {
1433 echo '<td>' . $tmp_initial . '</td>';
1436 echo '<td><a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;showall=1">[' . $GLOBALS['strShowAll'] . ']</a></td>' . "\n";
1437 echo '</tr></table>';
1440 * Display the user overview
1441 * (if less than 50 users, display them immediately)
1444 if (isset($initial) || isset($showall) || PMA_DBI_num_rows($res) < 50) {
1446 while ($row = PMA_DBI_fetch_assoc($res)) {
1447 $row['privs'] = PMA_extractPrivInfo($row, true);
1448 $db_rights[$row['User']][$row['Host']] = $row;
1450 @PMA_DBI_free_result($res);
1451 unset($res);
1453 echo '<form name="usersForm" id="usersForm" action="server_privileges.php" method="post">' . "\n"
1454 . PMA_generate_common_hidden_inputs('', '', 1)
1455 . ' <table id="tableuserrights" class="data">' . "\n"
1456 . ' <thead>' . "\n"
1457 . ' <tr><td></td>' . "\n"
1458 . ' <th>' . $GLOBALS['strUser'] . '</th>' . "\n"
1459 . ' <th>' . $GLOBALS['strHost'] . '</th>' . "\n"
1460 . ' <th>' . $GLOBALS['strPassword'] . '</th>' . "\n"
1461 . ' <th>' . $GLOBALS['strGlobalPrivileges'] . ' '
1462 . PMA_showHint($GLOBALS['strEnglishPrivileges']) . '</th>' . "\n"
1463 . ' <th>' . $GLOBALS['strGrantOption'] . '</th>' . "\n"
1464 . ' ' . ($GLOBALS['cfg']['PropertiesIconic'] ? '<td></td>' : '<th>' . $GLOBALS['strAction'] . '</th>') . "\n";
1465 echo ' </tr>' . "\n";
1466 echo ' </thead>' . "\n";
1467 echo ' <tbody>' . "\n";
1468 $odd_row = true;
1469 $index_checkbox = -1;
1470 foreach ($db_rights as $user) {
1471 $index_checkbox++;
1472 ksort($user);
1473 foreach ($user as $host) {
1474 $index_checkbox++;
1475 echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n"
1476 . ' <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"
1477 . ' <td><label for="checkbox_sel_users_' . $index_checkbox . '">' . (empty($host['User']) ? '<span style="color: #FF0000">' . $GLOBALS['strAny'] . '</span>' : htmlspecialchars($host['User'])) . '</label></td>' . "\n"
1478 . ' <td>' . htmlspecialchars($host['Host']) . '</td>' . "\n";
1479 echo ' <td>';
1480 switch ($host['Password']) {
1481 case 'Y':
1482 echo $GLOBALS['strYes'];
1483 break;
1484 case 'N':
1485 echo '<span style="color: #FF0000">' . $GLOBALS['strNo'] . '</span>';
1486 break;
1487 // this happens if this is a definition not coming from mysql.user
1488 default:
1489 echo '--'; // in future version, replace by "not present"
1490 break;
1491 } // end switch
1492 echo '</td>' . "\n"
1493 . ' <td><tt>' . "\n"
1494 . ' ' . implode(',' . "\n" . ' ', $host['privs']) . "\n"
1495 . ' </tt></td>' . "\n"
1496 . ' <td>' . ($host['Grant_priv'] == 'Y' ? $GLOBALS['strYes'] : $GLOBALS['strNo']) . '</td>' . "\n"
1497 . ' <td align="center">';
1498 printf($link_edit, urlencode($host['User']),
1499 urlencode($host['Host']), '', '');
1500 echo '</td>' . "\n"
1501 . ' </tr>' . "\n";
1502 $odd_row = ! $odd_row;
1506 unset($user, $host, $odd_row);
1507 echo ' </tbody></table>' . "\n"
1508 .'<img class="selectallarrow"'
1509 .' src="' . $pmaThemeImage . 'arrow_' . $text_dir . '.png"'
1510 .' width="38" height="22"'
1511 .' alt="' . $GLOBALS['strWithChecked'] . '" />' . "\n"
1512 .'<a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;checkall=1"'
1513 .' onclick="if (markAllRows(\'usersForm\')) return false;">'
1514 . $GLOBALS['strCheckAll'] . '</a>' . "\n"
1515 .'/' . "\n"
1516 .'<a href="server_privileges.php?' . $GLOBALS['url_query'] . '"'
1517 .' onclick="if (unMarkAllRows(\'usersForm\')) return false;">'
1518 . $GLOBALS['strUncheckAll'] . '</a>' . "\n";
1520 // add/delete user fieldset
1521 echo ' <fieldset id="fieldset_add_user">' . "\n"
1522 . ' <a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;adduser=1">' . "\n"
1523 . ($GLOBALS['cfg']['PropertiesIconic'] ? ' <img class="icon" src="' . $pmaThemeImage . 'b_usradd.png" width="16" height="16" alt="" />' . "\n" : '')
1524 . ' ' . $GLOBALS['strAddUser'] . '</a>' . "\n"
1525 . ' </fieldset>' . "\n"
1526 . ' <fieldset id="fieldset_delete_user">'
1527 . ' <legend>' . "\n"
1528 . ($GLOBALS['cfg']['PropertiesIconic'] ? ' <img class="icon" src="' . $pmaThemeImage . 'b_usrdrop.png" width="16" height="16" alt="" />' . "\n" : '')
1529 . ' ' . $GLOBALS['strRemoveSelectedUsers'] . '' . "\n"
1530 . ' </legend>' . "\n";
1532 // before MySQL 4.1.1, we offer some choices for the delete
1533 // mode, but for 4.1.1+, it will be done with REVOKEing the
1534 // privileges then a DROP USER (even no REVOKE at all
1535 // for MySQL 5), so no need to offer so many options
1536 if (PMA_MYSQL_INT_VERSION < 40101) {
1537 echo ' <input type="radio" title="' . $GLOBALS['strJustDelete'] . ' ' . $GLOBALS['strJustDeleteDescr'] . '" name="mode" id="radio_mode_1" value="1" checked="checked" />' . "\n"
1538 . ' <label for="radio_mode_1" title="' . $GLOBALS['strJustDelete'] . ' ' . $GLOBALS['strJustDeleteDescr'] . '">' . "\n"
1539 . ' ' . $GLOBALS['strJustDelete'] . "\n"
1540 . ' </label><br />' . "\n"
1541 . ' <input type="radio" title="' . $GLOBALS['strRevokeAndDelete'] . ' ' . $GLOBALS['strRevokeAndDeleteDescr'] . '" name="mode" id="radio_mode_2" value="2" />' . "\n"
1542 . ' <label for="radio_mode_2" title="' . $GLOBALS['strRevokeAndDelete'] . ' ' . $GLOBALS['strRevokeAndDeleteDescr'] . '">' . "\n"
1543 . ' ' . $GLOBALS['strRevokeAndDelete'] . "\n"
1544 . ' </label><br />' . "\n"
1545 . ' <input type="radio" title="' . $GLOBALS['strDeleteAndFlush'] . ' ' . $GLOBALS['strDeleteAndFlushDescr'] . '" name="mode" id="radio_mode_3" value="3" />' . "\n"
1546 . ' <label for="radio_mode_3" title="' . $GLOBALS['strDeleteAndFlush'] . ' ' . $GLOBALS['strDeleteAndFlushDescr'] . '">' . "\n"
1547 . ' ' . $GLOBALS['strDeleteAndFlush'] . "\n"
1548 . ' </label><br />' . "\n";
1549 } else {
1550 echo ' <input type="hidden" name="mode" value="2" />' . "\n"
1551 . '(' . $GLOBALS['strRevokeAndDelete'] . ')<br />' . "\n";
1554 echo ' <input type="checkbox" title="' . $GLOBALS['strDropUsersDb'] . '" name="drop_users_db" id="checkbox_drop_users_db" />' . "\n"
1555 . ' <label for="checkbox_drop_users_db" title="' . $GLOBALS['strDropUsersDb'] . '">' . "\n"
1556 . ' ' . $GLOBALS['strDropUsersDb'] . "\n"
1557 . ' </label>' . "\n"
1558 . ' </fieldset>' . "\n"
1559 . ' <fieldset id="fieldset_delete_user_footer" class="tblFooters">' . "\n"
1560 . ' <input type="submit" name="delete" value="' . $GLOBALS['strGo'] . '" id="buttonGo" />' . "\n"
1561 . ' </fieldset>' . "\n";
1562 } else {
1564 unset ($row);
1565 echo ' <fieldset id="fieldset_add_user">' . "\n"
1566 . ' <a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;adduser=1">' . "\n"
1567 . ($GLOBALS['cfg']['PropertiesIconic'] ? ' <img class="icon" src="' . $pmaThemeImage . 'b_usradd.png" width="16" height="16" alt="" />' . "\n" : '')
1568 . ' ' . $GLOBALS['strAddUser'] . '</a>' . "\n"
1569 . ' </fieldset>' . "\n";
1570 } // end if (display overview)
1571 echo '</form>' . "\n"
1572 . '<div class="warning">' . "\n"
1573 . ' ' . sprintf($GLOBALS['strFlushPrivilegesNote'], '<a href="server_privileges.php?' . $GLOBALS['url_query'] . '&amp;flush_privileges=1">', '</a>') . "\n"
1574 . '</div>' . "\n";
1578 } else {
1580 // A user was selected -> display the user's properties
1582 echo '<h2>' . "\n"
1583 . ($GLOBALS['cfg']['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_usredit.png" width="16" height="16" alt="" />' : '')
1584 . $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";
1585 if (isset($dbname) && strlen($dbname)) {
1586 if ($dbname_is_wildcard) {
1587 echo ' - ' . $GLOBALS['strDatabases'];
1588 } else {
1589 echo ' - ' . $GLOBALS['strDatabase'];
1591 $url_dbname = urlencode(str_replace('\_', '_', $dbname));
1592 echo ' <i><a href="' . $GLOBALS['cfg']['DefaultTabDatabase'] . '?' . $GLOBALS['url_query'] . '&amp;db=' . $url_dbname . '&amp;reload=1">' . htmlspecialchars($dbname) . '</a></i>' . "\n";
1593 if (isset($tablename) && strlen($tablename)) {
1594 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";
1596 unset($url_dbname);
1598 echo ' : ' . $GLOBALS['strEditPrivileges'] . '</h2>' . "\n";
1599 $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);
1600 $user_does_not_exists = (PMA_DBI_num_rows($res) < 1);
1601 PMA_DBI_free_result($res);
1602 unset($res);
1603 if ($user_does_not_exists) {
1604 echo $GLOBALS['strUserNotFound'];
1605 PMA_displayLoginInformationFields();
1606 //require_once './libraries/footer.inc.php';
1608 echo '<form name="usersForm" id="usersForm" action="server_privileges.php" method="post">' . "\n"
1609 . PMA_generate_common_hidden_inputs('', '', 3)
1610 . '<input type="hidden" name="username" value="' . htmlspecialchars($username) . '" />' . "\n"
1611 . '<input type="hidden" name="hostname" value="' . htmlspecialchars($hostname) . '" />' . "\n";
1612 if (isset($dbname) && strlen($dbname)) {
1613 echo '<input type="hidden" name="dbname" value="' . htmlspecialchars($dbname) . '" />' . "\n";
1614 if (isset($tablename) && strlen($tablename)) {
1615 echo ' <input type="hidden" name="tablename" value="' . htmlspecialchars($tablename) . '" />' . "\n";
1618 PMA_displayPrivTable(((! isset($dbname) || ! strlen($dbname)) ? '*' : $dbname),
1619 (((! isset($dbname) || ! strlen($dbname)) || (! isset($tablename) || ! strlen($tablename))) ? '*' : $tablename),
1620 TRUE, 3);
1621 echo '</form>' . "\n";
1623 if ((! isset($tablename) || ! strlen($tablename)) && empty($dbname_is_wildcard)) {
1625 // no table name was given, display all table specific rights
1626 // but only if $dbname contains no wildcards
1628 // table header
1629 echo '<form action="server_privileges.php" method="post">' . "\n"
1630 . PMA_generate_common_hidden_inputs('', '', 6)
1631 . '<input type="hidden" name="username" value="' . htmlspecialchars($username) . '" />' . "\n"
1632 . '<input type="hidden" name="hostname" value="' . htmlspecialchars($hostname) . '" />' . "\n"
1633 . '<fieldset>' . "\n"
1634 . '<legend>' . ((! isset($dbname) || ! strlen($dbname)) ? $GLOBALS['strDbPrivileges'] : $GLOBALS['strTblPrivileges']) . '</legend>' . "\n"
1635 . '<table class="data">' . "\n"
1636 . '<thead>' . "\n"
1637 . '<tr><th>' . ((! isset($dbname) || ! strlen($dbname)) ? $GLOBALS['strDatabase'] : $GLOBALS['strTable']) . '</th>' . "\n"
1638 . ' <th>' . $GLOBALS['strPrivileges'] . '</th>' . "\n"
1639 . ' <th>' . $GLOBALS['strGrantOption'] . '</th>' . "\n"
1640 . ' <th>' . ((! isset($dbname) || ! strlen($dbname)) ? $GLOBALS['strTblPrivileges'] : $GLOBALS['strColumnPrivileges']) . '</th>' . "\n"
1641 . ' <th colspan="2">' . $GLOBALS['strAction'] . '</th>' . "\n"
1642 . '</tr>' . "\n"
1643 . '</thead>' . "\n"
1644 . '<tbody>' . "\n";
1646 $user_host_condition =
1647 ' WHERE ' . PMA_convert_using('`User`')
1648 . ' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted')
1649 . ' AND ' . PMA_convert_using('`Host`')
1650 . ' = ' . PMA_convert_using($hostname, 'quoted');
1652 // table body
1653 // get data
1655 // we also want privielgs for this user not in table `db` but in other table
1656 $tables = PMA_DBI_fetch_result('SHOW TABLES FROM `mysql`;');
1657 if ((! isset($dbname) || ! strlen($dbname))) {
1659 // no db name given, so we want all privs for the given user
1661 $tables_to_search_for_users = array(
1662 'tables_priv', 'columns_priv',
1665 $db_rights_sqls = array();
1666 foreach ($tables_to_search_for_users as $table_search_in) {
1667 if (in_array($table_search_in, $tables)) {
1668 $db_rights_sqls[] = '
1669 SELECT DISTINCT `Db`
1670 FROM `mysql`.`' . $table_search_in . '`
1671 ' . $user_host_condition;
1675 $user_defaults = array(
1676 'Db' => '',
1677 'Grant_priv' => 'N',
1678 'privs' => array('USAGE'),
1679 'Table_privs' => true,
1682 // for the rights
1683 $db_rights = array();
1685 if (PMA_MYSQL_INT_VERSION >= 40000) {
1686 $db_rights_sql = '(' . implode(') UNION (', $db_rights_sqls) . ')'
1687 .' ORDER BY `Db` ASC';
1689 $db_rights_result = PMA_DBI_query($db_rights_sql);
1691 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1692 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1693 // only Db names in the table `mysql`.`db` uses wildcards
1694 // as we are in the db specific rights display we want
1695 // all db names escaped, also from other sources
1696 $db_rights_row['Db'] = PMA_escape_mysql_wildcards(
1697 $db_rights_row['Db']);
1698 $db_rights[$db_rights_row['Db']] = $db_rights_row;
1700 } else {
1701 foreach ($db_rights_sqls as $db_rights_sql) {
1702 $db_rights_result = PMA_DBI_query($db_rights_sql);
1704 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1705 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1706 $db_rights[$db_rights_row['Db']] = $db_rights_row;
1710 PMA_DBI_free_result($db_rights_result);
1711 unset($db_rights_sql, $db_rights_sqls, $db_rights_result, $db_rights_row);
1713 $sql_query = 'SELECT * FROM `mysql`.`db`' . $user_host_condition . ' ORDER BY `Db` ASC';
1714 $res = PMA_DBI_query($sql_query);
1715 $sql_query = '';
1717 while ($row = PMA_DBI_fetch_assoc($res)) {
1718 if (isset($db_rights[$row['Db']])) {
1719 $db_rights[$row['Db']] = array_merge($db_rights[$row['Db']], $row);
1720 } else {
1721 $db_rights[$row['Db']] = $row;
1723 // there are db specific rights for this user
1724 // so we can drop this db rights
1725 $db_rights[$row['Db']]['can_delete'] = true;
1727 PMA_DBI_free_result($res);
1728 unset($row, $res);
1730 } else {
1732 // db name was given,
1733 // so we want all user specific rights for this db
1735 $user_host_condition .=
1736 ' AND ' . PMA_convert_using('`Db`')
1737 .' LIKE ' . PMA_convert_using($dbname, 'quoted');
1739 $tables_to_search_for_users = array(
1740 'columns_priv',
1743 $db_rights_sqls = array();
1744 foreach ($tables_to_search_for_users as $table_search_in) {
1745 if (in_array($table_search_in, $tables)) {
1746 $db_rights_sqls[] = '
1747 SELECT DISTINCT `Table_name`
1748 FROM `mysql`.`' . $table_search_in . '`
1749 ' . $user_host_condition;
1753 $user_defaults = array(
1754 'Table_name' => '',
1755 'Grant_priv' => 'N',
1756 'privs' => array('USAGE'),
1757 'Column_priv' => true,
1760 // for the rights
1761 $db_rights = array();
1763 if (PMA_MYSQL_INT_VERSION >= 40000) {
1764 $db_rights_sql = '(' . implode(') UNION (', $db_rights_sqls) . ')'
1765 .' ORDER BY `Table_name` ASC';
1767 $db_rights_result = PMA_DBI_query($db_rights_sql);
1769 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1770 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1771 $db_rights[$db_rights_row['Table_name']] = $db_rights_row;
1773 } else {
1774 foreach ($db_rights_sqls as $db_rights_sql) {
1775 $db_rights_result = PMA_DBI_query($db_rights_sql);
1777 while ($db_rights_row = PMA_DBI_fetch_assoc($db_rights_result)) {
1778 $db_rights_row = array_merge($user_defaults, $db_rights_row);
1779 $db_rights[$db_rights_row['Table_name']] = $db_rights_row;
1783 PMA_DBI_free_result($db_rights_result);
1784 unset($db_rights_sql, $db_rights_sqls, $db_rights_result, $db_rights_row);
1786 $sql_query =
1787 'SELECT `Table_name`,'
1788 .' `Table_priv`,'
1789 .' IF(`Column_priv` = ' . (PMA_MYSQL_INT_VERSION >= 40100 ? '_latin1 ' : '') . ' \'\', 0, 1)'
1790 .' AS \'Column_priv\''
1791 .' FROM `mysql`.`tables_priv`'
1792 . $user_host_condition
1793 .' ORDER BY `Table_name` ASC;';
1794 $res = PMA_DBI_query($sql_query);
1795 $sql_query = '';
1797 while ($row = PMA_DBI_fetch_assoc($res)) {
1798 if (isset($db_rights[$row['Table_name']])) {
1799 $db_rights[$row['Table_name']] = array_merge($db_rights[$row['Table_name']], $row);
1800 } else {
1801 $db_rights[$row['Table_name']] = $row;
1804 PMA_DBI_free_result($res);
1805 unset($row, $res);
1807 ksort($db_rights);
1809 // display rows
1810 if (count($db_rights) < 1) {
1811 echo '<tr class="odd">' . "\n"
1812 . ' <td colspan="6"><center><i>' . $GLOBALS['strNone'] . '</i></center></td>' . "\n"
1813 . '</tr>' . "\n";
1814 } else {
1815 $odd_row = true;
1816 $found_rows = array();
1817 //while ($row = PMA_DBI_fetch_assoc($res)) {
1818 foreach ($db_rights as $row) {
1819 $found_rows[] = (! isset($dbname) || ! strlen($dbname)) ? $row['Db'] : $row['Table_name'];
1821 echo '<tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n"
1822 . ' <td>' . htmlspecialchars((! isset($dbname) || ! strlen($dbname)) ? $row['Db'] : $row['Table_name']) . '</td>' . "\n"
1823 . ' <td><tt>' . "\n"
1824 . ' ' . join(',' . "\n" . ' ', PMA_extractPrivInfo($row, TRUE)) . "\n"
1825 . ' </tt></td>' . "\n"
1826 . ' <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"
1827 . ' <td>';
1828 if (! empty($row['Table_privs']) || ! empty ($row['Column_priv'])) {
1829 echo $GLOBALS['strYes'];
1830 } else {
1831 echo $GLOBALS['strNo'];
1833 echo '</td>' . "\n"
1834 . ' <td>';
1835 printf($link_edit, urlencode($username),
1836 urlencode($hostname),
1837 urlencode((! isset($dbname) || ! strlen($dbname)) ? $row['Db'] : $dbname),
1838 urlencode((! isset($dbname) || ! strlen($dbname)) ? '' : $row['Table_name']));
1839 echo '</td>' . "\n"
1840 . ' <td>';
1841 if (! empty($row['can_delete']) || isset($row['Table_name']) && strlen($row['Table_name'])) {
1842 printf($link_revoke, urlencode($username),
1843 urlencode($hostname),
1844 urlencode((! isset($dbname) || ! strlen($dbname)) ? $row['Db'] : $dbname),
1845 urlencode((! isset($dbname) || ! strlen($dbname)) ? '' : $row['Table_name']));
1847 echo '</td>' . "\n"
1848 . '</tr>' . "\n";
1849 $odd_row = ! $odd_row;
1850 } // end while
1852 unset($row);
1853 echo '</tbody>' . "\n"
1854 . '</table>' . "\n";
1856 if (! isset($dbname) || ! strlen($dbname)) {
1858 // no database name was give, display select db
1860 if (! empty($found_rows)) {
1861 $pred_db_array = array_diff(
1862 PMA_DBI_fetch_result('SHOW DATABASES;'),
1863 $found_rows);
1864 } else {
1865 $pred_db_array =PMA_DBI_fetch_result('SHOW DATABASES;');
1868 echo ' <label for="text_dbname">' . $GLOBALS['strAddPrivilegesOnDb'] . ':</label>' . "\n";
1869 if (!empty($pred_db_array)) {
1870 echo ' <select name="pred_dbname" onchange="this.form.submit();">' . "\n"
1871 . ' <option value="" selected="selected">' . $GLOBALS['strUseTextField'] . ':</option>' . "\n";
1872 foreach ($pred_db_array as $current_db) {
1873 $current_db = PMA_escape_mysql_wildcards($current_db);
1874 echo ' <option value="' . htmlspecialchars($current_db) . '">'
1875 . htmlspecialchars($current_db) . '</option>' . "\n";
1877 echo ' </select>' . "\n";
1879 echo ' <input type="text" id="text_dbname" name="dbname" />' . "\n"
1880 .PMA_showHint($GLOBALS['strEscapeWildcards']);
1881 } else {
1882 echo ' <input type="hidden" name="dbname" value="' . htmlspecialchars($dbname) . '"/>' . "\n"
1883 . ' <label for="text_tablename">' . $GLOBALS['strAddPrivilegesOnTbl'] . ':</label>' . "\n";
1884 if ($res = @PMA_DBI_try_query('SHOW TABLES FROM ' . PMA_backquote($dbname) . ';', null, PMA_DBI_QUERY_STORE)) {
1885 $pred_tbl_array = array();
1886 while ($row = PMA_DBI_fetch_row($res)) {
1887 if (!isset($found_rows) || !in_array($row[0], $found_rows)) {
1888 $pred_tbl_array[] = $row[0];
1891 PMA_DBI_free_result($res);
1892 unset($res, $row);
1893 if (!empty($pred_tbl_array)) {
1894 echo ' <select name="pred_tablename" onchange="this.form.submit();">' . "\n"
1895 . ' <option value="" selected="selected">' . $GLOBALS['strUseTextField'] . ':</option>' . "\n";
1896 foreach ($pred_tbl_array as $current_table) {
1897 echo ' <option value="' . htmlspecialchars($current_table) . '">' . htmlspecialchars($current_table) . '</option>' . "\n";
1899 echo ' </select>' . "\n";
1901 } else {
1902 unset($res);
1904 echo ' <input type="text" id="text_tablename" name="tablename" />' . "\n";
1906 echo '</fieldset>' . "\n";
1907 echo '<fieldset class="tblFooters">' . "\n"
1908 . ' <input type="submit" value="' . $GLOBALS['strGo'] . '" />'
1909 . '</fieldset>' . "\n"
1910 . '</form>' . "\n";
1913 if ((! isset($dbname) || ! strlen($dbname)) && ! $user_does_not_exists) {
1914 echo '<form action="server_privileges.php" method="post" onsubmit="return checkPassword(this);">' . "\n"
1915 . PMA_generate_common_hidden_inputs('', '', 3)
1916 . '<input type="hidden" name="username" value="' . htmlspecialchars($username) . '" />' . "\n"
1917 . '<input type="hidden" name="hostname" value="' . htmlspecialchars($hostname) . '" />' . "\n"
1918 . '<fieldset id="fieldset_change_password">' . "\n"
1919 . ' <legend>' . $GLOBALS['strChangePassword'] . '</legend>' . "\n"
1920 . ' <table class="data">' . "\n"
1921 . ' <tr class="odd noclick">' . "\n"
1922 . ' <td><input type="radio" name="nopass" value="1" id="radio_nopass_1" onclick="pw_pma_pw.value=\'\'; pw_pma_pw2.value=\'\';" /></td>' . "\n"
1923 . ' <td colspan="2"><label for="radio_nopass_1">' . $GLOBALS['strNoPassword'] . '</label></td>' . "\n"
1924 . ' </tr>' . "\n"
1925 . ' <tr class="even noclick">' . "\n"
1926 . ' <td><input type="radio" name="nopass" value="0" id="radio_nopass_0" onclick="document.getElementById(\'pw_pma_pw\').focus();" /></td>' . "\n"
1927 . ' <td><label for="radio_nopass_0">' . $GLOBALS['strPassword'] . ':</label></td>' . "\n"
1928 . ' <td><input type="password" name="pma_pw" id="pw_pma_pw" onchange="nopass[1].checked = true;" /></td>' . "\n"
1929 . ' </tr>' . "\n"
1930 . ' <tr class="odd noclick">' . "\n"
1931 . ' <td></td>' . "\n"
1932 . ' <td><label for="pw_pma_pw2">' . $GLOBALS['strReType'] . ':</label></td>' . "\n"
1933 . ' <td><input type="password" name="pma_pw2" id="pw_pma_pw2" onchange="nopass[1].checked = true;" /></td>' . "\n"
1934 . ' </tr>' . "\n"
1935 . ' </table>' . "\n"
1936 . '</fieldset>' . "\n"
1937 . '<fieldset id="fieldset_change_password_footer" class="tblFooters">' . "\n"
1938 . ' <input type="submit" name="change_pw" value="' . $GLOBALS['strGo'] . '" />' . "\n"
1939 . '</fieldset>' . "\n"
1940 . '</form>' . "\n"
1941 . '<form action="server_privileges.php" method="post" onsubmit="return checkPassword(this);">' . "\n"
1942 . PMA_generate_common_hidden_inputs('', '', 3)
1943 . '<input type="hidden" name="old_username" value="' . htmlspecialchars($username) . '" />' . "\n"
1944 . '<input type="hidden" name="old_hostname" value="' . htmlspecialchars($hostname) . '" />' . "\n"
1945 . '<fieldset id="fieldset_change_copy_user">' . "\n"
1946 . ' <legend>' . $GLOBALS['strChangeCopyUser'] . '</legend>' . "\n";
1947 PMA_displayLoginInformationFields('change', 3);
1948 echo ' <fieldset>' . "\n"
1949 . ' <legend>' . $GLOBALS['strChangeCopyMode'] . '</legend>' . "\n"
1950 . ' <input type="radio" name="mode" value="4" id="radio_mode_4" checked="checked" /><label for="radio_mode_4">' . "\n"
1951 . ' ' . $GLOBALS['strChangeCopyModeCopy'] . "\n"
1952 . ' </label><br />' . "\n"
1953 . ' <input type="radio" name="mode" value="1" id="radio_mode_1" /><label for="radio_mode_1">' . "\n"
1954 . ' ' . $GLOBALS['strChangeCopyModeJustDelete'] . "\n"
1955 . ' </label><br />' . "\n"
1956 . ' <input type="radio" name="mode" value="2" id="radio_mode_2" /><label for="radio_mode_2">' . "\n"
1957 . ' ' . $GLOBALS['strChangeCopyModeRevoke'] . "\n"
1958 . ' </label><br />' . "\n"
1959 . ' <input type="radio" name="mode" value="3" id="radio_mode_3" /><label for="radio_mode_3">' . "\n"
1960 . ' ' . $GLOBALS['strChangeCopyModeDeleteAndReload'] . "\n"
1961 . ' </label>' . "\n"
1962 . ' </fieldset>' . "\n"
1963 . '</fieldset>' . "\n"
1964 . '<fieldset id="fieldset_change_copy_user_footer" class="tblFooters">' . "\n"
1965 . ' <input type="submit" name="change_copy" value="' . $GLOBALS['strGo'] . '" />' . "\n"
1966 . '</fieldset>' . "\n"
1967 . '</form>' . "\n";
1970 } elseif (!empty($adduser)) {
1971 // Add a new user
1972 $GLOBALS['url_query'] .= '&amp;adduser=1';
1973 echo '<h2>' . "\n"
1974 . ($GLOBALS['cfg']['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_usradd.png" width="16" height="16" alt="" />' : '')
1975 . ' ' . $GLOBALS['strAddUser'] . "\n"
1976 . '</h2>' . "\n"
1977 . '<form name="usersForm" id="usersForm" action="server_privileges.php" method="post" onsubmit="return checkAddUser(this);">' . "\n"
1978 . PMA_generate_common_hidden_inputs('', '', 1);
1979 PMA_displayLoginInformationFields('new', 2);
1980 echo '<fieldset id="fieldset_add_user_database">' . "\n"
1981 . '<legend>' . $GLOBALS['strCreateUserDatabase'] . '</legend>' . "\n"
1982 . ' <div class="item">' . "\n"
1983 . ' <input type="radio" name="createdb" value="0" id="radio_createdb_0" checked="checked" />' . "\n"
1984 . ' <label for="radio_createdb_0">' . $GLOBALS['strCreateUserDatabaseNone'] . '</label>' . "\n"
1985 . ' </div>' . "\n"
1986 . ' <div class="item">' . "\n"
1987 . ' <input type="radio" name="createdb" value="1" id="radio_createdb_1" />' . "\n"
1988 . ' <label for="radio_createdb_1">' . $GLOBALS['strCreateUserDatabaseName'] . '</label>' . "\n"
1989 . ' </div>' . "\n"
1990 . ' <div class="item">' . "\n"
1991 . ' <input type="radio" name="createdb" value="2" id="radio_createdb_2" />' . "\n"
1992 . ' <label for="radio_createdb_2">' . $GLOBALS['strCreateUserDatabaseWildcard'] . '</label>' . "\n"
1993 . ' </div>' . "\n"
1994 . '</fieldset>' . "\n";
1995 PMA_displayPrivTable('*', '*', FALSE, 1);
1996 echo ' <fieldset id="fieldset_add_user_footer" class="tblFooters">' . "\n"
1997 . ' <input type="submit" name="adduser_submit" value="' . $GLOBALS['strGo'] . '" />' . "\n"
1998 . ' </fieldset>' . "\n"
1999 . '</form>' . "\n";
2000 } else {
2001 // check the privileges for a particular database.
2002 echo '<table id="tablespecificuserrights" class="data">' . "\n"
2003 . '<caption class="tblHeaders">' . "\n"
2004 . ($GLOBALS['cfg']['PropertiesIconic'] ? ' <img class="icon" src="' . $pmaThemeImage . 'b_usrcheck.png" width="16" height="16" alt="" />' . "\n" : '')
2005 . ' ' . sprintf($GLOBALS['strUsersHavingAccessToDb'], '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase'] . '?' . PMA_generate_common_url($checkprivs) . '">' . htmlspecialchars($checkprivs) . '</a>') . "\n"
2006 . '</caption>' . "\n"
2007 . '<thead>' . "\n"
2008 . ' <tr><th>' . $GLOBALS['strUser'] . '</th>' . "\n"
2009 . ' <th>' . $GLOBALS['strHost'] . '</th>' . "\n"
2010 . ' <th>' . $GLOBALS['strType'] . '</th>' . "\n"
2011 . ' <th>' . $GLOBALS['strPrivileges'] . '</th>' . "\n"
2012 . ' <th>' . $GLOBALS['strGrantOption'] . '</th>' . "\n"
2013 . ' <th>' . $GLOBALS['strAction'] . '</th>' . "\n"
2014 . ' </tr>' . "\n"
2015 . '</thead>' . "\n"
2016 . '<tbody>' . "\n";
2017 $odd_row = TRUE;
2018 unset($row);
2019 unset($row1);
2020 unset($row2);
2021 // now, we build the table...
2022 if (PMA_MYSQL_INT_VERSION >= 40000) {
2023 // Starting with MySQL 4.0.0, we may use UNION SELECTs and this makes
2024 // the job much easier here!
2026 $no = PMA_convert_using('N', 'quoted');
2028 $list_of_privileges =
2029 PMA_convert_using('Select_priv') . ' AS Select_priv, '
2030 . PMA_convert_using('Insert_priv') . ' AS Insert_priv, '
2031 . PMA_convert_using('Update_priv') . ' AS Update_priv, '
2032 . PMA_convert_using('Delete_priv') . ' AS Delete_priv, '
2033 . PMA_convert_using('Create_priv') . ' AS Create_priv, '
2034 . PMA_convert_using('Drop_priv') . ' AS Drop_priv, '
2035 . PMA_convert_using('Grant_priv') . ' AS Grant_priv, '
2036 . PMA_convert_using('References_priv') . ' AS References_priv';
2038 $list_of_compared_privileges =
2039 PMA_convert_using('Select_priv') . ' = ' . $no
2040 . ' AND ' . PMA_convert_using('Insert_priv') . ' = ' . $no
2041 . ' AND ' . PMA_convert_using('Update_priv') . ' = ' . $no
2042 . ' AND ' . PMA_convert_using('Delete_priv') . ' = ' . $no
2043 . ' AND ' . PMA_convert_using('Create_priv') . ' = ' . $no
2044 . ' AND ' . PMA_convert_using('Drop_priv') . ' = ' . $no
2045 . ' AND ' . PMA_convert_using('Grant_priv') . ' = ' . $no
2046 . ' AND ' . PMA_convert_using('References_priv') . ' = ' . $no;
2048 $sql_query =
2049 '(SELECT ' . PMA_convert_using('`User`') . ' AS `User`, '
2050 . PMA_convert_using('`Host`') . ' AS `Host`, '
2051 . PMA_convert_using('`Db`') . ' AS `Db`, '
2052 . $list_of_privileges
2053 .' FROM `mysql`.`db`'
2054 .' WHERE ' . PMA_convert_using($checkprivs, 'quoted')
2055 .' LIKE ' . PMA_convert_using('`Db`')
2056 .' AND NOT (' . $list_of_compared_privileges. ')) '
2057 .'UNION '
2058 .'(SELECT ' . PMA_convert_using('`User`') . ' AS `User`, '
2059 . PMA_convert_using('`Host`') . ' AS `Host`, '
2060 . PMA_convert_using('*', 'quoted') .' AS `Db`, '
2061 . $list_of_privileges
2062 .' FROM `mysql`.`user` '
2063 .' WHERE NOT (' . $list_of_compared_privileges . ')) '
2064 .' ORDER BY `User` ASC,'
2065 .' `Host` ASC,'
2066 .' `Db` ASC;';
2067 $res = PMA_DBI_query($sql_query);
2068 $row = PMA_DBI_fetch_assoc($res);
2069 if ($row) {
2070 $found = TRUE;
2072 } else {
2073 // With MySQL 3, we need 2 seperate queries here.
2074 $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;';
2075 $res1 = PMA_DBI_query($sql_query);
2076 $row1 = PMA_DBI_fetch_assoc($res1);
2077 $sql_query =
2078 'SELECT * FROM `mysql`.`db`'
2079 .' WHERE \'' . $checkprivs . '\''
2080 .' LIKE `Db`'
2081 .' AND NOT (`Select_priv` = \'N\''
2082 .' AND `Insert_priv` = \'N\''
2083 .' AND `Update_priv` = \'N\''
2084 .' AND `Delete_priv` = \'N\''
2085 .' AND `Create_priv` = \'N\''
2086 .' AND `Drop_priv` = \'N\''
2087 .' AND `Grant_priv` = \'N\''
2088 .' AND `References_priv` = \'N\')'
2089 .' ORDER BY `User` ASC, `Host` ASC;';
2090 $res2 = PMA_DBI_query($sql_query);
2091 $row2 = PMA_DBI_fetch_assoc($res2);
2092 if ($row1 || $row2) {
2093 $found = TRUE;
2095 } // end if (PMA_MYSQL_INT_VERSION >= 40000) ... else ...
2096 if ($found) {
2097 while (TRUE) {
2098 // prepare the current user
2099 if (PMA_MYSQL_INT_VERSION >= 40000) {
2100 $current_privileges = array();
2101 $current_user = $row['User'];
2102 $current_host = $row['Host'];
2103 while ($row && $current_user == $row['User'] && $current_host == $row['Host']) {
2104 $current_privileges[] = $row;
2105 $row = PMA_DBI_fetch_assoc($res);
2107 } else {
2108 $current_privileges = array();
2109 if ($row1 && (!$row2 || ($row1['User'] < $row2['User'] || ($row1['User'] == $row2['User'] && $row1['Host'] <= $row2['Host'])))) {
2110 $current_user = $row1['User'];
2111 $current_host = $row1['Host'];
2112 $current_privileges = array($row1);
2113 $row1 = PMA_DBI_fetch_assoc($res1);
2114 } else {
2115 $current_user = $row2['User'];
2116 $current_host = $row2['Host'];
2117 $current_privileges = array();
2119 while ($row2 && $current_user == $row2['User'] && $current_host == $row2['Host']) {
2120 $current_privileges[] = $row2;
2121 $row2 = PMA_DBI_fetch_assoc($res2);
2124 echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n"
2125 . ' <td';
2126 if (count($current_privileges) > 1) {
2127 echo ' rowspan="' . count($current_privileges) . '"';
2129 echo '>' . (empty($current_user) ? '<span style="color: #FF0000">' . $GLOBALS['strAny'] . '</span>' : htmlspecialchars($current_user)) . "\n"
2130 . ' </td>' . "\n"
2131 . ' <td';
2132 if (count($current_privileges) > 1) {
2133 echo ' rowspan="' . count($current_privileges) . '"';
2135 echo '>' . htmlspecialchars($current_host) . '</td>' . "\n";
2136 foreach ($current_privileges as $current) {
2137 echo ' <td>' . "\n"
2138 . ' ';
2139 if (!isset($current['Db']) || $current['Db'] == '*') {
2140 echo $GLOBALS['strGlobal'];
2141 } elseif ($current['Db'] == PMA_escape_mysql_wildcards($checkprivs)) {
2142 echo $GLOBALS['strDbSpecific'];
2143 } else {
2144 echo $GLOBALS['strWildcard'], ': <tt>' . htmlspecialchars($current['Db']) . '</tt>';
2146 echo "\n"
2147 . ' </td>' . "\n"
2148 . ' <td>' . "\n"
2149 . ' <tt>' . "\n"
2150 . ' ' . join(',' . "\n" . ' ', PMA_extractPrivInfo($current, TRUE)) . "\n"
2151 . ' </tt>' . "\n"
2152 . ' </td>' . "\n"
2153 . ' <td>' . "\n"
2154 . ' ' . ($current['Grant_priv'] == 'Y' ? $GLOBALS['strYes'] : $GLOBALS['strNo']) . "\n"
2155 . ' </td>' . "\n"
2156 . ' <td>' . "\n";
2157 printf($link_edit, urlencode($current_user),
2158 urlencode($current_host),
2159 urlencode(! isset($current['Db']) || $current['Db'] == '*' ? '' : $current['Db']),
2160 '');
2161 echo '</td>' . "\n"
2162 . ' </tr>' . "\n";
2164 if (empty($row) && empty($row1) && empty($row2)) {
2165 break;
2167 $odd_row = ! $odd_row;
2169 } else {
2170 echo ' <tr class="odd">' . "\n"
2171 . ' <td colspan="6">' . "\n"
2172 . ' ' . $GLOBALS['strNoUsersFound'] . "\n"
2173 . ' </td>' . "\n"
2174 . ' </tr>' . "\n";
2176 echo '</tbody>' . "\n"
2177 . '</table>' . "\n";
2178 } // end if (empty($adduser) && empty($checkprivs)) ... elseif ... else ...
2182 * Displays the footer
2184 echo "\n\n";
2185 require_once './libraries/footer.inc.php';