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