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.
34 function PMA_analyseShowGrant()
36 if (PMA_cacheExists('is_create_db_priv', true)) {
37 $GLOBALS['is_create_db_priv'] = PMA_cacheGet('is_create_db_priv', true);
38 $GLOBALS['is_process_priv'] = PMA_cacheGet('is_process_priv', true);
39 $GLOBALS['is_reload_priv'] = PMA_cacheGet('is_reload_priv', true);
40 $GLOBALS['db_to_create'] = PMA_cacheGet('db_to_create', true);
41 $GLOBALS['dbs_where_create_table_allowed']
42 = PMA_cacheGet('dbs_where_create_table_allowed', true);
47 $GLOBALS['is_create_db_priv'] = false;
48 $GLOBALS['is_process_priv'] = true;
49 $GLOBALS['is_reload_priv'] = false;
50 $GLOBALS['db_to_create'] = '';
51 $GLOBALS['dbs_where_create_table_allowed'] = array();
53 $rs_usr = PMA_DBI_try_query('SHOW GRANTS');
59 $re0 = '(^|(\\\\\\\\)+|[^\\\\])'; // non-escaped wildcards
60 $re1 = '(^|[^\\\\])(\\\)+'; // escaped wildcards
62 while ($row = PMA_DBI_fetch_row($rs_usr)) {
63 // extract db from GRANT ... ON *.* or GRANT ... ON db.*
64 $db_name_offset = strpos($row[0], ' ON ') +
4;
65 $show_grants_dbname = substr($row[0],
67 strpos($row[0], '.', $db_name_offset) - $db_name_offset);
68 $show_grants_dbname = PMA_unQuote($show_grants_dbname, '`');
70 $show_grants_str = substr($row[0], 6, (strpos($row[0], ' ON ') - 6));
71 if ($show_grants_str == 'RELOAD') {
72 $GLOBALS['is_reload_priv'] = true;
76 * @todo if we find CREATE VIEW but not CREATE, do not offer
77 * the create database dialog box
79 if ($show_grants_str == 'ALL'
80 ||
$show_grants_str == 'ALL PRIVILEGES'
81 ||
$show_grants_str == 'CREATE'
82 ||
strpos($show_grants_str, 'CREATE,') !== false) {
83 if ($show_grants_dbname == '*') {
84 // a global CREATE privilege
85 $GLOBALS['is_create_db_priv'] = true;
86 $GLOBALS['is_reload_priv'] = true;
87 $GLOBALS['db_to_create'] = '';
88 $GLOBALS['dbs_where_create_table_allowed'][] = '*';
89 // @todo we should not break here, cause GRANT ALL *.*
90 // could be revoked by a later rule like GRANT SELECT ON db.*
93 // this array may contain wildcards
94 $GLOBALS['dbs_where_create_table_allowed'][] = $show_grants_dbname;
96 $dbname_to_test = PMA_backquote($show_grants_dbname);
98 if ($GLOBALS['is_create_db_priv']) {
99 // no need for any more tests if we already know this
103 if ((preg_match('/' . $re0 . '%|_/', $show_grants_dbname)
104 && ! preg_match('/\\\\%|\\\\_/', $show_grants_dbname))
105 // does this db exist?
106 ||
(! PMA_DBI_try_query('USE ' . preg_replace('/' . $re1 . '(%|_)/', '\\1\\3', $dbname_to_test))
107 && substr(PMA_DBI_getError(), 1, 4) != 1044)
109 if ($GLOBALS['cfg']['SuggestDBName']) {
110 $GLOBALS['db_to_create'] = preg_replace('/' . $re0 . '_/', '\\1?', $show_grants_dbname);
111 $GLOBALS['db_to_create'] = preg_replace('/' . $re0 . '%/', '\\1...', $GLOBALS['db_to_create']);
112 $GLOBALS['db_to_create'] = preg_replace('/' . $re1 . '(%|_)/', '\\1\\3', $GLOBALS['db_to_create']);
114 $GLOBALS['is_create_db_priv'] = true;
117 * @todo collect $GLOBALS['db_to_create'] into an array, to display a
118 * drop-down in the "Create database" dialog
120 // we don't break, we want all possible databases
127 PMA_DBI_free_result($rs_usr);
129 // must also PMA_cacheUnset() them in libraries/auth/cookie.auth.lib.php
130 PMA_cacheSet('is_create_db_priv', $GLOBALS['is_create_db_priv'], true);
131 PMA_cacheSet('is_process_priv', $GLOBALS['is_process_priv'], true);
132 PMA_cacheSet('is_reload_priv', $GLOBALS['is_reload_priv'], true);
133 PMA_cacheSet('db_to_create', $GLOBALS['db_to_create'], true);
134 PMA_cacheSet('dbs_where_create_table_allowed', $GLOBALS['dbs_where_create_table_allowed'], true);
138 PMA_analyseShowGrant();
140 // todo: for simple_user_policy only database with user's login can be created (unless logged in as root)
141 $GLOBALS['is_create_db_priv'] = $GLOBALS['is_superuser'];
142 $GLOBALS['is_process_priv'] = false;
143 $GLOBALS['is_reload_priv'] = false;
144 $GLOBALS['db_to_create'] = '';
145 $GLOBALS['dbs_where_create_table_allowed'] = array('*');