| 1 |
<?php |
| 2 |
/** |
| 3 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 4 |
* |
| 5 |
* @package WPDataAccess\Data_Dictionary |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPDataAccess\Data_Dictionary { |
| 9 |
|
| 10 |
use WPDataAccess\List_Table\WPDA_List_Table; |
| 11 |
use WPDataAccess\WPDA; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class WPDA_Dictionary_Access |
| 15 |
* |
| 16 |
* Check if access to a given table is granted. The existence of the table name (and schema name for back-end) is |
| 17 |
* not checked in this class. The class presumes that the table name (and schema name for back-end) is valid. |
| 18 |
* |
| 19 |
* The argument $done, which is used throughout the class, indicates whether the access check confirms the |
| 20 |
* existence of the table or whether no access is granted anyway and therefor no further checks are needed. In |
| 21 |
* some situations this saves us a query. |
| 22 |
* |
| 23 |
* For example: |
| 24 |
* |
| 25 |
* If table $wpdb->options is provided as an argument and access to WordPress tables is allowed we are done. If |
| 26 |
* only selected tables are allowed and the table provided as an argument is either in or not in the list we done |
| 27 |
* as well. When calling WPDA_Dictionary_Access functions check the return value as well as $done. |
| 28 |
* |
| 29 |
* @package WPDataAccess\Data_Dictionary |
| 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 |
* @since 1.0.0 |
| 46 |
* |
| 47 |
* @see WPDA_List_Table::LIST_BASE_TABLE |
| 48 |
* |
| 49 |
* @param string $schema_name Schema name in which the table or view is located. |
| 50 |
* @param string $table_name Table or view name. |
| 51 |
* @param boolean $done TRUE = no futher checks needed, FALSE = still need to check table name. |
| 52 |
* @return bool TRUE = access granted, FALSE = access denied. |
| 53 |
*/ |
| 54 |
public static function check_table_access_backend( $schema_name, $table_name, &$done ) { |
| 55 |
|
| 56 |
if ( WPDA_List_Table::LIST_BASE_TABLE === $schema_name . '.' . $table_name ) { |
| 57 |
// Always grant access to table list. |
| 58 |
$done = true; // No further checks needed. |
| 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 |
return true; |
| 67 |
} |
| 68 |
|
| 69 |
$table_access = WPDA::get_option( WPDA::OPTION_BE_TABLE_ACCESS ); |
| 70 |
$table_access_selected = WPDA::get_option( WPDA::OPTION_BE_TABLE_ACCESS_SELECTED ); |
| 71 |
|
| 72 |
return WPDA_Dictionary_Access::check_table_access( $table_name, $table_access, $table_access_selected, $done ); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Check tables access |
| 78 |
* |
| 79 |
* Checks if access to a given table is granted for back-end or front-end usage. Whether the check is performed |
| 80 |
* for the back-end or front-end depends on the arguments $table_access and $table_access_selected. |
| 81 |
* |
| 82 |
* The schema name is not reflected in this check. It presumed that the schema name if the schema in which |
| 83 |
* WordPress is installed. |
| 84 |
* |
| 85 |
* This function is code which is shared between {@see WPDA_Dictionary_Access::check_table_access_backend()} |
| 86 |
* and {@see WPDA_Dictionary_Access::check_table_access_frontend()}. |
| 87 |
* |
| 88 |
* @since 1.0.0 |
| 89 |
* |
| 90 |
* @see WPDA_Dictionary_Access::check_table_access_backend() |
| 91 |
* @see WPDA_Dictionary_Access::check_table_access_frontend() |
| 92 |
* |
| 93 |
* @param string $table_name Table or view name. |
| 94 |
* @param string $table_access Option value for table access as stored in wp_options. |
| 95 |
* @param string $table_access_selected Option value for tables selected access as stored in wp_options. |
| 96 |
* @param boolean $done TRUE = no futher checks needed, FALSE = still need to check table name. |
| 97 |
* @return bool TRUE = access granted, FALSE = access denied. |
| 98 |
*/ |
| 99 |
protected static function check_table_access( $table_name, $table_access, $table_access_selected, &$done ) { |
| 100 |
|
| 101 |
if ( 'hide' === $table_access ) { |
| 102 |
|
| 103 |
global $wpdb; |
| 104 |
|
| 105 |
// No access to WordPress tables: check if the requested table is a WordPress table. |
| 106 |
$wp_tables = $wpdb->tables( 'all', true ); |
| 107 |
if ( isset( $wp_tables[ substr( $table_name, strlen( $wpdb->prefix ) ) ] ) && |
| 108 |
$wp_tables[ substr( $table_name, strlen( $wpdb->prefix ) ) ] === $table_name |
| 109 |
) { |
| 110 |
|
| 111 |
// WordPress table: deny access. |
| 112 |
$done = true; // No further checks needed. |
| 113 |
return false; |
| 114 |
|
| 115 |
} else { |
| 116 |
|
| 117 |
// Non WordPress table: access granted. |
| 118 |
$done = false; // Still need to check if table exists. |
| 119 |
return true; |
| 120 |
|
| 121 |
} |
| 122 |
} elseif ( 'select' === $table_access ) { |
| 123 |
|
| 124 |
// Only access to selected tables and views (front-end settings). |
| 125 |
if ( '' !== $table_access_selected ) { |
| 126 |
foreach ( $table_access_selected as $key => $value ) { |
| 127 |
if ( $table_name === $value ) { |
| 128 |
// Access to this table or view is granted. |
| 129 |
$done = true; // No further checks needed. |
| 130 |
return true; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// No access. |
| 136 |
$done = true; // No further checks needed. |
| 137 |
return false; |
| 138 |
|
| 139 |
} else { |
| 140 |
|
| 141 |
// Access granted to all tables and views. |
| 142 |
$done = false; // Still need to check if table exists. |
| 143 |
return true; |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Check front-end table access |
| 151 |
* |
| 152 |
* Checks if access to a given table is granted for front-end usage. |
| 153 |
* |
| 154 |
* The schema name is not reflected in this check. It presumed that the schema name if the schema in which |
| 155 |
* WordPress is installed. |
| 156 |
* |
| 157 |
* @since 1.0.0 |
| 158 |
* |
| 159 |
* @param string $table_name Table or view name. |
| 160 |
* @param boolean $done TRUE = no futher checks needed, FALSE = still need to check table name. |
| 161 |
* @return bool TRUE = access granted, FALSE = access denied. |
| 162 |
*/ |
| 163 |
public static function check_table_access_frontend( $table_name, &$done ) { |
| 164 |
|
| 165 |
$table_access = WPDA::get_option( WPDA::OPTION_FE_TABLE_ACCESS ); |
| 166 |
$table_access_selected = WPDA::get_option( WPDA::OPTION_FE_TABLE_ACCESS_SELECTED ); |
| 167 |
|
| 168 |
return WPDA_Dictionary_Access::check_table_access( $table_name, $table_access, $table_access_selected, $done ); |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
|