Passed
Push — release-2.1 ( 2e48b8...01120d )
by Mathias
06:33
created

removeIllegalBBCHtmlPermission()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 16
rs 10
1
<?php
2
3
/**
4
 * ManagePermissions handles all possible permission stuff.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines http://www.simplemachines.org
10
 * @copyright 2019 Simple Machines and individual contributors
11
 * @license http://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 RC1
14
 */
15
16
if (!defined('SMF'))
17
	die('No direct access...');
18
19
/**
20
 * Dispatches to the right function based on the given subaction.
21
 * Checks the permissions, based on the sub-action.
22
 * Called by ?action=managepermissions.
23
 *
24
 * @uses ManagePermissions language file.
25
 */
26
27
function ModifyPermissions()
28
{
29
	global $txt, $context;
30
31
	loadLanguage('ManagePermissions+ManageMembers');
32
	loadTemplate('ManagePermissions');
33
34
	// Format: 'sub-action' => array('function_to_call', 'permission_needed'),
35
	$subActions = array(
36
		'board' => array('PermissionByBoard', 'manage_permissions'),
37
		'index' => array('PermissionIndex', 'manage_permissions'),
38
		'modify' => array('ModifyMembergroup', 'manage_permissions'),
39
		'modify2' => array('ModifyMembergroup2', 'manage_permissions'),
40
		'quick' => array('SetQuickGroups', 'manage_permissions'),
41
		'quickboard' => array('SetQuickBoards', 'manage_permissions'),
42
		'postmod' => array('ModifyPostModeration', 'manage_permissions'),
43
		'profiles' => array('EditPermissionProfiles', 'manage_permissions'),
44
		'settings' => array('GeneralPermissionSettings', 'admin_forum'),
45
	);
46
47
	$_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) && empty($subActions[$_REQUEST['sa']]['disabled']) ? $_REQUEST['sa'] : (allowedTo('manage_permissions') ? 'index' : 'settings');
48
	isAllowedTo($subActions[$_REQUEST['sa']][1]);
49
50
	// Create the tabs for the template.
51
	$context[$context['admin_menu_name']]['tab_data'] = array(
52
		'title' => $txt['permissions_title'],
53
		'help' => 'permissions',
54
		'description' => '',
55
		'tabs' => array(
56
			'index' => array(
57
				'description' => $txt['permissions_groups'],
58
			),
59
			'board' => array(
60
				'description' => $txt['permission_by_board_desc'],
61
			),
62
			'profiles' => array(
63
				'description' => $txt['permissions_profiles_desc'],
64
			),
65
			'postmod' => array(
66
				'description' => $txt['permissions_post_moderation_desc'],
67
			),
68
			'settings' => array(
69
				'description' => $txt['permission_settings_desc'],
70
			),
71
		),
72
	);
73
74
	call_integration_hook('integrate_manage_permissions', array(&$subActions));
75
76
	call_helper($subActions[$_REQUEST['sa']][0]);
77
}
78
79
/**
80
 * Sets up the permissions by membergroup index page.
81
 * Called by ?action=managepermissions
82
 * Creates an array of all the groups with the number of members and permissions.
83
 *
84
 * @uses ManagePermissions language file.
85
 * @uses ManagePermissions template file.
86
 * @uses ManageBoards template, permission_index sub-template.
87
 */
88
function PermissionIndex()
89
{
90
	global $txt, $scripturl, $context, $settings, $modSettings, $smcFunc;
91
92
	$context['page_title'] = $txt['permissions_title'];
93
94
	// Load all the permissions. We'll need them in the template.
95
	loadAllPermissions();
96
97
	// Also load profiles, we may want to reset.
98
	loadPermissionProfiles();
99
100
	// Are we going to show the advanced options?
101
	$context['show_advanced_options'] = empty($context['admin_preferences']['app']);
102
103
	// Determine the number of ungrouped members.
104
	$request = $smcFunc['db_query']('', '
105
		SELECT COUNT(*)
106
		FROM {db_prefix}members
107
		WHERE id_group = {int:regular_group}',
108
		array(
109
			'regular_group' => 0,
110
		)
111
	);
112
	list ($num_members) = $smcFunc['db_fetch_row']($request);
113
	$smcFunc['db_free_result']($request);
114
115
	// Fill the context variable with 'Guests' and 'Regular Members'.
116
	$context['groups'] = array(
117
		-1 => array(
118
			'id' => -1,
119
			'name' => $txt['membergroups_guests'],
120
			'num_members' => $txt['membergroups_guests_na'],
121
			'allow_delete' => false,
122
			'allow_modify' => true,
123
			'can_search' => false,
124
			'href' => '',
125
			'link' => '',
126
			'help' => 'membergroup_guests',
127
			'is_post_group' => false,
128
			'color' => '',
129
			'icons' => '',
130
			'children' => array(),
131
			'num_permissions' => array(
132
				'allowed' => 0,
133
				// Can't deny guest permissions!
134
				'denied' => '(' . $txt['permissions_none'] . ')'
135
			),
136
			'access' => false
137
		),
138
		0 => array(
139
			'id' => 0,
140
			'name' => $txt['membergroups_members'],
141
			'num_members' => $num_members,
142
			'allow_delete' => false,
143
			'allow_modify' => true,
144
			'can_search' => false,
145
			'href' => $scripturl . '?action=moderate;area=viewgroups;sa=members;group=0',
146
			'help' => 'membergroup_regular_members',
147
			'is_post_group' => false,
148
			'color' => '',
149
			'icons' => '',
150
			'children' => array(),
151
			'num_permissions' => array(
152
				'allowed' => 0,
153
				'denied' => 0
154
			),
155
			'access' => false
156
		),
157
	);
158
159
	$postGroups = array();
160
	$normalGroups = array();
161
162
	// Query the database defined membergroups.
163
	$query = $smcFunc['db_query']('', '
164
		SELECT id_group, id_parent, group_name, min_posts, online_color, icons
165
		FROM {db_prefix}membergroups' . (empty($modSettings['permission_enable_postgroups']) ? '
166
		WHERE min_posts = {int:min_posts}' : '') . '
167
		ORDER BY id_parent = {int:not_inherited} DESC, min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
168
		array(
169
			'min_posts' => -1,
170
			'not_inherited' => -2,
171
			'newbie_group' => 4,
172
		)
173
	);
174
	while ($row = $smcFunc['db_fetch_assoc']($query))
175
	{
176
		// If it's inherited, just add it as a child.
177
		if ($row['id_parent'] != -2)
178
		{
179
			if (isset($context['groups'][$row['id_parent']]))
180
				$context['groups'][$row['id_parent']]['children'][$row['id_group']] = $row['group_name'];
181
			continue;
182
		}
183
184
		$row['icons'] = explode('#', $row['icons']);
185
		$context['groups'][$row['id_group']] = array(
186
			'id' => $row['id_group'],
187
			'name' => $row['group_name'],
188
			'num_members' => $row['id_group'] != 3 ? 0 : $txt['membergroups_guests_na'],
189
			'allow_delete' => $row['id_group'] > 4,
190
			'allow_modify' => $row['id_group'] > 1,
191
			'can_search' => $row['id_group'] != 3,
192
			'href' => $scripturl . '?action=moderate;area=viewgroups;sa=members;group=' . $row['id_group'],
193
			'help' => $row['id_group'] == 1 ? 'membergroup_administrator' : ($row['id_group'] == 3 ? 'membergroup_moderator' : ''),
194
			'is_post_group' => $row['min_posts'] != -1,
195
			'color' => empty($row['online_color']) ? '' : $row['online_color'],
196
			'icons' => !empty($row['icons'][0]) && !empty($row['icons'][1]) ? str_repeat('<img src="' . $settings['images_url'] . '/' . $row['icons'][1] . '" alt="*">', $row['icons'][0]) : '',
197
			'children' => array(),
198
			'num_permissions' => array(
199
				'allowed' => $row['id_group'] == 1 ? '(' . $txt['permissions_all'] . ')' : 0,
200
				'denied' => $row['id_group'] == 1 ? '(' . $txt['permissions_none'] . ')' : 0
201
			),
202
			'access' => false,
203
		);
204
205
		if ($row['min_posts'] == -1)
206
			$normalGroups[$row['id_group']] = $row['id_group'];
207
		else
208
			$postGroups[$row['id_group']] = $row['id_group'];
209
	}
210
	$smcFunc['db_free_result']($query);
211
212
	// Get the number of members in this post group.
213
	if (!empty($postGroups))
214
	{
215
		$query = $smcFunc['db_query']('', '
216
			SELECT id_post_group AS id_group, COUNT(*) AS num_members
217
			FROM {db_prefix}members
218
			WHERE id_post_group IN ({array_int:post_group_list})
219
			GROUP BY id_post_group',
220
			array(
221
				'post_group_list' => $postGroups,
222
			)
223
		);
224
		while ($row = $smcFunc['db_fetch_assoc']($query))
225
			$context['groups'][$row['id_group']]['num_members'] += $row['num_members'];
226
		$smcFunc['db_free_result']($query);
227
	}
228
229
	if (!empty($normalGroups))
230
	{
231
		// First, the easy one!
232
		$query = $smcFunc['db_query']('', '
233
			SELECT id_group, COUNT(*) AS num_members
234
			FROM {db_prefix}members
235
			WHERE id_group IN ({array_int:normal_group_list})
236
			GROUP BY id_group',
237
			array(
238
				'normal_group_list' => $normalGroups,
239
			)
240
		);
241
		while ($row = $smcFunc['db_fetch_assoc']($query))
242
			$context['groups'][$row['id_group']]['num_members'] += $row['num_members'];
243
		$smcFunc['db_free_result']($query);
244
245
		// This one is slower, but it's okay... careful not to count twice!
246
		$query = $smcFunc['db_query']('', '
247
			SELECT mg.id_group, COUNT(*) AS num_members
248
			FROM {db_prefix}membergroups AS mg
249
				INNER JOIN {db_prefix}members AS mem ON (mem.additional_groups != {string:blank_string}
250
					AND mem.id_group != mg.id_group
251
					AND FIND_IN_SET(mg.id_group, mem.additional_groups) != 0)
252
			WHERE mg.id_group IN ({array_int:normal_group_list})
253
			GROUP BY mg.id_group',
254
			array(
255
				'normal_group_list' => $normalGroups,
256
				'blank_string' => '',
257
			)
258
		);
259
		while ($row = $smcFunc['db_fetch_assoc']($query))
260
			$context['groups'][$row['id_group']]['num_members'] += $row['num_members'];
261
		$smcFunc['db_free_result']($query);
262
	}
263
264
	foreach ($context['groups'] as $id => $data)
265
	{
266
		if ($data['href'] != '')
267
			$context['groups'][$id]['link'] = '<a href="' . $data['href'] . '">' . $data['num_members'] . '</a>';
268
	}
269
270
	if (empty($_REQUEST['pid']))
271
	{
272
		$request = $smcFunc['db_query']('', '
273
			SELECT id_group, COUNT(*) AS num_permissions, add_deny
274
			FROM {db_prefix}permissions
275
			' . (empty($context['hidden_permissions']) ? '' : ' WHERE permission NOT IN ({array_string:hidden_permissions})') . '
276
			GROUP BY id_group, add_deny',
277
			array(
278
				'hidden_permissions' => !empty($context['hidden_permissions']) ? $context['hidden_permissions'] : array(),
279
			)
280
		);
281
		while ($row = $smcFunc['db_fetch_assoc']($request))
282
			if (isset($context['groups'][(int) $row['id_group']]) && (!empty($row['add_deny']) || $row['id_group'] != -1))
283
				$context['groups'][(int) $row['id_group']]['num_permissions'][empty($row['add_deny']) ? 'denied' : 'allowed'] = $row['num_permissions'];
284
		$smcFunc['db_free_result']($request);
285
286
		// Get the "default" profile permissions too.
287
		$request = $smcFunc['db_query']('', '
288
			SELECT id_profile, id_group, COUNT(*) AS num_permissions, add_deny
289
			FROM {db_prefix}board_permissions
290
			WHERE id_profile = {int:default_profile}
291
			' . (empty($context['hidden_permissions']) ? '' : ' AND permission NOT IN ({array_string:hidden_permissions})') . '
292
			GROUP BY id_profile, id_group, add_deny',
293
			array(
294
				'default_profile' => 1,
295
				'hidden_permissions' => !empty($context['hidden_permissions']) ? $context['hidden_permissions'] : array(),
296
			)
297
		);
298
		while ($row = $smcFunc['db_fetch_assoc']($request))
299
		{
300
			if (isset($context['groups'][(int) $row['id_group']]) && (!empty($row['add_deny']) || $row['id_group'] != -1))
301
				$context['groups'][(int) $row['id_group']]['num_permissions'][empty($row['add_deny']) ? 'denied' : 'allowed'] += $row['num_permissions'];
302
		}
303
		$smcFunc['db_free_result']($request);
304
	}
305
	else
306
	{
307
		$_REQUEST['pid'] = (int) $_REQUEST['pid'];
308
309
		if (!isset($context['profiles'][$_REQUEST['pid']]))
310
			fatal_lang_error('no_access', false);
311
312
		// Change the selected tab to better reflect that this really is a board profile.
313
		$context[$context['admin_menu_name']]['current_subsection'] = 'profiles';
314
315
		$request = $smcFunc['db_query']('', '
316
			SELECT id_profile, id_group, COUNT(*) AS num_permissions, add_deny
317
			FROM {db_prefix}board_permissions
318
			WHERE id_profile = {int:current_profile}
319
			GROUP BY id_profile, id_group, add_deny',
320
			array(
321
				'current_profile' => $_REQUEST['pid'],
322
			)
323
		);
324
		while ($row = $smcFunc['db_fetch_assoc']($request))
325
		{
326
			if (isset($context['groups'][(int) $row['id_group']]) && (!empty($row['add_deny']) || $row['id_group'] != -1))
327
				$context['groups'][(int) $row['id_group']]['num_permissions'][empty($row['add_deny']) ? 'denied' : 'allowed'] += $row['num_permissions'];
328
		}
329
		$smcFunc['db_free_result']($request);
330
331
		$context['profile'] = array(
332
			'id' => $_REQUEST['pid'],
333
			'name' => $context['profiles'][$_REQUEST['pid']]['name'],
334
		);
335
	}
336
337
	// We can modify any permission set apart from the read only, reply only and no polls ones as they are redefined.
338
	$context['can_modify'] = empty($_REQUEST['pid']) || $_REQUEST['pid'] == 1 || $_REQUEST['pid'] > 4;
339
340
	// Load the proper template.
341
	$context['sub_template'] = 'permission_index';
342
	createToken('admin-mpq');
343
}
344
345
/**
346
 * Handle permissions by board... more or less. :P
347
 */
348
function PermissionByBoard()
349
{
350
	global $context, $txt, $smcFunc, $sourcedir, $cat_tree, $boardList, $boards;
351
352
	$context['page_title'] = $txt['permissions_boards'];
353
	$context['edit_all'] = isset($_GET['edit']);
354
355
	// Saving?
356
	if (!empty($_POST['save_changes']) && !empty($_POST['boardprofile']))
357
	{
358
		checkSession('request');
359
		validateToken('admin-mpb');
360
361
		$changes = array();
362
		foreach ($_POST['boardprofile'] as $pBoard => $profile)
363
		{
364
			$changes[(int) $profile][] = (int) $pBoard;
365
		}
366
367
		if (!empty($changes))
368
		{
369
			foreach ($changes as $profile => $boards)
370
				$smcFunc['db_query']('', '
371
					UPDATE {db_prefix}boards
372
					SET id_profile = {int:current_profile}
373
					WHERE id_board IN ({array_int:board_list})',
374
					array(
375
						'board_list' => $boards,
376
						'current_profile' => $profile,
377
					)
378
				);
379
		}
380
381
		$context['edit_all'] = false;
382
	}
383
384
	// Load all permission profiles.
385
	loadPermissionProfiles();
386
387
	// Get the board tree.
388
	require_once($sourcedir . '/Subs-Boards.php');
389
390
	getBoardTree();
391
392
	// Build the list of the boards.
393
	$context['categories'] = array();
394
	foreach ($cat_tree as $catid => $tree)
395
	{
396
		$context['categories'][$catid] = array(
397
			'name' => &$tree['node']['name'],
398
			'id' => &$tree['node']['id'],
399
			'boards' => array()
400
		);
401
		foreach ($boardList[$catid] as $boardid)
402
		{
403
			if (!isset($context['profiles'][$boards[$boardid]['profile']]))
404
				$boards[$boardid]['profile'] = 1;
405
406
			$context['categories'][$catid]['boards'][$boardid] = array(
407
				'id' => &$boards[$boardid]['id'],
408
				'name' => &$boards[$boardid]['name'],
409
				'description' => &$boards[$boardid]['description'],
410
				'child_level' => &$boards[$boardid]['level'],
411
				'profile' => &$boards[$boardid]['profile'],
412
				'profile_name' => $context['profiles'][$boards[$boardid]['profile']]['name'],
413
			);
414
		}
415
	}
416
417
	$context['sub_template'] = 'by_board';
418
	createToken('admin-mpb');
419
}
420
421
/**
422
 * Handles permission modification actions from the upper part of the
423
 * permission manager index.
424
 */
425
function SetQuickGroups()
426
{
427
	global $context, $smcFunc;
428
429
	checkSession();
430
	validateToken('admin-mpq', 'quick');
431
432
	loadIllegalPermissions();
433
	loadIllegalGuestPermissions();
434
	loadIllegalBBCHtmlGroups();
435
436
	// Make sure only one of the quick options was selected.
437
	if ((!empty($_POST['predefined']) && ((isset($_POST['copy_from']) && $_POST['copy_from'] != 'empty') || !empty($_POST['permissions']))) || (!empty($_POST['copy_from']) && $_POST['copy_from'] != 'empty' && !empty($_POST['permissions'])))
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (! empty($_POST['predefi...($_POST['permissions']), Probably Intended Meaning: ! empty($_POST['predefin...$_POST['permissions']))
Loading history...
438
		fatal_lang_error('permissions_only_one_option', false);
439
440
	if (empty($_POST['group']) || !is_array($_POST['group']))
441
		$_POST['group'] = array();
442
443
	// Only accept numeric values for selected membergroups.
444
	foreach ($_POST['group'] as $id => $group_id)
445
		$_POST['group'][$id] = (int) $group_id;
446
	$_POST['group'] = array_unique($_POST['group']);
447
448
	if (empty($_REQUEST['pid']))
449
		$_REQUEST['pid'] = 0;
450
	else
451
		$_REQUEST['pid'] = (int) $_REQUEST['pid'];
452
453
	// Fix up the old global to the new default!
454
	$bid = max(1, $_REQUEST['pid']);
455
456
	// No modifying the predefined profiles.
457
	if ($_REQUEST['pid'] > 1 && $_REQUEST['pid'] < 5)
458
		fatal_lang_error('no_access', false);
459
460
	// Clear out any cached authority.
461
	updateSettings(array('settings_updated' => time()));
462
463
	// No groups were selected.
464
	if (empty($_POST['group']))
465
		redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
466
467
	// Set a predefined permission profile.
468
	if (!empty($_POST['predefined']))
469
	{
470
		// Make sure it's a predefined permission set we expect.
471
		if (!in_array($_POST['predefined'], array('restrict', 'standard', 'moderator', 'maintenance')))
472
			redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
473
474
		foreach ($_POST['group'] as $group_id)
475
		{
476
			if (!empty($_REQUEST['pid']))
477
				setPermissionLevel($_POST['predefined'], $group_id, $_REQUEST['pid']);
478
			else
479
				setPermissionLevel($_POST['predefined'], $group_id);
480
		}
481
	}
482
	// Set a permission profile based on the permissions of a selected group.
483
	elseif ($_POST['copy_from'] != 'empty')
484
	{
485
		// Just checking the input.
486
		if (!is_numeric($_POST['copy_from']))
487
			redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
488
489
		// Make sure the group we're copying to is never included.
490
		$_POST['group'] = array_diff($_POST['group'], array($_POST['copy_from']));
491
492
		// No groups left? Too bad.
493
		if (empty($_POST['group']))
494
			redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
495
496
		if (empty($_REQUEST['pid']))
497
		{
498
			// Retrieve current permissions of group.
499
			$request = $smcFunc['db_query']('', '
500
				SELECT permission, add_deny
501
				FROM {db_prefix}permissions
502
				WHERE id_group = {int:copy_from}',
503
				array(
504
					'copy_from' => $_POST['copy_from'],
505
				)
506
			);
507
			$target_perm = array();
508
			while ($row = $smcFunc['db_fetch_assoc']($request))
509
				$target_perm[$row['permission']] = $row['add_deny'];
510
			$smcFunc['db_free_result']($request);
511
512
			$inserts = array();
513
			foreach ($_POST['group'] as $group_id)
514
				foreach ($target_perm as $perm => $add_deny)
515
				{
516
					// No dodgy permissions please!
517
					if (!empty($context['illegal_permissions']) && in_array($perm, $context['illegal_permissions']))
518
						continue;
519
					if (isset($context['permissions_excluded'][$perm]) && in_array($group_id, $context['permissions_excluded'][$perm]))
520
						continue;
521
522
					if ($group_id != 1 && $group_id != 3)
523
						$inserts[] = array($perm, $group_id, $add_deny);
524
				}
525
526
			// Delete the previous permissions...
527
			$smcFunc['db_query']('', '
528
				DELETE FROM {db_prefix}permissions
529
				WHERE id_group IN ({array_int:group_list})
530
					' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
531
				array(
532
					'group_list' => $_POST['group'],
533
					'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
534
				)
535
			);
536
537
			if (!empty($inserts))
538
			{
539
				// ..and insert the new ones.
540
				$smcFunc['db_insert']('',
541
					'{db_prefix}permissions',
542
					array(
543
						'permission' => 'string', 'id_group' => 'int', 'add_deny' => 'int',
544
					),
545
					$inserts,
546
					array('permission', 'id_group')
547
				);
548
			}
549
		}
550
551
		// Now do the same for the board permissions.
552
		$request = $smcFunc['db_query']('', '
553
			SELECT permission, add_deny
554
			FROM {db_prefix}board_permissions
555
			WHERE id_group = {int:copy_from}
556
				AND id_profile = {int:current_profile}',
557
			array(
558
				'copy_from' => $_POST['copy_from'],
559
				'current_profile' => $bid,
560
			)
561
		);
562
		$target_perm = array();
563
		while ($row = $smcFunc['db_fetch_assoc']($request))
564
			$target_perm[$row['permission']] = $row['add_deny'];
565
		$smcFunc['db_free_result']($request);
566
567
		$inserts = array();
568
		foreach ($_POST['group'] as $group_id)
569
			foreach ($target_perm as $perm => $add_deny)
570
			{
571
				// Are these for guests?
572
				if ($group_id == -1 && in_array($perm, $context['non_guest_permissions']))
573
					continue;
574
575
				$inserts[] = array($perm, $group_id, $bid, $add_deny);
576
			}
577
578
		// Delete the previous global board permissions...
579
		$smcFunc['db_query']('', '
580
			DELETE FROM {db_prefix}board_permissions
581
			WHERE id_group IN ({array_int:current_group_list})
582
				AND id_profile = {int:current_profile}',
583
			array(
584
				'current_group_list' => $_POST['group'],
585
				'current_profile' => $bid,
586
			)
587
		);
588
589
		// And insert the copied permissions.
590
		if (!empty($inserts))
591
		{
592
			// ..and insert the new ones.
593
			$smcFunc['db_insert']('',
594
				'{db_prefix}board_permissions',
595
				array('permission' => 'string', 'id_group' => 'int', 'id_profile' => 'int', 'add_deny' => 'int'),
596
				$inserts,
597
				array('permission', 'id_group', 'id_profile')
598
			);
599
		}
600
601
		// Update any children out there!
602
		updateChildPermissions($_POST['group'], $_REQUEST['pid']);
603
	}
604
	// Set or unset a certain permission for the selected groups.
605
	elseif (!empty($_POST['permissions']))
606
	{
607
		// Unpack two variables that were transported.
608
		list ($permissionType, $permission) = explode('/', $_POST['permissions']);
609
610
		// Check whether our input is within expected range.
611
		if (!in_array($_POST['add_remove'], array('add', 'clear', 'deny')) || !in_array($permissionType, array('membergroup', 'board')))
612
			redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
613
614
		if ($_POST['add_remove'] == 'clear')
615
		{
616
			if ($permissionType == 'membergroup')
617
			{
618
				$smcFunc['db_query']('', '
619
					DELETE FROM {db_prefix}permissions
620
					WHERE id_group IN ({array_int:current_group_list})
621
						AND permission = {string:current_permission}
622
						' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
623
					array(
624
						'current_group_list' => $_POST['group'],
625
						'current_permission' => $permission,
626
						'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
627
					)
628
				);
629
630
				// Did these changes make anyone lose eligibility for the bbc_html permission?
631
				if (!empty(array_diff($_POST['group'], $context['permissions_excluded']['bbc_html'])))
632
					removeIllegalBBCHtmlPermission(true);
633
			}
634
			else
635
				$smcFunc['db_query']('', '
636
					DELETE FROM {db_prefix}board_permissions
637
					WHERE id_group IN ({array_int:current_group_list})
638
						AND id_profile = {int:current_profile}
639
						AND permission = {string:current_permission}',
640
					array(
641
						'current_group_list' => $_POST['group'],
642
						'current_profile' => $bid,
643
						'current_permission' => $permission,
644
					)
645
				);
646
		}
647
		// Add a permission (either 'set' or 'deny').
648
		else
649
		{
650
			$add_deny = $_POST['add_remove'] == 'add' ? '1' : '0';
651
			$permChange = array();
652
			foreach ($_POST['group'] as $groupID)
653
			{
654
				if (isset($context['permissions_excluded'][$permission]) && in_array($groupID, $context['permissions_excluded'][$permission]))
655
					continue;
656
657
				if ($permissionType == 'membergroup' && $groupID != 1 && $groupID != 3 && (empty($context['illegal_permissions']) || !in_array($permission, $context['illegal_permissions'])))
658
					$permChange[] = array($permission, $groupID, $add_deny);
659
				elseif ($permissionType != 'membergroup')
660
					$permChange[] = array($permission, $groupID, $bid, $add_deny);
661
			}
662
663
			if (!empty($permChange))
664
			{
665
				if ($permissionType == 'membergroup')
666
					$smcFunc['db_insert']('replace',
667
						'{db_prefix}permissions',
668
						array('permission' => 'string', 'id_group' => 'int', 'add_deny' => 'int'),
669
						$permChange,
670
						array('permission', 'id_group')
671
					);
672
				// Board permissions go into the other table.
673
				else
674
					$smcFunc['db_insert']('replace',
675
						'{db_prefix}board_permissions',
676
						array('permission' => 'string', 'id_group' => 'int', 'id_profile' => 'int', 'add_deny' => 'int'),
677
						$permChange,
678
						array('permission', 'id_group', 'id_profile')
679
					);
680
			}
681
		}
682
683
		// Another child update!
684
		updateChildPermissions($_POST['group'], $_REQUEST['pid']);
685
	}
686
687
	redirectexit('action=admin;area=permissions;pid=' . $_REQUEST['pid']);
688
}
689
690
/**
691
 * Initializes the necessary to modify a membergroup's permissions.
692
 */
693
function ModifyMembergroup()
694
{
695
	global $context, $txt, $smcFunc, $modSettings;
696
697
	if (!isset($_GET['group']))
698
		fatal_lang_error('no_access', false);
699
700
	$context['group']['id'] = (int) $_GET['group'];
701
702
	// It's not likely you'd end up here with this setting disabled.
703
	if ($_GET['group'] == 1)
704
		redirectexit('action=admin;area=permissions');
705
706
	loadAllPermissions();
707
	loadPermissionProfiles();
708
	$context['hidden_perms'] = array();
709
710
	if ($context['group']['id'] > 0)
711
	{
712
		$result = $smcFunc['db_query']('', '
713
			SELECT group_name, id_parent
714
			FROM {db_prefix}membergroups
715
			WHERE id_group = {int:current_group}
716
			LIMIT 1',
717
			array(
718
				'current_group' => $context['group']['id'],
719
			)
720
		);
721
		list ($context['group']['name'], $parent) = $smcFunc['db_fetch_row']($result);
722
		$smcFunc['db_free_result']($result);
723
724
		// Cannot edit an inherited group!
725
		if ($parent != -2)
726
			fatal_lang_error('cannot_edit_permissions_inherited');
727
	}
728
	elseif ($context['group']['id'] == -1)
729
		$context['group']['name'] = $txt['membergroups_guests'];
730
	else
731
		$context['group']['name'] = $txt['membergroups_members'];
732
733
	$context['profile']['id'] = empty($_GET['pid']) ? 0 : (int) $_GET['pid'];
734
735
	// If this is a moderator and they are editing "no profile" then we only do boards.
736
	if ($context['group']['id'] == 3 && empty($context['profile']['id']))
737
	{
738
		// For sanity just check they have no general permissions.
739
		$smcFunc['db_query']('', '
740
			DELETE FROM {db_prefix}permissions
741
			WHERE id_group = {int:moderator_group}',
742
			array(
743
				'moderator_group' => 3,
744
			)
745
		);
746
747
		$context['profile']['id'] = 1;
748
	}
749
750
	$context['permission_type'] = empty($context['profile']['id']) ? 'membergroup' : 'board';
751
	$context['profile']['can_modify'] = !$context['profile']['id'] || $context['profiles'][$context['profile']['id']]['can_modify'];
752
753
	// Set up things a little nicer for board related stuff...
754
	if ($context['permission_type'] == 'board')
755
	{
756
		$context['profile']['name'] = $context['profiles'][$context['profile']['id']]['name'];
757
		$context[$context['admin_menu_name']]['current_subsection'] = 'profiles';
758
	}
759
760
	// Fetch the current permissions.
761
	$permissions = array(
762
		'membergroup' => array('allowed' => array(), 'denied' => array()),
763
		'board' => array('allowed' => array(), 'denied' => array())
764
	);
765
766
	// General permissions?
767
	if ($context['permission_type'] == 'membergroup')
768
	{
769
		$result = $smcFunc['db_query']('', '
770
			SELECT permission, add_deny
771
			FROM {db_prefix}permissions
772
			WHERE id_group = {int:current_group}',
773
			array(
774
				'current_group' => $_GET['group'],
775
			)
776
		);
777
		while ($row = $smcFunc['db_fetch_assoc']($result))
778
			$permissions['membergroup'][empty($row['add_deny']) ? 'denied' : 'allowed'][] = $row['permission'];
779
		$smcFunc['db_free_result']($result);
780
	}
781
782
	// Fetch current board permissions...
783
	$result = $smcFunc['db_query']('', '
784
		SELECT permission, add_deny
785
		FROM {db_prefix}board_permissions
786
		WHERE id_group = {int:current_group}
787
			AND id_profile = {int:current_profile}',
788
		array(
789
			'current_group' => $context['group']['id'],
790
			'current_profile' => $context['permission_type'] == 'membergroup' ? 1 : $context['profile']['id'],
791
		)
792
	);
793
	while ($row = $smcFunc['db_fetch_assoc']($result))
794
		$permissions['board'][empty($row['add_deny']) ? 'denied' : 'allowed'][] = $row['permission'];
795
	$smcFunc['db_free_result']($result);
796
797
	// Loop through each permission and set whether it's checked.
798
	foreach ($context['permissions'] as $permissionType => $tmp)
799
	{
800
		foreach ($tmp['columns'] as $position => $permissionGroups)
801
		{
802
			foreach ($permissionGroups as $permissionGroup => $permissionArray)
803
			{
804
				foreach ($permissionArray['permissions'] as $perm)
805
				{
806
					// Create a shortcut for the current permission.
807
					$curPerm = &$context['permissions'][$permissionType]['columns'][$position][$permissionGroup]['permissions'][$perm['id']];
808
809
					if ($perm['has_own_any'])
810
					{
811
						$curPerm['any']['select'] = in_array($perm['id'] . '_any', $permissions[$permissionType]['allowed']) ? 'on' : (in_array($perm['id'] . '_any', $permissions[$permissionType]['denied']) ? 'deny' : 'off');
812
						$curPerm['own']['select'] = in_array($perm['id'] . '_own', $permissions[$permissionType]['allowed']) ? 'on' : (in_array($perm['id'] . '_own', $permissions[$permissionType]['denied']) ? 'deny' : 'off');
813
					}
814
					else
815
						$curPerm['select'] = in_array($perm['id'], $permissions[$permissionType]['denied']) ? 'deny' : (in_array($perm['id'], $permissions[$permissionType]['allowed']) ? 'on' : 'off');
816
817
					// Keep the last value if it's hidden.
818
					if ($perm['hidden'] || $permissionArray['hidden'])
819
					{
820
						if ($perm['has_own_any'])
821
						{
822
							$context['hidden_perms'][] = array(
823
								$permissionType,
824
								$perm['own']['id'],
825
								$curPerm['own']['select'] == 'deny' && !empty($modSettings['permission_enable_deny']) ? 'deny' : $curPerm['own']['select'],
826
							);
827
							$context['hidden_perms'][] = array(
828
								$permissionType,
829
								$perm['any']['id'],
830
								$curPerm['any']['select'] == 'deny' && !empty($modSettings['permission_enable_deny']) ? 'deny' : $curPerm['any']['select'],
831
							);
832
						}
833
						else
834
							$context['hidden_perms'][] = array(
835
								$permissionType,
836
								$perm['id'],
837
								$curPerm['select'] == 'deny' && !empty($modSettings['permission_enable_deny']) ? 'deny' : $curPerm['select'],
838
							);
839
					}
840
				}
841
			}
842
		}
843
	}
844
	$context['sub_template'] = 'modify_group';
845
	$context['page_title'] = $txt['permissions_modify_group'];
846
847
	createToken('admin-mp');
848
}
849
850
/**
851
 * This function actually saves modifications to a membergroup's board permissions.
852
 */
853
function ModifyMembergroup2()
854
{
855
	global $smcFunc, $context;
856
857
	checkSession();
858
	validateToken('admin-mp');
859
860
	loadIllegalPermissions();
861
862
	$_GET['group'] = (int) $_GET['group'];
863
	$_GET['pid'] = (int) $_GET['pid'];
864
865
	// Cannot modify predefined profiles.
866
	if ($_GET['pid'] > 1 && $_GET['pid'] < 5)
867
		fatal_lang_error('no_access', false);
868
869
	// Verify this isn't inherited.
870
	if ($_GET['group'] == -1 || $_GET['group'] == 0)
871
		$parent = -2;
872
	else
873
	{
874
		$result = $smcFunc['db_query']('', '
875
			SELECT id_parent
876
			FROM {db_prefix}membergroups
877
			WHERE id_group = {int:current_group}
878
			LIMIT 1',
879
			array(
880
				'current_group' => $_GET['group'],
881
			)
882
		);
883
		list ($parent) = $smcFunc['db_fetch_row']($result);
884
		$smcFunc['db_free_result']($result);
885
	}
886
887
	if ($parent != -2)
888
		fatal_lang_error('cannot_edit_permissions_inherited');
889
890
	$givePerms = array('membergroup' => array(), 'board' => array());
891
892
	// Guest group, we need illegal, guest permissions.
893
	if ($_GET['group'] == -1)
894
	{
895
		loadIllegalGuestPermissions();
896
		$context['illegal_permissions'] = array_merge($context['illegal_permissions'], $context['non_guest_permissions']);
897
	}
898
899
	// Prepare all permissions that were set or denied for addition to the DB.
900
	if (isset($_POST['perm']) && is_array($_POST['perm']))
901
	{
902
		foreach ($_POST['perm'] as $perm_type => $perm_array)
903
		{
904
			if (is_array($perm_array))
905
			{
906
				foreach ($perm_array as $permission => $value)
907
					if ($value == 'on' || $value == 'deny')
908
					{
909
						// Don't allow people to escalate themselves!
910
						if (!empty($context['illegal_permissions']) && in_array($permission, $context['illegal_permissions']))
911
							continue;
912
913
						$givePerms[$perm_type][] = array($_GET['group'], $permission, $value == 'deny' ? 0 : 1);
914
					}
915
			}
916
		}
917
	}
918
919
	// Insert the general permissions.
920
	if ($_GET['group'] != 3 && empty($_GET['pid']))
921
	{
922
		$smcFunc['db_query']('', '
923
			DELETE FROM {db_prefix}permissions
924
			WHERE id_group = {int:current_group}
925
			' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
926
			array(
927
				'current_group' => $_GET['group'],
928
				'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
929
			)
930
		);
931
932
		if (!empty($givePerms['membergroup']))
933
		{
934
			$smcFunc['db_insert']('replace',
935
				'{db_prefix}permissions',
936
				array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
937
				$givePerms['membergroup'],
938
				array('id_group', 'permission')
939
			);
940
		}
941
	}
942
943
	// Insert the boardpermissions.
944
	$profileid = max(1, $_GET['pid']);
945
	$smcFunc['db_query']('', '
946
		DELETE FROM {db_prefix}board_permissions
947
		WHERE id_group = {int:current_group}
948
			AND id_profile = {int:current_profile}',
949
		array(
950
			'current_group' => $_GET['group'],
951
			'current_profile' => $profileid,
952
		)
953
	);
954
	if (!empty($givePerms['board']))
955
	{
956
		foreach ($givePerms['board'] as $k => $v)
957
			$givePerms['board'][$k][] = $profileid;
958
959
		$smcFunc['db_insert']('replace',
960
			'{db_prefix}board_permissions',
961
			array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int', 'id_profile' => 'int'),
962
			$givePerms['board'],
963
			array('id_group', 'permission', 'id_profile')
964
		);
965
	}
966
967
	// Update any inherited permissions as required.
968
	updateChildPermissions($_GET['group'], $_GET['pid']);
969
970
	removeIllegalBBCHtmlPermission();
971
972
	// Clear cached privs.
973
	updateSettings(array('settings_updated' => time()));
974
975
	redirectexit('action=admin;area=permissions;pid=' . $_GET['pid']);
976
}
977
978
/**
979
 * A screen to set some general settings for permissions.
980
 *
981
 * @param bool $return_config Whether to return the $config_vars array (used for admin search)
982
 * @return void|array Returns nothing or returns the config_vars array if $return_config is true
983
 */
984
function GeneralPermissionSettings($return_config = false)
985
{
986
	global $context, $modSettings, $sourcedir, $txt, $scripturl, $smcFunc;
987
988
	// All the setting variables
989
	$config_vars = array(
990
		array('title', 'settings'),
991
		// Inline permissions.
992
		array('permissions', 'manage_permissions'),
993
		'',
994
995
		// A few useful settings
996
		array('check', 'permission_enable_deny', 0, $txt['permission_settings_enable_deny'], 'help' => 'permissions_deny'),
997
		array('check', 'permission_enable_postgroups', 0, $txt['permission_settings_enable_postgroups'], 'help' => 'permissions_postgroups'),
998
	);
999
1000
	call_integration_hook('integrate_modify_permission_settings', array(&$config_vars));
1001
1002
	if ($return_config)
1003
		return $config_vars;
1004
1005
	$context['page_title'] = $txt['permission_settings_title'];
1006
	$context['sub_template'] = 'show_settings';
1007
1008
	// Needed for the inline permission functions, and the settings template.
1009
	require_once($sourcedir . '/ManageServer.php');
1010
1011
	$context['post_url'] = $scripturl . '?action=admin;area=permissions;save;sa=settings';
1012
1013
	// Saving the settings?
1014
	if (isset($_GET['save']))
1015
	{
1016
		checkSession();
1017
		call_integration_hook('integrate_save_permission_settings');
1018
		saveDBSettings($config_vars);
1019
1020
		// Clear all deny permissions...if we want that.
1021
		if (empty($modSettings['permission_enable_deny']))
1022
		{
1023
			$smcFunc['db_query']('', '
1024
				DELETE FROM {db_prefix}permissions
1025
				WHERE add_deny = {int:denied}',
1026
				array(
1027
					'denied' => 0,
1028
				)
1029
			);
1030
			$smcFunc['db_query']('', '
1031
				DELETE FROM {db_prefix}board_permissions
1032
				WHERE add_deny = {int:denied}',
1033
				array(
1034
					'denied' => 0,
1035
				)
1036
			);
1037
		}
1038
1039
		// Make sure there are no postgroup based permissions left.
1040
		if (empty($modSettings['permission_enable_postgroups']))
1041
		{
1042
			// Get a list of postgroups.
1043
			$post_groups = array();
1044
			$request = $smcFunc['db_query']('', '
1045
				SELECT id_group
1046
				FROM {db_prefix}membergroups
1047
				WHERE min_posts != {int:min_posts}',
1048
				array(
1049
					'min_posts' => -1,
1050
				)
1051
			);
1052
			while ($row = $smcFunc['db_fetch_assoc']($request))
1053
				$post_groups[] = $row['id_group'];
1054
			$smcFunc['db_free_result']($request);
1055
1056
			// Remove'em.
1057
			$smcFunc['db_query']('', '
1058
				DELETE FROM {db_prefix}permissions
1059
				WHERE id_group IN ({array_int:post_group_list})',
1060
				array(
1061
					'post_group_list' => $post_groups,
1062
				)
1063
			);
1064
			$smcFunc['db_query']('', '
1065
				DELETE FROM {db_prefix}board_permissions
1066
				WHERE id_group IN ({array_int:post_group_list})',
1067
				array(
1068
					'post_group_list' => $post_groups,
1069
				)
1070
			);
1071
			$smcFunc['db_query']('', '
1072
				UPDATE {db_prefix}membergroups
1073
				SET id_parent = {int:not_inherited}
1074
				WHERE id_parent IN ({array_int:post_group_list})',
1075
				array(
1076
					'post_group_list' => $post_groups,
1077
					'not_inherited' => -2,
1078
				)
1079
			);
1080
		}
1081
1082
		$_SESSION['adm-save'] = true;
1083
		redirectexit('action=admin;area=permissions;sa=settings');
1084
	}
1085
1086
	// We need this for the in-line permissions
1087
	createToken('admin-mp');
1088
1089
	prepareDBSettingContext($config_vars);
1090
}
1091
1092
/**
1093
 * Set the permission level for a specific profile, group, or group for a profile.
1094
 *
1095
 * @internal
1096
 *
1097
 * @param string $level The level ('restrict', 'standard', etc.)
1098
 * @param int $group The group to set the permission for
1099
 * @param string|int $profile The ID of the permissions profile or 'null' if we're setting it for a group
1100
 */
1101
function setPermissionLevel($level, $group, $profile = 'null')
1102
{
1103
	global $smcFunc, $context;
1104
1105
	loadIllegalPermissions();
1106
	loadIllegalGuestPermissions();
1107
	loadIllegalBBCHtmlGroups();
1108
1109
	// Levels by group... restrict, standard, moderator, maintenance.
1110
	$groupLevels = array(
1111
		'board' => array('inherit' => array()),
1112
		'group' => array('inherit' => array())
1113
	);
1114
	// Levels by board... standard, publish, free.
1115
	$boardLevels = array('inherit' => array());
1116
1117
	// Restrictive - ie. guests.
1118
	$groupLevels['global']['restrict'] = array(
1119
		'search_posts',
1120
		'calendar_view',
1121
		'view_stats',
1122
		'who_view',
1123
		'profile_identity_own',
1124
	);
1125
	$groupLevels['board']['restrict'] = array(
1126
		'poll_view',
1127
		'post_new',
1128
		'post_reply_own',
1129
		'post_reply_any',
1130
		'delete_own',
1131
		'modify_own',
1132
		'report_any',
1133
	);
1134
1135
	// Standard - ie. members.  They can do anything Restrictive can.
1136
	$groupLevels['global']['standard'] = array_merge($groupLevels['global']['restrict'], array(
1137
		'view_mlist',
1138
		'likes_like',
1139
		'mention',
1140
		'pm_read',
1141
		'pm_send',
1142
		'profile_view',
1143
		'profile_extra_own',
1144
		'profile_signature_own',
1145
		'profile_forum_own',
1146
		'profile_website_own',
1147
		'profile_password_own',
1148
		'profile_server_avatar',
1149
		'profile_displayed_name',
1150
		'profile_upload_avatar',
1151
		'profile_remote_avatar',
1152
		'profile_remove_own',
1153
		'report_user',
1154
	));
1155
	$groupLevels['board']['standard'] = array_merge($groupLevels['board']['restrict'], array(
1156
		'poll_vote',
1157
		'poll_edit_own',
1158
		'poll_post',
1159
		'poll_add_own',
1160
		'post_attachment',
1161
		'lock_own',
1162
		'remove_own',
1163
		'view_attachments',
1164
	));
1165
1166
	// Moderator - ie. moderators :P.  They can do what standard can, and more.
1167
	$groupLevels['global']['moderator'] = array_merge($groupLevels['global']['standard'], array(
1168
		'calendar_post',
1169
		'calendar_edit_own',
1170
		'access_mod_center',
1171
		'issue_warning',
1172
	));
1173
	$groupLevels['board']['moderator'] = array_merge($groupLevels['board']['standard'], array(
1174
		'make_sticky',
1175
		'poll_edit_any',
1176
		'delete_any',
1177
		'modify_any',
1178
		'lock_any',
1179
		'remove_any',
1180
		'move_any',
1181
		'merge_any',
1182
		'split_any',
1183
		'poll_lock_any',
1184
		'poll_remove_any',
1185
		'poll_add_any',
1186
		'approve_posts',
1187
	));
1188
1189
	// Maintenance - wannabe admins.  They can do almost everything.
1190
	$groupLevels['global']['maintenance'] = array_merge($groupLevels['global']['moderator'], array(
1191
		'manage_attachments',
1192
		'manage_smileys',
1193
		'manage_boards',
1194
		'moderate_forum',
1195
		'manage_membergroups',
1196
		'manage_bans',
1197
		'admin_forum',
1198
		'bbc_html',
1199
		'manage_permissions',
1200
		'edit_news',
1201
		'calendar_edit_any',
1202
		'profile_identity_any',
1203
		'profile_extra_any',
1204
		'profile_signature_any',
1205
		'profile_website_any',
1206
		'profile_displayed_name_any',
1207
		'profile_password_any',
1208
		'profile_title_any',
1209
	));
1210
	$groupLevels['board']['maintenance'] = array_merge($groupLevels['board']['moderator'], array(
1211
	));
1212
1213
	// Standard - nothing above the group permissions. (this SHOULD be empty.)
1214
	$boardLevels['standard'] = array(
1215
	);
1216
1217
	// Locked - just that, you can't post here.
1218
	$boardLevels['locked'] = array(
1219
		'poll_view',
1220
		'report_any',
1221
		'view_attachments',
1222
	);
1223
1224
	// Publisher - just a little more...
1225
	$boardLevels['publish'] = array_merge($boardLevels['locked'], array(
1226
		'post_new',
1227
		'post_reply_own',
1228
		'post_reply_any',
1229
		'delete_own',
1230
		'modify_own',
1231
		'delete_replies',
1232
		'modify_replies',
1233
		'poll_vote',
1234
		'poll_edit_own',
1235
		'poll_post',
1236
		'poll_add_own',
1237
		'poll_remove_own',
1238
		'post_attachment',
1239
		'lock_own',
1240
		'remove_own',
1241
	));
1242
1243
	// Free for All - Scary.  Just scary.
1244
	$boardLevels['free'] = array_merge($boardLevels['publish'], array(
1245
		'poll_lock_any',
1246
		'poll_edit_any',
1247
		'poll_add_any',
1248
		'poll_remove_any',
1249
		'make_sticky',
1250
		'lock_any',
1251
		'remove_any',
1252
		'delete_any',
1253
		'split_any',
1254
		'merge_any',
1255
		'modify_any',
1256
		'approve_posts',
1257
	));
1258
1259
	call_integration_hook('integrate_load_permission_levels', array(&$groupLevels, &$boardLevels));
1260
1261
	// Make sure we're not granting someone too many permissions!
1262
	foreach ($groupLevels['global'][$level] as $k => $permission)
1263
	{
1264
		if (!empty($context['illegal_permissions']) && in_array($permission, $context['illegal_permissions']))
1265
			unset($groupLevels['global'][$level][$k]);
1266
1267
		if (isset($context['permissions_excluded'][$permission]) && in_array($group, $context['permissions_excluded'][$permission]))
1268
			unset($groupLevels['global'][$level][$k]);
1269
	}
1270
	foreach ($groupLevels['board'][$level] as $k => $permission)
1271
		if (isset($context['permissions_excluded'][$permission]) && in_array($group, $context['permissions_excluded'][$permission]))
1272
			unset($groupLevels['board'][$level][$k]);
1273
1274
	// Reset all cached permissions.
1275
	updateSettings(array('settings_updated' => time()));
1276
1277
	// Setting group permissions.
1278
	if ($profile === 'null' && $group !== 'null')
1279
	{
1280
		$group = (int) $group;
1281
1282
		if (empty($groupLevels['global'][$level]))
1283
			return;
1284
1285
		$smcFunc['db_query']('', '
1286
			DELETE FROM {db_prefix}permissions
1287
			WHERE id_group = {int:current_group}
1288
			' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
1289
			array(
1290
				'current_group' => $group,
1291
				'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
1292
			)
1293
		);
1294
		$smcFunc['db_query']('', '
1295
			DELETE FROM {db_prefix}board_permissions
1296
			WHERE id_group = {int:current_group}
1297
				AND id_profile = {int:default_profile}',
1298
			array(
1299
				'current_group' => $group,
1300
				'default_profile' => 1,
1301
			)
1302
		);
1303
1304
		$groupInserts = array();
1305
		foreach ($groupLevels['global'][$level] as $permission)
1306
			$groupInserts[] = array($group, $permission);
1307
1308
		$smcFunc['db_insert']('insert',
1309
			'{db_prefix}permissions',
1310
			array('id_group' => 'int', 'permission' => 'string'),
1311
			$groupInserts,
1312
			array('id_group')
1313
		);
1314
1315
		$boardInserts = array();
1316
		foreach ($groupLevels['board'][$level] as $permission)
1317
			$boardInserts[] = array(1, $group, $permission);
1318
1319
		$smcFunc['db_insert']('insert',
1320
			'{db_prefix}board_permissions',
1321
			array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
1322
			$boardInserts,
1323
			array('id_profile', 'id_group')
1324
		);
1325
1326
		removeIllegalBBCHtmlPermission();
1327
	}
1328
	// Setting profile permissions for a specific group.
1329
	elseif ($profile !== 'null' && $group !== 'null' && ($profile == 1 || $profile > 4))
1330
	{
1331
		$group = (int) $group;
1332
		$profile = (int) $profile;
1333
1334
		if (!empty($groupLevels['global'][$level]))
1335
		{
1336
			$smcFunc['db_query']('', '
1337
				DELETE FROM {db_prefix}board_permissions
1338
				WHERE id_group = {int:current_group}
1339
					AND id_profile = {int:current_profile}',
1340
				array(
1341
					'current_group' => $group,
1342
					'current_profile' => $profile,
1343
				)
1344
			);
1345
		}
1346
1347
		if (!empty($groupLevels['board'][$level]))
1348
		{
1349
			$boardInserts = array();
1350
			foreach ($groupLevels['board'][$level] as $permission)
1351
				$boardInserts[] = array($profile, $group, $permission);
1352
1353
			$smcFunc['db_insert']('insert',
1354
				'{db_prefix}board_permissions',
1355
				array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
1356
				$boardInserts,
1357
				array('id_profile', 'id_group')
1358
			);
1359
		}
1360
	}
1361
	// Setting profile permissions for all groups.
1362
	elseif ($profile !== 'null' && $group === 'null' && ($profile == 1 || $profile > 4))
0 ignored issues
show
introduced by
The condition $group === 'null' is always false.
Loading history...
1363
	{
1364
		$profile = (int) $profile;
1365
1366
		$smcFunc['db_query']('', '
1367
			DELETE FROM {db_prefix}board_permissions
1368
			WHERE id_profile = {int:current_profile}',
1369
			array(
1370
				'current_profile' => $profile,
1371
			)
1372
		);
1373
1374
		if (empty($boardLevels[$level]))
1375
			return;
1376
1377
		// Get all the groups...
1378
		$query = $smcFunc['db_query']('', '
1379
			SELECT id_group
1380
			FROM {db_prefix}membergroups
1381
			WHERE id_group > {int:moderator_group}
1382
			ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
1383
			array(
1384
				'moderator_group' => 3,
1385
				'newbie_group' => 4,
1386
			)
1387
		);
1388
		while ($row = $smcFunc['db_fetch_row']($query))
1389
		{
1390
			$group = $row[0];
1391
1392
			$boardInserts = array();
1393
			foreach ($boardLevels[$level] as $permission)
1394
				$boardInserts[] = array($profile, $group, $permission);
1395
1396
			$smcFunc['db_insert']('insert',
1397
				'{db_prefix}board_permissions',
1398
				array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
1399
				$boardInserts,
1400
				array('id_profile', 'id_group')
1401
			);
1402
		}
1403
		$smcFunc['db_free_result']($query);
1404
1405
		// Add permissions for ungrouped members.
1406
		$boardInserts = array();
1407
		foreach ($boardLevels[$level] as $permission)
1408
			$boardInserts[] = array($profile, 0, $permission);
1409
1410
		$smcFunc['db_insert']('insert',
1411
			'{db_prefix}board_permissions',
1412
			array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string'),
1413
			$boardInserts,
1414
			array('id_profile', 'id_group')
1415
		);
1416
	}
1417
	// $profile and $group are both null!
1418
	else
1419
		fatal_lang_error('no_access', false);
1420
}
1421
1422
/**
1423
 * Load permissions into $context['permissions'].
1424
 *
1425
 * @internal
1426
 */
1427
function loadAllPermissions()
1428
{
1429
	global $context, $txt, $modSettings;
1430
1431
	// List of all the groups dependant on the currently selected view - for the order so it looks pretty, yea?
1432
	// Note to Mod authors - you don't need to stick your permission group here if you don't mind SMF sticking it the last group of the page.
1433
	$permissionGroups = array(
1434
		'membergroup' => array(
1435
			'general',
1436
			'pm',
1437
			'calendar',
1438
			'maintenance',
1439
			'member_admin',
1440
			'profile',
1441
			'likes',
1442
			'mentions',
1443
			'bbc',
1444
		),
1445
		'board' => array(
1446
			'general_board',
1447
			'topic',
1448
			'post',
1449
			'poll',
1450
			'notification',
1451
			'attachment',
1452
		),
1453
	);
1454
1455
	/*   The format of this list is as follows:
1456
		'membergroup' => array(
1457
			'permissions_inside' => array(has_multiple_options, view_group),
1458
		),
1459
		'board' => array(
1460
			'permissions_inside' => array(has_multiple_options, view_group),
1461
		);
1462
	*/
1463
	$permissionList = array(
1464
		'membergroup' => array(
1465
			'view_stats' => array(false, 'general'),
1466
			'view_mlist' => array(false, 'general'),
1467
			'who_view' => array(false, 'general'),
1468
			'search_posts' => array(false, 'general'),
1469
			'pm_read' => array(false, 'pm'),
1470
			'pm_send' => array(false, 'pm'),
1471
			'pm_draft' => array(false, 'pm'),
1472
			'calendar_view' => array(false, 'calendar'),
1473
			'calendar_post' => array(false, 'calendar'),
1474
			'calendar_edit' => array(true, 'calendar'),
1475
			'admin_forum' => array(false, 'maintenance'),
1476
			'manage_boards' => array(false, 'maintenance'),
1477
			'manage_attachments' => array(false, 'maintenance'),
1478
			'manage_smileys' => array(false, 'maintenance'),
1479
			'edit_news' => array(false, 'maintenance'),
1480
			'access_mod_center' => array(false, 'maintenance'),
1481
			'moderate_forum' => array(false, 'member_admin'),
1482
			'manage_membergroups' => array(false, 'member_admin'),
1483
			'manage_permissions' => array(false, 'member_admin'),
1484
			'manage_bans' => array(false, 'member_admin'),
1485
			'send_mail' => array(false, 'member_admin'),
1486
			'issue_warning' => array(false, 'member_admin'),
1487
			'profile_view' => array(false, 'profile'),
1488
			'profile_forum' => array(true, 'profile'),
1489
			'profile_extra' => array(true, 'profile'),
1490
			'profile_signature' => array(true, 'profile'),
1491
			'profile_website' => array(true, 'profile'),
1492
			'profile_title' => array(true, 'profile'),
1493
			'profile_blurb' => array(true, 'profile'),
1494
			'profile_server_avatar' => array(false, 'profile'),
1495
			'profile_upload_avatar' => array(false, 'profile'),
1496
			'profile_remote_avatar' => array(false, 'profile'),
1497
			'report_user' => array(false, 'profile'),
1498
			'profile_identity' => array(true, 'profile_account'),
1499
			'profile_displayed_name' => array(true, 'profile_account'),
1500
			'profile_password' => array(true, 'profile_account'),
1501
			'profile_remove' => array(true, 'profile_account'),
1502
			'view_warning' => array(true, 'profile_account'),
1503
			'likes_like' => array(false, 'likes'),
1504
			'mention' => array(false, 'mentions'),
1505
		),
1506
		'board' => array(
1507
			'moderate_board' => array(false, 'general_board'),
1508
			'approve_posts' => array(false, 'general_board'),
1509
			'post_new' => array(false, 'topic'),
1510
			'post_unapproved_topics' => array(false, 'topic'),
1511
			'post_unapproved_replies' => array(true, 'topic'),
1512
			'post_reply' => array(true, 'topic'),
1513
			'post_draft' => array(false, 'topic'),
1514
			'merge_any' => array(false, 'topic'),
1515
			'split_any' => array(false, 'topic'),
1516
			'make_sticky' => array(false, 'topic'),
1517
			'move' => array(true, 'topic', 'moderate'),
1518
			'lock' => array(true, 'topic', 'moderate'),
1519
			'remove' => array(true, 'topic', 'modify'),
1520
			'modify_replies' => array(false, 'topic'),
1521
			'delete_replies' => array(false, 'topic'),
1522
			'announce_topic' => array(false, 'topic'),
1523
			'delete' => array(true, 'post'),
1524
			'modify' => array(true, 'post'),
1525
			'report_any' => array(false, 'post'),
1526
			'poll_view' => array(false, 'poll'),
1527
			'poll_vote' => array(false, 'poll'),
1528
			'poll_post' => array(false, 'poll'),
1529
			'poll_add' => array(true, 'poll'),
1530
			'poll_edit' => array(true, 'poll'),
1531
			'poll_lock' => array(true, 'poll'),
1532
			'poll_remove' => array(true, 'poll'),
1533
			'view_attachments' => array(false, 'attachment'),
1534
			'post_unapproved_attachments' => array(false, 'attachment'),
1535
			'post_attachment' => array(false, 'attachment'),
1536
		),
1537
	);
1538
1539
	// In case a mod screwed things up...
1540
	if (!in_array('html', $context['restricted_bbc']))
1541
		$context['restricted_bbc'][] = 'html';
1542
1543
	// Add the permissions for the restricted BBCodes
1544
	foreach ($context['restricted_bbc'] as $bbc)
1545
	{
1546
		$permissionList['membergroup']['bbc_' . $bbc] = array(false, 'bbc');
1547
		$txt['permissionname_bbc_' . $bbc] = sprintf($txt['permissionname_bbc'], $bbc);
1548
	}
1549
1550
	// All permission groups that will be shown in the left column on classic view.
1551
	$leftPermissionGroups = array(
1552
		'general',
1553
		'calendar',
1554
		'maintenance',
1555
		'member_admin',
1556
		'topic',
1557
		'post',
1558
	);
1559
1560
	// We need to know what permissions we can't give to guests.
1561
	loadIllegalGuestPermissions();
1562
1563
	// We also need to know which groups can't be given the bbc_html permission.
1564
	loadIllegalBBCHtmlGroups();
1565
1566
	// Some permissions are hidden if features are off.
1567
	$hiddenPermissions = array();
1568
	$relabelPermissions = array(); // Permissions to apply a different label to.
1569
	if (empty($modSettings['cal_enabled']))
1570
	{
1571
		$hiddenPermissions[] = 'calendar_view';
1572
		$hiddenPermissions[] = 'calendar_post';
1573
		$hiddenPermissions[] = 'calendar_edit';
1574
	}
1575
	if ($modSettings['warning_settings'][0] == 0)
1576
	{
1577
		$hiddenPermissions[] = 'issue_warning';
1578
		$hiddenPermissions[] = 'view_warning';
1579
	}
1580
1581
	// Post moderation?
1582
	if (!$modSettings['postmod_active'])
1583
	{
1584
		$hiddenPermissions[] = 'approve_posts';
1585
		$hiddenPermissions[] = 'post_unapproved_topics';
1586
		$hiddenPermissions[] = 'post_unapproved_replies';
1587
		$hiddenPermissions[] = 'post_unapproved_attachments';
1588
	}
1589
	// If post moderation is enabled, these are named differently...
1590
	else
1591
	{
1592
		// Relabel the topics permissions
1593
		$relabelPermissions['post_new'] = 'auto_approve_topics';
1594
1595
		// Relabel the reply permissions
1596
		$relabelPermissions['post_reply'] = 'auto_approve_replies';
1597
1598
		// Relabel the attachment permissions
1599
		$relabelPermissions['post_attachment'] = 'auto_approve_attachments';
1600
	}
1601
1602
	// Are attachments enabled?
1603
	if (empty($modSettings['attachmentEnable']))
1604
	{
1605
		$hiddenPermissions[] = 'manage_attachments';
1606
		$hiddenPermissions[] = 'view_attachments';
1607
		$hiddenPermissions[] = 'post_unapproved_attachments';
1608
		$hiddenPermissions[] = 'post_attachment';
1609
	}
1610
1611
	// Hide Likes/Mentions permissions...
1612
	if (empty($modSettings['enable_likes']))
1613
	{
1614
		$hiddenPermissions[] = 'likes_like';
1615
	}
1616
	if (empty($modSettings['enable_mentions']))
1617
	{
1618
		$hiddenPermissions[] = 'mention';
1619
	}
1620
1621
	// Provide a practical way to modify permissions.
1622
	call_integration_hook('integrate_load_permissions', array(&$permissionGroups, &$permissionList, &$leftPermissionGroups, &$hiddenPermissions, &$relabelPermissions));
1623
1624
	$permissionList['membergroup']['bbc_cowsay'] = array(false, 'bbc');
1625
	$hiddenPermissions[] = 'bbc_cowsay';
1626
1627
	$context['permissions'] = array();
1628
	$context['hidden_permissions'] = array();
1629
	foreach ($permissionList as $permissionType => $permissionList)
1630
	{
1631
		$context['permissions'][$permissionType] = array(
1632
			'id' => $permissionType,
1633
			'columns' => array()
1634
		);
1635
		foreach ($permissionList as $permission => $permissionArray)
1636
		{
1637
			// If this permission shouldn't be given to certain groups (e.g. guests), don't.
1638
			if (isset($context['group']['id']) && isset($context['permissions_excluded'][$permission]) && in_array($context['group']['id'], $context['permissions_excluded'][$permission]))
1639
				continue;
1640
1641
			// What groups will this permission be in?
1642
			$own_group = $permissionArray[1];
1643
1644
			// First, Do these groups actually exist - if not add them.
1645
			if (!isset($permissionGroups[$permissionType][$own_group]))
1646
				$permissionGroups[$permissionType][$own_group] = true;
1647
1648
			// What column should this be located into?
1649
			$position = !in_array($own_group, $leftPermissionGroups) ? 1 : 0;
1650
1651
			// If the groups have not yet been created be sure to create them.
1652
			$bothGroups = array('own' => $own_group);
1653
1654
			foreach ($bothGroups as $group)
1655
				if (!isset($context['permissions'][$permissionType]['columns'][$position][$group]))
1656
					$context['permissions'][$permissionType]['columns'][$position][$group] = array(
1657
						'type' => $permissionType,
1658
						'id' => $group,
1659
						'name' => $txt['permissiongroup_' . $group],
1660
						'icon' => isset($txt['permissionicon_' . $group]) ? $txt['permissionicon_' . $group] : $txt['permissionicon'],
1661
						'help' => isset($txt['permissionhelp_' . $group]) ? $txt['permissionhelp_' . $group] : '',
1662
						'hidden' => false,
1663
						'permissions' => array()
1664
					);
1665
1666
			$context['permissions'][$permissionType]['columns'][$position][$own_group]['permissions'][$permission] = array(
1667
				'id' => $permission,
1668
				'name' => !isset($relabelPermissions[$permission]) ? $txt['permissionname_' . $permission] : $txt[$relabelPermissions[$permission]],
1669
				'show_help' => isset($txt['permissionhelp_' . $permission]),
1670
				'note' => isset($txt['permissionnote_' . $permission]) ? $txt['permissionnote_' . $permission] : '',
1671
				'has_own_any' => $permissionArray[0],
1672
				'own' => array(
1673
					'id' => $permission . '_own',
1674
					'name' => $permissionArray[0] ? $txt['permissionname_' . $permission . '_own'] : ''
1675
				),
1676
				'any' => array(
1677
					'id' => $permission . '_any',
1678
					'name' => $permissionArray[0] ? $txt['permissionname_' . $permission . '_any'] : ''
1679
				),
1680
				'hidden' => in_array($permission, $hiddenPermissions),
1681
			);
1682
1683
			if (in_array($permission, $hiddenPermissions))
1684
			{
1685
				if ($permissionArray[0])
1686
				{
1687
					$context['hidden_permissions'][] = $permission . '_own';
1688
					$context['hidden_permissions'][] = $permission . '_any';
1689
				}
1690
				else
1691
					$context['hidden_permissions'][] = $permission;
1692
			}
1693
		}
1694
		ksort($context['permissions'][$permissionType]['columns']);
1695
1696
		// Check we don't leave any empty groups - and mark hidden ones as such.
1697
		foreach ($context['permissions'][$permissionType]['columns'] as $column => $groups)
1698
			foreach ($groups as $id => $group)
1699
			{
1700
				if (empty($group['permissions']))
1701
					unset($context['permissions'][$permissionType]['columns'][$column][$id]);
1702
				else
1703
				{
1704
					$foundNonHidden = false;
1705
					foreach ($group['permissions'] as $permission)
1706
						if (empty($permission['hidden']))
1707
							$foundNonHidden = true;
1708
					if (!$foundNonHidden)
1709
						$context['permissions'][$permissionType]['columns'][$column][$id]['hidden'] = true;
1710
				}
1711
			}
1712
	}
1713
}
1714
1715
/**
1716
 * Initialize a form with inline permissions settings.
1717
 * It loads a context variable for each permission.
1718
 * This function is used by several settings screens to set specific permissions.
1719
 *
1720
 * To exclude groups from the form for a given permission, add the group IDs as
1721
 * an array to $context['excluded_permissions'][$permission]. For backwards
1722
 * compatibility, it is also possible to pass group IDs in via the
1723
 * $excluded_groups parameter, which will exclude the groups from the forms for
1724
 * all of the permissions passed in via $permissions.
1725
 *
1726
 * @internal
1727
 *
1728
 * @param array $permissions The permissions to display inline
1729
 * @param array $excluded_groups The IDs of one or more groups to exclude
1730
 *
1731
 * @uses ManagePermissions language
1732
 * @uses ManagePermissions template.
1733
 */
1734
function init_inline_permissions($permissions, $excluded_groups = array())
1735
{
1736
	global $context, $txt, $modSettings, $smcFunc;
1737
1738
	loadLanguage('ManagePermissions');
1739
	loadTemplate('ManagePermissions');
1740
	$context['can_change_permissions'] = allowedTo('manage_permissions');
1741
1742
	// Nothing to initialize here.
1743
	if (!$context['can_change_permissions'])
1744
		return;
1745
1746
	// Load the permission settings for guests
1747
	foreach ($permissions as $permission)
1748
		$context[$permission] = array(
1749
			-1 => array(
1750
				'id' => -1,
1751
				'name' => $txt['membergroups_guests'],
1752
				'is_postgroup' => false,
1753
				'status' => 'off',
1754
			),
1755
			0 => array(
1756
				'id' => 0,
1757
				'name' => $txt['membergroups_members'],
1758
				'is_postgroup' => false,
1759
				'status' => 'off',
1760
			),
1761
		);
1762
1763
	$request = $smcFunc['db_query']('', '
1764
		SELECT id_group, CASE WHEN add_deny = {int:denied} THEN {string:deny} ELSE {string:on} END AS status, permission
1765
		FROM {db_prefix}permissions
1766
		WHERE id_group IN (-1, 0)
1767
			AND permission IN ({array_string:permissions})',
1768
		array(
1769
			'denied' => 0,
1770
			'permissions' => $permissions,
1771
			'deny' => 'deny',
1772
			'on' => 'on',
1773
		)
1774
	);
1775
	while ($row = $smcFunc['db_fetch_assoc']($request))
1776
		$context[$row['permission']][$row['id_group']]['status'] = $row['status'];
1777
	$smcFunc['db_free_result']($request);
1778
1779
	$request = $smcFunc['db_query']('', '
1780
		SELECT mg.id_group, mg.group_name, mg.min_posts, COALESCE(p.add_deny, -1) AS status, p.permission
1781
		FROM {db_prefix}membergroups AS mg
1782
			LEFT JOIN {db_prefix}permissions AS p ON (p.id_group = mg.id_group AND p.permission IN ({array_string:permissions}))
1783
		WHERE mg.id_group NOT IN (1, 3)
1784
			AND mg.id_parent = {int:not_inherited}' . (empty($modSettings['permission_enable_postgroups']) ? '
1785
			AND mg.min_posts = {int:min_posts}' : '') . '
1786
		ORDER BY mg.min_posts, CASE WHEN mg.id_group < {int:newbie_group} THEN mg.id_group ELSE 4 END, mg.group_name',
1787
		array(
1788
			'not_inherited' => -2,
1789
			'min_posts' => -1,
1790
			'newbie_group' => 4,
1791
			'permissions' => $permissions,
1792
		)
1793
	);
1794
	while ($row = $smcFunc['db_fetch_assoc']($request))
1795
	{
1796
		// Initialize each permission as being 'off' until proven otherwise.
1797
		foreach ($permissions as $permission)
1798
			if (!isset($context[$permission][$row['id_group']]))
1799
				$context[$permission][$row['id_group']] = array(
1800
					'id' => $row['id_group'],
1801
					'name' => $row['group_name'],
1802
					'is_postgroup' => $row['min_posts'] != -1,
1803
					'status' => 'off',
1804
				);
1805
1806
		$context[$row['permission']][$row['id_group']]['status'] = empty($row['status']) ? 'deny' : ($row['status'] == 1 ? 'on' : 'off');
1807
	}
1808
	$smcFunc['db_free_result']($request);
1809
1810
	// Make sure we honor the "illegal guest permissions"
1811
	loadIllegalGuestPermissions();
1812
1813
	// Only special people can have this permission
1814
	if (in_array('bbc_html', $permissions))
1815
		loadIllegalBBCHtmlGroups();
1816
1817
	// Are any of these permissions that guests can't have?
1818
	$non_guest_perms = array_intersect(str_replace(array('_any', '_own'), '', $permissions), $context['non_guest_permissions']);
1819
	foreach ($non_guest_perms as $permission)
1820
	{
1821
		if (!isset($context['permissions_excluded'][$permission]) || !in_array(-1, $context['permissions_excluded'][$permission]))
1822
			$context['permissions_excluded'][$permission][] = -1;
1823
	}
1824
1825
	// Any explicitly excluded groups for this call?
1826
	if (!empty($excluded_groups))
1827
	{
1828
		// Make sure this is an array of integers
1829
		$excluded_groups = array_filter((array) $excluded_groups, function ($v)
1830
			{
1831
				return is_int($v) || is_string($v) && (string) intval($v) === $v;
1832
			});
1833
1834
		foreach ($permissions as $permission)
1835
			$context['permissions_excluded'][$permission] = array_unique(array_merge($context['permissions_excluded'][$permission], $excluded_groups));
1836
	}
1837
1838
	// Some permissions cannot be given to certain groups. Remove the groups.
1839
	foreach ($permissions as $permission)
1840
	{
1841
		if (!isset($context['permissions_excluded'][$permission]))
1842
			continue;
1843
1844
		foreach ($context['permissions_excluded'][$permission] as $group)
1845
		{
1846
			if (isset($context[$permission][$group]))
1847
				unset($context[$permission][$group]);
1848
		}
1849
1850
		// There's no point showing a form with nobody in it
1851
		if (empty($context[$permission]))
1852
			unset($context['config_vars'][$permission], $context[$permission]);
1853
	}
1854
1855
	// Create the token for the separate inline permission verification.
1856
	createToken('admin-mp');
1857
}
1858
1859
/**
1860
 * Show a collapsible box to set a specific permission.
1861
 * The function is called by templates to show a list of permissions settings.
1862
 * Calls the template function template_inline_permissions().
1863
 *
1864
 * @param string $permission The permission to display inline
1865
 */
1866
function theme_inline_permissions($permission)
1867
{
1868
	global $context;
1869
1870
	$context['current_permission'] = $permission;
1871
	$context['member_groups'] = $context[$permission];
1872
1873
	template_inline_permissions();
1874
}
1875
1876
/**
1877
 * Save the permissions of a form containing inline permissions.
1878
 *
1879
 * @internal
1880
 *
1881
 * @param array $permissions The permissions to save
1882
 */
1883
function save_inline_permissions($permissions)
1884
{
1885
	global $context, $smcFunc;
1886
1887
	// No permissions? Not a great deal to do here.
1888
	if (!allowedTo('manage_permissions'))
1889
		return;
1890
1891
	// Almighty session check, verify our ways.
1892
	checkSession();
1893
	validateToken('admin-mp');
1894
1895
	// Check they can't do certain things.
1896
	loadIllegalPermissions();
1897
	if (in_array('bbc_html', $permissions))
1898
		loadIllegalBBCHtmlGroups();
1899
1900
	$insertRows = array();
1901
	foreach ($permissions as $permission)
1902
	{
1903
		if (!isset($_POST[$permission]))
1904
			continue;
1905
1906
		foreach ($_POST[$permission] as $id_group => $value)
1907
		{
1908
			if ($value == 'on' && !empty($context['excluded_permissions'][$permission]) && in_array($id_group, $context['excluded_permissions'][$permission]))
1909
				continue;
1910
1911
			if (in_array($value, array('on', 'deny')) && (empty($context['illegal_permissions']) || !in_array($permission, $context['illegal_permissions'])))
1912
				$insertRows[] = array((int) $id_group, $permission, $value == 'on' ? 1 : 0);
1913
		}
1914
	}
1915
1916
	// Remove the old permissions...
1917
	$smcFunc['db_query']('', '
1918
		DELETE FROM {db_prefix}permissions
1919
		WHERE permission IN ({array_string:permissions})
1920
			' . (empty($context['illegal_permissions']) ? '' : ' AND permission NOT IN ({array_string:illegal_permissions})'),
1921
		array(
1922
			'illegal_permissions' => !empty($context['illegal_permissions']) ? $context['illegal_permissions'] : array(),
1923
			'permissions' => $permissions,
1924
		)
1925
	);
1926
1927
	// ...and replace them with new ones.
1928
	if (!empty($insertRows))
1929
		$smcFunc['db_insert']('insert',
1930
			'{db_prefix}permissions',
1931
			array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
1932
			$insertRows,
1933
			array('id_group', 'permission')
1934
		);
1935
1936
	// Do a full child update.
1937
	updateChildPermissions(array(), -1);
1938
1939
	// Just in case we cached this.
1940
	updateSettings(array('settings_updated' => time()));
1941
}
1942
1943
/**
1944
 * Load permissions profiles.
1945
 */
1946
function loadPermissionProfiles()
1947
{
1948
	global $context, $txt, $smcFunc;
1949
1950
	$request = $smcFunc['db_query']('', '
1951
		SELECT id_profile, profile_name
1952
		FROM {db_prefix}permission_profiles
1953
		ORDER BY id_profile',
1954
		array(
1955
		)
1956
	);
1957
	$context['profiles'] = array();
1958
	while ($row = $smcFunc['db_fetch_assoc']($request))
1959
	{
1960
		// Format the label nicely.
1961
		if (isset($txt['permissions_profile_' . $row['profile_name']]))
1962
			$name = $txt['permissions_profile_' . $row['profile_name']];
1963
		else
1964
			$name = $row['profile_name'];
1965
1966
		$context['profiles'][$row['id_profile']] = array(
1967
			'id' => $row['id_profile'],
1968
			'name' => $name,
1969
			'can_modify' => $row['id_profile'] == 1 || $row['id_profile'] > 4,
1970
			'unformatted_name' => $row['profile_name'],
1971
		);
1972
	}
1973
	$smcFunc['db_free_result']($request);
1974
}
1975
1976
/**
1977
 * Add/Edit/Delete profiles.
1978
 */
1979
function EditPermissionProfiles()
1980
{
1981
	global $context, $txt, $smcFunc;
1982
1983
	// Setup the template, first for fun.
1984
	$context['page_title'] = $txt['permissions_profile_edit'];
1985
	$context['sub_template'] = 'edit_profiles';
1986
1987
	// If we're creating a new one do it first.
1988
	if (isset($_POST['create']) && trim($_POST['profile_name']) != '')
1989
	{
1990
		checkSession();
1991
		validateToken('admin-mpp');
1992
1993
		$_POST['copy_from'] = (int) $_POST['copy_from'];
1994
		$_POST['profile_name'] = $smcFunc['htmlspecialchars']($_POST['profile_name']);
1995
1996
		// Insert the profile itself.
1997
		$profile_id = $smcFunc['db_insert']('',
1998
			'{db_prefix}permission_profiles',
1999
			array(
2000
				'profile_name' => 'string',
2001
			),
2002
			array(
2003
				$_POST['profile_name'],
2004
			),
2005
			array('id_profile'),
2006
			1
2007
		);
2008
2009
		// Load the permissions from the one it's being copied from.
2010
		$request = $smcFunc['db_query']('', '
2011
			SELECT id_group, permission, add_deny
2012
			FROM {db_prefix}board_permissions
2013
			WHERE id_profile = {int:copy_from}',
2014
			array(
2015
				'copy_from' => $_POST['copy_from'],
2016
			)
2017
		);
2018
		$inserts = array();
2019
		while ($row = $smcFunc['db_fetch_assoc']($request))
2020
			$inserts[] = array($profile_id, $row['id_group'], $row['permission'], $row['add_deny']);
2021
		$smcFunc['db_free_result']($request);
2022
2023
		if (!empty($inserts))
2024
			$smcFunc['db_insert']('insert',
2025
				'{db_prefix}board_permissions',
2026
				array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
2027
				$inserts,
2028
				array('id_profile', 'id_group', 'permission')
2029
			);
2030
	}
2031
	// Renaming?
2032
	elseif (isset($_POST['rename']))
2033
	{
2034
		checkSession();
2035
		validateToken('admin-mpp');
2036
2037
		// Just showing the boxes?
2038
		if (!isset($_POST['rename_profile']))
2039
			$context['show_rename_boxes'] = true;
2040
		else
2041
		{
2042
			foreach ($_POST['rename_profile'] as $id => $value)
2043
			{
2044
				$value = $smcFunc['htmlspecialchars']($value);
2045
2046
				if (trim($value) != '' && $id > 4)
2047
					$smcFunc['db_query']('', '
2048
						UPDATE {db_prefix}permission_profiles
2049
						SET profile_name = {string:profile_name}
2050
						WHERE id_profile = {int:current_profile}',
2051
						array(
2052
							'current_profile' => (int) $id,
2053
							'profile_name' => $value,
2054
						)
2055
					);
2056
			}
2057
		}
2058
	}
2059
	// Deleting?
2060
	elseif (isset($_POST['delete']) && !empty($_POST['delete_profile']))
2061
	{
2062
		checkSession();
2063
		validateToken('admin-mpp');
2064
2065
		$profiles = array();
2066
		foreach ($_POST['delete_profile'] as $profile)
2067
			if ($profile > 4)
2068
				$profiles[] = (int) $profile;
2069
2070
		// Verify it's not in use...
2071
		$request = $smcFunc['db_query']('', '
2072
			SELECT id_board
2073
			FROM {db_prefix}boards
2074
			WHERE id_profile IN ({array_int:profile_list})
2075
			LIMIT 1',
2076
			array(
2077
				'profile_list' => $profiles,
2078
			)
2079
		);
2080
		if ($smcFunc['db_num_rows']($request) != 0)
2081
			fatal_lang_error('no_access', false);
2082
		$smcFunc['db_free_result']($request);
2083
2084
		// Oh well, delete.
2085
		$smcFunc['db_query']('', '
2086
			DELETE FROM {db_prefix}permission_profiles
2087
			WHERE id_profile IN ({array_int:profile_list})',
2088
			array(
2089
				'profile_list' => $profiles,
2090
			)
2091
		);
2092
	}
2093
2094
	// Clearly, we'll need this!
2095
	loadPermissionProfiles();
2096
2097
	// Work out what ones are in use.
2098
	$request = $smcFunc['db_query']('', '
2099
		SELECT id_profile, COUNT(id_board) AS board_count
2100
		FROM {db_prefix}boards
2101
		GROUP BY id_profile',
2102
		array(
2103
		)
2104
	);
2105
	while ($row = $smcFunc['db_fetch_assoc']($request))
2106
		if (isset($context['profiles'][$row['id_profile']]))
2107
		{
2108
			$context['profiles'][$row['id_profile']]['in_use'] = true;
2109
			$context['profiles'][$row['id_profile']]['boards'] = $row['board_count'];
2110
			$context['profiles'][$row['id_profile']]['boards_text'] = $row['board_count'] > 1 ? sprintf($txt['permissions_profile_used_by_many'], $row['board_count']) : $txt['permissions_profile_used_by_' . ($row['board_count'] ? 'one' : 'none')];
2111
		}
2112
	$smcFunc['db_free_result']($request);
2113
2114
	// What can we do with these?
2115
	$context['can_edit_something'] = false;
2116
	foreach ($context['profiles'] as $id => $profile)
2117
	{
2118
		// Can't delete special ones.
2119
		$context['profiles'][$id]['can_edit'] = isset($txt['permissions_profile_' . $profile['unformatted_name']]) ? false : true;
2120
		if ($context['profiles'][$id]['can_edit'])
2121
			$context['can_edit_something'] = true;
2122
2123
		// You can only delete it if you can edit it AND it's not in use.
2124
		$context['profiles'][$id]['can_delete'] = $context['profiles'][$id]['can_edit'] && empty($profile['in_use']) ? true : false;
2125
	}
2126
2127
	createToken('admin-mpp');
2128
}
2129
2130
/**
2131
 * This function updates the permissions of any groups based off this group.
2132
 *
2133
 * @param null|array $parents The parent groups
2134
 * @param null|int $profile the ID of a permissions profile to update
2135
 * @return void|false Returns nothing if successful or false if there are no child groups to update
2136
 */
2137
function updateChildPermissions($parents, $profile = null)
2138
{
2139
	global $smcFunc;
2140
2141
	// All the parent groups to sort out.
2142
	if (!is_array($parents))
2143
		$parents = array($parents);
2144
2145
	// Find all the children of this group.
2146
	$request = $smcFunc['db_query']('', '
2147
		SELECT id_parent, id_group
2148
		FROM {db_prefix}membergroups
2149
		WHERE id_parent != {int:not_inherited}
2150
			' . (empty($parents) ? '' : 'AND id_parent IN ({array_int:parent_list})'),
2151
		array(
2152
			'parent_list' => $parents,
2153
			'not_inherited' => -2,
2154
		)
2155
	);
2156
	$children = array();
2157
	$parents = array();
2158
	$child_groups = array();
2159
	while ($row = $smcFunc['db_fetch_assoc']($request))
2160
	{
2161
		$children[$row['id_parent']][] = $row['id_group'];
2162
		$child_groups[] = $row['id_group'];
2163
		$parents[] = $row['id_parent'];
2164
	}
2165
	$smcFunc['db_free_result']($request);
2166
2167
	$parents = array_unique($parents);
2168
2169
	// Not a sausage, or a child?
2170
	if (empty($children))
2171
		return false;
2172
2173
	// First off, are we doing general permissions?
2174
	if ($profile < 1 || $profile === null)
2175
	{
2176
		// Fetch all the parent permissions.
2177
		$request = $smcFunc['db_query']('', '
2178
			SELECT id_group, permission, add_deny
2179
			FROM {db_prefix}permissions
2180
			WHERE id_group IN ({array_int:parent_list})',
2181
			array(
2182
				'parent_list' => $parents,
2183
			)
2184
		);
2185
		$permissions = array();
2186
		while ($row = $smcFunc['db_fetch_assoc']($request))
2187
			foreach ($children[$row['id_group']] as $child)
2188
				$permissions[] = array($child, $row['permission'], $row['add_deny']);
2189
		$smcFunc['db_free_result']($request);
2190
2191
		$smcFunc['db_query']('', '
2192
			DELETE FROM {db_prefix}permissions
2193
			WHERE id_group IN ({array_int:child_groups})',
2194
			array(
2195
				'child_groups' => $child_groups,
2196
			)
2197
		);
2198
2199
		// Finally insert.
2200
		if (!empty($permissions))
2201
		{
2202
			$smcFunc['db_insert']('insert',
2203
				'{db_prefix}permissions',
2204
				array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
2205
				$permissions,
2206
				array('id_group', 'permission')
2207
			);
2208
		}
2209
	}
2210
2211
	// Then, what about board profiles?
2212
	if ($profile != -1)
2213
	{
2214
		$profileQuery = $profile === null ? '' : ' AND id_profile = {int:current_profile}';
2215
2216
		// Again, get all the parent permissions.
2217
		$request = $smcFunc['db_query']('', '
2218
			SELECT id_profile, id_group, permission, add_deny
2219
			FROM {db_prefix}board_permissions
2220
			WHERE id_group IN ({array_int:parent_groups})
2221
				' . $profileQuery,
2222
			array(
2223
				'parent_groups' => $parents,
2224
				'current_profile' => $profile !== null && $profile ? $profile : 1,
2225
			)
2226
		);
2227
		$permissions = array();
2228
		while ($row = $smcFunc['db_fetch_assoc']($request))
2229
			foreach ($children[$row['id_group']] as $child)
2230
				$permissions[] = array($child, $row['id_profile'], $row['permission'], $row['add_deny']);
2231
		$smcFunc['db_free_result']($request);
2232
2233
		$smcFunc['db_query']('', '
2234
			DELETE FROM {db_prefix}board_permissions
2235
			WHERE id_group IN ({array_int:child_groups})
2236
				' . $profileQuery,
2237
			array(
2238
				'child_groups' => $child_groups,
2239
				'current_profile' => $profile !== null && $profile ? $profile : 1,
2240
			)
2241
		);
2242
2243
		// Do the insert.
2244
		if (!empty($permissions))
2245
		{
2246
			$smcFunc['db_insert']('insert',
2247
				'{db_prefix}board_permissions',
2248
				array('id_group' => 'int', 'id_profile' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
2249
				$permissions,
2250
				array('id_group', 'id_profile', 'permission')
2251
			);
2252
		}
2253
	}
2254
}
2255
2256
/**
2257
 * Load permissions someone cannot grant.
2258
 */
2259
function loadIllegalPermissions()
2260
{
2261
	global $context;
2262
2263
	$context['illegal_permissions'] = array();
2264
	if (!allowedTo('admin_forum'))
2265
	{
2266
		$context['illegal_permissions'][] = 'admin_forum';
2267
		$context['illegal_permissions'][] = 'bbc_html';
2268
	}
2269
	if (!allowedTo('manage_membergroups'))
2270
		$context['illegal_permissions'][] = 'manage_membergroups';
2271
	if (!allowedTo('manage_permissions'))
2272
		$context['illegal_permissions'][] = 'manage_permissions';
2273
2274
	call_integration_hook('integrate_load_illegal_permissions');
2275
}
2276
2277
/**
2278
 * Loads the permissions that can not be given to guests.
2279
 * Stores the permissions in $context['non_guest_permissions'].
2280
 * Also populates $context['permissions_excluded'] with the info.
2281
 */
2282
function loadIllegalGuestPermissions()
2283
{
2284
	global $context;
2285
2286
	$context['non_guest_permissions'] = array(
2287
		'access_mod_center',
2288
		'admin_forum',
2289
		'announce_topic',
2290
		'approve_posts',
2291
		'bbc_html',
2292
		'calendar_edit',
2293
		'delete',
2294
		'delete_replies',
2295
		'edit_news',
2296
		'issue_warning',
2297
		'likes_like',
2298
		'lock',
2299
		'make_sticky',
2300
		'manage_attachments',
2301
		'manage_bans',
2302
		'manage_boards',
2303
		'manage_membergroups',
2304
		'manage_permissions',
2305
		'manage_smileys',
2306
		'merge_any',
2307
		'moderate_board',
2308
		'moderate_forum',
2309
		'modify',
2310
		'modify_replies',
2311
		'move',
2312
		'pm_autosave_draft',
2313
		'pm_draft',
2314
		'pm_read',
2315
		'pm_send',
2316
		'poll_add',
2317
		'poll_edit',
2318
		'poll_lock',
2319
		'poll_remove',
2320
		'post_autosave_draft',
2321
		'post_draft',
2322
		'profile_blurb',
2323
		'profile_displayed_name',
2324
		'profile_extra',
2325
		'profile_forum',
2326
		'profile_identity',
2327
		'profile_website',
2328
		'profile_password',
2329
		'profile_remove',
2330
		'profile_remote_avatar',
2331
		'profile_server_avatar',
2332
		'profile_signature',
2333
		'profile_title',
2334
		'profile_upload_avatar',
2335
		'profile_warning',
2336
		'remove',
2337
		'report_any',
2338
		'report_user',
2339
		'send_mail',
2340
		'split_any',
2341
	);
2342
2343
	call_integration_hook('integrate_load_illegal_guest_permissions');
2344
2345
	// Also add this info to $context['permissions_excluded'] to make life easier for everyone
2346
	foreach ($context['non_guest_permissions'] as $permission)
2347
	{
2348
		if (empty($context['permissions_excluded'][$permission]) || !in_array($permission, $context['permissions_excluded'][$permission]))
2349
			$context['permissions_excluded'][$permission][] = -1;
2350
	}
2351
}
2352
2353
/**
2354
 * Loads a list of membergroups who cannot be granted the bbc_html permission.
2355
 * Stores the groups in $context['permissions_excluded']['bbc_html'].
2356
 */
2357
function loadIllegalBBCHtmlGroups()
2358
{
2359
	global $context, $smcFunc;
2360
2361
	$context['permissions_excluded']['bbc_html'] = array(-1, 0);
2362
2363
	$request = $smcFunc['db_query']('', '
2364
		SELECT id_group
2365
		FROM {db_prefix}membergroups
2366
		WHERE id_group != 1 AND id_group NOT IN (
2367
			SELECT DISTINCT id_group
2368
			FROM {db_prefix}permissions
2369
			WHERE permission IN ({array_string:permissions})
2370
				AND add_deny = {int:add}
2371
		)',
2372
		array(
2373
			'permissions' => array('admin_forum', 'manage_membergroups', 'manage_permissions'),
2374
			'add' => 1,
2375
		)
2376
	);
2377
	while ($row = $smcFunc['db_fetch_assoc']($request))
2378
		$context['permissions_excluded']['bbc_html'][] = $row['id_group'];
2379
	$smcFunc['db_free_result']($request);
2380
2381
	$context['permissions_excluded']['bbc_html'] = array_unique($context['permissions_excluded']['bbc_html']);
2382
}
2383
2384
/**
2385
 * Removes the bbc_html permission from anyone who shouldn't have it
2386
 *
2387
 * @param bool $reload Before acting, refresh the list of membergroups who cannot be granted the bbc_html permission
2388
 */
2389
function removeIllegalBBCHtmlPermission($reload = false)
2390
{
2391
	global $context, $smcFunc;
2392
2393
	if (empty($context['permissions_excluded']['bbc_html']) || $reload)
2394
		loadIllegalBBCHtmlGroups();
2395
2396
	$smcFunc['db_query']('', '
2397
		DELETE FROM {db_prefix}permissions
2398
		WHERE id_group IN ({array_int:current_group_list})
2399
			AND permission = {string:current_permission}
2400
			AND add_deny = {int:add}',
2401
		array(
2402
			'current_group_list' => $context['permissions_excluded']['bbc_html'],
2403
			'current_permission' => 'bbc_html',
2404
			'add' => 1,
2405
		)
2406
	);
2407
}
2408
2409
/**
2410
 * Present a nice way of applying post moderation.
2411
 */
2412
function ModifyPostModeration()
2413
{
2414
	global $context, $txt, $smcFunc, $modSettings, $sourcedir;
2415
2416
	// Just in case.
2417
	checkSession('get');
2418
2419
	$context['page_title'] = $txt['permissions_post_moderation'];
2420
	$context['sub_template'] = 'postmod_permissions';
2421
	$context['current_profile'] = isset($_REQUEST['pid']) ? (int) $_REQUEST['pid'] : 1;
2422
2423
	// Load all the permission profiles.
2424
	loadPermissionProfiles();
2425
2426
	// Mappings, our key => array(can_do_moderated, can_do_all)
2427
	$mappings = array(
2428
		'new_topic' => array('post_new', 'post_unapproved_topics'),
2429
		'replies_own' => array('post_reply_own', 'post_unapproved_replies_own'),
2430
		'replies_any' => array('post_reply_any', 'post_unapproved_replies_any'),
2431
		'attachment' => array('post_attachment', 'post_unapproved_attachments'),
2432
	);
2433
2434
	call_integration_hook('integrate_post_moderation_mapping', array(&$mappings));
2435
2436
	// Start this with the guests/members.
2437
	$context['profile_groups'] = array(
2438
		-1 => array(
2439
			'id' => -1,
2440
			'name' => $txt['membergroups_guests'],
2441
			'color' => '',
2442
			'new_topic' => 'disallow',
2443
			'replies_own' => 'disallow',
2444
			'replies_any' => 'disallow',
2445
			'attachment' => 'disallow',
2446
			'children' => array(),
2447
		),
2448
		0 => array(
2449
			'id' => 0,
2450
			'name' => $txt['membergroups_members'],
2451
			'color' => '',
2452
			'new_topic' => 'disallow',
2453
			'replies_own' => 'disallow',
2454
			'replies_any' => 'disallow',
2455
			'attachment' => 'disallow',
2456
			'children' => array(),
2457
		),
2458
	);
2459
2460
	// Load the groups.
2461
	$request = $smcFunc['db_query']('', '
2462
		SELECT id_group, group_name, online_color, id_parent
2463
		FROM {db_prefix}membergroups
2464
		WHERE id_group != {int:admin_group}
2465
			' . (empty($modSettings['permission_enable_postgroups']) ? ' AND min_posts = {int:min_posts}' : '') . '
2466
		ORDER BY id_parent ASC',
2467
		array(
2468
			'admin_group' => 1,
2469
			'min_posts' => -1,
2470
		)
2471
	);
2472
	while ($row = $smcFunc['db_fetch_assoc']($request))
2473
	{
2474
		if ($row['id_parent'] == -2)
2475
		{
2476
			$context['profile_groups'][$row['id_group']] = array(
2477
				'id' => $row['id_group'],
2478
				'name' => $row['group_name'],
2479
				'color' => $row['online_color'],
2480
				'new_topic' => 'disallow',
2481
				'replies_own' => 'disallow',
2482
				'replies_any' => 'disallow',
2483
				'attachment' => 'disallow',
2484
				'children' => array(),
2485
			);
2486
		}
2487
		elseif (isset($context['profile_groups'][$row['id_parent']]))
2488
			$context['profile_groups'][$row['id_parent']]['children'][] = $row['group_name'];
2489
	}
2490
	$smcFunc['db_free_result']($request);
2491
2492
	// What are the permissions we are querying?
2493
	$all_permissions = array();
2494
	foreach ($mappings as $perm_set)
2495
		$all_permissions = array_merge($all_permissions, $perm_set);
2496
2497
	// If we're saving the changes then do just that - save them.
2498
	if (!empty($_POST['save_changes']) && ($context['current_profile'] == 1 || $context['current_profile'] > 4))
2499
	{
2500
		validateToken('admin-mppm');
2501
2502
		// First, are we saving a new value for enabled post moderation?
2503
		$new_setting = !empty($_POST['postmod_active']);
2504
		if ($new_setting != $modSettings['postmod_active'])
2505
		{
2506
			if ($new_setting)
2507
			{
2508
				// Turning it on. This seems easy enough.
2509
				updateSettings(array('postmod_active' => 1));
2510
			}
2511
			else
2512
			{
2513
				// Turning it off. Not so straightforward. We have to turn off warnings to moderation level, and make everything approved.
2514
				updateSettings(array(
2515
					'postmod_active' => 0,
2516
					'warning_moderate' => 0,
2517
				));
2518
2519
				require_once($sourcedir . '/PostModeration.php');
2520
				approveAllData();
2521
			}
2522
		}
2523
		elseif ($modSettings['postmod_active'])
2524
		{
2525
			// We're not saving a new setting - and if it's still enabled we have more work to do.
2526
2527
			// Start by deleting all the permissions relevant.
2528
			$smcFunc['db_query']('', '
2529
				DELETE FROM {db_prefix}board_permissions
2530
				WHERE id_profile = {int:current_profile}
2531
					AND permission IN ({array_string:permissions})
2532
					AND id_group IN ({array_int:profile_group_list})',
2533
				array(
2534
					'profile_group_list' => array_keys($context['profile_groups']),
2535
					'current_profile' => $context['current_profile'],
2536
					'permissions' => $all_permissions,
2537
				)
2538
			);
2539
2540
			// Do it group by group.
2541
			$new_permissions = array();
2542
			foreach ($context['profile_groups'] as $id => $group)
2543
			{
2544
				foreach ($mappings as $index => $data)
2545
				{
2546
					if (isset($_POST[$index][$group['id']]))
2547
					{
2548
						if ($_POST[$index][$group['id']] == 'allow')
2549
						{
2550
							// Give them both sets for fun.
2551
							$new_permissions[] = array($context['current_profile'], $group['id'], $data[0], 1);
2552
							$new_permissions[] = array($context['current_profile'], $group['id'], $data[1], 1);
2553
						}
2554
						elseif ($_POST[$index][$group['id']] == 'moderate')
2555
							$new_permissions[] = array($context['current_profile'], $group['id'], $data[1], 1);
2556
					}
2557
				}
2558
			}
2559
2560
			// Insert new permissions.
2561
			if (!empty($new_permissions))
2562
				$smcFunc['db_insert']('',
2563
					'{db_prefix}board_permissions',
2564
					array('id_profile' => 'int', 'id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
2565
					$new_permissions,
2566
					array('id_profile', 'id_group', 'permission')
2567
				);
2568
		}
2569
	}
2570
2571
	// Now get all the permissions!
2572
	$request = $smcFunc['db_query']('', '
2573
		SELECT id_group, permission, add_deny
2574
		FROM {db_prefix}board_permissions
2575
		WHERE id_profile = {int:current_profile}
2576
			AND permission IN ({array_string:permissions})
2577
			AND id_group IN ({array_int:profile_group_list})',
2578
		array(
2579
			'profile_group_list' => array_keys($context['profile_groups']),
2580
			'current_profile' => $context['current_profile'],
2581
			'permissions' => $all_permissions,
2582
		)
2583
	);
2584
	while ($row = $smcFunc['db_fetch_assoc']($request))
2585
	{
2586
		foreach ($mappings as $key => $data)
2587
		{
2588
			foreach ($data as $index => $perm)
2589
			{
2590
				if ($perm == $row['permission'])
2591
				{
2592
					// Only bother if it's not denied.
2593
					if ($row['add_deny'])
2594
					{
2595
						// Full allowance?
2596
						if ($index == 0)
2597
							$context['profile_groups'][$row['id_group']][$key] = 'allow';
2598
						// Otherwise only bother with moderate if not on allow.
2599
						elseif ($context['profile_groups'][$row['id_group']][$key] != 'allow')
2600
							$context['profile_groups'][$row['id_group']][$key] = 'moderate';
2601
					}
2602
				}
2603
			}
2604
		}
2605
	}
2606
	$smcFunc['db_free_result']($request);
2607
2608
	createToken('admin-mppm');
2609
}
2610
2611
?>