Validate Domain

@param String $scheme HTTP scheme [http, https]
@param String $host   HTTP host   [domain.ext]
545888
public static function validate_domain(String $scheme, String $host)
    {
        // Dynamic URL
        $url = $scheme . '://' . $host;

        // Validate URL
        if (self::validate_url($url)) {
            // Validate HTTP request scheme
            self::validate_scheme(parse_url($url)['scheme']);
        } else {
            throw new Exception('Invalid URL: "' . $url . '"');
        }
    }
1164615
public function validate($field, $value, $validator) {

		if(empty($value)) return false;
		if(!filter_var($value, FILTER_VALIDATE_URL)) return false;

		$url = parse_url($value);
		if(!in_array($url['scheme'], $this->allowed_protocols)) return false;

		if($this->allowed_domains === null) return true;

		return in_array($url['host'], $this->allowed_domains);

	}
1109564
private function set_scheme($scheme)
    {
        if ($scheme === null)
        {
            $this->scheme = null;
        }
        elseif (!preg_match('/^[A-Za-z][0-9A-Za-z+\-.]*$/', $scheme))
        {
            $this->scheme = null;
            return false;
        }
        else
        {
            $this->scheme = strtolower($scheme);
        }
        return true;
    }
8030
protected function setHost($host)
    {
        $host = strtolower($host);
        
        if ($host !== '' && !$this->isValidDomain($host) && !$this->isValidIpv4($host) && !$this->isValidIpv6($host)) {
            throw new \InvalidArgumentException("Invalid hostname '$host'");
        }
        
        $this->host = $host;
    }
359799
public static function validDomains($url, $domains)
    {
        $theDomain = parse_url($url);
        foreach ($domains as $domain) {
            $check = strpos($theDomain['host'], $domain->client_domain);
            if ($check || $check === 0) {
                return true;
            }
        }
        return false;
    }
209971
private function validateUrl($url)
    {
        $parsed = parse_url($url);
        if (empty($parsed['scheme'])
            || empty($parsed['host'])
            || $parsed['scheme'] !== 'https'
            || substr($url, -4) !== '.pem'
            || !preg_match($this->hostPattern, $parsed['host'])
        ) {
            throw new InvalidSnsMessageException(
                'The certificate is located on an invalid domain.'
            );
        }
    }
1004499
public function matchDomain($domain)
    {
        $host = $this->host;
        if (is_null($host)) {
            return false;
        }

        $match_domain = explode('.', $domain);
        $match_host = array_slice(explode('.', $host), count($match_domain) * -1);

        return (strcasecmp($domain, implode('.', $match_host)) === 0);
    }
275249
def validate(self):
        
        if (self.scheme is None or self.scheme != '') \
          and (self.host is None or self.host == ''):
            return False
        return True
1220301
public static function parse(string $scheme): SchemeInterface
    {
        if (!self::myParse($scheme, $result, $type, $defaultPort, $error)) {
            throw new SchemeInvalidArgumentException($error);
        }

        return new self($result, $type, $defaultPort);
    }
946536
public function init(string $scheme, string $host, int $port): self
    {
        return $this
            ->setServerOrigin($scheme, $host, $port)
            ->setPreFlightCacheMaxAge(0)
            ->setCredentialsNotSupported()
            ->enableAllOriginsAllowed()
            ->setAllowedOrigins([])
            ->enableAllMethodsAllowed()
            ->setAllowedMethods([])
            ->enableAllHeadersAllowed()
            ->setAllowedHeaders([])
            ->setExposedHeaders([])
            ->disableAddAllowedMethodsToPreFlightResponse()
            ->disableAddAllowedHeadersToPreFlightResponse()
            ->disableCheckHost();
    }
341454
public function validate($host = null, $ip = null)
    {
        $this->validHost($host);
        $this->validIp($ip);
    }
743124
private static function isAllowedScheme($scheme)
    {
        if (preg_match(AbstractRequest::HTTP, $scheme) === 1) {
            return 'http';
        } elseif (preg_match(AbstractRequest::FTP, $scheme) === 1) {
            return 'ftp';
        } elseif (preg_match(AbstractRequest::SSH, $scheme) === 1) {
            return 'ssh';
        } else {
            return false;
        }
    }
566072
public function setScheme($scheme)
    {
        if ($this->scheme == 'http' && $this->port == 80) {
            $this->port = null;
        } elseif ($this->scheme == 'https' && $this->port == 443) {
            $this->port = null;
        }

        $this->scheme = $scheme;

        return $this;
    }
402766
public static function matchCookieDomain($cookieDomain, $host)
    {
        if (! $cookieDomain) {

            throw new Zend_Http_Exception("\$cookieDomain is expected to be a cookie domain");
        }

        if (! $host) {

            throw new Zend_Http_Exception("\$host is expected to be a host name");
        }

        $cookieDomain = strtolower($cookieDomain);
        $host = strtolower($host);

        if ($cookieDomain[0] == '.') {
            $cookieDomain = substr($cookieDomain, 1);
        }

        // Check for either exact match or suffix match
        return ($cookieDomain == $host ||
                preg_match('/\.' . preg_quote($cookieDomain) . '$/', $host));
    }
560791
public function validateHttpHost(&$element, FormStateInterface $form_state)
    {
        $value = $form_state->getValue($element['#parents']);

        if (empty($value)) {
            $form_state->setError($element, $this->t("Host name cannot be empty"));

            return;
        }

        $existing = $this->manager->getStorage()->findByHostname($value);
        if ($existing && $existing->getId() != $form_state->getTemporaryValue('site')->getId()) {
            $form_state->setError($element, $this->t("Host name already exists"));
        }

        // Validate host name format
        $regex = '@^(([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])$@i';
        if (!preg_match($regex, $value)) {
            $form_state->setError($element, $this->t("Host name contains invalid characters or has a wrong format"));
        }
    }
305434
public function checkPermission($host, $context)
    {
        if (!is_string($host)) {
            throw new \TypeError('The host parameter must be a string.');
        }
        if (!$host) {
            throw new \InvalidArgumentException('The host parameter cannot be empty.');
        }

        $currentRequest = $this->requestStack->getCurrentRequest();

        if (!$currentRequest) {
            return false;
        }

        return !!preg_match('{'.$host.'}i', $currentRequest->getHost());
    }
253528
public static function toUrl(string $url, string $scheme = 'http://'): string
    {
        $http = preg_match('/^http[s]?:\/\//', $url);
        return (!$http ? $scheme : '') . $url;
    }