-
Notifications
You must be signed in to change notification settings - Fork 35
/
Parser.php
469 lines (402 loc) · 13.6 KB
/
Parser.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<?php
/**
* Novutec Domain Tools
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @category Novutec
* @package DomainParser
* @copyright Copyright (c) 2007 - 2013 Novutec Inc. (http://www.novutec.com)
* @license http://www.apache.org/licenses/LICENSE-2.0
*/
/**
* @namespace Novutec\DomainParser
*/
namespace Novutec\DomainParser;
/**
* define DomainParser Path
*/
define('DOMAINPARSERPATH', dirname(__FILE__));
/**
* @see IdnaConverter
*/
require_once DOMAINPARSERPATH . '/Idna.php';
/**
* @see DomainParserResult
*/
require_once DOMAINPARSERPATH . '/Result.php';
/**
* @see DomainParserException
*/
require_once DOMAINPARSERPATH . '/Exception/AbstractException.php';
/**
* DomainParser
*
* @category Novutec
* @package DomainParser
* @copyright Copyright (c) 2007 - 2013 Novutec Inc. (http://www.novutec.com)
* @license http://www.apache.org/licenses/LICENSE-2.0
*/
class Parser
{
/**
* Is the top-level domain list already be loaded?
*
* @var boolean
* @access protected;
*/
protected $loaded = false;
/**
* Should the exceptions be thrown or caugth and trapped in the response?
*
* @var boolean
* @access protected
*/
protected $throwExceptions = false;
/**
* Should the cache file always be loaded from the server?
*
* @var boolean
* @access protected
*/
protected $reload = false;
/**
* Life time of cached file
*
* @var integer
* @access protected
*/
protected $cacheTime = 432000;
/**
* List of all top-level domain names
*
* @var array
* @access protected
*/
protected $tldList = array();
/**
* URL to top-level domain name list
*
* @var string
* @access protected
*/
protected $tldUrl = 'http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1';
/**
* Output format 'object', 'array', 'json', 'serialize' or 'xml'
*
* @var string
* @access protected
*/
protected $format = 'object';
/**
* Encoding of domain name
*
* @var string
* @access protected
*/
protected $encoding = 'utf-8';
/**
* Set cache path
*
* @var string
* @access protected
*/
protected $path;
/**
* Creates a DomainParser object
*
* @param string $format
* @return void
*/
public function __construct($format = 'object', $path = null)
{
$this->setFormat($format);
$this->setCachePath($path);
}
/**
* Set cache path
*
* @param string $path
* @return void
*/
public function setCachePath($path = null)
{
if (is_null($path)) {
$this->path = sys_get_temp_dir();
} else {
$this->path = filter_var($path, FILTER_SANITIZE_STRING);
}
}
/**
* Checks if given domain name is valid
*
* @param string $domain
* @return boolean
*/
public function isValid($domain)
{
$this->setFormat('object');
$Result = $this->parse($domain, '');
return $Result->validHostname;
}
/**
* Tries to parse a string and to get the domain name, tld and idn
* converted domain name.
*
* If given string is not a domain name, it will add a default tld.
*
* Also skips given string if it is longer than 63 characters.
*
* @throws instance of AbstractException if throwExceptions = true
* @param string $unparsedString
* @param string $defaultTld
* @return void
*/
public function parse($unparsedString, $defaultTld = 'com')
{
try {
if ($this->loaded === false) {
$this->load();
}
$matchedDomain = '';
$matchedDomainIdn = '';
$matchedTld = '';
$matchedTldIdn = '';
$matchedGroup = '';
$validHostname = true;
$IdnaConverter = new Idna(array('idn_version' => 2008));
preg_match('/^((http|https|ftp|ftps|news|ssh|sftp|gopher):[\/]{2,})?([^\/]+)/', mb_strtolower(trim($unparsedString), $this->encoding), $matches);
$parsedString = $IdnaConverter->encode(end($matches));
foreach ($this->tldList['content'] as $tldgroup => $tlds) {
foreach ($tlds as $tld) {
if (preg_match('/\.' . $tld . '$/', $parsedString, $trash)) {
$matchedTld = $tld;
$matchedTldIdn = $IdnaConverter->encode($tld);
$matchedDomain = str_replace('.' . $matchedTld, '', $parsedString);
$matchedDomain = rtrim($matchedDomain, '.');
$matchedDomain = ltrim($matchedDomain, '.');
if ($matchedTld != 'name' && strpos($matchedDomain, '.')) {
$matchedDomain = str_replace('.', '', strrchr($matchedDomain, '.'));
}
if (strpos($matchedDomain, ' ')) {
$matchedDomain = explode(' ', $matchedDomain);
$matchedDomain = end($matchedDomain);
}
$matchedDomainIdn = $IdnaConverter->encode($matchedDomain);
$matchedGroup = $tldgroup;
break;
}
if ($tld == $parsedString) {
$matchedTld = $tld;
$matchedTldIdn = $IdnaConverter->encode($tld);
break;
}
}
}
if ($matchedDomain == '' && strlen($matchedDomainIdn) <= 63 && $matchedTld == '') {
$matchedDomain = $IdnaConverter->decode(preg_replace_callback('/[^a-zA-Z0-9\-\.]/', function (
$match) use(&$validHostname)
{
$validHostname = false;
}, $IdnaConverter->encode($parsedString)));
$matchedDomainIdn = $IdnaConverter->encode($matchedDomain);
$matchedTld = $matchedTldIdn = $defaultTld;
} elseif ($matchedDomain != '' && strlen($matchedDomainIdn) <= 63 && $matchedTld != '') {
$matchedDomain = $IdnaConverter->decode(preg_replace_callback('/[^a-zA-Z0-9\-\.]/', function (
$match) use(&$validHostname)
{
$validHostname = false;
}, $IdnaConverter->encode($matchedDomain)));
$matchedDomainIdn = $IdnaConverter->encode($matchedDomain);
} elseif ($matchedDomain == '' && $matchedTld != '') {
$validHostname = false;
} else {
throw \Novutec\DomainParser\AbstractException::factory('UnparsableString', 'Unparsable domain name.');
}
$Result = new Result($matchedDomain, $matchedDomainIdn,
$IdnaConverter->decode($matchedTld), $matchedTldIdn, $matchedGroup,
$validHostname);
} catch (\Novutec\DomainParser\AbstractException $e) {
if ($this->throwExceptions) {
throw $e;
}
$Result = new Result();
$Result->error = $e->getMessage();
}
return $Result->get($this->format);
}
/**
* Checks if the domain list exists or cached time is reached
*
* @throws OpenFileErrorException
* @throws WriteFileErrorException
* @return void
*/
private function load()
{
$filename = $this->path . '/domainparsertld.txt';
if (file_exists($filename)) {
$this->tldList = unserialize(file_get_contents($filename));
// will reload tld list if timestamp of cache file is outdated
if (time() - $this->tldList['timestamp'] > $this->cacheTime) {
$this->reload = true;
}
// will reload tld list if changes to Additional.php have been made
if ($this->tldList['timestamp'] < filemtime(DOMAINPARSERPATH . '/Additional.php')) {
$this->reload = true;
}
}
// check connection - if there is no internet connection skip loading
$existFile = file_exists($filename);
if (! $existFile || $this->reload === true) {
$this->catchTlds($existFile);
$file = fopen($filename, 'w+');
if ($file === false) {
throw \Novutec\DomainParser\AbstractException::factory('OpenFile', 'Could not open cache file.');
}
if (fwrite($file, serialize($this->tldList)) === false) {
throw \Novutec\DomainParser\AbstractException::factory('WriteFile', 'Could not open cache file for writing.');
}
fclose($file);
}
$this->loaded = true;
}
/**
* Catch list from server and parse them to array.
*
* It only uses the official ICANN domain names and adds private
* domains and missing official third-levels by using an additional hash.
*
* The manual added list is not complete.
*
* @throws ConnectErrorException
* @see Novutec\Additional.php $additional
* @param boolean $existFile
* @return void
*/
private function catchTlds($existFile)
{
$content = @file_get_contents($this->tldUrl);
if ($content === false) {
if (! $existFile) {
throw \Novutec\DomainParser\AbstractException::factory('Connect', 'Could not catch file from server.');
}
return;
}
$IdnaConverter = new Idna(array('idn_version' => 2008));
// only match official ICANN domain tlds
if (preg_match('/\/\/ ===BEGIN ICANN DOMAINS===(.*)(?=\/\/ ===END ICANN DOMAINS===)/s', $content, $matches) !== 1) {
throw \Novutec\DomainParser\AbstractException::factory('UnparsableString', 'Could not fetch ICANN Domains of Mozilla TLD File.');
}
$tlds = array();
$list_str = $matches[1];
foreach (explode("\n", $list_str) as $line) {
$line = trim($line);
// skip empty or comment lines
if ($line == '' || $line[0] == '/' || strpos($line, '!') !== false) {
continue;
}
// reformat prefixed wildcards
if ($line[0] == '*') {
$line = substr($line, 2);
}
// convert to xn-- format
$tld = $IdnaConverter->encode($line);
// validate if toplevel domain
$pos = strrpos($tld, '.');
if ($pos === false) {
$match = $tld;
} else {
$match = substr($tld, $pos+1);
}
if (!isset($tlds[$match])) {
$tlds[$match] = array();
}
$tlds[$match][] = $tld;
}
// load additional to add to list
require_once 'Additional.php';
// merge list and sort tlds by length within its group
$this->tldList['content'] = array_merge_recursive($tlds, $additional);
foreach ($this->tldList['content'] as $tldGroup => $tld) {
usort($tld, function ($a, $b)
{
return strlen($b) - strlen($a);
});
$this->tldList['content'][$tldGroup] = $tld;
}
$this->tldList['timestamp'] = time();
}
/**
* Set output format
*
* You may choose between 'object', 'array', 'json', 'serialize' or 'xml' output format
*
* @param string $format
* @return void
*/
public function setFormat($format = 'object')
{
$this->format = filter_var($format, FILTER_SANITIZE_STRING);
}
/**
* Set encoding of domain name
*
* @param string $encoding
* @return void
*/
public function setEncodng($encoding = 'utf-8')
{
$this->encoding = filter_var($encoding, FILTER_SANITIZE_STRING);
}
/**
* Set the throwExceptions flag
*
* Set whether exceptions encounted during processing should be thrown
* or caught and trapped in the response as a string message.
*
* Default behaviour is to trap them in the response; call this
* method to have them thrown.
*
* @param boolean $throwExceptions
* @return void
*/
public function throwExceptions($throwExceptions = false)
{
$this->throwExceptions = filter_var($throwExceptions, FILTER_VALIDATE_BOOLEAN);
}
/**
* Set the reload flag
*
* Set if the top-level domain list should be reloaded independet from
* the cache time.
*
* @param boolean $reload
* @return void
*/
public function reload($reload = false)
{
$this->reload = filter_var($reload, FILTER_VALIDATE_BOOLEAN);
}
/**
* Set the cache time
*
* By default the cache time is 432000 (equal to 5 days)
*
* @param integer $cacheTime
* @return void
*/
public function cacheTime($cacheTime = 432000)
{
$this->cacheTime = filter_var($cacheTime, FILTER_VALIDATE_INT);
}
}