/
cleantalk-users.php
487 lines (452 loc) · 13.3 KB
/
cleantalk-users.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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
add_action('admin_menu', 'ct_add_users_menu');
function ct_add_users_menu()
{
if(current_user_can('activate_plugins'))
{
add_users_page( __("Check for spam", 'cleantalk'), __("Check for spam", 'cleantalk'), 'read', 'ct_check_users', 'ct_show_users_page');
}
}
function ct_show_users_page()
{
?>
<div class="wrap">
<h2><?php _e("Anti-spam by CleanTalk", 'cleantalk'); ?></h2><br />
<?php
$args_unchecked = array(
'meta_query' => array(
'relation' => 'AND',
Array(
'key' => 'ct_checked',
'value' => '1',
'compare' => 'NOT EXISTS'
),
Array(
'key' => 'ct_hash',
'value' => '1',
'compare' => 'NOT EXISTS'
)
)
);
$cnt_unchecked=sizeof(get_users($args_unchecked));
$args_spam = array(
'meta_query' => array(
Array(
'key' => 'ct_marked_as_spam',
'compare' => 'EXISTS'
)
)
);
$cnt_spam=sizeof(get_users($args_spam));
//if($cnt_unchecked>0)
{
?>
</div>
<?php
}
?>
<?php
//print '<button class="button" id="ct_insert_users">Insert users</button><br />';
?>
<div id="ct_working_message" style="margin:auto;padding:3px;width:70%;border:2px dotted gray;display:none;background:#ffff99;">
<?php _e("Please wait for a while. CleanTalk is checking all users via blacklist database at cleantalk.org. You will have option to delete found spam users after plugin finish.", 'cleantalk'); ?>
</div>
<div id="ct_deleting_message" style="display:none">
<?php _e("Please wait for a while. CleanTalk is deleting spam users. Users left: ", 'cleantalk'); ?> <span id="cleantalk_users_left"></span>
</div>
<div id="ct_done_message" <?php if($cnt_unchecked>0) print 'style="display:none"'; ?>>
<?php //_e("Done. All comments tested via blacklists database, please see result bellow.", 'cleantalk');
?>
</div>
<h3 id="ct_checking_users_status" style="text-align:center;width:90%;"></h3>
<?php
$args_spam = array(
'meta_query' => array(
Array(
'key' => 'ct_marked_as_spam',
'compare' => 'EXISTS'
)
)
);
$cnt_spam=sizeof(get_users($args_spam));
$page=1;
if(isset($_GET['spam_page']))
{
$page=intval($_GET['spam_page']);
}
$args_spam = array(
'meta_query' => array(
Array(
'key' => 'ct_marked_as_spam',
'value' => '1',
'compare' => 'NUMERIC'
)
),
'number'=>30,
'offset'=>($page-1)*30
);
$c_spam=get_users($args_spam);
if($cnt_spam>0)
{
?>
<table class="widefat fixed comments" id="ct_check_users_table">
<thead>
<th scope="col" id="cb" class="manage-column column-cb check-column">
<label class="screen-reader-text" for="cb-select-all-1">Select All</label>
<input id="cb-select-all-1" type="checkbox"/>
</th>
<th scope="col" id="author" class="manage-column column-slug"><?php print _e('Username');?></th>
<th scope="col" id="comment" class="manage-column column-comment"><?php print _x( 'Name', 'column name' );;?></th>
<th scope="col" id="response" class="manage-column column-comment"><?php print _x( 'E-mail', 'column name' );?></th>
<th scope="col" id="role" class="manage-column column-response sortable desc"><?php print _x( 'Role', 'column name' );?></th>
<th scope="col" id="posts" class="manage-column column-response sortable desc"><?php print _x( 'Posts', 'column name' );?></th>
</thead>
<tbody id="the-comment-list" data-wp-lists="list:comment">
<?php
for($i=0;$i<sizeof($c_spam);$i++)
{
?>
<tr id="comment-<?php print $c_spam[$i]->ID; ?>" class="comment even thread-even depth-1 approved cleantalk_user" data-id="<?php print $c_spam[$i]->ID; ?>">
<th scope="row" class="check-column">
<label class="screen-reader-text" for="cb-select-<?php print $c_spam[$i]->ID; ?>">Select user</label>
<input id="cb-select-<?php print $c_spam[$i]->ID; ?>" type="checkbox" name="del_comments[]" value="<?php print $c_spam[$i]->comment_ID; ?>"/>
</th>
<td class="author column-author" nowrap>
<strong>
<?php echo get_avatar( $c_spam[$i]->data->user_email , 32); ?>
<?php print $c_spam[$i]->data->user_login; ?>
</strong>
<br/>
<a href="mailto:<?php print $c_spam[$i]->data->user_email; ?>"><?php print $c_spam[$i]->data->user_email; ?></a> <a href="https://cleantalk.org/blacklists/<?php print $c_spam[$i]->data->user_email ; ?>" target="_blank"><img src="https://cleantalk.ru/images/icons/new_window.gif" border="0" style="float:none"/></a>
<br/>
<?php
$user_meta=get_user_meta($c_spam[$i]->ID, 'session_tokens', true);
if(is_array($user_meta))
{
$user_meta=array_values($user_meta);
}
$ip='';
if(@isset($user_meta[0]['ip']))
{
$ip=$user_meta[0]['ip'];
?>
<a href="user-edit.php?user_id=<?php print $c_spam[$i]->ID ; ?>"><?php print $ip ; ?></a>
<a href="https://cleantalk.org/blacklists/<?php print $ip ; ?>" target="_blank"><img src="https://cleantalk.ru/images/icons/new_window.gif" border="0" style="float:none"/></a>
<?php
}
?>
</td>
<td class="comment column-comment">
<div class="submitted-on">
<?php print $c_spam[$i]->data->display_name; ?>
<div style="height:16px;">
<a href="#" class="cleantalk_delete_user_button" id="cleantalk_delete_user_<?php print $c_spam[$i]->ID; ?>" data-id="<?php print $c_spam[$i]->ID; ?>" style="color:#a00;display:none;" onclick="return false;">Delete</a>
</div>
</div>
</td>
<td class="comment column-comment">
<?php print $c_spam[$i]->data->user_email; ?>
</td>
<td class="comment column-comment">
<?php
$info=get_userdata( $c_spam[$i]->ID );
print implode(', ', $info->roles);
?>
</td>
<td class="comment column-comment">
<?php
print count_user_posts($c_spam[$i]->ID);
?>
</td>
</tr>
<?php
}
$args_spam = array(
'meta_query' => array(
Array(
'key' => 'ct_marked_as_spam',
'value' => '1',
'compare' => 'NUMERIC'
)
)
);
$cnt_spam=sizeof(get_users($args_spam));
if($cnt_spam>30)
{
?>
<tr class="comment even thread-even depth-1 approved">
<td colspan="4">
<?php
$pages=ceil(intval($cnt_spam)/30);
for($i=1;$i<=$pages;$i++)
{
if($i==$page)
{
print "<a href='edit-comments.php?page=ct_check_spam&spam_page=$i'><b>$i</b></a> ";
}
else
{
print "<a href='edit-comments.php?page=ct_check_spam&spam_page=$i'>$i</a> ";
}
}
?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<button class="button" id="ct_delete_all_users"><?php _e('Delete all users from list'); ?></button>
<button class="button" id="ct_delete_checked_users"><?php _e('Delete selected', 'cleantalk'); ?></button>
<?php
}
?>
<br /><br />
<div id="ct_info_message"><?php _e("Anti-spam by CleanTalk will check all users against blacklists database and show you senders that have spam activity on other websites. Just click 'Find spam users' to start.", 'cleantalk'); ?>
<?php
if($cnt_spam>0)
{
print "<br />
There is some differencies between blacklists database and our API mechanisms. Blacklists shows all history of spam activity, but our API (that used in spam checking) used another parameters, too: last day of activity, number of spam attacks during last days etc. This mechanisms help us to reduce number of false positivitie. So, there is nothing strange, if some emails/IPs will be not found by this checking.<br /><br />";
}
?>
<button class="button" id="ct_check_users_button"><?php _e("Check for spam again", 'cleantalk'); ?></button><br /><br />
</div>
<br /><a href="options-general.php?page=cleantalk">«<?php print __('Back to CleanTalk settings', 'cleantalk'); ?></a>
<?php
}
add_action('admin_print_footer_scripts','ct_add_users_button');
function ct_add_users_button()
{
$screen = get_current_screen();
$ajax_nonce = wp_create_nonce( "ct_secret_nonce" );
?>
<script>
var ajax_nonce='<?php echo $ajax_nonce; ?>';
var spambutton_users_text='<?php _e("Find spam users", 'cleantalk'); ?>';
</script>
<?php
if( $screen->id == 'users' ){
?>
<script src="<?php print plugins_url( 'cleantalk-users-editscreen.js', __FILE__ ); ?>"></script>
<?php
}
if($screen->id == 'users_page_ct_check_users')
{
?>
<script src="<?php print plugins_url( 'cleantalk-users-checkspam.js', __FILE__ ); ?>"></script>
<?php
}
}
add_action( 'wp_ajax_ajax_check_users', 'ct_ajax_check_users' );
function ct_ajax_check_users()
{
check_ajax_referer('ct_secret_nonce', 'security');
global $ct_options;
$ct_options = ct_get_options();
$args_unchecked = array(
'meta_query' => array(
'relation' => 'AND',
Array(
'key' => 'ct_checked',
'value' => '1',
'compare' => 'NOT EXISTS'
),
Array(
'key' => 'ct_hash',
'value' => '1',
'compare' => 'NOT EXISTS'
)
),
'number'=>500
);
$u=get_users($args_unchecked);
//$u=array_slice($u,0,10);
if(sizeof($u)>0)
{
$data=Array();
for($i=0;$i<sizeof($u);$i++)
{
$user_meta=get_user_meta($u[$i]->ID, 'session_tokens', true);
if(is_array($user_meta))
{
$user_meta=array_values($user_meta);
}
if(@isset($user_meta[0]['ip']))
{
$data[]=$user_meta[0]['ip'];
}
else
{
$data[]='127.0.0.1';
}
$data[]=$u[$i]->data->user_email;
}
$data=implode(',',$data);
$request="data=$data";
$opts = array(
'http'=>array(
'method'=>"POST",
'content'=>$request,
)
);
$context = stream_context_create($opts);
$result = @file_get_contents("https://api.cleantalk.org/?method_name=spam_check&auth_key=".$ct_options['apikey'], 0, $context);
$result=json_decode($result);
if(isset($result->error_message))
{
print $result->error_message;
}
else
{
for($i=0;$i<sizeof($u);$i++)
{
update_user_meta($u[$i]->ID,'ct_checked',date("Y-m-d H:m:s"),true);
$user_meta=get_user_meta($u[$i]->ID, 'session_tokens', true);
if(is_array($user_meta))
{
$user_meta=array_values($user_meta);
}
if(@isset($user_meta[0]['ip']))
{
$uip=$user_meta[0]['ip'];
}
else
{
$uip='127.0.0.1';
}
//if($uip=='127.0.0.1')continue;
$uim=$u[$i]->data->user_email;
if(empty($uim))continue;
//print "uip: $uip, uim: $uim\n";
if($result->data->$uip->appears==1||$result->data->$uim->appears==1)
{
update_user_meta($u[$i]->ID,'ct_marked_as_spam','1',true);
}
}
print 1;
}
}
else
{
print 0;
}
die;
}
add_action( 'wp_ajax_ajax_info_users', 'ct_ajax_info_users' );
function ct_ajax_info_users()
{
check_ajax_referer( 'ct_secret_nonce', 'security' );
$cnt=sizeof(get_users());
$args_spam = array(
'meta_query' => array(
Array(
'key' => 'ct_marked_as_spam',
//'value' => '1',
'compare' => 'NUMERIC'
)
)
);
$cnt_spam=sizeof(get_users($args_spam));
$args_checked1=array(
'meta_query' => array(
Array(
'key' => 'ct_hash',
'compare' => 'EXISTS'
)
)
);
$args_checked2=array(
'meta_query' => array(
Array(
'key' => 'ct_checked',
'compare' => 'EXISTS'
)
)
);
$cnt_checked1=sizeof(get_users($args_checked1));
$cnt_checked2=sizeof(get_users($args_checked2));
$cnt_checked=$cnt_checked1+$cnt_checked2;
printf (__("Total users %s, checked %s, found %s spam users.", 'cleantalk'), $cnt, $cnt_checked, $cnt_spam);
die();
}
add_action( 'wp_ajax_ajax_insert_users', 'ct_ajax_insert_users' );
function ct_ajax_insert_users()
{
check_ajax_referer( 'ct_secret_nonce', 'security' );
$time = current_time('mysql');
for($i=0;$i<500;$i++)
{
$rnd=mt_rand(1,10000);
if($rnd<2000)
{
$email="stop_email@example.com";
}
else
{
$email="stop_email_$rnd@example.com";
}
$data = array(
'user_login'=>"user_$rnd",
'user_email'=>$email,
'user_pass'=>'123456',
);
wp_insert_user($data);
}
print "ok";
die();
}
add_action( 'wp_ajax_ajax_delete_checked_users', 'ct_ajax_delete_checked_users' );
function ct_ajax_delete_checked_users()
{
check_ajax_referer( 'ct_secret_nonce', 'security' );
foreach($_POST['ids'] as $key=>$value)
{
wp_delete_user($value);
}
die();
}
add_action( 'wp_ajax_ajax_delete_all_users', 'ct_ajax_delete_all_users' );
function ct_ajax_delete_all_users()
{
check_ajax_referer( 'ct_secret_nonce', 'security' );
$args_spam = array(
'number'=>100,
'meta_query' => array(
Array(
'key' => 'ct_marked_as_spam',
'value' => '1',
'compare' => 'NUMERIC'
)
)
);
$c_spam=get_users($args_spam);
$cnt=sizeof($c_spam);
$args_spam = array(
'meta_query' => array(
Array(
'key' => 'ct_marked_as_spam',
'value' => '1',
'compare' => 'NUMERIC'
)
)
);
$cnt_all=sizeof(get_users($args_spam));
for($i=0;$i<sizeof($c_spam);$i++)
{
wp_delete_user($c_spam[$i]->ID);
usleep(10000);
}
print $cnt_all;
die();
}
add_action( 'wp_ajax_ajax_clear_users', 'ct_ajax_clear_users' );
function ct_ajax_clear_users()
{
check_ajax_referer( 'ct_secret_nonce', 'security' );
global $wpdb;
$wpdb->query("delete from $wpdb->usermeta where meta_key='ct_hash' or meta_key='ct_checked' or meta_key='ct_marked_as_spam';");
die();
}
?>