PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.3
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.3
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 +150 -469 6.275.3 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,41 +43,14 @@
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 53 if ( ! isset( $values['item_key'] ) || $values['item_key'] == '' ) {
95 54 global $wpdb;
96 55 $values['item_key'] = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_items', 'item_key' );
97 56 $_POST['item_key'] = $values['item_key'];
@@ -97,14 +56,8 @@
97 56 $_POST['item_key'] = $values['item_key'];
98 57 }
99 58 }
100 59
101 - /**
102 - * @param array $values
103 - * @param array|string $exclude
104 - *
105 - * @return array
106 - */
107 60 private static function get_fields_to_validate( $values, $exclude ) {
108 61 $where = apply_filters( 'frm_posted_field_ids', array( 'fi.form_id' => $values['form_id'] ) );
109 62
110 63 // Don't get subfields
@@ -110,9 +63,9 @@
110 63 // Don't get subfields
111 64 $where['fr.parent_form_id'] = array( null, 0 );
112 65
113 66 // Don't get excluded fields (like file upload fields in the ajax validation)
114 - if ( $exclude ) {
67 + if ( ! empty( $exclude ) ) {
115 68 $where['fi.type not'] = $exclude;
116 69 }
117 70
118 71 $fields = FrmField::getAll( $where, 'field_order' );
@@ -127,29 +80,23 @@
127 80 */
128 81 return apply_filters( 'frm_fields_to_validate', $fields, compact( 'values', 'exclude', 'where' ) );
129 82 }
130 83
131 - /**
132 - * @param object $posted_field
133 - * @param array $errors
134 - * @param array $values
135 - * @param array $args
136 - *
137 - * @return void
138 - */
139 84 public static function validate_field( $posted_field, &$errors, $values, $args = array() ) {
140 85 $defaults = array(
141 86 '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(),
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 );
148 92
149 - );
150 - $args = wp_parse_args( $args, $defaults );
151 - $value = empty( $args['parent_field_id'] ) ? ( $values['item_meta'][ $args['id'] ] ?? '' ) : $values;
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 + }
152 99
153 100 // Check for values in "Other" fields
154 101 FrmEntriesHelper::maybe_set_other_validation( $posted_field, $value, $args );
155 102
@@ -154,10 +101,9 @@
154 101 FrmEntriesHelper::maybe_set_other_validation( $posted_field, $value, $args );
155 102
156 103 self::maybe_clear_value_for_default_blank_setting( $posted_field, $value );
157 104
158 - $should_trim = is_array( $value ) && count( $value ) === 1 && isset( $value[0] ) && $posted_field->type !== 'checkbox';
159 -
105 + $should_trim = is_array( $value ) && count( $value ) == 1 && isset( $value[0] ) && $posted_field->type !== 'checkbox';
160 106 if ( $should_trim ) {
161 107 $value = reset( $value );
162 108 }
163 109
@@ -164,9 +110,8 @@
164 110 if ( ! is_array( $value ) ) {
165 111 $value = trim( $value );
166 112 }
167 113
168 - // phpcs:ignore Universal.Operators.StrictComparisons
169 114 if ( $posted_field->required == '1' && FrmAppHelper::is_empty_value( $value ) ) {
170 115 $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $posted_field, 'blank' );
171 116 } elseif ( ! isset( $_POST['item_name'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
172 117 self::maybe_add_item_name( $value, $posted_field );
@@ -173,9 +118,8 @@
173 118 }
174 119
175 120 FrmEntriesHelper::set_posted_value( $posted_field, $value, $args );
176 121
177 - self::validate_options( $errors, $posted_field, $value, $args );
178 122 self::validate_field_types( $errors, $posted_field, $value, $args );
179 123
180 124 // Field might want to modify value before other parts of the system
181 125 // e.g. trim off excess values like in the case of fields with limit.
@@ -180,9 +124,8 @@
180 124 // Field might want to modify value before other parts of the system
181 125 // e.g. trim off excess values like in the case of fields with limit.
182 126 $value = apply_filters( 'frm_modify_posted_field_value', $value, $errors, $posted_field, $args );
183 127
184 - // phpcs:ignore Universal.Operators.StrictComparisons
185 128 if ( $value != '' ) {
186 129 self::validate_phone_field( $errors, $posted_field, $value, $args );
187 130 }
188 131
@@ -187,207 +130,19 @@
187 130 }
188 131
189 132 $errors = apply_filters( 'frm_validate_' . $posted_field->type . '_field_entry', $errors, $posted_field, $value, $args );
190 133 $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 134 }
196 135
197 136 /**
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 137 * Maybe add item_name to $_POST to save it in items table.
379 138 *
380 139 * @since 5.2.02
381 140 *
382 - * @param array|string $value Field value.
383 - * @param object $field Field object.
384 - *
385 - * @return void
141 + * @param object $field Field object.
386 142 */
387 143 private static function maybe_add_item_name( $value, $field ) {
388 144 $item_name = false;
389 -
390 145 if ( 'name' === $field->type ) {
391 146 $field_obj = FrmFieldFactory::get_field_object( $field );
392 147 $item_name = $field_obj->get_display_value( $value );
393 148 } elseif ( 'text' === $field->type ) {
@@ -394,10 +149,9 @@
394 149 $item_name = $value;
395 150 }
396 151
397 152 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 );
153 + $_POST['item_name'] = $item_name;
400 154 }
401 155 }
402 156
403 157 /**
@@ -404,14 +158,11 @@
404 158 * Set $value to an empty string if it matches its label
405 159 *
406 160 * @param object $field
407 161 * @param string $value
408 - *
409 - * @return void
410 162 */
411 163 private static function maybe_clear_value_for_default_blank_setting( $field, &$value ) {
412 164 $position = FrmField::get_option( $field, 'label' );
413 -
414 165 if ( ! $position ) {
415 166 $position = FrmStylesController::get_style_val( 'position', $field->form_id );
416 167 }
417 168
@@ -419,16 +170,8 @@
419 170 $value = '';
420 171 }
421 172 }
422 173
423 - /**
424 - * @param array $errors
425 - * @param object $posted_field
426 - * @param mixed $value
427 - * @param array $args
428 - *
429 - * @return void
430 - */
431 174 public static function validate_field_types( &$errors, $posted_field, $value, $args ) {
432 175 $field_obj = FrmFieldFactory::get_field_object( $posted_field );
433 176 $args['value'] = $value;
434 177 $args['errors'] = $errors;
@@ -433,26 +176,16 @@
433 176 $args['value'] = $value;
434 177 $args['errors'] = $errors;
435 178
436 179 $new_errors = $field_obj->validate( $args );
437 -
438 - if ( $new_errors ) {
180 + if ( ! empty( $new_errors ) ) {
439 181 $errors = array_merge( $errors, $new_errors );
440 182 }
441 183 }
442 184
443 - /**
444 - * @param array $errors
445 - * @param object $field
446 - * @param string $value
447 - * @param array $args
448 - *
449 - * @return void
450 - */
451 185 public static function validate_phone_field( &$errors, $field, $value, $args ) {
452 - $format_value = FrmField::get_option( $field, 'format' );
186 + if ( $field->type == 'phone' || ( $field->type == 'text' && FrmField::is_option_true_in_object( $field, 'format' ) ) ) {
453 187
454 - if ( $field->type === 'phone' || ( $field->type === 'text' && $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value ) ) ) {
455 188 $pattern = self::phone_format( $field );
456 189
457 190 if ( ! preg_match( $pattern, $value ) ) {
458 191 $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
@@ -459,13 +192,8 @@
459 192 }
460 193 }
461 194 }
462 195
463 - /**
464 - * @param object $field
465 - *
466 - * @return string
467 - */
468 196 public static function phone_format( $field ) {
469 197 if ( FrmField::is_option_empty( $field, 'format' ) ) {
470 198 $pattern = self::default_phone_format();
471 199 } else {
@@ -471,25 +199,22 @@
471 199 } else {
472 200 $pattern = FrmField::get_option( $field, 'format' );
473 201 }
474 202
475 - // Ampersands are saved as &.
476 - // Reverse it here so we are checking for the correct character.
477 - $pattern = html_entity_decode( $pattern );
478 203 $pattern = apply_filters( 'frm_phone_pattern', $pattern, $field );
479 204
480 205 // Create a regexp if format is not already a regexp
481 - if ( ! str_starts_with( $pattern, '^' ) ) {
206 + if ( strpos( $pattern, '^' ) !== 0 ) {
482 207 $pattern = self::create_regular_expression_from_format( $pattern );
483 208 }
484 209
485 - return '/' . $pattern . '/';
210 + $pattern = '/' . $pattern . '/';
211 +
212 + return $pattern;
486 213 }
487 214
488 215 /**
489 216 * @since 3.01
490 - *
491 - * @return string
492 217 */
493 218 private static function default_phone_format() {
494 219 return '^((\+\d{1,3}(-|.| )?\(?\d\)?(-| |.)?\d{1,5})|(\(?\d{2,6}\)?))(-|.| )?(\d{3,4})(-|.| )?(\d{4})(( x| ext)\d{1,5}){0,1}$';
495 220 }
@@ -515,14 +240,13 @@
515 240 $pattern = str_replace( 'a', '[a-zA-Z]', $pattern );
516 241 $pattern = str_replace( '*', 'w', $pattern );
517 242 $pattern = str_replace( '/', '\/', $pattern );
518 243
519 - if ( str_contains( $pattern, '\?' ) ) {
244 + if ( strpos( $pattern, '\?' ) !== false ) {
520 245 $parts = explode( '\?', $pattern );
521 246 $pattern = '';
522 -
523 247 foreach ( $parts as $part ) {
524 - if ( ! $pattern ) {
248 + if ( empty( $pattern ) ) {
525 249 $pattern .= $part;
526 250 } else {
527 251 $pattern .= '(' . $part . ')?';
528 252 }
@@ -527,45 +251,33 @@
527 251 $pattern .= '(' . $part . ')?';
528 252 }
529 253 }
530 254 }
255 + $pattern = '^' . $pattern . '$';
531 256
532 - return '^' . $pattern . '$';
257 + return $pattern;
533 258 }
534 259
535 260 /**
536 - * Check for spam.
261 + * Check for spam
537 262 *
538 - * @param bool $exclude
263 + * @param boolean $exclude
539 264 * @param array $values
540 - * @param array $errors By reference.
541 - *
542 - * @return void
265 + * @param array $errors by reference
543 266 */
544 267 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 ) ) {
268 + if ( ! empty( $exclude ) || ! isset( $values['item_meta'] ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) {
551 269 // only check spam if there are no other errors
552 270 return;
553 271 }
554 272
555 273 $antispam_check = self::is_antispam_check( $values['form_id'] );
556 - $spam_msg = FrmAntiSpamController::get_default_spam_message();
557 -
558 274 if ( is_string( $antispam_check ) ) {
559 275 $errors['spam'] = $antispam_check;
560 276 } 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 - }
277 + $errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' );
278 + } elseif ( self::blacklist_check( $values ) ) {
279 + $errors['spam'] = __( 'Your entry appears to be blocked spam!', 'formidable' );
568 280 }
569 281
570 282 if ( isset( $errors['spam'] ) || self::form_is_in_progress( $values ) ) {
571 283 return;
@@ -581,9 +293,8 @@
581 293 *
582 294 * @since 5.0.13
583 295 *
584 296 * @param array $values The values.
585 - *
586 297 * @return bool
587 298 */
588 299 private static function form_is_in_progress( $values ) {
589 300 return FrmAppHelper::pro_is_installed() &&
@@ -592,10 +303,9 @@
592 303 }
593 304
594 305 /**
595 306 * @param int $form_id
596 - *
597 - * @return bool|string
307 + * @return boolean
598 308 */
599 309 private static function is_antispam_check( $form_id ) {
600 310 $aspm = new FrmAntiSpam( $form_id );
601 311 return $aspm->validate();
@@ -602,10 +312,9 @@
602 312 }
603 313
604 314 /**
605 315 * @param array $values
606 - *
607 - * @return bool
316 + * @return boolean
608 317 */
609 318 private static function is_honeypot_spam( $values ) {
610 319 $honeypot = new FrmHoneypot( $values['form_id'] );
611 320 return ! $honeypot->validate();
@@ -611,52 +320,90 @@
611 320 return ! $honeypot->validate();
612 321 }
613 322
614 323 /**
615 - * @return bool
324 + * @return boolean
616 325 */
617 326 private static function is_spam_bot() {
618 327 $ip = FrmAppHelper::get_ip_address();
328 +
619 329 return empty( $ip );
620 330 }
621 331
622 332 /**
623 333 * @param array $values
624 - *
625 - * @return bool
334 + * @return boolean
626 335 */
627 336 private static function is_akismet_spam( $values ) {
628 337 global $wpcom_api_key;
629 - return is_callable( 'Akismet::http_post' ) && ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) && self::akismet( $values );
338 +
339 + return ( is_callable( 'Akismet::http_post' ) && ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) && self::akismet( $values ) );
630 340 }
631 341
632 342 /**
633 343 * @param int $form_id
634 - *
635 344 * @return bool
636 345 */
637 346 private static function is_akismet_enabled_for_user( $form_id ) {
638 347 $form = FrmForm::getOne( $form_id );
639 - return ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() );
348 +
349 + return ( ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() ) );
640 350 }
641 351
352 + public static function blacklist_check( $values ) {
353 + if ( ! apply_filters( 'frm_check_blacklist', true, $values ) ) {
354 + return false;
355 + }
356 +
357 + $mod_keys = trim( self::get_disallowed_words() );
358 + if ( empty( $mod_keys ) ) {
359 + return false;
360 + }
361 +
362 + $content = FrmEntriesHelper::entry_array_to_string( $values );
363 + if ( empty( $content ) ) {
364 + return false;
365 + }
366 +
367 + self::prepare_values_for_spam_check( $values );
368 + $ip = FrmAppHelper::get_ip_address();
369 + $user_agent = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' );
370 + $user_info = self::get_spam_check_user_info( $values );
371 +
372 + return self::check_disallowed_words( $user_info['comment_author'], $user_info['comment_author_email'], $user_info['comment_author_url'], $content, $ip, $user_agent );
373 + }
374 +
642 375 /**
643 - * Checks spam using WordPress disallowed words and Frm denylist.
376 + * For WP 5.5 compatibility.
644 377 *
645 - * @param array $values Entry values.
378 + * @since 4.06.02
379 + */
380 + private static function check_disallowed_words( $author, $email, $url, $content, $ip, $user_agent ) {
381 + if ( function_exists( 'wp_check_comment_disallowed_list' ) ) {
382 + return wp_check_comment_disallowed_list( $author, $email, $url, $content, $ip, $user_agent );
383 + } else {
384 + return wp_blacklist_check( $author, $email, $url, $content, $ip, $user_agent );
385 + }
386 + }
387 +
388 + /**
389 + * For WP 5.5 compatibility.
646 390 *
647 - * @return bool
391 + * @since 4.06.02
648 392 */
649 - public static function blacklist_check( $values ) {
650 - return FrmAntiSpamController::contains_wp_disallowed_words( $values ) || FrmAntiSpamController::is_denylist_spam( $values );
393 + private static function get_disallowed_words() {
394 + $keys = get_option( 'disallowed_keys' );
395 + if ( false === $keys ) {
396 + // Fallback for WP < 5.5.
397 + $keys = get_option( 'blacklist_keys' );
398 + }
399 + return $keys;
651 400 }
652 401
653 402 /**
654 403 * Check entries for Akismet spam
655 404 *
656 - * @param array $values Entry values.
657 - *
658 - * @return bool true if is spam
405 + * @return boolean true if is spam
659 406 */
660 407 public static function akismet( $values ) {
661 408 if ( empty( $values['item_meta'] ) ) {
662 409 return false;
@@ -678,18 +425,13 @@
678 425
679 426 $query_string = _http_build_query( $datas, '', '&' );
680 427 $response = Akismet::http_post( $query_string, 'comment-check' );
681 428
682 - return is_array( $response ) && $response[1] === 'true';
429 + return ( is_array( $response ) && $response[1] == 'true' );
683 430 }
684 431
685 432 /**
686 433 * @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 434 */
693 435 private static function parse_akismet_array( &$datas, $values ) {
694 436 self::add_site_info_to_akismet( $datas );
695 437 self::add_server_values_to_akismet( $datas );
@@ -694,19 +436,13 @@
694 436 self::add_site_info_to_akismet( $datas );
695 437 self::add_server_values_to_akismet( $datas );
696 438
697 439 self::prepare_values_for_spam_check( $values );
698 - self::skip_adding_values_to_akismet( $values );
699 440
700 441 self::add_user_info_to_akismet( $datas, $values );
701 442 self::add_comment_content_to_akismet( $datas, $values );
702 443 }
703 444
704 - /**
705 - * @param array $datas
706 - *
707 - * @return void
708 - */
709 445 private static function add_site_info_to_akismet( &$datas ) {
710 446 $datas['blog'] = FrmAppHelper::site_url();
711 447 $datas['user_ip'] = preg_replace( '/[^0-9., ]/', '', FrmAppHelper::get_ip_address() );
712 448 $datas['user_agent'] = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' );
@@ -718,14 +454,8 @@
718 454 $datas['is_test'] = 'true';
719 455 }
720 456 }
721 457
722 - /**
723 - * @param array $datas
724 - * @param array $values
725 - *
726 - * @return void
727 - */
728 458 private static function add_user_info_to_akismet( &$datas, $values ) {
729 459 $user_info = self::get_spam_check_user_info( $values );
730 460 $datas = $datas + $user_info;
731 461
@@ -737,15 +467,13 @@
737 467 /**
738 468 * Gets user info for Akismet spam check.
739 469 *
740 470 * @since 5.0.13 Separate code for guest. Handle value of embedded|repeater.
741 - * @since 6.21 This changed from private to public.
742 471 *
743 472 * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}.
744 - *
745 473 * @return array
746 474 */
747 - public static function get_spam_check_user_info( $values ) {
475 + private static function get_spam_check_user_info( $values ) {
748 476 if ( ! is_user_logged_in() ) {
749 477 return self::get_spam_check_user_info_for_guest( $values );
750 478 }
751 479
@@ -765,9 +493,8 @@
765 493 *
766 494 * @since 5.0.13
767 495 *
768 496 * @param array $values Entry values after flattened.
769 - *
770 497 * @return array
771 498 */
772 499 private static function get_spam_check_user_info_for_guest( $values ) {
773 500 $datas = array(
@@ -799,16 +526,13 @@
799 526 *
800 527 * @param array $datas Guest data.
801 528 * @param array $values The values.
802 529 * @param int|null $custom_index Custom index (or field ID).
803 - *
804 - * @return void
805 530 */
806 531 private static function recursive_add_akismet_guest_info( &$datas, $values, $custom_index = null ) {
807 532 foreach ( $values as $index => $value ) {
808 533 if ( ! $datas['missing_keys'] ) {
809 - // Found all info.
810 - return;
534 + return; // Found all info.
811 535 }
812 536
813 537 if ( is_array( $value ) ) {
814 538 self::recursive_add_akismet_guest_info( $datas, $value, $index );
@@ -815,12 +539,10 @@
815 539 continue;
816 540 }
817 541
818 542 $field_id = ! is_null( $custom_index ) ? $custom_index : $index;
819 -
820 543 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 -
544 + $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'] );
823 545 if ( $found ) {
824 546 $datas[ $key ] = $value;
825 547 $datas['frm_duplicated'][] = $field_id;
826 548 unset( $datas['missing_keys'][ $key_index ] );
@@ -825,9 +547,9 @@
825 547 $datas['frm_duplicated'][] = $field_id;
826 548 unset( $datas['missing_keys'][ $key_index ] );
827 549 }
828 550 }
829 - }//end foreach
551 + }
830 552 }
831 553
832 554 /**
833 555 * Checks if given value is an akismet guest info.
@@ -837,13 +559,11 @@
837 559 * @param string $key Guest info key.
838 560 * @param string $value Value to check.
839 561 * @param int $field_id Field ID.
840 562 * @param array $name_field_ids Name field IDs.
841 - * @param array $values Array of posted values.
842 - *
843 563 * @return bool
844 564 */
845 - private static function is_akismet_guest_info_value( $key, &$value, $field_id, $name_field_ids, $values ) {
565 + private static function is_akismet_guest_info_value( $key, $value, $field_id, $name_field_ids ) {
846 566 if ( ! $value || is_numeric( $value ) ) {
847 567 return false;
848 568 }
849 569
@@ -848,79 +568,24 @@
848 568 }
849 569
850 570 switch ( $key ) {
851 571 case 'comment_author_email':
852 - return str_contains( $value, '@' ) && is_email( $value );
572 + return strpos( $value, '@' ) && is_email( $value );
853 573
854 574 case 'comment_author_url':
855 - return str_starts_with( $value, 'http' );
575 + return 0 === strpos( $value, 'http' );
856 576
857 577 case 'comment_author':
858 - if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) {
578 + if ( $name_field_ids ) {
859 579 // If there is name field in the form, we should always use it as author name.
860 - return true;
580 + return in_array( $field_id, $name_field_ids, true );
861 581 }
582 + return strlen( $value ) < 200;
583 + }
862 584
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 585 return false;
883 586 }
884 587
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 588 private static function add_server_values_to_akismet( &$datas ) {
924 589 foreach ( $_SERVER as $key => $value ) {
925 590 $include_value = is_string( $value ) && ! preg_match( '/^HTTP_COOKIE/', $key ) && preg_match( '/^(HTTP_|REMOTE_ADDR|REQUEST_URI|DOCUMENT_URI)/', $key );
926 591
@@ -938,10 +603,8 @@
938 603 * @since 5.0.09
939 604 *
940 605 * @param array $datas The array of values being sent to Akismet.
941 606 * @param array $values Entry values.
942 - *
943 - * @return void
944 607 */
945 608 private static function add_comment_content_to_akismet( &$datas, $values ) {
946 609 if ( isset( $datas['frm_duplicated'] ) ) {
947 610 foreach ( $datas['frm_duplicated'] as $index ) {
@@ -953,8 +616,10 @@
953 616 }
954 617 unset( $datas['frm_duplicated'] );
955 618 }
956 619
620 + self::skip_adding_values_to_akismet( $values );
621 +
957 622 $datas['comment_content'] = FrmEntriesHelper::entry_array_to_string( $values );
958 623 }
959 624
960 625 /**
@@ -962,14 +627,11 @@
962 627 *
963 628 * @since 5.0.09
964 629 *
965 630 * @param array $values Entry values.
966 - *
967 - * @return void
968 631 */
969 632 private static function skip_adding_values_to_akismet( &$values ) {
970 633 $skipped_fields = self::get_akismet_skipped_field_ids( $values );
971 -
972 634 foreach ( $skipped_fields as $skipped_field ) {
973 635 if ( ! isset( $values['item_meta'][ $skipped_field->id ] ) ) {
974 636 continue;
975 637 }
@@ -975,9 +637,8 @@
975 637 }
976 638
977 639 if ( self::should_really_skip_field( $skipped_field, $values ) ) {
978 640 unset( $values['item_meta'][ $skipped_field->id ] );
979 -
980 641 if ( isset( $values['item_meta']['other'][ $skipped_field->id ] ) ) {
981 642 unset( $values['item_meta']['other'][ $skipped_field->id ] );
982 643 }
983 644 }
@@ -990,28 +651,25 @@
990 651 * @since 5.02.04
991 652 *
992 653 * @param object $field_data Object contains `id` and `options`.
993 654 * @param array $values Entry values.
994 - *
995 655 * @return bool
996 656 */
997 657 private static function should_really_skip_field( $field_data, $values ) {
998 - if ( empty( $field_data->options ) ) {
999 - // This is skipped field types.
658 + if ( empty( $field_data->options ) ) { // This is skipped field types.
1000 659 return true;
1001 660 }
1002 661
1003 662 FrmAppHelper::unserialize_or_decode( $field_data->options );
1004 -
1005 - if ( ! $field_data->options ) {
1006 - // Check if an error happens when unserializing, or empty options.
663 + if ( ! $field_data->options ) { // Check if an error happens when unserializing, or empty options.
1007 664 return true;
1008 665 }
1009 666
1010 - $last_key = array_key_last( $field_data->options );
667 + end( $field_data->options );
668 + $last_key = key( $field_data->options );
1011 669
1012 670 // If a choice field has no Other option.
1013 - if ( is_numeric( $last_key ) || ! str_starts_with( $last_key, 'other_' ) ) {
671 + if ( is_numeric( $last_key ) || 0 !== strpos( $last_key, 'other_' ) ) {
1014 672 return true;
1015 673 }
1016 674
1017 675 // If a choice field has Other option, but Other is not selected.
@@ -1020,10 +678,9 @@
1020 678 }
1021 679
1022 680 // Check if submitted value is same as one of field option.
1023 681 foreach ( $field_data->options as $option ) {
1024 - $option_value = ! is_array( $option ) ? $option : ( $option['value'] ?? '' );
1025 -
682 + $option_value = ! is_array( $option ) ? $option : ( isset( $option['value'] ) ? $option['value'] : '' );
1026 683 if ( $values['item_meta']['other'][ $field_data->id ] === $option_value ) {
1027 684 return true;
1028 685 }
1029 686 }
@@ -1038,9 +695,8 @@
1038 695 * @since 5.0.13 Move out get_all_form_ids_and_flatten_meta() call and get `form_ids` from `$values`.
1039 696 * @since 5.2.04 This method returns array of object contains `id` and `options` instead of array of `id` only.
1040 697 *
1041 698 * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}.
1042 - *
1043 699 * @return array
1044 700 */
1045 701 private static function get_akismet_skipped_field_ids( $values ) {
1046 702 if ( empty( $values['form_ids'] ) ) {
@@ -1063,15 +719,12 @@
1063 719 /**
1064 720 * Prepares values array for spam check.
1065 721 *
1066 722 * @since 5.0.13
1067 - * @since 6.21 This changed from private to public.
1068 723 *
1069 724 * @param array $values Entry values.
1070 - *
1071 - * @return void
1072 725 */
1073 - public static function prepare_values_for_spam_check( &$values ) {
726 + private static function prepare_values_for_spam_check( &$values ) {
1074 727 $form_ids = self::get_all_form_ids_and_flatten_meta( $values );
1075 728 $values['form_ids'] = $form_ids;
1076 729 }
1077 730
@@ -1082,20 +735,17 @@
1082 735 * @since 5.0.09
1083 736 * @since 5.0.13 Convert name field value to string.
1084 737 *
1085 738 * @param array $values Entry values.
1086 - *
1087 739 * @return array Form IDs.
1088 740 */
1089 - private static function get_all_form_ids_and_flatten_meta( &$values ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
741 + private static function get_all_form_ids_and_flatten_meta( &$values ) {
1090 742 $values['name_field_ids'] = array();
1091 743
1092 744 // Blacklist check for File field in the old version doesn't contain `form_id`.
1093 745 $form_ids = isset( $values['form_id'] ) ? array( absint( $values['form_id'] ) ) : array();
1094 -
1095 746 foreach ( $values['item_meta'] as $field_id => $value ) {
1096 - if ( ! is_numeric( $field_id ) ) {
1097 - // Maybe `other`.
747 + if ( ! is_numeric( $field_id ) ) { // Maybe `other`.
1098 748 continue;
1099 749 }
1100 750
1101 751 // Convert name array to string.
@@ -1126,20 +776,51 @@
1126 776 }
1127 777
1128 778 // Convert name array to string.
1129 779 if ( isset( $subsubvalue['first'] ) && isset( $subsubvalue['last'] ) ) {
1130 - $subsubvalue = trim( implode( ' ', $subsubvalue ) );
780 + $subsubvalue = trim( implode( ' ', $subsubvalue ) );
781 +
1131 782 $values['name_field_ids'][] = $subsubindex;
1132 783 }
1133 784
1134 - if ( is_array( $values['item_meta'][ $subsubindex ] ) ) {
1135 - $values['item_meta'][ $subsubindex ][] = $subsubvalue;
1136 - }
785 + $values['item_meta'][ $subsubindex ][] = $subsubvalue;
1137 786 }
1138 - }//end foreach
787 + }
1139 788
1140 789 unset( $values['item_meta'][ $field_id ] );
1141 - }//end foreach
790 + }
1142 791
1143 792 return $form_ids;
793 + }
794 +
795 + /**
796 + * @deprecated 3.0
797 + * @codeCoverageIgnore
798 + */
799 + public static function validate_url_field( &$errors, $field, $value, $args ) {
800 + FrmDeprecated::validate_url_field( $errors, $field, $value, $args );
801 + }
802 +
803 + /**
804 + * @deprecated 3.0
805 + * @codeCoverageIgnore
806 + */
807 + public static function validate_email_field( &$errors, $field, $value, $args ) {
808 + FrmDeprecated::validate_email_field( $errors, $field, $value, $args );
809 + }
810 +
811 + /**
812 + * @deprecated 3.0
813 + * @codeCoverageIgnore
814 + */
815 + public static function validate_number_field( &$errors, $field, $value, $args ) {
816 + FrmDeprecated::validate_number_field( $errors, $field, $value, $args );
817 + }
818 +
819 + /**
820 + * @deprecated 3.0
821 + * @codeCoverageIgnore
822 + */
823 + public static function validate_recaptcha( &$errors, $field, $args ) {
824 + FrmDeprecated::validate_recaptcha( $errors, $field, $args );
1144 825 }
1145 826 }