phpWhois.org/src/whois.ip.php

289 lines
7.4 KiB
PHP
Raw Permalink 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
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.
2005-08-26 06:46:49 +00:00
*/
2005-08-26 06:46:49 +00:00
if (!defined('__IP_HANDLER__'))
define('__IP_HANDLER__', 1);
require_once('whois.ip.lib.php');
2005-08-26 06:46:49 +00:00
class ip_handler extends WhoisClient
{
// Deep whois ?
var $deep_whois = true;
var $HANDLER_VERSION = '1.0';
var $REGISTRARS = array(
2005-08-26 06:46:49 +00:00
'European Regional Internet Registry/RIPE NCC' => 'whois.ripe.net',
'RIPE Network Coordination Centre' => 'whois.ripe.net',
'Asia Pacific Network Information Center' => 'whois.apnic.net',
2005-08-29 11:53:02 +00:00
'Asia Pacific Network Information Centre' => 'whois.apnic.net',
2005-08-26 06:46:49 +00:00
'Latin American and Caribbean IP address Regional Registry' => 'whois.lacnic.net',
'African Network Information Center' => 'whois.afrinic.net'
);
var $HANDLERS = array(
2005-08-26 06:46:49 +00:00
'whois.krnic.net' => 'krnic',
'whois.apnic.net' => 'apnic',
'whois.ripe.net' => 'ripe',
'whois.arin.net' => 'arin',
'whois.lacnic.net' => 'lacnic',
'whois.afrinic.net' => 'afrinic'
);
2010-07-20 19:59:17 +00:00
var $more_data = array(); // More queries to get more accurated data
2010-07-28 08:35:55 +00:00
var $done = array();
2005-08-26 06:46:49 +00:00
function parse($data, $query)
2005-07-27 17:59:08 +00:00
{
2006-02-03 16:30:33 +00:00
$result['regrinfo'] = array();
$result['regyinfo'] = array();
$result['regyinfo']['registrar'] = 'American Registry for Internet Numbers (ARIN)';
2006-02-03 16:30:33 +00:00
$result['rawdata'] = array();
2010-07-28 08:35:55 +00:00
if (strpos($query,'.') === false)
$result['regyinfo']['type'] = 'AS';
else
$result['regyinfo']['type'] = 'ip';
if (!$this->deep_whois) return null;
2005-07-27 17:59:08 +00:00
2005-08-26 06:46:49 +00:00
$this->Query = array();
$this->Query['server'] = 'whois.arin.net';
$this->Query['query'] = $query;
2005-08-26 06:46:49 +00:00
$rawdata = $data['rawdata'];
2007-04-06 14:26:44 +00:00
if (empty($rawdata)) return $result;
2010-07-28 08:35:55 +00:00
$presults[] = $rawdata;
$ip = ip2long($query);
$done = array();
2005-08-26 06:46:49 +00:00
2010-07-28 08:35:55 +00:00
while (count($presults) > 0)
2005-07-27 17:59:08 +00:00
{
2010-07-28 08:35:55 +00:00
$rwdata = array_shift($presults);
$found = false;
2010-07-28 08:35:55 +00:00
foreach($rwdata as $line)
{
2014-12-13 12:51:02 +00:00
// can be removed ??
2011-07-08 16:27:41 +00:00
if (!strncmp($line,'American Registry for Internet Numbers',38)) continue;
2010-07-28 08:35:55 +00:00
$p = strpos($line, '(NETBLK-');
2010-07-28 08:35:55 +00:00
if ($p === false) $p = strpos($line, '(NET-');
2014-12-13 12:51:02 +00:00
if ($p !== false && strpos($line,'Parent:') === false)
2005-08-26 06:46:49 +00:00
{
2010-07-28 08:35:55 +00:00
$net = strtok(substr($line,$p+1),') ');
list($low,$high) = explode('-',str_replace(' ','',substr($line,$p+strlen($net)+3)));
2010-07-28 08:35:55 +00:00
if (!isset($done[$net]) && $ip >= ip2long($low) && $ip <= ip2long($high))
2005-08-26 06:46:49 +00:00
{
2010-07-28 08:35:55 +00:00
$owner = substr($line,0,$p-1);
2010-07-28 08:35:55 +00:00
if (!empty($this->REGISTRARS['owner']))
{
$this->handle_rwhois($this->REGISTRARS['owner'],$query);
break 2;
}
else
2010-07-28 08:35:55 +00:00
{
$this->Query['args'] = 'n '.$net;
$presults[] = $this->GetRawData($net);
$done[$net] = 1;
}
2005-08-26 06:46:49 +00:00
}
2010-07-28 08:35:55 +00:00
$found = true;
2005-08-26 06:46:49 +00:00
}
2010-07-28 08:35:55 +00:00
}
2010-07-28 08:35:55 +00:00
if (!$found)
{
2010-07-28 08:35:55 +00:00
$this->Query['file'] = 'whois.ip.arin.php';
$this->Query['handler'] = 'arin';
$result = $this->parse_results($result,$rwdata,$query,true);
}
}
2010-07-28 08:35:55 +00:00
unset($this->Query['args']);
2005-08-30 10:14:08 +00:00
2014-12-13 12:51:02 +00:00
while (count($this->more_data) > 0)
2010-07-20 19:59:17 +00:00
{
2010-07-28 08:35:55 +00:00
$srv_data = array_shift($this->more_data);
$this->Query['server'] = $srv_data['server'];
unset($this->Query['handler']);
2010-07-20 19:59:17 +00:00
// Use original query
$rwdata = $this->GetRawData($srv_data['query']);
2006-06-12 14:25:45 +00:00
2005-08-30 10:14:08 +00:00
if (!empty($rwdata))
{
2010-07-28 08:35:55 +00:00
if (!empty($srv_data['handler']))
{
2010-07-28 08:35:55 +00:00
$this->Query['handler'] = $srv_data['handler'];
2010-07-28 08:35:55 +00:00
if (!empty($srv_data['file']))
$this->Query['file'] = $srv_data['file'];
else
$this->Query['file'] = 'whois.'.$this->Query['handler'].'.php';
}
2010-07-28 08:35:55 +00:00
$result = $this->parse_results($result,$rwdata,$query,$srv_data['reset']);
$result = $this->set_whois_info($result);
$reset = false;
2005-08-30 10:14:08 +00:00
}
}
2014-12-13 12:51:02 +00:00
// Just to be sure we get the origianl output if something goes wrong
if (empty($result['rawdata'])) $result['rawdata'] = $rawdata;
2010-07-28 08:35:55 +00:00
// Normalize nameserver fields
2010-07-28 08:35:55 +00:00
if (isset($result['regrinfo']['network']['nserver']))
2008-09-07 09:00:12 +00:00
{
if (!is_array($result['regrinfo']['network']['nserver']))
{
unset($result['regrinfo']['network']['nserver']);
}
else
$result['regrinfo']['network']['nserver'] = $this->FixNameServer($result['regrinfo']['network']['nserver']);
}
2010-07-28 08:35:55 +00:00
2005-08-26 06:46:49 +00:00
return $result;
}
2010-07-20 19:59:17 +00:00
//-----------------------------------------------------------------
2010-07-28 08:35:55 +00:00
function parse_results($result,$rwdata,$query,$reset)
{
$rwres = $this->Process($rwdata);
2010-07-28 08:35:55 +00:00
if ($result['regyinfo']['type'] == 'AS' && !empty($rwres['regrinfo']['network']))
{
$rwres['regrinfo']['AS'] = $rwres['regrinfo']['network'];
unset($rwres['regrinfo']['network']);
}
2010-07-28 08:35:55 +00:00
if ($reset)
{
$result['regrinfo'] = $rwres['regrinfo'];
$result['rawdata'] = $rwdata;
}
else
{
2010-07-28 08:35:55 +00:00
$result['rawdata'][] = '';
2010-07-28 08:35:55 +00:00
foreach ($rwdata as $line)
$result['rawdata'][] = $line;
foreach($rwres['regrinfo'] as $key => $data)
{
$result = $this->join_result($result,$key,$rwres);
}
}
if ($this->deep_whois)
{
if (isset($rwres['regrinfo']['rwhois']))
{
$this->handle_rwhois($rwres['regrinfo']['rwhois'],$query);
unset($result['regrinfo']['rwhois']);
}
else
if (!@empty($rwres['regrinfo']['owner']['organization']))
switch ($rwres['regrinfo']['owner']['organization'])
{
case 'KRNIC':
$this->handle_rwhois('whois.krnic.net',$query);
break;
2010-07-28 08:35:55 +00:00
case 'African Network Information Center':
$this->handle_rwhois('whois.afrinic.net',$query);
break;
}
}
if (!empty($rwres['regyinfo']))
$result['regyinfo'] = array_merge($result['regyinfo'],$rwres['regyinfo']);
return $result;
}
//-----------------------------------------------------------------
2010-07-20 19:59:17 +00:00
function handle_rwhois($server,$query)
{
// Avoid querying the same server twice
2010-07-20 19:59:17 +00:00
$parts = parse_url($server);
2010-07-28 08:35:55 +00:00
if (empty($parts['host']))
$host = $parts['path'];
2010-07-20 19:59:17 +00:00
else
2010-07-28 08:35:55 +00:00
$host = $parts['host'];
2010-07-20 19:59:17 +00:00
2010-07-28 08:35:55 +00:00
if (array_key_exists($host,$this->done)) return;
$q = array (
2010-07-20 19:59:17 +00:00
'query' => $query,
2010-07-28 08:35:55 +00:00
'server' => $server
2010-07-20 19:59:17 +00:00
);
2010-07-28 08:35:55 +00:00
if (isset($this->HANDLERS[$host]))
{
$q['handler'] = $this->HANDLERS[$host];
$q['file'] = sprintf('whois.ip.%s.php', $q['handler']);
$q['reset'] = true;
}
else
{
$q['handler'] = 'rwhois';
$q['reset'] = false;
unset($q['file']);
}
$this->more_data[] = $q;
$this->done[$host] = 1;
2010-07-20 19:59:17 +00:00
}
//-----------------------------------------------------------------
function join_result($result, $key, $newres)
{
if (isset($result['regrinfo'][$key]) && !array_key_exists(0,$result['regrinfo'][$key]))
{
$r = $result['regrinfo'][$key];
$result['regrinfo'][$key] = array($r);
}
2010-07-28 08:35:55 +00:00
$result['regrinfo'][$key][] = $newres['regrinfo'][$key];
return $result;
}
}
?>