'monitor', // monitor = 見守り(遮断しない) / enforce = 防御(遮断する). 'level' => 'standard', 'setup_done' => 0, 'started_at' => 0, 'report_enabled' => 1, 'report_email' => '', 'min_seconds' => 3, 'rate_limit' => 5, 'rate_window_min' => 10, 'max_urls' => 2, 'token_ttl_hours' => 24, 'dup_ttl_min' => 60, 'retention_days' => 30, // 営業メール対策(クラウド連携). 'cloud_enabled' => 0, 'cloud_key' => '', 'cloud_timeout' => 10, 'review_action' => 'tag', // tag = 件名に目印 / route = 別の宛先へ. 'review_tag' => '【営業の可能性】', 'review_email' => '', // 攻撃元の自動ブロック(防御モードのときだけ動く). 'ban_enabled' => 1, 'ban_threshold' => 3, // この回数、確かなボットの手がかりが続いたらブロック. 'ban_window_min' => 10, // 上の回数を数える期間(分). 'ban_hours' => 24, // ブロックする時間(時間). 'ban_allow' => '', // ブロックしない IP / CIDR(改行区切り). 'trusted_proxies' => '', // 信頼するプロキシ・CDN の IP / CIDR(改行区切り). ); } /** * Presets. */ public static function presets() { return array( 'loose' => array( 'label' => 'ゆるめ', 'desc' => '正規の問い合わせを止めたくない方向け。明らかなボットだけを検知します。', 'values' => array( 'min_seconds' => 2, 'rate_limit' => 10, 'rate_window_min' => 10, 'max_urls' => 5, 'dup_ttl_min' => 30, ), ), 'standard' => array( 'label' => '標準(おすすめ)', 'desc' => 'ほとんどのサイトにおすすめ。明らかなボットと、繰り返しの迷惑送信を検知します。', 'values' => array( 'min_seconds' => 3, 'rate_limit' => 5, 'rate_window_min' => 10, 'max_urls' => 2, 'dup_ttl_min' => 60, ), ), 'strict' => array( 'label' => 'きびしめ', 'desc' => 'スパムが多くて困っている方向け。まれに正規の送信を検知する可能性があります。', 'values' => array( 'min_seconds' => 5, 'rate_limit' => 3, 'rate_window_min' => 10, 'max_urls' => 1, 'dup_ttl_min' => 120, ), ), ); } /** * Get merged settings. */ public static function get() { $saved = get_option( self::OPTION, array() ); if ( ! is_array( $saved ) ) { $saved = array(); } unset( $saved['cloud_url'] ); unset( $saved['send_body'] ); return wp_parse_args( $saved, self::defaults() ); } /** * Current saved settings without running migration (for sanitize). * * @return array */ private static function current_from_option() { $saved = get_option( self::OPTION, array() ); if ( ! is_array( $saved ) ) { $saved = array(); } unset( $saved['cloud_url'] ); unset( $saved['send_body'] ); return wp_parse_args( $saved, self::defaults() ); } /** * Apply level. * * @param mixed $level Parameter. */ public static function apply_level( $level ) { $p = self::presets(); if ( ! isset( $p[ $level ] ) ) { return; } $s = array_merge( self::get(), $p[ $level ]['values'] ); update_option( self::OPTION, $s ); } /** * Sanitize. * * @param mixed $in Parameter. */ public static function sanitize( $in ) { $cur = self::current_from_option(); $in = is_array( $in ) ? $in : array(); $out = array(); $mode = isset( $in['mode'] ) ? $in['mode'] : $cur['mode']; $out['mode'] = ( 'enforce' === $mode ) ? 'enforce' : 'monitor'; $ranges = array( 'min_seconds' => array( 0, 60 ), 'rate_limit' => array( 1, 100 ), 'rate_window_min' => array( 1, 1440 ), 'max_urls' => array( 0, 50 ), 'token_ttl_hours' => array( 1, 168 ), 'dup_ttl_min' => array( 1, 1440 ), 'retention_days' => array( 1, 365 ), ); foreach ( $ranges as $key => $r ) { $v = isset( $in[ $key ] ) ? absint( $in[ $key ] ) : (int) $cur[ $key ]; $out[ $key ] = max( $r[0], min( $r[1], $v ) ); } // 保護レベルは数値から自動判定。詳しい設定でプリセットを選んだときは、 // 数値が旧値のまま(JS 未実行)でも、そのプリセットを採用する. $posted_level = isset( $in['level'] ) ? sanitize_key( (string) $in['level'] ) : ''; $presets = self::presets(); if ( isset( $presets[ $posted_level ] ) ) { $preset_vals = $presets[ $posted_level ]['values']; $matches_preset = true; $matches_current = true; foreach ( $preset_vals as $k => $v ) { if ( (int) $out[ $k ] !== (int) $v ) { $matches_preset = false; } if ( (int) $out[ $k ] !== (int) $cur[ $k ] ) { $matches_current = false; } } if ( $matches_preset || $matches_current ) { foreach ( $preset_vals as $k => $v ) { $out[ $k ] = (int) $v; } $out['level'] = $posted_level; } else { $out['level'] = self::infer_level( $out ); } } else { $out['level'] = self::infer_level( $out ); } $out['setup_done'] = array_key_exists( 'setup_done', $in ) ? ( empty( $in['setup_done'] ) ? 0 : 1 ) : (int) $cur['setup_done']; $out['started_at'] = array_key_exists( 'started_at', $in ) ? absint( $in['started_at'] ) : (int) $cur['started_at']; $out['report_enabled'] = array_key_exists( 'report_enabled', $in ) ? ( empty( $in['report_enabled'] ) ? 0 : 1 ) : (int) $cur['report_enabled']; if ( array_key_exists( 'report_email', $in ) ) { $raw = is_string( $in['report_email'] ) ? trim( $in['report_email'] ) : ''; if ( '' === $raw ) { $out['report_email'] = ''; } else { $email = sanitize_email( $in['report_email'] ); if ( is_email( $email ) ) { $out['report_email'] = $email; } else { $out['report_email'] = (string) $cur['report_email']; add_settings_error( self::OPTION, 'aisfg_report_email', '送り先のメールアドレスが正しくありません。', 'error' ); } } } else { $out['report_email'] = (string) $cur['report_email']; } $out['cloud_enabled'] = array_key_exists( 'cloud_enabled', $in ) ? ( empty( $in['cloud_enabled'] ) ? 0 : 1 ) : (int) $cur['cloud_enabled']; // API キーは、空欄のまま保存したときは今の値を保つ。削除専用の印があるときだけ空にする(印は保存しない). if ( ! empty( $in['cloud_key_clear'] ) ) { $out['cloud_key'] = ''; } else { $key = array_key_exists( 'cloud_key', $in ) ? trim( sanitize_text_field( $in['cloud_key'] ) ) : ''; $out['cloud_key'] = ( '' !== $key ) ? substr( $key, 0, 128 ) : (string) $cur['cloud_key']; } $to = array_key_exists( 'cloud_timeout', $in ) ? absint( $in['cloud_timeout'] ) : (int) $cur['cloud_timeout']; $out['cloud_timeout'] = max( 1, min( 15, $to ) ); $act = array_key_exists( 'review_action', $in ) ? $in['review_action'] : $cur['review_action']; $out['review_action'] = ( 'route' === $act ) ? 'route' : 'tag'; $tag = array_key_exists( 'review_tag', $in ) ? mb_substr( trim( sanitize_text_field( $in['review_tag'] ) ), 0, 30 ) : (string) $cur['review_tag']; $out['review_tag'] = ( '' !== $tag ) ? $tag : '【営業の可能性】'; $out['review_email'] = array_key_exists( 'review_email', $in ) ? (string) sanitize_email( $in['review_email'] ) : (string) $cur['review_email']; $out['ban_enabled'] = array_key_exists( 'ban_enabled', $in ) ? ( empty( $in['ban_enabled'] ) ? 0 : 1 ) : (int) $cur['ban_enabled']; foreach ( array( 'ban_threshold' => array( 1, 20 ), 'ban_window_min' => array( 1, 120 ), 'ban_hours' => array( 1, 720 ), ) as $key => $r ) { $v = array_key_exists( $key, $in ) ? absint( $in[ $key ] ) : (int) $cur[ $key ]; $out[ $key ] = max( $r[0], min( $r[1], $v ) ); } foreach ( array( 'ban_allow', 'trusted_proxies' ) as $key ) { $out[ $key ] = array_key_exists( $key, $in ) ? AISFG_Ban::clean_list( $in[ $key ] ) : (string) $cur[ $key ]; } return $out; } /** * Infer the protection level from numeric values. * * @param array $vals Settings values. * @return string */ private static function infer_level( array $vals ) { foreach ( self::presets() as $name => $p ) { $match = true; foreach ( $p['values'] as $k => $v ) { if ( (int) $vals[ $k ] !== (int) $v ) { $match = false; break; } } if ( $match ) { return $name; } } return 'custom'; } }