diff --git a/ludicrousdb/includes/class-ludicrousdb.php b/ludicrousdb/includes/class-ludicrousdb.php index 62f242f..7065c5d 100644 --- a/ludicrousdb/includes/class-ludicrousdb.php +++ b/ludicrousdb/includes/class-ludicrousdb.php @@ -1294,7 +1294,7 @@ protected function single_db_connect( $dbhname, $host, $user, $password ) { $port_or_socket = substr( $port_or_socket, 1 ); if ( 0 !== strpos( $port_or_socket, '/' ) ) { - $port = intval( $port_or_socket ); + $port = (int) $port_or_socket; $maybe_socket = strstr( $port_or_socket, ':' ); if ( ! empty( $maybe_socket ) ) { @@ -2311,9 +2311,39 @@ public function log_query( $query = '', $query_time = 0, $query_callstack = '', * @param int $port Port or socket. * @param float $float_timeout Timeout in seconds, as float number (). * - * @return bool true when $host:$post responds within $float_timeout seconds, else false + * @return bool true when $host:$port responds within $float_timeout seconds, else false */ public function check_tcp_responsiveness( $host, $port, $float_timeout ) { + $socket = ''; + + // Bail if disabled + if ( empty( $this->check_tcp_responsiveness ) ) { + $this->tcp_responsive = true; + return true; + } + + // Maybe split host:port:socket into $host and $port and $socket + if ( strpos( $host, ':' ) ) { + $port_or_socket = strstr( $host, ':' ); + + $host = substr( $host, 0, strpos( $host, ':' ) ); + $port_or_socket = substr( $port_or_socket, 1 ); + + if ( 0 !== strpos( $port_or_socket, '/' ) ) { + $port = (int) $port_or_socket; + $maybe_socket = strstr( $port_or_socket, ':' ); + + if ( ! empty( $maybe_socket ) ) { + $socket = substr( $maybe_socket, 1 ); + } + } else { + $socket = $port_or_socket; + } + } + + if ( ! empty( $socket ) ) { + $socket = 'unix://' . $socket; + } // Get the cache key $cache_key = $this->tcp_get_cache_key( $host, $port ); @@ -2332,24 +2362,25 @@ public function check_tcp_responsiveness( $host, $port, $float_timeout ) { return false; } - if ( empty( $this->check_tcp_responsiveness ) ) { - $this->tcp_responsive = true; - return true; - } - // Defaults $errno = 0; $errstr = ''; // Try to get a new socket // phpcs:disable - $socket = $this->is_debug() - ? fsockopen( $host, $port, $errno, $errstr, $float_timeout ) - : @fsockopen( $host, $port, $errno, $errstr, $float_timeout ); + if ( empty( $socket ) ) { + $check_socket = $this->is_debug() + ? fsockopen( $host, $port, $errno, $errstr, $float_timeout ) + : @fsockopen( $host, $port, $errno, $errstr, $float_timeout ); + } else { + $check_socket = $this->is_debug() + ? fsockopen( $socket, - 1, $errno, $errstr, $float_timeout ) + : @fsockopen( $socket, - 1, $errno, $errstr, $float_timeout ); + } // phpcs:enable // No socket - if ( false === $socket ) { + if ( false === $check_socket ) { $this->tcp_cache_set( $cache_key, 'down' ); $this->tcp_responsive = false; @@ -2358,7 +2389,7 @@ public function check_tcp_responsiveness( $host, $port, $float_timeout ) { // Close the socket // phpcs:ignore - fclose( $socket ); + fclose( $check_socket ); // Using API $this->tcp_cache_set( $cache_key, 'up' );