Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 1479
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
acp_users
0.00% covered (danger)
0.00%
0 / 1477
0.00% covered (danger)
0.00%
0 / 4
144020
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 / 1464
0.00% covered (danger)
0.00%
0 / 1
138756
 optionset
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 optionget
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
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*/
17
18use phpbb\controller\helper;
19use phpbb\messenger\method\messenger_interface;
20
21if (!defined('IN_PHPBB'))
22{
23    exit;
24}
25
26class acp_users
27{
28    var $u_action;
29    var $p_master;
30
31    function __construct($p_master)
32    {
33        $this->p_master = $p_master;
34    }
35
36    function main($id, $mode)
37    {
38        global $config, $db, $user, $auth, $template;
39        global $phpbb_root_path, $phpbb_admin_path, $phpEx;
40        global $phpbb_dispatcher, $request;
41        global $phpbb_container, $phpbb_log;
42
43        /** @var helper $controller_helper */
44        $controller_helper = $phpbb_container->get('controller.helper');
45
46        $user->add_lang(array('posting', 'ucp', 'acp/users'));
47        $this->tpl_name = 'acp_users';
48
49        $error        = array();
50        $username    = $request->variable('username', '', true);
51        $user_id    = $request->variable('u', 0);
52        $action        = $request->variable('action', '');
53
54        // Get referer to redirect user to the appropriate page after delete action
55        $redirect        = $request->variable('redirect', '');
56        $redirect_tag    = "redirect=$redirect";
57        $redirect_url    = append_sid("{$phpbb_admin_path}index.$phpEx", "i=$redirect");
58
59        $submit        = (isset($_POST['update']) && !isset($_POST['cancel'])) ? true : false;
60
61        $form_name = 'acp_users';
62        add_form_key($form_name);
63
64        // Whois (special case)
65        if ($action == 'whois')
66        {
67            if (!function_exists('user_get_id_name'))
68            {
69                include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
70            }
71
72            $this->page_title = 'WHOIS';
73            $this->tpl_name = 'simple_body';
74
75            $user_ip = phpbb_ip_normalise($request->variable('user_ip', ''));
76            $domain = gethostbyaddr($user_ip);
77            $ipwhois = user_ipwhois($user_ip);
78
79            $template->assign_vars(array(
80                'MESSAGE_TITLE'        => sprintf($user->lang['IP_WHOIS_FOR'], $domain),
81                'MESSAGE_TEXT'        => nl2br($ipwhois))
82            );
83
84            return;
85        }
86
87        // Show user selection mask
88        if (!$username && !$user_id)
89        {
90            $this->page_title = 'SELECT_USER';
91
92            $template->assign_vars(array(
93                'U_ACTION'            => $this->u_action,
94                'ANONYMOUS_USER_ID'    => ANONYMOUS,
95
96                'S_SELECT_USER'        => true,
97                'U_FIND_USERNAME'    => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=select_user&amp;field=username&amp;select_single=true'),
98            ));
99
100            return;
101        }
102
103        if (!$user_id)
104        {
105            $sql = 'SELECT user_id
106                FROM ' . USERS_TABLE . "
107                WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'";
108            $result = $db->sql_query($sql);
109            $user_id = (int) $db->sql_fetchfield('user_id');
110            $db->sql_freeresult($result);
111
112            if (!$user_id)
113            {
114                trigger_error($user->lang['NO_USER'] . adm_back_link($this->u_action), E_USER_WARNING);
115            }
116        }
117
118        // Generate content for all modes
119        $sql = 'SELECT u.*, s.*
120            FROM ' . USERS_TABLE . ' u
121                LEFT JOIN ' . SESSIONS_TABLE . ' s ON (s.session_user_id = u.user_id)
122            WHERE u.user_id = ' . $user_id . '
123            ORDER BY s.session_time DESC';
124        $result = $db->sql_query_limit($sql, 1);
125        $user_row = $db->sql_fetchrow($result);
126        $db->sql_freeresult($result);
127
128        if (!$user_row)
129        {
130            trigger_error($user->lang['NO_USER'] . adm_back_link($this->u_action), E_USER_WARNING);
131        }
132
133        // Generate overall "header" for user admin
134        $s_form_options = '';
135
136        // Build modes dropdown list
137        $sql = 'SELECT module_mode, module_auth
138            FROM ' . MODULES_TABLE . "
139            WHERE module_basename = 'acp_users'
140                AND module_enabled = 1
141                AND module_class = 'acp'
142            ORDER BY left_id, module_mode";
143        $result = $db->sql_query($sql);
144
145        $dropdown_modes = array();
146        while ($row = $db->sql_fetchrow($result))
147        {
148            if (!$this->p_master->module_auth_self($row['module_auth']))
149            {
150                continue;
151            }
152
153            $dropdown_modes[$row['module_mode']] = true;
154        }
155        $db->sql_freeresult($result);
156
157        foreach ($dropdown_modes as $module_mode => $null)
158        {
159            $selected = ($mode == $module_mode) ? ' selected="selected"' : '';
160            $s_form_options .= '<option value="' . $module_mode . '"' . $selected . '>' . $user->lang['ACP_USER_' . strtoupper($module_mode)] . '</option>';
161        }
162
163        $template->assign_vars(array(
164            'U_BACK'            => (empty($redirect)) ? $this->u_action : $redirect_url,
165            'U_MODE_SELECT'        => append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&amp;u=$user_id"),
166            'U_ACTION'            => $this->u_action . '&amp;u=' . $user_id . ((empty($redirect)) ? '' : '&amp;' . $redirect_tag),
167            'S_FORM_OPTIONS'    => $s_form_options,
168            'MANAGED_USERNAME'    => $user_row['username'])
169        );
170
171        // Prevent normal users/admins change/view founders if they are not a founder by themselves
172        if ($user->data['user_type'] != USER_FOUNDER && $user_row['user_type'] == USER_FOUNDER)
173        {
174            trigger_error($user->lang['NOT_MANAGE_FOUNDER'] . adm_back_link($this->u_action), E_USER_WARNING);
175        }
176
177        $this->page_title = $user_row['username'] . ' :: ' . $user->lang('ACP_USER_' . strtoupper($mode));
178
179        switch ($mode)
180        {
181            case 'overview':
182
183                if (!function_exists('user_get_id_name'))
184                {
185                    include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
186                }
187
188                $user->add_lang('acp/ban');
189
190                $delete            = $request->variable('delete', 0);
191                $delete_type    = $request->variable('delete_type', '');
192                $ip                = $request->variable('ip', 'ip');
193
194                /**
195                 * Run code at beginning of ACP users overview
196                 *
197                 * @event core.acp_users_overview_before
198                 * @var    array   user_row    Current user data
199                 * @var    string  mode        Active module
200                 * @var    string  action      Module that should be run
201                 * @var    bool    submit      Do we display the form only
202                 *                          or did the user press submit
203                 * @var    array   error       Array holding error messages
204                 * @since 3.1.3-RC1
205                 */
206                $vars = array('user_row', 'mode', 'action', 'submit', 'error');
207                extract($phpbb_dispatcher->trigger_event('core.acp_users_overview_before', compact($vars)));
208
209                if ($submit)
210                {
211                    if ($delete)
212                    {
213                        if (!$auth->acl_get('a_userdel'))
214                        {
215                            send_status_line(403, 'Forbidden');
216                            trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
217                        }
218
219                        // Check if the user wants to remove himself or the guest user account
220                        if ($user_id == ANONYMOUS)
221                        {
222                            trigger_error($user->lang['CANNOT_REMOVE_ANONYMOUS'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
223                        }
224
225                        // Founders can not be deleted.
226                        if ($user_row['user_type'] == USER_FOUNDER)
227                        {
228                            trigger_error($user->lang['CANNOT_REMOVE_FOUNDER'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
229                        }
230
231                        if ($user_id == $user->data['user_id'])
232                        {
233                            trigger_error($user->lang['CANNOT_REMOVE_YOURSELF'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
234                        }
235
236                        if ($delete_type)
237                        {
238                            if (confirm_box(true))
239                            {
240                                user_delete($delete_type, $user_id, $user_row['username']);
241
242                                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_USER_DELETED', false, array($user_row['username']));
243                                trigger_error($user->lang['USER_DELETED'] . adm_back_link(
244                                        (empty($redirect)) ? $this->u_action : $redirect_url
245                                    )
246                                );
247                            }
248                            else
249                            {
250                                $delete_confirm_hidden_fields = array(
251                                    'u'                => $user_id,
252                                    'i'                => $id,
253                                    'mode'            => $mode,
254                                    'action'        => $action,
255                                    'update'        => true,
256                                    'delete'        => 1,
257                                    'delete_type'    => $delete_type,
258                                );
259
260                                // Checks if the redirection page is specified
261                                if (!empty($redirect))
262                                {
263                                    $delete_confirm_hidden_fields['redirect'] = $redirect;
264                                }
265
266                                confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields($delete_confirm_hidden_fields));
267                            }
268                        }
269                        else
270                        {
271                            trigger_error($user->lang['NO_MODE'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
272                        }
273                    }
274
275                    // Handle quicktool actions
276                    switch ($action)
277                    {
278                        case 'banuser':
279                        case 'banemail':
280                        case 'banip':
281
282                            if ($user_id == $user->data['user_id'])
283                            {
284                                trigger_error($user->lang['CANNOT_BAN_YOURSELF'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
285                            }
286
287                            if ($user_id == ANONYMOUS)
288                            {
289                                trigger_error($user->lang['CANNOT_BAN_ANONYMOUS'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
290                            }
291
292                            if ($user_row['user_type'] == USER_FOUNDER)
293                            {
294                                trigger_error($user->lang['CANNOT_BAN_FOUNDER'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
295                            }
296
297                            if (!check_form_key($form_name))
298                            {
299                                trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
300                            }
301
302                            $ban = array();
303
304                            switch ($action)
305                            {
306                                case 'banuser':
307                                    $ban[] = $user_row['username'];
308                                    $reason = 'USER_ADMIN_BAN_NAME_REASON';
309                                break;
310
311                                case 'banemail':
312                                    $ban[] = $user_row['user_email'];
313                                    $reason = 'USER_ADMIN_BAN_EMAIL_REASON';
314                                break;
315
316                                case 'banip':
317                                    $ban[] = $user_row['user_ip'];
318
319                                    $sql = 'SELECT DISTINCT poster_ip
320                                        FROM ' . POSTS_TABLE . "
321                                        WHERE poster_id = $user_id";
322                                    $result = $db->sql_query($sql);
323
324                                    while ($row = $db->sql_fetchrow($result))
325                                    {
326                                        $ban[] = $row['poster_ip'];
327                                    }
328                                    $db->sql_freeresult($result);
329
330                                    $reason = 'USER_ADMIN_BAN_IP_REASON';
331                                break;
332                            }
333
334                            $ban_reason = $request->variable('ban_reason', $user->lang[$reason], true);
335                            $ban_give_reason = $request->variable('ban_give_reason', '', true);
336
337                            // Log not used at the moment, we simply utilize the ban function.
338                            $result = user_ban(substr($action, 3), $ban, 0, 0, 0, $ban_reason, $ban_give_reason);
339
340                            trigger_error((($result === false) ? $user->lang['BAN_ALREADY_ENTERED'] : $user->lang['BAN_SUCCESSFUL']) . adm_back_link($this->u_action . '&amp;u=' . $user_id));
341
342                        break;
343
344                        case 'reactivate':
345
346                            if ($user_id == $user->data['user_id'])
347                            {
348                                trigger_error($user->lang['CANNOT_FORCE_REACT_YOURSELF'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
349                            }
350
351                            if (!check_form_key($form_name))
352                            {
353                                trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
354                            }
355
356                            if ($user_row['user_type'] == USER_FOUNDER)
357                            {
358                                trigger_error($user->lang['CANNOT_FORCE_REACT_FOUNDER'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
359                            }
360
361                            if ($user_row['user_type'] == USER_IGNORE)
362                            {
363                                trigger_error($user->lang['CANNOT_FORCE_REACT_BOT'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
364                            }
365
366                            if ($config['email_enable'])
367                            {
368                                $server_url = generate_board_url();
369
370                                $user_actkey = gen_rand_string(mt_rand(6, 10));
371                                $email_template = ($user_row['user_type'] == USER_NORMAL) ? 'user_reactivate_account' : 'user_resend_inactive';
372
373                                if ($user_row['user_type'] == USER_NORMAL)
374                                {
375                                    user_active_flip('deactivate', $user_id, INACTIVE_REMIND);
376                                }
377                                else
378                                {
379                                    // Grabbing the last confirm key - we only send a reminder
380                                    $sql = 'SELECT user_actkey
381                                        FROM ' . USERS_TABLE . '
382                                        WHERE user_id = ' . $user_id;
383                                    $result = $db->sql_query($sql);
384                                    $user_activation_key = (string) $db->sql_fetchfield('user_actkey');
385                                    $db->sql_freeresult($result);
386
387                                    $user_actkey = empty($user_activation_key) ? $user_actkey : $user_activation_key;
388                                }
389
390                                // Always update actkey even if same and also update actkey expiration to 24 hours from now
391                                $sql_ary = [
392                                    'user_actkey'                => $user_actkey,
393                                    'user_actkey_expiration'    => $user::get_token_expiration(),
394                                ];
395
396                                $sql = 'UPDATE ' . USERS_TABLE . '
397                                    SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
398                                    WHERE user_id = ' . (int) $user_id;
399                                $db->sql_query($sql);
400
401                                // Start sending email
402                                $email_method = $phpbb_container->get('messenger.method.email');
403                                $email_method->set_use_queue(false);
404                                $email_method->template($email_template, $user_row['user_lang']);
405                                $email_method->set_addresses($user_row);
406                                $email_method->anti_abuse_headers($config, $user);
407                                $email_method->assign_vars([
408                                    'WELCOME_MSG'    => html_entity_decode(sprintf($user->lang['WELCOME_SUBJECT'], $config['sitename']), ENT_COMPAT),
409                                    'USERNAME'        => html_entity_decode($user_row['username'], ENT_COMPAT),
410                                    'U_ACTIVATE'    => "$server_url/ucp.$phpEx?mode=activate&u={$user_row['user_id']}&k=$user_actkey",
411                                ]);
412                                $email_method->send();
413
414                                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_USER_REACTIVATE', false, array($user_row['username']));
415                                $phpbb_log->add('user', $user->data['user_id'], $user->ip, 'LOG_USER_REACTIVATE_USER', false, array(
416                                    'reportee_id' => $user_id
417                                ));
418
419                                trigger_error($user->lang['FORCE_REACTIVATION_SUCCESS'] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
420                            }
421
422                        break;
423
424                        case 'active':
425
426                            if ($user_id == $user->data['user_id'])
427                            {
428                                // It is only deactivation since the user is already activated (else he would not have reached this page)
429                                trigger_error($user->lang['CANNOT_DEACTIVATE_YOURSELF'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
430                            }
431
432                            if (!check_form_key($form_name))
433                            {
434                                trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
435                            }
436
437                            if ($user_row['user_type'] == USER_FOUNDER)
438                            {
439                                trigger_error($user->lang['CANNOT_DEACTIVATE_FOUNDER'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
440                            }
441
442                            if ($user_row['user_type'] == USER_IGNORE)
443                            {
444                                trigger_error($user->lang['CANNOT_DEACTIVATE_BOT'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
445                            }
446
447                            user_active_flip('flip', $user_id);
448
449                            if ($user_row['user_type'] == USER_INACTIVE)
450                            {
451                                if ($config['require_activation'] == USER_ACTIVATION_ADMIN)
452                                {
453                                    /* @var $phpbb_notifications \phpbb\notification\manager */
454                                    $phpbb_notifications = $phpbb_container->get('notification_manager');
455                                    $phpbb_notifications->delete_notifications('notification.type.admin_activate_user', $user_row['user_id']);
456
457                                    $email_method = $phpbb_container->get('messenger.method.email');
458                                    $email_method->set_use_queue(false);
459                                    $email_method->template('admin_welcome_activated', $user_row['user_lang']);
460                                    $email_method->set_addresses($user_row);
461                                    $email_method->anti_abuse_headers($config, $user);
462                                    $email_method->assign_vars([
463                                        'USERNAME'    => html_entity_decode($user_row['username'], ENT_COMPAT),
464                                    ]);
465                                    $email_method->send();
466                                }
467                            }
468
469                            $message = ($user_row['user_type'] == USER_INACTIVE) ? 'USER_ADMIN_ACTIVATED' : 'USER_ADMIN_DEACTIVED';
470                            $log = ($user_row['user_type'] == USER_INACTIVE) ? 'LOG_USER_ACTIVE' : 'LOG_USER_INACTIVE';
471
472                            $phpbb_log->add('admin', $user->data['user_id'], $user->ip, $log, false, array($user_row['username']));
473                            $phpbb_log->add('user', $user->data['user_id'], $user->ip, $log . '_USER', false, array(
474                                'reportee_id' => $user_id
475                            ));
476
477                            trigger_error($user->lang[$message] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
478
479                        break;
480
481                        case 'delsig':
482
483                            if (!check_form_key($form_name))
484                            {
485                                trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
486                            }
487
488                            $sql_ary = array(
489                                'user_sig'                    => '',
490                                'user_sig_bbcode_uid'        => '',
491                                'user_sig_bbcode_bitfield'    => ''
492                            );
493
494                            $sql = 'UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
495                                WHERE user_id = $user_id";
496                            $db->sql_query($sql);
497
498                            $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_USER_DEL_SIG', false, array($user_row['username']));
499                            $phpbb_log->add('user', $user->data['user_id'], $user->ip, 'LOG_USER_DEL_SIG_USER', false, array(
500                                'reportee_id' => $user_id
501                            ));
502
503                            trigger_error($user->lang['USER_ADMIN_SIG_REMOVED'] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
504
505                        break;
506
507                        case 'delavatar':
508
509                            if (!check_form_key($form_name))
510                            {
511                                trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
512                            }
513
514                            // Delete old avatar if present
515                            /* @var $phpbb_avatar_manager \phpbb\avatar\manager */
516                            $phpbb_avatar_manager = $phpbb_container->get('avatar.manager');
517                            $phpbb_avatar_manager->handle_avatar_delete($db, $user, $phpbb_avatar_manager->clean_row($user_row, 'user'), USERS_TABLE, 'user_');
518
519                            $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_USER_DEL_AVATAR', false, array($user_row['username']));
520                            $phpbb_log->add('user', $user->data['user_id'], $user->ip, 'LOG_USER_DEL_AVATAR_USER', false, array(
521                                'reportee_id' => $user_id
522                            ));
523
524                            trigger_error($user->lang['USER_ADMIN_AVATAR_REMOVED'] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
525                        break;
526
527                        case 'delposts':
528
529                            if (confirm_box(true))
530                            {
531                                // Delete posts, attachments, etc.
532                                delete_posts('poster_id', $user_id);
533
534                                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_USER_DEL_POSTS', false, array($user_row['username']));
535                                trigger_error($user->lang['USER_POSTS_DELETED'] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
536                            }
537                            else
538                            {
539                                confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
540                                    'u'                => $user_id,
541                                    'i'                => $id,
542                                    'mode'            => $mode,
543                                    'action'        => $action,
544                                    'update'        => true))
545                                );
546                            }
547
548                        break;
549
550                        case 'delattach':
551
552                            if (confirm_box(true))
553                            {
554                                /** @var \phpbb\attachment\manager $attachment_manager */
555                                $attachment_manager = $phpbb_container->get('attachment.manager');
556                                $attachment_manager->delete('user', $user_id);
557                                unset($attachment_manager);
558
559                                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_USER_DEL_ATTACH', false, array($user_row['username']));
560                                trigger_error($user->lang['USER_ATTACHMENTS_REMOVED'] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
561                            }
562                            else
563                            {
564                                confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
565                                    'u'                => $user_id,
566                                    'i'                => $id,
567                                    'mode'            => $mode,
568                                    'action'        => $action,
569                                    'update'        => true))
570                                );
571                            }
572
573                        break;
574
575                        case 'deloutbox':
576
577                            if (confirm_box(true))
578                            {
579                                $msg_ids = array();
580                                $lang = 'EMPTY';
581
582                                $sql = 'SELECT msg_id
583                                    FROM ' . PRIVMSGS_TO_TABLE . "
584                                    WHERE author_id = $user_id
585                                        AND folder_id = " . PRIVMSGS_OUTBOX;
586                                $result = $db->sql_query($sql);
587
588                                if ($row = $db->sql_fetchrow($result))
589                                {
590                                    if (!function_exists('delete_pm'))
591                                    {
592                                        include($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);
593                                    }
594
595                                    do
596                                    {
597                                        $msg_ids[] = (int) $row['msg_id'];
598                                    }
599                                    while ($row = $db->sql_fetchrow($result));
600
601                                    $db->sql_freeresult($result);
602
603                                    delete_pm($user_id, $msg_ids, PRIVMSGS_OUTBOX);
604
605                                    $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_USER_DEL_OUTBOX', false, array($user_row['username']));
606
607                                    $lang = 'EMPTIED';
608                                }
609                                $db->sql_freeresult($result);
610
611                                trigger_error($user->lang['USER_OUTBOX_' . $lang] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
612                            }
613                            else
614                            {
615                                confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
616                                    'u'                => $user_id,
617                                    'i'                => $id,
618                                    'mode'            => $mode,
619                                    'action'        => $action,
620                                    'update'        => true))
621                                );
622                            }
623                        break;
624
625                        case 'moveposts':
626
627                            if (!check_form_key($form_name))
628                            {
629                                trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
630                            }
631
632                            $user->add_lang('acp/forums');
633
634                            $new_forum_id = $request->variable('new_f', 0);
635
636                            if (!$new_forum_id)
637                            {
638                                $this->page_title = 'USER_ADMIN_MOVE_POSTS';
639
640                                $template->assign_vars(array(
641                                    'S_SELECT_FORUM'        => true,
642                                    'U_ACTION'                => $this->u_action . "&amp;action=$action&amp;u=$user_id",
643                                    'U_BACK'                => $this->u_action . "&amp;u=$user_id",
644                                    'S_FORUM_OPTIONS'        => make_forum_select(false, false, false, true))
645                                );
646
647                                return;
648                            }
649
650                            // Is the new forum postable to?
651                            $sql = 'SELECT forum_name, forum_type
652                                FROM ' . FORUMS_TABLE . "
653                                WHERE forum_id = $new_forum_id";
654                            $result = $db->sql_query($sql);
655                            $forum_info = $db->sql_fetchrow($result);
656                            $db->sql_freeresult($result);
657
658                            if (!$forum_info)
659                            {
660                                trigger_error($user->lang['NO_FORUM'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
661                            }
662
663                            if ($forum_info['forum_type'] != FORUM_POST)
664                            {
665                                trigger_error($user->lang['MOVE_POSTS_NO_POSTABLE_FORUM'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
666                            }
667
668                            // Two stage?
669                            // Move topics comprising only posts from this user
670                            $topic_id_ary = $move_topic_ary = $move_post_ary = $new_topic_id_ary = array();
671                            $forum_id_ary = array($new_forum_id);
672
673                            $sql = 'SELECT topic_id, post_visibility, COUNT(post_id) AS total_posts
674                                FROM ' . POSTS_TABLE . "
675                                WHERE poster_id = $user_id
676                                    AND forum_id <> $new_forum_id
677                                GROUP BY topic_id, post_visibility";
678                            $result = $db->sql_query($sql);
679
680                            while ($row = $db->sql_fetchrow($result))
681                            {
682                                $topic_id_ary[$row['topic_id']][$row['post_visibility']] = $row['total_posts'];
683                            }
684                            $db->sql_freeresult($result);
685
686                            if (count($topic_id_ary))
687                            {
688                                $sql = 'SELECT topic_id, forum_id, topic_title, topic_posts_approved, topic_posts_unapproved, topic_posts_softdeleted, topic_attachment
689                                    FROM ' . TOPICS_TABLE . '
690                                    WHERE ' . $db->sql_in_set('topic_id', array_keys($topic_id_ary));
691                                $result = $db->sql_query($sql);
692
693                                while ($row = $db->sql_fetchrow($result))
694                                {
695                                    if ($topic_id_ary[$row['topic_id']][ITEM_APPROVED] == $row['topic_posts_approved']
696                                        && $topic_id_ary[$row['topic_id']][ITEM_UNAPPROVED] == $row['topic_posts_unapproved']
697                                        && $topic_id_ary[$row['topic_id']][ITEM_REAPPROVE] == $row['topic_posts_unapproved']
698                                        && $topic_id_ary[$row['topic_id']][ITEM_DELETED] == $row['topic_posts_softdeleted'])
699                                    {
700                                        $move_topic_ary[] = $row['topic_id'];
701                                    }
702                                    else
703                                    {
704                                        $move_post_ary[$row['topic_id']]['title'] = $row['topic_title'];
705                                        $move_post_ary[$row['topic_id']]['attach'] = ($row['topic_attachment']) ? 1 : 0;
706                                    }
707
708                                    $forum_id_ary[] = $row['forum_id'];
709                                }
710                                $db->sql_freeresult($result);
711                            }
712
713                            // Entire topic comprises posts by this user, move these topics
714                            if (count($move_topic_ary))
715                            {
716                                move_topics($move_topic_ary, $new_forum_id, false);
717                            }
718
719                            if (count($move_post_ary))
720                            {
721                                // Create new topic
722                                // Update post_ids, report_ids, attachment_ids
723                                foreach ($move_post_ary as $topic_id => $post_ary)
724                                {
725                                    // Create new topic
726                                    $sql = 'INSERT INTO ' . TOPICS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
727                                        'topic_poster'                => $user_id,
728                                        'topic_time'                => time(),
729                                        'forum_id'                     => $new_forum_id,
730                                        'icon_id'                    => 0,
731                                        'topic_visibility'            => ITEM_APPROVED,
732                                        'topic_title'                 => $post_ary['title'],
733                                        'topic_first_poster_name'    => $user_row['username'],
734                                        'topic_type'                => POST_NORMAL,
735                                        'topic_time_limit'            => 0,
736                                        'topic_attachment'            => $post_ary['attach'])
737                                    );
738                                    $db->sql_query($sql);
739
740                                    $new_topic_id = $db->sql_nextid();
741
742                                    // Move posts
743                                    $sql = 'UPDATE ' . POSTS_TABLE . "
744                                        SET forum_id = $new_forum_id, topic_id = $new_topic_id
745                                        WHERE topic_id = $topic_id
746                                            AND poster_id = $user_id";
747                                    $db->sql_query($sql);
748
749                                    if ($post_ary['attach'])
750                                    {
751                                        $sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
752                                            SET topic_id = $new_topic_id
753                                            WHERE topic_id = $topic_id
754                                                AND poster_id = $user_id";
755                                        $db->sql_query($sql);
756                                    }
757
758                                    $new_topic_id_ary[] = $new_topic_id;
759                                }
760                            }
761
762                            $forum_id_ary = array_unique($forum_id_ary);
763                            $topic_id_ary = array_unique(array_merge(array_keys($topic_id_ary), $new_topic_id_ary));
764
765                            if (count($topic_id_ary))
766                            {
767                                sync('topic_reported', 'topic_id', $topic_id_ary);
768                                sync('topic', 'topic_id', $topic_id_ary);
769                            }
770
771                            if (count($forum_id_ary))
772                            {
773                                sync('forum', 'forum_id', $forum_id_ary, false, true);
774                            }
775
776                            $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_USER_MOVE_POSTS', false, array($user_row['username'], $forum_info['forum_name']));
777                            $phpbb_log->add('user', $user->data['user_id'], $user->ip, 'LOG_USER_MOVE_POSTS_USER', false, array(
778                                'reportee_id' => $user_id,
779                                $forum_info['forum_name']
780                            ));
781
782                            trigger_error($user->lang['USER_POSTS_MOVED'] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
783
784                        break;
785
786                        case 'leave_nr':
787
788                            if (confirm_box(true))
789                            {
790                                remove_newly_registered($user_id, $user_row);
791
792                                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_USER_REMOVED_NR', false, array($user_row['username']));
793                                trigger_error($user->lang['USER_LIFTED_NR'] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
794                            }
795                            else
796                            {
797                                confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
798                                    'u'                => $user_id,
799                                    'i'                => $id,
800                                    'mode'            => $mode,
801                                    'action'        => $action,
802                                    'update'        => true))
803                                );
804                            }
805
806                        break;
807
808                        default:
809                            $u_action = $this->u_action;
810
811                            /**
812                            * Run custom quicktool code
813                            *
814                            * @event core.acp_users_overview_run_quicktool
815                            * @var    string    action        Quick tool that should be run
816                            * @var    array    user_row    Current user data
817                            * @var    string    u_action    The u_action link
818                            * @var    int        user_id        User id of the user to manage
819                            * @since 3.1.0-a1
820                            * @changed 3.2.2-RC1 Added u_action
821                            * @changed 3.2.10-RC1 Added user_id
822                            */
823                            $vars = array('action', 'user_row', 'u_action', 'user_id');
824                            extract($phpbb_dispatcher->trigger_event('core.acp_users_overview_run_quicktool', compact($vars)));
825
826                            unset($u_action);
827                        break;
828                    }
829
830                    // Handle registration info updates
831                    $data = array(
832                        'username'            => $request->variable('user', $user_row['username'], true),
833                        'user_founder'        => $request->variable('user_founder', ($user_row['user_type'] == USER_FOUNDER) ? 1 : 0),
834                        'email'                => strtolower($request->variable('user_email', $user_row['user_email'])),
835                        'new_password'        => $request->variable('new_password', '', true),
836                        'password_confirm'    => $request->variable('password_confirm', '', true),
837                    );
838
839                    // Validation data - we do not check the password complexity setting here
840                    $check_ary = array(
841                        'new_password'        => array(
842                            array('string', true, $config['min_pass_chars'], 0),
843                            array('password')),
844                        'password_confirm'    => array('string', true, $config['min_pass_chars'], 0),
845                    );
846
847                    // Check username if altered
848                    if ($data['username'] != $user_row['username'])
849                    {
850                        $check_ary += array(
851                            'username'            => array(
852                                array('string', false, $config['min_name_chars'], $config['max_name_chars']),
853                                array('username', $user_row['username'], true)
854                            ),
855                        );
856                    }
857
858                    // Check email if altered
859                    if ($data['email'] != $user_row['user_email'])
860                    {
861                        $check_ary += array(
862                            'email'                => array(
863                                array('string', false, 6, 60),
864                                array('user_email', $user_row['user_email']),
865                            ),
866                        );
867                    }
868
869                    $error = validate_data($data, $check_ary);
870
871                    if ($data['new_password'] && $data['password_confirm'] != $data['new_password'])
872                    {
873                        $error[] = 'NEW_PASSWORD_ERROR';
874                    }
875
876                    if (!check_form_key($form_name))
877                    {
878                        $error[] = 'FORM_INVALID';
879                    }
880
881                    // Instantiate passwords manager
882                    /* @var $passwords_manager \phpbb\passwords\manager */
883                    $passwords_manager = $phpbb_container->get('passwords.manager');
884
885                    // Which updates do we need to do?
886                    $update_username = ($user_row['username'] != $data['username']) ? $data['username'] : false;
887                    $update_password = $data['new_password'] && !$passwords_manager->check($data['new_password'], $user_row['user_password']);
888                    $update_email = ($data['email'] != $user_row['user_email']) ? $data['email'] : false;
889
890                    if (!count($error))
891                    {
892                        $sql_ary = array();
893
894                        if ($user_row['user_type'] != USER_FOUNDER || $user->data['user_type'] == USER_FOUNDER)
895                        {
896                            // Only allow founders updating the founder status...
897                            if ($user->data['user_type'] == USER_FOUNDER)
898                            {
899                                // Setting a normal member to be a founder
900                                if ($data['user_founder'] && $user_row['user_type'] != USER_FOUNDER)
901                                {
902                                    // Make sure the user is not setting an Inactive or ignored user to be a founder
903                                    if ($user_row['user_type'] == USER_IGNORE)
904                                    {
905                                        trigger_error($user->lang['CANNOT_SET_FOUNDER_IGNORED'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
906                                    }
907
908                                    if ($user_row['user_type'] == USER_INACTIVE)
909                                    {
910                                        trigger_error($user->lang['CANNOT_SET_FOUNDER_INACTIVE'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
911                                    }
912
913                                    $sql_ary['user_type'] = USER_FOUNDER;
914                                }
915                                else if (!$data['user_founder'] && $user_row['user_type'] == USER_FOUNDER)
916                                {
917                                    // Check if at least one founder is present
918                                    $sql = 'SELECT user_id
919                                        FROM ' . USERS_TABLE . '
920                                        WHERE user_type = ' . USER_FOUNDER . '
921                                            AND user_id <> ' . $user_id;
922                                    $result = $db->sql_query_limit($sql, 1);
923                                    $row = $db->sql_fetchrow($result);
924                                    $db->sql_freeresult($result);
925
926                                    if ($row)
927                                    {
928                                        $sql_ary['user_type'] = USER_NORMAL;
929                                    }
930                                    else
931                                    {
932                                        trigger_error($user->lang['AT_LEAST_ONE_FOUNDER'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
933                                    }
934                                }
935                            }
936                        }
937
938                        /**
939                        * Modify user data before we update it
940                        *
941                        * @event core.acp_users_overview_modify_data
942                        * @var    array    user_row    Current user data
943                        * @var    array    data        Submitted user data
944                        * @var    array    sql_ary        User data we udpate
945                        * @since 3.1.0-a1
946                        */
947                        $vars = array('user_row', 'data', 'sql_ary');
948                        extract($phpbb_dispatcher->trigger_event('core.acp_users_overview_modify_data', compact($vars)));
949
950                        if ($update_username !== false)
951                        {
952                            $sql_ary['username'] = $update_username;
953                            $sql_ary['username_clean'] = utf8_clean_string($update_username);
954
955                            $phpbb_log->add('user', $user->data['user_id'], $user->ip, 'LOG_USER_UPDATE_NAME', false, array(
956                                'reportee_id' => $user_id,
957                                $user_row['username'],
958                                $update_username
959                            ));
960                        }
961
962                        if ($update_email !== false)
963                        {
964                            $sql_ary += ['user_email'        => $update_email];
965
966                            $phpbb_log->add('user', $user->data['user_id'], $user->ip, 'LOG_USER_UPDATE_EMAIL', false, array(
967                                'reportee_id' => $user_id,
968                                $user_row['username'],
969                                $user_row['user_email'],
970                                $update_email
971                            ));
972                        }
973
974                        if ($update_password)
975                        {
976                            $sql_ary += array(
977                                'user_password'        => $passwords_manager->hash($data['new_password']),
978                                'user_passchg'        => time(),
979                            );
980
981                            $user->reset_login_keys($user_id);
982
983                            $phpbb_log->add('user', $user->data['user_id'], $user->ip, 'LOG_USER_NEW_PASSWORD', false, array(
984                                'reportee_id' => $user_id,
985                                $user_row['username']
986                            ));
987                        }
988
989                        if (count($sql_ary))
990                        {
991                            $sql = 'UPDATE ' . USERS_TABLE . '
992                                SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
993                                WHERE user_id = ' . $user_id;
994                            $db->sql_query($sql);
995                        }
996
997                        if ($update_username)
998                        {
999                            user_update_name($user_row['username'], $update_username);
1000                        }
1001
1002                        // Let the users permissions being updated
1003                        $auth->acl_clear_prefetch($user_id);
1004
1005                        $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_USER_USER_UPDATE', false, array($data['username']));
1006
1007                        trigger_error($user->lang['USER_OVERVIEW_UPDATED'] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
1008                    }
1009
1010                    // Replace "error" strings with their real, localised form
1011                    $error = array_map(array($user, 'lang'), $error);
1012                }
1013
1014                if ($user_id == $user->data['user_id'])
1015                {
1016                    $quick_tool_ary = array('delsig' => 'DEL_SIG', 'delavatar' => 'DEL_AVATAR', 'moveposts' => 'MOVE_POSTS', 'delposts' => 'DEL_POSTS', 'delattach' => 'DEL_ATTACH', 'deloutbox' => 'DEL_OUTBOX');
1017                    if ($user_row['user_new'])
1018                    {
1019                        $quick_tool_ary['leave_nr'] = 'LEAVE_NR';
1020                    }
1021                }
1022                else
1023                {
1024                    $quick_tool_ary = array();
1025
1026                    if ($user_row['user_type'] != USER_FOUNDER)
1027                    {
1028                        $quick_tool_ary += array('banuser' => 'BAN_USER', 'banemail' => 'BAN_EMAIL', 'banip' => 'BAN_IP');
1029                    }
1030
1031                    if ($user_row['user_type'] != USER_FOUNDER && $user_row['user_type'] != USER_IGNORE)
1032                    {
1033                        $quick_tool_ary += array('active' => (($user_row['user_type'] == USER_INACTIVE) ? 'ACTIVATE' : 'DEACTIVATE'));
1034                    }
1035
1036                    $quick_tool_ary += array('delsig' => 'DEL_SIG', 'delavatar' => 'DEL_AVATAR', 'moveposts' => 'MOVE_POSTS', 'delposts' => 'DEL_POSTS', 'delattach' => 'DEL_ATTACH', 'deloutbox' => 'DEL_OUTBOX');
1037
1038                    if ($config['email_enable'] && ($user_row['user_type'] == USER_NORMAL || $user_row['user_type'] == USER_INACTIVE))
1039                    {
1040                        $quick_tool_ary['reactivate'] = 'FORCE';
1041                    }
1042
1043                    if ($user_row['user_new'])
1044                    {
1045                        $quick_tool_ary['leave_nr'] = 'LEAVE_NR';
1046                    }
1047                }
1048
1049                if ($config['load_onlinetrack'])
1050                {
1051                    $sql = 'SELECT MAX(session_time) AS session_time, MIN(session_viewonline) AS session_viewonline
1052                        FROM ' . SESSIONS_TABLE . "
1053                        WHERE session_user_id = $user_id";
1054                    $result = $db->sql_query($sql);
1055                    $row = $db->sql_fetchrow($result);
1056                    $db->sql_freeresult($result);
1057
1058                    $user_row['session_time'] = (isset($row['session_time'])) ? $row['session_time'] : 0;
1059                    $user_row['session_viewonline'] = (isset($row['session_viewonline'])) ? $row['session_viewonline'] : 0;
1060                    unset($row);
1061                }
1062
1063                /**
1064                * Add additional quick tool options and overwrite user data
1065                *
1066                * @event core.acp_users_display_overview
1067                * @var    array    user_row            Array with user data
1068                * @var    array    quick_tool_ary        Ouick tool options
1069                * @since 3.1.0-a1
1070                */
1071                $vars = array('user_row', 'quick_tool_ary');
1072                extract($phpbb_dispatcher->trigger_event('core.acp_users_display_overview', compact($vars)));
1073
1074                $s_action_options = '<option class="sep" value="">' . $user->lang['SELECT_OPTION'] . '</option>';
1075                foreach ($quick_tool_ary as $value => $lang)
1076                {
1077                    $s_action_options .= '<option value="' . $value . '">' . $user->lang['USER_ADMIN_' . $lang] . '</option>';
1078                }
1079
1080                $last_active = $user_row['user_last_active'] ?: ($user_row['session_time'] ?? 0);
1081
1082                $inactive_reason = '';
1083                if ($user_row['user_type'] == USER_INACTIVE)
1084                {
1085                    $inactive_reason = $user->lang['INACTIVE_REASON_UNKNOWN'];
1086
1087                    switch ($user_row['user_inactive_reason'])
1088                    {
1089                        case INACTIVE_REGISTER:
1090                            $inactive_reason = $user->lang['INACTIVE_REASON_REGISTER'];
1091                        break;
1092
1093                        case INACTIVE_PROFILE:
1094                            $inactive_reason = $user->lang['INACTIVE_REASON_PROFILE'];
1095                        break;
1096
1097                        case INACTIVE_MANUAL:
1098                            $inactive_reason = $user->lang['INACTIVE_REASON_MANUAL'];
1099                        break;
1100
1101                        case INACTIVE_REMIND:
1102                            $inactive_reason = $user->lang['INACTIVE_REASON_REMIND'];
1103                        break;
1104                    }
1105                }
1106
1107                // Posts in Queue
1108                $sql = 'SELECT COUNT(post_id) as posts_in_queue
1109                    FROM ' . POSTS_TABLE . '
1110                    WHERE poster_id = ' . $user_id . '
1111                        AND ' . $db->sql_in_set('post_visibility', array(ITEM_UNAPPROVED, ITEM_REAPPROVE));
1112                $result = $db->sql_query($sql);
1113                $user_row['posts_in_queue'] = (int) $db->sql_fetchfield('posts_in_queue');
1114                $db->sql_freeresult($result);
1115
1116                $sql = 'SELECT post_id
1117                    FROM ' . POSTS_TABLE . '
1118                    WHERE poster_id = '. $user_id;
1119                $result = $db->sql_query_limit($sql, 1);
1120                $user_row['user_has_posts'] = (bool) $db->sql_fetchfield('post_id');
1121                $db->sql_freeresult($result);
1122
1123                $template->assign_vars(array(
1124                    'L_NAME_CHARS_EXPLAIN'        => $user->lang($config['allow_name_chars'] . '_EXPLAIN', $user->lang('CHARACTERS_XY', (int) $config['min_name_chars']), $user->lang('CHARACTERS_XY', (int) $config['max_name_chars'])),
1125                    'L_CHANGE_PASSWORD_EXPLAIN'    => $user->lang($config['pass_complex'] . '_EXPLAIN', $user->lang('CHARACTERS', (int) $config['min_pass_chars'])),
1126                    'L_POSTS_IN_QUEUE'            => $user->lang('NUM_POSTS_IN_QUEUE', $user_row['posts_in_queue']),
1127                    'S_FOUNDER'                    => ($user->data['user_type'] == USER_FOUNDER) ? true : false,
1128
1129                    'S_OVERVIEW'        => true,
1130                    'S_USER_IP'            => ($user_row['user_ip']) ? true : false,
1131                    'S_USER_FOUNDER'    => ($user_row['user_type'] == USER_FOUNDER) ? true : false,
1132                    'S_ACTION_OPTIONS'    => $s_action_options,
1133                    'S_OWN_ACCOUNT'        => ($user_id == $user->data['user_id']) ? true : false,
1134                    'S_USER_INACTIVE'    => ($user_row['user_type'] == USER_INACTIVE) ? true : false,
1135
1136                    'U_SHOW_IP'        => $this->u_action . "&amp;u=$user_id&amp;ip=" . (($ip == 'ip') ? 'hostname' : 'ip'),
1137                    'U_WHOIS'        => $this->u_action . "&amp;action=whois&amp;user_ip={$user_row['user_ip']}",
1138                    'U_MCP_QUEUE'    => ($auth->acl_getf_global('m_approve')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue') : '',
1139                    'U_SEARCH_USER'    => ($config['load_search'] && $auth->acl_get('u_search')) ? append_sid("{$phpbb_root_path}search.$phpEx", "author_id={$user_row['user_id']}&amp;sr=posts") : '',
1140
1141                    'U_SWITCH_PERMISSIONS'    => ($auth->acl_get('a_switchperm') && $user->data['user_id'] != $user_row['user_id']) ? append_sid("{$phpbb_root_path}ucp.$phpEx", "mode=switch_perm&amp;u={$user_row['user_id']}&amp;hash=" . generate_link_hash('switchperm')) : '',
1142
1143                    'POSTS_IN_QUEUE'    => $user_row['posts_in_queue'],
1144                    'USER'                => $user_row['username'],
1145                    'USER_REGISTERED'    => $user->format_date($user_row['user_regdate']),
1146                    'REGISTERED_IP'        => ($ip == 'hostname') ? gethostbyaddr($user_row['user_ip']) : $user_row['user_ip'],
1147                    'USER_LASTACTIVE'    => ($last_active) ? $user->format_date($last_active) : ' - ',
1148                    'USER_EMAIL'        => $user_row['user_email'],
1149                    'USER_WARNINGS'        => $user_row['user_warnings'],
1150                    'USER_POSTS'        => $user_row['user_posts'],
1151                    'USER_HAS_POSTS'    => $user_row['user_has_posts'],
1152                    'USER_INACTIVE_REASON'    => $inactive_reason,
1153                ));
1154
1155            break;
1156
1157            case 'feedback':
1158
1159                $user->add_lang('mcp');
1160
1161                // Set up general vars
1162                $start        = $request->variable('start', 0);
1163                $deletemark = (isset($_POST['delmarked'])) ? true : false;
1164                $deleteall    = (isset($_POST['delall'])) ? true : false;
1165                $marked        = $request->variable('mark', array(0));
1166                $message    = $request->variable('message', '', true);
1167
1168                /* @var $pagination \phpbb\pagination */
1169                $pagination = $phpbb_container->get('pagination');
1170
1171                // Sort keys
1172                $sort_days    = $request->variable('st', 0);
1173                $sort_key    = $request->variable('sk', 't');
1174                $sort_dir    = $request->variable('sd', 'd');
1175
1176                // Delete entries if requested and able
1177                if (($deletemark || $deleteall) && $auth->acl_get('a_clearlogs'))
1178                {
1179                    if (!check_form_key($form_name))
1180                    {
1181                        trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
1182                    }
1183
1184                    $where_sql = '';
1185                    if ($deletemark && $marked)
1186                    {
1187                        $sql_in = array();
1188                        foreach ($marked as $mark)
1189                        {
1190                            $sql_in[] = $mark;
1191                        }
1192                        $where_sql = ' AND ' . $db->sql_in_set('log_id', $sql_in);
1193                        unset($sql_in);
1194                    }
1195
1196                    if ($where_sql || $deleteall)
1197                    {
1198                        $sql = 'DELETE FROM ' . LOG_TABLE . '
1199                            WHERE log_type = ' . LOG_USERS . "
1200                            AND reportee_id = $user_id
1201                            $where_sql";
1202                        $db->sql_query($sql);
1203
1204                        $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_CLEAR_USER', false, array($user_row['username']));
1205                    }
1206                }
1207
1208                if ($submit && $message)
1209                {
1210                    if (!check_form_key($form_name))
1211                    {
1212                        trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
1213                    }
1214
1215                    $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_USER_FEEDBACK', false, array($user_row['username']));
1216                    $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_USER_FEEDBACK', false, array(
1217                        'forum_id' => 0,
1218                        'topic_id' => 0,
1219                        $user_row['username']
1220                    ));
1221                    $phpbb_log->add('user', $user->data['user_id'], $user->ip, 'LOG_USER_GENERAL', false, array(
1222                        'reportee_id' => $user_id,
1223                        $message
1224                    ));
1225
1226                    trigger_error($user->lang['USER_FEEDBACK_ADDED'] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
1227                }
1228
1229                // Sorting
1230                $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']);
1231                $sort_by_text = array('u' => $user->lang['SORT_USERNAME'], 't' => $user->lang['SORT_DATE'], 'i' => $user->lang['SORT_IP'], 'o' => $user->lang['SORT_ACTION']);
1232                $sort_by_sql = array('u' => 'u.username_clean', 't' => 'l.log_time', 'i' => 'l.log_ip', 'o' => 'l.log_operation');
1233
1234                $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
1235                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);
1236
1237                // Define where and sort sql for use in displaying logs
1238                $sql_where = ($sort_days) ? (time() - ($sort_days * 86400)) : 0;
1239                $sql_sort = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
1240
1241                // Grab log data
1242                $log_data = array();
1243                $log_count = 0;
1244                $start = view_log('user', $log_data, $log_count, $config['topics_per_page'], $start, 0, 0, $user_id, $sql_where, $sql_sort);
1245
1246                $base_url = $this->u_action . "&amp;u=$user_id&amp;$u_sort_param";
1247                $pagination->generate_template_pagination($base_url, 'pagination', 'start', $log_count, $config['topics_per_page'], $start);
1248
1249                $template->assign_vars(array(
1250                    'S_FEEDBACK'    => true,
1251
1252                    'S_LIMIT_DAYS'    => $s_limit_days,
1253                    'S_SORT_KEY'    => $s_sort_key,
1254                    'S_SORT_DIR'    => $s_sort_dir,
1255                    'S_CLEARLOGS'    => $auth->acl_get('a_clearlogs'))
1256                );
1257
1258                foreach ($log_data as $row)
1259                {
1260                    $template->assign_block_vars('log', array(
1261                        'USERNAME'        => $row['username_full'],
1262                        'IP'            => $row['ip'],
1263                        'DATE'            => $user->format_date($row['time']),
1264                        'ACTION'        => nl2br($row['action']),
1265                        'ID'            => $row['id'])
1266                    );
1267                }
1268
1269            break;
1270
1271            case 'warnings':
1272                $user->add_lang('mcp');
1273
1274                // Set up general vars
1275                $deletemark    = (isset($_POST['delmarked'])) ? true : false;
1276                $deleteall    = (isset($_POST['delall'])) ? true : false;
1277                $confirm    = (isset($_POST['confirm'])) ? true : false;
1278                $marked        = $request->variable('mark', array(0));
1279
1280                // Delete entries if requested and able
1281                if ($deletemark || $deleteall || $confirm)
1282                {
1283                    if (confirm_box(true))
1284                    {
1285                        $where_sql = '';
1286                        $deletemark = $request->variable('delmarked', 0);
1287                        $deleteall = $request->variable('delall', 0);
1288                        if ($deletemark && $marked)
1289                        {
1290                            $where_sql = ' AND ' . $db->sql_in_set('warning_id', array_values($marked));
1291                        }
1292
1293                        if ($where_sql || $deleteall)
1294                        {
1295                            $sql = 'DELETE FROM ' . WARNINGS_TABLE . "
1296                                WHERE user_id = $user_id
1297                                    $where_sql";
1298                            $db->sql_query($sql);
1299
1300                            if ($deleteall)
1301                            {
1302                                $log_warnings = $deleted_warnings = 0;
1303                            }
1304                            else
1305                            {
1306                                $num_warnings = (int) $db->sql_affectedrows();
1307                                $deleted_warnings = ' user_warnings - ' . $num_warnings;
1308                                $log_warnings = ($num_warnings > 2) ? 2 : $num_warnings;
1309                            }
1310
1311                            $sql = 'UPDATE ' . USERS_TABLE . "
1312                                SET user_warnings = $deleted_warnings
1313                                WHERE user_id = $user_id";
1314                            $db->sql_query($sql);
1315
1316                            if ($log_warnings)
1317                            {
1318                                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_WARNINGS_DELETED', false, array($user_row['username'], $num_warnings));
1319                            }
1320                            else
1321                            {
1322                                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_WARNINGS_DELETED_ALL', false, array($user_row['username']));
1323                            }
1324                        }
1325                    }
1326                    else
1327                    {
1328                        $s_hidden_fields = array(
1329                            'i'                => $id,
1330                            'mode'            => $mode,
1331                            'u'                => $user_id,
1332                            'mark'            => $marked,
1333                        );
1334                        if (isset($_POST['delmarked']))
1335                        {
1336                            $s_hidden_fields['delmarked'] = 1;
1337                        }
1338                        if (isset($_POST['delall']))
1339                        {
1340                            $s_hidden_fields['delall'] = 1;
1341                        }
1342                        if (isset($_POST['delall']) || (isset($_POST['delmarked']) && count($marked)))
1343                        {
1344                            confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields($s_hidden_fields));
1345                        }
1346                    }
1347                }
1348
1349                $sql = 'SELECT w.warning_id, w.warning_time, w.post_id, l.log_operation, l.log_data, l.user_id AS mod_user_id, m.username AS mod_username, m.user_colour AS mod_user_colour
1350                    FROM ' . WARNINGS_TABLE . ' w
1351                    LEFT JOIN ' . LOG_TABLE . ' l
1352                        ON (w.log_id = l.log_id)
1353                    LEFT JOIN ' . USERS_TABLE . ' m
1354                        ON (l.user_id = m.user_id)
1355                    WHERE w.user_id = ' . $user_id . '
1356                    ORDER BY w.warning_time DESC';
1357                $result = $db->sql_query($sql);
1358
1359                while ($row = $db->sql_fetchrow($result))
1360                {
1361                    if (!$row['log_operation'])
1362                    {
1363                        // We do not have a log-entry anymore, so there is no data available
1364                        $row['action'] = $user->lang['USER_WARNING_LOG_DELETED'];
1365                    }
1366                    else
1367                    {
1368                        $row['action'] = (isset($user->lang[$row['log_operation']])) ? $user->lang[$row['log_operation']] : '{' . ucfirst(str_replace('_', ' ', $row['log_operation'])) . '}';
1369                        if (!empty($row['log_data']))
1370                        {
1371                            $log_data_ary = @unserialize($row['log_data']);
1372                            $log_data_ary = ($log_data_ary === false) ? array() : $log_data_ary;
1373
1374                            if (isset($user->lang[$row['log_operation']]))
1375                            {
1376                                // Check if there are more occurrences of % than arguments, if there are we fill out the arguments array
1377                                // It doesn't matter if we add more arguments than placeholders
1378                                if ((substr_count($row['action'], '%') - count($log_data_ary)) > 0)
1379                                {
1380                                    $log_data_ary = array_merge($log_data_ary, array_fill(0, substr_count($row['action'], '%') - count($log_data_ary), ''));
1381                                }
1382                                $row['action'] = vsprintf($row['action'], $log_data_ary);
1383                                $row['action'] = bbcode_nl2br(censor_text($row['action']));
1384                            }
1385                            else if (!empty($log_data_ary))
1386                            {
1387                                $row['action'] .= '<br />' . implode('', $log_data_ary);
1388                            }
1389                        }
1390                    }
1391
1392                    $template->assign_block_vars('warn', array(
1393                        'ID'        => $row['warning_id'],
1394                        'USERNAME'    => ($row['log_operation']) ? get_username_string('full', $row['mod_user_id'], $row['mod_username'], $row['mod_user_colour']) : '-',
1395                        'ACTION'    => make_clickable($row['action']),
1396                        'DATE'        => $user->format_date($row['warning_time']),
1397                    ));
1398                }
1399                $db->sql_freeresult($result);
1400
1401                $template->assign_vars(array(
1402                    'S_WARNINGS'    => true,
1403                ));
1404
1405            break;
1406
1407            case 'profile':
1408
1409                if (!function_exists('user_get_id_name'))
1410                {
1411                    include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
1412                }
1413
1414                /* @var $cp \phpbb\profilefields\manager */
1415                $cp = $phpbb_container->get('profilefields.manager');
1416
1417                $cp_data = $cp_error = array();
1418
1419                $sql = 'SELECT lang_id
1420                    FROM ' . LANG_TABLE . "
1421                    WHERE lang_iso = '" . $db->sql_escape($user->data['user_lang']) . "'";
1422                $result = $db->sql_query($sql);
1423                $row = $db->sql_fetchrow($result);
1424                $db->sql_freeresult($result);
1425
1426                $user_row['iso_lang_id'] = $row['lang_id'];
1427
1428                $data = array(
1429                    'jabber'        => $request->variable('jabber', $user_row['user_jabber'], true),
1430                    'bday_day'        => 0,
1431                    'bday_month'    => 0,
1432                    'bday_year'        => 0,
1433                );
1434
1435                if ($user_row['user_birthday'])
1436                {
1437                    list($data['bday_day'], $data['bday_month'], $data['bday_year']) = explode('-', $user_row['user_birthday']);
1438                }
1439
1440                $data['bday_day']        = $request->variable('bday_day', $data['bday_day']);
1441                $data['bday_month']        = $request->variable('bday_month', $data['bday_month']);
1442                $data['bday_year']        = $request->variable('bday_year', $data['bday_year']);
1443                $data['user_birthday']    = sprintf('%2d-%2d-%4d', $data['bday_day'], $data['bday_month'], $data['bday_year']);
1444
1445                /**
1446                * Modify user data on editing profile in ACP
1447                *
1448                * @event core.acp_users_modify_profile
1449                * @var    array    data        Array with user profile data
1450                * @var    bool    submit        Flag indicating if submit button has been pressed
1451                * @var    int        user_id        The user id
1452                * @var    array    user_row    Array with the full user data
1453                * @since 3.1.4-RC1
1454                */
1455                $vars = array('data', 'submit', 'user_id', 'user_row');
1456                extract($phpbb_dispatcher->trigger_event('core.acp_users_modify_profile', compact($vars)));
1457
1458                if ($submit)
1459                {
1460                    $error = validate_data($data, array(
1461                        'jabber'        => array(
1462                            array('string', true, 5, 255),
1463                            array('jabber')),
1464                        'bday_day'        => array('num', true, 1, 31),
1465                        'bday_month'    => array('num', true, 1, 12),
1466                        'bday_year'        => array('num', true, 1901, gmdate('Y', time())),
1467                        'user_birthday'    => array('date', true),
1468                    ));
1469
1470                    // validate custom profile fields
1471                    $cp->submit_cp_field('profile', $user_row['iso_lang_id'], $cp_data, $cp_error);
1472
1473                    if (count($cp_error))
1474                    {
1475                        $error = array_merge($error, $cp_error);
1476                    }
1477                    if (!check_form_key($form_name))
1478                    {
1479                        $error[] = 'FORM_INVALID';
1480                    }
1481
1482                    /**
1483                    * Validate profile data in ACP before submitting to the database
1484                    *
1485                    * @event core.acp_users_profile_validate
1486                    * @var    array    data        Array with user profile data
1487                    * @var    int        user_id        The user id
1488                    * @var    array    user_row    Array with the full user data
1489                    * @var    array    error        Array with the form errors
1490                    * @since 3.1.4-RC1
1491                    * @changed 3.1.12-RC1        Removed submit, added user_id, user_row
1492                    */
1493                    $vars = array('data', 'user_id', 'user_row', 'error');
1494                    extract($phpbb_dispatcher->trigger_event('core.acp_users_profile_validate', compact($vars)));
1495
1496                    if (!count($error))
1497                    {
1498                        $sql_ary = array(
1499                            'user_jabber'    => $data['jabber'],
1500                            'user_birthday'    => $data['user_birthday'],
1501                        );
1502
1503                        /**
1504                        * Modify profile data in ACP before submitting to the database
1505                        *
1506                        * @event core.acp_users_profile_modify_sql_ary
1507                        * @var    array    cp_data        Array with the user custom profile fields data
1508                        * @var    array    data        Array with user profile data
1509                        * @var    int        user_id        The user id
1510                        * @var    array    user_row    Array with the full user data
1511                        * @var    array    sql_ary        Array with sql data
1512                        * @since 3.1.4-RC1
1513                        */
1514                        $vars = array('cp_data', 'data', 'user_id', 'user_row', 'sql_ary');
1515                        extract($phpbb_dispatcher->trigger_event('core.acp_users_profile_modify_sql_ary', compact($vars)));
1516
1517                        $sql = 'UPDATE ' . USERS_TABLE . '
1518                            SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
1519                            WHERE user_id = $user_id";
1520                        $db->sql_query($sql);
1521
1522                        // Update Custom Fields
1523                        $cp->update_profile_field_data($user_id, $cp_data);
1524
1525                        trigger_error($user->lang['USER_PROFILE_UPDATED'] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
1526                    }
1527
1528                    // Replace "error" strings with their real, localised form
1529                    $error = array_map(array($user, 'lang'), $error);
1530                }
1531
1532                $s_birthday_day_options = '<option value="0"' . ((!$data['bday_day']) ? ' selected="selected"' : '') . '>--</option>';
1533                for ($i = 1; $i < 32; $i++)
1534                {
1535                    $selected = ($i == $data['bday_day']) ? ' selected="selected"' : '';
1536                    $s_birthday_day_options .= "<option value=\"$i\"$selected>$i</option>";
1537                }
1538
1539                $s_birthday_month_options = '<option value="0"' . ((!$data['bday_month']) ? ' selected="selected"' : '') . '>--</option>';
1540                for ($i = 1; $i < 13; $i++)
1541                {
1542                    $selected = ($i == $data['bday_month']) ? ' selected="selected"' : '';
1543                    $s_birthday_month_options .= "<option value=\"$i\"$selected>$i</option>";
1544                }
1545
1546                $now = getdate();
1547                $s_birthday_year_options = '<option value="0"' . ((!$data['bday_year']) ? ' selected="selected"' : '') . '>--</option>';
1548                for ($i = $now['year'] - 100; $i <= $now['year']; $i++)
1549                {
1550                    $selected = ($i == $data['bday_year']) ? ' selected="selected"' : '';
1551                    $s_birthday_year_options .= "<option value=\"$i\"$selected>$i</option>";
1552                }
1553                unset($now);
1554
1555                $template->assign_vars(array(
1556                    'JABBER'        => $data['jabber'],
1557                    'S_BIRTHDAY_DAY_OPTIONS'    => $s_birthday_day_options,
1558                    'S_BIRTHDAY_MONTH_OPTIONS'    => $s_birthday_month_options,
1559                    'S_BIRTHDAY_YEAR_OPTIONS'    => $s_birthday_year_options,
1560
1561                    'S_PROFILE'        => true)
1562                );
1563
1564                // Get additional profile fields and assign them to the template block var 'profile_fields'
1565                $user->get_profile_fields($user_id);
1566
1567                $cp->generate_profile_fields('profile', $user_row['iso_lang_id']);
1568
1569            break;
1570
1571            case 'prefs':
1572
1573                if (!function_exists('user_get_id_name'))
1574                {
1575                    include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
1576                }
1577
1578                $data = array(
1579                    'dateformat'        => $request->variable('dateformat', $user_row['user_dateformat'], true),
1580                    'lang'                => basename($request->variable('lang', $user_row['user_lang'])),
1581                    'tz'                => $request->variable('tz', $user_row['user_timezone']),
1582                    'style'                => $request->variable('style', $user_row['user_style']),
1583                    'viewemail'            => $request->variable('viewemail', $user_row['user_allow_viewemail']),
1584                    'massemail'            => $request->variable('massemail', $user_row['user_allow_massemail']),
1585                    'hideonline'        => $request->variable('hideonline', !$user_row['user_allow_viewonline']),
1586                    'notifymethod'        => $request->variable('notifymethod', $user_row['user_notify_type']),
1587                    'notifypm'            => $request->variable('notifypm', $user_row['user_notify_pm']),
1588                    'allowpm'            => $request->variable('allowpm', $user_row['user_allow_pm']),
1589
1590                    'topic_sk'            => $request->variable('topic_sk', ($user_row['user_topic_sortby_type']) ? $user_row['user_topic_sortby_type'] : 't'),
1591                    'topic_sd'            => $request->variable('topic_sd', ($user_row['user_topic_sortby_dir']) ? $user_row['user_topic_sortby_dir'] : 'd'),
1592                    'topic_st'            => $request->variable('topic_st', ($user_row['user_topic_show_days']) ? $user_row['user_topic_show_days'] : 0),
1593
1594                    'post_sk'            => $request->variable('post_sk', ($user_row['user_post_sortby_type']) ? $user_row['user_post_sortby_type'] : 't'),
1595                    'post_sd'            => $request->variable('post_sd', ($user_row['user_post_sortby_dir']) ? $user_row['user_post_sortby_dir'] : 'a'),
1596                    'post_st'            => $request->variable('post_st', ($user_row['user_post_show_days']) ? $user_row['user_post_show_days'] : 0),
1597
1598                    'view_images'        => $request->variable('view_images', $this->optionget($user_row, 'viewimg')),
1599                    'view_smilies'        => $request->variable('view_smilies', $this->optionget($user_row, 'viewsmilies')),
1600                    'view_sigs'            => $request->variable('view_sigs', $this->optionget($user_row, 'viewsigs')),
1601                    'view_avatars'        => $request->variable('view_avatars', $this->optionget($user_row, 'viewavatars')),
1602                    'view_wordcensor'    => $request->variable('view_wordcensor', $this->optionget($user_row, 'viewcensors')),
1603
1604                    'bbcode'    => $request->variable('bbcode', $this->optionget($user_row, 'bbcode')),
1605                    'smilies'    => $request->variable('smilies', $this->optionget($user_row, 'smilies')),
1606                    'sig'        => $request->variable('sig', $this->optionget($user_row, 'attachsig')),
1607                    'notify'    => $request->variable('notify', $user_row['user_notify']),
1608                );
1609
1610                /**
1611                * Modify users preferences data
1612                *
1613                * @event core.acp_users_prefs_modify_data
1614                * @var    array    data            Array with users preferences data
1615                * @var    array    user_row        Array with user data
1616                * @since 3.1.0-b3
1617                */
1618                $vars = array('data', 'user_row');
1619                extract($phpbb_dispatcher->trigger_event('core.acp_users_prefs_modify_data', compact($vars)));
1620
1621                if ($submit)
1622                {
1623                    $error = validate_data($data, array(
1624                        'dateformat'    => array('string', false, 1, 64),
1625                        'lang'            => array('match', false, '#^[a-z_\-]{2,}$#i'),
1626                        'tz'            => array('timezone'),
1627
1628                        'topic_sk'        => array('string', false, 1, 1),
1629                        'topic_sd'        => array('string', false, 1, 1),
1630                        'post_sk'        => array('string', false, 1, 1),
1631                        'post_sd'        => array('string', false, 1, 1),
1632                    ));
1633
1634                    if (!check_form_key($form_name))
1635                    {
1636                        $error[] = 'FORM_INVALID';
1637                    }
1638
1639                    if (!count($error))
1640                    {
1641                        $this->optionset($user_row, 'viewimg', $data['view_images']);
1642                        $this->optionset($user_row, 'viewsmilies', $data['view_smilies']);
1643                        $this->optionset($user_row, 'viewsigs', $data['view_sigs']);
1644                        $this->optionset($user_row, 'viewavatars', $data['view_avatars']);
1645                        $this->optionset($user_row, 'viewcensors', $data['view_wordcensor']);
1646                        $this->optionset($user_row, 'bbcode', $data['bbcode']);
1647                        $this->optionset($user_row, 'smilies', $data['smilies']);
1648                        $this->optionset($user_row, 'attachsig', $data['sig']);
1649
1650                        $sql_ary = array(
1651                            'user_options'            => $user_row['user_options'],
1652
1653                            'user_allow_pm'            => $data['allowpm'],
1654                            'user_allow_viewemail'    => $data['viewemail'],
1655                            'user_allow_massemail'    => $data['massemail'],
1656                            'user_allow_viewonline'    => !$data['hideonline'],
1657                            'user_notify_type'        => $data['notifymethod'],
1658                            'user_notify_pm'        => $data['notifypm'],
1659
1660                            'user_dateformat'        => $data['dateformat'],
1661                            'user_lang'                => $data['lang'],
1662                            'user_timezone'            => $data['tz'],
1663                            'user_style'            => $data['style'],
1664
1665                            'user_topic_sortby_type'    => $data['topic_sk'],
1666                            'user_post_sortby_type'        => $data['post_sk'],
1667                            'user_topic_sortby_dir'        => $data['topic_sd'],
1668                            'user_post_sortby_dir'        => $data['post_sd'],
1669
1670                            'user_topic_show_days'    => $data['topic_st'],
1671                            'user_post_show_days'    => $data['post_st'],
1672
1673                            'user_notify'    => $data['notify'],
1674                        );
1675
1676                        /**
1677                        * Modify SQL query before users preferences are updated
1678                        *
1679                        * @event core.acp_users_prefs_modify_sql
1680                        * @var    array    data            Array with users preferences data
1681                        * @var    array    user_row        Array with user data
1682                        * @var    array    sql_ary            SQL array with users preferences data to update
1683                        * @var    array    error            Array with errors data
1684                        * @since 3.1.0-b3
1685                        */
1686                        $vars = array('data', 'user_row', 'sql_ary', 'error');
1687                        extract($phpbb_dispatcher->trigger_event('core.acp_users_prefs_modify_sql', compact($vars)));
1688
1689                        if (!count($error))
1690                        {
1691                            $sql = 'UPDATE ' . USERS_TABLE . '
1692                                SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
1693                                WHERE user_id = $user_id";
1694                            $db->sql_query($sql);
1695
1696                            // Check if user has an active session
1697                            if ($user_row['session_id'])
1698                            {
1699                                // We'll update the session if user_allow_viewonline has changed and the user is a bot
1700                                // Or if it's a regular user and the admin set it to hide the session
1701                                if ($user_row['user_allow_viewonline'] != $sql_ary['user_allow_viewonline'] && $user_row['user_type'] == USER_IGNORE
1702                                    || $user_row['user_allow_viewonline'] && !$sql_ary['user_allow_viewonline'])
1703                                {
1704                                    // We also need to check if the user has the permission to cloak.
1705                                    $user_auth = new \phpbb\auth\auth();
1706                                    $user_auth->acl($user_row);
1707
1708                                    $session_sql_ary = array(
1709                                        'session_viewonline'    => ($user_auth->acl_get('u_hideonline')) ? $sql_ary['user_allow_viewonline'] : true,
1710                                    );
1711
1712                                    $sql = 'UPDATE ' . SESSIONS_TABLE . '
1713                                        SET ' . $db->sql_build_array('UPDATE', $session_sql_ary) . "
1714                                        WHERE session_user_id = $user_id";
1715                                    $db->sql_query($sql);
1716
1717                                    unset($user_auth);
1718                                }
1719                            }
1720
1721                            trigger_error($user->lang['USER_PREFS_UPDATED'] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
1722                        }
1723                    }
1724
1725                    // Replace "error" strings with their real, localised form
1726                    $error = array_map(array($user, 'lang'), $error);
1727                }
1728
1729                $dateformat_options = '';
1730                foreach ($user->lang['dateformats'] as $format => $null)
1731                {
1732                    $dateformat_options .= '<option value="' . $format . '"' . (($format == $data['dateformat']) ? ' selected="selected"' : '') . '>';
1733                    $dateformat_options .= $user->format_date(time(), $format, false) . ((strpos($format, '|') !== false) ? $user->lang['VARIANT_DATE_SEPARATOR'] . $user->format_date(time(), $format, true) : '');
1734                    $dateformat_options .= '</option>';
1735                }
1736
1737                $s_custom = false;
1738
1739                $dateformat_options .= '<option value="custom"';
1740                if (!isset($user->lang['dateformats'][$data['dateformat']]))
1741                {
1742                    $dateformat_options .= ' selected="selected"';
1743                    $s_custom = true;
1744                }
1745                $dateformat_options .= '>' . $user->lang['CUSTOM_DATEFORMAT'] . '</option>';
1746
1747                $sort_dir_text = array('a' => $user->lang['ASCENDING'], 'd' => $user->lang['DESCENDING']);
1748
1749                // Topic ordering options
1750                $limit_topic_days = array(0 => $user->lang['ALL_TOPICS'], 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']);
1751                $sort_by_topic_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 'r' => $user->lang['REPLIES'], 's' => $user->lang['SUBJECT'], 'v' => $user->lang['VIEWS']);
1752
1753                // Post ordering options
1754                $limit_post_days = array(0 => $user->lang['ALL_POSTS'], 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']);
1755                $sort_by_post_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
1756
1757                $_options = array('topic', 'post');
1758                foreach ($_options as $sort_option)
1759                {
1760                    ${'s_limit_' . $sort_option . '_days'} = '<select name="' . $sort_option . '_st">';
1761                    foreach (${'limit_' . $sort_option . '_days'} as $day => $text)
1762                    {
1763                        $selected = ($data[$sort_option . '_st'] == $day) ? ' selected="selected"' : '';
1764                        ${'s_limit_' . $sort_option . '_days'} .= '<option value="' . $day . '"' . $selected . '>' . $text . '</option>';
1765                    }
1766                    ${'s_limit_' . $sort_option . '_days'} .= '</select>';
1767
1768                    ${'s_sort_' . $sort_option . '_key'} = '<select name="' . $sort_option . '_sk">';
1769                    foreach (${'sort_by_' . $sort_option . '_text'} as $key => $text)
1770                    {
1771                        $selected = ($data[$sort_option . '_sk'] == $key) ? ' selected="selected"' : '';
1772                        ${'s_sort_' . $sort_option . '_key'} .= '<option value="' . $key . '"' . $selected . '>' . $text . '</option>';
1773                    }
1774                    ${'s_sort_' . $sort_option . '_key'} .= '</select>';
1775
1776                    ${'s_sort_' . $sort_option . '_dir'} = '<select name="' . $sort_option . '_sd">';
1777                    foreach ($sort_dir_text as $key => $value)
1778                    {
1779                        $selected = ($data[$sort_option . '_sd'] == $key) ? ' selected="selected"' : '';
1780                        ${'s_sort_' . $sort_option . '_dir'} .= '<option value="' . $key . '"' . $selected . '>' . $value . '</option>';
1781                    }
1782                    ${'s_sort_' . $sort_option . '_dir'} .= '</select>';
1783                }
1784
1785                $timezone_select = phpbb_timezone_select($user, $data['tz'], true);
1786                $lang_options = phpbb_language_select($db, $data['lang']);
1787
1788                $user_prefs_data = array(
1789                    'S_PREFS'            => true,
1790                    'S_JABBER_DISABLED'    => ($config['jab_enable'] && $user_row['user_jabber'] && @extension_loaded('xml')) ? false : true,
1791
1792                    'VIEW_EMAIL'        => $data['viewemail'],
1793                    'MASS_EMAIL'        => $data['massemail'],
1794                    'ALLOW_PM'            => $data['allowpm'],
1795                    'HIDE_ONLINE'        => $data['hideonline'],
1796                    'NOTIFY_EMAIL'        => ($data['notifymethod'] == messenger_interface::NOTIFY_EMAIL) ? true : false,
1797                    'NOTIFY_IM'            => ($data['notifymethod'] == messenger_interface::NOTIFY_IM) ? true : false,
1798                    'NOTIFY_BOTH'        => ($data['notifymethod'] == messenger_interface::NOTIFY_BOTH) ? true : false,
1799                    'NOTIFY_PM'            => $data['notifypm'],
1800                    'BBCODE'            => $data['bbcode'],
1801                    'SMILIES'            => $data['smilies'],
1802                    'ATTACH_SIG'        => $data['sig'],
1803                    'NOTIFY'            => $data['notify'],
1804                    'VIEW_IMAGES'        => $data['view_images'],
1805                    'VIEW_SMILIES'        => $data['view_smilies'],
1806                    'VIEW_SIGS'            => $data['view_sigs'],
1807                    'VIEW_AVATARS'        => $data['view_avatars'],
1808                    'VIEW_WORDCENSOR'    => $data['view_wordcensor'],
1809
1810                    'S_TOPIC_SORT_DAYS'        => $s_limit_topic_days,
1811                    'S_TOPIC_SORT_KEY'        => $s_sort_topic_key,
1812                    'S_TOPIC_SORT_DIR'        => $s_sort_topic_dir,
1813                    'S_POST_SORT_DAYS'        => $s_limit_post_days,
1814                    'S_POST_SORT_KEY'        => $s_sort_post_key,
1815                    'S_POST_SORT_DIR'        => $s_sort_post_dir,
1816
1817                    'DATE_FORMAT'            => $data['dateformat'],
1818                    'S_DATEFORMAT_OPTIONS'    => $dateformat_options,
1819                    'S_CUSTOM_DATEFORMAT'    => $s_custom,
1820                    'DEFAULT_DATEFORMAT'    => $config['default_dateformat'],
1821                    'A_DEFAULT_DATEFORMAT'    => addslashes($config['default_dateformat']),
1822
1823                    'LANG_OPTIONS'        => [
1824                        'id'        => 'lang',
1825                        'name'        => 'lang',
1826                        'options'    => $lang_options,
1827                    ],
1828                    'S_STYLE_OPTIONS'    => [
1829                        'id'        => 'style',
1830                        'name'        => 'style',
1831                        'options' => style_select($data['style'])
1832                    ],
1833                    'TIMEZONE_OPTIONS'    => [
1834                        'tag'        => 'select',
1835                        'name'        => 'tz',
1836                        'options'    => $timezone_select,
1837                    ],
1838                );
1839
1840                /**
1841                * Modify users preferences data before assigning it to the template
1842                *
1843                * @event core.acp_users_prefs_modify_template_data
1844                * @var    array    data                Array with users preferences data
1845                * @var    array    user_row            Array with user data
1846                * @var    array    user_prefs_data        Array with users preferences data to be assigned to the template
1847                * @since 3.1.0-b3
1848                */
1849                $vars = array('data', 'user_row', 'user_prefs_data');
1850                extract($phpbb_dispatcher->trigger_event('core.acp_users_prefs_modify_template_data', compact($vars)));
1851
1852                $template->assign_vars($user_prefs_data);
1853
1854            break;
1855
1856            case 'avatar':
1857
1858                $avatars_enabled = false;
1859                /** @var \phpbb\avatar\manager $phpbb_avatar_manager */
1860                $phpbb_avatar_manager = $phpbb_container->get('avatar.manager');
1861
1862                if ($config['allow_avatar'])
1863                {
1864                    $avatar_drivers = $phpbb_avatar_manager->get_enabled_drivers();
1865
1866                    // This is normalised data, without the user_ prefix
1867                    $avatar_data = \phpbb\avatar\manager::clean_row($user_row, 'user');
1868
1869                    if ($submit)
1870                    {
1871                        if (check_form_key($form_name))
1872                        {
1873                            $driver_name = $phpbb_avatar_manager->clean_driver_name($request->variable('avatar_driver', ''));
1874
1875                            if (in_array($driver_name, $avatar_drivers) && !$request->is_set_post('avatar_delete'))
1876                            {
1877                                $driver = $phpbb_avatar_manager->get_driver($driver_name);
1878                                $result = $driver->process_form($request, $template, $user, $avatar_data, $error);
1879
1880                                if ($result && empty($error))
1881                                {
1882                                    // Success! Lets save the result in the database
1883                                    $result = array(
1884                                        'user_avatar_type' => $driver_name,
1885                                        'user_avatar' => $result['avatar'],
1886                                        'user_avatar_width' => $result['avatar_width'],
1887                                        'user_avatar_height' => $result['avatar_height'],
1888                                    );
1889
1890                                    /**
1891                                    * Modify users preferences data before assigning it to the template
1892                                    *
1893                                    * @event core.acp_users_avatar_sql
1894                                    * @var    array    user_row    Array with user data
1895                                    * @var    array    result        Array with user avatar data to be updated in the DB
1896                                    * @since 3.2.4-RC1
1897                                    */
1898                                    $vars = array('user_row', 'result');
1899                                    extract($phpbb_dispatcher->trigger_event('core.acp_users_avatar_sql', compact($vars)));
1900
1901                                    $sql = 'UPDATE ' . USERS_TABLE . '
1902                                        SET ' . $db->sql_build_array('UPDATE', $result) . '
1903                                        WHERE user_id = ' . (int) $user_id;
1904
1905                                    $db->sql_query($sql);
1906                                    trigger_error($user->lang['USER_AVATAR_UPDATED'] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
1907                                }
1908                            }
1909                        }
1910                        else
1911                        {
1912                            trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
1913                        }
1914                    }
1915
1916                    // Handle deletion of avatars
1917                    if ($request->is_set_post('avatar_delete'))
1918                    {
1919                        if (!confirm_box(true))
1920                        {
1921                            confirm_box(false, $user->lang('CONFIRM_AVATAR_DELETE'), build_hidden_fields(array(
1922                                    'avatar_delete'     => true))
1923                            );
1924                        }
1925                        else
1926                        {
1927                            $phpbb_avatar_manager->handle_avatar_delete($db, $user, $avatar_data, USERS_TABLE, 'user_');
1928
1929                            trigger_error($user->lang['USER_AVATAR_UPDATED'] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
1930                        }
1931                    }
1932
1933                    $selected_driver = $phpbb_avatar_manager->clean_driver_name($request->variable('avatar_driver', $user_row['user_avatar_type']));
1934
1935                    // Assign min and max values before generating avatar driver html
1936                    $template->assign_vars(array(
1937                        'AVATAR_MIN_WIDTH'        => $config['avatar_min_width'],
1938                        'AVATAR_MAX_WIDTH'        => $config['avatar_max_width'],
1939                        'AVATAR_MIN_HEIGHT'        => $config['avatar_min_height'],
1940                        'AVATAR_MAX_HEIGHT'        => $config['avatar_max_height'],
1941                    ));
1942
1943                    foreach ($avatar_drivers as $current_driver)
1944                    {
1945                        $driver = $phpbb_avatar_manager->get_driver($current_driver);
1946
1947                        $avatars_enabled = true;
1948                        $template->set_filenames(array(
1949                            'avatar' => $driver->get_acp_template_name(),
1950                        ));
1951
1952                        if ($driver->prepare_form($request, $template, $user, $avatar_data, $error))
1953                        {
1954                            $driver_name = $phpbb_avatar_manager->prepare_driver_name($current_driver);
1955                            $driver_upper = strtoupper($driver_name);
1956
1957                            $template->assign_block_vars('avatar_drivers', array(
1958                                'L_TITLE' => $user->lang($driver_upper . '_TITLE'),
1959                                'L_EXPLAIN' => $user->lang($driver_upper . '_EXPLAIN'),
1960
1961                                'DRIVER' => $driver_name,
1962                                'SELECTED' => $current_driver == $selected_driver,
1963                                'OUTPUT' => $template->assign_display('avatar'),
1964                            ));
1965                        }
1966                    }
1967                }
1968
1969                // Avatar manager is not initialized if avatars are disabled
1970                if (isset($phpbb_avatar_manager))
1971                {
1972                    // Replace "error" strings with their real, localised form
1973                    $error = $phpbb_avatar_manager->localize_errors($user, $error);
1974                }
1975
1976                /** @var \phpbb\avatar\helper $avatar_helper */
1977                $avatar_helper = $phpbb_container->get('avatar.helper');
1978
1979                $avatar = $avatar_helper->get_user_avatar($user_row, 'USER_AVATAR', true);
1980                $template->assign_vars($avatar_helper->get_template_vars($avatar));
1981
1982                $template->assign_vars(array(
1983                    'S_AVATAR'            => true,
1984                    'ERROR'                => !empty($error) ? implode('<br />', $error) : '',
1985
1986                    'S_FORM_ENCTYPE'    => ' enctype="multipart/form-data"',
1987
1988                    'L_AVATAR_EXPLAIN'    => $user->lang(($config['avatar_filesize'] == 0) ? 'AVATAR_EXPLAIN_NO_FILESIZE' : 'AVATAR_EXPLAIN', $config['avatar_max_width'], $config['avatar_max_height'], $config['avatar_filesize'] / 1024),
1989
1990                    'S_AVATARS_ENABLED'    => ($config['allow_avatar'] && $avatars_enabled),
1991                ));
1992
1993            break;
1994
1995            case 'rank':
1996
1997                if ($submit)
1998                {
1999                    if (!check_form_key($form_name))
2000                    {
2001                        trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
2002                    }
2003
2004                    $rank_id = $request->variable('user_rank', 0);
2005
2006                    $sql = 'UPDATE ' . USERS_TABLE . "
2007                        SET user_rank = $rank_id
2008                        WHERE user_id = $user_id";
2009                    $db->sql_query($sql);
2010
2011                    trigger_error($user->lang['USER_RANK_UPDATED'] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
2012                }
2013
2014                $sql = 'SELECT *
2015                    FROM ' . RANKS_TABLE . '
2016                    WHERE rank_special = 1
2017                    ORDER BY rank_title';
2018                $result = $db->sql_query($sql);
2019
2020                $s_rank_options = '<option value="0"' . ((!$user_row['user_rank']) ? ' selected="selected"' : '') . '>' . $user->lang['NO_SPECIAL_RANK'] . '</option>';
2021
2022                while ($row = $db->sql_fetchrow($result))
2023                {
2024                    $selected = ($user_row['user_rank'] && $row['rank_id'] == $user_row['user_rank']) ? ' selected="selected"' : '';
2025                    $s_rank_options .= '<option value="' . $row['rank_id'] . '"' . $selected . '>' . $row['rank_title'] . '</option>';
2026                }
2027                $db->sql_freeresult($result);
2028
2029                $template->assign_vars(array(
2030                    'S_RANK'            => true,
2031                    'S_RANK_OPTIONS'    => $s_rank_options)
2032                );
2033
2034            break;
2035
2036            case 'sig':
2037
2038                if (!function_exists('display_custom_bbcodes'))
2039                {
2040                    include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
2041                }
2042
2043                $enable_bbcode    = ($config['allow_sig_bbcode']) ? $this->optionget($user_row, 'sig_bbcode') : false;
2044                $enable_smilies    = ($config['allow_sig_smilies']) ? $this->optionget($user_row, 'sig_smilies') : false;
2045                $enable_urls    = ($config['allow_sig_links']) ? $this->optionget($user_row, 'sig_links') : false;
2046
2047                $bbcode_flags = ($enable_bbcode ? OPTION_FLAG_BBCODE : 0) + ($enable_smilies ? OPTION_FLAG_SMILIES : 0) + ($enable_urls ? OPTION_FLAG_LINKS : 0);
2048
2049                $decoded_message    = generate_text_for_edit($user_row['user_sig'], $user_row['user_sig_bbcode_uid'], $bbcode_flags);
2050                $signature            = $request->variable('signature', $decoded_message['text'], true);
2051                $signature_preview    = '';
2052
2053                if ($submit || $request->is_set_post('preview'))
2054                {
2055                    $enable_bbcode    = ($config['allow_sig_bbcode']) ? !$request->variable('disable_bbcode', false) : false;
2056                    $enable_smilies    = ($config['allow_sig_smilies']) ? !$request->variable('disable_smilies', false) : false;
2057                    $enable_urls    = ($config['allow_sig_links']) ? !$request->variable('disable_magic_url', false) : false;
2058
2059                    if (!check_form_key($form_name))
2060                    {
2061                        $error[] = 'FORM_INVALID';
2062                    }
2063                }
2064
2065                $bbcode_uid = $bbcode_bitfield = $bbcode_flags = '';
2066                $warn_msg = generate_text_for_storage(
2067                    $signature,
2068                    $bbcode_uid,
2069                    $bbcode_bitfield,
2070                    $bbcode_flags,
2071                    $enable_bbcode,
2072                    $enable_urls,
2073                    $enable_smilies,
2074                    $config['allow_sig_img'],
2075                    true,
2076                    $config['allow_sig_links'],
2077                    'sig'
2078                );
2079
2080                if (count($warn_msg))
2081                {
2082                    $error += $warn_msg;
2083                }
2084
2085                if (!$submit)
2086                {
2087                    // Parse it for displaying
2088                    $signature_preview = generate_text_for_display($signature, $bbcode_uid, $bbcode_bitfield, $bbcode_flags);
2089                }
2090                else
2091                {
2092                    if (!count($error))
2093                    {
2094                        $this->optionset($user_row, 'sig_bbcode', $enable_bbcode);
2095                        $this->optionset($user_row, 'sig_smilies', $enable_smilies);
2096                        $this->optionset($user_row, 'sig_links', $enable_urls);
2097
2098                        $sql_ary = array(
2099                            'user_sig'                    => $signature,
2100                            'user_options'                => $user_row['user_options'],
2101                            'user_sig_bbcode_uid'        => $bbcode_uid,
2102                            'user_sig_bbcode_bitfield'    => $bbcode_bitfield,
2103                        );
2104
2105                        /**
2106                        * Modify user signature before it is stored in the DB
2107                        *
2108                        * @event core.acp_users_modify_signature_sql_ary
2109                        * @var    array    user_row    Array with user data
2110                        * @var    array    sql_ary        Array with user signature data to be updated in the DB
2111                        * @since 3.2.4-RC1
2112                        */
2113                        $vars = array('user_row', 'sql_ary');
2114                        extract($phpbb_dispatcher->trigger_event('core.acp_users_modify_signature_sql_ary', compact($vars)));
2115
2116                        $sql = 'UPDATE ' . USERS_TABLE . '
2117                            SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
2118                            WHERE user_id = ' . $user_id;
2119                        $db->sql_query($sql);
2120
2121                        trigger_error($user->lang['USER_SIG_UPDATED'] . adm_back_link($this->u_action . '&amp;u=' . $user_id));
2122                    }
2123                }
2124
2125                // Replace "error" strings with their real, localised form
2126                $error = array_map(array($user, 'lang'), $error);
2127
2128                if ($request->is_set_post('preview'))
2129                {
2130                    $decoded_message = generate_text_for_edit($signature, $bbcode_uid, $bbcode_flags);
2131                }
2132
2133                $template->assign_vars(array(
2134                    'S_SIGNATURE'        => true,
2135
2136                    'SIGNATURE'            => $decoded_message['text'],
2137                    'SIGNATURE_PREVIEW'    => $signature_preview,
2138
2139                    'S_BBCODE_CHECKED'        => (!$enable_bbcode) ? ' checked="checked"' : '',
2140                    'S_SMILIES_CHECKED'        => (!$enable_smilies) ? ' checked="checked"' : '',
2141                    'S_MAGIC_URL_CHECKED'    => (!$enable_urls) ? ' checked="checked"' : '',
2142
2143                    'BBCODE_STATUS'            => $user->lang(($config['allow_sig_bbcode'] ? 'BBCODE_IS_ON' : 'BBCODE_IS_OFF'), '<a href="' . $controller_helper->route('phpbb_help_bbcode_controller') . '">', '</a>'),
2144                    'SMILIES_STATUS'        => ($config['allow_sig_smilies']) ? $user->lang['SMILIES_ARE_ON'] : $user->lang['SMILIES_ARE_OFF'],
2145                    'IMG_STATUS'            => ($config['allow_sig_img']) ? $user->lang['IMAGES_ARE_ON'] : $user->lang['IMAGES_ARE_OFF'],
2146                    'URL_STATUS'            => ($config['allow_sig_links']) ? $user->lang['URL_IS_ON'] : $user->lang['URL_IS_OFF'],
2147
2148                    'L_SIGNATURE_EXPLAIN'    => $user->lang('SIGNATURE_EXPLAIN', (int) $config['max_sig_chars']),
2149
2150                    'S_BBCODE_ALLOWED'        => $config['allow_sig_bbcode'],
2151                    'S_SMILIES_ALLOWED'        => $config['allow_sig_smilies'],
2152                    'S_BBCODE_IMG'            => ($config['allow_sig_img']) ? true : false,
2153                    'S_LINKS_ALLOWED'        => ($config['allow_sig_links']) ? true : false)
2154                );
2155
2156                // Assigning custom bbcodes
2157                display_custom_bbcodes();
2158
2159            break;
2160
2161            case 'attach':
2162                /* @var $pagination \phpbb\pagination */
2163                $pagination = $phpbb_container->get('pagination');
2164
2165                $start        = $request->variable('start', 0);
2166                $deletemark = (isset($_POST['delmarked'])) ? true : false;
2167                $marked        = $request->variable('mark', array(0));
2168
2169                // Sort keys
2170                $sort_key    = $request->variable('sk', 'a');
2171                $sort_dir    = $request->variable('sd', 'd');
2172
2173                if ($deletemark && count($marked))
2174                {
2175                    $sql = 'SELECT attach_id
2176                        FROM ' . ATTACHMENTS_TABLE . '
2177                        WHERE poster_id = ' . $user_id . '
2178                            AND is_orphan = 0
2179                            AND ' . $db->sql_in_set('attach_id', $marked);
2180                    $result = $db->sql_query($sql);
2181
2182                    $marked = array();
2183                    while ($row = $db->sql_fetchrow($result))
2184                    {
2185                        $marked[] = $row['attach_id'];
2186                    }
2187                    $db->sql_freeresult($result);
2188                }
2189
2190                if ($deletemark && count($marked))
2191                {
2192                    if (confirm_box(true))
2193                    {
2194                        $sql = 'SELECT real_filename
2195                            FROM ' . ATTACHMENTS_TABLE . '
2196                            WHERE ' . $db->sql_in_set('attach_id', $marked);
2197                        $result = $db->sql_query($sql);
2198
2199                        $log_attachments = array();
2200                        while ($row = $db->sql_fetchrow($result))
2201                        {
2202                            $log_attachments[] = $row['real_filename'];
2203                        }
2204                        $db->sql_freeresult($result);
2205
2206                        /** @var \phpbb\attachment\manager $attachment_manager */
2207                        $attachment_manager = $phpbb_container->get('attachment.manager');
2208                        $attachment_manager->delete('attach', $marked);
2209                        unset($attachment_manager);
2210
2211                        $message = (count($log_attachments) == 1) ? $user->lang['ATTACHMENT_DELETED'] : $user->lang['ATTACHMENTS_DELETED'];
2212
2213                        $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTACHMENTS_DELETED', false, array(implode($user->lang['COMMA_SEPARATOR'], $log_attachments)));
2214                        trigger_error($message . adm_back_link($this->u_action . '&amp;u=' . $user_id));
2215                    }
2216                    else
2217                    {
2218                        confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
2219                            'u'                => $user_id,
2220                            'i'                => $id,
2221                            'mode'            => $mode,
2222                            'action'        => $action,
2223                            'delmarked'        => true,
2224                            'mark'            => $marked))
2225                        );
2226                    }
2227                }
2228
2229                $sk_text = array('a' => $user->lang['SORT_FILENAME'], 'c' => $user->lang['SORT_EXTENSION'], 'd' => $user->lang['SORT_SIZE'], 'e' => $user->lang['SORT_DOWNLOADS'], 'f' => $user->lang['SORT_POST_TIME'], 'g' => $user->lang['SORT_TOPIC_TITLE']);
2230                $sk_sql = array('a' => 'a.real_filename', 'c' => 'a.extension', 'd' => 'a.filesize', 'e' => 'a.download_count', 'f' => 'a.filetime', 'g' => 't.topic_title');
2231
2232                $sd_text = array('a' => $user->lang['ASCENDING'], 'd' => $user->lang['DESCENDING']);
2233
2234                $s_sort_key = '';
2235                foreach ($sk_text as $key => $value)
2236                {
2237                    $selected = ($sort_key == $key) ? ' selected="selected"' : '';
2238                    $s_sort_key .= '<option value="' . $key . '"' . $selected . '>' . $value . '</option>';
2239                }
2240
2241                $s_sort_dir = '';
2242                foreach ($sd_text as $key => $value)
2243                {
2244                    $selected = ($sort_dir == $key) ? ' selected="selected"' : '';
2245                    $s_sort_dir .= '<option value="' . $key . '"' . $selected . '>' . $value . '</option>';
2246                }
2247
2248                if (!isset($sk_sql[$sort_key]))
2249                {
2250                    $sort_key = 'a';
2251                }
2252
2253                $order_by = $sk_sql[$sort_key] . ' ' . (($sort_dir == 'a') ? 'ASC' : 'DESC');
2254
2255                $sql = 'SELECT COUNT(attach_id) as num_attachments
2256                    FROM ' . ATTACHMENTS_TABLE . "
2257                    WHERE poster_id = $user_id
2258                        AND is_orphan = 0";
2259                $result = $db->sql_query_limit($sql, 1);
2260                $num_attachments = (int) $db->sql_fetchfield('num_attachments');
2261                $db->sql_freeresult($result);
2262
2263                $sql = 'SELECT a.*, t.topic_title, p.message_subject as message_title
2264                    FROM ' . ATTACHMENTS_TABLE . ' a
2265                        LEFT JOIN ' . TOPICS_TABLE . ' t ON (a.topic_id = t.topic_id
2266                            AND a.in_message = 0)
2267                        LEFT JOIN ' . PRIVMSGS_TABLE . ' p ON (a.post_msg_id = p.msg_id
2268                            AND a.in_message = 1)
2269                    WHERE a.poster_id = ' . $user_id . "
2270                        AND a.is_orphan = 0
2271                    ORDER BY $order_by";
2272                $result = $db->sql_query_limit($sql, $config['topics_per_page'], $start);
2273
2274                while ($row = $db->sql_fetchrow($result))
2275                {
2276                    if ($row['in_message'])
2277                    {
2278                        $view_topic = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&amp;p={$row['post_msg_id']}");
2279                    }
2280                    else
2281                    {
2282                        $view_topic = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "p={$row['post_msg_id']}") . '#p' . $row['post_msg_id'];
2283                    }
2284
2285                    $template->assign_block_vars('attach', array(
2286                        'REAL_FILENAME'        => $row['real_filename'],
2287                        'COMMENT'            => nl2br($row['attach_comment']),
2288                        'EXTENSION'            => $row['extension'],
2289                        'SIZE'                => get_formatted_filesize($row['filesize']),
2290                        'DOWNLOAD_COUNT'    => $row['download_count'],
2291                        'POST_TIME'            => $user->format_date($row['filetime']),
2292                        'TOPIC_TITLE'        => ($row['in_message']) ? $row['message_title'] : $row['topic_title'],
2293
2294                        'ATTACH_ID'            => $row['attach_id'],
2295                        'POST_ID'            => $row['post_msg_id'],
2296                        'TOPIC_ID'            => $row['topic_id'],
2297
2298                        'S_IN_MESSAGE'        => $row['in_message'],
2299
2300                        'U_DOWNLOAD'        => $controller_helper->route(
2301                            'phpbb_storage_attachment',
2302                            [
2303                                'id'        => (int) $row['attach_id'],
2304                                'filename'    => $row['real_filename'],
2305                            ]
2306                        ),
2307                        'U_VIEW_TOPIC'        => $view_topic)
2308                    );
2309                }
2310                $db->sql_freeresult($result);
2311
2312                $base_url = $this->u_action . "&amp;u=$user_id&amp;sk=$sort_key&amp;sd=$sort_dir";
2313                $pagination->generate_template_pagination($base_url, 'pagination', 'start', $num_attachments, $config['topics_per_page'], $start);
2314
2315                $template->assign_vars(array(
2316                    'S_ATTACHMENTS'        => true,
2317                    'S_SORT_KEY'        => $s_sort_key,
2318                    'S_SORT_DIR'        => $s_sort_dir,
2319                ));
2320
2321            break;
2322
2323            case 'groups':
2324
2325                if (!function_exists('group_user_attributes'))
2326                {
2327                    include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
2328                }
2329
2330                $user->add_lang(array('groups', 'acp/groups'));
2331                $group_id = $request->variable('g', 0);
2332
2333                if ($group_id)
2334                {
2335                    // Check the founder only entry for this group to make sure everything is well
2336                    $sql = 'SELECT group_founder_manage
2337                        FROM ' . GROUPS_TABLE . '
2338                        WHERE group_id = ' . $group_id;
2339                    $result = $db->sql_query($sql);
2340                    $founder_manage = (int) $db->sql_fetchfield('group_founder_manage');
2341                    $db->sql_freeresult($result);
2342
2343                    if ($user->data['user_type'] != USER_FOUNDER && $founder_manage)
2344                    {
2345                        trigger_error($user->lang['NOT_ALLOWED_MANAGE_GROUP'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
2346                    }
2347                }
2348
2349                switch ($action)
2350                {
2351                    case 'demote':
2352                    case 'promote':
2353                    case 'default':
2354                        if (!$group_id)
2355                        {
2356                            trigger_error($user->lang['NO_GROUP'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
2357                        }
2358
2359                        if (!check_link_hash($request->variable('hash', ''), 'acp_users'))
2360                        {
2361                            trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
2362                        }
2363
2364                        group_user_attributes($action, $group_id, $user_id);
2365
2366                        if ($action == 'default')
2367                        {
2368                            $user_row['group_id'] = $group_id;
2369                        }
2370                    break;
2371
2372                    case 'delete':
2373
2374                        if (confirm_box(true))
2375                        {
2376                            if (!$group_id)
2377                            {
2378                                trigger_error($user->lang['NO_GROUP'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
2379                            }
2380
2381                            if ($error = group_user_del($group_id, $user_id))
2382                            {
2383                                trigger_error($user->lang[$error] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
2384                            }
2385
2386                            $error = array();
2387
2388                            // The delete action was successful - therefore update the user row...
2389                            $sql = 'SELECT u.*, s.*
2390                                FROM ' . USERS_TABLE . ' u
2391                                    LEFT JOIN ' . SESSIONS_TABLE . ' s ON (s.session_user_id = u.user_id)
2392                                WHERE u.user_id = ' . $user_id . '
2393                                ORDER BY s.session_time DESC';
2394                            $result = $db->sql_query_limit($sql, 1);
2395                            $user_row = $db->sql_fetchrow($result);
2396                            $db->sql_freeresult($result);
2397                        }
2398                        else
2399                        {
2400                            confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
2401                                'u'                => $user_id,
2402                                'i'                => $id,
2403                                'mode'            => $mode,
2404                                'action'        => $action,
2405                                'g'                => $group_id))
2406                            );
2407                        }
2408
2409                    break;
2410
2411                    case 'approve':
2412
2413                        if (confirm_box(true))
2414                        {
2415                            if (!$group_id)
2416                            {
2417                                trigger_error($user->lang['NO_GROUP'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
2418                            }
2419                            group_user_attributes($action, $group_id, $user_id);
2420                        }
2421                        else
2422                        {
2423                            confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
2424                                'u'                => $user_id,
2425                                'i'                => $id,
2426                                'mode'            => $mode,
2427                                'action'        => $action,
2428                                'g'                => $group_id))
2429                            );
2430                        }
2431
2432                    break;
2433                }
2434
2435                // Add user to group?
2436                if ($submit)
2437                {
2438
2439                    if (!check_form_key($form_name))
2440                    {
2441                        trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
2442                    }
2443
2444                    if (!$group_id)
2445                    {
2446                        trigger_error($user->lang['NO_GROUP'] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
2447                    }
2448
2449                    // Add user/s to group
2450                    if ($error = group_user_add($group_id, $user_id))
2451                    {
2452                        trigger_error($user->lang[$error] . adm_back_link($this->u_action . '&amp;u=' . $user_id), E_USER_WARNING);
2453                    }
2454
2455                    $error = array();
2456                }
2457
2458                /** @var \phpbb\group\helper $group_helper */
2459                $group_helper = $phpbb_container->get('group_helper');
2460
2461                $sql = 'SELECT ug.*, g.*
2462                    FROM ' . GROUPS_TABLE . ' g, ' . USER_GROUP_TABLE . " ug
2463                    WHERE ug.user_id = $user_id
2464                        AND g.group_id = ug.group_id
2465                    ORDER BY g.group_type DESC, ug.user_pending ASC, g.group_name";
2466                $result = $db->sql_query($sql);
2467
2468                $i = 0;
2469                $group_data = $id_ary = array();
2470                while ($row = $db->sql_fetchrow($result))
2471                {
2472                    $type = ($row['group_type'] == GROUP_SPECIAL) ? 'special' : (($row['user_pending']) ? 'pending' : 'normal');
2473
2474                    $group_data[$type][$i]['group_id']        = $row['group_id'];
2475                    $group_data[$type][$i]['group_name']    = $row['group_name'];
2476                    $group_data[$type][$i]['group_leader']    = ($row['group_leader']) ? 1 : 0;
2477
2478                    $id_ary[] = $row['group_id'];
2479
2480                    $i++;
2481                }
2482                $db->sql_freeresult($result);
2483
2484                // Select box for other groups
2485                $sql = 'SELECT group_id, group_name, group_type, group_founder_manage
2486                    FROM ' . GROUPS_TABLE . '
2487                    ' . ((count($id_ary)) ? 'WHERE ' . $db->sql_in_set('group_id', $id_ary, true) : '') . '
2488                    ORDER BY group_type DESC, group_name ASC';
2489                $result = $db->sql_query($sql);
2490
2491                $s_group_options = '';
2492                while ($row = $db->sql_fetchrow($result))
2493                {
2494                    if (!$config['coppa_enable'] && $row['group_name'] == 'REGISTERED_COPPA')
2495                    {
2496                        continue;
2497                    }
2498
2499                    // Do not display those groups not allowed to be managed
2500                    if ($user->data['user_type'] != USER_FOUNDER && $row['group_founder_manage'])
2501                    {
2502                        continue;
2503                    }
2504
2505                    $s_group_options .= '<option' . (($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '') . ' value="' . $row['group_id'] . '">' . $group_helper->get_name($row['group_name']) . '</option>';
2506                }
2507                $db->sql_freeresult($result);
2508
2509                $current_type = '';
2510                foreach ($group_data as $group_type => $data_ary)
2511                {
2512                    if ($current_type != $group_type)
2513                    {
2514                        $template->assign_block_vars('group', array(
2515                            'S_NEW_GROUP_TYPE'        => true,
2516                            'GROUP_TYPE'            => $user->lang['USER_GROUP_' . strtoupper($group_type)])
2517                        );
2518                    }
2519
2520                    foreach ($data_ary as $data)
2521                    {
2522                        $template->assign_block_vars('group', array(
2523                            'U_EDIT_GROUP'        => append_sid("{$phpbb_admin_path}index.$phpEx", "i=groups&amp;mode=manage&amp;action=edit&amp;u=$user_id&amp;g={$data['group_id']}&amp;back_link=acp_users_groups"),
2524                            'U_DEFAULT'            => $this->u_action . "&amp;action=default&amp;u=$user_id&amp;g=" . $data['group_id'] . '&amp;hash=' . generate_link_hash('acp_users'),
2525                            'U_DEMOTE_PROMOTE'    => $this->u_action . '&amp;action=' . (($data['group_leader']) ? 'demote' : 'promote') . "&amp;u=$user_id&amp;g=" . $data['group_id'] . '&amp;hash=' . generate_link_hash('acp_users'),
2526                            'U_DELETE'            => count($id_ary) > 1 ? $this->u_action . "&amp;action=delete&amp;u=$user_id&amp;g=" . $data['group_id'] : '',
2527                            'U_APPROVE'            => ($group_type == 'pending') ? $this->u_action . "&amp;action=approve&amp;u=$user_id&amp;g=" . $data['group_id'] : '',
2528
2529                            'GROUP_NAME'        => $group_helper->get_name($data['group_name']),
2530                            'L_DEMOTE_PROMOTE'    => ($data['group_leader']) ? $user->lang['GROUP_DEMOTE'] : $user->lang['GROUP_PROMOTE'],
2531
2532                            'S_IS_MEMBER'        => ($group_type != 'pending') ? true : false,
2533                            'S_NO_DEFAULT'        => ($user_row['group_id'] != $data['group_id']) ? true : false,
2534                            'S_SPECIAL_GROUP'    => ($group_type == 'special') ? true : false,
2535                            )
2536                        );
2537                    }
2538                }
2539
2540                $template->assign_vars(array(
2541                    'S_GROUPS'            => true,
2542                    'S_GROUP_OPTIONS'    => $s_group_options)
2543                );
2544
2545            break;
2546
2547            case 'perm':
2548
2549                if (!class_exists('auth_admin'))
2550                {
2551                    include($phpbb_root_path . 'includes/acp/auth.' . $phpEx);
2552                }
2553
2554                $auth_admin = new auth_admin();
2555
2556                $user->add_lang('acp/permissions');
2557                add_permission_language();
2558
2559                $forum_id = $request->variable('f', 0);
2560
2561                // Global Permissions
2562                if (!$forum_id)
2563                {
2564                    // Select auth options
2565                    $sql = 'SELECT auth_option, is_local, is_global
2566                        FROM ' . ACL_OPTIONS_TABLE . '
2567                        WHERE auth_option ' . $db->sql_like_expression($db->get_any_char() . '_') . '
2568                            AND is_global = 1
2569                        ORDER BY auth_option';
2570                    $result = $db->sql_query($sql);
2571
2572                    $hold_ary = array();
2573
2574                    while ($row = $db->sql_fetchrow($result))
2575                    {
2576                        $hold_ary = $auth_admin->get_mask('view', $user_id, false, false, $row['auth_option'], 'global', ACL_NEVER);
2577                        $auth_admin->display_mask('view', $row['auth_option'], $hold_ary, 'user', false, false);
2578                    }
2579                    $db->sql_freeresult($result);
2580
2581                    unset($hold_ary);
2582                }
2583                else
2584                {
2585                    $sql = 'SELECT auth_option, is_local, is_global
2586                        FROM ' . ACL_OPTIONS_TABLE . "
2587                        WHERE auth_option " . $db->sql_like_expression($db->get_any_char() . '_') . "
2588                            AND is_local = 1
2589                        ORDER BY is_global DESC, auth_option";
2590                    $result = $db->sql_query($sql);
2591
2592                    while ($row = $db->sql_fetchrow($result))
2593                    {
2594                        $hold_ary = $auth_admin->get_mask('view', $user_id, false, $forum_id, $row['auth_option'], 'local', ACL_NEVER);
2595                        $auth_admin->display_mask('view', $row['auth_option'], $hold_ary, 'user', true, false);
2596                    }
2597                    $db->sql_freeresult($result);
2598                }
2599
2600                $s_forum_options = '<option value="0"' . ((!$forum_id) ? ' selected="selected"' : '') . '>' . $user->lang['VIEW_GLOBAL_PERMS'] . '</option>';
2601                $s_forum_options .= make_forum_select($forum_id, false, true, false, false, false);
2602
2603                $template->assign_vars(array(
2604                    'S_PERMISSIONS'                => true,
2605
2606                    'S_GLOBAL'                    => (!$forum_id) ? true : false,
2607                    'S_FORUM_OPTIONS'            => $s_forum_options,
2608
2609                    'U_ACTION'                    => $this->u_action . '&amp;u=' . $user_id,
2610                    'U_USER_PERMISSIONS'        => append_sid("{$phpbb_admin_path}index.$phpEx" ,'i=permissions&amp;mode=setting_user_global&amp;user_id[]=' . $user_id),
2611                    'U_USER_FORUM_PERMISSIONS'    => append_sid("{$phpbb_admin_path}index.$phpEx", 'i=permissions&amp;mode=setting_user_local&amp;user_id[]=' . $user_id))
2612                );
2613
2614            break;
2615
2616            default:
2617                $u_action = $this->u_action;
2618
2619                /**
2620                * Additional modes provided by extensions
2621                *
2622                * @event core.acp_users_mode_add
2623                * @var    string    mode            New mode
2624                * @var    int        user_id            User id of the user to manage
2625                * @var    array    user_row        Array with user data
2626                * @var    array    error            Array with errors data
2627                * @var    string    u_action        The u_action link
2628                * @since 3.2.2-RC1
2629                * @changed 3.2.10-RC1 Added u_action
2630                */
2631                $vars = array('mode', 'user_id', 'user_row', 'error', 'u_action');
2632                extract($phpbb_dispatcher->trigger_event('core.acp_users_mode_add', compact($vars)));
2633
2634                unset($u_action);
2635            break;
2636        }
2637
2638        // Assign general variables
2639        $template->assign_vars(array(
2640            'S_ERROR'            => (count($error)) ? true : false,
2641            'ERROR_MSG'            => (count($error)) ? implode('<br />', $error) : '')
2642        );
2643    }
2644
2645    /**
2646    * Set option bit field for user options in a user row array.
2647    *
2648    * Optionset replacement for this module based on $user->optionset.
2649    *
2650    * @param array $user_row Row from the users table.
2651    * @param int $key Option key, as defined in $user->keyoptions property.
2652    * @param bool $value True to set the option, false to clear the option.
2653    * @param int $data Current bit field value, or false to use $user_row['user_options']
2654    * @return int|bool If $data is false, the bit field is modified and
2655    *                  written back to $user_row['user_options'], and
2656    *                  return value is true if the bit field changed and
2657    *                  false otherwise. If $data is not false, the new
2658    *                  bitfield value is returned.
2659    */
2660    function optionset(&$user_row, $key, $value, $data = false)
2661    {
2662        global $user;
2663
2664        $var = ($data !== false) ? $data : $user_row['user_options'];
2665
2666        $new_var = phpbb_optionset($user->keyoptions[$key], $value, $var);
2667
2668        if ($data === false)
2669        {
2670            if ($new_var != $var)
2671            {
2672                $user_row['user_options'] = $new_var;
2673                return true;
2674            }
2675            else
2676            {
2677                return false;
2678            }
2679        }
2680        else
2681        {
2682            return $new_var;
2683        }
2684    }
2685
2686    /**
2687    * Get option bit field from user options in a user row array.
2688    *
2689    * Optionget replacement for this module based on $user->optionget.
2690    *
2691    * @param array $user_row Row from the users table.
2692    * @param int $key option key, as defined in $user->keyoptions property.
2693    * @param int $data bit field value to use, or false to use $user_row['user_options']
2694    * @return bool true if the option is set in the bit field, false otherwise
2695    */
2696    function optionget(&$user_row, $key, $data = false)
2697    {
2698        global $user;
2699
2700        $var = ($data !== false) ? $data : $user_row['user_options'];
2701        return phpbb_optionget($user->keyoptions[$key], $var);
2702    }
2703}