diff --git a/admin/admin.php b/admin/admin.php index 87a37ab..bebed67 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -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 } diff --git a/password-protected.php b/password-protected.php index 5008738..eea12aa 100644 --- a/password-protected.php +++ b/password-protected.php @@ -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; }