prop( 'mail' ); if ( ! is_array( $mail ) ) { return; } $tag = trim( (string) $s['review_tag'] ); if ( '' === $tag ) { $tag = '【営業の可能性】'; } $mail['subject'] = $tag . ' ' . ( isset( $mail['subject'] ) ? $mail['subject'] : '' ); if ( 'route' === $s['review_action'] && is_email( $s['review_email'] ) ) { $mail['recipient'] = $s['review_email']; } $contact_form->set_properties( array( 'mail' => $mail ) ); } catch ( \Throwable $e ) { // 目印を付けられなくても、メールは通常どおり送る. return; } } /** * Sign. * * @param mixed $ts Parameter. * @param mixed $form_id Parameter. */ private static function sign( $ts, $form_id ) { return hash_hmac( 'sha256', $ts . '|' . $form_id, wp_salt( 'auth' ) ); } /** * Make token. * * @param mixed $form_id Parameter. */ public static function make_token( $form_id ) { $ts = time(); return $ts . '.' . (int) $form_id . '.' . self::sign( $ts, (int) $form_id ); } /** * Verify token. * * @param mixed $token Parameter. * @param mixed $form_id Parameter. * @param array $s Parameter. */ public static function verify_token( $token, $form_id, array $s ) { $parts = explode( '.', (string) $token ); if ( 3 !== count( $parts ) ) { return array( 'TOKEN_MISSING' ); } list( $ts, $fid, $sig ) = $parts; if ( ! ctype_digit( $ts ) || ! ctype_digit( $fid ) || (int) $fid !== (int) $form_id || ! hash_equals( self::sign( $ts, $fid ), $sig ) ) { return array( 'TOKEN_INVALID' ); } $elapsed = time() - (int) $ts; if ( $elapsed < (int) $s['min_seconds'] ) { return array( 'TOO_FAST' ); } if ( $elapsed > (int) $s['token_ttl_hours'] * HOUR_IN_SECONDS ) { return array( 'TOKEN_EXPIRED' ); } return array(); } /** * Inject fields. * * @param mixed $html Parameter. */ public static function inject_fields( $html ) { $form = class_exists( 'WPCF7_ContactForm' ) ? WPCF7_ContactForm::get_current() : null; $form_id = $form ? (int) $form->id() : 0; $extra = ''; $extra .= ''; return $html . $extra; } /** * Check. * * @param mixed $spam Parameter. * @param mixed $submission Parameter. */ public static function check( $spam, $submission ) { self::$review = null; if ( $spam ) { return $spam; // すでに他の仕組みでSpam判定済み. } try { $s = AISFG_Settings::get(); $form = $submission->get_contact_form(); $form_id = $form ? (int) $form->id() : 0; $ip = (string) $submission->get_meta( 'remote_ip' ); $reasons = array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- IP normalization is inside banned_client_ip(). $banned_ip = AISFG_Ban::banned_client_ip( $_SERVER, $s ); if ( '' !== $banned_ip ) { AISFG_Ban::touch_blocked( $banned_ip ); AISFG_Logger::log( $form_id, 'block', array( 'AUTO_BAN' ), $banned_ip ); $submission->add_spam_log( array( 'agent' => 'ais-formguard', 'reason' => 'AUTO_BAN', ) ); return true; } // 1) Honeypot(配列投稿はボットとみなす。sanitize_text_field は文字列専用のため先に分岐). // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- CF7 submission; array honeypot is a bot signal $hp_raw = isset( $_POST[ self::HP_FIELD ] ) ? wp_unslash( $_POST[ self::HP_FIELD ] ) : ''; if ( is_array( $hp_raw ) || '' !== trim( sanitize_text_field( (string) $hp_raw ) ) ) { $reasons[] = 'HONEYPOT'; } // 2) 署名付きトークン+経過時間. $token = isset( $_POST[ self::TOKEN_FIELD ] ) ? sanitize_text_field( wp_unslash( $_POST[ self::TOKEN_FIELD ] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification -- CF7 form token; not an admin nonce. $reasons = array_merge( $reasons, self::verify_token( $token, $form_id, $s ) ); // 3) Rate Limit(同一IP). if ( self::rate_limited( $ip, (int) $s['rate_limit'], (int) $s['rate_window_min'] * MINUTE_IN_SECONDS ) ) { $reasons[] = 'RATE_LIMIT'; } // 4) 本文の URL数 / 重複. $text = self::collect_text( $submission->get_posted_data() ); $url_count = (int) preg_match_all( '#https?://|www\.#i', $text ); if ( $url_count > (int) $s['max_urls'] ) { $reasons[] = 'TOO_MANY_URLS'; } if ( self::is_duplicate( $form_id, $text, (int) $s['dup_ttl_min'] * MINUTE_IN_SECONDS ) ) { $reasons[] = 'DUPLICATE'; } // 5) 管理サーバー(辞書ルール+AI)による営業・スパムの判定. // ここまでで問題がなかった送信だけを送る(サーバーの負担と費用を抑える。期限切れのあいだは送らない). $review = null; $cloud_failed = false; if ( empty( $reasons ) && AISFG_Cloud::configured( $s ) && ! AISFG_Cloud::paused( $s ) && ! AISFG_Cloud::unbound( $s ) ) { $cloud = AISFG_Cloud::evaluate( $s, array( 'request_id' => wp_generate_uuid4(), 'form_id' => $form_id, 'ip_masked' => AISFG_Logger::mask_ip( $ip ), 'text' => $text, 'signals' => array( 'elapsed_ms' => self::token_elapsed_ms( $token ), 'honeypot' => false, 'url_count' => $url_count, 'content_length' => (int) mb_strlen( $text ), 'local_reasons' => array(), ), ) ); if ( null === $cloud ) { $cloud_failed = true; // 判定できなかったので通す. } elseif ( 'block' === $cloud['decision'] ) { $reasons = $cloud['reasons'] ? $cloud['reasons'] : array( 'CLOUD_BLOCK' ); } elseif ( 'review' === $cloud['decision'] ) { $review = $cloud['reasons'] ? $cloud['reasons'] : array( 'SALES_LIKELY' ); } } if ( empty( $reasons ) && null === $review ) { AISFG_Logger::log( $form_id, 'pass', $cloud_failed ? array( 'CLOUD_ERROR' ) : array( 'OK' ), $ip ); return false; } if ( ! empty( $reasons ) ) { if ( 'enforce' === $s['mode'] ) { AISFG_Logger::log( $form_id, 'block', $reasons, $ip ); $submission->add_spam_log( array( 'agent' => 'ais-formguard', 'reason' => implode( ',', $reasons ), ) ); self::maybe_strike( $s, $reasons ); return true; // CF7 はメール送信せず、Spam扱いで終了. } // 見守りモード:ログのみ、送信は通す. AISFG_Logger::log( $form_id, 'monitor', $reasons, $ip ); return false; } // 営業の疑い:防御モードでは、止めずに目印を付けて送る。見守りモードでは記録のみ。. if ( 'enforce' === $s['mode'] ) { AISFG_Logger::log( $form_id, 'review', $review, $ip ); self::$review = array( 'reasons' => $review ); } else { AISFG_Logger::log( $form_id, 'monitor', $review, $ip ); } return false; } catch ( \Throwable $e ) { // Fail-open:プラグイン側の障害で正規の問い合わせを止めない. return $spam; } } /* ---------- ヘルパー ---------- */ /** * Maybe strike. * * @param array $s Parameter. * @param array $reasons Parameter. */ private static function maybe_strike( array $s, array $reasons ) { if ( empty( $s['ban_enabled'] ) || ! array_intersect( $reasons, AISFG_Ban::STRONG ) ) { return; } $c = AISFG_Ban::client( null, $s ); if ( ! $c['safe'] || AISFG_Ban::is_allowed( $c['ip'], $s ) ) { return; } AISFG_Ban::strike( $c['ip'], in_array( 'HONEYPOT', $reasons, true ) ? 'HONEYPOT' : 'TOKEN_MISSING', $s ); } /** * Rate limited. * * @param mixed $ip Parameter. * @param mixed $limit Parameter. * @param mixed $window Parameter. */ private static function rate_limited( $ip, $limit, $window ) { if ( '' === $ip ) { return false; } $key = 'aisfg_r_' . md5( $ip ); $r = get_transient( $key ); if ( ! is_array( $r ) || $r['start'] + $window < time() ) { $r = array( 'n' => 0, 'start' => time(), ); } ++$r['n']; set_transient( $key, $r, max( 1, $r['start'] + $window - time() ) ); return $r['n'] > $limit; } /** * Is duplicate. * * @param mixed $form_id Parameter. * @param mixed $text Parameter. * @param mixed $ttl Parameter. */ private static function is_duplicate( $form_id, $text, $ttl ) { $norm = trim( preg_replace( '/\s+/u', ' ', function_exists( 'mb_strtolower' ) ? mb_strtolower( $text ) : strtolower( $text ) ) ); if ( strlen( $norm ) < 10 ) { return false; // 短すぎる内容は重複判定しない. } $key = 'aisfg_d_' . md5( $form_id . '|' . $norm ); if ( false !== get_transient( $key ) ) { return true; } set_transient( $key, 1, $ttl ); return false; } /** * Collect text. * * @param mixed $posted Parameter. */ private static function collect_text( $posted ) { $buf = array(); if ( is_array( $posted ) ) { foreach ( $posted as $k => $v ) { if ( 0 === strpos( (string) $k, 'aisfg_' ) ) { continue; } if ( is_array( $v ) ) { array_walk_recursive( $v, function ( $x ) use ( &$buf ) { if ( is_scalar( $x ) ) { $buf[] = (string) $x; } } ); } elseif ( is_scalar( $v ) ) { $buf[] = (string) $v; } } } return implode( ' ', $buf ); } }