databases[] = $db['schema_name']; } } public function show() { $this->css(); $this->js(); $esc_attr = 'esc_attr'; ?>
dbname; ?> suppress_errors( true ); } // Get column info. $wpda_list_columns = WPDA_List_Columns_Cache::get_list_columns( $schema_name, $table_name ); $columns = $wpda_list_columns->get_table_columns(); if ( ! is_array( $columns ) ) { // Table not found. WPDA::sent_header( 'application/json' ); WPDA::sent_msg( 'ERROR', 'Internal SQL error' ); die(); } // Determine case-sensitive search $search_case = 'true' === $_POST['c']; // phpcs:ignore // Perform query $result = self::execute_query( $wpdadb, $schema_name, $table_name, $columns, $search_value, $search_case, true ); // Process query results if ( '' === $wpdadb->last_error && is_array( $result ) && count( $result ) > 0 ) { // phpcs:ignore -- 8.1 proof WPDA::sent_header( 'application/json' ); WPDA::sent_msg( 'OK', $result[0][0] ); die(); } WPDA::sent_header( 'application/json' ); WPDA::sent_msg( 'ERROR', $wpdadb->last_error ); die(); } private static function execute_query( $wpdadb, $schema_name, $table_name, $columns, $search_value, $search_case, $just_count = false ) { if ( 'rdb:' === substr( $schema_name, 0, 4) ) { // Remote database $query = true === $just_count ? 'select count(*) from `%1s`' : 'select * from `%1s`'; // Define query. $query = $wpdadb->prepare( $query, array( WPDA::remove_backticks( $table_name ), ) ); } else { // Local database $query = true === $just_count ? 'select count(*) from `%1s`.`%1s`' : 'select * from `%1s`.`%1s`'; // Define query. $query = $wpdadb->prepare( $query, array( WPDA::remove_backticks( $schema_name ), WPDA::remove_backticks( $table_name ), ) ); } // Construct where clause. $where = WPDA::construct_where_clause( $schema_name, $table_name, $columns, $search_value, $search_case ); if ( trim( $where ) !== '' ) { $query .= " where {$where} "; } // Perform query. return $wpdadb->get_results( $query, ( true === $just_count ? 'ARRAY_N' : 'ARRAY_A' ) ); } public static function replace() { self::check_request(); if ( ! isset( $_POST['r'] ) ) { // phpcs:ignore WPDA::sent_header( 'application/json' ); WPDA::sent_msg( 'ERROR', 'Invalid arguments' ); die(); } $schema_name = sanitize_text_field( wp_unslash( $_POST['sn'] ) ); // phpcs:ignore $table_name = sanitize_text_field( wp_unslash( $_POST['tn'] ) ); // phpcs:ignore $search_value = sanitize_text_field( wp_unslash( $_POST['q'] ) ); // phpcs:ignore $replace_value = sanitize_text_field( wp_unslash( $_POST['r'] ) ); // phpcs:ignore $wpdadb = WPDADB::get_db_connection( $schema_name ); if ( null !== $wpdadb ) { $wpdadb->suppress_errors( true ); } // Get column info. $wpda_list_columns = WPDA_List_Columns_Cache::get_list_columns( $schema_name, $table_name ); $columns = $wpda_list_columns->get_table_columns(); if ( ! is_array( $columns ) ) { // Table not found. WPDA::sent_header( 'application/json' ); WPDA::sent_msg( 'ERROR', 'Internal SQL error' ); die(); } // Determine case-sensitive search if ( 'true' === $_POST['c'] ) { // phpcs:ignore // Case-sensitive search and replace // Use built-in SQL replace function // Define query. $query = $wpdadb->prepare( 'update `%1s`.`%1s` set ', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders array( WPDA::remove_backticks( $schema_name ), WPDA::remove_backticks( $table_name ), ) ); // Add columns to be updated. $set = ''; foreach ( $columns as $column ) { if ( 'string' === WPDA::get_type( $column['data_type'] ) ) { if ( '' !== $set ) { $set .= ','; } $set .= $wpdadb->prepare( " `%1s` = replace( `%1s`, '%s', '%s' ) ", array( WPDA::remove_backticks( $column['column_name'] ), WPDA::remove_backticks( $column['column_name'] ), $search_value, $replace_value ) ); } } if ( '' === $set ) { WPDA::sent_header( 'application/json' ); WPDA::sent_msg( 'ERROR', 'No updatable columns found in this table' ); die(); } $query .= $set; // Construct where clause. $where = WPDA::construct_where_clause( $schema_name, $table_name, $columns, $search_value ); if ( trim( $where ) !== '' ) { $query .= " where {$where} "; } // Perform update. $wpdadb->get_results( $query, 'ARRAY_A' ); if ( '' === $wpdadb->last_error ) { WPDA::sent_header( 'application/json' ); WPDA::sent_msg( 'OK', $wpdadb->rows_affected ); die(); } } else { // Case-insensitive search and replace // There is no standard SQL function to replace case-insensitive, so we need to implement our own solution // Determine updatable columns $update_columns = array(); foreach ( $columns as $column ) { if ( 'string' === WPDA::get_type( $column['data_type'] ) ) { $update_columns[] = $column['column_name']; } } // Determine primary key for record update $pk = $wpda_list_columns->get_table_primary_key(); $rows_affected = 0; // Perform query $results = self::execute_query( $wpdadb, $schema_name, $table_name, $columns, $search_value, false ); foreach ( $results as $result ) { $update_values = array(); $pk_values = array(); foreach ( $update_columns as $update_column ) { if ( false !== stripos( $result[ $update_column ], $search_value ) ) { $update_values[ $update_column ] = str_ireplace( $search_value, $replace_value, $result[ $update_column ] ); foreach ( $pk as $key ) { $pk_values[ $key ] = $result[ $key ]; } } } // Update row $wpdadb->update( $table_name, $update_values, // phpcs:ignore -- 8.1 proof ( is_array( $pk ) && count( $pk ) > 0 ? $pk_values : $result ) // fall back to all cols if no pk ); $rows_affected += $wpdadb->rows_affected; } WPDA::sent_header( 'application/json' ); WPDA::sent_msg( 'OK', $rows_affected ); die(); } WPDA::sent_header( 'application/json' ); WPDA::sent_msg( 'ERROR', $wpdadb->last_error ); die(); } private static function check_request() { $wpnonce = isset( $_POST['n'] ) ? sanitize_text_field( wp_unslash( $_POST['n'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification if ( ! wp_verify_nonce( $wpnonce, self::NONCE_SEED . WPDA::get_current_user_login() ) ) { WPDA::sent_header( 'application/json' ); WPDA::sent_msg( 'ERROR', 'Not authorized' ); die(); } if ( ! isset( $_POST['sn'], $_POST['tn'], $_POST['q'], $_POST['c'] ) || '' === $_POST['sn'] || '' === $_POST['tn'] ) { WPDA::sent_header( 'application/json' ); WPDA::sent_msg( 'ERROR', 'Invalid arguments' ); die(); } if ( '' === $_POST['q'] ) { WPDA::sent_header( 'application/json' ); WPDA::sent_msg( 'ERROR', 'Nothing to search' ); die(); } } } }