*/ class Network { /** * Check if a hostname is public and non-reserved * * @param string $host * @return bool */ public static function isPublicHost(string $host): bool { if (!$host) { return false; } if ($host === 'localhost') { return false; } // RFC 2606 if ($host === 'example.com' || $host === 'example.net' || $host === 'example.org') { return false; } // RFC 2606 -continued $tld = substr($host, strrpos($host, '.')); if ($tld === '.test' || $tld === '.example' || $tld === '.invalid' || $tld === '.localhost') { return false; } // Private/Reserved IP ranges if (filter_var($host, FILTER_VALIDATE_IP) && !filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { return false; } return true; } }