Example #1
0
function IPv4To6($Ip, $expand = false)
{
    static $Mask = '::ffff:';
    // This tells IPv6 it has an IPv4 address
    $IPv6 = strpos($Ip, ':') !== false;
    $IPv4 = strpos($Ip, '.') !== false;
    if (!$IPv4 && !$IPv6) {
        return false;
    }
    if ($IPv6 && $IPv4) {
        $Ip = substr($Ip, strrpos($Ip, ':') + 1);
    } elseif (!$IPv4) {
        return ExpandIPv6Notation($Ip);
    }
    // Seems to be IPv6 already?
    $Ip = array_pad(explode('.', $Ip), 4, 0);
    if (count($Ip) > 4) {
        return false;
    }
    for ($i = 0; $i < 4; $i++) {
        if ($Ip[$i] > 255) {
            return false;
        }
    }
    $Part7 = base_convert($Ip[0] * 256 + $Ip[1], 10, 16);
    $Part8 = base_convert($Ip[2] * 256 + $Ip[3], 10, 16);
    if ($expand) {
        return ExpandIPv6Notation($Mask . $Part7 . ':' . $Part8);
    } else {
        return $Mask . $Part7 . ':' . $Part8;
    }
}
function IPv6ToLong($Ip, $DatabaseParts = 2)
{
    //  Convert IPv6 address to an integer
    //  Optionally split in to two parts.
    //  @see http://stackoverflow.com/questions/420680/
    $Ip = ExpandIPv6Notation($Ip);
    $Parts = explode(':', $Ip);
    $Ip = array('', '');
    for ($i = 0; $i < 4; $i++) {
        $Ip[0] .= str_pad(base_convert($Parts[$i], 16, 2), 16, 0, STR_PAD_LEFT);
    }
    for ($i = 4; $i < 8; $i++) {
        $Ip[1] .= str_pad(base_convert($Parts[$i], 16, 2), 16, 0, STR_PAD_LEFT);
    }
    if ($DatabaseParts == 2) {
        return array(base_convert($Ip[0], 2, 10), base_convert($Ip[1], 2, 10));
    } else {
        return base_convert($Ip[0], 2, 10) + base_convert($Ip[1], 2, 10);
    }
}
Example #3
0
 protected final function textToNumericFormatV6(\blaze\lang\String $ip)
 {
     $ip = ExpandIPv6Notation($ip);
     $parts = explode(':', $ip);
     $ip = array('', '');
     for ($i = 0; $i < 4; $i++) {
         $ip[0] .= str_pad(base_convert($parts[$i], 16, 2), 16, 0, STR_PAD_LEFT);
     }
     for ($i = 4; $i < 8; $i++) {
         $ip[1] .= str_pad(base_convert($parts[$i], 16, 2), 16, 0, STR_PAD_LEFT);
     }
     return base_convert($ip[0], 2, 10) + base_convert($ip[1], 2, 10);
 }