Completed
Branch v2 (e11fee)
by Sam
01:28
created

Validator::zone()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 17
cts 17
cp 1
rs 9.488
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 4
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 1
    public static function hostName(string $name): bool
40
    {
41 1
        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
        $labels = explode('.', rtrim($name, '.'));
54
55 3
        $isValid = true;
56
57
        //Is there a trailing dot?
58 3
        $isValid &= ('.' === substr($name, -1, 1));
59
60
        //Are there less than 254 characters?
61 3
        $isValid &= strlen($name) < 254;
62
63
        //Are there less than 128 levels?
64 3
        $isValid &= count($labels) < 128;
65
66 3
        foreach ($labels as $label) {
67
            //Does the label start with a hyphen?
68 3
            $isValid &= ('-' !== substr($label, 0, 1));
69
70
            //Does the label end with a hyphen?
71 3
            $isValid &= ('-' !== substr($label, -1, 1));
72
73
            //Does the label contain anything other than alphanumeric characters or hyphens?
74 3
            $isValid &= (1 === preg_match('/^[a-zA-Z0-9\-]+$/i', $label));
75
76
            //Is the length of the label between 1 and 63 characters?
77 3
            $isValid &= strlen($label) > 0 && strlen($label) < 64;
78
        }
79
80 3
        return $isValid;
81
    }
82
83
    /**
84
     * Validate the name for a Resource Record. This is distinct from validating a hostname in that this function
85
     * will permit '@' and wildcards as well as underscores used in SRV records.
86
     *
87
     * @param string $name
88
     *
89
     * @return bool
90
     */
91 3
    public static function resourceRecordName(string $name): bool
92
    {
93 3
        if ('@' === $name) {
94 1
            return true;
95
        }
96
97 3
        if ('*.' === $name) {
98 2
            return false;
99
        }
100
101 3
        $labels = explode('.', rtrim($name, '.'));
102
103 3
        $isValid = true;
104
105
        //Are there less than 254 characters?
106 3
        $isValid &= strlen($name) < 254;
107
108
        //Are there less than 128 levels?
109 3
        $isValid &= count($labels) < 128;
110
111 3
        foreach ($labels as $i => $label) {
112
            //Is the first label a wildcard?
113 3
            if (0 === $i && '*' === $label) {
114 2
                $isValid &= true;
115 2
                continue;
116
            }
117
118
            //Does the label start with a hyphen?
119 3
            $isValid &= ('-' !== substr($label, 0, 1));
120
121
            //Does the label end with a hyphen?
122 3
            $isValid &= ('-' !== substr($label, -1, 1));
123
124
            //Does the label contain anything other than alphanumeric characters, underscores or hyphens?
125 3
            $isValid &= (1 === preg_match('/^[a-zA-Z0-9\-_]+$/i', $label));
126
127
            //Is the length of the label between 1 and 63 characters?
128 3
            $isValid &= strlen($label) > 0 && strlen($label) < 64;
129
        }
130
131 3
        return $isValid;
132
    }
133
134
    /**
135
     * Validates an IPv4 Address.
136
     *
137
     * @static
138
     *
139
     * @param string $address
140
     *
141
     * @return bool
142
     */
143 1
    public static function ipv4(string $address): bool
144
    {
145 1
        return (bool) filter_var($address, FILTER_VALIDATE_IP, [
146 1
            'flags' => FILTER_FLAG_IPV4,
147
        ]);
148
    }
149
150
    /**
151
     * Validates an IPv6 Address.
152
     *
153
     * @static
154
     *
155
     * @param string $address
156
     *
157
     * @return bool
158
     */
159 5
    public static function ipv6(string $address): bool
160
    {
161 5
        return (bool) filter_var($address, FILTER_VALIDATE_IP, [
162 5
            'flags' => FILTER_FLAG_IPV6,
163
        ]);
164
    }
165
166
    /**
167
     * Validates an IPv4 or IPv6 address.
168
     *
169
     * @static
170
     *
171
     * @param $address
172
     *
173
     * @return bool
174
     */
175 1
    public static function ipAddress(string $address): bool
176
    {
177 1
        return (bool) filter_var($address, FILTER_VALIDATE_IP);
178
    }
179
180
    /**
181
     * Validates that the zone meets
182
     * RFC-1035 especially that:
183
     *   1) 5.2.1 All RRs in the file should be of the same class.
184
     *   2) 5.2.2 Exactly one SOA RR should be present at the top of the zone.
185
     *   3) There is at least one NS record.
186
     *
187
     * Return values are:
188
     *   - ZONE_NO_SOA
189
     *   - ZONE_TOO_MANY_SOA
190
     *   - ZONE_NO_NS
191
     *   - ZONE_NO_CLASS
192
     *   - ZONE_TOO_MANY_CLASSES
193
     *   - ZONE_OKAY
194
     *
195
     * You SHOULD compare these return values to the defined constants of this
196
     * class rather than against integers directly.
197
     *
198
     * @param Zone $zone
199
     *
200
     * @return int
201
     */
202 4
    public static function zone(Zone $zone): int
203
    {
204 4
        $n_soa = self::countResourceRecords($zone, SOA::TYPE);
205 4
        $n_ns = self::countResourceRecords($zone, NS::TYPE);
206 4
        $classes = [];
207
208 4
        foreach ($zone as $rr) {
209 4
            if (null !== $rr->getClass()) {
210 4
                $classes[$rr->getClass()] = null;
211
            }
212
        }
213
214 4
        $n_class = count($classes);
215 4
        $totalError = 0;
216
217
        $incrementError = function (bool $errorCondition, int $errorOrdinal) use (&$totalError) {
218 4
            $totalError += $errorCondition ? $errorOrdinal : 0;
219 4
        };
220
221 4
        $incrementError($n_soa < 1, self::ZONE_NO_SOA);
222 4
        $incrementError($n_soa > 1, self::ZONE_TOO_MANY_SOA);
223 4
        $incrementError($n_ns < 1, self::ZONE_NO_NS);
224 4
        $incrementError($n_class < 1, self::ZONE_NO_CLASS);
225 4
        $incrementError($n_class > 1, self::ZONE_TOO_MANY_CLASSES);
226
227 4
        return $totalError;
228
    }
229
230
    /**
231
     * Counts the number of Resource Records of a particular type ($type) in a Zone.
232
     *
233
     * @param Zone   $zone
234
     * @param string $type The ResourceRecord type to be counted. If NULL, then the method will return
235
     *                     the number of records without RData.
236
     *
237
     * @return int the number of records to be counted
238
     */
239 4
    public static function countResourceRecords(Zone $zone, ?string $type = null): int
240
    {
241 4
        $n = 0;
242 4
        foreach ($zone as $rr) {
243 4
            $n += (int) ($type === $rr->getType());
244
        }
245
246 4
        return $n;
247
    }
248
249
    /**
250
     * Validates a reverse IPv4 address. Ensures that all octets are in the range [0-255].
251
     *
252
     * @param string $address
253
     *
254
     * @return bool
255
     */
256 1
    public static function reverseIpv4(string $address): bool
257
    {
258 1
        $pattern = '/^((?:[0-9]+\.){1,4})in\-addr\.arpa\.$/i';
259
260 1
        if (1 !== preg_match($pattern, $address, $matches)) {
261 1
            return false;
262
        }
263
264 1
        $octets = explode('.', $matches[1]);
265 1
        array_pop($octets); //Remove the last decimal from the array
266
267 1
        foreach ($octets as $octet) {
268 1
            if ((int) $octet > 255) {
269 1
                return false;
270
            }
271
        }
272
273 1
        return true;
274
    }
275
276
    /**
277
     * Validates a reverse IPv6 address.
278
     *
279
     * @param string $address
280
     *
281
     * @return bool
282
     */
283 1
    public static function reverseIpv6(string $address): bool
284
    {
285 1
        $pattern = '/^(?:[0-9a-f]\.){1,32}ip6\.arpa\.$/i';
286
287 1
        return 1 === preg_match($pattern, $address);
288
    }
289
}
290