Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Whois.php
4
 *
5
 * PHP Version 4 and 5
6
 *
7
 * Copyright (c) 1997-2003 The PHP Group
8
 * Portions Copyright (c) 1980, 1993 The Regents of the University of
9
 *   California.  All rights reserved.
10
 *
11
 * This source file is subject to version 3.01 of the PHP license,
12
 * that is bundled with this package in the file LICENSE, and is
13
 * available at through the world-wide-web at
14
 * http://www.php.net/license/3_01.txt.
15
 * If you did not receive a copy of the PHP license and are unable to
16
 * obtain it through the world-wide-web, please send a note to
17
 * license@php.net so we can mail you a copy immediately.
18
 *
19
 * @category  Net
20
 * @package   Net_Whois
21
 * @author    Seamus Venasse <seamus.venasse@polaris.ca>
22
 * @copyright 1997-2003 The PHP Group
23
 * @copyright 1980-1993 The Regents of the University of California (Portions)
24
 * @license   http://www.php.net/license/3_01.txt PHP 3.01
25
 * @version   CVS: $Id: Whois.php 314693 2011-08-10 00:31:45Z kguest $
26
 * @link      http://pear.php.net/package/Net_Whois
27
 */
28
 
29
require_once 'PEAR.php';
30
 
31
/**
32
 * Looks up records in the databases maintained by several Network Information
33
 * Centres (NICs).  This class uses PEAR's Net_Socket:: class.
34
 *
35
 * @category Net
36
 * @package  Net_Whois
37
 * @author   Seamus Venasse <seamus.venasse@polaris.ca>
38
 * @license  http://www.php.net/license/3_01.txt PHP 3.01
39
 * @link     http://pear.php.net/package/Net_Whois
40
 */
41
class Net_Whois extends PEAR
42
{
43
 
44
    // {{{ properties
45
 
46
    /**
47
     * Retrieve authoritative definition only
48
     *
49
     * @var boolean
50
     * @access public
51
     */
52
    var $authoritative = false;
53
 
54
    /**
55
     * Port for whois servers
56
     *
57
     * @var int
58
     * @access public
59
     */
60
    var $port = 43;
61
 
62
    /**
63
     * See options for stream_context_create.
64
     *
65
     * @param array
66
     * @access public
67
     */
68
    var $options = null;
69
 
70
    /**
71
     * List of NICs to query
72
     *
73
     * @var array
74
     * @access private
75
     */
76
    var $_nicServers = array (
77
        'NICHOST'           => 'whois.crsnic.net.',
78
        'INICHOST'          => 'whois.networksolutions.com.',
79
        'GNICHOST'          => 'whois.nic.gov.',
80
        'ANICHOST'          => 'whois.arin.net.',
81
        'RNICHOST'          => 'whois.ripe.net.',
82
        'PNICHOST'          => 'whois.apnic.net.',
83
        'RUNICHOST'         => 'whois.ripn.net.',
84
        'MNICHOST'          => 'whois.ra.net.',
85
        'QNICHOST_TAIL'     => '.whois-servers.net.',
86
        'SNICHOST'          => 'whois.6bone.net.',
87
        'BNICHOST'          => 'whois.registro.br.'
88
    );
89
 
90
    /**
91
     * Search string of server to search on
92
     *
93
     * @var string
94
     * @access private
95
     */
96
    var $_whoisServerID = 'Whois Server: ';
97
 
98
    /**
99
     * Server to search for IP address lookups
100
     *
101
     * @var array
102
     * @access private
103
     */
104
    var $_ipNicServers = array ('RNICHOST', 'PNICHOST', 'BNICHOST');
105
 
106
    /**
107
     * List of error codes and text
108
     *
109
     * @var array
110
     * @access private
111
     */
112
    var $_errorCodes = array (
113
        010 => 'Unable to create a socket object',
114
        011 => 'Unable to open socket',
115
        012 => 'Write to socket failed',
116
        013 => 'Read from socket failed',
117
        014 => 'Specified server is null or empty',
118
    );
119
 
120
    /**
121
     * Number of seconds to wait on socket connections before assuming
122
     * there's no more data from the Whois server. Defaults to no timeout.
123
     * @var integer $timeout
124
     * @access private
125
     */
126
    var $_timeout = false;
127
    // }}}
128
 
129
    // {{{ constructor
130
    /**
131
     * Constructs a new Net_Whois object
132
     *
133
     * @access public
134
     */
135
    function Net_Whois()
136
    {
137
        $this->PEAR();
138
 
139
        $this->setPort();
140
        $this->setAuthoritative();
141
        $this->setTimeout();
142
    }
143
    // }}}
144
 
145
    // {{{ setTimeout()
146
    /**
147
     * Set timeout value - number of seconds afterwhich an attempt to connect
148
     * to a whois server should be aborted.
149
     *
150
     * @param integer $timeout false is also an acceptable value
151
     *
152
     * @access public
153
     * @return void
154
     */
155
    function setTimeout($timeout = false)
156
    {
157
        $this->_timeout = $timeout;
158
    }
159
    // }}}
160
 
161
    // {{{ getTimeout()
162
    /**
163
     * Retrieve timeout value
164
     *
165
     * @access public
166
     *
167
     * @return mixed either false or an integer value
168
     */
169
    function getTimeout()
170
    {
171
        return $this->_timeout;
172
    }
173
    // }}}
174
 
175
    // {{{ setTimeout()
176
    /**
177
     * setAuthoritative
178
     *
179
     * @param bool $authoritative defaults to false
180
     *
181
     * @access public
182
     * @return void
183
     */
184
    function setAuthoritative($authoritative = false)
185
    {
186
        $this->authoritative = $authoritative;
187
    }
188
    // }}}
189
 
190
    // {{{ getAuthoritative()
191
    /**
192
     * getAuthoritative
193
     *
194
     * @return bool Query for authoritative result?
195
     */
196
    function getAuthoritative()
197
    {
198
        return (bool) $this->authoritative;
199
    }
200
    // }}}
201
 
202
 
203
    /**
204
     * set which port should be used
205
     *
206
     * @param integer $port Port to use
207
     *
208
     * @access public
209
     * @return void
210
     */
211
    function setPort($port = false)
212
    {
213
        $port = is_numeric($port) ? $port : getservbyname('whois', 'tcp');
214
        $this->port = $port ? $port : 43;
215
    }
216
    // }}}
217
 
218
    // {{{ getPort()
219
    /**
220
     * Retrieve which port to connect to.
221
     *
222
     * @return integer port to connect to
223
     */
224
    function getPort()
225
    {
226
        return $this->port;
227
    }
228
    // }}}
229
 
230
    /**
231
     * setOptions
232
     *
233
     * @param mixed $options
234
     * @return void
235
     */
236
    function setOptions($options)
237
    {
238
        if ((!is_null($options)) && (!is_array($options))) {
239
            return;
240
        }
241
        $this->options = $options;
242
    }
243
 
244
    // {{{ getOptions()
245
    /**
246
     * Retrieve which port to connect to.
247
     *
248
     * @return array
249
     */
250
    function getOptions()
251
    {
252
        return $this->options;
253
    }
254
    // }}}
255
 
256
    // {{{ query()
257
    /**
258
     * Connect to the necessary servers to perform a domain whois query.  Prefix
259
     * queries with a "!" to lookup information in InterNIC handle database.
260
     * Add a "-arin" suffix to queries to lookup information in ARIN handle
261
     * database.
262
     *
263
     * @param string $domain          IP address or host name
264
     * @param string $userWhoisServer server to query (optional)
265
     *
266
     * @access public
267
     * @return mixed returns a PEAR_Error on failure, or a string on success
268
     */
269
    function query($domain, $userWhoisServer = null)
270
    {
271
        $domain = trim($domain);
272
 
273
        if (isset($userWhoisServer)) {
274
            $whoisServer = $userWhoisServer;
275
        } elseif (preg_match('/^!.*/', $domain)) {
276
            $whoisServer = $this->_nicServers['INICHOST'];
277
        } elseif (preg_match('/.*?-arin/i', $domain)) {
278
            $whoisServer = $this->_nicServers['ANICHOST'];
279
        } else {
280
            $whoisServer = $this->_chooseServer($domain);
281
        }
282
 
283
        $_domain = $this->authoritative ? 'domain ' . $domain : $domain;
284
        $whoisData = $this->_connect($whoisServer, $_domain);
285
 
286
        if (PEAR::isError($whoisData)) {
287
            return $whoisData;
288
        }
289
 
290
        if ($this->authoritative) {
291
            $pattern = '/\s+' . preg_quote($this->_whoisServerID) . '(.+?)\n/i';
292
 
293
            if (preg_match($pattern, $whoisData, $matches)) {
294
                $whoisData = $this->_connect(trim(array_pop($matches)), $domain);
295
            }
296
        }
297
        return $whoisData;
298
    }
299
    // }}}
300
 
301
    // {{{ queryAPNIC()
302
    /**
303
     * Use the Asia/Pacific Network Information Center (APNIC) database.
304
     * It contains network numbers used in East Asia, Australia, New
305
     * Zealand, and the Pacific islands.
306
     *
307
     * @param string $domain IP address or host name
308
     *
309
     * @access public
310
     * @return mixed returns a PEAR_Error on failure, or a string on success
311
     */
312
    function queryAPNIC($domain)
313
    {
314
        return $this->query($domain, $this->_nicServers['PNICHOST']);
315
    }
316
    // }}}
317
 
318
    // {{{ queryIPv6()
319
    /**
320
     * Use the IPv6 Resource Center (6bone) database.  It contains network
321
     * names and addresses for the IPv6 network.
322
     *
323
     * @param string $domain IP address or host name
324
     *
325
     * @access public
326
     * @return mixed returns a PEAR_Error on failure, or a string on success
327
     */
328
    function queryIPv6($domain)
329
    {
330
        return $this->query($domain, $this->_nicServers['SNICHOST']);
331
    }
332
    // }}}
333
 
334
    // {{{ queryRADB()
335
    /**
336
     * Use the Route Arbiter Database (RADB) database.  It contains
337
     * route policy specifications for a large number of operators'
338
     * networks.
339
     *
340
     * @param string $ipAddress IP address
341
     *
342
     * @access public
343
     * @return mixed returns a PEAR_Error on failure, or a string on success
344
     */
345
    function queryRADB($ipAddress)
346
    {
347
        return $this->query($ipAddress, $this->_nicServers['MNICHOST']);
348
    }
349
    // }}}
350
 
351
    // {{{ _chooseServer()
352
    /**
353
     * Determines the correct server to connect to based upon the domain
354
     *
355
     * @param string $query IP address or host name
356
     *
357
     * @access private
358
     * @return string whois server host name
359
     */
360
    function _chooseServer($query)
361
    {
362
        if (!strpos($query, '.')) {
363
            return $this->_nicServers['NICHOST'];
364
        }
365
 
366
        $TLD = substr($query, strrpos($query, '.') + 1);
367
 
368
        if (is_numeric($TLD)) {
369
            $whoisServer = $this->_nicServers['ANICHOST'];
370
        } else {
371
            $whoisServer = $this->getDomainServer($query);
372
        }
373
 
374
        return $whoisServer;
375
    }
376
    // }}}
377
 
378
    // {{{ getDomainServer()
379
    /**
380
     * Determines the correct whois server to connect to based upon the domain
381
     *
382
     * @param string $q domain name
383
     *
384
     * @access public
385
     * @return string whois server ip address
386
     */
387
    function getDomainServer($q)
388
    {
389
        $tail = $this->_nicServers['QNICHOST_TAIL'];
390
        if (strchr($q, '.')) {
391
            //get the last 2 parts
392
            $q = array_reverse(explode('.', $q));
393
            $a = array($q[1] . '.' . $q[0], $q[0]);
394
        } else {
395
            $a = array($q);
396
        }
397
        foreach ($a as $q) {
398
            //check host has real ip
399
            $q = gethostbyname($q . $tail);
400
            if (filter_var($q, FILTER_VALIDATE_IP)) {
401
                return $q;
402
            }
403
        }
404
    }
405
    // }}}
406
 
407
    // {{{ _connect()
408
    /**
409
     * Connects to the whois server and retrieves domain information
410
     *
411
     * @param string $nicServer FQDN of whois server to query
412
     * @param string $domain    Domain name to query
413
     *
414
     * @access private
415
     * @return mixed returns a PEAR_Error on failure, string of whois data on success
416
     */
417
    function _connect($nicServer, $domain)
418
    {
419
        include_once 'Net/Socket.php';
420
 
421
        if (is_null($nicServer) || (empty($nicServer))) {
422
            return new PEAR_Error($this->_errorCodes[014], 14);
423
        }
424
 
425
        if (PEAR::isError($socket = new Net_Socket())) {
426
            return new PEAR_Error($this->_errorCodes[010], 10);
427
        }
428
 
429
        $result = $socket->connect(
430
            $nicServer,
431
            $this->getPort(),
432
            null,
433
            $this->getTimeout(),
434
            $this->getOptions()
435
        );
436
        if (PEAR::isError($result)) {
437
            return new PEAR_Error($this->_errorCodes[011], 11);
438
        }
439
        $socket->setBlocking(false);
440
        // Querying denic.de requires some special coaxing for a domain query.
441
        // http://www.denic.de/en/faq-single/2978/1115.html
442
        if (substr($domain, -3) == '.de') {
443
            if (PEAR::isError($socket->writeLine("-T dn,ace " . $domain))) {
444
                return new PEAR_Error($this->_errorCodes[012], 12);
445
            }
446
        } else {
447
 
448
            if (PEAR::isError($socket->writeLine($domain))) {
449
                return new PEAR_Error($this->_errorCodes[012], 12);
450
            }
451
        }
452
 
453
        $nHost = null;
454
 
455
        $whoisData = $socket->readAll();
456
        if (PEAR::isError($whoisData)) {
457
            return new PEAR_Error($this->_errorCodes[013], 13);
458
        }
459
 
460
        $data = explode("\n", $whoisData);
461
        foreach ($data as $line) {
462
            $line = rtrim($line);
463
 
464
            // check for whois server redirection
465
            if (!isset($nHost)) {
466
                $pattern='/'.$this->_whoisServerID.'([a-z0-9.]+)\n/i';
467
                if (preg_match($pattern, $line, $matches)) {
468
                    $nHost = $matches[1];
469
                } elseif ($nicServer == $this->_nicServers['ANICHOST']) {
470
                    foreach ($this->_ipNicServers as $ipNicServer) {
471
                        $server = trim($this->_nicServers[$ipNicServer], '.');
472
                        if (strstr($line, $server)) {
473
                            $nHost = $this->_nicServers[$ipNicServer];
474
                        }
475
                    }
476
                }
477
            }
478
        }
479
 
480
        // this should fail, but we'll call it anyway and ignore the error
481
        $socket->disconnect();
482
 
483
        if ($nHost && $nHost != $nicServer) {
484
            $tmpBuffer = $this->_connect($nHost, $domain);
485
            if (PEAR::isError($tmpBuffer)) {
486
                return $tmpBuffer;
487
            }
488
            $whoisData .= $tmpBuffer;
489
        }
490
 
491
        return $whoisData;
492
    }
493
    // }}}
494
}
495
?>