Inspekt\Inspekt::isHostname PHP Метод

isHostname() публичный статический Метод

Depending upon the value of $allow, Internet domain names, IP addresses, and/or local network names are considered valid. The default is HOST_ALLOW_ALL, which considers all of the above to be valid.
public static isHostname ( mixed $value, integer $allow = self::ISPK_HOST_ALLOW_ALL ) : boolean
$value mixed
$allow integer bitfield for self::ISPK_HOST_ALLOW_DNS, self::ISPK_HOST_ALLOW_IP, self::ISPK_HOST_ALLOW_LOCAL
Результат boolean
    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;
        }
    }

Usage Example

Пример #1
0
 /**
  * @expectedException \Inspekt\Exception
  */
 public function testIsHostnameBadAllow3()
 {
     $input = '192.168.1.1';
     $this->assertTrue(Inspekt::isHostname($input, 8));
 }