Rev 5 | Rev 51 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 5 | Rev 46 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * IPv6 functions for PHP |
4 | * IPv6 functions for PHP |
5 | * Copyright 2012-2021 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2012-2022 Daniel Marschall, ViaThinkSoft |
6 | * Version 2021-05-21 |
6 | * Version 2022-09-18 |
7 | * |
7 | * |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
9 | * you may not use this file except in compliance with the License. |
9 | * you may not use this file except in compliance with the License. |
10 | * You may obtain a copy of the License at |
10 | * You may obtain a copy of the License at |
11 | * |
11 | * |
Line 377... | Line 377... | ||
377 | if (strpos($line, '/') !== false) { |
377 | if (strpos($line, '/') !== false) { |
378 | $rng = ipv6_cidr2range($line); |
378 | $rng = ipv6_cidr2range($line); |
379 | } else { |
379 | } else { |
380 | $rng = explode('-', $line); |
380 | $rng = explode('-', $line); |
381 | $rng[0] = trim($rng[0]); |
381 | $rng[0] = trim($rng[0]); |
382 | $rng[1] = trim($rng[1]); |
382 | if (isset($rng[1])) $rng[1] = trim($rng[1]); |
383 | $rng[0] = ipv6_normalize($rng[0]); |
383 | $rng[0] = ipv6_normalize($rng[0]); |
384 | if (!isset($rng[1])) $rng[1] = $rng[0]; |
384 | if (!isset($rng[1])) $rng[1] = $rng[0]; |
385 | $rng[1] = ipv6_normalize($rng[1]); |
385 | $rng[1] = ipv6_normalize($rng[1]); |
386 | } |
386 | } |
387 | 387 | ||
Line 450... | Line 450... | ||
450 | $vvv = ($compress ? 'T' : 'F').$ipv6long; |
450 | $vvv = ($compress ? 'T' : 'F').$ipv6long; |
451 | if (isset($cache_long2ip6[$vvv])) return $cache_long2ip6[$vvv]; |
451 | if (isset($cache_long2ip6[$vvv])) return $cache_long2ip6[$vvv]; |
452 | 452 | ||
453 | // $bin = gmp_strval(gmp_init($ipv6long, 10), 2); |
453 | // $bin = gmp_strval(gmp_init($ipv6long, 10), 2); |
454 | $bin = gmp_strval($ipv6long, 2); |
454 | $bin = gmp_strval($ipv6long, 2); |
455 | if (strlen($bin) < 128) { |
455 | $bin = str_pad($bin, 128, '0', STR_PAD_LEFT); |
456 | $pad = 128 - strlen($bin); |
- | |
457 | for ($i = 1; $i <= $pad; $i++) { |
- | |
458 | $bin = '0'.$bin; |
- | |
459 | } |
- | |
460 | } |
- | |
461 | 456 | ||
462 | $bytes = 0; |
457 | $bytes = 0; |
463 | $ipv6 = ''; |
458 | $ipv6 = ''; |
464 | while ($bytes < 8) { // 16 bytes x 8 bit/byte = 128bit |
459 | while ($bytes < 8) { // 16 bytes x 8 bit/byte = 128bit |
465 | $bin_part = substr($bin,($bytes*16),16); |
460 | $bin_part = substr($bin,($bytes*16),16); |
Line 722... | Line 717... | ||
722 | } |
717 | } |
723 | if (strlen($bin) != 128) { |
718 | if (strlen($bin) != 128) { |
724 | $cache_bin2ip[$bin] = false; |
719 | $cache_bin2ip[$bin] = false; |
725 | return false; |
720 | return false; |
726 | } |
721 | } |
727 | $pad = 128 - strlen($bin); |
722 | //$bin = str_pad($bin, 128, '0', STR_PAD_LEFT); |
728 | for ($i = 1; $i <= $pad; $i++) { |
- | |
729 | $bin = '0'.$bin; |
- | |
730 | } |
- | |
731 | $bits = 0; |
723 | $bits = 0; |
732 | $ipv6 = ''; # added by vts to avoid warning |
724 | $ipv6 = ''; # added by vts to avoid warning |
733 | while ($bits <= 7) { |
725 | while ($bits <= 7) { |
734 | $bin_part = substr($bin,($bits*16),16); |
726 | $bin_part = substr($bin,($bits*16),16); |
735 | $ipv6 .= dechex(bindec($bin_part)) . ':'; |
727 | $ipv6 .= dechex(bindec($bin_part)) . ':'; |