summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/validator/src/CidrValidator.php
blob: 32c11625911a23bfa26eea2c5848e98905524b7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

namespace ipl\Validator;

use ipl\I18n\Translation;
use ipl\Stdlib\Str;

/**
 * Validate a classless inter-domain routing (CIDR)
 */
class CidrValidator extends BaseValidator
{
    use Translation;

    public function isValid($value): bool
    {
        $this->clearMessages();

        $pieces = Str::trimSplit($value, '/');
        if (count($pieces) !== 2) {
            $this->addMessage(sprintf(
                $this->translate('CIDR "%s" does not conform to the required format $address/$prefix'),
                $value
            ));

            return false;
        }

        list($address, $prefix) = $pieces;
        $inaddr = @inet_pton($address);
        if ($inaddr === false) {
            $this->addMessage(sprintf($this->translate('CIDR "%s" contains an invalid address'), $value));

            return false;
        }

        if (! is_numeric($prefix)) {
            $this->addMessage(sprintf($this->translate('Prefix of CIDR "%s" must be a number'), $value));

            return false;
        }

        $isIPv6 = isset($inaddr[4]);
        $prefix = (int) $prefix;
        $maxPrefixLength = $isIPv6 ? 128 : 32;

        if ($prefix < 0 || $prefix > $maxPrefixLength) {
            $this->addMessage(sprintf(
                $this->translate('Prefix length of CIDR "%s" must be between 0 and %d for IPv%d addresses'),
                $value,
                $maxPrefixLength,
                $isIPv6 ? 6 : 4
            ));

            return false;
        }

        return true;
    }
}