Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 141
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
acp_ranks
0.00% covered (danger)
0.00%
0 / 139
0.00% covered (danger)
0.00%
0 / 1
1260
0.00% covered (danger)
0.00%
0 / 1
 main
0.00% covered (danger)
0.00%
0 / 139
0.00% covered (danger)
0.00%
0 / 1
1260
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14/**
15* @ignore
16*/
17if (!defined('IN_PHPBB'))
18{
19    exit;
20}
21
22class acp_ranks
23{
24    var $u_action;
25
26    function main($id, $mode)
27    {
28        global $db, $user, $template, $cache, $request, $phpbb_dispatcher;
29        global $config, $phpbb_root_path, $phpbb_admin_path, $phpbb_log;
30
31        $user->add_lang('acp/posting');
32
33        // Set up general vars
34        $action = $request->variable('action', '');
35        $action = (isset($_POST['add'])) ? 'add' : $action;
36        $action = (isset($_POST['save'])) ? 'save' : $action;
37        $rank_id = $request->variable('id', 0);
38
39        $this->tpl_name = 'acp_ranks';
40        $this->page_title = 'ACP_MANAGE_RANKS';
41
42        $form_name = 'acp_ranks';
43        add_form_key($form_name);
44
45        switch ($action)
46        {
47            case 'save':
48
49                if (!check_form_key($form_name))
50                {
51                    trigger_error($user->lang['FORM_INVALID']. adm_back_link($this->u_action), E_USER_WARNING);
52                }
53                $rank_title = $request->variable('title', '', true);
54                $special_rank = $request->variable('special_rank', 0);
55                $min_posts = ($special_rank) ? 0 : max(0, $request->variable('min_posts', 0));
56                $rank_image = $request->variable('rank_image', '');
57
58                // The rank image has to be a jp(e)g, gif, png, svg or webp
59                if ($rank_image != '' && !preg_match('#(\.gif|\.png|\.jpg|\.jpeg|\.svg|\.webp)$#i', $rank_image))
60                {
61                    $rank_image = '';
62                }
63
64                if (!$rank_title)
65                {
66                    trigger_error($user->lang['NO_RANK_TITLE'] . adm_back_link($this->u_action), E_USER_WARNING);
67                }
68
69                $sql_ary = array(
70                    'rank_title'        => $rank_title,
71                    'rank_special'        => $special_rank,
72                    'rank_min'            => $min_posts,
73                    'rank_image'        => html_entity_decode($rank_image, ENT_COMPAT)
74                );
75
76                /**
77                * Modify the SQL array when saving a rank
78                *
79                * @event core.acp_ranks_save_modify_sql_ary
80                * @var    int        rank_id        The ID of the rank (if available)
81                * @var    array    sql_ary        Array with the rank's data
82                * @since 3.1.0-RC3
83                */
84                $vars = array('rank_id', 'sql_ary');
85                extract($phpbb_dispatcher->trigger_event('core.acp_ranks_save_modify_sql_ary', compact($vars)));
86
87                if ($rank_id)
88                {
89                    $sql = 'UPDATE ' . RANKS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . " WHERE rank_id = $rank_id";
90                    $message = $user->lang['RANK_UPDATED'];
91
92                    $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_RANK_UPDATED', false, array($rank_title));
93                }
94                else
95                {
96                    $sql = 'INSERT INTO ' . RANKS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
97                    $message = $user->lang['RANK_ADDED'];
98
99                    $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_RANK_ADDED', false, array($rank_title));
100                }
101                $db->sql_query($sql);
102
103                $cache->destroy('_ranks');
104
105                trigger_error($message . adm_back_link($this->u_action));
106
107            break;
108
109            case 'delete':
110
111                if (!$rank_id)
112                {
113                    trigger_error($user->lang['MUST_SELECT_RANK'] . adm_back_link($this->u_action), E_USER_WARNING);
114                }
115
116                if (confirm_box(true))
117                {
118                    $sql = 'SELECT rank_title
119                        FROM ' . RANKS_TABLE . '
120                        WHERE rank_id = ' . $rank_id;
121                    $result = $db->sql_query($sql);
122                    $rank_title = (string) $db->sql_fetchfield('rank_title');
123                    $db->sql_freeresult($result);
124
125                    $sql = 'DELETE FROM ' . RANKS_TABLE . "
126                        WHERE rank_id = $rank_id";
127                    $db->sql_query($sql);
128
129                    $sql = 'UPDATE ' . USERS_TABLE . "
130                        SET user_rank = 0
131                        WHERE user_rank = $rank_id";
132                    $db->sql_query($sql);
133
134                    $cache->destroy('_ranks');
135
136                    $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_RANK_REMOVED', false, array($rank_title));
137
138                    if ($request->is_ajax())
139                    {
140                        $json_response = new \phpbb\json_response;
141                        $json_response->send(array(
142                            'MESSAGE_TITLE'    => $user->lang['INFORMATION'],
143                            'MESSAGE_TEXT'    => $user->lang['RANK_REMOVED'],
144                            'REFRESH_DATA'    => array(
145                                'time'    => 3
146                            )
147                        ));
148                    }
149                }
150                else
151                {
152                    confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
153                        'i'            => $id,
154                        'mode'        => $mode,
155                        'rank_id'    => $rank_id,
156                        'action'    => 'delete',
157                    )));
158                }
159
160            break;
161
162            case 'edit':
163            case 'add':
164
165                $ranks = $existing_imgs = array();
166
167                $sql = 'SELECT *
168                    FROM ' . RANKS_TABLE . '
169                    ORDER BY rank_min ASC, rank_special ASC';
170                $result = $db->sql_query($sql);
171
172                while ($row = $db->sql_fetchrow($result))
173                {
174                    $existing_imgs[] = $row['rank_image'];
175
176                    if ($action == 'edit' && $rank_id == $row['rank_id'])
177                    {
178                        $ranks = $row;
179                    }
180                }
181                $db->sql_freeresult($result);
182
183                $imglist = filelist($phpbb_root_path . $config['ranks_path'], '');
184                $edit_img = $filename_list = '';
185
186                foreach ($imglist as $path => $img_ary)
187                {
188                    sort($img_ary);
189
190                    foreach ($img_ary as $img)
191                    {
192                        $img = $path . $img;
193
194                        if ($ranks && $img == $ranks['rank_image'])
195                        {
196                            $selected = ' selected="selected"';
197                            $edit_img = $img;
198                        }
199                        else
200                        {
201                            $selected = '';
202                        }
203
204                        if (strlen($img) > 255)
205                        {
206                            continue;
207                        }
208
209                        $filename_list .= '<option value="' . htmlspecialchars($img, ENT_COMPAT) . '"' . $selected . '>' . $img . ((in_array($img, $existing_imgs)) ? ' ' . $user->lang['RANK_IMAGE_IN_USE'] : '') . '</option>';
210                    }
211                }
212
213                $filename_list = '<option value=""' . (($edit_img == '') ? ' selected="selected"' : '') . '>----------</option>' . $filename_list;
214                unset($existing_imgs, $imglist);
215
216                $tpl_ary = array(
217                    'S_EDIT'            => true,
218                    'U_BACK'            => $this->u_action,
219                    'RANKS_PATH'        => $phpbb_root_path . $config['ranks_path'],
220                    'U_ACTION'            => $this->u_action . '&amp;id=' . $rank_id,
221
222                    'RANK_TITLE'        => (isset($ranks['rank_title'])) ? $ranks['rank_title'] : '',
223                    'S_FILENAME_LIST'    => $filename_list,
224                    'RANK_IMAGE'        => ($edit_img) ? $phpbb_root_path . $config['ranks_path'] . '/' . $edit_img : htmlspecialchars($phpbb_admin_path, ENT_COMPAT) . 'images/spacer.gif',
225                    'S_SPECIAL_RANK'    => (isset($ranks['rank_special']) && $ranks['rank_special']) ? true : false,
226                    'MIN_POSTS'            => (isset($ranks['rank_min']) && !$ranks['rank_special']) ? $ranks['rank_min'] : 0,
227                );
228
229                /**
230                * Modify the template output array for editing/adding ranks
231                *
232                * @event core.acp_ranks_edit_modify_tpl_ary
233                * @var    array    ranks        Array with the rank's data
234                * @var    array    tpl_ary        Array with the rank's template data
235                * @since 3.1.0-RC3
236                */
237                $vars = array('ranks', 'tpl_ary');
238                extract($phpbb_dispatcher->trigger_event('core.acp_ranks_edit_modify_tpl_ary', compact($vars)));
239
240                $template->assign_vars($tpl_ary);
241                return;
242
243            break;
244        }
245
246        $template->assign_vars(array(
247            'U_ACTION'        => $this->u_action)
248        );
249
250        $sql = 'SELECT *
251            FROM ' . RANKS_TABLE . '
252            ORDER BY rank_special DESC, rank_min ASC, rank_title ASC';
253        $result = $db->sql_query($sql);
254
255        while ($row = $db->sql_fetchrow($result))
256        {
257            $rank_row = array(
258                'S_RANK_IMAGE'        => ($row['rank_image']) ? true : false,
259                'S_SPECIAL_RANK'    => ($row['rank_special']) ? true : false,
260
261                'RANK_IMAGE'        => $phpbb_root_path . $config['ranks_path'] . '/' . $row['rank_image'],
262                'RANK_TITLE'        => $row['rank_title'],
263                'MIN_POSTS'            => $row['rank_min'],
264
265                'U_EDIT'            => $this->u_action . '&amp;action=edit&amp;id=' . $row['rank_id'],
266                'U_DELETE'            => $this->u_action . '&amp;action=delete&amp;id=' . $row['rank_id'],
267            );
268
269            /**
270            * Modify the template output array for each listed rank
271            *
272            * @event core.acp_ranks_list_modify_rank_row
273            * @var    array    row            Array with the rank's data
274            * @var    array    rank_row    Array with the rank's template data
275            * @since 3.1.0-RC3
276            */
277            $vars = array('row', 'rank_row');
278            extract($phpbb_dispatcher->trigger_event('core.acp_ranks_list_modify_rank_row', compact($vars)));
279
280            $template->assign_block_vars('ranks', $rank_row);
281        }
282        $db->sql_freeresult($result);
283
284    }
285}