Parse Host Parts

Break a host down into subdomain, domain, and
suffix parts.

@param string $host Host.
@return array|bool Parts or false.
486863
public static function parse_host_parts($host) {
		// Tease out the hostname.
		if (false === ($host = static::parse_host($host))) {
			return false;
		}

		$out = static::HOST_PARTS;

		// If this is an IP, we don't have to do anything else.
		if (\filter_var($host, \FILTER_VALIDATE_IP)) {
			$out['host'] = $host;
			$out['domain'] = $host;
			return $out;
		}

		// Now the hard part. See if any parts of the host
		// correspond to a registered suffix.
		$suffixes = data::SUFFIXES;
		$suffix = array();
		$parts = \explode('.', $host);
		$parts = \array_reverse($parts);

		foreach ($parts as $k=>$part) {
			// Override rule.
			if (isset($suffixes[$part], $suffixes[$part]['!'])) {
				break;
			}

			// A match.
			if (isset($suffixes[$part])) {
				\array_unshift($suffix, $part);
				$suffixes = $suffixes[$part];
				unset($parts[$k]);
				continue;
			}

			// A wildcard.
			if (isset($suffixes['*'])) {
				\array_unshift($suffix, $part);
				$suffixes = $suffixes['*'];
				unset($parts[$k]);
				continue;
			}

			// We're done.
			break;
		}

		// The suffix can't be all there is.
		if (! \count($parts)) {
			return false;
		}

		// The domain.
		$parts = \array_reverse($parts);
		$out['domain'] = \array_pop($parts);

		// The subdomain.
		if (\count($parts)) {
			$out['subdomain'] = \implode('.', $parts);
		}

		// The suffix.
		if (\count($suffix)) {
			$out['suffix'] = \implode('.', $suffix);
		}

		$out['host'] = $host;

		return $out;
	}
318751
public final function withHost($host): UriInterface
   {
       if (!is_string($host) || !($parsedHost = $this->parseHost($host))) {
           throw new InvalidArgumentException(
               "Can't get with host: invalid host name");
       }
       return $this->with("host", $parsedHost);
   }
346427
public function host($host = null)
    {
        if (func_num_args() === 0) {
            $scheme = $this->_classes['scheme'];
            $port = $scheme::port($this->_scheme) !== $this->_port ? $this->_port : null;
            return ($port !== null ? "{$this->_host}:{$port}" : $this->_host);
        }
        if (strpos($host, ':')) {
            list($host, $port) = explode(':', $host);
            $this->port($port);
        }
        $this->_host = $host;
        $headers = $this->headers();
        $headers->prepend('Host', $this->host());
        return $this;
    }
972165
public function setHostFull(string $host) : self
    {
        $uri = new static($host);

        $this->uri['host'] = $uri->uri['host'];

        if (isset($uri->uri['scheme'])) {
            $this->uri['scheme'] = $uri->uri['scheme'];
        }

        if (isset($uri->uri['port'])) {
            $this->uri['port'] = $uri->uri['port'];
        }

        if (isset($uri->uri['user'])) {
            $this->uri['user'] = $uri->uri['user'];
        }

        if (isset($uri->uri['pass'])) {
            $this->uri['pass'] = $uri->uri['pass'];
        }

        return $this;
    }
141720
public function isBlocked($host)
    {
        $host = parse_url($host, PHP_URL_HOST);
        $host = utf8_encode(trim($host));

        if (empty($host)) return false;

        $fullDomain = $this->getFullDomain($host);
        $rootDomain = $this->getRootDomain($fullDomain);

        return $this->spammers()
            ->whereHostIn([$fullDomain, $rootDomain])
            ->whereBlocked()
            ->count() > 0;
    }
1373240
def _extract_subdomain(host):
        
        host = host.split(':')[0]
        # If the host is an IP address, there is no subdomain to extract
        try:
            # Check if the host is an ip address
            socket.inet_aton(host)
        except socket.error:
            # It isn't an IP address, return the subdomain
            return '.'.join(host.split('.')[:-2])
396861
public static function getConfigDomain($host)
    {
        if (!$host) return null;

        $longDomainEndings = array('or.at', 'co.at', 'gv.at', 'co.uk');

        // cleaning out the dots of long domain endings
        foreach ($longDomainEndings as $k => $v) {
            $longDomainEndings[$k] = str_replace('.', '', $v);
        }

        $hostParts = explode('.', $host);
        if (count($hostParts) <= 1) {
            $ret = $host;
        } else {
            $ret = $hostParts[count($hostParts)-2]  // zB 'vivid-planet'
                            .$hostParts[count($hostParts)-1]; // zB 'com'
        }
        if (in_array($ret, $longDomainEndings)) {
            $ret = $hostParts[count($hostParts)-3].$ret;
        }
        return $ret;
    }
1035310
public static function tryParse(string $host): ?HostInterface
    {
        if (!self::myParse($host, $hostname, $ipAddress)) {
            return null;
        }

        return new self($hostname, $ipAddress);
    }
729371
public function parseHost($string) {
        $segments = [
            'tld' => null,
            'domain' => null,
            'subdomain' => null,
        ];

        $parts = explode('.', $string);

        if (count($parts) == 1) {
            $segments['domain'] = $parts[0];

            return $segments;
        }

        if (count($parts)) {
            $segments['tld'] = array_pop($parts);
        }

        if (count($parts)) {
            $segments['domain'] = array_pop($parts);
        }

        if (count($parts)) {
            $segments['subdomain'] = implode('.', $parts);
        }

        return $this->replaceEmptyStringsWithNull($segments);
    }
675980
public function hasHost(string $host): bool
	{
		$host = Host::filter($host);
		return $this->offsetExists($host);
	}
1132249
public function host($host = null)
    {
        $this->host = empty($host) ? null : $host;

        return $this->host;
    }
1039417
public static function serializeHost($host)
    {
        if (is_int($host) || is_float($host)) {
            $string = self::serializeIPv4($host);
        } elseif (is_array($host)) {
            $string = '[' . self::serializeIPv6($host) . ']';
        } else {
            $string = (string)$host;
        }
        return $string;
    }
378889
public function host($host = '') {
        if ($host) {
            $this->host = $host;
        }
        if (!$this->host) {
            $this->host = self::server('HTTP_HOST');
        }
        return $this->host;
    }
94975
protected function getHostcd($host)
    {
        $pattern = '//epp:epp/epp:response/epp:resData/host:chkData/host:cd[host:name[php:functionString("XPath\quote", text()) = \'%s\']]';
        $query = sprintf($pattern, XPath\quote($host));
        $node = $this->getFirst($query);

        if ($node === null) {
            throw new UnexpectedValueException(sprintf('Host with name [%s] not found.', $host));
        }

        return $node;
    }
936999
public function getReason($host)
    {
        $hostcdNode = $this->getHostcd($host);
        $node = $this->getFirst('host:reason', $hostcdNode);

        return $node === null ? null : $node->nodeValue;
    }
493145
public function hostMatches($host)
    {
        $pattern = '@^' . str_replace('\*', '.*', preg_quote($this->options['_host'], '@')) . '$@';

        return preg_match($pattern, $host) !== 0;
    }
694746
public function withHost($host)
    {
        $uri = $this->getUri()->withHost($host);

        /** @var static $clone */
        $clone = $this->withUri($uri);

        return $clone;
    }