allowlist.php
4 years ago
class-gglcptch-settings-tabs.php
4 years ago
forms.php
4 years ago
pro_banners.php
4 years ago
allowlist.php
381 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Display content of "Allow List" tab on settings page |
| 4 | * @subpackage reCaptcha |
| 5 | * @since 1.27 |
| 6 | * @version 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | if ( ! class_exists( 'Gglcptch_Allowlist' ) ) { |
| 10 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 11 | require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); |
| 12 | } |
| 13 | |
| 14 | class Gglcptch_Allowlist extends WP_List_Table { |
| 15 | private |
| 16 | $basename, |
| 17 | $order_by, |
| 18 | $per_page, |
| 19 | $paged, |
| 20 | $order, |
| 21 | $s; |
| 22 | |
| 23 | /** |
| 24 | * Constructor of class |
| 25 | */ |
| 26 | function __construct( $plugin_basename ) { |
| 27 | global $gglcptch_options; |
| 28 | if ( empty( $gglcptch_options ) ) { |
| 29 | $gglcptch_options = get_option( 'gglcptch_options' ); |
| 30 | } |
| 31 | parent::__construct( array( |
| 32 | 'singular' => 'IP', |
| 33 | 'plural' => 'IP', |
| 34 | 'ajax' => true, |
| 35 | ) |
| 36 | ); |
| 37 | $this->basename = $plugin_basename; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Display content |
| 42 | * @return void |
| 43 | */ |
| 44 | function display_content() { |
| 45 | global $gglcptch_options; ?> |
| 46 | <h1 class="wp-heading-inline"><?php _e( 'reCaptcha Allow List', 'google-captcha' ); ?></h1> |
| 47 | <?php if ( ! ( isset( $_REQUEST['gglcptch_show_allowlist_form'] ) || isset( $_REQUEST['gglcptch_add_to_allowlist'] ) ) ) { ?> |
| 48 | <form method="post" action="admin.php?page=google-captcha-allowlist.php" style="display: inline;"> |
| 49 | <button class="page-title-action" name="gglcptch_show_allowlist_form" value="on"<?php echo ( isset( $_POST['gglcptch_add_to_allowlist'] ) ) ? ' style="display: none;"' : ''; ?>><?php _e( 'Add New', 'google-captcha' ); ?></button> |
| 50 | </form> |
| 51 | <?php } |
| 52 | |
| 53 | if ( isset( $_SERVER ) ) { |
| 54 | $sever_vars = array( 'HTTP_X_REAL_IP', 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR' ); |
| 55 | foreach ( $sever_vars as $var ) { |
| 56 | if ( ! empty( $_SERVER[ $var ] ) ) { |
| 57 | if ( filter_var( $_SERVER[ $var ], FILTER_VALIDATE_IP ) ) { |
| 58 | $my_ip = $_SERVER[ $var ]; |
| 59 | break; |
| 60 | } else { /* if proxy */ |
| 61 | $ip_array = explode( ',', $_SERVER[ $var ] ); |
| 62 | if ( is_array( $ip_array ) && ! empty( $ip_array ) && filter_var( $ip_array[0], FILTER_VALIDATE_IP ) ) { |
| 63 | $my_ip = $ip_array[0]; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | $this->display_notices(); |
| 72 | $this->prepare_items(); ?> |
| 73 | <form class="form-table gglcptch_allowlist_form" method="post" action="admin.php?page=google-captcha-allowlist.php" <?php if ( ! ( isset( $_REQUEST['gglcptch_show_allowlist_form'] ) || isset( $_REQUEST['gglcptch_add_to_allowlist'] ) ) ) echo ' style="display: none;"'; ?>> |
| 74 | <label><?php _e( 'IP to Allow List', 'google-captcha' ); ?></label> |
| 75 | <br /> |
| 76 | <input type="text" maxlength="31" name="gglcptch_add_to_allowlist" /> |
| 77 | <?php if ( isset( $my_ip ) ) { ?> |
| 78 | <br /> |
| 79 | <label id="gglcptch_add_my_ip"> |
| 80 | <input type="checkbox" name="gglcptch_add_to_allowlist_my_ip" value="1" /> |
| 81 | <?php _e( 'My IP', 'google-captcha' ); ?> |
| 82 | <input type="hidden" name="gglcptch_add_to_allowlist_my_ip_value" value="<?php echo $my_ip; ?>" /> |
| 83 | </label> |
| 84 | <?php } ?> |
| 85 | <div> |
| 86 | <span class="bws_info" style="line-height: 2;"><?php _e( "Allowed formats", 'google-captcha' ); ?>: <code>192.168.0.1</code></span> |
| 87 | <br/> |
| 88 | <span class="bws_info" style="line-height: 2;"><?php _e( "Allowed diapason", 'google-captcha' ); ?>: <code>0.0.0.0 - 255.255.255.255</code></span> |
| 89 | </div> |
| 90 | <!-- pls --> |
| 91 | <?php if ( isset( $_POST['bws_hide_premium_options'] ) ) { |
| 92 | $gglcptch_options['hide_premium_options'][0] = 1; |
| 93 | update_option( 'gglcptch_options', $gglcptch_options ); |
| 94 | } |
| 95 | $display_pro_options_for_allowlist = get_option( 'gglcptch_options' ); |
| 96 | if( empty( $display_pro_options_for_allowlist['hide_premium_options'][0] ) ) { |
| 97 | gglcptch_pro_block( 'gglcptch_allowlist_banner' ); |
| 98 | } ?> |
| 99 | <!-- end pls --> |
| 100 | <p> |
| 101 | <input type="submit" name="gglcptch_submit_add_to_allowlist" class="button-secondary" value="<?php _e( 'Add IP to Allow List', 'google-captcha' ) ?>" /> |
| 102 | <?php wp_nonce_field( $this->basename, 'gglcptch_nonce_name' ); ?> |
| 103 | </p> |
| 104 | </form> |
| 105 | <form id="gglcptch_allowlist_search" method="post" action="admin.php?page=google-captcha-allowlist.php"> |
| 106 | <?php $this->search_box( __( 'Search IP', 'google-captcha' ), 'search_allowlisted_ip' ); |
| 107 | wp_nonce_field( $this->basename, 'gglcptch_nonce_name' ); ?> |
| 108 | </form> |
| 109 | <form id="gglcptch_allowlist" method="post" action="admin.php?page=google-captcha-allowlist.php"> |
| 110 | <?php $this->display(); |
| 111 | wp_nonce_field( $this->basename, 'gglcptch_nonce_name' ); ?> |
| 112 | </form> |
| 113 | <?php } |
| 114 | |
| 115 | /** |
| 116 | * Function to prepare data before display |
| 117 | * @return void |
| 118 | */ |
| 119 | function prepare_items() { |
| 120 | if ( isset( $_GET['orderby'] ) && in_array( $_GET['orderby'], array_keys( $this->get_sortable_columns() ) ) ) { |
| 121 | switch ( $_GET['orderby'] ) { |
| 122 | case 'ip': |
| 123 | $this->order_by = 'ip_from_int'; |
| 124 | break; |
| 125 | case 'ip_from': |
| 126 | $this->order_by = 'ip_from_int'; |
| 127 | break; |
| 128 | case 'ip_to': |
| 129 | $this->order_by = 'ip_to_int'; |
| 130 | break; |
| 131 | default: |
| 132 | $this->order_by = esc_sql( $_GET['orderby'] ); |
| 133 | break; |
| 134 | } |
| 135 | } else { |
| 136 | $this->order_by = 'add_time'; |
| 137 | } |
| 138 | $this->order = isset( $_REQUEST['order'] ) && in_array( strtoupper( $_REQUEST['order'] ), array( 'ASC', 'DESC' ) ) ? $_REQUEST['order'] : ''; |
| 139 | $this->paged = isset( $_REQUEST['paged'] ) && is_numeric( $_REQUEST['paged'] ) ? $_REQUEST['paged'] : ''; |
| 140 | $this->s = isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : ''; |
| 141 | $this->per_page = $this->get_items_per_page( 'gglcptch_per_page', 20 ); |
| 142 | |
| 143 | $columns = $this->get_columns(); |
| 144 | $hidden = array(); |
| 145 | $sortable = $this->get_sortable_columns(); |
| 146 | $primary = 'ip'; |
| 147 | $this->_column_headers = array( $columns, $hidden, $sortable, $primary ); |
| 148 | $this->items = $this->get_content(); |
| 149 | $this->set_pagination_args( array( |
| 150 | 'total_items' => $this->get_items_number(), |
| 151 | 'per_page' => 20, |
| 152 | ) |
| 153 | ); |
| 154 | } |
| 155 | /** |
| 156 | * Function to show message if empty list |
| 157 | * @return void |
| 158 | */ |
| 159 | function no_items() { |
| 160 | $label = isset( $_REQUEST['s'] ) ? __( 'Nothing found', 'google-captcha' ) : __( 'No IP in the Allow List', 'google-captcha' ); ?> |
| 161 | <p><?php echo $label; ?></p> |
| 162 | <?php } |
| 163 | |
| 164 | function get_columns() { |
| 165 | $columns = array( |
| 166 | 'cb' => '<input type="checkbox" />', |
| 167 | 'ip' => __( 'IP Address', 'google-captcha' ), |
| 168 | 'add_time' => __( 'Date Added', 'google-captcha' ) |
| 169 | ); |
| 170 | return $columns; |
| 171 | } |
| 172 | /** |
| 173 | * Get a list of sortable columns. |
| 174 | * @return array list of sortable columns |
| 175 | */ |
| 176 | function get_sortable_columns() { |
| 177 | $sortable_columns = array( |
| 178 | 'ip' => array( 'ip', true ), |
| 179 | 'add_time' => array( 'add_time', false ) |
| 180 | ); |
| 181 | return $sortable_columns; |
| 182 | } |
| 183 | /** |
| 184 | * Fires when the default column output is displayed for a single row. |
| 185 | * @param string $column_name The custom column's name. |
| 186 | * @param array $item The cuurrent letter data. |
| 187 | * @return void |
| 188 | */ |
| 189 | function column_default( $item, $column_name ) { |
| 190 | switch ( $column_name ) { |
| 191 | case 'ip': |
| 192 | case 'add_time': |
| 193 | return $item[ $column_name ]; |
| 194 | default: |
| 195 | /* Show whole array for bugfix */ |
| 196 | return print_r( $item, true ); |
| 197 | } |
| 198 | } |
| 199 | /** |
| 200 | * Function to manage content of column with checboxes |
| 201 | * @param array $item The cuurrent letter data. |
| 202 | * @return string with html-structure of <input type=['checkbox']> |
| 203 | */ |
| 204 | function column_cb( $item ) { |
| 205 | /* customize displaying cb collumn */ |
| 206 | return sprintf( |
| 207 | '<input type="checkbox" name="id[]" value="%s"/>', $item['id'] |
| 208 | ); |
| 209 | } |
| 210 | /** |
| 211 | * Function to manage content of column with IP-adresses |
| 212 | * @param array $item The cuurrent letter data. |
| 213 | * @return string with html-structure of <input type=['checkbox']> |
| 214 | */ |
| 215 | function column_ip( $item ) { |
| 216 | $order_by = empty( $this->order_by ) ? '' : "&orderby={$this->order_by}"; |
| 217 | $order = empty( $this->order ) ? '' : "&order={$this->order}"; |
| 218 | $paged = empty( $this->paged ) ? '' : "&paged={$this->paged}"; |
| 219 | $s = empty( $this->s ) ? '' : "&s={$this->s}"; |
| 220 | $url = "?page=google-captcha-allowlist.php&gglcptch_remove={$item['id']}{$order_by}{$order}{$paged}{$s}"; |
| 221 | $actions = array( |
| 222 | 'delete' => '<a href="' . wp_nonce_url( $url, "gglcptch_nonce_remove_{$item['id']}" ) . '">' . __( 'Delete', 'google-captcha' ) . '</a>' |
| 223 | ); |
| 224 | return sprintf('%1$s %2$s', $item['ip'], $this->row_actions( $actions ) ); |
| 225 | } |
| 226 | /** |
| 227 | * List with bulk action for IP |
| 228 | * @return array $actions |
| 229 | */ |
| 230 | function get_bulk_actions() { |
| 231 | /* adding bulk action */ |
| 232 | return array( 'gglcptch_remove'=> __( 'Delete', 'google-captcha' ) ); |
| 233 | } |
| 234 | /** |
| 235 | * Get content for table |
| 236 | * @return array |
| 237 | */ |
| 238 | function get_content() { |
| 239 | global $wpdb; |
| 240 | |
| 241 | if ( empty( $this->s ) ) { |
| 242 | $where = ''; |
| 243 | } else { |
| 244 | $ip_int = filter_var( $this->s, FILTER_VALIDATE_IP ) ? sprintf( '%u', ip2long( $this->s ) ) : 0; |
| 245 | $where = |
| 246 | 0 == $ip_int |
| 247 | ? |
| 248 | " WHERE `ip` LIKE '%{$this->s}%'" |
| 249 | : |
| 250 | " WHERE ( `ip_from_int` <= {$ip_int} AND `ip_to_int` >= {$ip_int} )"; |
| 251 | } |
| 252 | $order_by = empty( $this->order_by ) ? '' : " ORDER BY `{$this->order_by}`"; |
| 253 | $order = empty( $this->order ) ? '' : strtoupper( " {$this->order}" ); |
| 254 | $offset = empty( $this->paged ) ? '' : " OFFSET " . ( $this->per_page * ( absint( $this->paged ) - 1 ) ); |
| 255 | |
| 256 | return $wpdb->get_results( "SELECT * FROM `{$wpdb->prefix}gglcptch_allowlist`{$where}{$order_by}{$order} LIMIT {$this->per_page}{$offset}", ARRAY_A ); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Get number of all IPs which were added to database |
| 261 | * @since 1.6.9 |
| 262 | * @param void |
| 263 | * @return int the number of IPs |
| 264 | */ |
| 265 | private function get_items_number() { |
| 266 | global $wpdb; |
| 267 | if ( empty( $this->s ) ) { |
| 268 | $where = ''; |
| 269 | } else { |
| 270 | $ip_int = filter_var( $this->s, FILTER_VALIDATE_IP ) ? sprintf( '%u', ip2long( $this->s ) ) : 0; |
| 271 | $where = |
| 272 | 0 == $ip_int |
| 273 | ? |
| 274 | " WHERE `ip` LIKE '%{$this->s}%'" |
| 275 | : |
| 276 | " WHERE ( `ip_from_int` <= {$ip_int} AND `ip_to_int` >= {$ip_int} )"; |
| 277 | } |
| 278 | return absint( $wpdb->get_var( "SELECT COUNT(`id`) FROM `{$wpdb->prefix}gglcptch_allowlist`{$where}" ) ); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Handle necessary reqquests and display notices |
| 283 | * @return void |
| 284 | */ |
| 285 | function display_notices() { |
| 286 | global $wpdb, $gglcptch_options; |
| 287 | $error = $message = ''; |
| 288 | |
| 289 | $bulk_action = isset( $_REQUEST['action'] ) && 'gglcptch_remove' == $_REQUEST['action'] ? true : false; |
| 290 | if ( ! $bulk_action ) { |
| 291 | $bulk_action = isset( $_REQUEST['action2'] ) && 'gglcptch_remove' == $_REQUEST['action2'] ? true : false; |
| 292 | } |
| 293 | |
| 294 | /* Add IP to the database */ |
| 295 | if ( |
| 296 | isset( $_POST['gglcptch_add_to_allowlist'] ) && |
| 297 | ( ! empty( $_POST['gglcptch_add_to_allowlist'] ) || isset( $_POST['gglcptch_add_to_allowlist_my_ip'] ) ) && |
| 298 | check_admin_referer( $this->basename, 'gglcptch_nonce_name' ) |
| 299 | ) { |
| 300 | $add_ip = isset( $_POST['gglcptch_add_to_allowlist_my_ip'] ) ? sanitize_text_field( $_POST['gglcptch_add_to_allowlist_my_ip_value'] ) : sanitize_text_field( $_POST['gglcptch_add_to_allowlist'] ); |
| 301 | |
| 302 | $valid_ip = filter_var( stripslashes( trim( $add_ip ) ), FILTER_VALIDATE_IP ); |
| 303 | |
| 304 | if ( $valid_ip ) { |
| 305 | $ip_int = sprintf( '%u', ip2long( $valid_ip ) ); |
| 306 | $id = $wpdb->get_var( "SELECT `id` FROM " . $wpdb->prefix . "gglcptch_allowlist WHERE ( `ip_from_int` <= " . $ip_int . " AND `ip_to_int` >= " . $ip_int . " ) OR `ip` LIKE '" . $valid_ip . "' LIMIT 1;" ); |
| 307 | /* check if IP already in database */ |
| 308 | if ( is_null( $id ) ) { |
| 309 | $time = current_time( 'mysql' ); |
| 310 | $wpdb->insert( |
| 311 | $wpdb->prefix . "gglcptch_allowlist", |
| 312 | array( |
| 313 | 'ip' => $valid_ip, |
| 314 | 'ip_from_int' => $ip_int, |
| 315 | 'ip_to_int' => $ip_int, |
| 316 | 'add_time' => $time |
| 317 | ) |
| 318 | ); |
| 319 | if ( ! $wpdb->last_error ) { |
| 320 | $message = __( 'IP added to the allow list successfully.', 'google-captcha' ); |
| 321 | } else { |
| 322 | $error = __( 'Some errors occurred.', 'google-captcha' ); |
| 323 | } |
| 324 | } else { |
| 325 | $error = __( 'IP is already in the allow list.', 'google-captcha' ); |
| 326 | } |
| 327 | } else { |
| 328 | $error = __( 'Invalid IP. See allowed formats.', 'google-captcha' ); |
| 329 | } |
| 330 | if ( empty( $error ) ) { |
| 331 | $gglcptch_options['allowlist_is_empty'] = false; |
| 332 | update_option( 'gglcptch_options', $gglcptch_options ); |
| 333 | } |
| 334 | /* Remove IP from database */ |
| 335 | } elseif ( $bulk_action && check_admin_referer( $this->basename, 'gglcptch_nonce_name' ) ) { |
| 336 | if ( ! empty( $_REQUEST['id'] ) ) { |
| 337 | foreach ( $_REQUEST['id'] as $key => $value ) { |
| 338 | $_REQUEST['id'][ $key ] = intval( $value ); |
| 339 | } |
| 340 | $list = implode( ',', $_REQUEST['id'] ); |
| 341 | $result = $wpdb->query( "DELETE FROM `" . $wpdb->prefix . "gglcptch_allowlist` WHERE `id` IN (" . $list . ");" ); |
| 342 | |
| 343 | if ( ! $wpdb->last_error ) { |
| 344 | $message = sprintf( _n( "%s IP was deleted successfully.", "%s IPs were deleted successfully.", $result, 'google-captcha' ), $result ); |
| 345 | $gglcptch_options['allowlist_is_empty'] = is_null( $wpdb->get_var( "SELECT `id` FROM `{$wpdb->prefix}gglcptch_allowlist` LIMIT 1" ) ) ? true : false; |
| 346 | update_option( 'gglcptch_options', $gglcptch_options ); |
| 347 | } else { |
| 348 | $error = __( 'Some errors occurred.', 'google-captcha' ); |
| 349 | } |
| 350 | } |
| 351 | } elseif ( isset( $_GET['gglcptch_remove'] ) && check_admin_referer( 'gglcptch_nonce_remove_' . $_GET['gglcptch_remove'] ) ) { |
| 352 | |
| 353 | $wpdb->delete( $wpdb->prefix . "gglcptch_allowlist", |
| 354 | array( 'id' => (int)$_GET['gglcptch_remove'] ) |
| 355 | ); |
| 356 | |
| 357 | if ( ! $wpdb->last_error ) { |
| 358 | $message = __( "One IP was deleted successfully.", 'google-captcha' ); |
| 359 | $gglcptch_options['allowlist_is_empty'] = is_null( $wpdb->get_var( "SELECT `id` FROM `{$wpdb->prefix}gglcptch_allowlist` LIMIT 1" ) ) ? true : false; |
| 360 | update_option( 'gglcptch_options', $gglcptch_options ); |
| 361 | } else { |
| 362 | $error = __( 'Some errors occurred.', 'google-captcha' ); |
| 363 | } |
| 364 | } elseif ( isset( $_POST['gglcptch_submit_add_to_allowlist'] ) && empty( $_POST['gglcptch_add_to_allowlist'] ) ) { |
| 365 | $error = __( 'You have not entered any IP.', 'google-captcha' ); |
| 366 | } elseif ( isset( $_REQUEST['s'] ) ) { |
| 367 | if ( '' == $_REQUEST['s'] ) { |
| 368 | $error = __( 'You have not entered any IP in to the search form.', 'google-captcha' ); |
| 369 | } else { |
| 370 | $message = __( 'Search results for', 'google-captcha' ) . ' : ' . sanitize_text_field( $_REQUEST['s'] ); |
| 371 | } |
| 372 | } |
| 373 | if ( ! empty( $message ) ) { ?> |
| 374 | <div class="updated fade below-h2"><p><strong><?php echo $message; ?></strong></p></div> |
| 375 | <?php } |
| 376 | if ( ! empty( $error ) ) { ?> |
| 377 | <div class="error below-h2"><p><strong><?php echo $error; ?></strong></p></div> |
| 378 | <?php } |
| 379 | } |
| 380 | } |
| 381 | } |