forked from 3name/WhoisParser
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Parser.php
669 lines (578 loc) · 18.4 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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
<?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 WhoisParser
* @copyright Copyright (c) 2007 - 2013 Novutec Inc. (http://www.novutec.com)
* @license http://www.apache.org/licenses/LICENSE-2.0
*/
/**
* @namespace Novutec\WhoisParser
*/
namespace Novutec\WhoisParser;
/**
* @see Result/Result
*/
require_once 'Result/Result.php';
use Novutec\WhoisParser\Adapter\AbstractAdapter;
use Novutec\WhoisParser\Config\Config;
use Novutec\WhoisParser\Exception\AbstractException;
use Novutec\WhoisParser\Exception\NoAdapterException;
use Novutec\WhoisParser\Exception\NoQueryException;
use Novutec\WhoisParser\Exception\NoTemplateException;
use Novutec\WhoisParser\Exception\RateLimitException;
use Novutec\WhoisParser\Result\Result;
use Novutec\WhoisParser\Templates\Type\AbstractTemplate;
/**
* WhoisParser
*
* Automatically follows the WHOIS registry referral chains until it finds the
* correct WHOIS for the most complete WHOIS data. Exceptionally robust WHOIS parser
* that parses a variety of free form WHOIS data into well-structured data that your
* application may read. Also returns an indication of whether a domain is available.
*
* @category Novutec
* @package WhoisParser
* @copyright Copyright (c) 2007 - 2013 Novutec Inc. (http://www.novutec.com)
* @license http://www.apache.org/licenses/LICENSE-2.0
*/
class Parser
{
/**
* WhoisParserConfig object
*
* @var object
* @access protected
*/
protected $Config;
/**
* Query string sent to the WhoisParser
*
* @var object
* @access protected
*/
protected $Query;
/**
* Raw output from whois server unformatted
*
* @var string
* @access protected
*/
protected $rawdata;
/**
* WhoisParserResult object
*
* @var \Novutec\WhoisParser\Result\Result
* @access protected
*/
protected $Result;
/**
* Should the exceptions be thrown or caugth and trapped in the response?
*
* @var boolean
* @access protected
*/
protected $throwExceptions = false;
/**
* Contains special whois server like member whois configuration. It will be
* used to overload the respective template in WhoisParserConfig.
*
* @var array
* @access protected
*/
protected $specialWhois = array();
/**
* Output format 'object', 'array', 'json', 'serialize' or 'xml'
*
* @var string
* @access protected
*/
protected $format = 'object';
/**
* Output format for dates
*
* @var string
* @access protected
*/
protected $dateformat = 'Y-m-d H:i:s';
/**
* Activate cache
*
* @var string Cache location
* @access protected
*/
protected $cachePath = null;
/**
* Rate limited servers list.
* Allows us to prevent additional queries. Must be cleared manually.
*
* @var array List of servers which are currently rate limited
*/
protected $rateLimitedServers = array();
protected $customConfigFile = null;
protected $proxyConfigFile = null;
protected $customTemplateNamespace = null;
protected $customAdapterNamespace = null;
/**
* @var array Custom domain groups for DomainParser
*/
protected $customDomainGroups = array();
/**
* Creates a WhoisParser object
*
* @param string $format
*/
public function __construct($format = 'object')
{
$this->setFormat($format);
}
/**
* Set special whois server like member whois configuration. It will be
* used to overload the respective template in WhoisParserConfig.
*
* @param array $specialWhois
* @return void
*/
public function setSpecialWhois($specialWhois)
{
$this->specialWhois = $specialWhois;
}
/**
* Lookup an IP address (ipv4 and ipv6) and domain names
*
* @throws NoQueryException
* @throws instance of AbstractException if throwExceptions = true
* @param string $query
* @return object
*/
public function lookup($query = '')
{
$this->Result = new Result();
$this->Config = new Config($this->specialWhois, $this->customConfigFile);
try {
if ($query == '') {
throw new NoQueryException('No lookup query given');
}
$this->prepare($query);
if (isset($this->Query->ip)) {
$config = $this->Config->get('iana');
} else {
if (isset($this->Query->tldGroup)) {
$config = $this->Config->get($this->Query->tldGroup, $this->Query->idnTld);
} else {
$config = $this->Config->get($this->Query->asn);
}
if ($config['server'] == '' || $this->Query->domain == '') {
$config = $this->Config->get('iana');
}
}
$this->Config->setCurrent($config);
$this->call();
} catch (AbstractException $e) {
if (strpos($e->getMessage(), 'Unable to connect') !== false) {
// If unable to connect, use the prereferralconfig
// Some referall servers fail, timeout, etc.
return $this->Config->prereferralResult;
} else {
if ($this->throwExceptions) {
throw $e;
}
$this->Result->addItem('exception', $e->getMessage());
$this->Result->addItem('rawdata', $this->rawdata);
if (isset($this->Query)) {
if (isset($this->Query->ip)) {
$this->Result->addItem('name', $this->Query->ip);
} else {
$this->Result->addItem('name', $this->Query->fqdn);
}
} else {
$this->Result->addItem('name', $query);
}
}
}
// call cleanUp method
$this->Result->cleanUp($this->Config->getCurrent(), $this->dateformat);
// peparing output of Result by format
switch ($this->format) {
case 'json':
return $this->Result->toJson();
break;
case 'serialize':
return $this->Result->serialize();
break;
case 'array':
return $this->Result->toArray();
break;
case 'xml':
return $this->Result->toXml();
break;
default:
return $this->Result;
}
}
/**
* Parses the given query to get either the domain name or an IP address
*
* @param string $query
* @return void
*/
private function prepare($query)
{
// check if given query is an IP address and AS number or possible
// domain name
if ($this->bin2ip($this->ip2bin($query)) === $query) {
$this->Query = new \stdClass();
$this->Query->ip = $query;
} elseif (preg_match('/^AS[0-9]*$/im', $query)) {
$this->Query = new \stdClass();
$this->Query->asn = $query;
} else {
$Parser = new \Novutec\DomainParser\Parser();
$Parser->setCustomDomainGroups($this->customDomainGroups);
if ($this->cachePath !== null) {
$Parser->setCachePath($this->cachePath);
}
$this->Query = $Parser->parse(filter_var($query, FILTER_UNSAFE_RAW));
}
}
/**
* Send data to whois server and call parse() to process rawdata
*
* @throws NoAdapterException
* @throws RateLimitException
* @param string $query
* @return void
*/
public function call($query = '')
{
if ($query != '') {
$this->Query = filter_var($query, FILTER_UNSAFE_RAW);
}
$Config = $this->Config->getCurrent();
$Adapter = AbstractAdapter::factory($Config['adapter'], $this->proxyConfigFile, $this->customAdapterNamespace);
$server = $Config['server'];
if (in_array($server, $this->rateLimitedServers)) {
throw new RateLimitException("Rate limit exceeded for server: ". $server);
}
if ($Adapter instanceof AbstractAdapter) {
$this->rawdata = $Adapter->call($this->Query, $Config);
$this->parse();
} else {
throw new NoAdapterException('Adapter '. $Config['adapter'] .' could not be found');
}
}
/**
* Parses rawdata from whois server and call postProcess if exists afterwards
*
* @throws NoTemplateException
* @throws RateLimitException
* @return void
*/
private function parse()
{
$Config = $this->Config->getCurrent();
$Template = AbstractTemplate::factory($Config['template'], $this->customTemplateNamespace);
// If Template is null then we do not have a template for that, but we
// can still proceed to the end with just the rawdata
if ($Template instanceof AbstractTemplate) {
$this->Result->template[$Config['server']] = $Config['template'];
$this->rawdata = $Template->translateRawData($this->rawdata, $Config);
try {
$Template->parse($this->Result, $this->rawdata);
} catch (RateLimitException $e) {
$server = $Config['server'];
if (!in_array($server, $this->rateLimitedServers)) {
$this->rateLimitedServers[] = $server;
}
throw new RateLimitException("Rate limit exceeded for server: ". $server);
}
// set rawdata to Result - this happens here because sometimes we
// have to fix the rawdata as well in postProcess
$this->Result->addItem('rawdata', $this->rawdata);
// set registered to Result
$this->Result->addItem('registered', isset($this->Result->registered) ? $this->Result->registered : false);
if (! isset($this->Result->whoisserver)) {
$this->Result->addItem('whoisserver', $Config['server']);
}
// start post processing
$Template->postProcess($this);
// set name to Result
if (isset($this->Query->tld) && ! isset($this->Query->fqdn)) {
$this->Result->addItem('name', $this->Query->tld);
} elseif (isset($this->Query->ip)) {
$this->Result->addItem('name', $this->Query->ip);
} elseif (isset($this->Query->asn)) {
$this->Result->addItem('name', $this->Query->asn);
} else {
$this->Result->addItem('name', $this->Query->fqdn);
$this->Result->addItem('idnName', $this->Query->idnFqdn);
}
} else {
if (false !== strpos($this->rawdata,'rwhois')) {
$this->Result->reset();
$this->Config->setCurrent($this->Config->get('Ubersmith'));
$this->parse();
} else {
throw new NoTemplateException('Template '. $Config['template'] .' could not be found');
}
}
}
/**
* Converts IP address to binary
*
* @param string $ip
* @return mixed
*/
private function ip2bin($ip)
{
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
return base_convert(ip2long($ip), 10, 2);
}
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
return false;
}
if (($ip_n = inet_pton($ip)) === false) {
return false;
}
$bits = 15; // 16 x 8 bit = 128bit (ipv6)
$ipbin = '';
while ($bits >= 0) {
$bin = sprintf('%08b', (ord($ip_n[$bits])));
$ipbin = $bin . $ipbin;
$bits = $bits - 1;
}
return $ipbin;
}
/**
* Converts binary to IP address
*
* @param string $bin
* @return mixed
*/
private function bin2ip($bin)
{
if (strlen($bin) <= 32) {
// 32bits (ipv4)
return long2ip(base_convert($bin, 2, 10));
}
if (strlen($bin) !== 128) {
return false;
}
$pad = 128 - strlen($bin);
for ($i = 1; $i <= $pad; $i++) {
$bin = '0' . $bin;
}
$bits = 0;
$ipv6 = '';
while ($bits <= 7) {
$bin_part = substr($bin, ($bits * 16), 16);
$ipv6 = $ipv6 . dechex(bindec($bin_part)) . ':';
$bits = $bits + 1;
}
return inet_ntop(inet_pton(substr($ipv6, 0, - 1)));
}
/**
* Returns WhoisParserResult instance
*
* @return object
*/
public function getResult()
{
return $this->Result;
}
/**
* Returns WhoisParserConfig instance
*
* @return object
*/
public function getConfig()
{
return $this->Config;
}
/**
* Returns WhoisParser Query
*
* @return object
*/
public function getQuery()
{
return $this->Query;
}
/**
* 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_UNSAFE_RAW);
}
/**
* Set date format
*
* You may choose your own date format. Please check http://php.net/strftime for further
* details
*
* @param string $dateformat
* @return void
*/
public function setDateFormat($dateformat = 'Y-m-d H:i:s')
{
$this->dateformat = $dateformat;
}
/**
* Set the throwExceptions flag
*
* Set whether exceptions encounted in the dispatch loop should be thrown
* or caught and trapped in the response object.
*
* Default behaviour is to trap them in the response object; 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 path to use for on-disk cache. If NULL, cache is disabled.
*
* @param string|null $path Cache path
*/
public function setCachePath($path)
{
$this->cachePath = $path;
}
/**
* Return the list of rate limited servers
*
* @return array
*/
public function getRateLimitedServers()
{
return $this->rateLimitedServers;
}
/**
* Remove a specific server from the list of rate limited servers.
*
* @param string $server
* @return bool Server was present in list?
*/
public function removeRateLimitedServer($server)
{
$key = array_search($server, $this->rateLimitedServers);
if ($key !== false) {
unset($this->rateLimitedServers[$key]);
}
return ($key !== false);
}
/**
* Clear the list of rate limited servers
*
* @return int Number of entries removed from list
*/
public function clearRateLimitedServers()
{
$count = count($this->rateLimitedServers);
$this->rateLimitedServers = array();
return $count;
}
/**
* Set a custom config file.
* Settings in this file will override the default config.
* Set to NULL to clear.
*
* @param null|string $iniFile INI file
*/
public function setCustomConfigFile($iniFile)
{
$this->customConfigFile = $iniFile;
}
/**
* Set a proxy config file.
* Set to NULL to clear.
*
* @param null|string $iniFile
*/
public function setProxyConfigFile($iniFile)
{
$this->proxyConfigFile = $iniFile;
}
/**
* Set a custom template namespace
* Templates in this namespace will override the default templates.
* Set to NULL to clear.
*
* @param null|string $namespace
*/
public function setCustomTemplateNamespace($namespace)
{
$this->customTemplateNamespace = $namespace;
}
/**
* Set a custom adapter namespace.
* Adapters in this namespace will override the default adapters.
* Set to NULL to clear.
*
* @param null|string $namespace
*/
public function setCustomAdapterNamespace($namespace)
{
$this->customAdapterNamespace = $namespace;
}
/**
* Add a custom domain group for DomainParser. This will override the built-in domain groups.
*
* @param string $groupName
* @param array $tldList
*/
public function addCustomDomainGroup($groupName, array $tldList)
{
$this->customDomainGroups[$groupName] = $tldList;
}
/**
* Set the custom domain groups for DomainParser. The array should be in the same format as in Additional.php.
* These will override the built-in domain groups
*
* @param array $domainGroups Array of domain groups and their tld lists
*/
public function setCustomDomainGroups(array $domainGroups)
{
$this->customDomainGroups = $domainGroups;
}
/**
* Return the current configured proxy config file location
*
* @return null|string
*/
public function getProxyConfigFile()
{
return $this->proxyConfigFile;
}
/**
* Return the currently configured custom adapter namespace.
*
* @return null|string
*/
public function getCustomAdapterNamespace()
{
return $this->customAdapterNamespace;
}
}