2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Get user's global privileges and some db-specific privileges
12 $GLOBALS['is_superuser'] = PMA_isSuperuser();
15 * sets privilege information extracted from SHOW GRANTS result
17 * Detection for some CREATE privilege.
19 * Since MySQL 4.1.2, we can easily detect current user's grants using $userlink
20 * (no control user needed) and we don't have to try any other method for
23 * @todo fix to get really all privileges, not only explicitly defined for this user
24 * from MySQL manual: (http://dev.mysql.com/doc/refman/5.0/en/show-grants.html)
25 * SHOW GRANTS displays only the privileges granted explicitly to the named
26 * account. Other privileges might be available to the account, but they are not
27 * displayed. For example, if an anonymous account exists, the named account
28 * might be able to use its privileges, but SHOW GRANTS will not display them.
30 * @uses $_SESSION['is_create_db_priv'] for caching
31 * @uses $_SESSION['is_process_priv'] for caching
32 * @uses $_SESSION['is_reload_priv'] for caching
33 * @uses $_SESSION['db_to_create'] for caching
34 * @uses $_SESSION['dbs_where_create_table_allowed'] for caching
35 * @uses $GLOBALS['is_create_db_priv'] to set it
36 * @uses $GLOBALS['is_process_priv'] to set it
37 * @uses $GLOBALS['is_reload_priv'] to set it
38 * @uses $GLOBALS['db_to_create'] to set it
39 * @uses $GLOBALS['dbs_where_create_table_allowed'] to set it
40 * @uses $GLOBALS['server']
41 * @uses PMA_DBI_try_query()
42 * @uses PMA_DBI_fetch_row()
43 * @uses PMA_DBI_free_result()
44 * @uses PMA_DBI_getError()
46 * @uses PMA_backquote()
48 * @uses preg_replace()
52 function PMA_analyseShowGrant()
54 if (PMA_cacheExists('is_create_db_priv', true)) {
55 $GLOBALS['is_create_db_priv'] = PMA_cacheGet('is_create_db_priv', true);
56 $GLOBALS['is_process_priv'] = PMA_cacheGet('is_process_priv', true);
57 $GLOBALS['is_reload_priv'] = PMA_cacheGet('is_reload_priv', true);
58 $GLOBALS['db_to_create'] = PMA_cacheGet('db_to_create', true);
59 $GLOBALS['dbs_where_create_table_allowed']
60 = PMA_cacheGet('dbs_where_create_table_allowed', true);
65 $GLOBALS['is_create_db_priv'] = false;
66 $GLOBALS['is_process_priv'] = true;
67 $GLOBALS['is_reload_priv'] = false;
68 $GLOBALS['db_to_create'] = '';
69 $GLOBALS['dbs_where_create_table_allowed'] = array();
71 $rs_usr = PMA_DBI_try_query('SHOW GRANTS');
77 $re0 = '(^|(\\\\\\\\)+|[^\\\\])'; // non-escaped wildcards
78 $re1 = '(^|[^\\\\])(\\\)+'; // escaped wildcards
80 while ($row = PMA_DBI_fetch_row($rs_usr)) {
81 // extract db from GRANT ... ON *.* or GRANT ... ON db.*
82 $db_name_offset = strpos($row[0], ' ON ') +
4;
83 $show_grants_dbname = substr($row[0],
85 strpos($row[0], '.', $db_name_offset) - $db_name_offset);
86 $show_grants_dbname = PMA_unQuote($show_grants_dbname, '`');
88 $show_grants_str = substr($row[0], 6, (strpos($row[0], ' ON ') - 6));
89 if ($show_grants_str == 'RELOAD') {
90 $GLOBALS['is_reload_priv'] = true;
94 * @todo if we find CREATE VIEW but not CREATE, do not offer
95 * the create database dialog box
97 if ($show_grants_str == 'ALL'
98 ||
$show_grants_str == 'ALL PRIVILEGES'
99 ||
$show_grants_str == 'CREATE'
100 ||
strpos($show_grants_str, 'CREATE,') !== false) {
101 if ($show_grants_dbname == '*') {
102 // a global CREATE privilege
103 $GLOBALS['is_create_db_priv'] = true;
104 $GLOBALS['is_reload_priv'] = true;
105 $GLOBALS['db_to_create'] = '';
106 $GLOBALS['dbs_where_create_table_allowed'][] = '*';
107 // @todo we should not break here, cause GRANT ALL *.*
108 // could be revoked by a later rule like GRANT SELECT ON db.*
111 // this array may contain wildcards
112 $GLOBALS['dbs_where_create_table_allowed'][] = $show_grants_dbname;
114 $dbname_to_test = PMA_backquote($show_grants_dbname);
116 if ($GLOBALS['is_create_db_priv']) {
117 // no need for any more tests if we already know this
121 if ((preg_match('/' . $re0 . '%|_/', $show_grants_dbname)
122 && ! preg_match('/\\\\%|\\\\_/', $show_grants_dbname))
123 // does this db exist?
124 ||
(! PMA_DBI_try_query('USE ' . preg_replace('/' . $re1 . '(%|_)/', '\\1\\3', $dbname_to_test))
125 && substr(PMA_DBI_getError(), 1, 4) != 1044)
127 if ($GLOBALS['cfg']['SuggestDBName']) {
128 $GLOBALS['db_to_create'] = preg_replace('/' . $re0 . '_/', '\\1?', $show_grants_dbname);
129 $GLOBALS['db_to_create'] = preg_replace('/' . $re0 . '%/', '\\1...', $GLOBALS['db_to_create']);
130 $GLOBALS['db_to_create'] = preg_replace('/' . $re1 . '(%|_)/', '\\1\\3', $GLOBALS['db_to_create']);
132 $GLOBALS['is_create_db_priv'] = true;
135 * @todo collect $GLOBALS['db_to_create'] into an array, to display a
136 * drop-down in the "Create new database" dialog
138 // we don't break, we want all possible databases
145 PMA_DBI_free_result($rs_usr);
147 PMA_cacheSet('is_create_db_priv', $GLOBALS['is_create_db_priv'], true);
148 PMA_cacheSet('is_process_priv', $GLOBALS['is_process_priv'], true);
149 PMA_cacheSet('is_reload_priv', $GLOBALS['is_reload_priv'], true);
150 PMA_cacheSet('db_to_create', $GLOBALS['db_to_create'], true);
151 PMA_cacheSet('dbs_where_create_table_allowed', $GLOBALS['dbs_where_create_table_allowed'], true);
154 PMA_analyseShowGrant();