PluginProbe
Captcha Code / 2.8
Captcha Code v2.8
trunk 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.31 3.32
captcha-code-authentication / wpCaptcha.php

wpCaptcha.php in Captcha Code 2.8, at wpCaptcha.php

434 lines 15.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Captcha Code
4 Description: Adds captcha to front-end forms.
5 Version: 2.8
6 Author: WebFactory Ltd
7 Author URI: http://www.webfactoryltd.com/
8 Text Domain: captcha-code-authentication
9 Domain Path: /languages
10 License: GPL2
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License, version 2, as
14 published by the Free Software Foundation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 define('WP_CAPTCHA_DIR_URL', plugin_dir_url(__FILE__));
27 define('WP_CAPTCHA_DIR', dirname(__FILE__));
28
29 require 'general_options.php';
30
31 /* Hook to initalize the admin menu */
32 add_action('admin_menu', 'wp_captcha_admin_menu');
33 /* Hook to initialize sessions */
34 add_action('init', 'wp_captcha_init_sessions');
35
36 /* Hook to store the plugin status */
37 register_activation_hook(__FILE__, 'wp_captcha_enabled');
38 register_deactivation_hook(__FILE__, 'wp_captcha_disabled');
39
40 function wp_captcha_enabled()
41 {
42 update_option('wpcaptcha_status', 'enabled');
43 }
44 function wp_captcha_disabled()
45 {
46 update_option('wpcaptcha_status', 'disabled');
47 }
48
49 /* To add the menus in the admin section */
50 function wp_captcha_admin_menu()
51 {
52 add_options_page(
53 __('Captcha Settings'),
54 __('Captcha Settings'),
55 'manage_options',
56 'wp_captcha_slug',
57 'wp_captcha_general_options');
58 }
59
60 function wp_captcha_init_sessions()
61 {
62 if(!session_id()){
63 session_start();
64 }
65 load_plugin_textdomain('captcha-code-authentication', false, dirname( plugin_basename(__FILE__)).'/languages');
66 }
67
68 /* Captcha for login authentication starts here */
69
70 $login_captcha = get_option('wpcaptcha_login');
71 if($login_captcha == 'yes'){
72 add_action('login_form', 'include_ctl_captcha_for_login');
73 add_filter( 'login_errors', 'include_ctl_captcha_login_errors' );
74 add_filter( 'login_redirect', 'include_ctl_captcha_login_redirect', 10, 3 );
75 }
76
77 /* Function to include captcha for login form */
78 function include_ctl_captcha_for_login()
79 {
80 echo '<p class="login-form-captcha">
81 <label><b>'. __('Captcha', 'captcha-code-authentication').' </b> <span class="required">*</span></label>
82 <div style="clear:both;"></div><div style="clear:both;"></div>';
83 ctl_captcha_generate_code();
84
85 /* Will retrieve the get varibale and prints a message from url if the captcha is wrong */
86 if(isset($_GET['captcha']) && $_GET['captcha'] == 'confirm_error' ) {
87 echo '<label style="color:#FF0000;" id="capt_err">'.esc_html($_SESSION['captcha_error']).'</label><div style="clear:both;"></div>';;
88 $_SESSION['captcha_error'] = '';
89 }
90
91 echo '<label>'.__('Type the text displayed above', 'captcha-code-authentication').':</label>
92 <input id="captcha_code" name="captcha_code" size="15" type="text" tabindex="30" />
93 </p>';
94 return true;
95 }
96
97 /* Hook to find out the errors while logging in */
98 function include_ctl_captcha_login_errors($errors)
99 {
100 if( isset( $_REQUEST['action'] ) && 'register' == $_REQUEST['action'] )
101 return($errors);
102
103 if(esc_html($_SESSION['captcha_code']) != $_REQUEST['captcha_code']){
104 return $errors.'<label id="capt_err" for="captcha_code_error">'.__('Captcha confirmation error!', 'captcha-code-authentication').'</label>';
105 }
106 return $errors;
107 }
108
109 /* Hook to redirect after captcha confirmation */
110 function include_ctl_captcha_login_redirect($url)
111 {
112
113 /* Captcha mismatch */
114 if(isset($_SESSION['captcha_code']) && isset($_REQUEST['captcha_code']) && esc_html($_SESSION['captcha_code']) != $_REQUEST['captcha_code']){
115 $_SESSION['captcha_error'] = __('Incorrect captcha confirmation!', 'captcha-code-authentication');
116 wp_clear_auth_cookie();
117 return $_SERVER["REQUEST_URI"]."/?captcha='confirm_error'";
118 }
119 /* Captcha match: take to the admin panel */
120 else{
121 return home_url('/wp-admin/');
122 }
123 }
124
125 /* <!-- Captcha for login authentication ends here --> */
126
127 /* Captcha for Comments ends here */
128 $comment_captcha = get_option('wpcaptcha_comments');
129 if($comment_captcha == 'yes')
130 {
131 global $wp_version;
132 if( version_compare($wp_version,'3','>=') ) { // wp 3.0 +
133 add_action( 'comment_form_after_fields', 'include_ctl_captcha_comment_form_wp3', 1 );
134 add_action( 'comment_form_logged_in_after', 'include_ctl_captcha_comment_form_wp3', 1 );
135 }
136 // for WP before WP 3.0
137 add_action( 'comment_form', 'include_ctl_captcha_comment_form' );
138 add_filter( 'preprocess_comment', 'include_ctl_captcha_comment_post' );
139 }
140
141 /* Function to include captcha for comments form */
142 function include_ctl_captcha_comment_form()
143 {
144 $c_registered = get_option('wpcaptcha_registered');
145 if ( is_user_logged_in() && $c_registered == 'yes') {
146 return true;
147 }
148 echo '<p class="comment-form-captcha">
149 <label><b>'. __('Captcha', 'captcha-code-authentication').' </b><span class="required">*</span></label>
150 <div style="clear:both;"></div><div style="clear:both;"></div>';
151 ctl_captcha_generate_code();
152 echo '<label>'.__('Type the text displayed above', 'captcha-code-authentication').':</label>
153 <input id="captcha_code" name="captcha_code" size="15" type="text" />
154 <div style="clear:both;"></div>
155 </p>';
156 return true;
157 }
158
159 /* Function to include captcha for comments form > wp3 */
160 function include_ctl_captcha_comment_form_wp3()
161 {
162 $c_registered = get_option('wpcaptcha_registered');
163 if ( is_user_logged_in() && $c_registered == 'yes') {
164 return true;
165 }
166
167 echo '<p class="comment-form-captcha">
168 <label><b>'. __('Captcha', 'captcha-code-authentication').' </b><span class="required">*</span></label>
169 <div style="clear:both;"></div><div style="clear:both;"></div>';
170 ctl_captcha_generate_code();
171 echo '<label>'.__('Type the text displayed above', 'captcha-code-authentication').':</label>
172 <input id="captcha_code" name="captcha_code" size="15" type="text" />
173 <div style="clear:both;"></div>
174 </p>';
175
176 remove_action( 'comment_form', 'include_ctl_captcha_comment_form' );
177
178 return true;
179 }
180
181 // this function checks captcha posted with the comment
182 function include_ctl_captcha_comment_post($comment)
183 {
184 $c_registered = get_option('wpcaptcha_registered');
185 if (is_user_logged_in() && $c_registered == 'yes') {
186 return $comment;
187 }
188
189 // skip captcha for comment replies from the admin menu
190 if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'replyto-comment' &&
191 ( check_ajax_referer( 'replyto-comment', '_ajax_nonce', false ) || check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment', false ) ) ) {
192 // skip capthca
193 return $comment;
194 }
195
196 // Skip captcha for trackback or pingback
197 if ( $comment['comment_type'] != '' && $comment['comment_type'] != 'comment' ) {
198 // skip captcha
199 return $comment;
200 }
201
202 // If captcha is empty
203 if(empty($_REQUEST['captcha_code']))
204 wp_die( __('CAPTCHA cannot be empty.', 'captcha-code-authentication' ) );
205
206 // captcha was matched
207 if($_SESSION['captcha_code'] == $_REQUEST['captcha_code']) return($comment);
208 else wp_die( __('Error: Incorrect CAPTCHA. Press your browser\'s back button and try again.', 'captcha-code-authentication'));
209 }
210
211 /* <!-- Captcha for Comments authentication ends here --> */
212
213 // Add captcha in the register form
214 $register_captcha = get_option('wpcaptcha_register');
215 if($register_captcha == 'yes')
216 {
217 add_action('register_form', 'include_ctl_wp_captcha_register');
218 add_action( 'register_post', 'include_ctl_captcha_register_post', 10, 3 );
219 add_action( 'signup_extra_fields', 'include_ctl_wp_captcha_register' );
220 add_filter( 'wpmu_validate_user_signup', 'include_ctl_captcha_register_validate' );
221 }
222
223 /* Function to include captcha for register form */
224 function include_ctl_wp_captcha_register($default)
225 {
226 echo '<p class="register-form-captcha">
227 <label><b>'. __('Captcha', 'captcha-code-authentication').' </b><span class="required">*</span></label>
228 <div style="clear:both;"></div><div style="clear:both;"></div>';
229 ctl_captcha_generate_code();
230 echo '<label>'.__('Type the text displayed above', 'captcha-code-authentication').':</label>
231 <input id="captcha_code" name="captcha_code" size="15" type="text" />
232 </p>';
233 return true;
234 }
235
236 /* This function checks captcha posted with registration */
237 function include_ctl_captcha_register_post($login,$email,$errors)
238 {
239 // If captcha is blank - add error
240 if ( isset( $_REQUEST['captcha_code'] ) && "" == $_REQUEST['captcha_code'] )
241 {
242 $errors->add('captcha_blank', '<strong>'.__('ERROR', 'captcha-code-authentication').'</strong>: '.__('Please complete the CAPTCHA.', 'captcha-code-authentication'));
243 return $errors;
244 }
245
246 if ( isset( $_REQUEST['captcha_code'] ) && ($_SESSION['captcha_code'] == $_REQUEST['captcha_code'] )) {
247 // captcha was matched
248 } else {
249 $errors->add('captcha_wrong', '<strong>'.__('ERROR', 'captcha-code-authentication').'</strong>: '.__('That CAPTCHA was incorrect.', 'captcha-code-authentication'));
250 }
251 return($errors);
252 }
253 /* End of the function include_ctl_captcha_register_post */
254
255 function include_ctl_captcha_register_validate($results)
256 {
257 if ( isset( $_REQUEST['captcha_code'] ) && "" == $_REQUEST['captcha_code'] ) {
258 $results['errors']->add('captcha_blank', '<strong>'.__('ERROR', 'captcha-code-authentication').'</strong>: '.__('Please complete the CAPTCHA.', 'captcha-code-authentication'));
259 return $results;
260 }
261
262 if ( isset( $_REQUEST['captcha_code'] ) && ($_SESSION['captcha_code'] == $_REQUEST['captcha_code'] )) {
263 // captcha was matched
264 } else {
265 $results['errors']->add('captcha_wrong', '<strong>'.__('ERROR', 'captcha-code-authentication').'</strong>: '.__('That CAPTCHA was incorrect.', 'captcha-code-authentication'));
266 }
267 return($results);
268 }
269 /* End of the function include_ctl_captcha_register_validate */
270
271 $lost_captcha = get_option('wpcaptcha_lost');
272 // Add captcha into lost password form
273 if($lost_captcha == 'yes'){
274 add_action( 'lostpassword_form', 'include_ctl_captcha_lostpassword' );
275 add_action( 'lostpassword_post', 'include_ctl_captcha_lostpassword_post', 10, 3 );
276 }
277
278 /* Function to include captcha for lost password form */
279 function include_ctl_captcha_lostpassword($default)
280 {
281 echo '<p class="lost-form-captcha">
282 <label><b>'. __('Captcha', 'captcha-code-authentication').' </b><span class="required">*</span></label>
283 <div style="clear:both;"></div><div style="clear:both;"></div>';
284 ctl_captcha_generate_code();
285 echo '<label>'.__('Type the text displayed above', 'captcha-code-authentication').':</label>
286 <input id="captcha_code" name="captcha_code" size="15" type="text" />
287 </p>';
288 }
289
290 function include_wp_captcha_lostpassword_post() {
291 if( isset( $_REQUEST['user_login'] ) && "" == $_REQUEST['user_login'] )
292 return;
293
294 // If captcha doesn't entered
295 if ( isset( $_REQUEST['captcha_code'] ) && "" == $_REQUEST['captcha_code'] ) {
296 wp_die( __( 'Please complete the CAPTCHA.', 'captcha-code-authentication' ) );
297 }
298
299 // Check entered captcha
300 if ( isset( $_REQUEST['captcha_code'] ) && ($_SESSION['captcha_code'] == $_REQUEST['captcha_code'] )) {
301 return;
302 } else {
303 wp_die( __( 'Error: Incorrect CAPTCHA. Press your browser\'s back button and try again.', 'captcha-code-authentication' ) );
304 }
305 }
306
307 function ctl_captcha_generate_code()
308 {
309 //Settings: You can customize the captcha here
310 $image_width = 120;
311 $image_height = 40;
312
313 $characters_on_image = get_option('wpcaptcha_total_no_of_characters');
314 if($characters_on_image < 3 || $characters_on_image > 6) $characters_on_image = 6;
315
316 $wpcaptcha_type = get_option('wpcaptcha_type');
317 $wpcaptcha_letters = get_option('wpcaptcha_letters');
318
319 $font = dirname(__FILE__) . '/monofont.ttf';
320
321 //The characters that can be used in the CAPTCHA code.
322 //avoid confusing characters (l 1 and i for example)
323 if(!empty($wpcaptcha_type) && $wpcaptcha_type == 'alphanumeric')
324 {
325 switch($wpcaptcha_letters)
326 {
327 case 'capital':
328 $possible_letters = '23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
329 break;
330 case 'small':
331 $possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
332 break;
333 case 'capitalsmall':
334 $possible_letters = '23456789bcdfghjkmnpqrstvwxyzABCEFGHJKMNPRSTVWXYZ';
335 break;
336 default:
337 $possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
338 break;
339 }
340 }
341 elseif(!empty($wpcaptcha_type) && $wpcaptcha_type == 'alphabets')
342 {
343 switch($wpcaptcha_letters)
344 {
345 case 'capital':
346 $possible_letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
347 break;
348 case 'small':
349 $possible_letters = 'bcdfghjkmnpqrstvwxyz';
350 break;
351 case 'capitalsmall':
352 $possible_letters = 'bcdfghjkmnpqrstvwxyzABCEFGHJKMNPRSTVWXYZ';
353 break;
354 default:
355 $possible_letters = 'abcdefghijklmnopqrstuvwxyz';
356 break;
357 }
358 }
359 elseif(!empty($wpcaptcha_type) && $wpcaptcha_type == 'numbers')
360 {
361 $possible_letters = '0123456789';
362 }
363 else
364 {
365 $possible_letters = '0123456789';
366 }
367 $random_dots = 0;
368 $random_lines = 20;
369 $captcha_text_color="0x142864";
370 $captcha_noice_color = "0x142864";
371
372 $code = '';
373
374
375 $i = 0;
376 while ($i < $characters_on_image)
377 {
378 $code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
379 $i++;
380 }
381
382
383 $font_size = $image_height * 0.75;
384 $image = @imagecreate($image_width, $image_height);
385
386
387 /* setting the background, text and noise colours here */
388 $background_color = imagecolorallocate($image, 255, 255, 255);
389
390 $arr_text_color = ctl_captcha_hexrgb($captcha_text_color);
391 $text_color = imagecolorallocate($image, $arr_text_color['red'],
392 $arr_text_color['green'], $arr_text_color['blue']);
393
394 $arr_noice_color = ctl_captcha_hexrgb($captcha_noice_color);
395 $image_noise_color = imagecolorallocate($image, $arr_noice_color['red'],
396 $arr_noice_color['green'], $arr_noice_color['blue']);
397
398
399 /* generating the dots randomly in background */
400 for( $i=0; $i<$random_dots; $i++ )
401 {
402 imagefilledellipse($image, mt_rand(0,$image_width), mt_rand(0,$image_height), 2, 3, $image_noise_color);
403 }
404
405
406 /* generating lines randomly in background of image */
407 for( $i=0; $i<$random_lines; $i++ ) {
408 imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height), mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
409 }
410
411
412 /* create a text box and add 6 letters code in it */
413 $textbox = imagettfbbox($font_size, 0, $font, $code);
414 $x = ($image_width - $textbox[4])/2;
415 $y = ($image_height - $textbox[5])/2;
416 imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
417
418 ob_start();
419 imagejpeg($image);//showing the image
420 printf('<img src="data:image/png;base64,%s"/ width="100">', base64_encode(ob_get_clean()));
421 imagedestroy($image);//destroying the image instance
422 $_SESSION['captcha_code'] = $code;
423 }
424
425 function ctl_captcha_hexrgb ($hexstr)
426 {
427 $int = hexdec($hexstr);
428
429 return array("red" => 0xFF & ($int >> 0x10),
430 "green" => 0xFF & ($int >> 0x8),
431 "blue" => 0xFF & $int);
432 }
433 ?>
434