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