page = sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ); // input var okay. } else { wp_die( __( 'ERROR: Wrong arguments [missing page]', 'wp-data-access' ) ); } if ( isset( $_REQUEST['action'] ) ) { $this->action = sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ); } if ( isset( $_REQUEST['action2'] ) ) { $this->action2 = sanitize_text_field( wp_unslash( $_REQUEST['action2'] ) ); } $this->tabs = array( 'tableinfo' => __( 'Table Settings', 'wp-data-access' ), 'relation' => __( 'Relationships', 'wp-data-access' ), 'listtable' => __( 'List Table', 'wp-data-access' ), 'tableform' => __( 'Data Entry', 'wp-data-access' ), 'reconcile' => __( 'Reconcile', 'wp-data-access' ), ); $this->current_tab = 'tableinfo'; if ( isset( $_REQUEST['tab'] ) ) { $tab = sanitize_text_field( wp_unslash( $_REQUEST['tab'] ) ); // input var okay. if ( isset( $this->tabs[$tab] ) ) { $this->current_tab = $tab; } } $this->wpnonce_requested = ( isset( $_POST['wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['wpnonce'] ) ) : null ); // input var okay. if ( isset( $_REQUEST['wpda_table_name'] ) && !is_array( $_REQUEST['wpda_table_name'] ) ) { $wpda_project_design_table_model = new WPDP_Project_Design_Table_Model(); if ( 'reconcile' === $this->action ) { $wpda_table_name_re = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) ); // input var okay. $this->is_authorized( $wpda_table_name_re ); $wpda_schema_name_re = ( isset( $_REQUEST['wpda_schema_name'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_schema_name'] ) ) : '' ); // input var okay. $wpda_reverse_engineering = new WPDA_Reverse_Engineering($wpda_table_name_re, $wpda_schema_name_re); $table_structure = $wpda_reverse_engineering->get_designer_format( 'advanced' ); if ( isset( $_REQUEST['keep_options'] ) ) { $param_keep_options = sanitize_text_field( wp_unslash( $_REQUEST['keep_options'] ) ); } else { $param_keep_options = 'off'; } $result_update = $wpda_project_design_table_model->reconcile( $table_structure, $param_keep_options ); if ( false === $result_update ) { $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Update failed', 'wp-data-access' ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); } if ( 0 === $result_update ) { $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Nothing to save', 'wp-data-access' ), )); $msg->box(); } else { $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Succesfully saved changes to database', 'wp-data-access' ), )); $msg->box(); } } elseif ( 'reverse_engineering' === $this->action ) { if ( isset( $_REQUEST['wpda_table_name'] ) ) { $wpda_table_name_re = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) ); // input var okay. $this->is_authorized( WPDP_Project_Design_Table_Model::get_base_table_name() ); $wpda_schema_name_re = ( isset( $_REQUEST['wpda_schema_name'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_schema_name'] ) ) : '' ); // input var okay. $wpda_reverse_engineering = new WPDA_Reverse_Engineering($wpda_table_name_re, $wpda_schema_name_re); $table_structure = $wpda_reverse_engineering->get_designer_format( 'advanced' ); if ( count( $table_structure ) > 0 ) { //phpcs:ignore - 8.1 proof $this->wpda_schema_name = $wpda_schema_name_re; $this->wpda_table_name = $wpda_table_name_re; $this->get_unique_setname(); $this->wpda_table_design = $table_structure; } else { wp_die( __( 'ERROR: Reverse engineering table failed [invalid structure]', 'wp-data-access' ) ); } if ( !WPDP_Project_Design_Table_Model::insert_reverse_engineered( $this->wpda_table_name, $this->wpda_table_setname, $this->wpda_table_design, $this->wpda_schema_name ) ) { $db_error = ( '' === $wpdb->last_error ? '' : ' [' . $wpdb->last_error . ']' ); wp_die( __( 'ERROR: Reverse engineering table failed' . $db_error, 'wp-data-access' ) ); } else { // Convert named array to object (needed to display structure). $this->wpda_table_design = json_decode( json_encode( $table_structure ) ); $wpda_project_design_table_model->prepare_query( $this->wpda_table_setname ); } $this->action2 = 'edit'; $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Table added to respository', 'wp-data-access' ), )); $msg->box(); } else { wp_die( __( 'ERROR: Wrong arguments', 'wp-data-access' ) ); } } elseif ( null !== $this->action2 ) { // Check authorization $table_name = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) ); // input var okay. $this->is_authorized( $table_name ); $result_update = $wpda_project_design_table_model->update(); if ( false === $result_update ) { $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Update failed', 'wp-data-access' ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); } else { if ( 0 === $result_update ) { $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Nothing to save', 'wp-data-access' ), )); $msg->box(); } else { $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Successfully saved changes to database', 'wp-data-access' ), )); $msg->box(); } } } $wpda_project_design_table_model->query(); $structure_messages = $wpda_project_design_table_model->validate(); foreach ( $structure_messages as $messages ) { if ( 'ERR' === $messages[0] ) { $msg = new WPDA_Message_Box(array( 'message_text' => $messages[1], 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); } else { $msg = new WPDA_Message_Box(array( 'message_text' => $messages[1], )); $msg->box(); } } $this->table_structure = $wpda_project_design_table_model->get_table_design(); $this->wpda_table_setname = $wpda_project_design_table_model->get_table_setname(); $this->wpda_schema_name = ( isset( $_REQUEST['wpda_schema_name'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_schema_name'] ) ) : '' ); // input var okay. $this->wpda_schema_name_db = $this->wpda_schema_name; if ( '' === $this->wpda_schema_name ) { $this->wpda_schema_name = $wpdb->dbname; } $this->wpda_table_name = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) ); $this->wpda_data_dictionary = new WPDA_Dictionary_Exist($this->wpda_schema_name, $this->wpda_table_name); if ( $this->wpda_data_dictionary->table_exists() ) { $this->wpda_list_columns = WPDP_List_Columns_Cache::get_list_columns( $this->wpda_schema_name, $this->wpda_table_name, 'tableform', $this->wpda_table_setname ); if ( 0 === count( $this->wpda_list_columns->get_table_primary_key() ) ) { //phpcs:ignore - 8.1 proof $this->has_primary_key = false; } else { $this->has_primary_key = true; } } else { wp_die( __( 'ERROR: Invalid table name or not authorized', 'wp-data-access' ) ); } } else { wp_die( __( 'ERROR: Argument wpda_table_name not found', 'wp-data-access' ) ); } $this->wpnonce = wp_create_nonce( self::WPNONCE_SEED . $this->wpda_table_name ); } public function prepare_form() { } protected function is_authorized( $table_name ) { if ( !wp_verify_nonce( $this->wpnonce_requested, self::WPNONCE_SEED . $table_name ) ) { wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) ); } } /** * Create a unique setname for this table */ public function get_unique_setname() { $this->wpda_table_setname = 'default'; // Default for first options set global $wpdb; $query = "select 'x' from `%1s` where wpda_table_name = %s"; $wpdb->get_results( $wpdb->prepare( $query, // phpcs:ignore WordPress.DB.PreparedSQL array(WPDA::remove_backticks( WPDP_Project_Design_Table_Model::get_base_table_name() ), $this->wpda_table_name) ) ); if ( $wpdb->num_rows > 0 ) { $query = "select 'x' from `%1s` where wpda_schema_name = %s and wpda_table_name = %s and wpda_table_setname = %s"; $i = $wpdb->num_rows + 1; $this->wpda_table_setname = "options_set_{$i}"; $wpdb->get_results( $wpdb->prepare( $query, // phpcs:ignore WordPress.DB.PreparedSQL array( WPDA::remove_backticks( WPDP_Project_Design_Table_Model::get_base_table_name() ), $this->wpda_schema_name, $this->wpda_table_name, $this->wpda_table_setname ) ) ); while ( $wpdb->num_rows > 0 ) { // Search until a free options set is found $this->wpda_table_setname = "options_set_{$i}"; $wpdb->get_results( $wpdb->prepare( $query, // phpcs:ignore WordPress.DB.PreparedSQL array( WPDA::remove_backticks( WPDP_Project_Design_Table_Model::get_base_table_name() ), $this->wpda_schema_name, $this->wpda_table_name, $this->wpda_table_setname ) ) ); $i++; } } } /** * Adds tabs to page */ protected function add_tabs() { ?>