PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / system / class-ip.php

class-ip.php in DecaLog 4.4.0, at includes/system/class-ip.php

171 lines 4.0 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 Decalog\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 ( false !== filter_var( trim( $ip ), FILTER_VALIDATE_IP ) ) {
65 return self::expand( $ip );
66 }
67 }
68 }
69 foreach ( $iplist as $ip ) {
70 if ( false !== 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 'HTTP_X_CLUSTER_CLIENT_IP',
93 'HTTP_CF_CONNECTING_IP',
94 'HTTP_X_REAL_IP',
95 'REMOTE_ADDR',
96 ] as $field
97 ) {
98 if ( array_key_exists( $field, $_SERVER ) ) {
99 $ip = self::maybe_extract_ip( explode( ',', filter_input( INPUT_SERVER, $field ) ?? '' ), 1 === $i );
100 if ( '' !== $ip ) {
101 return $ip;
102 }
103 }
104 }
105 }
106 return '127.0.0.1';
107 }
108
109 /**
110 * Expands an IPv4 or IPv6 address.
111 *
112 * @param string $ip The IP to expand.
113 * @return string The expanded IP.
114 * @since 1.0.0
115 */
116 public static function expand( $ip ) {
117 if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) ) {
118 return self::expand_v6( $ip );
119 }
120 if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {
121 return self::expand_v4( $ip );
122 }
123 return '';
124 }
125
126 /**
127 * Expands an IPv4 address.
128 *
129 * @param string $ip The IP to expand.
130 * @return string The expanded IP.
131 * @since 1.0.0
132 */
133 public static function expand_v4( $ip ) {
134 return long2ip( ip2long( str_replace( self::$clean, '', $ip ) ) );
135 }
136
137 /**
138 * Normalizes an IPv4 address.
139 *
140 * @param string $ip The IP to normalize.
141 * @return string The normalized IP.
142 * @since 1.0.0
143 */
144 public static function normalize_v4( $ip ) {
145 return long2ip( (int) str_replace( self::$clean, '', $ip ) );
146 }
147
148 /**
149 * Expands an IPv6 address.
150 *
151 * @param string $ip The IP to expand.
152 * @return string The expanded IP.
153 * @since 1.0.0
154 */
155 public static function expand_v6( $ip ) {
156 return implode( ':', str_split( bin2hex( inet_pton( str_replace( self::$clean, '', $ip ) ) ), 4 ) );
157 }
158
159 /**
160 * Normalizes an IPv6 address.
161 *
162 * @param string $ip The IP to normalize.
163 * @return string The normalized IP.
164 * @since 1.0.0
165 */
166 public static function normalize_v6( $ip ) {
167 return inet_ntop( inet_pton( str_replace( self::$clean, '', $ip ) ) );
168 }
169
170 }
171