PluginProbe ʕ •ᴥ•ʔ
SiteGuard WP Plugin / 1.2.1
SiteGuard WP Plugin v1.2.1
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 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
220 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 = $this->check_extensions( );
47 if ( is_wp_error( $error ) ) {
48 return $error;
49 }
50 $error = $this->check_image_access( );
51 if ( is_wp_error( $error ) ) {
52 return $error;
53 }
54 return true;
55 }
56 function check_extensions( ) {
57 $error_extensions = array();
58 $extensions = array(
59 'mbstring',
60 'gd',
61 );
62 foreach ( $extensions as $extension ) {
63 if ( ! extension_loaded( $extension ) ) {
64 $error_extensions[] = $extension;
65 }
66 }
67 if ( empty( $error_extensions ) ) {
68 return true;
69 }
70
71 $message = esc_html__( 'In order to enable this function, it is necessary to install expanded modules', 'siteguard' );
72 $message .= ' ( ';
73 $count = 0;
74 foreach ( $error_extensions as $extension ) {
75 if ( 0 != $count ) {
76 $message .= ', ';
77 }
78 $message .= $extension;
79 $count ++;
80 }
81 $message .= ' ) ';
82 $message .= esc_html__( 'in the server.', 'siteguard' );
83
84 $error = new WP_Error( 'siteguard_captcha', $message );
85 return $error;
86 }
87 function check_image_access( ) {
88 if ( is_object( $this->captcha ) ) {
89 $this->captcha->make_tmp_dir( );
90 } else {
91 $captcha = new SiteGuardReallySimpleCaptcha( );
92 $captcha->make_tmp_dir( );
93 }
94 $result = wp_remote_get( SITEGUARD_URL_PATH . 'really-simple-captcha/tmp/dummy.png' );
95 if ( ! is_wp_error( $result ) && $result['response']['code'] === 200 ) {
96 return true;
97 }
98 $message = esc_html__( 'In order to enable this function, it is necessary to specify Limit to AllowOverride in httpd.conf.', 'siteguard' );
99 $error = new WP_Error( 'siteguard_captcha', $message );
100 return $error;
101 }
102 function handler_login_errors( $error ) {
103 if ( strlen( $error ) > 0 && false === strpos( $error, esc_html__( 'ERROR: LOGIN LOCKED', 'siteguard' ) ) && false === strpos( $error, esc_html__( 'ERROR: Please login entry again', 'siteguard' ) ) ) {
104 $error = esc_html__( 'ERROR: Please check the input and resend.', 'siteguard' );
105 }
106 return $error;
107 }
108 function handler_shake_error_codes( $shake_error_codes ) {
109 array_push( $shake_error_codes, 'siteguard-captcha-error' );
110 return $shake_error_codes;
111 }
112
113 function init( ) {
114 global $config;
115 $errors = $this->check_requirements( );
116 if ( ! is_wp_error( $errors ) ) {
117 $switch = '1';
118 } else {
119 $switch = '0';
120 }
121 $config->set( 'captcha_enable', $switch );
122 $language = get_bloginfo( 'language' );
123 if ( 'ja' == $language ) {
124 $mode = '1'; // hiragana
125 } else {
126 $mode = '2'; // alphanumeric
127 }
128 $config->set( 'captcha_login', $mode );
129 $config->set( 'captcha_comment', $mode );
130 $config->set( 'captcha_lostpasswd', $mode );
131 $config->set( 'captcha_registuser', $mode );
132 $config->set( 'same_login_error', '1' );
133 $config->update( );
134 }
135 function get_captcha( ) {
136 $result = '<p>';
137 $result .= '<img src="'. SITEGUARD_URL_PATH . 'really-simple-captcha/tmp/' . $this->prefix . '.png" alt="CAPTCHA">';
138 $result .= '</p><p>';
139 $result .= '<label for="siteguard_captcha">' . esc_html__( 'Please input characters displayed above.', 'siteguard' ) . '</label><br />';
140 $result .= '<input type="text" name="siteguard_captcha" id="siteguard_captcha" class="input" value="" size="10" aria-required="true" />';
141 $result .= '<input type="hidden" name="siteguard_captcha_prefix" id="siteguard_captcha_prefix" value="'.$this->prefix.'" />';
142 $result .= '</p>';
143
144 return $result;
145 }
146 function put_captcha( ) {
147 $this->word = $this->captcha->generate_random_word( );
148 $this->prefix = mt_rand( );
149 $this->captcha->generate_image( $this->prefix, $this->word );
150 echo $this->get_captcha( );
151 }
152 function handler_login_form( ) {
153 global $config;
154 ( '2' == $config->get( 'captcha_login' ) ) ? $this->captcha->lang_mode = 'en' : $this->captcha->lang_mode = 'jp';
155 $this->put_captcha( );
156 }
157 function handler_comment_form( $post_id ) {
158 global $config;
159 if ( defined( 'PUT_COMMENT_FORM' ) ) {
160 return;
161 }
162 ( '2' == $config->get( 'captcha_comment' ) ) ? $this->captcha->lang_mode = 'en' : $this->captcha->lang_mode = 'jp';
163 $this->put_captcha( );
164 define( 'PUT_COMMENT_FORM', '1' );
165 }
166 function handler_lostpassword_form( ) {
167 global $config;
168 ( '2' == $config->get( 'captcha_lostpasswd' ) ) ? $this->captcha->lang_mode = 'en' : $this->captcha->lang_mode = 'jp';
169 $this->put_captcha( );
170 }
171 function handler_register_form( ) {
172 global $config;
173 ( '2' == $config->get( 'captcha_registuser' ) ) ? $this->captcha->lang_mode = 'en' : $this->captcha->lang_mode = 'jp';
174 $this->put_captcha( );
175 }
176 function handler_wp_authenticate_user( $user, $password ) {
177 if ( array_key_exists( 'siteguard_captcha', $_POST ) && array_key_exists( 'siteguard_captcha_prefix', $_POST ) ) {
178 if ( $this->captcha->check( $_POST['siteguard_captcha_prefix'], $_POST['siteguard_captcha'] ) ) {
179 return $user;
180 }
181 }
182 $error = new WP_Error( );
183 $error->add( 'siteguard-captcha-error', esc_html__( 'ERROR: Invalid CAPTCHA.', 'siteguard' ) );
184 return $error;
185 }
186 function add_captcha_error( ) {
187 return new WP_Error( 'siteguard-captcha-error', esc_html__( 'ERROR: Invalid CAPTCHA.', 'siteguard' ) );
188 }
189 function handler_lostpassword_post( ) {
190 if ( array_key_exists( 'siteguard_captcha', $_POST ) && array_key_exists( 'siteguard_captcha_prefix', $_POST ) ) {
191 if ( $this->captcha->check( $_POST['siteguard_captcha_prefix'], $_POST['siteguard_captcha'] ) ) {
192 return;
193 }
194 }
195 add_filter( 'allow_password_reset', array( $this, 'add_captcha_error' ) );
196 }
197 function handler_registration_errors( $errors, $sanitized_user_login, $user_email ) {
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 $errors;
201 }
202 }
203 $new_errors = new WP_Error( );
204 $new_errors->add( 'siteguard-captcha-error', esc_html__( 'ERROR: Invalid CAPTCHA.', 'siteguard' ) );
205 return $new_errors;
206 }
207 function handler_process_comment_post( $comment ) {
208 if ( array_key_exists( 'siteguard_captcha', $_POST ) && array_key_exists( 'siteguard_captcha_prefix', $_POST ) ) {
209 if ( ! empty( $_POST['siteguard_captcha'] ) ) {
210 if ( $this->captcha->check( $_POST['siteguard_captcha_prefix'], $_POST['siteguard_captcha'] ) ) {
211 return $comment;
212 }
213 }
214 }
215 wp_die( esc_html__( 'ERROR: Invalid CAPTCHA.', 'siteguard' ) );
216 }
217 }
218
219 ?>
220