diff --git a/dapper.php b/dapper.php index 879a5f8..a37014b 100644 --- a/dapper.php +++ b/dapper.php @@ -1146,206 +1146,108 @@ add_action( 'plugins_loaded', 'dapper_woo_conditional_load' ); // Late hook – // ──────────────────────────────────────────────── if ( class_exists( 'WPCF7' ) ) { - // 1. Old honeypot + token + speed check (keep it — layers are good) - add_action( 'wpcf7_before_send_mail', 'dapper_cf7_human_check', 9, 3 ); - function dapper_cf7_human_check( $contact_form, &$abort, $submission ) { - $form_id = $contact_form->id(); - $posted = $_POST; + // Add hidden fields to Contact Form 7 forms + add_filter('wpcf7_form_hidden_fields', 'dapper_cf7_hidden_fields'); + function dapper_cf7_hidden_fields($fields) { - // Speed check - $posted_time = isset( $posted['dapper_cf7_time'] ) ? (int) $posted['dapper_cf7_time'] : 0; - if ( $posted_time && ( time() - $posted_time < 5 ) ) { - $abort = true; - $submission->add_error( 'dapper_speed', 'Submission too fast — please try again.' ); - dapper_debug_log( "CF7 #$form_id blocked — too fast" ); - return; - } + $fields['dapper_ts'] = time(); + $fields['dapper_token'] = ''; - // Honeypot - foreach ( $posted as $key => $val ) { - if ( strpos( $key, 'dapper_cf7_hp_' ) === 0 && strlen( trim( $val ) ) > 0 ) { - $abort = true; - $submission->add_error( 'dapper_honeypot', 'Spam detected.' ); - dapper_debug_log( "CF7 #$form_id blocked — honeypot filled" ); - return; - } - } - - // Old JS token (keep for extra layer) - $token = trim( $posted['dapper_cf7_token'] ?? '' ); - if ( empty( $token ) || strpos( $token, 'cf7human_' ) !== 0 ) { - $abort = true; - $submission->add_error( 'dapper_js', 'Please enable JavaScript and try again.' ); - dapper_debug_log( "CF7 #$form_id blocked — missing/invalid JS token" ); - } + return $fields; } - add_filter( 'wpcf7_form_elements', 'dapper_cf7_inject_anti_spam_fields' ); - function dapper_cf7_inject_anti_spam_fields( $form ) { - $honeypot_name = 'dapper_cf7_hp_' . wp_generate_password( 7, false ); - $hidden_fields = ' - - -

- -

'; - return str_replace( '', $hidden_fields . '', $form ); - } + add_action('wp_footer', 'dapper_cf7_js_token', 100); + function dapper_cf7_js_token() { + if (!defined('WPCF7_VERSION')) return; + ?> + - + get_posted_data(); + // 0. HONEYPOT CHECK (FIRST LINE OF DEFENSE) + if (!empty($data['dapper_hp'])) { + dapper_debug_log('BLOCKED: Honeypot triggered'); + $result->invalidate('', 'Spam detected.'); + return $result; } - dapper_debug_log( 'Universal CF7 shortcode filter FIRED - processing form ID: ' . ( $attr['id'] ?? 'unknown' ) ); - - $unique = uniqid(); - $checkbox_html = ' -
- - - -

Quick check to help stop spam. Thanks!

-
'; - - // Insert after response div or before — aggressive match - $output = preg_replace( '/(
]*>.*?<\/div>)/is', '$1' . $checkbox_html, $output, 1 ); - - if ( strpos( $output, $checkbox_html ) === false ) { - $output = str_replace( '', $checkbox_html . '', $output ); + dapper_debug_log('CF7 VALIDATION FIRED'); + // 1. JS TOKEN REQUIRED + if (empty($data['dapper_token']) || strpos($data['dapper_token'], 'dpr_') !== 0) { + dapper_debug_log('BLOCKED: Missing JS token'); + $result->invalidate('', 'Spam detected.'); + return $result; + } + // 2. TIME CHECK + if (empty($data['dapper_ts'])) { + dapper_debug_log('BLOCKED: Missing timestamp'); + $result->invalidate('', 'Spam detected.'); + return $result; } - if ( strpos( $output, $checkbox_html ) === false ) { - $output .= $checkbox_html; // Last resort append - dapper_debug_log( 'Universal CF7 fallback append used for shortcode/widget' ); + $elapsed = time() - (int)$data['dapper_ts']; + + if ($elapsed < 3) { + dapper_debug_log('BLOCKED: Too fast'); + $result->invalidate('', 'Spam detected.'); + return $result; } - dapper_debug_log( 'Universal CF7 insertion COMPLETE for shortcode' ); + // 3. EMAIL PROTECTION + if (!empty($data['your-email'])) { + $email = sanitize_email($data['your-email']); + if (!is_email($email)) { + dapper_debug_log('BLOCKED: Invalid email'); + $result->invalidate('your-email', 'Invalid email.'); + return $result; + } + if (preg_match('/(test|asdf|123|spam)/i', $email)) { + dapper_debug_log('BLOCKED: Suspicious email'); + $result->invalidate('your-email', 'Invalid email.'); + return $result; + } + $domain = substr(strrchr($email, "@"), 1); - return $output; + if (!$domain || strlen($domain) < 3) { + dapper_debug_log('BLOCKED: Bad email domain'); + $result->invalidate('your-email', 'Invalid email.'); + return $result; + } + } + + + return $result; } + add_filter('wpcf7_form_elements', 'dapper_cf7_honeypot'); - - // 2. NEW: Visible checkbox — use reliable append method - add_filter( 'wpcf7_form_elements', 'dapper_cf7_append_human_checkbox', 100 ); // Higher priority = later - function dapper_cf7_append_human_checkbox( $form ) { - if ( get_option( 'dapper_enable_cf7_human_checkbox', 'on' ) !== 'on' ) { - return $form; - } - - // Debug: Confirm filter is firing (now using your helper) - dapper_debug_log( 'CF7 checkbox filter FIRED - form length before insert: ' . strlen( $form ) ); - - $unique = uniqid(); - $checkbox_html = ' -
- - - -

Quick check to help stop spam. Thanks!

-
'; - - // 1. Try after the response output div (common in widgets/Flatsome) - $form = preg_replace( '/(
]*>.*?<\/div>)/is', '$1' . $checkbox_html, $form, 1 ); - - // 2. If not found, insert before - if ( strpos( $form, $checkbox_html ) === false ) { - $form = str_replace( '', $checkbox_html . '', $form ); - } - - // 3. Ultimate fallback: just append to the end - if ( strpos( $form, $checkbox_html ) === false ) { - $form .= $checkbox_html; - dapper_debug_log( 'CF7 checkbox FALLBACK append used (no or response div match)' ); - } - - dapper_debug_log( 'CF7 checkbox filter COMPLETE - insertion attempted' ); - - return $form; - } - - // Tiny JS to set token + disable submit until checked - add_action( 'wp_footer', 'dapper_cf7_human_checkbox_js', 100 ); - function dapper_cf7_human_checkbox_js() { - if ( ! did_action( 'wpcf7_enqueue_scripts' ) ) return; - ?> - - add_error( 'dapper_human', 'Please confirm you are human.' ); - dapper_debug_log( "CF7 #{$contact_form->id()} blocked — checkbox not checked" ); - return; - } - - $token = trim( $posted['dapper_cf7_human_token'] ?? '' ); - if ( empty( $token ) || strpos( $token, 'cf7_human_' ) !== 0 || strlen( $token ) < 20 ) { - $abort = true; - $submission->add_error( 'dapper_human', 'Verification failed — please try again.' ); - dapper_debug_log( "CF7 #{$contact_form->id()} blocked — invalid/missing human token" ); - return; - } - - $time = (int) ( $posted['dapper_cf7_human_time'] ?? 0 ); - if ( $time && ( time() - $time < 4 ) ) { - $abort = true; - $submission->add_error( 'dapper_human', 'Submission too fast — please try again.' ); - dapper_debug_log( "CF7 #{$contact_form->id()} blocked — human check too fast" ); - } - } + function dapper_cf7_honeypot($form) { + $hp = ' + + '; + return $hp . $form; + } }