You are here

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

Same name and namespace in other branches
  1. 8.2 src/MoAuthUtilities.php \Drupal\miniorange_2fa\MoAuthUtilities::check_for_valid_IPs()
1 call to MoAuthUtilities::check_for_valid_IPs()
MoAuthLoginSettings::submitForm in src/Form/MoAuthLoginSettings.php
Form submission handler.

File

src/MoAuthUtilities.php, line 175
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) {
  if (!empty(is_null($mo_saved_IP_address) || !empty($mo_saved_IP_address))) {
    $whitelisted_IP_array = explode(";", $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)) {
          \Drupal::messenger()
            ->addMessage(t("Invalid IP address. Please check lower range and upper range."), 'error');
          return;
        }
        $lower_range = ip2long($lower);
        $upper_range = ip2long($upper);
        if ($lower_range >= $upper_range) {
          \Drupal::messenger()
            ->addMessage(t("Invalid IP range. Please enter range in <strong>( lower_range - upper_range )</strong> format."), 'error');
          return;
        }
      }
      else {

        /* Check if it is a single IP address */
        if (!filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
          \Drupal::messenger()
            ->addMessage(t('<strong>' . $value . "</strong> Invalid IP address. Please enter valid IP address."), 'error');
          return;
        }
      }
    }
  }
}