PluginProbe
OPcache Manager / 3.3.0
OPcache Manager v3.3.0
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 2.0.0 2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.14.0 2.2.0 2.3.0 2.3.1 2.3.2 2.4.0 2.5.0 2.6.0 All 36 releases
opcache-manager / includes / system / class-ip.php

class-ip.php in OPcache Manager 3.3.0, at includes/system/class-ip.php

182 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * IP handling
4 *
5 * Handles all IP operations and transformation.
6 *
7 * @package System
8 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
9 * @since 1.0.0
10 */
11
12 namespace OPcacheManager\System;
13
14 /**
15 * Define the IP functionality.
16 *
17 * Handles all IP operations and transformation.
18 *
19 * @package System
20 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
21 * @since 1.0.0
22 */
23 class IP {
24
25 /**
26 * The list of char to clean.
27 *
28 * @since 1.0.0
29 * @var array $clean Maintains the char list.
30 */
31 private static $clean = [ '"', '%' ];
32
33 /**
34 * Verify if the current client IP is private.
35 *
36 * @return boolean True if the IP is private, false otherwise.
37 * @since 1.0.0
38 */
39 public static function is_current_private() {
40 return ! filter_var( self::get_current(), FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE );
41 }
42
43 /**
44 * Verify if the current client IP is public.
45 *
46 * @return boolean True if the IP is public, false otherwise.
47 * @since 1.0.0
48 */
49 public static function is_current_public() {
50 return ! self::is_current_private();
51 }
52
53 /**
54 * Try to extract real ip if any exists.
55 *
56 * @param array $iplist A list of IPs.
57 * @param boolean $include_private optional. Include private IPs too.
58 * @return string The real ip if found, '' otherwise.
59 * @since 1.0.0
60 */
61 public static function maybe_extract_ip( $iplist, $include_private = false ) {
62 if ( $include_private ) {
63 foreach ( $iplist as $ip ) {
64 if ( filter_var( trim( $ip ), FILTER_VALIDATE_IP ) ) {
65 return self::expand( $ip );
66 }
67 }
68 }
69 foreach ( $iplist as $ip ) {
70 if ( filter_var( trim( $ip ), FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) ) {
71 return self::expand( $ip );
72 }
73 }
74 return '';
75 }
76
77 /**
78 * Get the current client IP.
79 *
80 * @return string The current client IP.
81 * @since 1.0.0
82 */
83 public static function get_current() {
84 for ( $i = 0 ; $i < 2 ; $i++ ) {
85 foreach (
86 [
87 'HTTP_FORWARDED',
88 'HTTP_FORWARDED_FOR',
89 'HTTP_X_FORWARDED',
90 'HTTP_CLIENT_IP',
91 'HTTP_X_FORWARDED_FOR',
92 ] as $field
93 ) {
94 if ( array_key_exists( $field, $_SERVER ) ) {
95 $ip = self::maybe_extract_ip( array_reverse( explode( ',', filter_input( INPUT_SERVER, $field ) ) ), 1 === $i );
96 if ( '' !== $ip ) {
97 return $ip;
98 }
99 }
100 }
101 foreach (
102 [
103 'HTTP_X_CLUSTER_CLIENT_IP',
104 'HTTP_CF_CONNECTING_IP',
105 'HTTP_X_REAL_IP',
106 'REMOTE_ADDR',
107 ] as $field
108 ) {
109 if ( array_key_exists( $field, $_SERVER ) ) {
110 $ip = self::maybe_extract_ip( explode( ',', (string) filter_input( INPUT_SERVER, $field ) ?? '' ), 1 === $i );
111 if ( '' !== $ip ) {
112 return $ip;
113 }
114 }
115 }
116
117 }
118 return '127.0.0.1';
119 }
120
121 /**
122 * Expands an IPv4 or IPv6 address.
123 *
124 * @param string $ip The IP to expand.
125 * @return string The expanded IP.
126 * @since 1.0.0
127 */
128 public static function expand( $ip ) {
129 if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) ) {
130 return self::expand_v6( $ip );
131 }
132 if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {
133 return self::expand_v4( $ip );
134 }
135 return '';
136 }
137
138 /**
139 * Expands an IPv4 address.
140 *
141 * @param string $ip The IP to expand.
142 * @return string The expanded IP.
143 * @since 1.0.0
144 */
145 public static function expand_v4( $ip ) {
146 return long2ip( ip2long( str_replace( self::$clean, '', $ip ) ) );
147 }
148
149 /**
150 * Normalizes an IPv4 address.
151 *
152 * @param string $ip The IP to normalize.
153 * @return string The normalized IP.
154 * @since 1.0.0
155 */
156 public static function normalize_v4( $ip ) {
157 return long2ip( (int) str_replace( self::$clean, '', $ip ) );
158 }
159
160 /**
161 * Expands an IPv6 address.
162 *
163 * @param string $ip The IP to expand.
164 * @return string The expanded IP.
165 * @since 1.0.0
166 */
167 public static function expand_v6( $ip ) {
168 return implode( ':', str_split( bin2hex( inet_pton( str_replace( self::$clean, '', $ip ) ) ), 4 ) );
169 }
170
171 /**
172 * Normalizes an IPv6 address.
173 *
174 * @param string $ip The IP to normalize.
175 * @return string The normalized IP.
176 * @since 1.0.0
177 */
178 public static function normalize_v6( $ip ) {
179 return inet_ntop( inet_pton( str_replace( self::$clean, '', $ip ) ) );
180 }
181
182 }