Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ip
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
4 / 4
11
100.00% covered (success)
100.00%
1 / 1
 get_type
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_user_column
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 check
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 prepare_for_storage
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\ban\type;
15
16use phpbb\ban\exception\no_valid_ips_exception;
17use Symfony\Component\HttpFoundation\IpUtils;
18
19class ip extends base
20{
21    private const USER_IP = 'user_ip';
22
23    /**
24     * @inheritDoc
25     */
26    public function get_type(): string
27    {
28        return 'ip';
29    }
30
31    /**
32     * @inheritDoc
33     */
34    public function get_user_column(): ?string
35    {
36        return null;
37    }
38
39    /**
40     * {@inheritDoc}
41     */
42    public function check(array $ban_rows, array $user_data)
43    {
44        if (!isset($user_data[self::USER_IP]))
45        {
46            return false;
47        }
48
49        foreach ($ban_rows as $ip_ban)
50        {
51            if (IpUtils::checkIp($user_data[self::USER_IP], $ip_ban['item']))
52            {
53                return $ip_ban;
54            }
55        }
56
57        return false;
58    }
59
60    /**
61     * @inheritDoc
62     */
63    public function prepare_for_storage(array $items): array
64    {
65        $ban_items = [];
66        foreach ($items as $ip)
67        {
68            try
69            {
70                // Misuse checkIp for checking validity of IP. Should return true if defined IP is valid.
71                if (!IpUtils::checkIp($ip, $ip))
72                {
73                    continue;
74                }
75
76                $ban_items[] = $ip;
77            }
78            // @codeCoverageIgnoreStart
79            catch (\RuntimeException $exception)
80            {
81                // IPv6 not supported, therefore IPv6 address will not be added
82            }
83            // @codeCoverageIgnoreEnd
84        }
85
86        if (empty($ban_items))
87        {
88            throw new no_valid_ips_exception('NO_IPS_DEFINED');
89        }
90
91        return $ban_items;
92    }
93}