PluginProbe ʕ •ᴥ•ʔ
SiteGuard WP Plugin / 1.8.7
SiteGuard WP Plugin v1.8.7
1.8.7 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 1 month ago siteguard-base.php 1 month ago siteguard-captcha.php 1 month ago siteguard-config.php 11 months ago siteguard-disable-author-query.php 2 weeks ago siteguard-disable-pingback.php 1 month ago siteguard-disable-xmlrpc.php 1 month ago siteguard-htaccess.php 2 weeks ago siteguard-login-alert.php 1 month ago siteguard-login-history.php 1 month ago siteguard-login-lock.php 1 month ago siteguard-rename-login.php 1 week ago siteguard-updates-notify.php 1 month ago siteguard-waf-exclude-rule.php 1 month ago
siteguard-captcha.php
287 lines
1 <?php
2
3 require_once SITEGUARD_PATH . 'really-simple-captcha/siteguard-really-simple-captcha.php';
4
5 class SiteGuard_CAPTCHA extends SiteGuard_Base {
6 protected $captcha;
7 protected $prefix;
8 protected $word;
9
10 private $last_check_result = null;
11 private $last_check_prefix = null;
12
13 function __construct() {
14 global $siteguard_config;
15 if ( '1' == $siteguard_config->get( 'captcha_enable' ) && 'xmlrpc.php' != basename( $_SERVER['SCRIPT_NAME'] ) && ! is_admin() ) {
16 $this->captcha = new SiteGuardReallySimpleCaptcha();
17
18 add_filter( 'shake_error_codes', array( $this, 'handler_shake_error_codes' ) );
19
20 // for login
21 if ( '0' !== $siteguard_config->get( 'captcha_login' ) ) {
22 add_filter( 'login_form', array( $this, 'handler_login_form' ) );
23 add_filter( 'wp_authenticate_user', array( $this, 'handler_wp_authenticate_user' ), 1, 2 );
24 }
25 // for lost password
26 if ( '0' !== $siteguard_config->get( 'captcha_lostpasswd' ) ) {
27 add_filter( 'lostpassword_form', array( $this, 'handler_lostpassword_form' ) );
28 add_filter( 'lostpassword_post', array( $this, 'handler_lostpassword_post' ), 1 );
29 }
30 // for register user
31 if ( '0' !== $siteguard_config->get( 'captcha_registuser' ) ) {
32 add_filter( 'register_form', array( $this, 'handler_register_form' ) );
33 add_action( 'registration_errors', array( $this, 'handler_registration_errors' ), 10, 3 );
34 }
35 // for comment
36 if ( '0' !== $siteguard_config->get( 'captcha_comment' ) ) {
37 add_action( 'comment_form_after_fields', array( $this, 'handler_comment_form' ), 1 );
38 add_action( 'comment_form_logged_in_after', array( $this, 'handler_comment_form' ), 1 );
39 add_action( 'comment_form', array( $this, 'handler_comment_form' ) );
40 add_filter( 'preprocess_comment', array( $this, 'handler_process_comment_post' ) );
41 add_action( 'wp_footer', array( $this, 'comment_captcha_reload_script' ) );
42 }
43 }
44 if ( '1' == $siteguard_config->get( 'same_login_error' ) ) {
45 add_filter( 'login_errors', array( $this, 'handler_login_errors' ) );
46 }
47 }
48 private function check_captcha_with_cache() {
49 $current_prefix = isset( $_POST['siteguard_captcha_prefix'] ) ? $_POST['siteguard_captcha_prefix'] : '';
50 if ( $this->last_check_prefix !== $current_prefix ) {
51 $this->last_check_result = null;
52 $this->last_check_prefix = $current_prefix;
53 }
54 if ( null !== $this->last_check_result ) {
55 return $this->last_check_result;
56 }
57
58 $is_ok = false;
59 if ( array_key_exists( 'siteguard_captcha', $_POST ) && array_key_exists( 'siteguard_captcha_prefix', $_POST ) ) {
60 $is_ok = $this->captcha->check( $_POST['siteguard_captcha_prefix'], $_POST['siteguard_captcha'], true );
61 }
62
63 $this->last_check_result = $is_ok;
64 return $is_ok;
65 }
66 function check_requirements() {
67 $error = siteguard_check_multisite();
68 if ( is_wp_error( $error ) ) {
69 return $error;
70 }
71 $error = $this->check_extensions();
72 if ( is_wp_error( $error ) ) {
73 return $error;
74 }
75 $error = $this->check_image_access();
76 if ( is_wp_error( $error ) ) {
77 return $error;
78 }
79 $error = $this->check_support_freetype();
80 if ( is_wp_error( $error ) ) {
81 return $error;
82 }
83 return true;
84 }
85
86 function check_extensions() {
87 $error_extensions = array();
88 $extensions = array(
89 'mbstring',
90 'gd',
91 );
92 foreach ( $extensions as $extension ) {
93 if ( ! extension_loaded( $extension ) ) {
94 $error_extensions[] = $extension;
95 }
96 }
97 if ( empty( $error_extensions ) ) {
98 return true;
99 }
100
101 $message = esc_html__( 'This feature requires additional server components. Please contact your hosting provider to enable it.', 'siteguard' );
102
103 $error = new WP_Error( 'siteguard_captcha', $message );
104 return $error;
105 }
106
107 function check_image_access() {
108 if ( is_object( $this->captcha ) ) {
109 $ret = $this->captcha->make_tmp_dir();
110 } else {
111 $captcha = new SiteGuardReallySimpleCaptcha();
112 $ret = $captcha->make_tmp_dir();
113 }
114 if ( false === $ret ) {
115 $message = esc_html__( 'Failed to write the CAPTCHA image file.', 'siteguard' );
116 $error = new WP_Error( 'siteguard_captcha', $message );
117 return $error;
118 }
119
120 return true;
121 }
122
123 function check_support_freetype() {
124 if ( function_exists( 'imagettftext' ) ) {
125 return true;
126 }
127 $message = esc_html__( 'Your server does not support the image rendering required for this feature. Please contact your hosting provider.', 'siteguard' );
128 $error = new WP_Error( 'siteguard_captcha', $message );
129 return $error;
130 }
131
132 function handler_login_errors( $error ) {
133 if ( strlen( $error ) > 0 && false === strpos( $error, esc_html__( 'ERROR: LOGIN LOCKED', 'siteguard' ) ) ) {
134 $error = esc_html__( 'ERROR: Please check your input and try again.', 'siteguard' );
135 }
136 return $error;
137 }
138
139 function handler_shake_error_codes( $shake_error_codes ) {
140 array_push( $shake_error_codes, 'siteguard-captcha-error' );
141 return $shake_error_codes;
142 }
143
144 function init() {
145 global $siteguard_config;
146 $errors = $this->check_requirements();
147 if ( ! is_wp_error( $errors ) ) {
148 $switch = '1';
149 } else {
150 $switch = '0';
151 }
152 $siteguard_config->set( 'captcha_enable', $switch );
153
154 $language = get_bloginfo( 'language' );
155 if ( 'ja' == $language ) {
156 $mode = '1'; // hiragana
157 } else {
158 $mode = '2'; // alphanumeric
159 }
160 $siteguard_config->set( 'captcha_login', $mode );
161 $siteguard_config->set( 'captcha_comment', $mode );
162 $siteguard_config->set( 'captcha_lostpasswd', $mode );
163 $siteguard_config->set( 'captcha_registuser', $mode );
164
165 if ( true === siteguard_check_multisite() ) {
166 $siteguard_config->set( 'same_login_error', '1' );
167 } else {
168 $siteguard_config->set( 'same_login_error', '0' );
169 }
170 $siteguard_config->update();
171 }
172
173 function get_captcha() {
174 $result = '<p>';
175 $result .= '<img src="' . WP_CONTENT_URL . '/siteguard/' . $this->prefix . '.png" alt="CAPTCHA">';
176 $result .= '</p><p>';
177 $result .= '<label for="siteguard_captcha">' . esc_html__( 'Please enter the characters shown above.', 'siteguard' ) . '</label><br />';
178 $result .= '<input type="text" name="siteguard_captcha" id="siteguard_captcha" class="input" value="" size="10" aria-required="true" />';
179 $result .= '<input type="hidden" name="siteguard_captcha_prefix" id="siteguard_captcha_prefix" value="' . $this->prefix . '" />';
180 $result .= '</p>';
181
182 return $result;
183 }
184
185 function put_captcha() {
186 $this->word = $this->captcha->generate_random_word();
187 $this->prefix = siteguard_rand();
188 $this->captcha->generate_image( $this->prefix, $this->word );
189 echo $this->get_captcha();
190 }
191
192 function handler_login_form() {
193 global $siteguard_config;
194 ( '2' === $siteguard_config->get( 'captcha_login' ) ) ? $this->captcha->set_lang_mode( 'en' ) : $this->captcha->set_lang_mode( 'jp' );
195 $this->put_captcha();
196 }
197
198 function handler_comment_form( $post_id ) {
199 global $siteguard_config;
200 if ( defined( 'SITEGUARD_PUT_COMMENT_FORM' ) ) {
201 return;
202 }
203 ( '2' === $siteguard_config->get( 'captcha_comment' ) ) ? $this->captcha->set_lang_mode( 'en' ) : $this->captcha->set_lang_mode( 'jp' );
204 $this->put_captcha();
205 define( 'SITEGUARD_PUT_COMMENT_FORM', '1' );
206 }
207
208 function handler_lostpassword_form() {
209 global $siteguard_config;
210 ( '2' === $siteguard_config->get( 'captcha_lostpasswd' ) ) ? $this->captcha->set_lang_mode( 'en' ) : $this->captcha->set_lang_mode( 'jp' );
211 $this->put_captcha();
212 }
213
214 function handler_register_form() {
215 global $siteguard_config;
216 ( '2' == $siteguard_config->get( 'captcha_registuser' ) ) ? $this->captcha->set_lang_mode( 'en' ) : $this->captcha->set_lang_mode( 'jp' );
217 $this->put_captcha();
218 }
219
220 function handler_wp_authenticate_user( $user, $password ) {
221 if ( array_key_exists( 'siteguard_captcha', $_POST ) && array_key_exists( 'siteguard_captcha_prefix', $_POST ) ) {
222 if ( $this->check_captcha_with_cache() ) {
223 return $user;
224 }
225 }
226 $error = new WP_Error();
227 $error->add( 'siteguard-captcha-error', esc_html__( 'ERROR: Invalid CAPTCHA.', 'siteguard' ) );
228 return $error;
229 }
230
231 function add_captcha_error() {
232 return new WP_Error( 'siteguard-captcha-error', esc_html__( 'ERROR: Invalid CAPTCHA.', 'siteguard' ) );
233 }
234
235 function handler_lostpassword_post() {
236 if ( array_key_exists( 'siteguard_captcha', $_POST ) && array_key_exists( 'siteguard_captcha_prefix', $_POST ) ) {
237 if ( $this->check_captcha_with_cache() ) {
238 return;
239 }
240 }
241 add_filter( 'allow_password_reset', array( $this, 'add_captcha_error' ) );
242 }
243
244 function handler_registration_errors( $errors, $sanitized_user_login, $user_email ) {
245 if ( array_key_exists( 'siteguard_captcha', $_POST ) && array_key_exists( 'siteguard_captcha_prefix', $_POST ) ) {
246 if ( $this->check_captcha_with_cache() ) {
247 return $errors;
248 }
249 }
250 $new_errors = new WP_Error();
251 $new_errors->add( 'siteguard-captcha-error', esc_html__( 'ERROR: Invalid CAPTCHA.', 'siteguard' ) );
252 return $new_errors;
253 }
254
255 function handler_process_comment_post( $comment ) {
256 if ( is_admin() ) {
257 return $comment;
258 }
259 if ( array_key_exists( 'siteguard_captcha', $_POST ) && array_key_exists( 'siteguard_captcha_prefix', $_POST ) ) {
260 if ( ! empty( $_POST['siteguard_captcha'] ) ) {
261 if ( $this->check_captcha_with_cache( ) ) {
262 return $comment;
263 }
264 }
265 }
266 wp_die( esc_html__( 'ERROR: Invalid CAPTCHA.', 'siteguard' ), esc_html( 'ERROR'), array( 'back_link' => true ) );
267 }
268 public function comment_captcha_reload_script( ) {
269 if ( is_singular() && comments_open() ) {
270 ?>
271 <script>
272 window.addEventListener('pageshow', function(event) {
273 var isBackForward = false;
274 if (window.performance && typeof performance.getEntriesByType === 'function') {
275 var perfEntries = performance.getEntriesByType('navigation');
276 isBackForward = perfEntries.length > 0 && perfEntries[0].type === 'back_forward';
277 }
278 if (event.persisted || isBackForward) {
279 window.location.reload();
280 }
281 });
282 </script>
283 <?php
284 }
285 }
286 }
287