Rev 65 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 65 | Rev 97 | ||
---|---|---|---|
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-22 |
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 376... | Line 376... | ||
376 | 376 | ||
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]); |
- | |
382 | $rng[1] = trim($rng[1]); |
- | |
383 | $rng[0] = ipv6_normalize($rng[0]); |
381 | $rng[0] = ipv6_normalize(trim($rng[0])); |
384 | if (!isset($rng[1])) $rng[1] = $rng[0]; |
382 | $rng[1] = isset($rng[1]) ? ipv6_normalize(trim($rng[1])) : $rng[0]; |
385 | $rng[1] = ipv6_normalize($rng[1]); |
- | |
386 | } |
383 | } |
387 | 384 | ||
388 | return $rng; |
385 | return $rng; |
389 | } |
386 | } |
390 | 387 | ||
Line 450... | Line 447... | ||
450 | $vvv = ($compress ? 'T' : 'F').$ipv6long; |
447 | $vvv = ($compress ? 'T' : 'F').$ipv6long; |
451 | if (isset($cache_long2ip6[$vvv])) return $cache_long2ip6[$vvv]; |
448 | if (isset($cache_long2ip6[$vvv])) return $cache_long2ip6[$vvv]; |
452 | 449 | ||
453 | // $bin = gmp_strval(gmp_init($ipv6long, 10), 2); |
450 | // $bin = gmp_strval(gmp_init($ipv6long, 10), 2); |
454 | $bin = gmp_strval($ipv6long, 2); |
451 | $bin = gmp_strval($ipv6long, 2); |
455 | if (strlen($bin) < 128) { |
452 | $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 | 453 | ||
462 | $bytes = 0; |
454 | $bytes = 0; |
463 | $ipv6 = ''; |
455 | $ipv6 = ''; |
464 | while ($bytes < 8) { // 16 bytes x 8 bit/byte = 128bit |
456 | while ($bytes < 8) { // 16 bytes x 8 bit/byte = 128bit |
465 | $bin_part = substr($bin,($bytes*16),16); |
457 | $bin_part = substr($bin,($bytes*16),16); |
Line 722... | Line 714... | ||
722 | } |
714 | } |
723 | if (strlen($bin) != 128) { |
715 | if (strlen($bin) != 128) { |
724 | $cache_bin2ip[$bin] = false; |
716 | $cache_bin2ip[$bin] = false; |
725 | return false; |
717 | return false; |
726 | } |
718 | } |
727 | $pad = 128 - strlen($bin); |
719 | //$bin = str_pad($bin, 128, '0', STR_PAD_LEFT); |
728 | for ($i = 1; $i <= $pad; $i++) { |
- | |
729 | $bin = '0'.$bin; |
- | |
730 | } |
- | |
731 | $bits = 0; |
720 | $bits = 0; |
732 | $ipv6 = ''; # added by vts to avoid warning |
721 | $ipv6 = ''; # added by vts to avoid warning |
733 | while ($bits <= 7) { |
722 | while ($bits <= 7) { |
734 | $bin_part = substr($bin,($bits*16),16); |
723 | $bin_part = substr($bin,($bits*16),16); |
735 | $ipv6 .= dechex(bindec($bin_part)) . ':'; |
724 | $ipv6 .= dechex(bindec($bin_part)) . ':'; |