forked from wp-plugins/cleantalk-spam-protect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleantalk-comments.php
454 lines (420 loc) · 13.4 KB
/
cleantalk-comments.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
<?php
add_action('admin_menu', 'ct_add_comments_menu');
function ct_add_comments_menu()
{
if(current_user_can('activate_plugins'))
{
add_comments_page( __("Check for spam", 'cleantalk'), __("Check for spam", 'cleantalk'), 'read', 'ct_check_spam', 'ct_show_checkspam_page');
}
}
function ct_show_checkspam_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'
)
),
'count'=>true
);
$cnt_unchecked=get_comments($args_unchecked);
//if($cnt_unchecked>0)
{
?>
<button class="button" id="ct_check_spam_button"><?php _e("Find spam comments", 'cleantalk'); ?></button><br /><br />
<div id="ct_info_message"><?php _e("Anti-spam by CleanTalk will check all not spam comments against blacklists database and show you senders that have spam activity on other websites. Just click 'Find spam comments' to start.", 'cleantalk'); ?></div>
<?php
}
?>
<?php
if($_SERVER['REMOTE_ADDR']=='127.0.0.1')print '<button class="button" id="ct_insert_comments">Insert comments</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 approved and pending comments via blacklist database at cleantalk.org. You will have option to delete found spam comments after plugin finish.", 'cleantalk'); ?>
</div>
<div id="ct_deleting_message" style="display:none;">
<?php _e("Please wait for a while. CleanTalk is deleting spam comments. Comments left: ", 'cleantalk'); ?> <span id="cleantalk_comments_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_status" style="text-align:center;width:90%;"></h3>
<?php
$args_spam = array(
'meta_query' => array(
Array(
'key' => 'ct_marked_as_spam',
'compare' => 'EXISTS'
)
),
'count'=>true
);
$cnt_spam=get_comments($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_comments($args_spam);
if($cnt_spam>0)
{
?>
<table class="widefat fixed comments" id="ct_check_comments_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('Author');?></th>
<th scope="col" id="comment" class="manage-column column-comment"><?php print _x( 'Comment', 'column name' );;?></th>
<th scope="col" id="response" class="manage-column column-response sortable desc"><?php print _x( 'In Response To', '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]->comment_ID; ?>" class="comment even thread-even depth-1 approved">
<th scope="row" class="check-column">
<label class="screen-reader-text" for="cb-select-<?php print $c_spam[$i]->comment_ID; ?>">Select comment</label>
<input id="cb-select-<?php print $c_spam[$i]->comment_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]->comment_author_email , 32); ?>
<?php print $c_spam[$i]->comment_author; ?>
</strong>
<br/>
<a href="mailto:<?php print $c_spam[$i]->comment_author_email; ?>"><?php print $c_spam[$i]->comment_author_email; ?></a> <a href="https://cleantalk.org/blacklists/<?php print $c_spam[$i]->comment_author_email ; ?>" target="_blank"><img src="https://cleantalk.ru/images/icons/new_window.gif" border="0" style="float:none"/></a>
<br/>
<a href="edit-comments.php?s=<?php print $c_spam[$i]->comment_author_IP ; ?>&mode=detail"><?php print $c_spam[$i]->comment_author_IP ; ?></a>
<a href="https://cleantalk.org/blacklists/<?php print $c_spam[$i]->comment_author_IP ; ?>" target="_blank"><img src="https://cleantalk.ru/images/icons/new_window.gif" border="0" style="float:none"/></a>
</td>
<td class="comment column-comment">
<div class="submitted-on">
<?php printf( __( 'Submitted on <a href="%1$s">%2$s at %3$s</a>' ), get_comment_link($c_spam[$i]->comment_ID),
/* translators: comment date format. See http://php.net/date */
get_comment_date( __( 'Y/m/d' ),$c_spam[$i]->comment_ID ),
get_comment_date( get_option( 'time_format' ),$c_spam[$i]->comment_ID )
);
?>
</div>
<p>
<?php print $c_spam[$i]->comment_content; ?>
</p>
</td>
<td class="response column-response">
<div class="response-links">
<span class="post-com-count-wrapper">
<a href="http://ct_wp/wp-admin/post.php?post=<?php print $c_spam[$i]->comment_post_ID; ?>&action=edit"><?php print get_the_title($c_spam[$i]->comment_post_ID); ?></a>
<br/>
<a href="http://ct_wp/wp-admin/edit-comments.php?p=<?php print $c_spam[$i]->comment_post_ID; ?>" class="post-com-count">
<span class="comment-count"><?php
$p_cnt=wp_count_comments();
print $p_cnt->total_comments;
?></span>
</a>
</span>
<a href="<?php print get_permalink($c_spam[$i]->comment_post_ID); ?>"><?php print _e('View Post');?></a>
</div>
</td>
</tr>
<?php
}
$args_spam = array(
'meta_query' => array(
Array(
'key' => 'ct_marked_as_spam',
'value' => '1',
'compare' => 'NUMERIC'
)
),
'count'=>true
);
$cnt_spam=get_comments($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"><?php _e('Delete all content'); ?></button>
<button class="button" id="ct_delete_checked"><?php _e('Delete selected', 'cleantalk'); ?></button>
<?php
}
?>
</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_checkspam_button');
function ct_add_checkspam_button()
{
$screen = get_current_screen();
$ajax_nonce = wp_create_nonce( "ct_secret_nonce" );
?>
<script>
var ajax_nonce='<?php echo $ajax_nonce; ?>';
var spambutton_text='<?php _e("Find spam comments", 'cleantalk'); ?>';
</script>
<?php
if( $screen->id == 'edit-comments' ){
?>
<script src="<?php print plugins_url( 'cleantalk-comments-editscreen.js', __FILE__ ); ?>"></script>
<?php
}
if($screen->id == 'comments_page_ct_check_spam')
{
?>
<script src="<?php print plugins_url( 'cleantalk-comments-checkspam.js', __FILE__ ); ?>"></script>
<?php
}
}
add_action( 'wp_ajax_ajax_check_comments', 'ct_ajax_check_comments' );
function ct_ajax_check_comments()
{
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,
'status' => 'all'
);
$u=get_comments($args_unchecked);
$u=array_slice($u,0,500);
if(sizeof($u)>0)
{
//print_r($u);
//die();
$data=Array();
for($i=0;$i<sizeof($u);$i++)
{
$data[]=$u[$i]->comment_author_IP;
$data[]=$u[$i]->comment_author_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++)
{
add_comment_meta($u[$i]->comment_ID,'ct_checked',date("Y-m-d H:m:s"),true);
$uip=$u[$i]->comment_author_IP;
if(empty($uip))continue;
$uim=$u[$i]->comment_author_email;
if(empty($uim))continue;
if($result->data->$uip->appears==1||$result->data->$uim->appears==1)
{
add_comment_meta($u[$i]->comment_ID,'ct_marked_as_spam','1',true);
}
}
print 1;
}
}
else
{
print 0;
}
die;
}
add_action( 'wp_ajax_ajax_info_comments', 'ct_ajax_info_comments' );
function ct_ajax_info_comments()
{
check_ajax_referer( 'ct_secret_nonce', 'security' );
$cnt=get_comments(Array('count'=>true));
$args_spam = array(
'meta_query' => array(
Array(
'key' => 'ct_marked_as_spam',
'value' => '1',
'compare' => 'NUMERIC'
)
),
'count'=>true
);
$cnt_spam=get_comments($args_spam);
$args_checked1=array(
'meta_query' => array(
Array(
'key' => 'ct_hash',
'value'=>'1',
'compare' => 'EXISTS'
)
),
'count'=>true
);
$args_checked2=array(
'meta_query' => array(
Array(
'key' => 'ct_checked',
'value'=>'1',
'compare' => 'EXISTS'
)
),
'count'=>true
);
$cnt_checked1=get_comments($args_checked1);
$cnt_checked2=get_comments($args_checked2);
$cnt_checked=$cnt_checked1+$cnt_checked2;
printf (__("Total comments %s, checked %s, found %s spam comments.", 'cleantalk'), $cnt, $cnt_checked, $cnt_spam);
die();
}
add_action( 'wp_ajax_ajax_insert_comments', 'ct_ajax_insert_comments' );
function ct_ajax_insert_comments()
{
check_ajax_referer( 'ct_secret_nonce', 'security' );
$time = current_time('mysql');
for($i=0;$i<500;$i++)
{
$rnd=mt_rand(1,100);
if($rnd<20)
{
$email="stop_email@example.com";
}
else
{
$email="stop_email_$rnd@example.com";
}
$data = array(
'comment_post_ID' => 1,
'comment_author' => "author_$rnd",
'comment_author_email' => $email,
'comment_author_url' => 'http://',
'comment_content' => "comment content ".mt_rand(1,10000)." ".mt_rand(1,10000)." ".mt_rand(1,10000),
'comment_type' => '',
'comment_parent' => 0,
'user_id' => 1,
'comment_author_IP' => '127.0.0.1',
'comment_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)',
'comment_date' => $time,
'comment_approved' => 1,
);
wp_insert_comment($data);
}
print "ok";
die();
}
add_action( 'wp_ajax_ajax_delete_checked', 'ct_ajax_delete_checked' );
function ct_ajax_delete_checked()
{
check_ajax_referer( 'ct_secret_nonce', 'security' );
foreach($_POST['ids'] as $key=>$value)
{
wp_delete_comment($value, false);
}
die();
}
add_action( 'wp_ajax_ajax_delete_all', 'ct_ajax_delete_all' );
function ct_ajax_delete_all()
{
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_comments($args_spam);
$cnt=sizeof($c_spam);
$args_spam = array(
'count'=>true,
'meta_query' => array(
Array(
'key' => 'ct_marked_as_spam',
'value' => '1',
'compare' => 'NUMERIC'
)
)
);
$cnt_all=get_comments($args_spam);
for($i=0;$i<sizeof($c_spam);$i++)
{
wp_delete_comment($c_spam[$i]->comment_ID, false);
usleep(10000);
}
print $cnt_all;
die();
}
add_action( 'wp_ajax_ajax_clear_comments', 'ct_ajax_clear_comments' );
function ct_ajax_clear_comments()
{
check_ajax_referer( 'ct_secret_nonce', 'security' );
global $wpdb;
$wpdb->query("delete from $wpdb->commentmeta where meta_key='ct_hash' or meta_key='ct_checked' or meta_key='ct_marked_as_spam';");
die();
}
?>