Учел критику (всем спасибо).
ЗЫ Новый метод _getTimeByDate - *дохуя эпитетов* говнокода.
<?php
/**
* Класс whois
*
* @author Matvey Vasenin
* @version 0.8 Betta
*/
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();
}
/**
* Получение полного ответа whois.
*
* @var bool $html Если true, то переводы строки будут в виде <br />, иначе \n
*/
public function getText ($html = false) {
return $html ? nl2br($this->_sInfo) : $this->_sInfo;
}
/**
* Получение массива парамеров домена
*
* @var string $element Если не задан, то вернет весь массив параметров. Если сюда ввести существующий параметр, то вернет параметр
*/
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;
}
/**
* Возвращает дату регистрации в формате timestamp
*/
public function getRegDate() {
return $this->_getTimeByDate($this->_getPart('created'));
}
/**
* Возвращает дату следующего продления в формате timestamp
*/
public function getExpireDate() {
return $this->_getTimeByDate($this->_getPart('expire'));
}
/**
* Возвращает ns домена
*/
public function getNS() {
return $this->_getPart('ns');
}
/**
* Возвращает тело домена
*/
public function getName() {
return $this->_domainName;
}
/**
* Возвращает зону домена
*/
public function getZone() {
return $this->_tldName;
}
/**
* Возвращает домен
*/
public function getDomain() {
return $this->_domainName . '.' . $this->_tldName;
}
private function _getTimeByDate ($date) {
if ($this->conf[$this->_tldName]['date']) {
$format = array(
stripos($this->conf[$this->_tldName]['date'], 'y') => 'y',
stripos($this->conf[$this->_tldName]['date'], 'm') => 'm',
stripos($this->conf[$this->_tldName]['date'], 'd') => 'd',
);
ksort($format);
$regexp = str_ireplace(array('y', 'm', 'd', '.'), array('([0-9]{2,4})', '([0-9]{1,2})', '([0-9]{1,2})', '\.'), '#' . $this->conf[$this->_tldName]['date'] . '#i');
preg_match ($regexp, $date, $aDate);
$i = 0;
foreach ($format as $key => $value) {
$i++;
switch ($value) {
case 'y':
$year = $aDate[$i];
break;
case 'm':
$month = $aDate[$i];
break;
case 'd':
$day = $aDate[$i];
break;
}
}
return strtotime("$year/$month/$day");
} else {
return strtotime($date);
}
}
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])) {
$lowerInfo = strtolower($aInfo[0]);
if (!empty($aInfo[1])) {
if (!isset($this->_aInfo[$lowerInfo])) {
$this->_aInfo[$lowerInfo] = $aInfo[1];
} else {
if(is_array($this->_aInfo[$lowerInfo])) {
$this->_aInfo[$lowerInfo][] = $aInfo[1];
} else {
$this->_aInfo[$lowerInfo] = array($this->_aInfo[$lowerInfo], $aInfo[1]);
}
}
} else {
do {
$i++;
$string = $answer[$i];
if(empty($string) || strpos($string, ':') !== false) break;
$this->_sInfo .= "$string\r\n";
$this->_aInfo[$lowerInfo][] = $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',
'date' => 'y.m.d' // поддеживается только y - год, m - месяй, d - день
),
'su' => array(
'server' => array('whois.ripn.ru', 'No entries found'),
'created' => 'created',
'expire' => 'paid-till',
'ns' => 'nserver',
'date' => 'y.m.d'
),
'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'
),
);