added function to set specific whois servers

This commit is contained in:
sparc 2006-01-20 11:42:02 +00:00
parent 5fa072898c
commit 70dfd4b98f
7 changed files with 272 additions and 81 deletions

View file

@ -1,3 +1,7 @@
2006/01/20 David Saez <david@ols.es>
- changed .es http based whois server
- added function to set specific whois servers
2006/01/18 David Saez <david@ols.es>
- minor fixes in enom handler

View file

@ -76,7 +76,51 @@ $result = $whois->Lookup('62.97.102.115');
$whois = new Whois();
$result = $whois->Lookup('AS220');
Notes
Using special whois server
--------------------------
Some registrars can give special access to registered whois gatewais
in order to have more fine control against abusing the whois services.
The currently know whois services that offer special acccess are:
- ripe
The new ripe whois server software support some special parameters
that allow to pass the real client ip address. This feature is only
available to registered gateways. If you are registered you can use
this service when querying ripe ip addresses that way:
$whois = new Whois();
$whois->UseServer('uk','whois.ripe.net?-V{version},{ip} {query}');
$result = $whois->Lookup('62.97.102.115');
- whois.isoc.org.il
This server is also using the new ripe whois server software and
thus works the same way. If you are registered you can use this service
when querying .il domains that way:
$whois = new Whois();
$whois->UseServer('uk','whois.isoc.org.il?-V{version},{ip} {query}');
$result = $whois->Lookup('example.co.uk');
- whois.nic.uk
They offer what they call WHOIS2 (see http://www.nominet.org.uk/go/whois2 )
to registered users (usually Nominet members) with a higher amount of
permited queries by hour. If you are registered you can use this service
when querying .uk domains that way:
$whois = new Whois();
$whois->UseServer('uk','whois.nic.uk:1043?{hname} {ip} {query}');
$result = $whois->Lookup('example.co.uk');
This new feature also allows you to use a different whois server than
the preconfigured o discovered one by just calling whois->UseServer
and passing the tld and the server and args to use for the named tld.
UseServer can be called as many times as necessary.
Notes
-----
There is an extended class called "whois.utils.php" which contains a

View file

@ -44,7 +44,12 @@ if(isSet($_GET['query']))
include_once('whois.utils.php');
$whois = new Whois();
// To use special whois servers (see README)
// $whois->UseServer('uk','whois.isoc.org.il?-V{version},{ip} {query}');
$result = $whois->Lookup($query);
echo "<b>Results for $query :</b><p>";
switch ($output)

View file

@ -25,6 +25,8 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
require_once('whois.ip.lib.php');
class WhoisClient {
// Recursion allowed ?
@ -56,6 +58,12 @@ class WhoisClient {
'status',
'server'
);
// This release of the package
var $CODE_VERSION = '4.0.1';
// Full code and data version string (e.g. 'Whois2.php v3.01:16')
var $VERSION;
/*
* Constructor function
@ -63,6 +71,9 @@ class WhoisClient {
function WhoisClient () {
// Load DATA array
@require('whois.servers.php');
// Set version
$this->VERSION = sprintf("phpWhois v%s-%s", $this->CODE_VERSION, $this->DATA_VERSION);
}
/*
@ -85,14 +96,53 @@ class WhoisClient {
}
// Check if protocol is http
if (substr($this->Query['server'],0,7)=='http://' ||
substr($this->Query['server'],0,8)=='https://')
{
$output = $this->httpQuery($this->Query['server']);
$query_args = '';
}
else
{
// Get args
if (strpos($this->Query['server'],'?'))
{
$parts = explode('?',$this->Query['server']);
$this->Query['server'] = trim($parts[0]);
$query_args = trim($parts[1]);
// replace substitution parameters
$query_args = str_replace('{query}', $string, $query_args);
$query_args = str_replace('{version}', 'phpWhois'.$this->CODE_VERSION, $query_args);
if (strpos($query_args,'{ip}')!==false)
{
$query_args = str_replace('{ip}', getclientip(), $query_args);
}
if (strpos($query_args,'{hname}')!==false)
{
$query_args = str_replace('{hname}', gethostbyaddr(getclientip()), $query_args);
}
}
else
$query_args = $string;
// Get port
if (strpos($this->Query['server'],':'))
{
$parts = explode(':',$this->Query['server']);
$this->Query['server'] = trim($parts[0]);
$this->Query['server_port'] = trim($parts[1]);
}
else
$this->Query['server_port'] = $this->PORT;
// Connect to whois server, or return if failed
$ptr = $this->Connect();
if($ptr < 0) {
@ -104,11 +154,9 @@ class WhoisClient {
if (version_compare(phpversion(),'4.3.0')>=0)
stream_set_timeout($ptr,$this->STIMEOUT);
if (isset($this->WHOIS_PARAM[$this->Query['server']]))
fputs($ptr, $this->WHOIS_PARAM[$this->Query['server']].trim($string)."\r\n");
else
fputs($ptr, trim($string)."\r\n");
// Send query
fputs($ptr, trim($query_args)."\r\n");
// Prepare to receive result
$raw = '';
$output = array();
@ -134,7 +182,6 @@ class WhoisClient {
$result['rawdata'] = $output;
// If we have a handler, post-process it with that
if(isSet($this->Query['handler']))
$result = $this->Process($result);
@ -142,6 +189,14 @@ class WhoisClient {
if (!isset($result['regyinfo']['whois']))
$result['regyinfo']['whois'] = $this->Query['server'];
// Set whois server full query
if (!isset($result['regyinfo']['args']))
$result['regyinfo']['args'] = $query_args;
// Set whois server port
if (!isset($result['regyinfo']['port']))
$result['regyinfo']['port'] = $this->Query['server_port'] ;
// Type defaults to domain
if (!isset($result['regyinfo']['type']))
$result['regyinfo']['type'] = 'domain';
@ -236,7 +291,7 @@ class WhoisClient {
return(-1);
// Get rid of protocol and/or get port
$port = $this->PORT;
$port = $this->Query['server_port'];
$pos = strpos($server,'://');

115
src/whois.ip.lib.php Executable file
View file

@ -0,0 +1,115 @@
<?php
/*
Whois.php PHP classes to conduct whois queries
Copyright (C)1999,2005 easyDNS Technologies Inc. & Mark Jeftovic
Maintained by David Saez (david@ols.es)
For the most recent version of this package visit:
http://www.phpwhois.org
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.
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.
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.
*/
//-----------------------------------------------------------------
// Check if ip adddress is valid
function validip($ip)
{
if (!empty($ip) && ip2long($ip)!=-1) {
$reserved_ips = array (
array('0.0.0.0','2.255.255.255'),
array('10.0.0.0','10.255.255.255'),
array('127.0.0.0','127.255.255.255'),
array('169.254.0.0','169.254.255.255'),
array('172.16.0.0','172.31.255.255'),
array('192.0.2.0','192.0.2.255'),
array('192.168.0.0','192.168.255.255'),
array('255.255.255.0','255.255.255.255')
);
foreach ($reserved_ips as $r) {
$min = ip2long($r[0]);
$max = ip2long($r[1]);
if ((ip2long($ip) >= $min) && (ip2long($ip) <= $max)) return false;
}
return true;
}
else
return false;
}
//-----------------------------------------------------------------
// Get real client ip address
function getclientip()
{
if (!empty($_SERVER['HTTP_CLIENT_IP']) && validip($_SERVER['HTTP_CLIENT_IP']))
return $_SERVER['HTTP_CLIENT_IP'];
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
foreach (explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']) as $ip)
if (validip(trim($ip)))
return $ip;
if (!empty($_SERVER['HTTP_X_FORWARDED']) && validip($_SERVER['HTTP_X_FORWARDED']))
return $_SERVER['HTTP_X_FORWARDED'];
if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && validip($_SERVER['HTTP_FORWARDED_FOR']))
return $_SERVER['HTTP_FORWARDED_FOR'];
if (!empty($_SERVER['HTTP_FORWARDED']) && validip($_SERVER['HTTP_FORWARDED']))
return $_SERVER['HTTP_FORWARDED'];
if (!empty($_SERVER['HTTP_X_FORWARDED']) && validip($_SERVER['HTTP_X_FORWARDED']))
return $_SERVER['HTTP_X_FORWARDED'];
return $_SERVER['REMOTE_ADDR'];
}
//-----------------------------------------------------------------
// Convert from CIDR to net range
function cidr_conv($net)
{
$start = strtok($net, '/');
$n = 3-substr_count($net, '.');
if ($n > 0)
{
for ($i = $n; $i > 0; $i--)
$start.= '.0';
}
$bits1 = str_pad(decbin(ip2long($start)), 32, '0', 'STR_PAD_LEFT');
$net = pow(2, (32-substr(strstr($net, '/'), 1))) - 1;
$bits2 = str_pad(decbin($net), 32, '0', 'STR_PAD_LEFT');
$final = '';
for ($i = 0; $i < 32; $i++)
{
if ($bits1[$i] == $bits2[$i])
$final.= $bits1[$i];
if ($bits1[$i] == 1 and $bits2[$i] == 0)
$final.= $bits1[$i];
if ($bits1[$i] == 0 and $bits2[$i] == 1)
$final.= $bits2[$i];
}
return $start." - ".long2ip(bindec($final));
}
?>

View file

@ -39,6 +39,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
if (!defined('__IP_HANDLER__'))
define('__IP_HANDLER__', 1);
require_once('whois.ip.lib.php');
class ip_handler extends WhoisClient
{
@ -212,13 +214,13 @@ class ip_handler extends WhoisClient
{
//Convert CDIR to inetnum
$result['regrinfo']['network']['cdir'] = $result['regrinfo']['network']['inetnum'];
$result['regrinfo']['network']['inetnum'] = $this->cidr_conv($result['regrinfo']['network']['cdir']);
$result['regrinfo']['network']['inetnum'] = cidr_conv($result['regrinfo']['network']['cdir']);
}
if (!isset($result['regrinfo']['network']['inetnum']) && isset($result['regrinfo']['network']['cdir']))
{
//Convert CDIR to inetnum
$result['regrinfo']['network']['inetnum'] = $this->cidr_conv($result['regrinfo']['network']['cdir']);
$result['regrinfo']['network']['inetnum'] = cidr_conv($result['regrinfo']['network']['cdir']);
}
// Try to find abuse email address
@ -270,37 +272,5 @@ class ip_handler extends WhoisClient
return $result;
}
//-----------------------------------------------------------------
function cidr_conv($net)
{
$start = strtok($net, '/');
$n = 3-substr_count($net, '.');
if ($n > 0)
{
for ($i = $n; $i > 0; $i--)
$start.= '.0';
}
$bits1 = str_pad(decbin(ip2long($start)), 32, '0', 'STR_PAD_LEFT');
$net = pow(2, (32-substr(strstr($net, '/'), 1))) - 1;
$bits2 = str_pad(decbin($net), 32, '0', 'STR_PAD_LEFT');
$final = '';
for ($i = 0; $i < 32; $i++)
{
if ($bits1[$i] == $bits2[$i])
$final.= $bits1[$i];
if ($bits1[$i] == 1 and $bits2[$i] == 0)
$final.= $bits1[$i];
if ($bits1[$i] == 0 and $bits2[$i] == 1)
$final.= $bits2[$i];
}
return $start." - ".long2ip(bindec($final));
}
}
?>

View file

@ -35,12 +35,6 @@ class Whois extends WhoisClient
// Recursion allowed ?
var $gtld_recurse = true;
// Full code and data version string (e.g. 'Whois2.php v3.01:16')
var $VERSION;
// This release of the package
var $CODE_VERSION = "4.0.1";
// Network Solutions registry server
var $NSI_REGISTRY = "whois.nsiregistry.net";
@ -56,12 +50,20 @@ class Whois extends WhoisClient
$this->windows = true;
else
$this->windows = false;
// Set version
$this->VERSION = sprintf("Whois.php v%s:%s", $this->CODE_VERSION, $this->DATA_VERSION);
$this->VERSION = sprintf("phpWhois v%s-%s", $this->CODE_VERSION, $this->DATA_VERSION);
}
/*
* Use special whois server
*/
function UseServer ($tld, $server)
{
$this->WHOIS_SPECIAL[$tld] = $server;
}
function Lookup($query = '')
{
// start clean
@ -118,20 +120,7 @@ class Whois extends WhoisClient
// 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
/*
reset($this->WHOIS_SPECIAL);
while (list($key, $val) = each($this->WHOIS_SPECIAL))
if ($tld == $key)
{
$domain = substr($query, 0, - strlen($key) - 1);
$val = str_replace('{domain}', $domain, $val);
$server = str_replace('{tld}', $key, $val);
break;
}
if ($server != '')
break;
*/
if (isset($this->WHOIS_SPECIAL[$tld]))
{
$val = $this->WHOIS_SPECIAL[$tld];
@ -140,23 +129,27 @@ class Whois extends WhoisClient
$server = str_replace('{tld}', $tld, $val);
break;
}
// 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 (!?)
if ($this->windows)
$cname = $this->checkdnsrr_win($tld.'.whois-servers.net', 'CNAME');
else
$cname = checkdnsrr($tld.'.whois-servers.net', 'CNAME');
if (!$cname)
continue;
//This also works
//$server = gethostbyname($tld.".whois-servers.net");
$server = $tld.'.whois-servers.net';
break;
}
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 (!?)
if ($this->windows)
$cname = $this->checkdnsrr_win($tld.'.whois-servers.net', 'CNAME');
else
$cname = checkdnsrr($tld.'.whois-servers.net', 'CNAME');
if (!$cname) continue;
//This also works
//$server = gethostbyname($tld.".whois-servers.net");
$server = $tld.'.whois-servers.net';
break;
}
if ($tld && $server)
{
@ -175,7 +168,7 @@ class Whois extends WhoisClient
}
}
// If there is a handler process the data
// If there is a handler set it
if ($handler != '')
{
@ -183,6 +176,11 @@ class Whois extends WhoisClient
$this->Query['handler'] = $handler;
}
// Special parameters ?
if (isset($this->WHOIS_PARAM[$server]))
$this->Query['server'] = $this->Query['server'].'?'.$this->WHOIS_PARAM[$server].$domain;
return $this->GetData();
}