page = sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ); // input var okay. } $this->user_can_create_db = WPDA_Dictionary_Access::can_create_db(); if ( WPDA::current_user_is_admin() ) { if ( isset( $_REQUEST['action'] ) ) { // Process create, drop and alter database actions. if ( 'create_db' === $_REQUEST['action'] ) { $this->create_db(); } elseif ( 'drop_db' === $_REQUEST['action'] ) { $this->drop_db(); } elseif ( 'edit_db' === $_REQUEST['action'] ) { $this->edit_db(); } } } } private function create_db() { if ( !$this->check_wpnonce( 'wpda-create-db-from-data-explorer-' . WPDA::get_current_user_login(), '_wpnoncecreatedb' ) ) { return; } if ( isset( $_REQUEST['database_location'] ) && 'local' === $_REQUEST['database_location'] ) { // Add local database if ( !isset( $_REQUEST['local_database'] ) ) { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Cannot create database [missing argument]', 'wp-data-access' ) ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); return; } $database = str_replace( '`', '', sanitize_text_field( wp_unslash( $_REQUEST['local_database'] ) ) ); // input var okay. global $wpdb; if ( false === $wpdb->query( $wpdb->prepare( 'create database `%1s`', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders array(WPDA::remove_backticks( $database )) ) ) ) { // db call ok; no-cache ok. $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Error creating database `%s`', 'wp-data-access' ), $database ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); } else { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Database `%s` created', 'wp-data-access' ), $database ), )); $msg->box(); $this->switch_schema_name = $database; } } else { // Add remote database $database = ( isset( $_REQUEST['remote_database'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_database'] ) ) : '' ); // input var okay. if ( false !== WPDADB::get_remote_database( $database ) ) { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Remote database connection already exists', 'wp-data-access' ) ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); return; } $host = ( isset( $_REQUEST['remote_host'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_host'] ) ) : '' ); // input var okay. $user = ( isset( $_REQUEST['remote_user'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_user'] ) ) : '' ); // input var okay. $passwd = ( isset( $_REQUEST['remote_passwd'] ) ? wp_unslash( $_REQUEST['remote_passwd'] ) : '' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $port = ( isset( $_REQUEST['remote_port'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_port'] ) ) : '' ); // input var okay. $schema = ( isset( $_REQUEST['remote_schema'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_schema'] ) ) : '' ); // input var okay. $ssl = ( isset( $_REQUEST['remote_ssl'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_ssl'] ) ) : 'off' ); // input var okay. $ssl_key = ( isset( $_REQUEST['remote_client_key'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_client_key'] ) ) : '' ); // input var okay. $ssl_cert = ( isset( $_REQUEST['remote_client_certificate'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_client_certificate'] ) ) : '' ); // input var okay. $ssl_ca = ( isset( $_REQUEST['remote_ca_certificate'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_ca_certificate'] ) ) : '' ); // input var okay. $ssl_path = ( isset( $_REQUEST['remote_certificate_path'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_certificate_path'] ) ) : '' ); // input var okay. $ssl_cipher = ( isset( $_REQUEST['remote_specified_cipher'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_specified_cipher'] ) ) : '' ); // input var okay. if ( '' === $database || '' === $host || '' === $user || '' === $schema ) { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Cannot add remote database connection [missing argument]', 'wp-data-access' ) ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); return; } if ( 'rdb:' === $database ) { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Invalid database name [enter a valid database name, for example rdb:remotedb]', 'wp-data-access' ) ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); return; } if ( !WPDADB::add_remote_database( $database, $host, $user, $passwd, $port, $schema, $ssl, $ssl_key, $ssl_cert, $ssl_ca, $ssl_path, $ssl_cipher ) ) { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Cannot add remote database connection', 'wp-data-access' ) ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); } else { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Remote database connection `%s` added', 'wp-data-access' ), $database ), )); $msg->box(); $this->switch_schema_name = $database; } } } private function drop_db() { if ( !$this->check_wpnonce( 'wpda-drop-db-from-data-explorer-' . WPDA::get_current_user_login(), '_wpnoncedropdb' ) ) { return; } if ( !isset( $_REQUEST['database'] ) ) { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Cannot drop database [missing argument]', 'wp-data-access' ) ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); return; } global $wpdb; $database = str_replace( '`', '', sanitize_text_field( wp_unslash( $_REQUEST['database'] ) ) ); // input var okay. if ( 'rdb:' === substr( $database, 0, 4 ) ) { // Delete remote database if ( false === WPDADB::get_remote_database( $database ) ) { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Cannot delete remote database connection `%s` [remote database connection not found]', 'wp-data-access' ), $database ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); } else { if ( false === WPDADB::del_remote_database( $database ) ) { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Cannot delete remote database connection `%s`', 'wp-data-access' ), $database ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); } else { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Remove database `%s` deleted', 'wp-data-access' ), $database ), )); $msg->box(); $this->switch_schema_name = $wpdb->dbname; } } } else { // Drop local database if ( $wpdb->dbname === $database ) { $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Cannot drop WordPress database', 'wp-data-access' ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); return; } if ( 'sys' === $database || 'mysql' === $database || 'information_schema' === $database || 'performance_schema' === $database || '' === $database ) { $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Cannot drop MySQL database', 'wp-data-access' ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); return; } if ( false === $wpdb->query( $wpdb->prepare( 'drop database `%1s`', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders array(WPDA::remove_backticks( $database )) ) ) ) { // db call ok; no-cache ok. $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Error dropping database `%s`', 'wp-data-access' ), $database ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); } else { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Database `%s` dropped', 'wp-data-access' ), $database ), )); $msg->box(); $this->switch_schema_name = $wpdb->dbname; } } } private function edit_db() { if ( !$this->check_wpnonce( 'wpda-edit-db-from-data-explorer-' . WPDA::get_current_user_login(), '_wpnonceeditdb' ) ) { return; } if ( !isset( $_REQUEST['edit_remote_database'] ) ) { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Cannot update remote database connection [missing argument]', 'wp-data-access' ) ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); return; } $database = sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_database'] ) ); // input var okay. $database_old = sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_database_old'] ) ); // input var okay. if ( $database !== $database_old ) { // Update database connection name if ( false === WPDADB::get_remote_database( $database_old ) ) { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Cannot update remote database connection [remote database connection not found]', 'wp-data-access' ) ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); return; } } else { // Update database connection information if ( false === WPDADB::get_remote_database( $database ) ) { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Cannot update remote database connection [remote database connection not found]', 'wp-data-access' ) ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); return; } } $host = ( isset( $_REQUEST['edit_remote_host'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_host'] ) ) : '' ); // input var okay. $user = ( isset( $_REQUEST['edit_remote_user'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_user'] ) ) : '' ); // input var okay. $passwd = ( isset( $_REQUEST['edit_remote_passwd'] ) ? wp_unslash( $_REQUEST['edit_remote_passwd'] ) : '' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $port = ( isset( $_REQUEST['edit_remote_port'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_port'] ) ) : '' ); // input var okay. $schema = ( isset( $_REQUEST['edit_remote_schema'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_schema'] ) ) : '' ); // input var okay. $ssl = ( isset( $_REQUEST['edit_remote_ssl'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_ssl'] ) ) : 'off' ); // input var okay. $ssl_key = ( isset( $_REQUEST['edit_remote_client_key'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_client_key'] ) ) : '' ); // input var okay. $ssl_cert = ( isset( $_REQUEST['edit_remote_client_certificate'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_client_certificate'] ) ) : '' ); // input var okay. $ssl_ca = ( isset( $_REQUEST['edit_remote_ca_certificate'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_ca_certificate'] ) ) : '' ); // input var okay. $ssl_path = ( isset( $_REQUEST['edit_remote_certificate_path'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_certificate_path'] ) ) : '' ); // input var okay. $ssl_cipher = ( isset( $_REQUEST['edit_remote_specified_cipher'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['edit_remote_specified_cipher'] ) ) : '' ); // input var okay. if ( '' === $database || '' === $host || '' === $user || '' === $schema ) { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Cannot edit remote database connection [missing arguments]', 'wp-data-access' ) ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); return; } if ( !WPDADB::upd_remote_database( $database, $host, $user, $passwd, $port, $schema, false, $database_old, $ssl, $ssl_key, $ssl_cert, $ssl_ca, $ssl_path, $ssl_cipher ) ) { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Cannot update remote database connection `%s`', 'wp-data-access' ), $database ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); } else { $msg = new WPDA_Message_Box(array( 'message_text' => sprintf( __( 'Remote database connection `%s` updated', 'wp-data-access' ), $database ), )); $msg->box(); if ( $database !== $database_old ) { $this->switch_schema_name = $database; } } } private function check_wpnonce( $wp_nonce_action, $wp_nonce_arg ) { $wp_nonce = ( isset( $_REQUEST[$wp_nonce_arg] ) ? sanitize_text_field( wp_unslash( $_REQUEST[$wp_nonce_arg] ) ) : '' ); // input var okay. if ( !wp_verify_nonce( $wp_nonce, $wp_nonce_action ) ) { $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Not authorized', 'wp-data-access' ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); return false; } return true; } public function show() { if ( null !== $this->page ) { $this->add_containers(); } } private function add_containers() { ?>