You are here

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

Same name and namespace in other branches
  1. 9.1.x src/lib/Cleantalk/Antispam/Cleantalk.php \Cleantalk\Antispam\Cleantalk::get_servers_ip()

Function DNS request

Parameters

$host:

Return value

array

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

File

src/lib/Cleantalk/Antispam/Cleantalk.php, line 493

Class

Cleantalk
Cleantalk class create request

Namespace

Cleantalk\Antispam

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;
}