|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* Topic Prefixes extension for the phpBB Forum Software package. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright (c) 2016 phpBB Limited <https://www.phpbb.com> |
|
7
|
|
|
* @license GNU General Public License, version 2 (GPL-2.0) |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace phpbb\topicprefixes\controller; |
|
12
|
|
|
|
|
13
|
|
|
use phpbb\log\log; |
|
14
|
|
|
use phpbb\request\request; |
|
15
|
|
|
use phpbb\template\template; |
|
16
|
|
|
use phpbb\topicprefixes\prefixes\manager; |
|
17
|
|
|
use phpbb\user; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class admin_controller |
|
21
|
|
|
*/ |
|
22
|
|
|
class admin_controller implements admin_controller_interface |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var manager */ |
|
25
|
|
|
protected $manager; |
|
26
|
|
|
|
|
27
|
|
|
/** @var log */ |
|
28
|
|
|
protected $log; |
|
29
|
|
|
|
|
30
|
|
|
/** @var request */ |
|
31
|
|
|
protected $request; |
|
32
|
|
|
|
|
33
|
|
|
/** @var template */ |
|
34
|
|
|
protected $template; |
|
35
|
|
|
|
|
36
|
|
|
/** @var user */ |
|
37
|
|
|
protected $user; |
|
38
|
|
|
|
|
39
|
|
|
/** @var string */ |
|
40
|
|
|
protected $root_path; |
|
41
|
|
|
|
|
42
|
|
|
/** @var string */ |
|
43
|
|
|
protected $php_ext; |
|
44
|
|
|
|
|
45
|
|
|
/** @var string */ |
|
46
|
|
|
protected $form_key; |
|
47
|
|
|
|
|
48
|
|
|
/** @var int */ |
|
49
|
|
|
protected $forum_id; |
|
50
|
|
|
|
|
51
|
|
|
/** @var string */ |
|
52
|
|
|
protected $u_action; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Constructor |
|
56
|
|
|
* |
|
57
|
|
|
* @param manager $manager |
|
58
|
|
|
* @param log $log |
|
59
|
|
|
* @param request $request |
|
60
|
|
|
* @param template $template |
|
61
|
|
|
* @param user $user |
|
62
|
|
|
* @param string $phpbb_root_path |
|
63
|
|
|
* @param string $phpEx |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct(manager $manager, log $log, request $request, template $template, user $user, $phpbb_root_path, $phpEx) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->manager = $manager; |
|
68
|
|
|
$this->log = $log; |
|
69
|
|
|
$this->request = $request; |
|
70
|
|
|
$this->template = $template; |
|
71
|
|
|
$this->user = $user; |
|
72
|
|
|
$this->root_path = $phpbb_root_path; |
|
73
|
|
|
$this->php_ext = $phpEx; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function main() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->form_key = 'acp_topic_prefixes'; |
|
79
|
|
|
add_form_key($this->form_key); |
|
80
|
|
|
|
|
81
|
|
|
$action = $this->request->variable('action', ''); |
|
82
|
|
|
$prefix_id = $this->request->variable('prefix_id', 0); |
|
83
|
|
|
$this->set_forum_id($this->request->variable('forum_id', 0)); |
|
84
|
|
|
|
|
85
|
|
|
switch ($action) |
|
86
|
|
|
{ |
|
87
|
|
|
case 'add': |
|
88
|
|
|
$this->add_prefix(); |
|
89
|
|
|
break; |
|
90
|
|
|
|
|
91
|
|
|
case 'edit': |
|
92
|
|
|
case 'delete': |
|
93
|
|
|
$this->{$action . '_prefix'}($prefix_id); |
|
94
|
|
|
break; |
|
95
|
|
|
|
|
96
|
|
|
case 'move_up': |
|
97
|
|
|
case 'move_down': |
|
98
|
|
|
$this->move_prefix($prefix_id, str_replace('move_', '', $action)); |
|
99
|
|
|
break; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->display_settings(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @inheritdoc |
|
107
|
|
|
*/ |
|
108
|
|
|
public function display_settings() |
|
109
|
|
|
{ |
|
110
|
|
|
foreach ($this->manager->get_prefixes($this->forum_id) as $prefix) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->template->assign_block_vars('prefixes', [ |
|
113
|
|
|
'PREFIX_TAG' => $prefix['prefix_tag'], |
|
114
|
|
|
'PREFIX_ENABLED' => (int) $prefix['prefix_enabled'], |
|
115
|
|
|
'U_EDIT' => "{$this->u_action}&action=edit&prefix_id=" . $prefix['prefix_id'] . '&forum_id=' . $this->forum_id . '&hash=' . generate_link_hash('edit' . $prefix['prefix_id']), |
|
116
|
|
|
'U_DELETE' => "{$this->u_action}&action=delete&prefix_id=" . $prefix['prefix_id'] . '&forum_id=' . $this->forum_id, |
|
117
|
|
|
'U_MOVE_UP' => "{$this->u_action}&action=move_up&prefix_id=" . $prefix['prefix_id'] . '&forum_id=' . $this->forum_id . '&hash=' . generate_link_hash('up' . $prefix['prefix_id']), |
|
118
|
|
|
'U_MOVE_DOWN' => "{$this->u_action}&action=move_down&prefix_id=" . $prefix['prefix_id'] . '&forum_id=' . $this->forum_id . '&hash=' . generate_link_hash('down' . $prefix['prefix_id']), |
|
119
|
|
|
]); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$this->template->assign_vars([ |
|
123
|
|
|
'S_FORUM_OPTIONS' => make_forum_select($this->forum_id, false, false, true), |
|
124
|
|
|
'FORUM_ID' => $this->forum_id, |
|
125
|
|
|
'U_ACTION' => $this->u_action, |
|
126
|
|
|
]); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @inheritdoc |
|
131
|
|
|
*/ |
|
132
|
|
|
public function add_prefix() |
|
133
|
|
|
{ |
|
134
|
|
|
if ($this->request->is_set_post('submit')) |
|
135
|
|
|
{ |
|
136
|
|
|
if (!check_form_key($this->form_key)) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->trigger_message('FORM_INVALID', E_USER_WARNING); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$tag = $this->request->variable('prefix_tag', '', true); |
|
142
|
|
|
$prefix = $this->manager->add_prefix($tag, $this->forum_id); |
|
143
|
|
|
$this->log($prefix['prefix_tag'], 'ACP_LOG_PREFIX_ADDED'); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @inheritdoc |
|
149
|
|
|
*/ |
|
150
|
|
|
public function edit_prefix($prefix_id) |
|
151
|
|
|
{ |
|
152
|
|
|
if (!$this->check_hash('edit' . $prefix_id)) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->trigger_message('FORM_INVALID', E_USER_WARNING); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
try |
|
158
|
|
|
{ |
|
159
|
|
|
$prefix = $this->manager->get_prefix($prefix_id); |
|
160
|
|
|
$this->manager->update_prefix($prefix['prefix_id'], ['prefix_enabled' => !$prefix['prefix_enabled']]); |
|
161
|
|
|
} |
|
162
|
|
|
catch (\OutOfBoundsException $e) |
|
163
|
|
|
{ |
|
164
|
|
|
$this->trigger_message($e->getMessage(), E_USER_WARNING); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @inheritdoc |
|
170
|
|
|
*/ |
|
171
|
|
|
public function delete_prefix($prefix_id) |
|
172
|
|
|
{ |
|
173
|
|
|
if (confirm_box(true)) |
|
174
|
|
|
{ |
|
175
|
|
|
try |
|
176
|
|
|
{ |
|
177
|
|
|
$prefix = $this->manager->get_prefix($prefix_id); |
|
178
|
|
|
$this->manager->delete_prefix($prefix['prefix_id']); |
|
179
|
|
|
$this->log($prefix['prefix_tag'], 'ACP_LOG_PREFIX_DELETED'); |
|
180
|
|
|
} |
|
181
|
|
|
catch (\OutOfBoundsException $e) |
|
182
|
|
|
{ |
|
183
|
|
|
$this->trigger_message($e->getMessage(), E_USER_WARNING); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
$this->trigger_message('TOPIC_PREFIX_DELETED'); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
confirm_box(false, $this->user->lang('DELETE_TOPIC_PREFIX_CONFIRM'), build_hidden_fields([ |
|
190
|
|
|
'mode' => 'manage', |
|
191
|
|
|
'action' => 'delete', |
|
192
|
|
|
'prefix_id' => $prefix_id, |
|
193
|
|
|
'forum_id' => $this->forum_id, |
|
194
|
|
|
])); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @inheritdoc |
|
199
|
|
|
*/ |
|
200
|
|
|
public function move_prefix($prefix_id, $direction, $amount = 1) |
|
201
|
|
|
{ |
|
202
|
|
|
if (!$this->check_hash($direction . $prefix_id)) |
|
203
|
|
|
{ |
|
204
|
|
|
$this->trigger_message('FORM_INVALID', E_USER_WARNING); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
try |
|
208
|
|
|
{ |
|
209
|
|
|
$this->manager->move_prefix($prefix_id, $direction, $amount); |
|
210
|
|
|
} |
|
211
|
|
|
catch (\OutOfBoundsException $e) |
|
212
|
|
|
{ |
|
213
|
|
|
$this->trigger_message($e->getMessage(), E_USER_WARNING); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
if ($this->request->is_ajax()) |
|
217
|
|
|
{ |
|
218
|
|
|
$json_response = new \phpbb\json_response; |
|
219
|
|
|
$json_response->send(['success' => true]); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @inheritdoc |
|
225
|
|
|
*/ |
|
226
|
|
|
public function set_u_action($u_action) |
|
227
|
|
|
{ |
|
228
|
|
|
$this->u_action = $u_action; |
|
229
|
|
|
return $this; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @inheritdoc |
|
234
|
|
|
*/ |
|
235
|
|
|
public function set_forum_id($forum_id) |
|
236
|
|
|
{ |
|
237
|
|
|
$this->forum_id = $forum_id; |
|
238
|
|
|
return $this; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Check link hash helper |
|
243
|
|
|
* |
|
244
|
|
|
* @param string $hash A hashed string |
|
245
|
|
|
* @return bool True if hash matches, false if not |
|
246
|
|
|
*/ |
|
247
|
|
|
protected function check_hash($hash) |
|
248
|
|
|
{ |
|
249
|
|
|
return check_link_hash($this->request->variable('hash', ''), $hash); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Trigger a message and back link for error/success dialogs |
|
254
|
|
|
* |
|
255
|
|
|
* @param string $message A language key |
|
256
|
|
|
* @param int $error Error type constant, optional |
|
257
|
|
|
* @return null |
|
258
|
|
|
*/ |
|
259
|
|
|
protected function trigger_message($message = '', $error = E_USER_NOTICE) |
|
260
|
|
|
{ |
|
261
|
|
|
trigger_error($this->user->lang($message) . adm_back_link("{$this->u_action}&forum_id={$this->forum_id}"), $error); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Helper for logging topic prefix admin actions |
|
266
|
|
|
* |
|
267
|
|
|
* @param string $tag The topic prefix tag |
|
268
|
|
|
* @param string $message The log action language key |
|
269
|
|
|
*/ |
|
270
|
|
|
protected function log($tag, $message) |
|
271
|
|
|
{ |
|
272
|
|
|
$forum_data = $this->get_forum_info($this->forum_id); |
|
273
|
|
|
|
|
274
|
|
|
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, $message, time(), array($tag, $forum_data['forum_name'])); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Get a forum's information |
|
279
|
|
|
* |
|
280
|
|
|
* @param int $forum_id |
|
281
|
|
|
* @return mixed Array with the current row, false, if the row does not exist |
|
282
|
|
|
*/ |
|
283
|
|
|
protected function get_forum_info($forum_id) |
|
284
|
|
|
{ |
|
285
|
|
|
if (!class_exists('acp_forums')) |
|
286
|
|
|
{ |
|
287
|
|
|
include $this->root_path . 'includes/acp/acp_forums.' . $this->php_ext; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
$acp_forums = new \acp_forums(); |
|
291
|
|
|
|
|
292
|
|
|
return $acp_forums->get_forum_info($forum_id); |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|