Merge branch 'QA_3_3'
[phpmyadmin/dkf.git] / libraries / check_user_privileges.lib.php
blob398101bf67ea891a3a9ece06d7dc5a3cca14fbb7
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 * @version $Id$
7 * @package phpMyAdmin
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 $GLOBALS['is_superuser'] = PMA_isSuperuser();
18 /**
19 * sets privilege information extracted from SHOW GRANTS result
21 * Detection for some CREATE privilege.
23 * Since MySQL 4.1.2, we can easily detect current user's grants using $userlink
24 * (no control user needed) and we don't have to try any other method for
25 * detection
27 * @todo fix to get really all privileges, not only explicitly defined for this user
28 * from MySQL manual: (http://dev.mysql.com/doc/refman/5.0/en/show-grants.html)
29 * SHOW GRANTS displays only the privileges granted explicitly to the named
30 * account. Other privileges might be available to the account, but they are not
31 * displayed. For example, if an anonymous account exists, the named account
32 * might be able to use its privileges, but SHOW GRANTS will not display them.
34 * @uses $_SESSION['is_create_db_priv'] for caching
35 * @uses $_SESSION['is_process_priv'] for caching
36 * @uses $_SESSION['is_reload_priv'] for caching
37 * @uses $_SESSION['db_to_create'] for caching
38 * @uses $_SESSION['dbs_where_create_table_allowed'] for caching
39 * @uses $GLOBALS['is_create_db_priv'] to set it
40 * @uses $GLOBALS['is_process_priv'] to set it
41 * @uses $GLOBALS['is_reload_priv'] to set it
42 * @uses $GLOBALS['db_to_create'] to set it
43 * @uses $GLOBALS['dbs_where_create_table_allowed'] to set it
44 * @uses $GLOBALS['server']
45 * @uses PMA_DBI_try_query()
46 * @uses PMA_DBI_fetch_row()
47 * @uses PMA_DBI_free_result()
48 * @uses PMA_DBI_getError()
49 * @uses PMA_unQuote()
50 * @uses PMA_backquote()
51 * @uses preg_match()
52 * @uses preg_replace()
53 * @uses substr()
54 * @uses strpos()
56 function PMA_analyseShowGrant()
58 if (PMA_cacheExists('is_create_db_priv', true)) {
59 $GLOBALS['is_create_db_priv'] = PMA_cacheGet('is_create_db_priv', true);
60 $GLOBALS['is_process_priv'] = PMA_cacheGet('is_process_priv', true);
61 $GLOBALS['is_reload_priv'] = PMA_cacheGet('is_reload_priv', true);
62 $GLOBALS['db_to_create'] = PMA_cacheGet('db_to_create', true);
63 $GLOBALS['dbs_where_create_table_allowed']
64 = PMA_cacheGet('dbs_where_create_table_allowed', true);
65 return;
68 // defaults
69 $GLOBALS['is_create_db_priv'] = false;
70 $GLOBALS['is_process_priv'] = true;
71 $GLOBALS['is_reload_priv'] = false;
72 $GLOBALS['db_to_create'] = '';
73 $GLOBALS['dbs_where_create_table_allowed'] = array();
75 $rs_usr = PMA_DBI_try_query('SHOW GRANTS');
77 if (! $rs_usr) {
78 return;
81 $re0 = '(^|(\\\\\\\\)+|[^\\\\])'; // non-escaped wildcards
82 $re1 = '(^|[^\\\\])(\\\)+'; // escaped wildcards
84 while ($row = PMA_DBI_fetch_row($rs_usr)) {
85 // extract db from GRANT ... ON *.* or GRANT ... ON db.*
86 $db_name_offset = strpos($row[0], ' ON ') + 4;
87 $show_grants_dbname = substr($row[0],
88 $db_name_offset,
89 strpos($row[0], '.', $db_name_offset) - $db_name_offset);
90 $show_grants_dbname = PMA_unQuote($show_grants_dbname, '`');
92 $show_grants_str = substr($row[0], 6, (strpos($row[0], ' ON ') - 6));
93 if ($show_grants_str == 'RELOAD') {
94 $GLOBALS['is_reload_priv'] = true;
97 /**
98 * @todo if we find CREATE VIEW but not CREATE, do not offer
99 * the create database dialog box
101 if ($show_grants_str == 'ALL'
102 || $show_grants_str == 'ALL PRIVILEGES'
103 || $show_grants_str == 'CREATE'
104 || strpos($show_grants_str, 'CREATE,') !== false) {
105 if ($show_grants_dbname == '*') {
106 // a global CREATE privilege
107 $GLOBALS['is_create_db_priv'] = true;
108 $GLOBALS['is_reload_priv'] = true;
109 $GLOBALS['db_to_create'] = '';
110 $GLOBALS['dbs_where_create_table_allowed'][] = '*';
111 // @todo we should not break here, cause GRANT ALL *.*
112 // could be revoked by a later rule like GRANT SELECT ON db.*
113 break;
114 } else {
115 // this array may contain wildcards
116 $GLOBALS['dbs_where_create_table_allowed'][] = $show_grants_dbname;
118 $dbname_to_test = PMA_backquote($show_grants_dbname);
120 if ($GLOBALS['is_create_db_priv']) {
121 // no need for any more tests if we already know this
122 continue;
125 if ((preg_match('/' . $re0 . '%|_/', $show_grants_dbname)
126 && ! preg_match('/\\\\%|\\\\_/', $show_grants_dbname))
127 // does this db exist?
128 || (! PMA_DBI_try_query('USE ' . preg_replace('/' . $re1 . '(%|_)/', '\\1\\3', $dbname_to_test))
129 && substr(PMA_DBI_getError(), 1, 4) != 1044)
131 if ($GLOBALS['cfg']['SuggestDBName']) {
132 $GLOBALS['db_to_create'] = preg_replace('/' . $re0 . '_/', '\\1?', $show_grants_dbname);
133 $GLOBALS['db_to_create'] = preg_replace('/' . $re0 . '%/', '\\1...', $GLOBALS['db_to_create']);
134 $GLOBALS['db_to_create'] = preg_replace('/' . $re1 . '(%|_)/', '\\1\\3', $GLOBALS['db_to_create']);
136 $GLOBALS['is_create_db_priv'] = true;
139 * @todo collect $GLOBALS['db_to_create'] into an array, to display a
140 * drop-down in the "Create new database" dialog
142 // we don't break, we want all possible databases
143 //break;
144 } // end if
145 } // end elseif
146 } // end if
147 } // end while
149 PMA_DBI_free_result($rs_usr);
151 // must also PMA_cacheUnset() them in libraries/auth/cookie.auth.lib.php
152 PMA_cacheSet('is_create_db_priv', $GLOBALS['is_create_db_priv'], true);
153 PMA_cacheSet('is_process_priv', $GLOBALS['is_process_priv'], true);
154 PMA_cacheSet('is_reload_priv', $GLOBALS['is_reload_priv'], true);
155 PMA_cacheSet('db_to_create', $GLOBALS['db_to_create'], true);
156 PMA_cacheSet('dbs_where_create_table_allowed', $GLOBALS['dbs_where_create_table_allowed'], true);
157 } // end function
159 PMA_analyseShowGrant();