PluginProbe ʕ •ᴥ•ʔ
SiteGuard WP Plugin / 1.2.2
SiteGuard WP Plugin v1.2.2
1.8.6 1.8.6-beta1 1.8.6-beta2 1.8.4 1.8.5 1.8.3 1.8.2 1.8.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.4.3 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.11 1.7.12 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8.0 1.8.0-beta1 1.8.0-beta2 1.8.0-beta3 1.8.0-beta4
siteguard / classes / siteguard-captcha.php
siteguard / classes Last commit date
siteguard-admin-filter.php 11 years ago siteguard-base.php 11 years ago siteguard-captcha.php 11 years ago siteguard-config.php 11 years ago siteguard-disable-pingback.php 11 years ago siteguard-htaccess.php 11 years ago siteguard-login-alert.php 11 years ago siteguard-login-history.php 11 years ago siteguard-login-lock.php 11 years ago siteguard-rename-login.php 11 years ago siteguard-updates-notify.php 11 years ago siteguard-waf-exclude-rule.php 11 years ago
siteguard-captcha.php
228 lines
1 <?php
2
3 include_once( SITEGUARD_PATH . 'really-simple-captcha/siteguard-really-simple-captcha.php' );
4
5 class SiteGuard_CAPTCHA extends SiteGuard_Base {
6 var $captcha;
7 var $prefix;
8 var $word;
9
10 function __construct( ) {
11 global $config;
12 if ( '1' == $config->get( 'captcha_enable' ) && 'xmlrpc.php' != basename( $_SERVER['SCRIPT_NAME'] ) ) {
13 $this->captcha = new SiteGuardReallySimpleCaptcha( );
14 $this->captcha->bg = array( 255, 255, 255 );
15
16 add_filter( 'shake_error_codes', array( $this, 'handler_shake_error_codes' ) );
17
18 // for logiin
19 if ( '0' != $config->get( 'captcha_login' ) ) {
20 add_filter( 'login_form', array( $this, 'handler_login_form' ) );
21 add_filter( 'wp_authenticate_user', array( $this, 'handler_wp_authenticate_user' ), 1, 2 );
22 }
23 // for lost password
24 if ( '0' != $config->get( 'captcha_lostpasswd' ) ) {
25 add_filter( 'lostpassword_form', array( $this, 'handler_lostpassword_form' ) );
26 add_filter( 'lostpassword_post', array( $this, 'handler_lostpassword_post' ), 1 );
27 }
28 // for register user
29 if ( '0' != $config->get( 'captcha_registuser' ) ) {
30 add_filter( 'register_form', array( $this, 'handler_register_form' ) );
31 add_action( 'registration_errors', array( $this, 'handler_registration_errors' ), 10, 3 );
32 }
33 // for comment
34 if ( '0' != $config->get( 'captcha_comment' ) ) {
35 add_action( 'comment_form_after_fields', array( $this, 'handler_comment_form' ), 1 );
36 add_action( 'comment_form_logged_in_after', array( $this, 'handler_comment_form' ), 1 );
37 add_action( 'comment_form', array( $this, 'handler_comment_form' ) );
38 add_filter( 'preprocess_comment', array( $this, 'handler_process_comment_post' ) );
39 }
40 }
41 if ( '1' == $config->get( 'same_login_error' ) ) {
42 add_filter( 'login_errors', array( $this, 'handler_login_errors' ) );
43 }
44 }
45 function check_requirements( ) {
46 $error = check_multisite( );
47 if ( is_wp_error( $error ) ) {
48 return $error;
49 }
50 $error = $this->check_extensions( );
51 if ( is_wp_error( $error ) ) {
52 return $error;
53 }
54 $error = $this->check_image_access( );
55 if ( is_wp_error( $error ) ) {
56 return $error;
57 }
58 return true;
59 }
60 function check_extensions( ) {
61 $error_extensions = array();
62 $extensions = array(
63 'mbstring',
64 'gd',
65 );
66 foreach ( $extensions as $extension ) {
67 if ( ! extension_loaded( $extension ) ) {
68 $error_extensions[] = $extension;
69 }
70 }
71 if ( empty( $error_extensions ) ) {
72 return true;
73 }
74
75 $message = esc_html__( 'In order to enable this function, it is necessary to install expanded modules', 'siteguard' );
76 $message .= ' ( ';
77 $count = 0;
78 foreach ( $error_extensions as $extension ) {
79 if ( 0 != $count ) {
80 $message .= ', ';
81 }
82 $message .= $extension;
83 $count ++;
84 }
85 $message .= ' ) ';
86 $message .= esc_html__( 'in the server.', 'siteguard' );
87
88 $error = new WP_Error( 'siteguard_captcha', $message );
89 return $error;
90 }
91 function check_image_access( ) {
92 if ( is_object( $this->captcha ) ) {
93 $this->captcha->make_tmp_dir( );
94 } else {
95 $captcha = new SiteGuardReallySimpleCaptcha( );
96 $captcha->make_tmp_dir( );
97 }
98 $result = wp_remote_get( SITEGUARD_URL_PATH . 'really-simple-captcha/tmp/dummy.png' );
99 if ( ! is_wp_error( $result ) && 200 === $result['response']['code'] ) {
100 return true;
101 }
102 $message = esc_html__( 'In order to enable this function, it is necessary to specify Limit to AllowOverride in httpd.conf.', 'siteguard' );
103 $error = new WP_Error( 'siteguard_captcha', $message );
104 return $error;
105 }
106 function handler_login_errors( $error ) {
107 if ( strlen( $error ) > 0 && false === strpos( $error, esc_html__( 'ERROR: LOGIN LOCKED', 'siteguard' ) ) && false === strpos( $error, esc_html__( 'ERROR: Please login entry again', 'siteguard' ) ) ) {
108 $error = esc_html__( 'ERROR: Please check the input and resend.', 'siteguard' );
109 }
110 return $error;
111 }
112 function handler_shake_error_codes( $shake_error_codes ) {
113 array_push( $shake_error_codes, 'siteguard-captcha-error' );
114 return $shake_error_codes;
115 }
116
117 function init( ) {
118 global $config;
119 $errors = $this->check_requirements( );
120 if ( ! is_wp_error( $errors ) ) {
121 $switch = '1';
122 } else {
123 $switch = '0';
124 }
125 $config->set( 'captcha_enable', $switch );
126 $language = get_bloginfo( 'language' );
127 if ( 'ja' == $language ) {
128 $mode = '1'; // hiragana
129 } else {
130 $mode = '2'; // alphanumeric
131 }
132 $config->set( 'captcha_login', $mode );
133 $config->set( 'captcha_comment', $mode );
134 $config->set( 'captcha_lostpasswd', $mode );
135 $config->set( 'captcha_registuser', $mode );
136 if ( true === check_multisite( ) ) {
137 $config->set( 'same_login_error', '1' );
138 } else {
139 $config->set( 'same_login_error', '0' );
140 }
141 $config->update( );
142 }
143 function get_captcha( ) {
144 $result = '<p>';
145 $result .= '<img src="'. SITEGUARD_URL_PATH . 'really-simple-captcha/tmp/' . $this->prefix . '.png" alt="CAPTCHA">';
146 $result .= '</p><p>';
147 $result .= '<label for="siteguard_captcha">' . esc_html__( 'Please input characters displayed above.', 'siteguard' ) . '</label><br />';
148 $result .= '<input type="text" name="siteguard_captcha" id="siteguard_captcha" class="input" value="" size="10" aria-required="true" />';
149 $result .= '<input type="hidden" name="siteguard_captcha_prefix" id="siteguard_captcha_prefix" value="'.$this->prefix.'" />';
150 $result .= '</p>';
151
152 return $result;
153 }
154 function put_captcha( ) {
155 $this->word = $this->captcha->generate_random_word( );
156 $this->prefix = mt_rand( );
157 $this->captcha->generate_image( $this->prefix, $this->word );
158 echo $this->get_captcha( );
159 }
160 function handler_login_form( ) {
161 global $config;
162 ( '2' == $config->get( 'captcha_login' ) ) ? $this->captcha->lang_mode = 'en' : $this->captcha->lang_mode = 'jp';
163 $this->put_captcha( );
164 }
165 function handler_comment_form( $post_id ) {
166 global $config;
167 if ( defined( 'PUT_COMMENT_FORM' ) ) {
168 return;
169 }
170 ( '2' == $config->get( 'captcha_comment' ) ) ? $this->captcha->lang_mode = 'en' : $this->captcha->lang_mode = 'jp';
171 $this->put_captcha( );
172 define( 'PUT_COMMENT_FORM', '1' );
173 }
174 function handler_lostpassword_form( ) {
175 global $config;
176 ( '2' == $config->get( 'captcha_lostpasswd' ) ) ? $this->captcha->lang_mode = 'en' : $this->captcha->lang_mode = 'jp';
177 $this->put_captcha( );
178 }
179 function handler_register_form( ) {
180 global $config;
181 ( '2' == $config->get( 'captcha_registuser' ) ) ? $this->captcha->lang_mode = 'en' : $this->captcha->lang_mode = 'jp';
182 $this->put_captcha( );
183 }
184 function handler_wp_authenticate_user( $user, $password ) {
185 if ( array_key_exists( 'siteguard_captcha', $_POST ) && array_key_exists( 'siteguard_captcha_prefix', $_POST ) ) {
186 if ( $this->captcha->check( $_POST['siteguard_captcha_prefix'], $_POST['siteguard_captcha'] ) ) {
187 return $user;
188 }
189 }
190 $error = new WP_Error( );
191 $error->add( 'siteguard-captcha-error', esc_html__( 'ERROR: Invalid CAPTCHA.', 'siteguard' ) );
192 return $error;
193 }
194 function add_captcha_error( ) {
195 return new WP_Error( 'siteguard-captcha-error', esc_html__( 'ERROR: Invalid CAPTCHA.', 'siteguard' ) );
196 }
197 function handler_lostpassword_post( ) {
198 if ( array_key_exists( 'siteguard_captcha', $_POST ) && array_key_exists( 'siteguard_captcha_prefix', $_POST ) ) {
199 if ( $this->captcha->check( $_POST['siteguard_captcha_prefix'], $_POST['siteguard_captcha'] ) ) {
200 return;
201 }
202 }
203 add_filter( 'allow_password_reset', array( $this, 'add_captcha_error' ) );
204 }
205 function handler_registration_errors( $errors, $sanitized_user_login, $user_email ) {
206 if ( array_key_exists( 'siteguard_captcha', $_POST ) && array_key_exists( 'siteguard_captcha_prefix', $_POST ) ) {
207 if ( $this->captcha->check( $_POST['siteguard_captcha_prefix'], $_POST['siteguard_captcha'] ) ) {
208 return $errors;
209 }
210 }
211 $new_errors = new WP_Error( );
212 $new_errors->add( 'siteguard-captcha-error', esc_html__( 'ERROR: Invalid CAPTCHA.', 'siteguard' ) );
213 return $new_errors;
214 }
215 function handler_process_comment_post( $comment ) {
216 if ( array_key_exists( 'siteguard_captcha', $_POST ) && array_key_exists( 'siteguard_captcha_prefix', $_POST ) ) {
217 if ( ! empty( $_POST['siteguard_captcha'] ) ) {
218 if ( $this->captcha->check( $_POST['siteguard_captcha_prefix'], $_POST['siteguard_captcha'] ) ) {
219 return $comment;
220 }
221 }
222 }
223 wp_die( esc_html__( 'ERROR: Invalid CAPTCHA.', 'siteguard' ) );
224 }
225 }
226
227 ?>
228