google-captcha
Last commit date
bws_menu
7 years ago
css
7 years ago
includes
7 years ago
js
7 years ago
languages
7 years ago
google-captcha.php
7 years ago
readme.txt
7 years ago
screenshot-1.png
7 years ago
screenshot-2.png
7 years ago
screenshot-3.png
7 years ago
screenshot-4.png
7 years ago
screenshot-5.png
7 years ago
screenshot-6.png
7 years ago
screenshot-7.png
7 years ago
screenshot-8.png
7 years ago
google-captcha.php
1211 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Google Captcha (reCAPTCHA) by BestWebSoft |
| 4 | Plugin URI: https://bestwebsoft.com/products/wordpress/plugins/google-captcha/ |
| 5 | Description: Protect WordPress website forms from spam entries with Google Captcha (reCaptcha). |
| 6 | Author: BestWebSoft |
| 7 | Text Domain: google-captcha |
| 8 | Domain Path: /languages |
| 9 | Version: 1.42 |
| 10 | Author URI: https://bestwebsoft.com/ |
| 11 | License: GPLv3 or later |
| 12 | */ |
| 13 | |
| 14 | /* © Copyright 2019 BestWebSoft ( https://support.bestwebsoft.com ) |
| 15 | |
| 16 | This program is free software; you can redistribute it and/or modify |
| 17 | it under the terms of the GNU General Public License, version 2, as |
| 18 | published by the Free Software Foundation. |
| 19 | |
| 20 | This program is distributed in the hope that it will be useful, |
| 21 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | GNU General Public License for more details. |
| 24 | |
| 25 | You should have received a copy of the GNU General Public License |
| 26 | along with this program; if not, write to the Free Software |
| 27 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 28 | */ |
| 29 | |
| 30 | require_once( dirname( __FILE__ ) . '/includes/forms.php' ); |
| 31 | |
| 32 | /* Add menu page */ |
| 33 | if ( ! function_exists( 'gglcptch_admin_menu' ) ) { |
| 34 | function gglcptch_admin_menu() { |
| 35 | global $submenu, $wp_version, $gglcptch_plugin_info; |
| 36 | |
| 37 | if ( ! is_plugin_active( 'google-captcha-pro/google-captcha-pro.php' ) ) { |
| 38 | $settings_page = add_menu_page( __( 'Google Captcha Settings', 'google-captcha' ), 'Google Captcha', 'manage_options', 'google-captcha.php', 'gglcptch_add_settings_page', 'none' ); |
| 39 | |
| 40 | add_submenu_page( 'google-captcha.php', __( 'Google Captcha Settings', 'google-captcha'), __( 'Settings', 'google-captcha' ), 'manage_options', 'google-captcha.php', 'gglcptch_add_settings_page' ); |
| 41 | |
| 42 | $whitelist_page = add_submenu_page( 'google-captcha.php', __( 'Google Captcha Whitelist', 'google-captcha' ), __( 'Whitelist', 'google-captcha' ), 'manage_options', 'google-captcha-whitelist.php', 'gglcptch_add_settings_page' ); |
| 43 | |
| 44 | add_submenu_page( 'google-captcha.php', 'BWS Panel', 'BWS Panel', 'manage_options', 'gglcptch-bws-panel', 'bws_add_menu_render' ); |
| 45 | |
| 46 | if ( isset($submenu['google-captcha.php'] ) ) { |
| 47 | $submenu['google-captcha.php'][] = array( |
| 48 | '<span style="color:#d86463"> ' . __('Upgrade to Pro', 'google-captcha' ) . '</span>', |
| 49 | 'manage_options', |
| 50 | 'https://bestwebsoft.com/products/wordpress/plugins/google-captcha/?k=b850d949ccc1239cab0da315c3c822ab&pn=109&v=' . $gglcptch_plugin_info["Version"] . '&wp_v=' . $wp_version ); |
| 51 | } |
| 52 | |
| 53 | add_action( "load-{$settings_page}", 'gglcptch_add_tabs' ); |
| 54 | add_action( "load-{$whitelist_page}", 'gglcptch_add_tabs' ); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if ( ! function_exists( 'gglcptch_plugins_loaded' ) ) { |
| 60 | function gglcptch_plugins_loaded() { |
| 61 | /* Internationalization, first(!) */ |
| 62 | load_plugin_textdomain( 'google-captcha', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if ( ! function_exists( 'gglcptch_init' ) ) { |
| 67 | function gglcptch_init() { |
| 68 | global $gglcptch_plugin_info, $gglcptch_options; |
| 69 | |
| 70 | require_once( dirname( __FILE__ ) . '/bws_menu/bws_include.php' ); |
| 71 | bws_include_init( plugin_basename( __FILE__ ) ); |
| 72 | |
| 73 | if ( empty( $gglcptch_plugin_info ) ) { |
| 74 | if ( ! function_exists( 'get_plugin_data' ) ) { |
| 75 | require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 76 | } |
| 77 | $gglcptch_plugin_info = get_plugin_data( __FILE__ ); |
| 78 | } |
| 79 | |
| 80 | /* Function check if plugin is compatible with current WP version */ |
| 81 | bws_wp_min_version_check( plugin_basename( __FILE__ ), $gglcptch_plugin_info, '3.9' ); |
| 82 | |
| 83 | $is_admin = is_admin() && ! defined( 'DOING_AJAX' ); |
| 84 | /* Call register settings function */ |
| 85 | if ( ! $is_admin || ( isset( $_GET['page'] ) && 'google-captcha.php' == $_GET['page'] ) ) { |
| 86 | register_gglcptch_settings(); |
| 87 | } |
| 88 | |
| 89 | /* Add hooks */ |
| 90 | if ( ! $is_admin && ! empty( $gglcptch_options['public_key'] ) && ! empty( $gglcptch_options['private_key'] ) ) { |
| 91 | gglcptch_add_actions(); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Activation plugin function |
| 98 | */ |
| 99 | if ( ! function_exists( 'gglcptch_plugin_activate' ) ) { |
| 100 | function gglcptch_plugin_activate( $networkwide ) { |
| 101 | /* Activation function for network, check if it is a network activation - if so, run the activation function for each blog id */ |
| 102 | if ( is_multisite() ) { |
| 103 | switch_to_blog( 1 ); |
| 104 | register_uninstall_hook( __FILE__, 'gglcptch_delete_options' ); |
| 105 | restore_current_blog(); |
| 106 | } else { |
| 107 | register_uninstall_hook( __FILE__, 'gglcptch_delete_options' ); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if ( ! function_exists( 'gglcptch_admin_init' ) ) { |
| 113 | function gglcptch_admin_init() { |
| 114 | global $bws_plugin_info, $gglcptch_plugin_info; |
| 115 | |
| 116 | if ( empty( $bws_plugin_info ) ) { |
| 117 | $bws_plugin_info = array( 'id' => '109', 'version' => $gglcptch_plugin_info["Version"] ); |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /* Add google captcha styles */ |
| 124 | if ( ! function_exists( 'gglcptch_add_admin_script_styles' ) ) { |
| 125 | function gglcptch_add_admin_script_styles() { |
| 126 | global $gglcptch_plugin_info; |
| 127 | |
| 128 | /* css for displaing an icon */ |
| 129 | wp_enqueue_style( 'gglcptch_admin_page_stylesheet', plugins_url( 'css/admin_page.css', __FILE__ ) ); |
| 130 | |
| 131 | if ( isset( $_REQUEST['page'] ) && ( 'google-captcha.php' == $_REQUEST['page'] || 'google-captcha-whitelist.php' == $_REQUEST['page'] ) ) { |
| 132 | wp_enqueue_style( 'gglcptch_stylesheet', plugins_url( 'css/style.css', __FILE__ ), array(), $gglcptch_plugin_info['Version'] ); |
| 133 | wp_enqueue_script( 'gglcptch_admin_script', plugins_url( 'js/admin_script.js', __FILE__ ), array( 'jquery', 'jquery-ui-accordion' ), $gglcptch_plugin_info['Version'] ); |
| 134 | |
| 135 | bws_enqueue_settings_scripts(); |
| 136 | bws_plugins_include_codemirror(); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /* Add google captcha admin styles for test key */ |
| 142 | if ( ! function_exists( 'gglcptch_admin_footer' ) ) { |
| 143 | function gglcptch_admin_footer() { |
| 144 | global $gglcptch_plugin_info, $gglcptch_options; |
| 145 | if ( isset( $_REQUEST['page'] ) && 'google-captcha.php' == $_REQUEST['page'] ) { |
| 146 | |
| 147 | /* update $gglcptch_options */ |
| 148 | register_gglcptch_settings(); |
| 149 | |
| 150 | $api_url = gglcptch_get_api_url(); |
| 151 | |
| 152 | /* for gglcptch test key */ |
| 153 | if ( isset( $gglcptch_options['recaptcha_version'] ) && in_array( $gglcptch_options['recaptcha_version'], array( 'v2', 'invisible' ) ) ) { |
| 154 | $deps = ( ! empty( $gglcptch_options['disable_submit'] ) ) ? array( 'gglcptch_pre_api' ) : array( 'jquery' ); |
| 155 | } else { |
| 156 | $deps = array(); |
| 157 | } |
| 158 | wp_register_script( 'gglcptch_api', $api_url,$deps, $gglcptch_plugin_info['Version'], true ); |
| 159 | gglcptch_add_scripts(); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Remove dublicate scripts |
| 166 | */ |
| 167 | if ( ! function_exists( 'gglcptch_remove_dublicate_scripts' ) ) { |
| 168 | function gglcptch_remove_dublicate_scripts() { |
| 169 | global $wp_scripts; |
| 170 | |
| 171 | if ( ! is_object( $wp_scripts ) || empty( $wp_scripts ) ) { |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | foreach ( $wp_scripts->registered as $script_name => $args ) { |
| 176 | if ( preg_match( "|google\.com/recaptcha/api\.js|", $args->src ) && 'gglcptch_api' != $script_name ) { |
| 177 | /* remove a previously enqueued script */ |
| 178 | wp_dequeue_script( $script_name ); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Add google captcha styles |
| 186 | */ |
| 187 | if ( ! function_exists( 'gglcptch_add_styles' ) ) { |
| 188 | function gglcptch_add_styles() { |
| 189 | global $gglcptch_plugin_info, $gglcptch_options; |
| 190 | wp_enqueue_style( 'gglcptch', plugins_url( 'css/gglcptch.css', __FILE__ ), array(), $gglcptch_plugin_info["Version"] ); |
| 191 | |
| 192 | if ( defined( 'BWS_ENQUEUE_ALL_SCRIPTS' ) && BWS_ENQUEUE_ALL_SCRIPTS ) { |
| 193 | if ( ! wp_script_is( 'gglcptch_api', 'registered' ) ) { |
| 194 | $api_url = gglcptch_get_api_url(); |
| 195 | if ( isset( $gglcptch_options['recaptcha_version'] ) && in_array( $gglcptch_options['recaptcha_version'], array( 'v2', 'invisible' ) ) ) { |
| 196 | $deps = ( ! empty( $gglcptch_options['disable_submit'] ) ) ? array( 'gglcptch_pre_api' ) : array( 'jquery' ); |
| 197 | } else { |
| 198 | $deps = array(); |
| 199 | } |
| 200 | |
| 201 | wp_register_script( 'gglcptch_api', $api_url,$deps, $gglcptch_plugin_info['Version'], true ); |
| 202 | |
| 203 | add_action( 'wp_footer', 'gglcptch_add_scripts' ); |
| 204 | if ( |
| 205 | '1' == $gglcptch_options['login_form'] || |
| 206 | '1' == $gglcptch_options['reset_pwd_form'] || |
| 207 | '1' == $gglcptch_options['registration_form'] |
| 208 | ) { |
| 209 | add_action('login_footer', 'gglcptch_add_scripts'); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Add google captcha js scripts |
| 218 | */ |
| 219 | if ( ! function_exists( 'gglcptch_add_scripts' ) ) { |
| 220 | function gglcptch_add_scripts() { |
| 221 | global $gglcptch_options, $gglcptch_plugin_info; |
| 222 | |
| 223 | if ( empty( $gglcptch_options ) ) { |
| 224 | register_gglcptch_settings(); |
| 225 | } |
| 226 | |
| 227 | if ( isset( $gglcptch_options['recaptcha_version'] ) && 'v1' != $gglcptch_options['recaptcha_version'] ) { |
| 228 | gglcptch_remove_dublicate_scripts(); |
| 229 | if ( ! empty( $gglcptch_options['disable_submit'] ) ) { |
| 230 | wp_enqueue_script( 'gglcptch_pre_api', plugins_url( 'js/pre-api-script.js', __FILE__ ), array( 'jquery'), $gglcptch_plugin_info['Version'], true ); |
| 231 | wp_localize_script( 'gglcptch_pre_api', 'gglcptch_pre', array( |
| 232 | 'messages' => array( |
| 233 | 'in_progress' => __( 'Please wait until Google reCAPTCHA is loaded.', 'google-captcha' ), |
| 234 | 'timeout' => __( 'Failed to load Google reCAPTCHA. Please check your internet connection and reload this page.', 'google-captcha' ) |
| 235 | ), |
| 236 | 'custom_callback' => apply_filters( 'gglcptch_custom_callback', '' ) |
| 237 | ) ); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | wp_enqueue_script( 'gglcptch_script', plugins_url( 'js/script.js', __FILE__ ), array( 'jquery', 'gglcptch_api' ), $gglcptch_plugin_info["Version"], true ); |
| 242 | |
| 243 | $version = $gglcptch_options['recaptcha_version'] == 'v2' ? '_v2' : ''; |
| 244 | |
| 245 | wp_localize_script( 'gglcptch_script', 'gglcptch', array( |
| 246 | 'options' => array( |
| 247 | 'version' => $gglcptch_options['recaptcha_version'], |
| 248 | 'sitekey' => $gglcptch_options['public_key'], |
| 249 | 'theme' => $gglcptch_options[ 'theme' . $version ], |
| 250 | 'error' => sprintf( '<strong>%s</strong>: %s', __( 'Warning', 'google-captcha' ), gglcptch_get_message( 'multiple_blocks' ) ) |
| 251 | ), |
| 252 | 'vars' => array( |
| 253 | 'visibility' => ( 'login_footer' == current_filter() ) |
| 254 | ) |
| 255 | ) ); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | if ( ! function_exists( 'gglcptch_pagination_callback' ) ) { |
| 260 | function gglcptch_pagination_callback( $content ) { |
| 261 | $content .= "if ( typeof Recaptcha != 'undefined' || typeof grecaptcha != 'undefined' ) { gglcptch.prepare(); }"; |
| 262 | return $content; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Add the "async" attribute to our registered script. |
| 268 | */ |
| 269 | if ( ! function_exists( 'gglcptch_add_async_attribute' ) ) { |
| 270 | function gglcptch_add_async_attribute( $tag, $handle ) { |
| 271 | if ( 'gglcptch_api' == $handle ) { |
| 272 | $tag = str_replace( ' src', ' data-cfasync="false" async="async" defer="defer" src', $tag ); |
| 273 | } |
| 274 | return $tag; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if ( ! function_exists( 'gglcptch_create_table' ) ) { |
| 279 | function gglcptch_create_table() { |
| 280 | global $wpdb; |
| 281 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 282 | $whitelist_exist = $wpdb->query( "SHOW TABLES LIKE '{$wpdb->prefix}gglcptch_whitelist';" ); |
| 283 | if ( ! $whitelist_exist ) { |
| 284 | $sql = "CREATE TABLE `{$wpdb->prefix}gglcptch_whitelist` ( |
| 285 | `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, |
| 286 | `ip` CHAR(31) NOT NULL, |
| 287 | `ip_from_int` BIGINT, |
| 288 | `ip_to_int` BIGINT, |
| 289 | `add_time` DATETIME, |
| 290 | PRIMARY KEY (`id`) |
| 291 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; |
| 292 | dbDelta( $sql ); |
| 293 | |
| 294 | /* add unique key */ |
| 295 | if ( 0 == $wpdb->query( "SHOW KEYS FROM `{$wpdb->prefix}gglcptch_whitelist` WHERE Key_name='ip'" ) ) { |
| 296 | $wpdb->query( "ALTER TABLE `{$wpdb->prefix}gglcptch_whitelist` ADD UNIQUE(`ip`);" ); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /* Google catpcha settings */ |
| 303 | if ( ! function_exists( 'register_gglcptch_settings' ) ) { |
| 304 | function register_gglcptch_settings() { |
| 305 | global $gglcptch_options, $bws_plugin_info, $gglcptch_plugin_info; |
| 306 | |
| 307 | $plugin_db_version = '0.1'; |
| 308 | |
| 309 | /* Install the option defaults */ |
| 310 | if ( ! get_option( 'gglcptch_options' ) ) { |
| 311 | add_option( 'gglcptch_options', gglcptch_get_default_options() ); |
| 312 | } |
| 313 | /* Get options from the database */ |
| 314 | $gglcptch_options = get_option( 'gglcptch_options' ); |
| 315 | |
| 316 | /* Array merge incase this version has added new options */ |
| 317 | if ( ! isset( $gglcptch_options['plugin_option_version'] ) || $gglcptch_options['plugin_option_version'] != $gglcptch_plugin_info["Version"] ) { |
| 318 | $gglcptch_options = array_merge( gglcptch_get_default_options(), $gglcptch_options ); |
| 319 | $gglcptch_options['plugin_option_version'] = $gglcptch_plugin_info["Version"]; |
| 320 | |
| 321 | if ( is_multisite() ) { |
| 322 | switch_to_blog( 1 ); |
| 323 | register_uninstall_hook( __FILE__, 'gglcptch_delete_options' ); |
| 324 | restore_current_blog(); |
| 325 | } else { |
| 326 | register_uninstall_hook( __FILE__, 'gglcptch_delete_options' ); |
| 327 | } |
| 328 | update_option( 'gglcptch_options', $gglcptch_options ); |
| 329 | } |
| 330 | /* Update tables when update plugin and tables changes*/ |
| 331 | if ( |
| 332 | ! isset( $gglcptch_options['plugin_db_version'] ) || |
| 333 | ( isset( $gglcptch_options['plugin_db_version'] ) && $gglcptch_options['plugin_db_version'] != $plugin_db_version ) |
| 334 | ) { |
| 335 | if ( ! isset( $gglcptch_options['plugin_db_version'] ) ) { |
| 336 | gglcptch_create_table(); |
| 337 | } |
| 338 | $gglcptch_options['plugin_db_version'] = $plugin_db_version; |
| 339 | update_option( 'gglcptch_options', $gglcptch_options ); |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if ( ! function_exists( 'gglcptch_get_default_options' ) ) { |
| 345 | function gglcptch_get_default_options() { |
| 346 | global $gglcptch_plugin_info; |
| 347 | |
| 348 | $default_options = array( |
| 349 | 'whitelist_message' => __( 'You are in the whitelist', 'google-captcha' ), |
| 350 | 'public_key' => '', |
| 351 | 'private_key' => '', |
| 352 | 'login_form' => 0, |
| 353 | 'registration_form' => 0, |
| 354 | 'reset_pwd_form' => 1, |
| 355 | 'comments_form' => 0, |
| 356 | 'contact_form' => 0, |
| 357 | 'testimonials' => 0, |
| 358 | 'theme' => 'red', |
| 359 | 'theme_v2' => 'light', |
| 360 | 'recaptcha_version' => 'v2', |
| 361 | 'plugin_option_version' => $gglcptch_plugin_info["Version"], |
| 362 | 'first_install' => strtotime( "now" ), |
| 363 | 'display_settings_notice' => 1, |
| 364 | 'suggest_feature_banner' => 1, |
| 365 | 'score_v3' => 0.5 |
| 366 | ); |
| 367 | |
| 368 | if ( function_exists( 'get_editable_roles' ) ) { |
| 369 | foreach ( get_editable_roles() as $role => $fields ) { |
| 370 | $default_options[ $role ] = '0'; |
| 371 | } |
| 372 | } |
| 373 | return $default_options; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | if ( ! function_exists( 'gglcptch_plugin_status' ) ) { |
| 378 | function gglcptch_plugin_status( $plugins, $all_plugins, $is_network ) { |
| 379 | $result = array( |
| 380 | 'status' => '', |
| 381 | 'plugin' => '', |
| 382 | 'plugin_info' => array(), |
| 383 | ); |
| 384 | foreach ( ( array )$plugins as $plugin ) { |
| 385 | if ( array_key_exists( $plugin, $all_plugins ) ) { |
| 386 | if ( |
| 387 | ( $is_network && is_plugin_active_for_network( $plugin ) ) || |
| 388 | ( ! $is_network && is_plugin_active( $plugin ) ) |
| 389 | ) { |
| 390 | $result['status'] = 'activated'; |
| 391 | $result['plugin'] = $plugin; |
| 392 | $result['plugin_info'] = $all_plugins[ $plugin ]; |
| 393 | break; |
| 394 | } else { |
| 395 | $result['status'] = 'deactivated'; |
| 396 | $result['plugin'] = $plugin; |
| 397 | $result['plugin_info'] = $all_plugins[ $plugin ]; |
| 398 | } |
| 399 | |
| 400 | } |
| 401 | } |
| 402 | if ( empty( $result['status'] ) ) { |
| 403 | $result['status'] = 'not_installed'; |
| 404 | } |
| 405 | return $result; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | if ( ! function_exists( 'gglcptch_whitelisted_ip' ) ) { |
| 410 | function gglcptch_whitelisted_ip() { |
| 411 | global $wpdb, $gglcptch_options; |
| 412 | $checked = false; |
| 413 | if ( empty( $gglcptch_options ) ) { |
| 414 | $gglcptch_options = get_option( 'gglcptch_options' ); |
| 415 | } |
| 416 | $whitelist_exist = $wpdb->query( "SHOW TABLES LIKE '{$wpdb->prefix}gglcptch_whitelist'" ); |
| 417 | if ( 1 === $whitelist_exist ) { |
| 418 | $ip = gglcptch_get_ip(); |
| 419 | |
| 420 | if ( ! empty( $ip ) ) { |
| 421 | $ip_int = sprintf( '%u', ip2long( $ip ) ); |
| 422 | $result = $wpdb->get_var( |
| 423 | "SELECT `id` |
| 424 | FROM `{$wpdb->prefix}gglcptch_whitelist` |
| 425 | WHERE ( `ip_from_int` <= {$ip_int} AND `ip_to_int` >= {$ip_int} ) OR `ip` LIKE '{$ip}' LIMIT 1;" |
| 426 | ); |
| 427 | $checked = is_null( $result ) || ! $result ? false : true; |
| 428 | } else { |
| 429 | $checked = false; |
| 430 | } |
| 431 | } |
| 432 | return $checked; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /* Display settings page */ |
| 437 | if ( ! function_exists( 'gglcptch_add_settings_page' ) ) { |
| 438 | function gglcptch_add_settings_page() { |
| 439 | global $gglcptch_plugin_info; |
| 440 | require_once( dirname( __FILE__ ) . '/includes/pro_banners.php' ); ?> |
| 441 | <div class="wrap"> |
| 442 | <?php if ( 'google-captcha.php' == $_GET['page'] ) { |
| 443 | require_once( dirname( __FILE__ ) . '/includes/class-gglcptch-settings-tabs.php' ); |
| 444 | $page = new Gglcptch_Settings_Tabs( plugin_basename( __FILE__ ) ); ?> |
| 445 | <h1><?php _e( 'Google Captcha Settings', 'google-captcha' ); ?></h1> |
| 446 | <noscript><div class="error below-h2"><p><strong><?php _e( "Please enable JavaScript in your browser.", 'google-captcha' ); ?></strong></p></div></noscript> |
| 447 | <?php $page->display_content(); |
| 448 | } else { |
| 449 | require_once( dirname( __FILE__ ) . '/includes/whitelist.php' ); |
| 450 | $page = new Gglcptch_Whitelist( plugin_basename( __FILE__ ) ); |
| 451 | if ( is_object( $page ) ) { |
| 452 | $page->display_content(); |
| 453 | } |
| 454 | |
| 455 | bws_plugin_reviews_block( $gglcptch_plugin_info['Name'], 'google-captcha' ); |
| 456 | } ?> |
| 457 | </div> |
| 458 | <?php } |
| 459 | } |
| 460 | |
| 461 | if ( ! function_exists( 'gglcptch_is_recaptcha_required' ) ) { |
| 462 | function gglcptch_is_recaptcha_required( $form_slug = '', $is_user_logged_in = null ) { |
| 463 | global $gglcptch_options; |
| 464 | |
| 465 | if ( is_null( $is_user_logged_in ) ) { |
| 466 | $is_user_logged_in = is_user_logged_in(); |
| 467 | } |
| 468 | |
| 469 | if ( empty( $gglcptch_options ) ) { |
| 470 | $gglcptch_options = get_option( 'gglcptch_options' ); |
| 471 | if ( empty( $gglcptch_options ) ) { |
| 472 | register_gglcptch_settings(); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | return |
| 477 | ! isset( $gglcptch_options[ $form_slug ] ) || |
| 478 | ( |
| 479 | ! empty( $gglcptch_options[ $form_slug ] ) && |
| 480 | ( ! $is_user_logged_in || ! gglcptch_is_hidden_for_role() ) |
| 481 | ); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /* Checking current user role */ |
| 486 | if ( ! function_exists( 'gglcptch_is_hidden_for_role' ) ) { |
| 487 | function gglcptch_is_hidden_for_role() { |
| 488 | global $current_user, $gglcptch_options; |
| 489 | |
| 490 | if ( ! is_user_logged_in() ) { |
| 491 | return false; |
| 492 | } |
| 493 | |
| 494 | if ( ! empty( $current_user->roles[0] ) ) { |
| 495 | $role = $current_user->roles[0]; |
| 496 | if ( empty( $gglcptch_options ) ) { |
| 497 | register_gglcptch_settings(); |
| 498 | } |
| 499 | return ! empty( $gglcptch_options[ $role ] ); |
| 500 | } else { |
| 501 | return false; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /* Display google captcha via shortcode */ |
| 507 | if ( ! function_exists( 'gglcptch_display' ) ) { |
| 508 | function gglcptch_display( $content = false ) { |
| 509 | global $gglcptch_options, $gglcptch_count, $gglcptch_plugin_info; |
| 510 | |
| 511 | if ( empty( $gglcptch_options ) ) { |
| 512 | register_gglcptch_settings(); |
| 513 | } |
| 514 | |
| 515 | if ( ! gglcptch_whitelisted_ip() || ( isset( $_GET['action'] ) && 'gglcptch-test-keys' == $_GET['action'] ) ) { |
| 516 | |
| 517 | if ( ! $gglcptch_count ) { |
| 518 | $gglcptch_count = 1; |
| 519 | } |
| 520 | |
| 521 | $publickey = $gglcptch_options['public_key']; |
| 522 | $privatekey = $gglcptch_options['private_key']; |
| 523 | |
| 524 | $content .= '<div class="gglcptch gglcptch_' . $gglcptch_options['recaptcha_version'] . '">'; |
| 525 | if ( ! $privatekey || ! $publickey ) { |
| 526 | if ( current_user_can( 'manage_options' ) ) { |
| 527 | $content .= sprintf( |
| 528 | '<strong>%s <a target="_blank" href="https://www.google.com/recaptcha/admin#list">%s</a> %s <a target="_blank" href="%s">%s</a>.</strong>', |
| 529 | __( 'To use Google Captcha you must get the keys from', 'google-captcha' ), |
| 530 | __( 'here', 'google-captcha' ), |
| 531 | __( 'and enter them on the', 'google-captcha' ), |
| 532 | admin_url( '/admin.php?page=google-captcha.php' ), |
| 533 | __( 'plugin setting page', 'google-captcha' ) |
| 534 | ); |
| 535 | } |
| 536 | $content .= '</div>'; |
| 537 | $gglcptch_count++; |
| 538 | return $content; |
| 539 | } |
| 540 | |
| 541 | $api_url = gglcptch_get_api_url(); |
| 542 | |
| 543 | /* generating random id value in case of getting content with pagination plugin for not getting duplicate id values */ |
| 544 | $id = mt_rand(); |
| 545 | if ( isset( $gglcptch_options['recaptcha_version'] ) && in_array( $gglcptch_options['recaptcha_version'], array( 'v2', 'invisible' ) ) ) { |
| 546 | $content .= '<div id="gglcptch_recaptcha_' . $id . '" class="gglcptch_recaptcha"></div> |
| 547 | <noscript> |
| 548 | <div style="width: 302px;"> |
| 549 | <div style="width: 302px; height: 422px; position: relative;"> |
| 550 | <div style="width: 302px; height: 422px; position: absolute;"> |
| 551 | <iframe src="https://www.google.com/recaptcha/api/fallback?k=' . $publickey . '" frameborder="0" scrolling="no" style="width: 302px; height:422px; border-style: none;"></iframe> |
| 552 | </div> |
| 553 | </div> |
| 554 | <div style="border-style: none; bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px; height: 60px; width: 300px;"> |
| 555 | <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px !important; height: 40px !important; border: 1px solid #c1c1c1 !important; margin: 10px 25px !important; padding: 0px !important; resize: none !important;"></textarea> |
| 556 | </div> |
| 557 | </div> |
| 558 | </noscript>'; |
| 559 | $deps = ( ! empty( $gglcptch_options['disable_submit'] ) ) ? array( 'gglcptch_pre_api' ) : array( 'jquery' ); |
| 560 | } elseif ( isset( $gglcptch_options['recaptcha_version'] ) && 'v3' == $gglcptch_options['recaptcha_version'] ) { |
| 561 | $action = 'BWS_reCaptcha'; |
| 562 | $content .= '<input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response"> ' ; |
| 563 | $content .= '<script src="' . $api_url . '"></script> |
| 564 | <script> |
| 565 | grecaptcha.ready(function() { |
| 566 | grecaptcha.execute(\''.$publickey.'\', {action: \''. $action. '\'}).then(function(token) { |
| 567 | document.getElementById(\'g-recaptcha-response\').value=token; |
| 568 | }); |
| 569 | }); |
| 570 | </script>'; |
| 571 | } else { |
| 572 | /** |
| 573 | * @deprecated |
| 574 | * @todo remove after 01.02.2019 |
| 575 | * Also need delete lib/recaptchalib.php' |
| 576 | */ |
| 577 | // require_once( 'lib/recaptchalib.php' ); |
| 578 | // $content .= '<div id="gglcptch_recaptcha_' . $id . '" class="gglcptch_recaptcha"></div>'; |
| 579 | // $content .= gglcptch_recaptcha_get_html( $publickey, null, is_ssl() ); |
| 580 | // $deps = array(); |
| 581 | /* @todo end */ |
| 582 | } |
| 583 | $content .= '</div>'; |
| 584 | $gglcptch_count++; |
| 585 | |
| 586 | /* register reCAPTCHA script */ |
| 587 | if ( ! wp_script_is( 'gglcptch_api', 'registered' ) ) { |
| 588 | if ( isset( $gglcptch_options['recaptcha_version'] ) && 'v3' == $gglcptch_options['recaptcha_version'] ) { |
| 589 | wp_register_script( 'gglcptch_api', $api_url,false, null, false ); |
| 590 | } else { |
| 591 | wp_register_script( 'gglcptch_api', $api_url, $deps, $gglcptch_plugin_info['Version'], true ); |
| 592 | } |
| 593 | add_action( 'wp_footer', 'gglcptch_add_scripts' ); |
| 594 | if ( |
| 595 | '1' == $gglcptch_options['login_form'] || |
| 596 | '1' == $gglcptch_options['reset_pwd_form'] || |
| 597 | '1' == $gglcptch_options['registration_form'] |
| 598 | ) { |
| 599 | add_action( 'login_footer', 'gglcptch_add_scripts' ); |
| 600 | } |
| 601 | } |
| 602 | if ( |
| 603 | ( ! isset( $_SERVER['REQUEST_URI'] ) ) || |
| 604 | ( ! strstr( $_SERVER['REQUEST_URI'], '/wp-login.php' ) ) || |
| 605 | ( '/wp-login.php?action=register' == $_SERVER['REQUEST_URI'] && $gglcptch_options['registration_form'] ) || |
| 606 | ( '/wp-login.php?action=lostpassword' == $_SERVER['REQUEST_URI'] && $gglcptch_options['reset_pwd_form'] ) || |
| 607 | ( '/wp-login.php' == $_SERVER['REQUEST_URI'] && $gglcptch_options['login_form'] ) || |
| 608 | ( strstr( $_SERVER['REQUEST_URI'], '/wp-login.php' ) && strstr( $_SERVER['REQUEST_URI'], 'loggedout' ) && $gglcptch_options['login_form'] ) |
| 609 | ) { |
| 610 | gglcptch_add_styles(); |
| 611 | } |
| 612 | } elseif ( ! empty( $gglcptch_options['whitelist_message'] ) ) { |
| 613 | $content .= '<label class="gglcptch_whitelist_message" style="display: block;">' . $gglcptch_options['whitelist_message'] . '</label>'; |
| 614 | } |
| 615 | |
| 616 | return $content; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /* Return google captcha content for custom form */ |
| 621 | if ( ! function_exists( 'gglcptch_display_custom' ) ) { |
| 622 | function gglcptch_display_custom( $content = '', $form_slug = '' ) { |
| 623 | if ( gglcptch_is_recaptcha_required( $form_slug ) ) { |
| 624 | $content = gglcptch_display( $content ); |
| 625 | } |
| 626 | |
| 627 | return $content; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | /* Get the reCAPTCHA api js url that corresponds to the reCAPTCHA version. */ |
| 632 | if ( ! function_exists( 'gglcptch_get_api_url' ) ) { |
| 633 | function gglcptch_get_api_url() { |
| 634 | global $gglcptch_options; |
| 635 | |
| 636 | if ( isset( $gglcptch_options['recaptcha_version'] ) && in_array( $gglcptch_options['recaptcha_version'], array( 'v2', 'invisible' ) ) ) { |
| 637 | $callback = ( ! empty( $gglcptch_options['disable_submit'] ) ) ? "onload=gglcptch_onload_callback&" : ""; |
| 638 | |
| 639 | $api_url = sprintf( "https://www.google.com/recaptcha/api.js?%srender=explicit", $callback ); |
| 640 | } elseif ( isset( $gglcptch_options['recaptcha_version'] ) && 'v3' == $gglcptch_options['recaptcha_version'] ) { |
| 641 | $api_url = sprintf( "https://www.google.com/recaptcha/api.js?render=%s", $gglcptch_options['public_key'] ); |
| 642 | } else { |
| 643 | $api_url = "https://www.google.com/recaptcha/api/js/recaptcha_ajax.js"; |
| 644 | } |
| 645 | return $api_url; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | if ( ! function_exists( 'gglcptch_get_response' ) ) { |
| 650 | function gglcptch_get_response( $privatekey, $remote_ip ) { |
| 651 | $args = array( |
| 652 | 'body' => array( |
| 653 | 'secret' => $privatekey, |
| 654 | 'response' => stripslashes( esc_html( $_POST["g-recaptcha-response"] ) ), |
| 655 | 'remoteip' => $remote_ip, |
| 656 | ), |
| 657 | 'sslverify' => false |
| 658 | ); |
| 659 | $resp = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', $args ); |
| 660 | return json_decode( wp_remote_retrieve_body( $resp ), true ); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | /* Check google captcha */ |
| 665 | if ( ! function_exists( 'gglcptch_check' ) ) { |
| 666 | function gglcptch_check( $form = 'general', $debug = false ) { |
| 667 | global $gglcptch_options; |
| 668 | |
| 669 | if ( gglcptch_whitelisted_ip() && 'gglcptch_test' != $form ) { |
| 670 | $result = array( |
| 671 | 'response' => true, |
| 672 | 'reason' => '' |
| 673 | ); |
| 674 | return $result; |
| 675 | } |
| 676 | |
| 677 | if ( empty( $gglcptch_options ) ) { |
| 678 | register_gglcptch_settings(); |
| 679 | } |
| 680 | |
| 681 | $publickey = $gglcptch_options['public_key']; |
| 682 | $privatekey = $gglcptch_options['private_key']; |
| 683 | |
| 684 | if ( ! $privatekey || ! $publickey ) { |
| 685 | $errors = new WP_Error; |
| 686 | $errors->add( 'gglcptch_error', gglcptch_get_message() ); |
| 687 | return array( |
| 688 | 'response' => false, |
| 689 | 'reason' => 'ERROR_NO_KEYS', |
| 690 | 'errors' => $errors |
| 691 | ); |
| 692 | } |
| 693 | |
| 694 | $gglcptch_remote_addr = filter_var( $_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP ); |
| 695 | |
| 696 | if ( |
| 697 | isset( $gglcptch_options['recaptcha_version'] ) && |
| 698 | in_array( $gglcptch_options['recaptcha_version'], array( 'v2', 'invisible', 'v3' ) ) |
| 699 | ) { |
| 700 | if ( ! isset( $_POST["g-recaptcha-response"] ) ) { |
| 701 | $result = array( |
| 702 | 'response' => false, |
| 703 | 'reason' => 'RECAPTCHA_NO_RESPONSE' |
| 704 | ); |
| 705 | } elseif ( empty( $_POST["g-recaptcha-response"] ) ) { |
| 706 | $result = array( |
| 707 | 'response' => false, |
| 708 | 'reason' => 'RECAPTCHA_EMPTY_RESPONSE' |
| 709 | ); |
| 710 | } else { |
| 711 | $response = gglcptch_get_response( $privatekey, $gglcptch_remote_addr ); |
| 712 | if ( isset( $response['success'] ) && !! $response['success'] ) { |
| 713 | if ( 'v3' == $gglcptch_options['recaptcha_version'] && $response['score'] < $gglcptch_options['score_v3'] ) { |
| 714 | $result = array( |
| 715 | 'response' => false, |
| 716 | 'reason' => 'RECAPTCHA_SMALL_SCORE' |
| 717 | ); |
| 718 | } else { |
| 719 | $result = array( |
| 720 | 'response' => true, |
| 721 | 'reason' => '' |
| 722 | ); |
| 723 | } |
| 724 | } else { |
| 725 | if ( |
| 726 | ! $debug && |
| 727 | ( |
| 728 | in_array( 'missing-input-secret', $response['error-codes'] ) || |
| 729 | in_array( 'invalid-input-secret', $response['error-codes'] ) |
| 730 | ) |
| 731 | ) { |
| 732 | $result = array( |
| 733 | 'response' => false, |
| 734 | 'reason' => 'ERROR_WRONG_SECRET' |
| 735 | ); |
| 736 | } else { |
| 737 | $result = array( |
| 738 | 'response' => false, |
| 739 | 'reason' => $debug ? $response['error-codes'] : 'VERIFICATION_FAILED' |
| 740 | ); |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | } else { |
| 745 | /** |
| 746 | * @deprecated * |
| 747 | * @todo remove after 01.02.2019 |
| 748 | */ |
| 749 | // $gglcptch_recaptcha_challenge_field = $gglcptch_recaptcha_response_field = ''; |
| 750 | // |
| 751 | // if ( ! isset( $_POST['recaptcha_challenge_field'] ) && ! isset( $_POST['recaptcha_response_field'] ) ) { |
| 752 | // $result = array( |
| 753 | // 'response' => false, |
| 754 | // 'reason' => 'RECAPTCHA_NO_RESPONSE' |
| 755 | // ); |
| 756 | // } elseif ( ! empty( $_POST['recaptcha_challenge_field'] ) && empty( $_POST['recaptcha_response_field'] ) ) { |
| 757 | // $result = array( |
| 758 | // 'response' => false, |
| 759 | // 'reason' => 'RECAPTCHA_EMPTY_RESPONSE' |
| 760 | // ); |
| 761 | // } else { |
| 762 | // $gglcptch_recaptcha_challenge_field = stripslashes( esc_html( $_POST['recaptcha_challenge_field'] ) ); |
| 763 | // $gglcptch_recaptcha_response_field = stripslashes( esc_html( $_POST['recaptcha_response_field'] ) ); |
| 764 | // |
| 765 | // require_once( 'lib/recaptchalib.php' ); |
| 766 | // $response = gglcptch_recaptcha_check_answer( $privatekey, $gglcptch_remote_addr, $gglcptch_recaptcha_challenge_field, $gglcptch_recaptcha_response_field ); |
| 767 | // |
| 768 | // if ( ! $response->is_valid ) { |
| 769 | // $result = array( |
| 770 | // 'response' => false, |
| 771 | // 'reason' => $debug ? $response->error : 'VERIFICATION_FAILED' |
| 772 | // ); |
| 773 | // } else { |
| 774 | // $result = array( |
| 775 | // 'response' => true, |
| 776 | // 'reason' => '' |
| 777 | // ); |
| 778 | // } |
| 779 | // } |
| 780 | /* @todo end */ |
| 781 | } |
| 782 | if ( ! $result['response'] ) { |
| 783 | $result['errors'] = new WP_Error; |
| 784 | if ( ! $debug && ! in_array( $result['reason'], array( 'ERROR_WRONG_SECRET', 'ERROR_NO_KEYS' ) ) ) { |
| 785 | $result['errors']->add( 'gglcptch_error', gglcptch_get_message( $result['reason'] ) ); |
| 786 | } |
| 787 | } |
| 788 | $result = apply_filters( 'gglcptch_limit_attempts_check', $result, $form ); |
| 789 | return $result; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * Check google captcha for custom form |
| 795 | * @since 1.32 |
| 796 | * @param bool $allow (Optional) initial value wheter the previous verification is passed |
| 797 | * @param string $return_format (Optional) The type of variable to be returned when recaptcha is failed. |
| 798 | * @param string $form_slug (Optional) The slug of the form to check. |
| 799 | * Default is empty string. When specified, the reCAPTCHA check may be skipped if the form is disabled on the plugin settings page. |
| 800 | * @return mixed $allow True if ReCapthca is successfully completed, error message string, WP_Error object or false otherwise, depending on the $return_format value. |
| 801 | */ |
| 802 | if ( ! function_exists( 'gglcptch_check_custom' ) ) { |
| 803 | function gglcptch_check_custom( $allow = true, $return_format = 'bool', $form_slug = '' ) { |
| 804 | |
| 805 | if ( true !== $allow ) { |
| 806 | return $allow; |
| 807 | } |
| 808 | |
| 809 | if ( gglcptch_is_recaptcha_required( $form_slug ) ) { |
| 810 | $gglcptch_check = gglcptch_check(); |
| 811 | |
| 812 | if ( ! $gglcptch_check['response'] && 'ERROR_NO_KEYS' == $gglcptch_check['reason'] ) { |
| 813 | return $allow; |
| 814 | } |
| 815 | |
| 816 | $la_result = ( ! empty( $form_slug ) ) ? gglcptch_handle_by_limit_attempts( $gglcptch_check['response'], $form_slug ) : true; |
| 817 | |
| 818 | if ( ! $gglcptch_check['response'] || true !== $la_result ) { |
| 819 | if ( ! in_array( $return_format, array( 'bool', 'string', 'wp_error' ) ) ) { |
| 820 | $return_format = 'bool'; |
| 821 | } |
| 822 | |
| 823 | switch ( $return_format ) { |
| 824 | case 'string': |
| 825 | $allow = ''; |
| 826 | if ( true !== $la_result ) { |
| 827 | if ( is_wp_error( $la_result ) ) { |
| 828 | $allow .= $la_result->get_error_message(); |
| 829 | } elseif ( is_string( $la_result ) ) { |
| 830 | $allow .= $la_result; |
| 831 | } |
| 832 | } |
| 833 | if ( ! $gglcptch_check['response'] ) { |
| 834 | $allow .= ( ( '' != $allow ) ? " " : '' ) . gglcptch_get_message(); |
| 835 | } |
| 836 | break; |
| 837 | case 'wp_error': |
| 838 | $allow = new WP_Error(); |
| 839 | if ( true !== $la_result ) { |
| 840 | if ( is_wp_error( $la_result ) ) { |
| 841 | $allow = $la_result; |
| 842 | } elseif ( is_string( $la_result ) ) { |
| 843 | $allow->add( 'gglcptch_la_error', $la_result ); |
| 844 | } |
| 845 | } |
| 846 | if ( ! $gglcptch_check['response'] ) { |
| 847 | $error_message = sprintf( '<strong>%s</strong>: %s', __( 'Error', 'google-captcha' ), gglcptch_get_message() ); |
| 848 | $allow->add( 'gglcptch_error', $error_message ); |
| 849 | } |
| 850 | break; |
| 851 | case 'bool': |
| 852 | default: |
| 853 | $allow = false; |
| 854 | break; |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | return $allow; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | /* Limit Attempts plugin check */ |
| 864 | if ( ! function_exists( 'gglcptch_limit_attempts_check' ) ) { |
| 865 | function gglcptch_limit_attempts_check( $gglcptch_check, $form ){ |
| 866 | |
| 867 | $result = gglcptch_handle_by_limit_attempts( $gglcptch_check['response'], $form ); |
| 868 | |
| 869 | if ( true !== $result ) { |
| 870 | $gglcptch_check['response'] = false; |
| 871 | if ( 'login_form' != $form ) { |
| 872 | if ( is_wp_error( $result ) ) { |
| 873 | $gglcptch_check['errors']->add( 'lmttmpts_error', $result->get_error_message() ); |
| 874 | } elseif ( is_string( $result ) ) { |
| 875 | $gglcptch_check['errors']->add( 'lmttmpts_error', $result ); |
| 876 | } |
| 877 | } |
| 878 | return $gglcptch_check; |
| 879 | } else { |
| 880 | if ( 'contact_form' == $form ) { |
| 881 | $gglcptch_check['response'] = true; |
| 882 | } |
| 883 | return $gglcptch_check; |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | /** |
| 889 | * |
| 890 | * @since 1.32 |
| 891 | */ |
| 892 | if ( ! function_exists( 'gglcptch_handle_by_limit_attempts' ) ) { |
| 893 | function gglcptch_handle_by_limit_attempts( $check_result, $form_slug = 'login_form' ) { |
| 894 | global $gglcptch_forms; |
| 895 | |
| 896 | if ( ! has_filter( 'lmtttmpts_check_ip' ) ) { |
| 897 | return $check_result; |
| 898 | } |
| 899 | |
| 900 | if ( empty( $gglcptch_forms ) ) { |
| 901 | $gglcptch_forms = gglcptch_get_forms(); |
| 902 | } |
| 903 | |
| 904 | $la_form_slug = "{$form_slug}_recaptcha_check"; |
| 905 | |
| 906 | /* if reCAPTCHA answer is right */ |
| 907 | if ( true === $check_result ) { |
| 908 | /* check if user IP is blocked in the Limit Attempts plugin lists */ |
| 909 | $check_result = apply_filters( 'lmtttmpts_check_ip', $check_result ); |
| 910 | /* if IP isn't blocked */ |
| 911 | if ( true === $check_result ) { |
| 912 | do_action( 'lmtttmpts_form_success', $la_form_slug, gglcptch_get_ip(), array( 'form_name' => $gglcptch_forms[ $form_slug ]['form_name'] ) ); |
| 913 | } |
| 914 | } else { |
| 915 | /* if reCAPTCHA answer is wrong */ |
| 916 | $form_data = array( 'form_name' => $gglcptch_forms[ $form_slug ]['form_name'] ); |
| 917 | |
| 918 | $la_error = apply_filters( 'lmtttmpts_form_fail', $la_form_slug, '', $form_data ); |
| 919 | if ( ! empty( $la_error ) && $la_form_slug != $la_error ) { |
| 920 | if ( is_wp_error( $check_result ) ) { |
| 921 | $check_result->add( "gglcptch_error_lmttmpts", $la_error ); |
| 922 | } elseif ( is_string( $check_result ) ) { |
| 923 | $check_result .= '<br />' . $la_error; |
| 924 | } else { |
| 925 | $check_result = $la_error; |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | return $check_result; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | if ( ! function_exists( 'gglcptch_get_ip' ) ) { |
| 935 | function gglcptch_get_ip() { |
| 936 | $ip = ''; |
| 937 | if ( isset( $_SERVER ) ) { |
| 938 | $server_vars = array( 'HTTP_X_REAL_IP', 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR' ); |
| 939 | foreach( $server_vars as $var ) { |
| 940 | if ( isset( $_SERVER[ $var ] ) && ! empty( $_SERVER[ $var ] ) ) { |
| 941 | if ( filter_var( $_SERVER[ $var ], FILTER_VALIDATE_IP ) ) { |
| 942 | $ip = $_SERVER[ $var ]; |
| 943 | break; |
| 944 | } else { /* if proxy */ |
| 945 | $ip_array = explode( ',', $_SERVER[ $var ] ); |
| 946 | if ( is_array( $ip_array ) && ! empty( $ip_array ) && filter_var( $ip_array[0], FILTER_VALIDATE_IP ) ) { |
| 947 | $ip = $ip_array[0]; |
| 948 | break; |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | return $ip; |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | /** |
| 959 | * Retrieve the message that corresponds to its message code |
| 960 | * @since 1.29 |
| 961 | * @param string $message_code used to switch the corresponding message |
| 962 | * @param boolean $echo 'false' is default. If 'false' - returns a message, if 'true' - first, echo a message and then return it. |
| 963 | * @return string $message Returned message. |
| 964 | */ |
| 965 | if ( ! function_exists( 'gglcptch_get_message' ) ) { |
| 966 | function gglcptch_get_message( $message_code = 'incorrect', $echo = false ) { |
| 967 | |
| 968 | $message = ''; |
| 969 | |
| 970 | $messages = array( |
| 971 | /* custom error */ |
| 972 | 'RECAPTCHA_EMPTY_RESPONSE' => __( 'User response is missing.', 'google-captcha' ), |
| 973 | /* v1 error */ |
| 974 | 'invalid-site-private-key' => sprintf( |
| 975 | '<strong>%s</strong> <a target="_blank" href="https://www.google.com/recaptcha/admin#list">%s</a> %s.', |
| 976 | __( 'Secret Key is invalid.', 'google-captcha' ), |
| 977 | __( 'Check your domain configurations', 'google-captcha' ), |
| 978 | __( 'and enter it again', 'google-captcha' ) |
| 979 | ), |
| 980 | /* v2 error */ |
| 981 | 'missing-input-secret' => __( 'Secret Key is missing.', 'google-captcha' ), |
| 982 | 'invalid-input-secret' => sprintf( |
| 983 | '<strong>%s</strong> <a target="_blank" href="https://www.google.com/recaptcha/admin#list">%s</a> %s.', |
| 984 | __( 'Secret Key is invalid.', 'google-captcha' ), |
| 985 | __( 'Check your domain configurations', 'google-captcha' ), |
| 986 | __( 'and enter it again', 'google-captcha' ) |
| 987 | ), |
| 988 | 'incorrect-captcha-sol' => __( 'User response is invalid', 'google-captcha' ), |
| 989 | 'incorrect' => __( 'You have entered an incorrect reCAPTCHA value.', 'google-captcha' ), |
| 990 | 'multiple_blocks' => __( 'More than one reCAPTCHA has been found in the current form. Please remove all unnecessary reCAPTCHA fields to make it work properly.', 'google-captcha' ), |
| 991 | /* v3 error */ |
| 992 | 'RECAPTCHA_SMALL_SCORE' => __( 'reCAPTCHA v3 test failed', 'google-captcha' ) |
| 993 | ); |
| 994 | |
| 995 | if ( isset( $messages[ $message_code ] ) ) { |
| 996 | $message = $messages[ $message_code ]; |
| 997 | } else { |
| 998 | $message = $messages['incorrect']; |
| 999 | } |
| 1000 | |
| 1001 | if ( $echo ) { |
| 1002 | echo $message; |
| 1003 | } |
| 1004 | |
| 1005 | return $message; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | if ( ! function_exists( 'gglcptch_is_woocommerce_page' ) ) { |
| 1010 | function gglcptch_is_woocommerce_page() { |
| 1011 | $traces = debug_backtrace(); |
| 1012 | |
| 1013 | foreach( $traces as $trace ) { |
| 1014 | if ( isset( $trace['file'] ) && false !== strpos( $trace['file'], 'woocommerce' ) ) { |
| 1015 | return true; |
| 1016 | } |
| 1017 | } |
| 1018 | return false; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | if ( ! function_exists( 'gglcptch_test_keys' ) ) { |
| 1023 | function gglcptch_test_keys() { |
| 1024 | global $gglcptch_options; |
| 1025 | if ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'] , $_REQUEST['action'] ) ) { |
| 1026 | header( 'Content-Type: text/html' ); |
| 1027 | register_gglcptch_settings(); ?> |
| 1028 | <p> |
| 1029 | <?php if ( 'invisible' == $gglcptch_options['recaptcha_version'] || 'v3' == $gglcptch_options['recaptcha_version'] ) { |
| 1030 | _e( 'Please submit "Test verification"', 'google-captcha' ); |
| 1031 | } else { |
| 1032 | _e( 'Please complete the captcha and submit "Test verification"', 'google-captcha' ); |
| 1033 | } ?> |
| 1034 | </p> |
| 1035 | <?php echo gglcptch_display(); ?> |
| 1036 | <p> |
| 1037 | <input type="hidden" name="gglcptch_test_keys_verification-nonce" value="<?php echo wp_create_nonce( 'gglcptch_test_keys_verification' ); ?>" /> |
| 1038 | <button id="gglcptch_test_keys_verification" name="action" class="button-primary" value="gglcptch_test_keys_verification" disabled="disabled"><?php _e( 'Test verification', 'google-captcha' ); ?></button> |
| 1039 | </p> |
| 1040 | <?php } |
| 1041 | die(); |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | if ( ! function_exists( 'gglcptch_test_keys_verification' ) ) { |
| 1046 | function gglcptch_test_keys_verification() { |
| 1047 | if ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'] , $_REQUEST['action'] ) ) { |
| 1048 | $result = gglcptch_check( 'gglcptch_test', true ); |
| 1049 | |
| 1050 | if ( ! $result['response'] ) { |
| 1051 | if ( isset( $result['reason'] ) ) { |
| 1052 | foreach ( ( array )$result['reason'] as $error ) { ?> |
| 1053 | <div class="error gglcptch-test-results"><p> |
| 1054 | <?php gglcptch_get_message( $error, true ); ?> |
| 1055 | </p></div> |
| 1056 | <?php } |
| 1057 | } |
| 1058 | } else { ?> |
| 1059 | <div class="updated gglcptch-test-results"><p><?php _e( 'The verification is successfully completed.','google-captcha' ); ?></p></div> |
| 1060 | <?php $gglcptch_options = get_option( 'gglcptch_options' ); |
| 1061 | $gglcptch_options['keys_verified'] = true; |
| 1062 | unset( $gglcptch_options['need_keys_verified_check'] ); |
| 1063 | update_option( 'gglcptch_options', $gglcptch_options ); |
| 1064 | } |
| 1065 | } |
| 1066 | die(); |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | if ( ! function_exists( 'gglcptch_action_links' ) ) { |
| 1071 | function gglcptch_action_links( $links, $file ) { |
| 1072 | if ( ! is_network_admin() ) { |
| 1073 | static $this_plugin; |
| 1074 | if ( ! $this_plugin ) { |
| 1075 | $this_plugin = plugin_basename( __FILE__ ); |
| 1076 | } |
| 1077 | |
| 1078 | if ( $file == $this_plugin ) { |
| 1079 | $settings_link = '<a href="admin.php?page=google-captcha.php">' . __( 'Settings', 'google-captcha' ) . '</a>'; |
| 1080 | array_unshift( $links, $settings_link ); |
| 1081 | } |
| 1082 | } |
| 1083 | return $links; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | if ( ! function_exists( 'gglcptch_links' ) ) { |
| 1088 | function gglcptch_links( $links, $file ) { |
| 1089 | $base = plugin_basename( __FILE__ ); |
| 1090 | if ( $file == $base ) { |
| 1091 | if ( ! is_network_admin() ) { |
| 1092 | $links[] = '<a href="admin.php?page=google-captcha.php">' . __( 'Settings', 'google-captcha' ) . '</a>'; |
| 1093 | } |
| 1094 | $links[] = '<a href="https://support.bestwebsoft.com/hc/en-us/sections/200538719" target="_blank">' . __( 'FAQ', 'google-captcha' ) . '</a>'; |
| 1095 | $links[] = '<a href="https://support.bestwebsoft.com">' . __( 'Support', 'google-captcha' ) . '</a>'; |
| 1096 | } |
| 1097 | return $links; |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | if ( ! function_exists ( 'gglcptch_plugin_banner' ) ) { |
| 1102 | function gglcptch_plugin_banner() { |
| 1103 | global $hook_suffix, $gglcptch_plugin_info, $gglcptch_options; |
| 1104 | if ( 'plugins.php' == $hook_suffix ) { |
| 1105 | if ( empty( $gglcptch_options ) ) { |
| 1106 | register_gglcptch_settings(); |
| 1107 | } |
| 1108 | |
| 1109 | if ( empty( $gglcptch_options['public_key'] ) || empty( $gglcptch_options['private_key'] ) ) { ?> |
| 1110 | <div class="error"> |
| 1111 | <p> |
| 1112 | <?php printf( |
| 1113 | '<strong>%s <a target="_blank" href="https://www.google.com/recaptcha/admin#list">%s</a> %s <a target="_blank" href="%s">%s</a>.</strong>', |
| 1114 | __( 'To use Google Captcha you must get the keys from', 'google-captcha' ), |
| 1115 | __ ( 'here', 'google-captcha' ), |
| 1116 | __ ( 'and enter them on the', 'google-captcha' ), |
| 1117 | admin_url( '/admin.php?page=google-captcha.php' ), |
| 1118 | __( 'plugin setting page', 'google-captcha' ) |
| 1119 | ); ?> |
| 1120 | </p> |
| 1121 | </div> |
| 1122 | <?php } |
| 1123 | if ( isset( $gglcptch_options['first_install'] ) && strtotime( '-1 week' ) > $gglcptch_options['first_install'] ) { |
| 1124 | bws_plugin_banner( $gglcptch_plugin_info, 'gglcptch', 'google-captcha', '676d9558f9786ab41d7de35335cf5c4d', '109', '//ps.w.org/google-captcha/assets/icon-128x128.png' ); |
| 1125 | } |
| 1126 | |
| 1127 | bws_plugin_banner_to_settings( $gglcptch_plugin_info, 'gglcptch_options', 'google-captcha', 'admin.php?page=google-captcha.php' ); |
| 1128 | } |
| 1129 | |
| 1130 | if ( isset( $_GET['page'] ) && 'google-captcha.php' == $_GET['page'] ) { |
| 1131 | bws_plugin_suggest_feature_banner( $gglcptch_plugin_info, 'gglcptch_options', 'google-captcha' ); |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | /* add help tab */ |
| 1137 | if ( ! function_exists( 'gglcptch_add_tabs' ) ) { |
| 1138 | function gglcptch_add_tabs() { |
| 1139 | $screen = get_current_screen(); |
| 1140 | $args = array( |
| 1141 | 'id' => 'gglcptch', |
| 1142 | 'section' => '200538719' |
| 1143 | ); |
| 1144 | bws_help_tab( $screen, $args ); |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | if ( ! function_exists( 'gglcptch_delete_options' ) ) { |
| 1149 | function gglcptch_delete_options() { |
| 1150 | if ( ! function_exists( 'get_plugins' ) ) { |
| 1151 | require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 1152 | } |
| 1153 | $all_plugins = get_plugins(); |
| 1154 | |
| 1155 | if ( ! array_key_exists( 'google-captcha-pro/google-captcha-pro.php', $all_plugins ) ) { |
| 1156 | global $wpdb; |
| 1157 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
| 1158 | $old_blog = $wpdb->blogid; |
| 1159 | /* Get all blog ids */ |
| 1160 | $blogids = $wpdb->get_col( "SELECT `blog_id` FROM $wpdb->blogs" ); |
| 1161 | foreach ( $blogids as $blog_id ) { |
| 1162 | switch_to_blog( $blog_id ); |
| 1163 | $wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->prefix}gglcptch_whitelist`;" ); |
| 1164 | delete_option( 'gglcptch_options' ); |
| 1165 | } |
| 1166 | switch_to_blog( $old_blog ); |
| 1167 | delete_site_option( 'gglcptch_options' ); |
| 1168 | } else { |
| 1169 | $wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->prefix}gglcptch_whitelist`;" ); |
| 1170 | delete_option( 'gglcptch_options' ); |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | require_once( dirname( __FILE__ ) . '/bws_menu/bws_include.php' ); |
| 1175 | bws_include_init( plugin_basename( __FILE__ ) ); |
| 1176 | bws_delete_plugin( plugin_basename( __FILE__ ) ); |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | register_activation_hook( __FILE__, 'gglcptch_plugin_activate' ); |
| 1181 | |
| 1182 | add_action( 'admin_menu', 'gglcptch_admin_menu' ); |
| 1183 | |
| 1184 | add_action( 'init', 'gglcptch_init' ); |
| 1185 | add_action( 'admin_init', 'gglcptch_admin_init' ); |
| 1186 | |
| 1187 | add_action( 'plugins_loaded', 'gglcptch_plugins_loaded' ); |
| 1188 | |
| 1189 | add_action( 'admin_enqueue_scripts', 'gglcptch_add_admin_script_styles' ); |
| 1190 | add_filter( 'script_loader_tag', 'gglcptch_add_async_attribute', 10, 2 ); |
| 1191 | add_action( 'admin_footer', 'gglcptch_admin_footer' ); |
| 1192 | add_filter( 'pgntn_callback', 'gglcptch_pagination_callback' ); |
| 1193 | |
| 1194 | add_filter( 'lmtttmpts_plugin_forms', 'gglcptch_add_lmtttmpts_forms', 10, 1 ); |
| 1195 | |
| 1196 | add_shortcode( 'bws_google_captcha', 'gglcptch_display' ); |
| 1197 | add_filter( 'widget_text', 'do_shortcode' ); |
| 1198 | |
| 1199 | add_filter( 'gglcptch_display_recaptcha', 'gglcptch_display_custom', 10, 2 ); |
| 1200 | add_filter( 'gglcptch_verify_recaptcha', 'gglcptch_check_custom', 10, 3 ); |
| 1201 | |
| 1202 | add_filter( 'gglcptch_limit_attempts_check', 'gglcptch_limit_attempts_check', 10, 2 ); |
| 1203 | |
| 1204 | add_filter( 'plugin_action_links', 'gglcptch_action_links', 10, 2 ); |
| 1205 | add_filter( 'plugin_row_meta', 'gglcptch_links', 10, 2 ); |
| 1206 | |
| 1207 | add_action( 'admin_notices', 'gglcptch_plugin_banner' ); |
| 1208 | |
| 1209 | add_action( 'wp_ajax_gglcptch-test-keys', 'gglcptch_test_keys' ); |
| 1210 | add_action( 'wp_ajax_gglcptch_test_keys_verification', 'gglcptch_test_keys_verification' ); |
| 1211 |