diff --git a/phpBB/config/default/container/services_ban.yml b/phpBB/config/default/container/services_ban.yml index 141374d890..e9665f745f 100644 --- a/phpBB/config/default/container/services_ban.yml +++ b/phpBB/config/default/container/services_ban.yml @@ -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: diff --git a/phpBB/phpbb/ban/exception/no_valid_ips_exception.php b/phpBB/phpbb/ban/exception/no_valid_ips_exception.php new file mode 100644 index 0000000000..e5725d27c9 --- /dev/null +++ b/phpBB/phpbb/ban/exception/no_valid_ips_exception.php @@ -0,0 +1,20 @@ + + * @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 +{ +} diff --git a/phpBB/phpbb/ban/type/ip.php b/phpBB/phpbb/ban/type/ip.php new file mode 100644 index 0000000000..22ebce2ec2 --- /dev/null +++ b/phpBB/phpbb/ban/type/ip.php @@ -0,0 +1,77 @@ + + * @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; + } +}