/
Domain.php
373 lines (328 loc) · 9.62 KB
/
Domain.php
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/**
* PHP Domain Parser: Public Suffix List based URL parsing.
*
* @see http://github.com/jeremykendall/php-domain-parser for the canonical source repository
*
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
* @license http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License
*/
declare(strict_types=1);
namespace Pdp;
use JsonSerializable;
/**
* Domain Value Object
*
* WARNING: "Some people use the PSL to determine what is a valid domain name
* and what isn't. This is dangerous, particularly in these days where new
* gTLDs are arriving at a rapid pace, if your software does not regularly
* receive PSL updates, it will erroneously think new gTLDs are not
* valid. The DNS is the proper source for this innormalizeion. If you must use
* it for this purpose, please do not bake static copies of the PSL into your
* software with no update mechanism."
*
* @author Jeremy Kendall <jeremy@jeremykendall.net>
* @author Ignace Nyamagana Butera <nyamsprod@gmail.com>
*/
final class Domain implements DomainInterface, JsonSerializable
{
use IDNAConverterTrait;
/**
* @var string|null
*/
private $domain;
/**
* @var string[]
*/
private $labels;
/**
* @var PublicSuffix
*/
private $publicSuffix;
/**
* @var string|null
*/
private $registrableDomain;
/**
* @var string|null
*/
private $subDomain;
/**
* {@inheritdoc}
*/
public static function __set_state(array $properties): self
{
return new self($properties['domain'], $properties['publicSuffix']);
}
/**
* New instance.
*
* @param mixed $domain
* @param PublicSuffix $publicSuffix
*/
public function __construct($domain = null, PublicSuffix $publicSuffix = null)
{
list($this->domain, $this->labels) = $this->setDomain($domain);
$this->publicSuffix = $this->setPublicSuffix($publicSuffix ?? new PublicSuffix());
$this->registrableDomain = $this->setRegistrableDomain();
$this->subDomain = $this->setSubDomain();
}
/**
* Sets the public suffix domain part.
*
* @param PublicSuffix $publicSuffix
*
* @throws Exception If the domain can not contain a public suffix
* @throws Exception If the domain value is the same as the public suffix value
* @throws Exception If the domain can not be match with the public suffix
*
* @return PublicSuffix
*/
private function setPublicSuffix(PublicSuffix $publicSuffix): PublicSuffix
{
if (null === $publicSuffix->getContent()) {
return new PublicSuffix();
}
if (null === $this->domain || false === strpos($this->domain, '.')) {
throw new Exception(sprintf('The domain `%s` can not contain a public suffix', $this->domain));
}
static $pattern = '/[^\x20-\x7f]/';
if (preg_match($pattern, $this->domain)) {
$publicSuffix = $publicSuffix->toUnicode();
}
$publicSuffixContent = $publicSuffix->getContent();
if ($this->domain === $publicSuffixContent) {
throw new Exception(sprintf('The public suffix `%s` can not be equal to the domain name `%s`', $publicSuffixContent, $this->domain));
}
if ('.'.$publicSuffixContent !== substr($this->domain, - strlen($publicSuffixContent) - 1)) {
throw new Exception(sprintf('The public suffix `%s` can not be assign to the domain name `%s`', $publicSuffixContent, $this->domain));
}
return $publicSuffix;
}
/**
* Computes the registrable domain part.
*/
private function setRegistrableDomain()
{
if (null === $this->publicSuffix->getContent()) {
return null;
}
return implode('.', array_slice(
explode('.', $this->domain),
count($this->labels) - count($this->publicSuffix) - 1
));
}
/**
* Computes the sub domain part.
*
* @return string|null
*/
private function setSubDomain()
{
if (null === $this->registrableDomain) {
return null;
}
$nbLabels = count($this->labels);
$nbRegistrableLabels = count($this->publicSuffix) + 1;
if ($nbLabels === $nbRegistrableLabels) {
return null;
}
return implode('.', array_slice(
explode('.', $this->domain),
0,
$nbLabels - $nbRegistrableLabels
));
}
/**
* {@inheritdoc}
*/
public function getIterator()
{
foreach ($this->labels as $offset => $label) {
yield $label;
}
}
/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return [
'domain' => $this->domain,
'registrableDomain' => $this->registrableDomain,
'subDomain' => $this->subDomain,
'publicSuffix' => $this->publicSuffix->getContent(),
'isKnown' => $this->isKnown(),
'isICANN' => $this->isICANN(),
'isPrivate' => $this->isPrivate(),
];
}
/**
* {@inheritdoc}
*/
public function __debugInfo()
{
return $this->jsonSerialize();
}
/**
* {@inheritdoc}
*/
public function count()
{
return count($this->labels);
}
/**
* {@inheritdoc}
*/
public function getContent()
{
return $this->domain;
}
/**
* Returns the full domain name.
*
* DEPRECATION WARNING! This method will be removed in the next major point release
*
* @deprecated 5.3 deprecated
* @see Domain::getContent
*
* This method should return null on seriously malformed domain name
*
* @return string|null
*/
public function getDomain()
{
return $this->getContent();
}
/**
* {@inheritdoc}
*/
public function getLabel(int $key)
{
if ($key < 0) {
$key += count($this->labels);
}
return $this->labels[$key] ?? null;
}
/**
* {@inheritdoc}
*/
public function keys(string $label): array
{
return array_keys($this->labels, $label, true);
}
/**
* Returns the registrable domain.
*
* The registered or registrable domain is the public suffix plus one additional label.
*
* This method should return null if the registrable domain is the same as the public suffix.
*
* @return string|null registrable domain
*/
public function getRegistrableDomain()
{
return $this->registrableDomain;
}
/**
* Returns the sub domain.
*
* The sub domain represents the remaining labels without the registrable domain.
*
* This method should return null if the registrable domain is null
* This method should return null if the registrable domain is the same as the public suffix
*
* @return string|null registrable domain
*/
public function getSubDomain()
{
return $this->subDomain;
}
/**
* Returns the public suffix.
*
* @return string|null
*/
public function getPublicSuffix()
{
return $this->publicSuffix->getContent();
}
/**
* Tells whether the public suffix has a matching rule in a Public Suffix List.
*
* @return bool
*/
public function isKnown(): bool
{
return $this->publicSuffix->isKnown();
}
/**
* Tells whether the public suffix has a matching rule in a Public Suffix List ICANN Section.
*
* @return bool
*/
public function isICANN(): bool
{
return $this->publicSuffix->isICANN();
}
/**
* Tells whether the public suffix has a matching rule in a Public Suffix List Private Section.
*
* @return bool
*/
public function isPrivate(): bool
{
return $this->publicSuffix->isPrivate();
}
/**
* {@inheritdoc}
*/
public function toAscii()
{
static $pattern = '/[^\x20-\x7f]/';
if (null === $this->domain || !preg_match($pattern, $this->domain)) {
return $this;
}
$clone = clone $this;
$clone->domain = $this->idnToAscii($this->domain);
$clone->labels = array_reverse(explode('.', $clone->domain));
$clone->publicSuffix = $this->publicSuffix->toAscii();
$clone->registrableDomain = $clone->setRegistrableDomain();
$clone->subDomain = $clone->setSubDomain();
return $clone;
}
/**
* {@inheritdoc}
*/
public function toUnicode()
{
if (null === $this->domain || false === strpos($this->domain, 'xn--')) {
return $this;
}
$clone = clone $this;
$clone->domain = $this->idnToUnicode($this->domain);
$clone->labels = array_reverse(explode('.', $clone->domain));
$clone->publicSuffix = $this->publicSuffix->toUnicode();
$clone->registrableDomain = $clone->setRegistrableDomain();
$clone->subDomain = $clone->setSubDomain();
return $clone;
}
/**
* Returns a new domain name with a different public suffix.
*
* @param PublicSuffix $publicSuffix
*
* @return self
*/
public function withPublicSuffix(PublicSuffix $publicSuffix): self
{
if ($this->publicSuffix == $publicSuffix) {
return $this;
}
$clone = clone $this;
$clone->publicSuffix = $clone->setPublicSuffix($publicSuffix);
$clone->registrableDomain = $clone->setRegistrableDomain();
$clone->subDomain = $clone->setSubDomain();
return $clone;
}
}