1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* League.Uri (http://uri.thephpleague.com) |
4
|
|
|
* |
5
|
|
|
* @package League.uri |
6
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
7
|
|
|
* @copyright 2013-2015 Ignace Nyamagana Butera |
8
|
|
|
* @license https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License) |
9
|
|
|
* @version 4.2.0 |
10
|
|
|
* @link https://github.com/thephpleague/uri/ |
11
|
|
|
*/ |
12
|
|
|
namespace League\Uri\Components; |
13
|
|
|
|
14
|
|
|
use Pdp\Parser; |
15
|
|
|
use Pdp\PublicSuffixListManager; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Value object representing a URI host component. |
19
|
|
|
* |
20
|
|
|
* @package League.uri |
21
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
22
|
|
|
* @since 4.0.0 |
23
|
|
|
*/ |
24
|
|
|
trait HostnameInfoTrait |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Pdp Parser |
28
|
|
|
* |
29
|
|
|
* @var Parser |
30
|
|
|
*/ |
31
|
|
|
protected static $pdpParser; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Hostname public info |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $hostnameInfo = [ |
39
|
|
|
'isPublicSuffixValid' => false, |
40
|
|
|
'publicSuffix' => null, |
41
|
|
|
'registerableDomain' => null, |
42
|
|
|
'subdomain' => null, |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* is the Hostname Info loaded |
47
|
|
|
* |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
protected $hostnameInfoLoaded = false; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return the host public suffix |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
21 |
|
public function getPublicSuffix() |
58
|
|
|
{ |
59
|
21 |
|
return $this->getHostnameInfo('publicSuffix'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return the host registrable domain |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
24 |
|
public function getRegisterableDomain() |
68
|
|
|
{ |
69
|
24 |
|
return $this->getHostnameInfo('registerableDomain'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return the hostname subdomain |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
21 |
|
public function getSubdomain() |
78
|
|
|
{ |
79
|
21 |
|
return $this->getHostnameInfo('subdomain'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Tell whether the current public suffix is valid |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
21 |
|
public function isPublicSuffixValid() |
88
|
|
|
{ |
89
|
21 |
|
return $this->getHostnameInfo('isPublicSuffixValid'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Load the hostname info |
94
|
|
|
* |
95
|
|
|
* @param string $key hostname info key |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
24 |
|
protected function getHostnameInfo($key) |
100
|
|
|
{ |
101
|
24 |
|
$this->loadHostnameInfo(); |
102
|
24 |
|
return $this->hostnameInfo[$key]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* parse and save the Hostname information from the Parser |
107
|
|
|
*/ |
108
|
24 |
|
protected function loadHostnameInfo() |
109
|
|
|
{ |
110
|
24 |
|
if ($this->isIp() || $this->hostnameInfoLoaded) { |
111
|
21 |
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
18 |
|
$host = $this->__toString(); |
115
|
18 |
|
if ($this->isAbsolute()) { |
116
|
6 |
|
$host = mb_substr($host, 0, -1, 'UTF-8'); |
117
|
4 |
|
} |
118
|
|
|
|
119
|
18 |
|
$this->hostnameInfo = array_merge($this->hostnameInfo, $this->getPdpParser()->parseHost($host)->toArray()); |
120
|
|
|
|
121
|
18 |
|
if (null !== $this->hostnameInfo['publicSuffix']) { |
122
|
15 |
|
$this->hostnameInfo['isPublicSuffixValid'] = $this->getPdpParser()->isSuffixValid($host); |
123
|
10 |
|
} |
124
|
|
|
|
125
|
18 |
|
$this->hostnameInfoLoaded = true; |
126
|
18 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns the instance string representation; If the |
130
|
|
|
* instance is not defined an empty string is returned |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
abstract public function __toString(); |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns whether or not the host is an IP address |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
abstract public function isIp(); |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns whether or not the host is a full qualified domain name |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
abstract public function isAbsolute(); |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Initialize and access the Parser object |
152
|
|
|
* |
153
|
|
|
* @return Parser |
154
|
|
|
*/ |
155
|
18 |
|
protected function getPdpParser() |
156
|
|
|
{ |
157
|
18 |
|
if (!static::$pdpParser instanceof Parser) { |
158
|
3 |
|
static::$pdpParser = new Parser((new PublicSuffixListManager())->getList()); |
159
|
2 |
|
} |
160
|
|
|
|
161
|
18 |
|
return static::$pdpParser; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|