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