phpWhois.org/src/whois.main.php

328 lines
7.8 KiB
PHP
Raw Normal View History

<?php
/*
2005-07-27 09:57:53 +00:00
Whois.php PHP classes to conduct whois queries
2005-07-27 09:57:53 +00:00
Copyright (C)1999,2005 easyDNS Technologies Inc. & Mark Jeftovic
Maintained by David Saez
2005-07-27 09:57:53 +00:00
For the most recent version of this package visit:
2005-08-31 15:26:22 +00:00
http://www.phpwhois.org
2005-07-27 09:57:53 +00:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
2005-07-27 09:57:53 +00:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2005-07-27 09:57:53 +00:00
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2005-08-26 06:46:49 +00:00
*/
require_once('whois.client.php');
require_once('whois.idna.php');
2005-08-26 06:46:49 +00:00
class Whois extends WhoisClient
{
// Deep whois ?
var $deep_whois = true;
2005-03-03 17:14:53 +00:00
// Windows based ?
var $windows = false;
2004-04-22 17:07:57 +00:00
// Recursion allowed ?
var $gtld_recurse = true;
// Support for non-ICANN tld's
var $non_icann = false;
// Network Solutions registry server
2011-07-09 08:11:51 +00:00
var $NSI_REGISTRY = 'whois.nsiregistry.net';
/*
* Constructor function
*/
function __construct()
2005-08-26 06:46:49 +00:00
{
// Load DATA array
@require('whois.servers.php');
2006-05-31 15:18:46 +00:00
if ( ( substr( php_uname(), 0, 7 ) == 'Windows' ) )
2005-08-26 06:46:49 +00:00
$this->windows = true;
2005-10-31 17:41:11 +00:00
else
$this->windows = false;
// Set version
$this->VERSION = sprintf("phpWhois v%s-%s", $this->CODE_VERSION, $this->DATA_VERSION);
2005-08-26 06:46:49 +00:00
}
2005-07-19 02:04:15 +00:00
/*
* Use special whois server
*/
function UseServer ($tld, $server)
{
$this->WHOIS_SPECIAL[$tld] = $server;
}
/*
* Lookup query
*/
2007-04-20 16:45:39 +00:00
function Lookup($query = '', $is_utf = true)
2005-08-26 06:46:49 +00:00
{
2005-09-05 14:47:54 +00:00
// start clean
2010-07-22 07:43:51 +00:00
$this->Query = array( 'status' => '' );
2003-10-27 15:38:15 +00:00
$query = trim($query);
$IDN = new idna_convert();
2007-04-20 16:45:39 +00:00
if ($is_utf)
$query = $IDN->encode($query);
else
$query = $IDN->encode(utf8_encode($query));
// If domain to query was not set
2005-08-26 06:46:49 +00:00
if (!isSet($query) || $query == '')
{
// Configure to use default whois server
2005-07-25 07:24:57 +00:00
$this->Query['server'] = $this->NSI_REGISTRY;
2005-08-26 06:46:49 +00:00
return ;
}
// Set domain to query in query array
2003-02-16 00:24:45 +00:00
2007-05-24 16:17:39 +00:00
$this->Query['query'] = $domain = strtolower($query);
2005-07-25 07:24:57 +00:00
// If query is an ip address do ip lookup
2010-07-28 08:35:55 +00:00
if ($query == long2ip(ip2long($query)))
2005-07-25 07:24:57 +00:00
{
2010-08-23 17:22:27 +00:00
// IPv4 Prepare to do lookup via the 'ip' handler
2005-07-25 07:24:57 +00:00
$ip = @gethostbyname($query);
if (isset($this->WHOIS_SPECIAL['ip']))
{
$this->Query['server'] = $this->WHOIS_SPECIAL['ip'];
$this->Query['args'] = $ip;
}
else
{
$this->Query['server'] = 'whois.arin.net';
$this->Query['args'] = "n $ip";
$this->Query['file'] = 'whois.ip.php';
$this->Query['handler'] = 'ip';
}
2005-07-25 07:24:57 +00:00
$this->Query['host_ip'] = $ip;
2007-05-24 16:17:39 +00:00
$this->Query['query'] = $ip;
2005-07-25 07:24:57 +00:00
$this->Query['tld'] = 'ip';
$this->Query['host_name'] = @gethostbyaddr($ip);
return $this->GetData('',$this->deep_whois);
2005-07-25 07:24:57 +00:00
}
2010-08-23 17:22:27 +00:00
if (strpos($query, ':'))
{
// IPv6 AS Prepare to do lookup via the 'ip' handler
$ip = @gethostbyname($query);
if (isset($this->WHOIS_SPECIAL['ip']))
{
$this->Query['server'] = $this->WHOIS_SPECIAL['ip'];
}
else
{
$this->Query['server'] = 'whois.ripe.net';
$this->Query['file'] = 'whois.ip.ripe.php';
$this->Query['handler'] = 'ripe';
}
2010-08-23 17:22:27 +00:00
$this->Query['query'] = $ip;
$this->Query['tld'] = 'ip';
return $this->GetData('',$this->deep_whois);
}
2010-07-28 08:35:55 +00:00
if (!strpos($query, '.'))
{
2010-08-23 17:22:27 +00:00
// AS Prepare to do lookup via the 'ip' handler
2010-07-28 08:35:55 +00:00
$ip = @gethostbyname($query);
$this->Query['server'] = 'whois.arin.net';
if (strtolower(substr($ip,0,2)) == 'as')
$as = substr($ip,2);
else
$as = $ip;
$this->Query['args'] = "a $as";
$this->Query['file'] = 'whois.ip.php';
$this->Query['handler'] = 'ip';
$this->Query['query'] = $ip;
2010-08-23 17:22:27 +00:00
$this->Query['tld'] = 'as';
2010-07-28 08:35:55 +00:00
return $this->GetData('',$this->deep_whois);
}
2005-07-25 07:24:57 +00:00
// Build array of all possible tld's for that domain
2005-08-26 06:46:49 +00:00
2005-07-25 07:24:57 +00:00
$tld = '';
$server = '';
$dp = explode('.', $domain);
$np = count($dp)-1;
2003-02-09 19:56:02 +00:00
$tldtests = array();
2005-08-26 06:46:49 +00:00
for ($i = 0; $i < $np; $i++)
2005-07-25 07:24:57 +00:00
{
2003-10-27 15:38:15 +00:00
array_shift($dp);
2005-08-26 06:46:49 +00:00
$tldtests[] = implode('.', $dp);
2005-07-25 07:24:57 +00:00
}
2003-02-09 19:56:02 +00:00
2005-07-25 07:24:57 +00:00
// Search the correct whois server
2003-10-29 14:59:33 +00:00
if ($this->non_icann)
$special_tlds = array_merge($this->WHOIS_SPECIAL,$this->WHOIS_NON_ICANN);
else
$special_tlds = $this->WHOIS_SPECIAL;
2005-07-25 07:24:57 +00:00
foreach($tldtests as $tld)
{
// Test if we know in advance that no whois server is
// available for this domain and that we can get the
// data via http or whois request
if (isset($special_tlds[$tld]))
2005-08-26 06:46:49 +00:00
{
$val = $special_tlds[$tld];
if ($val == '') return $this->Unknown();
2005-09-05 14:47:54 +00:00
$domain = substr($query, 0, - strlen($tld) - 1);
2005-08-26 06:46:49 +00:00
$val = str_replace('{domain}', $domain, $val);
2005-09-05 14:47:54 +00:00
$server = str_replace('{tld}', $tld, $val);
2005-08-26 06:46:49 +00:00
break;
}
2005-07-25 07:24:57 +00:00
}
if ($server == '')
foreach($tldtests as $tld)
{
// Determine the top level domain, and it's whois server using
// DNS lookups on 'whois-servers.net'.
// Assumes a valid DNS response indicates a recognised tld (!?)
2006-05-31 15:18:46 +00:00
$cname = $tld.'.whois-servers.net';
2006-05-31 15:18:46 +00:00
if (gethostbyname($cname) == $cname) continue;
$server = $tld.'.whois-servers.net';
break;
}
2005-07-25 07:24:57 +00:00
if ($tld && $server)
{
// If found, set tld and whois server in query array
2005-07-25 07:24:57 +00:00
$this->Query['server'] = $server;
2005-08-26 06:46:49 +00:00
$this->Query['tld'] = $tld;
2005-07-25 07:24:57 +00:00
$handler = '';
2005-07-16 07:19:32 +00:00
foreach($tldtests as $htld)
{
// special handler exists for the tld ?
2005-07-25 07:24:57 +00:00
if (isSet($this->DATA[$htld]))
{
2005-07-16 07:19:32 +00:00
$handler = $this->DATA[$htld];
break;
2005-07-25 07:24:57 +00:00
}
// Regular handler exists for the tld ?
if (($fp = @fopen('whois.'.$htld.'.php', 'r', 1)) and fclose($fp))
{
$handler = $htld;
break;
}
2005-07-25 07:24:57 +00:00
}
// If there is a handler set it
2005-08-26 06:46:49 +00:00
if ($handler != '')
2005-07-25 07:24:57 +00:00
{
$this->Query['file'] = "whois.$handler.php";
$this->Query['handler'] = $handler;
2005-07-16 07:19:32 +00:00
}
2005-07-25 07:24:57 +00:00
// Special parameters ?
if (isset($this->WHOIS_PARAM[$server]))
2010-08-23 11:28:46 +00:00
$this->Query['server'] = $this->Query['server'].'?'.str_replace('$',$domain,$this->WHOIS_PARAM[$server]);
$result = $this->GetData('',$this->deep_whois);
$this->Checkdns($result);
return $result;
2005-07-25 07:24:57 +00:00
}
// If tld not known, and domain not in DNS, return error
return $this->Unknown();
}
/* Unsupported domains */
function Unknown()
{
2005-07-25 07:24:57 +00:00
unset($this->Query['server']);
$this->Query['status'] = 'error';
$result['rawdata'][] = $this->Query['errstr'][] = $this->Query['query'].' domain is not supported';
$this->Checkdns($result);
$this->FixResult($result, $this->Query['query']);
return $result;
}
/* Get nameservers if missing */
function Checkdns(&$result)
{
if ($this->deep_whois && empty($result['regrinfo']['domain']['nserver']) && function_exists('dns_get_record'))
{
2011-07-08 18:56:36 +00:00
$ns = @dns_get_record($this->Query['query'],DNS_NS);
if (!is_array($ns)) return;
$nserver = array();
foreach($ns as $row) $nserver[] = $row['target'];
2011-06-26 10:41:24 +00:00
if (count($nserver) > 0)
$result['regrinfo']['domain']['nserver'] = $this->FixNameServer($nserver);
}
2005-08-26 06:46:49 +00:00
}
2005-03-02 16:04:13 +00:00
/*
2005-08-26 06:46:49 +00:00
* Fix and/or add name server information
*/
2005-08-26 06:46:49 +00:00
function FixResult(&$result, $domain)
{
2005-03-03 17:14:53 +00:00
// Add usual fields
2005-08-26 06:46:49 +00:00
$result['regrinfo']['domain']['name'] = $domain;
2005-03-03 17:14:53 +00:00
// Check if nameservers exist
2005-07-16 07:19:32 +00:00
2005-08-26 06:46:49 +00:00
if (!isset($result['regrinfo']['registered']))
{
2010-08-24 19:10:27 +00:00
if (function_exists('checkdnsrr') && checkdnsrr($domain, 'NS'))
2005-08-26 06:46:49 +00:00
$result['regrinfo']['registered'] = 'yes';
2005-03-03 17:14:53 +00:00
else
2005-08-26 06:46:49 +00:00
$result['regrinfo']['registered'] = 'unknown';
2005-03-03 17:14:53 +00:00
}
2005-03-02 16:04:13 +00:00
2005-03-03 17:14:53 +00:00
// Normalize nameserver fields
2008-09-07 09:00:12 +00:00
if (isset($result['regrinfo']['domain']['nserver']))
2005-08-26 06:46:49 +00:00
{
2008-09-07 09:00:12 +00:00
if (!is_array($result['regrinfo']['domain']['nserver']))
2005-07-16 07:19:32 +00:00
{
2008-09-07 09:00:12 +00:00
unset($result['regrinfo']['domain']['nserver']);
2005-07-16 07:19:32 +00:00
}
2008-09-07 09:00:12 +00:00
else
$result['regrinfo']['domain']['nserver'] = $this->FixNameServer($result['regrinfo']['domain']['nserver']);
2005-08-26 06:46:49 +00:00
}
}
2005-03-02 16:04:13 +00:00
}
?>