You are here

public function Cleantalk::get_servers_ip in Anti Spam by CleanTalk 8

Function DNS request

Parameters

$host:

Return value

array

1 call to Cleantalk::get_servers_ip()
Cleantalk::httpRequest in src/Cleantalk.php
httpRequest

File

src/Cleantalk.php, line 430

Class

Cleantalk
Cleantalk class create request

Namespace

Drupal\cleantalk

Code

public function get_servers_ip($host) {
  $response = null;
  if (!isset($host)) {
    return $response;
  }
  if (function_exists('dns_get_record')) {
    $records = dns_get_record($host, DNS_A);
    if ($records !== FALSE) {
      foreach ($records as $server) {
        $response[] = $server;
      }
    }
  }
  if (count($response) == 0 && function_exists('gethostbynamel')) {
    $records = gethostbynamel($host);
    if ($records !== FALSE) {
      foreach ($records as $server) {
        $response[] = array(
          "ip" => $server,
          "host" => $host,
          "ttl" => $this->server_ttl,
        );
      }
    }
  }
  if (count($response) == 0) {
    $response[] = array(
      "ip" => null,
      "host" => $host,
      "ttl" => $this->server_ttl,
    );
  }
  else {

    // $i - to resolve collisions with localhost
    $i = 0;
    $r_temp = null;
    $fast_server_found = false;
    foreach ($response as $server) {

      // Do not test servers because fast work server found
      if ($fast_server_found) {
        $ping = $this->min_server_timeout;
      }
      else {
        $ping = $this
          ->httpPing($server['ip']);
        $ping = $ping * 1000;
      }

      // -1 server is down, skips not reachable server
      if ($ping != -1) {
        $r_temp[$ping + $i] = $server;
      }
      $i++;
      if ($ping < $this->min_server_timeout) {
        $fast_server_found = true;
      }
    }
    if (count($r_temp)) {
      ksort($r_temp);
      $response = $r_temp;
    }
  }
  return $response;
}