2010-10-18 06:26:02 +02:00
|
|
|
<?php
|
2015-06-08 16:47:14 +02:00
|
|
|
|
2015-06-08 17:09:25 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Respect/Validation.
|
|
|
|
*
|
|
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the "LICENSE.md"
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2010-10-18 06:26:02 +02:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2013-01-09 15:48:11 +01:00
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
|
|
|
2010-10-18 06:26:02 +02:00
|
|
|
class Ip extends AbstractRule
|
|
|
|
{
|
2011-02-10 21:49:48 +01:00
|
|
|
public $ipOptions;
|
|
|
|
|
2018-05-23 12:24:11 +02:00
|
|
|
public $range;
|
2013-01-09 15:48:11 +01:00
|
|
|
public $networkRange;
|
|
|
|
|
2015-01-08 03:15:35 +01:00
|
|
|
public function __construct($ipOptions = null)
|
2010-10-18 06:26:02 +02:00
|
|
|
{
|
2013-01-09 15:48:11 +01:00
|
|
|
if (is_int($ipOptions)) {
|
|
|
|
$this->ipOptions = $ipOptions;
|
2013-01-14 22:55:03 +01:00
|
|
|
|
2015-01-08 03:15:35 +01:00
|
|
|
return;
|
2013-01-09 15:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->networkRange = $this->parseRange($ipOptions);
|
2018-05-23 12:24:11 +02:00
|
|
|
$this->range = $this->createRange();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function createRange(): ?string
|
|
|
|
{
|
|
|
|
if (!$this->networkRange) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$range = $this->networkRange;
|
|
|
|
$message = $range['min'];
|
|
|
|
|
|
|
|
if (isset($range['max'])) {
|
|
|
|
$message .= '-'.$range['max'];
|
|
|
|
} else {
|
|
|
|
$message .= '/'.long2ip((int) $range['mask']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $message;
|
2013-01-09 15:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function parseRange($input)
|
|
|
|
{
|
2017-11-19 19:01:31 +01:00
|
|
|
if (null === $input || '*' == $input || '*.*.*.*' == $input
|
|
|
|
|| '0.0.0.0-255.255.255.255' == $input) {
|
2015-01-08 03:15:35 +01:00
|
|
|
return;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2013-01-09 15:48:11 +01:00
|
|
|
|
2015-10-18 03:44:47 +02:00
|
|
|
$range = ['min' => null, 'max' => null, 'mask' => null];
|
2013-01-09 15:48:11 +01:00
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
if (false !== mb_strpos($input, '-')) {
|
2013-01-09 15:48:11 +01:00
|
|
|
list($range['min'], $range['max']) = explode('-', $input);
|
2017-11-19 19:01:31 +01:00
|
|
|
} elseif (false !== mb_strpos($input, '*')) {
|
2013-01-09 15:48:11 +01:00
|
|
|
$this->parseRangeUsingWildcards($input, $range);
|
2017-11-19 19:01:31 +01:00
|
|
|
} elseif (false !== mb_strpos($input, '/')) {
|
2013-01-09 15:48:11 +01:00
|
|
|
$this->parseRangeUsingCidr($input, $range);
|
2013-01-14 22:55:03 +01:00
|
|
|
} else {
|
2013-01-09 15:48:11 +01:00
|
|
|
throw new ComponentException('Invalid network range');
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2013-01-09 15:48:11 +01:00
|
|
|
|
|
|
|
if (!$this->verifyAddress($range['min'])) {
|
|
|
|
throw new ComponentException('Invalid network range');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($range['max']) && !$this->verifyAddress($range['max'])) {
|
|
|
|
throw new ComponentException('Invalid network range');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $range;
|
|
|
|
}
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
protected function fillAddress(&$input, $char = '*'): void
|
2013-01-09 15:48:11 +01:00
|
|
|
{
|
2016-10-30 10:44:52 +01:00
|
|
|
while (mb_substr_count($input, '.') < 3) {
|
2015-01-08 03:15:35 +01:00
|
|
|
$input .= '.'.$char;
|
2013-01-09 15:48:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
protected function parseRangeUsingWildcards($input, &$range): void
|
2013-01-09 15:48:11 +01:00
|
|
|
{
|
|
|
|
$this->fillAddress($input);
|
|
|
|
|
|
|
|
$range['min'] = strtr($input, '*', '0');
|
|
|
|
$range['max'] = str_replace('*', '255', $input);
|
|
|
|
}
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
protected function parseRangeUsingCidr($input, &$range): void
|
2013-01-09 15:48:11 +01:00
|
|
|
{
|
|
|
|
$input = explode('/', $input);
|
|
|
|
$this->fillAddress($input[0], '0');
|
|
|
|
|
|
|
|
$range['min'] = $input[0];
|
2017-11-19 19:01:31 +01:00
|
|
|
$isAddressMask = false !== mb_strpos($input[1], '.');
|
2013-01-09 15:48:11 +01:00
|
|
|
|
|
|
|
if ($isAddressMask && $this->verifyAddress($input[1])) {
|
2013-01-14 15:35:04 +01:00
|
|
|
$range['mask'] = sprintf('%032b', ip2long($input[1]));
|
2013-01-09 15:48:11 +01:00
|
|
|
|
2015-01-08 03:15:35 +01:00
|
|
|
return;
|
2013-01-09 15:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($isAddressMask || $input[1] < 8 || $input[1] > 30) {
|
|
|
|
throw new ComponentException('Invalid network mask');
|
|
|
|
}
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
$range['mask'] = sprintf('%032b', ip2long(long2ip(~(2 ** (32 - $input[1]) - 1))));
|
2010-10-18 06:26:02 +02:00
|
|
|
}
|
|
|
|
|
2018-01-28 15:41:31 +01:00
|
|
|
public function validate($input): bool
|
2013-01-09 15:48:11 +01:00
|
|
|
{
|
|
|
|
return $this->verifyAddress($input) && $this->verifyNetwork($input);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function verifyAddress($address)
|
2010-10-18 06:26:02 +02:00
|
|
|
{
|
2016-10-30 10:39:23 +01:00
|
|
|
return (bool) filter_var(
|
2013-01-09 15:48:11 +01:00
|
|
|
$address,
|
|
|
|
FILTER_VALIDATE_IP,
|
2015-10-18 03:44:47 +02:00
|
|
|
[
|
2015-01-08 03:15:35 +01:00
|
|
|
'flags' => $this->ipOptions,
|
2015-10-18 03:44:47 +02:00
|
|
|
]
|
2010-10-18 06:26:02 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-01-09 15:48:11 +01:00
|
|
|
protected function verifyNetwork($input)
|
|
|
|
{
|
2017-11-19 19:01:31 +01:00
|
|
|
if (null === $this->networkRange) {
|
2013-01-09 15:48:11 +01:00
|
|
|
return true;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2013-01-09 15:48:11 +01:00
|
|
|
|
2013-01-14 15:35:04 +01:00
|
|
|
if (isset($this->networkRange['mask'])) {
|
2013-01-14 17:11:51 +01:00
|
|
|
return $this->belongsToSubnet($input);
|
2013-01-09 15:48:11 +01:00
|
|
|
}
|
|
|
|
|
2013-01-14 15:35:04 +01:00
|
|
|
$input = sprintf('%u', ip2long($input));
|
|
|
|
|
|
|
|
return bccomp($input, sprintf('%u', ip2long($this->networkRange['min']))) >= 0
|
|
|
|
&& bccomp($input, sprintf('%u', ip2long($this->networkRange['max']))) <= 0;
|
|
|
|
}
|
|
|
|
|
2013-01-14 17:11:51 +01:00
|
|
|
protected function belongsToSubnet($input)
|
2013-01-14 15:35:04 +01:00
|
|
|
{
|
|
|
|
$range = $this->networkRange;
|
|
|
|
$min = sprintf('%032b', ip2long($range['min']));
|
|
|
|
$input = sprintf('%032b', ip2long($input));
|
|
|
|
|
|
|
|
return ($input & $range['mask']) === ($min & $range['mask']);
|
2013-01-09 15:48:11 +01:00
|
|
|
}
|
|
|
|
}
|