|
@@ -12,8 +12,9 @@ use Psr\Http\Message\ResponseInterface;
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
-use function Hyperf\Support\env;
|
|
|
-
|
|
|
+use Hyperf\HttpMessage\Stream\SwooleStream;
|
|
|
+use Hyperf\Di\Annotation\Inject;
|
|
|
+use Hyperf\Redis\Redis;
|
|
|
class SensitiveMiddleware implements MiddlewareInterface
|
|
|
{
|
|
|
protected ContainerInterface $container;
|
|
@@ -21,7 +22,8 @@ class SensitiveMiddleware implements MiddlewareInterface
|
|
|
protected RequestInterface $request;
|
|
|
|
|
|
protected HttpResponse $response;
|
|
|
-
|
|
|
+ #[Inject]
|
|
|
+ protected Redis $redis;
|
|
|
public function __construct( RequestInterface $request, HttpResponse $response)
|
|
|
{
|
|
|
$this->request = $request;
|
|
@@ -31,53 +33,36 @@ class SensitiveMiddleware implements MiddlewareInterface
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
|
{
|
|
|
try {
|
|
|
- $req = $this->request->all();
|
|
|
- $concatenated = "";
|
|
|
- if($req){
|
|
|
- foreach ($req as $value) {
|
|
|
- if (is_array($value)) {
|
|
|
- // 如果值是数组,将数组元素用逗号连接
|
|
|
- $concatenated.= implode(',', $value);
|
|
|
- } else {
|
|
|
- // 如果不是数组,直接拼接
|
|
|
- $concatenated.= $value;
|
|
|
+ $badWords = $this->redis->sMembers('black_word'); //黑名单
|
|
|
+ $whiteWords = $this->redis->sMembers('white_word');//白名单
|
|
|
+ // 获取所有请求参数并拼接成文本
|
|
|
+ $params = $this->request->all();
|
|
|
+ $concatenated = "";
|
|
|
+ if($params){
|
|
|
+ foreach ($params as $value) {
|
|
|
+ if (is_array($value)) {
|
|
|
+ // 如果值是数组,将数组元素用逗号连接
|
|
|
+ $concatenated.= implode(',', $value);
|
|
|
+ } else {
|
|
|
+ // 如果不是数组,直接拼接
|
|
|
+ $concatenated.= $value;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- if(empty($concatenated)){
|
|
|
- return $handler->handle($request);
|
|
|
- }else{
|
|
|
- $concatenated = preg_replace('/[0-9]/', '', $concatenated);
|
|
|
- //白名单
|
|
|
- $filterArray = array("http", "https",".","cn");
|
|
|
- $concatenated = str_replace($filterArray, '', $concatenated);
|
|
|
- }
|
|
|
- $url = env("SENSITIVE_WORD")."?msg=".urlencode($concatenated);
|
|
|
- $rep = PublicData::http_get($url);
|
|
|
- if (preg_match('/\{[^{}]*"code":[^}]*\}/', $rep, $matches)) {
|
|
|
- $jsonString = $matches[0];
|
|
|
- // 解析 JSON 字符串为 PHP 数组
|
|
|
- $jsonData = json_decode($jsonString, true);
|
|
|
- if ($jsonData!== null) {
|
|
|
- // 输出解析后的 JSON 数据
|
|
|
- print_r($jsonData);
|
|
|
- if($jsonData['code']==200){
|
|
|
- if(isset($jsonData['num']) && $jsonData['num']>0){
|
|
|
- return $this->response->json(
|
|
|
- [
|
|
|
- 'code' => 0,
|
|
|
- 'data' => [],
|
|
|
- 'message' => "存在敏感词:".$jsonData['ci'],
|
|
|
- ]
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
+ // 遍历违禁词,检查是否在文本中存在
|
|
|
+ foreach ($badWords as $badWord) {
|
|
|
+ // 判断该违禁词是否在白名单中,如果在则跳过
|
|
|
+ if (in_array($badWord, $whiteWords)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (str_contains($concatenated, $badWord)) {
|
|
|
+ // 处理找到违禁词的情况,例如返回错误信息
|
|
|
+ $message = '发现违禁词: '. $badWord;
|
|
|
return $this->response->json(
|
|
|
[
|
|
|
'code' => 0,
|
|
|
'data' => [],
|
|
|
- 'message' => '解析敏感词接口失败',
|
|
|
+ 'message' => $message
|
|
|
]
|
|
|
);
|
|
|
}
|