You are here

public static function MoAuthUtilities::check_for_valid_IPs in Google Authenticator / 2 Factor Authentication - 2FA 8.2

Same name and namespace in other branches
  1. 8 src/MoAuthUtilities.php \Drupal\miniorange_2fa\MoAuthUtilities::check_for_valid_IPs()

Parameters

$mo_saved_IP_address = IP Addresses entered by user:

Return value

boolean | string if error Check whether provided IP is valid or not

1 call to MoAuthUtilities::check_for_valid_IPs()
MoAuthLoginSettings::validateForm in src/Form/MoAuthLoginSettings.php
Form validation handler.

File

src/MoAuthUtilities.php, line 330
This file is part of miniOrange 2FA module.

Class

MoAuthUtilities

Namespace

Drupal\miniorange_2fa

Code

public static function check_for_valid_IPs($mo_saved_IP_address) {

  /** Separate IP address with the semicolon (;) **/
  $whitelisted_IP_array = explode(";", rtrim($mo_saved_IP_address, ";"));
  foreach ($whitelisted_IP_array as $key => $value) {
    if ($value == "::1") {
      continue;
    }
    if (stristr($value, '-')) {

      /** Check if it is a range of IP address **/
      list($lower, $upper) = explode('-', $value, 2);
      if (!filter_var($lower, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) && !filter_var($upper, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
        return "Invalid IP (<strong> " . $lower . "-" . $upper . "</strong> ) address. Please check lower range and upper range.";
      }
      $lower_range = ip2long($lower);
      $upper_range = ip2long($upper);
      if ($lower_range >= $upper_range) {
        return "Invalid IP range (<strong> " . $lower . "-" . $upper . "</strong> ) address. Please enter range in <strong>( lower_range - upper_range )</strong> format.";
      }
    }
    else {

      /** Check if it is a single IP address **/
      if (!filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
        return " Invalid IP (<strong> " . $value . "</strong> ) address. Please enter valid IP address.";
      }
    }
  }
  return TRUE;
}