Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for whitelisting of domains as well as IP addresses. #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,27 @@ function sanitize_ip_addresses( $val ) {
*/
private function validate_ip_address( $ip_address ) {

return filter_var( $ip_address, FILTER_VALIDATE_IP );
$valid = filter_var( $ip_address, FILTER_VALIDATE_IP );

if ( ( false === $ip_address || empty( $valid ) ) && $this->is_valid_domain_name( $ip_address ) ) {
$valid = $ip_address;
}

return $valid;

}

/**
* Is Valid Domain Name?
*
* @param string $domain_name Domain name to check.
* @return boolean
*/
private function is_valid_domain_name( $domain_name ) {

return ( preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $domain_name ) // Valid chars check
&& preg_match( "/^.{1,253}$/", $domain_name ) // Overall length check
&& preg_match( "/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $domain_name ) ); // Length of each label

}

Expand Down
11 changes: 6 additions & 5 deletions password-protected.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,19 @@ function allow_users( $bool ) {
}

/**
* Allow IP Addresses
* Allow IP Addresses and Hosts
*
* If user has a valid email address, return false to disable password protection.
* If user has a valid IP address or host, return false to disable password protection.
*
* @param boolean $bool Allow IP addresses.
* @return boolean True/false.
* @param boolean $bool Allow IP addresses or hostnames.
* @return boolean
*/
function allow_ip_addresses( $bool ) {

$ip_addresses = $this->get_allowed_ip_addresses();
$hosts = array_map( 'gethostbyname', $ip_addresses );

if ( in_array( $_SERVER['REMOTE_ADDR'], $ip_addresses ) ) {
if ( in_array( $_SERVER['REMOTE_ADDR'], $ip_addresses ) || in_array( $_SERVER['REMOTE_ADDR'], $hosts ) ) {
$bool = false;
}

Expand Down