Plugin Directory

Changeset 3049438


Ignore:
Timestamp:
03/12/2024 01:19:26 AM (12 months ago)
Author:
dynahsty
Message:

v1.23

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ip-guard/tags/v1.23/ip-guard.php

    r3049421 r3049438  
    1616*/
    1717
    18 defined('ABSPATH') or die('No direct access allowed. Kindly exist');
    1918$max_ip_addresses = get_option('ip_guard_max_ip_addresses', 2);
    2019
     
    2524
    2625function ip_guard_authenticate($user, $username, $password) {
    27     if ($user instanceof WP_User) {
    28         if (in_array('administrator', (array) $user->roles)) {
    29             return $user;
    30         }
     26    if ($user instanceof WP_User && !in_array('administrator', (array) $user->roles)) {
    3127        $stored_ips = get_user_meta($user->ID, '_stored_ips', true);
    32 
    3328        if (!$stored_ips) {
    3429            $stored_ips = array();
     
    3833
    3934        if (filter_var($client_ip, FILTER_VALIDATE_IP)) {
    40            $escaped_ip = esc_attr($client_ip);
    41 
    42         } else {
    43         }
    44 
    45         if (!in_array($client_ip, $stored_ips)) {
    46             $stored_ips[] = $client_ip;
    47             update_user_meta($user->ID, '_stored_ips', $stored_ips);
    48         }
    49 
    50         $max_ip_addresses = intval(get_option('ip_guard_max_ip_addresses', 2));
    51 
    52         $all_similar = count(array_unique($stored_ips)) === 1;
    53 
    54         if (!$all_similar && count($stored_ips) >= $max_ip_addresses) {
    55             ip_guard_lock_user($user->ID);
    56             return new WP_Error('account_locked', __('Account locked due to unusual activity. Contact our support team.', 'ip-guard'));
     35            if (!in_array($client_ip, $stored_ips)) {
     36                $stored_ips[] = $client_ip;
     37                update_user_meta($user->ID, '_stored_ips', $stored_ips);
     38            }
     39
     40            $max_ip_addresses = intval(get_option('ip_guard_max_ip_addresses', 2));
     41
     42            $all_similar = ip_addresses_similar($stored_ips);
     43
     44            if (!$all_similar && count($stored_ips) >= $max_ip_addresses) {
     45                ip_guard_lock_user($user->ID);
     46                return new WP_Error('account_locked', __('Account locked due to unusual activity. Contact our support team.', 'ip-guard'));
     47            }
    5748        }
    5849    }
    5950
    6051    return $user;
     52}
     53
     54function ip_addresses_similar($ip_addresses) {
     55    $first_ip = reset($ip_addresses);
     56    foreach ($ip_addresses as $ip) {
     57        if ($ip !== $first_ip) {
     58            return false;
     59        }
     60    }
     61    return true;
    6162}
    6263
Note: See TracChangeset for help on using the changeset viewer.