/** * Queries the WHOIS server for the given domain name. * * @param Domain|string $domain Domain name. * * @return string Result of the WHOIS server. * * @throws WhoisException when the TLD does not exist. * @throws WhoisException when the connection to the WHOIS server failed. * @throws QuotaExceededException if the quota for the WHOIS server is exceeded. */ public function query($domain) { if (false === $domain instanceof Domain) { $domain = Domain::create($domain); } $tld = $domain->getTld(); try { $data = $this->data->getByTld($tld); } catch (DataException $e) { throw new WhoisException(sprintf('The TLD "%s" does not exist.', $tld), 0, $e); } $connection = $this->factory->createStreamConnection(); try { $connection->open($data['whoisServer'], 43); $connection->write(sprintf("%s\r\n", $domain->getDomainName())); $whois = $connection->read(); $connection->close(); } catch (ConnectionException $e) { throw new WhoisException(sprintf('Could not query WHOIS for "%s".', $domain->getDomainName()), 0, $e); } if (0 === strlen(trim($whois))) { throw new WhoisException(sprintf('Retrieved empty WHOIS for "%s".', $domain->getDomainName())); } if (true === isset($data['patterns']['quotaExceeded']) && preg_match($data['patterns']['quotaExceeded'], $whois)) { throw new QuotaExceededException(sprintf('Quota exceeded for WHOIS server "%s".', $data['whoisServer'])); } if (true === isset($data['patterns']['waitPeriod']) && true === isset($data['waitPeriod']) && preg_match($data['patterns']['waitPeriod'], $whois)) { sleep($data['waitPeriod']); return $this->query($domain); } return $whois; }
/** * Returns if the given domain is available. * * @param Domain|string $domain Domain. * * @return boolean `true` if the domain is available, `false` if not. * * @throws AvailabilityException when no pattern exists for the given TLD. */ public function isAvailable($domain) { if (false === $domain instanceof Domain) { $domain = Domain::create($domain); } $data = $this->data->getByTld($domain->getTld()); if (false === isset($data['patterns']['notRegistered'])) { throw new AvailabilityException(sprintf('No pattern exists to check availability of %s domains.', $domain->getTld())); } $this->lastWhoisResult = $this->whoisClient->query($domain); if (preg_match($data['patterns']['notRegistered'], $this->lastWhoisResult)) { return true; } return false; }
/** * @test * @covers Cocur\Domain\Domain::getTld() */ public function getTld() { $this->domain->setDomainName('florian.ec'); $this->assertEquals('ec', $this->domain->getTld()); }