PluginProbe ʕ •ᴥ•ʔ
reCaptcha by BestWebSoft / 1.71
reCaptcha by BestWebSoft v1.71
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 3 years ago class-gglcptch-settings-tabs.php 3 years ago forms.php 3 years ago pro_banners.php 3 years ago
allowlist.php
420 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 $valid_ip = filter_var( stripslashes( trim( $add_ip ) ), FILTER_VALIDATE_IP );
334
335 if ( $valid_ip ) {
336 $ip_int = sprintf( '%u', ip2long( $valid_ip ) );
337 $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, $valid_ip ) );
338 /* check if IP already in database */
339 if ( is_null( $id ) ) {
340 $time = current_time( 'mysql' );
341 $wpdb->insert(
342 $wpdb->prefix . 'gglcptch_allowlist',
343 array(
344 'ip' => $valid_ip,
345 'ip_from_int' => $ip_int,
346 'ip_to_int' => $ip_int,
347 'add_time' => $time,
348 )
349 );
350 if ( ! $wpdb->last_error ) {
351 $message = __( 'IP added to the allow list successfully.', 'google-captcha' );
352 } else {
353 $error = __( 'Some errors occurred.', 'google-captcha' );
354 }
355 } else {
356 $error = __( 'IP is already in the allow list.', 'google-captcha' );
357 }
358 } else {
359 $error = __( 'Invalid IP. See allowed formats.', 'google-captcha' );
360 }
361 if ( empty( $error ) ) {
362 $gglcptch_options['allowlist_is_empty'] = false;
363 update_option( 'gglcptch_options', $gglcptch_options );
364 }
365 /* Remove IP from database */
366 } elseif ( $bulk_action && check_admin_referer( $this->basename, 'gglcptch_nonce_name' ) ) {
367 if ( ! empty( $_REQUEST['id'] ) ) {
368 foreach ( $_REQUEST['id'] as $key => $value ) {
369 $_REQUEST['id'][ $key ] = absint( $value );
370 }
371 $list = implode( ',', $_REQUEST['id'] );
372 $result = $wpdb->query( 'DELETE FROM `' . $wpdb->prefix . 'gglcptch_allowlist` WHERE `id` IN (' . $list . ');' );
373
374 if ( ! $wpdb->last_error ) {
375 $message = sprintf( _n( '%s IP was deleted successfully.', '%s IPs were deleted successfully.', $result, 'google-captcha' ), $result );
376 $gglcptch_options['allowlist_is_empty'] = is_null( $wpdb->get_var( "SELECT `id` FROM `{$wpdb->prefix}gglcptch_allowlist` LIMIT 1" ) ) ? true : false;
377 update_option( 'gglcptch_options', $gglcptch_options );
378 } else {
379 $error = __( 'Some errors occurred.', 'google-captcha' );
380 }
381 }
382 } elseif ( isset( $_GET['gglcptch_remove'] ) && check_admin_referer( 'gglcptch_nonce_remove_' . sanitize_text_field( wp_unslash( $_GET['gglcptch_remove'] ) ) ) ) {
383
384 $wpdb->delete(
385 $wpdb->prefix . 'gglcptch_allowlist',
386 array(
387 'id' => absint( sanitize_text_field( wp_unslash( $_GET['gglcptch_remove'] ) ) )
388 )
389 );
390
391 if ( ! $wpdb->last_error ) {
392 $message = __( 'One IP was deleted successfully.', 'google-captcha' );
393 $gglcptch_options['allowlist_is_empty'] = is_null( $wpdb->get_var( "SELECT `id` FROM `{$wpdb->prefix}gglcptch_allowlist` LIMIT 1" ) ) ? true : false;
394 update_option( 'gglcptch_options', $gglcptch_options );
395 } else {
396 $error = __( 'Some errors occurred.', 'google-captcha' );
397 }
398 } elseif ( isset( $_POST['gglcptch_submit_add_to_allowlist'] ) && empty( $_POST['gglcptch_add_to_allowlist'] ) ) {
399 $error = __( 'You have not entered any IP.', 'google-captcha' );
400 } elseif ( isset( $_REQUEST['s'] ) ) {
401 if ( '' === sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) ) {
402 $error = __( 'You have not entered any IP in to the search form.', 'google-captcha' );
403 } else {
404 $message = __( 'Search results for', 'google-captcha' ) . '&nbsp;:&nbsp;' . sanitize_text_field( wp_unslash( $_REQUEST['s'] ) );
405 }
406 }
407 if ( ! empty( $message ) ) {
408 ?>
409 <div class="updated fade below-h2"><p><strong><?php echo esc_html( $message ); ?></strong></p></div>
410 <?php
411 }
412 if ( ! empty( $error ) ) {
413 ?>
414 <div class="error below-h2"><p><strong><?php echo esc_html( $error ); ?></strong></p></div>
415 <?php
416 }
417 }
418 }
419 }
420