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