~ubuntu-branches/ubuntu/raring/php-net-whois/raring

« back to all changes in this revision

Viewing changes to Net_Whois-1.0.4/Whois.php

  • Committer: Bazaar Package Importer
  • Author(s): Dario Minnucci
  • Date: 2011-08-23 14:26:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110823142612-s6t1dbye9qh050yo
Tags: 1.0.5-1
* New upstream version (1.0.5)
* debian/control:
  - Added Vcs-{Git|Browser} fields: php-net-whois goes collab-maint
  - Bump Standards-Version to 3.9.2 (no changes)
  - Use debhelper >= 8
* debian/copyright: updated

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 301534 2010-07-25 13:21:32Z 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
 
    function setOptions($options)
231
 
    {
232
 
        if ((!is_null($options)) && (!is_array($options))) {
233
 
            return;
234
 
        }
235
 
        $this->options = $options;
236
 
    }
237
 
 
238
 
    // {{{ getOptions()
239
 
    /**
240
 
     * Retrieve which port to connect to.
241
 
     *
242
 
     * @return array
243
 
     */
244
 
    function getOptions()
245
 
    {
246
 
        return $this->options;
247
 
    }
248
 
    // }}}
249
 
 
250
 
    // {{{ query()
251
 
    /**
252
 
     * Connect to the necessary servers to perform a domain whois query.  Prefix
253
 
     * queries with a "!" to lookup information in InterNIC handle database.
254
 
     * Add a "-arin" suffix to queries to lookup information in ARIN handle
255
 
     * database.
256
 
     *
257
 
     * @param string $domain          IP address or host name
258
 
     * @param string $userWhoisServer server to query (optional)
259
 
     *
260
 
     * @access public
261
 
     * @return mixed returns a PEAR_Error on failure, or a string on success
262
 
     */
263
 
    function query($domain, $userWhoisServer = null)
264
 
    {
265
 
        $domain = trim($domain);
266
 
 
267
 
        if (isset($userWhoisServer)) {
268
 
            $whoisServer = $userWhoisServer;
269
 
        } elseif (preg_match('/^!.*/', $domain)) {
270
 
            $whoisServer = $this->_nicServers['INICHOST'];
271
 
        } elseif (preg_match('/.*?-arin/i', $domain)) {
272
 
            $whoisServer = $this->_nicServers['ANICHOST'];
273
 
        } else {
274
 
            $whoisServer = $this->_chooseServer($domain);
275
 
        }
276
 
 
277
 
        $_domain = $this->authoritative ? 'domain ' . $domain : $domain;
278
 
        $whoisData = $this->_connect($whoisServer, $_domain);
279
 
 
280
 
        if (PEAR::isError($whoisData)) {
281
 
            return $whoisData;
282
 
        }
283
 
 
284
 
        if ($this->authoritative) {
285
 
                        $pattern = '/\s+' . preg_quote($this->_whoisServerID) . '(.+?)\n/i';
286
 
 
287
 
            if (preg_match($pattern, $whoisData, $matches)) {
288
 
                $whoisData = $this->_connect(trim(array_pop($matches)), $domain);
289
 
            }
290
 
        }
291
 
        return $whoisData;
292
 
    }
293
 
    // }}}
294
 
 
295
 
    // {{{ queryAPNIC()
296
 
    /**
297
 
     * Use the Asia/Pacific Network Information Center (APNIC) database.
298
 
     * It contains network numbers used in East Asia, Australia, New
299
 
     * Zealand, and the Pacific islands.
300
 
     *
301
 
     * @param string $domain IP address or host name
302
 
     *
303
 
     * @access public
304
 
     * @return mixed returns a PEAR_Error on failure, or a string on success
305
 
     */
306
 
    function queryAPNIC($domain)
307
 
    {
308
 
        return $this->query($domain, $this->_nicServers['PNICHOST']);
309
 
    }
310
 
    // }}}
311
 
 
312
 
    // {{{ queryIPv6()
313
 
    /**
314
 
     * Use the IPv6 Resource Center (6bone) database.  It contains network
315
 
     * names and addresses for the IPv6 network.
316
 
     *
317
 
     * @param string $domain IP address or host name
318
 
     *
319
 
     * @access public
320
 
     * @return mixed returns a PEAR_Error on failure, or a string on success
321
 
     */
322
 
    function queryIPv6($domain)
323
 
    {
324
 
        return $this->query($domain, $this->_nicServers['SNICHOST']);
325
 
    }
326
 
    // }}}
327
 
 
328
 
    // {{{ queryRADB()
329
 
    /**
330
 
     * Use the Route Arbiter Database (RADB) database.  It contains
331
 
     * route policy specifications for a large number of operators'
332
 
     * networks.
333
 
     *
334
 
     * @param string $ipAddress IP address
335
 
     *
336
 
     * @access public
337
 
     * @return mixed returns a PEAR_Error on failure, or a string on success
338
 
     */
339
 
    function queryRADB($ipAddress)
340
 
    {
341
 
        return $this->query($ipAddress, $this->_nicServers['MNICHOST']);
342
 
    }
343
 
    // }}}
344
 
 
345
 
    // {{{ _chooseServer()
346
 
    /**
347
 
     * Determines the correct server to connect to based upon the domain
348
 
     *
349
 
     * @param string $query IP address or host name
350
 
     *
351
 
     * @access private
352
 
     * @return string whois server host name
353
 
     */
354
 
    function _chooseServer($query)
355
 
    {
356
 
        if (!strpos($query, '.')) {
357
 
            return $this->_nicServers['NICHOST'];
358
 
        }
359
 
 
360
 
        $TLD = substr($query, strrpos($query, '.') + 1);
361
 
 
362
 
        if (is_numeric($TLD)) {
363
 
            $whoisServer = $this->_nicServers['ANICHOST'];
364
 
        } else {
365
 
            $whoisServer = $this->getDomainServer($query);
366
 
        }
367
 
 
368
 
        return $whoisServer;
369
 
    }
370
 
    // }}}
371
 
 
372
 
    // {{{ getDomainServer()
373
 
    /**
374
 
     * Determines the correct whois server to connect to based upon the domain
375
 
     *
376
 
     * @param string $q domain name
377
 
     *
378
 
     * @access public
379
 
     * @return string whois server ip address
380
 
     */
381
 
    function getDomainServer($q)
382
 
    {
383
 
        $tail = $this->_nicServers['QNICHOST_TAIL'];
384
 
        if (strchr($q, '.')) {
385
 
            //get the last 2 parts
386
 
            $q = array_reverse(explode('.', $q));
387
 
            $a = array($q[1] . '.' . $q[0], $q[0]);
388
 
        } else {
389
 
            $a = array($q);
390
 
        }
391
 
        foreach ($a as $q) {
392
 
            //check host has real ip
393
 
            $q = gethostbyname($q . $tail);
394
 
            if (filter_var($q, FILTER_VALIDATE_IP)) {
395
 
                return $q;
396
 
            }
397
 
        }
398
 
    }
399
 
    // }}}
400
 
 
401
 
    // {{{ _connect()
402
 
    /**
403
 
     * Connects to the whois server and retrieves domain information
404
 
     *
405
 
     * @param string $nicServer FQDN of whois server to query
406
 
     * @param string $domain    Domain name to query
407
 
     *
408
 
     * @access private
409
 
     * @return mixed returns a PEAR_Error on failure, string of whois data on success
410
 
     */
411
 
    function _connect($nicServer, $domain)
412
 
    {
413
 
        include_once 'Net/Socket.php';
414
 
 
415
 
        if (is_null($nicServer) || (empty($nicServer))) {
416
 
            return new PEAR_Error($this->_errorCodes[014], 14);
417
 
        }
418
 
 
419
 
        if (PEAR::isError($socket = new Net_Socket())) {
420
 
            return new PEAR_Error($this->_errorCodes[010], 10);
421
 
        }
422
 
 
423
 
        $result = $socket->connect(
424
 
            $nicServer,
425
 
            $this->getPort(),
426
 
            null,
427
 
            $this->getTimeout(),
428
 
            $this->getOptions()
429
 
        );
430
 
        if (PEAR::isError($result)) {
431
 
            return new PEAR_Error($this->_errorCodes[011], 11);
432
 
        }
433
 
        $socket->setBlocking(false);
434
 
        if (PEAR::isError($socket->writeLine($domain))) {
435
 
            return new PEAR_Error($this->_errorCodes[012], 12);
436
 
        }
437
 
 
438
 
        $nHost = null;
439
 
 
440
 
        $whoisData = $socket->readAll();
441
 
        if (PEAR::isError($whoisData)) {
442
 
            return new PEAR_Error($this->_errorCodes[013], 13);
443
 
        }
444
 
 
445
 
        $data = explode("\n", $whoisData);
446
 
        foreach ($data as $line) {
447
 
            $line = rtrim($line);
448
 
 
449
 
            // check for whois server redirection
450
 
            if (!isset($nHost)) {
451
 
                $pattern='/'.$this->_whoisServerID.'(.*)/';
452
 
                if (preg_match($pattern, $line, $matches)) {
453
 
                    $nHost = $matches[1];
454
 
                } elseif ($nicServer == $this->_nicServers['ANICHOST']) {
455
 
                    foreach ($this->_ipNicServers as $ipNicServer) {
456
 
                        if (strstr($line, trim($this->_nicServers[$ipNicServer],'.'))) {
457
 
                            $nHost = $this->_nicServers[$ipNicServer];
458
 
                        }
459
 
                    }
460
 
                }
461
 
            }
462
 
        }
463
 
 
464
 
        // this should fail, but we'll call it anyway and ignore the error
465
 
        $socket->disconnect();
466
 
 
467
 
        if ($nHost && $nHost != $nicServer) {
468
 
            $tmpBuffer = $this->_connect($nHost, $domain);
469
 
            if (PEAR::isError($tmpBuffer)) {
470
 
                return $tmpBuffer;
471
 
            }
472
 
            $whoisData .= $tmpBuffer;
473
 
        }
474
 
 
475
 
        return $whoisData;
476
 
    }
477
 
    // }}}
478
 
}
479
 
?>