';
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();
$numFails = $wpdb->get_var(
$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;
}
$wpdb->insert(
$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;
}
$wpdb->insert(
$wpdb->wpcatcha_accesslocks,
array(
'user_id' => $user_id,
'accesslock_date' => current_time('mysql'),
'release_date' => date('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();
$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)));
return $stillLocked;
}
static function is_rest_request()
{
if (defined('REST_REQUEST') && REST_REQUEST || isset($_GET['rest_route']) && strpos(sanitize_text_field(wp_unslash($_GET['rest_route'])), '/', 0) === 0) {
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 (!$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) {
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 (!is_string($password) || !is_string($userdata->user_pass) || is_null($userdata->ID) || !wp_check_password($password, $userdata->user_pass, $userdata->ID)) {
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()
{
$options = WPCaptcha_Setup::get_options();
if ($options['captcha'] == 'recaptchav2') {
if (!isset($_POST['g-recaptcha-response']) || empty($_POST['g-recaptcha-response'])) {
return new WP_Error('wpcaptcha_recaptchav2_not_submitted', __("ERROR: reCAPTCHA verification failed.