Created
December 6, 2016 10:20
-
-
Save phlbnks/e00cf84025dd067a9941a33f4d0d9db6 to your computer and use it in GitHub Desktop.
Remove BWS captcha for whitelisted IPs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Check if a given ip is in a network https://gist.github.com/tott/7684443 | |
* @param string $ip IP to check in IPV4 format eg. 127.0.0.1 | |
* @param string $range IP/CIDR netmask eg. 127.0.0.0/24, also 127.0.0.1 is accepted and /32 assumed | |
* @return boolean true if the ip is in this range / false if not. | |
*/ | |
function ip_in_range( $ip, $range ) { | |
if ( strpos( $range, '/' ) == false ) { | |
$range .= '/32'; | |
} | |
// $range is in IP/CIDR format eg 127.0.0.1/24 | |
list( $range, $netmask ) = explode( '/', $range, 2 ); | |
$range_decimal = ip2long( $range ); | |
$ip_decimal = ip2long( $ip ); | |
$wildcard_decimal = pow( 2, ( 32 - $netmask ) ) - 1; | |
$netmask_decimal = ~ $wildcard_decimal; | |
return ( ( $ip_decimal & $netmask_decimal ) == ( $range_decimal & $netmask_decimal ) ); | |
} | |
/** | |
* Remove BWS captcha for whitelisted IPs | |
* Depends on ip_in_range function | |
*/ | |
function mysite_captcha_login_whitelist(){ | |
$captcha_bypass_ips = array( | |
'192.168.1.1', # Single IP | |
'10.0.0.0/24', # IP range | |
); | |
foreach ($captcha_bypass_ips as $key => $value) { | |
if ( ip_in_range( $_SERVER['REMOTE_ADDR'], $value ) ) { | |
remove_action( 'login_form', 'gglcptch_login_display' ); | |
remove_action( 'authenticate', 'gglcptch_login_check', 21, 1 ); | |
break; | |
} | |
} | |
} | |
add_action('init', 'mysite_captcha_login_whitelist'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment