Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / check_user_privileges.lib.php
blobaba20d7ddb1710e363fdab725e7f8ca823aeedc1
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 * @return void
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);
44 return;
47 // defaults
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');
56 if (! $rs_usr) {
57 return;
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
70 $show_grants_dbname
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;
78 /**
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
86 ) {
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.*
95 break;
96 } else {
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
104 continue;
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...',
119 $show_grants_dbname
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
132 //break;
133 } // end if
134 } // end elseif
135 } // end if
136 } // end while
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);
146 PMA_Util::cacheSet(
147 'dbs_where_create_table_allowed',
148 $GLOBALS['dbs_where_create_table_allowed'],
149 true
151 } // end function
153 if (!PMA_DRIZZLE) {
154 PMA_analyseShowGrant();
155 } else {
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('*');