'POST', 'callback' => array( $this, 'handle_submit' ), 'permission_callback' => array( $this, 'verify_nonce' ), ) ); } /** Reject unauthenticated form submissions before their body is processed. */ public function verify_nonce( WP_REST_Request $request ) { $nonce = $request->get_header( 'X-WP-Nonce' ); if ( $nonce && wp_verify_nonce( $nonce, 'wp_rest' ) ) { return true; } return new WP_Error( 'kiyaanix_cwc_invalid_nonce', 'Nonce verification failed.', array( 'status' => 403 ) ); } public function handle_submit( WP_REST_Request $request ) { $permission = $this->verify_nonce( $request ); if ( is_wp_error( $permission ) ) { return $permission; } $body = $request->get_json_params(); if ( ! is_array( $body ) || empty( $body ) ) { return new WP_Error( 'kiyaanix_cwc_empty_body', 'No data received.', array( 'status' => 400 ) ); } // 2. Honeypot — a field named e.g. "website" that's hidden via CSS // and must stay empty. Real visitors never fill it; most bots do. $settings = Kiyaanix_CWC_Settings::get_all(); if ( ! empty( $settings['honeypot_enabled'] ) && ! empty( $body['_hp'] ) ) { // Return a fake success so the bot doesn't learn it was caught. return new WP_REST_Response( array( 'success' => true ), 200 ); } // 3. Identify the visitor from trusted server-side data. $ip = $this->get_client_ip(); $user_id = 0; $cooldown_user_id = 0; if ( is_user_logged_in() ) { $user = wp_get_current_user(); $cooldown_user_id = $user->ID; if ( ! empty( $settings['skip_fields_for_logged_in'] ) ) { $user_id = $user->ID; $body['name'] = $user->display_name; $body['email'] = $user->user_email; } } $cooldown_remaining = Kiyaanix_CWC_Rate_Limiter::cooldown_remaining( $cooldown_user_id, $ip ); if ( $cooldown_remaining > 0 ) { return new WP_Error( 'kiyaanix_cwc_cooldown_active', 'Please wait before sending another message.', array( 'status' => 429, 'retry_after' => $cooldown_remaining ) ); } // 4. Keep the existing per-IP submission-count limiter. if ( ! Kiyaanix_CWC_Rate_Limiter::check_and_increment( $ip ) ) { return new WP_Error( 'kiyaanix_cwc_rate_limited', 'Too many submissions. Please try again later.', array( 'status' => 429 ) ); } // 5. Validate the request after applying any logged-in auto-fill. // the server fills/overrides name & email from the WP user // record rather than trusting whatever the client sent for // those fields — closes the gap where a logged-in user's // browser could otherwise submit a different name/email. $name = isset( $body['name'] ) && is_scalar( $body['name'] ) ? sanitize_text_field( $body['name'] ) : ''; $email = isset( $body['email'] ) && is_scalar( $body['email'] ) ? sanitize_email( $body['email'] ) : ''; $phone = isset( $body['phone'] ) && is_scalar( $body['phone'] ) ? sanitize_text_field( $body['phone'] ) : ''; $message = isset( $body['message'] ) && is_scalar( $body['message'] ) ? sanitize_textarea_field( $body['message'] ) : ''; if ( empty( $message ) || ( empty( $name ) && ! $user_id ) || ( empty( $email ) && ! $user_id ) ) { return new WP_Error( 'kiyaanix_cwc_missing_fields', 'Required fields are missing.', array( 'status' => 422 ) ); } if ( ! $user_id && ! is_email( $email ) ) { return new WP_Error( 'kiyaanix_cwc_invalid_email', 'A valid email address is required.', array( 'status' => 422 ) ); } $meta = isset( $body['meta'] ) && is_array( $body['meta'] ) ? $body['meta'] : array(); $row = array( 'user_id' => $user_id, 'name' => $name, 'email' => $email, 'phone' => $phone, 'message' => $message, 'page_url' => isset( $meta['pageUrl'] ) && is_scalar( $meta['pageUrl'] ) ? esc_url_raw( $meta['pageUrl'] ) : '', 'referrer' => isset( $meta['referrer'] ) && is_scalar( $meta['referrer'] ) ? esc_url_raw( $meta['referrer'] ) : '', 'ip_address' => $ip, // authoritative, server-captured 'country' => $this->get_client_country( $ip ), // server-side only; never from the request body 'browser' => isset( $meta['browserName'] ) && is_scalar( $meta['browserName'] ) ? sanitize_text_field( $meta['browserName'] ) : '', 'os' => isset( $meta['os'] ) && is_scalar( $meta['os'] ) ? sanitize_text_field( $meta['os'] ) : '', 'device_type' => isset( $meta['deviceType'] ) && is_scalar( $meta['deviceType'] ) ? sanitize_text_field( $meta['deviceType'] ) : '', 'user_agent' => isset( $meta['userAgent'] ) && is_scalar( $meta['userAgent'] ) ? sanitize_text_field( $meta['userAgent'] ) : '', ); // Any other fields the site owner added via the widget's data-driven // `fields` config (e.g. a "topic" select) ride along into fields_json // rather than being dropped. $known = array( 'name', 'email', 'phone', 'message', 'meta', '_hp' ); foreach ( $body as $key => $value ) { if ( ! in_array( $key, $known, true ) && is_scalar( $value ) ) { $key = sanitize_key( $key ); if ( $key ) { $row[ $key ] = sanitize_text_field( $value ); } } } $id = Kiyaanix_CWC_DB::insert( $row ); if ( ! $id ) { return new WP_Error( 'kiyaanix_cwc_submission_failed', 'Unable to store the submission.', array( 'status' => 500 ) ); } $row['id'] = $id; $cooldown_seconds = Kiyaanix_CWC_Rate_Limiter::start_cooldown( $cooldown_user_id, $ip ); Kiyaanix_CWC_Notifications::dispatch( $row ); return new WP_REST_Response( array( 'success' => true, 'id' => $id, 'cooldown_seconds' => $cooldown_seconds ), 200 ); } /** Sanitized $_SERVER value, or '' when missing. */ private function server_value( $key ) { return isset( $_SERVER[ $key ] ) && is_scalar( $_SERVER[ $key ] ) ? trim( sanitize_text_field( wp_unslash( $_SERVER[ $key ] ) ) ) : ''; } /** * The visitor's IP address — the only source for the `ip_address` column * and the rate limiter. * * Uses REMOTE_ADDR by default. Proxy headers (CF-Connecting-IP, * X-Forwarded-For) can be sent by any visitor, so they are trusted only * when the site owner has said the site really sits behind that proxy * (the "trusted_proxy" setting), or via the kiyaanix_cwc_client_ip filter. */ public function get_client_ip() { $settings = Kiyaanix_CWC_Settings::get_all(); $mode = $settings['trusted_proxy']; $remote = $this->server_value( 'REMOTE_ADDR' ); $remote = filter_var( $remote, FILTER_VALIDATE_IP ) ? $remote : ''; $ip = $remote; if ( 'cloudflare' === $mode ) { $cf = $this->server_value( 'HTTP_CF_CONNECTING_IP' ); if ( filter_var( $cf, FILTER_VALIDATE_IP ) ) { $ip = $cf; } } elseif ( 'load_balancer' === $mode ) { // The proxy appends the address it received the request from, so // the right-most entry is the one it added. Entries to the left // may have been supplied by the visitor and are not trusted. $forwarded = array_reverse( array_map( 'trim', explode( ',', $this->server_value( 'HTTP_X_FORWARDED_FOR' ) ) ) ); foreach ( $forwarded as $candidate ) { if ( filter_var( $candidate, FILTER_VALIDATE_IP ) ) { $ip = $candidate; break; } } } /** * Filters the detected visitor IP address. * * @param string $ip Detected IP address. * @param string $remote REMOTE_ADDR as seen by PHP. * @param string $mode The trusted_proxy setting: none, cloudflare or load_balancer. */ $ip = (string) apply_filters( 'kiyaanix_cwc_client_ip', $ip, $remote, $mode ); return filter_var( $ip, FILTER_VALIDATE_IP ) ? $ip : ''; } /** * The visitor's country as an ISO 3166-1 alpha-2 code (e.g. "IN"), or ''. * * No external lookup: the country is read from the country header a CDN * adds (Cloudflare's CF-IPCountry, AWS CloudFront's * CloudFront-Viewer-Country). Like the proxy IP headers, these are only * trusted when the matching "trusted_proxy" setting is on, because a * visitor could otherwise send them. Developers can supply the country * from another source with the kiyaanix_cwc_client_country filter. */ public function get_client_country( $ip ) { $settings = Kiyaanix_CWC_Settings::get_all(); $mode = $settings['trusted_proxy']; $country = ''; if ( 'cloudflare' === $mode ) { $country = $this->server_value( 'HTTP_CF_IPCOUNTRY' ); } elseif ( 'load_balancer' === $mode ) { $country = $this->server_value( 'HTTP_CLOUDFRONT_VIEWER_COUNTRY' ); } $country = strtoupper( $country ); // "XX" = unknown and "T1" = Tor in Cloudflare's header. if ( ! preg_match( '/^[A-Z]{2}$/', $country ) || in_array( $country, array( 'XX', 'T1' ), true ) ) { $country = ''; } /** * Filters the visitor's country stored with a submission. * * @param string $country ISO 3166-1 alpha-2 code from a trusted CDN header, or ''. * @param string $ip The visitor IP from get_client_ip(). */ $country = (string) apply_filters( 'kiyaanix_cwc_client_country', $country, $ip ); return substr( sanitize_text_field( $country ), 0, 100 ); } }