fix square quoting for fsockopen

This commit is contained in:
ghost 2023-12-16 20:02:06 +02:00
parent cc9ddf3647
commit 0d840357e6

View File

@ -6,15 +6,30 @@ namespace Yggverse\Net;
class Socket class Socket
{ {
public static function isIPv4(mixed $value): bool
{
return false !== filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
}
public static function isIPv6(mixed $value): bool
{
return false !== filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
}
public static function isHostName(mixed $value): bool
{
return false !== filter_var($value, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
}
public static function isHost(mixed $value): bool public static function isHost(mixed $value): bool
{ {
return return
( (
is_string($value) && is_string($value) &&
( (
false !== filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) || false !== self::isIPv4($value) ||
false !== filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || false !== self::isIPv6($value) ||
false !== filter_var($value, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) false !== self::isHostName($value)
) )
); );
} }
@ -37,7 +52,7 @@ class Socket
if (self::isHost($host) && self::isPort($port)) if (self::isHost($host) && self::isPort($port))
{ {
$connection = @fsockopen( $connection = @fsockopen(
$host, self::isIPv6($host) ? "[$host]" : $host, // PHP is shit 💩.
$port, $port,
$error_code, $error_code,
$error_message, $error_message,