1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\Model; |
4
|
|
|
use App\Foundation\Tire; |
5
|
|
|
use Illuminate\Support\Facades\Cache; |
6
|
|
|
use App\Model\Admin\SensitiveWord; |
7
|
|
|
use App\Model\Admin\Config as SiteConfig; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* 直接从数据库获取系统后台配置 |
11
|
|
|
* |
12
|
|
|
* @param string $key key |
13
|
|
|
* @param mixed $default key不存在时的默认值 |
14
|
|
|
* @return mixed key对应的value |
15
|
|
|
*/ |
16
|
|
|
function getConfig($key, $default = null) |
17
|
|
|
{ |
18
|
|
|
$v = SiteConfig::where('key', $key)->value('value'); |
19
|
|
|
return !is_null($v) ? $v : $default; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
function parseEntityFieldParams($params) |
23
|
|
|
{ |
24
|
|
|
if (strpos($params, 'getFormItemsFrom') === 0 && function_exists($params)) { |
25
|
|
|
$params = call_user_func($params); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$items = explode("\n", $params); |
29
|
|
|
return array_map(function ($item) { |
30
|
|
|
return explode("=", $item); |
31
|
|
|
}, $items); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
function isChecked($value, $options) |
35
|
|
|
{ |
36
|
|
|
return in_array($value, explode(',', $options), true); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
function isCheckedByAnd($value, $options) |
40
|
|
|
{ |
41
|
|
|
return ($options & $value) == $value; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
function xssFilter($data) |
45
|
|
|
{ |
46
|
|
|
if (is_string($data)) { |
47
|
|
|
return htmlspecialchars($data, ENT_QUOTES | ENT_SUBSTITUTE, 'utf-8'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$attributes = $data->getAttributes(); |
51
|
|
|
foreach ($attributes as &$v) { |
52
|
|
|
if (is_string($v)) { |
53
|
|
|
$v = htmlspecialchars($v, ENT_QUOTES | ENT_SUBSTITUTE, 'utf-8'); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
$data->setRawAttributes($attributes); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
function initTire() |
60
|
|
|
{ |
61
|
|
|
return Cache::rememberForever('sensitive_words_tire', function () { |
62
|
|
|
$tires = []; |
63
|
|
|
|
64
|
|
|
foreach (['noun', 'verb', 'exclusive'] as $v) { |
65
|
|
|
$words = SensitiveWord::query()->select($v)->where($v, '<>', '')->get(); |
66
|
|
|
|
67
|
|
|
$tire = new Tire(); |
68
|
|
|
foreach ($words as $k) { |
69
|
|
|
$tire->add($k->$v); |
70
|
|
|
} |
71
|
|
|
$tires[$v] = $tire; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $tires; |
75
|
|
|
}); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
function initTireSingle() |
79
|
|
|
{ |
80
|
|
|
return Cache::rememberForever('sensitive_words_tire_single', function () { |
81
|
|
|
$types = SensitiveWord::query()->select('type')->groupBy('type')->get(); |
82
|
|
|
$tire = new Tire(); |
83
|
|
|
foreach ($types as $type) { |
84
|
|
|
$words = SensitiveWord::query()->where('type', $type->type)->get(); |
85
|
|
|
$nouns = []; |
86
|
|
|
$verbs = []; |
87
|
|
|
$exclusives = []; |
88
|
|
|
foreach ($words as $word) { |
89
|
|
|
if ($word->noun !== '') { |
|
|
|
|
90
|
|
|
$nouns[] = $word->noun; |
91
|
|
|
} elseif ($word->verb !== '') { |
|
|
|
|
92
|
|
|
$verbs[] = $word->verb; |
93
|
|
|
} elseif ($word->exclusive !== '') { |
|
|
|
|
94
|
|
|
$exclusives[] = $word->exclusive; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
foreach ($exclusives as $k) { |
99
|
|
|
$tire->add($k); |
100
|
|
|
} |
101
|
|
|
foreach ($verbs as $vk) { |
102
|
|
|
foreach ($nouns as $nk) { |
103
|
|
|
$tire->add($vk . $nk); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $tire; |
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
function mapTypeToVerbOfSensitiveWords() |
113
|
|
|
{ |
114
|
|
|
return Cache::rememberForever('sensitive_verb_words', function () { |
115
|
|
|
$words = SensitiveWord::query()->select('verb', 'type')->where('verb', '<>', '')->get(); |
116
|
|
|
|
117
|
|
|
$data = []; |
118
|
|
|
foreach ($words as $word) { |
119
|
|
|
$data[$word->type <> '' ? $word->type : 'others'][] = $word->verb; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $data; |
123
|
|
|
}); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* 敏感词检查 |
128
|
|
|
* |
129
|
|
|
* @param string $text 待检查文本 |
130
|
|
|
* @param string $type 名词、动词的检测方法。默认为 join 。join:名词和动词相连组合在一起视为违规 all:名词和动词只要同时出现即为违规 |
131
|
|
|
* @param mixed $mode 检查模式。仅 $type 为 all 时有效。默认名词、动词、专用词都检查,显示可指定为 noun verb exclusive |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
function checkSensitiveWords(string $text, $type = 'join', $mode = null) |
135
|
|
|
{ |
136
|
|
|
if (!is_null($mode) && !in_array($mode, ['noun', 'verb', 'exclusive'])) { |
137
|
|
|
throw new \InvalidArgumentException('mode参数无效,只能为null值、noun、exclusive'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ($type === 'join') { |
141
|
|
|
$tire = initTireSingle(); |
142
|
|
|
$result = $tire->seek($text); |
143
|
|
|
return $result; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$tires = initTire(); |
147
|
|
|
if (!is_null($mode)) { |
148
|
|
|
return $tires[$mode]->seek($text); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$result = []; |
152
|
|
|
$return = []; |
153
|
|
|
foreach ($tires as $k => $tire) { |
154
|
|
|
$result[$k] = $tire->seek($text); |
155
|
|
|
} |
156
|
|
|
if (!empty($result['noun']) && !empty($result['verb'])) { |
157
|
|
|
$data = mapTypeToVerbOfSensitiveWords(); |
158
|
|
|
foreach ($result['noun'] as $noun) { |
159
|
|
|
$type = Cache::rememberForever('sensitive_words_noun_type:' . $noun, function () use ($noun) { |
160
|
|
|
return SensitiveWord::query()->where('noun', $noun)->value('type'); |
161
|
|
|
}); |
162
|
|
|
$type = $type ? $type : 'others'; |
163
|
|
|
$verbs = array_intersect($data[$type], $result['verb']); |
164
|
|
|
if (!empty($verbs)) { |
165
|
|
|
array_push($verbs, $noun); |
166
|
|
|
$return[] = implode(' ', $verbs); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
return array_merge($return, $result['exclusive']); |
171
|
|
|
} |
172
|
|
|
|