This repository was archived by the owner on Jan 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblogspam.php
323 lines (264 loc) · 8.35 KB
/
blogspam.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
<?php
/*
Plugin Name: Blogspam
Plugin URI: http://blogspam.net/plugins/
Description: This plugin allows you to test submitted comments against a centralised service that will filter out a lot of junk. No manual intervention required.
Author: Steve Kemp
Version: 2.6
Author URI: http://www.steve.org.uk/
*/
/* Copyright 2008-2014 Steve Kemp (email : http://www.steve.org.uk/contact/ )
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//
// Find the version number of our plugin.
//
function skx_self_version()
{
if ( !function_exists( 'get_plugins' ) )
{
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
$plugins = get_plugins();
$version = "v0.0";
foreach ( $plugins as $p )
{
if ( $p['Name'] == "Blogspam" )
{
$version = $p['Version'];
}
}
return $version;
}
//
// Check the content of a comment, by submitting the body
// via HTTP to the JSON server.
//
function skx_check_comment( $author, $email,
$url, $comment,
$user_ip, $user_agent)
{
//
// Get the server name, and any options.
//
$server_name = get_option('skx_blogspam_server');
if ( !$server_name ) { $server_name = "http://test.blogspam.net:9999/"; }
$server_options = get_option('skx_blogspam_options');
if ( !$server_options ) { $server_options = ""; }
//
// Try to ensure the comment is valid UTF-8, which is mandatory
// for the JSON extension module.
//
$updated = iconv('UTF-8', 'UTF-8//IGNORE', $comment);
//
// iconv returns false on failure, test for that here.
//
if ( $updated )
{
$comment = $updated;
}
//
// Make the structure we'll send.
//
// This corresponds to:
//
// http://blogspam.net/api/2.0/testComment.html
//
$struct = array(
'ip' => $_SERVER['REMOTE_ADDR'],
'name' => $author,
'mail' => $email,
'comment' => $comment,
'site' => get_bloginfo('url'),
'options' => $server_options,
'version' => "Blogspam.php " . skx_self_version() . " on wordpress " . get_bloginfo('version')
);
//
// Send the JSON result.
//
$result = wp_remote_post( $server_name, array( 'body' => json_encode( $struct ) ) );
if ( ! is_wp_error( $result ) )
{
$obj = json_decode( $result['body'], true );
if ( $obj['result'] == "SPAM" )
{
//
// Mark the result as spam.
//
add_filter('pre_comment_approved',
create_function('$a', 'return \'spam\';'), 99);
return 1;
}
}
return 0;
}
//
// Add a new action page underneath "settings".
//
function skx_add_pages()
{
add_options_page('Blogspam', 'Blogspam', 8, 'blogspam', 'skx_options_page');
}
//
// This is called when the admin page is loaded.
//
function skx_options_page()
{
//
// Get the configured value from the db.
//
$server_name = get_option('skx_blogspam_server');
$server_options = get_option('skx_blogspam_options');
//
// If not set then use sane defaults.
//
if ( !$server_name )
{
$server_name = "http://test.blogspam.net:9999/" ;
}
if ( !$server_options )
{
$server_options = "";
}
//
// See if the user has submitted a test-request.
//
if( $_POST[ 'test' ] == 'Test' )
{
$plugins_url = get_option( 'skx_blogspam_server' );
if ( !$plugins_url ) {
$plugins_url = "http://test.blogspam.net:9999/plugins" ;
} else {
$plugins_url = $plugins_url . "plugins";
}
$res = wp_remote_get( $plugins_url );
if ( is_wp_error( $res ) )
{
echo "<div class=\"updated\"><p><strong>Test failed :" . $res->get_error_message(). "</strong></p></div>"; }
else
{
$obj = json_decode( $res['body'], true );
if ( $obj && is_array($obj) ) {
echo "<div class=\"updated\"><p><strong>Test SUCCEEDED</strong></p></div>";
}
else {
echo "<div class=\"updated\"><p><strong>Test FAILED</strong></p></div>"; }
}
}
//
// See if the user has submitted updated values.
//
if( $_POST[ 'submit' ] == 'Submit' )
{
// Read the submitted valies.
$server_name = $_POST[ 'server_name' ];
$server_options = $_POST[ 'server_options' ];
// Save the posted value in the database
update_option( 'skx_blogspam_server', $server_name );
update_option( 'skx_blogspam_options', $server_options );
// Put an options updated message on the screen
?>
<div class="updated"><p><strong><?php _e('Options saved.', 'mt_trans_domain' ); ?></strong></p></div>
<?php
}
// Now display the options editing screen
echo '<div class="wrap">';
// header
$version = skx_self_version();
echo "<h2>Blogspam v$version Options </h2>";
?>
<p>The blogspam plugin will pass all submitted comments through a remote service which will test comments.</p><p>Here you can specify the URL of that testing service, along with some optional configuration settings.</p>
<form name="form1" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
<p>Server:
<input type="text" name="server_name" value="<?php echo $server_name; ?>" size="40">
</p>
<p><a href="http://blogspam.net/api/2.0/testComment.html#options">Server Options</a>:
<input type="text" name="server_options" value="<?php echo $server_options; ?>" size="40">
</p><hr />
<p class="submit">
<input type="submit" name="submit" value="Submit" />
<input type="submit" name="test" value="Test" />
</p>
</form>
</div>
<?php
//
// Stats
//
$stats_url = get_option( 'skx_blogspam_server' );
$stats_data = array( 'site' => get_bloginfo('url') );
if ( !$stats_url ) {
$stats_url = "http://test.blogspam.net:9999/stats" ;
} else {
$stats_url = $stats_url . "stats";
}
$stats = wp_remote_post( $stats_url , array( 'body' => json_encode( $stats_data ) ) );
if ( ! is_wp_error( $stats ) )
{
$obj = json_decode( $stats['body'], true );
if ( $obj && array_key_exists( "spam", $obj ) ) {
echo "<p>Dropped " . $obj['spam'] . " comment(s).</p>";
}
if ( $obj && array_key_exists( "ok", $obj ) ) {
echo "<p>Permitted " . $obj['ok'] . " comment(s)</p>";
}
}
echo "<iframe height=\"800\" width=\"90%\" style=\"text-align:center;border:1px solid black\" src=\"http://blogspam.net/news.html\"></iframe>";
}
//
// This function is called when a comment is submitted as
// spam, by the wordpress admin.
//
// We fire off a JSON request to blacklist the IP.
//
function skx_train_comment( $id, $status )
{
if ( $status == "spam" )
{
//
// Get the comment data.
//
$data = get_comment( $id, ARRAY_A );
$ip = $data['comment_author_IP'];
//
// The data we'll train with.
//
$json_data = array( 'ip' => $ip,
'site' => get_bloginfo('url'),
'train' => 'spam' );
//
// Post to the URL
//
$train_url = get_option( 'skx_blogspam_server' );
if ( !$train_url ) {
$train_url = "http://test.blogspam.net:9999/classify" ;
} else {
$train_url = $train_url . "classify";
}
$stats = wp_remote_post( $train_url , array( 'body' => json_encode( $json_data ) ) );
if ( ! is_wp_error( $stats ) )
{
}
else
{
die( $stats->get_error_message() );
}
}
}
//
// Add the hooks
//
add_action( 'wp_blacklist_check', 'skx_check_comment', 10, 6);
add_action( 'wp_set_comment_status', 'skx_train_comment', 10, 2 );
add_action( 'admin_menu', 'skx_add_pages' );
?>