*/ protected $settings; /** @param array $settings */ public function __construct( array $settings = [] ) { $this->settings = empty( $settings ) ? WTeam_Executor_Settings::get_settings() : $settings; } /** @return bool */ public function reads_enabled() { return ! empty( $this->settings['database_reads_enabled'] ); } /** @return bool */ public function redact_sensitive_data() { return ! array_key_exists( 'redact_sensitive_data', $this->settings ) || ! empty( $this->settings['redact_sensitive_data'] ); } /** @return int */ public function max_read_rows() { $value = isset( $this->settings['database_max_read_rows'] ) ? (int) $this->settings['database_max_read_rows'] : self::DEFAULT_MAX_READ_ROWS; return max( 1, min( self::MAX_ALLOWED_READ_ROWS, $value ) ); } /** * @param string $table * @return string|WP_Error Unqualified table name. */ public function normalize_table_name( $table ) { global $wpdb; $table = trim( str_replace( '`', '', (string) $table ) ); if ( '' === $table ) { return new WP_Error( 'wteam_executor_empty_table', __( 'A database table name is required.', 'wteam-executor' ) ); } $parts = explode( '.', $table ); if ( count( $parts ) > 2 ) { return new WP_Error( 'wteam_executor_invalid_table', __( 'The database table name is invalid.', 'wteam-executor' ) ); } if ( 2 === count( $parts ) ) { $database = $parts[0]; $table = $parts[1]; $current = isset( $wpdb->dbname ) ? (string) $wpdb->dbname : ( defined( 'DB_NAME' ) ? (string) DB_NAME : '' ); if ( '' === $current || 0 !== strcasecmp( $database, $current ) ) { return new WP_Error( 'wteam_executor_cross_database_table', __( 'Only tables in the active WordPress database are allowed.', 'wteam-executor' ) ); } } if ( ! preg_match( '/^[A-Za-z0-9_$]{1,192}$/', $table ) ) { return new WP_Error( 'wteam_executor_invalid_table', __( 'The database table name contains unsupported characters.', 'wteam-executor' ) ); } return $table; } /** * @param string $identifier * @return string|WP_Error */ public function normalize_identifier( $identifier ) { $identifier = trim( str_replace( '`', '', (string) $identifier ) ); if ( '' === $identifier || ! preg_match( '/^[A-Za-z0-9_$-]{1,192}$/', $identifier ) ) { return new WP_Error( 'wteam_executor_invalid_identifier', __( 'A database column identifier is invalid.', 'wteam-executor' ) ); } return $identifier; } }