Project

General

Profile

Bug #14299 » class.t3lib_div.php.ipv6.patch

Administrator Admin, 2005-04-19 00:15

View differences:

class.t3lib_div.php.ipv6 2005-04-19 00:04:59.137497294 +0200
* @return boolean True if an IP-mask from $list matches $baseIP
*/
function cmpIP($baseIP, $list) {
debug($baseIP);
if(t3lib_div::validIPv6($baseIP)) {
$status = t3lib_div::cmpIPv6($baseIP, $list);
if($status) { return true; }
else { return false; }
}
$IPpartsReq = explode('.',$baseIP);
if (count($IPpartsReq)==4) {
$values = t3lib_div::trimExplode(',',$list,1);
......
}
return false;
}
/**
* Match IPv6 address with a list of IPv6 prefixes
*
* @param string $baseIP is the current remote IP address for instance
* @param string $list is a comma-list of IPv6 prefixes, could also contain IPv4 addresses
* @return boolean True if an baseIP matches any prefix
*/
function cmpIPv6($baseIP, $list) {
/* kickout all ipv6 addresses */
$mixedList = explode(',', $list);
$ipv6List = array();
foreach($mixedList as $listmember) {
/* strip of the netmask */
$parts = t3lib_div::trimExplode('/',$listmember,1);
$address = $parts[0];
$netmask = $parts[1] ? $parts[1] : '';
if(t3lib_div::validIPv6(address)) {
$validMember = $address . '/' . $netmask;
array_push($ipv6List, $validMember);
}
}
/* now compare address to list */
/* current checks: full address match, /48 /64 prefixes */
return false;
}
/**
* Normalizes a given IPv6 address to full length
*
* @param string Given IPv6 address
* @return string Normalized address
*/
function normalizeIPv6($address) {
$normalizedAddress = '';
$stageOneAddress = '';
/* check if address has hidden zero blocks */
/* if so, count is 2 */
$chunks = explode('::', $address);
if(count($chunks) == 2) {
$chunksLeft = explode(':', $chunks[0]);
$chunksRight = explode(':', $chunks[1]);
$left = count($chunksLeft);
$right = count($chunksRight);
/* specialcase: leading zero-only blocks count to 1, should be 0 */
if(($left == 1) && (strlen($chunksLeft[0]) == 0)) { $left = 0; }
$hiddenBlocks = 8 - ($left + $right);
$hiddenPart = '';
while($h < $hiddenBlocks) {
$hiddenPart .= '0000:';
$h++;
}
if($left == 0) { $stageOneAddress = $hiddenPart . $chunks[1]; }
else { $stageOneAddress = $chunks[0] . ':' . $hiddenPart . $chunks[1]; }
} else { $stageOneAddress = $address; }
/* now normalize the blocks */
$blocks = explode(':', $stageOneAddress);
$divCounter = 0;
foreach($blocks as $block) {
$tmpBlock = '';
$i = 0;
$hiddenZeros = 4 - strlen($block);
while($i < $hiddenZeros) {
$tmpBlock .= '0';
$i++;
}
$normalizedAddress .= $tmpBlock . $block;
if($divCounter < 7) {
$normalizedAddress .= ':';
$divCounter++;
}
}
return $normalizedAddress;
}
/**
* Validate a given ip address to the IPv6 address format.
*
* @param string The given ip address.
* @return boolean True if the given address is an IPv6 format.
*/
function validIPv6($ip) {
/* e.g. possible formats
* 43FB::BB3F:A0A0:0 | ::1
*/
$uppercaseIP = strtoupper($ip);
$regex = "/^(";
$regex .= "(([\dA-F]{1,4}:){7}[\dA-F]{1,4})|";
$regex .= "(([\dA-F]{1,4}){1}::([\dA-F]{1,4}:){1,5}[\dA-F]{1,4})|";
$regex .= "(([\dA-F]{1,4}:){2}:([\dA-F]{1,4}:){1,4}[\dA-F]{1,4})|";
$regex .= "(([\dA-F]{1,4}:){3}:([\dA-F]{1,4}:){1,3}[\dA-F]{1,4})|";
$regex .= "(([\dA-F]{1,4}:){4}:([\dA-F]{1,4}:){1,2}[\dA-F]{1,4})|";
$regex .= "(([\dA-F]{1,4}:){5}:([\dA-F]{1,4}:){0,1}[\dA-F]{1,4})|";
$regex .= "(::([\dA-F]{1,4}:){0,6}[\dA-F]{1,4})";
$regex .= ")$/";
if(preg_match($regex, $uppercaseIP)) { return true; }
else { return false; }
}
/**
* Match fully qualified domain name with list of strings with wildcard
(1-1/3)