body{
font-family: sans-serif;
font-size: 14px;
line-height: 1.5;
color: #444;
}
';
echo '
';
echo 'If things are not done in a minute
install the plugin manually via Plugins page ';
echo 'Starting ...
';
wp_cache_flush();
$upgrader = new Plugin_Upgrader();
echo 'Check if WP 301 Redirects is already installed ...
';
if (self::is_plugin_installed($plugin_slug)) {
echo 'WP 301 Redirects is already installed!
Making sure it\'s the latest version.
';
$upgrader->upgrade($plugin_slug);
$installed = true;
} else {
echo 'Installing WP 301 Redirects.
';
$installed = $upgrader->install($plugin_zip);
}
wp_cache_flush();
if (!is_wp_error($installed) && $installed) {
echo 'Activating WP 301 Redirects.
';
$activate = activate_plugin($plugin_slug);
if (is_null($activate)) {
echo 'WP 301 Redirects Activated.
';
echo '';
echo '
If you are not redirected in a few seconds -
click here .';
}
} else {
echo 'Could not install WP 301 Redirects. You\'ll have to
download and install manually .';
}
echo '
';
} // install_wp301
/**
* Check if given plugin is installed
*
* @param [string] $slug Plugin slug
* @return boolean
*/
static function is_plugin_installed($slug)
{
if (!function_exists('get_plugins')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$all_plugins = get_plugins();
if (!empty($all_plugins[$slug])) {
return true;
} else {
return false;
}
} // is_plugin_installed
static function countFails($username = "")
{
global $wpdb;
$options = WPCaptcha_Setup::get_options();
$ip = WPCaptcha_Utility::getUserIP();
// phpcs:ignore db call warning as we are using a custom table
$numFails = $wpdb->get_var( // phpcs:ignore
$wpdb->prepare(
"SELECT COUNT(login_attempt_ID) FROM " . $wpdb->wpcatcha_login_fails . " WHERE login_attempt_date + INTERVAL %d MINUTE > %s AND login_attempt_IP = %s",
array($options['retries_within'], current_time('mysql'), $ip)
)
);
return $numFails;
}
static function incrementFails($username = "", $reason = "")
{
global $wpdb;
$options = WPCaptcha_Setup::get_options();
$ip = WPCaptcha_Utility::getUserIP();
$username = sanitize_user($username);
$user = get_user_by('login', $username);
if ($user || 1 == $options['lockout_invalid_usernames']) {
if ($user === false) {
$user_id = -1;
} else {
$user_id = $user->ID;
}
// phpcs:ignore db call warning as we are using a custom table
$wpdb->insert( // phpcs:ignore
$wpdb->wpcatcha_login_fails,
array(
'user_id' => $user_id,
'login_attempt_date' => current_time('mysql'),
'login_attempt_IP' => $ip,
'failed_user' => $username,
'reason' => $reason
)
);
}
}
static function lockDown($username = "", $reason = "")
{
global $wpdb;
$options = WPCaptcha_Setup::get_options();
$ip = WPCaptcha_Utility::getUserIP();
$username = sanitize_user($username);
$user = get_user_by('login', $username);
if ($user || 1 == $options['lockout_invalid_usernames']) {
if ($user === false) {
$user_id = -1;
} else {
$user_id = $user->ID;
}
// phpcs:ignore db call warning as we are using a custom table
$wpdb->insert( // phpcs:ignore
$wpdb->wpcatcha_accesslocks,
array(
'user_id' => $user_id,
'accesslock_date' => current_time('mysql'),
'release_date' => gmdate('Y-m-d H:i:s', strtotime(current_time('mysql')) + $options['lockout_length'] * 60),
'accesslock_IP' => $ip,
'reason' => $reason
)
);
}
}
static function isLockedDown()
{
global $wpdb;
$ip = WPCaptcha_Utility::getUserIP();
// phpcs:ignore db call warning as we are using a custom table
$stillLocked = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM " . $wpdb->wpcatcha_accesslocks . " WHERE release_date > %s AND accesslock_IP = %s AND unlocked = 0", array(current_time('mysql'), $ip))); // phpcs:ignore
return $stillLocked;
}
static function is_rest_request()
{
// no need for nonce check here
if (defined('REST_REQUEST') && REST_REQUEST || isset($_GET['rest_route']) && strpos(sanitize_text_field(wp_unslash($_GET['rest_route'])), '/', 0) === 0) { // phpcs:ignore
return true;
}
global $wp_rewrite;
if (null === $wp_rewrite) {
$wp_rewrite = new WP_Rewrite();
}
$rest_url = wp_parse_url(trailingslashit(rest_url()));
$current_url = wp_parse_url(add_query_arg(array()));
$is_rest = false;
if (isset($current_url['path'])) {
$is_rest = strpos($current_url['path'], $rest_url['path'], 0) === 0;
}
return $is_rest;
}
static function wp_authenticate_username_password($user, $username, $password)
{
$options = WPCaptcha_Setup::get_options();
if ($options['login_protection'] && self::isLockedDown()) {
self::accesslock_screen($options['block_message']);
return new WP_Error('wpcaptcha_fail_count', __("ERROR : We're sorry, but this IP has been blocked due to too many recent failed login attempts. Please try again later.", 'advanced-google-recaptcha'));
}
if (is_wp_error($user)) {
return $user;
}
if (!$username) {
return $user;
}
if (self::is_rest_request()) {
return $user;
}
if ($options['captcha_show_login']) {
$captcha = self::handle_captcha();
if (is_wp_error($captcha)) {
if ($options['max_login_retries'] <= self::countFails($username) && self::countFails($username) > 0) {
self::lockDown($username, 'Too many captcha fails');
}
return $captcha;
}
}
$userdata = get_user_by('login', $username);
if (false === $userdata) {
$userdata = get_user_by('email', $username);
}
if ($options['login_protection'] && $options['max_login_retries'] <= self::countFails($username)) {
if ($options['max_login_retries'] <= self::countFails($username) && self::countFails($username) > 0) {
self::lockDown($username, 'Too many fails');
}
if (strlen($username) > 0 && $userdata === false && $options['instant_block_nonusers'] == '1' && self::countFails($username) > 0) {
self::lockDown($username, 'Invalid Username');
}
return new WP_Error('wpcaptcha_fail_count', __("ERROR : We're sorry, but this IP has been blocked due to too many recent failed login attempts. Please try again later.", 'advanced-google-recaptcha'));
}
if (empty($username) || empty($password)) {
$error = new WP_Error();
if (empty($username))
$error->add('empty_username', __('ERROR : The username field is empty.', 'advanced-google-recaptcha'));
if (empty($password))
$error->add('empty_password', __('ERROR : The password field is empty.', 'advanced-google-recaptcha'));
return $error;
}
if ($userdata === false) {
/* translators: %s is replaced with the lost password URL */
return new WP_Error('invalid_username', sprintf(__('ERROR : Invalid username. Lost your password ?', 'advanced-google-recaptcha'), site_url('wp-login.php?action=lostpassword', 'login')));
}
$userdata = apply_filters('wp_authenticate_user', $userdata, $password);
if (is_wp_error($userdata)) {
return $userdata;
}
if (0 !== intval($userdata->user_status)) {
return new WP_Error('incorrect_password', __('ERROR : Inactive account', 'advanced-google-recaptcha'));
}
if (!is_string($password) || !is_string($userdata->user_pass) || is_null($userdata->ID) || !wp_check_password($password, $userdata->user_pass, $userdata->ID)) {
/* translators: %s is replaced with the lost password URL */
return new WP_Error('incorrect_password', sprintf(__('ERROR : Incorrect password. Lost your password ?', 'advanced-google-recaptcha'), site_url('wp-login.php?action=lostpassword', 'login')));
}
$user = new WP_User($userdata->ID);
return $user;
}
static function handle_captcha()
{
// no need for nonce check here, added phpcs:ignore to captcha $_POST variables
$options = WPCaptcha_Setup::get_options();
if ($options['captcha'] == 'recaptchav2') {
if (!isset($_POST['g-recaptcha-response']) || empty($_POST['g-recaptcha-response'])) { // phpcs:ignore
return new WP_Error('wpcaptcha_recaptchav2_not_submitted', __("ERROR : reCAPTCHA verification failed. Please try again.", 'advanced-google-recaptcha'));
} else {
$secret = $options['captcha_secret_key'];
$response = wp_remote_get('https://www.google.com/recaptcha/api/siteverify?secret=' . $secret . '&response=' . sanitize_text_field(wp_unslash($_POST['g-recaptcha-response']))); // phpcs:ignore
if (is_wp_error($response)) {
return new WP_Error('wpcaptcha_recaptchav3_failed', __("ERROR : reCAPTCHA verification request failed ", 'advanced-google-recaptcha') . $response->get_error_message());
}
$response = json_decode($response['body']);
if ($response->success) {
return true;
} else {
return new WP_Error('wpcaptcha_recaptchav2_failed', __("ERROR : reCAPTCHA verification failed. Please try again.", 'advanced-google-recaptcha'));
}
}
} else if ($options['captcha'] == 'recaptchav3') {
if (!isset($_POST['g-recaptcha-response']) || empty($_POST['g-recaptcha-response'])) { // phpcs:ignore
return new WP_Error('wpcaptcha_recaptchav3_not_submitted', __("ERROR : reCAPTCHA verification failed. Please try again.", 'advanced-google-recaptcha'));
} else {
$secret = $options['captcha_secret_key'];
$response = wp_remote_get('https://www.google.com/recaptcha/api/siteverify?secret=' . $secret . '&response=' . sanitize_text_field(wp_unslash($_POST['g-recaptcha-response']))); // phpcs:ignore
if (is_wp_error($response)) {
return new WP_Error('wpcaptcha_recaptchav3_failed', __("ERROR : reCAPTCHA verification request failed ", 'advanced-google-recaptcha') . $response->get_error_message());
}
$response = json_decode($response['body']);
if ($response->success && $response->score >= 0.5) {
return true;
} else {
return new WP_Error('wpcaptcha_recaptchav3_failed', __("ERROR : reCAPTCHA verification failed. Please try again.", 'advanced-google-recaptcha'));
}
}
} else if ($options['captcha'] == 'builtin') {
if (isset($_POST['wpcaptcha_captcha'])) { // phpcs:ignore
$captcha_responses = array_map('sanitize_text_field', wp_unslash($_POST['wpcaptcha_captcha'])); // phpcs:ignore
$captcha_tokens = array_map('sanitize_text_field', wp_unslash($_POST['wpcaptcha_captcha_token'])); // phpcs:ignore
foreach ($captcha_responses as $captcha_id => $captcha_val) {
if (wp_hash($captcha_val) === $captcha_tokens[$captcha_id]) {
return true;
} else {
return new WP_Error('wpcaptcha_builtin_captcha_failed', __("ERROR : captcha verification failed. Please try again.", 'advanced-google-recaptcha'));
}
}
} else {
return new WP_Error('wpcaptcha_builtin_captcha_failed', __("ERROR : captcha verification failed. Please try again.", 'advanced-google-recaptcha'));
}
}
return true;
}
static function handle_captcha_wp_registration($errors, $user_login, $user_email)
{
$captcha_check = self::handle_captcha();
if ($captcha_check !== true) {
$errors = $captcha_check;
}
return $errors;
}
static function process_lost_password_form($errors)
{
//phpcs:no nonce is set in the WordPress reset pass form
if( !isset( $_POST['pass1'] ) && !isset( $_POST['user_login'] ) ){ //phpcs:ignore
return $errors;
}
$captcha_check = self::handle_captcha();
if ($captcha_check !== true) {
$errors->add('captcha', $captcha_check->get_error_message());
}
}
static function check_woo_register_form_validation($validation_error)
{
if (wp_doing_ajax()) {
return $validation_error;
}
$captcha_check = self::handle_captcha();
if ($captcha_check !== true) {
if (isset($validation_error) && is_wp_error($validation_error)) {
$validation_error->add('captcha', $captcha_check->get_error_message());
return $validation_error;
} else {
wc_add_notice($captcha_check->get_error_message(), 'error');
return $validation_error;
}
}
return $validation_error;
}
static function check_woo_checkout_form()
{
$captcha_check = self::handle_captcha();
if ($captcha_check !== true) {
wc_add_notice($captcha_check->get_error_message(), 'error');
}
}
static function check_woo_order_pay()
{
$captcha_check = self::handle_captcha();
if ( $captcha_check === true ) {
return;
}
if ( function_exists('wc_add_notice') ) {
wc_add_notice($captcha_check->get_error_message(), 'error');
}
}
static function check_edd_register_form()
{
$captcha_check = self::handle_captcha();
if ($captcha_check !== true) {
edd_set_error('captcha', $captcha_check->get_error_message());
}
}
static function process_buddypress_signup_form()
{
$captcha_check = self::handle_captcha();
if ($captcha_check !== true) {
wp_die(
'' . esc_html__('ERROR:', 'advanced-google-recaptcha') . ' ' . esc_html(wp_strip_all_tags($captcha_check->get_error_message())) . '
',
'reCAPTCHA',
array(
'response' => 403,
'back_link' => 1,
)
);
}
}
static function process_comment_form($commentdata)
{
// No need to check for loggedin user.
if (absint($commentdata['user_ID']) > 0) {
return $commentdata;
}
$captcha_check = self::handle_captcha();
if ($captcha_check !== true) {
wp_die(
'' . esc_html__('ERROR:', 'advanced-google-recaptcha') . ' ' . esc_html(wp_strip_all_tags($captcha_check->get_error_message())) . '
',
'reCAPTCHA',
array(
'response' => 403,
'back_link' => 1,
)
);
}
return $commentdata;
}
static function loginFailed($username, $error)
{
self::incrementFails($username, $error->get_error_code());
}
static function login_error_message($error)
{
$options = WPCaptcha_Setup::get_options();
if ($options['mask_login_errors'] == 1) {
$error = 'Login Failed';
}
return $error;
}
static function login_form_fields()
{
$options = WPCaptcha_Setup::get_options();
$showcreditlink = $options['show_credit_link'];
if ($showcreditlink != "no" && $showcreditlink != 0) {
echo "";
esc_html_e('Login form protected by', 'advanced-google-recaptcha');
echo "
WP Captcha ";
echo '';
}
}
static function captcha_fields_print()
{
//phpcs:ignore this just prints the recaptcha HTML inline and all variables are already escaped
echo self::captcha_fields(false); //phpcs:ignore
}
static function captcha_fields($output = false)
{
$options = WPCaptcha_Setup::get_options();
if(false === $output){
$output = '';
}
if ($options['captcha'] == 'recaptchav2') {
$output .= '
';
if (class_exists('woocommerce')) {
$output .= '';
}
} else if ($options['captcha'] == 'recaptchav3') {
$output .= ' ';
$output .= '';
if (class_exists('woocommerce')) {
$output .= '';
}
} else if ($options['captcha'] == 'builtin') {
$output .= '' . !empty($options['captcha_challenge_text'])?$options['captcha_challenge_text']:'Are you human? Please solve: ';
$captcha_id = wp_rand(1000, 9999);
$captcha = self::math_captcha_generate($captcha_id);
$output .= ' ';
$output .= ' ';
$output .= ' ';
$output .= '
';
}
return $output;
}
static function login_enqueue_scripts()
{
$options = WPCaptcha_Setup::get_options();
if ($options['captcha'] == 'recaptchav2') {
wp_enqueue_script('wpcaptcha-recaptcha', 'https://www.google.com/recaptcha/api.js', array('jquery'), self::$version, true);
} else if ($options['captcha'] == 'recaptchav3') {
wp_enqueue_script('wpcaptcha-recaptcha', 'https://www.google.com/recaptcha/api.js?onload=wpcaptchaRefreshToken&render=' . esc_html($options['captcha_site_key']), array('jquery'), self::$version, true);
}
}
static function login_scripts_print()
{
//phpcs:ignore this just prints the recaptcha scripts inline as they can be used in various forms where enqueing normally is not always working
echo self::login_scripts(false); //phpcs:ignore
}
static function login_scripts($output = false)
{
$options = WPCaptcha_Setup::get_options();
if(false === $output){
$output = '';
}
// scripts might need to be printed in odd contexts so wp_enqueue_script is not always ideal
if ($options['captcha'] == 'recaptchav2') {
$output .= ""; // phpcs:ignore
} else if ($options['captcha'] == 'recaptchav3') {
$output .= ""; // phpcs:ignore
}
return $output;
}
static function accesslock_screen($block_message = false)
{
$main_color = '#4285f4';
$secondary_color = '#8eb8ff';
echo '
';
echo '';
exit();
}
static function handle_unblock()
{
global $wpdb;
$options = WPCaptcha_Setup::get_options();
// no need for nonce check here as URL can be entered manually
if (isset($_GET['wpcaptcha_unblock']) && $options['global_unblock_key'] === $_GET['wpcaptcha_unblock']) { // phpcs:ignore
$user_ip = WPCaptcha_Utility::getUserIP();
if (!in_array($user_ip, $options['whitelist'])) {
$options['whitelist'][] = WPCaptcha_Utility::getUserIP();
}
update_option(WPCAPTCHA_OPTIONS_KEY, $options);
}
if (isset($_GET['wpcaptcha_unblock']) && strlen(sanitize_text_field(wp_unslash($_GET['wpcaptcha_unblock']))) == 32) { // phpcs:ignore
$unblock_key = sanitize_key($_GET['wpcaptcha_unblock']); // phpcs:ignore
$unblock_transient = get_transient('wpcaptcha_unlock_' . $unblock_key);
if ($unblock_transient == $unblock_key) {
$user_ip = WPCaptcha_Utility::getUserIP();
// phpcs:ignore db call warning as we are using a custom table
$wpdb->delete( // phpcs:ignore
$wpdb->wpcatcha_accesslocks,
array(
'accesslock_IP' => $user_ip
)
);
if (!in_array($user_ip, $options['whitelist'])) {
$options['whitelist'][] = WPCaptcha_Utility::getUserIP();
}
update_option(WPCAPTCHA_OPTIONS_KEY, $options);
}
}
}
static function wp_template_loader()
{
global $pagenow;
$pagenow = 'index.php';
if (!defined('WP_USE_THEMES')) {
define('WP_USE_THEMES', true);
}
wp();
require_once(ABSPATH . WPINC . '/template-loader.php');
die();
}
public static function pretty_fail_errors($error_code)
{
switch ($error_code) {
case 'wpcaptcha_location_blocked':
return 'Blocked Location';
break;
case 'wpcaptcha_fail_count':
return 'User exceeded maximum number of fails';
break;
case 'wpcaptcha_bot':
return 'Bot';
break;
case 'empty_username':
return 'Empty Username';
break;
case 'empty_password':
return 'Empty Password';
break;
case 'incorrect_password':
return 'Incorrect Password';
break;
case 'invalid_username':
return 'Invalid Username';
break;
case 'wpcaptcha_recaptchav2_not_submitted':
return 'reCAPTCHA v2 not submitted';
break;
case 'wpcaptcha_recaptchav3_not_submitted':
return 'reCAPTCHA v3 not submitted';
break;
case 'wpcaptcha_recaptchav2_failed':
return 'reCAPTCHA v2 failed verification';
break;
case 'wpcaptcha_recaptchav3_not_submitted':
return 'reCAPTCHA v3 failed verification';
break;
case 'wpcaptcha_builtin_captcha_failed':
return 'Built-in captcha failed verification';
break;
case 'wpcaptcha_hcaptcha_failed':
return 'hCaptcha failed verification';
break;
case 'wpcaptcha_icons_captcha_failed':
return 'Icon captcha failed verification';
default:
return 'Unknown';
break;
}
}
static function login_head()
{
$options = WPCaptcha_Setup::get_options();
if ($options['design_enable']) {
echo '';
}
}
static function get_templates()
{
$templates = array();
$templates['white'] = array(
'design_background_color' => '#FFFFFF',
'design_background_image' => '',
'design_logo' => 'white-wpcaptcha-icon',
'design_logo_width' => '100',
'design_logo_height' => '100',
'design_logo_margin_bottom' => '30',
'design_text_color' => '#300000',
'design_link_color' => '#06a8e8',
'design_link_hover_color' => '#005b93',
'design_form_border_color' => '#cbcbcb',
'design_form_border_width' => '1',
'design_form_width' => '',
'design_form_height' => '',
'design_form_padding' => '20',
'design_form_border_radius' => '4',
'design_form_background_color' => '#ffffff',
'design_form_background_image' => '',
'design_label_font_size' => '14',
'design_label_text_color' => '#383838',
'design_field_font_size' => '14',
'design_field_text_color' => '#222222',
'design_field_border_color' => '#d1d1d1',
'design_field_border_width' => '1',
'design_field_border_radius' => '2',
'design_field_background_color' => '#ffffff',
'design_button_font_size' => '14',
'design_button_text_color' => '#ffffff',
'design_button_border_color' => '#000000',
'design_button_border_width' => '0',
'design_button_border_radius' => '4',
'design_button_background_color' => '#595959',
'design_button_hover_text_color' => '#ffffff',
'design_button_hover_border_color' => '#ffffff',
'design_button_hover_background_color' => '#878787',
'design_custom_css' => ''
);
$templates['orange'] = array(
'design_background_color' => '#ef9b00',
'design_background_image' => '',
'design_logo' => 'white-wpcaptcha-icon',
'design_logo_width' => '100',
'design_logo_height' => '100',
'design_logo_margin_bottom' => '30',
'design_text_color' => '#4c3d00',
'design_link_color' => '#7c6e13',
'design_link_hover_color' => '#896709',
'design_form_border_color' => '#725f00',
'design_form_border_width' => '0',
'design_form_width' => '',
'design_form_height' => '',
'design_form_padding' => '20',
'design_form_border_radius' => '4',
'design_form_background_color' => '#f9e7ac',
'design_form_background_image' => '',
'design_label_font_size' => '14',
'design_label_text_color' => '#634000',
'design_field_font_size' => '14',
'design_field_text_color' => '#222222',
'design_field_border_color' => '#634000',
'design_field_border_width' => '1',
'design_field_border_radius' => '2',
'design_field_background_color' => '#ffffff',
'design_button_font_size' => '14',
'design_button_text_color' => '#ffffff',
'design_button_border_color' => '#634000',
'design_button_border_width' => '1',
'design_button_border_radius' => '4',
'design_button_background_color' => '#634000',
'design_button_hover_text_color' => '#ffffff',
'design_button_hover_border_color' => '#8c5f00',
'design_button_hover_background_color' => '#8c5f00',
'design_custom_css' => ''
);
$templates['red'] = array(
'design_background_color' => '#ce0000',
'design_background_image' => '',
'design_logo' => 'white-wpcaptcha-icon',
'design_logo_width' => '100',
'design_logo_height' => '100',
'design_logo_margin_bottom' => '30',
'design_text_color' => '#300000',
'design_link_color' => '#c91e1e',
'design_link_hover_color' => '#d15959',
'design_form_border_color' => '#c90000',
'design_form_border_width' => '2',
'design_form_width' => '',
'design_form_height' => '',
'design_form_padding' => '20',
'design_form_border_radius' => '4',
'design_form_background_color' => '#ffffff',
'design_form_background_image' => '',
'design_label_font_size' => '14',
'design_label_text_color' => '#383838',
'design_field_font_size' => '14',
'design_field_text_color' => '#222222',
'design_field_border_color' => '#d1d1d1',
'design_field_border_width' => '1',
'design_field_border_radius' => '2',
'design_field_background_color' => '#ffffff',
'design_button_font_size' => '14',
'design_button_text_color' => '#ffffff',
'design_button_border_color' => '#000000',
'design_button_border_width' => '0',
'design_button_border_radius' => '4',
'design_button_background_color' => '#d30000',
'design_button_hover_text_color' => '#ffffff',
'design_button_hover_border_color' => '#ffffff',
'design_button_hover_background_color' => '#9e0000',
'design_custom_css' => ''
);
$templates['green'] = array(
'design_background_color' => '#2c6600',
'design_background_image' => '',
'design_logo' => 'white-icon.png',
'design_logo_width' => '100',
'design_logo_height' => '100',
'design_logo_margin_bottom' => '30',
'design_text_color' => '#c6e500',
'design_link_color' => '#c6e500',
'design_link_hover_color' => '#acbf00',
'design_form_border_color' => '#c6e500',
'design_form_border_width' => '2',
'design_form_width' => '',
'design_form_height' => '',
'design_form_padding' => '20',
'design_form_border_radius' => '4',
'design_form_background_color' => '#4b7c01',
'design_form_background_image' => '',
'design_label_font_size' => '14',
'design_label_text_color' => '#ffffff',
'design_field_font_size' => '14',
'design_field_text_color' => '#222222',
'design_field_border_color' => '#87d642',
'design_field_border_width' => '1',
'design_field_border_radius' => '2',
'design_field_background_color' => '#3c7f02',
'design_button_font_size' => '14',
'design_button_text_color' => '#ffffff',
'design_button_border_color' => '#000000',
'design_button_border_width' => '0',
'design_button_border_radius' => '4',
'design_button_background_color' => '#66b500',
'design_button_hover_text_color' => '#ffffff',
'design_button_hover_border_color' => '#ffffff',
'design_button_hover_background_color' => '#a6d800',
'design_custom_css' => ''
);
$templates['blue'] = array(
'design_background_color' => '#005cb2',
'design_background_image' => '',
'design_logo' => 'white-icon.png',
'design_logo_width' => '100',
'design_logo_height' => '100',
'design_logo_margin_bottom' => '30',
'design_text_color' => '#300000',
'design_link_color' => '#2ca8ea',
'design_link_hover_color' => '#005b93',
'design_form_border_color' => '#008ed1',
'design_form_border_width' => '2',
'design_form_width' => '',
'design_form_height' => '',
'design_form_padding' => '20',
'design_form_border_radius' => '4',
'design_form_background_color' => '#ffffff',
'design_form_background_image' => '',
'design_label_font_size' => '14',
'design_label_text_color' => '#383838',
'design_field_font_size' => '14',
'design_field_text_color' => '#222222',
'design_field_border_color' => '#d1d1d1',
'design_field_border_width' => '1',
'design_field_border_radius' => '2',
'design_field_background_color' => '#ffffff',
'design_button_font_size' => '14',
'design_button_text_color' => '#ffffff',
'design_button_border_color' => '#000000',
'design_button_border_width' => '0',
'design_button_border_radius' => '4',
'design_button_background_color' => '#0084cc',
'design_button_hover_text_color' => '#ffffff',
'design_button_hover_border_color' => '#ffffff',
'design_button_hover_background_color' => '#005796',
'design_custom_css' => ''
);
$templates['gray'] = array(
'design_background_color' => '#353535',
'design_background_image' => '',
'design_logo' => 'white-icon.png',
'design_logo_width' => '100',
'design_logo_height' => '100',
'design_logo_margin_bottom' => '30',
'design_text_color' => '#300000',
'design_link_color' => '#06a8e8',
'design_link_hover_color' => '#005b93',
'design_form_border_color' => '#474747',
'design_form_border_width' => '2',
'design_form_width' => '',
'design_form_height' => '',
'design_form_padding' => '20',
'design_form_border_radius' => '4',
'design_form_background_color' => '#ffffff',
'design_form_background_image' => '',
'design_label_font_size' => '14',
'design_label_text_color' => '#383838',
'design_field_font_size' => '14',
'design_field_text_color' => '#222222',
'design_field_border_color' => '#d1d1d1',
'design_field_border_width' => '1',
'design_field_border_radius' => '2',
'design_field_background_color' => '#ffffff',
'design_button_font_size' => '14',
'design_button_text_color' => '#ffffff',
'design_button_border_color' => '#000000',
'design_button_border_width' => '0',
'design_button_border_radius' => '4',
'design_button_background_color' => '#595959',
'design_button_hover_text_color' => '#ffffff',
'design_button_hover_border_color' => '#ffffff',
'design_button_hover_background_color' => '#878787',
'design_custom_css' => ''
);
return $templates;
}
static function install_template()
{
check_admin_referer('wpcaptcha_install_template');
$options = WPCaptcha_Setup::get_options();
$template = false;
if (isset($_GET['template'])) {
$template = sanitize_key(wp_unslash($_GET['template']));
}
$templates = self::get_templates();
if (array_key_exists($template, $templates)) {
$options = array_merge($options, $templates[$template]);
if ($options['design_logo'] == 'white-wpcaptcha-icon') {
$options['design_logo'] = WPCAPTCHA_PLUGIN_URL . 'images/white-icon.png';
}
$options['design_template'] = $template;
$options['design_enable'] = 1;
update_option(WPCAPTCHA_OPTIONS_KEY, $options);
WPCaptcha_Admin::add_notice('template_activated', __('Template activated.', 'advanced-google-recaptcha'), 'success', true);
} else {
WPCaptcha_Admin::add_notice('template_not_found', __('Unknown template ID.', 'advanced-google-recaptcha'), 'error', true);
}
if (!empty($_GET['redirect'])) {
$redirect_url = sanitize_url(wp_unslash($_GET['redirect']));
wp_safe_redirect($redirect_url);
}
}
// convert HEX(HTML) color notation to RGB
static function hex2rgb($color)
{
if ($color[0] == '#') {
$color = substr($color, 1);
}
if (strlen($color) == 6) {
list($r, $g, $b) = array(
$color[0] . $color[1],
$color[2] . $color[3],
$color[4] . $color[5]
);
} elseif (strlen($color) == 3) {
list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]);
} else {
return array(255, 255, 255);
}
$r = hexdec($r);
$g = hexdec($g);
$b = hexdec($b);
return array($r, $g, $b);
} // html2rgb
// output captcha image
static function math_captcha_generate($captcha_id = false)
{
ob_start();
$a = wp_rand(0, (int) 10);
$b = wp_rand(0, (int) 10);
if(isset($_GET['color'])){ // phpcs:ignore
$color = substr($_GET['color'],0,7); // phpcs:ignore
$color = urldecode($color);
} else{
$color = '#FFFFFF';
}
if ($a > $b) {
$out = "$a - $b";
$captcha_value = $a - $b;
} else {
$out = "$a + $b";
$captcha_value = $a + $b;
}
$font = 5;
$width = ImageFontWidth($font) * strlen($out);
$height = ImageFontHeight($font);
$im = ImageCreate($width, $height);
$x = imagesx($im) - $width;
$y = imagesy($im) - $height;
$white = imagecolorallocate($im, 255, 255, 255);
$gray = imagecolorallocate($im, 66, 66, 66);
$black = imagecolorallocate($im, 0, 0, 0);
$trans_color = $white; //transparent color
if ($color) {
$color = self::hex2rgb($color);
$new_color = imagecolorallocate($im, $color[0], $color[1], $color[2]);
imagefill($im, 1, 1, $new_color);
} else {
imagecolortransparent($im, $trans_color);
}
imagestring($im, $font, $x, $y, $out, $black);
// always add noise
if (1 == 1) {
$color_min = 100;
$color_max = 200;
$rand1 = imagecolorallocate($im, wp_rand($color_min, $color_max), wp_rand($color_min, $color_max), wp_rand($color_min, $color_max));
$rand2 = imagecolorallocate($im, wp_rand($color_min, $color_max), wp_rand($color_min, $color_max), wp_rand($color_min, $color_max));
$rand3 = imagecolorallocate($im, wp_rand($color_min, $color_max), wp_rand($color_min, $color_max), wp_rand($color_min, $color_max));
$rand4 = imagecolorallocate($im, wp_rand($color_min, $color_max), wp_rand($color_min, $color_max), wp_rand($color_min, $color_max));
$rand5 = imagecolorallocate($im, wp_rand($color_min, $color_max), wp_rand($color_min, $color_max), wp_rand($color_min, $color_max));
$style = array($rand1, $rand2, $rand3, $rand4, $rand5);
imagesetstyle($im, $style);
imageline($im, wp_rand(0, $width), 0, wp_rand(0, $width), $height, IMG_COLOR_STYLED);
imageline($im, wp_rand(0, $width), 0, wp_rand(0, $width), $height, IMG_COLOR_STYLED);
imageline($im, wp_rand(0, $width), 0, wp_rand(0, $width), $height, IMG_COLOR_STYLED);
imageline($im, wp_rand(0, $width), 0, wp_rand(0, $width), $height, IMG_COLOR_STYLED);
imageline($im, wp_rand(0, $width), 0, wp_rand(0, $width), $height, IMG_COLOR_STYLED);
}
imagegif($im);
// Get image data
$image_data = ob_get_clean();
return array('value' => $captcha_value, 'img' => 'data:image/png;base64,' . base64_encode($image_data));
} // create
} // class