<?php
 class vB_Utility_Ip { use vB_Utility_Trait_NoSerialize; public function isIpV4($ip) { $result = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); return (bool) $result; } public function isIpV6($ip) { $result = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); return (bool) $result; } public function isValid($ip) { return (bool) filter_var($ip, FILTER_VALIDATE_IP); } public function isPublic($ip) { return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE); } public function iptype($ip) { if ($this->isIpV6($ip)) { return 6; } if ($this->isIpV4($ip)) { return 4; } return 0; } public function normalize($ip) { $trimmedip = trim($ip); if (!$this->isIpV6($trimmedip)) { return $trimmedip; } return inet_ntop(inet_pton($trimmedip)); } public function isInList($ip, $list) { foreach ($list AS $listip) { if (substr($listip, -1) == '*') { if (strncasecmp($ip, $listip, strlen($listip) - 1) == 0) { return true; } } else { if ($ip == $listip) { return true; } } } return false; } public function ipSubstring($ip, $octets) { return implode('.', array_slice(explode('.', $ip), 0, $octets)); } } 