PluginProbe ʕ •ᴥ•ʔ
SiteGuard WP Plugin / 1.7.7
SiteGuard WP Plugin v1.7.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 3 years ago siteguard-base.php 3 years ago siteguard-captcha.php 3 years ago siteguard-config.php 3 years ago siteguard-disable-author-query.php 3 years ago siteguard-disable-pingback.php 3 years ago siteguard-disable-xmlrpc.php 3 years ago siteguard-htaccess.php 3 years ago siteguard-login-alert.php 3 years ago siteguard-login-history.php 3 years ago siteguard-login-lock.php 3 years ago siteguard-rename-login.php 2 years ago siteguard-updates-notify.php 3 years ago siteguard-waf-exclude-rule.php 3 years ago
siteguard-captcha.php
258 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 function __construct() {
11 global $siteguard_config;
12 if ( '1' == $siteguard_config->get( 'captcha_enable' ) && 'xmlrpc.php' != basename( $_SERVER['SCRIPT_NAME'] ) && ! is_admin() ) {
13 $this->captcha = new SiteGuardReallySimpleCaptcha();
14
15 add_filter( 'shake_error_codes', array( $this, 'handler_shake_error_codes' ) );
16
17 // for logiin
18 if ( '0' !== $siteguard_config->get( 'captcha_login' ) ) {
19 add_filter( 'login_form', array( $this, 'handler_login_form' ) );
20 add_filter( 'wp_authenticate_user', array( $this, 'handler_wp_authenticate_user' ), 1, 2 );
21 }
22 // for lost password
23 if ( '0' !== $siteguard_config->get( 'captcha_lostpasswd' ) ) {
24 add_filter( 'lostpassword_form', array( $this, 'handler_lostpassword_form' ) );
25 add_filter( 'lostpassword_post', array( $this, 'handler_lostpassword_post' ), 1 );
26 }
27 // for register user
28 if ( '0' !== $siteguard_config->get( 'captcha_registuser' ) ) {
29 add_filter( 'register_form', array( $this, 'handler_register_form' ) );
30 add_action( 'registration_errors', array( $this, 'handler_registration_errors' ), 10, 3 );
31 }
32 // for comment
33 if ( '0' !== $siteguard_config->get( 'captcha_comment' ) ) {
34 add_action( 'comment_form_after_fields', array( $this, 'handler_comment_form' ), 1 );
35 add_action( 'comment_form_logged_in_after', array( $this, 'handler_comment_form' ), 1 );
36 add_action( 'comment_form', array( $this, 'handler_comment_form' ) );
37 add_filter( 'preprocess_comment', array( $this, 'handler_process_comment_post' ) );
38 }
39 }
40 if ( '1' == $siteguard_config->get( 'same_login_error' ) ) {
41 add_filter( 'login_errors', array( $this, 'handler_login_errors' ) );
42 }
43 }
44 function check_requirements() {
45 $error = siteguard_check_multisite();
46 if ( is_wp_error( $error ) ) {
47 return $error;
48 }
49 $error = $this->check_extensions();
50 if ( is_wp_error( $error ) ) {
51 return $error;
52 }
53 $error = $this->check_image_access();
54 if ( is_wp_error( $error ) ) {
55 return $error;
56 }
57 $error = $this->check_support_freetype();
58 if ( is_wp_error( $error ) ) {
59 return $error;
60 }
61 $error = $this->check_htaccess();
62 if ( is_wp_error( $error ) ) {
63 return $error;
64 }
65 return true;
66 }
67 function check_htaccess() {
68 if ( false === SiteGuard_Htaccess::test_htaccess() ) {
69 $message = esc_html__( 'mod_rewrite of .htaccess can not be used', 'siteguard' );
70 $error = new WP_Error( 'siteguard_captcha', $message );
71 return $error;
72 }
73 return true;
74 }
75 function check_extensions() {
76 $error_extensions = array();
77 $extensions = array(
78 'mbstring',
79 'gd',
80 );
81 foreach ( $extensions as $extension ) {
82 if ( ! extension_loaded( $extension ) ) {
83 $error_extensions[] = $extension;
84 }
85 }
86 if ( empty( $error_extensions ) ) {
87 return true;
88 }
89
90 $message = esc_html__( 'In order to enable this function, it is necessary to install expanded modules', 'siteguard' );
91 $message .= ' ( ';
92 $message .= implode( ', ', $error_extensions );
93 $message .= ' ) ';
94 $message .= esc_html__( 'in the server.', 'siteguard' );
95
96 $error = new WP_Error( 'siteguard_captcha', $message );
97 return $error;
98 }
99 function check_image_access() {
100 if ( is_object( $this->captcha ) ) {
101 $ret = $this->captcha->make_tmp_dir();
102 } else {
103 $captcha = new SiteGuardReallySimpleCaptcha();
104 $ret = $captcha->make_tmp_dir();
105 }
106 if ( false === $ret ) {
107 $message = esc_html__( 'The image file write failed.', 'siteguard' );
108 $error = new WP_Error( 'siteguard_captcha', $message );
109 return $error;
110 }
111
112 return true;
113 // $result = wp_remote_get( SITEGUARD_URL_PATH . 'really-simple-captcha/tmp/dummy.png' );
114 // if ( ! is_wp_error( $result ) && 200 === $result['response']['code'] ) {
115 // return true;
116 // }
117
118 // $message = esc_html__( 'The image file access failed.', 'siteguard' );
119 // if ( is_wp_error( $result ) ) {
120 // $error_detail = '( Error: ' . $result->get_error_message( ) . ' )';
121 // } else {
122 // $error_detail = '( ResponseCode: ' . $result['response']['code'] . ' )';
123 // }
124 // $error = new WP_Error( 'siteguard_captcha', $message . $error_detail);
125 // return $error;
126 }
127 function check_support_freetype() {
128 if ( function_exists( 'imagettftext' ) ) {
129 return true;
130 }
131 $message = esc_html__( 'In order to enable this function, php must be compiled with FreeType support enabled.', 'siteguard' );
132 $error = new WP_Error( 'siteguard_captcha', $message );
133 return $error;
134 }
135 function handler_login_errors( $error ) {
136 if ( strlen( $error ) > 0 && false === strpos( $error, esc_html__( 'ERROR: LOGIN LOCKED', 'siteguard' ) ) ) {
137 $error = esc_html__( 'ERROR: Please check the input and resend.', 'siteguard' );
138 }
139 return $error;
140 }
141 function handler_shake_error_codes( $shake_error_codes ) {
142 array_push( $shake_error_codes, 'siteguard-captcha-error' );
143 return $shake_error_codes;
144 }
145
146 function init() {
147 global $siteguard_config;
148 $errors = $this->check_requirements();
149 if ( ! is_wp_error( $errors ) ) {
150 $switch = '1';
151 } else {
152 $switch = '0';
153 }
154 $siteguard_config->set( 'captcha_enable', $switch );
155 $language = get_bloginfo( 'language' );
156 if ( 'ja' == $language ) {
157 $mode = '1'; // hiragana
158 } else {
159 $mode = '2'; // alphanumeric
160 }
161 $siteguard_config->set( 'captcha_login', $mode );
162 $siteguard_config->set( 'captcha_comment', $mode );
163 $siteguard_config->set( 'captcha_lostpasswd', $mode );
164 $siteguard_config->set( 'captcha_registuser', $mode );
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 function get_captcha() {
173 $result = '<p>';
174 $result .= '<img src="' . WP_CONTENT_URL . '/siteguard/' . $this->prefix . '.png" alt="CAPTCHA">';
175 $result .= '</p><p>';
176 $result .= '<label for="siteguard_captcha">' . esc_html__( 'Please input characters displayed above.', 'siteguard' ) . '</label><br />';
177 $result .= '<input type="text" name="siteguard_captcha" id="siteguard_captcha" class="input" value="" size="10" aria-required="true" />';
178 $result .= '<input type="hidden" name="siteguard_captcha_prefix" id="siteguard_captcha_prefix" value="' . $this->prefix . '" />';
179 $result .= '</p>';
180
181 return $result;
182 }
183 function put_captcha() {
184 $this->word = $this->captcha->generate_random_word();
185 $this->prefix = siteguard_rand();
186 $this->captcha->generate_image( $this->prefix, $this->word );
187 echo $this->get_captcha();
188 }
189 function handler_login_form() {
190 global $siteguard_config;
191 ( '2' === $siteguard_config->get( 'captcha_login' ) ) ? $this->captcha->set_lang_mode( 'en' ) : $this->captcha->set_lang_mode( 'jp' );
192 $this->put_captcha();
193 }
194 function handler_comment_form( $post_id ) {
195 global $siteguard_config;
196 if ( defined( 'SITEGUARD_PUT_COMMENT_FORM' ) ) {
197 return;
198 }
199 ( '2' === $siteguard_config->get( 'captcha_comment' ) ) ? $this->captcha->set_lang_mode( 'en' ) : $this->captcha->set_lang_mode( 'jp' );
200 $this->put_captcha();
201 define( 'SITEGUARD_PUT_COMMENT_FORM', '1' );
202 }
203 function handler_lostpassword_form() {
204 global $siteguard_config;
205 ( '2' === $siteguard_config->get( 'captcha_lostpasswd' ) ) ? $this->captcha->set_lang_mode( 'en' ) : $this->captcha->set_lang_mode( 'jp' );
206 $this->put_captcha();
207 }
208 function handler_register_form() {
209 global $siteguard_config;
210 ( '2' == $siteguard_config->get( 'captcha_registuser' ) ) ? $this->captcha->set_lang_mode( 'en' ) : $this->captcha->set_lang_mode( 'jp' );
211 $this->put_captcha();
212 }
213 function handler_wp_authenticate_user( $user, $password ) {
214 if ( array_key_exists( 'siteguard_captcha', $_POST ) && array_key_exists( 'siteguard_captcha_prefix', $_POST ) ) {
215 if ( $this->captcha->check( $_POST['siteguard_captcha_prefix'], $_POST['siteguard_captcha'], false ) ) {
216 return $user;
217 }
218 }
219 $error = new WP_Error();
220 $error->add( 'siteguard-captcha-error', esc_html__( 'ERROR: Invalid CAPTCHA.', 'siteguard' ) );
221 return $error;
222 }
223 function add_captcha_error() {
224 return new WP_Error( 'siteguard-captcha-error', esc_html__( 'ERROR: Invalid CAPTCHA.', 'siteguard' ) );
225 }
226 function handler_lostpassword_post() {
227 if ( array_key_exists( 'siteguard_captcha', $_POST ) && array_key_exists( 'siteguard_captcha_prefix', $_POST ) ) {
228 if ( $this->captcha->check( $_POST['siteguard_captcha_prefix'], $_POST['siteguard_captcha'], false ) ) {
229 return;
230 }
231 }
232 add_filter( 'allow_password_reset', array( $this, 'add_captcha_error' ) );
233 }
234 function handler_registration_errors( $errors, $sanitized_user_login, $user_email ) {
235 if ( array_key_exists( 'siteguard_captcha', $_POST ) && array_key_exists( 'siteguard_captcha_prefix', $_POST ) ) {
236 if ( $this->captcha->check( $_POST['siteguard_captcha_prefix'], $_POST['siteguard_captcha'], false ) ) {
237 return $errors;
238 }
239 }
240 $new_errors = new WP_Error();
241 $new_errors->add( 'siteguard-captcha-error', esc_html__( 'ERROR: Invalid CAPTCHA.', 'siteguard' ) );
242 return $new_errors;
243 }
244 function handler_process_comment_post( $comment ) {
245 if ( is_admin() ) {
246 return $comment;
247 }
248 if ( array_key_exists( 'siteguard_captcha', $_POST ) && array_key_exists( 'siteguard_captcha_prefix', $_POST ) ) {
249 if ( ! empty( $_POST['siteguard_captcha'] ) ) {
250 if ( $this->captcha->check( $_POST['siteguard_captcha_prefix'], $_POST['siteguard_captcha'], false ) ) {
251 return $comment;
252 }
253 }
254 }
255 wp_die( esc_html__( 'ERROR: Invalid CAPTCHA.', 'siteguard' ) );
256 }
257 }
258