phpWhois.org/main.whois

315 lines
8.7 KiB
Text
Raw Normal View History

2002-10-11 12:16:06 +00:00
<?php
2002-10-08 16:49:26 +00:00
/*
2002-10-11 12:16:06 +00:00
Whois2.php PHP classes to conduct whois queries
2002-10-08 16:49:26 +00:00
2002-10-11 12:16:06 +00:00
Copyright (C)1999,2000 easyDNS Technologies Inc. & Mark Jeftovic
2002-10-08 16:49:26 +00:00
2002-10-11 12:16:06 +00:00
Maintained by Mark Jeftovic <markjr@easydns.com>
2002-10-08 16:49:26 +00:00
2002-10-11 12:16:06 +00:00
For the most recent version of this package:
2002-10-08 16:49:26 +00:00
2002-10-11 12:16:06 +00:00
http://www.easydns.com/~markjr/whois2/
2002-10-08 16:49:26 +00:00
2002-10-11 12:16:06 +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.
2002-10-08 16:49:26 +00:00
2002-10-11 12:16:06 +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.
2002-10-08 16:49:26 +00:00
2002-10-11 12:16:06 +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.
2002-10-08 16:49:26 +00:00
*/
class Whois {
// Full code and data version string (e.g. 'Whois2.php v3.01:16')
2002-10-11 12:16:06 +00:00
var $VERSION;
// This release of the package
2002-10-11 12:16:06 +00:00
var $CODE_VERSION = "3.0.1";
// Network Solutions registry server
2002-10-11 12:16:06 +00:00
var $NSI_REGISTRY = "whois.nsiregistry.net";
// Network Solutions registrar server (?)
2002-10-11 12:16:06 +00:00
var $NSI_REGISTRAR = "whois.networksolutions.com";
// Default WHOIS port
2002-10-11 12:16:06 +00:00
var $PORT = 43;
// Maximum number of retries on connection failure
var $RETRY = 0;
// Time to wait between retries
2002-10-11 12:16:06 +00:00
var $SLEEP = 2;
// Read buffer size (0 == char by char)
var $BUFFER = 0;
// Status response codes
2002-10-11 12:16:06 +00:00
var $STAT = array(
-1 => "error",
0 => "ready",
1 => "ok"
);
// Array to contain all query variables
2002-10-11 12:16:06 +00:00
var $Query = array(
"tld" => "",
"type" => "domain",
"string" => "",
"status",
"server"
);
// Various hacks. In a perfect world we don't need these.
2002-10-11 12:16:06 +00:00
var $HACKS = array(
// force "dom" keywork
"nsi_force_dom" => 1,
// set if nsiregistry gives wrong whois server for netsol
"nsi_referral_loop" => 0,
// ???
2002-10-11 12:16:06 +00:00
"wrong_netsol_whois" => "rs.internic.net",
// ???
2002-10-11 12:16:06 +00:00
"real_netsol_whois" => "whois.networksolutions.com",
// force english output on .jp for us ethnocentric types, unset or comment out for Japanese output
"force_slash_e" => "whois.nic.ad.jp",
// whois.nic.cx hangs forever
"cx_is_broken" => 1
2002-10-11 12:16:06 +00:00
);
// List of servers and handlers (loaded from servers.whois)
var $DATA = array();
2002-10-11 12:16:06 +00:00
/*
* Constructor function
*/
function Whois ($query = "") {
// Load DATA array
2002-10-11 12:16:06 +00:00
require("servers.whois");
// Set version
$this->VERSION = sprintf("Whois2.php v%s:%s", $this->CODE_VERSION, $this->DATA_VERSION);
// If domain to query was not set
if(!isSet($query)) {
// Configure to use default whois server
$this->Query["server"] = $this->NSI_REGISTRY;
return;
}
// Set domain to query in query array
$this->Query["string"] = strtolower($query);
// Determine top level whois server
$tld = $this->GetTld($this->Query["string"]);
if($tld) {
// If found, set tld and whois server in query array
$this->Query["server"] = $this->DATA[$tld][0];
$this->Query["tld"] = $tld;
// If a handler exists for the tld
if(isSet($this->DATA[$tld][1])) {
// Set fle/handler in query array
$handler = $this->DATA[$tld][1];
$this->Query["file"] = sprintf("%s.whois", $handler);
$this->Query["handler"] = $handler;
2002-10-11 12:16:06 +00:00
}
return;
2002-10-11 12:16:06 +00:00
}
2002-10-08 16:49:26 +00:00
// If top level domain not known, check availability in DNS
if(checkdnsrr($query, "A")) {
// Prepare to do lookup via the 'ipw' handler
$ip = gethostbyname($query);
$this->Query["server"] = "whois.arin.net";
$this->Query["host_ip"] = $ip;
$this->Query["file"] = "ipw.whois";
$this->Query["handler"] = "ipw";
$this->Query["string"] = $ip;
$this->Query["tld"] = "ipw";
// If query was an IP, determine the hostname
if ($query==$ip)
$this->Query["host_name"] = gethostbyaddr($ip);
else
$this->Query["host_name"] = $query;
return;
}
2002-10-08 16:49:26 +00:00
// If tld not known, and domain not in DNS, return error
unset($this->Query["server"]);
$this->Query["status"] = -1;
$this->Query["errstr"][] = $this->Query["string"]." domain is not supported";
return;
}
2002-10-08 16:49:26 +00:00
/*
* Open a socket to the whois server.
*
* Returns a socket connection pointer on success, or -1 on failure.
*/
2002-10-11 12:16:06 +00:00
function Connect () {
// Fail if server not set
if(!isSet($this->Query["server"]))
return(-1);
// Enter connection attempt loop
2002-10-11 12:16:06 +00:00
$server = $this->Query["server"];
$retry = 0;
while($retry <= $this->RETRY) {
// Set query status
$this->Query["status"] = "ready";
// Connect to whois port
$ptr = fsockopen($server, $this->PORT);
if($ptr > 0) {
$this->Query["status"]="ok";
return($ptr);
}
// Failed this attempt
$this->Query["status"] = "error";
$retry++;
// Sleep before retrying
sleep($this->SLEEP);
}
// If we get this far, it hasn't worked
2002-10-11 12:16:06 +00:00
return(-1);
}
2002-10-08 16:49:26 +00:00
/*
* Post-process result with handler class. On success, returns the result
* from the handler. On failure, returns passed in result unaltered.
*/
function Process (&$result) {
// Disable error reporting temporarily (probably not necessary)
2002-10-11 12:16:06 +00:00
$old_val = error_reporting(FALSE);
// If the handler has not already been included somehow, include it now
2002-10-11 12:16:06 +00:00
$HANDLER_FLAG = sprintf("__%s_HANDLER__", strtoupper($this->Query["handler"]));
if(!defined($HANDLER_FLAG))
include($this->Query["file"]);
// Restore error reporting
2002-10-11 12:16:06 +00:00
error_reporting($old_val);
// If the handler has still not been included, append to query errors list and return
if(!defined($HANDLER_FLAG)) {
$this->Query["errstr"][] = "Can't find ".$this->Query["tld"]." handler: ".$this->Query["file"];
return($result);
2002-10-11 12:16:06 +00:00
}
2002-10-08 16:49:26 +00:00
// Pass result to handler
$object = $this->Query["handler"];
$handler = new $object($result, $this->Query);
// If handler returned an error, append it to the query errors list
if(isSet($handler->Query["errstr"]))
$this->Query["errstr"][] = $handler->Query["errstr"];
// Return the result
return($handler->result);
2002-10-08 16:49:26 +00:00
}
2002-10-11 12:16:06 +00:00
/*
* Perform lookup. Returns an array. The 'rawdata' element contains an
* array of lines gathered from the whois query. If a top level domain
* handler class was found for the domain, other elements will have been
* populated too.
*/
function Lookup ($query = "") {
// If domain to query passed in, use it, otherwise use domain from initialisation
2002-10-11 12:16:06 +00:00
$string = !empty($query) ? $query : $this->Query["string"];
// If the '.cx' whois server is broken, return an error now (saves attempting and timing out)
if($this->HACKS["cx_is_broken"] && $this->Query["tld"] == "cx") {
$this->Query["errstr"][] = ".cx doesn't work. Turn off HACKS[\"cx_is_broken\"] if ".$this->Query["server"]." finally got fixed.";
2002-10-11 12:16:06 +00:00
return("");
}
// Connect to whois server, or return if failed
$ptr = $this->Connect();
if($ptr < 0) {
$this->Query["status"] = -1;
$this->Query["errstr"][] = "Connect failed to: ".$this->Query["server"];
2002-10-11 12:16:06 +00:00
return(array());
2002-10-08 16:49:26 +00:00
}
// Determining on server/hacks in operation, send appropriate request
if(($this->Query["server"] == $this->NSI_REGISTRY || $this->Query["server"] == $this->NSI_REGISTRAR) && $this->HACKS["nsi_force_dom"]) {
fputs($ptr, sprintf("dom %s\r\n", trim($string)));
}
elseif($this->Query["server"] == $this->HACKS["force_slash_e"]) {
fputs($ptr, sprintf("%s/e\r\n", trim($string)));
}
else {
fputs($ptr, sprintf("%s\r\n", trim($string)));
}
// Prepare to receive result
$raw = "";
$output = array();
while(!feof($ptr)) {
// If a buffer size is set, fetch line-by-line into an array
if($this->BUFFER)
$output[] = fgets($ptr, $this->BUFFER);
// If not, fetch char-by-char into a string
else
$raw .= fgetc($ptr);
}
// If captured char-by-char, convert to an array of lines
if(!$this->BUFFER)
$output = explode("\n", $raw);
// Drop empty last line
unset($output[count($output)-1]);
// Create result and set 'rawdata'
$result = array();
$result['rawdata'] = $output;
// If we have a handler, post-process it with that
if(isSet($this->DATA[$this->Query["tld"]][1]))
$result = $this->Process($result);
return($result);
2002-10-08 16:49:26 +00:00
}
/*
* Determine top-level whois server for a given domain
*/
2002-10-11 12:16:06 +00:00
function GetTld ($domain) {
// Loop through server/tld list
2002-10-11 12:16:06 +00:00
reset($this->DATA);
$curr_match = "";
while(list($tld, $param) = each($this->DATA)) {
// If the tld matches the end of the given domain
2002-10-11 12:16:06 +00:00
if(eregi("\.$tld$", $domain)) {
// If we have already found a match
2002-10-11 12:16:06 +00:00
if(!empty($curr_match)) {
// FIXME: we assume we'll never support a 3rd
// level registry. If we do, this breaks.
$curr_match = strchr($tld,".") ? $tld : $curr_match;
2002-10-08 16:49:26 +00:00
}
else
$curr_match = $tld;
}
2002-10-08 16:49:26 +00:00
}
2002-10-11 12:16:06 +00:00
return($curr_match);
2002-10-08 16:49:26 +00:00
}
}
?>