[ticket/11765] Support IPv4 embedded IPv6 addresses in short_ipv6()

PHPBB3-11765
This commit is contained in:
Marc Alexander 2023-07-26 17:21:43 +02:00
parent 0ede362120
commit 56f0846378
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
2 changed files with 58 additions and 1 deletions

View file

@ -2918,7 +2918,7 @@ function get_censor_preg_expression($word)
/**
* Returns the first block of the specified IPv6 address and as many additional
* ones as specified in the length paramater.
* ones as specified in the length parameter.
* If length is zero, then an empty string is returned.
* If length is greater than 3 the complete IP will be returned
*/
@ -2929,6 +2929,14 @@ function short_ipv6($ip, $length)
return '';
}
// Handle IPv4 embedded IPv6 addresses
if (preg_match('/(?:\d{1,3}\.){3}\d{1,3}$/i', $ip))
{
$binary_ip = inet_pton($ip);
$ip_v6 = $binary_ip ? inet_ntop($binary_ip) : $ip;
$ip = $ip_v6 ?: $ip;
}
// extend IPv6 addresses
$blocks = substr_count($ip, ':') + 1;
if ($blocks < 9)

View file

@ -0,0 +1,49 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
class short_ipv6__test extends phpbb_test_case
{
public function data_short_ipv6(): array
{
return [
['::1', 0, ''],
['::1', 1, '0000:0000'],
['::1', 2, '0000:0000:0000'],
['::1', 3, '0000:0000:0000:0000'],
['::1', 4, '0000:0000:0000:0000:0000:0000:0000:1'],
['2001:db8:3333:4444:5555:6666:7777:8888', 0, ''],
['2001:db8:3333:4444:5555:6666:7777:8888', 1, '2001:db8'],
['2001:db8:3333:4444:5555:6666:7777:8888', 2, '2001:db8:3333'],
['2001:db8:3333:4444:5555:6666:7777:8888', 3, '2001:db8:3333:4444'],
['2001:db8:3333:4444:5555:6666:7777:8888', 4, '2001:db8:3333:4444:5555:6666:7777:8888'],
['::ffff:192.168.1.1', 0, ''],
['::ffff:192.168.1.1', 1, '0000:0000'],
['::ffff:192.168.1.1', 2, '0000:0000:0000'],
['::ffff:192.168.1.1', 3, '0000:0000:0000:0000'],
['::ffff:192.168.1.1', 4, '0000:0000:0000:0000:0000:0000:ffff:192.168.1.1'],
['FADE:BAD::192.168.0.1', 0, ''],
['FADE:BAD::192.168.0.1', 1, 'fade:bad'],
['FADE:BAD::192.168.0.1', 2, 'fade:bad:0000'],
['FADE:BAD::192.168.0.1', 3, 'fade:bad:0000:0000'],
['FADE:BAD::192.168.0.1', 4, 'fade:bad:0000:0000:0000:0000:c0a8:1'],
];
}
/**
* @dataProvider data_short_ipv6
*/
public function test_short_ipv6($ip, $length, $expected)
{
$this->assertEquals($expected, short_ipv6($ip, $length));
}
}