2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Get user's global privileges and some db-specific privileges
8 if (! defined('PHPMYADMIN')) {
15 $GLOBALS['is_superuser'] = PMA_isSuperuser();
18 * sets privilege information extracted from SHOW GRANTS result
20 * Detection for some CREATE privilege.
22 * Since MySQL 4.1.2, we can easily detect current user's grants using $userlink
23 * (no control user needed) and we don't have to try any other method for
26 * @todo fix to get really all privileges, not only explicitly defined for this user
27 * from MySQL manual: (http://dev.mysql.com/doc/refman/5.0/en/show-grants.html)
28 * SHOW GRANTS displays only the privileges granted explicitly to the named
29 * account. Other privileges might be available to the account, but they are not
30 * displayed. For example, if an anonymous account exists, the named account
31 * might be able to use its privileges, but SHOW GRANTS will not display them.
35 function PMA_analyseShowGrant()
37 if (PMA_Util
::cacheExists('is_create_db_priv', true)) {
38 $GLOBALS['is_create_db_priv'] = PMA_Util
::cacheGet('is_create_db_priv', true);
39 $GLOBALS['is_process_priv'] = PMA_Util
::cacheGet('is_process_priv', true);
40 $GLOBALS['is_reload_priv'] = PMA_Util
::cacheGet('is_reload_priv', true);
41 $GLOBALS['db_to_create'] = PMA_Util
::cacheGet('db_to_create', true);
42 $GLOBALS['dbs_where_create_table_allowed']
43 = PMA_Util
::cacheGet('dbs_where_create_table_allowed', true);
48 $GLOBALS['is_create_db_priv'] = false;
49 $GLOBALS['is_process_priv'] = true;
50 $GLOBALS['is_reload_priv'] = false;
51 $GLOBALS['db_to_create'] = '';
52 $GLOBALS['dbs_where_create_table_allowed'] = array();
54 $rs_usr = PMA_DBI_try_query('SHOW GRANTS');
60 $re0 = '(^|(\\\\\\\\)+|[^\\\\])'; // non-escaped wildcards
61 $re1 = '(^|[^\\\\])(\\\)+'; // escaped wildcards
63 while ($row = PMA_DBI_fetch_row($rs_usr)) {
64 // extract db from GRANT ... ON *.* or GRANT ... ON db.*
65 $db_name_offset = strpos($row[0], ' ON ') +
4;
66 $show_grants_dbname = substr(
67 $row[0], $db_name_offset,
68 strpos($row[0], '.', $db_name_offset) - $db_name_offset
71 = PMA_Util
::unQuote($show_grants_dbname, '`');
73 $show_grants_str = substr($row[0], 6, (strpos($row[0], ' ON ') - 6));
74 if ($show_grants_str == 'RELOAD') {
75 $GLOBALS['is_reload_priv'] = true;
79 * @todo if we find CREATE VIEW but not CREATE, do not offer
80 * the create database dialog box
82 if ($show_grants_str == 'ALL'
83 ||
$show_grants_str == 'ALL PRIVILEGES'
84 ||
$show_grants_str == 'CREATE'
85 ||
strpos($show_grants_str, 'CREATE,') !== false
87 if ($show_grants_dbname == '*') {
88 // a global CREATE privilege
89 $GLOBALS['is_create_db_priv'] = true;
90 $GLOBALS['is_reload_priv'] = true;
91 $GLOBALS['db_to_create'] = '';
92 $GLOBALS['dbs_where_create_table_allowed'][] = '*';
93 // @todo we should not break here, cause GRANT ALL *.*
94 // could be revoked by a later rule like GRANT SELECT ON db.*
97 // this array may contain wildcards
98 $GLOBALS['dbs_where_create_table_allowed'][] = $show_grants_dbname;
100 $dbname_to_test = PMA_Util
::backquote($show_grants_dbname);
102 if ($GLOBALS['is_create_db_priv']) {
103 // no need for any more tests if we already know this
107 // does this db exist?
108 if ((preg_match('/' . $re0 . '%|_/', $show_grants_dbname)
109 && ! preg_match('/\\\\%|\\\\_/', $show_grants_dbname))
110 ||
(! PMA_DBI_try_query('USE ' . preg_replace('/' . $re1 . '(%|_)/', '\\1\\3', $dbname_to_test))
111 && substr(PMA_DBI_getError(), 1, 4) != 1044)
114 * Do not handle the underscore wildcard
115 * (this case must be rare anyway)
117 $GLOBALS['db_to_create'] = preg_replace(
118 '/' . $re0 . '%/', '\\1...',
121 $GLOBALS['db_to_create'] = preg_replace(
122 '/' . $re1 . '(%|_)/', '\\1\\3',
123 $GLOBALS['db_to_create']
125 $GLOBALS['is_create_db_priv'] = true;
128 * @todo collect $GLOBALS['db_to_create'] into an array,
129 * to display a drop-down in the "Create database" dialog
131 // we don't break, we want all possible databases
138 PMA_DBI_free_result($rs_usr);
140 // must also cacheUnset() them in
141 // libraries/plugins/auth/AuthenticationCookie.class.php
142 PMA_Util
::cacheSet('is_create_db_priv', $GLOBALS['is_create_db_priv'], true);
143 PMA_Util
::cacheSet('is_process_priv', $GLOBALS['is_process_priv'], true);
144 PMA_Util
::cacheSet('is_reload_priv', $GLOBALS['is_reload_priv'], true);
145 PMA_Util
::cacheSet('db_to_create', $GLOBALS['db_to_create'], true);
147 'dbs_where_create_table_allowed',
148 $GLOBALS['dbs_where_create_table_allowed'],
154 PMA_analyseShowGrant();
156 // todo: for simple_user_policy only database with user's login can be created
157 // (unless logged in as root)
158 $GLOBALS['is_create_db_priv'] = $GLOBALS['is_superuser'];
159 $GLOBALS['is_process_priv'] = false;
160 $GLOBALS['is_reload_priv'] = false;
161 $GLOBALS['db_to_create'] = '';
162 $GLOBALS['dbs_where_create_table_allowed'] = array('*');