Created
March 4, 2016 14:09
-
-
Save wp-kitten/ad8da10000afe1d4276c to your computer and use it in GitHub Desktop.
VaultPress - validate IP address
This file contains hidden or 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
| function vaultpress_validate_ip_address( $rxs ) { | |
| $remote_ips = array(); | |
| if ( !empty( $_SERVER['REMOTE_ADDR'] ) ) | |
| $remote_ips[] = $_SERVER['REMOTE_ADDR']; | |
| if ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) | |
| $remote_ips[] = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
| $iprx = '/^([0-9]+\.[0-9]+\.[0-9]+\.)([0-9]+)$/'; | |
| foreach ( $remote_ips as $remote_ip ) { | |
| if ( !preg_match( $iprx, $remote_ip, $r ) ) { | |
| $__vp_validate_error = "remote_addr_fail"; | |
| return false; | |
| } | |
| foreach ( (array)$rxs as $begin => $end ) { | |
| if ( !preg_match( $iprx, $begin, $b ) ) | |
| continue; | |
| if ( !preg_match( $iprx, $end, $e ) ) | |
| continue; | |
| if ( $r[1] != $b[1] || $r[1] != $e[1] ) | |
| continue; | |
| $me = $r[2]; | |
| $b = min( $b[2],$e[2] ); | |
| $e = max( $b[2],$e[2] ); | |
| if ( $me >= $b && $me <= $e ) { | |
| return true; | |
| } | |
| } | |
| } | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment