You are here

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

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()
mo_auth_login_settings_submit in ./mo_auth_login_settings.inc
Send support query.

File

classes/Utilities.php, line 379
This file is part of miniOrange 2FA module.

Class

MoAuthUtilities
@file This file is part of miniOrange 2FA module.

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 (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;
}