Merge branch 'QA_4_4' into QA_4_5
[phpmyadmin.git] / libraries / server_privileges.lib.php
blobbc060c35b9d52a3ddc9e476655194951fd20b4ad
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * set of functions with the Privileges section in pma
6 * @package PhpMyAdmin
7 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * Get Html for User Group Dialog
16 * @param string $username username
17 * @param bool $is_menuswork Is menuswork set in configuration
19 * @return string html
21 function PMA_getHtmlForUserGroupDialog($username, $is_menuswork)
23 $html = '';
24 if (! empty($_REQUEST['edit_user_group_dialog']) && $is_menuswork) {
25 $dialog = PMA_getHtmlToChooseUserGroup($username);
26 $response = PMA_Response::getInstance();
27 if ($GLOBALS['is_ajax_request']) {
28 $response->addJSON('message', $dialog);
29 exit;
30 } else {
31 $html .= $dialog;
35 return $html;
38 /**
39 * Escapes wildcard in a database+table specification
40 * before using it in a GRANT statement.
42 * Escaping a wildcard character in a GRANT is only accepted at the global
43 * or database level, not at table level; this is why I remove
44 * the escaping character. Internally, in mysql.tables_priv.Db there are
45 * no escaping (for example test_db) but in mysql.db you'll see test\_db
46 * for a db-specific privilege.
48 * @param string $dbname Database name
49 * @param string $tablename Table name
51 * @return string the escaped (if necessary) database.table
53 function PMA_wildcardEscapeForGrant($dbname, $tablename)
55 if (!/*overload*/mb_strlen($dbname)) {
56 $db_and_table = '*.*';
57 } else {
58 if (/*overload*/mb_strlen($tablename)) {
59 $db_and_table = PMA_Util::backquote(
60 PMA_Util::unescapeMysqlWildcards($dbname)
62 . '.' . PMA_Util::backquote($tablename);
63 } else {
64 $db_and_table = PMA_Util::backquote($dbname) . '.*';
67 return $db_and_table;
70 /**
71 * Generates a condition on the user name
73 * @param string $initial the user's initial
75 * @return string the generated condition
77 function PMA_rangeOfUsers($initial = '')
79 // strtolower() is used because the User field
80 // might be BINARY, so LIKE would be case sensitive
81 if ($initial === null || $initial === '') {
82 return '';
85 $ret = " WHERE `User` LIKE '"
86 . PMA_Util::sqlAddSlashes($initial, true) . "%'"
87 . " OR `User` LIKE '"
88 . PMA_Util::sqlAddSlashes(/*overload*/mb_strtolower($initial), true)
89 . "%'";
90 return $ret;
91 } // end function
93 /**
94 * Formats privilege name for a display
96 * @param array $privilege Privilege information
97 * @param boolean $html Whether to use HTML
99 * @return string
101 function PMA_formatPrivilege($privilege, $html)
103 if ($html) {
104 return '<dfn title="' . $privilege[2] . '">'
105 . $privilege[1] . '</dfn>';
106 } else {
107 return $privilege[1];
112 * Parses privileges into an array, it modifies the array
114 * @param array &$row Results row from
116 * @return void
118 function PMA_fillInTablePrivileges(&$row)
120 $row1 = $GLOBALS['dbi']->fetchSingleRow(
121 'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';',
122 'ASSOC', $GLOBALS['userlink']
124 // note: in MySQL 5.0.3 we get "Create View', 'Show view';
125 // the View for Create is spelled with uppercase V
126 // the view for Show is spelled with lowercase v
127 // and there is a space between the words
129 $av_grants = explode(
130 '\',\'',
131 /*overload*/mb_substr(
132 $row1['Type'],
133 /*overload*/mb_strpos($row1['Type'], '(') + 2,
134 /*overload*/mb_strpos($row1['Type'], ')')
135 - /*overload*/mb_strpos($row1['Type'], '(') - 3
139 $users_grants = explode(',', $row['Table_priv']);
141 foreach ($av_grants as $current_grant) {
142 $row[$current_grant . '_priv']
143 = in_array($current_grant, $users_grants) ? 'Y' : 'N';
145 unset($row['Table_priv']);
150 * Extracts the privilege information of a priv table row
152 * @param array|null $row the row
153 * @param boolean $enableHTML add <dfn> tag with tooltips
154 * @param boolean $tablePrivs whether row contains table privileges
156 * @global resource $user_link the database connection
158 * @return array
160 function PMA_extractPrivInfo($row = null, $enableHTML = false, $tablePrivs = false)
162 if ($tablePrivs) {
163 $grants = PMA_getTableGrantsArray();
164 } else {
165 $grants = PMA_getGrantsArray();
168 if (! is_null($row) && isset($row['Table_priv'])) {
169 PMA_fillInTablePrivileges($row);
172 $privs = array();
173 $allPrivileges = true;
174 foreach ($grants as $current_grant) {
175 if ((! is_null($row) && isset($row[$current_grant[0]]))
176 || (is_null($row) && isset($GLOBALS[$current_grant[0]]))
178 if ((! is_null($row) && $row[$current_grant[0]] == 'Y')
179 || (is_null($row)
180 && ($GLOBALS[$current_grant[0]] == 'Y'
181 || (is_array($GLOBALS[$current_grant[0]])
182 && count($GLOBALS[$current_grant[0]]) == $_REQUEST['column_count']
183 && empty($GLOBALS[$current_grant[0] . '_none']))))
185 $privs[] = PMA_formatPrivilege($current_grant, $enableHTML);
186 } elseif (! empty($GLOBALS[$current_grant[0]])
187 && is_array($GLOBALS[$current_grant[0]])
188 && empty($GLOBALS[$current_grant[0] . '_none'])
190 $privs[] = PMA_formatPrivilege($current_grant, $enableHTML)
191 . ' (`' . join('`, `', $GLOBALS[$current_grant[0]]) . '`)';
192 } else {
193 $allPrivileges = false;
197 if (empty($privs)) {
198 if ($enableHTML) {
199 $privs[] = '<dfn title="' . __('No privileges.') . '">USAGE</dfn>';
200 } else {
201 $privs[] = 'USAGE';
203 } elseif ($allPrivileges
204 && (! isset($_POST['grant_count']) || count($privs) == $_POST['grant_count'])
206 if ($enableHTML) {
207 $privs = array('<dfn title="'
208 . __('Includes all privileges except GRANT.')
209 . '">ALL PRIVILEGES</dfn>'
211 } else {
212 $privs = array('ALL PRIVILEGES');
215 return $privs;
216 } // end of the 'PMA_extractPrivInfo()' function
219 * Returns an array of table grants and their descriptions
221 * @return array array of table grants
223 function PMA_getTableGrantsArray()
225 return array(
226 array(
227 'Delete',
228 'DELETE',
229 $GLOBALS['strPrivDescDelete']
231 array(
232 'Create',
233 'CREATE',
234 $GLOBALS['strPrivDescCreateTbl']
236 array(
237 'Drop',
238 'DROP',
239 $GLOBALS['strPrivDescDropTbl']
241 array(
242 'Index',
243 'INDEX',
244 $GLOBALS['strPrivDescIndex']
246 array(
247 'Alter',
248 'ALTER',
249 $GLOBALS['strPrivDescAlter']
251 array(
252 'Create View',
253 'CREATE_VIEW',
254 $GLOBALS['strPrivDescCreateView']
256 array(
257 'Show view',
258 'SHOW_VIEW',
259 $GLOBALS['strPrivDescShowView']
261 array(
262 'Trigger',
263 'TRIGGER',
264 $GLOBALS['strPrivDescTrigger']
270 * Get the grants array which contains all the privilege types
271 * and relevant grant messages
273 * @return array
275 function PMA_getGrantsArray()
277 return array(
278 array(
279 'Select_priv',
280 'SELECT',
281 __('Allows reading data.')
283 array(
284 'Insert_priv',
285 'INSERT',
286 __('Allows inserting and replacing data.')
288 array(
289 'Update_priv',
290 'UPDATE',
291 __('Allows changing data.')
293 array(
294 'Delete_priv',
295 'DELETE',
296 __('Allows deleting data.')
298 array(
299 'Create_priv',
300 'CREATE',
301 __('Allows creating new databases and tables.')
303 array(
304 'Drop_priv',
305 'DROP',
306 __('Allows dropping databases and tables.')
308 array(
309 'Reload_priv',
310 'RELOAD',
311 __('Allows reloading server settings and flushing the server\'s caches.')
313 array(
314 'Shutdown_priv',
315 'SHUTDOWN',
316 __('Allows shutting down the server.')
318 array(
319 'Process_priv',
320 'PROCESS',
321 __('Allows viewing processes of all users.')
323 array(
324 'File_priv',
325 'FILE',
326 __('Allows importing data from and exporting data into files.')
328 array(
329 'References_priv',
330 'REFERENCES',
331 __('Has no effect in this MySQL version.')
333 array(
334 'Index_priv',
335 'INDEX',
336 __('Allows creating and dropping indexes.')
338 array(
339 'Alter_priv',
340 'ALTER',
341 __('Allows altering the structure of existing tables.')
343 array(
344 'Show_db_priv',
345 'SHOW DATABASES',
346 __('Gives access to the complete list of databases.')
348 array(
349 'Super_priv',
350 'SUPER',
352 'Allows connecting, even if maximum number of connections '
353 . 'is reached; required for most administrative operations '
354 . 'like setting global variables or killing threads of other users.'
357 array(
358 'Create_tmp_table_priv',
359 'CREATE TEMPORARY TABLES',
360 __('Allows creating temporary tables.')
362 array(
363 'Lock_tables_priv',
364 'LOCK TABLES',
365 __('Allows locking tables for the current thread.')
367 array(
368 'Repl_slave_priv',
369 'REPLICATION SLAVE',
370 __('Needed for the replication slaves.')
372 array(
373 'Repl_client_priv',
374 'REPLICATION CLIENT',
375 __('Allows the user to ask where the slaves / masters are.')
377 array(
378 'Create_view_priv',
379 'CREATE VIEW',
380 __('Allows creating new views.')
382 array(
383 'Event_priv',
384 'EVENT',
385 __('Allows to set up events for the event scheduler.')
387 array(
388 'Trigger_priv',
389 'TRIGGER',
390 __('Allows creating and dropping triggers.')
392 // for table privs:
393 array(
394 'Create View_priv',
395 'CREATE VIEW',
396 __('Allows creating new views.')
398 array(
399 'Show_view_priv',
400 'SHOW VIEW',
401 __('Allows performing SHOW CREATE VIEW queries.')
403 // for table privs:
404 array(
405 'Show view_priv',
406 'SHOW VIEW',
407 __('Allows performing SHOW CREATE VIEW queries.')
409 array(
410 'Create_routine_priv',
411 'CREATE ROUTINE',
412 __('Allows creating stored routines.')
414 array(
415 'Alter_routine_priv',
416 'ALTER ROUTINE',
417 __('Allows altering and dropping stored routines.')
419 array(
420 'Create_user_priv',
421 'CREATE USER',
422 __('Allows creating, dropping and renaming user accounts.')
424 array(
425 'Execute_priv',
426 'EXECUTE',
427 __('Allows executing stored routines.')
433 * Displays on which column(s) a table-specific privilege is granted
435 * @param array $columns columns array
436 * @param array $row first row from result or boolean false
437 * @param string $name_for_select privilege types - Select_priv, Insert_priv
438 * Update_priv, References_priv
439 * @param string $priv_for_header privilege for header
440 * @param string $name privilege name: insert, select, update, references
441 * @param string $name_for_dfn name for dfn
442 * @param string $name_for_current name for current
444 * @return string $html_output html snippet
446 function PMA_getHtmlForColumnPrivileges($columns, $row, $name_for_select,
447 $priv_for_header, $name, $name_for_dfn, $name_for_current
449 $html_output = '<div class="item" id="div_item_' . $name . '">' . "\n"
450 . '<label for="select_' . $name . '_priv">' . "\n"
451 . '<code><dfn title="' . $name_for_dfn . '">'
452 . $priv_for_header . '</dfn></code>' . "\n"
453 . '</label><br />' . "\n"
454 . '<select id="select_' . $name . '_priv" name="'
455 . $name_for_select . '[]" multiple="multiple" size="8">' . "\n";
457 foreach ($columns as $currCol => $currColPrivs) {
458 $html_output .= '<option '
459 . 'value="' . htmlspecialchars($currCol) . '"';
460 if ($row[$name_for_select] == 'Y'
461 || $currColPrivs[$name_for_current]
463 $html_output .= ' selected="selected"';
465 $html_output .= '>'
466 . htmlspecialchars($currCol) . '</option>' . "\n";
469 $html_output .= '</select>' . "\n"
470 . '<i>' . __('Or') . '</i>' . "\n"
471 . '<label for="checkbox_' . $name_for_select
472 . '_none"><input type="checkbox"'
473 . ' name="' . $name_for_select . '_none" id="checkbox_'
474 . $name_for_select . '_none" title="'
475 . _pgettext('None privileges', 'None') . '" />'
476 . _pgettext('None privileges', 'None') . '</label>' . "\n"
477 . '</div>' . "\n";
478 return $html_output;
479 } // end function
482 * Get sql query for display privileges table
484 * @param string $db the database
485 * @param string $table the table
486 * @param string $username username for database connection
487 * @param string $hostname hostname for database connection
489 * @return string sql query
491 function PMA_getSqlQueryForDisplayPrivTable($db, $table, $username, $hostname)
493 if ($db == '*') {
494 return "SELECT * FROM `mysql`.`user`"
495 . " WHERE `User` = '" . PMA_Util::sqlAddSlashes($username) . "'"
496 . " AND `Host` = '" . PMA_Util::sqlAddSlashes($hostname) . "';";
497 } elseif ($table == '*') {
498 return "SELECT * FROM `mysql`.`db`"
499 . " WHERE `User` = '" . PMA_Util::sqlAddSlashes($username) . "'"
500 . " AND `Host` = '" . PMA_Util::sqlAddSlashes($hostname) . "'"
501 . " AND '" . PMA_Util::unescapeMysqlWildcards($db) . "'"
502 . " LIKE `Db`;";
504 return "SELECT `Table_priv`"
505 . " FROM `mysql`.`tables_priv`"
506 . " WHERE `User` = '" . PMA_Util::sqlAddSlashes($username) . "'"
507 . " AND `Host` = '" . PMA_Util::sqlAddSlashes($hostname) . "'"
508 . " AND `Db` = '" . PMA_Util::unescapeMysqlWildcards($db) . "'"
509 . " AND `Table_name` = '" . PMA_Util::sqlAddSlashes($table) . "';";
513 * Displays a dropdown to select the user group
514 * with menu items configured to each of them.
516 * @param string $username username
518 * @return string html to select the user group
520 function PMA_getHtmlToChooseUserGroup($username)
522 $html_output = '<form class="ajax" id="changeUserGroupForm"'
523 . ' action="server_privileges.php" method="post">';
524 $params = array('username' => $username);
525 $html_output .= PMA_URL_getHiddenInputs($params);
526 $html_output .= '<fieldset id="fieldset_user_group_selection">';
527 $html_output .= '<legend>' . __('User group') . '</legend>';
529 $cfgRelation = PMA_getRelationsParam();
530 $groupTable = PMA_Util::backquote($cfgRelation['db'])
531 . "." . PMA_Util::backquote($cfgRelation['usergroups']);
532 $userTable = PMA_Util::backquote($cfgRelation['db'])
533 . "." . PMA_Util::backquote($cfgRelation['users']);
535 $userGroups = array();
536 $sql_query = "SELECT DISTINCT `usergroup` FROM " . $groupTable;
537 $result = PMA_queryAsControlUser($sql_query, false);
538 if ($result) {
539 while ($row = $GLOBALS['dbi']->fetchRow($result)) {
540 $userGroups[] = $row[0];
543 $GLOBALS['dbi']->freeResult($result);
545 $userGroup = '';
546 if (isset($GLOBALS['username'])) {
547 $sql_query = "SELECT `usergroup` FROM " . $userTable
548 . " WHERE `username` = '" . PMA_Util::sqlAddSlashes($username) . "'";
549 $userGroup = $GLOBALS['dbi']->fetchValue(
550 $sql_query, 0, 0, $GLOBALS['controllink']
554 $html_output .= __('User group') . ': ';
555 $html_output .= '<select name="userGroup">';
556 $html_output .= '<option value=""></option>';
557 foreach ($userGroups as $oneUserGroup) {
558 $html_output .= '<option value="' . htmlspecialchars($oneUserGroup) . '"'
559 . ($oneUserGroup == $userGroup ? ' selected="selected"' : '')
560 . '>'
561 . htmlspecialchars($oneUserGroup)
562 . '</option>';
564 $html_output .= '</select>';
565 $html_output .= '<input type="hidden" name="changeUserGroup" value="1">';
566 $html_output .= '</fieldset>';
567 $html_output .= '</form>';
568 return $html_output;
572 * Sets the user group from request values
574 * @param string $username username
575 * @param string $userGroup user group to set
577 * @return void
579 function PMA_setUserGroup($username, $userGroup)
581 $cfgRelation = PMA_getRelationsParam();
582 $userTable = PMA_Util::backquote($cfgRelation['db'])
583 . "." . PMA_Util::backquote($cfgRelation['users']);
585 $sql_query = "SELECT `usergroup` FROM " . $userTable
586 . " WHERE `username` = '" . PMA_Util::sqlAddSlashes($username) . "'";
587 $oldUserGroup = $GLOBALS['dbi']->fetchValue(
588 $sql_query, 0, 0, $GLOBALS['controllink']
591 if ($oldUserGroup === false) {
592 $upd_query = "INSERT INTO " . $userTable . "(`username`, `usergroup`)"
593 . " VALUES ('" . PMA_Util::sqlAddSlashes($username) . "', "
594 . "'" . PMA_Util::sqlAddSlashes($userGroup) . "')";
595 } else {
596 if (empty($userGroup)) {
597 $upd_query = "DELETE FROM " . $userTable
598 . " WHERE `username`='" . PMA_Util::sqlAddSlashes($username) . "'";
599 } elseif ($oldUserGroup != $userGroup) {
600 $upd_query = "UPDATE " . $userTable
601 . " SET `usergroup`='" . PMA_Util::sqlAddSlashes($userGroup) . "'"
602 . " WHERE `username`='" . PMA_Util::sqlAddSlashes($username) . "'";
605 if (isset($upd_query)) {
606 PMA_queryAsControlUser($upd_query);
611 * Displays the privileges form table
613 * @param string $db the database
614 * @param string $table the table
615 * @param boolean $submit whether to display the submit button or not
617 * @global array $cfg the phpMyAdmin configuration
618 * @global resource $user_link the database connection
620 * @return string html snippet
622 function PMA_getHtmlToDisplayPrivilegesTable($db = '*',
623 $table = '*', $submit = true
625 $html_output = '';
626 $sql_query = '';
628 if ($db == '*') {
629 $table = '*';
632 if (isset($GLOBALS['username'])) {
633 $username = $GLOBALS['username'];
634 $hostname = $GLOBALS['hostname'];
635 $sql_query = PMA_getSqlQueryForDisplayPrivTable(
636 $db, $table, $username, $hostname
638 $row = $GLOBALS['dbi']->fetchSingleRow($sql_query);
640 if (empty($row)) {
641 if ($table == '*' && $GLOBALS['is_superuser']) {
642 if ($db == '*') {
643 $sql_query = 'SHOW COLUMNS FROM `mysql`.`user`;';
644 } elseif ($table == '*') {
645 $sql_query = 'SHOW COLUMNS FROM `mysql`.`db`;';
647 $res = $GLOBALS['dbi']->query($sql_query);
648 while ($row1 = $GLOBALS['dbi']->fetchRow($res)) {
649 if (mb_substr($row1[0], 0, 4) == 'max_') {
650 $row[$row1[0]] = 0;
651 } elseif (mb_substr($row1[0], 0, 5) == 'x509_'
652 || mb_substr($row1[0], 0, 4) == 'ssl_'
654 $row[$row1[0]] = '';
655 } else {
656 $row[$row1[0]] = 'N';
659 $GLOBALS['dbi']->freeResult($res);
660 } elseif ($table == '*') {
661 $row = array();
662 } else {
663 $row = array('Table_priv' => '');
666 if (isset($row['Table_priv'])) {
667 PMA_fillInTablePrivileges($row);
669 // get columns
670 $res = $GLOBALS['dbi']->tryQuery(
671 'SHOW COLUMNS FROM '
672 . PMA_Util::backquote(
673 PMA_Util::unescapeMysqlWildcards($db)
675 . '.' . PMA_Util::backquote($table) . ';'
677 $columns = array();
678 if ($res) {
679 while ($row1 = $GLOBALS['dbi']->fetchRow($res)) {
680 $columns[$row1[0]] = array(
681 'Select' => false,
682 'Insert' => false,
683 'Update' => false,
684 'References' => false
687 $GLOBALS['dbi']->freeResult($res);
689 unset($res, $row1);
691 // table-specific privileges
692 if (! empty($columns)) {
693 $html_output .= PMA_getHtmlForTableSpecificPrivileges(
694 $username, $hostname, $db, $table, $columns, $row
696 } else {
697 // global or db-specific
698 $html_output .= PMA_getHtmlForGlobalOrDbSpecificPrivs($db, $table, $row);
700 $html_output .= '</fieldset>' . "\n";
701 if ($submit) {
702 $html_output .= '<fieldset id="fieldset_user_privtable_footer" '
703 . 'class="tblFooters">' . "\n"
704 . '<input type="hidden" name="update_privs" value="1" />' . "\n"
705 . '<input type="submit" value="' . __('Go') . '" />' . "\n"
706 . '</fieldset>' . "\n";
708 return $html_output;
709 } // end of the 'PMA_displayPrivTable()' function
712 * Get HTML for "Require"
714 * @param array $row privilege array
716 * @return string html snippet
718 function PMA_getHtmlForRequires($row)
720 $html_output = '<fieldset>';
722 $html_output .= '<legend>';
723 $html_output .= '<input type="checkbox" name="SSL_priv" id="checkbox_SSL_priv"'
724 . ' value="Y" title="'
725 . __(
726 'Requires SSL-encrypted connections.'
728 . '"'
729 . ((isset($row['ssl_type']) && $row['ssl_type'] != '')
730 ? ' checked="checked"'
731 : ''
733 . '/>';
734 $html_output .= __('Require SSL') . '</legend>';
735 $html_output .= '<div id="require_ssl_div">';
737 // Specified
738 $html_output .= '<div class="item">';
739 $html_output .= '<input type="radio" name="ssl_type" id="ssl_type_specified"'
740 . ' value="specified"'
741 . ((isset($row['ssl_type']) && $row['ssl_type'] == 'SPECIFIED')
742 ? ' checked="checked"'
743 : ''
745 . '/>';
747 $html_output .= '<label for="ssl_type_speified"><code>'
748 . 'SPECIFIED'
749 . '</code></label>';
750 $html_output .= '</div>';
752 $html_output .= '<div id="specified_div" style="padding-left:20px;">';
754 // REQUIRE CIPHER
755 $html_output .= '<div class="item">';
756 $html_output .= '<label for="text_ssl_cipher">'
757 . '<code><dfn title="'
758 . __(
759 'Requires that a specific cipher method be used for a connection.'
761 . '">'
762 . 'REQUIRE CIPHER'
763 . '</dfn></code></label>';
764 $html_output .= '<input type="text" name="ssl_cipher" id="text_ssl_cipher" '
765 . 'value="' . (isset($row['ssl_cipher']) ? $row['ssl_cipher'] : '') . '" '
766 . 'size=80" title="'
767 . __(
768 'Requires that a specific cipher method be used for a connection.'
770 . '" />';
771 $html_output .= '</div>';
773 // REQUIRE ISSUER
774 $html_output .= '<div class="item">';
775 $html_output .= '<label for="text_x509_issuer">'
776 . '<code><dfn title="'
777 . __(
778 'Requires that a valid X509 certificate issued by this CA be presented.'
780 . '">'
781 . 'REQUIRE ISSUER'
782 . '</dfn></code></label>';
783 $html_output .= '<input type="text" name="x509_issuer" id="text_x509_issuer" '
784 . 'value="' . (isset($row['x509_issuer']) ? $row['x509_issuer'] : '') . '" '
785 . 'size=80" title="'
786 . __(
787 'Requires that a valid X509 certificate issued by this CA be presented.'
789 . '" />';
790 $html_output .= '</div>';
792 // REQUIRE SUBJECT
793 $html_output .= '<div class="item">';
794 $html_output .= '<label for="text_x509_subject">'
795 . '<code><dfn title="'
796 . __(
797 'Requires that a valid X509 certificate with this subject be presented.'
799 . '">'
800 . 'REQUIRE SUBJECT'
801 . '</dfn></code></label>';
802 $html_output .= '<input type="text" name="x509_subject" id="text_x509_subject" '
803 . 'value="' . (isset($row['x509_subject']) ? $row['x509_subject'] : '')
804 . '" size=80" title="'
805 . __(
806 'Requires that a valid X509 certificate with this subject be presented.'
808 . '" />';
809 $html_output .= '</div>';
811 $html_output .= '</div>';
813 // REQUIRE X509
814 $html_output .= '<div class="item">';
815 $html_output .= '<input type="radio" name="ssl_type" id="ssl_type_X509"'
816 . ' value="X509" title="'
817 . __(
818 'Requires a valid X509 certificate.'
820 . '"'
821 . ((isset($row['ssl_type']) && $row['ssl_type'] == 'X509')
822 ? ' checked="checked"'
823 : ''
825 . '/>';
827 $html_output .= '<label for="radio_X509_priv"><code>'
828 . 'REQUIRE X509'
829 . '</code></label>';
830 $html_output .= '</div>';
832 // REQUIRE SSL
833 $html_output .= '<div class="item">';
834 $html_output .= '<input type="radio" name="ssl_type" id="ssl_type_ANY"'
835 . ' value="ANY" title="'
836 . __(
837 'Requires SSL-encrypted connections.'
839 . '"'
840 . ((isset($row['ssl_type'])
841 && ($row['ssl_type'] == 'ANY'
842 || $row['ssl_type'] == ''))
843 ? ' checked="checked"'
844 : ''
846 . '/>';
848 $html_output .= '<label for="ssl_type_ANY"><code>'
849 . 'REQUIRE SSL'
850 . '</code></label>';
851 $html_output .= '</div>';
853 $html_output .= '</div>';
854 $html_output .= '</fieldset>';
856 return $html_output;
860 * Get HTML for "Resource limits"
862 * @param array $row first row from result or boolean false
864 * @return string html snippet
866 function PMA_getHtmlForResourceLimits($row)
868 $html_output = '<fieldset>' . "\n"
869 . '<legend>' . __('Resource limits') . '</legend>' . "\n"
870 . '<p><small>'
871 . '<i>' . __('Note: Setting these options to 0 (zero) removes the limit.')
872 . '</i></small></p>' . "\n";
874 $html_output .= '<div class="item">' . "\n"
875 . '<label for="text_max_questions">'
876 . '<code><dfn title="'
877 . __(
878 'Limits the number of queries the user may send to the server per hour.'
880 . '">'
881 . 'MAX QUERIES PER HOUR'
882 . '</dfn></code></label>' . "\n"
883 . '<input type="number" name="max_questions" id="text_max_questions" '
884 . 'value="'
885 . (isset($row['max_questions']) ? $row['max_questions'] : '0')
886 . '" min="0" '
887 . 'title="'
888 . __(
889 'Limits the number of queries the user may send to the server per hour.'
891 . '" />' . "\n"
892 . '</div>' . "\n";
894 $html_output .= '<div class="item">' . "\n"
895 . '<label for="text_max_updates">'
896 . '<code><dfn title="'
897 . __(
898 'Limits the number of commands that change any table '
899 . 'or database the user may execute per hour.'
900 ) . '">'
901 . 'MAX UPDATES PER HOUR'
902 . '</dfn></code></label>' . "\n"
903 . '<input type="number" name="max_updates" id="text_max_updates" '
904 . 'value="'
905 . (isset($row['max_updates']) ? $row['max_updates'] : '0')
906 . '" min="0" '
907 . 'title="'
908 . __(
909 'Limits the number of commands that change any table '
910 . 'or database the user may execute per hour.'
912 . '" />' . "\n"
913 . '</div>' . "\n";
915 $html_output .= '<div class="item">' . "\n"
916 . '<label for="text_max_connections">'
917 . '<code><dfn title="'
918 . __(
919 'Limits the number of new connections the user may open per hour.'
920 ) . '">'
921 . 'MAX CONNECTIONS PER HOUR'
922 . '</dfn></code></label>' . "\n"
923 . '<input type="number" name="max_connections" id="text_max_connections" '
924 . 'value="'
925 . (isset($row['max_connections']) ? $row['max_connections'] : '0')
926 . '" min="0" '
927 . 'title="' . __(
928 'Limits the number of new connections the user may open per hour.'
930 . '" />' . "\n"
931 . '</div>' . "\n";
933 $html_output .= '<div class="item">' . "\n"
934 . '<label for="text_max_user_connections">'
935 . '<code><dfn title="'
936 . __('Limits the number of simultaneous connections the user may have.')
937 . '">'
938 . 'MAX USER_CONNECTIONS'
939 . '</dfn></code></label>' . "\n"
940 . '<input type="number" name="max_user_connections" '
941 . 'id="text_max_user_connections" '
942 . 'value="'
943 . (isset($row['max_user_connections']) ? $row['max_user_connections'] : '0')
944 . '" '
945 . 'title="'
946 . __('Limits the number of simultaneous connections the user may have.')
947 . '" />' . "\n"
948 . '</div>' . "\n";
950 $html_output .= '</fieldset>' . "\n";
952 return $html_output;
956 * Get the HTML snippet for table specific privileges
958 * @param string $username username for database connection
959 * @param string $hostname hostname for database connection
960 * @param string $db the database
961 * @param string $table the table
962 * @param array $columns columns array
963 * @param array $row current privileges row
965 * @return string $html_output
967 function PMA_getHtmlForTableSpecificPrivileges(
968 $username, $hostname, $db, $table, $columns, $row
970 $res = $GLOBALS['dbi']->query(
971 'SELECT `Column_name`, `Column_priv`'
972 . ' FROM `mysql`.`columns_priv`'
973 . ' WHERE `User`'
974 . ' = \'' . PMA_Util::sqlAddSlashes($username) . "'"
975 . ' AND `Host`'
976 . ' = \'' . PMA_Util::sqlAddSlashes($hostname) . "'"
977 . ' AND `Db`'
978 . ' = \'' . PMA_Util::sqlAddSlashes(
979 PMA_Util::unescapeMysqlWildcards($db)
980 ) . "'"
981 . ' AND `Table_name`'
982 . ' = \'' . PMA_Util::sqlAddSlashes($table) . '\';'
985 while ($row1 = $GLOBALS['dbi']->fetchRow($res)) {
986 $row1[1] = explode(',', $row1[1]);
987 foreach ($row1[1] as $current) {
988 $columns[$row1[0]][$current] = true;
991 $GLOBALS['dbi']->freeResult($res);
992 unset($res, $row1, $current);
994 $html_output = '<input type="hidden" name="grant_count" '
995 . 'value="' . count($row) . '" />' . "\n"
996 . '<input type="hidden" name="column_count" '
997 . 'value="' . count($columns) . '" />' . "\n"
998 . '<fieldset id="fieldset_user_priv">' . "\n"
999 . '<legend data-submenu-label="Table">' . __('Table-specific privileges')
1000 . PMA_Util::showHint(
1001 __('Note: MySQL privilege names are expressed in English.')
1003 . '</legend>' . "\n";
1005 // privs that are attached to a specific column
1006 $html_output .= PMA_getHtmlForAttachedPrivilegesToTableSpecificColumn(
1007 $columns, $row
1010 // privs that are not attached to a specific column
1011 $html_output .= '<div class="item">' . "\n"
1012 . PMA_getHtmlForNotAttachedPrivilegesToTableSpecificColumn($row)
1013 . '</div>' . "\n";
1015 // for Safari 2.0.2
1016 $html_output .= '<div class="clearfloat"></div>' . "\n";
1018 return $html_output;
1022 * Get HTML snippet for privileges that are attached to a specific column
1024 * @param array $columns columns array
1025 * @param array $row first row from result or boolean false
1027 * @return string $html_output
1029 function PMA_getHtmlForAttachedPrivilegesToTableSpecificColumn($columns, $row)
1031 $html_output = PMA_getHtmlForColumnPrivileges(
1032 $columns, $row, 'Select_priv', 'SELECT',
1033 'select', __('Allows reading data.'), 'Select'
1036 $html_output .= PMA_getHtmlForColumnPrivileges(
1037 $columns, $row, 'Insert_priv', 'INSERT',
1038 'insert', __('Allows inserting and replacing data.'), 'Insert'
1041 $html_output .= PMA_getHtmlForColumnPrivileges(
1042 $columns, $row, 'Update_priv', 'UPDATE',
1043 'update', __('Allows changing data.'), 'Update'
1046 $html_output .= PMA_getHtmlForColumnPrivileges(
1047 $columns, $row, 'References_priv', 'REFERENCES', 'references',
1048 __('Has no effect in this MySQL version.'), 'References'
1050 return $html_output;
1054 * Get HTML for privileges that are not attached to a specific column
1056 * @param array $row first row from result or boolean false
1058 * @return string $html_output
1060 function PMA_getHtmlForNotAttachedPrivilegesToTableSpecificColumn($row)
1062 $html_output = '';
1064 foreach ($row as $current_grant => $current_grant_value) {
1065 $grant_type = substr($current_grant, 0, -5);
1066 if (in_array($grant_type, array('Select', 'Insert', 'Update', 'References'))
1068 continue;
1070 // make a substitution to match the messages variables;
1071 // also we must substitute the grant we get, because we can't generate
1072 // a form variable containing blanks (those would get changed to
1073 // an underscore when receiving the POST)
1074 if ($current_grant == 'Create View_priv') {
1075 $tmp_current_grant = 'CreateView_priv';
1076 $current_grant = 'Create_view_priv';
1077 } elseif ($current_grant == 'Show view_priv') {
1078 $tmp_current_grant = 'ShowView_priv';
1079 $current_grant = 'Show_view_priv';
1080 } else {
1081 $tmp_current_grant = $current_grant;
1084 $html_output .= '<div class="item">' . "\n"
1085 . '<input type="checkbox"'
1086 . ' name="' . $current_grant . '" id="checkbox_' . $current_grant
1087 . '" value="Y" '
1088 . ($current_grant_value == 'Y' ? 'checked="checked" ' : '')
1089 . 'title="';
1091 $privGlobalName = 'strPrivDesc'
1092 . /*overload*/mb_substr(
1093 $tmp_current_grant,
1095 (/*overload*/mb_strlen($tmp_current_grant) - 5)
1097 $html_output .= (isset($GLOBALS[$privGlobalName])
1098 ? $GLOBALS[$privGlobalName]
1099 : $GLOBALS[$privGlobalName . 'Tbl']
1101 . '"/>' . "\n";
1103 $privGlobalName1 = 'strPrivDesc'
1104 . /*overload*/mb_substr(
1105 $tmp_current_grant,
1109 $html_output .= '<label for="checkbox_' . $current_grant
1110 . '"><code><dfn title="'
1111 . (isset($GLOBALS[$privGlobalName1])
1112 ? $GLOBALS[$privGlobalName1]
1113 : $GLOBALS[$privGlobalName1 . 'Tbl']
1115 . '">'
1116 . /*overload*/mb_strtoupper(
1117 /*overload*/mb_substr(
1118 $current_grant,
1123 . '</dfn></code></label>' . "\n"
1124 . '</div>' . "\n";
1125 } // end foreach ()
1126 return $html_output;
1130 * Get HTML for global or database specific privileges
1132 * @param string $db the database
1133 * @param string $table the table
1134 * @param array $row first row from result or boolean false
1136 * @return string $html_output
1138 function PMA_getHtmlForGlobalOrDbSpecificPrivs($db, $table, $row)
1140 $privTable_names = array(0 => __('Data'),
1141 1 => __('Structure'),
1142 2 => __('Administration')
1144 $privTable = array();
1145 // d a t a
1146 $privTable[0] = PMA_getDataPrivilegeTable($db);
1148 // s t r u c t u r e
1149 $privTable[1] = PMA_getStructurePrivilegeTable($table, $row);
1151 // a d m i n i s t r a t i o n
1152 $privTable[2] = PMA_getAdministrationPrivilegeTable($db);
1154 $html_output = '<input type="hidden" name="grant_count" value="'
1155 . (count($privTable[0])
1156 + count($privTable[1])
1157 + count($privTable[2])
1158 - (isset($row['Grant_priv']) ? 1 : 0)
1160 . '" />';
1161 if ($db == '*') {
1162 $legend = __('Global privileges');
1163 $menu_label = __('Global');
1164 } else if ($table == '*') {
1165 $legend = __('Database-specific privileges');
1166 $menu_label = __('Database');
1167 } else {
1168 $legend = __('Table-specific privileges');
1169 $menu_label = __('Table');
1171 $html_output .= '<fieldset id="fieldset_user_global_rights">'
1172 . '<legend data-submenu-label="' . $menu_label . '">' . $legend
1173 . '<input type="checkbox" id="addUsersForm_checkall" '
1174 . 'class="checkall_box" title="' . __('Check all') . '" /> '
1175 . '<label for="addUsersForm_checkall">' . __('Check all') . '</label> '
1176 . '</legend>'
1177 . '<p><small><i>'
1178 . __('Note: MySQL privilege names are expressed in English.')
1179 . '</i></small></p>';
1181 // Output the Global privilege tables with checkboxes
1182 $html_output .= PMA_getHtmlForGlobalPrivTableWithCheckboxes(
1183 $privTable, $privTable_names, $row
1186 // The "Resource limits" box is not displayed for db-specific privs
1187 if ($db == '*') {
1188 $html_output .= PMA_getHtmlForResourceLimits($row);
1189 $html_output .= PMA_getHtmlForRequires($row);
1191 // for Safari 2.0.2
1192 $html_output .= '<div class="clearfloat"></div>';
1194 return $html_output;
1198 * Get data privilege table as an array
1200 * @param string $db the database
1202 * @return string data privilege table
1204 function PMA_getDataPrivilegeTable($db)
1206 $data_privTable = array(
1207 array('Select', 'SELECT', __('Allows reading data.')),
1208 array('Insert', 'INSERT', __('Allows inserting and replacing data.')),
1209 array('Update', 'UPDATE', __('Allows changing data.')),
1210 array('Delete', 'DELETE', __('Allows deleting data.'))
1212 if ($db == '*') {
1213 $data_privTable[]
1214 = array('File',
1215 'FILE',
1216 __('Allows importing data from and exporting data into files.')
1219 return $data_privTable;
1223 * Get structure privilege table as an array
1225 * @param string $table the table
1226 * @param array $row first row from result or boolean false
1228 * @return string structure privilege table
1230 function PMA_getStructurePrivilegeTable($table, $row)
1232 $structure_privTable = array(
1233 array('Create',
1234 'CREATE',
1235 ($table == '*'
1236 ? __('Allows creating new databases and tables.')
1237 : __('Allows creating new tables.')
1240 array('Alter',
1241 'ALTER',
1242 __('Allows altering the structure of existing tables.')
1244 array('Index', 'INDEX', __('Allows creating and dropping indexes.')),
1245 array('Drop',
1246 'DROP',
1247 ($table == '*'
1248 ? __('Allows dropping databases and tables.')
1249 : __('Allows dropping tables.')
1252 array('Create_tmp_table',
1253 'CREATE TEMPORARY TABLES',
1254 __('Allows creating temporary tables.')
1256 array('Show_view',
1257 'SHOW VIEW',
1258 __('Allows performing SHOW CREATE VIEW queries.')
1260 array('Create_routine',
1261 'CREATE ROUTINE',
1262 __('Allows creating stored routines.')
1264 array('Alter_routine',
1265 'ALTER ROUTINE',
1266 __('Allows altering and dropping stored routines.')
1268 array('Execute', 'EXECUTE', __('Allows executing stored routines.')),
1270 // this one is for a db-specific priv: Create_view_priv
1271 if (isset($row['Create_view_priv'])) {
1272 $structure_privTable[] = array('Create_view',
1273 'CREATE VIEW',
1274 __('Allows creating new views.')
1277 // this one is for a table-specific priv: Create View_priv
1278 if (isset($row['Create View_priv'])) {
1279 $structure_privTable[] = array('Create View',
1280 'CREATE VIEW',
1281 __('Allows creating new views.')
1284 if (isset($row['Event_priv'])) {
1285 // MySQL 5.1.6
1286 $structure_privTable[] = array('Event',
1287 'EVENT',
1288 __('Allows to set up events for the event scheduler.')
1290 $structure_privTable[] = array('Trigger',
1291 'TRIGGER',
1292 __('Allows creating and dropping triggers.')
1295 return $structure_privTable;
1299 * Get administration privilege table as an array
1301 * @param string $db the table
1303 * @return string administration privilege table
1305 function PMA_getAdministrationPrivilegeTable($db)
1307 $adminPrivTable = array(
1308 array('Grant',
1309 'GRANT',
1311 'Allows adding users and privileges '
1312 . 'without reloading the privilege tables.'
1316 if ($db == '*') {
1317 $adminPrivTable[] = array('Super',
1318 'SUPER',
1320 'Allows connecting, even if maximum number '
1321 . 'of connections is reached; required for '
1322 . 'most administrative operations like '
1323 . 'setting global variables or killing threads of other users.'
1326 $adminPrivTable[] = array('Process',
1327 'PROCESS',
1328 __('Allows viewing processes of all users.')
1330 $adminPrivTable[] = array('Reload',
1331 'RELOAD',
1332 __('Allows reloading server settings and flushing the server\'s caches.')
1334 $adminPrivTable[] = array('Shutdown',
1335 'SHUTDOWN',
1336 __('Allows shutting down the server.')
1338 $adminPrivTable[] = array('Show_db',
1339 'SHOW DATABASES',
1340 __('Gives access to the complete list of databases.')
1343 $adminPrivTable[] = array('Lock_tables',
1344 'LOCK TABLES',
1345 __('Allows locking tables for the current thread.')
1347 $adminPrivTable[] = array('References',
1348 'REFERENCES',
1349 __('Has no effect in this MySQL version.')
1351 if ($db == '*') {
1352 $adminPrivTable[] = array('Repl_client',
1353 'REPLICATION CLIENT',
1354 __('Allows the user to ask where the slaves / masters are.')
1356 $adminPrivTable[] = array('Repl_slave',
1357 'REPLICATION SLAVE',
1358 __('Needed for the replication slaves.')
1360 $adminPrivTable[] = array('Create_user',
1361 'CREATE USER',
1362 __('Allows creating, dropping and renaming user accounts.')
1365 return $adminPrivTable;
1369 * Get HTML snippet for global privileges table with check boxes
1371 * @param array $privTable privileges table array
1372 * @param array $privTable_names names of the privilege tables
1373 * (Data, Structure, Administration)
1374 * @param array $row first row from result or boolean false
1376 * @return string $html_output
1378 function PMA_getHtmlForGlobalPrivTableWithCheckboxes(
1379 $privTable, $privTable_names, $row
1381 $html_output = '';
1382 foreach ($privTable as $i => $table) {
1383 $html_output .= '<fieldset>' . "\n"
1384 . '<legend>' . "\n"
1385 . '<input type="checkbox" class="sub_checkall_box"'
1386 . ' id="checkall_' . $privTable_names[$i] . '_priv"'
1387 . ' title="' . __('Check all') . '"/>'
1388 . '<label for="checkall_' . $privTable_names[$i] . '_priv">'
1389 . $privTable_names[$i] . '</label>' . "\n"
1390 . '</legend>' . "\n";
1391 foreach ($table as $priv) {
1392 $html_output .= '<div class="item">' . "\n"
1393 . '<input type="checkbox" class="checkall"'
1394 . ' name="' . $priv[0] . '_priv" '
1395 . 'id="checkbox_' . $priv[0] . '_priv"'
1396 . ' value="Y" title="' . $priv[2] . '"'
1397 . ((isset($row[$priv[0] . '_priv'])
1398 && $row[$priv[0] . '_priv'] == 'Y')
1399 ? ' checked="checked"'
1400 : ''
1402 . '/>' . "\n"
1403 . '<label for="checkbox_' . $priv[0] . '_priv">'
1404 . '<code>'
1405 . PMA_formatPrivilege($priv, true)
1406 . '</code></label>' . "\n"
1407 . '</div>' . "\n";
1409 $html_output .= '</fieldset>' . "\n";
1411 return $html_output;
1415 * Displays the fields used by the "new user" form as well as the
1416 * "change login information / copy user" form.
1418 * @param string $mode are we creating a new user or are we just
1419 * changing one? (allowed values: 'new', 'change')
1420 * @param string $username User name
1421 * @param string $hostname Host name
1423 * @global array $cfg the phpMyAdmin configuration
1424 * @global resource $user_link the database connection
1426 * @return string $html_output a HTML snippet
1428 function PMA_getHtmlForLoginInformationFields(
1429 $mode = 'new',
1430 $username = null,
1431 $hostname = null
1433 list($username_length, $hostname_length) = PMA_getUsernameAndHostnameLength();
1435 if (isset($GLOBALS['username'])
1436 && /*overload*/mb_strlen($GLOBALS['username']) === 0
1438 $GLOBALS['pred_username'] = 'any';
1440 $html_output = '<fieldset id="fieldset_add_user_login">' . "\n"
1441 . '<legend>' . __('Login Information') . '</legend>' . "\n"
1442 . '<div class="item">' . "\n"
1443 . '<label for="select_pred_username">' . "\n"
1444 . ' ' . __('User name:') . "\n"
1445 . '</label>' . "\n"
1446 . '<span class="options">' . "\n";
1448 $html_output .= '<select name="pred_username" id="select_pred_username" '
1449 . 'title="' . __('User name') . '"' . "\n";
1451 $html_output .= ' onchange="'
1452 . 'if (this.value == \'any\') {'
1453 . ' username.value = \'\'; '
1454 . ' user_exists_warning.style.display = \'none\'; '
1455 . ' username.required = false; '
1456 . '} else if (this.value == \'userdefined\') {'
1457 . ' username.focus(); username.select(); '
1458 . ' username.required = true; '
1459 . '}">' . "\n";
1461 $html_output .= '<option value="any"'
1462 . ((isset($GLOBALS['pred_username']) && $GLOBALS['pred_username'] == 'any')
1463 ? ' selected="selected"'
1464 : '') . '>'
1465 . __('Any user')
1466 . '</option>' . "\n";
1468 $html_output .= '<option value="userdefined"'
1469 . ((! isset($GLOBALS['pred_username'])
1470 || $GLOBALS['pred_username'] == 'userdefined'
1472 ? ' selected="selected"'
1473 : '') . '>'
1474 . __('Use text field')
1475 . ':</option>' . "\n";
1477 $html_output .= '</select>' . "\n"
1478 . '</span>' . "\n";
1480 $html_output .= '<input type="text" name="username" class="autofocus"'
1481 . ' maxlength="' . $username_length . '" title="' . __('User name') . '"'
1482 . (empty($GLOBALS['username'])
1483 ? ''
1484 : ' value="' . htmlspecialchars(
1485 isset($GLOBALS['new_username'])
1486 ? $GLOBALS['new_username']
1487 : $GLOBALS['username']
1488 ) . '"'
1490 . ' onchange="pred_username.value = \'userdefined\'; this.required = true;" '
1491 . ((! isset($GLOBALS['pred_username'])
1492 || $GLOBALS['pred_username'] == 'userdefined'
1494 ? 'required="required"'
1495 : '') . ' />' . "\n";
1497 $html_output .= '<div id="user_exists_warning"'
1498 . ' name="user_exists_warning" style="display:none;">'
1499 . PMA_Message::notice(
1501 'An account already exists with the same username '
1502 . 'but possibly a different hostname.'
1504 )->getDisplay()
1505 . '</div>';
1506 $html_output .= '</div>';
1508 $html_output .= '<div class="item">' . "\n"
1509 . '<label for="select_pred_hostname">' . "\n"
1510 . ' ' . __('Host name:') . "\n"
1511 . '</label>' . "\n";
1513 $html_output .= '<span class="options">' . "\n"
1514 . ' <select name="pred_hostname" id="select_pred_hostname" '
1515 . 'title="' . __('Host name') . '"' . "\n";
1516 $_current_user = $GLOBALS['dbi']->fetchValue('SELECT USER();');
1517 if (! empty($_current_user)) {
1518 $thishost = str_replace(
1519 "'",
1521 /*overload*/mb_substr(
1522 $_current_user,
1523 (/*overload*/mb_strrpos($_current_user, '@') + 1)
1526 if ($thishost == 'localhost' || $thishost == '127.0.0.1') {
1527 unset($thishost);
1530 $html_output .= ' onchange="'
1531 . 'if (this.value == \'any\') { '
1532 . ' hostname.value = \'%\'; '
1533 . '} else if (this.value == \'localhost\') { '
1534 . ' hostname.value = \'localhost\'; '
1535 . '} '
1536 . (empty($thishost)
1537 ? ''
1538 : 'else if (this.value == \'thishost\') { '
1539 . ' hostname.value = \'' . addslashes(htmlspecialchars($thishost))
1540 . '\'; '
1541 . '} '
1543 . 'else if (this.value == \'hosttable\') { '
1544 . ' hostname.value = \'\'; '
1545 . ' hostname.required = false; '
1546 . '} else if (this.value == \'userdefined\') {'
1547 . ' hostname.focus(); hostname.select(); '
1548 . ' hostname.required = true; '
1549 . '}">' . "\n";
1550 unset($_current_user);
1552 // when we start editing a user, $GLOBALS['pred_hostname'] is not defined
1553 if (! isset($GLOBALS['pred_hostname']) && isset($GLOBALS['hostname'])) {
1554 switch (/*overload*/mb_strtolower($GLOBALS['hostname'])) {
1555 case 'localhost':
1556 case '127.0.0.1':
1557 $GLOBALS['pred_hostname'] = 'localhost';
1558 break;
1559 case '%':
1560 $GLOBALS['pred_hostname'] = 'any';
1561 break;
1562 default:
1563 $GLOBALS['pred_hostname'] = 'userdefined';
1564 break;
1567 $html_output .= '<option value="any"'
1568 . ((isset($GLOBALS['pred_hostname'])
1569 && $GLOBALS['pred_hostname'] == 'any'
1571 ? ' selected="selected"'
1572 : '') . '>'
1573 . __('Any host')
1574 . '</option>' . "\n"
1575 . '<option value="localhost"'
1576 . ((isset($GLOBALS['pred_hostname'])
1577 && $GLOBALS['pred_hostname'] == 'localhost'
1579 ? ' selected="selected"'
1580 : '') . '>'
1581 . __('Local')
1582 . '</option>' . "\n";
1583 if (! empty($thishost)) {
1584 $html_output .= '<option value="thishost"'
1585 . ((isset($GLOBALS['pred_hostname'])
1586 && $GLOBALS['pred_hostname'] == 'thishost'
1588 ? ' selected="selected"'
1589 : '') . '>'
1590 . __('This Host')
1591 . '</option>' . "\n";
1593 unset($thishost);
1594 $html_output .= '<option value="hosttable"'
1595 . ((isset($GLOBALS['pred_hostname'])
1596 && $GLOBALS['pred_hostname'] == 'hosttable'
1598 ? ' selected="selected"'
1599 : '') . '>'
1600 . __('Use Host Table')
1601 . '</option>' . "\n";
1603 $html_output .= '<option value="userdefined"'
1604 . ((isset($GLOBALS['pred_hostname'])
1605 && $GLOBALS['pred_hostname'] == 'userdefined'
1607 ? ' selected="selected"'
1608 : '') . '>'
1609 . __('Use text field:') . '</option>' . "\n"
1610 . '</select>' . "\n"
1611 . '</span>' . "\n";
1613 $html_output .= '<input type="text" name="hostname" maxlength="'
1614 . $hostname_length . '" value="'
1615 // use default value of '%' to match with the default 'Any host'
1616 . htmlspecialchars(isset($GLOBALS['hostname']) ? $GLOBALS['hostname'] : '%')
1617 . '" title="' . __('Host name')
1618 . '" onchange="pred_hostname.value = \'userdefined\'; '
1619 . 'this.required = true;" '
1620 . ((isset($GLOBALS['pred_hostname'])
1621 && $GLOBALS['pred_hostname'] == 'userdefined'
1623 ? 'required="required"'
1624 : '')
1625 . ' />' . "\n"
1626 . PMA_Util::showHint(
1628 'When Host table is used, this field is ignored '
1629 . 'and values stored in Host table are used instead.'
1632 . '</div>' . "\n";
1634 $orig_auth_plugin = PMA_getCurrentAuthenticationPlugin(
1635 $mode, $username, $hostname
1638 $html_output .= '<div class="item">' . "\n"
1639 . '<label for="select_pred_password">' . "\n"
1640 . ' ' . __('Password:') . "\n"
1641 . '</label>' . "\n"
1642 . '<span class="options">' . "\n"
1643 . '<select name="pred_password" id="select_pred_password" title="'
1644 . __('Password') . '"' . "\n";
1646 $html_output .= ' onchange="'
1647 . 'if (this.value == \'none\') { '
1648 . ' pma_pw.value = \'\'; pma_pw2.value = \'\'; '
1649 . ' pma_pw.required = false; pma_pw2.required = false; '
1650 . '} else if (this.value == \'userdefined\') { '
1651 . ' pma_pw.focus(); pma_pw.select(); '
1652 . ' pma_pw.required = true; pma_pw2.required = true; '
1653 . '} else { '
1654 . ' pma_pw.required = false; pma_pw2.required = false; '
1655 . '}">' . "\n"
1656 . ($mode == 'change' ? '<option value="keep" selected="selected">'
1657 . __('Do not change the password')
1658 . '</option>' . "\n" : '')
1659 . '<option value="none"';
1661 if (isset($GLOBALS['username']) && $mode != 'change') {
1662 $html_output .= ' selected="selected"';
1664 $html_output .= '>' . __('No Password') . '</option>' . "\n"
1665 . '<option value="userdefined"'
1666 . (isset($GLOBALS['username']) ? '' : ' selected="selected"') . '>'
1667 . __('Use text field')
1668 . ':</option>' . "\n"
1669 . '</select>' . "\n"
1670 . '</span>' . "\n"
1671 . '<input type="password" id="text_pma_pw" name="pma_pw" '
1672 . 'title="' . __('Password') . '" '
1673 . 'onchange="pred_password.value = \'userdefined\'; this.required = true; '
1674 . 'pma_pw2.required = true;" '
1675 . (isset($GLOBALS['username']) ? '' : 'required="required"')
1676 . '/>' . "\n"
1677 . '</div>' . "\n";
1679 $html_output .= '<div class="item" '
1680 . 'id="div_element_before_generate_password">' . "\n"
1681 . '<label for="text_pma_pw2">' . "\n"
1682 . ' ' . __('Re-type:') . "\n"
1683 . '</label>' . "\n"
1684 . '<span class="options">&nbsp;</span>' . "\n"
1685 . '<input type="password" name="pma_pw2" id="text_pma_pw2" '
1686 . 'title="' . __('Re-type') . '" '
1687 . 'onchange="pred_password.value = \'userdefined\'; this.required = true; '
1688 . 'pma_pw.required = true;" '
1689 . (isset($GLOBALS['username']) ? '' : 'required="required"')
1690 . '/>' . "\n"
1691 . '</div>' . "\n"
1692 . '<div class="item" id="authentication_plugin_div">'
1693 . '<label for="select_authentication_plugin" >'
1694 . __('Authentication Plugin')
1695 . '</label><span class="options">&nbsp;</span>' . "\n"
1696 . '<select id="select_authentication_plugin" name="authentication_plugin" '
1697 . 'title="' . __('Authentication Plugin') . '" >'
1698 . '<option value="mysql_native_password" '
1699 . ($orig_auth_plugin == 'mysql_native_password' ? 'selected ' : '')
1700 . '>' . __('MySQL native password') . '</option>';
1702 // sha256 auth plugin exists only for 5.6.6+
1703 if (PMA_Util::getServerType() == 'MySQL'
1704 && PMA_MYSQL_INT_VERSION >= 50606
1706 $html_output .= '<option value="sha256_password" '
1707 . ($orig_auth_plugin == 'sha256_password' ? ' selected ' : '')
1708 . ' >' . __('SHA256 password') . '</option>';
1711 $html_output .= '</select>'
1712 . '<div id="ssl_reqd_warning" '
1713 . ($orig_auth_plugin == 'sha256_password' ? '' : ' style="display:none"')
1714 . ' >'
1715 . PMA_Message::notice(
1717 'This method requires using an \'<i>SSL connection</i>\' '
1718 . 'or an \'<i>unencrypted connection that encrypts the password '
1719 . 'using RSA</i>\'; while connecting to the server.'
1721 . PMA_Util::showMySQLDocu('sha256-authentication-plugin')
1722 )->getDisplay()
1723 . '</div>'
1724 . '</div>' . "\n"
1725 // Generate password added here via jQuery
1726 . '</fieldset>' . "\n";
1728 return $html_output;
1729 } // end of the 'PMA_getHtmlForLoginInformationFields()' function
1732 * Get username and hostname length
1734 * @return array username length and hostname length
1736 function PMA_getUsernameAndHostnameLength()
1738 /* Fallback values */
1739 $username_length = 16;
1740 $hostname_length = 41;
1742 /* Try to get real lengths from the database */
1743 $fields_info = $GLOBALS['dbi']->fetchResult(
1744 'SELECT COLUMN_NAME, CHARACTER_MAXIMUM_LENGTH '
1745 . 'FROM information_schema.columns '
1746 . "WHERE table_schema = 'mysql' AND table_name = 'user' "
1747 . "AND COLUMN_NAME IN ('User', 'Host')"
1749 foreach ($fields_info as $val) {
1750 if ($val['COLUMN_NAME'] == 'User') {
1751 $username_length = $val['CHARACTER_MAXIMUM_LENGTH'];
1752 } elseif ($val['COLUMN_NAME'] == 'Host') {
1753 $hostname_length = $val['CHARACTER_MAXIMUM_LENGTH'];
1756 return array($username_length, $hostname_length);
1760 * Get current authentication plugin in use - for a user or globally
1762 * @param string $mode are we creating a new user or are we just
1763 * changing one? (allowed values: 'new', 'change')
1764 * @param string $username User name
1765 * @param string $hostname Host name
1767 * @return string authentication plugin in use
1769 function PMA_getCurrentAuthenticationPlugin(
1770 $mode = 'new',
1771 $username = null,
1772 $hostname = null
1774 /* Fallback (standard) value */
1775 $authentication_plugin = 'mysql_native_password';
1777 if (isset($username) && isset($hostname)
1778 && $mode == 'change'
1780 $row = $GLOBALS['dbi']->fetchSingleRow(
1781 'SELECT `plugin` FROM `mysql`.`user` WHERE '
1782 . '`User` = "' . $username . '" AND `Host` = "' . $hostname . '" LIMIT 1'
1784 // Table 'mysql'.'user' may not exist for some previous
1785 // versions of MySQL - in that case consider fallback value
1786 if (isset($row) && $row) {
1787 $authentication_plugin = $row['plugin'];
1789 } elseif ($mode == 'change') {
1790 $row = $GLOBALS['dbi']->fetchSingleRow(
1791 'SELECT CURRENT_USER() as user;'
1793 if (isset($row) && $row) {
1794 list($username, $hostname) = explode('@', $row['user']);
1797 $row = $GLOBALS['dbi']->fetchSingleRow(
1798 'SELECT `plugin` FROM `mysql`.`user` WHERE '
1799 . '`User` = "' . $username . '" AND `Host` = "' . $hostname . '"'
1801 if (isset($row) && $row && ! empty($row['plugin'])) {
1802 $authentication_plugin = $row['plugin'];
1804 } else {
1805 $row = $GLOBALS['dbi']->fetchSingleRow(
1806 'SHOW VARIABLES like \'default_authentication_plugin\''
1808 $authentication_plugin = $row['Value'];
1811 return $authentication_plugin;
1815 * Returns all the grants for a certain user on a certain host
1816 * Used in the export privileges for all users section
1818 * @param string $user User name
1819 * @param string $host Host name
1821 * @return string containing all the grants text
1823 function PMA_getGrants($user, $host)
1825 $grants = $GLOBALS['dbi']->fetchResult(
1826 "SHOW GRANTS FOR '"
1827 . PMA_Util::sqlAddSlashes($user) . "'@'"
1828 . PMA_Util::sqlAddSlashes($host) . "'"
1830 $response = '';
1831 foreach ($grants as $one_grant) {
1832 $response .= $one_grant . ";\n\n";
1834 return $response;
1835 } // end of the 'PMA_getGrants()' function
1838 * Update password and get message for password updating
1840 * @param string $err_url error url
1841 * @param string $username username
1842 * @param string $hostname hostname
1844 * @return string $message success or error message after updating password
1846 function PMA_updatePassword($err_url, $username, $hostname)
1848 // similar logic in user_password.php
1849 $message = '';
1851 if (empty($_REQUEST['nopass'])
1852 && isset($_POST['pma_pw'])
1853 && isset($_POST['pma_pw2'])
1855 if ($_POST['pma_pw'] != $_POST['pma_pw2']) {
1856 $message = PMA_Message::error(__('The passwords aren\'t the same!'));
1857 } elseif (empty($_POST['pma_pw']) || empty($_POST['pma_pw2'])) {
1858 $message = PMA_Message::error(__('The password is empty!'));
1862 // here $nopass could be == 1
1863 if (empty($message)) {
1864 if (PMA_Util::getServerType() == 'MySQL'
1865 && PMA_MYSQL_INT_VERSION >= 50706
1867 if (! empty($_REQUEST['pw_hash']) && $_REQUEST['pw_hash'] != 'old') {
1868 $query_prefix = "ALTER USER '"
1869 . PMA_Util::sqlAddSlashes($username)
1870 . "'@'" . PMA_Util::sqlAddSlashes($hostname) . "'"
1871 . " IDENTIFIED WITH " . $_REQUEST['pw_hash']
1872 . " BY '";
1873 } else {
1874 $query_prefix = "ALTER USER '"
1875 . PMA_Util::sqlAddSlashes($username)
1876 . "'@'" . PMA_Util::sqlAddSlashes($hostname) . "'"
1877 . " IDENTIFIED BY '";
1880 // in $sql_query which will be displayed, hide the password
1881 $sql_query = $query_prefix . "*'";
1883 $local_query = $query_prefix
1884 . PMA_Util::sqlAddSlashes($_POST['pma_pw']) . "'";
1885 } else {
1886 if (! empty($_REQUEST['pw_hash']) && $_REQUEST['pw_hash'] == 'old') {
1887 $hashing_function = 'OLD_PASSWORD';
1888 } elseif (! empty($_REQUEST['pw_hash'])
1889 && $_REQUEST['pw_hash'] == 'sha256_password'
1891 $hashing_function = 'PASSWORD';
1893 // Backup the old value, to be reset later
1894 $row = $GLOBALS['dbi']->fetchSingleRow(
1895 'SHOW VARIABLES like \'old_passwords\';'
1897 $orig_value = $row['Value'];
1898 // Set the hashing method used by PASSWORD()
1899 // to be 'sha256_password' type
1900 $GLOBALS['dbi']->tryQuery('SET old_passwords = 2;');
1901 } else {
1902 $hashing_function = 'PASSWORD';
1905 $sql_query = 'SET PASSWORD FOR \''
1906 . PMA_Util::sqlAddSlashes($username)
1907 . '\'@\'' . PMA_Util::sqlAddSlashes($hostname) . '\' = '
1908 . (($_POST['pma_pw'] == '')
1909 ? '\'\''
1910 : $hashing_function . '(\''
1911 . preg_replace('@.@s', '*', $_POST['pma_pw']) . '\')');
1913 $local_query = 'SET PASSWORD FOR \''
1914 . PMA_Util::sqlAddSlashes($username)
1915 . '\'@\'' . PMA_Util::sqlAddSlashes($hostname) . '\' = '
1916 . (($_POST['pma_pw'] == '') ? '\'\'' : $hashing_function
1917 . '(\'' . PMA_Util::sqlAddSlashes($_POST['pma_pw']) . '\')');
1920 $GLOBALS['dbi']->tryQuery($local_query)
1921 or PMA_Util::mysqlDie(
1922 $GLOBALS['dbi']->getError(), $sql_query, false, $err_url
1924 $message = PMA_Message::success(
1925 __('The password for %s was changed successfully.')
1927 $message->addParam(
1928 '\'' . htmlspecialchars($username)
1929 . '\'@\'' . htmlspecialchars($hostname) . '\''
1931 if (isset($orig_value)) {
1932 $GLOBALS['dbi']->tryQuery(
1933 'SET `old_passwords` = ' . $orig_value . ';'
1937 return $message;
1941 * Revokes privileges and get message and SQL query for privileges revokes
1943 * @param string $dbname database name
1944 * @param string $tablename table name
1945 * @param string $username username
1946 * @param string $hostname host name
1948 * @return array ($message, $sql_query)
1950 function PMA_getMessageAndSqlQueryForPrivilegesRevoke($dbname,
1951 $tablename, $username, $hostname
1953 $db_and_table = PMA_wildcardEscapeForGrant($dbname, $tablename);
1955 $sql_query0 = 'REVOKE ALL PRIVILEGES ON ' . $db_and_table
1956 . ' FROM \''
1957 . PMA_Util::sqlAddSlashes($username) . '\'@\''
1958 . PMA_Util::sqlAddSlashes($hostname) . '\';';
1960 $sql_query1 = 'REVOKE GRANT OPTION ON ' . $db_and_table
1961 . ' FROM \'' . PMA_Util::sqlAddSlashes($username) . '\'@\''
1962 . PMA_Util::sqlAddSlashes($hostname) . '\';';
1964 $GLOBALS['dbi']->query($sql_query0);
1965 if (! $GLOBALS['dbi']->tryQuery($sql_query1)) {
1966 // this one may fail, too...
1967 $sql_query1 = '';
1969 $sql_query = $sql_query0 . ' ' . $sql_query1;
1970 $message = PMA_Message::success(
1971 __('You have revoked the privileges for %s.')
1973 $message->addParam(
1974 '\'' . htmlspecialchars($username)
1975 . '\'@\'' . htmlspecialchars($hostname) . '\''
1978 return array($message, $sql_query);
1982 * Get REQUIRE cluase
1984 * @return string REQUIRE clause
1986 function PMA_getRequireClause()
1988 $require_clause = "";
1989 if (isset($_POST['SSL_priv']) && $_POST['SSL_priv'] == 'Y') {
1990 if (isset($_POST['ssl_type']) && $_POST['ssl_type'] == 'specified') {
1991 $require = array();
1992 if (! empty($_POST['ssl_cipher'])) {
1993 $require[] = "CIPHER '"
1994 . PMA_Util::sqlAddSlashes($_POST['ssl_cipher']) . "'";
1996 if (! empty($_POST['x509_issuer'])) {
1997 $require[] = "ISSUER '"
1998 . PMA_Util::sqlAddSlashes($_POST['x509_issuer']) . "'";
2000 if (! empty($_POST['x509_subject'])) {
2001 $require[] = "SUBJECT '"
2002 . PMA_Util::sqlAddSlashes($_POST['x509_subject']) . "'";
2004 if (count($require)) {
2005 $require_clause = " REQUIRE " . implode(" AND ", $require);
2006 } else {
2007 $require_clause = " REQUIRE NONE";
2009 } elseif (isset($_POST['ssl_type']) && $_POST['ssl_type'] == 'X509') {
2010 $require_clause = " REQUIRE X509";
2011 } elseif (isset($_POST['ssl_type']) && $_POST['ssl_type'] == 'ANY') {
2012 $require_clause = " REQUIRE SSL";
2014 } else {
2015 $require_clause = " REQUIRE NONE";
2018 return $require_clause;
2022 * Get a WITH clause for 'update privileges' and 'add user'
2024 * @return string $sql_query
2026 function PMA_getWithClauseForAddUserAndUpdatePrivs()
2028 $sql_query = '';
2029 if (isset($_POST['Grant_priv']) && $_POST['Grant_priv'] == 'Y') {
2030 $sql_query .= ' GRANT OPTION';
2032 if (isset($_POST['max_questions'])) {
2033 $max_questions = max(0, (int)$_POST['max_questions']);
2034 $sql_query .= ' MAX_QUERIES_PER_HOUR ' . $max_questions;
2036 if (isset($_POST['max_connections'])) {
2037 $max_connections = max(0, (int)$_POST['max_connections']);
2038 $sql_query .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections;
2040 if (isset($_POST['max_updates'])) {
2041 $max_updates = max(0, (int)$_POST['max_updates']);
2042 $sql_query .= ' MAX_UPDATES_PER_HOUR ' . $max_updates;
2044 if (isset($_POST['max_user_connections'])) {
2045 $max_user_connections = max(0, (int)$_POST['max_user_connections']);
2046 $sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections;
2048 return ((!empty($sql_query)) ? ' WITH' . $sql_query : '');
2052 * Get HTML for addUsersForm, This function call if isset($_REQUEST['adduser'])
2054 * @param string $dbname database name
2056 * @return string HTML for addUserForm
2058 function PMA_getHtmlForAddUser($dbname)
2060 $html_output = '<h2>' . "\n"
2061 . PMA_Util::getIcon('b_usradd.png') . __('Add user account') . "\n"
2062 . '</h2>' . "\n"
2063 . '<form name="usersForm" id="addUsersForm"'
2064 . ' onsubmit="return checkAddUser(this);"'
2065 . ' action="server_privileges.php" method="post" autocomplete="off" >' . "\n"
2066 . PMA_URL_getHiddenInputs('', '')
2067 . PMA_getHtmlForLoginInformationFields('new');
2069 $html_output .= '<fieldset id="fieldset_add_user_database">' . "\n"
2070 . '<legend>' . __('Database for user account') . '</legend>' . "\n";
2072 $html_output .= PMA_Util::getCheckbox(
2073 'createdb-1',
2074 __('Create database with same name and grant all privileges.'),
2075 false, false, 'createdb-1'
2077 $html_output .= '<br />' . "\n";
2078 $html_output .= PMA_Util::getCheckbox(
2079 'createdb-2',
2080 __('Grant all privileges on wildcard name (username\\_%).'),
2081 false, false, 'createdb-2'
2083 $html_output .= '<br />' . "\n";
2085 if (! empty($dbname) ) {
2086 $html_output .= PMA_Util::getCheckbox(
2087 'createdb-3',
2088 sprintf(
2089 __('Grant all privileges on database "%s".'),
2090 htmlspecialchars($dbname)
2092 true,
2093 false,
2094 'createdb-3'
2096 $html_output .= '<input type="hidden" name="dbname" value="'
2097 . htmlspecialchars($dbname) . '" />' . "\n";
2098 $html_output .= '<br />' . "\n";
2101 $html_output .= '</fieldset>' . "\n";
2102 if ($GLOBALS['is_grantuser']) {
2103 $html_output .= PMA_getHtmlToDisplayPrivilegesTable('*', '*', false);
2105 $html_output .= '<fieldset id="fieldset_add_user_footer" class="tblFooters">'
2106 . "\n"
2107 . '<input type="hidden" name="adduser_submit" value="1" />' . "\n"
2108 . '<input type="submit" id="adduser_submit" value="' . __('Go') . '" />'
2109 . "\n"
2110 . '</fieldset>' . "\n"
2111 . '</form>' . "\n";
2113 return $html_output;
2117 * Get the list of privileges and list of compared privileges as strings
2118 * and return a array that contains both strings
2120 * @return array $list_of_privileges, $list_of_compared_privileges
2122 function PMA_getListOfPrivilegesAndComparedPrivileges()
2124 $list_of_privileges
2125 = '`User`, '
2126 . '`Host`, '
2127 . '`Select_priv`, '
2128 . '`Insert_priv`, '
2129 . '`Update_priv`, '
2130 . '`Delete_priv`, '
2131 . '`Create_priv`, '
2132 . '`Drop_priv`, '
2133 . '`Grant_priv`, '
2134 . '`Index_priv`, '
2135 . '`Alter_priv`, '
2136 . '`References_priv`, '
2137 . '`Create_tmp_table_priv`, '
2138 . '`Lock_tables_priv`, '
2139 . '`Create_view_priv`, '
2140 . '`Show_view_priv`, '
2141 . '`Create_routine_priv`, '
2142 . '`Alter_routine_priv`, '
2143 . '`Execute_priv`';
2145 $listOfComparedPrivs
2146 = '`Select_priv` = \'N\''
2147 . ' AND `Insert_priv` = \'N\''
2148 . ' AND `Update_priv` = \'N\''
2149 . ' AND `Delete_priv` = \'N\''
2150 . ' AND `Create_priv` = \'N\''
2151 . ' AND `Drop_priv` = \'N\''
2152 . ' AND `Grant_priv` = \'N\''
2153 . ' AND `References_priv` = \'N\''
2154 . ' AND `Create_tmp_table_priv` = \'N\''
2155 . ' AND `Lock_tables_priv` = \'N\''
2156 . ' AND `Create_view_priv` = \'N\''
2157 . ' AND `Show_view_priv` = \'N\''
2158 . ' AND `Create_routine_priv` = \'N\''
2159 . ' AND `Alter_routine_priv` = \'N\''
2160 . ' AND `Execute_priv` = \'N\'';
2162 $list_of_privileges .=
2163 ', `Event_priv`, '
2164 . '`Trigger_priv`';
2165 $listOfComparedPrivs .=
2166 ' AND `Event_priv` = \'N\''
2167 . ' AND `Trigger_priv` = \'N\'';
2168 return array($list_of_privileges, $listOfComparedPrivs);
2172 * Get the HTML for user form and check the privileges for a particular database.
2174 * @param string $db database name
2176 * @return string $html_output
2178 function PMA_getHtmlForSpecificDbPrivileges($db)
2180 $html_output = '';
2181 if ($GLOBALS['is_superuser']) {
2182 // check the privileges for a particular database.
2183 $html_output = '<form id="usersForm" action="server_privileges.php">';
2184 $html_output .= PMA_URL_getHiddenInputs($db);
2185 $html_output .= '<fieldset>';
2186 $html_output .= '<legend>' . "\n"
2187 . PMA_Util::getIcon('b_usrcheck.png')
2188 . ' '
2189 . sprintf(
2190 __('Users having access to "%s"'),
2191 '<a href="' . PMA_Util::getScriptNameForOption(
2192 $GLOBALS['cfg']['DefaultTabDatabase'], 'database'
2194 . PMA_URL_getCommon(array('db' => $db)) . '">'
2195 . htmlspecialchars($db)
2196 . '</a>'
2198 . "\n"
2199 . '</legend>' . "\n";
2201 $html_output .= '<table id="dbspecificuserrights" class="data">';
2202 $html_output .= PMA_getHtmlForPrivsTableHead();
2203 $privMap = PMA_getPrivMap($db);
2204 $html_output .= PMA_getHtmlTableBodyForSpecificDbOrTablePrivs($privMap, $db);
2205 $html_output .= '</table>';
2207 $html_output .= '<div class="floatleft">';
2208 $html_output .= PMA_Util::getWithSelected(
2209 $GLOBALS['pmaThemeImage'], $GLOBALS['text_dir'], "usersForm"
2211 $html_output .= PMA_Util::getButtonOrImage(
2212 'submit_mult', 'mult_submit', 'submit_mult_export',
2213 __('Export'), 'b_tblexport.png', 'export'
2216 $html_output .= '</fieldset>';
2217 $html_output .= '</form>';
2218 } else {
2219 $html_output .= PMA_getHtmlForViewUsersError();
2222 if ($GLOBALS['is_ajax_request'] == true
2223 && empty($_REQUEST['ajax_page_request'])
2225 $message = PMA_Message::success(__('User has been added.'));
2226 $response = PMA_Response::getInstance();
2227 $response->addJSON('message', $message);
2228 $response->addJSON('user_form', $html_output);
2229 exit;
2230 } else {
2231 // Offer to create a new user for the current database
2232 $html_output .= PMA_getAddUserHtmlFieldset($db);
2234 return $html_output;
2238 * Get the HTML for user form and check the privileges for a particular table.
2240 * @param string $db database name
2241 * @param string $table table name
2243 * @return string $html_output
2245 function PMA_getHtmlForSpecificTablePrivileges($db, $table)
2247 $html_output = '';
2248 if ($GLOBALS['is_superuser']) {
2249 // check the privileges for a particular table.
2250 $html_output = '<form id="usersForm" action="server_privileges.php">';
2251 $html_output .= PMA_URL_getHiddenInputs($db, $table);
2252 $html_output .= '<fieldset>';
2253 $html_output .= '<legend>'
2254 . PMA_Util::getIcon('b_usrcheck.png')
2255 . sprintf(
2256 __('Users having access to "%s"'),
2257 '<a href="' . PMA_Util::getScriptNameForOption(
2258 $GLOBALS['cfg']['DefaultTabTable'], 'table'
2260 . PMA_URL_getCommon(
2261 array(
2262 'db' => $db,
2263 'table' => $table,
2265 ) . '">'
2266 . htmlspecialchars($db) . '.' . htmlspecialchars($table)
2267 . '</a>'
2269 . '</legend>';
2271 $html_output .= '<table id="tablespecificuserrights" class="data">';
2272 $html_output .= PMA_getHtmlForPrivsTableHead();
2273 $privMap = PMA_getPrivMap($db);
2274 $sql_query = "SELECT `User`, `Host`, `Db`,"
2275 . " 't' AS `Type`, `Table_name`, `Table_priv`"
2276 . " FROM `mysql`.`tables_priv`"
2277 . " WHERE '" . PMA_Util::sqlAddSlashes($db) . "' LIKE `Db`"
2278 . " AND '" . PMA_Util::sqlAddSlashes($table) . "' LIKE `Table_name`"
2279 . " AND NOT (`Table_priv` = '' AND Column_priv = '')"
2280 . " ORDER BY `User` ASC, `Host` ASC, `Db` ASC, `Table_priv` ASC;";
2281 $res = $GLOBALS['dbi']->query($sql_query);
2282 PMA_mergePrivMapFromResult($privMap, $res);
2283 $html_output .= PMA_getHtmlTableBodyForSpecificDbOrTablePrivs($privMap, $db);
2284 $html_output .= '</table>';
2286 $html_output .= '<div class="floatleft">';
2287 $html_output .= PMA_Util::getWithSelected(
2288 $GLOBALS['pmaThemeImage'], $GLOBALS['text_dir'], "usersForm"
2290 $html_output .= PMA_Util::getButtonOrImage(
2291 'submit_mult', 'mult_submit', 'submit_mult_export',
2292 __('Export'), 'b_tblexport.png', 'export'
2295 $html_output .= '</fieldset>';
2296 $html_output .= '</form>';
2297 } else {
2298 $html_output .= PMA_getHtmlForViewUsersError();
2300 // Offer to create a new user for the current database
2301 $html_output .= PMA_getAddUserHtmlFieldset($db, $table);
2302 return $html_output;
2306 * gets privilege map
2308 * @param string $db the database
2310 * @return array $privMap the privilege map
2312 function PMA_getPrivMap($db)
2314 list($listOfPrivs, $listOfComparedPrivs)
2315 = PMA_getListOfPrivilegesAndComparedPrivileges();
2316 $sql_query
2317 = "("
2318 . " SELECT " . $listOfPrivs . ", '*' AS `Db`, 'g' AS `Type`"
2319 . " FROM `mysql`.`user`"
2320 . " WHERE NOT (" . $listOfComparedPrivs . ")"
2321 . ")"
2322 . " UNION "
2323 . "("
2324 . " SELECT " . $listOfPrivs . ", `Db`, 'd' AS `Type`"
2325 . " FROM `mysql`.`db`"
2326 . " WHERE '" . PMA_Util::sqlAddSlashes($db) . "' LIKE `Db`"
2327 . " AND NOT (" . $listOfComparedPrivs . ")"
2328 . ")"
2329 . " ORDER BY `User` ASC, `Host` ASC, `Db` ASC;";
2330 $res = $GLOBALS['dbi']->query($sql_query);
2331 $privMap = array();
2332 PMA_mergePrivMapFromResult($privMap, $res);
2333 return $privMap;
2337 * merge privilege map and rows from resultset
2339 * @param array &$privMap the privilege map reference
2340 * @param object $result the resultset of query
2342 * @return void
2344 function PMA_mergePrivMapFromResult(&$privMap, $result)
2346 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
2347 $user = $row['User'];
2348 $host = $row['Host'];
2349 if (! isset($privMap[$user])) {
2350 $privMap[$user] = array();
2352 if (! isset($privMap[$user][$host])) {
2353 $privMap[$user][$host] = array();
2355 $privMap[$user][$host][] = $row;
2360 * Get HTML snippet for privileges table head
2362 * @return string $html_output
2364 function PMA_getHtmlForPrivsTableHead()
2366 return '<thead>'
2367 . '<tr>'
2368 . '<th></th>'
2369 . '<th>' . __('User name') . '</th>'
2370 . '<th>' . __('Host name') . '</th>'
2371 . '<th>' . __('Type') . '</th>'
2372 . '<th>' . __('Privileges') . '</th>'
2373 . '<th>' . __('Grant') . '</th>'
2374 . '<th>' . __('Action') . '</th>'
2375 . '</tr>'
2376 . '</thead>';
2380 * Get HTML error for View Users form
2381 * For non superusers such as grant/create users
2383 * @return string $html_output
2385 function PMA_getHtmlForViewUsersError()
2387 return PMA_Message::error(
2388 __('Not enough privilege to view users.')
2389 )->getDisplay();
2393 * Get HTML snippet for table body of specific database or table privileges
2395 * @param array $privMap privilege map
2396 * @param string $db database
2398 * @return string $html_output
2400 function PMA_getHtmlTableBodyForSpecificDbOrTablePrivs($privMap, $db)
2402 $html_output = '<tbody>';
2403 $index_checkbox = 0;
2404 $odd_row = true;
2405 if (empty($privMap)) {
2406 $html_output .= '<tr class="odd">'
2407 . '<td colspan="6">'
2408 . __('No user found.')
2409 . '</td>'
2410 . '</tr>'
2411 . '</tbody>';
2412 return $html_output;
2415 foreach ($privMap as $current_user => $val) {
2416 foreach ($val as $current_host => $current_privileges) {
2417 $nbPrivileges = count($current_privileges);
2418 $html_output .= '<tr class="' . ($odd_row ? 'odd' : 'even') . '">';
2420 $value = htmlspecialchars($current_user . '&amp;#27;' . $current_host);
2421 $html_output .= '<td';
2422 if ($nbPrivileges > 1) {
2423 $html_output .= ' rowspan="' . $nbPrivileges . '"';
2425 $html_output .= '>';
2426 $html_output .= '<input type="checkbox" class="checkall" '
2427 . 'name="selected_usr[]" '
2428 . 'id="checkbox_sel_users_' . ($index_checkbox++) . '" '
2429 . 'value="' . $value . '" /></td>' . "\n";
2431 // user
2432 $html_output .= '<td';
2433 if ($nbPrivileges > 1) {
2434 $html_output .= ' rowspan="' . $nbPrivileges . '"';
2436 $html_output .= '>';
2437 if (empty($current_user)) {
2438 $html_output .= '<span style="color: #FF0000">'
2439 . __('Any') . '</span>';
2440 } else {
2441 $html_output .= htmlspecialchars($current_user);
2443 $html_output .= '</td>';
2445 // host
2446 $html_output .= '<td';
2447 if ($nbPrivileges > 1) {
2448 $html_output .= ' rowspan="' . $nbPrivileges . '"';
2450 $html_output .= '>';
2451 $html_output .= htmlspecialchars($current_host);
2452 $html_output .= '</td>';
2454 $html_output .= PMA_getHtmlListOfPrivs(
2455 $db, $current_privileges, $current_user,
2456 $current_host, $odd_row
2459 $odd_row = ! $odd_row;
2462 $html_output .= '</tbody>';
2464 return $html_output;
2468 * Get HTML to display privileges
2470 * @param string $db Database name
2471 * @param array $current_privileges List of privileges
2472 * @param string $current_user Current user
2473 * @param string $current_host Current host
2474 * @param boolean $odd_row Current row is odd
2476 * @return string HTML to display privileges
2478 function PMA_getHtmlListOfPrivs(
2479 $db, $current_privileges, $current_user,
2480 $current_host, $odd_row
2482 $nbPrivileges = count($current_privileges);
2483 $html_output = null;
2484 for ($i = 0; $i < $nbPrivileges; $i++) {
2485 $current = $current_privileges[$i];
2487 // type
2488 $html_output .= '<td>';
2489 if ($current['Type'] == 'g') {
2490 $html_output .= __('global');
2491 } elseif ($current['Type'] == 'd') {
2492 if ($current['Db'] == PMA_Util::escapeMysqlWildcards($db)) {
2493 $html_output .= __('database-specific');
2494 } else {
2495 $html_output .= __('wildcard') . ': '
2496 . '<code>'
2497 . htmlspecialchars($current['Db'])
2498 . '</code>';
2500 } elseif ($current['Type'] == 't') {
2501 $html_output .= __('table-specific');
2503 $html_output .= '</td>';
2505 // privileges
2506 $html_output .= '<td>';
2507 if (isset($current['Table_name'])) {
2508 $privList = explode(',', $current['Table_priv']);
2509 $privs = array();
2510 $grantsArr = PMA_getTableGrantsArray();
2511 foreach ($grantsArr as $grant) {
2512 $privs[$grant[0]] = 'N';
2513 foreach ($privList as $priv) {
2514 if ($grant[0] == $priv) {
2515 $privs[$grant[0]] = 'Y';
2519 $html_output .= '<code>'
2520 . join(
2521 ',',
2522 PMA_extractPrivInfo($privs, true, true)
2524 . '</code>';
2525 } else {
2526 $html_output .= '<code>'
2527 . join(
2528 ',',
2529 PMA_extractPrivInfo($current, true, false)
2531 . '</code>';
2533 $html_output .= '</td>';
2535 // grant
2536 $html_output .= '<td>';
2537 $containsGrant = false;
2538 if (isset($current['Table_name'])) {
2539 $privList = explode(',', $current['Table_priv']);
2540 foreach ($privList as $priv) {
2541 if ($priv == 'Grant') {
2542 $containsGrant = true;
2545 } else {
2546 $containsGrant = $current['Grant_priv'] == 'Y';
2548 $html_output .= ($containsGrant ? __('Yes') : __('No'));
2549 $html_output .= '</td>';
2551 // action
2552 $html_output .= '<td>';
2553 if ($GLOBALS['is_grantuser']) {
2554 $specific_db = (isset($current['Db']) && $current['Db'] != '*')
2555 ? $current['Db'] : '';
2556 $specific_table = (isset($current['Table_name'])
2557 && $current['Table_name'] != '*')
2558 ? $current['Table_name'] : '';
2559 $html_output .= PMA_getUserLink(
2560 'edit',
2561 $current_user,
2562 $current_host,
2563 $specific_db,
2564 $specific_table
2567 $html_output .= '</td>';
2569 $html_output .= '</tr>';
2570 if (($i + 1) < $nbPrivileges) {
2571 $html_output .= '<tr class="noclick '
2572 . ($odd_row ? 'odd' : 'even') . '">';
2575 return $html_output;
2579 * Returns edit, revoke or export link for a user.
2581 * @param string $linktype The link type (edit | revoke | export)
2582 * @param string $username User name
2583 * @param string $hostname Host name
2584 * @param string $dbname Database name
2585 * @param string $tablename Table name
2586 * @param string $initial Initial value
2588 * @return string HTML code with link
2590 function PMA_getUserLink(
2591 $linktype, $username, $hostname, $dbname = '', $tablename = '', $initial = ''
2593 $html = '<a';
2594 switch($linktype) {
2595 case 'edit':
2596 $html .= ' class="edit_user_anchor"';
2597 break;
2598 case 'export':
2599 $html .= ' class="export_user_anchor ajax"';
2600 break;
2602 $params = array(
2603 'username' => $username,
2604 'hostname' => $hostname
2606 switch($linktype) {
2607 case 'edit':
2608 $params['dbname'] = $dbname;
2609 $params['tablename'] = $tablename;
2610 break;
2611 case 'revoke':
2612 $params['dbname'] = $dbname;
2613 $params['tablename'] = $tablename;
2614 $params['revokeall'] = 1;
2615 break;
2616 case 'export':
2617 $params['initial'] = $initial;
2618 $params['export'] = 1;
2619 break;
2622 $html .= ' href="server_privileges.php'
2623 . PMA_URL_getCommon($params)
2624 . '">';
2626 switch($linktype) {
2627 case 'edit':
2628 $html .= PMA_Util::getIcon('b_usredit.png', __('Edit privileges'));
2629 break;
2630 case 'revoke':
2631 $html .= PMA_Util::getIcon('b_usrdrop.png', __('Revoke'));
2632 break;
2633 case 'export':
2634 $html .= PMA_Util::getIcon('b_tblexport.png', __('Export'));
2635 break;
2637 $html .= '</a>';
2639 return $html;
2643 * Returns user group edit link
2645 * @param string $username User name
2647 * @return string HTML code with link
2649 function PMA_getUserGroupEditLink($username)
2651 return '<a class="edit_user_group_anchor ajax"'
2652 . ' href="server_privileges.php'
2653 . PMA_URL_getCommon(array('username' => $username))
2654 . '">'
2655 . PMA_Util::getIcon('b_usrlist.png', __('Edit user group'))
2656 . '</a>';
2660 * Returns number of defined user groups
2662 * @return integer $user_group_count
2664 function PMA_getUserGroupCount()
2666 $cfgRelation = PMA_getRelationsParam();
2667 $user_group_table = PMA_Util::backquote($cfgRelation['db'])
2668 . '.' . PMA_Util::backquote($cfgRelation['usergroups']);
2669 $sql_query = 'SELECT COUNT(*) FROM ' . $user_group_table;
2670 $user_group_count = $GLOBALS['dbi']->fetchValue(
2671 $sql_query, 0, 0, $GLOBALS['controllink']
2674 return $user_group_count;
2678 * This function return the extra data array for the ajax behavior
2680 * @param string $password password
2681 * @param string $sql_query sql query
2682 * @param string $hostname hostname
2683 * @param string $username username
2685 * @return array $extra_data
2687 function PMA_getExtraDataForAjaxBehavior(
2688 $password, $sql_query, $hostname, $username
2690 if (isset($GLOBALS['dbname'])) {
2691 //if (preg_match('/\\\\(?:_|%)/i', $dbname)) {
2692 if (preg_match('/(?<!\\\\)(?:_|%)/i', $GLOBALS['dbname'])) {
2693 $dbname_is_wildcard = true;
2694 } else {
2695 $dbname_is_wildcard = false;
2699 $user_group_count = 0;
2700 if ($GLOBALS['cfgRelation']['menuswork']) {
2701 $user_group_count = PMA_getUserGroupCount();
2704 $extra_data = array();
2705 if (/*overload*/mb_strlen($sql_query)) {
2706 $extra_data['sql_query'] = PMA_Util::getMessage(null, $sql_query);
2709 if (isset($_REQUEST['change_copy'])) {
2711 * generate html on the fly for the new user that was just created.
2713 $new_user_string = '<tr>' . "\n"
2714 . '<td> <input type="checkbox" name="selected_usr[]" '
2715 . 'id="checkbox_sel_users_"'
2716 . 'value="'
2717 . htmlspecialchars($username)
2718 . '&amp;#27;' . htmlspecialchars($hostname) . '" />'
2719 . '</td>' . "\n"
2720 . '<td><label for="checkbox_sel_users_">'
2721 . (empty($_REQUEST['username'])
2722 ? '<span style="color: #FF0000">' . __('Any') . '</span>'
2723 : htmlspecialchars($username) ) . '</label></td>' . "\n"
2724 . '<td>' . htmlspecialchars($hostname) . '</td>' . "\n";
2726 $new_user_string .= '<td>';
2728 if (! empty($password) || isset($_POST['pma_pw'])) {
2729 $new_user_string .= __('Yes');
2730 } else {
2731 $new_user_string .= '<span style="color: #FF0000">'
2732 . __('No')
2733 . '</span>';
2736 $new_user_string .= '</td>' . "\n";
2737 $new_user_string .= '<td>'
2738 . '<code>' . join(', ', PMA_extractPrivInfo(null, true)) . '</code>'
2739 . '</td>'; //Fill in privileges here
2741 // if $cfg['Servers'][$i]['users'] and $cfg['Servers'][$i]['usergroups'] are
2742 // enabled
2743 $cfgRelation = PMA_getRelationsParam();
2744 if (isset($cfgRelation['users']) && isset($cfgRelation['usergroups'])) {
2745 $new_user_string .= '<td class="usrGroup"></td>';
2748 $new_user_string .= '<td>';
2749 if ((isset($_POST['Grant_priv']) && $_POST['Grant_priv'] == 'Y')) {
2750 $new_user_string .= __('Yes');
2751 } else {
2752 $new_user_string .= __('No');
2754 $new_user_string .='</td>';
2756 if ($GLOBALS['is_grantuser']) {
2757 $new_user_string .= '<td>'
2758 . PMA_getUserLink('edit', $username, $hostname)
2759 . '</td>' . "\n";
2762 if ($cfgRelation['menuswork'] && $user_group_count > 0) {
2763 $new_user_string .= '<td>'
2764 . PMA_getUserGroupEditLink($username)
2765 . '</td>' . "\n";
2768 $new_user_string .= '<td>'
2769 . PMA_getUserLink(
2770 'export',
2771 $username,
2772 $hostname,
2775 isset($_GET['initial']) ? $_GET['initial'] : ''
2777 . '</td>' . "\n";
2779 $new_user_string .= '</tr>';
2781 $extra_data['new_user_string'] = $new_user_string;
2784 * Generate the string for this alphabet's initial, to update the user
2785 * pagination
2787 $new_user_initial = /*overload*/mb_strtoupper(
2788 /*overload*/mb_substr($username, 0, 1)
2790 $newUserInitialString = '<a href="server_privileges.php'
2791 . PMA_URL_getCommon(array('initial' => $new_user_initial)) . '">'
2792 . $new_user_initial . '</a>';
2793 $extra_data['new_user_initial'] = $new_user_initial;
2794 $extra_data['new_user_initial_string'] = $newUserInitialString;
2797 if (isset($_POST['update_privs'])) {
2798 $extra_data['db_specific_privs'] = false;
2799 $extra_data['db_wildcard_privs'] = false;
2800 if (isset($dbname_is_wildcard)) {
2801 $extra_data['db_specific_privs'] = ! $dbname_is_wildcard;
2802 $extra_data['db_wildcard_privs'] = $dbname_is_wildcard;
2804 $new_privileges = join(', ', PMA_extractPrivInfo(null, true));
2806 $extra_data['new_privileges'] = $new_privileges;
2809 if (isset($_REQUEST['validate_username'])) {
2810 $sql_query = "SELECT * FROM `mysql`.`user` WHERE `User` = '"
2811 . $_REQUEST['username'] . "';";
2812 $res = $GLOBALS['dbi']->query($sql_query);
2813 $row = $GLOBALS['dbi']->fetchRow($res);
2814 if (empty($row)) {
2815 $extra_data['user_exists'] = false;
2816 } else {
2817 $extra_data['user_exists'] = true;
2821 return $extra_data;
2825 * Get the HTML snippet for change user login information
2827 * @param string $username username
2828 * @param string $hostname host name
2830 * @return string HTML snippet
2832 function PMA_getChangeLoginInformationHtmlForm($username, $hostname)
2834 $choices = array(
2835 '4' => __('… keep the old one.'),
2836 '1' => __('… delete the old one from the user tables.'),
2837 '2' => __(
2838 '… revoke all active privileges from '
2839 . 'the old one and delete it afterwards.'
2841 '3' => __(
2842 '… delete the old one from the user tables '
2843 . 'and reload the privileges afterwards.'
2847 $html_output = '<form action="server_privileges.php" '
2848 . 'onsubmit="return checkAddUser(this);" '
2849 . 'method="post" class="copyUserForm submenu-item">' . "\n"
2850 . PMA_URL_getHiddenInputs('', '')
2851 . '<input type="hidden" name="old_username" '
2852 . 'value="' . htmlspecialchars($username) . '" />' . "\n"
2853 . '<input type="hidden" name="old_hostname" '
2854 . 'value="' . htmlspecialchars($hostname) . '" />' . "\n"
2855 . '<fieldset id="fieldset_change_copy_user">' . "\n"
2856 . '<legend data-submenu-label="' . __('Login Information') . '">' . "\n"
2857 . __('Change login information / Copy user account')
2858 . '</legend>' . "\n"
2859 . PMA_getHtmlForLoginInformationFields('change', $username, $hostname);
2861 $html_output .= '<fieldset id="fieldset_mode">' . "\n"
2862 . ' <legend>'
2863 . __('Create a new user account with the same privileges and …')
2864 . '</legend>' . "\n";
2865 $html_output .= PMA_Util::getRadioFields(
2866 'mode', $choices, '4', true
2868 $html_output .= '</fieldset>' . "\n"
2869 . '</fieldset>' . "\n";
2871 $html_output .= '<fieldset id="fieldset_change_copy_user_footer" '
2872 . 'class="tblFooters">' . "\n"
2873 . '<input type="hidden" name="change_copy" value="1" />' . "\n"
2874 . '<input type="submit" value="' . __('Go') . '" />' . "\n"
2875 . '</fieldset>' . "\n"
2876 . '</form>' . "\n";
2878 return $html_output;
2882 * Provide a line with links to the relevant database and table
2884 * @param string $url_dbname url database name that urlencode() string
2885 * @param string $dbname database name
2886 * @param string $tablename table name
2888 * @return string HTML snippet
2890 function PMA_getLinkToDbAndTable($url_dbname, $dbname, $tablename)
2892 $html_output = '[ ' . __('Database')
2893 . ' <a href="' . PMA_Util::getScriptNameForOption(
2894 $GLOBALS['cfg']['DefaultTabDatabase'], 'database'
2896 . PMA_URL_getCommon(
2897 array(
2898 'db' => $url_dbname,
2899 'reload' => 1
2902 . '">'
2903 . htmlspecialchars($dbname) . ': '
2904 . PMA_Util::getTitleForTarget(
2905 $GLOBALS['cfg']['DefaultTabDatabase']
2907 . "</a> ]\n";
2909 if (/*overload*/mb_strlen($tablename)) {
2910 $html_output .= ' [ ' . __('Table') . ' <a href="'
2911 . PMA_Util::getScriptNameForOption(
2912 $GLOBALS['cfg']['DefaultTabTable'], 'table'
2914 . PMA_URL_getCommon(
2915 array(
2916 'db' => $url_dbname,
2917 'table' => $tablename,
2918 'reload' => 1,
2921 . '">' . htmlspecialchars($tablename) . ': '
2922 . PMA_Util::getTitleForTarget(
2923 $GLOBALS['cfg']['DefaultTabTable']
2925 . "</a> ]\n";
2927 return $html_output;
2931 * no db name given, so we want all privs for the given user
2932 * db name was given, so we want all user specific rights for this db
2933 * So this function returns user rights as an array
2935 * @param array $tables tables
2936 * @param string $user_host_condition a where clause that contained user's host
2937 * condition
2938 * @param string $dbname database name
2940 * @return array $db_rights database rights
2942 function PMA_getUserSpecificRights($tables, $user_host_condition, $dbname)
2944 if (!/*overload*/mb_strlen($dbname)) {
2945 $tables_to_search_for_users = array(
2946 'tables_priv', 'columns_priv',
2948 $dbOrTableName = 'Db';
2949 } else {
2950 $user_host_condition .=
2951 ' AND `Db`'
2952 . ' LIKE \''
2953 . PMA_Util::sqlAddSlashes($dbname, true) . "'";
2954 $tables_to_search_for_users = array('columns_priv',);
2955 $dbOrTableName = 'Table_name';
2958 $db_rights_sqls = array();
2959 foreach ($tables_to_search_for_users as $table_search_in) {
2960 if (in_array($table_search_in, $tables)) {
2961 $db_rights_sqls[] = '
2962 SELECT DISTINCT `' . $dbOrTableName . '`
2963 FROM `mysql`.' . PMA_Util::backquote($table_search_in)
2964 . $user_host_condition;
2968 $user_defaults = array(
2969 $dbOrTableName => '',
2970 'Grant_priv' => 'N',
2971 'privs' => array('USAGE'),
2972 'Column_priv' => true,
2975 // for the rights
2976 $db_rights = array();
2978 $db_rights_sql = '(' . implode(') UNION (', $db_rights_sqls) . ')'
2979 . ' ORDER BY `' . $dbOrTableName . '` ASC';
2981 $db_rights_result = $GLOBALS['dbi']->query($db_rights_sql);
2983 while ($db_rights_row = $GLOBALS['dbi']->fetchAssoc($db_rights_result)) {
2984 $db_rights_row = array_merge($user_defaults, $db_rights_row);
2985 if (!/*overload*/mb_strlen($dbname)) {
2986 // only Db names in the table `mysql`.`db` uses wildcards
2987 // as we are in the db specific rights display we want
2988 // all db names escaped, also from other sources
2989 $db_rights_row['Db'] = PMA_Util::escapeMysqlWildcards(
2990 $db_rights_row['Db']
2993 $db_rights[$db_rights_row[$dbOrTableName]] = $db_rights_row;
2996 $GLOBALS['dbi']->freeResult($db_rights_result);
2998 if (!/*overload*/mb_strlen($dbname)) {
2999 $sql_query = 'SELECT * FROM `mysql`.`db`'
3000 . $user_host_condition . ' ORDER BY `Db` ASC';
3001 } else {
3002 $sql_query = 'SELECT `Table_name`,'
3003 . ' `Table_priv`,'
3004 . ' IF(`Column_priv` = _latin1 \'\', 0, 1)'
3005 . ' AS \'Column_priv\''
3006 . ' FROM `mysql`.`tables_priv`'
3007 . $user_host_condition
3008 . ' ORDER BY `Table_name` ASC;';
3011 $result = $GLOBALS['dbi']->query($sql_query);
3013 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
3014 if (isset($db_rights[$row[$dbOrTableName]])) {
3015 $db_rights[$row[$dbOrTableName]]
3016 = array_merge($db_rights[$row[$dbOrTableName]], $row);
3017 } else {
3018 $db_rights[$row[$dbOrTableName]] = $row;
3020 if (!/*overload*/mb_strlen($dbname)) {
3021 // there are db specific rights for this user
3022 // so we can drop this db rights
3023 $db_rights[$row['Db']]['can_delete'] = true;
3026 $GLOBALS['dbi']->freeResult($result);
3027 return $db_rights;
3031 * Display user rights in table rows(Table specific or database specific privs)
3033 * @param array $db_rights user's database rights array
3034 * @param string $dbname database name
3035 * @param string $hostname host name
3036 * @param string $username username
3038 * @return array $found_rows, $html_output
3040 function PMA_getHtmlForUserRights($db_rights, $dbname,
3041 $hostname, $username
3043 $html_output = '';
3044 $found_rows = array();
3046 // display rows
3047 if (count($db_rights) < 1) {
3048 $html_output .= '<tr class="odd">' . "\n"
3049 . '<td colspan="6"><center><i>' . __('None') . '</i></center></td>' . "\n"
3050 . '</tr>' . "\n";
3051 return array($found_rows, $html_output);
3054 $odd_row = true;
3055 //while ($row = $GLOBALS['dbi']->fetchAssoc($res)) {
3056 foreach ($db_rights as $row) {
3057 $dbNameLength = /*overload*/mb_strlen($dbname);
3058 $found_rows[] = (!$dbNameLength)
3059 ? $row['Db']
3060 : $row['Table_name'];
3062 $html_output .= '<tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n"
3063 . '<td>'
3064 . htmlspecialchars(
3065 (!$dbNameLength)
3066 ? $row['Db']
3067 : $row['Table_name']
3069 . '</td>' . "\n"
3070 . '<td><code>' . "\n"
3071 . ' '
3072 . join(
3073 ',' . "\n" . ' ',
3074 PMA_extractPrivInfo($row, true)
3075 ) . "\n"
3076 . '</code></td>' . "\n"
3077 . '<td>'
3078 . ((((!$dbNameLength) && $row['Grant_priv'] == 'Y')
3079 || ($dbNameLength
3080 && in_array('Grant', explode(',', $row['Table_priv']))))
3081 ? __('Yes')
3082 : __('No'))
3083 . '</td>' . "\n"
3084 . '<td>';
3085 if (!empty($row['Table_privs']) || !empty($row['Column_priv'])) {
3086 $html_output .= __('Yes');
3087 } else {
3088 $html_output .= __('No');
3090 $html_output .= '</td>';
3092 $html_output .= '<td>';
3093 if ($GLOBALS['is_grantuser']) {
3094 $html_output .= PMA_getUserLink(
3095 'edit',
3096 $username,
3097 $hostname,
3098 (!$dbNameLength) ? $row['Db'] : $dbname,
3099 (!$dbNameLength) ? '' : $row['Table_name']
3102 $html_output .= '</td>';
3104 $html_output .= '<td>';
3105 if (! empty($row['can_delete'])
3106 || isset($row['Table_name'])
3107 && /*overload*/mb_strlen($row['Table_name'])
3109 $html_output .= PMA_getUserLink(
3110 'revoke',
3111 $username,
3112 $hostname,
3113 (!$dbNameLength) ? $row['Db'] : $dbname,
3114 (!$dbNameLength) ? '' : $row['Table_name']
3117 $html_output .= '</td>' . "\n"
3118 . '</tr>' . "\n";
3119 $odd_row = ! $odd_row;
3120 } // end while
3122 return array($found_rows, $html_output);
3126 * Get a HTML table for display user's tabel specific or database specific rights
3128 * @param string $username username
3129 * @param string $hostname host name
3130 * @param string $dbname database name
3132 * @return array $html_output, $found_rows
3134 function PMA_getHtmlForAllTableSpecificRights(
3135 $username, $hostname, $dbname
3137 // table header
3138 $html_output = PMA_URL_getHiddenInputs('', '')
3139 . '<input type="hidden" name="username" '
3140 . 'value="' . htmlspecialchars($username) . '" />' . "\n"
3141 . '<input type="hidden" name="hostname" '
3142 . 'value="' . htmlspecialchars($hostname) . '" />' . "\n"
3143 . '<fieldset>' . "\n"
3144 . '<legend data-submenu-label="'
3145 . (!/*overload*/mb_strlen($dbname)
3146 ? __('Database')
3147 : __('Table')
3149 . '">'
3150 . (!/*overload*/mb_strlen($dbname)
3151 ? __('Database-specific privileges')
3152 : __('Table-specific privileges')
3154 . '</legend>' . "\n"
3155 . '<table class="data">' . "\n"
3156 . '<thead>' . "\n"
3157 . '<tr><th>'
3158 . (!/*overload*/mb_strlen($dbname) ? __('Database') : __('Table'))
3159 . '</th>' . "\n"
3160 . '<th>' . __('Privileges') . '</th>' . "\n"
3161 . '<th>' . __('Grant') . '</th>' . "\n"
3162 . '<th>'
3163 . (!/*overload*/mb_strlen($dbname)
3164 ? __('Table-specific privileges')
3165 : __('Column-specific privileges')
3167 . '</th>' . "\n"
3168 . '<th colspan="2">' . __('Action') . '</th>' . "\n"
3169 . '</tr>' . "\n"
3170 . '</thead>' . "\n";
3172 $user_host_condition = ' WHERE `User`'
3173 . ' = \'' . PMA_Util::sqlAddSlashes($username) . "'"
3174 . ' AND `Host`'
3175 . ' = \'' . PMA_Util::sqlAddSlashes($hostname) . "'";
3177 // table body
3178 // get data
3180 // we also want privileges for this user not in table `db` but in other table
3181 $tables = $GLOBALS['dbi']->fetchResult('SHOW TABLES FROM `mysql`;');
3184 * no db name given, so we want all privs for the given user
3185 * db name was given, so we want all user specific rights for this db
3187 $db_rights = PMA_getUserSpecificRights($tables, $user_host_condition, $dbname);
3189 ksort($db_rights);
3191 $html_output .= '<tbody>' . "\n";
3192 // display rows
3193 list ($found_rows, $html_out) = PMA_getHtmlForUserRights(
3194 $db_rights, $dbname, $hostname, $username
3197 $html_output .= $html_out;
3198 $html_output .= '</tbody>' . "\n";
3199 $html_output .='</table>' . "\n";
3201 return array($html_output, $found_rows);
3205 * Get HTML for display select db
3207 * @param array $found_rows isset($dbname)) ? $row['Db'] : $row['Table_name']
3209 * @return string HTML snippet
3211 function PMA_getHtmlForSelectDbInEditPrivs($found_rows)
3213 // we already have the list of databases from libraries/common.inc.php
3214 // via $pma = new PMA;
3215 $pred_db_array = $GLOBALS['pma']->databases;
3217 $databases_to_skip = array('information_schema', 'performance_schema');
3219 $html_output = '<label for="text_dbname">'
3220 . __('Add privileges on the following database(s):') . '</label>' . "\n";
3221 if (! empty($pred_db_array)) {
3222 $html_output .= '<select name="pred_dbname[]" multiple="multiple">' . "\n";
3223 foreach ($pred_db_array as $current_db) {
3224 if (in_array($current_db, $databases_to_skip)) {
3225 continue;
3227 $current_db_show = $current_db;
3228 $current_db = PMA_Util::escapeMysqlWildcards($current_db);
3229 // cannot use array_diff() once, outside of the loop,
3230 // because the list of databases has special characters
3231 // already escaped in $found_rows,
3232 // contrary to the output of SHOW DATABASES
3233 if (empty($found_rows) || ! in_array($current_db, $found_rows)) {
3234 $html_output .= '<option value="'
3235 . htmlspecialchars($current_db) . '">'
3236 . htmlspecialchars($current_db_show) . '</option>' . "\n";
3239 $html_output .= '</select>' . "\n";
3241 $html_output .= '<input type="text" id="text_dbname" name="dbname" />'
3242 . "\n"
3243 . PMA_Util::showHint(
3244 __('Wildcards % and _ should be escaped with a \ to use them literally.')
3246 return $html_output;
3250 * Get HTML for display table in edit privilege
3252 * @param string $dbname database naame
3253 * @param array $found_rows isset($dbname)) ? $row['Db'] : $row['Table_name']
3255 * @return string HTML snippet
3257 function PMA_displayTablesInEditPrivs($dbname, $found_rows)
3259 $html_output = '<input type="hidden" name="dbname"
3260 ' . 'value="' . htmlspecialchars($dbname) . '"/>' . "\n";
3261 $html_output .= '<label for="text_tablename">'
3262 . __('Add privileges on the following table:') . '</label>' . "\n";
3264 $result = @$GLOBALS['dbi']->tryQuery(
3265 'SHOW TABLES FROM ' . PMA_Util::backquote(
3266 PMA_Util::unescapeMysqlWildcards($dbname)
3267 ) . ';',
3268 null,
3269 PMA_DatabaseInterface::QUERY_STORE
3272 if ($result) {
3273 $pred_tbl_array = array();
3274 while ($row = $GLOBALS['dbi']->fetchRow($result)) {
3275 if (! isset($found_rows) || ! in_array($row[0], $found_rows)) {
3276 $pred_tbl_array[] = $row[0];
3279 $GLOBALS['dbi']->freeResult($result);
3281 if (! empty($pred_tbl_array)) {
3282 $html_output .= '<select name="pred_tablename" '
3283 . 'class="autosubmit">' . "\n"
3284 . '<option value="" selected="selected">' . __('Use text field')
3285 . ':</option>' . "\n";
3286 foreach ($pred_tbl_array as $current_table) {
3287 $html_output .= '<option '
3288 . 'value="' . htmlspecialchars($current_table) . '">'
3289 . htmlspecialchars($current_table)
3290 . '</option>' . "\n";
3292 $html_output .= '</select>' . "\n";
3295 $html_output .= '<input type="text" id="text_tablename" name="tablename" />'
3296 . "\n";
3298 return $html_output;
3302 * Get HTML for display the users overview
3303 * (if less than 50 users, display them immediately)
3305 * @param array $result ran sql query
3306 * @param array $db_rights user's database rights array
3307 * @param string $pmaThemeImage a image source link
3308 * @param string $text_dir text directory
3310 * @return string HTML snippet
3312 function PMA_getUsersOverview($result, $db_rights, $pmaThemeImage, $text_dir)
3314 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
3315 $row['privs'] = PMA_extractPrivInfo($row, true);
3316 $db_rights[$row['User']][$row['Host']] = $row;
3318 @$GLOBALS['dbi']->freeResult($result);
3319 $user_group_count = 0;
3320 if ($GLOBALS['cfgRelation']['menuswork']) {
3321 $user_group_count = PMA_getUserGroupCount();
3324 $html_output
3325 = '<form name="usersForm" id="usersForm" action="server_privileges.php" '
3326 . 'method="post">' . "\n"
3327 . PMA_URL_getHiddenInputs('', '')
3328 . '<table id="tableuserrights" class="data">' . "\n"
3329 . '<thead>' . "\n"
3330 . '<tr><th></th>' . "\n"
3331 . '<th>' . __('User name') . '</th>' . "\n"
3332 . '<th>' . __('Host name') . '</th>' . "\n"
3333 . '<th>' . __('Password') . '</th>' . "\n"
3334 . '<th>' . __('Global privileges') . ' '
3335 . PMA_Util::showHint(
3336 __('Note: MySQL privilege names are expressed in English.')
3338 . '</th>' . "\n";
3339 if ($GLOBALS['cfgRelation']['menuswork']) {
3340 $html_output .= '<th>' . __('User group') . '</th>' . "\n";
3342 $html_output .= '<th>' . __('Grant') . '</th>' . "\n"
3343 . '<th colspan="' . ($user_group_count > 0 ? '3' : '2') . '">'
3344 . __('Action') . '</th>' . "\n"
3345 . '</tr>' . "\n"
3346 . '</thead>' . "\n";
3348 $html_output .= '<tbody>' . "\n";
3349 $html_output .= PMA_getHtmlTableBodyForUserRights($db_rights);
3350 $html_output .= '</tbody>'
3351 . '</table>' . "\n";
3353 $html_output .= '<div class="floatleft">'
3354 . PMA_Util::getWithSelected($pmaThemeImage, $text_dir, "usersForm") . "\n";
3356 $html_output .= PMA_Util::getButtonOrImage(
3357 'submit_mult', 'mult_submit', 'submit_mult_export',
3358 __('Export'), 'b_tblexport.png', 'export'
3360 $html_output .= '<input type="hidden" name="initial" '
3361 . 'value="' . (isset($_GET['initial']) ? $_GET['initial'] : '') . '" />';
3362 $html_output .= '</div>'
3363 . '<div class="clear_both" style="clear:both"></div>';
3365 // add/delete user fieldset
3366 $html_output .= PMA_getFieldsetForAddDeleteUser();
3367 $html_output .= '</form>' . "\n";
3369 return $html_output;
3373 * Get table body for 'tableuserrights' table in userform
3375 * @param array $db_rights user's database rights array
3377 * @return string HTML snippet
3379 function PMA_getHtmlTableBodyForUserRights($db_rights)
3381 $cfgRelation = PMA_getRelationsParam();
3382 if ($cfgRelation['menuswork']) {
3383 $users_table = PMA_Util::backquote($cfgRelation['db'])
3384 . "." . PMA_Util::backquote($cfgRelation['users']);
3385 $sql_query = 'SELECT * FROM ' . $users_table;
3386 $result = PMA_queryAsControlUser($sql_query, false);
3387 $group_assignment = array();
3388 if ($result) {
3389 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
3390 $group_assignment[$row['username']] = $row['usergroup'];
3393 $GLOBALS['dbi']->freeResult($result);
3395 $user_group_count = PMA_getUserGroupCount();
3398 $odd_row = true;
3399 $index_checkbox = 0;
3400 $html_output = '';
3401 foreach ($db_rights as $user) {
3402 ksort($user);
3403 foreach ($user as $host) {
3404 $index_checkbox++;
3405 $html_output .= '<tr class="' . ($odd_row ? 'odd' : 'even') . '">'
3406 . "\n";
3407 $html_output .= '<td>'
3408 . '<input type="checkbox" class="checkall" name="selected_usr[]" '
3409 . 'id="checkbox_sel_users_'
3410 . $index_checkbox . '" value="'
3411 . htmlspecialchars($host['User'] . '&amp;#27;' . $host['Host'])
3412 . '"'
3413 . ' /></td>' . "\n";
3415 $html_output .= '<td><label '
3416 . 'for="checkbox_sel_users_' . $index_checkbox . '">'
3417 . (empty($host['User'])
3418 ? '<span style="color: #FF0000">' . __('Any') . '</span>'
3419 : htmlspecialchars($host['User'])) . '</label></td>' . "\n"
3420 . '<td>' . htmlspecialchars($host['Host']) . '</td>' . "\n";
3422 $html_output .= '<td>';
3424 $password_column = 'Password';
3426 if (PMA_Util::getServerType() == 'MySQL'
3427 && PMA_MYSQL_INT_VERSION >= 50606
3428 && PMA_MYSQL_INT_VERSION < 50706
3430 $check_plugin_query = "SELECT * FROM `mysql`.`user` WHERE "
3431 . "`User` = '" . $host['User'] . "' AND `Host` = '"
3432 . $host['Host'] . "'";
3433 $res = $GLOBALS['dbi']->fetchSingleRow($check_plugin_query);
3434 if (isset($res['plugin'])
3435 && $res['plugin'] == 'sha256_password'
3436 && isset($res['authentication_string'])
3438 $password_column = 'authentication_string';
3439 if (! empty($res['authentication_string'])) {
3440 $host[$password_column] = 'Y';
3441 } else {
3442 $host[$password_column] = 'N';
3447 switch ($host[$password_column]) {
3448 case 'Y':
3449 $html_output .= __('Yes');
3450 break;
3451 case 'N':
3452 $html_output .= '<span style="color: #FF0000">' . __('No')
3453 . '</span>';
3454 break;
3455 // this happens if this is a definition not coming from mysql.user
3456 default:
3457 $html_output .= '--'; // in future version, replace by "not present"
3458 break;
3459 } // end switch
3461 $html_output .= '</td>' . "\n";
3463 $html_output .= '<td><code>' . "\n"
3464 . '' . implode(',' . "\n" . ' ', $host['privs']) . "\n"
3465 . '</code></td>' . "\n";
3466 if ($cfgRelation['menuswork']) {
3467 $html_output .= '<td class="usrGroup">' . "\n"
3468 . (isset($group_assignment[$host['User']])
3469 ? $group_assignment[$host['User']]
3470 : ''
3472 . '</td>' . "\n";
3474 $html_output .= '<td>'
3475 . ($host['Grant_priv'] == 'Y' ? __('Yes') : __('No'))
3476 . '</td>' . "\n";
3478 if ($GLOBALS['is_grantuser']) {
3479 $html_output .= '<td class="center">'
3480 . PMA_getUserLink(
3481 'edit',
3482 $host['User'],
3483 $host['Host']
3485 . '</td>';
3487 if ($cfgRelation['menuswork'] && $user_group_count > 0) {
3488 if (empty($host['User'])) {
3489 $html_output .= '<td class="center"></td>';
3490 } else {
3491 $html_output .= '<td class="center">'
3492 . PMA_getUserGroupEditLink($host['User'])
3493 . '</td>';
3496 $html_output .= '<td class="center">'
3497 . PMA_getUserLink(
3498 'export',
3499 $host['User'],
3500 $host['Host'],
3503 isset($_GET['initial']) ? $_GET['initial'] : ''
3505 . '</td>';
3506 $html_output .= '</tr>';
3507 $odd_row = ! $odd_row;
3510 return $html_output;
3514 * Get HTML fieldset for Add/Delete user
3516 * @return string HTML snippet
3518 function PMA_getFieldsetForAddDeleteUser()
3520 $html_output = PMA_getAddUserHtmlFieldset();
3521 $html_output .= '<fieldset id="fieldset_delete_user">'
3522 . '<legend>' . "\n"
3523 . PMA_Util::getIcon('b_usrdrop.png')
3524 . ' ' . __('Remove selected user accounts') . '' . "\n"
3525 . '</legend>' . "\n";
3527 $html_output .= '<input type="hidden" name="mode" value="2" />' . "\n"
3528 . '('
3529 . __(
3530 'Revoke all active privileges from the users '
3531 . 'and delete them afterwards.'
3533 . ')'
3534 . '<br />' . "\n";
3536 $html_output .= '<input type="checkbox" '
3537 . 'title="'
3538 . __('Drop the databases that have the same names as the users.')
3539 . '" '
3540 . 'name="drop_users_db" id="checkbox_drop_users_db" />' . "\n";
3542 $html_output .= '<label for="checkbox_drop_users_db" '
3543 . 'title="'
3544 . __('Drop the databases that have the same names as the users.')
3545 . '">' . "\n"
3546 . ' '
3547 . __('Drop the databases that have the same names as the users.')
3548 . "\n"
3549 . '</label>' . "\n"
3550 . '</fieldset>' . "\n";
3552 $html_output .= '<fieldset id="fieldset_delete_user_footer" class="tblFooters">'
3553 . "\n";
3554 $html_output .= '<input type="submit" name="delete" '
3555 . 'value="' . __('Go') . '" id="buttonGo" '
3556 . 'class="ajax"/>' . "\n";
3558 $html_output .= '</fieldset>' . "\n";
3560 return $html_output;
3564 * Get HTML for Displays the initials
3566 * @param array $array_initials array for all initials, even non A-Z
3568 * @return string HTML snippet
3570 function PMA_getHtmlForInitials($array_initials)
3572 // initialize to false the letters A-Z
3573 for ($letter_counter = 1; $letter_counter < 27; $letter_counter++) {
3574 if (! isset($array_initials[/*overload*/mb_chr($letter_counter + 64)])) {
3575 $array_initials[/*overload*/mb_chr($letter_counter + 64)] = false;
3579 $initials = $GLOBALS['dbi']->tryQuery(
3580 'SELECT DISTINCT UPPER(LEFT(`User`,1)) FROM `user` ORDER BY `User` ASC',
3581 null,
3582 PMA_DatabaseInterface::QUERY_STORE
3584 while (list($tmp_initial) = $GLOBALS['dbi']->fetchRow($initials)) {
3585 $array_initials[$tmp_initial] = true;
3588 // Display the initials, which can be any characters, not
3589 // just letters. For letters A-Z, we add the non-used letters
3590 // as greyed out.
3592 uksort($array_initials, "strnatcasecmp");
3594 $html_output = '<table id="initials_table" cellspacing="5">'
3595 . '<tr>';
3596 foreach ($array_initials as $tmp_initial => $initial_was_found) {
3597 if ($tmp_initial === null) {
3598 continue;
3601 if (!$initial_was_found) {
3602 $html_output .= '<td>' . $tmp_initial . '</td>';
3603 continue;
3606 $html_output .= '<td>'
3607 . '<a class="ajax'
3608 . ((isset($_REQUEST['initial'])
3609 && $_REQUEST['initial'] === $tmp_initial
3610 ) ? ' active' : '')
3611 . '" href="server_privileges.php'
3612 . PMA_URL_getCommon(array('initial' => $tmp_initial))
3613 . '">' . $tmp_initial
3614 . '</a>'
3615 . '</td>' . "\n";
3617 $html_output .= '<td>'
3618 . '<a href="server_privileges.php'
3619 . PMA_URL_getCommon(array('showall' => 1))
3620 . '" class="nowrap">' . __('Show all') . '</a></td>' . "\n";
3621 $html_output .= '</tr></table>';
3623 return $html_output;
3627 * Get the database rights array for Display user overview
3629 * @return array $db_rights database rights array
3631 function PMA_getDbRightsForUserOverview()
3633 // we also want users not in table `user` but in other table
3634 $tables = $GLOBALS['dbi']->fetchResult('SHOW TABLES FROM `mysql`;');
3636 $tablesSearchForUsers = array(
3637 'user', 'db', 'tables_priv', 'columns_priv', 'procs_priv',
3640 $db_rights_sqls = array();
3641 foreach ($tablesSearchForUsers as $table_search_in) {
3642 if (in_array($table_search_in, $tables)) {
3643 $db_rights_sqls[] = 'SELECT DISTINCT `User`, `Host` FROM `mysql`.`'
3644 . $table_search_in . '` '
3645 . (isset($_GET['initial'])
3646 ? PMA_rangeOfUsers($_GET['initial'])
3647 : '');
3650 $user_defaults = array(
3651 'User' => '',
3652 'Host' => '%',
3653 'Password' => '?',
3654 'Grant_priv' => 'N',
3655 'privs' => array('USAGE'),
3658 // for the rights
3659 $db_rights = array();
3661 $db_rights_sql = '(' . implode(') UNION (', $db_rights_sqls) . ')'
3662 . ' ORDER BY `User` ASC, `Host` ASC';
3664 $db_rights_result = $GLOBALS['dbi']->query($db_rights_sql);
3666 while ($db_rights_row = $GLOBALS['dbi']->fetchAssoc($db_rights_result)) {
3667 $db_rights_row = array_merge($user_defaults, $db_rights_row);
3668 $db_rights[$db_rights_row['User']][$db_rights_row['Host']]
3669 = $db_rights_row;
3671 $GLOBALS['dbi']->freeResult($db_rights_result);
3672 ksort($db_rights);
3674 return $db_rights;
3678 * Delete user and get message and sql query for delete user in privileges
3680 * @param array $queries queries
3682 * @return array PMA_message
3684 function PMA_deleteUser($queries)
3686 $sql_query = '';
3687 if (empty($queries)) {
3688 $message = PMA_Message::error(__('No users selected for deleting!'));
3689 } else {
3690 if ($_REQUEST['mode'] == 3) {
3691 $queries[] = '# ' . __('Reloading the privileges') . ' …';
3692 $queries[] = 'FLUSH PRIVILEGES;';
3694 $drop_user_error = '';
3695 foreach ($queries as $sql_query) {
3696 if ($sql_query{0} != '#') {
3697 if (! $GLOBALS['dbi']->tryQuery($sql_query, $GLOBALS['userlink'])) {
3698 $drop_user_error .= $GLOBALS['dbi']->getError() . "\n";
3702 // tracking sets this, causing the deleted db to be shown in navi
3703 unset($GLOBALS['db']);
3705 $sql_query = join("\n", $queries);
3706 if (! empty($drop_user_error)) {
3707 $message = PMA_Message::rawError($drop_user_error);
3708 } else {
3709 $message = PMA_Message::success(
3710 __('The selected users have been deleted successfully.')
3714 return array($sql_query, $message);
3718 * Update the privileges and return the success or error message
3720 * @param string $username username
3721 * @param string $hostname host name
3722 * @param string $tablename table name
3723 * @param string $dbname database name
3725 * @return PMA_message success message or error message for update
3727 function PMA_updatePrivileges($username, $hostname, $tablename, $dbname)
3729 $db_and_table = PMA_wildcardEscapeForGrant($dbname, $tablename);
3731 $sql_query0 = 'REVOKE ALL PRIVILEGES ON ' . $db_and_table
3732 . ' FROM \'' . PMA_Util::sqlAddSlashes($username)
3733 . '\'@\'' . PMA_Util::sqlAddSlashes($hostname) . '\';';
3735 if (! isset($_POST['Grant_priv']) || $_POST['Grant_priv'] != 'Y') {
3736 $sql_query1 = 'REVOKE GRANT OPTION ON ' . $db_and_table
3737 . ' FROM \'' . PMA_Util::sqlAddSlashes($username) . '\'@\''
3738 . PMA_Util::sqlAddSlashes($hostname) . '\';';
3739 } else {
3740 $sql_query1 = '';
3743 // Should not do a GRANT USAGE for a table-specific privilege, it
3744 // causes problems later (cannot revoke it)
3745 if (! (/*overload*/mb_strlen($tablename)
3746 && 'USAGE' == implode('', PMA_extractPrivInfo()))
3748 $sql_query2 = 'GRANT ' . join(', ', PMA_extractPrivInfo())
3749 . ' ON ' . $db_and_table
3750 . ' TO \'' . PMA_Util::sqlAddSlashes($username) . '\'@\''
3751 . PMA_Util::sqlAddSlashes($hostname) . '\'';
3753 if (! /*overload*/mb_strlen($dbname)) {
3754 // add REQUIRE clause
3755 $sql_query2 .= PMA_getRequireClause();
3758 if ((isset($_POST['Grant_priv']) && $_POST['Grant_priv'] == 'Y')
3759 || (! /*overload*/mb_strlen($dbname)
3760 && (isset($_POST['max_questions']) || isset($_POST['max_connections'])
3761 || isset($_POST['max_updates'])
3762 || isset($_POST['max_user_connections'])))
3764 $sql_query2 .= PMA_getWithClauseForAddUserAndUpdatePrivs();
3766 $sql_query2 .= ';';
3768 if (! $GLOBALS['dbi']->tryQuery($sql_query0)) {
3769 // This might fail when the executing user does not have
3770 // ALL PRIVILEGES himself.
3771 // See https://sourceforge.net/p/phpmyadmin/bugs/3270/
3772 $sql_query0 = '';
3774 if (! empty($sql_query1) && ! $GLOBALS['dbi']->tryQuery($sql_query1)) {
3775 // this one may fail, too...
3776 $sql_query1 = '';
3778 if (! empty($sql_query2)) {
3779 $GLOBALS['dbi']->query($sql_query2);
3780 } else {
3781 $sql_query2 = '';
3783 $sql_query = $sql_query0 . ' ' . $sql_query1 . ' ' . $sql_query2;
3784 $message = PMA_Message::success(__('You have updated the privileges for %s.'));
3785 $message->addParam(
3786 '\'' . htmlspecialchars($username)
3787 . '\'@\'' . htmlspecialchars($hostname) . '\''
3790 return array($sql_query, $message);
3794 * Get List of information: Changes / copies a user
3796 * @return array
3798 function PMA_getDataForChangeOrCopyUser()
3800 $queries = null;
3801 $password = null;
3803 if (isset($_REQUEST['change_copy'])) {
3804 $user_host_condition = ' WHERE `User` = '
3805 . "'" . PMA_Util::sqlAddSlashes($_REQUEST['old_username']) . "'"
3806 . ' AND `Host` = '
3807 . "'" . PMA_Util::sqlAddSlashes($_REQUEST['old_hostname']) . "';";
3808 $row = $GLOBALS['dbi']->fetchSingleRow(
3809 'SELECT * FROM `mysql`.`user` ' . $user_host_condition
3811 if (! $row) {
3812 $response = PMA_Response::getInstance();
3813 $response->addHTML(
3814 PMA_Message::notice(__('No user found.'))->getDisplay()
3816 unset($_REQUEST['change_copy']);
3817 } else {
3818 extract($row, EXTR_OVERWRITE);
3819 // Recent MySQL versions have the field "Password" in mysql.user,
3820 // so the previous extract creates $Password but this script
3821 // uses $password
3822 if (! isset($password) && isset($Password)) {
3823 $password = $Password;
3825 if (PMA_Util::getServerType() == 'MySQL'
3826 && PMA_MYSQL_INT_VERSION >= 50606
3827 && PMA_MYSQL_INT_VERSION < 50706
3828 && isset($password)
3829 && empty($password)
3830 && isset($plugin)
3831 && $plugin == 'sha256_password'
3833 $password = $authentication_string;
3836 // Always use 'authentication_string' column
3837 // for MySQL 5.7.6+ since it does not have
3838 // the 'password' column at all
3839 if (PMA_Util::getServerType() == 'MySQL'
3840 && PMA_MYSQL_INT_VERSION >= 50706
3841 && isset($authentication_string)
3843 $password = $authentication_string;
3846 $queries = array();
3850 return array($queries, $password);
3854 * Update Data for information: Deletes users
3856 * @param array $queries queries array
3858 * @return array
3860 function PMA_getDataForDeleteUsers($queries)
3862 if (isset($_REQUEST['change_copy'])) {
3863 $selected_usr = array(
3864 $_REQUEST['old_username'] . '&amp;#27;' . $_REQUEST['old_hostname']
3866 } else {
3867 $selected_usr = $_REQUEST['selected_usr'];
3868 $queries = array();
3870 foreach ($selected_usr as $each_user) {
3871 list($this_user, $this_host) = explode('&amp;#27;', $each_user);
3872 $queries[] = '# '
3873 . sprintf(
3874 __('Deleting %s'),
3875 '\'' . $this_user . '\'@\'' . $this_host . '\''
3877 . ' ...';
3878 $queries[] = 'DROP USER \''
3879 . PMA_Util::sqlAddSlashes($this_user)
3880 . '\'@\'' . PMA_Util::sqlAddSlashes($this_host) . '\';';
3881 PMA_relationsCleanupUser($this_user);
3883 if (isset($_REQUEST['drop_users_db'])) {
3884 $queries[] = 'DROP DATABASE IF EXISTS '
3885 . PMA_Util::backquote($this_user) . ';';
3886 $GLOBALS['reload'] = true;
3889 return $queries;
3893 * update Message For Reload
3895 * @return array
3897 function PMA_updateMessageForReload()
3899 $message = null;
3900 if (isset($_REQUEST['flush_privileges'])) {
3901 $sql_query = 'FLUSH PRIVILEGES;';
3902 $GLOBALS['dbi']->query($sql_query);
3903 $message = PMA_Message::success(
3904 __('The privileges were reloaded successfully.')
3908 if (isset($_REQUEST['validate_username'])) {
3909 $message = PMA_Message::success();
3912 return $message;
3916 * update Data For Queries from queries_for_display
3918 * @param array $queries queries array
3919 * @param array|null $queries_for_display queries array for display
3921 * @return null
3923 function PMA_getDataForQueries($queries, $queries_for_display)
3925 $tmp_count = 0;
3926 foreach ($queries as $sql_query) {
3927 if ($sql_query{0} != '#') {
3928 $GLOBALS['dbi']->query($sql_query);
3930 // when there is a query containing a hidden password, take it
3931 // instead of the real query sent
3932 if (isset($queries_for_display[$tmp_count])) {
3933 $queries[$tmp_count] = $queries_for_display[$tmp_count];
3935 $tmp_count++;
3938 return $queries;
3942 * update Data for information: Adds a user
3944 * @param string $dbname db name
3945 * @param string $username user name
3946 * @param string $hostname host name
3947 * @param string $password password
3948 * @param bool $is_menuwork is_menuwork set?
3950 * @return array
3952 function PMA_addUser(
3953 $dbname, $username, $hostname,
3954 $password, $is_menuwork
3956 $_add_user_error = false;
3957 $message = null;
3958 $queries = null;
3959 $queries_for_display = null;
3960 $sql_query = null;
3962 if (!isset($_REQUEST['adduser_submit']) && !isset($_REQUEST['change_copy'])) {
3963 return array(
3964 $message, $queries, $queries_for_display, $sql_query, $_add_user_error
3968 if (!isset($_REQUEST['adduser_submit']) && !isset($_REQUEST['change_copy'])) {
3969 return array(
3970 $message,
3971 $queries,
3972 $queries_for_display,
3973 $sql_query,
3974 $_add_user_error
3978 $sql_query = '';
3979 if ($_POST['pred_username'] == 'any') {
3980 $username = '';
3982 switch ($_POST['pred_hostname']) {
3983 case 'any':
3984 $hostname = '%';
3985 break;
3986 case 'localhost':
3987 $hostname = 'localhost';
3988 break;
3989 case 'hosttable':
3990 $hostname = '';
3991 break;
3992 case 'thishost':
3993 $_user_name = $GLOBALS['dbi']->fetchValue('SELECT USER()');
3994 $hostname = /*overload*/mb_substr(
3995 $_user_name,
3996 (/*overload*/mb_strrpos($_user_name, '@') + 1)
3998 unset($_user_name);
3999 break;
4001 $sql = "SELECT '1' FROM `mysql`.`user`"
4002 . " WHERE `User` = '" . PMA_Util::sqlAddSlashes($username) . "'"
4003 . " AND `Host` = '" . PMA_Util::sqlAddSlashes($hostname) . "';";
4004 if ($GLOBALS['dbi']->fetchValue($sql) == 1) {
4005 $message = PMA_Message::error(__('The user %s already exists!'));
4006 $message->addParam(
4007 '[em]\'' . $username . '\'@\'' . $hostname . '\'[/em]'
4009 $_REQUEST['adduser'] = true;
4010 $_add_user_error = true;
4012 return array(
4013 $message,
4014 $queries,
4015 $queries_for_display,
4016 $sql_query,
4017 $_add_user_error
4021 list(
4022 $create_user_real, $create_user_show, $real_sql_query, $sql_query,
4023 $password_set_real, $password_set_show
4024 ) = PMA_getSqlQueriesForDisplayAndAddUser(
4025 $username, $hostname, (isset($password) ? $password : '')
4028 if (empty($_REQUEST['change_copy'])) {
4029 $_error = false;
4031 if (isset($create_user_real)) {
4032 if (!$GLOBALS['dbi']->tryQuery($create_user_real)) {
4033 $_error = true;
4035 if (isset($password_set_real) && !empty($password_set_real)
4036 && isset($_REQUEST['authentication_plugin'])
4038 PMA_setProperPasswordHashing(
4039 $_REQUEST['authentication_plugin']
4041 if ($GLOBALS['dbi']->tryQuery($password_set_real)) {
4042 $sql_query .= $password_set_show;
4045 $sql_query = $create_user_show . $sql_query;
4048 list($sql_query, $message) = PMA_addUserAndCreateDatabase(
4049 $_error,
4050 $real_sql_query,
4051 $sql_query,
4052 $username,
4053 $hostname,
4054 isset($dbname) ? $dbname : null
4056 if (!empty($_REQUEST['userGroup']) && $is_menuwork) {
4057 PMA_setUserGroup($GLOBALS['username'], $_REQUEST['userGroup']);
4060 return array(
4061 $message,
4062 $queries,
4063 $queries_for_display,
4064 $sql_query,
4065 $_add_user_error
4069 if (isset($create_user_real)) {
4070 $queries[] = $create_user_real;
4072 $queries[] = $real_sql_query;
4074 if (isset($password_set_real) && ! empty($password_set_real)
4075 && isset($_REQUEST['authentication_plugin'])
4077 PMA_setProperPasswordHashing(
4078 $_REQUEST['authentication_plugin']
4081 $queries[] = $password_set_real;
4083 // we put the query containing the hidden password in
4084 // $queries_for_display, at the same position occupied
4085 // by the real query in $queries
4086 $tmp_count = count($queries);
4087 if (isset($create_user_real)) {
4088 $queries_for_display[$tmp_count - 2] = $create_user_show;
4090 if (isset($password_set_real) && ! empty($password_set_real)) {
4091 $queries_for_display[$tmp_count - 3] = $create_user_show;
4092 $queries_for_display[$tmp_count - 2] = $sql_query;
4093 $queries_for_display[$tmp_count - 1] = $password_set_show;
4094 } else {
4095 $queries_for_display[$tmp_count - 1] = $sql_query;
4098 return array(
4099 $message, $queries, $queries_for_display, $sql_query, $_add_user_error
4104 * Sets proper value of `old_passwords` according to
4105 * the authentication plugin selected
4107 * @param string $auth_plugin authentication plugin selected
4109 * @return void
4111 function PMA_setProperPasswordHashing($auth_plugin)
4113 // Set the hashing method used by PASSWORD()
4114 // to be of type depending upon $authentication_plugin
4115 if ($auth_plugin == 'sha256_password') {
4116 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = 2');
4117 } else {
4118 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = 0');
4123 * Update DB information: DB, Table, isWildcard
4125 * @return array
4127 function PMA_getDataForDBInfo()
4129 $username = null;
4130 $hostname = null;
4131 $dbname = null;
4132 $tablename = null;
4133 $dbname_is_wildcard = null;
4135 if (isset($_REQUEST['username'])) {
4136 $username = $_REQUEST['username'];
4138 if (isset($_REQUEST['hostname'])) {
4139 $hostname = $_REQUEST['hostname'];
4142 * Checks if a dropdown box has been used for selecting a database / table
4144 if (PMA_isValid($_REQUEST['pred_tablename'])) {
4145 $tablename = $_REQUEST['pred_tablename'];
4146 } elseif (PMA_isValid($_REQUEST['tablename'])) {
4147 $tablename = $_REQUEST['tablename'];
4148 } else {
4149 unset($tablename);
4152 if (isset($_REQUEST['pred_dbname'])) {
4153 $is_valid_pred_dbname = true;
4154 foreach ($_REQUEST['pred_dbname'] as $key => $db_name) {
4155 if (! PMA_isValid($db_name)) {
4156 $is_valid_pred_dbname = false;
4157 break;
4162 if (isset($_REQUEST['dbname'])) {
4163 $is_valid_dbname = true;
4164 if (is_array($_REQUEST['dbname'])) {
4165 foreach ($_REQUEST['dbname'] as $key => $db_name) {
4166 if (! PMA_isValid($db_name)) {
4167 $is_valid_dbname = false;
4168 break;
4171 } else {
4172 if (! PMA_isValid($_REQUEST['dbname'])) {
4173 $is_valid_dbname = false;
4178 if (isset($is_valid_pred_dbname) && $is_valid_pred_dbname) {
4179 $dbname = $_REQUEST['pred_dbname'];
4180 // If dbname contains only one database.
4181 if (count($dbname) == 1) {
4182 $dbname = $dbname[0];
4184 } elseif (isset($is_valid_dbname) && $is_valid_dbname) {
4185 $dbname = $_REQUEST['dbname'];
4186 } else {
4187 unset($dbname);
4188 unset($tablename);
4191 if (isset($dbname)) {
4192 if (is_array($dbname)) {
4193 $db_and_table = $dbname;
4194 foreach ($db_and_table as $key => $db_name) {
4195 $db_and_table[$key] .= '.';
4197 } else {
4198 $unescaped_db = PMA_Util::unescapeMysqlWildcards($dbname);
4199 $db_and_table = PMA_Util::backquote($unescaped_db) . '.';
4201 if (isset($tablename)) {
4202 $db_and_table .= PMA_Util::backquote($tablename);
4203 } else {
4204 if (is_array($db_and_table)) {
4205 foreach ($db_and_table as $key => $db_name) {
4206 $db_and_table[$key] .= '*';
4208 } else {
4209 $db_and_table .= '*';
4212 } else {
4213 $db_and_table = '*.*';
4216 // check if given $dbname is a wildcard or not
4217 if (isset($dbname)) {
4218 //if (preg_match('/\\\\(?:_|%)/i', $dbname)) {
4219 if (! is_array($dbname) && preg_match('/(?<!\\\\)(?:_|%)/i', $dbname)) {
4220 $dbname_is_wildcard = true;
4221 } else {
4222 $dbname_is_wildcard = false;
4226 return array(
4227 $username, $hostname,
4228 isset($dbname)? $dbname : null,
4229 isset($tablename)? $tablename : null,
4230 $db_and_table,
4231 $dbname_is_wildcard,
4236 * Get title and textarea for export user definition in Privileges
4238 * @param string $username username
4239 * @param string $hostname host name
4241 * @return array ($title, $export)
4243 function PMA_getListForExportUserDefinition($username, $hostname)
4245 $export = '<textarea class="export" cols="60" rows="15">';
4247 if (isset($_REQUEST['selected_usr'])) {
4248 // export privileges for selected users
4249 $title = __('Privileges');
4251 foreach ($_REQUEST['selected_usr'] as $export_user) {
4252 $export_username = /*overload*/mb_substr(
4253 $export_user, 0, /*overload*/mb_strpos($export_user, '&')
4255 $export_hostname = /*overload*/mb_substr(
4256 $export_user, /*overload*/mb_strrpos($export_user, ';') + 1
4258 $export .= '# '
4259 . sprintf(
4260 __('Privileges for %s'),
4261 '`' . htmlspecialchars($export_username)
4262 . '`@`' . htmlspecialchars($export_hostname) . '`'
4264 . "\n\n";
4265 $export .= PMA_getGrants($export_username, $export_hostname) . "\n";
4267 } else {
4268 // export privileges for a single user
4269 $title = __('User') . ' `' . htmlspecialchars($username)
4270 . '`@`' . htmlspecialchars($hostname) . '`';
4271 $export .= PMA_getGrants($username, $hostname);
4273 // remove trailing whitespace
4274 $export = trim($export);
4276 $export .= '</textarea>';
4278 return array($title, $export);
4282 * Get HTML for display Add userfieldset
4284 * @param string $db the database
4285 * @param string $table the table name
4287 * @return string html output
4289 function PMA_getAddUserHtmlFieldset($db = '', $table = '')
4291 if (!$GLOBALS['is_createuser']) {
4292 return '';
4294 $rel_params = array();
4295 $url_params = array(
4296 'adduser' => 1
4298 if (!empty($db)) {
4299 $url_params['dbname']
4300 = $rel_params['checkprivsdb']
4301 = $db;
4303 if (!empty($table)) {
4304 $url_params['tablename']
4305 = $rel_params['checkprivstable']
4306 = $table;
4309 return '<fieldset id="fieldset_add_user">' . "\n"
4310 . '<legend>' . _pgettext('Create new user', 'New') . '</legend>'
4311 . '<a id="add_user_anchor" href="server_privileges.php'
4312 . PMA_URL_getCommon($url_params) . '" '
4313 . (!empty($rel_params)
4314 ? ('rel="' . PMA_URL_getCommon($rel_params) . '" ')
4315 : '')
4316 . '>' . "\n"
4317 . PMA_Util::getIcon('b_usradd.png')
4318 . ' ' . __('Add user account') . '</a>' . "\n"
4319 . '</fieldset>' . "\n";
4323 * Get HTML header for display User's properties
4325 * @param boolean $dbname_is_wildcard whether database name is wildcard or not
4326 * @param string $url_dbname url database name that urlencode() string
4327 * @param string $dbname database name
4328 * @param string $username username
4329 * @param string $hostname host name
4330 * @param string $tablename table name
4332 * @return string $html_output
4334 function PMA_getHtmlHeaderForUserProperties(
4335 $dbname_is_wildcard, $url_dbname, $dbname, $username, $hostname, $tablename
4337 $html_output = '<h2>' . "\n"
4338 . PMA_Util::getIcon('b_usredit.png')
4339 . __('Edit privileges:') . ' '
4340 . __('User account');
4342 if (! empty($dbname)) {
4343 $html_output .= ' <i><a class="edit_user_anchor"'
4344 . ' href="server_privileges.php'
4345 . PMA_URL_getCommon(
4346 array(
4347 'username' => $username,
4348 'hostname' => $hostname,
4349 'dbname' => '',
4350 'tablename' => '',
4353 . '">\'' . htmlspecialchars($username)
4354 . '\'@\'' . htmlspecialchars($hostname)
4355 . '\'</a></i>' . "\n";
4357 $html_output .= ' - ';
4358 $html_output .= ($dbname_is_wildcard
4359 || is_array($dbname) && count($dbname) > 1)
4360 ? __('Databases') : __('Database');
4361 if (! empty($_REQUEST['tablename'])) {
4362 $html_output .= ' <i><a href="server_privileges.php'
4363 . PMA_URL_getCommon(
4364 array(
4365 'username' => $username,
4366 'hostname' => $hostname,
4367 'dbname' => $url_dbname,
4368 'tablename' => '',
4371 . '">' . htmlspecialchars($dbname)
4372 . '</a></i>';
4374 $html_output .= ' - ' . __('Table')
4375 . ' <i>' . htmlspecialchars($tablename) . '</i>';
4376 } else {
4377 if (! is_array($dbname)) {
4378 $dbname = array($dbname);
4380 $html_output .= ' <i>'
4381 . htmlspecialchars(implode(', ', $dbname))
4382 . '</i>';
4385 } else {
4386 $html_output .= ' <i>\'' . htmlspecialchars($username)
4387 . '\'@\'' . htmlspecialchars($hostname)
4388 . '\'</i>' . "\n";
4391 $html_output .= '</h2>' . "\n";
4392 $cur_user = htmlspecialchars($GLOBALS['dbi']->getCurrentUser());
4393 $user = htmlspecialchars($username . '@' . $hostname);
4394 // Add a short notice for the user
4395 // to remind him that he is editing his own privileges
4396 if ($user === $cur_user) {
4397 $html_output .= PMA_Message::notice(
4399 'Note: You are attempting to edit privileges of the '
4400 . 'user with which you are currently logged in.'
4402 )->getDisplay();
4404 return $html_output;
4408 * Get HTML snippet for display user overview page
4410 * @param string $pmaThemeImage a image source link
4411 * @param string $text_dir text directory
4413 * @return string $html_output
4415 function PMA_getHtmlForUserOverview($pmaThemeImage, $text_dir)
4417 $html_output = '<h2>' . "\n"
4418 . PMA_Util::getIcon('b_usrlist.png')
4419 . __('User accounts overview') . "\n"
4420 . '</h2>' . "\n";
4422 $password_column = 'Password';
4423 if (PMA_Util::getServerType() == 'MySQL'
4424 && PMA_MYSQL_INT_VERSION >= 50706
4426 $password_column = 'authentication_string';
4428 // $sql_query is for the initial-filtered,
4429 // $sql_query_all is for counting the total no. of users
4431 $sql_query = $sql_query_all = 'SELECT *,' .
4432 " IF(`" . $password_column . "` = _latin1 '', 'N', 'Y') AS 'Password'" .
4433 ' FROM `mysql`.`user`';
4435 $sql_query .= (isset($_REQUEST['initial'])
4436 ? PMA_rangeOfUsers($_REQUEST['initial'])
4437 : '');
4439 $sql_query .= ' ORDER BY `User` ASC, `Host` ASC;';
4440 $sql_query_all .= ' ;';
4442 $res = $GLOBALS['dbi']->tryQuery(
4443 $sql_query, null, PMA_DatabaseInterface::QUERY_STORE
4445 $res_all = $GLOBALS['dbi']->tryQuery(
4446 $sql_query_all, null, PMA_DatabaseInterface::QUERY_STORE
4449 if (! $res) {
4450 // the query failed! This may have two reasons:
4451 // - the user does not have enough privileges
4452 // - the privilege tables use a structure of an earlier version.
4453 // so let's try a more simple query
4455 $GLOBALS['dbi']->freeResult($res);
4456 $GLOBALS['dbi']->freeResult($res_all);
4457 $sql_query = 'SELECT * FROM `mysql`.`user`';
4458 $res = $GLOBALS['dbi']->tryQuery(
4459 $sql_query, null, PMA_DatabaseInterface::QUERY_STORE
4462 if (! $res) {
4463 $html_output .= PMA_getHtmlForViewUsersError();
4464 $html_output .= PMA_getAddUserHtmlFieldset();
4465 } else {
4466 // This message is hardcoded because I will replace it by
4467 // a automatic repair feature soon.
4468 $raw = 'Your privilege table structure seems to be older than'
4469 . ' this MySQL version!<br />'
4470 . 'Please run the <code>mysql_upgrade</code> command'
4471 . '(<code>mysql_fix_privilege_tables</code> on older systems)'
4472 . ' that should be included in your MySQL server distribution'
4473 . ' to solve this problem!';
4474 $html_output .= PMA_Message::rawError($raw)->getDisplay();
4476 $GLOBALS['dbi']->freeResult($res);
4477 } else {
4478 $db_rights = PMA_getDbRightsForUserOverview();
4479 // for all initials, even non A-Z
4480 $array_initials = array();
4482 foreach ($db_rights as $right) {
4483 foreach ($right as $account) {
4484 if (empty($account['User']) && $account['Host'] == 'localhost') {
4485 $html_output .= PMA_Message::notice(
4487 'A user account allowing any user from localhost to '
4488 . 'connect is present. This will prevent other users '
4489 . 'from connecting if the host part of their account '
4490 . 'allows a connection from any (%) host.'
4492 . PMA_Util::showMySQLDocu('problems-connecting')
4493 )->getDisplay();
4494 break 2;
4500 * Displays the initials
4501 * Also not necessary if there is less than 20 privileges
4503 if ($GLOBALS['dbi']->numRows($res_all) > 20) {
4504 $html_output .= PMA_getHtmlForInitials($array_initials);
4508 * Display the user overview
4509 * (if less than 50 users, display them immediately)
4511 if (isset($_REQUEST['initial'])
4512 || isset($_REQUEST['showall'])
4513 || $GLOBALS['dbi']->numRows($res) < 50
4515 $html_output .= PMA_getUsersOverview(
4516 $res, $db_rights, $pmaThemeImage, $text_dir
4518 } else {
4519 $html_output .= PMA_getAddUserHtmlFieldset();
4520 } // end if (display overview)
4522 if (! $GLOBALS['is_ajax_request']
4523 || ! empty($_REQUEST['ajax_page_request'])
4525 if (isset($GLOBALS['flush_priv']) && $GLOBALS['flush_priv']) {
4526 $flushnote = new PMA_Message(
4528 'Note: phpMyAdmin gets the users\' privileges directly '
4529 . 'from MySQL\'s privilege tables. The content of these '
4530 . 'tables may differ from the privileges the server uses, '
4531 . 'if they have been changed manually. In this case, '
4532 . 'you should %sreload the privileges%s before you continue.'
4534 PMA_Message::NOTICE
4536 $flushLink = '<a href="server_privileges.php'
4537 . PMA_URL_getCommon(array('flush_privileges' => 1))
4538 . '" id="reload_privileges_anchor">';
4539 $flushnote->addParam(
4540 $flushLink,
4541 false
4543 $flushnote->addParam('</a>', false);
4544 } else {
4545 $flushnote = new PMA_Message(
4547 'Note: phpMyAdmin gets the users\' privileges directly '
4548 . 'from MySQL\'s privilege tables. The content of these '
4549 . 'tables may differ from the privileges the server uses, '
4550 . 'if they have been changed manually. In this case, '
4551 . 'the privileges have to be reloaded but currently, you '
4552 . 'don\'t have the RELOAD privilege.'
4554 . PMA_Util::showMySQLDocu(
4555 'privileges-provided',
4556 false,
4557 'priv_reload'
4559 PMA_Message::NOTICE
4562 $html_output .= $flushnote->getDisplay();
4566 return $html_output;
4570 * Get HTML snippet for display user properties
4572 * @param boolean $dbname_is_wildcard whether database name is wildcard or not
4573 * @param string $url_dbname url database name that urlencode() string
4574 * @param string $username username
4575 * @param string $hostname host name
4576 * @param string $dbname database name
4577 * @param string $tablename table name
4579 * @return string $html_output
4581 function PMA_getHtmlForUserProperties($dbname_is_wildcard,$url_dbname,
4582 $username, $hostname, $dbname, $tablename
4584 $html_output = '<div id="edit_user_dialog">';
4585 $html_output .= PMA_getHtmlHeaderForUserProperties(
4586 $dbname_is_wildcard, $url_dbname, $dbname, $username, $hostname, $tablename
4589 $sql = "SELECT '1' FROM `mysql`.`user`"
4590 . " WHERE `User` = '" . PMA_Util::sqlAddSlashes($username) . "'"
4591 . " AND `Host` = '" . PMA_Util::sqlAddSlashes($hostname) . "';";
4593 $user_does_not_exists = (bool) ! $GLOBALS['dbi']->fetchValue($sql);
4595 if ($user_does_not_exists) {
4596 $html_output .= PMA_Message::error(
4597 __('The selected user was not found in the privilege table.')
4598 )->getDisplay();
4599 $html_output .= PMA_getHtmlForLoginInformationFields();
4600 //exit;
4603 $_params = array(
4604 'username' => $username,
4605 'hostname' => $hostname,
4607 if (! is_array($dbname) && /*overload*/mb_strlen($dbname)) {
4608 $_params['dbname'] = $dbname;
4609 if (/*overload*/mb_strlen($tablename)) {
4610 $_params['tablename'] = $tablename;
4612 } else {
4613 $_params['dbname'] = $dbname;
4616 $html_output .= '<form class="submenu-item" name="usersForm" '
4617 . 'id="addUsersForm" action="server_privileges.php" method="post">' . "\n";
4618 $html_output .= PMA_URL_getHiddenInputs($_params);
4619 $html_output .= PMA_getHtmlToDisplayPrivilegesTable(
4620 // If $dbname is an array, pass any one db as all have same privs.
4621 PMA_ifSetOr($dbname, (is_array($dbname)) ? $dbname[0] : '*', 'length'),
4622 PMA_ifSetOr($tablename, '*', 'length')
4625 $html_output .= '</form>' . "\n";
4627 if (! is_array($dbname) && ! /*overload*/mb_strlen($tablename)
4628 && empty($dbname_is_wildcard)
4631 // no table name was given, display all table specific rights
4632 // but only if $dbname contains no wildcards
4634 $html_output .= '<form class="submenu-item" action="server_privileges.php" '
4635 . 'id="db_or_table_specific_priv" method="post">' . "\n";
4637 // unescape wildcards in dbname at table level
4638 $unescaped_db = PMA_Util::unescapeMysqlWildcards($dbname);
4639 list($html_rightsTable, $found_rows)
4640 = PMA_getHtmlForAllTableSpecificRights(
4641 $username, $hostname, $unescaped_db
4643 $html_output .= $html_rightsTable;
4645 if (! /*overload*/mb_strlen($dbname)) {
4646 // no database name was given, display select db
4647 $html_output .= PMA_getHtmlForSelectDbInEditPrivs($found_rows);
4649 } else {
4650 $html_output .= PMA_displayTablesInEditPrivs($dbname, $found_rows);
4652 $html_output .= '</fieldset>' . "\n";
4654 $html_output .= '<fieldset class="tblFooters">' . "\n"
4655 . ' <input type="submit" value="' . __('Go') . '" />'
4656 . '</fieldset>' . "\n"
4657 . '</form>' . "\n";
4660 // Provide a line with links to the relevant database and table
4661 if (! is_array($dbname) && /*overload*/mb_strlen($dbname)
4662 && empty($dbname_is_wildcard)
4664 $html_output .= PMA_getLinkToDbAndTable($url_dbname, $dbname, $tablename);
4668 if (! is_array($dbname) && ! /*overload*/mb_strlen($dbname)
4669 && ! $user_does_not_exists
4671 //change login information
4672 $html_output .= PMA_getHtmlForChangePassword($username, $hostname);
4673 $html_output .= PMA_getChangeLoginInformationHtmlForm($username, $hostname);
4675 $html_output .= '</div>';
4677 return $html_output;
4681 * Get queries for Table privileges to change or copy user
4683 * @param string $user_host_condition user host condition to
4684 * select relevant table privileges
4685 * @param array $queries queries array
4686 * @param string $username username
4687 * @param string $hostname host name
4689 * @return array $queries
4691 function PMA_getTablePrivsQueriesForChangeOrCopyUser($user_host_condition,
4692 $queries, $username, $hostname
4694 $res = $GLOBALS['dbi']->query(
4695 'SELECT `Db`, `Table_name`, `Table_priv` FROM `mysql`.`tables_priv`'
4696 . $user_host_condition,
4697 $GLOBALS['userlink'],
4698 PMA_DatabaseInterface::QUERY_STORE
4700 while ($row = $GLOBALS['dbi']->fetchAssoc($res)) {
4702 $res2 = $GLOBALS['dbi']->query(
4703 'SELECT `Column_name`, `Column_priv`'
4704 . ' FROM `mysql`.`columns_priv`'
4705 . ' WHERE `User`'
4706 . ' = \'' . PMA_Util::sqlAddSlashes($_REQUEST['old_username']) . "'"
4707 . ' AND `Host`'
4708 . ' = \'' . PMA_Util::sqlAddSlashes($_REQUEST['old_username']) . '\''
4709 . ' AND `Db`'
4710 . ' = \'' . PMA_Util::sqlAddSlashes($row['Db']) . "'"
4711 . ' AND `Table_name`'
4712 . ' = \'' . PMA_Util::sqlAddSlashes($row['Table_name']) . "'"
4713 . ';',
4714 null,
4715 PMA_DatabaseInterface::QUERY_STORE
4718 $tmp_privs1 = PMA_extractPrivInfo($row);
4719 $tmp_privs2 = array(
4720 'Select' => array(),
4721 'Insert' => array(),
4722 'Update' => array(),
4723 'References' => array()
4726 while ($row2 = $GLOBALS['dbi']->fetchAssoc($res2)) {
4727 $tmp_array = explode(',', $row2['Column_priv']);
4728 if (in_array('Select', $tmp_array)) {
4729 $tmp_privs2['Select'][] = $row2['Column_name'];
4731 if (in_array('Insert', $tmp_array)) {
4732 $tmp_privs2['Insert'][] = $row2['Column_name'];
4734 if (in_array('Update', $tmp_array)) {
4735 $tmp_privs2['Update'][] = $row2['Column_name'];
4737 if (in_array('References', $tmp_array)) {
4738 $tmp_privs2['References'][] = $row2['Column_name'];
4741 if (count($tmp_privs2['Select']) > 0 && ! in_array('SELECT', $tmp_privs1)) {
4742 $tmp_privs1[] = 'SELECT (`' . join('`, `', $tmp_privs2['Select']) . '`)';
4744 if (count($tmp_privs2['Insert']) > 0 && ! in_array('INSERT', $tmp_privs1)) {
4745 $tmp_privs1[] = 'INSERT (`' . join('`, `', $tmp_privs2['Insert']) . '`)';
4747 if (count($tmp_privs2['Update']) > 0 && ! in_array('UPDATE', $tmp_privs1)) {
4748 $tmp_privs1[] = 'UPDATE (`' . join('`, `', $tmp_privs2['Update']) . '`)';
4750 if (count($tmp_privs2['References']) > 0
4751 && ! in_array('REFERENCES', $tmp_privs1)
4753 $tmp_privs1[]
4754 = 'REFERENCES (`' . join('`, `', $tmp_privs2['References']) . '`)';
4757 $queries[] = 'GRANT ' . join(', ', $tmp_privs1)
4758 . ' ON ' . PMA_Util::backquote($row['Db']) . '.'
4759 . PMA_Util::backquote($row['Table_name'])
4760 . ' TO \'' . PMA_Util::sqlAddSlashes($username)
4761 . '\'@\'' . PMA_Util::sqlAddSlashes($hostname) . '\''
4762 . (in_array('Grant', explode(',', $row['Table_priv']))
4763 ? ' WITH GRANT OPTION;'
4764 : ';');
4766 return $queries;
4770 * Get queries for database specific privileges for change or copy user
4772 * @param array $queries queries array with string
4773 * @param string $username username
4774 * @param string $hostname host name
4776 * @return array $queries
4778 function PMA_getDbSpecificPrivsQueriesForChangeOrCopyUser(
4779 $queries, $username, $hostname
4781 $user_host_condition = ' WHERE `User`'
4782 . ' = \'' . PMA_Util::sqlAddSlashes($_REQUEST['old_username']) . "'"
4783 . ' AND `Host`'
4784 . ' = \'' . PMA_Util::sqlAddSlashes($_REQUEST['old_hostname']) . '\';';
4786 $res = $GLOBALS['dbi']->query(
4787 'SELECT * FROM `mysql`.`db`' . $user_host_condition
4790 while ($row = $GLOBALS['dbi']->fetchAssoc($res)) {
4791 $queries[] = 'GRANT ' . join(', ', PMA_extractPrivInfo($row))
4792 . ' ON ' . PMA_Util::backquote($row['Db']) . '.*'
4793 . ' TO \'' . PMA_Util::sqlAddSlashes($username)
4794 . '\'@\'' . PMA_Util::sqlAddSlashes($hostname) . '\''
4795 . ($row['Grant_priv'] == 'Y' ? ' WITH GRANT OPTION;' : ';');
4797 $GLOBALS['dbi']->freeResult($res);
4799 $queries = PMA_getTablePrivsQueriesForChangeOrCopyUser(
4800 $user_host_condition, $queries, $username, $hostname
4803 return $queries;
4807 * Prepares queries for adding users and
4808 * also create database and return query and message
4810 * @param boolean $_error whether user create or not
4811 * @param string $real_sql_query SQL query for add a user
4812 * @param string $sql_query SQL query to be displayed
4813 * @param string $username username
4814 * @param string $hostname host name
4815 * @param string $dbname database name
4817 * @return array $sql_query, $message
4819 function PMA_addUserAndCreateDatabase($_error, $real_sql_query, $sql_query,
4820 $username, $hostname, $dbname
4822 if ($_error || (!empty($real_sql_query)
4823 && !$GLOBALS['dbi']->tryQuery($real_sql_query))
4825 $_REQUEST['createdb-1'] = $_REQUEST['createdb-2']
4826 = $_REQUEST['createdb-3'] = null;
4827 $message = PMA_Message::rawError($GLOBALS['dbi']->getError());
4828 } else {
4829 $message = PMA_Message::success(__('You have added a new user.'));
4832 if (isset($_REQUEST['createdb-1'])) {
4833 // Create database with same name and grant all privileges
4834 $q = 'CREATE DATABASE IF NOT EXISTS '
4835 . PMA_Util::backquote(
4836 PMA_Util::sqlAddSlashes($username)
4837 ) . ';';
4838 $sql_query .= $q;
4839 if (! $GLOBALS['dbi']->tryQuery($q)) {
4840 $message = PMA_Message::rawError($GLOBALS['dbi']->getError());
4844 * Reload the navigation
4846 $GLOBALS['reload'] = true;
4847 $GLOBALS['db'] = $username;
4849 $q = 'GRANT ALL PRIVILEGES ON '
4850 . PMA_Util::backquote(
4851 PMA_Util::escapeMysqlWildcards(
4852 PMA_Util::sqlAddSlashes($username)
4854 ) . '.* TO \''
4855 . PMA_Util::sqlAddSlashes($username)
4856 . '\'@\'' . PMA_Util::sqlAddSlashes($hostname) . '\';';
4857 $sql_query .= $q;
4858 if (! $GLOBALS['dbi']->tryQuery($q)) {
4859 $message = PMA_Message::rawError($GLOBALS['dbi']->getError());
4863 if (isset($_REQUEST['createdb-2'])) {
4864 // Grant all privileges on wildcard name (username\_%)
4865 $q = 'GRANT ALL PRIVILEGES ON '
4866 . PMA_Util::backquote(
4867 PMA_Util::sqlAddSlashes($username) . '\_%'
4868 ) . '.* TO \''
4869 . PMA_Util::sqlAddSlashes($username)
4870 . '\'@\'' . PMA_Util::sqlAddSlashes($hostname) . '\';';
4871 $sql_query .= $q;
4872 if (! $GLOBALS['dbi']->tryQuery($q)) {
4873 $message = PMA_Message::rawError($GLOBALS['dbi']->getError());
4877 if (isset($_REQUEST['createdb-3'])) {
4878 // Grant all privileges on the specified database to the new user
4879 $q = 'GRANT ALL PRIVILEGES ON '
4880 . PMA_Util::backquote(
4881 PMA_Util::sqlAddSlashes($dbname)
4882 ) . '.* TO \''
4883 . PMA_Util::sqlAddSlashes($username)
4884 . '\'@\'' . PMA_Util::sqlAddSlashes($hostname) . '\';';
4885 $sql_query .= $q;
4886 if (! $GLOBALS['dbi']->tryQuery($q)) {
4887 $message = PMA_Message::rawError($GLOBALS['dbi']->getError());
4890 return array($sql_query, $message);
4894 * Get SQL queries for Display and Add user
4896 * @param string $username username
4897 * @param string $hostname host name
4898 * @param string $password password
4900 * @return array ($create_user_real, $create_user_show,$real_sql_query, $sql_query
4901 * $password_set_real, $password_set_show)
4903 function PMA_getSqlQueriesForDisplayAndAddUser($username, $hostname, $password)
4905 $slashedUsername = PMA_Util::sqlAddSlashes($username);
4906 $slashedHostname = PMA_Util::sqlAddSlashes($hostname);
4908 $create_user_stmt = sprintf(
4909 'CREATE USER \'%s\'@\'%s\'',
4910 $slashedUsername,
4911 $slashedHostname
4913 $create_user_real = $create_user_show = $create_user_stmt;
4915 $password_set_stmt = 'SET PASSWORD FOR \'%s\'@\'%s\' = PASSWORD(\'%s\')';
4916 $password_set_show = sprintf(
4917 $password_set_stmt,
4918 $slashedUsername,
4919 $slashedHostname,
4920 '***'
4922 $password_set_real = null;
4924 $sql_query_stmt = sprintf(
4925 'GRANT %s ON *.* TO \'%s\'@\'%s\'',
4926 join(', ', PMA_extractPrivInfo()),
4927 $slashedUsername,
4928 $slashedHostname
4930 $real_sql_query = $sql_query = $sql_query_stmt;
4932 //@todo Following blocks should be delegated to another function and factorized.
4933 //There are too much duplication here.
4934 if ($_POST['pred_password'] != 'none' && $_POST['pred_password'] != 'keep') {
4935 $slashedPassword = PMA_Util::sqlAddSlashes($_POST['pma_pw']);
4936 if (isset($_REQUEST['authentication_plugin'])
4937 && $_REQUEST['authentication_plugin']
4939 if (PMA_MYSQL_INT_VERSION >= 50700) {
4940 $create_user_stmt .= ' IDENTIFIED WITH '
4941 . $_REQUEST['authentication_plugin'] . ' BY \'%s\'';
4942 $create_user_show = sprintf($create_user_stmt, '***');
4943 $create_user_real = sprintf(
4944 $create_user_stmt,
4945 $slashedPassword
4947 } else {
4948 $create_user_stmt .= ' IDENTIFIED WITH '
4949 . $_REQUEST['authentication_plugin'];
4950 $create_user_show = $create_user_real = $create_user_stmt;
4952 } else {
4953 $sql_query_stmt .= ' IDENTIFIED BY \'%s\' ';
4954 $sql_query = sprintf($sql_query_stmt, '***');
4955 $real_sql_query = sprintf($sql_query_stmt, $slashedPassword);
4957 $password_set_real = sprintf(
4958 $password_set_stmt,
4959 $slashedUsername,
4960 $slashedHostname,
4961 $slashedPassword
4963 } else {
4964 $slashedPassword = PMA_Util::sqlAddSlashes($password);
4965 if ($_POST['pred_password'] == 'keep' && ! empty($password)) {
4966 if (isset($_REQUEST['authentication_plugin'])
4967 && $_REQUEST['authentication_plugin']
4969 if (PMA_MYSQL_INT_VERSION >= 50700) {
4970 $create_user_stmt .= ' IDENTIFIED WITH '
4971 . $_REQUEST['authentication_plugin'] . ' BY \'%s\'';
4972 $create_user_show = sprintf($create_user_stmt, '***');
4973 $create_user_real = sprintf(
4974 $create_user_stmt,
4975 $slashedPassword
4977 } else {
4978 $create_user_stmt .= ' IDENTIFIED WITH '
4979 . $_REQUEST['authentication_plugin'];
4980 $create_user_show = $create_user_real = $create_user_stmt;
4983 $password_set_real = sprintf(
4984 $password_set_stmt,
4985 $slashedUsername,
4986 $slashedHostname,
4987 $slashedPassword
4989 } else {
4990 $sql_query_stmt .= ' IDENTIFIED BY \'%s\' ';
4991 $sql_query = sprintf($sql_query_stmt, '***');
4992 $real_sql_query = sprintf($sql_query_stmt, $slashedPassword);
4993 $password_set_real = null;
4995 } elseif ($_POST['pred_password'] == 'keep' && empty($password)) {
4996 if (isset($_REQUEST['authentication_plugin'])
4997 && $_REQUEST['authentication_plugin']
4999 if (PMA_MYSQL_INT_VERSION >= 50700) {
5000 $create_user_stmt .= ' IDENTIFIED WITH '
5001 . $_REQUEST['authentication_plugin'] . ' BY \'%s\'';
5002 $create_user_show = sprintf($create_user_stmt, '***');
5003 $create_user_real = sprintf(
5004 $create_user_stmt,
5005 null
5007 } else {
5008 $create_user_stmt .= ' IDENTIFIED WITH '
5009 . $_REQUEST['authentication_plugin'];
5010 $create_user_show = $create_user_real = $create_user_stmt;
5012 $password_set_real = sprintf(
5013 $password_set_stmt,
5014 $slashedUsername,
5015 $slashedHostname,
5016 null
5018 } else {
5019 $sql_query_stmt .= ' IDENTIFIED BY \'%s\' ';
5020 $sql_query = sprintf($sql_query_stmt, '***');
5021 $real_sql_query = sprintf($sql_query_stmt, null);
5022 $password_set_real = null;
5027 // add REQUIRE clause
5028 $require_clause = PMA_getRequireClause();
5029 $real_sql_query .= $require_clause;
5030 $sql_query .= $require_clause;
5032 if ((isset($_POST['Grant_priv']) && $_POST['Grant_priv'] == 'Y')
5033 || (isset($_POST['max_questions']) || isset($_POST['max_connections'])
5034 || isset($_POST['max_updates']) || isset($_POST['max_user_connections']))
5036 $with_clause = PMA_getWithClauseForAddUserAndUpdatePrivs();
5037 $real_sql_query .= $with_clause;
5038 $sql_query .= $with_clause;
5041 if (isset($create_user_real)) {
5042 $create_user_real .= ';';
5043 $create_user_show .= ';';
5045 $real_sql_query .= ';';
5046 $sql_query .= ';';
5047 // No Global GRANT_OPTION privilege
5048 if (!$GLOBALS['is_grantuser']) {
5049 $real_sql_query = '';
5050 $sql_query = '';
5053 if (PMA_Util::getServerType() == 'MySQL'
5054 && PMA_MYSQL_INT_VERSION >= 50700
5056 $password_set_real = null;
5057 $password_set_show = null;
5058 } else {
5059 $password_set_real .= ";";
5060 $password_set_show .= ";";
5063 return array($create_user_real,
5064 $create_user_show,
5065 $real_sql_query,
5066 $sql_query,
5067 $password_set_real,
5068 $password_set_show