Skip to content

Commit

Permalink
Fixes regru#28: Don't make two queries
Browse files Browse the repository at this point in the history
Signed-off-by: NabiKAZ <[email protected]>
  • Loading branch information
shapito27 committed Oct 24, 2020
1 parent 7103589 commit 36b7ce4
Show file tree
Hide file tree
Showing 3 changed files with 836 additions and 55 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
],
"require": {
"php": ">=5.3.2",
"php": ">=7.1",
"ext-curl": "*"
},
"autoload": {
Expand Down
76 changes: 62 additions & 14 deletions src/Phois/Whois/Whois.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ class Whois
private $subDomain;

private $servers;

private $whoisInfo;

private $timeout = 20;

//socket options
private $socErrno;
private $socErrstr;

/**
* @param string $domain full domain name (without trailing dot)
Expand All @@ -20,7 +28,7 @@ public function __construct($domain)
$this->domain = strtolower($domain);
// check $domain syntax and split full domain name on subdomain and TLDs
if (
preg_match('/^([\p{L}\d\-]+)\.((?:[\p{L}\-]+\.?)+)$/ui', $this->domain, $matches)
preg_match('/^([\p{L}\d\-]+)\.((?:[\p{L}\d\-]+\.?)+)$/ui', $this->domain, $matches)
|| preg_match('/^(xn\-\-[\p{L}\d\-]+)\.(xn\-\-(?:[a-z\d-]+\.?1?)+)$/ui', $this->domain, $matches)
) {
$this->subDomain = $matches[1];
Expand All @@ -39,21 +47,24 @@ public function __construct($domain)
*/
public function info()
{
if ($this->whoisInfo != '')
return $this->whoisInfo;

if ($this->isValid()) {
$whois_server = $this->servers[$this->TLDs][0];

// If TLDs have been found
if ($whois_server != '') {

// if whois server serve replay over HTTP protocol instead of WHOIS protocol
// if whois server serve reply over HTTP protocol instead of WHOIS protocol
if (preg_match("/^https?:\/\//i", $whois_server)) {

// curl session to get whois reposnse
// curl session to get whois response
$ch = curl_init();
$url = $whois_server . $this->subDomain . '.' . $this->TLDs;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
Expand All @@ -70,10 +81,13 @@ public function info()
} else {

// Getting whois information
$fp = fsockopen($whois_server, 43);
$fp = fsockopen($whois_server, 43, $this->socErrno, $this->socErrstr, $this->timeout);
if (!$fp) {
return "Connection error!";
return "Connection error! ".$this->socErrno.":".$this->socErrstr;
}
stream_set_blocking($fp, true);
stream_set_timeout($fp, $this->timeout);
$info = stream_get_meta_data($fp);

$dom = $this->subDomain . '.' . $this->TLDs;
fputs($fp, "$dom\r\n");
Expand All @@ -82,8 +96,8 @@ public function info()
$string = '';

// Checking whois server for .com and .net
if ($this->TLDs == 'com' || $this->TLDs == 'net') {
while (!feof($fp)) {
if ($this->TLDs === 'com' || $this->TLDs === 'net') {
while ( (!feof($fp)) && (!$info['timed_out']) ) {
$line = trim(fgets($fp, 128));

$string .= $line;
Expand All @@ -93,13 +107,18 @@ public function info()
if (strtolower($lineArr[0]) == 'whois server') {
$whois_server = trim($lineArr[1]);
}
$info = stream_get_meta_data($fp);
}
// Getting whois information
$fp = fsockopen($whois_server, 43);
$fp = fsockopen($whois_server, 43, $this->socErrno, $this->socErrstr, $this->timeout);
if (!$fp) {
return "Connection error!";
return "Connection error! ".$this->socErrno.":".$this->socErrstr;
}

stream_set_blocking($fp, TRUE);
stream_set_timeout($fp,$this->timeout);
$info = stream_get_meta_data($fp);

$dom = $this->subDomain . '.' . $this->TLDs;
fputs($fp, "$dom\r\n");

Expand All @@ -112,8 +131,9 @@ public function info()

// Checking for other tld's
} else {
while (!feof($fp)) {
while ( (!feof($fp)) && (!$info['timed_out']) ) {
$string .= fgets($fp, 128);
$info = stream_get_meta_data($fp);
}
}
fclose($fp);
Expand All @@ -122,7 +142,8 @@ public function info()
$string_encoding = mb_detect_encoding($string, "UTF-8, ISO-8859-1, ISO-8859-15", true);
$string_utf8 = mb_convert_encoding($string, "UTF-8", $string_encoding);

return htmlspecialchars($string_utf8, ENT_COMPAT, "UTF-8", true);
$this->whoisInfo = htmlspecialchars($string_utf8, ENT_COMPAT, "UTF-8", true);
return $this->whoisInfo;
} else {
return "No whois server for this tld in list!";
}
Expand Down Expand Up @@ -213,6 +234,14 @@ public function data()
return $result;
}

/**
* @return bool
*/
public function isServerDefined(): bool
{
return isset($this->servers[$this->TLDs]);
}

public function htmlInfo()
{
return nl2br($this->info());
Expand Down Expand Up @@ -247,7 +276,10 @@ public function getSubDomain()
*/
public function isAvailable()
{
$whois_string = $this->info();
if ($this->whoisInfo == '')
$whois_string = $this->info();
else
$whois_string = $this->whoisInfo;
$not_found_string = '';
if (isset($this->servers[$this->TLDs][1])) {
$not_found_string = $this->servers[$this->TLDs][1];
Expand Down Expand Up @@ -280,7 +312,7 @@ public function isValid()
) {
$tmp_domain = strtolower($this->subDomain);
if (
preg_match("/^[a-z0-9\-]{3,}$/", $tmp_domain)
preg_match("/^[a-z0-9\-]{1,}$/", $tmp_domain)
&& !preg_match("/^-|-$/", $tmp_domain) //&& !preg_match("/--/", $tmp_domain)
) {
return true;
Expand All @@ -289,4 +321,20 @@ public function isValid()

return false;
}

/**
* @return int
*/
public function getTimeout(): int
{
return $this->timeout;
}

/**
* @param int $timeout
*/
public function setTimeout(int $timeout): void
{
$this->timeout = $timeout;
}
}
Loading

0 comments on commit 36b7ce4

Please sign in to comment.