PluginProbe ʕ •ᴥ•ʔ
reCaptcha by BestWebSoft / 1.73
reCaptcha by BestWebSoft v1.73
1.79 1.80 1.82 1.83 1.84 1.85 1.86 1.87 trunk 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.10 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.18 1.19 1.20 1.21 1.22 1.23 1.24 1.25 1.26 1.27 1.28 1.29 1.30 1.31 1.32 1.33 1.34 1.35 1.36 1.37 1.38 1.39 1.40 1.41 1.42 1.43 1.44 1.45 1.46 1.47 1.48 1.49 1.50 1.51 1.52 1.53 1.54 1.55 1.56 1.57 1.58 1.59 1.60 1.61 1.62 1.63 1.64 1.65 1.66 1.67 1.68 1.70 1.71 1.72 1.73 1.74 1.75 1.78
google-captcha / includes / allowlist.php
google-captcha / includes Last commit date
allowlist.php 2 years ago class-gglcptch-settings-tabs.php 2 years ago forms.php 2 years ago pro_banners.php 2 years ago
allowlist.php
520 lines
1 <?php
2 /**
3 * Display content of "Allow List" tab on settings page
4 *
5 * @subpackage reCaptcha
6 * @since 1.27
7 * @version 1.0.0
8 */
9
10 if ( ! class_exists( 'Gglcptch_Allowlist' ) ) {
11 if ( ! class_exists( 'WP_List_Table' ) ) {
12 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
13 }
14
15 class Gglcptch_Allowlist extends WP_List_Table {
16 private
17 $basename,
18 $order_by,
19 $per_page,
20 $paged,
21 $order,
22 $s;
23
24 /**
25 * Constructor of class
26 */
27 public function __construct( $plugin_basename ) {
28 global $gglcptch_options;
29 if ( empty( $gglcptch_options ) ) {
30 $gglcptch_options = get_option( 'gglcptch_options' );
31 }
32 parent::__construct(
33 array(
34 'singular' => 'IP',
35 'plural' => 'IP',
36 'ajax' => true,
37 )
38 );
39 $this->basename = $plugin_basename;
40 }
41
42 /**
43 * Display content
44 *
45 * @return void
46 */
47 public function display_content() {
48 global $gglcptch_options; ?>
49 <h1 class="wp-heading-inline"><?php esc_html_e( 'reCaptcha Allow List', 'google-captcha' ); ?></h1>
50 <?php if ( ! ( isset( $_REQUEST['gglcptch_show_allowlist_form'] ) || isset( $_REQUEST['gglcptch_add_to_allowlist'] ) ) ) { ?>
51 <form method="post" action="admin.php?page=google-captcha-allowlist.php" style="display: inline;">
52 <button class="page-title-action" name="gglcptch_show_allowlist_form" value="on"<?php echo ( isset( $_POST['gglcptch_add_to_allowlist'] ) ) ? ' style="display: none;"' : ''; ?>><?php esc_html_e( 'Add New', 'google-captcha' ); ?></button>
53 </form>
54 <?php
55 }
56
57 if ( isset( $_SERVER ) ) {
58 $sever_vars = array( 'REMOTE_ADDR', 'HTTP_X_REAL_IP', 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR' );
59 foreach ( $sever_vars as $var ) {
60 if ( ! empty( $_SERVER[ $var ] ) ) {
61 if ( filter_var( sanitize_text_field( wp_unslash( $_SERVER[ $var ] ) ), FILTER_VALIDATE_IP ) ) {
62 $my_ip = sanitize_text_field( wp_unslash( $_SERVER[ $var ] ) );
63 break;
64 } else { /* if proxy */
65 $ip_array = explode( ',', sanitize_text_field( wp_unslash( $_SERVER[ $var ] ) ) );
66 if ( is_array( $ip_array ) && ! empty( $ip_array ) && filter_var( $ip_array[0], FILTER_VALIDATE_IP ) ) {
67 $my_ip = $ip_array[0];
68 break;
69 }
70 }
71 }
72 }
73 }
74
75 $this->display_notices();
76 $this->prepare_items();
77 ?>
78 <form class="form-table gglcptch_allowlist_form" method="post" action="admin.php?page=google-captcha-allowlist.php"
79 <?php
80 if ( ! ( isset( $_REQUEST['gglcptch_show_allowlist_form'] ) || isset( $_REQUEST['gglcptch_add_to_allowlist'] ) ) ) {
81 echo ' style="display: none;"';}
82 ?>
83 >
84 <label><?php esc_html_e( 'IP to Allow List', 'google-captcha' ); ?></label>
85 <br />
86 <input type="text" maxlength="31" name="gglcptch_add_to_allowlist" />
87 <?php if ( isset( $my_ip ) ) { ?>
88 <br />
89 <label id="gglcptch_add_my_ip">
90 <input type="checkbox" name="gglcptch_add_to_allowlist_my_ip" value="1" />
91 <?php esc_html_e( 'My IP', 'google-captcha' ); ?>
92 <input type="hidden" name="gglcptch_add_to_allowlist_my_ip_value" value="<?php echo esc_attr( $my_ip ); ?>" />
93 </label>
94 <?php } ?>
95 <div>
96 <span class="bws_info" style="line-height: 2;"><?php esc_html_e( 'Allowed formats', 'google-captcha' ); ?>:&nbsp;<code>192.168.0.1</code></span>
97 <br/>
98 <span class="bws_info" style="line-height: 2;"><?php esc_html_e( 'Allowed diapason', 'google-captcha' ); ?>:&nbsp;<code>0.0.0.0-255.255.255.255</code></span>
99 </div>
100 <!-- pls -->
101 <?php
102 if ( isset( $_POST['bws_hide_premium_options'] ) ) {
103 $gglcptch_options['hide_premium_options'][0] = 1;
104 update_option( 'gglcptch_options', $gglcptch_options );
105 }
106 $display_pro_options_for_allowlist = get_option( 'gglcptch_options' );
107 if ( empty( $display_pro_options_for_allowlist['hide_premium_options'][0] ) ) {
108 gglcptch_pro_block( 'gglcptch_allowlist_banner' );
109 }
110 ?>
111 <!-- end pls -->
112 <p>
113 <input type="submit" name="gglcptch_submit_add_to_allowlist" class="button-secondary" value="<?php esc_html_e( 'Add IP to Allow List', 'google-captcha' ); ?>" />
114 <?php wp_nonce_field( $this->basename, 'gglcptch_nonce_name' ); ?>
115 </p>
116 </form>
117 <form id="gglcptch_allowlist_search" method="post" action="admin.php?page=google-captcha-allowlist.php">
118 <?php
119 $this->search_box( __( 'Search IP', 'google-captcha' ), 'search_allowlisted_ip' );
120 wp_nonce_field( $this->basename, 'gglcptch_nonce_name' );
121 ?>
122 </form>
123 <form id="gglcptch_allowlist" method="post" action="admin.php?page=google-captcha-allowlist.php">
124 <?php
125 $this->display();
126 wp_nonce_field( $this->basename, 'gglcptch_nonce_name' );
127 ?>
128 </form>
129 <?php
130 }
131
132 /**
133 * Function to prepare data before display
134 *
135 * @return void
136 */
137 public function prepare_items() {
138 if ( isset( $_GET['orderby'] ) && in_array( $_GET['orderby'], array_keys( $this->get_sortable_columns() ) ) ) {
139 switch ( $_GET['orderby'] ) {
140 case 'ip':
141 $this->order_by = 'ip_from_int';
142 break;
143 case 'ip_from':
144 $this->order_by = 'ip_from_int';
145 break;
146 case 'ip_to':
147 $this->order_by = 'ip_to_int';
148 break;
149 default:
150 $this->order_by = esc_sql( sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) );
151 break;
152 }
153 } else {
154 $this->order_by = 'add_time';
155 }
156 $this->order = isset( $_REQUEST['order'] ) && in_array( strtoupper( sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ) ), array( 'ASC', 'DESC' ), true ) ? sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ) : '';
157 $this->paged = isset( $_REQUEST['paged'] ) && is_numeric( $_REQUEST['paged'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['paged'] ) ) : '';
158 $this->s = isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : '';
159 $this->per_page = $this->get_items_per_page( 'gglcptch_per_page', 20 );
160
161 $columns = $this->get_columns();
162 $hidden = array();
163 $sortable = $this->get_sortable_columns();
164 $primary = 'ip';
165 $this->_column_headers = array( $columns, $hidden, $sortable, $primary );
166 $this->items = $this->get_content();
167 $this->set_pagination_args(
168 array(
169 'total_items' => $this->get_items_number(),
170 'per_page' => 20,
171 )
172 );
173 }
174 /**
175 * Function to show message if empty list
176 *
177 * @return void
178 */
179 public function no_items() {
180 $label = isset( $_REQUEST['s'] ) ? __( 'Nothing found', 'google-captcha' ) : __( 'No IP in the Allow List', 'google-captcha' );
181 ?>
182 <p><?php echo esc_html( $label ); ?></p>
183 <?php
184 }
185
186 public function get_columns() {
187 $columns = array(
188 'cb' => '<input type="checkbox" />',
189 'ip' => __( 'IP Address', 'google-captcha' ),
190 'add_time' => __( 'Date Added', 'google-captcha' ),
191 );
192 return $columns;
193 }
194 /**
195 * Get a list of sortable columns.
196 *
197 * @return array list of sortable columns
198 */
199 public function get_sortable_columns() {
200 $sortable_columns = array(
201 'ip' => array( 'ip', true ),
202 'add_time' => array( 'add_time', false ),
203 );
204 return $sortable_columns;
205 }
206 /**
207 * Fires when the default column output is displayed for a single row.
208 *
209 * @param string $column_name The custom column's name.
210 * @param array $item The cuurrent letter data.
211 * @return void
212 */
213 public function column_default( $item, $column_name ) {
214 switch ( $column_name ) {
215 case 'ip':
216 case 'add_time':
217 return $item[ $column_name ];
218 default:
219 /* Show whole array for bugfix */
220 return print_r( $item, true );
221 }
222 }
223 /**
224 * Function to manage content of column with checboxes
225 *
226 * @param array $item The cuurrent letter data.
227 * @return string with html-structure of <input type=['checkbox']>
228 */
229 public function column_cb( $item ) {
230 /* customize displaying cb collumn */
231 return sprintf(
232 '<input type="checkbox" name="id[]" value="%s"/>',
233 $item['id']
234 );
235 }
236 /**
237 * Function to manage content of column with IP-adresses
238 *
239 * @param array $item The cuurrent letter data.
240 * @return string with html-structure of <input type=['checkbox']>
241 */
242 public function column_ip( $item ) {
243 $order_by = empty( $this->order_by ) ? '' : "&orderby={$this->order_by}";
244 $order = empty( $this->order ) ? '' : "&order={$this->order}";
245 $paged = empty( $this->paged ) ? '' : "&paged={$this->paged}";
246 $s = empty( $this->s ) ? '' : "&s={$this->s}";
247 $url = "?page=google-captcha-allowlist.php&gglcptch_remove={$item['id']}{$order_by}{$order}{$paged}{$s}";
248 $actions = array(
249 'delete' => '<a href="' . wp_nonce_url( $url, "gglcptch_nonce_remove_{$item['id']}" ) . '">' . __( 'Delete', 'google-captcha' ) . '</a>',
250 );
251 return sprintf( '%1$s %2$s', $item['ip'], $this->row_actions( $actions ) );
252 }
253 /**
254 * List with bulk action for IP
255 *
256 * @return array $actions
257 */
258 public function get_bulk_actions() {
259 /* adding bulk action */
260 return array( 'gglcptch_remove' => __( 'Delete', 'google-captcha' ) );
261 }
262 /**
263 * Get content for table
264 *
265 * @return array
266 */
267 public function get_content() {
268 global $wpdb;
269
270 if ( empty( $this->s ) ) {
271 $where = '';
272 } else {
273 $ip_int = filter_var( $this->s, FILTER_VALIDATE_IP ) ? sprintf( '%u', ip2long( $this->s ) ) : 0;
274 $where =
275 0 === $ip_int
276 ?
277 " WHERE `ip` LIKE '%{$this->s}%'"
278 :
279 " WHERE ( `ip_from_int` <= {$ip_int} AND `ip_to_int` >= {$ip_int} )";
280 }
281 $order_by = empty( $this->order_by ) ? '' : " ORDER BY `{$this->order_by}`";
282 $order = empty( $this->order ) ? '' : strtoupper( " {$this->order}" );
283 $offset = empty( $this->paged ) ? '' : ' OFFSET ' . ( $this->per_page * ( absint( $this->paged ) - 1 ) );
284
285 return $wpdb->get_results( "SELECT * FROM `{$wpdb->prefix}gglcptch_allowlist`{$where}{$order_by}{$order} LIMIT {$this->per_page}{$offset}", ARRAY_A );
286 }
287
288 /**
289 * Get number of all IPs which were added to database
290 *
291 * @since 1.6.9
292 * @param void
293 * @return int the number of IPs
294 */
295 private function get_items_number() {
296 global $wpdb;
297 if ( empty( $this->s ) ) {
298 $where = '';
299 } else {
300 $ip_int = filter_var( $this->s, FILTER_VALIDATE_IP ) ? sprintf( '%u', ip2long( $this->s ) ) : 0;
301 $where =
302 0 === $ip_int
303 ?
304 " WHERE `ip` LIKE '%{$this->s}%'"
305 :
306 " WHERE ( `ip_from_int` <= {$ip_int} AND `ip_to_int` >= {$ip_int} )";
307 }
308 return absint( $wpdb->get_var( "SELECT COUNT(`id`) FROM `{$wpdb->prefix}gglcptch_allowlist`{$where}" ) );
309 }
310
311 /**
312 * Handle necessary reqquests and display notices
313 *
314 * @return void
315 */
316 public function display_notices() {
317 global $wpdb, $gglcptch_options;
318 $error = $message = '';
319
320 $bulk_action = isset( $_REQUEST['action'] ) && 'gglcptch_remove' === $_REQUEST['action'] ? true : false;
321 if ( ! $bulk_action ) {
322 $bulk_action = isset( $_REQUEST['action2'] ) && 'gglcptch_remove' === $_REQUEST['action2'] ? true : false;
323 }
324
325 /* Add IP to the database */
326 if (
327 isset( $_POST['gglcptch_add_to_allowlist'] ) &&
328 ( ! empty( $_POST['gglcptch_add_to_allowlist'] ) || isset( $_POST['gglcptch_add_to_allowlist_my_ip'] ) ) &&
329 check_admin_referer( $this->basename, 'gglcptch_nonce_name' )
330 ) {
331 $add_ip = isset( $_POST['gglcptch_add_to_allowlist_my_ip'] ) ? sanitize_text_field( wp_unslash( $_POST['gglcptch_add_to_allowlist_my_ip_value'] ) ) : sanitize_text_field( wp_unslash( $_POST['gglcptch_add_to_allowlist'] ) );
332
333 $list_ip = preg_split( '/[\s,;]+/', trim( $add_ip, " \s\r\n\t,;" ) );
334
335 foreach ( $list_ip as $new_ip ) {
336 $type_ip = $this->valid_ip( trim( $new_ip ) );
337 if ( $type_ip ) {
338 $ip_int = sprintf( '%u', ip2long( $new_ip ) );
339 $id = $wpdb->get_var( $wpdb->prepare( 'SELECT `id` FROM ' . $wpdb->prefix . 'gglcptch_allowlist WHERE ( `ip_from_int` <= %d AND `ip_to_int` >= %d ) OR `ip` LIKE %s LIMIT 1;', $ip_int, $ip_int, $new_ip ) );
340 /* check if IP already in database */
341 if ( is_null( $id ) ) {
342 $time = current_time( 'mysql' );
343 $result = $this->save_ip( $new_ip, $type_ip, $time );
344 if ( false !== $result ) {
345 $message = __( 'IP added to the allow list successfully.', 'google-captcha' );
346 } else {
347 $error = __( 'Some errors occurred.', 'google-captcha' );
348 }
349 } else {
350 $error = __( 'IP is already in the allow list.', 'google-captcha' );
351 }
352 } else {
353 $error = __( 'Invalid IP. See allowed formats.', 'google-captcha' );
354 }
355 }
356 if ( empty( $error ) ) {
357 $gglcptch_options['allowlist_is_empty'] = false;
358 update_option( 'gglcptch_options', $gglcptch_options );
359 }
360 /* Remove IP from database */
361 } elseif ( $bulk_action && check_admin_referer( $this->basename, 'gglcptch_nonce_name' ) ) {
362 if ( ! empty( $_REQUEST['id'] ) ) {
363 foreach ( $_REQUEST['id'] as $key => $value ) {
364 $_REQUEST['id'][ $key ] = absint( $value );
365 }
366 $list = implode( ',', $_REQUEST['id'] );
367 $result = $wpdb->query( 'DELETE FROM `' . $wpdb->prefix . 'gglcptch_allowlist` WHERE `id` IN (' . $list . ');' );
368
369 if ( ! $wpdb->last_error ) {
370 $message = sprintf( _n( '%s IP was deleted successfully.', '%s IPs were deleted successfully.', $result, 'google-captcha' ), $result );
371 $gglcptch_options['allowlist_is_empty'] = is_null( $wpdb->get_var( "SELECT `id` FROM `{$wpdb->prefix}gglcptch_allowlist` LIMIT 1" ) ) ? true : false;
372 update_option( 'gglcptch_options', $gglcptch_options );
373 } else {
374 $error = __( 'Some errors occurred.', 'google-captcha' );
375 }
376 }
377 } elseif ( isset( $_GET['gglcptch_remove'] ) && check_admin_referer( 'gglcptch_nonce_remove_' . sanitize_text_field( wp_unslash( $_GET['gglcptch_remove'] ) ) ) ) {
378
379 $wpdb->delete(
380 $wpdb->prefix . 'gglcptch_allowlist',
381 array(
382 'id' => absint( sanitize_text_field( wp_unslash( $_GET['gglcptch_remove'] ) ) ),
383 )
384 );
385
386 if ( ! $wpdb->last_error ) {
387 $message = __( 'One IP was deleted successfully.', 'google-captcha' );
388 $gglcptch_options['allowlist_is_empty'] = is_null( $wpdb->get_var( "SELECT `id` FROM `{$wpdb->prefix}gglcptch_allowlist` LIMIT 1" ) ) ? true : false;
389 update_option( 'gglcptch_options', $gglcptch_options );
390 } else {
391 $error = __( 'Some errors occurred.', 'google-captcha' );
392 }
393 } elseif ( isset( $_POST['gglcptch_submit_add_to_allowlist'] ) && empty( $_POST['gglcptch_add_to_allowlist'] ) ) {
394 $error = __( 'You have not entered any IP.', 'google-captcha' );
395 } elseif ( isset( $_REQUEST['s'] ) ) {
396 if ( '' === sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) ) {
397 $error = __( 'You have not entered any IP in to the search form.', 'google-captcha' );
398 } else {
399 $message = __( 'Search results for', 'google-captcha' ) . '&nbsp;:&nbsp;' . sanitize_text_field( wp_unslash( $_REQUEST['s'] ) );
400 }
401 }
402 if ( ! empty( $message ) ) {
403 ?>
404 <div class="updated fade below-h2"><p><strong><?php echo esc_html( $message ); ?></strong></p></div>
405 <?php
406 }
407 if ( ! empty( $error ) ) {
408 ?>
409 <div class="error below-h2"><p><strong><?php echo esc_html( $error ); ?></strong></p></div>
410 <?php
411 }
412 }
413
414 /**
415 * Function to check if IP (mask/diapason) is valid
416 *
417 * @param $ip_to_check string IP, mask or diapason to check
418 * @return bool False - if it's not valid IP, mask or diapason | string with the type of entered value - if valid IP, mask or diapason
419 */
420 function valid_ip( $ip_to_check = null ) {
421 if ( empty( $ip_to_check ) ) {
422 return false;
423 } else {
424 /* if IP (or mask/diapason) is not empty*/
425 if ( preg_match( '/^(25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9][0-9]|[0-9])(\.(25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9][0-9]|[0-9])){3}$/', $ip_to_check ) ) {
426 /* single IP */
427 return 'single_ip';
428 } elseif ( preg_match( '/^(25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9][0-9]|[0-9])(\.(25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9][0-9]|[0-9])){3}\/(3[0-2]|[1-2][0-9]|[0-9])$/', $ip_to_check ) ) {
429 /* normal mask like 128.45.25.0/8 */
430 return 'normal_mask';
431 } elseif ( preg_match( '/^(25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9][0-9]|[0-9])(\.(25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9][0-9]|[0-9])){0,2}\.$/', $ip_to_check ) ) {
432 /* shorten mask like 192.168. or 128.45.25. */
433 return 'shorten_mask';
434 } elseif ( preg_match( '/^(25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9][0-9]|[0-9])(\.(25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9][0-9]|[0-9])){3}\-(25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9][0-9]|[0-9])(\.(25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9][0-9]|[0-9])){3}$/', $ip_to_check ) ) {
435 /* diapason like 128.45.25.0-188.5.5.5 */
436 $ip_to_check = explode( '-', $ip_to_check ); /*$ips[0] - diapason from, $ips[1] - diapason to*/
437 if ( sprintf( '%u', ip2long( $ip_to_check[0] ) ) <= sprintf( '%u', ip2long( $ip_to_check[1] ) ) ) {
438 return 'diapason';
439 } else {
440 return false;
441 }
442 } else {
443 return false;
444 }
445 }
446 }
447
448 /**
449 * Save IP in database
450 */
451 function save_ip( $ip, $type_ip, $time ) {
452 global $wpdb;
453 switch ( $type_ip ) {
454 case 'single_ip': /* if insert single ip address */
455 $ip_from_int = $ip_to_int = sprintf( '%u', ip2long( $ip ) ); /*because adding a single address diapason will contain one address*/
456 $ip_from = $ip_to = $ip;
457 break;
458 case 'shorten_mask': /* if insert ip mask like 'xxx.' or 'xxx.xxx.' or 'xxx.xxx.xxx.' */
459 $dot_entry = substr_count( $ip, '.' );
460 switch ( $dot_entry ) {
461 case 3: /* in case if mask like xxx.xxx.xxx. */
462 $ip_from = $ip . '0';
463 $ip_to = $ip . '255';
464 break;
465 case 2: /* in case if mask like xxx.xxx. */
466 $ip_from = $ip . '0.0';
467 $ip_to = $ip . '255.255';
468 break;
469 case 1: /*i n case if mask like xxx. */
470 $ip_from = $ip . '0.0.0';
471 $ip_to = $ip . '255.255.255';
472 break;
473 default: /* insurance */
474 $ip_from = '0.0.0.0';
475 $ip_to = '0.0.0.0';
476 break;
477 }
478 $ip_from_int = sprintf( '%u', ip2long( $ip_from ) );
479 $ip_to_int = sprintf( '%u', ip2long( $ip_to ) );
480 break;
481 case 'diapason': /* if insert diapason of ip addresses like xxx.xxx.xxx.xxx-yyy.yyy.yyy.yyy */
482 $ips = explode( '-', $ip ); /* $ips[0] - diapason from, $ips[1] - diapason to */
483 $ip_from = trim( $ips[0] );
484 $ip_to = trim( $ips[1] );
485 $ip_from_int = sprintf( '%u', ip2long( $ip_from ) );
486 $ip_to_int = sprintf( '%u', ip2long( $ip_to ) );
487 break;
488 case 'normal_mask': /* if insert ip mask like xxx.xxx.xxx.xxx/yy */
489 $mask = explode( '/', $ip ); /* $mask[0] - is ip address, $mask[1] - is cidr mask */
490 $nmask = 4294967295 - ( pow( 2, 32 - $mask[1] ) - 1 ); /* calculation netmask in decimal view from cidr mask */
491 $ip_from_int = ip2long( $mask[0] ) & $nmask; /* calculating network address signed (this is doing for correct worl with netmsk) */
492 $ip_from_int = sprintf( '%u', $ip_from_int ); /* and now unsigned */
493 $ip_to_int = $ip_from_int + ( pow( 2, 32 - $mask[1] ) - 1 ); /* calculating broadcast */
494 $ip_from = long2ip( $ip_from_int );
495 $ip_to = long2ip( $ip_to_int );
496 default:
497 break;
498 }
499 /* add a new row to db */
500 $result = $wpdb->insert(
501 $wpdb->prefix . 'gglcptch_allowlist',
502 array(
503 'ip' => $ip,
504 'ip_from_int' => $ip_from_int,
505 'ip_to_int' => $ip_to_int,
506 'add_time' => $time,
507 ),
508 array(
509 '%s', /* all '%s' because max value in '%d' is 2147483647 */
510 '%s',
511 '%s',
512 '%s',
513 )
514 );
515 return $result;
516 }
517
518 }
519 }
520