Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
62 / 62
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
user
100.00% covered (success)
100.00%
62 / 62
100.00% covered (success)
100.00%
6 / 6
12
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
 after_ban
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 after_unban
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 get_ban_options
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
 prepare_for_storage
100.00% covered (success)
100.00%
36 / 36
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_users_exception;
17
18class user extends base
19{
20    /** @var array */
21    private $banned_users;
22
23    /**
24     * {@inheritDoc}
25     */
26    public function get_type(): string
27    {
28        return 'user';
29    }
30
31    /**
32     * {@inheritDoc}
33     */
34    public function get_user_column(): string
35    {
36        return 'user_id';
37    }
38
39    /**
40     * {@inheritDoc}
41     */
42    public function after_ban(array $data): array
43    {
44        $this->logout_affected_users($data['items']);
45        return $this->banned_users;
46    }
47
48    /**
49     * {@inheritDoc}
50     */
51    public function after_unban(array $data): array
52    {
53        $user_ids = array_map('intval', $data['items']);
54
55        $sql = 'SELECT user_id, username
56            FROM ' . $this->users_table . '
57            WHERE ' . $this->db->sql_in_set('user_id', $user_ids);
58        $result = $this->db->sql_query($sql);
59
60        $unbanned_users = [];
61        while ($row = $this->db->sql_fetchrow($result))
62        {
63            $unbanned_users[(int) $row['user_id']] = $row['username'];
64        }
65        $this->db->sql_freeresult($result);
66
67        return $unbanned_users;
68    }
69
70    /**
71     * {@inheritDoc}
72     */
73    public function get_ban_options(): array
74    {
75        $ban_options = [];
76
77        $sql = 'SELECT b.*, u.user_id, u.username, u.username_clean
78            FROM ' . $this->bans_table . ' b, ' . $this->users_table . ' u
79            WHERE (b.ban_end >= ' . time() . "
80                    OR b.ban_end = 0)
81                AND b.ban_userid = u.user_id
82                AND b.ban_mode = '{$this->db->sql_escape($this->get_type())}'
83            ORDER BY u.username_clean ASC";
84        $result = $this->db->sql_query($sql);
85        while ($row = $this->db->sql_fetchrow($result))
86        {
87            $row['label'] = $row['username'];
88            $ban_options[] = $row;
89        }
90        $this->db->sql_freeresult($result);
91
92        return $ban_options;
93    }
94
95    /**
96     * {@inheritDoc}
97     */
98    public function prepare_for_storage(array $items): array
99    {
100        // Fill excluded user list
101        $this->get_excluded();
102
103        // Prevent banning of anonymous
104        $this->excluded[ANONYMOUS] = ANONYMOUS;
105
106        $sql_usernames = [];
107        $sql_or_like = [];
108        foreach ($items as $item)
109        {
110            $cleaned_username = utf8_clean_string($item);
111            if (stripos($cleaned_username, '*') === false)
112            {
113                $sql_usernames[] = $cleaned_username;
114            }
115            else
116            {
117                $sql_or_like[] = ['username_clean', 'LIKE', str_replace('*', $this->db->get_any_char(), $cleaned_username)];
118            }
119        }
120
121        $sql_array = [
122            'SELECT'    => 'user_id, username',
123            'FROM'        => [
124                $this->users_table    => '',
125            ],
126            'WHERE'        => ['AND',
127                [
128                    ['OR',
129                        array_merge([
130                            ['username_clean', 'IN', $sql_usernames]
131                        ], $sql_or_like),
132                    ],
133                    ['user_id', 'NOT_IN', array_map('intval', $this->excluded)],
134                ],
135            ],
136        ];
137        $sql = $this->db->sql_build_query('SELECT', $sql_array);
138        $result = $this->db->sql_query($sql);
139
140        $ban_items = [];
141        $this->banned_users = [];
142        while ($row = $this->db->sql_fetchrow($result))
143        {
144            $ban_items[] = (string) $row['user_id'];
145            $this->banned_users[(int) $row['user_id']] = $row['username'];
146        }
147        $this->db->sql_freeresult($result);
148
149        if (empty($ban_items))
150        {
151            throw new no_valid_users_exception('NO_USER_SPECIFIED');
152        }
153
154        return $ban_items;
155    }
156}