PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.85
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.85
5.5.85 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 All 161 releases
← All changes | WPDataAccess/CSV_Files/WPDA_CSV_Import.php +127 -75 5.5.34 → 5.5.85 View file →
@@ -1,5 +1,5 @@
1 -<?php // phpcs:ignore Standard.Category.SniffName.ErrorCode
1 +<?php
2 2
3 3 namespace WPDataAccess\CSV_Files {
4 4
5 5 use WPDataAccess\Connection\WPDADB;
@@ -28,8 +28,10 @@
28 28 * @var null
29 29 */
30 30 protected $action = null;
31 31
32 + protected $file_pointer;
33 +
32 34 /**
33 35 * Constructor
34 36 */
35 37 public function __construct() {
@@ -68,9 +70,9 @@
68 70 ></a>
69 71 <?php
70 72 }
71 73 ?>
72 - <?php echo __( 'Import CSV ' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
74 + <?php esc_html_e( 'Import CSV', 'wp-data-access' ); ?>
73 75 </h1>
74 76 <?php $this->show_body(); ?>
75 77 </div>
76 78 <?php
@@ -134,60 +136,93 @@
134 136 *
135 137 * @return void
136 138 */
137 139 protected function show_body_upload() {
138 - $csv_id =
140 + $csv_id =
139 141 isset( $_REQUEST['csv_id'] ) ? // phpcs:ignore WordPress.Security.NonceVerification
140 142 sanitize_text_field( wp_unslash( $_REQUEST['csv_id'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
141 - $csv_name =
143 + $csv_name =
142 144 isset( $_REQUEST['csv_name'] ) ? // phpcs:ignore WordPress.Security.NonceVerification
143 145 sanitize_text_field( wp_unslash( $_REQUEST['csv_name'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
144 - ?>
145 - <br/>
146 + $csv_encoding =
147 + isset( $_REQUEST['csv_encoding'] ) ? // phpcs:ignore WordPress.Security.NonceVerification
148 + sanitize_text_field( wp_unslash( $_REQUEST['csv_encoding'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
149 + ?>
150 + <br/><br/>
146 151 <fieldset class="wpda_fieldset">
147 152 <legend>
148 - <?php echo __( 'Select a file and click upload', 'wp-data-access' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
153 + <?php esc_html_e( 'Select a file and click upload', 'wp-data-access' ); ?>
149 154 </legend>
155 +
150 156 <form id="form_import_table"
151 157 method="post"
152 158 action="?page=wpda&page_action=wpda_import_csv"
153 159 enctype="multipart/form-data">
154 - <label for="csv_name">
155 - Import name
156 - <input id="csv_name"
157 - name="csv_name"
158 - type="text"
159 - value="<?php echo esc_attr( $csv_name ); ?>"
160 - <?php
161 - if ( '' !== $csv_name ) {
162 - echo 'disabled';
163 - }
164 - ?>
165 - />
166 - <?php if ( '' !== $csv_name ) { ?>
167 - <input id="csv_name"
168 - name="csv_name"
169 - type="hidden"
170 - value="<?php echo esc_attr( $csv_name ); ?>"
171 - />
172 - <?php } ?>
173 - </label>
174 - <br/><br/>
175 - <input type="file" name="filename" id="filename" accept=".csv">
176 - <button type="submit"
177 - class="button button-primary"
178 - onclick="if (jQuery('#csv_name').val()===''||jQuery('#filename').val()==='') { alert('Please enter an import name and select a file'); return false; }"
179 - >
180 - <i class="fas fa-check wpda_icon_on_button"></i>
181 - <?php echo __( 'Upload', 'wp-data-access' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
182 - </button>
183 - <button type="button"
184 - onclick="window.location.href='?page=wpda&page_action=wpda_import_csv'"
185 - class="button button-secondary"
186 - >
187 - <i class="fas fa-times-circle wpda_icon_on_button"></i>
188 - <?php echo __( 'Cancel', 'wp-data-access' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
189 - </button>
160 + <table class="wpda_table_csv_upload">
161 + <tr>
162 + <td>
163 + <label for="csv_name" class="wpda-label">
164 + Import name
165 + </label>
166 + </td>
167 + <td>
168 + <input id="csv_name"
169 + name="csv_name"
170 + type="text"
171 + value="<?php echo esc_attr( $csv_name ); ?>"
172 + <?php
173 + if ( '' !== $csv_name ) {
174 + echo 'disabled';
175 + }
176 + ?>
177 + />
178 + <?php if ( '' !== $csv_name ) { ?>
179 + <input id="csv_name"
180 + name="csv_name"
181 + type="hidden"
182 + value="<?php echo esc_attr( $csv_name ); ?>"
183 + />
184 + <?php } ?>
185 + <label>
186 + <input id="csv_encoding"
187 + name="csv_encoding"
188 + type="checkbox"
189 + <?php
190 + if ( 'UTF-8' === $csv_encoding ) {
191 + echo 'checked="checked"';
192 + }
193 + ?>
194 + />
195 + UTF-8
196 + </label>
197 + </td>
198 + </tr>
199 + <tr>
200 + <td></td>
201 + <td>
202 + <input type="file" name="filename" id="filename" accept=".csv">
203 + </td>
204 + </tr>
205 + <tr>
206 + <td></td>
207 + <td>
208 + <button type="submit"
209 + class="button button-primary"
210 + onclick="if (jQuery('#csv_name').val()===''||jQuery('#filename').val()==='') { alert('Please enter an import name and select a file'); return false; }"
211 + >
212 + <i class="fas fa-check wpda_icon_on_button"></i>
213 + <?php esc_html_e( 'Upload', 'wp-data-access' ); ?>
214 + </button>
215 + <button type="button"
216 + onclick="window.location.href='?page=wpda&page_action=wpda_import_csv'"
217 + class="button button-secondary"
218 + >
219 + <i class="fas fa-times-circle wpda_icon_on_button"></i>
220 + <?php esc_html_e( 'Cancel', 'wp-data-access' ); ?>
221 + </button>
222 + </td>
223 + </tr>
224 + </table>
190 225 <input type="hidden"
191 226 name="action"
192 227 value="upload"
193 228 />
@@ -201,8 +236,18 @@
201 236 />
202 237 <?php wp_nonce_field( "wpda-import-csv-{$this->schema_name}", '_wpnonce', false ); ?>
203 238 </form>
204 239 </fieldset>
240 + <style>
241 + .wpda-label {
242 + font-weight: bold;
243 + display: inline-block;
244 + width: 110px;
245 + }
246 + table.wpda_table_csv_upload tr {
247 + height: 30px;
248 + }
249 + </style>
205 250 <?php
206 251 }
207 252
208 253 /**
@@ -215,9 +260,9 @@
215 260 $csv_name = isset( $_REQUEST['csv_name'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['csv_name'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
216 261 $wp_nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
217 262 $page = isset( $_REQUEST['page'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
218 263 ?>
219 - <style type="text/css">
264 + <style>
220 265 .wpda-label {
221 266 font-weight: bold;
222 267 display: inline-block;
223 268 width: 110px;
@@ -267,9 +312,9 @@
267 312
268 313 // Security check.
269 314 $wp_nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : '?'; // phpcs:ignore WordPress.Security.NonceVerification
270 315 if ( ! wp_verify_nonce( $wp_nonce, "wpda-import-csv-{$csv_id}" ) ) {
271 - wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) ); // phpcs:ignore WordPress.Security.EscapeOutput
316 + wp_die( esc_html__( 'ERROR: Not authorized', 'wp-data-access' ) );
272 317 }
273 318
274 319 $truncate_table =
275 320 isset( $_REQUEST['truncate_table'] ) ? // phpcs:ignore WordPress.Security.NonceVerification
@@ -343,9 +388,9 @@
343 388 $data_type_after[ $column['column_name'] ] = "'";
344 389 }
345 390 }
346 391
347 - // phpcs:disable
392 + // phpcs:disable Squiz.PHP.DiscouragedFunctions.Discouraged, PluginCheck.CodeAnalysis.PHPErrorReporting.IniDirectiveDisplay_errors
348 393 echo '<p>Enabling buffering...</p>';
349 394 set_time_limit( 0 );
350 395 @ini_set( 'zlib.output_compression', false );
351 396 @ini_set( 'implicit_flush', true );
@@ -353,9 +398,9 @@
353 398 @ini_set( 'display_errors', false );
354 399 ob_implicit_flush( true );
355 400 echo '<p>Reading CSV file...</p>';
356 401 @ini_set( 'auto_detect_line_endings', true );
357 - // phpcs:enable
402 + // phpcs:enable Squiz.PHP.DiscouragedFunctions.Discouraged, PluginCheck.CodeAnalysis.PHPErrorReporting.IniDirectiveDisplay_errors
358 403
359 404 if ( false !== ( $fp = fopen( $file_name, 'rb' ) ) ) { // phpcs:ignore
360 405 $wpdadb = WPDADB::get_db_connection( $schema_name );
361 406 if ( null !== $wpdadb ) {
@@ -389,9 +434,9 @@
389 434 $date_value = substr( $data[ $column ], 0, 10 );
390 435 $convert_date = \DateTime::createFromFormat( str_replace( '%', '', $date_format ), $date_value );
391 436 if ( false === $convert_date ) {
392 437 $error_msg = 'Cannot convert ' . esc_attr( $date_value ) . ' to date (using format ' . esc_attr( $date_format ) . ')';
393 - echo "<div>ERROR: {$error_msg}</div>";
438 + echo '<div>ERROR: ' . esc_attr( $error_msg ) . '</div>';
394 439 $row_values_valid = false;
395 440
396 441 // Write error to log file
397 442 WPDA::wpda_log_wp_error( $error_msg );
@@ -399,9 +444,9 @@
399 444 $wpda_insert_column_values[ $table_columns[ $column ] ] = $convert_date->format( 'Y-m-d' );
400 445 }
401 446 } catch ( \Exception $e ) {
402 447 echo '<div>Cannot convert ' . esc_attr( $date_value ) . ' to date (using format ' . esc_attr( $date_format ) . ')</div>';
403 - echo "<div>ERROR: {$error_msg}</div>";
448 + echo '<div>ERROR: ' . esc_attr( $error_msg ) . '</div>';
404 449 $row_values_valid = false;
405 450
406 451 // Write error to log file
407 452 WPDA::wpda_log_wp_error( $error_msg );
@@ -472,9 +517,9 @@
472 517 $row++;
473 518 }
474 519 $wpdadb->suppress_errors( $suppress_errors );
475 520 }
476 - fclose( $fp ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
521 + fclose( $fp ); // phpcs:ignore
477 522
478 523 $row--;
479 524 echo "<p style='font-weight: bold'>Import ready!</p>";
480 525 echo esc_attr( $row ) . ' rows processed<br/>';
@@ -498,9 +543,9 @@
498 543
499 544 // Security check.
500 545 $wp_nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : '?'; // input var okay.
501 546 if ( ! wp_verify_nonce( $wp_nonce, "wpda-reload-csv-{$csv_id}" ) ) {
502 - wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) ); // phpcs:ignore WordPress.Security.EscapeOutput
547 + wp_die( esc_html__( 'ERROR: Not authorized', 'wp-data-access' ) );
503 548 }
504 549
505 550 $this->show_body_upload();
506 551 }
@@ -513,15 +558,15 @@
513 558 protected function upload_file() {
514 559 // Security check.
515 560 $wp_nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : '?'; // input var okay.
516 561 if ( ! wp_verify_nonce( $wp_nonce, "wpda-import-csv-{$this->schema_name}" ) ) {
517 - wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) ); // phpcs:ignore WordPress.Security.EscapeOutput
562 + wp_die( esc_html__( 'ERROR: Not authorized', 'wp-data-access' ) );
518 563 }
519 564
520 - // phpcs:disable
565 + // phpcs:disable Squiz.PHP.DiscouragedFunctions.Discouraged
521 566 $temp_file_name = isset( $_FILES['filename']['tmp_name'] ) ?
522 567 sanitize_text_field( $_FILES['filename']['tmp_name'] ) : ''; // For Windows: do NOT unslash!
523 - // phpcs:enable
568 + // phpcs:enable Squiz.PHP.DiscouragedFunctions.Discouraged
524 569 $orig_file_name = isset( $_FILES['filename']['name'] ) ?
525 570 sanitize_text_field( wp_unslash( $_FILES['filename']['name'] ) ) : '';
526 571
527 572 if ( isset( $_FILES['filename'] ) && isset( $_REQUEST['csv_name'] ) ) {
@@ -531,49 +576,56 @@
531 576 UPLOAD_ERR_OK === $_FILES['filename']['error'] &&
532 577 is_uploaded_file( $temp_file_name )
533 578 ) {
534 579 echo '<br/>';
535 - echo __( 'Uploading file', 'wp-data-access' ) . ' <strong>' . esc_attr( $orig_file_name ) . '</strong>...'; // phpcs:ignore WordPress.Security.EscapeOutput
580 + esc_html_e( 'Uploading file', 'wp-data-access' ) . ' <strong>' . esc_attr( $orig_file_name ) . '</strong>...';
536 581 echo '<br/><br/>';
537 582
538 583 $upload_dir = WPDA::get_plugin_upload_dir();
539 584 $real_file_name = 'wpda_csv_upload_' . gmdate( 'YmdHis' ) . '.csv';
540 585
541 - // Process file and save a local copy.
586 + $csv_id = isset( $_REQUEST['csv_id'] ) ?
587 + sanitize_text_field( wp_unslash( $_REQUEST['csv_id'] ) ) : ''; // input var okay.
588 + $csv_name = sanitize_text_field( wp_unslash( $_REQUEST['csv_name'] ) ); // input var okay.
589 + $csv_encoding = isset( $_REQUEST['csv_encoding'] ) ? 'UTF-8' : '';
590 +
591 + // Process file and save a local copy.
542 592 $fp = $this->file_pointer = fopen( $temp_file_name, 'rb' ); // phpcs:ignore
543 593 if ( false !== $this->file_pointer ) {
544 594 if ( ! is_dir( $upload_dir ) ) {
545 - mkdir( $upload_dir, 0755, true );
595 + WPDA::wpda_create_content_folder();
546 596 }
547 597
548 - $fw = fopen( $upload_dir . "{$real_file_name}", 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fopen
598 + $fw = fopen( $upload_dir . "{$real_file_name}", 'w' ); // phpcs:ignore
549 599 while ( ! feof( $this->file_pointer ) ) {
550 - $file_content = fread( $this->file_pointer, 1024 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fread
551 - fwrite( $fw, $file_content ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fwrite
600 + $file_content = fread( $this->file_pointer, 1024 ); // phpcs:ignore
601 +
602 + if ( 'UTF-8' === $csv_encoding ) {
603 + // UTF-8 encoding
604 + fwrite( $fw, mb_convert_encoding( $file_content, 'UTF-8', 'ISO-8859-1' ) ); // phpcs:ignore
605 + } else {
606 + // No encoding
607 + fwrite( $fw, $file_content ); // phpcs:ignore
608 + }
552 609 }
553 610 }
554 - fclose( $fp ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
555 - fclose( $fw ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
611 + fclose( $fp ); // phpcs:ignore
612 + fclose( $fw ); // phpcs:ignore
556 613
557 - echo __( 'Saving file info...', 'wp-data-access' ); // phpcs:ignore WordPress.Security.EscapeOutput
614 + esc_html_e( 'Saving file info...', 'wp-data-access' );
558 615 echo '<br/><br/>';
559 616
560 - $csv_id =
561 - isset( $_REQUEST['csv_id'] ) ?
562 - sanitize_text_field( wp_unslash( $_REQUEST['csv_id'] ) ) : ''; // input var okay.
563 - $csv_name = sanitize_text_field( wp_unslash( $_REQUEST['csv_name'] ) ); // input var okay.
564 -
565 617 if ( '' === $csv_id ) {
566 618 // New CSV import.
567 - $result = WPDA_CSV_Uploads_Model::insert( $csv_name, $real_file_name, $orig_file_name );
619 + $result = WPDA_CSV_Uploads_Model::insert( $csv_name, $real_file_name, $orig_file_name, $csv_encoding );
568 620 } else {
569 621 // Reload CSV.
570 622 $oldrow = WPDA_CSV_Uploads_Model::query( $csv_id );
571 - $result = WPDA_CSV_Uploads_Model::update( $csv_id, $real_file_name, $orig_file_name );
623 + $result = WPDA_CSV_Uploads_Model::update( $csv_id, $real_file_name, $orig_file_name, $csv_encoding );
572 624 if ( $result ) {
573 625 // Remove old file.
574 626 if ( isset( $oldrow[0]->csv_real_file_name ) ) {
575 - unlink( WPDA::get_plugin_upload_dir() . $oldrow[0]->csv_real_file_name );
627 + unlink( WPDA::get_plugin_upload_dir() . $oldrow[0]->csv_real_file_name ); // phpcs:ignore
576 628 }
577 629 }
578 630 }
579 631 if ( false === $result ) {
@@ -578,9 +630,9 @@
578 630 }
579 631 if ( false === $result ) {
580 632 $msg = new WPDA_Message_Box(
581 633 array(
582 - 'message_text' => __( 'Processing CSV file failed', 'wp-data-access' ),
634 + 'message_text' => esc_html__( 'Processing CSV file failed', 'wp-data-access' ),
583 635 'message_type' => 'error',
584 636 'message_is_dismissible' => false,
585 637 )
586 638 );
@@ -605,9 +657,9 @@
605 657 >
606 658 <input type="submit"
607 659 class="page-title-action"
608 660 style="margin-left: 0;"
609 - value="<?php echo __( 'Column mapping' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>"
661 + value="<?php esc_html_e( 'Column mapping', 'wp-data-access' ); ?>"
610 662 />
611 663 <input type="hidden" name="_wpnonce" value="<?php echo esc_attr( $wp_nonce ); ?>" />
612 664 </form>&nbsp;
613 665 <form method="post"
@@ -615,9 +667,9 @@
615 667 style="display: inline-block; vertical-align: baseline;">
616 668 <input type="submit"
617 669 class="page-title-action"
618 670 style="margin-left: 0;"
619 - value="<?php echo __( 'CSV file list' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>"
671 + value="<?php esc_html_e( 'CSV file list', 'wp-data-access' ); ?>"
620 672 />
621 673 </form>
622 674 <?php
623 675 }
@@ -625,9 +677,9 @@
625 677 } else {
626 678 // File upload failed: inform user.
627 679 $msg = new WPDA_Message_Box(
628 680 array(
629 - 'message_text' => __( 'File upload failed', 'wp-data-access' ),
681 + 'message_text' => esc_html__( 'File upload failed', 'wp-data-access' ),
630 682 'message_type' => 'error',
631 683 'message_is_dismissible' => false,
632 684 )
633 685 );