Browse Source

feat: add block

igb 2 days ago
parent
commit
9be006d75e
2 changed files with 142 additions and 0 deletions
  1. 81 0
      app/Http/Controllers/ContactController.php
  2. 61 0
      config/content_filter.php

+ 81 - 0
app/Http/Controllers/ContactController.php

@@ -16,6 +16,71 @@ class ContactController extends Controller
         $this->liquidRenderer = $liquidRenderer;
     }
 
+    /**
+     * 关键字过滤方法
+     */
+    private function filterKeywords($data)
+    {
+        // 检查是否启用内容过滤
+        if (!config('content_filter.enabled', true)) {
+            return [
+                'blocked' => false,
+                'data' => $data,
+                'filtered_words' => []
+            ];
+        }
+
+        // 从配置文件获取过滤设置
+        $blockedWords = config('content_filter.blocked_words', []);
+        $fieldsToCheck = config('content_filter.filter_fields', ['customer_name', 'content']);
+        $maxLengths = config('content_filter.max_length', []);
+
+        $result = [
+            'blocked' => false,
+            'data' => $data,
+            'filtered_words' => []
+        ];
+
+        foreach ($fieldsToCheck as $field) {
+            if (!isset($data[$field])) continue;
+
+            $originalText = $data[$field];
+            $cleanText = $originalText;
+
+            // 长度检查
+            $maxLength = $maxLengths[$field] ?? 5000;
+            if (strlen($originalText) > $maxLength) {
+                $result['blocked'] = true;
+                break;
+            }
+
+            // 检查敏感词 - 直接阻止
+            foreach ($blockedWords as $word) {
+                if (stripos($originalText, $word) !== false) {
+                    $result['blocked'] = true;
+                    $result['filtered_words'][] = $word;
+                    
+                    // 记录过滤日志
+                    if (config('content_filter.log_filtered', false)) {
+                        \Log::warning('Content blocked due to sensitive word', [
+                            'field' => $field,
+                            'word' => $word,
+                            'ip' => request()->ip(),
+                            'user_agent' => request()->userAgent()
+                        ]);
+                    }
+                    
+                    break 2; // 跳出所有循环
+                }
+            }
+
+            // 更新清理后的数据
+            $result['data'][$field] = trim($originalText);
+        }
+
+        return $result;
+    }
+
 
     function create()
     {
@@ -63,10 +128,26 @@ class ContactController extends Controller
                 // 其他字段的验证规则可以在这里添加
             ]);
 
+            // 关键字过滤
+            $filteredResult = $this->filterKeywords($validatedData);
+            if ($filteredResult['blocked']) {
 
+                $response = [
+                    'status' => 'error',
+                    'message' => 'System error, please try again later.',
+                ];
+    
+    
+                return response()->json($response, 403);
+
+            }
 
             // 从请求中获取所有数据
             $data = $request->all();
+            
+            // 应用过滤后的数据
+            $data['customer_name'] = $filteredResult['data']['customer_name'];
+            $data['content'] = $filteredResult['data']['content'];
 
             // 指定特殊字段的值
             $data['dist_id'] = 0; // app('dist')->id; // 指定当前登录的分销商ID

+ 61 - 0
config/content_filter.php

@@ -0,0 +1,61 @@
+<?php
+
+return [
+    
+    /*
+    |--------------------------------------------------------------------------
+    | 内容过滤配置
+    |--------------------------------------------------------------------------
+    |
+    | 这里配置了内容过滤相关的敏感词汇和过滤规则
+    |
+    */
+
+    // 敏感词 - 遇到这些词汇会直接阻止提交
+    'blocked_words' => [
+        // 严重脏话
+        // 'fuck', 'shit', 'damn', 'bitch', 'asshole',
+        // 中文严重敏感词
+        // '垃圾', '诈骗', '骗子', '傻逼', '操你妈', '草泥马', '妈的', '他妈的',
+        // // 政治敏感词
+        // '法轮功', '六四', '天安门事件',
+        // // 其他严重违规内容
+        // 'terror', 'bomb', 'kill', 'die', 'death',
+        // // 垃圾信息相关
+        // 'spam', 'scam', 'fake', 'fraud', 'cheat',
+        // // 恶意内容
+        // 'hack', 'virus', 'malware', 'phishing',
+        // // 商业垃圾信息
+        // 'casino', 'gambling', 'loan', 'debt', 'credit', 'money', 'cash',
+        // // 测试相关
+        // 'test', 'testing', '测试', 'asdf', 'qwerty', 'demo',
+        // // 广告相关
+        // 'advertisement', 'promotion', 'discount', 'free', 'win', 'prize',
+        // // 中文敏感词
+        // '赌博', '博彩', '贷款', '借钱', '投资', '理财', '股票', '彩票',
+        //http相关
+        'http', 'https', 'www',
+    ],
+
+    // 内容长度限制
+    'max_length' => [
+        'customer_name' => 255,
+        'content' => 5000,
+    ],
+
+    // 需要过滤的字段
+    'filter_fields' => [
+        'customer_name',
+        'content',
+        'email',
+        'whats_app',
+        'consulting_products',
+    ],
+
+    // 是否启用过滤功能
+    'enabled' => env('CONTENT_FILTER_ENABLED', true),
+
+    // 是否记录过滤日志
+    'log_filtered' => env('CONTENT_FILTER_LOG', false),
+
+];