You are here

function _field_ipaddress_shorthand2dottedquad in IP address fields 7

Take shorthand input and return the dotted quad start and end range.

@example "111.111.111.*" => array("111.111.111.0", "111.111.111.255") "111.111.111.111-222" => array("111.111.111.111", "111.111.111.222")

Parameters

input $string An IP range.:

Return value

array Start and end addresses in dotted quad format, else FALSE.

1 call to _field_ipaddress_shorthand2dottedquad()
_field_ipaddress_shorthand2long in ./field_ipaddress.module

File

./field_ipaddress.module, line 384

Code

function _field_ipaddress_shorthand2dottedquad($network) {
  $star = strpos($network, '*');
  $dash = strpos($network, '-');
  if (ctype_alpha($network[strlen($network) - 1])) {

    // Hostname conversion
    $start = $end = gethostbyname($network);
    if ($start === $network) {
      return FALSE;
    }
  }
  else {

    // Simple validation
    if ($star === false && $dash === false) {
      $res = long2ip(ip2long($network));
      $start = $end = $res;
      if ($res === '0.0.0.0') {
        return FALSE;
      }
    }

    // Using a star
    if ($star !== false) {
      $start = long2ip(ip2long(str_replace('*', '0', $network)));
      $end = long2ip(ip2long(str_replace('*', '255', $network)));
      if ($start === '0.0.0.0' || $end === '0.0.0.0') {
        return FALSE;
      }
    }

    // Using a dash
    if ($dash !== FALSE) {
      list($start, $end) = explode('-', $network);

      // Check whether $end is a full IP or just the last quad
      if (strpos($end, '.') !== FALSE) {
        $end = long2ip(ip2long(trim($end)));
      }
      else {

        // Get the first 3 quads of the start address
        $classc = substr($start, 0, strrpos($start, '.'));
        $classc .= '.' . $end;
        $end = long2ip(ip2long(trim($classc)));
      }

      // Check for failure
      $start = long2ip(ip2long(trim($start)));
      if ($start === '0.0.0.0' || $end === '0.0.0.0') {
        return FALSE;
      }
    }
  }
  return array(
    $start,
    $end,
  );
}