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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
<?php
/**
* Vegan, an anti-spam plugin for Elgg 1.8
*
* @author hellekin
* @package lorea
* @subpackage vegan
* @copyright 2012,2013 Lorea Faeries <federation@lorea.org>
* @license GNU Affero General Public License version 3 or later
* @website https://lorea.org/plugins/vegan
*
* Friendly note to spammers: never cross my path.
*/
elgg_register_event_handler('init', 'system', 'vegan_init');
/**
* vegan_init -- Initialize plugin functionality
*
* @return void
*/
function vegan_init() {
// Register Tests
// elgg_register_plugin_hook_handler('test', 'system', 'vegan_tests');
// Provide Admin Interface
elgg_register_event_handler('pagesetup', 'system', 'vegan_admin_sandbox_page_setup');
elgg_extend_view('admin/user_opt/extend', 'vegan/admin_sandboxed_users');
elgg_extend_view('css/admin', 'vegan/css_admin');
// Extend CSS
elgg_extend_view('elgg', 'css', 'vegan/css');
// @todo Replace ReportedContent functionality
// Hook to registration to block obvious spammers
elgg_register_plugin_hook_handler('register', 'user', 'vegan_blacklist_email');
// Hook to entity creation to restrict access to saved objects
elgg_register_event_handler('create', 'group', 'vegan_block_create_group');
elgg_register_event_handler('create', 'object', 'vegan_block_create_object');
// Hook to actions to block spammers from saving objects
$actions = array(
'blog/save',
'bookmark/save',
'thewire/add',
);
foreach ($actions AS $action) {
elgg_register_plugin_hook_handler('action', $action, 'vegan_block_action');
}
// Hook to creating,river to prevent river spam
elgg_register_plugin_hook_handler('creating', 'river', 'vegan_block_river');
// @todo clear vegan_sandboxed flag when user joins group/make friends
// make it smart enough to make spammers' job hard to circumvent
}
/**
* vegan_block_action -- Prevent spammers from saving anything
*
* @hook Plugin action, *save
*
* @param String $hook the action to block
* @param String $type the entity type (usually object)
* @param Mixed $foo ignored parameter
* @param Mixed $bar ignored parameter
* @return Boolean TRUE if the current user is a recognized spammer, FALSE otherwise.
*/
function vegan_block_action($hook, $type, $foo, $bar) {
$user_guid = elgg_get_logged_in_user_guid();
return vegan_is_spammer($user_guid);
}
/**
* vegan_block_create_group -- Force access to ACCESS_PRIVATE for sandboxed users
*
* @hook Event create, group
*
* @param String $event the event (create)
* @param String $type the entity type (group)
* @param Array $params contains 'entity' key
*
* @return Boolean TRUE to allow action, FALSE to prevent it
*/
function vegan_block_create_group($event, $type, $params) {
$group = $params['entity'];
$guid = $group->owner_guid;
if ($guid && vegan_is_sandboxed($guid)) {
system_message(elgg_echo('vegan:sandboxed:group'));
$group->set('access_id', ACCESS_PRIVATE);
// Return FALSE to delete group if sandboxed user is a spammer
return !vegan_is_spammer($guid);
}
return TRUE; // Accept group creation
}
/**
* vegan_block_create_object -- Force access to ACCESS_PRIVATE for sandboxed users.
*
* @hook Event create, object
*
* @param String $event the event (create)
* @param String $type the entity type (object)
* @param Array $params contains 'entity' key
*
* @return Boolean TRUE to allow action, FALSE to prevent it
*/
function vegan_block_create_object($event, $type, $params) {
$object = $params['entity'];
$guid = $object->owner_guid;
if ($guid && vegan_is_sandboxed($guid)) {
system_message(elgg_echo('vegan:sandboxed:object'));
$object->set('access_id', ACCESS_PRIVATE);
// Return FALSE to delete object if sandboxed user is a spammer
return !vegan_is_spammer($guid);
}
return TRUE; // Accept object creation
}
/**
* vegan_block_river -- Prevent river spam from sandboxed users
*
* @hook Plugin creating,river
*
* @param String $hook the plugin hook (creating)
* @param String $type the entity type (river)
* @param Mixed $foo ignored parameter
* @param Array $values arguments for view
*
* @return Mixed TRUE to prevent item from being sent to the river, void otherwise.
*/
function vegan_block_river($hook, $type, $foo, $values) {
error_log("===== vegan_block_river $hook,$type ($foo) $values");
if (vegan_is_sandboxed($values['subject_guid'])) {
return TRUE;
}
}
/**
* vegan_is_sandboxed -- Determine whether to sandbox the user or not
*
* @param Integer $user_guid the GUID of the user entity to test
*
* @return Boolean TRUE if sandboxed, FALSE otherwise.
*/
function vegan_is_sandboxed($user_guid) {
$sandboxed = FALSE;
// Get user and current time
$user = get_entity($user_guid);
$now = time();
if (!elgg_instanceof($user, 'user')) {
error_log("===== vegan_sandboxed content owner '$user_guid' is not a user!");
return FALSE;
}
// Account was activated by email
if ($user->validation_method == 'email') {
$md = elgg_get_metadata(array(
'guid' => $user_guid,
'metadata_name' => 'validated',
'limit' => 1,
));
error_log("===== vegan_sandboxed md $md->time_updated");
// and less than 2 minutes ago: die motherfucker
if ($user->last_login == 0 && $md && ($now - $md->time_updated) < 120) {
error_log("===== Gotcha! Killing spammer on first post");
$user->delete();
$sandboxed = TRUE; // return TRUE to block the river
}
// and less than an hour ago
/*
else if ($md && ($now - $md->updated_at) < 3600) {
error_log("===== vegan_sandboxed recent account");
$sandboxed = TRUE;
}
*/
}
// If it's sandboxed, force ACCESS_PRIVATE
if ($user->vegan_sandboxed) {
// Keep track of retries, for future use
error_log("===== User is sandboxed ({$owner->vegan_sandboxed_tries})");
$user->vegan_sandboxed_tries = (int)$user->vegan_sandboxed_tries + 1;
$sandboxed = TRUE;
}
// Skip admins
if (!$user->isAdmin()) {
// You have zero friends
if (!$user->getFriends()) {
error_log("===== Sandboxed user (no friends)");
$sandboxed = TRUE;
}
// You're not cooperating
if (!$user->getGroups()) {
error_log("===== Sandboxed user (no groups)");
$sandboxed = TRUE;
}
}
if ($sandboxed) {
$user->set('vegan_sandboxed', TRUE);
// @todo limit access collections to private
}
return $sandboxed;
}
/**
* vegan_blacklist_email -- Prevent user registration from a notorious
* spam source
*
* Blacklists suck, but enough is enough.
*
* @hook Plugin register, user
*
* @param String $hook the plugin hook (register)
* @param String $type the entity type (user)
* @param Mixed $value the return value
* @param Array $params contains 'user' key
*
* @return FALSE to prevent user registration
*/
function vegan_blacklist_email($hook, $type, $value, $params) {
// Hardcode forbidden patterns: humans don't use them
static $noway = array(
'/\+.*@hotmail\.com$/',
);
// Grab the email
$email = $params['user']->email;
error_log("===== vegan_blacklist_email $hook,$type $email");
if (!empty($email)) {
// Exclude forbidden patterns
foreach($noway AS $pattern) {
if (preg_match($pattern, $email)) {
error_log("===== Vegan prevented registration of $email");
return FALSE;
}
}
// Exclude additional site-specific blacklisted domains
$bad_domains = string_to_tag_array(elgg_get_plugin_setting('bad_domains', 'vegan'));
$domain = explode('@', $email, 2);
error_log("===== Vegan testing $email against " . implode(', ', $bad_domains));
if (is_array($bad_domains) && in_array($domain[1], $bad_domains)) {
error_log("===== Vegan prevented registration of $email (bad domain: $domain[1])");
return FALSE;
}
}
}
/**
* vegan_admin_sandboxed_users -- View and manage suspicious users
*
*/
/**
* vegan_admin_sandboxed_page_setup -- Provide admin interface
*
*/
function vegan_admin_sandboxed_page_setup() {
if (elgg_get_context() == 'admin' && elgg_is_admin_logged_in()) {
elgg_register_admin_menu_item('administer', 'vegan', 'lorea');
}
}
/*
function vegan_strike($hook, $type, $value, $params) {
$report = $params['report'];
$reporter = $report->getOwnerEntity();
$current_user_guid = elgg_get_loggedin_user_guid();
if ($reporter->guid == $current_user_guid && $reporter->spam_hunter) {
// Kill the beast
// TODO get reported object owner, or reported user
$spammer_guid = -1;
$spammer = elgg_get_user_entity($spammer_guid);
// Ban
if ($spammer->ban()) {
system_message(elgg_echo('vegan:killed_spammer'));
$report->state = 'archived';
$report->save();
} else {
register_error(elgg_echo('vegan:spammer_escaped'));
}
}
}
function vegan_grant_hunter_role($user_guid) {
$user = elgg_get_user($user_guid);
if (elgg_instanceof($user, 'user') && !$user->banned) {
$user->spam_hunter = true;
return TRUE;
}
return FALSE;
}
function vegan_can_hunt($user = NULL) {
if (!$user) {
$user = elgg_get_loggedin_user();
}
if (!elgg_instanceof($user, 'user') || $user->banned) {
return FALSE;
}
return ($user->spam_hunter);
}
*/
/**
* Vegan Reporting functions
*
* Use the `vegan_flag` function, it will sort things out.
* `vegan_*_spammer` function are for internal use.
*/
/*
function vegan_flag($stuff) {
// Build function name
$flag_func = "vegan_";
// Check who is reporting
$reporter = elgg_get_loggedin_user();
if (!$user) {
// Anonymous reporting
$vegan_func.= "watch";
} else if ($user->spam_hunter || $user->admin) {
// Direct kill
$vegan_func = "ban";
} else {
// Report by authenticated user
$vegan_func = "block";
}
$vegan_func.= "_spammer";
// Check what is reported
if ($stuff instanceof ElggUser) {
// A user
} else if (is_numeric($stuff)) {
// A GUID
$stuff = get_entity($stuff);
} else if (preg_match($stuff, "/^https?:\/\/[^\/]+/(?:pg\/)profile\/([^\/\?\b]+)/", $stuff, $m)) {
$stuff = get_user_by_username($m[2]);
}
if ($stuff instanceof ElggUser) {
$spammer_guid = $stuff->getGUID();
} else if ($stuff instanceof ElggEntity) {
$spammer_guid = $stuff->getOwnerGUID();
} else {
elgg_register_error(elgg_echo('vegan:error:invalid_reference'));
return FALSE;
}
return $vegan_func($spammer_guid);
}
// Report a spammer anonymously
// will raise the vegan_watch count for that entity
function vegan_snitch_spammer($user) {
$user->vegan_watch = (int)$user->vegan_watch + 1;
// notify...
system_message(elgg_echo('vegan:action:success'));
return TRUE;
}
// Block a spammer
// Reporter won't see it anymore
function vegan_block_spammer($user) {
$reporter_guid = get_loggedin_user_guid();
// Watch it
$user->vegan_watch = (int)$user->vegan_watch + 1;
// Block it
elgg_create_entity_relationship($reporter_guid, $user->guid, "blocked");
// Notify...
system_message(elgg_echo('vegan:action:success'));
return TRUE;
}
// Ban a spammer
function vegan_ban_spammer($user) {
$reporter_guid = elgg_get_loggedin_user_guid();
// Ban it
$user->ban("spammer");
// Notify
system_message(elgg_echo('vegan:action:success'));
return TRUE;
}
*/
/**
* vegan_spammer -- Determine whether a user is a spammer
*
* @param Integer $user_guid GUID of the user entity to test
*
* @return Boolean TRUE is user is a recognized spammer, FALSE otherwise.
*/
function vegan_is_spammer($user_guid) {
$user = get_entity($user_guid);
if (elgg_instanceof($user, 'user') && !$user->isBanned()) {
return (bool)$user->spammer;
}
return FALSE;
}
|