-
Notifications
You must be signed in to change notification settings - Fork 4
/
wp-smart-honeypot.php
266 lines (251 loc) · 9.36 KB
/
wp-smart-honeypot.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
<?php
/**
* Plugin Name: WP Comment Smart Honeypot
* Plugin URI: https://github.com/freak3dot/wp-smart-honeypot
* Description: Processes the comment form to stop spam bots. Renames the normal fields on this page. Then, creates a honeypot with one of the names of the original fields. The Honeypot is removed with JavaScript.
* Version: 1.0.0
* Author: Ryan Johnston
* Author URI: http://www.newsunflowerchurch.org
*
* @package wp-smart-honeypot
*/
new wpCommentSmartHoneyPotPlugin();
/**
* Wordpress Comment Smart Honeypot Plugin.
*
* @package wp-smart-honeypot
*/
class wpCommentSmartHoneyPotPlugin {
/** @var int Location index of the inserted honeypot. */
protected $insertAt;
/** @var array List of field names. */
protected $label_list = array( 'Name', 'Email', 'Website', 'Comment' );
/** @var array List of bootstrap icons corresponding to the above field names. */
protected $icon_list = array( 'user', 'envelope', 'home', 'comment' );
/** @var int Honeypot Label id. */
protected $label;
/** @var string Random string for obfuscation of field names. */
protected $addOn;
/** @var string Salt for obfuscation of field names.
* Change the salt when you install this.
* http://www.sethcardoza.com/tools/random-password-generator
*/
protected $salt = '(xPj(77ios0V5iikTZ9W!K1NQ)0aLexnLuKGNam1am7$(pO74KFf&22@m2rRgze&';
/** @var string Regex to find name html property. */
protected $nameRegex = '/name="[a-z0-9]*"/i';
/** @var string Regex to find id html property. */
protected $idRegex = '/id="[a-z0-9]*"/i';
/** @var string Approved Comment. */
protected $approvedComment = '';
/**
* Construct the class.
*
* Set up the wordpress filters that will use this plugin.
*
* @since 1.0.0
*/
function __construct() {
add_filter( 'comment_form_before_fields', array( &$this, 'wpcsh_init' ) );
add_filter( 'comment_form_field_author', array( &$this, 'wpcsh_encrypt_author' ) );
add_filter( 'comment_form_field_email', array( &$this, 'wpcsh_encrypt_email' ) );
add_filter( 'comment_form_field_url', array( &$this, 'wpcsh_encrypt_url' ) );
add_filter( 'comment_form_field_comment', array( &$this, 'wpcsh_encrypt_comment' ) );
add_action( 'pre_comment_approved', array( &$this, 'wpcsh_check_form' ) );
add_action( 'pre_comment_on_post', array( &$this, 'wpcsh_pre_post' ) );
add_action( 'comment_form', array( &$this, 'wpcsh_hidden_field_and_script' ) );
}
/**
* Initialize WP Comment Smart Honeypot Plugin.
*
* @since 1.0.0
*/
function wpcsh_init() {
$this->insertAt = rand( )&3;
$this->label = rand( )&3;
$this->addOn = substr( sha1( time( ) . $this->salt ), 0, 6 );
}
/**
* Insert the Honeypot Field
*
* Copy one of the existing comment fields and return this wrapped in a
* bootstrap control-group.
*
* @since 1.0.0
*
* @return string Honeypot Field as HTML
*/
function wpcsh_insert_honey_pot() {
$hp_field = '<div class="control-group"><label for="' . strtolower( $this->label_list[ $this->label ] ) . '">' .
$this->label_list[ $this->label ] . '</label><div class="input-prepend"><span class="add-on"><i class="icon-' .
$this->icon_list[ $this->label ] . '"></i></span><input type="text" name="' . strtolower( $this->label_list[ $this->label ] ) .
'" id="' . strtolower( $this->label_list[ $this->label ] ) . '" value="" placeholder="' .
$this->label_list[ $this->label ] . '" aria-required="true"></div></div>';
return $hp_field;
}
/**
* Generate a unique md5 obfuscated field name.
*
* Add the field name to the $addOn and encode it in md5. If $addOn is not
* provided, then the default $addOn for this class will be used.
*
* @since 1.0.0
*
* @param string $unique Original Field Name.
* @param string $addOn Add on, like a salt, so the md5 is more difficult to undo.
*
* @return string Honeypot Field as HTML
*/
private function wpcsh_make_field($unique, $addOn = false) {
if ( false === $addOn ) { $addOn = $this->addOn; }
return md5( $unique . $addOn );
}
/**
* Replace name and id HTML attributes with unique md5 obfuscated field name.
*
* Add the field name to the $addOn and encode it in md5. If $addOn is not
* provided, then the default $addOn for this class will be used.
*
* @since 1.0.0
*
* @param string $unique Original Field Name.
* @param string $field HTML for the field.
*
* @return string HTML for the field.
*/
private function wpcsh_replace_name_id($unique, $field) {
$field_name = $this->wpcsh_make_field( $unique );
$field = preg_replace( $this->nameRegex, 'name="' . $field_name . '"', $field );
$field = preg_replace( $this->idRegex, 'id="' . $field_name . '"', $field );
return $field;
}
/**
* Obfuscate the author field.
*
* Replaces name and id HTML attributes in the author field with unique md5 obfuscated field name.
* Also inserts the honeypot if the random location selected is at 0. 0 would be before the author field.
* This is used by a wordpress filter.
*
* @since 1.0.0
*
* @param string $author_field HTML for the author field.
*
* @return string HTML for the field.
*/
function wpcsh_encrypt_author($author_field) {
$author_field = $this->wpcsh_replace_name_id( 'author', $author_field );
if ( $this->insertAt === 0 ) { $author_field = $this->wpcsh_insert_honey_pot() . $author_field; }
return $author_field;
}
/**
* Obfuscate the email field.
*
* Replaces name and id HTML attributes in the email field with unique md5 obfuscated field name.
* Also inserts the honeypot if the random location selected is at 1. 1 would be before the email field.
* This is used by a wordpress filter.
*
* @since 1.0.0
*
* @param string $email_field HTML for the email field.
*
* @return string HTML for the field.
*/
function wpcsh_encrypt_email($email_field) {
$email_field = $this->wpcsh_replace_name_id( 'email', $email_field );
if ( $this->insertAt === 1 ) { $email_field = $this->wpcsh_insert_honey_pot() . $email_field; }
return $email_field;
}
/**
* Obfuscate the url field.
*
* Replaces name and id HTML attributes in the url field with unique md5 obfuscated field name.
* Also inserts the honeypot if the random location selected is at 2. 2 would be before the url field.
* This is used by a wordpress filter.
*
* @since 1.0.0
*
* @param string $url_field HTML for the url field.
*
* @return string HTML for the field.
*/
function wpcsh_encrypt_url($url_field) {
$url_field = $this->wpcsh_replace_name_id( 'url', $url_field );
if ( $this->insertAt === 2 ) { $url_field = $this->wpcsh_insert_honey_pot() . $url_field; }
return $url_field;
}
/**
* Obfuscate the comment field.
*
* Replaces name and id HTML attributes in the comment field with unique md5 obfuscated field name.
* Also inserts the honeypot if the random location selected is at 3. 3 would be before the comment field.
* This is used by a wordpress filter.
*
* @since 1.0.0
*
* @param string $comment_field HTML for the comment field.
*
* @return string HTML for the field.
*/
function wpcsh_encrypt_comment($comment_field) {
$comment_field = $this->wpcsh_replace_name_id( 'comment', $comment_field );
if ( $this->insertAt === 3 ) { $comment_field = $this->wpcsh_insert_honey_pot() . $comment_field; }
return $comment_field;
}
/**
* Pre Comment On Post
*
* Undo the obfuscate that was done on the comment form. This will enable wordpress to understand the form again.
* This method also checks the honeypot for content and will mark the comment form as spam.
* This is used by a wordpress action.
*
* @since 1.0.0
*/
function wpcsh_pre_post() {
$output = base64_decode( $_POST['enc-type'] );
$output = rtrim( $output, '' );
$addOn = substr( $output, 0, -1 );
if ( ! empty( $_POST[ strtolower( $this->label_list[ substr( $output, -1, 1 ) ] ) ] ) ) {
$this->approved = 'spam';
}
$author = $this->wpcsh_make_field( 'author', $addOn );
$_POST['author'] = $_POST[ $author ];
$email = $this->wpcsh_make_field( 'email', $addOn );
$_POST['email'] = $_POST[ $email ];
$url = $this->wpcsh_make_field( 'url', $addOn );
$_POST['url'] = $_POST[ $url ];
$comment = $this->wpcsh_make_field( 'comment', $addOn );
$_POST['comment'] = $_POST[ $comment ];
}
/**
* Comment Approved.
*
* Determine if the comment form was flagged as spam by the wpcsh_pre_post method.
* If the comment form is not honeypot spam, return the original status.
* The original status was presumably approved.
* This is used by a wordpress action.
*
* @since 1.0.0
*
* @param string $approved Form Status.
*
* @return string Form Status.
*/
function wpcsh_check_form($approved) {
if ( $this->approved === 'spam' ) { return 'spam'; }
return $approved;
}
/**
* Comment Form Creation.
*
* Inject the hidden field for our $addOn which will be used to undo the
* obfuscation of field names. Also, add the intentionally raw JavaScript
* that will remove the honeypot for our human users.
* This is used by a wordpress action.
*
* @since 1.0.0
*/
function wpcsh_hidden_field_and_script() {
$output = base64_encode( $this->addOn . $this->label );
echo '<input type="hidden" name="enc-type" value="' . esc_attr( $output ) . '"/>';
echo '<script type="text/JavaScript">nscrmhp = document.getElementById("' . esc_js( strtolower( $this->label_list[ $this->label ] ) ) . '"); nscrmhpp = nscrmhp.parentNode; nscrmhppp = nscrmhpp.parentNode; nscrmhppp.parentNode.removeChild(nscrmhppp);</script>';
}
}