PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.0
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.0
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/models/FrmEntryValidate.php +140 -811 6.275.0 View file →
@@ -3,22 +3,8 @@
3 3 die( 'You are not allowed to call this page directly.' );
4 4 }
5 5
6 6 class FrmEntryValidate {
7 -
8 - /**
9 - * @since 6.17
10 - *
11 - * @var array|null
12 - */
13 - private static $name_text_fields;
14 -
15 - /**
16 - * @param array $values
17 - * @param bool|string[] $exclude
18 - *
19 - * @return array
20 - */
21 7 public static function validate( $values, $exclude = false ) {
22 8 FrmEntry::sanitize_entry_post( $values );
23 9 $errors = array();
24 10
@@ -23,17 +9,16 @@
23 9 $errors = array();
24 10
25 11 if ( ! isset( $values['form_id'] ) || ! isset( $values['item_meta'] ) ) {
26 12 $errors['form'] = __( 'There was a problem with your submission. Please try again.', 'formidable' );
13 +
27 14 return $errors;
28 15 }
29 16
30 - if ( FrmAppHelper::is_admin() && is_user_logged_in() && ( ! isset( $values[ 'frm_submit_entry_' . $values['form_id'] ] ) || ! wp_verify_nonce( $values[ 'frm_submit_entry_' . $values['form_id'] ], 'frm_submit_entry_nonce' ) ) ) { // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
31 - $frm_settings = FrmAppHelper::get_settings();
32 - $errors['form'] = $frm_settings->admin_permission;
17 + if ( FrmAppHelper::is_admin() && is_user_logged_in() && ( ! isset( $values[ 'frm_submit_entry_' . $values['form_id'] ] ) || ! wp_verify_nonce( $values[ 'frm_submit_entry_' . $values['form_id'] ], 'frm_submit_entry_nonce' ) ) ) {
18 + $errors['form'] = __( 'You do not have permission to do that', 'formidable' );
33 19 }
34 20
35 - self::maybe_fix_item_meta();
36 21 self::set_item_key( $values );
37 22
38 23 $posted_fields = self::get_fields_to_validate( $values, $exclude );
39 24
@@ -44,54 +29,18 @@
44 29 self::validate_field( $posted_field, $errors, $values, $args );
45 30 unset( $posted_field );
46 31 }
47 32
48 - if ( ! $errors ) {
33 + if ( empty( $errors ) ) {
49 34 self::spam_check( $exclude, $values, $errors );
50 35 }
51 36
52 - /**
53 - * Allows modifying the validation errors after validating all fields.
54 - *
55 - * @since 5.0.04 Added `posted_fields` to the third param.
56 - *
57 - * @param array $errors Errors data.
58 - * @param array $values Value data of the form.
59 - * @param array $args Custom arguments. Contains `exclude` and `posted_fields`.
60 - */
61 - $filtered_errors = apply_filters( 'frm_validate_entry', $errors, $values, compact( 'exclude', 'posted_fields' ) );
37 + $errors = apply_filters( 'frm_validate_entry', $errors, $values, compact( 'exclude' ) );
62 38
63 - if ( is_array( $filtered_errors ) ) {
64 - $errors = $filtered_errors;
65 - } else {
66 - _doing_it_wrong( __METHOD__, 'Only arrays should be returned when using the frm_validate_entry filter.', '6.3' );
67 - }
68 -
69 39 return $errors;
70 40 }
71 41
72 - /**
73 - * In case $_POST['item_meta'] is not an array, change it to an empty array.
74 - * This helps to avoid some warnings and errors when $_POST['item_meta'] is updated.
75 - *
76 - * @since 6.6
77 - *
78 - * @return void
79 - */
80 - private static function maybe_fix_item_meta() {
81 - // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotValidated
82 - if ( ! isset( $_POST['item_meta'] ) || ! is_array( $_POST['item_meta'] ) ) {
83 - $_POST['item_meta'] = array();
84 - }
85 - }
86 -
87 - /**
88 - * @param array $values
89 - *
90 - * @return void
91 - */
92 42 private static function set_item_key( &$values ) {
93 - // phpcs:ignore Universal.Operators.StrictComparisons
94 43 if ( ! isset( $values['item_key'] ) || $values['item_key'] == '' ) {
95 44 global $wpdb;
96 45 $values['item_key'] = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_items', 'item_key' );
97 46 $_POST['item_key'] = $values['item_key'];
@@ -97,14 +46,8 @@
97 46 $_POST['item_key'] = $values['item_key'];
98 47 }
99 48 }
100 49
101 - /**
102 - * @param array $values
103 - * @param array|string $exclude
104 - *
105 - * @return array
106 - */
107 50 private static function get_fields_to_validate( $values, $exclude ) {
108 51 $where = apply_filters( 'frm_posted_field_ids', array( 'fi.form_id' => $values['form_id'] ) );
109 52
110 53 // Don't get subfields
@@ -110,46 +53,30 @@
110 53 // Don't get subfields
111 54 $where['fr.parent_form_id'] = array( null, 0 );
112 55
113 56 // Don't get excluded fields (like file upload fields in the ajax validation)
114 - if ( $exclude ) {
57 + if ( ! empty( $exclude ) ) {
115 58 $where['fi.type not'] = $exclude;
116 59 }
117 60
118 - $fields = FrmField::getAll( $where, 'field_order' );
119 -
120 - /**
121 - * Allows modifying fields to validate.
122 - *
123 - * @since 5.0.06
124 - *
125 - * @param array $fields List of fields.
126 - * @param array $args Includes `values`, `exclude`, `where`.
127 - */
128 - return apply_filters( 'frm_fields_to_validate', $fields, compact( 'values', 'exclude', 'where' ) );
61 + return FrmField::getAll( $where, 'field_order' );
129 62 }
130 63
131 - /**
132 - * @param object $posted_field
133 - * @param array $errors
134 - * @param array $values
135 - * @param array $args
136 - *
137 - * @return void
138 - */
139 64 public static function validate_field( $posted_field, &$errors, $values, $args = array() ) {
140 65 $defaults = array(
141 66 'id' => $posted_field->id,
142 - // The id of the repeat or embed form.
143 - 'parent_field_id' => '',
144 - // The pointer in the posted array.
145 - 'key_pointer' => '',
146 - // Exclude these field types from validation.
147 - 'exclude' => array(),
67 + 'parent_field_id' => '', // the id of the repeat or embed form
68 + 'key_pointer' => '', // the pointer in the posted array
69 + 'exclude' => array(), // exclude these field types from validation
70 + );
71 + $args = wp_parse_args( $args, $defaults );
148 72
149 - );
150 - $args = wp_parse_args( $args, $defaults );
151 - $value = empty( $args['parent_field_id'] ) ? ( $values['item_meta'][ $args['id'] ] ?? '' ) : $values;
73 + if ( empty( $args['parent_field_id'] ) ) {
74 + $value = isset( $values['item_meta'][ $args['id'] ] ) ? $values['item_meta'][ $args['id'] ] : '';
75 + } else {
76 + // value is from a nested form
77 + $value = $values;
78 + }
152 79
153 80 // Check for values in "Other" fields
154 81 FrmEntriesHelper::maybe_set_other_validation( $posted_field, $value, $args );
155 82
@@ -154,10 +81,9 @@
154 81 FrmEntriesHelper::maybe_set_other_validation( $posted_field, $value, $args );
155 82
156 83 self::maybe_clear_value_for_default_blank_setting( $posted_field, $value );
157 84
158 - $should_trim = is_array( $value ) && count( $value ) === 1 && isset( $value[0] ) && $posted_field->type !== 'checkbox';
159 -
85 + $should_trim = is_array( $value ) && count( $value ) == 1 && isset( $value[0] ) && $posted_field->type !== 'checkbox';
160 86 if ( $should_trim ) {
161 87 $value = reset( $value );
162 88 }
163 89
@@ -164,18 +90,16 @@
164 90 if ( ! is_array( $value ) ) {
165 91 $value = trim( $value );
166 92 }
167 93
168 - // phpcs:ignore Universal.Operators.StrictComparisons
169 94 if ( $posted_field->required == '1' && FrmAppHelper::is_empty_value( $value ) ) {
170 95 $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $posted_field, 'blank' );
171 - } elseif ( ! isset( $_POST['item_name'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
172 - self::maybe_add_item_name( $value, $posted_field );
96 + } elseif ( $posted_field->type == 'text' && ! isset( $_POST['item_name'] ) ) { // WPCS: CSRF ok.
97 + $_POST['item_name'] = $value;
173 98 }
174 99
175 100 FrmEntriesHelper::set_posted_value( $posted_field, $value, $args );
176 101
177 - self::validate_options( $errors, $posted_field, $value, $args );
178 102 self::validate_field_types( $errors, $posted_field, $value, $args );
179 103
180 104 // Field might want to modify value before other parts of the system
181 105 // e.g. trim off excess values like in the case of fields with limit.
@@ -180,9 +104,8 @@
180 104 // Field might want to modify value before other parts of the system
181 105 // e.g. trim off excess values like in the case of fields with limit.
182 106 $value = apply_filters( 'frm_modify_posted_field_value', $value, $errors, $posted_field, $args );
183 107
184 - // phpcs:ignore Universal.Operators.StrictComparisons
185 108 if ( $value != '' ) {
186 109 self::validate_phone_field( $errors, $posted_field, $value, $args );
187 110 }
188 111
@@ -187,231 +110,18 @@
187 110 }
188 111
189 112 $errors = apply_filters( 'frm_validate_' . $posted_field->type . '_field_entry', $errors, $posted_field, $value, $args );
190 113 $errors = apply_filters( 'frm_validate_field_entry', $errors, $posted_field, $value, $args );
191 -
192 - if ( ! FrmAppHelper::pro_is_installed() && empty( $args['other'] ) ) {
193 - FrmEntriesHelper::get_posted_value( $posted_field, $value, $args );
194 - }
195 114 }
196 115
197 116 /**
198 - * @since 6.21
199 - *
200 - * @param array $errors
201 - * @param object $posted_field
202 - * @param array|string $value
203 - * @param array $args
204 - *
205 - * @return void
206 - */
207 - private static function validate_options( &$errors, $posted_field, $value, $args ) {
208 - if ( empty( $posted_field->options ) ) {
209 - return;
210 - }
211 -
212 - $option_is_valid = self::option_is_valid( $posted_field, $value, $posted_field->options );
213 -
214 - /**
215 - * @since 6.21
216 - *
217 - * @param bool $option_is_valid
218 - * @param array|string $value
219 - * @param object $field
220 - */
221 - $option_is_valid = (bool) apply_filters( 'frm_option_is_valid', $option_is_valid, $value, $posted_field );
222 -
223 - if ( ! $option_is_valid ) {
224 - $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $posted_field, 'invalid' );
225 - }
226 - }
227 -
228 - /**
229 - * Validate that value matches one of the options for the field.
230 - *
231 - * @since 6.21
232 - *
233 - * @param stdClass $field
234 - * @param array|string $value
235 - * @param array $options
236 - *
237 - * @return bool
238 - */
239 - private static function option_is_valid( $field, $value, $options ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
240 - if ( '' === $value ) {
241 - return true;
242 - }
243 -
244 - $field_object = FrmFieldFactory::get_field_type( $field->type, $field );
245 -
246 - if ( ! $field_object->field_type_has_options_settings() ) {
247 - return true;
248 - }
249 -
250 - if ( in_array( $field->type, array( 'likert', 'ranking' ), true ) ) {
251 - // Ignore these field types automatically.
252 - return true;
253 - }
254 -
255 - if ( 'product' === $field->type && 'user_def' === FrmField::get_option( $field, 'data_type' ) ) {
256 - return true;
257 - }
258 -
259 - if ( ! empty( $field->field_options['post_field'] ) ) {
260 - return true;
261 - }
262 -
263 - $value = (array) $value;
264 -
265 - foreach ( $value as $current_value ) {
266 - $match = false;
267 -
268 - foreach ( $options as $key => $option ) {
269 - if ( str_starts_with( $key, 'other_' ) ) {
270 - // Always return true if an other option is found.
271 - return true;
272 - }
273 -
274 - if ( is_array( $option ) ) {
275 - $separate_value = FrmField::get_option( $field, 'separate_value' );
276 - $option_value = $separate_value ? $option['value'] : $option['label'];
277 - } else {
278 - $option_value = $option;
279 - }
280 -
281 - $match = trim( $current_value ) === trim( $option_value );
282 -
283 - if ( $match ) {
284 - break;
285 - }
286 -
287 - $match = trim( $current_value ) === trim( do_shortcode( $option_value ) );
288 -
289 - if ( $match ) {
290 - break;
291 - }
292 -
293 - $match = self::is_filtered_match( $current_value, $option_value );
294 -
295 - if ( $match ) {
296 - break;
297 - }
298 -
299 - if ( is_numeric( $current_value ) ) {
300 - $match = (int) $current_value === (int) $option_value;
301 -
302 - if ( $match ) {
303 - break;
304 - }
305 - }
306 - }//end foreach
307 -
308 - if ( ! $match ) {
309 - return self::options_are_dynamic_based_on_hook( $field, $value );
310 - }
311 - }//end foreach
312 -
313 - return true;
314 - }
315 -
316 - /**
317 - * Make an extra check after passing $option_value through the_content filter.
318 - * This is to help catch cases where the option's formatting has been modified using
319 - * the_content filter.
320 - *
321 - * @since 6.22
322 - *
323 - * @param string $value
324 - * @param string $option_value
325 - *
326 - * @return bool
327 - */
328 - private static function is_filtered_match( $value, $option_value ) {
329 - // First remove the wpautop filter so it doesn't add extra tags to $option_value.
330 - $filter_priority = has_filter( 'the_content', 'wpautop' );
331 -
332 - if ( is_numeric( $filter_priority ) ) {
333 - remove_filter( 'the_content', 'wpautop', $filter_priority );
334 - }
335 -
336 - $filtered_option = apply_filters( 'the_content', $option_value );
337 -
338 - if ( is_numeric( $filter_priority ) ) {
339 - add_filter( 'the_content', 'wpautop', $filter_priority );
340 - }
341 -
342 - return trim( $value ) === trim( $filtered_option );
343 - }
344 -
345 - /**
346 - * Do not validate options if they have been modified with a hook.
347 - * This is to help avoid issues where the options could be based on a URL param for example.
348 - *
349 - * @since 6.21
350 - *
351 - * @param object $field_object The field object.
352 - * @param array|string $value The value to validate.
353 - *
354 - * @return bool
355 - */
356 - private static function options_are_dynamic_based_on_hook( $field_object, $value ) {
357 - $values = (array) $field_object;
358 - $values['value'] = $value;
359 - FrmFieldsHelper::prepare_new_front_field( $values, $field_object );
360 -
361 - $separate_value = FrmField::get_option( $field_object, 'separate_value' );
362 - $map_callback = function ( $option ) use ( $separate_value ) {
363 - if ( is_array( $option ) ) {
364 - $option_value = $separate_value ? $option['value'] : $option['label'];
365 - } else {
366 - $option_value = $option;
367 - }
368 - return do_shortcode( $option_value );
369 - };
370 -
371 - $values_options = array_map( $map_callback, $values['options'] );
372 - $field_object_options = array_map( $map_callback, $field_object->options );
373 -
374 - return $values_options !== $field_object_options;
375 - }
376 -
377 - /**
378 - * Maybe add item_name to $_POST to save it in items table.
379 - *
380 - * @since 5.2.02
381 - *
382 - * @param array|string $value Field value.
383 - * @param object $field Field object.
384 - *
385 - * @return void
386 - */
387 - private static function maybe_add_item_name( $value, $field ) {
388 - $item_name = false;
389 -
390 - if ( 'name' === $field->type ) {
391 - $field_obj = FrmFieldFactory::get_field_object( $field );
392 - $item_name = $field_obj->get_display_value( $value );
393 - } elseif ( 'text' === $field->type ) {
394 - $item_name = $value;
395 - }
396 -
397 - if ( false !== $item_name ) {
398 - // Item name has a max length of 255 characters so truncate it so it doesn't fail to save in the database.
399 - $_POST['item_name'] = substr( $item_name, 0, 255 );
400 - }
401 - }
402 -
403 - /**
404 117 * Set $value to an empty string if it matches its label
405 118 *
406 119 * @param object $field
407 120 * @param string $value
408 - *
409 - * @return void
410 121 */
411 122 private static function maybe_clear_value_for_default_blank_setting( $field, &$value ) {
412 123 $position = FrmField::get_option( $field, 'label' );
413 -
414 124 if ( ! $position ) {
415 125 $position = FrmStylesController::get_style_val( 'position', $field->form_id );
416 126 }
417 127
@@ -419,16 +129,8 @@
419 129 $value = '';
420 130 }
421 131 }
422 132
423 - /**
424 - * @param array $errors
425 - * @param object $posted_field
426 - * @param mixed $value
427 - * @param array $args
428 - *
429 - * @return void
430 - */
431 133 public static function validate_field_types( &$errors, $posted_field, $value, $args ) {
432 134 $field_obj = FrmFieldFactory::get_field_object( $posted_field );
433 135 $args['value'] = $value;
434 136 $args['errors'] = $errors;
@@ -433,26 +135,16 @@
433 135 $args['value'] = $value;
434 136 $args['errors'] = $errors;
435 137
436 138 $new_errors = $field_obj->validate( $args );
437 -
438 - if ( $new_errors ) {
139 + if ( ! empty( $new_errors ) ) {
439 140 $errors = array_merge( $errors, $new_errors );
440 141 }
441 142 }
442 143
443 - /**
444 - * @param array $errors
445 - * @param object $field
446 - * @param string $value
447 - * @param array $args
448 - *
449 - * @return void
450 - */
451 144 public static function validate_phone_field( &$errors, $field, $value, $args ) {
452 - $format_value = FrmField::get_option( $field, 'format' );
145 + if ( $field->type == 'phone' || ( $field->type == 'text' && FrmField::is_option_true_in_object( $field, 'format' ) ) ) {
453 146
454 - if ( $field->type === 'phone' || ( $field->type === 'text' && $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value ) ) ) {
455 147 $pattern = self::phone_format( $field );
456 148
457 149 if ( ! preg_match( $pattern, $value ) ) {
458 150 $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
@@ -459,13 +151,8 @@
459 151 }
460 152 }
461 153 }
462 154
463 - /**
464 - * @param object $field
465 - *
466 - * @return string
467 - */
468 155 public static function phone_format( $field ) {
469 156 if ( FrmField::is_option_empty( $field, 'format' ) ) {
470 157 $pattern = self::default_phone_format();
471 158 } else {
@@ -471,25 +158,22 @@
471 158 } else {
472 159 $pattern = FrmField::get_option( $field, 'format' );
473 160 }
474 161
475 - // Ampersands are saved as &.
476 - // Reverse it here so we are checking for the correct character.
477 - $pattern = html_entity_decode( $pattern );
478 162 $pattern = apply_filters( 'frm_phone_pattern', $pattern, $field );
479 163
480 164 // Create a regexp if format is not already a regexp
481 - if ( ! str_starts_with( $pattern, '^' ) ) {
165 + if ( strpos( $pattern, '^' ) !== 0 ) {
482 166 $pattern = self::create_regular_expression_from_format( $pattern );
483 167 }
484 168
485 - return '/' . $pattern . '/';
169 + $pattern = '/' . $pattern . '/';
170 +
171 + return $pattern;
486 172 }
487 173
488 174 /**
489 175 * @since 3.01
490 - *
491 - * @return string
492 176 */
493 177 private static function default_phone_format() {
494 178 return '^((\+\d{1,3}(-|.| )?\(?\d\)?(-| |.)?\d{1,5})|(\(?\d{2,6}\)?))(-|.| )?(\d{3,4})(-|.| )?(\d{4})(( x| ext)\d{1,5}){0,1}$';
495 179 }
@@ -515,14 +199,13 @@
515 199 $pattern = str_replace( 'a', '[a-zA-Z]', $pattern );
516 200 $pattern = str_replace( '*', 'w', $pattern );
517 201 $pattern = str_replace( '/', '\/', $pattern );
518 202
519 - if ( str_contains( $pattern, '\?' ) ) {
203 + if ( strpos( $pattern, '\?' ) !== false ) {
520 204 $parts = explode( '\?', $pattern );
521 205 $pattern = '';
522 -
523 206 foreach ( $parts as $part ) {
524 - if ( ! $pattern ) {
207 + if ( empty( $pattern ) ) {
525 208 $pattern .= $part;
526 209 } else {
527 210 $pattern .= '(' . $part . ')?';
528 211 }
@@ -527,75 +210,41 @@
527 210 $pattern .= '(' . $part . ')?';
528 211 }
529 212 }
530 213 }
214 + $pattern = '^' . $pattern . '$';
531 215
532 - return '^' . $pattern . '$';
216 + return $pattern;
533 217 }
534 218
535 219 /**
536 - * Check for spam.
220 + * Check for spam
537 221 *
538 - * @param bool $exclude
222 + * @param boolean $exclude
539 223 * @param array $values
540 - * @param array $errors By reference.
541 - *
542 - * @return void
224 + * @param array $errors by reference
543 225 */
544 226 public static function spam_check( $exclude, $values, &$errors ) {
545 - if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
546 - // Do not check spam on importing.
547 - return;
548 - }
549 -
550 - if ( $exclude || empty( $values['item_meta'] ) || ! empty( $errors ) ) {
227 + if ( ! empty( $exclude ) || ! isset( $values['item_meta'] ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) {
551 228 // only check spam if there are no other errors
552 229 return;
553 230 }
554 231
555 232 $antispam_check = self::is_antispam_check( $values['form_id'] );
556 - $spam_msg = FrmAntiSpamController::get_default_spam_message();
557 -
558 233 if ( is_string( $antispam_check ) ) {
559 234 $errors['spam'] = $antispam_check;
560 235 } elseif ( self::is_honeypot_spam( $values ) || self::is_spam_bot() ) {
561 - $errors['spam'] = $spam_msg;
562 - } else {
563 - $is_spam = FrmAntiSpamController::is_spam( $values );
564 -
565 - if ( $is_spam ) {
566 - $errors['spam'] = $is_spam;
567 - }
568 - }
569 -
570 - if ( isset( $errors['spam'] ) || self::form_is_in_progress( $values ) ) {
571 - return;
572 - }
573 -
574 - if ( self::is_akismet_enabled_for_user( $values['form_id'] ) && self::is_akismet_spam( $values ) ) {
575 236 $errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' );
237 + } elseif ( self::blacklist_check( $values ) ) {
238 + $errors['spam'] = __( 'Your entry appears to be blocked spam!', 'formidable' );
239 + } elseif ( self::is_akismet_spam( $values ) && self::is_akismet_enabled_for_user( $values['form_id'] ) ) {
240 + $errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' );
576 241 }
577 242 }
578 243
579 244 /**
580 - * Checks if form is in progress.
581 - *
582 - * @since 5.0.13
583 - *
584 - * @param array $values The values.
585 - *
586 - * @return bool
587 - */
588 - private static function form_is_in_progress( $values ) {
589 - return FrmAppHelper::pro_is_installed() &&
590 - ( isset( $values[ 'frm_page_order_' . $values['form_id'] ] ) || FrmAppHelper::get_post_param( 'frm_next_page' ) ) &&
591 - FrmField::get_all_types_in_form( $values['form_id'], 'break' );
592 - }
593 -
594 - /**
595 245 * @param int $form_id
596 - *
597 - * @return bool|string
246 + * @return boolean
598 247 */
599 248 private static function is_antispam_check( $form_id ) {
600 249 $aspm = new FrmAntiSpam( $form_id );
601 250 return $aspm->validate();
@@ -602,10 +251,9 @@
602 251 }
603 252
604 253 /**
605 254 * @param array $values
606 - *
607 - * @return bool
255 + * @return boolean
608 256 */
609 257 private static function is_honeypot_spam( $values ) {
610 258 $honeypot = new FrmHoneypot( $values['form_id'] );
611 259 return ! $honeypot->validate();
@@ -611,102 +259,117 @@
611 259 return ! $honeypot->validate();
612 260 }
613 261
614 262 /**
615 - * @return bool
263 + * @return boolean
616 264 */
617 265 private static function is_spam_bot() {
618 266 $ip = FrmAppHelper::get_ip_address();
267 +
619 268 return empty( $ip );
620 269 }
621 270
622 271 /**
623 272 * @param array $values
624 - *
625 - * @return bool
273 + * @return boolean
626 274 */
627 275 private static function is_akismet_spam( $values ) {
628 276 global $wpcom_api_key;
629 - return is_callable( 'Akismet::http_post' ) && ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) && self::akismet( $values );
277 +
278 + return ( is_callable( 'Akismet::http_post' ) && ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) && self::akismet( $values ) );
630 279 }
631 280
632 281 /**
633 282 * @param int $form_id
634 - *
635 283 * @return bool
636 284 */
637 285 private static function is_akismet_enabled_for_user( $form_id ) {
638 286 $form = FrmForm::getOne( $form_id );
639 - return ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() );
287 +
288 + return ( isset( $form->options['akismet'] ) && ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() ) );
640 289 }
641 290
291 + public static function blacklist_check( $values ) {
292 + if ( ! apply_filters( 'frm_check_blacklist', true, $values ) ) {
293 + return false;
294 + }
295 +
296 + $mod_keys = trim( self::get_disallowed_words() );
297 + if ( empty( $mod_keys ) ) {
298 + return false;
299 + }
300 +
301 + $content = FrmEntriesHelper::entry_array_to_string( $values );
302 + if ( empty( $content ) ) {
303 + return false;
304 + }
305 +
306 + $ip = FrmAppHelper::get_ip_address();
307 + $user_agent = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' );
308 + $user_info = self::get_spam_check_user_info( $values );
309 +
310 + return self::check_disallowed_words( $user_info['comment_author'], $user_info['comment_author_email'], $user_info['comment_author_url'], $content, $ip, $user_agent );
311 + }
312 +
642 313 /**
643 - * Checks spam using WordPress disallowed words and Frm denylist.
314 + * For WP 5.5 compatibility.
644 315 *
645 - * @param array $values Entry values.
316 + * @since 4.06.02
317 + */
318 + private static function check_disallowed_words( $author, $email, $url, $content, $ip, $user_agent ) {
319 + if ( function_exists( 'wp_check_comment_disallowed_list' ) ) {
320 + return wp_check_comment_disallowed_list( $author, $email, $url, $content, $ip, $user_agent );
321 + } else {
322 + return wp_blacklist_check( $author, $email, $url, $content, $ip, $user_agent );
323 + }
324 + }
325 +
326 + /**
327 + * For WP 5.5 compatibility.
646 328 *
647 - * @return bool
329 + * @since 4.06.02
648 330 */
649 - public static function blacklist_check( $values ) {
650 - return FrmAntiSpamController::contains_wp_disallowed_words( $values ) || FrmAntiSpamController::is_denylist_spam( $values );
331 + private static function get_disallowed_words() {
332 + $keys = get_option( 'disallowed_keys' );
333 + if ( false === $keys ) {
334 + // Fallback for WP < 5.5.
335 + $keys = get_option( 'blacklist_keys' );
336 + }
337 + return $keys;
651 338 }
652 339
653 340 /**
654 341 * Check entries for Akismet spam
655 342 *
656 - * @param array $values Entry values.
657 - *
658 - * @return bool true if is spam
343 + * @return boolean true if is spam
659 344 */
660 345 public static function akismet( $values ) {
661 - if ( empty( $values['item_meta'] ) ) {
346 + $content = FrmEntriesHelper::entry_array_to_string( $values );
347 + if ( empty( $content ) ) {
662 348 return false;
663 349 }
664 350
665 351 $datas = array(
666 - 'comment_type' => 'formidable',
352 + 'comment_type' => 'formidable',
353 + 'comment_content' => $content,
667 354 );
668 355 self::parse_akismet_array( $datas, $values );
669 356
670 - /**
671 - * Allows modifying the values sent to Akismet.
672 - *
673 - * @since 5.0.07
674 - *
675 - * @param array $datas The array of values being sent to Akismet.
676 - */
677 - $datas = apply_filters( 'frm_akismet_values', $datas );
678 -
679 357 $query_string = _http_build_query( $datas, '', '&' );
680 358 $response = Akismet::http_post( $query_string, 'comment-check' );
681 359
682 - return is_array( $response ) && $response[1] === 'true';
360 + return ( is_array( $response ) && $response[1] == 'true' );
683 361 }
684 362
685 363 /**
686 364 * @since 2.0
687 - *
688 - * @param array $datas The array of values being sent to Akismet.
689 - * @param array $values Entry values.
690 - *
691 - * @return void
692 365 */
693 366 private static function parse_akismet_array( &$datas, $values ) {
694 367 self::add_site_info_to_akismet( $datas );
368 + self::add_user_info_to_akismet( $datas, $values );
695 369 self::add_server_values_to_akismet( $datas );
696 -
697 - self::prepare_values_for_spam_check( $values );
698 - self::skip_adding_values_to_akismet( $values );
699 -
700 - self::add_user_info_to_akismet( $datas, $values );
701 - self::add_comment_content_to_akismet( $datas, $values );
702 370 }
703 371
704 - /**
705 - * @param array $datas
706 - *
707 - * @return void
708 - */
709 372 private static function add_site_info_to_akismet( &$datas ) {
710 373 $datas['blog'] = FrmAppHelper::site_url();
711 374 $datas['user_ip'] = preg_replace( '/[^0-9., ]/', '', FrmAppHelper::get_ip_address() );
712 375 $datas['user_agent'] = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' );
@@ -718,14 +381,8 @@
718 381 $datas['is_test'] = 'true';
719 382 }
720 383 }
721 384
722 - /**
723 - * @param array $datas
724 - * @param array $values
725 - *
726 - * @return void
727 - */
728 385 private static function add_user_info_to_akismet( &$datas, $values ) {
729 386 $user_info = self::get_spam_check_user_info( $values );
730 387 $datas = $datas + $user_info;
731 388
@@ -733,194 +390,45 @@
733 390 $datas['user_role'] = Akismet::get_user_roles( $user_info['user_ID'] );
734 391 }
735 392 }
736 393
737 - /**
738 - * Gets user info for Akismet spam check.
739 - *
740 - * @since 5.0.13 Separate code for guest. Handle value of embedded|repeater.
741 - * @since 6.21 This changed from private to public.
742 - *
743 - * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}.
744 - *
745 - * @return array
746 - */
747 - public static function get_spam_check_user_info( $values ) {
748 - if ( ! is_user_logged_in() ) {
749 - return self::get_spam_check_user_info_for_guest( $values );
750 - }
394 + private static function get_spam_check_user_info( $values ) {
395 + $datas = array();
751 396
752 - $user = wp_get_current_user();
397 + if ( is_user_logged_in() ) {
398 + $user = wp_get_current_user();
753 399
754 - return array(
755 - 'user_ID' => $user->ID,
756 - 'user_id' => $user->ID,
757 - 'comment_author' => $user->display_name,
758 - 'comment_author_email' => $user->user_email,
759 - 'comment_author_url' => $user->user_url,
760 - );
761 - }
400 + $datas['user_ID'] = $user->ID;
401 + $datas['user_id'] = $user->ID;
402 + $datas['comment_author'] = $user->display_name;
403 + $datas['comment_author_email'] = $user->user_email;
404 + $datas['comment_author_url'] = $user->user_url;
405 + } else {
406 + $datas['comment_author'] = '';
407 + $datas['comment_author_email'] = '';
408 + $datas['comment_author_url'] = '';
762 409
763 - /**
764 - * Gets user info for Akismet spam check for guest.
765 - *
766 - * @since 5.0.13
767 - *
768 - * @param array $values Entry values after flattened.
769 - *
770 - * @return array
771 - */
772 - private static function get_spam_check_user_info_for_guest( $values ) {
773 - $datas = array(
774 - 'comment_author' => '',
775 - 'comment_author_email' => '',
776 - 'comment_author_url' => '',
777 - 'name_field_ids' => $values['name_field_ids'],
778 - 'missing_keys' => array( 'comment_author_email', 'comment_author_url', 'comment_author' ),
779 - 'frm_duplicated' => array(),
780 - );
781 -
782 - if ( isset( $values['item_meta'] ) ) {
783 - $values = $values['item_meta'];
784 - }
785 -
786 - $values = array_filter( $values );
787 -
788 - self::recursive_add_akismet_guest_info( $datas, $values );
789 - unset( $datas['name_field_ids'] );
790 - unset( $datas['missing_keys'] );
791 -
792 - return $datas;
793 - }
794 -
795 - /**
796 - * Recursive adds akismet guest info.
797 - *
798 - * @since 5.0.13
799 - *
800 - * @param array $datas Guest data.
801 - * @param array $values The values.
802 - * @param int|null $custom_index Custom index (or field ID).
803 - *
804 - * @return void
805 - */
806 - private static function recursive_add_akismet_guest_info( &$datas, $values, $custom_index = null ) {
807 - foreach ( $values as $index => $value ) {
808 - if ( ! $datas['missing_keys'] ) {
809 - // Found all info.
810 - return;
410 + if ( isset( $values['item_meta'] ) ) {
411 + $values = $values['item_meta'];
811 412 }
812 413
813 - if ( is_array( $value ) ) {
814 - self::recursive_add_akismet_guest_info( $datas, $value, $index );
815 - continue;
816 - }
817 -
818 - $field_id = ! is_null( $custom_index ) ? $custom_index : $index;
819 -
820 - foreach ( $datas['missing_keys'] as $key_index => $key ) {
821 - $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'], $values );
822 -
823 - if ( $found ) {
824 - $datas[ $key ] = $value;
825 - $datas['frm_duplicated'][] = $field_id;
826 - unset( $datas['missing_keys'][ $key_index ] );
414 + $values = array_filter( $values );
415 + foreach ( $values as $value ) {
416 + if ( ! is_array( $value ) ) {
417 + if ( $datas['comment_author_email'] == '' && strpos( $value, '@' ) && is_email( $value ) ) {
418 + $datas['comment_author_email'] = $value;
419 + } elseif ( $datas['comment_author_url'] == '' && strpos( $value, 'http' ) === 0 ) {
420 + $datas['comment_author_url'] = $value;
421 + } elseif ( $datas['comment_author'] == '' && ! is_numeric( $value ) && strlen( $value ) < 200 ) {
422 + $datas['comment_author'] = $value;
423 + }
827 424 }
828 425 }
829 - }//end foreach
830 - }
831 -
832 - /**
833 - * Checks if given value is an akismet guest info.
834 - *
835 - * @since 5.0.13
836 - *
837 - * @param string $key Guest info key.
838 - * @param string $value Value to check.
839 - * @param int $field_id Field ID.
840 - * @param array $name_field_ids Name field IDs.
841 - * @param array $values Array of posted values.
842 - *
843 - * @return bool
844 - */
845 - private static function is_akismet_guest_info_value( $key, &$value, $field_id, $name_field_ids, $values ) {
846 - if ( ! $value || is_numeric( $value ) ) {
847 - return false;
848 426 }
849 427
850 - switch ( $key ) {
851 - case 'comment_author_email':
852 - return str_contains( $value, '@' ) && is_email( $value );
853 -
854 - case 'comment_author_url':
855 - return str_starts_with( $value, 'http' );
856 -
857 - case 'comment_author':
858 - if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) {
859 - // If there is name field in the form, we should always use it as author name.
860 - return true;
861 - }
862 -
863 - $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
864 - $fields = self::get_name_text_fields( $form_id );
865 -
866 - foreach ( $fields as $index => $field ) {
867 - if ( 'Name' !== $field->name ) {
868 - continue;
869 - }
870 -
871 - if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) {
872 - if ( empty( $values[ absint( $fields[ $index + 1 ]->id ) ] ) ) {
873 - continue;
874 - }
875 -
876 - $value .= ' ' . $values[ $fields[ $index + 1 ]->id ];
877 - return true;
878 - }
879 - }
880 - }//end switch
881 -
882 - return false;
428 + return $datas;
883 429 }
884 430
885 - /**
886 - * Returns fields that have 'Name' and 'Last' as their name.
887 - *
888 - * @since 6.17
889 - *
890 - * @param int $form_id
891 - *
892 - * @return array
893 - */
894 - private static function get_name_text_fields( $form_id ) {
895 - $name_text_fields_is_initialized = is_array( self::$name_text_fields );
896 -
897 - if ( $name_text_fields_is_initialized && isset( self::$name_text_fields[ $form_id ] ) ) {
898 - return self::$name_text_fields[ $form_id ];
899 - }
900 -
901 - if ( ! $name_text_fields_is_initialized ) {
902 - self::$name_text_fields = array();
903 - }
904 - self::$name_text_fields[ $form_id ] = FrmDb::get_results(
905 - 'frm_fields',
906 - array(
907 - 'form_id' => $form_id,
908 - 'type' => 'text',
909 - 'name' => array( 'Name', 'Last' ),
910 - ),
911 - 'id,name',
912 - array( 'order_by' => 'field_order ASC' )
913 - );
914 -
915 - return self::$name_text_fields[ $form_id ];
916 - }
917 -
918 - /**
919 - * @param array $datas
920 - *
921 - * @return void
922 - */
923 431 private static function add_server_values_to_akismet( &$datas ) {
924 432 foreach ( $_SERVER as $key => $value ) {
925 433 $include_value = is_string( $value ) && ! preg_match( '/^HTTP_COOKIE/', $key ) && preg_match( '/^(HTTP_|REMOTE_ADDR|REQUEST_URI|DOCUMENT_URI)/', $key );
926 434
@@ -932,214 +440,35 @@
932 440 }
933 441 }
934 442
935 443 /**
936 - * Adds comment content to Akismet data.
937 - *
938 - * @since 5.0.09
939 - *
940 - * @param array $datas The array of values being sent to Akismet.
941 - * @param array $values Entry values.
942 - *
943 - * @return void
444 + * @deprecated 3.0
445 + * @codeCoverageIgnore
944 446 */
945 - private static function add_comment_content_to_akismet( &$datas, $values ) {
946 - if ( isset( $datas['frm_duplicated'] ) ) {
947 - foreach ( $datas['frm_duplicated'] as $index ) {
948 - if ( isset( $values['item_meta'][ $index ] ) ) {
949 - unset( $values['item_meta'][ $index ] );
950 - } else {
951 - unset( $values[ $index ] );
952 - }
953 - }
954 - unset( $datas['frm_duplicated'] );
955 - }
956 -
957 - $datas['comment_content'] = FrmEntriesHelper::entry_array_to_string( $values );
447 + public static function validate_url_field( &$errors, $field, $value, $args ) {
448 + FrmDeprecated::validate_url_field( $errors, $field, $value, $args );
958 449 }
959 450
960 451 /**
961 - * Skips adding field values to Akismet.
962 - *
963 - * @since 5.0.09
964 - *
965 - * @param array $values Entry values.
966 - *
967 - * @return void
452 + * @deprecated 3.0
453 + * @codeCoverageIgnore
968 454 */
969 - private static function skip_adding_values_to_akismet( &$values ) {
970 - $skipped_fields = self::get_akismet_skipped_field_ids( $values );
971 -
972 - foreach ( $skipped_fields as $skipped_field ) {
973 - if ( ! isset( $values['item_meta'][ $skipped_field->id ] ) ) {
974 - continue;
975 - }
976 -
977 - if ( self::should_really_skip_field( $skipped_field, $values ) ) {
978 - unset( $values['item_meta'][ $skipped_field->id ] );
979 -
980 - if ( isset( $values['item_meta']['other'][ $skipped_field->id ] ) ) {
981 - unset( $values['item_meta']['other'][ $skipped_field->id ] );
982 - }
983 - }
984 - }
455 + public static function validate_email_field( &$errors, $field, $value, $args ) {
456 + FrmDeprecated::validate_email_field( $errors, $field, $value, $args );
985 457 }
986 458
987 459 /**
988 - * Checks if a skip field should be really skipped.
989 - *
990 - * @since 5.02.04
991 - *
992 - * @param object $field_data Object contains `id` and `options`.
993 - * @param array $values Entry values.
994 - *
995 - * @return bool
460 + * @deprecated 3.0
461 + * @codeCoverageIgnore
996 462 */
997 - private static function should_really_skip_field( $field_data, $values ) {
998 - if ( empty( $field_data->options ) ) {
999 - // This is skipped field types.
1000 - return true;
1001 - }
1002 -
1003 - FrmAppHelper::unserialize_or_decode( $field_data->options );
1004 -
1005 - if ( ! $field_data->options ) {
1006 - // Check if an error happens when unserializing, or empty options.
1007 - return true;
1008 - }
1009 -
1010 - $last_key = array_key_last( $field_data->options );
1011 -
1012 - // If a choice field has no Other option.
1013 - if ( is_numeric( $last_key ) || ! str_starts_with( $last_key, 'other_' ) ) {
1014 - return true;
1015 - }
1016 -
1017 - // If a choice field has Other option, but Other is not selected.
1018 - if ( empty( $values['item_meta']['other'][ $field_data->id ] ) ) {
1019 - return true;
1020 - }
1021 -
1022 - // Check if submitted value is same as one of field option.
1023 - foreach ( $field_data->options as $option ) {
1024 - $option_value = ! is_array( $option ) ? $option : ( $option['value'] ?? '' );
1025 -
1026 - if ( $values['item_meta']['other'][ $field_data->id ] === $option_value ) {
1027 - return true;
1028 - }
1029 - }
1030 -
1031 - return false;
463 + public static function validate_number_field( &$errors, $field, $value, $args ) {
464 + FrmDeprecated::validate_number_field( $errors, $field, $value, $args );
1032 465 }
1033 466
1034 467 /**
1035 - * Gets field IDs that are skipped from sending to Akismet spam check.
1036 - *
1037 - * @since 5.0.09
1038 - * @since 5.0.13 Move out get_all_form_ids_and_flatten_meta() call and get `form_ids` from `$values`.
1039 - * @since 5.2.04 This method returns array of object contains `id` and `options` instead of array of `id` only.
1040 - *
1041 - * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}.
1042 - *
1043 - * @return array
468 + * @deprecated 3.0
469 + * @codeCoverageIgnore
1044 470 */
1045 - private static function get_akismet_skipped_field_ids( $values ) {
1046 - if ( empty( $values['form_ids'] ) ) {
1047 - return array();
1048 - }
1049 -
1050 - $skipped_types = array( 'divider', 'form', 'hidden', 'user_id', 'file', 'date', 'time', 'scale', 'star', 'range', 'toggle', 'data', 'lookup', 'likert', 'nps' );
1051 - $has_other_types = array( 'radio', 'checkbox', 'select' );
1052 -
1053 - $where = array(
1054 - array(
1055 - 'form_id' => $values['form_ids'],
1056 - 'type' => array_merge( $skipped_types, $has_other_types ),
1057 - ),
1058 - );
1059 -
1060 - return FrmDb::get_results( 'frm_fields', $where, 'id,options' );
1061 - }
1062 -
1063 - /**
1064 - * Prepares values array for spam check.
1065 - *
1066 - * @since 5.0.13
1067 - * @since 6.21 This changed from private to public.
1068 - *
1069 - * @param array $values Entry values.
1070 - *
1071 - * @return void
1072 - */
1073 - public static function prepare_values_for_spam_check( &$values ) {
1074 - $form_ids = self::get_all_form_ids_and_flatten_meta( $values );
1075 - $values['form_ids'] = $form_ids;
1076 - }
1077 -
1078 - /**
1079 - * Gets all form IDs (include child form IDs) and flatten item_meta array. Used for skipping values sent to Akismet.
1080 - * This also removes some unused data from the item_meta.
1081 - *
1082 - * @since 5.0.09
1083 - * @since 5.0.13 Convert name field value to string.
1084 - *
1085 - * @param array $values Entry values.
1086 - *
1087 - * @return array Form IDs.
1088 - */
1089 - private static function get_all_form_ids_and_flatten_meta( &$values ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
1090 - $values['name_field_ids'] = array();
1091 -
1092 - // Blacklist check for File field in the old version doesn't contain `form_id`.
1093 - $form_ids = isset( $values['form_id'] ) ? array( absint( $values['form_id'] ) ) : array();
1094 -
1095 - foreach ( $values['item_meta'] as $field_id => $value ) {
1096 - if ( ! is_numeric( $field_id ) ) {
1097 - // Maybe `other`.
1098 - continue;
1099 - }
1100 -
1101 - // Convert name array to string.
1102 - if ( isset( $value['first'] ) && isset( $value['last'] ) ) {
1103 - $values['item_meta'][ $field_id ] = trim( implode( ' ', $value ) );
1104 - $values['name_field_ids'][] = $field_id;
1105 - continue;
1106 - }
1107 -
1108 - if ( ! is_array( $value ) || empty( $value['form'] ) ) {
1109 - continue;
1110 - }
1111 -
1112 - $form_ids[] = absint( $value['form'] );
1113 -
1114 - foreach ( $value as $subindex => $subvalue ) {
1115 - if ( ! is_numeric( $subindex ) || ! is_array( $subvalue ) ) {
1116 - continue;
1117 - }
1118 -
1119 - foreach ( $subvalue as $subsubindex => $subsubvalue ) {
1120 - if ( ! $subsubvalue ) {
1121 - continue;
1122 - }
1123 -
1124 - if ( ! isset( $values['item_meta'][ $subsubindex ] ) ) {
1125 - $values['item_meta'][ $subsubindex ] = array();
1126 - }
1127 -
1128 - // Convert name array to string.
1129 - if ( isset( $subsubvalue['first'] ) && isset( $subsubvalue['last'] ) ) {
1130 - $subsubvalue = trim( implode( ' ', $subsubvalue ) );
1131 - $values['name_field_ids'][] = $subsubindex;
1132 - }
1133 -
1134 - if ( is_array( $values['item_meta'][ $subsubindex ] ) ) {
1135 - $values['item_meta'][ $subsubindex ][] = $subsubvalue;
1136 - }
1137 - }
1138 - }//end foreach
1139 -
1140 - unset( $values['item_meta'][ $field_id ] );
1141 - }//end foreach
1142 -
1143 - return $form_ids;
471 + public static function validate_recaptcha( &$errors, $field, $args ) {
472 + FrmDeprecated::validate_recaptcha( $errors, $field, $args );
1144 473 }
1145 474 }