PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.2
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.2
5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 5.5.43 All 159 releases
wp-data-access / WPDataAccess / Data_Dictionary / WPDA_Dictionary_Access.php

WPDA_Dictionary_Access.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.2, at WPDataAccess/Data_Dictionary/WPDA_Dictionary_Access.php

234 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Suppress "error - 0 - No summary was found for this file" on phpdoc generation
5 *
6 * @package WPDataAccess\Data_Dictionary
7 */
8
9 namespace WPDataAccess\Data_Dictionary {
10
11 use WPDataAccess\List_Table\WPDA_List_Table;
12 use WPDataAccess\WPDA;
13
14 /**
15 * Class WPDA_Dictionary_Access
16 *
17 * Check if access to a given table is granted. The existence of the table name (and schema name for back-end) is
18 * not checked in this class. The class presumes that the table name (and schema name for back-end) is valid.
19 *
20 * The argument $done, which is used throughout the class, indicates whether the access check confirms the
21 * existence of the table or whether no access is granted anyway and therefor no further checks are needed. In
22 * some situations this saves us a query.
23 *
24 * For example:
25 *
26 * If table $wpdb->options is provided as an argument and access to WordPress tables is allowed we are done. If
27 * only selected tables are allowed and the table provided as an argument is either in or not in the list we done
28 * as well. When calling WPDA_Dictionary_Access functions check the return value as well as $done.
29 *
30 * @author Peter Schulz
31 * @since 1.0.0
32 */
33 class WPDA_Dictionary_Access {
34
35 /**
36 * Check back-end table access
37 *
38 * Checks if access to a given schema and table is granted for back-end usage.
39 *
40 * The schema name must be provided as an argument. This argument is mainly added to support a clean access
41 * check for the data explorer view which uses the view TABLES from MySQL schema INFORMATION_SCHEMA (see
42 * {@see WPDA_List_Table::LIST_BASE_TABLE}). This is the only table/view outside the WordPress schema to
43 * which access is granted.
44 *
45 * @param string $schema_name Schema name in which the table or view is located.
46 * @param string $table_name Table or view name.
47 * @param boolean $done TRUE = no futher checks needed, FALSE = still need to check table name.
48 *
49 * @return bool TRUE = access granted, FALSE = access denied.
50 * @since 1.0.0
51 *
52 * @see WPDA_List_Table::LIST_BASE_TABLE
53 */
54 public static function check_table_access_backend( $schema_name, $table_name, &$done ) {
55 if ( WPDA_List_Table::LIST_BASE_TABLE === $schema_name . '.' . $table_name ) {
56 // Always grant access to table list.
57 $done = true; // No further checks needed.
58
59 return true;
60 }
61
62 global $wpdb;
63 if ( $schema_name === $wpdb->dbname && WPDA::is_wpda_table( $table_name ) ) {
64 // Always grant access to WPDA table's in the back-end.
65 $done = true; // No further checks needed.
66
67 return true;
68 }
69
70 if ( $schema_name === $wpdb->dbname ) {
71 $table_access = WPDA::get_option( WPDA::OPTION_BE_TABLE_ACCESS );
72 $table_access_selected = WPDA::get_option( WPDA::OPTION_BE_TABLE_ACCESS_SELECTED );
73 } else {
74 $table_access = get_option( WPDA::BACKEND_OPTIONNAME_DATABASE_ACCESS . $schema_name );
75 if ( false === $table_access ) {
76 $table_access = 'show';
77 }
78 $table_access_selected = get_option( WPDA::BACKEND_OPTIONNAME_DATABASE_SELECTED . $schema_name );
79 if ( false === $table_access_selected ) {
80 $table_access_selected = '';
81 }
82 }
83
84 return self::check_table_access( $schema_name, $table_name, $table_access, $table_access_selected, $done );
85 }
86
87 /**
88 * Check tables access
89 *
90 * Checks if access to a given table is granted for back-end or front-end usage. Whether the check is performed
91 * for the back-end or front-end depends on the arguments $table_access and $table_access_selected.
92 *
93 * The schema name is not reflected in this check. It presumed that the schema name if the schema in which
94 * WordPress is installed.
95 *
96 * This function is code which is shared between {@see WPDA_Dictionary_Access::check_table_access_backend()}
97 * and {@see WPDA_Dictionary_Access::check_table_access_frontend()}.
98 *
99 * @param string $schema_name Schema name in which the table or view is located.
100 * @param string $table_name Table or view name.
101 * @param string $table_access Option value for table access as stored in wp_options.
102 * @param string $table_access_selected Option value for tables selected access as stored in wp_options.
103 * @param boolean $done TRUE = no futher checks needed, FALSE = still need to check table name.
104 *
105 * @return bool TRUE = access granted, FALSE = access denied.
106 * @see WPDA_Dictionary_Access::check_table_access_frontend()
107 *
108 * @since 1.0.0
109 *
110 * @see WPDA_Dictionary_Access::check_table_access_backend()
111 */
112 protected static function check_table_access( $schema_name, $table_name, $table_access, $table_access_selected, &$done ) {
113 if ( 'hide' === $table_access ) {
114 global $wpdb;
115 if ( $wpdb->dbname !== $schema_name ) {
116 // Non WordPress database: access granted.
117 $done = false; // Still need to check if table exists.
118
119 return true;
120 }
121
122 // No access to WordPress tables: check if the requested table is a WordPress table.
123 $wp_tables = $wpdb->tables( 'all', true );
124 if ( isset( $wp_tables[ substr( $table_name, strlen( $wpdb->prefix ) ) ] ) &&
125 $wp_tables[ substr( $table_name, strlen( $wpdb->prefix ) ) ] === $table_name
126 ) {
127 // WordPress table: deny access.
128 $done = true; // No further checks needed.
129
130 return false;
131 } else {
132 // Non WordPress table: access granted.
133 $done = false; // Still need to check if table exists.
134
135 return true;
136 }
137 } elseif ( 'select' === $table_access ) {
138 // Only access to selected tables and views (front-end settings).
139 if ( '' !== $table_access_selected ) {
140 foreach ( $table_access_selected as $key => $value ) {
141 if ( $table_name === $value ) {
142 // Access to this table or view is granted.
143 $done = true; // No further checks needed.
144
145 return true;
146 }
147 }
148 }
149
150 // No access.
151 $done = true; // No further checks needed.
152
153 return false;
154 } else {
155 // Access granted to all tables and views.
156 $done = false; // Still need to check if table exists.
157
158 return true;
159 }
160 }
161
162 /**
163 * Check front-end table access
164 *
165 * Checks if access to a given table is granted for front-end usage.
166 *
167 * The schema name is not reflected in this check. It presumed that the schema name if the schema in which
168 * WordPress is installed.
169 *
170 * @param string $schema_name Schema name in which the table or view is located.
171 * @param string $table_name Table or view name.
172 * @param boolean $done TRUE = no futher checks needed, FALSE = still need to check table name.
173 *
174 * @return bool TRUE = access granted, FALSE = access denied.
175 * @since 1.0.0
176 */
177 public static function check_table_access_frontend( $schema_name, $table_name, &$done ) {
178 global $wpdb;
179
180 if ( $wpdb->dbname === $schema_name ) {
181 $table_access = WPDA::get_option( WPDA::OPTION_FE_TABLE_ACCESS );
182 $table_access_selected = WPDA::get_option( WPDA::OPTION_FE_TABLE_ACCESS_SELECTED );
183 } else {
184 $table_access = get_option( WPDA::FRONTEND_OPTIONNAME_DATABASE_ACCESS . $schema_name );
185 if ( false === $table_access ) {
186 $table_access = 'select';
187 }
188 $table_access_selected = get_option( WPDA::FRONTEND_OPTIONNAME_DATABASE_SELECTED . $schema_name );
189 if ( false === $table_access_selected ) {
190 $table_access_selected = '';
191 }
192 }
193
194 return self::check_table_access( $schema_name, $table_name, $table_access, $table_access_selected, $done );
195 }
196
197 /**
198 * Check if user has CREATE (database) privilege
199 *
200 * @return bool
201 * @since 2.7.2
202 */
203 public static function can_create_db() {
204 global $wpdb;
205 $wpdb->get_results(
206 "select * from information_schema.user_privileges
207 where privilege_type = 'CREATE'
208 and replace(grantee, '\'', '') = current_user()"
209 );
210
211 return $wpdb->num_rows > 0;
212 }
213
214 /**
215 * Check if user has DROP (database) privilege
216 *
217 * @return bool
218 * @since 2.7.2
219 */
220 public static function can_drop_db() {
221 global $wpdb;
222 $wpdb->get_results(
223 "select * from information_schema.user_privileges
224 where privilege_type = 'DROP'
225 and replace(grantee, '\'', '') = current_user()"
226 );
227
228 return $wpdb->num_rows > 0;
229 }
230
231 }
232
233 }
234