Commit e0bc958a by Scott

Move ipv6 functions out of polyfill section

parent e5040060
...@@ -213,6 +213,61 @@ ...@@ -213,6 +213,61 @@
} }
/**
* Convert an IPv6 address to its numeric representation. Based on http://stackoverflow.com/a/18277167/37947
* @param string $ip The IP address to convert
* @return string
*/
function qa_ipv6_numeric($ip)
{
$binNum = '';
foreach (unpack('C*', @inet_pton($ip)) as $byte) {
$binNum .= str_pad(decbin($byte), 8, "0", STR_PAD_LEFT);
}
return base_convert(ltrim($binNum, '0'), 2, 10);
}
/**
* Expands an IPv6 address (possibly containing wildcards), e.g. ::ffff:1 to 0000:0000:0000:0000:0000:0000:ffff:0001.
* Based on http://stackoverflow.com/a/12095836/753676
* @param string $ip The IP address to expand.
* @return string
*/
function qa_ipv6_expand($ip)
{
$ipv6_wildcard = false;
$wildcards = '';
$wildcards_matched = array();
if (strpos($ip, "*") !== false) {
$ipv6_wildcard = true;
}
if ($ipv6_wildcard) {
$wildcards = explode(":", $ip);
foreach ($wildcards as $index => $value) {
if ($value == "*") {
$wildcards_matched[] = count($wildcards) - 1 - $index;
$wildcards[$index] = "0";
}
}
$ip = implode($wildcards, ":");
}
$hex = unpack("H*hex", @inet_pton($ip));
$ip = substr(preg_replace("/([0-9A-Fa-f]{4})/", "$1:", $hex['hex']), 0, -1);
if ($ipv6_wildcard) {
$wildcards = explode(":", $ip);
foreach ($wildcards_matched as $value) {
$i = count($wildcards) - 1 - $value;
$wildcards[$i] = "*";
}
$ip = implode($wildcards, ":");
}
return $ip;
}
function qa_report_write_action($userid, $cookieid, $action, $questionid, $answerid, $commentid) function qa_report_write_action($userid, $cookieid, $action, $questionid, $answerid, $commentid)
/* /*
Called after a database write $action performed by a user identified by $userid and/or $cookieid. Called after a database write $action performed by a user identified by $userid and/or $cookieid.
......
...@@ -216,49 +216,6 @@ ...@@ -216,49 +216,6 @@
} }
} }
} }
// http://stackoverflow.com/a/18277167
function ipv6_numeric($ip) {
$binNum = '';
foreach (unpack('C*', @inet_pton($ip)) as $byte) {
$binNum .= str_pad(decbin($byte), 8, "0", STR_PAD_LEFT);
}
return base_convert(ltrim($binNum, '0'), 2, 10);
}
// http://stackoverflow.com/a/12095836/753676
function ipv6_expand($ip){
$ipv6_wildcard = false;
$wildcards = '';
$wildcards_matched=array();
if(strpos($ip, "*")!==false){
$ipv6_wildcard = true;
}
if($ipv6_wildcard){
$wildcards = explode(":", $ip);
foreach($wildcards as $index => $value){
if($value == "*"){
$wildcards_matched[]=count($wildcards)-1-$index;
$wildcards[$index]="0";
}
}
$ip=implode($wildcards,":");
}
$hex = unpack("H*hex", @inet_pton($ip));
$ip = substr(preg_replace("/([0-9A-Fa-f]{4})/", "$1:", $hex['hex']), 0, -1);
if($ipv6_wildcard){
$wildcards = explode(":", $ip);
foreach($wildcards_matched as $value){
$i = count($wildcards)-1-$value;
$wildcards[$i]="*";
}
$ip=implode($wildcards,":");
}
return $ip;
}
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment