You are here

function _restrict_by_ip_validate_ip in Restrict Login or Role Access by IP Address 7.3

Same name and namespace in other branches
  1. 6.3 restrict_by_ip.module \_restrict_by_ip_validate_ip()
  2. 6.2 restrict_by_ip.module \_restrict_by_ip_validate_ip()

This function just makes sure the user input for the ip address section is valid.

5 calls to _restrict_by_ip_validate_ip()
RestrictByIpUnitTestCase::testRestrictByIpUnitTestIpValidation in ./restrict_by_ip.test
restrict_by_ip_login_add_edit_user_validate in ./restrict_by_ip.module
Validation function for add/edit login IP restriction form.
restrict_by_ip_login_settings_validate in ./restrict_by_ip.module
Validation function for global ip restriction settings
restrict_by_ip_role_settings_validate in ./restrict_by_ip.module
Validation function for role ip restriction settings
restrict_by_ip_user_profile_validate in ./restrict_by_ip.module
Custom validation function for the user_profile_form page.

File

./restrict_by_ip.module, line 554
Allows the admin to select which ip addresses role or a user can login from for this site Some of the code below is taken from the cck_ipaddress_module

Code

function _restrict_by_ip_validate_ip($ip_address) {
  $ret = array(
    'result' => TRUE,
    'messages' => '',
  );
  $ipaddresses = explode(";", $ip_address);

  // Check each ip address individually
  foreach ($ipaddresses as $ipaddress) {

    // Separate in to address and cidr mask
    $cidr = explode("/", $ipaddress);

    // Check address and cidr mask entered
    if (count($cidr) == 2) {
      $ipaddr = explode(".", $cidr[0]);

      // Check four octets entered
      if (count($ipaddr) == 4) {

        // Check each octet is valid - numeric and 0 < value < 255
        for ($i = 0; $i < count($ipaddr); $i++) {
          if (!is_numeric($ipaddr[$i]) || $ipaddr[$i] < 0 || $ipaddr[$i] > 255) {
            $ret['messages'][] .= 'Illegal value for an IP Address. Each IP Address must be valid.  Check IP Address ' . $ipaddress;
            $ret['result'] = FALSE;
          }
        }

        // Check cidr mask value - numeric and 0 < value < 33
        if (!is_numeric($cidr[1]) || $cidr[1] < 1 || $cidr[1] > 32) {
          $ret['messages'][] .= 'Illegal value for CIDR. Please correct CIDR with value of ' . $ipaddress;
          $ret['result'] = FALSE;
        }
      }
      else {
        $ret['messages'][] .= 'IP Address Incorrect Number of Octets. Check IP Address ' . $ipaddress;
        $ret['result'] = FALSE;
      }
    }
    else {
      $ret['messages'][] .= 'IP Address in Incorrect Format. Check IP Address ' . $ipaddress;
      $ret['result'] = FALSE;
    }

    // Check the validity of the network address in the given CIDR block,
    // by ensuring that the network address part is valid within the
    // CIDR block itself. If it's not, the notation is invalid.
    if ($ret['result'] && !_restrict_by_ip_cidrcheck($cidr[0], $ipaddress)) {
      $ret['messages'][] .= 'The network address in the "' . $ipaddress . '" block is not valid.';
      $ret['result'] = FALSE;
    }
  }
  return $ret;
}