diff --git a/classes/helpers/FrmXMLHelper.php b/classes/helpers/FrmXMLHelper.php index 1331f94cbe..d9fc3a34aa 100644 --- a/classes/helpers/FrmXMLHelper.php +++ b/classes/helpers/FrmXMLHelper.php @@ -612,10 +612,10 @@ private static function fill_field( $field, $form_id ) { return array( 'id' => (int) $field->id, 'field_key' => (string) $field->field_key, - 'name' => (string) $field->name, - 'description' => (string) $field->description, + 'name' => self::field_text_from_xml( $field->name, $field ), + 'description' => self::field_text_from_xml( $field->description, $field ), 'type' => (string) $field->type, - 'default_value' => FrmAppHelper::maybe_json_decode( (string) $field->default_value ), + 'default_value' => self::field_value_from_xml( $field->default_value, $field ), 'field_order' => (int) $field->field_order, 'form_id' => (int) $form_id, 'required' => (int) $field->required, @@ -624,6 +624,24 @@ private static function fill_field( $field, $form_id ) { ); } + /** + * Decode marked text columns without interpreting unmarked JSON as an array. + * + * @since x.x + * + * @param SimpleXMLElement $value XML text column. + * @param object $field Exported field definition. + * + * @return string + */ + private static function field_text_from_xml( $value, $field ) { + if ( 'base64' === (string) $value['encoding'] ) { + return (string) self::field_value_from_xml( $value, $field ); + } + + return (string) $value; + } + /** * Reads a field's options out of the file as an array. * @@ -1985,6 +2003,103 @@ public static function cdata( $str ) { return '', ']]]]>', $str ) . ']]>'; } + /** + * Whether a field stores literal source rather than encoded compound values. + * + * @since x.x + * + * @param object|SimpleXMLElement $field Field being exported, or the raw XML field being imported. + * + * @return bool + */ + private static function field_value_is_literal( $field ) { + $field = self::normalize_field_for_literal_check( $field ); + + /** + * @since x.x + * + * @param bool $literal Whether XML must preserve this field's text exactly. + * @param stdClass $field Field with string id, field_key and type properties. Export and import pass the same shape. + */ + return (bool) apply_filters( 'frm_xml_field_value_is_literal', false, $field ); + } + + /** + * Give the literal value filter the same field shape on export and import. + * + * Import passes a SimpleXMLElement whose properties are objects, so a strict + * comparison such as 'code' === $field->type would never match there. + * + * @since x.x + * + * @param object|SimpleXMLElement $field Field being exported, or the raw XML field being imported. + * + * @return stdClass + */ + private static function normalize_field_for_literal_check( $field ) { + return (object) array( + 'id' => isset( $field->id ) ? (string) $field->id : '', + 'field_key' => isset( $field->field_key ) ? (string) $field->field_key : '', + 'type' => isset( $field->type ) ? (string) $field->type : '', + ); + } + + /** + * Encode a field value without interpreting literal source as JSON or PHP serialization. + * + * @since x.x + * + * @param string $value Stored field value. + * @param object $field Field being exported. + * + * @return string XML character data. + */ + public static function field_value_to_xml( $value, $field ) { + if ( ! self::field_value_is_literal( $field ) ) { + return self::cdata( $value ); + } + + // XML normalizes carriage returns and rejects some otherwise valid source characters. + return ''; + } + + /** + * Mark literal values so imports can recover their exact bytes. + * + * @since x.x + * + * @param object $field Field being exported. + * + * @return array + */ + public static function field_value_xml_attributes( $field ) { + return self::field_value_is_literal( $field ) ? array( 'encoding' => 'base64' ) : array(); + } + + /** + * Decode compound field data while preserving literal source text. + * + * @since x.x + * + * @param SimpleXMLElement|string $value XML field value. + * @param object $field Destination field or exported field definition. + * + * @return array|string + */ + public static function field_value_from_xml( $value, $field ) { + if ( $value instanceof SimpleXMLElement && 'base64' === (string) $value['encoding'] ) { + // Pretty-printed or wrapped files can add whitespace, which strict decoding rejects. + $encoded = (string) preg_replace( '/\s+/', '', (string) $value ); + $decoded = base64_decode( $encoded, true ); + + // Keep the raw text rather than silently wiping the value when it is not valid base64. + return false === $decoded ? (string) $value : $decoded; + } + + $value = (string) $value; + return self::field_value_is_literal( $field ) ? $value : FrmAppHelper::maybe_json_decode( $value ); + } + /** * Remove character (unit separator) from exported strings * diff --git a/classes/models/FrmEntry.php b/classes/models/FrmEntry.php index 602a085c6f..646f42e674 100644 --- a/classes/models/FrmEntry.php +++ b/classes/models/FrmEntry.php @@ -517,17 +517,47 @@ public static function getOne( $id, $meta = false ) { * @since 4.02.03 * * @param object $entry + * @param array $field_types Field types already looked up, keyed by field ID or key. + * Shared across entries so each field type is only looked up once. * * @return void */ - private static function prepare_entry( &$entry ) { + private static function prepare_entry( &$entry, &$field_types = array() ) { if ( ! $entry ) { return; } FrmAppHelper::unserialize_or_decode( $entry->description ); // TODO: Remove slashes on input only, not output. + $has_metas = property_exists( $entry, 'metas' ); + $metas = $entry->metas ?? null; + unset( $entry->metas ); $entry = wp_unslash( $entry ); + + if ( ! is_array( $metas ) ) { + if ( $has_metas ) { + $entry->metas = wp_unslash( $metas ); + } + + return; + } + + $check_types = has_filter( 'frm_should_unslash_entry_meta_value' ); + + foreach ( $metas as $field_id => $value ) { + $type = null; + + if ( $check_types ) { + if ( ! isset( $field_types[ $field_id ] ) ) { + $field_types[ $field_id ] = (string) FrmField::get_type( $field_id ); + } + + $type = $field_types[ $field_id ]; + } + + $metas[ $field_id ] = FrmEntryMeta::maybe_unslash_field_value( $value, $field_id, $type ); + } + $entry->metas = $metas; } /** @@ -538,8 +568,10 @@ private static function prepare_entry( &$entry ) { * @return void */ private static function prepare_entries( &$entries ) { + $field_types = array(); + foreach ( $entries as $k => $entry ) { - self::prepare_entry( $entry ); + self::prepare_entry( $entry, $field_types ); $entries[ $k ] = $entry; } } diff --git a/classes/models/FrmEntryMeta.php b/classes/models/FrmEntryMeta.php index 8bdc91c255..6ea6783a4a 100644 --- a/classes/models/FrmEntryMeta.php +++ b/classes/models/FrmEntryMeta.php @@ -363,7 +363,7 @@ public static function get_entry_meta_by_field( $entry_id, $field_id ) { if ( $cached && isset( $cached->metas ) && isset( $cached->metas[ $field_id ] ) ) { $result = $cached->metas[ $field_id ]; - return wp_unslash( $result ); + return self::maybe_unslash_field_value( $result, $field_id ); } $get_table = $wpdb->prefix . 'frm_item_metas'; @@ -382,7 +382,43 @@ public static function get_entry_meta_by_field( $entry_id, $field_id ) { $field_type = FrmField::get_type( $field_id ); FrmFieldsHelper::prepare_field_value( $result, $field_type ); - return wp_unslash( $result ); + return self::maybe_unslash_field_value( $result, $field_id, $field_type ); + } + + /** + * Keep literal source backslashes when a field opts out of legacy unslashing. + * + * Applied by FrmEntry, get_entry_meta_by_field(), get_entry_metas_for_field() and getAll() + * so every read path returns the same value for a field. The field type is only + * looked up when something is hooked to the filter. + * + * @since x.x + * + * @internal + * + * @param mixed $value Stored or cached field value. + * @param int|string $field_id Field ID or key. + * @param string|null $type Field type, when the caller already knows it. + * + * @return mixed + */ + public static function maybe_unslash_field_value( $value, $field_id, $type = null ) { + if ( ! has_filter( 'frm_should_unslash_entry_meta_value' ) ) { + return wp_unslash( $value ); + } + + if ( null === $type ) { + $type = FrmField::get_type( $field_id ); + } + + /** + * @since x.x + * + * @param bool $unslash Whether the field value should be unslashed. + * @param string $type Field type. + * @param int|string $field_id Field ID or key. + */ + return apply_filters( 'frm_should_unslash_entry_meta_value', true, $type, $field_id ) ? wp_unslash( $value ) : $value; } /** @@ -417,7 +453,7 @@ public static function get_entry_metas_for_field( $field_id, $order = '', $limit unset( $k, $v ); } - return wp_unslash( $values ); + return self::maybe_unslash_field_value( $values, $field_id ); } /** @@ -491,7 +527,7 @@ public static function getAll( $where = array(), $order_by = '', $limit = '', $s foreach ( $results as $k => $result ) { FrmAppHelper::unserialize_or_decode( $result->meta_value ); - $results[ $k ]->meta_value = wp_unslash( $result->meta_value ); + $results[ $k ]->meta_value = self::maybe_unslash_field_value( $result->meta_value, $result->field_id, $result->field_type ); unset( $k, $result ); } diff --git a/classes/models/FrmField.php b/classes/models/FrmField.php index 2ea52ebe8d..69d5100d07 100644 --- a/classes/models/FrmField.php +++ b/classes/models/FrmField.php @@ -551,9 +551,10 @@ public static function create( $values, $return = true ) { * * @since 7.1 * - * @param int $new_id Created field ID. + * @param int $new_id Created field ID. + * @param array $new_values Stored field columns. */ - do_action( 'frm_field_created', $new_id ); + do_action( 'frm_field_created', $new_id, $new_values ); if ( ! $return ) { return false; @@ -865,7 +866,6 @@ public static function update( $id, $values ) { } unset( $field ); } - unset( $values ); if ( $query_results ) { wp_cache_delete( $id, 'frm_field' ); @@ -881,9 +881,10 @@ public static function update( $id, $values ) { * * @since 7.1 * - * @param int $id Updated field ID. + * @param int $id Updated field ID. + * @param array $values Stored field columns. */ - do_action( 'frm_field_updated', $id ); + do_action( 'frm_field_updated', $id, $values ); } return $query_results; diff --git a/classes/views/xml/forms_xml.php b/classes/views/xml/forms_xml.php index 8a873cc97f..01c21e977f 100644 --- a/classes/views/xml/forms_xml.php +++ b/classes/views/xml/forms_xml.php @@ -38,14 +38,15 @@ foreach ( $fields as $field ) { FrmXMLHelper::prepare_field_for_export( $field ); + $default_value_attributes = FrmXMLHelper::field_value_xml_attributes( $field ); ?> id ); ?> field_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> - name ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> - description ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> + >name, $field ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> + >description, $field ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> type ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> - default_value ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> + >default_value, $field ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> field_order ); ?> form_id ); ?> required ); ?> diff --git a/tests/phpunit/xml/test_FrmLiteralFieldValues.php b/tests/phpunit/xml/test_FrmLiteralFieldValues.php new file mode 100644 index 0000000000..ab0f9aa9ed --- /dev/null +++ b/tests/phpunit/xml/test_FrmLiteralFieldValues.php @@ -0,0 +1,251 @@ + 'code' ); + $source = ""; + add_filter( 'frm_xml_field_value_is_literal', '__return_true' ); + + try { + $this->assertSame( array( 'encoding' => 'base64' ), FrmXMLHelper::field_value_xml_attributes( $field ), 'Literal values declare their encoding.' ); + $xml = simplexml_load_string( '' . FrmXMLHelper::field_value_to_xml( $source, $field ) . '' ); + $this->assertSame( $source, FrmXMLHelper::field_value_from_xml( $xml, $field ), 'Literal source survives an XML round trip.' ); + $this->assertSame( '{"key":1}', FrmXMLHelper::field_value_from_xml( '{"key":1}', $field ), 'Unmarked literal JSON remains text.' ); + } finally { + remove_filter( 'frm_xml_field_value_is_literal', '__return_true' ); + } + } + + public function test_compound_values_keep_legacy_decoding() { + $field = (object) array( 'type' => 'checkbox' ); + $this->assertSame( array(), FrmXMLHelper::field_value_xml_attributes( $field ), 'Existing fields have no encoding marker.' ); + $this->assertSame( array( 'red', 'blue' ), FrmXMLHelper::field_value_from_xml( '["red","blue"]', $field ), 'Existing compound values still decode.' ); + } + + public function test_import_passes_the_same_field_shape_as_export() { + $seen = array(); + $filter = function ( $literal, $field ) use ( &$seen ) { + $seen[] = array( get_class( $field ), $field->type ); + return 'code' === $field->type; + }; + add_filter( 'frm_xml_field_value_is_literal', $filter, 10, 2 ); + + try { + $xml_field = simplexml_load_string( + '5abc' + ); + $this->assertSame( '{"a":1}', FrmXMLHelper::field_value_from_xml( $xml_field->default_value, $xml_field ), 'A strict type check matches on import.' ); + FrmXMLHelper::field_value_xml_attributes( + (object) array( + 'id' => 5, + 'field_key' => 'abc', + 'type' => 'code', + ) + ); + } finally { + remove_filter( 'frm_xml_field_value_is_literal', $filter, 10 ); + } + + $this->assertSame( array( array( 'stdClass', 'code' ), array( 'stdClass', 'code' ) ), $seen ); + } + + public function test_base64_with_whitespace_or_invalid_data() { + $source = "line one\r\nline \\two"; + $wrapped = chunk_split( base64_encode( $source ), 8, "\n " ); + $xml = simplexml_load_string( '' ); + $field = (object) array( 'type' => 'code' ); + $this->assertSame( $source, FrmXMLHelper::field_value_from_xml( $xml, $field ), 'Whitespace in the encoded value is ignored.' ); + + $xml = simplexml_load_string( '' ); + $this->assertSame( 'not base64!', FrmXMLHelper::field_value_from_xml( $xml, $field ), 'Invalid base64 keeps the raw text.' ); + } + + public function test_forms_xml_export_and_import_round_trip() { + $form_id = $this->factory->form->create(); + $field_key = 'literal_round_trip'; + $source = "{\"a\":1}\r\n\$path = 'C:\\temp';"; + $field_id = $this->factory->field->create( + array( + 'type' => 'text', + 'form_id' => $form_id, + 'field_key' => $field_key, + 'default_value' => $source, + 'name' => $source, + 'description' => $source, + ) + ); + $filter = function ( $literal, $field ) use ( $field_key ) { + return 'text' === $field->type && $field_key === $field->field_key; + }; + add_filter( 'frm_xml_field_value_is_literal', $filter, 10, 2 ); + + try { + $path = self::generate_xml( array( 'forms' ), array( 'ids' => array( $form_id ) ) ); + $contents = file_get_contents( $path ); + $this->assertStringContainsString( '', $contents ); + + FrmField::update( + $field_id, + array( + 'default_value' => 'changed', + 'name' => 'changed', + 'description' => 'changed', + ) + ); + FrmXMLHelper::import_xml_now( simplexml_load_string( $contents ) ); + } finally { + remove_filter( 'frm_xml_field_value_is_literal', $filter, 10 ); + } + + // FrmField::getOne() unslashes every column, so compare the stored value. + foreach ( array( 'default_value', 'name', 'description' ) as $column ) { + $this->assertSame( $source, FrmDb::get_var( 'frm_fields', array( 'field_key' => $field_key ), $column ) ); + } + } + + public function test_unslash_filter_applies_to_every_read_path() { + $form_id = $this->factory->form->create(); + $field_id = $this->factory->field->create( + array( + 'type' => 'text', + 'form_id' => $form_id, + ) + ); + $entry_id = $this->factory->entry->create( + array( + 'form_id' => $form_id, + 'item_meta' => array( $field_id => 'placeholder' ), + ) + ); + $stored = 'C:\\temp\\new {"a":1}'; + + global $wpdb; + $wpdb->update( + $wpdb->prefix . 'frm_item_metas', + array( 'meta_value' => $stored ), + array( + 'item_id' => $entry_id, + 'field_id' => $field_id, + ) + ); + FrmEntryMeta::clear_cache(); + wp_cache_flush(); + + add_filter( 'frm_should_unslash_entry_meta_value', '__return_false' ); + + try { + $this->assertSame( $stored, FrmEntryMeta::get_entry_meta_by_field( $entry_id, $field_id ) ); + $this->assertSame( $stored, FrmEntry::getOne( $entry_id, true )->metas[ $field_id ] ); + $this->assertSame( $stored, FrmEntry::getOne( $entry_id, true )->metas[ $field_id ], 'Cached entries preserve literal values too.' ); + $entries = FrmEntry::getAll( array( 'it.id' => $entry_id ), '', '', true ); + $this->assertSame( $stored, reset( $entries )->metas[ $field_id ] ); + add_filter( 'frm_include_meta_keys', '__return_true' ); + + try { + wp_cache_flush(); + $field_key = FrmField::get_key_by_id( $field_id ); + $this->assertSame( $stored, FrmEntry::getOne( $entry_id, true )->metas[ $field_key ] ); + } finally { + remove_filter( 'frm_include_meta_keys', '__return_true' ); + } + $this->assertSame( array( $stored ), FrmEntryMeta::get_entry_metas_for_field( $field_id ) ); + $all = FrmEntryMeta::getAll( + array( + 'it.item_id' => $entry_id, + 'it.field_id' => $field_id, + ), + '', + '', + true + ); + $this->assertSame( $stored, reset( $all )->meta_value ); + } finally { + remove_filter( 'frm_should_unslash_entry_meta_value', '__return_false' ); + } + + FrmEntryMeta::clear_cache(); + wp_cache_flush(); + $this->assertSame( 'C:tempnew {"a":1}', FrmEntry::getOne( $entry_id, true )->metas[ $field_id ] ); + $this->assertSame( 'C:tempnew {"a":1}', FrmEntryMeta::get_entry_meta_by_field( $entry_id, $field_id ), 'Without the filter the legacy unslashing is unchanged.' ); + } + + public function test_get_all_passes_each_field_type_to_unslash_filter() { + $form_id = $this->factory->form->create(); + $field_ids = array( + 'text' => $this->factory->field->create( + array( + 'type' => 'text', + 'form_id' => $form_id, + ) + ), + 'textarea' => $this->factory->field->create( + array( + 'type' => 'textarea', + 'form_id' => $form_id, + ) + ), + ); + $entry_ids = array(); + + for ( $i = 0; $i < 3; $i++ ) { + $entry_ids[] = $this->factory->entry->create( + array( + 'form_id' => $form_id, + 'item_meta' => array( + $field_ids['text'] => 'placeholder', + $field_ids['textarea'] => 'placeholder', + ), + ) + ); + } + + global $wpdb; + + foreach ( $field_ids as $type => $field_id ) { + $wpdb->update( + $wpdb->prefix . 'frm_item_metas', + array( 'meta_value' => 'C:\\' . $type ), + array( 'field_id' => $field_id ) + ); + } + + FrmEntryMeta::clear_cache(); + wp_cache_flush(); + + $seen = array(); + $filter = function ( $unslash, $type, $field_id ) use ( &$seen ) { + $seen[] = array( (int) $field_id, $type ); + return 'textarea' !== $type; + }; + add_filter( 'frm_should_unslash_entry_meta_value', $filter, 10, 3 ); + + try { + $entries = FrmEntry::getAll( array( 'it.id' => $entry_ids ), '', '', true ); + } finally { + remove_filter( 'frm_should_unslash_entry_meta_value', $filter ); + } + + $this->assertCount( 3, $entries ); + $this->assertCount( 6, $seen, 'The filter runs once per meta per entry.' ); + + foreach ( $seen as $call ) { + $this->assertSame( array_search( $call[0], $field_ids, true ), $call[1], 'Each meta gets its own field type, including from the shared lookup.' ); + } + + foreach ( $entries as $entry ) { + $this->assertSame( 'C:text', $entry->metas[ $field_ids['text'] ] ); + $this->assertSame( 'C:\\textarea', $entry->metas[ $field_ids['textarea'] ] ); + } + } + + public function test_prepare_entry_preserves_non_array_meta_values() { + $entry = (object) array( + 'description' => '', + 'metas' => 'C:\\temp', + ); + $args = array( &$entry ); + $this->run_private_method( array( 'FrmEntry', 'prepare_entry' ), $args ); + $this->assertSame( 'C:temp', $entry->metas, 'Legacy non-array metas should still be unslashed and preserved.' ); + } +}