public static function isHostname($value, $allow = self::ISPK_HOST_ALLOW_ALL)
{
if (!is_numeric($allow) || !is_int($allow)) {
throw new Exception('Illegal value for $allow; expected an integer');
}
if ($allow < self::ISPK_HOST_ALLOW_DNS || self::ISPK_HOST_ALLOW_ALL < $allow) {
throw new Exception('Illegal value for $allow; expected integer between ' . self::ISPK_HOST_ALLOW_DNS . ' and ' . self::ISPK_HOST_ALLOW_ALL);
}
// determine whether the input is formed as an IP address
$status = self::isIp($value);
// if the input looks like an IP address
if ($status) {
// if IP addresses are not allowed, then fail validation
if (($allow & self::ISPK_HOST_ALLOW_IP) == 0) {
return false;
}
// IP passed validation
return true;
}
// check input against domain name schema
$status = @preg_match('/^(?:[^\\W_]((?:[^\\W_]|-){0,61}[^\\W_])?\\.)+[a-zA-Z]{2,6}\\.?$/', $value);
if ($status === false) {
throw new Exception('Internal error: DNS validation failed');
}
// if the input passes as an Internet domain name, and domain names are allowed, then the hostname
// passes validation
if ($status == 1 && ($allow & self::ISPK_HOST_ALLOW_DNS) != 0) {
return true;
}
// if local network names are not allowed, then fail validation
if (($allow & self::ISPK_HOST_ALLOW_LOCAL) == 0) {
return false;
}
// check input against local network name schema; last chance to pass validation
$status = @preg_match('/^(?:[^\\W_](?:[^\\W_]|-){0,61}[^\\W_]\\.)*(?:[^\\W_](?:[^\\W_]|-){0,61}[^\\W_])\\.?$/', $value);
if ($status === false) {
throw new Exception('Internal error: local network name validation failed');
}
if ($status == 0) {
return false;
} else {
return true;
}
}