see solutions)'; /** * URL where to post data * * @var string */ protected $url; /** * Database schema name * * @var string */ protected $schema_name; /** * Pointer to import file * * @var string */ protected $file_pointer; /** * Content of import file * * @var string */ protected $file_content; /** * Text to inform user (line 1). * * @var string */ protected $info_title = ''; /** * Text to inform user (line 1). * * @var string */ protected $info_text = ''; /** * Indicicates whether ZipArchive is installed. * * @var bool */ protected $isinstalled_ziparchive = false; /** * WPDA_Import constructor * * Checks if ZipArchive is installed to support the import of ZIP files. * * @param string $page Page where to post data (url). * @param string $schema_name Database schema name. * @param array $args Extra arguments. * * @since 1.6.0 */ public function __construct( $page, $schema_name, $args = null ) { $this->url = $page; $this->schema_name = $schema_name; $this->isinstalled_ziparchive = class_exists( '\ZipArchive' ); if ( ! is_null( $args ) && isset( $args[0] ) && isset( $args[1] ) ) { $this->info_title = $args[0]; $this->info_text = $args[1]; } else { $this->info_title = __( 'IMPORT DATA/EXECUTE SCRIPT(S)', 'wp-data-access' ); if ( $this->isinstalled_ziparchive ) { $this->info_text = __( 'Supports sql and zip file types.', 'wp-data-access' ); } else { $this->info_text = __( 'Supports only file type sql (install ZipArchive for zip file type support).', 'wp-data-access' ); } } } /** * Checks if request is valid and allowed * * If the requested import is valid and allowed, the import file is loaded and its content imported. * * @since 1.6.0 */ public function check_post() { // Check if import was requested. if ( isset( $_REQUEST['action'] ) && 'import' === sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) // input var okay. ) { // Security check. $wp_nonce = isset( $_REQUEST['_wpnonceimport'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonceimport'] ) ) : '?'; // input var okay. if ( ! wp_verify_nonce( $wp_nonce, 'wpda-import-from-data-explorer-' . WPDA::get_current_user_login() ) ) { wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) ); } if ( isset( $_FILES['filename'] ) ) { // phpcs:disable $temp_file_name = sanitize_text_field( $_FILES['filename']['tmp_name'] ); // For Windows: do NOT unslash! // phpcs:enable $temp_file_type = sanitize_text_field( wp_unslash( $_FILES['filename']['type'] ) ); $orig_file_name = sanitize_text_field( wp_unslash( $_FILES['filename']['name'] ) ); if ( 0 === $_FILES['filename']['error'] && is_uploaded_file( $temp_file_name ) ) { if ( 'application/zip' === $temp_file_type || 'application/x-zip' === $temp_file_type || 'application/x-zip-compressed' === $temp_file_type ) { // Process ZIP file. if ( $this->isinstalled_ziparchive ) { $zip = new \ZipArchive(); if ( $zip->open( $temp_file_name ) ) { for ( $i = 0; $i < $zip->numFiles; $i ++ ) { $this->file_pointer = $zip->getStream( $zip->getNameIndex( $i ) ); $this->import( $zip->getNameIndex( $i ) ); } } else { // Error reading ZIP file. $this->import_failed( sprintf( __( 'Import failed [error reading ZIP file `%s`]', 'wp-data-access' ), $orig_file_name ) ); } } else { // ZipArchive not installed. $this->import_failed( sprintf( __( 'Import failed - ZipArchive not installed %s', 'wp-data-access' ), self::SOLUTIONS ) ); } } else { // Process plain file. $this->file_pointer = fopen( $temp_file_name, 'rb' ); $this->import( $orig_file_name ); } } } else { $this->upload_failed(); } } elseif ( isset( $_REQUEST['impchk'] ) ) { $this->upload_failed(); } } /** * Perform import * * Import file is read in chunks to allow imports of large files without resource limitations. An indicator * is maintained to follow the progress of the import. The indicator is used to show a message at the end * whether the import succeeded or failed. * * @param string $file_name Import file name * * @since 1.6.0 */ protected function import( $file_name ) { // Check if errors should be shown. $hide_errors = isset( $_REQUEST['hide_errors'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['hide_errors'] ) ) : 'off'; $result = true; global $wpdb; $wpdadb = WPDADB::get_db_connection( $this->schema_name ); if ( null === $wpdadb ) { if ( is_admin() ) { wp_die( sprintf( __( 'ERROR - Remote database %s not available', 'wp-data-access' ), esc_attr( $this->schema_name ) ) ); } else { die( sprintf( __( 'ERROR - Remote database %s not available', 'wp-data-access' ), esc_attr( $this->schema_name ) ) ); } } $suppress = $wpdadb->suppress_errors( 'on' === $hide_errors ); if ( false !== $this->file_pointer ) { while ( ! feof( $this->file_pointer ) ) { $this->file_content .= fread( $this->file_pointer, 4096 ); // Replace WP prefix and WPDA prefix. $this->file_content = str_replace( '{wp_schema}', $wpdb->dbname, $this->file_content ); $this->file_content = str_replace( '{wp_prefix}', $wpdb->prefix, $this->file_content ); $this->file_content = str_replace( '{wpda_prefix}', 'wpda', $this->file_content ); // for backward compatibility // Find and process SQL statements. $sql_end_unix = strpos( $this->file_content, ";\n" ); $sql_end_windows = strpos( $this->file_content, ";\r\n" ); while ( false !== $sql_end_unix || false !== $sql_end_windows ) { if ( false === $sql_end_unix ) { $sql_end = $sql_end_windows; } elseif ( false === $sql_end_windows ) { $sql_end = $sql_end_unix; } else { $sql_end = min( $sql_end_unix, $sql_end_windows ); } $sql = rtrim( substr( $this->file_content, 0, $sql_end ) ); $this->file_content = substr( $this->file_content, strpos( $this->file_content, $sql ) + strlen( $sql ) + 1 ); if ( false === $wpdadb->query( $sql ) ) { $result = false; } // Find next SQL statement. $sql_end_unix = strpos( $this->file_content, ";\n" ); $sql_end_windows = strpos( $this->file_content, ";\r\n" ); } } } $wpdadb->suppress_errors( $suppress ); // Process file content. if ( ! $result ) { $this->import_failed( sprintf( __( 'Import `%s` failed [check import file]', 'wp-data-access' ), $file_name ) ); } else { // Import succeeded. $msg = new WPDA_Message_Box( array( 'message_text' => sprintf( __( 'Import `%s` completed succesfully', 'wp-data-access' ), $file_name ), ) ); $msg->box(); } } /** * Inform user that an error occured * * @param string $msg Error message text * * @since 1.6.0 */ protected function import_failed( $msg ) { $msg = new WPDA_Message_Box( array( 'message_text' => $msg, 'message_type' => 'error', 'message_is_dismissible' => false, ) ); $msg->box(); } /** * Inform user that the import failed * * @since 1.6.0 */ protected function upload_failed() { $msg = new WPDA_Message_Box( array( 'message_text' => sprintf( __( 'File upload failed %s', 'wp-data-access' ), self::SOLUTIONS ), 'message_type' => 'error', 'message_is_dismissible' => false, ) ); $msg->box(); } /** * Adds an import button * * @param string $label Button label. * @param string $class Button CSS class. * * @since 1.6.0 */ public function add_button( $label = '', $class = 'page-title-action' ) { if ( '' === $label ) { $label = __( 'Imports and scripts', 'wp-data-access' ); } ?> isinstalled_ziparchive ) { $file_extensions = '.sql,.zip'; } else { $file_extensions = '.sql'; } ?>

info_text . ' ' . __( 'Maximum supported file size is', 'wp-data-access' ) . ' ' . @ini_get( 'upload_max_filesize' ) . '. '; // phpcs:ignore WordPress.Security.EscapeOutput ?>

' . __( 'IMPORTANT', 'wp-data-access' ) . ' − ' . __( 'Add a ; and a new line character at the end of every SQL statement', 'wp-data-access' ); ?>