forked from koroban/WhoisParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.php
568 lines (500 loc) · 16.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
<?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 Config/Config
*/
require_once 'Config/Config.php';
/**
* @see Adapter/Adapter
*/
require_once 'Adapter/AbstractAdapter.php';
/**
* @see Templates/Template
*/
require_once 'Templates/AbstractTemplate.php';
/**
* @see Result/Result
*/
require_once 'Result/Result.php';
/**
* @see Exception
*/
require_once 'Exception/AbstractException.php';
/**
* 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 object
* @access protected
*/
protected $Result;
/**
* Should the exceptions be thrown or caugth and trapped in the response?
*
* @var boolean
* @access protected
*/
protected $throwExceptions = false;
/**
* Use cache for whois output?
*
* @var boolean
* @access protected
*/
protected $useCache = 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:%M:%S';
/**
* Activate cache
*
* @var boolean
* @access protected
*/
protected $cache = true;
/**
* Creates a WhoisParser object
*
* @param string $format
* @return void
*/
public function __construct($format = 'object')
{
date_default_timezone_set('UTC');
$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);
try {
if ($query == '') {
throw AbstractException::factory('NoQuery', '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 ($this->throwExceptions) {
throw $e;
}
$this->Result->addItem('exception', $e->getMessage());
$this->Result->addItem('rawdata', explode("\n", $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();
$this->Query = $Parser->parse(filter_var($query, FILTER_SANITIZE_STRING));
}
}
/**
* Send data to whois server and call parse() to process rawdata
*
* @throws NoAdapterException
* @param object $query
* @return void
*/
public function call($query = '')
{
if ($query != '') {
$this->Query = filter_var($query, FILTER_SANITIZE_STRING);
}
$Config = $this->Config->getCurrent();
$Adapter = AbstractAdapter::factory($Config['adapter']);
if ($Adapter instanceof AbstractAdapter) {
$this->rawdata = strip_tags($Adapter->call($this->Query, $Config));
$this->parse();
} else {
throw AbstractException::factory('NoAdapter', 'Adapter ' . $Config['adapter'] .
' could not be found.');
}
}
/**
* Parses rawdata from whois server and call postProcess if exists afterwards
*
* @throws NoTemplateException
* @return void
*/
private function parse()
{
$Config = $this->Config->getCurrent();
$Template = AbstractTemplate::factory($Config['template']);
// 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->parseTemplate($Template);
// set rawdata to Result - this happens here because sometimes we
// have to fix the rawdata as well in postProcess
$this->Result->addItem('rawdata', explode("\n", $this->rawdata));
// check availability upon type - IP addresses are always registered
if (isset($Template->available) && $Template->available != '') {
preg_match_all($Template->available, $this->rawdata, $matches);
$this->Result->addItem('registered', empty($matches[0]));
}
// 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 {
throw AbstractException::factory('NoTemplate', '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)));
}
/**
* Parses rawdata by Template
*
* @param object $Template
* @param string $rawdata
* @return void
*/
private function parseTemplate($Template)
{
// check if there is a block to be cutted from HTML response
if (isset($Template->htmlBlock)) {
preg_match($Template->htmlBlock, $this->rawdata, $htmlMatches);
if (isset($htmlMatches[0])) {
$this->rawdata = preg_replace('/\s\s+/', "\n", $htmlMatches[0]);
}
}
// lookup all blocks of template
foreach ($Template->blocks as $blockKey => $blockRegEx) {
// try to match block regex against WHOIS rawdata
if (preg_match_all($blockRegEx, $this->rawdata, $blockMatches)) {
// use matched block to lookup for blockItems
foreach ($blockMatches[0] as $item) {
foreach ($Template->blockItems[$blockKey] as $itemRegEx => $target) {
// try to match blockItem regex against block
if (preg_match_all($itemRegEx, $item, $itemMatches)) {
// set matched items to Result
$this->Result->addItem($target, end($itemMatches));
}
}
}
}
}
// if there are still contact handles after parsing then
// these contacts are used for more types e.g. one handle for admin and
// tech so we are going to clone this matching handles
if (isset($this->Result->network->contacts)) {
// lookup all left over handles in network
foreach ($this->Result->network->contacts as $type => $handle) {
if (is_string($handle)) {
// lookup all contacts in Result
foreach ($this->Result->contacts as $contactType => $contactArray) {
foreach ($contactArray as $contactObject) {
// if contact handle in network matches the one in
// Result, we have to clone it
if (strtolower($contactObject->handle) === strtolower($handle)) {
if (empty($this->Result->contacts->$type)) {
$this->Result->contacts->$type = Array();
}
array_push($this->Result->contacts->$type, $contactObject);
unset($this->Result->network->contacts->$type);
break 2;
}
}
}
}
}
}
}
/**
* 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_SANITIZE_STRING);
}
/**
* 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:%M:%S')
{
$this->dateformat = $dateformat;
}
/**
* Set the cache flag
*
* If cache flag is set, the WHOIS parser will cache the query and rawdata.
*
* @param boolean $cache
* @return void
*/
public function useCache($cache = true)
{
$this->cache = filter_var($cache, FILTER_VALIDATE_BOOLEAN);
}
/**
* 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);
}
}