1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* Auto Groups extension for the phpBB Forum Software package. |
5
|
|
|
* |
6
|
|
|
* @copyright (c) 2014 phpBB Limited <https://www.phpbb.com> |
7
|
|
|
* @license GNU General Public License, version 2 (GPL-2.0) |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace phpbb\autogroups\controller; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Admin controller |
15
|
|
|
*/ |
16
|
|
|
class admin_controller implements admin_interface |
17
|
|
|
{ |
18
|
|
|
/** @var \phpbb\cache\driver\driver_interface */ |
19
|
|
|
protected $cache; |
20
|
|
|
|
21
|
|
|
/** @var \phpbb\db\driver\driver_interface */ |
22
|
|
|
protected $db; |
23
|
|
|
|
24
|
|
|
/** @var \phpbb\group\helper */ |
25
|
|
|
protected $group_helper; |
26
|
|
|
|
27
|
|
|
/** @var \phpbb\language\language */ |
28
|
|
|
protected $language; |
29
|
|
|
|
30
|
|
|
/** @var \phpbb\log\log */ |
31
|
|
|
protected $log; |
32
|
|
|
|
33
|
|
|
/** @var \phpbb\autogroups\conditions\manager */ |
34
|
|
|
protected $manager; |
35
|
|
|
|
36
|
|
|
/** @var \phpbb\request\request */ |
37
|
|
|
protected $request; |
38
|
|
|
|
39
|
|
|
/** @var \phpbb\template\template */ |
40
|
|
|
protected $template; |
41
|
|
|
|
42
|
|
|
/** @var \phpbb\user */ |
43
|
|
|
protected $user; |
44
|
|
|
|
45
|
|
|
/** @var string The database table the auto group rules are stored in */ |
46
|
|
|
protected $autogroups_rules_table; |
47
|
|
|
|
48
|
|
|
/** @var string The database table the auto group types are stored in */ |
49
|
|
|
protected $autogroups_types_table; |
50
|
|
|
|
51
|
|
|
/** @var string Custom form action */ |
52
|
|
|
protected $u_action; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Constructor |
56
|
|
|
* |
57
|
|
|
* @param \phpbb\cache\driver\driver_interface $cache Cache driver interface |
58
|
|
|
* @param \phpbb\db\driver\driver_interface $db Database object |
59
|
|
|
* @param \phpbb\group\helper $group_helper Group helper object |
60
|
|
|
* @param \phpbb\language\language $language Language object |
61
|
|
|
* @param \phpbb\log\log $log The phpBB log system |
62
|
|
|
* @param \phpbb\autogroups\conditions\manager $manager Auto groups condition manager object |
63
|
|
|
* @param \phpbb\request\request $request Request object |
64
|
|
|
* @param \phpbb\template\template $template Template object |
65
|
|
|
* @param \phpbb\user $user User object |
66
|
|
|
* @param string $autogroups_rules_table Name of the table used to store auto group rules data |
67
|
|
|
* @param string $autogroups_types_table Name of the table used to store auto group types data |
68
|
|
|
* @access public |
69
|
|
|
*/ |
70
|
|
|
public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, \phpbb\group\helper $group_helper, \phpbb\language\language $language, \phpbb\log\log $log, \phpbb\autogroups\conditions\manager $manager, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, $autogroups_rules_table, $autogroups_types_table) |
71
|
|
|
{ |
72
|
|
|
$this->cache = $cache; |
73
|
|
|
$this->db = $db; |
74
|
|
|
$this->group_helper = $group_helper; |
75
|
|
|
$this->language = $language; |
76
|
|
|
$this->log = $log; |
77
|
|
|
$this->manager = $manager; |
78
|
|
|
$this->request = $request; |
79
|
|
|
$this->template = $template; |
80
|
|
|
$this->user = $user; |
81
|
|
|
$this->autogroups_rules_table = $autogroups_rules_table; |
82
|
|
|
$this->autogroups_types_table = $autogroups_types_table; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function display_autogroups() |
89
|
|
|
{ |
90
|
|
|
// Get all auto groups data from the database |
91
|
|
|
$autogroup_rows = $this->get_all_autogroups(); |
92
|
|
|
|
93
|
|
|
// Process all auto groups data for display in the template |
94
|
|
|
foreach ($autogroup_rows as $row) |
95
|
|
|
{ |
96
|
|
|
$this->template->assign_block_vars('autogroups', array( |
97
|
|
|
'GROUP_NAME' => $row['group_name'], |
98
|
|
|
'CONDITION_NAME' => $this->manager->get_condition_lang($row['autogroups_type_name']), |
99
|
|
|
'MIN_VALUE' => $row['autogroups_min_value'], |
100
|
|
|
'MAX_VALUE' => $row['autogroups_max_value'], |
101
|
|
|
|
102
|
|
|
'S_DEFAULT' => $row['autogroups_default'], |
103
|
|
|
'S_NOTIFY' => $row['autogroups_notify'], |
104
|
|
|
|
105
|
|
|
'U_EDIT' => "{$this->u_action}&action=edit&autogroups_id=" . $row['autogroups_id'], |
106
|
|
|
'U_DELETE' => "{$this->u_action}&action=delete&autogroups_id=" . $row['autogroups_id'], |
107
|
|
|
'U_SYNC' => "{$this->u_action}&action=sync&autogroups_id=" . $row['autogroups_id'] . '&hash=' . generate_link_hash('sync' . $row['autogroups_id']), |
108
|
|
|
)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->template->assign_vars(array( |
112
|
|
|
'U_ACTION' => $this->u_action, |
113
|
|
|
'U_ADD_AUTOGROUP_RULE' => "{$this->u_action}&action=add", |
114
|
|
|
)); |
115
|
|
|
|
116
|
|
|
// Display the group exemption select box |
117
|
|
|
$exempt_groups = $this->get_exempt_groups(); |
118
|
|
|
$this->build_groups_menu(array_keys($exempt_groups)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
|
|
public function save_autogroup_rule($autogroups_id = 0) |
125
|
|
|
{ |
126
|
|
|
// Process auto group form data if form was submitted |
127
|
|
|
if ($this->request->is_set_post('submit')) |
128
|
|
|
{ |
129
|
|
|
$this->submit_autogroup_rule($autogroups_id); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Get data for the auto group so we can display it |
133
|
|
|
$autogroups_data = $this->get_autogroup($autogroups_id); |
134
|
|
|
|
135
|
|
|
// Process the auto group data for display in the template |
136
|
|
|
$this->build_groups_menu(array($autogroups_data['autogroups_group_id']), true); |
137
|
|
|
$this->build_conditions_menu($autogroups_data['autogroups_type_id']); |
138
|
|
|
$this->template->assign_vars(array( |
139
|
|
|
'S_ADD' => (bool) !$autogroups_id, |
140
|
|
|
'S_EDIT' => (bool) $autogroups_id, |
141
|
|
|
|
142
|
|
|
'MIN_VALUE' => (int) $autogroups_data['autogroups_min_value'], |
143
|
|
|
'MAX_VALUE' => (int) $autogroups_data['autogroups_max_value'], |
144
|
|
|
|
145
|
|
|
'S_DEFAULT' => (bool) $autogroups_data['autogroups_default'], |
146
|
|
|
'S_NOTIFY' => (bool) $autogroups_data['autogroups_notify'], |
147
|
|
|
|
148
|
|
|
'EXEMPT_GROUPS' => implode(', ', array_map([$this, 'display_group_name'], $this->get_exempt_groups())), |
149
|
|
|
|
150
|
|
|
'U_FORM_ACTION' => $this->u_action . '&action=' . ($autogroups_id ? 'edit' : 'add') . '&autogroups_id=' . $autogroups_id, |
151
|
|
|
'U_ACTION' => $this->u_action, |
152
|
|
|
'U_BACK' => $this->u_action, |
153
|
|
|
)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
|
|
public function delete_autogroup_rule($autogroups_id) |
160
|
|
|
{ |
161
|
|
|
// Delete and auto group rule |
162
|
|
|
$sql = 'DELETE FROM ' . $this->autogroups_rules_table . ' |
163
|
|
|
WHERE autogroups_id = ' . (int) $autogroups_id; |
164
|
|
|
$this->db->sql_query($sql); |
165
|
|
|
|
166
|
|
|
// Log the action |
167
|
|
|
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_AUTOGROUPS_DELETE_LOG', time()); |
168
|
|
|
|
169
|
|
|
// If AJAX was used, show user a result message |
170
|
|
|
if ($this->request->is_ajax()) |
171
|
|
|
{ |
172
|
|
|
$json_response = new \phpbb\json_response; |
173
|
|
|
$json_response->send(array( |
174
|
|
|
'MESSAGE_TITLE' => $this->language->lang('INFORMATION'), |
175
|
|
|
'MESSAGE_TEXT' => $this->language->lang('ACP_AUTOGROUPS_DELETE_SUCCESS'), |
176
|
|
|
'REFRESH_DATA' => array( |
177
|
|
|
'time' => 3 |
178
|
|
|
) |
179
|
|
|
)); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
|
|
public function resync_autogroup_rule($autogroups_id) |
187
|
|
|
{ |
188
|
|
|
// If the link hash is invalid, stop and show an error message to the user |
189
|
|
|
if (!check_link_hash($this->request->variable('hash', ''), 'sync' . $autogroups_id)) |
190
|
|
|
{ |
191
|
|
|
trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
try |
195
|
|
|
{ |
196
|
|
|
$this->manager->sync_autogroups($autogroups_id); |
197
|
|
|
} |
198
|
|
|
catch (\Exception $e) |
199
|
|
|
{ |
200
|
|
|
trigger_error($e->getMessage() . adm_back_link($this->u_action), E_USER_WARNING); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritdoc} |
206
|
|
|
*/ |
207
|
|
|
public function submit_autogroups_options() |
208
|
|
|
{ |
209
|
|
|
// Get data from the form |
210
|
|
|
$autogroups_default_exempt = $this->request->variable('group_ids', array(0)); |
211
|
|
|
|
212
|
|
|
// Use a confirmation box routine before setting the data |
213
|
|
|
if (confirm_box(true)) |
214
|
|
|
{ |
215
|
|
|
// Set selected groups to 1 |
216
|
|
|
$sql = 'UPDATE ' . GROUPS_TABLE . ' |
217
|
|
|
SET autogroup_default_exempt = 1 |
218
|
|
|
WHERE ' . $this->db->sql_in_set('group_id', $autogroups_default_exempt, false, true); |
219
|
|
|
$this->db->sql_query($sql); |
220
|
|
|
|
221
|
|
|
// Set all other groups to 0 |
222
|
|
|
$sql = 'UPDATE ' . GROUPS_TABLE . ' |
223
|
|
|
SET autogroup_default_exempt = 0 |
224
|
|
|
WHERE ' . $this->db->sql_in_set('group_id', $autogroups_default_exempt, true, true); |
225
|
|
|
$this->db->sql_query($sql); |
226
|
|
|
|
227
|
|
|
// Clear the cached group table data |
228
|
|
|
$this->cache->destroy('sql', GROUPS_TABLE); |
229
|
|
|
} |
230
|
|
|
else |
231
|
|
|
{ |
232
|
|
|
confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields(array( |
233
|
|
|
'generalsubmit' => true, |
234
|
|
|
'group_ids' => $autogroups_default_exempt, |
235
|
|
|
))); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Submit auto group rule form data |
241
|
|
|
* |
242
|
|
|
* @param int $autogroups_id An auto group identifier |
243
|
|
|
* A value of 0 is new, otherwise we're updating |
244
|
|
|
* @return void |
245
|
|
|
* @access protected |
246
|
|
|
*/ |
247
|
|
|
protected function submit_autogroup_rule($autogroups_id = 0) |
248
|
|
|
{ |
249
|
|
|
$data = array( |
250
|
|
|
'autogroups_type_id' => $this->request->variable('autogroups_type_id', 0), |
251
|
|
|
'autogroups_min_value' => $this->request->variable('autogroups_min_value', 0), |
252
|
|
|
'autogroups_max_value' => $this->request->variable('autogroups_max_value', 0), |
253
|
|
|
'autogroups_group_id' => $this->request->variable('autogroups_group_id', 0), |
254
|
|
|
'autogroups_default' => $this->request->variable('autogroups_default', false), |
255
|
|
|
'autogroups_notify' => $this->request->variable('autogroups_notify', false), |
256
|
|
|
); |
257
|
|
|
|
258
|
|
|
// Prevent form submit when no user groups are available or selected |
259
|
|
|
if (!$data['autogroups_group_id']) |
260
|
|
|
{ |
261
|
|
|
trigger_error($this->language->lang('ACP_AUTOGROUPS_INVALID_GROUPS') . adm_back_link($this->u_action), E_USER_WARNING); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
// Prevent form submit when min and max values are identical |
265
|
|
|
if ($data['autogroups_min_value'] == $data['autogroups_max_value']) |
266
|
|
|
{ |
267
|
|
|
trigger_error($this->language->lang('ACP_AUTOGROUPS_INVALID_RANGE') . adm_back_link($this->u_action), E_USER_WARNING); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
if ($autogroups_id != 0) // Update existing auto group data |
271
|
|
|
{ |
272
|
|
|
$sql = 'UPDATE ' . $this->autogroups_rules_table . ' |
273
|
|
|
SET ' . $this->db->sql_build_array('UPDATE', $data) . ' |
274
|
|
|
WHERE autogroups_id = ' . (int) $autogroups_id; |
275
|
|
|
$this->db->sql_query($sql); |
276
|
|
|
} |
277
|
|
|
else // Insert new auto group data |
278
|
|
|
{ |
279
|
|
|
$sql = 'INSERT INTO ' . $this->autogroups_rules_table . ' ' . $this->db->sql_build_array('INSERT', $data); |
280
|
|
|
$this->db->sql_query($sql); |
281
|
|
|
$autogroups_id = (int) $this->db->sql_nextid(); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
// Apply the auto group to all users |
285
|
|
|
$this->manager->sync_autogroups($autogroups_id); |
286
|
|
|
|
287
|
|
|
// Log the action |
288
|
|
|
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_AUTOGROUPS_SAVED_LOG', time()); |
289
|
|
|
|
290
|
|
|
// Output message to user after submitting the form |
291
|
|
|
trigger_error($this->language->lang('ACP_AUTOGROUPS_SUBMIT_SUCCESS') . adm_back_link($this->u_action)); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Get one auto group rule from the database |
296
|
|
|
* |
297
|
|
|
* @param int $id An auto group rule identifier |
298
|
|
|
* @return array An auto group rule and it's associated data |
299
|
|
|
* @access public |
300
|
|
|
*/ |
301
|
|
|
protected function get_autogroup($id) |
302
|
|
|
{ |
303
|
|
|
$sql = 'SELECT * |
304
|
|
|
FROM ' . $this->autogroups_rules_table . ' |
305
|
|
|
WHERE autogroups_id = ' . (int) $id; |
306
|
|
|
$result = $this->db->sql_query($sql); |
307
|
|
|
$autogroups_data = $this->db->sql_fetchrow($result); |
308
|
|
|
$this->db->sql_freeresult($result); |
309
|
|
|
|
310
|
|
|
return $autogroups_data; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Get all auto group rules from the database |
315
|
|
|
* |
316
|
|
|
* @return array Array of auto group rules and their associated data |
317
|
|
|
* @access public |
318
|
|
|
*/ |
319
|
|
|
protected function get_all_autogroups() |
320
|
|
|
{ |
321
|
|
|
$sql_array = array( |
322
|
|
|
'SELECT' => 'agr.*, agt.autogroups_type_name, g.group_name', |
323
|
|
|
'FROM' => array( |
324
|
|
|
$this->autogroups_rules_table => 'agr', |
325
|
|
|
$this->autogroups_types_table => 'agt', |
326
|
|
|
GROUPS_TABLE => 'g', |
327
|
|
|
), |
328
|
|
|
'WHERE' => 'agr.autogroups_type_id = agt.autogroups_type_id |
329
|
|
|
AND agr.autogroups_group_id = g.group_id', |
330
|
|
|
'ORDER_BY' => 'g.group_name ASC, autogroups_min_value ASC', |
331
|
|
|
); |
332
|
|
|
$sql = $this->db->sql_build_query('SELECT', $sql_array); |
333
|
|
|
$result = $this->db->sql_query($sql); |
334
|
|
|
$rows = $this->db->sql_fetchrowset($result); |
335
|
|
|
$this->db->sql_freeresult($result); |
336
|
|
|
|
337
|
|
|
return $rows; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Get an array of user groups marked as exempt from default switching |
342
|
|
|
* |
343
|
|
|
* @return array An array of exempted groups array('group_id' => 'group_name') |
344
|
|
|
* @access protected |
345
|
|
|
*/ |
346
|
|
|
protected function get_exempt_groups() |
347
|
|
|
{ |
348
|
|
|
$groups = array(); |
349
|
|
|
|
350
|
|
|
// Get default exempted groups |
351
|
|
|
$sql = 'SELECT group_id, group_name |
352
|
|
|
FROM ' . GROUPS_TABLE . ' |
353
|
|
|
WHERE autogroup_default_exempt = 1'; |
354
|
|
|
$result = $this->db->sql_query($sql, 7200); |
355
|
|
|
|
356
|
|
|
while ($row = $this->db->sql_fetchrow($result)) |
357
|
|
|
{ |
358
|
|
|
$groups[$row['group_id']] = $row['group_name']; |
359
|
|
|
} |
360
|
|
|
$this->db->sql_freeresult($result); |
361
|
|
|
|
362
|
|
|
return $groups; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Build template vars for a select menu of user groups |
367
|
|
|
* |
368
|
|
|
* @param array $selected An array of identifiers for selected group(s) |
369
|
|
|
* @param bool $exclude_predefined_groups Exclude GROUP_SPECIAL |
370
|
|
|
* @return void |
371
|
|
|
* @access protected |
372
|
|
|
*/ |
373
|
|
|
protected function build_groups_menu($selected, $exclude_predefined_groups = false) |
374
|
|
|
{ |
375
|
|
|
// Get groups excluding BOTS, Guests, and optionally predefined |
376
|
|
|
$sql = 'SELECT group_id, group_name, group_type |
377
|
|
|
FROM ' . GROUPS_TABLE . ' |
378
|
|
|
WHERE ' . $this->db->sql_in_set('group_name', array('BOTS', 'GUESTS'), true, true) . |
379
|
|
|
($exclude_predefined_groups ? ' AND group_type <> ' . GROUP_SPECIAL : '') . ' |
380
|
|
|
ORDER BY group_name'; |
381
|
|
|
$result = $this->db->sql_query($sql); |
382
|
|
|
|
383
|
|
|
while ($group_row = $this->db->sql_fetchrow($result)) |
384
|
|
|
{ |
385
|
|
|
$this->template->assign_block_vars('groups', array( |
386
|
|
|
'GROUP_ID' => $group_row['group_id'], |
387
|
|
|
'GROUP_NAME' => $this->display_group_name($group_row['group_name']), |
388
|
|
|
|
389
|
|
|
'S_SELECTED' => in_array($group_row['group_id'], $selected), |
390
|
|
|
)); |
391
|
|
|
} |
392
|
|
|
$this->db->sql_freeresult($result); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Get the display name of a user group |
397
|
|
|
* |
398
|
|
|
* @param string $group_name |
399
|
|
|
* @return string |
400
|
|
|
*/ |
401
|
|
|
protected function display_group_name($group_name) |
402
|
|
|
{ |
403
|
|
|
return $this->group_helper->get_name($group_name); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Build template vars for a select menu of auto group conditions |
408
|
|
|
* |
409
|
|
|
* @param int $selected An identifier for the selected group |
410
|
|
|
* @return void |
411
|
|
|
* @access protected |
412
|
|
|
*/ |
413
|
|
|
protected function build_conditions_menu($selected) |
414
|
|
|
{ |
415
|
|
|
$conditions = $this->manager->get_autogroups_type_ids(); |
416
|
|
|
|
417
|
|
|
foreach ($conditions as $condition_name => $condition_id) |
418
|
|
|
{ |
419
|
|
|
$this->template->assign_block_vars('conditions', array( |
420
|
|
|
'CONDITION_ID' => $condition_id, |
421
|
|
|
'CONDITION_NAME' => $this->manager->get_condition_lang($condition_name), |
422
|
|
|
|
423
|
|
|
'S_SELECTED' => $condition_id == $selected, |
424
|
|
|
)); |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* {@inheritdoc} |
430
|
|
|
*/ |
431
|
|
|
public function set_page_url($u_action) |
432
|
|
|
{ |
433
|
|
|
$this->u_action = $u_action; |
434
|
|
|
} |
435
|
|
|
} |
436
|
|
|
|