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