You are here

public static function Helper::ip_get in Anti Spam by CleanTalk 8.3

Same name in this branch
  1. 8.3 src/lib/Cleantalk/Common/Helper.php \Drupal\cleantalk\lib\Cleantalk\Common\Helper::ip_get()
  2. 8.3 src/lib/Cleantalk/ApbctDrupal/Helper.php \Drupal\cleantalk\lib\Cleantalk\ApbctDrupal\Helper::ip_get()
2 calls to Helper::ip_get()
Helper::ip_get in src/lib/Cleantalk/ApbctDrupal/Helper.php
SFW::ip_get in src/lib/Cleantalk/Antispam/SFW.php
1 method overrides Helper::ip_get()
Helper::ip_get in src/lib/Cleantalk/ApbctDrupal/Helper.php

File

src/lib/Cleantalk/Common/Helper.php, line 73

Class

Helper
Cleantalk's hepler class

Namespace

Drupal\cleantalk\lib\Cleantalk\Common

Code

public static function ip_get($ips_input = array(
  'real',
  'remote_addr',
  'x_forwarded_for',
  'x_real_ip',
  'cloud_flare',
), $v4_only = true) {
  $ips = array();
  foreach ($ips_input as $ip_type) {
    $ips[$ip_type] = '';
  }
  unset($ip_type);
  $headers = function_exists('apache_request_headers') ? apache_request_headers() : self::apache_request_headers();

  // REMOTE_ADDR
  if (isset($ips['remote_addr'])) {
    $ips['remote_addr'] = $_SERVER['REMOTE_ADDR'];
  }

  // X-Forwarded-For
  if (isset($ips['x_forwarded_for'])) {
    if (isset($headers['X-Forwarded-For'])) {
      $tmp = explode(",", trim($headers['X-Forwarded-For']));
      $ips['x_forwarded_for'] = trim($tmp[0]);
    }
  }

  // X-Real-Ip
  if (isset($ips['x_real_ip'])) {
    if (isset($headers['X-Real-Ip'])) {
      $tmp = explode(",", trim($headers['X-Real-Ip']));
      $ips['x_real_ip'] = trim($tmp[0]);
    }
  }

  // Cloud Flare
  if (isset($ips['cloud_flare'])) {
    if (isset($headers['Cf-Connecting-Ip'])) {
      if (self::ip_mask_match($ips['remote_addr'], self::$cdn_pool['cloud_flare']['ipv4'])) {
        $ips['cloud_flare'] = $headers['Cf-Connecting-Ip'];
      }
    }
  }

  // Getting real IP from REMOTE_ADDR or Cf_Connecting_Ip if set or from (X-Forwarded-For, X-Real-Ip) if REMOTE_ADDR is local.
  if (isset($ips['real'])) {
    $ips['real'] = $_SERVER['REMOTE_ADDR'];

    // Cloud Flare
    if (isset($headers['Cf-Connecting-Ip'])) {
      if (self::ip_mask_match($ips['real'], self::$cdn_pool['cloud_flare']['ipv4'])) {
        $ips['real'] = $headers['Cf-Connecting-Ip'];
      }

      // Incapsula proxy
    }
    elseif (isset($headers['Incap-Client-Ip'])) {
      $ips['real'] = $headers['Incap-Client-Ip'];

      // Private networks. Looking for X-Forwarded-For and X-Real-Ip
    }
    elseif (self::ip_mask_match($ips['real'], self::$private_networks)) {
      if (isset($headers['X-Forwarded-For'])) {
        $tmp = explode(",", trim($headers['X-Forwarded-For']));
        $ips['real'] = trim($tmp[0]);
      }
      elseif (isset($headers['X-Real-Ip'])) {
        $tmp = explode(",", trim($headers['X-Real-Ip']));
        $ips['real'] = trim($tmp[0]);
      }
    }
  }

  // Validating IPs
  $result = array();
  foreach ($ips as $key => $ip) {
    if ($v4_only) {
      if (self::ip_validate($ip) == 'v4') {
        $result[$key] = $ip;
      }
    }
    else {
      if (self::ip_validate($ip)) {
        $result[$key] = $ip;
      }
    }
  }
  $result = array_unique($result);
  return count($ips_input) > 1 ? $result : (reset($result) !== false ? reset($result) : null);
}