You are here

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

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

This function just makes sure the user input for the ip address section is valid. $sms is the error message if the ip validation fails

2 calls to _restrict_by_ip_validate_ip()
restrict_by_ip_user_admin_perm_validate in ./restrict_by_ip.module
Hook for the permissions validate function. Checks to make sure the ip-address is valid.
_restrict_by_ip_validate in ./restrict_by_ip.module
Validate function Validate the restrict by ip form data

File

./restrict_by_ip.module, line 369
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, $sms) {
  $ret = TRUE;
  $ipaddresses = explode(";", $ip_address);
  $j = 1;

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

    // Seperate 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 quads entered
      if (count($ipaddr) == 4) {

        // Check each quad is valid - numeric and 0 < value < 255
        for ($i = 0; $i < 4; $i++) {
          if (!is_numeric($ipaddr[$i]) || $ipaddr[$i] < 0 || $ipaddr[$i] > 255) {
            form_set_error('restrict_by_ip_address', t($sms . '. Illegal value for the an IP Address. Each IP Address must be valid. Check IP Address No. ' . $j));
            $ret = FALSE;
          }
        }

        // Check cidr mask value - numeric and 0 < value < 33
        if (!is_numeric($cidr[1]) || $cidr[1] < 1 || $cidr[1] > 32) {
          form_set_error('restrict_by_ip_address', t($sms . '. Illegal value for the CIDR value. Check CIDR No. ' . $j));
          $ret = FALSE;
        }
      }
      else {
        form_set_error('restrict_by_ip_address', t($sms . '. IP Address Incorrect Number of Quads. Check IP Address No. ' . $j));
        $ret = FALSE;
      }
    }
    else {
      form_set_error('restrict_by_ip_address', t($sms . '. IP Address in Incorrect Format. Check IP Address No. ' . $j));
      $ret = FALSE;
    }
    $j++;
  }
  return $ret;
}