-
Notifications
You must be signed in to change notification settings - Fork 3
/
start.php
executable file
·215 lines (183 loc) · 5.63 KB
/
start.php
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Community spammer tools
*/
elgg_register_event_handler('init', 'system', 'community_spam_init');
/**
* Initialize the spam tools plugin
*/
function community_spam_init() {
// messages spam
if (!elgg_is_admin_logged_in()) {
elgg_register_event_handler('create', 'object', 'community_spam_messages_throttle');
}
elgg_register_event_handler('create', 'object', 'community_spam_content_filter');
elgg_register_event_handler('update', 'object', 'community_spam_content_filter');
// profile spam
elgg_register_plugin_hook_handler('action', 'profile/edit', 'community_spam_profile_blacklist');
elgg_register_action('spam_tools/bulk_user_delete', __DIR__ . '/actions/bulk_user_delete.php', 'admin');
// limit access to the add links
elgg_register_event_handler('pagesetup', 'system', 'community_spam_remove_add_links');
elgg_register_plugin_hook_handler('action', 'bookmarks/save', 'community_spam_stop_add');
elgg_register_plugin_hook_handler('action', 'pages/edit', 'community_spam_stop_add');
elgg_register_admin_menu_item('administer', 'bookmarks/audit', 'administer_utilities');
}
/**
* ban user if sending too many messages
*
* @param string $event
* @param string $type
* @param ElggObject $object
* @return bool
*/
function community_spam_messages_throttle($event, $type, $object) {
if ($object->getSubtype() !== 'messages') {
return;
}
if (community_spam_is_new_user()) {
$msg_limit = elgg_get_plugin_setting('new_user_msg_limit', 'community_spam_tools');
} else {
$msg_limit = elgg_get_plugin_setting('msg_limit', 'community_spam_tools');
}
if (!$msg_limit) {
return;
}
// two message objects created per message but after they are saved,
// both are set to private so we only have access to one later on
$msg_limit = $msg_limit + 1;
$params = array(
'type' => 'object',
'subtype' => 'messages',
'created_time_lower' => time() - (5*60), // 5 minutes
'metadata_names' => 'fromId',
'metadata_values' => elgg_get_logged_in_user_guid(),
'count' => true,
);
$num_msgs = elgg_get_entities_from_metadata($params);
if ($num_msgs > $msg_limit) {
$spammer = elgg_get_logged_in_user_entity();
$spammer->annotate('banned', 1); // this integrates with ban plugin
$spammer->ban("Sent $num_msgs in 5 minutes");
return false;
}
}
/**
* Filter profile fields by blacklist
*/
function community_spam_profile_blacklist() {
$blacklist = elgg_get_plugin_setting('profile_blacklist', 'community_spam_tools');
$blacklist = explode(",", $blacklist);
$blacklist = array_map('trim', $blacklist);
foreach ($_REQUEST as $key => $value) {
if (is_string($value)) {
foreach ($blacklist as $word) {
if (stripos($value, $word) !== false) {
ban_user(elgg_get_logged_in_user_guid(), "used '$word' on profile");
$user->automated_ban = true;
return false;
}
}
}
}
// if the email address is a phrase, block
$profile_fields = elgg_get_config('profile_fields');
foreach ($profile_fields as $name => $type) {
if ($type == 'email') {
$value = get_input($name);
if ($value && substr_count($value, ' ') > 1) {
ban_user(elgg_get_logged_in_user_guid(), "Used multiple spaces in email field.");
$user->automated_ban = true;
return false;
}
}
}
}
/**
* Is this a new user
* @return bool
*/
function community_spam_is_new_user() {
$user = elgg_get_logged_in_user_entity();
if (!$user) {
// logged out users are new users I guess
return true;
}
if ($user->__spam_tools_has_participated) {
return false;
}
// 2 days
$cutoff = time() - 2 * 24 * 60 * 60;
if ($user->getTimeCreated() > $cutoff) {
return true;
} else {
// lets see if they've participated in general discussion
$count = elgg_get_entities(array(
'type' => 'object',
'subtypes' => array('comment', 'discussion', 'discussion_reply'),
'owner_guid' => $user->guid,
'count' => true
));
if ($count > 3) {
$user->__spam_tools_has_participated = 1;
return false;
}
return true;
}
}
/**
* Remove some add links for new users
*/
function community_spam_remove_add_links() {
if (!elgg_is_logged_in()) {
return;
}
if (!community_spam_is_new_user()) {
return;
}
elgg_unregister_menu_item('extras', 'bookmark');
if (elgg_in_context('bookmarks') || elgg_in_context('pages')) {
// remove bookmarklet menu item
elgg_unregister_plugin_hook_handler('register', 'menu:page', 'bookmarks_page_menu');
// remove add buttons
$callback = function() { return array(); };
elgg_register_plugin_hook_handler('register','menu:title', $callback);
}
}
/**
* Catch users trying to hit actions directly
*/
function community_spam_stop_add() {
if (community_spam_is_new_user()) {
// spammer tried to directly hit the action
$spammer = elgg_get_logged_in_user_entity();
$spammer->annotate('banned', 1); // this integrates with ban plugin
$spammer->ban('tried to post content before allowed');
return false;
}
}
/**
* Filter content based on common spam terms and ban spammers
*/
function community_spam_content_filter($event, $type, $object) {
if (!community_spam_is_new_user()) {
return;
}
$blacklist = elgg_get_plugin_setting('profile_blacklist', 'community_spam_tools');
$blacklist = explode(",", $blacklist);
$blacklist = array_map('trim', $blacklist);
$terms = array();
foreach ($blacklist as $term) {
if (stripos($object->description, $term) !== false) {
$terms[] = $term;
}
}
if (count($terms) < 2) {
// Allow the content to be created
return;
}
$spammer = elgg_get_logged_in_user_entity();
$spammer->annotate('banned', 1); // this integrates with ban plugin
$terms = implode(', ', $terms);
$spammer->ban("Used the following blacklisted terms: $terms");
return false;
}