Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 98
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
acp_words
0.00% covered (danger)
0.00%
0 / 96
0.00% covered (danger)
0.00%
0 / 1
380
0.00% covered (danger)
0.00%
0 / 1
 main
0.00% covered (danger)
0.00%
0 / 96
0.00% covered (danger)
0.00%
0 / 1
380
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
22/**
23  * @todo {words} check regular expressions for special char replacements (stored specialchared in db)
24  */
25class acp_words
26{
27    var $u_action;
28
29    function main($id, $mode)
30    {
31        global $db, $user, $template, $cache, $phpbb_log, $request, $phpbb_container;
32
33        $user->add_lang('acp/posting');
34
35        // Set up general vars
36        $action = $request->variable('action', '');
37        $action = (isset($_POST['add'])) ? 'add' : ((isset($_POST['save'])) ? 'save' : $action);
38
39        $s_hidden_fields = '';
40        $word_info = array();
41
42        $this->tpl_name = 'acp_words';
43        $this->page_title = 'ACP_WORDS';
44
45        $form_name = 'acp_words';
46        add_form_key($form_name);
47
48        switch ($action)
49        {
50            case 'edit':
51
52                $word_id = $request->variable('id', 0);
53
54                if (!$word_id)
55                {
56                    trigger_error($user->lang['NO_WORD'] . adm_back_link($this->u_action), E_USER_WARNING);
57                }
58
59                $sql = 'SELECT *
60                    FROM ' . WORDS_TABLE . "
61                    WHERE word_id = $word_id";
62                $result = $db->sql_query($sql);
63                $word_info = $db->sql_fetchrow($result);
64                $db->sql_freeresult($result);
65
66                $s_hidden_fields .= '<input type="hidden" name="id" value="' . $word_id . '" />';
67
68            case 'add':
69
70                $template->assign_vars(array(
71                    'S_EDIT_WORD'        => true,
72                    'U_ACTION'            => $this->u_action,
73                    'U_BACK'            => $this->u_action,
74                    'WORD'                => (isset($word_info['word'])) ? $word_info['word'] : '',
75                    'REPLACEMENT'        => (isset($word_info['replacement'])) ? $word_info['replacement'] : '',
76                    'S_HIDDEN_FIELDS'    => $s_hidden_fields)
77                );
78
79                return;
80
81            break;
82
83            case 'save':
84
85                if (!check_form_key($form_name))
86                {
87                    trigger_error($user->lang['FORM_INVALID']. adm_back_link($this->u_action), E_USER_WARNING);
88                }
89
90                $word_id        = $request->variable('id', 0);
91                $word            = $request->variable('word', '', true);
92                $replacement    = $request->variable('replacement', '', true);
93
94                if ($word === '' || $replacement === '')
95                {
96                    trigger_error($user->lang['ENTER_WORD'] . adm_back_link($this->u_action), E_USER_WARNING);
97                }
98
99                // Replace multiple consecutive asterisks with single one as those are not needed
100                $word = preg_replace('#\*{2,}#', '*', $word);
101
102                $sql_ary = array(
103                    'word'            => $word,
104                    'replacement'    => $replacement
105                );
106
107                if ($word_id)
108                {
109                    $db->sql_query('UPDATE ' . WORDS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . ' WHERE word_id = ' . $word_id);
110                }
111                else
112                {
113                    $db->sql_query('INSERT INTO ' . WORDS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
114                }
115
116                $cache->destroy('_word_censors');
117                $phpbb_container->get('text_formatter.cache')->invalidate();
118
119                $log_action = ($word_id) ? 'LOG_WORD_EDIT' : 'LOG_WORD_ADD';
120
121                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, $log_action, false, array($word));
122
123                $message = ($word_id) ? $user->lang['WORD_UPDATED'] : $user->lang['WORD_ADDED'];
124                trigger_error($message . adm_back_link($this->u_action));
125
126            break;
127
128            case 'delete':
129
130                $word_id = $request->variable('id', 0);
131
132                if (!$word_id)
133                {
134                    trigger_error($user->lang['NO_WORD'] . adm_back_link($this->u_action), E_USER_WARNING);
135                }
136
137                if (confirm_box(true))
138                {
139                    $sql = 'SELECT word
140                        FROM ' . WORDS_TABLE . "
141                        WHERE word_id = $word_id";
142                    $result = $db->sql_query($sql);
143                    $deleted_word = $db->sql_fetchfield('word');
144                    $db->sql_freeresult($result);
145
146                    $sql = 'DELETE FROM ' . WORDS_TABLE . "
147                        WHERE word_id = $word_id";
148                    $db->sql_query($sql);
149
150                    $cache->destroy('_word_censors');
151                    $phpbb_container->get('text_formatter.cache')->invalidate();
152
153                    $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_WORD_DELETE', false, array($deleted_word));
154
155                    trigger_error($user->lang['WORD_REMOVED'] . adm_back_link($this->u_action));
156                }
157                else
158                {
159                    confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
160                        'i'            => $id,
161                        'mode'        => $mode,
162                        'id'        => $word_id,
163                        'action'    => 'delete',
164                    )));
165                }
166
167            break;
168        }
169
170        $template->assign_vars(array(
171            'U_ACTION'            => $this->u_action,
172            'S_HIDDEN_FIELDS'    => $s_hidden_fields)
173        );
174
175        $sql = 'SELECT *
176            FROM ' . WORDS_TABLE . '
177            ORDER BY word';
178        $result = $db->sql_query($sql);
179
180        while ($row = $db->sql_fetchrow($result))
181        {
182            $template->assign_block_vars('words', array(
183                'WORD'            => $row['word'],
184                'REPLACEMENT'    => $row['replacement'],
185                'U_EDIT'        => $this->u_action . '&amp;action=edit&amp;id=' . $row['word_id'],
186                'U_DELETE'        => $this->u_action . '&amp;action=delete&amp;id=' . $row['word_id'])
187            );
188        }
189        $db->sql_freeresult($result);
190    }
191}