public static function Helper::ip__get in Anti Spam by CleanTalk 9.1.x
Same name and namespace in other branches
- 8.4 src/lib/Cleantalk/Common/Helper.php \Cleantalk\Common\Helper::ip__get()
Getting arrays of IP (REMOTE_ADDR, X-Forwarded-For, X-Real-Ip, Cf_Connecting_Ip)
Parameters
string $ip_type_to_get Type of IP you want to receive:
bool $v4_only:
Return value
string|null
1 call to Helper::ip__get()
- Firewall::ipGet in src/
lib/ Cleantalk/ Common/ Firewall/ Firewall.php - Getting arrays of IP (REMOTE_ADDR, X-Forwarded-For, X-Real-Ip, Cf_Connecting_Ip)
File
- src/
lib/ Cleantalk/ Common/ Helper.php, line 74
Class
- Helper
- CleanTalk Helper class. Compatible with any CMS.
Namespace
Cleantalk\CommonCode
public static function ip__get($ip_type_to_get = 'real', $v4_only = true, $headers = array()) {
$out = null;
switch ($ip_type_to_get) {
// Cloud Flare
case 'cloud_flare':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['Cf-Connecting-Ip'], $headers['Cf-Ipcountry'], $headers['Cf-Ray'])) {
$tmp = strpos($headers['Cf-Connecting-Ip'], ',') !== false ? explode(',', $headers['Cf-Connecting-Ip']) : (array) $headers['Cf-Connecting-Ip'];
$ip_version = self::ip__validate(trim($tmp[0]));
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize(trim($tmp[0])) : trim($tmp[0]);
}
}
break;
// GTranslate
case 'gtranslate':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['X-Gt-Clientip'], $headers['X-Gt-Viewer-Ip'])) {
$ip_version = self::ip__validate($headers['X-Gt-Viewer-Ip']);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($headers['X-Gt-Viewer-Ip']) : $headers['X-Gt-Viewer-Ip'];
}
}
break;
// ezoic
case 'ezoic':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['X-Middleton'], $headers['X-Middleton-Ip'])) {
$ip_version = self::ip__validate($headers['X-Middleton-Ip']);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($headers['X-Middleton-Ip']) : $headers['X-Middleton-Ip'];
}
}
break;
// Sucury
case 'sucury':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['X-Sucuri-Clientip'])) {
$ip_version = self::ip__validate($headers['X-Sucuri-Clientip']);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($headers['X-Sucuri-Clientip']) : $headers['X-Sucuri-Clientip'];
}
}
break;
// X-Forwarded-By
case 'x_forwarded_by':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['X-Forwarded-By'], $headers['X-Client-Ip'])) {
$ip_version = self::ip__validate($headers['X-Client-Ip']);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($headers['X-Client-Ip']) : $headers['X-Client-Ip'];
}
}
break;
// Stackpath
case 'stackpath':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['X-Sp-Edge-Host'], $headers['X-Sp-Forwarded-Ip'])) {
$ip_version = self::ip__validate($headers['X-Sp-Forwarded-Ip']);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($headers['X-Sp-Forwarded-Ip']) : $headers['X-Sp-Forwarded-Ip'];
}
}
break;
// Ico-X-Forwarded-For
case 'ico_x_forwarded_for':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['Ico-X-Forwarded-For'], $headers['X-Forwarded-Host'])) {
$ip_version = self::ip__validate($headers['Ico-X-Forwarded-For']);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($headers['Ico-X-Forwarded-For']) : $headers['Ico-X-Forwarded-For'];
}
}
break;
// OVH
case 'ovh':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['X-Cdn-Any-Ip'], $headers['Remote-Ip'])) {
$ip_version = self::ip__validate($headers['Remote-Ip']);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($headers['Remote-Ip']) : $headers['Remote-Ip'];
}
}
break;
// Incapsula proxy
case 'incapsula':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['Incap-Client-Ip'], $headers['X-Forwarded-For'])) {
$ip_version = self::ip__validate($headers['Incap-Client-Ip']);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($headers['Incap-Client-Ip']) : $headers['Incap-Client-Ip'];
}
}
break;
// Remote addr
case 'remote_addr':
$ip_version = self::ip__validate(Server::get('REMOTE_ADDR'));
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize(Server::get('REMOTE_ADDR')) : Server::get('REMOTE_ADDR');
}
break;
// X-Forwarded-For
case 'x_forwarded_for':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['X-Forwarded-For'])) {
$tmp = explode(',', trim($headers['X-Forwarded-For']));
$tmp = trim($tmp[0]);
$ip_version = self::ip__validate($tmp);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($tmp) : $tmp;
}
}
break;
// X-Real-Ip
case 'x_real_ip':
$headers = $headers ?: self::http__get_headers();
if (isset($headers['X-Real-Ip'])) {
$tmp = explode(",", trim($headers['X-Real-Ip']));
$tmp = trim($tmp[0]);
$ip_version = self::ip__validate($tmp);
if ($ip_version) {
$out = $ip_version === 'v6' && !$v4_only ? self::ip__v6_normalize($tmp) : $tmp;
}
}
break;
// Real
// 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.
case 'real':
// Detect IP type
$out = self::ip__get('cloud_flare', $v4_only, $headers);
$out = $out ?: self::ip__get('sucury', $v4_only, $headers);
$out = $out ?: self::ip__get('gtranslate', $v4_only, $headers);
$out = $out ?: self::ip__get('ezoic', $v4_only, $headers);
$out = $out ?: self::ip__get('stackpath', $v4_only, $headers);
$out = $out ?: self::ip__get('x_forwarded_by', $v4_only, $headers);
$out = $out ?: self::ip__get('ico_x_forwarded_for', $v4_only, $headers);
$out = $out ?: self::ip__get('ovh', $v4_only, $headers);
$out = $out ?: self::ip__get('incapsula', $v4_only, $headers);
$ip_version = self::ip__validate($out);
// Is private network
if (!$out || $out && (self::ip__is_private_network($out, $ip_version) || self::ip__mask_match($out, Server::get('SERVER_ADDR') . '/24', $ip_version))) {
//@todo Remove local IP from x-forwarded-for and x-real-ip
$out = $out ?: self::ip__get('x_forwarded_for', $v4_only, $headers);
$out = $out ?: self::ip__get('x_real_ip', $v4_only, $headers);
}
$out = $out ?: self::ip__get('remote_addr', $v4_only, $headers);
break;
default:
$out = self::ip__get('real', $v4_only, $headers);
}
// Final validating IP
$ip_version = self::ip__validate($out);
if (!$ip_version) {
return null;
}
elseif ($ip_version === 'v6' && $v4_only) {
return null;
}
else {
return $out;
}
}