|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Derafu: Support - Essential PHP Utilities. |
| 7 | + * |
| 8 | + * Copyright (c) 2025 Esteban De La Fuente Rubio / Derafu <https://www.derafu.dev> |
| 9 | + * Licensed under the MIT License. |
| 10 | + * See LICENSE file for more details. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace Derafu\Support; |
| 14 | + |
| 15 | +/** |
| 16 | + * IP utilities. |
| 17 | + * |
| 18 | + * Provides functionality to get the real client IP address from various headers |
| 19 | + * and validate if it is private or reserved. |
| 20 | + */ |
| 21 | +final class Ip |
| 22 | +{ |
| 23 | + /** |
| 24 | + * Gets the real client IP address from various headers. |
| 25 | + * |
| 26 | + * @return string The real client IP address. |
| 27 | + */ |
| 28 | + public static function getRealClientIp(): string |
| 29 | + { |
| 30 | + // Headers to check in order of priority. |
| 31 | + $headers = [ |
| 32 | + 'HTTP_X_FORWARDED_FOR', |
| 33 | + 'HTTP_X_REAL_IP', |
| 34 | + 'HTTP_CF_CONNECTING_IP', // Cloudflare. |
| 35 | + 'HTTP_X_CLIENT_IP', |
| 36 | + 'HTTP_X_FORWARDED', |
| 37 | + 'HTTP_FORWARDED_FOR', |
| 38 | + 'HTTP_FORWARDED', |
| 39 | + 'HTTP_X_CLUSTER_CLIENT_IP', |
| 40 | + 'HTTP_X_FORWARDED_FOR_IP', |
| 41 | + 'HTTP_VIA', |
| 42 | + 'HTTP_X_VIA', |
| 43 | + 'HTTP_X_COMING_FROM', |
| 44 | + 'HTTP_COMING_FROM', |
| 45 | + 'HTTP_X_COMING_FROM_IP', |
| 46 | + ]; |
| 47 | + |
| 48 | + foreach ($headers as $header) { |
| 49 | + if (!empty($_SERVER[$header])) { |
| 50 | + $ip = $_SERVER[$header]; |
| 51 | + |
| 52 | + // If there are multiple IPs (separated by comma), take the first one. |
| 53 | + if (str_contains($ip, ',')) { |
| 54 | + $ips = explode(',', $ip); |
| 55 | + $ip = trim($ips[0]); |
| 56 | + } |
| 57 | + |
| 58 | + // Validate that it is a valid IP address. |
| 59 | + $isValid = filter_var( |
| 60 | + $ip, |
| 61 | + FILTER_VALIDATE_IP, |
| 62 | + FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE |
| 63 | + ); |
| 64 | + if ($isValid !== false) { |
| 65 | + return $ip; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Fallback to REMOTE_ADDR. |
| 71 | + return $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0'; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Gets the real client IP address (alias for compatibility). |
| 76 | + * |
| 77 | + * @return string The real client IP address. |
| 78 | + */ |
| 79 | + public static function getClientIp(): string |
| 80 | + { |
| 81 | + return self::getRealClientIp(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Checks if the IP is private. |
| 86 | + * |
| 87 | + * @param string $ip The IP address to check. |
| 88 | + * @return bool True if the IP is private. |
| 89 | + */ |
| 90 | + public static function isPrivateIp(string $ip): bool |
| 91 | + { |
| 92 | + return !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Checks if the IP is reserved. |
| 97 | + * |
| 98 | + * @param string $ip The IP address to check. |
| 99 | + * @return bool True if the IP is reserved. |
| 100 | + */ |
| 101 | + public static function isReservedIp(string $ip): bool |
| 102 | + { |
| 103 | + return !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Gets detailed information about the client IP address. |
| 108 | + * |
| 109 | + * Formatted as: |
| 110 | + * |
| 111 | + * ```php |
| 112 | + * [ |
| 113 | + * 'real_ip' => string, |
| 114 | + * 'remote_addr' => string, |
| 115 | + * 'is_private' => bool, |
| 116 | + * 'is_reserved' => bool, |
| 117 | + * 'headers' => array<string,string|null>, |
| 118 | + * ] |
| 119 | + * ``` |
| 120 | + * |
| 121 | + * @return array<string,mixed> Detailed information about the client IP address. |
| 122 | + */ |
| 123 | + public static function getClientIpInfo(): array |
| 124 | + { |
| 125 | + $realIp = self::getRealClientIp(); |
| 126 | + $remoteAddr = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0'; |
| 127 | + |
| 128 | + return [ |
| 129 | + 'real_ip' => $realIp, |
| 130 | + 'remote_addr' => $remoteAddr, |
| 131 | + 'is_private' => self::isPrivateIp($realIp), |
| 132 | + 'is_reserved' => self::isReservedIp($realIp), |
| 133 | + 'headers' => [ |
| 134 | + 'X-Forwarded-For' => $_SERVER['HTTP_X_FORWARDED_FOR'] ?? null, |
| 135 | + 'X-Real-IP' => $_SERVER['HTTP_X_REAL_IP'] ?? null, |
| 136 | + 'CF-Connecting-IP' => $_SERVER['HTTP_CF_CONNECTING_IP'] ?? null, |
| 137 | + 'X-Client-IP' => $_SERVER['HTTP_X_CLIENT_IP'] ?? null, |
| 138 | + ], |
| 139 | + ]; |
| 140 | + } |
| 141 | +} |
0 commit comments