| @@ -15,14 +15,14 @@ | ||
| 15 | 15 | |
| 16 | 16 | /** |
| 17 | 17 | * Check if the IP is IPv4 address. |
| 18 | 18 | * |
| 19 | - * @param mixed $ip IP address. | |
| 19 | + * @param string $ip The IP address to check. | |
| 20 | 20 | * |
| 21 | - * @return mixed | |
| 21 | + * @return bool Returns true if the IP address is an IPv4 address, false otherwise. | |
| 22 | 22 | */ |
| 23 | 23 | private function is_v4( $ip ) { |
| 24 | - return filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ); | |
| 24 | + return false !== filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ); | |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | 28 | * Check if the given IP address is an IPv6 address. |
| @@ -31,9 +31,9 @@ | ||
| 31 | 31 | * |
| 32 | 32 | * @return bool Returns true if the IP address is an IPv6 address, false otherwise. |
| 33 | 33 | */ |
| 34 | 34 | private function is_v6( $ip ) { |
| 35 | - return filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ); | |
| 35 | + return false !== filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ); | |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | 39 | * Check if IPv6 is supported. |
| @@ -133,9 +133,9 @@ | ||
| 133 | 133 | $bits = 32; |
| 134 | 134 | } |
| 135 | 135 | $ip = ip2long( $ip ); |
| 136 | 136 | $subnet = ip2long( $subnet ); |
| 137 | - $mask = - 1 << ( 32 - $bits ); | |
| 137 | + $mask = - 1 << ( 32 - (int) $bits ); | |
| 138 | 138 | $subnet &= $mask;// nb: in case the supplied subnet wasn't correctly aligned. |
| 139 | 139 | |
| 140 | 140 | return ( $ip & $mask ) === $subnet; |
| 141 | 141 | } |
| @@ -156,10 +156,10 @@ | ||
| 156 | 156 | $subnet = $this->expand_ip_v6( $subnet ); |
| 157 | 157 | $subnet = inet_pton( $subnet ); |
| 158 | 158 | $b_subnet = $this->ine_to_bits( $subnet ); |
| 159 | 159 | |
| 160 | - $ip_net_bits = substr( $b_ip, 0, $bits ); | |
| 161 | - $subnet_bits = substr( $b_subnet, 0, $bits ); | |
| 160 | + $ip_net_bits = substr( $b_ip, 0, (int) $bits ); | |
| 161 | + $subnet_bits = substr( $b_subnet, 0, (int) $bits ); | |
| 162 | 162 | |
| 163 | 163 | return $ip_net_bits === $subnet_bits; |
| 164 | 164 | } |
| 165 | 165 | |
| @@ -330,9 +330,10 @@ | ||
| 330 | 330 | } |
| 331 | 331 | } |
| 332 | 332 | |
| 333 | 333 | // @since 2.6.3 |
| 334 | - return (array) apply_filters( 'wd_display_ip_validations', $errors ); | |
| 334 | + $errors = apply_filters( 'wd_display_ip_validations', $errors ); | |
| 335 | + return is_array( $errors ) ? $errors : (array) $errors; | |
| 335 | 336 | } |
| 336 | 337 | |
| 337 | 338 | /** |
| 338 | 339 | * Validate IP. |
| @@ -458,9 +459,9 @@ | ||
| 458 | 459 | */ |
| 459 | 460 | public function check_ip_by_remote_addr( $blocked_ip ): string { |
| 460 | 461 | $ip_addr = defender_get_data_from_request( 'REMOTE_ADDR', 's' ); |
| 461 | 462 | |
| 462 | - return ! empty( $ip_addr ) ? $ip_addr : $blocked_ip; | |
| 463 | + return '' !== $ip_addr ? $ip_addr : $blocked_ip; | |
| 463 | 464 | } |
| 464 | 465 | |
| 465 | 466 | /** |
| 466 | 467 | * Check if the given IP is a private IP. |
| @@ -493,9 +494,9 @@ | ||
| 493 | 494 | * @return bool True if it's a valid server-originated loopback request; false otherwise. |
| 494 | 495 | */ |
| 495 | 496 | public function request_is_from_server(): bool { |
| 496 | 497 | $user_ips = $this->get_user_ip(); |
| 497 | - if ( empty( $user_ips ) ) { | |
| 498 | + if ( array() === $user_ips ) { | |
| 498 | 499 | return false; |
| 499 | 500 | } |
| 500 | 501 | |
| 501 | 502 | // Start building list of trusted server IPs. |
| @@ -525,17 +526,18 @@ | ||
| 525 | 526 | * @since 5.3.0 |
| 526 | 527 | * |
| 527 | 528 | * @param array $trusted_ips An array of trusted server IP addresses. |
| 528 | 529 | */ |
| 529 | - $trusted_ips = (array) apply_filters( 'wp_defender_server_ips', array_unique( $trusted_ips ) ); | |
| 530 | + $trusted_ips = apply_filters( 'wp_defender_server_ips', array_unique( $trusted_ips ) ); | |
| 531 | + $trusted_ips = ! is_array( $trusted_ips ) ? (array) $trusted_ips : $trusted_ips; | |
| 530 | 532 | // Only return true if the request IP is in the trusted list. |
| 531 | - if ( ! empty( array_intersect( $user_ips, $trusted_ips ) ) ) { | |
| 533 | + if ( array() !== array_intersect( $user_ips, $trusted_ips ) ) { | |
| 532 | 534 | return true; |
| 533 | 535 | } |
| 534 | 536 | |
| 535 | 537 | // User-Agent missing? Not a valid loopback. |
| 536 | 538 | $server_agent = defender_get_data_from_request( 'HTTP_USER_AGENT', 's' ); |
| 537 | - if ( empty( $server_agent ) ) { | |
| 539 | + if ( '' === $server_agent ) { | |
| 538 | 540 | return false; |
| 539 | 541 | } |
| 540 | 542 | |
| 541 | 543 | // User-Agent must match WordPress loopback format. |