-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathController.php
More file actions
72 lines (54 loc) · 1.79 KB
/
Copy pathController.php
File metadata and controls
72 lines (54 loc) · 1.79 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
<?php
namespace SpamDetector;
use MapasCulturais\App;
use MapasCulturais\i;
use MapasCulturais\Controller as SpamDetectorController;
class Controller extends SpamDetectorController
{
function __construct() {}
public function POST_saveterms()
{
$app = App::i();
$this->requireAuthentication();
if (!$app->user->is("admin")) {
$app->pass();
}
$notification = $this->sanitizeTerms($this->data['notification'] ?? null);
$blocked = $this->sanitizeTerms($this->data['blocked'] ?? null);
if (null === $notification || null === $blocked) {
$this->json(['error' => i::__('invalid payload: "notification" and "blocked" must be arrays')], 400);
}
$data = [
'notification' => $notification,
'blocked' => $blocked,
];
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
if (!Plugin::writeFileTerms($json)) {
$this->json(['error' => i::__('unable to persist the terms file')], 500);
}
$this->json($data);
}
/**
* Sanitizes a list of spam terms: strings only, trimmed, stripped of tags,
* empty entries dropped, duplicates removed, array reindexed.
* Returns null when the input is not an array (invalid payload).
*/
protected function sanitizeTerms($terms): ?array
{
if (!is_array($terms)) {
return null;
}
$clean = [];
foreach ($terms as $term) {
if (!is_string($term)) {
continue;
}
$term = trim(strip_tags($term));
if ('' === $term) {
continue;
}
$clean[] = $term;
}
return array_values(array_unique($clean));
}
}