You are here

public static function CleantalkHelper::ip_get in Anti Spam by CleanTalk 7.4

Same name and namespace in other branches
  1. 7.5 src/CleantalkHelper.php \CleantalkHelper::ip_get()
4 calls to CleantalkHelper::ip_get()
CleantalkFuncs::_apbct_alt_session__id__get in src/CleantalkFuncs.php
Get hash session ID
CleantalkFuncs::_cleantalk_check_spam in src/CleantalkFuncs.php
Cleantalk inner function - performs antispam checking.
CleantalkHelper::api_method__get_api_key in src/CleantalkHelper.php
* Function gets access key automatically * *
CleantalkSFW::ip_get in src/CleantalkSFW.php
1 method overrides CleantalkHelper::ip_get()
CleantalkSFW::ip_get in src/CleantalkSFW.php

File

src/CleantalkHelper.php, line 40

Class

CleantalkHelper
Cleantalk's hepler class

Code

public static function ip_get($ip_types = array(
  'real',
  'remote_addr',
  'x_forwarded_for',
  'x_real_ip',
  'cloud_flare',
), $v4_only = true) {
  $ips = array_flip($ip_types);
  $headers = function_exists('apache_request_headers') ? apache_request_headers() : self::apache_request_headers();

  // REMOTE_ADDR
  if (isset($ips['remote_addr'])) {
    $ip_type = self::ip_validate($_SERVER['REMOTE_ADDR']);
    if ($ip_type) {
      $ips['remote_addr'] = $ip_type == 'v6' ? self::ip_v6_normalize($_SERVER['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']));
      $tmp = trim($tmp[0]);
      $ip_type = self::ip_validate($tmp);
      if ($ip_type) {
        $ips['x_forwarded_for'] = $ip_type == 'v6' ? self::ip_v6_normalize($tmp) : $tmp;
      }
    }
  }

  // X-Real-Ip
  if (isset($ips['x_real_ip'])) {
    if (isset($headers['X-Real-Ip'])) {
      $tmp = explode(",", trim($headers['X-Real-Ip']));
      $tmp = trim($tmp[0]);
      $ip_type = self::ip_validate($tmp);
      if ($ip_type) {
        $ips['x_forwarded_for'] = $ip_type == 'v6' ? self::ip_v6_normalize($tmp) : $tmp;
      }
    }
  }

  // Cloud Flare
  if (isset($ips['cloud_flare'])) {
    if (isset($headers['CF-Connecting-IP'], $headers['CF-IPCountry'], $headers['CF-RAY']) || isset($headers['Cf-Connecting-Ip'], $headers['Cf-Ipcountry'], $headers['Cf-Ray'])) {
      $tmp = isset($headers['CF-Connecting-IP']) ? $headers['CF-Connecting-IP'] : $headers['Cf-Connecting-Ip'];
      $tmp = strpos($tmp, ',') !== false ? explode(',', $tmp) : (array) $tmp;
      $ip_type = self::ip_validate(trim($tmp[0]));
      if ($ip_type) {
        $ips['real'] = $ip_type == 'v6' ? self::ip_v6_normalize(trim($tmp[0])) : trim($tmp[0]);
      }
    }
  }

  // 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'])) {
    $ip_type = self::ip_validate($_SERVER['REMOTE_ADDR']);
    if ($ip_type) {
      $ips['real'] = $ip_type == 'v6' ? self::ip_v6_normalize($_SERVER['REMOTE_ADDR']) : $_SERVER['REMOTE_ADDR'];
    }

    // Cloud Flare
    if (isset($headers['Cf-Connecting-Ip'], $headers['Cf-Ipcountry'], $headers['Cf-Ray'])) {
      $ip_type = self::ip_validate($headers['Cf-Connecting-Ip']);
      if ($ip_type) {
        $ips['real'] = $ip_type == 'v6' ? self::ip_v6_normalize($headers['Cf-Connecting-Ip']) : $headers['Cf-Connecting-Ip'];
      }

      // Sucury
    }
    elseif (isset($headers['X-Sucuri-Clientip'], $headers['X-Sucuri-Country'])) {
      $ip_type = self::ip_validate($headers['X-Sucuri-Clientip']);
      if ($ip_type) {
        $ips['real'] = $ip_type == 'v6' ? self::ip_v6_normalize($headers['X-Sucuri-Clientip']) : $headers['X-Sucuri-Clientip'];
      }

      // OVH
    }
    elseif (isset($headers['X-Cdn-Any-Ip'], $headers['Remote-Ip'])) {
      $ip_type = self::ip_validate($headers['X-Cdn-Any-Ip']);
      if ($ip_type) {
        $ips['real'] = $ip_type == 'v6' ? self::ip_v6_normalize($headers['X-Cdn-Any-Ip']) : $headers['X-Cdn-Any-Ip'];
      }

      // Incapsula proxy
    }
    elseif (isset($headers['Incap-Client-Ip'])) {
      $ip_type = self::ip_validate($headers['Incap-Client-Ip']);
      if ($ip_type) {
        $ips['real'] = $ip_type == 'v6' ? self::ip_v6_normalize($headers['Incap-Client-Ip']) : $headers['Incap-Client-Ip'];
      }
    }

    // Is private network
    if ($ip_type === false || ($ip_type && self::ip_is_private_network($ips['real'], $ip_type) || self::ip_mask_match($ips['real'], filter_input(INPUT_SERVER, 'SERVER_ADDR') . '/24', $ip_type))) {

      // X-Forwarded-For
      if (isset($headers['X-Forwarded-For'])) {
        $tmp = explode(",", trim($headers['X-Forwarded-For']));
        $tmp = trim($tmp[0]);
        $ip_type = self::ip_validate($tmp);
        if ($ip_type) {
          $ips['real'] = $ip_type == 'v6' ? self::ip_v6_normalize($tmp) : $tmp;
        }

        // X-Real-Ip
      }
      elseif (isset($headers['X-Real-Ip'])) {
        $tmp = explode(",", trim($headers['X-Real-Ip']));
        $tmp = trim($tmp[0]);
        $ip_type = self::ip_validate($tmp);
        if ($ip_type) {
          $ips['real'] = $ip_type == 'v6' ? self::ip_v6_normalize($tmp) : $tmp;
        }
      }
    }
  }

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