| @@ -5,10 +5,17 @@ | ||
| 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 | + /** | |
| 9 | 16 | * @param array $values |
| 10 | - * @param string[]|bool $exclude | |
| 17 | + * @param bool|string[] $exclude | |
| 11 | 18 | * @return array |
| 12 | 19 | */ |
| 13 | 20 | public static function validate( $values, $exclude = false ) { |
| 14 | 21 | FrmEntry::sanitize_entry_post( $values ); |
| @@ -24,8 +31,9 @@ | ||
| 24 | 31 | $frm_settings = FrmAppHelper::get_settings(); |
| 25 | 32 | $errors['form'] = $frm_settings->admin_permission; |
| 26 | 33 | } |
| 27 | 34 | |
| 35 | + self::maybe_fix_item_meta(); | |
| 28 | 36 | self::set_item_key( $values ); |
| 29 | 37 | |
| 30 | 38 | $posted_fields = self::get_fields_to_validate( $values, $exclude ); |
| 31 | 39 | |
| @@ -54,14 +62,29 @@ | ||
| 54 | 62 | |
| 55 | 63 | if ( is_array( $filtered_errors ) ) { |
| 56 | 64 | $errors = $filtered_errors; |
| 57 | 65 | } else { |
| 58 | - _doing_it_wrong( __FUNCTION__, 'Only arrays should be returned when using the frm_validate_entry filter.', '6.3' ); | |
| 66 | + _doing_it_wrong( __METHOD__, 'Only arrays should be returned when using the frm_validate_entry filter.', '6.3' ); | |
| 59 | 67 | } |
| 60 | 68 | |
| 61 | 69 | return $errors; |
| 62 | 70 | } |
| 63 | 71 | |
| 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 | + | |
| 64 | 87 | private static function set_item_key( &$values ) { |
| 65 | 88 | if ( ! isset( $values['item_key'] ) || $values['item_key'] == '' ) { |
| 66 | 89 | global $wpdb; |
| 67 | 90 | $values['item_key'] = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_items', 'item_key' ); |
| @@ -95,13 +118,17 @@ | ||
| 95 | 118 | |
| 96 | 119 | public static function validate_field( $posted_field, &$errors, $values, $args = array() ) { |
| 97 | 120 | $defaults = array( |
| 98 | 121 | 'id' => $posted_field->id, |
| 99 | - 'parent_field_id' => '', // the id of the repeat or embed form | |
| 100 | - 'key_pointer' => '', // the pointer in the posted array | |
| 101 | - 'exclude' => array(), // exclude these field types from validation | |
| 122 | + // The id of the repeat or embed form. | |
| 123 | + 'parent_field_id' => '', | |
| 124 | + // The pointer in the posted array. | |
| 125 | + 'key_pointer' => '', | |
| 126 | + // Exclude these field types from validation. | |
| 127 | + 'exclude' => array(), | |
| 128 | + | |
| 102 | 129 | ); |
| 103 | - $args = wp_parse_args( $args, $defaults ); | |
| 130 | + $args = wp_parse_args( $args, $defaults ); | |
| 104 | 131 | |
| 105 | 132 | if ( empty( $args['parent_field_id'] ) ) { |
| 106 | 133 | $value = isset( $values['item_meta'][ $args['id'] ] ) ? $values['item_meta'][ $args['id'] ] : ''; |
| 107 | 134 | } else { |
| @@ -142,8 +169,12 @@ | ||
| 142 | 169 | } |
| 143 | 170 | |
| 144 | 171 | $errors = apply_filters( 'frm_validate_' . $posted_field->type . '_field_entry', $errors, $posted_field, $value, $args ); |
| 145 | 172 | $errors = apply_filters( 'frm_validate_field_entry', $errors, $posted_field, $value, $args ); |
| 173 | + | |
| 174 | + if ( ! FrmAppHelper::pro_is_installed() && empty( $args['other'] ) ) { | |
| 175 | + FrmEntriesHelper::get_posted_value( $posted_field, $value, $args ); | |
| 176 | + } | |
| 146 | 177 | } |
| 147 | 178 | |
| 148 | 179 | /** |
| 149 | 180 | * Maybe add item_name to $_POST to save it in items table. |
| @@ -149,9 +180,10 @@ | ||
| 149 | 180 | * Maybe add item_name to $_POST to save it in items table. |
| 150 | 181 | * |
| 151 | 182 | * @since 5.2.02 |
| 152 | 183 | * |
| 153 | - * @param object $field Field object. | |
| 184 | + * @param array|string $value Field value. | |
| 185 | + * @param object $field Field object. | |
| 154 | 186 | */ |
| 155 | 187 | private static function maybe_add_item_name( $value, $field ) { |
| 156 | 188 | $item_name = false; |
| 157 | 189 | if ( 'name' === $field->type ) { |
| @@ -195,10 +227,11 @@ | ||
| 195 | 227 | } |
| 196 | 228 | } |
| 197 | 229 | |
| 198 | 230 | public static function validate_phone_field( &$errors, $field, $value, $args ) { |
| 199 | - if ( $field->type == 'phone' || ( $field->type == 'text' && FrmField::is_option_true_in_object( $field, 'format' ) ) ) { | |
| 231 | + $format_value = FrmField::get_option( $field, 'format' ); | |
| 200 | 232 | |
| 233 | + if ( $field->type === 'phone' || ( $field->type === 'text' && $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value ) ) ) { | |
| 201 | 234 | $pattern = self::phone_format( $field ); |
| 202 | 235 | |
| 203 | 236 | if ( ! preg_match( $pattern, $value ) ) { |
| 204 | 237 | $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' ); |
| @@ -212,8 +245,11 @@ | ||
| 212 | 245 | } else { |
| 213 | 246 | $pattern = FrmField::get_option( $field, 'format' ); |
| 214 | 247 | } |
| 215 | 248 | |
| 249 | + // Ampersands are saved as &. | |
| 250 | + // Reverse it here so we are checking for the correct character. | |
| 251 | + $pattern = html_entity_decode( $pattern ); | |
| 216 | 252 | $pattern = apply_filters( 'frm_phone_pattern', $pattern, $field ); |
| 217 | 253 | |
| 218 | 254 | // Create a regexp if format is not already a regexp |
| 219 | 255 | if ( strpos( $pattern, '^' ) !== 0 ) { |
| @@ -272,14 +308,14 @@ | ||
| 272 | 308 | |
| 273 | 309 | /** |
| 274 | 310 | * Check for spam |
| 275 | 311 | * |
| 276 | - * @param boolean $exclude | |
| 312 | + * @param bool $exclude | |
| 277 | 313 | * @param array $values |
| 278 | - * @param array $errors by reference | |
| 314 | + * @param array $errors By reference. | |
| 279 | 315 | */ |
| 280 | 316 | public static function spam_check( $exclude, $values, &$errors ) { |
| 281 | - if ( ! empty( $exclude ) || ! isset( $values['item_meta'] ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) { | |
| 317 | + if ( ! empty( $exclude ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) { | |
| 282 | 318 | // only check spam if there are no other errors |
| 283 | 319 | return; |
| 284 | 320 | } |
| 285 | 321 | |
| @@ -326,9 +362,9 @@ | ||
| 326 | 362 | } |
| 327 | 363 | |
| 328 | 364 | /** |
| 329 | 365 | * @param array $values |
| 330 | - * @return boolean | |
| 366 | + * @return bool | |
| 331 | 367 | */ |
| 332 | 368 | private static function is_honeypot_spam( $values ) { |
| 333 | 369 | $honeypot = new FrmHoneypot( $values['form_id'] ); |
| 334 | 370 | return ! $honeypot->validate(); |
| @@ -334,9 +370,9 @@ | ||
| 334 | 370 | return ! $honeypot->validate(); |
| 335 | 371 | } |
| 336 | 372 | |
| 337 | 373 | /** |
| 338 | - * @return boolean | |
| 374 | + * @return bool | |
| 339 | 375 | */ |
| 340 | 376 | private static function is_spam_bot() { |
| 341 | 377 | $ip = FrmAppHelper::get_ip_address(); |
| 342 | 378 | |
| @@ -344,9 +380,9 @@ | ||
| 344 | 380 | } |
| 345 | 381 | |
| 346 | 382 | /** |
| 347 | 383 | * @param array $values |
| 348 | - * @return boolean | |
| 384 | + * @return bool | |
| 349 | 385 | */ |
| 350 | 386 | private static function is_akismet_spam( $values ) { |
| 351 | 387 | global $wpcom_api_key; |
| 352 | 388 | |
| @@ -390,11 +426,11 @@ | ||
| 390 | 426 | */ |
| 391 | 427 | private static function check_disallowed_words( $author, $email, $url, $content, $ip, $user_agent ) { |
| 392 | 428 | if ( function_exists( 'wp_check_comment_disallowed_list' ) ) { |
| 393 | 429 | return wp_check_comment_disallowed_list( $author, $email, $url, $content, $ip, $user_agent ); |
| 394 | - } else { | |
| 395 | - return wp_blacklist_check( $author, $email, $url, $content, $ip, $user_agent ); | |
| 396 | 430 | } |
| 431 | + // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_blacklist_checkFound | |
| 432 | + return wp_blacklist_check( $author, $email, $url, $content, $ip, $user_agent ); | |
| 397 | 433 | } |
| 398 | 434 | |
| 399 | 435 | /** |
| 400 | 436 | * For WP 5.5 compatibility. |
| @@ -404,8 +440,9 @@ | ||
| 404 | 440 | private static function get_disallowed_words() { |
| 405 | 441 | $keys = get_option( 'disallowed_keys' ); |
| 406 | 442 | if ( false === $keys ) { |
| 407 | 443 | // Fallback for WP < 5.5. |
| 444 | + // phpcs:ignore WordPress.WP.DeprecatedParameterValues.Found | |
| 408 | 445 | $keys = get_option( 'blacklist_keys' ); |
| 409 | 446 | } |
| 410 | 447 | return $keys; |
| 411 | 448 | } |
| @@ -412,9 +449,9 @@ | ||
| 412 | 449 | |
| 413 | 450 | /** |
| 414 | 451 | * Check entries for Akismet spam |
| 415 | 452 | * |
| 416 | - * @return boolean true if is spam | |
| 453 | + * @return bool true if is spam | |
| 417 | 454 | */ |
| 418 | 455 | public static function akismet( $values ) { |
| 419 | 456 | if ( empty( $values['item_meta'] ) ) { |
| 420 | 457 | return false; |
| @@ -436,9 +473,9 @@ | ||
| 436 | 473 | |
| 437 | 474 | $query_string = _http_build_query( $datas, '', '&' ); |
| 438 | 475 | $response = Akismet::http_post( $query_string, 'comment-check' ); |
| 439 | 476 | |
| 440 | - return ( is_array( $response ) && $response[1] == 'true' ); | |
| 477 | + return ( is_array( $response ) && $response[1] === 'true' ); | |
| 441 | 478 | } |
| 442 | 479 | |
| 443 | 480 | /** |
| 444 | 481 | * @since 2.0 |
| @@ -447,8 +484,9 @@ | ||
| 447 | 484 | self::add_site_info_to_akismet( $datas ); |
| 448 | 485 | self::add_server_values_to_akismet( $datas ); |
| 449 | 486 | |
| 450 | 487 | self::prepare_values_for_spam_check( $values ); |
| 488 | + self::skip_adding_values_to_akismet( $values ); | |
| 451 | 489 | |
| 452 | 490 | self::add_user_info_to_akismet( $datas, $values ); |
| 453 | 491 | self::add_comment_content_to_akismet( $datas, $values ); |
| 454 | 492 | } |
| @@ -541,9 +579,10 @@ | ||
| 541 | 579 | */ |
| 542 | 580 | private static function recursive_add_akismet_guest_info( &$datas, $values, $custom_index = null ) { |
| 543 | 581 | foreach ( $values as $index => $value ) { |
| 544 | 582 | if ( ! $datas['missing_keys'] ) { |
| 545 | - return; // Found all info. | |
| 583 | + // Found all info. | |
| 584 | + return; | |
| 546 | 585 | } |
| 547 | 586 | |
| 548 | 587 | if ( is_array( $value ) ) { |
| 549 | 588 | self::recursive_add_akismet_guest_info( $datas, $value, $index ); |
| @@ -551,9 +590,9 @@ | ||
| 551 | 590 | } |
| 552 | 591 | |
| 553 | 592 | $field_id = ! is_null( $custom_index ) ? $custom_index : $index; |
| 554 | 593 | foreach ( $datas['missing_keys'] as $key_index => $key ) { |
| 555 | - $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'] ); | |
| 594 | + $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'], $values ); | |
| 556 | 595 | if ( $found ) { |
| 557 | 596 | $datas[ $key ] = $value; |
| 558 | 597 | $datas['frm_duplicated'][] = $field_id; |
| 559 | 598 | unset( $datas['missing_keys'][ $key_index ] ); |
| @@ -558,9 +597,9 @@ | ||
| 558 | 597 | $datas['frm_duplicated'][] = $field_id; |
| 559 | 598 | unset( $datas['missing_keys'][ $key_index ] ); |
| 560 | 599 | } |
| 561 | 600 | } |
| 562 | - } | |
| 601 | + }//end foreach | |
| 563 | 602 | } |
| 564 | 603 | |
| 565 | 604 | /** |
| 566 | 605 | * Checks if given value is an akismet guest info. |
| @@ -570,11 +609,13 @@ | ||
| 570 | 609 | * @param string $key Guest info key. |
| 571 | 610 | * @param string $value Value to check. |
| 572 | 611 | * @param int $field_id Field ID. |
| 573 | 612 | * @param array $name_field_ids Name field IDs. |
| 613 | + * @param array $values Array of posted values. | |
| 614 | + * | |
| 574 | 615 | * @return bool |
| 575 | 616 | */ |
| 576 | - private static function is_akismet_guest_info_value( $key, $value, $field_id, $name_field_ids ) { | |
| 617 | + private static function is_akismet_guest_info_value( $key, &$value, $field_id, $name_field_ids, $values ) { | |
| 577 | 618 | if ( ! $value || is_numeric( $value ) ) { |
| 578 | 619 | return false; |
| 579 | 620 | } |
| 580 | 621 | |
| @@ -585,16 +626,60 @@ | ||
| 585 | 626 | case 'comment_author_url': |
| 586 | 627 | return 0 === strpos( $value, 'http' ); |
| 587 | 628 | |
| 588 | 629 | case 'comment_author': |
| 589 | - if ( $name_field_ids ) { | |
| 630 | + if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { | |
| 590 | 631 | // If there is name field in the form, we should always use it as author name. |
| 591 | - return in_array( $field_id, $name_field_ids, true ); | |
| 632 | + return true; | |
| 592 | 633 | } |
| 593 | - return strlen( $value ) < 200; | |
| 634 | + $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); | |
| 635 | + $fields = self::get_name_text_fields( $form_id ); | |
| 636 | + | |
| 637 | + foreach ( $fields as $index => $field ) { | |
| 638 | + if ( 'Name' !== $field->name ) { | |
| 639 | + continue; | |
| 640 | + } | |
| 641 | + if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) { | |
| 642 | + if ( empty( $values[ absint( $fields[ $index + 1 ]->id ) ] ) ) { | |
| 643 | + continue; | |
| 644 | + } | |
| 645 | + $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; | |
| 646 | + return true; | |
| 647 | + } | |
| 648 | + } | |
| 649 | + }//end switch | |
| 650 | + | |
| 651 | + return false; | |
| 652 | + } | |
| 653 | + | |
| 654 | + /** | |
| 655 | + * Returns fields that have 'Name' and 'Last' as their name. | |
| 656 | + * | |
| 657 | + * @since 6.17 | |
| 658 | + * | |
| 659 | + * @param int $form_id | |
| 660 | + * @return array | |
| 661 | + */ | |
| 662 | + private static function get_name_text_fields( $form_id ) { | |
| 663 | + $name_text_fields_is_initialized = is_array( self::$name_text_fields ); | |
| 664 | + if ( $name_text_fields_is_initialized && isset( self::$name_text_fields[ $form_id ] ) ) { | |
| 665 | + return self::$name_text_fields[ $form_id ]; | |
| 594 | 666 | } |
| 667 | + if ( ! $name_text_fields_is_initialized ) { | |
| 668 | + self::$name_text_fields = array(); | |
| 669 | + } | |
| 670 | + self::$name_text_fields[ $form_id ] = FrmDb::get_results( | |
| 671 | + 'frm_fields', | |
| 672 | + array( | |
| 673 | + 'form_id' => $form_id, | |
| 674 | + 'type' => 'text', | |
| 675 | + 'name' => array( 'Name', 'Last' ), | |
| 676 | + ), | |
| 677 | + 'id,name', | |
| 678 | + array( 'order_by' => 'field_order ASC' ) | |
| 679 | + ); | |
| 595 | 680 | |
| 596 | - return false; | |
| 681 | + return self::$name_text_fields[ $form_id ]; | |
| 597 | 682 | } |
| 598 | 683 | |
| 599 | 684 | private static function add_server_values_to_akismet( &$datas ) { |
| 600 | 685 | foreach ( $_SERVER as $key => $value ) { |
| @@ -627,10 +712,8 @@ | ||
| 627 | 712 | } |
| 628 | 713 | unset( $datas['frm_duplicated'] ); |
| 629 | 714 | } |
| 630 | 715 | |
| 631 | - self::skip_adding_values_to_akismet( $values ); | |
| 632 | - | |
| 633 | 716 | $datas['comment_content'] = FrmEntriesHelper::entry_array_to_string( $values ); |
| 634 | 717 | } |
| 635 | 718 | |
| 636 | 719 | /** |
| @@ -665,14 +748,16 @@ | ||
| 665 | 748 | * @param array $values Entry values. |
| 666 | 749 | * @return bool |
| 667 | 750 | */ |
| 668 | 751 | private static function should_really_skip_field( $field_data, $values ) { |
| 669 | - if ( empty( $field_data->options ) ) { // This is skipped field types. | |
| 752 | + if ( empty( $field_data->options ) ) { | |
| 753 | + // This is skipped field types. | |
| 670 | 754 | return true; |
| 671 | 755 | } |
| 672 | 756 | |
| 673 | 757 | FrmAppHelper::unserialize_or_decode( $field_data->options ); |
| 674 | - if ( ! $field_data->options ) { // Check if an error happens when unserializing, or empty options. | |
| 758 | + if ( ! $field_data->options ) { | |
| 759 | + // Check if an error happens when unserializing, or empty options. | |
| 675 | 760 | return true; |
| 676 | 761 | } |
| 677 | 762 | |
| 678 | 763 | end( $field_data->options ); |
| @@ -754,9 +839,10 @@ | ||
| 754 | 839 | |
| 755 | 840 | // Blacklist check for File field in the old version doesn't contain `form_id`. |
| 756 | 841 | $form_ids = isset( $values['form_id'] ) ? array( absint( $values['form_id'] ) ) : array(); |
| 757 | 842 | foreach ( $values['item_meta'] as $field_id => $value ) { |
| 758 | - if ( ! is_numeric( $field_id ) ) { // Maybe `other`. | |
| 843 | + if ( ! is_numeric( $field_id ) ) { | |
| 844 | + // Maybe `other`. | |
| 759 | 845 | continue; |
| 760 | 846 | } |
| 761 | 847 | |
| 762 | 848 | // Convert name array to string. |
| @@ -794,44 +880,12 @@ | ||
| 794 | 880 | } |
| 795 | 881 | |
| 796 | 882 | $values['item_meta'][ $subsubindex ][] = $subsubvalue; |
| 797 | 883 | } |
| 798 | - } | |
| 884 | + }//end foreach | |
| 799 | 885 | |
| 800 | 886 | unset( $values['item_meta'][ $field_id ] ); |
| 801 | - } | |
| 887 | + }//end foreach | |
| 802 | 888 | |
| 803 | 889 | return $form_ids; |
| 804 | - } | |
| 805 | - | |
| 806 | - /** | |
| 807 | - * @deprecated 3.0 | |
| 808 | - * @codeCoverageIgnore | |
| 809 | - */ | |
| 810 | - public static function validate_url_field( &$errors, $field, $value, $args ) { | |
| 811 | - FrmDeprecated::validate_url_field( $errors, $field, $value, $args ); | |
| 812 | - } | |
| 813 | - | |
| 814 | - /** | |
| 815 | - * @deprecated 3.0 | |
| 816 | - * @codeCoverageIgnore | |
| 817 | - */ | |
| 818 | - public static function validate_email_field( &$errors, $field, $value, $args ) { | |
| 819 | - FrmDeprecated::validate_email_field( $errors, $field, $value, $args ); | |
| 820 | - } | |
| 821 | - | |
| 822 | - /** | |
| 823 | - * @deprecated 3.0 | |
| 824 | - * @codeCoverageIgnore | |
| 825 | - */ | |
| 826 | - public static function validate_number_field( &$errors, $field, $value, $args ) { | |
| 827 | - FrmDeprecated::validate_number_field( $errors, $field, $value, $args ); | |
| 828 | - } | |
| 829 | - | |
| 830 | - /** | |
| 831 | - * @deprecated 3.0 | |
| 832 | - * @codeCoverageIgnore | |
| 833 | - */ | |
| 834 | - public static function validate_recaptcha( &$errors, $field, $args ) { | |
| 835 | - FrmDeprecated::validate_recaptcha( $errors, $field, $args ); | |
| 836 | 890 | } |
| 837 | 891 | } |