PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 4.2.5
Jetpack – WP Security, Backup, Speed, & Growth v4.2.5
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / protect / shared-functions.php

shared-functions.php in Jetpack – WP Security, Backup, Speed, & Growth 4.2.5, at modules/protect/shared-functions.php

317 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * These functions are shared by the Protect module and its related json-endpoints
4 */
5
6 /**
7 * Returns an array of IP objects that will never be blocked by the Protect module
8 *
9 * The array is segmented into a local whitelist which applies only to the current site
10 * and a global whitelist which, for multisite installs, applies to the entire networko
11 *
12 * @return array
13 */
14 function jetpack_protect_format_whitelist() {
15
16 $local_whitelist = jetpack_protect_get_local_whitelist();
17
18 $formatted = array (
19 'local' => array (),
20 );
21
22 foreach ( $local_whitelist as $item ) {
23 if ( $item->range ) {
24 $formatted['local'][] = $item->range_low . ' - ' . $item->range_high;
25 } else {
26 $formatted['local'][] = $item->ip_address;
27 }
28 }
29
30 if ( is_multisite() && current_user_can( 'manage_network' ) ) {
31 $formatted['global'] = array ();
32 $global_whitelist = jetpack_protect_get_global_whitelist();
33
34 if ( false === $global_whitelist ) {
35 // if the global whitelist has never been set, check for a legacy option set prior to 3.6
36 $global_whitelist = get_site_option( 'jetpack_protect_whitelist', array () );
37 }
38
39 foreach ( $global_whitelist as $item ) {
40 if ( $item->range ) {
41 $formatted['global'][] = $item->range_low . ' - ' . $item->range_high;
42 } else {
43 $formatted['global'][] = $item->ip_address;
44 }
45 }
46 }
47
48 return $formatted;
49 }
50
51 /**
52 * Gets the local Protect whitelist
53 *
54 * The 'local' part of the whitelist only really applies to multisite installs,
55 * which can have a network wide whitelist, as well as a local list that applies
56 * only to the current site. On single site installs, there will only be a local
57 * whitelist.
58 *
59 * @return array A list of IP Address objects or an empty array
60 */
61 function jetpack_protect_get_local_whitelist() {
62 $whitelist = Jetpack_Options::get_option( 'protect_whitelist' );
63
64 if ( false === $whitelist ) {
65 // The local whitelist has never been set
66 if ( is_multisite() ) {
67 // On a multisite, we can check for a legacy site_option that existed prior to v 3.6, or default to an empty array
68 $whitelist = get_site_option( 'jetpack_protect_whitelist', array () );
69 } else {
70 // On a single site, we can just use an empty array
71 $whitelist = array ();
72 }
73 }
74
75 return $whitelist;
76 }
77
78 /**
79 * Get the global, network-wide whitelist
80 *
81 * It will revert to the legacy site_option if jetpack_protect_global_whitelist has never been set
82 *
83 * @return array
84 */
85 function jetpack_protect_get_global_whitelist() {
86 $whitelist = get_site_option( 'jetpack_protect_global_whitelist' );
87
88 if ( false === $whitelist ) {
89 // The global whitelist has never been set. Check for legacy site_option, or default to an empty array
90 $whitelist = get_site_option( 'jetpack_protect_whitelist', array () );
91 }
92
93 return $whitelist;
94 }
95
96 function jetpack_protect_save_whitelist( $whitelist, $global = false ) {
97 $whitelist_error = false;
98 $new_items = array ();
99
100 if ( ! is_array( $whitelist ) ) {
101 return new WP_Error( 'invalid_parameters', __( 'Expecting an array', 'jetpack' ) );
102 }
103
104 if ( $global && ! is_multisite() ) {
105 return new WP_Error( 'invalid_parameters', __( 'Cannot use global flag on non-multisites', 'jetpack' ) );
106 }
107
108 if ( $global && ! current_user_can( 'manage_network' ) ) {
109 return new WP_Error( 'permission_denied', __( 'Only super admins can edit the global whitelist', 'jetpack' ) );
110 }
111
112 // validate each item
113 foreach ( $whitelist as $item ) {
114
115 $item = trim( $item );
116
117 if ( empty( $item ) ) {
118 continue;
119 }
120
121 $range = false;
122 if ( strpos( $item, '-' ) ) {
123 $item = explode( '-', $item );
124 $range = true;
125 }
126 $new_item = new stdClass();
127 $new_item->range = $range;
128
129 if ( ! empty( $range ) ) {
130
131 $low = trim( $item[0] );
132 $high = trim( $item[1] );
133
134 if ( ! filter_var( $low, FILTER_VALIDATE_IP ) || ! filter_var( $high, FILTER_VALIDATE_IP ) ) {
135 $whitelist_error = true;
136 break;
137 }
138
139 if ( ! jetpack_convert_ip_address( $low ) || ! jetpack_convert_ip_address( $high ) ) {
140 $whitelist_error = true;
141 break;
142 }
143
144 $new_item->range_low = $low;
145 $new_item->range_high = $high;
146
147 } else {
148
149 if ( ! filter_var( $item, FILTER_VALIDATE_IP ) ) {
150 $whitelist_error = true;
151 break;
152 }
153
154 if ( ! jetpack_convert_ip_address( $item ) ) {
155 $whitelist_error = true;
156 break;
157 }
158 $new_item->ip_address = $item;
159 }
160
161 $new_items[] = $new_item;
162
163 } // end item loop
164
165 if ( ! empty( $whitelist_error ) ) {
166 return new WP_Error( 'invalid_ip', __( 'One of your IP addresses was not valid.', 'jetpack' ) );
167 }
168
169 if ( $global ) {
170 update_site_option( 'jetpack_protect_global_whitelist', $new_items );
171 // once a user has saved their global whitelist, we can permanently remove the legacy option
172 delete_site_option( 'jetpack_protect_whitelist' );
173 } else {
174 Jetpack_Options::update_option( 'protect_whitelist', $new_items );
175 }
176
177 return true;
178 }
179
180 function jetpack_protect_get_ip() {
181
182 $trusted_header_data = get_site_option( 'trusted_ip_header' );
183
184 if ( isset( $trusted_header_data->trusted_header ) && isset( $_SERVER[ $trusted_header_data->trusted_header ] ) ) {
185 $ip = $_SERVER[ $trusted_header_data->trusted_header ];
186 $segments = $trusted_header_data->segments;
187 $reverse_order = $trusted_header_data->reverse;
188 } else {
189 $ip = $_SERVER['REMOTE_ADDR'];
190 }
191
192 $ips = explode( ',', $ip );
193
194 if ( ! isset( $segments ) || ! $segments ) {
195 $segments = 1;
196 }
197 if ( isset( $reverse_order ) && $reverse_order ) {
198 $ips = array_reverse( $ips );
199 }
200
201
202
203 $ip_count = count( $ips );
204
205 if ( 1 == $ip_count ) {
206 return jetpack_clean_ip( $ips[0] );
207 } elseif ( $ip_count >= $segments ) {
208 $the_one = $ip_count - $segments;
209
210 return jetpack_clean_ip( $ips[ $the_one ] );
211 } else {
212 return jetpack_clean_ip( $_SERVER['REMOTE_ADDR'] );
213 }
214 }
215
216 function jetpack_clean_ip( $ip ) {
217 $ip = trim( $ip );
218
219 // Check for IPv4 IP cast as IPv6
220 if ( preg_match( '/^::ffff:(\d+\.\d+\.\d+\.\d+)$/', $ip, $matches ) ) {
221 $ip = $matches[1];
222 }
223
224 return $ip;
225 }
226
227 /**
228 * Checks an IP to see if it is within a private range
229 *
230 * @param int $ip
231 *
232 * @return bool
233 */
234 function jetpack_protect_ip_is_private( $ip ) {
235
236 // we are dealing with ipv6, so we can simply rely on filter_var
237 if ( false === strpos( $ip, '.' ) ) {
238 return ! filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE );
239 }
240
241 // we are dealing with ipv4
242 $private_ip4_addresses = array (
243 '10.0.0.0|10.255.255.255', // single class A network
244 '172.16.0.0|172.31.255.255', // 16 contiguous class B network
245 '192.168.0.0|192.168.255.255', // 256 contiguous class C network
246 '169.254.0.0|169.254.255.255', // Link-local address also referred to as Automatic Private IP Addressing
247 '127.0.0.0|127.255.255.255' // localhost
248 );
249 $long_ip = ip2long( $ip );
250 if ( -1 != $long_ip ) {
251 foreach ( $private_ip4_addresses as $pri_addr ) {
252 list ( $start, $end ) = explode( '|', $pri_addr );
253 if ( $long_ip >= ip2long( $start ) && $long_ip <= ip2long( $end ) ) {
254 return true;
255 }
256 }
257 }
258
259 return false;
260 }
261
262 /**
263 * Uses inet_pton if available to convert an IP address to a binary string.
264 * If inet_pton is not available, ip2long will convert the address to an integer.
265 * Returns false if an invalid IP address is given.
266 *
267 * NOTE: ip2long will return false for any ipv6 address. servers that do not support
268 * inet_pton will not support ipv6
269 *
270 * @param $ip
271 *
272 * @return int|string|bool
273 */
274 function jetpack_convert_ip_address( $ip ) {
275 if ( function_exists( 'inet_pton' ) ) {
276 return inet_pton( $ip );
277 }
278
279 return ip2long( $ip );
280 }
281
282 /**
283 * Checks that a given IP address is within a given low - high range.
284 * Servers that support inet_pton will use that function to convert the ip to number,
285 * while other servers will use ip2long.
286 *
287 * NOTE: servers that do not support inet_pton cannot support ipv6.
288 *
289 * @param $ip
290 * @param $range_low
291 * @param $range_high
292 *
293 * @return bool
294 */
295 function jetpack_protect_ip_address_is_in_range( $ip, $range_low, $range_high ) {
296 // inet_pton will give us binary string of an ipv4 or ipv6
297 // we can then use strcmp to see if the address is in range
298 if ( function_exists( 'inet_pton' ) ) {
299 $ip_num = inet_pton( $ip );
300 $ip_low = inet_pton( $range_low );
301 $ip_high = inet_pton( $range_high );
302 if ( $ip_num && $ip_low && $ip_high && strcmp( $ip_num, $ip_low ) >= 0 && strcmp( $ip_num, $ip_high ) <= 0 ) {
303 return true;
304 }
305 // ip2long will give us an integer of an ipv4 address only. it will produce FALSE for ipv6
306 } else {
307 $ip_num = ip2long( $ip );
308 $ip_low = ip2long( $range_low );
309 $ip_high = ip2long( $range_high );
310 if ( $ip_num && $ip_low && $ip_high && $ip_num >= $ip_low && $ip_num <= $ip_high ) {
311 return true;
312 }
313 }
314
315 return false;
316
317 }