You are here

public static function Helper::ip__v6_normalize in Anti Spam by CleanTalk 8.4

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

* Expand IPv6 * *

Parameters

string $ip: * * @return string IPv6

1 call to Helper::ip__v6_normalize()
Helper::ip__get in src/lib/Cleantalk/Common/Helper.php
Getting arrays of IP (REMOTE_ADDR, X-Forwarded-For, X-Real-Ip, Cf_Connecting_Ip)

File

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

Class

Helper
CleanTalk Helper class. Compatible with any CMS.

Namespace

Cleantalk\Common

Code

public static function ip__v6_normalize($ip) {
  $ip = trim($ip);

  // Searching for ::ffff:xx.xx.xx.xx patterns and turn it to IPv6
  if (preg_match('/^::ffff:([0-9]{1,3}\\.?){4}$/', $ip)) {
    $ip = dechex(sprintf("%u", ip2long(substr($ip, 7))));
    $ip = '0:0:0:0:0:0:' . (strlen($ip) > 4 ? substr('abcde', 0, -4) : '0') . ':' . substr($ip, -4, 4);

    // Normalizing hextets number
  }
  elseif (strpos($ip, '::') !== false) {
    $ip = str_replace('::', str_repeat(':0', 8 - substr_count($ip, ':')) . ':', $ip);
    $ip = strpos($ip, ':') === 0 ? '0' . $ip : $ip;
    $ip = strpos(strrev($ip), ':') === 0 ? $ip . '0' : $ip;
  }

  // Simplifyng hextets
  if (preg_match('/:0(?=[a-z0-9]+)/', $ip)) {
    $ip = preg_replace('/:0(?=[a-z0-9]+)/', ':', strtolower($ip));
    $ip = self::ip__v6_normalize($ip);
  }
  return $ip;
}