Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 166
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
acp_inactive
0.00% covered (danger)
0.00%
0 / 164
0.00% covered (danger)
0.00%
0 / 2
1260
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 main
0.00% covered (danger)
0.00%
0 / 163
0.00% covered (danger)
0.00%
0 / 1
1190
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
14/**
15* @ignore
16*/
17if (!defined('IN_PHPBB'))
18{
19    exit;
20}
21
22class acp_inactive
23{
24    var $u_action;
25    var $p_master;
26
27    function __construct($p_master)
28    {
29        $this->p_master = $p_master;
30    }
31
32    function main($id, $mode)
33    {
34        global $config, $db, $user, $auth, $template, $phpbb_container, $phpbb_log, $request;
35        global $phpbb_root_path, $phpbb_admin_path, $phpEx;
36
37        if (!function_exists('user_active_flip'))
38        {
39            include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
40        }
41
42        $user->add_lang('memberlist');
43
44        $action = $request->variable('action', '');
45        $mark    = (isset($_REQUEST['mark'])) ? $request->variable('mark', array(0)) : array();
46        $start    = $request->variable('start', 0);
47        $submit = isset($_POST['submit']);
48
49        // Sort keys
50        $sort_days    = $request->variable('st', 0);
51        $sort_key    = $request->variable('sk', 'i');
52        $sort_dir    = $request->variable('sd', 'd');
53
54        $form_key = 'acp_inactive';
55        add_form_key($form_key);
56
57        /* @var $pagination \phpbb\pagination */
58        $pagination = $phpbb_container->get('pagination');
59
60        // We build the sort key and per page settings here, because they may be needed later
61
62        // Number of entries to display
63        $per_page = $request->variable('users_per_page', (int) $config['topics_per_page']);
64
65        // Sorting
66        $limit_days = array(0 => $user->lang['ALL_ENTRIES'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
67        $sort_by_text = array('i' => $user->lang['SORT_INACTIVE'], 'j' => $user->lang['SORT_REG_DATE'], 'l' => $user->lang['SORT_LAST_VISIT'], 'd' => $user->lang['SORT_LAST_REMINDER'], 'r' => $user->lang['SORT_REASON'], 'u' => $user->lang['SORT_USERNAME'], 'p' => $user->lang['SORT_POSTS'], 'e' => $user->lang['SORT_REMINDER']);
68        $sort_by_sql = array('i' => 'user_inactive_time', 'j' => 'user_regdate', 'l' => 'user_lastvisit', 'd' => 'user_reminded_time', 'r' => 'user_inactive_reason', 'u' => 'username_clean', 'p' => 'user_posts', 'e' => 'user_reminded');
69
70        $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
71        gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
72
73        if ($submit && count($mark))
74        {
75            if ($action !== 'delete' && !check_form_key($form_key))
76            {
77                trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
78            }
79
80            switch ($action)
81            {
82                case 'activate':
83                case 'delete':
84
85                    $sql = 'SELECT user_id, username
86                        FROM ' . USERS_TABLE . '
87                        WHERE ' . $db->sql_in_set('user_id', $mark);
88                    $result = $db->sql_query($sql);
89
90                    $user_affected = array();
91                    while ($row = $db->sql_fetchrow($result))
92                    {
93                        $user_affected[$row['user_id']] = $row['username'];
94                    }
95                    $db->sql_freeresult($result);
96
97                    if ($action == 'activate')
98                    {
99                        // Get those 'being activated'...
100                        $sql = 'SELECT user_id, username' . (($config['require_activation'] == USER_ACTIVATION_ADMIN) ? ', user_email, user_lang' : '') . '
101                            FROM ' . USERS_TABLE . '
102                            WHERE ' . $db->sql_in_set('user_id', $mark) . '
103                                AND user_type = ' . USER_INACTIVE;
104                        $result = $db->sql_query($sql);
105
106                        $inactive_users = array();
107                        while ($row = $db->sql_fetchrow($result))
108                        {
109                            $inactive_users[] = $row;
110                        }
111                        $db->sql_freeresult($result);
112
113                        user_active_flip('activate', $mark);
114
115                        if ($config['require_activation'] == USER_ACTIVATION_ADMIN && !empty($inactive_users))
116                        {
117                            $email_method = $phpbb_container->get('messenger.method.email');
118                            $email_method->set_use_queue(false);
119
120                            foreach ($inactive_users as $row)
121                            {
122                                $email_method->template('admin_welcome_activated', $row['user_lang']);
123                                $email_method->set_addresses($row);
124                                $email_method->anti_abuse_headers($config, $user);
125                                $email_method->assign_vars([
126                                    'USERNAME'    => html_entity_decode($row['username'], ENT_COMPAT),
127                                ]);
128                                $email_method->send();
129                            }
130                        }
131
132                        if (!empty($inactive_users))
133                        {
134                            foreach ($inactive_users as $row)
135                            {
136                                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_USER_ACTIVE', false, array($row['username']));
137                                $phpbb_log->add('user', $user->data['user_id'], $user->ip, 'LOG_USER_ACTIVE_USER', false, array(
138                                    'reportee_id' => $row['user_id']
139                                ));
140                            }
141
142                            trigger_error(sprintf($user->lang['LOG_INACTIVE_ACTIVATE'], implode($user->lang['COMMA_SEPARATOR'], $user_affected) . ' ' . adm_back_link($this->u_action)));
143                        }
144
145                        // For activate we really need to redirect, else a refresh can result in users being deactivated again
146                        $u_action = $this->u_action . "&amp;$u_sort_param&amp;start=$start";
147                        $u_action .= ($per_page != $config['topics_per_page']) ? "&amp;users_per_page=$per_page" : '';
148
149                        redirect($u_action);
150                    }
151                    else if ($action == 'delete')
152                    {
153                        if (confirm_box(true))
154                        {
155                            if (!$auth->acl_get('a_userdel'))
156                            {
157                                send_status_line(403, 'Forbidden');
158                                trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
159                            }
160
161                            user_delete('retain', $mark, true);
162
163                            $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_INACTIVE_' . strtoupper($action), false, array(implode(', ', $user_affected)));
164
165                            trigger_error(sprintf($user->lang['LOG_INACTIVE_DELETE'], implode($user->lang['COMMA_SEPARATOR'], $user_affected) . ' ' . adm_back_link($this->u_action)));
166                        }
167                        else
168                        {
169                            $s_hidden_fields = array(
170                                'mode'            => $mode,
171                                'action'        => $action,
172                                'mark'            => $mark,
173                                'submit'        => 1,
174                                'start'            => $start,
175                            );
176                            confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields($s_hidden_fields));
177                        }
178                    }
179
180                break;
181
182                case 'remind':
183                    if (empty($config['email_enable']))
184                    {
185                        trigger_error($user->lang['EMAIL_DISABLED'] . adm_back_link($this->u_action), E_USER_WARNING);
186                    }
187
188                    $sql = 'SELECT user_id, username, user_email, user_lang, user_regdate, user_actkey
189                        FROM ' . USERS_TABLE . '
190                        WHERE ' . $db->sql_in_set('user_id', $mark) . '
191                            AND user_inactive_reason';
192
193                    $sql .= ($config['require_activation'] == USER_ACTIVATION_ADMIN) ? ' = ' . INACTIVE_REMIND : ' <> ' . INACTIVE_MANUAL;
194
195                    $result = $db->sql_query($sql);
196
197                    /** @var \phpbb\di\service_collection $messenger_collection */
198                    $messenger_collection = $phpbb_container->get('messenger.method_collection');
199                    /** @var \phpbb\messenger\method\messenger_interface $messenger_method */
200                    $messenger_method = $messenger_collection->offsetGet('messenger.method.email');
201
202                    if ($row = $db->sql_fetchrow($result))
203                    {
204                        // Send the messages
205                        $usernames = $user_ids = array();
206
207                        do
208                        {
209                            $messenger_method->template('user_remind_inactive', $row['user_lang']);
210                            $messenger_method->set_addresses($row);
211                            $messenger_method->anti_abuse_headers($config, $user);
212                            $messenger_method->assign_vars([
213                                'USERNAME'        => html_entity_decode($row['username'], ENT_COMPAT),
214                                'REGISTER_DATE'    => $user->format_date($row['user_regdate'], false, true),
215                                'U_ACTIVATE'    => generate_board_url() . "/ucp.$phpEx?mode=activate&u=" . $row['user_id'] . '&k=' . $row['user_actkey'],
216                            ]);
217
218                            $messenger_method->send();
219                            $messenger_method->save_queue();
220
221                            $usernames[] = $row['username'];
222                            $user_ids[] = (int) $row['user_id'];
223                        }
224                        while ($row = $db->sql_fetchrow($result));
225
226                        // Add the remind state to the database and increase activation expiration by one day
227                        $sql = 'UPDATE ' . USERS_TABLE . '
228                            SET user_reminded = user_reminded + 1,
229                                user_reminded_time = ' . time() . ',
230                                user_actkey_expiration = ' . (int) $user::get_token_expiration() . '
231                            WHERE ' . $db->sql_in_set('user_id', $user_ids);
232                        $db->sql_query($sql);
233
234                        $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_INACTIVE_REMIND', false, array(implode(', ', $usernames)));
235
236                        trigger_error(sprintf($user->lang['LOG_INACTIVE_REMIND'], implode($user->lang['COMMA_SEPARATOR'], $usernames) . ' ' . adm_back_link($this->u_action)));
237                    }
238                    $db->sql_freeresult($result);
239
240                    // For remind we really need to redirect, else a refresh can result in more than one reminder
241                    $u_action = $this->u_action . "&amp;$u_sort_param&amp;start=$start";
242                    $u_action .= ($per_page != $config['topics_per_page']) ? "&amp;users_per_page=$per_page" : '';
243
244                    redirect($u_action);
245
246                break;
247            }
248        }
249
250        // Define where and sort sql for use in displaying logs
251        $sql_where = ($sort_days) ? (time() - ($sort_days * 86400)) : 0;
252        $sql_sort = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
253
254        $inactive = array();
255        $inactive_count = 0;
256
257        $start = view_inactive_users($inactive, $inactive_count, $per_page, $start, $sql_where, $sql_sort);
258
259        foreach ($inactive as $row)
260        {
261            $template->assign_block_vars('inactive', array(
262                'INACTIVE_DATE'    => $user->format_date($row['user_inactive_time']),
263                'REMINDED_DATE'    => $user->format_date($row['user_reminded_time']),
264                'JOINED'        => $user->format_date($row['user_regdate']),
265                'LAST_VISIT'    => (!$row['user_lastvisit']) ? ' - ' : $user->format_date($row['user_lastvisit']),
266
267                'REASON'        => $row['inactive_reason'],
268                'USER_ID'        => $row['user_id'],
269                'POSTS'            => ($row['user_posts']) ? $row['user_posts'] : 0,
270                'REMINDED'        => $row['user_reminded'],
271
272                'REMINDED_EXPLAIN'    => $user->lang('USER_LAST_REMINDED', (int) $row['user_reminded'], $user->format_date($row['user_reminded_time'])),
273
274                'USERNAME_FULL'        => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], false, append_sid("{$phpbb_admin_path}index.$phpEx", 'i=users&amp;mode=overview&amp;redirect=acp_inactive')),
275                'USERNAME'            => get_username_string('username', $row['user_id'], $row['username'], $row['user_colour']),
276                'USER_COLOR'        => get_username_string('colour', $row['user_id'], $row['username'], $row['user_colour']),
277                'USER_EMAIL'        => $row['user_email'],
278
279                'U_USER_ADMIN'    => append_sid("{$phpbb_admin_path}index.$phpEx", "i=users&amp;mode=overview&amp;u={$row['user_id']}"),
280                'U_SEARCH_USER'    => ($auth->acl_get('u_search')) ? append_sid("{$phpbb_root_path}search.$phpEx", "author_id={$row['user_id']}&amp;sr=posts") : '',
281            ));
282        }
283
284        $option_ary = array('activate' => 'ACTIVATE', 'delete' => 'DELETE');
285        if ($config['email_enable'])
286        {
287            $option_ary += array('remind' => 'REMIND');
288        }
289
290        $base_url = $this->u_action . "&amp;$u_sort_param&amp;users_per_page=$per_page";
291        $pagination->generate_template_pagination($base_url, 'pagination', 'start', $inactive_count, $per_page, $start);
292
293        $template->assign_vars(array(
294            'S_INACTIVE_USERS'        => true,
295            'INACTIVE_OPTIONS'    => [
296                'name'    => 'action',
297                'options' => build_select($option_ary),
298            ],
299
300            'S_LIMIT_DAYS'    => $s_limit_days,
301            'S_SORT_KEY'    => $s_sort_key,
302            'S_SORT_DIR'    => $s_sort_dir,
303            'USERS_PER_PAGE'    => $per_page,
304
305            'U_ACTION'        => $this->u_action . "&amp;$u_sort_param&amp;users_per_page=$per_page&amp;start=$start",
306        ));
307
308        $this->tpl_name = 'acp_inactive';
309        $this->page_title = 'ACP_INACTIVE_USERS';
310    }
311}