<commit_before><?php
/**
 * Date: 16.05.2016
 * Time: 16:48
 */

namespace Kofel\Whois;


use Kofel\Whois\Exception\WhoisServerConnectionException;

class WhoisServer
{
    const WHOIS_PORT = 43;

    private $host;

    private $tld;

    /**
     * WhoisServer constructor.
     * @param $host
     * @param $tld
     */
    public function __construct($host, $tld)
    {
        $this->host = $host;
        $this->tld = $tld;
    }

    /** @throws WhoisServerConnectionException @return string */
    public function query($sld)
    {
        if (in_array($this->tld, ['com', 'net'])) {
            $serversQuery = $this->queryHost($this->host, '=' . $sld);

            $match = [];
            if (!preg_match('/Whois Server: ([^\\s]+)/', $serversQuery, $match)) {
                return $serversQuery;
            }

            $host = $match[1];
            return $this->queryHost($host, $sld);
        }
        else {
            return $this->queryHost($this->host, $sld);
        }
    }

    protected function queryHost($host, $sld)
    {
        $socket = fsockopen($host, self::WHOIS_PORT);
        if (false === $socket) {
            throw new WhoisServerConnectionException(sprintf(
                'Cannot establish connection to "%s".',
                $this->host
            ));
        }

        fwrite($socket, $sld);
        fwrite($socket, "\r\n");

        $buffer = '';
        while(!feof($socket)) {
            $buffer .= fgets($socket, 128);
        }

        fclose($socket);

        $encoding = mb_detect_encoding($buffer, "UTF-8, ISO-8859-1, ISO-8859-15", true);
        $buffer = mb_convert_encoding($buffer, "UTF-8", $encoding);
        $buffer = htmlspecialchars($buffer, ENT_COMPAT, "UTF-8", true);

        return $buffer;
    }
}<commit_msg>Added timeout option for WhoisServer<commit_after><?php
/**
 * Date: 16.05.2016
 * Time: 16:48
 */

namespace Kofel\Whois;


use Kofel\Whois\Exception\WhoisServerConnectionException;

class WhoisServer
{
    const WHOIS_PORT = 43;

    private $host;

    private $tld;

    /** @var int */
    private $timeout = 5;

    /**
     * WhoisServer constructor.
     * @param $host
     * @param $tld
     */
    public function __construct($host, $tld)
    {
        $this->host = $host;
        $this->tld = $tld;
    }

    /** @throws WhoisServerConnectionException @return string */
    public function query($sld)
    {
        if (in_array($this->tld, ['com', 'net'])) {
            $serversQuery = $this->queryHost($this->host, '=' . $sld);

            $match = [];
            if (!preg_match('/Whois Server: ([^\\s]+)/', $serversQuery, $match)) {
                return $serversQuery;
            }

            $host = $match[1];
            return $this->queryHost($host, $sld);
        }
        else {
            return $this->queryHost($this->host, $sld);
        }
    }

    protected function queryHost($host, $sld)
    {
        $socket = fsockopen($host, self::WHOIS_PORT);
        socket_set_timeout($socket, $this->timeout);
        if (false === $socket) {
            throw new WhoisServerConnectionException(sprintf(
                'Cannot establish connection to "%s".',
                $this->host
            ));
        }

        fwrite($socket, $sld);
        fwrite($socket, "\r\n");

        $buffer = '';
        while(!feof($socket)) {
            $buffer .= fgets($socket, 128);
        }

        fclose($socket);

        $encoding = mb_detect_encoding($buffer, "UTF-8, ISO-8859-1, ISO-8859-15", true);
        $buffer = mb_convert_encoding($buffer, "UTF-8", $encoding);
        $buffer = htmlspecialchars($buffer, ENT_COMPAT, "UTF-8", true);

        return $buffer;
    }

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

    /**
     * @param int $timeout
     */
    public function setTimeout($timeout)
    {
        $this->timeout = $timeout;
    }
}
{'fraction_non_alphanumeric': 0.1599493029150824, 'fraction_numerical': 0.016476552598225603, 'mean_word_length': 1.9121771217712178, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7225820', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1225, 'n_words': 248}