Completed
Pull Request — master (#122)
by Matt
04:11 queued 37s
created

controller/admin_controller.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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}&amp;action=edit&amp;autogroups_id=" . $row['autogroups_id'],
106
				'U_DELETE'	=> "{$this->u_action}&amp;action=delete&amp;autogroups_id=" . $row['autogroups_id'],
107
				'U_SYNC'	=> "{$this->u_action}&amp;action=sync&amp;autogroups_id=" . $row['autogroups_id'] . '&amp;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}&amp;action=add",
114
		));
115
116
		// Display the group exemption select box
117
		$this->display_group_exempt_options();
118
	}
119
120
	/**
121
	 * {@inheritdoc}
122
	 */
123
	public function save_autogroup_rule($autogroups_id = 0)
124
	{
125
		// Process auto group form data if form was submitted
126
		if ($this->request->is_set_post('submit'))
127
		{
128
			$this->submit_autogroup_rule($autogroups_id);
129
		}
130
131
		// Get data for the auto group so we can display it
132
		$autogroups_data = $this->get_autogroup($autogroups_id);
133
134
		// Process the auto group data for display in the template
135
		$this->build_groups_menu(array($autogroups_data['autogroups_group_id']), true);
136
		$this->build_conditions_menu($autogroups_data['autogroups_type_id']);
137
		$this->template->assign_vars(array(
138
			'S_ADD'			=> (bool) !$autogroups_id,
139
			'S_EDIT'		=> (bool) $autogroups_id,
140
141
			'MIN_VALUE'		=> (int) $autogroups_data['autogroups_min_value'],
142
			'MAX_VALUE'		=> (int) $autogroups_data['autogroups_max_value'],
143
144
			'S_DEFAULT'		=> (bool) $autogroups_data['autogroups_default'],
145
			'S_NOTIFY'		=> (bool) $autogroups_data['autogroups_notify'],
146
147
			'U_FORM_ACTION'	=> $this->u_action . '&amp;action=' . ($autogroups_id ? 'edit' : 'add') . '&amp;autogroups_id=' . $autogroups_id,
148
			'U_ACTION'		=> $this->u_action,
149
			'U_BACK'		=> $this->u_action,
150
		));
151
	}
152
153
	/**
154
	 * {@inheritdoc}
155
	 */
156
	public function delete_autogroup_rule($autogroups_id)
157
	{
158
		// Delete and auto group rule
159
		$sql = 'DELETE FROM ' . $this->autogroups_rules_table . '
160
			WHERE autogroups_id = ' . (int) $autogroups_id;
161
		$this->db->sql_query($sql);
162
163
		// Log the action
164
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_AUTOGROUPS_DELETE_LOG', time());
165
166
		// If AJAX was used, show user a result message
167
		if ($this->request->is_ajax())
168
		{
169
			$json_response = new \phpbb\json_response;
170
			$json_response->send(array(
171
				'MESSAGE_TITLE'	=> $this->language->lang('INFORMATION'),
172
				'MESSAGE_TEXT'	=> $this->language->lang('ACP_AUTOGROUPS_DELETE_SUCCESS'),
173
				'REFRESH_DATA'	=> array(
174
					'time'	=> 3
175
				)
176
			));
177
		}
178
	}
179
180
	/**
181
	 * {@inheritdoc}
182
	 */
183
	public function resync_autogroup_rule($autogroups_id)
184
	{
185
		// If the link hash is invalid, stop and show an error message to the user
186
		if (!check_link_hash($this->request->variable('hash', ''), 'sync' . $autogroups_id))
187
		{
188
			trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
189
		}
190
191
		try
192
		{
193
			$this->manager->sync_autogroups($autogroups_id);
194
		}
195
		catch (\Exception $e)
196
		{
197
			trigger_error($e->getMessage() . adm_back_link($this->u_action), E_USER_WARNING);
198
		}
199
	}
200
201
	/**
202
	 * {@inheritdoc}
203
	 */
204
	public function submit_autogroups_options()
205
	{
206
		// Get data from the form
207
		$autogroups_default_exempt = $this->request->variable('group_ids', array(0));
208
209
		// Use a confirmation box routine before setting the data
210
		if (confirm_box(true))
211
		{
212
			// Set selected groups to 1
213
			$sql = 'UPDATE ' . GROUPS_TABLE . '
214
				SET autogroup_default_exempt = 1
215
				WHERE ' . $this->db->sql_in_set('group_id', $autogroups_default_exempt, false, true);
216
			$this->db->sql_query($sql);
217
218
			// Set all other groups to 0
219
			$sql = 'UPDATE ' . GROUPS_TABLE . '
220
				SET autogroup_default_exempt = 0
221
				WHERE ' . $this->db->sql_in_set('group_id', $autogroups_default_exempt, true, true);
222
			$this->db->sql_query($sql);
223
224
			// Clear the cached group table data
225
			$this->cache->destroy('sql', GROUPS_TABLE);
226
		}
227
		else
228
		{
229
			confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
230
				'generalsubmit' => true,
231
				'group_ids' => $autogroups_default_exempt,
232
			)));
233
		}
234
	}
235
236
	/**
237
	 * Submit auto group rule form data
238
	 *
239
	 * @param int $autogroups_id An auto group identifier
240
	 *                           A value of 0 is new, otherwise we're updating
241
	 * @return void
242
	 * @access protected
243
	 */
244
	protected function submit_autogroup_rule($autogroups_id = 0)
245
	{
246
		$data = array(
247
			'autogroups_type_id'	=> $this->request->variable('autogroups_type_id', 0),
248
			'autogroups_min_value'	=> $this->request->variable('autogroups_min_value', 0),
249
			'autogroups_max_value'	=> $this->request->variable('autogroups_max_value', 0),
250
			'autogroups_group_id'	=> $this->request->variable('autogroups_group_id', 0),
251
			'autogroups_default'	=> $this->request->variable('autogroups_default', false),
252
			'autogroups_notify'		=> $this->request->variable('autogroups_notify', false),
253
		);
254
255
		// Prevent form submit when no user groups are available or selected
256 View Code Duplication
		if (!$data['autogroups_group_id'])
1 ignored issue
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
257
		{
258
			trigger_error($this->language->lang('ACP_AUTOGROUPS_INVALID_GROUPS') . adm_back_link($this->u_action), E_USER_WARNING);
259
		}
260
261
		// Prevent form submit when min and max values are identical
262 View Code Duplication
		if ($data['autogroups_min_value'] == $data['autogroups_max_value'])
1 ignored issue
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
263
		{
264
			trigger_error($this->language->lang('ACP_AUTOGROUPS_INVALID_RANGE') . adm_back_link($this->u_action), E_USER_WARNING);
265
		}
266
267
		if ($autogroups_id != 0) // Update existing auto group data
268
		{
269
			$sql = 'UPDATE ' . $this->autogroups_rules_table . '
270
				SET ' . $this->db->sql_build_array('UPDATE', $data) . '
271
				WHERE autogroups_id = ' . (int) $autogroups_id;
272
			$this->db->sql_query($sql);
273
		}
274
		else // Insert new auto group data
275
		{
276
			$sql = 'INSERT INTO ' . $this->autogroups_rules_table . ' ' . $this->db->sql_build_array('INSERT', $data);
277
			$this->db->sql_query($sql);
278
			$autogroups_id = (int) $this->db->sql_nextid();
279
		}
280
281
		// Apply the auto group to all users
282
		$this->manager->sync_autogroups($autogroups_id);
283
284
		// Log the action
285
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_AUTOGROUPS_SAVED_LOG', time());
286
287
		// Output message to user after submitting the form
288
		trigger_error($this->language->lang('ACP_AUTOGROUPS_SUBMIT_SUCCESS') . adm_back_link($this->u_action));
289
	}
290
291
	/**
292
	 * Get one auto group rule from the database
293
	 *
294
	 * @param int $id An auto group rule identifier
295
	 * @return array An auto group rule and it's associated data
296
	 * @access public
297
	 */
298
	protected function get_autogroup($id)
299
	{
300
		$sql = 'SELECT *
301
			FROM ' . $this->autogroups_rules_table . '
302
			WHERE autogroups_id = ' . (int) $id;
303
		$result = $this->db->sql_query($sql);
304
		$autogroups_data = $this->db->sql_fetchrow($result);
305
		$this->db->sql_freeresult($result);
306
307
		return $autogroups_data;
308
	}
309
310
	/**
311
	 * Get all auto group rules from the database
312
	 *
313
	 * @return array Array of auto group rules and their associated data
314
	 * @access public
315
	 */
316
	protected function get_all_autogroups()
317
	{
318
		$sql_array = array(
319
			'SELECT'	=> 'agr.*, agt.autogroups_type_name, g.group_name',
320
			'FROM'	=> array(
321
				$this->autogroups_rules_table => 'agr',
322
				$this->autogroups_types_table => 'agt',
323
				GROUPS_TABLE => 'g',
324
			),
325
			'WHERE'	=> 'agr.autogroups_type_id = agt.autogroups_type_id
326
				AND agr.autogroups_group_id = g.group_id',
327
			'ORDER_BY'	=> 'g.group_name ASC, autogroups_min_value ASC',
328
		);
329
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
330
		$result = $this->db->sql_query($sql);
331
		$rows = $this->db->sql_fetchrowset($result);
332
		$this->db->sql_freeresult($result);
333
334
		return $rows;
335
	}
336
337
	/**
338
	 * Display multi-select box containing all user groups
339
	 *
340
	 * @return void
341
	 * @access protected
342
	 */
343
	protected function display_group_exempt_options()
344
	{
345
		$group_id_ary = array();
346
347
		// Get default exempted groups
348
		$sql = 'SELECT group_id
349
			FROM ' . GROUPS_TABLE . '
350
			WHERE autogroup_default_exempt = 1';
351
		$result = $this->db->sql_query($sql, 7200);
352
353
		while ($row = $this->db->sql_fetchrow($result))
354
		{
355
			$group_id_ary[] = $row['group_id'];
356
		}
357
		$this->db->sql_freeresult($result);
358
359
		// Build groups menu. The exempted groups we found
360
		// are to be marked as selected in the menu.
361
		$this->build_groups_menu($group_id_ary);
362
	}
363
364
	/**
365
	 * Build template vars for a select menu of user groups
366
	 *
367
	 * @param array $selected                  An array of identifiers for selected group(s)
368
	 * @param bool  $exclude_predefined_groups Exclude GROUP_SPECIAL
369
	 * @return void
370
	 * @access protected
371
	 */
372
	protected function build_groups_menu($selected, $exclude_predefined_groups = false)
373
	{
374
		// Get groups excluding BOTS, Guests, and optionally predefined
375
		$sql = 'SELECT group_id, group_name, group_type
376
			FROM ' . GROUPS_TABLE . '
377
			WHERE ' . $this->db->sql_in_set('group_name', array('BOTS', 'GUESTS'), true, true) .
378
				($exclude_predefined_groups ? ' AND group_type <> ' . GROUP_SPECIAL : '') . '
379
			ORDER BY group_name';
380
		$result = $this->db->sql_query($sql);
381
382
		while ($group_row = $this->db->sql_fetchrow($result))
383
		{
384
			$this->template->assign_block_vars('groups', array(
385
				'GROUP_ID'		=> $group_row['group_id'],
386
				'GROUP_NAME'	=> $this->group_helper->get_name($group_row['group_name']),
387
388
				'S_SELECTED'	=> in_array($group_row['group_id'], $selected),
389
			));
390
		}
391
		$this->db->sql_freeresult($result);
392
	}
393
394
	/**
395
	 * Build template vars for a select menu of auto group conditions
396
	 *
397
	 * @param int $selected An identifier for the selected group
398
	 * @return void
399
	 * @access protected
400
	 */
401
	protected function build_conditions_menu($selected)
402
	{
403
		$conditions = $this->manager->get_autogroups_type_ids();
404
405
		foreach ($conditions as $condition_name => $condition_id)
406
		{
407
			$this->template->assign_block_vars('conditions', array(
408
				'CONDITION_ID'		=> $condition_id,
409
				'CONDITION_NAME'	=> $this->manager->get_condition_lang($condition_name),
410
411
				'S_SELECTED'		=> $condition_id == $selected,
412
			));
413
		}
414
	}
415
416
	/**
417
	 * {@inheritdoc}
418
	 */
419
	public function set_page_url($u_action)
420
	{
421
		$this->u_action = $u_action;
422
	}
423
}
424