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.
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.
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
29
require_once 'PEAR.php';
32
* Looks up records in the databases maintained by several Network Information
33
* Centres (NICs). This class uses PEAR's Net_Socket:: class.
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
41
class Net_Whois extends PEAR
47
* Retrieve authoritative definition only
52
var $authoritative = false;
55
* Port for whois servers
63
* See options for stream_context_create.
71
* List of NICs to query
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.'
91
* Search string of server to search on
96
var $_whoisServerID = 'Whois Server: ';
99
* Server to search for IP address lookups
104
var $_ipNicServers = array ('RNICHOST', 'PNICHOST', 'BNICHOST');
107
* List of error codes and text
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',
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
126
var $_timeout = false;
131
* Constructs a new Net_Whois object
140
$this->setAuthoritative();
147
* Set timeout value - number of seconds afterwhich an attempt to connect
148
* to a whois server should be aborted.
150
* @param integer $timeout false is also an acceptable value
155
function setTimeout($timeout = false)
157
$this->_timeout = $timeout;
163
* Retrieve timeout value
167
* @return mixed either false or an integer value
169
function getTimeout()
171
return $this->_timeout;
179
* @param bool $authoritative defaults to false
184
function setAuthoritative($authoritative = false)
186
$this->authoritative = $authoritative;
190
// {{{ getAuthoritative()
194
* @return bool Query for authoritative result?
196
function getAuthoritative()
198
return (bool) $this->authoritative;
204
* set which port should be used
206
* @param integer $port Port to use
211
function setPort($port = false)
213
$port = is_numeric($port) ? $port : getservbyname('whois', 'tcp');
214
$this->port = $port ? $port : 43;
220
* Retrieve which port to connect to.
222
* @return integer port to connect to
230
function setOptions($options)
232
if ((!is_null($options)) && (!is_array($options))) {
235
$this->options = $options;
240
* Retrieve which port to connect to.
244
function getOptions()
246
return $this->options;
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
257
* @param string $domain IP address or host name
258
* @param string $userWhoisServer server to query (optional)
261
* @return mixed returns a PEAR_Error on failure, or a string on success
263
function query($domain, $userWhoisServer = null)
265
$domain = trim($domain);
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'];
274
$whoisServer = $this->_chooseServer($domain);
277
$_domain = $this->authoritative ? 'domain ' . $domain : $domain;
278
$whoisData = $this->_connect($whoisServer, $_domain);
280
if (PEAR::isError($whoisData)) {
284
if ($this->authoritative) {
285
$pattern = '/\s+' . preg_quote($this->_whoisServerID) . '(.+?)\n/i';
287
if (preg_match($pattern, $whoisData, $matches)) {
288
$whoisData = $this->_connect(trim(array_pop($matches)), $domain);
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.
301
* @param string $domain IP address or host name
304
* @return mixed returns a PEAR_Error on failure, or a string on success
306
function queryAPNIC($domain)
308
return $this->query($domain, $this->_nicServers['PNICHOST']);
314
* Use the IPv6 Resource Center (6bone) database. It contains network
315
* names and addresses for the IPv6 network.
317
* @param string $domain IP address or host name
320
* @return mixed returns a PEAR_Error on failure, or a string on success
322
function queryIPv6($domain)
324
return $this->query($domain, $this->_nicServers['SNICHOST']);
330
* Use the Route Arbiter Database (RADB) database. It contains
331
* route policy specifications for a large number of operators'
334
* @param string $ipAddress IP address
337
* @return mixed returns a PEAR_Error on failure, or a string on success
339
function queryRADB($ipAddress)
341
return $this->query($ipAddress, $this->_nicServers['MNICHOST']);
345
// {{{ _chooseServer()
347
* Determines the correct server to connect to based upon the domain
349
* @param string $query IP address or host name
352
* @return string whois server host name
354
function _chooseServer($query)
356
if (!strpos($query, '.')) {
357
return $this->_nicServers['NICHOST'];
360
$TLD = substr($query, strrpos($query, '.') + 1);
362
if (is_numeric($TLD)) {
363
$whoisServer = $this->_nicServers['ANICHOST'];
365
$whoisServer = $this->getDomainServer($query);
372
// {{{ getDomainServer()
374
* Determines the correct whois server to connect to based upon the domain
376
* @param string $q domain name
379
* @return string whois server ip address
381
function getDomainServer($q)
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]);
392
//check host has real ip
393
$q = gethostbyname($q . $tail);
394
if (filter_var($q, FILTER_VALIDATE_IP)) {
403
* Connects to the whois server and retrieves domain information
405
* @param string $nicServer FQDN of whois server to query
406
* @param string $domain Domain name to query
409
* @return mixed returns a PEAR_Error on failure, string of whois data on success
411
function _connect($nicServer, $domain)
413
include_once 'Net/Socket.php';
415
if (is_null($nicServer) || (empty($nicServer))) {
416
return new PEAR_Error($this->_errorCodes[014], 14);
419
if (PEAR::isError($socket = new Net_Socket())) {
420
return new PEAR_Error($this->_errorCodes[010], 10);
423
$result = $socket->connect(
430
if (PEAR::isError($result)) {
431
return new PEAR_Error($this->_errorCodes[011], 11);
433
$socket->setBlocking(false);
434
if (PEAR::isError($socket->writeLine($domain))) {
435
return new PEAR_Error($this->_errorCodes[012], 12);
440
$whoisData = $socket->readAll();
441
if (PEAR::isError($whoisData)) {
442
return new PEAR_Error($this->_errorCodes[013], 13);
445
$data = explode("\n", $whoisData);
446
foreach ($data as $line) {
447
$line = rtrim($line);
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];
464
// this should fail, but we'll call it anyway and ignore the error
465
$socket->disconnect();
467
if ($nHost && $nHost != $nicServer) {
468
$tmpBuffer = $this->_connect($nHost, $domain);
469
if (PEAR::isError($tmpBuffer)) {
472
$whoisData .= $tmpBuffer;