Completed
Push — master ( 5836e1...99cc50 )
by Sam
15s queued 10s
created

Validator::resourceRecordName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of Badcow DNS Library.
5
 *
6
 * (c) Samuel Williams <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Badcow\DNS;
13
14
use Badcow\DNS\Rdata\NS;
15
use Badcow\DNS\Rdata\SOA;
16
17
class Validator
18
{
19
    const ZONE_OKAY = 0;
20
21
    const ZONE_NO_SOA = 1;
22
23
    const ZONE_TOO_MANY_SOA = 2;
24
25
    const ZONE_NO_NS = 4;
26
27
    const ZONE_NO_CLASS = 8;
28
29
    const ZONE_TOO_MANY_CLASSES = 16;
30
31
    /**
32
     * Validate the string as a valid hostname in accordance with RFC 952 {@link https://tools.ietf.org/html/rfc952}
33
     * and RFC 1123 {@link https://tools.ietf.org/html/rfc1123}.
34
     *
35
     * @param string $name
36
     *
37
     * @return bool
38
     */
39
    public static function hostName(string $name): bool
40
    {
41
        return self::fullyQualifiedDomainName(rtrim($name, '.').'.');
42
    }
43
44
    /**
45
     * Validate the string is a Fully Qualified Domain Name.
46
     *
47
     * @param string $name
48
     *
49
     * @return bool
50
     */
51 3
    public static function fullyQualifiedDomainName(string $name): bool
52
    {
53 3
        $isValid = strlen($name) < 254;
54 3
        $isValid &= 1 === preg_match('/^(?:(?!-)[a-z0-9\-]{1,63}(?<!-)\.){1,127}$/i', $name);
55
56 3
        return $isValid;
57
    }
58
59
    /**
60
     * Validate the name for a Resource Record. This is distinct from validating a hostname in that this function
61
     * will permit '@' and wildcards as well as underscores used in SRV records.
62
     *
63
     * @param string $name
64
     *
65
     * @return bool
66
     */
67 3
    public static function resourceRecordName(string $name): bool
68
    {
69 3
        $isValid = strlen($name) < 254;
70 3
        $isValid &= 1 === preg_match('/(?:^(?:\*\.)?((?!-)[a-z0-9_\-]{1,63}(?<!-)\.?){1,127}$)|^@$|^\*$/i', $name);
71
72 3
        return $isValid;
73
    }
74
75
    /**
76
     * Validates an IPv4 Address.
77
     *
78
     * @static
79
     *
80
     * @param string $address
81
     *
82
     * @return bool
83
     */
84 1
    public static function ipv4(string $address): bool
85
    {
86 1
        return (bool) filter_var($address, FILTER_VALIDATE_IP, [
87 1
            'flags' => FILTER_FLAG_IPV4,
88
        ]);
89
    }
90
91
    /**
92
     * Validates an IPv6 Address.
93
     *
94
     * @static
95
     *
96
     * @param string $address
97
     *
98
     * @return bool
99
     */
100 5
    public static function ipv6(string $address): bool
101
    {
102 5
        return (bool) filter_var($address, FILTER_VALIDATE_IP, [
103 5
            'flags' => FILTER_FLAG_IPV6,
104
        ]);
105
    }
106
107
    /**
108
     * Validates an IPv4 or IPv6 address.
109
     *
110
     * @static
111
     *
112
     * @param $address
113
     *
114
     * @return bool
115
     */
116 1
    public static function ipAddress(string $address): bool
117
    {
118 1
        return (bool) filter_var($address, FILTER_VALIDATE_IP);
119
    }
120
121
    /**
122
     * Validates that the zone meets
123
     * RFC-1035 especially that:
124
     *   1) 5.2.1 All RRs in the file should be of the same class.
125
     *   2) 5.2.2 Exactly one SOA RR should be present at the top of the zone.
126
     *   3) There is at least one NS record.
127
     *
128
     * Return values are:
129
     *   - ZONE_NO_SOA
130
     *   - ZONE_TOO_MANY_SOA
131
     *   - ZONE_NO_NS
132
     *   - ZONE_NO_CLASS
133
     *   - ZONE_TOO_MANY_CLASSES
134
     *   - ZONE_OKAY
135
     *
136
     * You SHOULD compare these return values to the defined constants of this
137
     * class rather than against integers directly.
138
     *
139
     * @param Zone $zone
140
     *
141
     * @return int
142
     */
143 4
    public static function zone(Zone $zone): int
144
    {
145 4
        $n_soa = self::countResourceRecords($zone, SOA::TYPE);
146 4
        $n_ns = self::countResourceRecords($zone, NS::TYPE);
147 4
        $n_class = self::countClasses($zone);
148
149 4
        $totalError = 0;
150
151
        $incrementError = function (bool $errorCondition, int $errorOrdinal) use (&$totalError) {
152 4
            $totalError += $errorCondition ? $errorOrdinal : 0;
153 4
        };
154
155 4
        $incrementError($n_soa < 1, self::ZONE_NO_SOA);
156 4
        $incrementError($n_soa > 1, self::ZONE_TOO_MANY_SOA);
157 4
        $incrementError($n_ns < 1, self::ZONE_NO_NS);
158 4
        $incrementError($n_class < 1, self::ZONE_NO_CLASS);
159 4
        $incrementError($n_class > 1, self::ZONE_TOO_MANY_CLASSES);
160
161 4
        return $totalError;
162
    }
163
164
    /**
165
     * Counts the number of Resource Records of a particular type ($type) in a Zone.
166
     *
167
     * @param Zone   $zone
168
     * @param string $type The ResourceRecord type to be counted. If NULL, then the method will return
169
     *                     the number of records without RData.
170
     *
171
     * @return int the number of records to be counted
172
     */
173 4
    public static function countResourceRecords(Zone $zone, ?string $type = null): int
174
    {
175 4
        $n = 0;
176 4
        foreach ($zone as $rr) {
177 4
            $n += (int) ($type === $rr->getType());
178
        }
179
180 4
        return $n;
181
    }
182
183
    /**
184
     * Validates a reverse IPv4 address. Ensures that all octets are in the range [0-255].
185
     *
186
     * @param string $address
187
     *
188
     * @return bool
189
     */
190 1
    public static function reverseIpv4(string $address): bool
191
    {
192 1
        $pattern = '/^((?:[0-9]+\.){1,4})in\-addr\.arpa\.$/i';
193
194 1
        if (1 !== preg_match($pattern, $address, $matches)) {
195 1
            return false;
196
        }
197
198 1
        $octets = explode('.', $matches[1]);
199 1
        array_pop($octets); //Remove the last decimal from the array
200
201 1
        foreach ($octets as $octet) {
202 1
            if ((int) $octet > 255) {
203 1
                return false;
204
            }
205
        }
206
207 1
        return true;
208
    }
209
210
    /**
211
     * Validates a reverse IPv6 address.
212
     *
213
     * @param string $address
214
     *
215
     * @return bool
216
     */
217 1
    public static function reverseIpv6(string $address): bool
218
    {
219 1
        $pattern = '/^(?:[0-9a-f]\.){1,32}ip6\.arpa\.$/i';
220
221 1
        return 1 === preg_match($pattern, $address);
222
    }
223
224
    /**
225
     * Determine the number of unique non-null classes is a Zone. In a valid zone this MUST be 1.
226
     *
227
     * @param Zone $zone
228
     *
229
     * @return int
230
     */
231 4
    private static function countClasses(Zone $zone): int
232
    {
233 4
        $classes = [];
234
235 4
        foreach ($zone as $rr) {
236 4
            if (null !== $rr->getClass()) {
237 4
                $classes[$rr->getClass()] = null;
238
            }
239
        }
240
241 4
        return count($classes);
242
    }
243
}
244