You are here

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

Same name and namespace in other branches
  1. 7.5 src/CleantalkHelper.php \CleantalkHelper::ip_v6_normalize()

* Expand IPv6 * param (string) $ip * returns (string) IPv6

1 call to CleantalkHelper::ip_v6_normalize()
CleantalkHelper::ip_get in src/CleantalkHelper.php

File

src/CleantalkHelper.php, line 249

Class

CleantalkHelper
Cleantalk's hepler class

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