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