| @@ -5,15 +5,8 @@ | ||
| 5 | 5 | |
| 6 | 6 | class FrmEntryValidate { |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | - * @since 6.17 | |
| 10 | - * | |
| 11 | - * @var array|null | |
| 12 | - */ | |
| 13 | - private static $name_text_fields; | |
| 14 | - | |
| 15 | - /** | |
| 16 | 9 | * @param array $values |
| 17 | 10 | * @param bool|string[] $exclude |
| 18 | 11 | * @return array |
| 19 | 12 | */ |
| @@ -129,9 +122,9 @@ | ||
| 129 | 122 | ); |
| 130 | 123 | $args = wp_parse_args( $args, $defaults ); |
| 131 | 124 | |
| 132 | 125 | if ( empty( $args['parent_field_id'] ) ) { |
| 133 | - $value = $values['item_meta'][ $args['id'] ] ?? ''; | |
| 126 | + $value = isset( $values['item_meta'][ $args['id'] ] ) ? $values['item_meta'][ $args['id'] ] : ''; | |
| 134 | 127 | } else { |
| 135 | 128 | // value is from a nested form |
| 136 | 129 | $value = $values; |
| 137 | 130 | } |
| @@ -157,9 +150,8 @@ | ||
| 157 | 150 | } |
| 158 | 151 | |
| 159 | 152 | FrmEntriesHelper::set_posted_value( $posted_field, $value, $args ); |
| 160 | 153 | |
| 161 | - self::validate_options( $errors, $posted_field, $value, $args ); | |
| 162 | 154 | self::validate_field_types( $errors, $posted_field, $value, $args ); |
| 163 | 155 | |
| 164 | 156 | // Field might want to modify value before other parts of the system |
| 165 | 157 | // e.g. trim off excess values like in the case of fields with limit. |
| @@ -177,175 +169,8 @@ | ||
| 177 | 169 | } |
| 178 | 170 | } |
| 179 | 171 | |
| 180 | 172 | /** |
| 181 | - * @since 6.21 | |
| 182 | - * | |
| 183 | - * @param array $errors | |
| 184 | - * @param object $posted_field | |
| 185 | - * @param array|string $value | |
| 186 | - * @param array $args | |
| 187 | - * | |
| 188 | - * @return void | |
| 189 | - */ | |
| 190 | - private static function validate_options( &$errors, $posted_field, $value, $args ) { | |
| 191 | - if ( empty( $posted_field->options ) ) { | |
| 192 | - return; | |
| 193 | - } | |
| 194 | - | |
| 195 | - $option_is_valid = self::option_is_valid( $posted_field, $value, $posted_field->options ); | |
| 196 | - | |
| 197 | - /** | |
| 198 | - * @since 6.21 | |
| 199 | - * | |
| 200 | - * @param bool $option_is_valid | |
| 201 | - * @param array|string $value | |
| 202 | - * @param object $field | |
| 203 | - */ | |
| 204 | - $option_is_valid = (bool) apply_filters( 'frm_option_is_valid', $option_is_valid, $value, $posted_field ); | |
| 205 | - | |
| 206 | - if ( ! $option_is_valid ) { | |
| 207 | - $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $posted_field, 'invalid' ); | |
| 208 | - } | |
| 209 | - } | |
| 210 | - | |
| 211 | - /** | |
| 212 | - * Validate that value matches one of the options for the field. | |
| 213 | - * | |
| 214 | - * @since 6.21 | |
| 215 | - * | |
| 216 | - * @param stdClass $field | |
| 217 | - * @param array|string $value | |
| 218 | - * @param array $options | |
| 219 | - * @return bool | |
| 220 | - */ | |
| 221 | - private static function option_is_valid( $field, $value, $options ) { | |
| 222 | - if ( '' === $value ) { | |
| 223 | - return true; | |
| 224 | - } | |
| 225 | - | |
| 226 | - $field_object = FrmFieldFactory::get_field_type( $field->type, $field ); | |
| 227 | - if ( ! $field_object->field_type_has_options_settings() ) { | |
| 228 | - return true; | |
| 229 | - } | |
| 230 | - | |
| 231 | - if ( in_array( $field->type, array( 'likert', 'ranking' ), true ) ) { | |
| 232 | - // Ignore these field types automatically. | |
| 233 | - return true; | |
| 234 | - } | |
| 235 | - | |
| 236 | - if ( 'product' === $field->type && 'user_def' === FrmField::get_option( $field, 'data_type' ) ) { | |
| 237 | - return true; | |
| 238 | - } | |
| 239 | - | |
| 240 | - if ( ! empty( $field->field_options['post_field'] ) ) { | |
| 241 | - return true; | |
| 242 | - } | |
| 243 | - | |
| 244 | - $value = (array) $value; | |
| 245 | - | |
| 246 | - foreach ( $value as $current_value ) { | |
| 247 | - $match = false; | |
| 248 | - | |
| 249 | - foreach ( $options as $key => $option ) { | |
| 250 | - if ( strpos( $key, 'other_' ) === 0 ) { | |
| 251 | - // Always return true if an other option is found. | |
| 252 | - return true; | |
| 253 | - } | |
| 254 | - | |
| 255 | - if ( is_array( $option ) ) { | |
| 256 | - $separate_value = FrmField::get_option( $field, 'separate_value' ); | |
| 257 | - $option_value = $separate_value ? $option['value'] : $option['label']; | |
| 258 | - } else { | |
| 259 | - $option_value = $option; | |
| 260 | - } | |
| 261 | - | |
| 262 | - $match = trim( $current_value ) === trim( $option_value ); | |
| 263 | - if ( $match ) { | |
| 264 | - break; | |
| 265 | - } | |
| 266 | - | |
| 267 | - $match = trim( $current_value ) === trim( do_shortcode( $option_value ) ); | |
| 268 | - if ( $match ) { | |
| 269 | - break; | |
| 270 | - } | |
| 271 | - | |
| 272 | - $match = self::is_filtered_match( $current_value, $option_value ); | |
| 273 | - if ( $match ) { | |
| 274 | - break; | |
| 275 | - } | |
| 276 | - | |
| 277 | - if ( is_numeric( $current_value ) ) { | |
| 278 | - $match = (int) $current_value === (int) $option_value; | |
| 279 | - if ( $match ) { | |
| 280 | - break; | |
| 281 | - } | |
| 282 | - } | |
| 283 | - }//end foreach | |
| 284 | - | |
| 285 | - if ( ! $match ) { | |
| 286 | - return self::options_are_dynamic_based_on_hook( $field, $value ); | |
| 287 | - } | |
| 288 | - }//end foreach | |
| 289 | - | |
| 290 | - return true; | |
| 291 | - } | |
| 292 | - | |
| 293 | - /** | |
| 294 | - * Make an extra check after passing $option_value through the_content filter. | |
| 295 | - * This is to help catch cases where the option's formatting has been modified using | |
| 296 | - * the_content filter. | |
| 297 | - * | |
| 298 | - * @since 6.22 | |
| 299 | - * | |
| 300 | - * @param string $value | |
| 301 | - * @param string $option_value | |
| 302 | - * @return bool | |
| 303 | - */ | |
| 304 | - private static function is_filtered_match( $value, $option_value ) { | |
| 305 | - // First remove the wpautop filter so it doesn't add extra tags to $option_value. | |
| 306 | - $filter_priority = has_filter( 'the_content', 'wpautop' ); | |
| 307 | - if ( is_numeric( $filter_priority ) ) { | |
| 308 | - remove_filter( 'the_content', 'wpautop', $filter_priority ); | |
| 309 | - } | |
| 310 | - $filtered_option = apply_filters( 'the_content', $option_value ); | |
| 311 | - if ( is_numeric( $filter_priority ) ) { | |
| 312 | - add_filter( 'the_content', 'wpautop', $filter_priority ); | |
| 313 | - } | |
| 314 | - return trim( $value ) === trim( $filtered_option ); | |
| 315 | - } | |
| 316 | - | |
| 317 | - /** | |
| 318 | - * Do not validate options if they have been modified with a hook. | |
| 319 | - * This is to help avoid issues where the options could be based on a URL param for example. | |
| 320 | - * | |
| 321 | - * @since 6.21 | |
| 322 | - * | |
| 323 | - * @return bool | |
| 324 | - */ | |
| 325 | - private static function options_are_dynamic_based_on_hook( $field_object, $value ) { | |
| 326 | - $values = (array) $field_object; | |
| 327 | - $values['value'] = $value; | |
| 328 | - FrmFieldsHelper::prepare_new_front_field( $values, $field_object ); | |
| 329 | - | |
| 330 | - $separate_value = FrmField::get_option( $field_object, 'separate_value' ); | |
| 331 | - $map_callback = function ( $option ) use ( $separate_value ) { | |
| 332 | - if ( is_array( $option ) ) { | |
| 333 | - $option_value = $separate_value ? $option['value'] : $option['label']; | |
| 334 | - } else { | |
| 335 | - $option_value = $option; | |
| 336 | - } | |
| 337 | - $option_value = do_shortcode( $option_value ); | |
| 338 | - return $option_value; | |
| 339 | - }; | |
| 340 | - | |
| 341 | - $values_options = array_map( $map_callback, $values['options'] ); | |
| 342 | - $field_object_options = array_map( $map_callback, $field_object->options ); | |
| 343 | - | |
| 344 | - return $values_options !== $field_object_options; | |
| 345 | - } | |
| 346 | - | |
| 347 | - /** | |
| 348 | 173 | * Maybe add item_name to $_POST to save it in items table. |
| 349 | 174 | * |
| 350 | 175 | * @since 5.2.02 |
| 351 | 176 | * |
| @@ -395,11 +220,10 @@ | ||
| 395 | 220 | } |
| 396 | 221 | } |
| 397 | 222 | |
| 398 | 223 | public static function validate_phone_field( &$errors, $field, $value, $args ) { |
| 399 | - $format_value = FrmField::get_option( $field, 'format' ); | |
| 224 | + if ( $field->type === 'phone' || ( $field->type === 'text' && FrmField::is_option_true_in_object( $field, 'format' ) ) ) { | |
| 400 | 225 | |
| 401 | - if ( $field->type === 'phone' || ( $field->type === 'text' && $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value ) ) ) { | |
| 402 | 226 | $pattern = self::phone_format( $field ); |
| 403 | 227 | |
| 404 | 228 | if ( ! preg_match( $pattern, $value ) ) { |
| 405 | 229 | $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' ); |
| @@ -474,9 +298,9 @@ | ||
| 474 | 298 | return $pattern; |
| 475 | 299 | } |
| 476 | 300 | |
| 477 | 301 | /** |
| 478 | - * Check for spam. | |
| 302 | + * Check for spam | |
| 479 | 303 | * |
| 480 | 304 | * @param bool $exclude |
| 481 | 305 | * @param array $values |
| 482 | 306 | * @param array $errors By reference. |
| @@ -481,13 +305,8 @@ | ||
| 481 | 305 | * @param array $values |
| 482 | 306 | * @param array $errors By reference. |
| 483 | 307 | */ |
| 484 | 308 | public static function spam_check( $exclude, $values, &$errors ) { |
| 485 | - if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { | |
| 486 | - // Do not check spam on importing. | |
| 487 | - return; | |
| 488 | - } | |
| 489 | - | |
| 490 | 309 | if ( ! empty( $exclude ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) { |
| 491 | 310 | // only check spam if there are no other errors |
| 492 | 311 | return; |
| 493 | 312 | } |
| @@ -492,18 +311,14 @@ | ||
| 492 | 311 | return; |
| 493 | 312 | } |
| 494 | 313 | |
| 495 | 314 | $antispam_check = self::is_antispam_check( $values['form_id'] ); |
| 496 | - $spam_msg = FrmAntiSpamController::get_default_spam_message(); | |
| 497 | 315 | if ( is_string( $antispam_check ) ) { |
| 498 | 316 | $errors['spam'] = $antispam_check; |
| 499 | 317 | } elseif ( self::is_honeypot_spam( $values ) || self::is_spam_bot() ) { |
| 500 | - $errors['spam'] = $spam_msg; | |
| 501 | - } else { | |
| 502 | - $is_spam = FrmAntiSpamController::is_spam( $values ); | |
| 503 | - if ( $is_spam ) { | |
| 504 | - $errors['spam'] = $is_spam; | |
| 505 | - } | |
| 318 | + $errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' ); | |
| 319 | + } elseif ( self::blacklist_check( $values ) ) { | |
| 320 | + $errors['spam'] = __( 'Your entry appears to be blocked spam!', 'formidable' ); | |
| 506 | 321 | } |
| 507 | 322 | |
| 508 | 323 | if ( isset( $errors['spam'] ) || self::form_is_in_progress( $values ) ) { |
| 509 | 324 | return; |
| @@ -575,17 +390,54 @@ | ||
| 575 | 390 | |
| 576 | 391 | return ( ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() ) ); |
| 577 | 392 | } |
| 578 | 393 | |
| 394 | + public static function blacklist_check( $values ) { | |
| 395 | + if ( ! apply_filters( 'frm_check_blacklist', true, $values ) ) { | |
| 396 | + return false; | |
| 397 | + } | |
| 398 | + | |
| 399 | + $mod_keys = trim( self::get_disallowed_words() ); | |
| 400 | + if ( empty( $mod_keys ) ) { | |
| 401 | + return false; | |
| 402 | + } | |
| 403 | + | |
| 404 | + $content = FrmEntriesHelper::entry_array_to_string( $values ); | |
| 405 | + | |
| 406 | + self::prepare_values_for_spam_check( $values ); | |
| 407 | + $ip = FrmAppHelper::get_ip_address(); | |
| 408 | + $user_agent = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ); | |
| 409 | + $user_info = self::get_spam_check_user_info( $values ); | |
| 410 | + | |
| 411 | + return self::check_disallowed_words( $user_info['comment_author'], $user_info['comment_author_email'], $user_info['comment_author_url'], $content, $ip, $user_agent ); | |
| 412 | + } | |
| 413 | + | |
| 579 | 414 | /** |
| 580 | - * Checks spam using WordPress disallowed words and Frm denylist. | |
| 415 | + * For WP 5.5 compatibility. | |
| 581 | 416 | * |
| 582 | - * @param array $values Entry values. | |
| 417 | + * @since 4.06.02 | |
| 418 | + */ | |
| 419 | + private static function check_disallowed_words( $author, $email, $url, $content, $ip, $user_agent ) { | |
| 420 | + if ( function_exists( 'wp_check_comment_disallowed_list' ) ) { | |
| 421 | + return wp_check_comment_disallowed_list( $author, $email, $url, $content, $ip, $user_agent ); | |
| 422 | + } | |
| 423 | + // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_blacklist_checkFound | |
| 424 | + return wp_blacklist_check( $author, $email, $url, $content, $ip, $user_agent ); | |
| 425 | + } | |
| 426 | + | |
| 427 | + /** | |
| 428 | + * For WP 5.5 compatibility. | |
| 583 | 429 | * |
| 584 | - * @return bool | |
| 430 | + * @since 4.06.02 | |
| 585 | 431 | */ |
| 586 | - public static function blacklist_check( $values ) { | |
| 587 | - return FrmAntiSpamController::contains_wp_disallowed_words( $values ) || FrmAntiSpamController::is_denylist_spam( $values ); | |
| 432 | + private static function get_disallowed_words() { | |
| 433 | + $keys = get_option( 'disallowed_keys' ); | |
| 434 | + if ( false === $keys ) { | |
| 435 | + // Fallback for WP < 5.5. | |
| 436 | + // phpcs:ignore WordPress.WP.DeprecatedParameterValues.Found | |
| 437 | + $keys = get_option( 'blacklist_keys' ); | |
| 438 | + } | |
| 439 | + return $keys; | |
| 588 | 440 | } |
| 589 | 441 | |
| 590 | 442 | /** |
| 591 | 443 | * Check entries for Akismet spam |
| @@ -656,14 +508,13 @@ | ||
| 656 | 508 | /** |
| 657 | 509 | * Gets user info for Akismet spam check. |
| 658 | 510 | * |
| 659 | 511 | * @since 5.0.13 Separate code for guest. Handle value of embedded|repeater. |
| 660 | - * @since 6.21 This changed from private to public. | |
| 661 | 512 | * |
| 662 | 513 | * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}. |
| 663 | 514 | * @return array |
| 664 | 515 | */ |
| 665 | - public static function get_spam_check_user_info( $values ) { | |
| 516 | + private static function get_spam_check_user_info( $values ) { | |
| 666 | 517 | if ( ! is_user_logged_in() ) { |
| 667 | 518 | return self::get_spam_check_user_info_for_guest( $values ); |
| 668 | 519 | } |
| 669 | 520 | |
| @@ -731,9 +582,9 @@ | ||
| 731 | 582 | } |
| 732 | 583 | |
| 733 | 584 | $field_id = ! is_null( $custom_index ) ? $custom_index : $index; |
| 734 | 585 | foreach ( $datas['missing_keys'] as $key_index => $key ) { |
| 735 | - $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'], $values ); | |
| 586 | + $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'] ); | |
| 736 | 587 | if ( $found ) { |
| 737 | 588 | $datas[ $key ] = $value; |
| 738 | 589 | $datas['frm_duplicated'][] = $field_id; |
| 739 | 590 | unset( $datas['missing_keys'][ $key_index ] ); |
| @@ -750,13 +601,11 @@ | ||
| 750 | 601 | * @param string $key Guest info key. |
| 751 | 602 | * @param string $value Value to check. |
| 752 | 603 | * @param int $field_id Field ID. |
| 753 | 604 | * @param array $name_field_ids Name field IDs. |
| 754 | - * @param array $values Array of posted values. | |
| 755 | - * | |
| 756 | 605 | * @return bool |
| 757 | 606 | */ |
| 758 | - private static function is_akismet_guest_info_value( $key, &$value, $field_id, $name_field_ids, $values ) { | |
| 607 | + private static function is_akismet_guest_info_value( $key, $value, $field_id, $name_field_ids ) { | |
| 759 | 608 | if ( ! $value || is_numeric( $value ) ) { |
| 760 | 609 | return false; |
| 761 | 610 | } |
| 762 | 611 | |
| @@ -767,62 +616,18 @@ | ||
| 767 | 616 | case 'comment_author_url': |
| 768 | 617 | return 0 === strpos( $value, 'http' ); |
| 769 | 618 | |
| 770 | 619 | case 'comment_author': |
| 771 | - if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { | |
| 620 | + if ( $name_field_ids ) { | |
| 772 | 621 | // If there is name field in the form, we should always use it as author name. |
| 773 | - return true; | |
| 622 | + return in_array( $field_id, $name_field_ids, true ); | |
| 774 | 623 | } |
| 775 | - $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); | |
| 776 | - $fields = self::get_name_text_fields( $form_id ); | |
| 624 | + return strlen( $value ) < 200; | |
| 625 | + } | |
| 777 | 626 | |
| 778 | - foreach ( $fields as $index => $field ) { | |
| 779 | - if ( 'Name' !== $field->name ) { | |
| 780 | - continue; | |
| 781 | - } | |
| 782 | - if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) { | |
| 783 | - if ( empty( $values[ absint( $fields[ $index + 1 ]->id ) ] ) ) { | |
| 784 | - continue; | |
| 785 | - } | |
| 786 | - $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; | |
| 787 | - return true; | |
| 788 | - } | |
| 789 | - } | |
| 790 | - }//end switch | |
| 791 | - | |
| 792 | 627 | return false; |
| 793 | 628 | } |
| 794 | 629 | |
| 795 | - /** | |
| 796 | - * Returns fields that have 'Name' and 'Last' as their name. | |
| 797 | - * | |
| 798 | - * @since 6.17 | |
| 799 | - * | |
| 800 | - * @param int $form_id | |
| 801 | - * @return array | |
| 802 | - */ | |
| 803 | - private static function get_name_text_fields( $form_id ) { | |
| 804 | - $name_text_fields_is_initialized = is_array( self::$name_text_fields ); | |
| 805 | - if ( $name_text_fields_is_initialized && isset( self::$name_text_fields[ $form_id ] ) ) { | |
| 806 | - return self::$name_text_fields[ $form_id ]; | |
| 807 | - } | |
| 808 | - if ( ! $name_text_fields_is_initialized ) { | |
| 809 | - self::$name_text_fields = array(); | |
| 810 | - } | |
| 811 | - self::$name_text_fields[ $form_id ] = FrmDb::get_results( | |
| 812 | - 'frm_fields', | |
| 813 | - array( | |
| 814 | - 'form_id' => $form_id, | |
| 815 | - 'type' => 'text', | |
| 816 | - 'name' => array( 'Name', 'Last' ), | |
| 817 | - ), | |
| 818 | - 'id,name', | |
| 819 | - array( 'order_by' => 'field_order ASC' ) | |
| 820 | - ); | |
| 821 | - | |
| 822 | - return self::$name_text_fields[ $form_id ]; | |
| 823 | - } | |
| 824 | - | |
| 825 | 630 | private static function add_server_values_to_akismet( &$datas ) { |
| 826 | 631 | foreach ( $_SERVER as $key => $value ) { |
| 827 | 632 | $include_value = is_string( $value ) && ! preg_match( '/^HTTP_COOKIE/', $key ) && preg_match( '/^(HTTP_|REMOTE_ADDR|REQUEST_URI|DOCUMENT_URI)/', $key ); |
| 828 | 633 | |
| @@ -915,9 +720,9 @@ | ||
| 915 | 720 | } |
| 916 | 721 | |
| 917 | 722 | // Check if submitted value is same as one of field option. |
| 918 | 723 | foreach ( $field_data->options as $option ) { |
| 919 | - $option_value = ! is_array( $option ) ? $option : ( $option['value'] ?? '' ); | |
| 724 | + $option_value = ! is_array( $option ) ? $option : ( isset( $option['value'] ) ? $option['value'] : '' ); | |
| 920 | 725 | if ( $values['item_meta']['other'][ $field_data->id ] === $option_value ) { |
| 921 | 726 | return true; |
| 922 | 727 | } |
| 923 | 728 | } |
| @@ -956,13 +761,12 @@ | ||
| 956 | 761 | /** |
| 957 | 762 | * Prepares values array for spam check. |
| 958 | 763 | * |
| 959 | 764 | * @since 5.0.13 |
| 960 | - * @since 6.21 This changed from private to public. | |
| 961 | 765 | * |
| 962 | 766 | * @param array $values Entry values. |
| 963 | 767 | */ |
| 964 | - public static function prepare_values_for_spam_check( &$values ) { | |
| 768 | + private static function prepare_values_for_spam_check( &$values ) { | |
| 965 | 769 | $form_ids = self::get_all_form_ids_and_flatten_meta( $values ); |
| 966 | 770 | $values['form_ids'] = $form_ids; |
| 967 | 771 | } |
| 968 | 772 | |
| @@ -1020,11 +824,9 @@ | ||
| 1020 | 824 | |
| 1021 | 825 | $values['name_field_ids'][] = $subsubindex; |
| 1022 | 826 | } |
| 1023 | 827 | |
| 1024 | - if ( is_array( $values['item_meta'][ $subsubindex ] ) ) { | |
| 1025 | - $values['item_meta'][ $subsubindex ][] = $subsubvalue; | |
| 1026 | - } | |
| 828 | + $values['item_meta'][ $subsubindex ][] = $subsubvalue; | |
| 1027 | 829 | } |
| 1028 | 830 | }//end foreach |
| 1029 | 831 | |
| 1030 | 832 | unset( $values['item_meta'][ $field_id ] ); |