/** * Try get the range instance starting from its string representation. * * @param string|mixed $range * * @return static|null */ public static function fromString($range) { $result = null; if (is_string($range)) { $parts = explode('/', $range); if (count($parts) === 2) { $address = Factory::addressFromString($parts[0]); if ($address !== null) { if (preg_match('/^[0-9]{1,9}$/', $parts[1])) { $networkPrefix = @intval($parts[1]); if ($networkPrefix >= 0) { $addressBytes = $address->getBytes(); $totalBytes = count($addressBytes); $numDifferentBits = $totalBytes * 8 - $networkPrefix; if ($numDifferentBits >= 0) { $numSameBytes = $networkPrefix >> 3; $sameBytes = array_slice($addressBytes, 0, $numSameBytes); $differentBytesStart = $totalBytes === $numSameBytes ? array() : array_fill(0, $totalBytes - $numSameBytes, 0); $differentBytesEnd = $totalBytes === $numSameBytes ? array() : array_fill(0, $totalBytes - $numSameBytes, 255); $startSameBits = $networkPrefix % 8; if ($startSameBits !== 0) { $varyingByte = $addressBytes[$numSameBytes]; $differentBytesStart[0] = $varyingByte & bindec(str_pad(str_repeat('1', $startSameBits), 8, '0', STR_PAD_RIGHT)); $differentBytesEnd[0] = $differentBytesStart[0] + bindec(str_repeat('1', 8 - $startSameBits)); } $result = new static(Factory::addressFromBytes(array_merge($sameBytes, $differentBytesStart)), Factory::addressFromBytes(array_merge($sameBytes, $differentBytesEnd)), $networkPrefix); } } } } } } return $result; }
/** * Try get the range instance starting from its string representation. * * @param string|mixed $range * * @return static|null */ public static function fromString($range) { $result = null; $address = Factory::addressFromString($range); if ($address !== null) { $result = new static($address); } return $result; }
/** * @param string $ipList * * @return bool */ protected function isUserIpInList($ipList) { $result = false; if ($ipList !== '') { $ip = $this->getCurrentUserIP(); if ($ip !== null) { foreach (explode('|', $ipList) as $rangeString) { $range = IPFactory::rangeFromString($rangeString); if ($range !== null && $range->contains($ip)) { $result = true; break; } } } } return $result; }