| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Captcha Code |
| 4 |
Plugin URI: http://www.vinojcardoza.com/captcha-code-authentication/ |
| 5 |
Description: Adds Captcha Code anti-spam methods to User front-end WordPress forms. |
| 6 |
Version: 2.7 |
| 7 |
Author: Vinoj Cardoza |
| 8 |
Author URI: http://www.vinojcardoza.com |
| 9 |
License: GPL2 |
| 10 |
*/ |
| 11 |
|
| 12 |
define('WP_CAPTCHA_DIR_URL', plugin_dir_url(__FILE__)); |
| 13 |
define('WP_CAPTCHA_DIR', dirname(__FILE__)); |
| 14 |
|
| 15 |
require 'general_options.php'; |
| 16 |
|
| 17 |
/* Hook to initalize the admin menu */ |
| 18 |
add_action('admin_menu', 'wp_captcha_admin_menu'); |
| 19 |
/* Hook to initialize sessions */ |
| 20 |
add_action('init', 'wp_captcha_init_sessions'); |
| 21 |
|
| 22 |
/* Hook to store the plugin status */ |
| 23 |
register_activation_hook(__FILE__, 'wp_captcha_enabled'); |
| 24 |
register_deactivation_hook(__FILE__, 'wp_captcha_disabled'); |
| 25 |
|
| 26 |
function wp_captcha_enabled(){ |
| 27 |
update_option('wpcaptcha_status', 'enabled'); |
| 28 |
} |
| 29 |
function wp_captcha_disabled(){ |
| 30 |
update_option('wpcaptcha_status', 'disabled'); |
| 31 |
} |
| 32 |
|
| 33 |
/* To add the menus in the admin section */ |
| 34 |
function wp_captcha_admin_menu(){ |
| 35 |
add_options_page( |
| 36 |
__('Captcha settings'), |
| 37 |
__('Captcha settings'), |
| 38 |
'manage_options', |
| 39 |
'wp_captcha_slug', |
| 40 |
'wp_captcha_general_options'); |
| 41 |
} |
| 42 |
|
| 43 |
function wp_captcha_init_sessions(){ |
| 44 |
if(!session_id()){ |
| 45 |
session_start(); |
| 46 |
} |
| 47 |
load_plugin_textdomain('wpcaptchadomain', false, dirname( plugin_basename(__FILE__)).'/languages'); |
| 48 |
$_SESSION['captcha_type'] = get_option('wpcaptcha_type'); |
| 49 |
$_SESSION['captcha_letters'] = get_option('wpcaptcha_letters'); |
| 50 |
$_SESSION['total_no_of_characters'] = get_option('wpcaptcha_total_no_of_characters'); |
| 51 |
if(empty($_SESSION['total_no_of_characters'])){ |
| 52 |
$_SESSION['total_no_of_characters'] = 6; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/* Captcha for login authentication starts here */ |
| 57 |
|
| 58 |
$login_captcha = get_option('wpcaptcha_login'); |
| 59 |
if($login_captcha == 'yes'){ |
| 60 |
add_action('login_form', 'include_wp_captcha_login'); |
| 61 |
add_filter( 'login_errors', 'include_captcha_login_errors' ); |
| 62 |
add_filter( 'login_redirect', 'include_captcha_login_redirect', 10, 3 ); |
| 63 |
} |
| 64 |
|
| 65 |
/* Function to include captcha for login form */ |
| 66 |
function include_wp_captcha_login(){ |
| 67 |
echo '<p class="login-form-captcha"> |
| 68 |
<label><b>'. __('Captcha', 'wpcaptchadomain').' </b></label> |
| 69 |
<span class="required">*</span> |
| 70 |
<div style="clear:both;"></div> |
| 71 |
<img alt="code" src="'.WP_CAPTCHA_DIR_URL.'captcha_code_file.php?rand='.rand().'" /> |
| 72 |
<div style="clear:both;"></div>'; |
| 73 |
|
| 74 |
/* Will retrieve the get varibale and prints a message from url if the captcha is wrong */ |
| 75 |
if(isset($_GET['captcha']) && $_GET['captcha'] == 'confirm_error' ) { |
| 76 |
echo '<label style="color:#FF0000;" id="capt_err">'.$_SESSION['captcha_error'].'</label><div style="clear:both;"></div>';; |
| 77 |
$_SESSION['captcha_error'] = ''; |
| 78 |
} |
| 79 |
|
| 80 |
echo '<label>'.__('Type the text displayed above', 'wpcaptchadomain').':</label> |
| 81 |
<input id="captcha_code" name="captcha_code" size="15" type="text" tabindex="30" /> |
| 82 |
</p>'; |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
/* Hook to find out the errors while logging in */ |
| 87 |
function include_captcha_login_errors($errors){ |
| 88 |
if( isset( $_REQUEST['action'] ) && 'register' == $_REQUEST['action'] ) |
| 89 |
return($errors); |
| 90 |
|
| 91 |
if($_SESSION['captcha_code'] != $_REQUEST['captcha_code']){ |
| 92 |
return $errors.'<label id="capt_err" for="captcha_code_error">'.__('Captcha confirmation error!', 'wpcaptchadomain').'</label>'; |
| 93 |
} |
| 94 |
return $errors; |
| 95 |
} |
| 96 |
|
| 97 |
/* Hook to redirect after captcha confirmation */ |
| 98 |
function include_captcha_login_redirect($url){ |
| 99 |
|
| 100 |
/* Captcha mismatch */ |
| 101 |
if(isset($_SESSION['captcha_code']) && isset($_REQUEST['captcha_code']) && $_SESSION['captcha_code'] != $_REQUEST['captcha_code']){ |
| 102 |
$_SESSION['captcha_error'] = __('Incorrect captcha confirmation!', 'wpcaptchadomain'); |
| 103 |
wp_clear_auth_cookie(); |
| 104 |
return $_SERVER["REQUEST_URI"]."/?captcha='confirm_error'"; |
| 105 |
} |
| 106 |
/* Captcha match: take to the admin panel */ |
| 107 |
else{ |
| 108 |
return home_url('/wp-admin/'); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/* <!-- Captcha for login authentication ends here --> */ |
| 113 |
|
| 114 |
/* Captcha for Comments ends here */ |
| 115 |
$comment_captcha = get_option('wpcaptcha_comments'); |
| 116 |
if($comment_captcha == 'yes'){ |
| 117 |
global $wp_version; |
| 118 |
if( version_compare($wp_version,'3','>=') ) { // wp 3.0 + |
| 119 |
add_action( 'comment_form_after_fields', 'include_wp_captcha_comment_form_wp3', 1 ); |
| 120 |
add_action( 'comment_form_logged_in_after', 'include_wp_captcha_comment_form_wp3', 1 ); |
| 121 |
} |
| 122 |
// for WP before WP 3.0 |
| 123 |
add_action( 'comment_form', 'include_captcha_comment_form' ); |
| 124 |
add_filter( 'preprocess_comment', 'include_captcha_comment_post' ); |
| 125 |
} |
| 126 |
|
| 127 |
/* Function to include captcha for comments form */ |
| 128 |
function include_captcha_comment_form(){ |
| 129 |
$c_registered = get_option('wpcaptcha_registered'); |
| 130 |
if ( is_user_logged_in() && $c_registered == 'yes') { |
| 131 |
return true; |
| 132 |
} |
| 133 |
echo '<p class="comment-form-captcha"> |
| 134 |
<label><b>'. __('Captcha', 'wpcaptchadomain').' </b></label> |
| 135 |
<span class="required">*</span> |
| 136 |
<div style="clear:both;"></div> |
| 137 |
<img alt="code" src="'.WP_CAPTCHA_DIR_URL.'captcha_code_file.php?rand='.rand().'" /> |
| 138 |
<div style="clear:both;"></div> |
| 139 |
<label>'.__('Type the text displayed above', 'wpcaptchadomain').':</label> |
| 140 |
<input id="captcha_code" name="captcha_code" size="15" type="text" /> |
| 141 |
<div style="clear:both;"></div> |
| 142 |
</p>'; |
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
/* Function to include captcha for comments form > wp3 */ |
| 147 |
function include_wp_captcha_comment_form_wp3(){ |
| 148 |
$c_registered = get_option('wpcaptcha_registered'); |
| 149 |
if ( is_user_logged_in() && $c_registered == 'yes') { |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
echo '<p class="comment-form-captcha"> |
| 154 |
<label><b>'. __('Captcha', 'wpcaptchadomain').' </b></label> |
| 155 |
<span class="required">*</span> |
| 156 |
<div style="clear:both;"></div> |
| 157 |
<img alt="code" src="'.WP_CAPTCHA_DIR_URL.'captcha_code_file.php?rand='.rand().'" /> |
| 158 |
<div style="clear:both;"></div> |
| 159 |
<label>'.__('Type the text displayed above', 'wpcaptchadomain').':</label> |
| 160 |
<input id="captcha_code" name="captcha_code" size="15" type="text" /> |
| 161 |
<div style="clear:both;"></div> |
| 162 |
</p>'; |
| 163 |
|
| 164 |
remove_action( 'comment_form', 'include_captcha_comment_form' ); |
| 165 |
|
| 166 |
return true; |
| 167 |
} |
| 168 |
|
| 169 |
// this function checks captcha posted with the comment |
| 170 |
function include_captcha_comment_post($comment) { |
| 171 |
$c_registered = get_option('wpcaptcha_registered'); |
| 172 |
if (is_user_logged_in() && $c_registered == 'yes') { |
| 173 |
return $comment; |
| 174 |
} |
| 175 |
|
| 176 |
// skip captcha for comment replies from the admin menu |
| 177 |
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'replyto-comment' && |
| 178 |
( check_ajax_referer( 'replyto-comment', '_ajax_nonce', false ) || check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment', false ) ) ) { |
| 179 |
// skip capthca |
| 180 |
return $comment; |
| 181 |
} |
| 182 |
|
| 183 |
// Skip captcha for trackback or pingback |
| 184 |
if ( $comment['comment_type'] != '' && $comment['comment_type'] != 'comment' ) { |
| 185 |
// skip captcha |
| 186 |
return $comment; |
| 187 |
} |
| 188 |
|
| 189 |
// If captcha is empty |
| 190 |
if(empty($_REQUEST['captcha_code'])) |
| 191 |
wp_die( __('CAPTCHA cannot be empty.', 'wpcaptchadomain' ) ); |
| 192 |
|
| 193 |
// captcha was matched |
| 194 |
if($_SESSION['captcha_code'] == $_REQUEST['captcha_code']) return($comment); |
| 195 |
else wp_die( __('Error: Incorrect CAPTCHA. Press your browser\'s back button and try again.', 'wpcaptchadomain')); |
| 196 |
} |
| 197 |
|
| 198 |
/* <!-- Captcha for Comments authentication ends here --> */ |
| 199 |
|
| 200 |
// Add captcha in the register form |
| 201 |
$register_captcha = get_option('wpcaptcha_register'); |
| 202 |
if($register_captcha == 'yes'){ |
| 203 |
add_action('register_form', 'include_wp_captcha_register'); |
| 204 |
add_action( 'register_post', 'include_captcha_register_post', 10, 3 ); |
| 205 |
add_action( 'signup_extra_fields', 'include_wp_captcha_register' ); |
| 206 |
add_filter( 'wpmu_validate_user_signup', 'include_captcha_register_validate' ); |
| 207 |
} |
| 208 |
|
| 209 |
/* Function to include captcha for register form */ |
| 210 |
function include_wp_captcha_register($default){ |
| 211 |
echo '<p class="register-form-captcha"> |
| 212 |
<label><b>'. __('Captcha', 'wpcaptchadomain').' </b></label> |
| 213 |
<span class="required">*</span> |
| 214 |
<div style="clear:both;"></div> |
| 215 |
<img alt="code" src="'.WP_CAPTCHA_DIR_URL.'captcha_code_file.php?rand='.rand().'" /> |
| 216 |
<div style="clear:both;"></div> |
| 217 |
<label>'.__('Type the text displayed above', 'wpcaptchadomain').':</label> |
| 218 |
<input id="captcha_code" name="captcha_code" size="15" type="text" /> |
| 219 |
</p>'; |
| 220 |
return true; |
| 221 |
} |
| 222 |
|
| 223 |
/* This function checks captcha posted with registration */ |
| 224 |
function include_captcha_register_post($login,$email,$errors) { |
| 225 |
|
| 226 |
// If captcha is blank - add error |
| 227 |
if ( isset( $_REQUEST['captcha_code'] ) && "" == $_REQUEST['captcha_code'] ) { |
| 228 |
$errors->add('captcha_blank', '<strong>'.__('ERROR', 'wpcaptchadomain').'</strong>: '.__('Please complete the CAPTCHA.', 'wpcaptchadomain')); |
| 229 |
return $errors; |
| 230 |
} |
| 231 |
|
| 232 |
if ( isset( $_REQUEST['captcha_code'] ) && ($_SESSION['captcha_code'] == $_REQUEST['captcha_code'] )) { |
| 233 |
// captcha was matched |
| 234 |
} else { |
| 235 |
$errors->add('captcha_wrong', '<strong>'.__('ERROR', 'wpcaptchadomain').'</strong>: '.__('That CAPTCHA was incorrect.', 'wpcaptchadomain')); |
| 236 |
} |
| 237 |
return($errors); |
| 238 |
} |
| 239 |
/* End of the function include_captcha_register_post */ |
| 240 |
|
| 241 |
function include_captcha_register_validate($results) { |
| 242 |
if ( isset( $_REQUEST['captcha_code'] ) && "" == $_REQUEST['captcha_code'] ) { |
| 243 |
$results['errors']->add('captcha_blank', '<strong>'.__('ERROR', 'wpcaptchadomain').'</strong>: '.__('Please complete the CAPTCHA.', 'wpcaptchadomain')); |
| 244 |
return $results; |
| 245 |
} |
| 246 |
|
| 247 |
if ( isset( $_REQUEST['captcha_code'] ) && ($_SESSION['captcha_code'] == $_REQUEST['captcha_code'] )) { |
| 248 |
// captcha was matched |
| 249 |
} else { |
| 250 |
$results['errors']->add('captcha_wrong', '<strong>'.__('ERROR', 'wpcaptchadomain').'</strong>: '.__('That CAPTCHA was incorrect.', 'wpcaptchadomain')); |
| 251 |
} |
| 252 |
return($results); |
| 253 |
} |
| 254 |
/* End of the function include_captcha_register_validate */ |
| 255 |
|
| 256 |
$lost_captcha = get_option('wpcaptcha_lost'); |
| 257 |
// Add captcha into lost password form |
| 258 |
if($lost_captcha == 'yes'){ |
| 259 |
add_action( 'lostpassword_form', 'include_wp_captcha_lostpassword' ); |
| 260 |
add_action( 'lostpassword_post', 'include_wp_captcha_lostpassword_post', 10, 3 ); |
| 261 |
} |
| 262 |
|
| 263 |
/* Function to include captcha for lost password form */ |
| 264 |
function include_wp_captcha_lostpassword($default){ |
| 265 |
echo '<p class="lost-form-captcha"> |
| 266 |
<label><b>'. __('Captcha', 'wpcaptchadomain').' </b></label> |
| 267 |
<span class="required">*</span> |
| 268 |
<div style="clear:both;"></div> |
| 269 |
<img alt="code" src="'.WP_CAPTCHA_DIR_URL.'captcha_code_file.php?rand='.rand().'" /> |
| 270 |
<div style="clear:both;"></div> |
| 271 |
<label>'.__('Type the text displayed above', 'wpcaptchadomain').':</label> |
| 272 |
<input id="captcha_code" name="captcha_code" size="15" type="text" /> |
| 273 |
</p>'; |
| 274 |
} |
| 275 |
|
| 276 |
function include_wp_captcha_lostpassword_post() { |
| 277 |
if( isset( $_REQUEST['user_login'] ) && "" == $_REQUEST['user_login'] ) |
| 278 |
return; |
| 279 |
|
| 280 |
// If captcha doesn't entered |
| 281 |
if ( isset( $_REQUEST['captcha_code'] ) && "" == $_REQUEST['captcha_code'] ) { |
| 282 |
wp_die( __( 'Please complete the CAPTCHA.', 'wpcaptchadomain' ) ); |
| 283 |
} |
| 284 |
|
| 285 |
// Check entered captcha |
| 286 |
if ( isset( $_REQUEST['captcha_code'] ) && ($_SESSION['captcha_code'] == $_REQUEST['captcha_code'] )) { |
| 287 |
return; |
| 288 |
} else { |
| 289 |
wp_die( __( 'Error: Incorrect CAPTCHA. Press your browser\'s back button and try again.', 'wpcaptchadomain' ) ); |
| 290 |
} |
| 291 |
} |
| 292 |
?> |
| 293 |
|