-
Notifications
You must be signed in to change notification settings - Fork 2
/
tldextract.php
executable file
·341 lines (301 loc) · 9.78 KB
/
tldextract.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
<?php
/**
* TLDExtract accurately extracts subdomain, domain and TLD components from URLs.
*
* Usage:
*
* $extract = new TLDExtract();
* $components = $extract('http://forums.news.cnn.com/'); //PHP 5.3 style. In PHP 5.2, use $extract->extract().
*
* echo $components->subdomain; // forum.news
* echo $components->domain; // cnn
* echo $components->tld; // com
*
* // Array access syntax also works.
* $components = $extract('http://forums.bbc.co.uk/');
* echo $components['tld']; // co.uk
*
* @see TLDExtractResult for more information on the returned data structure.
* @see tldextract() Handy shortcut function.
*/
class TLDExtract {
const SCHEME_RE = '#^([a-zA-Z][a-zA-Z0-9+\-.]*:)?//#';
private $fetch;
private $cacheFile;
private $extractor = null;
/**
* Construct an extractor instance.
*
* If $fetch is TRUE (the default) and no cached TLD set is found, the extractor will
* fetch the Public Suffix List live over HTTP on first use. Set to FALSE to disable
* this behaviour. Either way, if the TLD set can't be read, the extractor will fall
* back to the included snapshot.
*
* Specifying $cacheFile will override the location of the cached TLD set.
* Defaults to /path/to/tldextractphp/.tld_set.
*
* @param bool $fetch
* @param string $cacheFile
*/
public function __construct($fetch = true, $cacheFile = '') {
$this->fetch = $fetch;
$this->cacheFile = !empty($cacheFile) ? $cacheFile : dirname(__FILE__) . DIRECTORY_SEPARATOR . '.tld_set';
}
/**
* Make it possible to call the extractor instance directly.
*
* $extract = new TLDExtract();
* echo $extract('http://www.google.co.uk/');
* //Output: TLDExtractResult(subdomain='www', domain='google', tld='co.uk')
*
* @param string $url
* @return TLDExtractResult
*/
public function __invoke($url) {
return $this->extract($url);
}
/**
* Extract the subdomain, domain, and gTLD/ccTLD components from a URL.
*
* @param string $url
* @return TLDExtractResult
*/
public function extract($url) {
$host = $this->getHost($url);
$extractor = $this->getTldExtractor();
list($registeredDomain, $tld) = $extractor->extract($host);
//Check for IPv4 and IPv6 addresses.
if ( empty($tld) && $this->isIp($host) ) {
return new TLDExtractResult('', $host, '');
}
$lastDot = strrpos($registeredDomain, '.');
if ( $lastDot !== false ) {
$subdomain = substr($registeredDomain, 0, $lastDot);
$domain = substr($registeredDomain, $lastDot + 1);
} else {
$subdomain = '';
$domain = $registeredDomain;
}
return new TLDExtractResult($subdomain, $domain, $tld);
}
/**
* Extract the hostname from a URL.
*
* @param string $url
* @return string
*/
private function getHost($url) {
//Remove scheme and path.
$host = preg_replace(self::SCHEME_RE, '', $url);
list($host, ) = explode('/', $host, 2);
//Remove username and password.
$pieces = explode('@', $host, 2);
if ( count($pieces) == 2 ) {
$host = $pieces[1];
}
//Check for IPv6 literals like "[3ffe:2a00:100:7031::1]"
//See http://www.ietf.org/rfc/rfc2732.txt
$closingBracket = strrpos($host, ']');
if ( $this->startsWith($host, '[') && $closingBracket ) {
$host = substr($host, 0, $closingBracket + 1);
} else {
//This is either a normal hostname or an IPv4 address. Just remove the port.
list($host, ) = explode(':', $host);
}
return $host;
}
/**
* @return PublicSuffixListTLDExtractor
*/
private function getTldExtractor() {
if ( $this->extractor !== null ) {
return $this->extractor;
}
//Load the public suffix list from the cache, if possible.
$serializedTlds = @file_get_contents($this->cacheFile);
if ( !empty($serializedTlds) ) {
$this->extractor = new PublicSuffixListTLDExtractor(unserialize($serializedTlds));
return $this->extractor;
}
//Or attempt to download it.
$tlds = array();
if ( $this->fetch ) {
$tlds = $this->fetchTldList();
}
if ( empty($tlds) ) {
//If all else fails, try the local snapshot.
$snapshotFile = dirname(__FILE__) . DIRECTORY_SEPARATOR . '.tld_set_snapshot';
$serializedTlds = @file_get_contents($snapshotFile);
if ( !empty($serializedTlds) ) {
$this->extractor = new PublicSuffixListTLDExtractor(unserialize($serializedTlds));
return $this->extractor;
}
} else {
//Update the cache.
@file_put_contents($this->cacheFile, serialize($tlds));
}
$this->extractor = new PublicSuffixListTLDExtractor($tlds);
return $this->extractor;
}
private function fetchTldList() {
$page = $this->fetchPage('http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1');
$tlds = array();
if ( !empty($page) && preg_match_all('@^(?P<tld>[.*!]*\w[\S]*)@um', $page, $matches) ) {
$tlds = array_fill_keys($matches['tld'], true);
}
return $tlds;
}
private function fetchPage($url) {
if( ini_get('allow_url_fopen') ) {
return @file_get_contents($url);
} else if ( is_callable('curl_exec') ) {
$handle = curl_init($url);
curl_setopt_array($handle, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FAILONERROR => true,
));
$content = curl_exec($handle);
curl_close($handle);
return $content;
}
return '';
}
/**
* Check if the input is a valid IP address.
* Recognizes both IPv4 and IPv6 addresses.
*
* @param string $host
* @return bool
*/
private function isIp($host) {
//Strip the wrapping square brackets from IPv6 addresses
if ( $this->startsWith($host, '[') && $this->endsWith($host, ']') ) {
$host = substr($host, 1, -1);
}
return (bool)filter_var($host, FILTER_VALIDATE_IP);
}
private function startsWith($haystack, $needle) {
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
private function endsWith($haystack, $needle) {
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
}
/**
* This class holds the components of a domain name.
*
* @property string $subdomain The subdomain. For example, the subdomain of "a.b.google.com" is "a.b".
* @property string $domain The registered domain. For example, in "a.b.google.com" the registered domain is "google".
* @property string $tld The top-level domain / public suffix. For example: "com", "co.uk", "act.edu.au".
*
* You can access the components using either property syntax or array syntax. For example,
* "echo $result->tld" and "echo $result['tld']" will both work and output the same string.
*
* All properties are read-only.
*/
class TLDExtractResult implements ArrayAccess {
private $fields;
public function __construct($subdomain, $domain, $tld) {
$this->fields = array(
'subdomain' => $subdomain,
'domain' => $domain,
'tld' => $tld,
);
}
public function __get($name) {
if ( array_key_exists($name, $this->fields) ) {
return $this->fields[$name];
}
throw new OutOfRangeException(sprintf('Unknown field "%s"', $name));
}
public function __isset($name) {
return array_key_exists($name, $this->fields);
}
public function __set($name, $value) {
throw new LogicException('Can\'t modify an immutable object.');
}
public function __toString() {
return sprintf('%s(subdomain=\'%s\', domain=\'%s\', tld=\'%s\')', __CLASS__, $this->subdomain, $this->domain, $this->tld);
}
public function offsetExists($offset) {
return array_key_exists($offset, $this->fields);
}
public function offsetGet($offset) {
return $this->__get($offset);
}
public function offsetSet($offset, $value) {
throw new LogicException(sprintf('Can\'t modify an immutable object. You tried to set "%s".', $offset));
}
public function offsetUnset($offset) {
throw new LogicException(sprintf('Can\'t modify an immutable object. You tried to unset "%s".', $offset));
}
/**
* Get the domain name components as a native PHP array.
* The returned array will contain these keys: 'subdomain', 'domain' and 'tld'.
*
* @return array
*/
public function toArray() {
return $this->fields;
}
}
/**
* This class splits domain names into the registered domain and public suffix components
* using the TLD rule set from the Public Suffix List project.
*/
class PublicSuffixListTLDExtractor {
private $tlds;
/**
* @param array $tlds The TLD set from PSL.
*/
public function __construct($tlds) {
$this->tlds = $tlds;
}
/**
* @param string $host
* @return array An array with two items - the reg. domain (possibly with subdomains) and the public suffix.
*/
public function extract($host) {
$parts = explode('.', $host);
for ($i = 0; $i < count($parts); $i++) {
$maybeTld = join('.', array_slice($parts, $i));
$exceptionTld = '!' . $maybeTld;
if ( array_key_exists($exceptionTld, $this->tlds) ) {
return array(
join('.', array_slice($parts, 0, $i + 1)),
join('.', array_slice($parts, $i + 1)),
);
}
$wildcardTld = '*.' . join('.', array_slice($parts, $i + 1));
if ( array_key_exists($wildcardTld, $this->tlds) || array_key_exists($maybeTld, $this->tlds) ) {
return array(
join('.', array_slice($parts, 0, $i)),
$maybeTld
);
}
}
return array($host, '');
}
}
/**
* Extract the subdomain, domain and TLD components from a URL.
* A convenient alias for TLDExtract::extract().
*
* @uses TLDExtract::extract()
*
* @param string $url
* @return TLDExtractResult
*/
function tldextract($url) {
static $tldExtractor = null;
if ( $tldExtractor === null ) {
$tldExtractor = new TLDExtract();
}
return $tldExtractor->extract($url);
}