Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
filter.php
1
<?php
2
namespace
Bitrix\Forum\BadWords
;
3
4
use
Bitrix\Forum\Forum
;
5
use
Bitrix\Main\ArgumentException
;
6
use
Bitrix\Main
;
7
use \Bitrix\Main\Localization\Loc;
8
use
Bitrix\Main\ORM\Data\Result
;
9
use
Bitrix\Main\ORM\Event
;
10
use
Bitrix\Main\ORM\Fields\BooleanField
;
11
use
Bitrix\Main\ORM\Fields\IntegerField
;
12
use
Bitrix\Main\ORM\Fields\StringField
;
13
use
Bitrix\Main\ORM\Fields\TextField
;
14
use
Bitrix\Main\ORM\Fields\EnumField
;
15
use \Bitrix\Main\ORM\Data\UpdateResult;
16
17
Loc::loadMessages
(__FILE__);
18
35
class
FilterTable
extends
\Bitrix\Main\Entity\DataManager
36
{
37
private
static
$dataById = [];
38
private
const
PATTERN_CREATE_METHOD_SIMPLE =
"WORDS"
;
39
private
const
PATTERN_CREATE_METHOD_TRANSLATE =
"TRNSL"
;
40
private
const
PATTERN_CREATE_METHOD_NONE =
"PTTRN"
;
41
42
public
static
function
getTableName
()
43
{
44
return
'b_forum_filter'
;
45
}
46
47
public
static
function
getMap
()
48
{
49
return
[
50
(
new
IntegerField
(
"ID"
, [
"primary"
=>
true
,
"autocomplete"
=>
true
])),
51
(
new
IntegerField
(
"DICTIONARY_ID"
, [
"required"
=>
true
])),
52
(
new
StringField
(
"WORDS"
, [
"size"
=> 255])),
53
(
new
TextField
(
"PATTERN"
, [
"required"
=>
true
])),
54
(
new
EnumField
(
"PATTERN_CREATE"
, [
55
"values"
=> [
56
self::PATTERN_CREATE_METHOD_SIMPLE,
57
self::PATTERN_CREATE_METHOD_TRANSLATE,
58
self::PATTERN_CREATE_METHOD_NONE
59
],
60
"default_value"
=> self::PATTERN_CREATE_METHOD_TRANSLATE,
61
"required"
=>
true
62
])),
63
(
new
StringField
(
"REPLACEMENT"
, [
"size"
=> 255,
"default_value"
=>
""
])),
64
(
new
TextField
(
"DESCRIPTION"
)),
65
(
new
BooleanField
(
"USE_IT"
, [
"values"
=> [
"N"
,
"Y"
],
"default_value"
=>
"Y"
])),
66
];
67
}
68
69
public
static
function
checkFields
(
Result
$result, $primary, array $data)
70
{
71
parent::checkFields($result, $primary, $data);
72
if
($result->
isSuccess
())
73
{
74
if
(array_key_exists(
"PATTERN"
, $data))
75
{
76
$res =
self::checkPattern
($data[
"PATTERN"
]);
77
if
(!$res->isSuccess())
78
{
79
$result->
addErrors
($res->getErrors());
80
}
81
82
}
83
}
84
if
($result->
isSuccess
() && ($result instanceof
UpdateResult
))
85
{
86
unset(self::$dataById[$primary[
"ID"
]]);
87
}
88
return
$result;
89
}
90
91
protected
static
function
checkPattern
(
string
$pattern)
92
{
93
$result =
new
Main\Result
();
94
95
if
($pattern ==
''
)
96
{
97
$result->addError(
new
Main\
Error
(
Loc::getMessage
(
"FLT_ERR_BAD_PATTERN"
).
" [empty]"
,
"emptyPattern"
));
98
}
99
else
if
(strpos($pattern,
"/"
) !== 0)
100
{
101
$result->addError(
new
Main\
Error
(
Loc::getMessage
(
"FLT_ERR_BAD_DELIMITER"
),
"badDelimiter"
));
102
}
103
else
if
(($modificators = substr($pattern, strrpos($pattern,
"/"
))) && strpos($modificators,
"e"
) !==
false
)
104
{
105
$result->addError(
new
Main\
Error
(
Loc::getMessage
(
"FLT_ERR_BAD_MODIFICATOR"
),
"badModificator"
));
106
}
107
else
108
{
109
try
110
{
111
preg_match($pattern,
"test string"
, $arTest);
112
}
113
catch
(\Throwable $exception)
114
{
115
$result->addError(
116
new
Main\
Error
(
117
Loc::getMessage
(
"FLT_ERR_BAD_PATTERN"
).
" "
.$exception->getMessage(),
118
"badPattern"
119
)
120
);
121
}
122
}
123
return
$result;
124
}
125
126
public
static
function
getDataById
($id, $ttl = 84600)
127
{
128
if
(!array_key_exists($id, self::$dataById))
129
{
130
self::$dataById[$id] = self::getList([
131
"select"
=> [
"*"
],
132
"filter"
=> [
"ID"
=> $id],
133
"cache"
=> [
134
"ttl"
=> $ttl
135
]
136
])->fetch();
137
}
138
return
self::$dataById[$id];
139
}
140
}
141
142
class
Filter
implements
\ArrayAccess
143
{
144
use \Bitrix\Forum\Internals\EntityFabric;
145
use \Bitrix\Forum\Internals\EntityBaseMethods;
146
148
protected
$id
;
150
protected
$data
;
151
152
public
function
__construct
($id)
153
{
154
$this->
id
=
$id
;
155
if
(
$id
<= 0)
156
{
157
throw
new \Bitrix\Main\ArgumentException(__CLASS__ .
" empty id."
);
158
}
159
else
if
(!(
$data
= FilterTable::getDataById($this->
id
)))
160
{
161
throw
new \Bitrix\Main\ArgumentException(__CLASS__ .
" data with id is empty."
);
162
}
163
else
164
{
165
$this->data =
$data
;
166
}
167
}
168
169
public
function
update
(array $fields)
170
{
171
$result =
new
Main\Result
();
172
173
if
(isset($fields[
"WORDS"
]) && $fields[
"WORDS"
] ==
''
)
174
{
175
$result->addError(
new
Main\Orm\
EntityError
(
176
Loc::getMessage
(
"FLT_ERR_DICT_PATT_MISSED"
),
177
"emptyData"
178
));
179
}
180
else
if
(isset($fields[
"DICTIONARY_ID"
]) && intval($fields[
"DICTIONARY_ID"
]) <= 0)
181
{
182
$result->addError(
new
Main\Orm\
EntityError
(
183
Loc::getMessage
(
"FLT_ERR_DICTIONARY_MISSED"
),
184
"emptyDictionaryId"
185
));
186
}
187
else
if
(isset($fields[
"WORDS"
]) && $fields[
"WORDS"
] !== $this[
"WORDS"
] ||
188
isset($fields[
"DICTIONARY_ID"
]) && $fields[
"DICTIONARY_ID"
] !== $this[
"DICTIONARY_ID"
] ||
189
isset($fields[
"PATTERN_CREATE"
]) && $fields[
"PATTERN_CREATE"
] !== $this[
"PATTERN_CREATE"
]
190
)
191
{
192
$fields[
"WORDS"
] = isset($fields[
"WORDS"
]) ? $fields[
"WORDS"
] : $this[
"WORDS"
];
193
$fields[
"DICTIONARY_ID"
] = isset($fields[
"DICTIONARY_ID"
]) ? $fields[
"DICTIONARY_ID"
] : $this[
"DICTIONARY_ID"
];
194
$fields[
"PATTERN_CREATE"
] = isset($fields[
"PATTERN_CREATE"
]) ? $fields[
"PATTERN_CREATE"
] : $this[
"PATTERN_CREATE"
];
195
if
(($wordEqual = FilterTable::getList([
196
"select"
=> [
"ID"
],
197
"filter"
=> [
198
"DICTIONARY_ID"
=> $fields[
"DICTIONARY_ID"
],
199
"WORDS"
=> $fields[
"WORDS"
],
200
"!ID"
=> $this->
id
201
]
202
])->fetch()) && !empty($wordEqual))
203
{
204
$result->addError(
new
Main\Orm\
EntityError
(
205
Loc::getMessage
(
"FLT_ALREADY_EXIST"
),
206
"alreadyExists"
207
));
208
}
209
else
if
($fields[
"PATTERN_CREATE"
] ===
"PTTRN"
)
210
{
211
$fields[
"PATTERN"
] = $fields[
"WORDS"
];
212
}
213
else
if
($fields[
"PATTERN_CREATE"
] ===
"TRNSL"
)
214
{
215
$fields[
"PATTERN"
] = Dictionary::getById($fields[
"DICTIONARY_ID"
])->translitAndCreatePattern($fields[
"WORDS"
]);
216
}
217
else
218
{
219
$fields[
"PATTERN_CREATE"
] =
"WORDS"
;
220
$fields[
"PATTERN"
] = Dictionary::getById($fields[
"DICTIONARY_ID"
])->createPattern($fields[
"WORDS"
]);
221
}
222
}
223
224
if
($result->isSuccess())
225
{
226
$result = FilterTable::update($this->
id
, $fields);
227
}
228
return
$result;
229
}
230
231
public
function
delete
()
232
{
233
return
FilterTable::delete($this->
id
);
234
}
235
236
public
function
generatePattern
()
237
{
238
$fields = [];
239
if
($this[
"PATTERN_CREATE"
] ===
"PTTRN"
)
240
{
241
$fields[
"PATTERN"
] = $this[
"WORDS"
];
242
}
243
else
if
($this[
"PATTERN_CREATE"
] ===
"TRNSL"
)
244
{
245
$fields[
"PATTERN"
] = Dictionary::getById($this[
"DICTIONARY_ID"
])->translitAndCreatePattern($this[
"WORDS"
]);
246
}
247
else
248
{
249
$fields[
"PATTERN_CREATE"
] =
"WORDS"
;
250
$fields[
"PATTERN"
] = Dictionary::getById($this[
"DICTIONARY_ID"
])->createPattern($this[
"WORDS"
]);
251
}
252
return
FilterTable::update($this->
id
, $fields);
253
}
254
255
public
static
function
add
(array $fields)
256
{
257
$result =
new
Main\Result
();
258
if
(!isset($fields[
"WORDS"
]) || $fields[
"WORDS"
] ==
''
)
259
{
260
$result->addError(
new
Main\Orm\
EntityError
(
261
Loc::getMessage
(
"FLT_ERR_DICT_PATT_MISSED"
),
262
"emptyData"
263
));
264
}
265
else
if
(intval($fields[
"DICTIONARY_ID"
]) <= 0)
266
{
267
$result->addError(
new
Main\Orm\
EntityError
(
268
Loc::getMessage
(
"FLT_ERR_DICTIONARY_MISSED"
),
269
"emptyDictionaryId"
270
));
271
}
272
else
if
(($wordEqual = FilterTable::getList([
273
"select"
=> [
"ID"
],
274
"filter"
=> [
275
"DICTIONARY_ID"
=> $fields[
"DICTIONARY_ID"
],
276
"=WORDS"
=> $fields[
"WORDS"
]
277
]
278
])->fetch()) && !empty($wordEqual))
279
{
280
$result->addError(
new
Main\Orm\
EntityError
(
281
Loc::getMessage
(
"FLT_ALREADY_EXIST"
),
282
"alreadyExists"
283
));
284
}
285
else
if
($fields[
"PATTERN_CREATE"
] ===
"PTTRN"
)
286
{
287
$fields[
"PATTERN"
] = $fields[
"WORDS"
];
288
}
289
else
if
($fields[
"PATTERN_CREATE"
] ===
"TRNSL"
)
290
{
291
$fields[
"PATTERN"
] = Dictionary::getById($fields[
"DICTIONARY_ID"
])->translitAndCreatePattern($fields[
"WORDS"
]);
292
}
293
else
294
{
295
$fields[
"PATTERN_CREATE"
] =
"WORDS"
;
296
$fields[
"PATTERN"
] = Dictionary::getById($fields[
"DICTIONARY_ID"
])->createPattern($fields[
"WORDS"
]);
297
}
298
299
if
($result->isSuccess())
300
{
301
$result = FilterTable::add($fields);
302
}
303
return
$result;
304
}
305
}
Bitrix\Forum\BadWords\Filter
Definition
filter.php:143
Bitrix\Forum\BadWords\Filter\__construct
__construct($id)
Definition
filter.php:152
Bitrix\Forum\BadWords\Filter\generatePattern
generatePattern()
Definition
filter.php:236
Bitrix\Forum\BadWords\Filter\$data
$data
Definition
filter.php:150
Bitrix\Forum\BadWords\Filter\add
static add(array $fields)
Definition
filter.php:255
Bitrix\Forum\BadWords\Filter\update
update(array $fields)
Definition
filter.php:169
Bitrix\Forum\BadWords\Filter\$id
$id
Definition
filter.php:148
Bitrix\Forum\BadWords\FilterTable
Definition
filter.php:36
Bitrix\Forum\BadWords\FilterTable\getMap
static getMap()
Definition
filter.php:47
Bitrix\Forum\BadWords\FilterTable\getDataById
static getDataById($id, $ttl=84600)
Definition
filter.php:126
Bitrix\Forum\BadWords\FilterTable\checkPattern
static checkPattern(string $pattern)
Definition
filter.php:91
Bitrix\Forum\BadWords\FilterTable\checkFields
static checkFields(Result $result, $primary, array $data)
Definition
filter.php:69
Bitrix\Forum\BadWords\FilterTable\getTableName
static getTableName()
Definition
filter.php:42
Bitrix\Forum\Forum
Definition
forum.php:321
Bitrix\Main\ArgumentException
Definition
exception.php:34
Bitrix\Main\Error
Definition
error.php:14
Bitrix\Main\Event
Definition
event.php:5
Bitrix\Main\Localization\Loc\loadMessages
static loadMessages($file)
Definition
loc.php:64
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\ORM\Data\Result
Definition
result.php:16
Bitrix\Main\ORM\Data\Result\isSuccess
isSuccess($internalCall=false)
Definition
result.php:52
Bitrix\Main\ORM\Data\UpdateResult
Definition
updateresult.php:14
Bitrix\Main\ORM\EntityError
Definition
entityerror.php:12
Bitrix\Main\ORM\Fields\BooleanField
Definition
booleanfield.php:20
Bitrix\Main\ORM\Fields\EnumField
Definition
enumfield.php:20
Bitrix\Main\ORM\Fields\IntegerField
Definition
integerfield.php:20
Bitrix\Main\ORM\Fields\StringField
Definition
stringfield.php:20
Bitrix\Main\ORM\Fields\TextField
Definition
textfield.php:20
Bitrix\Main\Result\addErrors
addErrors(array $errors)
Definition
result.php:98
Bitrix\Forum\BadWords
Definition
dictionary.php:2
Bitrix\Main
modules
forum
lib
badwords
filter.php
Создано системой
1.10.0