| @@ -3,21 +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 | - * @return array | |
| 19 | - */ | |
| 20 | 7 | public static function validate( $values, $exclude = false ) { |
| 21 | 8 | FrmEntry::sanitize_entry_post( $values ); |
| 22 | 9 | $errors = array(); |
| 23 | 10 | |
| @@ -27,13 +14,11 @@ | ||
| 27 | 14 | return $errors; |
| 28 | 15 | } |
| 29 | 16 | |
| 30 | 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 | - $frm_settings = FrmAppHelper::get_settings(); | |
| 32 | - $errors['form'] = $frm_settings->admin_permission; | |
| 18 | + $errors['form'] = __( 'You do not have permission to do that', 'formidable' ); | |
| 33 | 19 | } |
| 34 | 20 | |
| 35 | - self::maybe_fix_item_meta(); | |
| 36 | 21 | self::set_item_key( $values ); |
| 37 | 22 | |
| 38 | 23 | $posted_fields = self::get_fields_to_validate( $values, $exclude ); |
| 39 | 24 | |
| @@ -57,34 +42,13 @@ | ||
| 57 | 42 | * @param array $errors Errors data. |
| 58 | 43 | * @param array $values Value data of the form. |
| 59 | 44 | * @param array $args Custom arguments. Contains `exclude` and `posted_fields`. |
| 60 | 45 | */ |
| 61 | - $filtered_errors = apply_filters( 'frm_validate_entry', $errors, $values, compact( 'exclude', 'posted_fields' ) ); | |
| 46 | + $errors = apply_filters( 'frm_validate_entry', $errors, $values, compact( 'exclude', 'posted_fields' ) ); | |
| 62 | 47 | |
| 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 | 48 | return $errors; |
| 70 | 49 | } |
| 71 | 50 | |
| 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 | 51 | private static function set_item_key( &$values ) { |
| 88 | 52 | if ( ! isset( $values['item_key'] ) || $values['item_key'] == '' ) { |
| 89 | 53 | global $wpdb; |
| 90 | 54 | $values['item_key'] = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_items', 'item_key' ); |
| @@ -118,20 +82,16 @@ | ||
| 118 | 82 | |
| 119 | 83 | public static function validate_field( $posted_field, &$errors, $values, $args = array() ) { |
| 120 | 84 | $defaults = array( |
| 121 | 85 | 'id' => $posted_field->id, |
| 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 | - | |
| 86 | + 'parent_field_id' => '', // the id of the repeat or embed form | |
| 87 | + 'key_pointer' => '', // the pointer in the posted array | |
| 88 | + 'exclude' => array(), // exclude these field types from validation | |
| 129 | 89 | ); |
| 130 | - $args = wp_parse_args( $args, $defaults ); | |
| 90 | + $args = wp_parse_args( $args, $defaults ); | |
| 131 | 91 | |
| 132 | 92 | if ( empty( $args['parent_field_id'] ) ) { |
| 133 | - $value = $values['item_meta'][ $args['id'] ] ?? ''; | |
| 93 | + $value = isset( $values['item_meta'][ $args['id'] ] ) ? $values['item_meta'][ $args['id'] ] : ''; | |
| 134 | 94 | } else { |
| 135 | 95 | // value is from a nested form |
| 136 | 96 | $value = $values; |
| 137 | 97 | } |
| @@ -151,15 +111,14 @@ | ||
| 151 | 111 | } |
| 152 | 112 | |
| 153 | 113 | if ( $posted_field->required == '1' && FrmAppHelper::is_empty_value( $value ) ) { |
| 154 | 114 | $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $posted_field, 'blank' ); |
| 155 | - } elseif ( ! isset( $_POST['item_name'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 156 | - self::maybe_add_item_name( $value, $posted_field ); | |
| 115 | + } elseif ( $posted_field->type === 'text' && ! isset( $_POST['item_name'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 116 | + $_POST['item_name'] = $value; | |
| 157 | 117 | } |
| 158 | 118 | |
| 159 | 119 | FrmEntriesHelper::set_posted_value( $posted_field, $value, $args ); |
| 160 | 120 | |
| 161 | - self::validate_options( $errors, $posted_field, $value, $args ); | |
| 162 | 121 | self::validate_field_types( $errors, $posted_field, $value, $args ); |
| 163 | 122 | |
| 164 | 123 | // Field might want to modify value before other parts of the system |
| 165 | 124 | // e.g. trim off excess values like in the case of fields with limit. |
| @@ -170,205 +129,11 @@ | ||
| 170 | 129 | } |
| 171 | 130 | |
| 172 | 131 | $errors = apply_filters( 'frm_validate_' . $posted_field->type . '_field_entry', $errors, $posted_field, $value, $args ); |
| 173 | 132 | $errors = apply_filters( 'frm_validate_field_entry', $errors, $posted_field, $value, $args ); |
| 174 | - | |
| 175 | - if ( ! FrmAppHelper::pro_is_installed() && empty( $args['other'] ) ) { | |
| 176 | - FrmEntriesHelper::get_posted_value( $posted_field, $value, $args ); | |
| 177 | - } | |
| 178 | 133 | } |
| 179 | 134 | |
| 180 | 135 | /** |
| 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 | - * Maybe add item_name to $_POST to save it in items table. | |
| 349 | - * | |
| 350 | - * @since 5.2.02 | |
| 351 | - * | |
| 352 | - * @param array|string $value Field value. | |
| 353 | - * @param object $field Field object. | |
| 354 | - */ | |
| 355 | - private static function maybe_add_item_name( $value, $field ) { | |
| 356 | - $item_name = false; | |
| 357 | - if ( 'name' === $field->type ) { | |
| 358 | - $field_obj = FrmFieldFactory::get_field_object( $field ); | |
| 359 | - $item_name = $field_obj->get_display_value( $value ); | |
| 360 | - } elseif ( 'text' === $field->type ) { | |
| 361 | - $item_name = $value; | |
| 362 | - } | |
| 363 | - | |
| 364 | - if ( false !== $item_name ) { | |
| 365 | - // Item name has a max length of 255 characters so truncate it so it doesn't fail to save in the database. | |
| 366 | - $_POST['item_name'] = substr( $item_name, 0, 255 ); | |
| 367 | - } | |
| 368 | - } | |
| 369 | - | |
| 370 | - /** | |
| 371 | 136 | * Set $value to an empty string if it matches its label |
| 372 | 137 | * |
| 373 | 138 | * @param object $field |
| 374 | 139 | * @param string $value |
| @@ -395,11 +160,10 @@ | ||
| 395 | 160 | } |
| 396 | 161 | } |
| 397 | 162 | |
| 398 | 163 | public static function validate_phone_field( &$errors, $field, $value, $args ) { |
| 399 | - $format_value = FrmField::get_option( $field, 'format' ); | |
| 164 | + if ( $field->type == 'phone' || ( $field->type == 'text' && FrmField::is_option_true_in_object( $field, 'format' ) ) ) { | |
| 400 | 165 | |
| 401 | - if ( $field->type === 'phone' || ( $field->type === 'text' && $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value ) ) ) { | |
| 402 | 166 | $pattern = self::phone_format( $field ); |
| 403 | 167 | |
| 404 | 168 | if ( ! preg_match( $pattern, $value ) ) { |
| 405 | 169 | $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' ); |
| @@ -413,11 +177,8 @@ | ||
| 413 | 177 | } else { |
| 414 | 178 | $pattern = FrmField::get_option( $field, 'format' ); |
| 415 | 179 | } |
| 416 | 180 | |
| 417 | - // Ampersands are saved as &. | |
| 418 | - // Reverse it here so we are checking for the correct character. | |
| 419 | - $pattern = html_entity_decode( $pattern ); | |
| 420 | 181 | $pattern = apply_filters( 'frm_phone_pattern', $pattern, $field ); |
| 421 | 182 | |
| 422 | 183 | // Create a regexp if format is not already a regexp |
| 423 | 184 | if ( strpos( $pattern, '^' ) !== 0 ) { |
| @@ -474,36 +235,27 @@ | ||
| 474 | 235 | return $pattern; |
| 475 | 236 | } |
| 476 | 237 | |
| 477 | 238 | /** |
| 478 | - * Check for spam. | |
| 239 | + * Check for spam | |
| 479 | 240 | * |
| 480 | - * @param bool $exclude | |
| 241 | + * @param boolean $exclude | |
| 481 | 242 | * @param array $values |
| 482 | - * @param array $errors By reference. | |
| 243 | + * @param array $errors by reference | |
| 483 | 244 | */ |
| 484 | 245 | 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 | - if ( ! empty( $exclude ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) { | |
| 246 | + if ( ! empty( $exclude ) || ! isset( $values['item_meta'] ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) { | |
| 491 | 247 | // only check spam if there are no other errors |
| 492 | 248 | return; |
| 493 | 249 | } |
| 494 | 250 | |
| 495 | 251 | $antispam_check = self::is_antispam_check( $values['form_id'] ); |
| 496 | - $spam_msg = FrmAntiSpamController::get_default_spam_message(); | |
| 497 | 252 | if ( is_string( $antispam_check ) ) { |
| 498 | 253 | $errors['spam'] = $antispam_check; |
| 499 | 254 | } 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 | - } | |
| 255 | + $errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' ); | |
| 256 | + } elseif ( self::blacklist_check( $values ) ) { | |
| 257 | + $errors['spam'] = __( 'Your entry appears to be blocked spam!', 'formidable' ); | |
| 506 | 258 | } |
| 507 | 259 | |
| 508 | 260 | if ( isset( $errors['spam'] ) || self::form_is_in_progress( $values ) ) { |
| 509 | 261 | return; |
| @@ -529,10 +281,9 @@ | ||
| 529 | 281 | } |
| 530 | 282 | |
| 531 | 283 | /** |
| 532 | 284 | * @param int $form_id |
| 533 | - * | |
| 534 | - * @return bool|string | |
| 285 | + * @return boolean | |
| 535 | 286 | */ |
| 536 | 287 | private static function is_antispam_check( $form_id ) { |
| 537 | 288 | $aspm = new FrmAntiSpam( $form_id ); |
| 538 | 289 | return $aspm->validate(); |
| @@ -539,9 +290,9 @@ | ||
| 539 | 290 | } |
| 540 | 291 | |
| 541 | 292 | /** |
| 542 | 293 | * @param array $values |
| 543 | - * @return bool | |
| 294 | + * @return boolean | |
| 544 | 295 | */ |
| 545 | 296 | private static function is_honeypot_spam( $values ) { |
| 546 | 297 | $honeypot = new FrmHoneypot( $values['form_id'] ); |
| 547 | 298 | return ! $honeypot->validate(); |
| @@ -547,9 +298,9 @@ | ||
| 547 | 298 | return ! $honeypot->validate(); |
| 548 | 299 | } |
| 549 | 300 | |
| 550 | 301 | /** |
| 551 | - * @return bool | |
| 302 | + * @return boolean | |
| 552 | 303 | */ |
| 553 | 304 | private static function is_spam_bot() { |
| 554 | 305 | $ip = FrmAppHelper::get_ip_address(); |
| 555 | 306 | |
| @@ -557,9 +308,9 @@ | ||
| 557 | 308 | } |
| 558 | 309 | |
| 559 | 310 | /** |
| 560 | 311 | * @param array $values |
| 561 | - * @return bool | |
| 312 | + * @return boolean | |
| 562 | 313 | */ |
| 563 | 314 | private static function is_akismet_spam( $values ) { |
| 564 | 315 | global $wpcom_api_key; |
| 565 | 316 | |
| @@ -575,23 +326,62 @@ | ||
| 575 | 326 | |
| 576 | 327 | return ( ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() ) ); |
| 577 | 328 | } |
| 578 | 329 | |
| 330 | + public static function blacklist_check( $values ) { | |
| 331 | + if ( ! apply_filters( 'frm_check_blacklist', true, $values ) ) { | |
| 332 | + return false; | |
| 333 | + } | |
| 334 | + | |
| 335 | + $mod_keys = trim( self::get_disallowed_words() ); | |
| 336 | + if ( empty( $mod_keys ) ) { | |
| 337 | + return false; | |
| 338 | + } | |
| 339 | + | |
| 340 | + $content = FrmEntriesHelper::entry_array_to_string( $values ); | |
| 341 | + if ( empty( $content ) ) { | |
| 342 | + return false; | |
| 343 | + } | |
| 344 | + | |
| 345 | + self::prepare_values_for_spam_check( $values ); | |
| 346 | + $ip = FrmAppHelper::get_ip_address(); | |
| 347 | + $user_agent = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ); | |
| 348 | + $user_info = self::get_spam_check_user_info( $values ); | |
| 349 | + | |
| 350 | + return self::check_disallowed_words( $user_info['comment_author'], $user_info['comment_author_email'], $user_info['comment_author_url'], $content, $ip, $user_agent ); | |
| 351 | + } | |
| 352 | + | |
| 579 | 353 | /** |
| 580 | - * Checks spam using WordPress disallowed words and Frm denylist. | |
| 354 | + * For WP 5.5 compatibility. | |
| 581 | 355 | * |
| 582 | - * @param array $values Entry values. | |
| 356 | + * @since 4.06.02 | |
| 357 | + */ | |
| 358 | + private static function check_disallowed_words( $author, $email, $url, $content, $ip, $user_agent ) { | |
| 359 | + if ( function_exists( 'wp_check_comment_disallowed_list' ) ) { | |
| 360 | + return wp_check_comment_disallowed_list( $author, $email, $url, $content, $ip, $user_agent ); | |
| 361 | + } else { | |
| 362 | + return wp_blacklist_check( $author, $email, $url, $content, $ip, $user_agent ); | |
| 363 | + } | |
| 364 | + } | |
| 365 | + | |
| 366 | + /** | |
| 367 | + * For WP 5.5 compatibility. | |
| 583 | 368 | * |
| 584 | - * @return bool | |
| 369 | + * @since 4.06.02 | |
| 585 | 370 | */ |
| 586 | - public static function blacklist_check( $values ) { | |
| 587 | - return FrmAntiSpamController::contains_wp_disallowed_words( $values ) || FrmAntiSpamController::is_denylist_spam( $values ); | |
| 371 | + private static function get_disallowed_words() { | |
| 372 | + $keys = get_option( 'disallowed_keys' ); | |
| 373 | + if ( false === $keys ) { | |
| 374 | + // Fallback for WP < 5.5. | |
| 375 | + $keys = get_option( 'blacklist_keys' ); | |
| 376 | + } | |
| 377 | + return $keys; | |
| 588 | 378 | } |
| 589 | 379 | |
| 590 | 380 | /** |
| 591 | 381 | * Check entries for Akismet spam |
| 592 | 382 | * |
| 593 | - * @return bool true if is spam | |
| 383 | + * @return boolean true if is spam | |
| 594 | 384 | */ |
| 595 | 385 | public static function akismet( $values ) { |
| 596 | 386 | if ( empty( $values['item_meta'] ) ) { |
| 597 | 387 | return false; |
| @@ -613,9 +403,9 @@ | ||
| 613 | 403 | |
| 614 | 404 | $query_string = _http_build_query( $datas, '', '&' ); |
| 615 | 405 | $response = Akismet::http_post( $query_string, 'comment-check' ); |
| 616 | 406 | |
| 617 | - return ( is_array( $response ) && $response[1] === 'true' ); | |
| 407 | + return ( is_array( $response ) && $response[1] == 'true' ); | |
| 618 | 408 | } |
| 619 | 409 | |
| 620 | 410 | /** |
| 621 | 411 | * @since 2.0 |
| @@ -624,9 +414,8 @@ | ||
| 624 | 414 | self::add_site_info_to_akismet( $datas ); |
| 625 | 415 | self::add_server_values_to_akismet( $datas ); |
| 626 | 416 | |
| 627 | 417 | self::prepare_values_for_spam_check( $values ); |
| 628 | - self::skip_adding_values_to_akismet( $values ); | |
| 629 | 418 | |
| 630 | 419 | self::add_user_info_to_akismet( $datas, $values ); |
| 631 | 420 | self::add_comment_content_to_akismet( $datas, $values ); |
| 632 | 421 | } |
| @@ -656,14 +445,13 @@ | ||
| 656 | 445 | /** |
| 657 | 446 | * Gets user info for Akismet spam check. |
| 658 | 447 | * |
| 659 | 448 | * @since 5.0.13 Separate code for guest. Handle value of embedded|repeater. |
| 660 | - * @since 6.21 This changed from private to public. | |
| 661 | 449 | * |
| 662 | 450 | * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}. |
| 663 | 451 | * @return array |
| 664 | 452 | */ |
| 665 | - public static function get_spam_check_user_info( $values ) { | |
| 453 | + private static function get_spam_check_user_info( $values ) { | |
| 666 | 454 | if ( ! is_user_logged_in() ) { |
| 667 | 455 | return self::get_spam_check_user_info_for_guest( $values ); |
| 668 | 456 | } |
| 669 | 457 | |
| @@ -720,10 +508,9 @@ | ||
| 720 | 508 | */ |
| 721 | 509 | private static function recursive_add_akismet_guest_info( &$datas, $values, $custom_index = null ) { |
| 722 | 510 | foreach ( $values as $index => $value ) { |
| 723 | 511 | if ( ! $datas['missing_keys'] ) { |
| 724 | - // Found all info. | |
| 725 | - return; | |
| 512 | + return; // Found all info. | |
| 726 | 513 | } |
| 727 | 514 | |
| 728 | 515 | if ( is_array( $value ) ) { |
| 729 | 516 | self::recursive_add_akismet_guest_info( $datas, $value, $index ); |
| @@ -731,9 +518,9 @@ | ||
| 731 | 518 | } |
| 732 | 519 | |
| 733 | 520 | $field_id = ! is_null( $custom_index ) ? $custom_index : $index; |
| 734 | 521 | 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 ); | |
| 522 | + $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'] ); | |
| 736 | 523 | if ( $found ) { |
| 737 | 524 | $datas[ $key ] = $value; |
| 738 | 525 | $datas['frm_duplicated'][] = $field_id; |
| 739 | 526 | unset( $datas['missing_keys'][ $key_index ] ); |
| @@ -738,9 +525,9 @@ | ||
| 738 | 525 | $datas['frm_duplicated'][] = $field_id; |
| 739 | 526 | unset( $datas['missing_keys'][ $key_index ] ); |
| 740 | 527 | } |
| 741 | 528 | } |
| 742 | - }//end foreach | |
| 529 | + } | |
| 743 | 530 | } |
| 744 | 531 | |
| 745 | 532 | /** |
| 746 | 533 | * Checks if given value is an akismet guest info. |
| @@ -750,13 +537,11 @@ | ||
| 750 | 537 | * @param string $key Guest info key. |
| 751 | 538 | * @param string $value Value to check. |
| 752 | 539 | * @param int $field_id Field ID. |
| 753 | 540 | * @param array $name_field_ids Name field IDs. |
| 754 | - * @param array $values Array of posted values. | |
| 755 | - * | |
| 756 | 541 | * @return bool |
| 757 | 542 | */ |
| 758 | - private static function is_akismet_guest_info_value( $key, &$value, $field_id, $name_field_ids, $values ) { | |
| 543 | + private static function is_akismet_guest_info_value( $key, $value, $field_id, $name_field_ids ) { | |
| 759 | 544 | if ( ! $value || is_numeric( $value ) ) { |
| 760 | 545 | return false; |
| 761 | 546 | } |
| 762 | 547 | |
| @@ -767,62 +552,18 @@ | ||
| 767 | 552 | case 'comment_author_url': |
| 768 | 553 | return 0 === strpos( $value, 'http' ); |
| 769 | 554 | |
| 770 | 555 | case 'comment_author': |
| 771 | - if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { | |
| 556 | + if ( $name_field_ids ) { | |
| 772 | 557 | // If there is name field in the form, we should always use it as author name. |
| 773 | - return true; | |
| 558 | + return in_array( $field_id, $name_field_ids, true ); | |
| 774 | 559 | } |
| 775 | - $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); | |
| 776 | - $fields = self::get_name_text_fields( $form_id ); | |
| 560 | + return strlen( $value ) < 200; | |
| 561 | + } | |
| 777 | 562 | |
| 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 | 563 | return false; |
| 793 | 564 | } |
| 794 | 565 | |
| 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 | 566 | private static function add_server_values_to_akismet( &$datas ) { |
| 826 | 567 | foreach ( $_SERVER as $key => $value ) { |
| 827 | 568 | $include_value = is_string( $value ) && ! preg_match( '/^HTTP_COOKIE/', $key ) && preg_match( '/^(HTTP_|REMOTE_ADDR|REQUEST_URI|DOCUMENT_URI)/', $key ); |
| 828 | 569 | |
| @@ -853,8 +594,10 @@ | ||
| 853 | 594 | } |
| 854 | 595 | unset( $datas['frm_duplicated'] ); |
| 855 | 596 | } |
| 856 | 597 | |
| 598 | + self::skip_adding_values_to_akismet( $values ); | |
| 599 | + | |
| 857 | 600 | $datas['comment_content'] = FrmEntriesHelper::entry_array_to_string( $values ); |
| 858 | 601 | } |
| 859 | 602 | |
| 860 | 603 | /** |
| @@ -864,74 +607,21 @@ | ||
| 864 | 607 | * |
| 865 | 608 | * @param array $values Entry values. |
| 866 | 609 | */ |
| 867 | 610 | private static function skip_adding_values_to_akismet( &$values ) { |
| 868 | - $skipped_fields = self::get_akismet_skipped_field_ids( $values ); | |
| 869 | - foreach ( $skipped_fields as $skipped_field ) { | |
| 870 | - if ( ! isset( $values['item_meta'][ $skipped_field->id ] ) ) { | |
| 871 | - continue; | |
| 611 | + $skipped_field_ids = self::get_akismet_skipped_field_ids( $values ); | |
| 612 | + foreach ( $skipped_field_ids as $field_id ) { | |
| 613 | + if ( isset( $values['item_meta'][ $field_id ] ) ) { | |
| 614 | + unset( $values['item_meta'][ $field_id ] ); | |
| 872 | 615 | } |
| 873 | - | |
| 874 | - if ( self::should_really_skip_field( $skipped_field, $values ) ) { | |
| 875 | - unset( $values['item_meta'][ $skipped_field->id ] ); | |
| 876 | - if ( isset( $values['item_meta']['other'][ $skipped_field->id ] ) ) { | |
| 877 | - unset( $values['item_meta']['other'][ $skipped_field->id ] ); | |
| 878 | - } | |
| 879 | - } | |
| 880 | 616 | } |
| 881 | 617 | } |
| 882 | 618 | |
| 883 | 619 | /** |
| 884 | - * Checks if a skip field should be really skipped. | |
| 885 | - * | |
| 886 | - * @since 5.02.04 | |
| 887 | - * | |
| 888 | - * @param object $field_data Object contains `id` and `options`. | |
| 889 | - * @param array $values Entry values. | |
| 890 | - * @return bool | |
| 891 | - */ | |
| 892 | - private static function should_really_skip_field( $field_data, $values ) { | |
| 893 | - if ( empty( $field_data->options ) ) { | |
| 894 | - // This is skipped field types. | |
| 895 | - return true; | |
| 896 | - } | |
| 897 | - | |
| 898 | - FrmAppHelper::unserialize_or_decode( $field_data->options ); | |
| 899 | - if ( ! $field_data->options ) { | |
| 900 | - // Check if an error happens when unserializing, or empty options. | |
| 901 | - return true; | |
| 902 | - } | |
| 903 | - | |
| 904 | - end( $field_data->options ); | |
| 905 | - $last_key = key( $field_data->options ); | |
| 906 | - | |
| 907 | - // If a choice field has no Other option. | |
| 908 | - if ( is_numeric( $last_key ) || 0 !== strpos( $last_key, 'other_' ) ) { | |
| 909 | - return true; | |
| 910 | - } | |
| 911 | - | |
| 912 | - // If a choice field has Other option, but Other is not selected. | |
| 913 | - if ( empty( $values['item_meta']['other'][ $field_data->id ] ) ) { | |
| 914 | - return true; | |
| 915 | - } | |
| 916 | - | |
| 917 | - // Check if submitted value is same as one of field option. | |
| 918 | - foreach ( $field_data->options as $option ) { | |
| 919 | - $option_value = ! is_array( $option ) ? $option : ( $option['value'] ?? '' ); | |
| 920 | - if ( $values['item_meta']['other'][ $field_data->id ] === $option_value ) { | |
| 921 | - return true; | |
| 922 | - } | |
| 923 | - } | |
| 924 | - | |
| 925 | - return false; | |
| 926 | - } | |
| 927 | - | |
| 928 | - /** | |
| 929 | 620 | * Gets field IDs that are skipped from sending to Akismet spam check. |
| 930 | 621 | * |
| 931 | 622 | * @since 5.0.09 |
| 932 | 623 | * @since 5.0.13 Move out get_all_form_ids_and_flatten_meta() call and get `form_ids` from `$values`. |
| 933 | - * @since 5.2.04 This method returns array of object contains `id` and `options` instead of array of `id` only. | |
| 934 | 624 | * |
| 935 | 625 | * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}. |
| 936 | 626 | * @return array |
| 937 | 627 | */ |
| @@ -945,13 +635,20 @@ | ||
| 945 | 635 | |
| 946 | 636 | $where = array( |
| 947 | 637 | array( |
| 948 | 638 | 'form_id' => $values['form_ids'], |
| 949 | - 'type' => array_merge( $skipped_types, $has_other_types ), | |
| 639 | + array( | |
| 640 | + array( | |
| 641 | + 'field_options not like' => ';s:5:"other";s:1:"1"', | |
| 642 | + 'type' => $has_other_types, | |
| 643 | + ), | |
| 644 | + 'or' => 1, | |
| 645 | + 'type' => $skipped_types, | |
| 646 | + ), | |
| 950 | 647 | ), |
| 951 | 648 | ); |
| 952 | 649 | |
| 953 | - return FrmDb::get_results( 'frm_fields', $where, 'id,options' ); | |
| 650 | + return FrmDb::get_col( 'frm_fields', $where ); | |
| 954 | 651 | } |
| 955 | 652 | |
| 956 | 653 | /** |
| 957 | 654 | * Prepares values array for spam check. |
| @@ -956,13 +653,12 @@ | ||
| 956 | 653 | /** |
| 957 | 654 | * Prepares values array for spam check. |
| 958 | 655 | * |
| 959 | 656 | * @since 5.0.13 |
| 960 | - * @since 6.21 This changed from private to public. | |
| 961 | 657 | * |
| 962 | 658 | * @param array $values Entry values. |
| 963 | 659 | */ |
| 964 | - public static function prepare_values_for_spam_check( &$values ) { | |
| 660 | + private static function prepare_values_for_spam_check( &$values ) { | |
| 965 | 661 | $form_ids = self::get_all_form_ids_and_flatten_meta( $values ); |
| 966 | 662 | $values['form_ids'] = $form_ids; |
| 967 | 663 | } |
| 968 | 664 | |
| @@ -981,10 +677,9 @@ | ||
| 981 | 677 | |
| 982 | 678 | // Blacklist check for File field in the old version doesn't contain `form_id`. |
| 983 | 679 | $form_ids = isset( $values['form_id'] ) ? array( absint( $values['form_id'] ) ) : array(); |
| 984 | 680 | foreach ( $values['item_meta'] as $field_id => $value ) { |
| 985 | - if ( ! is_numeric( $field_id ) ) { | |
| 986 | - // Maybe `other`. | |
| 681 | + if ( ! is_numeric( $field_id ) ) { // Maybe `other`. | |
| 987 | 682 | continue; |
| 988 | 683 | } |
| 989 | 684 | |
| 990 | 685 | // Convert name array to string. |
| @@ -1020,16 +715,46 @@ | ||
| 1020 | 715 | |
| 1021 | 716 | $values['name_field_ids'][] = $subsubindex; |
| 1022 | 717 | } |
| 1023 | 718 | |
| 1024 | - if ( is_array( $values['item_meta'][ $subsubindex ] ) ) { | |
| 1025 | - $values['item_meta'][ $subsubindex ][] = $subsubvalue; | |
| 1026 | - } | |
| 719 | + $values['item_meta'][ $subsubindex ][] = $subsubvalue; | |
| 1027 | 720 | } |
| 1028 | - }//end foreach | |
| 721 | + } | |
| 1029 | 722 | |
| 1030 | 723 | unset( $values['item_meta'][ $field_id ] ); |
| 1031 | - }//end foreach | |
| 724 | + } | |
| 1032 | 725 | |
| 1033 | 726 | return $form_ids; |
| 727 | + } | |
| 728 | + | |
| 729 | + /** | |
| 730 | + * @deprecated 3.0 | |
| 731 | + * @codeCoverageIgnore | |
| 732 | + */ | |
| 733 | + public static function validate_url_field( &$errors, $field, $value, $args ) { | |
| 734 | + FrmDeprecated::validate_url_field( $errors, $field, $value, $args ); | |
| 735 | + } | |
| 736 | + | |
| 737 | + /** | |
| 738 | + * @deprecated 3.0 | |
| 739 | + * @codeCoverageIgnore | |
| 740 | + */ | |
| 741 | + public static function validate_email_field( &$errors, $field, $value, $args ) { | |
| 742 | + FrmDeprecated::validate_email_field( $errors, $field, $value, $args ); | |
| 743 | + } | |
| 744 | + | |
| 745 | + /** | |
| 746 | + * @deprecated 3.0 | |
| 747 | + * @codeCoverageIgnore | |
| 748 | + */ | |
| 749 | + public static function validate_number_field( &$errors, $field, $value, $args ) { | |
| 750 | + FrmDeprecated::validate_number_field( $errors, $field, $value, $args ); | |
| 751 | + } | |
| 752 | + | |
| 753 | + /** | |
| 754 | + * @deprecated 3.0 | |
| 755 | + * @codeCoverageIgnore | |
| 756 | + */ | |
| 757 | + public static function validate_recaptcha( &$errors, $field, $args ) { | |
| 758 | + FrmDeprecated::validate_recaptcha( $errors, $field, $args ); | |
| 1034 | 759 | } |
| 1035 | 760 | } |