| <?php |
|
|
| namespace JsonRPC\Validator; |
|
|
| use JsonRPC\Exception\AccessDeniedException; |
|
|
| |
| |
| |
| |
| |
| |
| class HostValidator |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function validate(array $hosts, $remoteAddress) |
| { |
| if (!empty($hosts)) { |
| foreach ($hosts as $host) { |
| if (self::ipMatch($remoteAddress, $host)) { |
| return; |
| } |
| } |
| throw new AccessDeniedException('Access Forbidden'); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| public static function ipMatch($remoteAddress, $host) |
| { |
| $host = trim($host); |
| if (strpos($host, '/') !== false) { |
| list($network, $mask) = explode('/', $host); |
| if (self::netMatch($remoteAddress, $network, $mask)) { |
| return true; |
| } |
| } |
|
|
| if ($host === $remoteAddress) { |
| return true; |
| } |
|
|
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function netMatch($clientIp, $networkIp, $mask) |
| { |
| $mask1 = 32 - $mask; |
| return ((ip2long($clientIp) >> $mask1) == (ip2long($networkIp) >> $mask1)); |
| } |
| } |
|
|