[ticket/9687] Add ip banning support incl. CIDR support

PHPBB3-9687
This commit is contained in:
Marc Alexander 2023-07-04 21:21:54 +02:00
parent d99968a800
commit ec4a53169a
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
3 changed files with 107 additions and 0 deletions

View file

@ -27,6 +27,16 @@ services:
tags:
- { name: ban.type }
ban.type.ip:
class: \phpbb\ban\type\ip
arguments:
- '@dbal.conn'
- '%tables.users%'
- '%tables.sessions%'
- '%tables.sessions_keys%'
tags:
- { name: ban.type }
ban.type.user:
class: \phpbb\ban\type\user
arguments:

View file

@ -0,0 +1,20 @@
<?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.
*
*/
namespace phpbb\ban\exception;
use phpbb\exception\runtime_exception;
class no_valid_ips_exception extends runtime_exception
{
}

View file

@ -0,0 +1,77 @@
<?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.
*
*/
namespace phpbb\ban\type;
use phpbb\ban\exception\no_valid_ips_exception;
use Symfony\Component\HttpFoundation\IpUtils;
class ip extends base
{
/**
* @inheritDoc
*/
public function get_type(): string
{
return 'ip';
}
/**
* @inheritDoc
*/
public function get_user_column(): ?string
{
return 'user_ip';
}
/**
* {@inheritDoc}
*/
public function check(array $ban_rows, array $user_data)
{
return parent::check($ban_rows, $user_data); // TODO: Change the autogenerated stub
}
/**
* @inheritDoc
*/
public function prepare_for_storage(array $items): array
{
$ban_items = [];
foreach ($items as $ip)
{
try
{
// Misuse checkIp for checking validity of IP. Should return true if defined IP is valid.
if (!IpUtils::checkIp($ip, $ip))
{
continue;
}
$ban_items[] = $ip;
}
catch (\RuntimeException $exception)
{
// IPv6 not supported, therefore IPv6 address will not be added
}
}
if (empty($ban_items))
{
throw new no_valid_ips_exception(); // TODO
}
return $ban_items;
}
}