PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.82
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.82
5.5.84 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 All 160 releases
wp-data-access / WPDataAccess / Utilities / WPDA_Import.php

WPDA_Import.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.82, at WPDataAccess/Utilities/WPDA_Import.php

263 lines 9.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\Utilities
7 */
8
9 namespace WPDataAccess\Utilities {
10
11 use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Exist;
12 use WPDataAccess\List_Table\WPDA_List_Table;
13 use WPDataAccess\WPDA;
14
15 /**
16 * Class WPDA_Import
17 *
18 * Imports a script file that contains exactly one insert into statement (can insert multiple records). Only
19 * insert statements are allowed. Insert is only allowed into the table name provided in constructor. Subqueries
20 * are not allowed (checked with explain).
21 *
22 * @author Peter Schulz
23 * @since 1.0.0
24 */
25 class WPDA_Import {
26
27 /**
28 * URL where to post data
29 *
30 * @var string
31 */
32 protected $url;
33
34 /**
35 * Database schema name
36 *
37 * @var string
38 */
39 protected $schema_name;
40
41 /**
42 * Database table name
43 *
44 * @var string
45 */
46 protected $table_name;
47
48 /**
49 * Indicates where imports are allowed
50 *
51 * @var string 'on' or 'off'
52 */
53 protected $allow_imports;
54
55 /**
56 * WPDA_Import constructor
57 *
58 * Checks if imports are allowed. Throws an exception if imports are not allowed.
59 *
60 * @param string $page Page where to post data (url).
61 * @param string $schema_name Database schema name.
62 * @param string $table_name Database table name.
63 *
64 * @throws \Exception Throws exception if export is disabled.
65 * @since 1.0.0
66 */
67 public function __construct( $page, $schema_name, $table_name ) {
68
69 if ( ! WPDA::is_wpda_table( $table_name ) ) {
70 // Check access rights for non WPDA tables.
71 if ( 'on' !== WPDA::get_option( WPDA::OPTION_BE_ALLOW_IMPORTS ) ) {
72 // Prevent import object being created: exception must be handled in calling method.
73 throw new \Exception( esc_attr__( 'ERROR: Not authorized', 'wp-data-access' ) );
74 }
75 // Disable import for views.
76 $wpda_dictionary_exists = new WPDA_Dictionary_Exist( $schema_name, $table_name );
77 if ( $wpda_dictionary_exists->is_view() ) {
78 // Prevent import object being created: exception must be handled in calling method.
79 throw new \Exception( esc_attr__( 'ERROR: Import not possible on views', 'wp-data-access' ) );
80 }
81 }
82
83 $this->url = $page;
84 $this->schema_name = $schema_name;
85 $this->table_name = $table_name;
86
87 }
88
89 /**
90 * Checks if request is valid and allowed
91 *
92 * If the requested import is valid and allowed, the import file is loaded and its content imported.
93 *
94 * @since 1.0.0
95 */
96 public function check_post() {
97
98 // Check if import was requested.
99 // Import is not possible for WPDA_List_Table::LIST_BASE_TABLE (view in mysql information_schema).
100 if ( WPDA_List_Table::LIST_BASE_TABLE !== $this->table_name &&
101 isset( $_REQUEST['action'] ) && 'import' === sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) // input var okay.
102 ) {
103 // Security check.
104 $wp_nonce = isset( $_REQUEST['_wpnonceimport'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonceimport'] ) ) : '?'; // input var okay.
105 if ( ! wp_verify_nonce( $wp_nonce, "wpda-import-{$this->table_name}" ) ) {
106 wp_die( esc_attr__( 'ERROR: Not authorized', 'wp-data-access' ) );
107 }
108
109 if ( isset( $_FILES['filename'] ) ) {
110 // phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotValidated
111 $temp_file_name = sanitize_text_field( $_FILES['filename']['tmp_name'] ); // For Windows: do NOT unslash!
112 // phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotValidated
113 if ( UPLOAD_ERR_OK === $_FILES['filename']['error'] && is_uploaded_file( $temp_file_name ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
114 // Get file content.
115 $wpda_import = new WPDA_Import_File( $temp_file_name );
116
117 // Check if errors should be shown.
118 $hide_errors = isset( $_REQUEST['hide_errors'] ) ?
119 sanitize_text_field( wp_unslash( $_REQUEST['hide_errors'] ) ) : 'off'; // input var okay.
120
121 // Process file content.
122 $wpda_import->import( $this->schema_name, $this->table_name, $hide_errors );
123 }
124 } else {
125 // File upload failed: inform user.
126 $msg = new WPDA_Message_Box(
127 array(
128 'message_text' => __( 'File upload failed', 'wp-data-access' ),
129 'message_type' => 'error',
130 'message_is_dismissible' => false,
131 )
132 );
133 $msg->box();
134 }
135 }
136
137 }
138
139 /**
140 * Adds an import button
141 *
142 * @param string $label Button label.
143 * @param string $class Button CSS class.
144 *
145 * @since 1.0.0
146 */
147 public function add_button( $label = '', $class = 'page-title-action' ) {
148 $storage_type =
149 WPDA::is_wpda_table( $this->table_name ) ?
150 __( 'current respository table', 'wp-data-access' ) :
151 __( 'table', 'wp-data-access' ) . " {$this->table_name}";
152 /* translators: %s = storage type */
153 $title = sprintf( __( 'Allows only imports into %s', 'wp-data-access' ), esc_attr( $storage_type ) );
154 ?>
155 <button type="button"
156 onclick="jQuery('#upload_file_container').show()"
157 class="wpda_tooltip <?php echo esc_attr( $class ); ?>"
158 title="<?php echo esc_attr( $title ); ?>">
159 <i class="fas fa-cloud-upload wpda_icon_on_button"></i>
160 <?php echo '' === $label ? esc_attr__( 'Import', 'wp-data-access' ) : esc_attr( $label ); ?>
161 </button>
162 <?php
163 }
164
165 /**
166 * Adds an import container
167 *
168 * The container contains an upload form. The container is hidden by default. When the button created in
169 * {@see WPDA_Import::add_button()} is clicked, the container is shown.
170 *
171 * @since 1.0.0
172 */
173 public function add_container() {
174 $file_uploads_enabled = @ini_get( 'file_uploads' );
175 ?>
176 <script type='text/javascript'>
177 function before_submit_upload() {
178 if (jQuery('#filename').val() == '') {
179 alert('<?php esc_html_e( 'No file to import!', 'wp-data-access' ); ?>');
180 return false;
181 }
182 if (!(jQuery('#filename')[0].files[0].size < <?php echo esc_attr( WPDA::convert_memory_to_decimal( @ini_get( 'upload_max_filesize' ) ) ); ?>)) {
183 alert("<?php esc_html_e( 'File exceeds maximum size of', 'wp-data-access' ); ?> <?php echo esc_attr( @ini_get( 'upload_max_filesize' ) ); ?>!");
184 return false;
185 }
186 }
187 </script>
188
189 <div id="upload_file_container" style="display: none">
190 <div>&nbsp;</div>
191 <div>
192 <?php if ( $file_uploads_enabled ) { ?>
193 <form id="form_import_table" method="post" action="<?php echo esc_attr( $this->url ); ?>"
194 enctype="multipart/form-data">
195 <fieldset class="wpda_fieldset" style="position:relative;padding:20px;padding-top:10px;padding-bottom:10px">
196 <legend>
197 <span>
198 <?php /* translators: %s = table name */ echo sprintf( esc_attr__( 'SUPPORTS ONLY DATA IMPORTS FOR TABLE `%s`', 'wp-data-access' ), esc_attr( $this->table_name ) ); ?>
199 </span>
200 </legend>
201 <p>
202 <?php
203 esc_html_e( 'Supports only file type', 'wp-data-access' ) . ' <strong>sql</strong>. ' . __( 'Maximum supported file size is', 'wp-data-access' ) . ' <strong>' . esc_attr( @ini_get( 'upload_max_filesize' ) ) . '</strong>.';
204 ?>
205 </p>
206 <input type="file" name="filename" id="filename" class="wpda_tooltip" accept=".sql">
207 <label style="vertical-align:baseline;">
208 <input type="checkbox" name="hide_errors" style="vertical-align:sub;" checked>
209 <?php esc_html_e( 'Hide errors', 'wp-data-access' ); ?>
210 </label>
211 <p>
212 <button type="submit"
213 class="button button-primary"
214 onclick="return before_submit_upload()">
215 <i class="fas fa-code wpda_icon_on_button"></i>
216 <?php esc_html_e( 'Import file', 'wp-data-access' ); ?>
217 </button>
218 <button type="button"
219 onclick="jQuery('#upload_file_container').hide()"
220 class="button button-secondary">
221 <i class="fas fa-times-circle wpda_icon_on_button"></i>
222 <?php esc_html_e( 'Cancel', 'wp-data-access' ); ?>
223 </button>
224 </p>
225 <input type="hidden" name="wpdaschema_name" value="<?php echo esc_attr( $this->schema_name ); ?>">
226 <input type="hidden" name="table_name" value="<?php echo esc_attr( $this->table_name ); ?>">
227 <input type="hidden" name="action" value="import">
228 <?php wp_nonce_field( "wpda-import-{$this->table_name}", '_wpnonceimport', false ); ?>
229 </fieldset>
230 </form>
231 <?php } else { ?>
232 <p>
233 <strong><?php esc_html_e( 'ERROR', 'wp-data-access' ); ?></strong>
234 </p>
235 <p class="wpda_list_indent">
236 <?php
237 esc_html_e( 'Your configuration does not allow file uploads!', 'wp-data-access' );
238 echo ' ';
239 esc_html_e( 'Set', 'wp-data-access' );
240 echo ' <strong>';
241 esc_html_e( 'file_uploads', 'wp-data-access' );
242 echo '</strong> ';
243 esc_html_e( 'to', 'wp-data-access' );
244 echo ' <strong>';
245 esc_html_e( 'On', 'wp-data-access' );
246 echo '</strong> (<a href="https://docs.wpdataaccess.com/limitations.html">';
247 esc_html_e( 'see documentation', 'wp-data-access' );
248 echo '</a>).';
249 ?>
250 </p>
251 <?php } ?>
252 </div>
253 <div>&nbsp;</div>
254 </div>
255
256 <?php
257
258 }
259
260 }
261
262 }
263