Cannot create a table in information_schema
[phpmyadmin/ammaryasirr.git] / libraries / check_user_privileges.lib.php
blob6dbc251e450eedc9a10f7f87d8573640cd113be9
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Get user's global privileges and some db-specific privileges
6 * @package phpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 $GLOBALS['is_superuser'] = PMA_isSuperuser();
17 /**
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
24 * detection
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.
33 * @uses $_SESSION['is_create_db_priv'] for caching
34 * @uses $_SESSION['is_process_priv'] for caching
35 * @uses $_SESSION['is_reload_priv'] for caching
36 * @uses $_SESSION['db_to_create'] for caching
37 * @uses $_SESSION['dbs_where_create_table_allowed'] for caching
38 * @uses $GLOBALS['is_create_db_priv'] to set it
39 * @uses $GLOBALS['is_process_priv'] to set it
40 * @uses $GLOBALS['is_reload_priv'] to set it
41 * @uses $GLOBALS['db_to_create'] to set it
42 * @uses $GLOBALS['dbs_where_create_table_allowed'] to set it
43 * @uses $GLOBALS['server']
44 * @uses PMA_DBI_try_query()
45 * @uses PMA_DBI_fetch_row()
46 * @uses PMA_DBI_free_result()
47 * @uses PMA_DBI_getError()
48 * @uses PMA_unQuote()
49 * @uses PMA_backquote()
50 * @uses preg_match()
51 * @uses preg_replace()
52 * @uses substr()
53 * @uses strpos()
55 function PMA_analyseShowGrant()
57 if (PMA_cacheExists('is_create_db_priv', true)) {
58 $GLOBALS['is_create_db_priv'] = PMA_cacheGet('is_create_db_priv', true);
59 $GLOBALS['is_process_priv'] = PMA_cacheGet('is_process_priv', true);
60 $GLOBALS['is_reload_priv'] = PMA_cacheGet('is_reload_priv', true);
61 $GLOBALS['db_to_create'] = PMA_cacheGet('db_to_create', true);
62 $GLOBALS['dbs_where_create_table_allowed']
63 = PMA_cacheGet('dbs_where_create_table_allowed', true);
64 return;
67 // defaults
68 $GLOBALS['is_create_db_priv'] = false;
69 $GLOBALS['is_process_priv'] = true;
70 $GLOBALS['is_reload_priv'] = false;
71 $GLOBALS['db_to_create'] = '';
72 $GLOBALS['dbs_where_create_table_allowed'] = array();
74 $rs_usr = PMA_DBI_try_query('SHOW GRANTS');
76 if (! $rs_usr) {
77 return;
80 $re0 = '(^|(\\\\\\\\)+|[^\\\\])'; // non-escaped wildcards
81 $re1 = '(^|[^\\\\])(\\\)+'; // escaped wildcards
83 while ($row = PMA_DBI_fetch_row($rs_usr)) {
84 // extract db from GRANT ... ON *.* or GRANT ... ON db.*
85 $db_name_offset = strpos($row[0], ' ON ') + 4;
86 $show_grants_dbname = substr($row[0],
87 $db_name_offset,
88 strpos($row[0], '.', $db_name_offset) - $db_name_offset);
89 $show_grants_dbname = PMA_unQuote($show_grants_dbname, '`');
91 $show_grants_str = substr($row[0], 6, (strpos($row[0], ' ON ') - 6));
92 if ($show_grants_str == 'RELOAD') {
93 $GLOBALS['is_reload_priv'] = true;
96 /**
97 * @todo if we find CREATE VIEW but not CREATE, do not offer
98 * the create database dialog box
100 if ($show_grants_str == 'ALL'
101 || $show_grants_str == 'ALL PRIVILEGES'
102 || $show_grants_str == 'CREATE'
103 || strpos($show_grants_str, 'CREATE,') !== false) {
104 if ($show_grants_dbname == '*') {
105 // a global CREATE privilege
106 $GLOBALS['is_create_db_priv'] = true;
107 $GLOBALS['is_reload_priv'] = true;
108 $GLOBALS['db_to_create'] = '';
109 $GLOBALS['dbs_where_create_table_allowed'][] = '*';
110 // @todo we should not break here, cause GRANT ALL *.*
111 // could be revoked by a later rule like GRANT SELECT ON db.*
112 break;
113 } else {
114 // this array may contain wildcards
115 $GLOBALS['dbs_where_create_table_allowed'][] = $show_grants_dbname;
117 $dbname_to_test = PMA_backquote($show_grants_dbname);
119 if ($GLOBALS['is_create_db_priv']) {
120 // no need for any more tests if we already know this
121 continue;
124 if ((preg_match('/' . $re0 . '%|_/', $show_grants_dbname)
125 && ! preg_match('/\\\\%|\\\\_/', $show_grants_dbname))
126 // does this db exist?
127 || (! PMA_DBI_try_query('USE ' . preg_replace('/' . $re1 . '(%|_)/', '\\1\\3', $dbname_to_test))
128 && substr(PMA_DBI_getError(), 1, 4) != 1044)
130 if ($GLOBALS['cfg']['SuggestDBName']) {
131 $GLOBALS['db_to_create'] = preg_replace('/' . $re0 . '_/', '\\1?', $show_grants_dbname);
132 $GLOBALS['db_to_create'] = preg_replace('/' . $re0 . '%/', '\\1...', $GLOBALS['db_to_create']);
133 $GLOBALS['db_to_create'] = preg_replace('/' . $re1 . '(%|_)/', '\\1\\3', $GLOBALS['db_to_create']);
135 $GLOBALS['is_create_db_priv'] = true;
138 * @todo collect $GLOBALS['db_to_create'] into an array, to display a
139 * drop-down in the "Create new database" dialog
141 // we don't break, we want all possible databases
142 //break;
143 } // end if
144 } // end elseif
145 } // end if
146 } // end while
148 PMA_DBI_free_result($rs_usr);
150 // must also PMA_cacheUnset() them in libraries/auth/cookie.auth.lib.php
151 PMA_cacheSet('is_create_db_priv', $GLOBALS['is_create_db_priv'], true);
152 PMA_cacheSet('is_process_priv', $GLOBALS['is_process_priv'], true);
153 PMA_cacheSet('is_reload_priv', $GLOBALS['is_reload_priv'], true);
154 PMA_cacheSet('db_to_create', $GLOBALS['db_to_create'], true);
155 PMA_cacheSet('dbs_where_create_table_allowed', $GLOBALS['dbs_where_create_table_allowed'], true);
156 } // end function
158 PMA_analyseShowGrant();