<?php
class whois {
protected $tldName;
protected $domainName;
protected $aInfo = array();
protected $sInfo = '';
private $conf = array();
public function __construct ($domain, $conf) {
$this->conf = $conf;
$domain = $this->validate($domain);
$this->parseDomain($domain);
$this->getAnswer();
}
public function getText ($html = false) {
return $html ? nl2bt($this->sInfo) : $this->sInfo;
}
public function getInfo ($element = false) {
return $element ? $this->aInfo[strtolower($element)] : $this->aInfo;
}
public function isAvailable() {
return (stripos($this->sInfo, $this->conf[$this->tldName]['server'][1]) === false) ? false : true;
}
public function getRegDate() {
return $this->getPart('created');
}
public function getExpireDate() {
return $this->getPart('expire');
}
public function getNS() {
return $this->getPart('ns');
}
private function getPart($type) {
$info = $this->aInfo[strtolower($this->conf[$this->tldName][$type])];
if (!$info && $this->conf[$this->tldName]['regexp'][$type]) {
preg_match($this->conf[$this->tldName]['regexp'][$type], $this->sInfo, $info);
$info = $info[1];
}
return $info;
}
private function parseDomain($domain) {
$this->tldName = explode(".", $domain);
$this->domainName = array_shift($this->tldName);
$this->tldName = implode('.', $this->tldName);
}
protected function getAnswer() {
$server = $this->conf[$this->tldName]['server'][0];
$domain = $this->domainName . '.' . $this->tldName;
if ($this->conf[$this->tldName]['server'][2]) {
$this->getDoubleAnswer($domain, $server);
} else {
$this->getSimpleAnswer($domain, $server);
}
}
protected function getSimpleAnswer($domain, $server) {
$fp = @fsockopen($server, 43);
if (!$fp) throw new Exception ('Whois server not available.');
fputs($fp, "$domain\r\n");
$this->sInfo = '';
$answer = array();
while (!feof($fp)) {
$answer[] = trim(fgets($fp, 128));
}
for ($i = 0;$i<count($answer);$i++) {
$sInfo = $answer[$i];
$this->sInfo .= "$sInfo\r\n";
$aInfo = array_map('trim', explode(':', $sInfo, 2));
if (count($aInfo) == 2 && preg_match('/^[-a-z ]+$/i', $aInfo[0])) {
if (!empty($aInfo[1])) {
if (!isset($this->aInfo[strtolower($aInfo[0])])) {
$this->aInfo[strtolower($aInfo[0])] = $aInfo[1];
} else {
if(is_array($this->aInfo[strtolower($aInfo[0])])) {
$this->aInfo[strtolower($aInfo[0])][] = $aInfo[1];
} else {
$this->aInfo[strtolower($aInfo[0])] = array($this->aInfo[strtolower($aInfo[0])], $aInfo[1]);
}
}
} else {
do {
$i++;
$string = $answer[$i];
if(empty($string) || strpos($string, ':') !== false) break;
$this->sInfo .= "$string\r\n";
$this->aInfo[strtolower($aInfo[0])][] = $string;
} while (!empty($string) && strpos($string, ':') === false);
$i–;
}
}
}
}
protected function getDoubleAnswer($domain, $server) {
$fp = @fsockopen($server, 43);
if (!$fp) throw new Exception ('Whois server not available.');
fputs($fp, "$domain\r\n");
$this->sInfo = '';
while (!feof($fp)) {
$sInfo = trim(fgets($fp, 128));
$this->sInfo .= "$sInfo\r\n";
$aInfo = array_map('trim', explode(':', $sInfo, 2));
if (strtolower($aInfo[0]) == 'whois server') {
$doubleServer = $aInfo[1];
}
}
$doubleServer = $doubleServer ? $doubleServer : 'whois.networksolutions.com';
$this->getSimpleAnswer($domain, $doubleServer);
}
protected function validate ($domain) {
preg_match ('#^(?:(?:http|https|ftp|svn)?://)?([a-z0-9\.\-]+)(?:/(?:.*))?$#i', $domain, $domain);
if (!$domain) throw new Exception('Not Valid Domain');
$domain = $domain[1];
if (in_array('-', array($domain{1}, substr($domain, -1))) && strpos($domain, '–') !== false) throw new Exception('Not Valid Domain');
return $domain;
}
}
//Настройки Хуиза
$whoisConf = array (
'ru' => array(
'server' => array('whois.ripn.ru', 'No entries found'),
'created' => 'created',
'expire' => 'paid-till',
'ns' => 'nserver'
),
'su' => array(
'server' => array('whois.ripn.ru', 'No entries found'),
'created' => 'created',
'expire' => 'paid-till',
'ns' => 'nserver'
),
'com' => array(
'server' => array('whois.crsnic.net', 'No match', true),
'created' => 'Creation date',
'expire' => 'Expiration Date',
'ns' => 'Domain servers in listed order',
'regexp' => array(
'created' => '/Record created on ([0-9]{2}-[a-z]{3}-[0-9]{4})/i',
'expire' => '/Record expires on ([0-9]{2}-[a-z]{3}-[0-9]{4})/i',
)
),
'net' => array(
'server' => array('whois.crsnic.net', 'No match', true),
'created' => 'Creation Date',
'expire' => 'Expiration Date',
'ns' => 'Domain servers in listed order',
'regexp' => array(
'created' => '/Record created on ([0-9]{2}-[a-z]{3}-[0-9]{4})/i',
'expire' => '/Record expires on ([0-9]{2}-[a-z]{3}-[0-9]{4})/i',
)
),
'org' => array(
'server' => array('whois.pir.org', 'NOT FOUND'),
'created' => 'Created On',
'expire' => 'Expiration Date',
'ns' => 'Name Server'
),
'biz' => array(
'server' => array('whois.biz', 'Not found'),
'created' => 'Name Server:',
'expire' => 'Expiration Date',
'ns' => 'Name Server'
),
'info' => array(
'server' => array('whois.afilias.info', 'Not found'),
'created' => 'Created On',
'expire' => 'Expiration Date',
'ns' => 'Name Server'
),
'mobi' => array(
'server' => array('whois.dotmobiregistry.net', 'NOT FOUND'),
'created' => 'Created On',
'expire' => 'Expiration Date',
'ns' => 'Name Server'
),
'name' => array(
'server' => array('whois.nic.name', 'No match'),
'created' => 'Created On',
'expire' => 'Expires On',
'ns' => 'Name Server'
),
'tv' => array(
'server' => array('whois.nic.tv', 'No match'),
'created' => 'Creation date',
'expire' => 'Expiration date',
'ns' => 'Name Server'
),
'cn' => array(
'server' => array('whois.cnnic.net.cn', 'No entries found'),
'created' => 'Registration Date',
'expire' => 'Expiration Date',
'ns' => 'Name Server'
),
'ki' => array(
'server' => array('whois.nic.ki', 'NOT FOUND'),
'created' => 'Created',
'expire' => 'Expires',
'ns' => 'Name Server'
),
'in' => array(
'server' => array('whois.inregistry.in', 'NOT FOUND'),
'created' => 'Created On',
'expire' => 'Expiration Date',
'ns' => 'Name Server'
),
'mn' => array(
'server' => array('whois2.afilias-grs.net', 'NOT FOUND'),
'created' => 'Created On',
'expire' => 'Expiration Date',
'ns' => 'Name Server'
),
'cc' => array(
'server' => array('whois.nic.cc', 'No match'),
'created' => 'Creation Date',
'expire' => 'Expiration Date',
'ns' => 'Name Server'
),
'ws' => array(
'server' => array('whois.worldsite.ws', 'No match for'),
'created' => 'Domain Created',
'expire' => 'Domain Currently Expires',
'ns' => 'Name Server'
),
'asia' => array(
'server' => array('whois.nic.asia', 'NOT FOUND'),
'created' => 'Domain Create Date',
'expire' => 'Domain Expiration Date',
'ns' => 'Nameservers'
),
);
буду благоарен за кодревью =)