Skip to content

Commit cedac5b

Browse files
committedJan 23, 2015
Protect Module: new function
Creating function is_ip_whitelisted. removed a redundant function
1 parent b8e2cd3 commit cedac5b

File tree

2 files changed

+23
-91
lines changed

2 files changed

+23
-91
lines changed
 

‎modules/protect.php

+23-90
Original file line numberDiff line numberDiff line change
@@ -324,28 +324,14 @@ function ip_is_private( $ip )
324324
return false;
325325
}
326326

327-
/**
328-
* Checks the status for a given IP. API results are cached as transients
329-
*
330-
* @param bool $preauth Wether or not we are checking prior to authorization
331-
*
332-
* @return bool Either returns true, fires $this->kill_login, or includes a math fallback
333-
*/
334-
function check_loginability( $preauth = false ) {
335-
336-
$ip = $this->get_ip();
337-
$headers = $this->get_headers();
338-
$header_hash = md5( json_encode( $headers ) );
339-
$transient_name = 'jpp_li_' . $header_hash;
340-
$transient_value = $this->get_transient( $transient_name );
327+
function ip_is_whitelisted( $ip ) {
341328

342-
//Never block login from whitelisted IPs
343329
if ( defined( 'JETPACK_IP_ADDRESS_OK' ) && 'JETPACK_IP_ADDRESS_OK' == $ip ) { // found an exact match in wp-config
344330
return true;
345331
}
346332

347-
$whitelist = get_site_option( 'jetpack_protect_whitelist', array() );
348-
$ip_long = inet_pton( $ip );
333+
$whitelist = get_site_option( 'jetpack_protect_whitelist', array() );
334+
$ip_long = inet_pton( $ip );
349335

350336
if ( ! empty( $whitelist ) ) :
351337
foreach ( $whitelist as $item ) :
@@ -364,7 +350,27 @@ function check_loginability( $preauth = false ) {
364350

365351
endforeach;
366352
endif;
353+
return false;
354+
}
355+
356+
/**
357+
* Checks the status for a given IP. API results are cached as transients
358+
*
359+
* @param bool $preauth Wether or not we are checking prior to authorization
360+
*
361+
* @return bool Either returns true, fires $this->kill_login, or includes a math fallback
362+
*/
363+
function check_loginability( $preauth = false ) {
364+
365+
$headers = $this->get_headers();
366+
$header_hash = md5( json_encode( $headers ) );
367+
$transient_name = 'jpp_li_' . $header_hash;
368+
$transient_value = $this->get_transient( $transient_name );
369+
$ip = $this->get_ip();
367370

371+
if( $this->ip_is_whitelisted( $ip ) ) {
372+
return true;
373+
}
368374

369375
//Check out our transients
370376
if ( isset( $transient_value ) && $transient_value[ 'status' ] == 'ok' ) {
@@ -564,79 +570,6 @@ public function save_whitelist_ajax() {
564570
exit;
565571
}
566572

567-
public function save_whitelist( $global = false ) {
568-
569-
global $current_user;
570-
$whitelist = is_array( $_POST['whitelist'] ) ? $_POST['whitelist'] : array();
571-
$new_items = array();
572-
573-
// validate each item
574-
foreach( $whitelist as $item ) {
575-
576-
if ( ! isset( $item['range'] ) ) {
577-
$this->whitelist_error = true;
578-
break;
579-
}
580-
581-
if ( ! in_array( $item['range'], array( '1', '0' ) ) ) {
582-
$this->whitelist_error = true;
583-
break;
584-
}
585-
586-
$range = $item['range'];
587-
$new_item = new stdClass();
588-
$new_item->range = (bool) $range;
589-
$new_item->global = $global;
590-
$new_item->user_id = $current_user->ID;
591-
592-
if ( $range ) {
593-
594-
if ( ! isset( $item['range_low'] ) || ! isset( $item['range_high'] ) ) {
595-
$this->whitelist_error = true;
596-
break;
597-
}
598-
599-
if ( ! inet_pton( $item['range_low'] ) || ! inet_pton( $item['range_high'] ) ) {
600-
$this->whitelist_error = true;
601-
break;
602-
}
603-
604-
$new_item->range_low = $item['range_low'];
605-
$new_item->range_high = $item['range_high'];
606-
607-
} else {
608-
609-
if ( ! isset( $item['ip_address'] ) ) {
610-
$this->whitelist_error = true;
611-
break;
612-
}
613-
614-
if ( ! inet_pton( $item['ip_address'] ) ) {
615-
$this->whitelist_error = true;
616-
break;
617-
}
618-
619-
$new_item->ip_address = $item['ip_address'];
620-
}
621-
622-
$new_items[] = $new_item;
623-
624-
} // end item loop
625-
626-
if ( ! empty( $this->whitelist_error ) ) {
627-
return false;
628-
}
629-
630-
// merge new items with existing items
631-
$this->whitelist = get_site_option( 'jetpack_protect_whitelist', array() );
632-
$current_user_whitelist = wp_list_filter( $this->whitelist, array( 'user_id' => $current_user->ID, 'global'=> ! $global) );
633-
$other_user_whtielist = wp_list_filter( $this->whitelist, array( 'user_id' => $current_user->ID ), 'NOT' );
634-
$new_whitelist = array_merge( $new_items, $current_user_whitelist, $other_user_whtielist );
635-
636-
update_site_option( 'jetpack_protect_whitelist', $new_whitelist );
637-
return true;
638-
}
639-
640573
/**
641574
* Calls over to the api using wp_remote_post
642575
*

‎modules/protect/config-ui.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php /*
22
To-do:
3-
1. Add current IP above table (I added the placeholder - jeff)
43
2. Have current ip autofilled in the IP field (I added the placeholder - jeff)
54
3. Pissibly remove "range" toggle for this: https://cloudup.com/c_gL7b8vmwn
65
4. Prevent duplicates froms displaying / being added

0 commit comments

Comments
 (0)
Please sign in to comment.