~azzar1/unity/add-show-desktop-key

443 by dcoles
Added Forum application along with unmodifed version of phpBB3 "Olympus" 3.0.0
1
<?php
2
/**
3
*
4
* @package acp
5
* @version $Id: acp_permissions.php,v 1.65 2007/10/05 14:36:32 acydburn Exp $
6
* @copyright (c) 2005 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8
*
9
*/
10
11
/**
12
* @ignore
13
*/
14
if (!defined('IN_PHPBB'))
15
{
16
	exit;
17
}
18
19
/**
20
* @package acp
21
*/
22
class acp_permissions
23
{
24
	var $u_action;
25
	var $permission_dropdown;
26
	
27
	function main($id, $mode)
28
	{
29
		global $db, $user, $auth, $template, $cache;
30
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
31
32
		include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
33
		include_once($phpbb_root_path . 'includes/acp/auth.' . $phpEx);
34
35
		$auth_admin = new auth_admin();
36
37
		$user->add_lang('acp/permissions');
38
		add_permission_language();
39
40
		$this->tpl_name = 'acp_permissions';
41
42
		// Trace has other vars
43
		if ($mode == 'trace')
44
		{
45
			$user_id = request_var('u', 0);
46
			$forum_id = request_var('f', 0);
47
			$permission = request_var('auth', '');
48
49
			$this->tpl_name = 'permission_trace';
50
51
			if ($user_id && isset($auth_admin->option_ids[$permission]) && $auth->acl_get('a_viewauth'))
52
			{
53
				$this->page_title = sprintf($user->lang['TRACE_PERMISSION'], $user->lang['acl_' . $permission]['lang']);
54
				$this->permission_trace($user_id, $forum_id, $permission);
55
				return;
56
			}
57
			trigger_error('NO_MODE', E_USER_ERROR);
58
		}
59
60
		// Set some vars
61
		$action = request_var('action', array('' => 0));
62
		$action = key($action);
63
		$action = (isset($_POST['psubmit'])) ? 'apply_permissions' : $action;
64
65
		$all_forums = request_var('all_forums', 0);
66
		$subforum_id = request_var('subforum_id', 0);
67
		$forum_id = request_var('forum_id', array(0));
68
69
		$username = request_var('username', array(''), true);
70
		$usernames = request_var('usernames', '', true);
71
		$user_id = request_var('user_id', array(0));
72
73
		$group_id = request_var('group_id', array(0));
74
		$select_all_groups = request_var('select_all_groups', 0);
75
76
		$form_name = 'acp_permissions';
77
		add_form_key($form_name);
78
79
		// If select all groups is set, we pre-build the group id array (this option is used for other screens to link to the permission settings screen)
80
		if ($select_all_groups)
81
		{
82
			// Add default groups to selection
83
			$sql_and = (!$config['coppa_enable']) ? " AND group_name <> 'REGISTERED_COPPA'" : '';
84
85
			$sql = 'SELECT group_id
86
				FROM ' . GROUPS_TABLE . '
87
				WHERE group_type = ' . GROUP_SPECIAL . "
88
				$sql_and";
89
			$result = $db->sql_query($sql);
90
91
			while ($row = $db->sql_fetchrow($result))
92
			{
93
				$group_id[] = $row['group_id'];
94
			}
95
			$db->sql_freeresult($result);
96
		}
97
		
98
		// Map usernames to ids and vice versa
99
		if ($usernames)
100
		{
101
			$username = explode("\n", $usernames);
102
		}
103
		unset($usernames);
104
105
		if (sizeof($username) && !sizeof($user_id))
106
		{
107
			user_get_id_name($user_id, $username);
108
109
			if (!sizeof($user_id))
110
			{
111
				trigger_error($user->lang['SELECTED_USER_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
112
			}
113
		}
114
		unset($username);
115
		
116
		// Build forum ids (of all forums are checked or subforum listing used)
117
		if ($all_forums)
118
		{
119
			$sql = 'SELECT forum_id
120
				FROM ' . FORUMS_TABLE . '
121
				ORDER BY left_id';
122
			$result = $db->sql_query($sql);
123
124
			$forum_id = array();
125
			while ($row = $db->sql_fetchrow($result))
126
			{
127
				$forum_id[] = $row['forum_id'];
128
			}
129
			$db->sql_freeresult($result);
130
		}
131
		else if ($subforum_id)
132
		{
133
			$forum_id = array();
134
			foreach (get_forum_branch($subforum_id, 'children') as $row)
135
			{
136
				$forum_id[] = $row['forum_id'];
137
			}
138
		}
139
140
		// Define some common variables for every mode
141
		$error = array();
142
143
		$permission_scope = (strpos($mode, '_global') !== false) ? 'global' : 'local';
144
145
		// Showing introductionary page?
146
		if ($mode == 'intro')
147
		{
148
			$this->page_title = 'ACP_PERMISSIONS';
149
150
			$template->assign_vars(array(
151
				'S_INTRO'		=> true)
152
			);
153
154
			return;
155
		}
156
157
		switch ($mode)
158
		{
159
			case 'setting_user_global':
160
			case 'setting_group_global':
161
				$this->permission_dropdown = array('u_', 'm_', 'a_');
162
				$permission_victim = ($mode == 'setting_user_global') ? array('user') : array('group');
163
				$this->page_title = ($mode == 'setting_user_global') ? 'ACP_USERS_PERMISSIONS' : 'ACP_GROUPS_PERMISSIONS';
164
			break;
165
166
			case 'setting_user_local':
167
			case 'setting_group_local':
168
				$this->permission_dropdown = array('f_', 'm_');
169
				$permission_victim = ($mode == 'setting_user_local') ? array('user', 'forums') : array('group', 'forums');
170
				$this->page_title = ($mode == 'setting_user_local') ? 'ACP_USERS_FORUM_PERMISSIONS' : 'ACP_GROUPS_FORUM_PERMISSIONS';
171
			break;
172
173
			case 'setting_admin_global':
174
			case 'setting_mod_global':
175
				$this->permission_dropdown = (strpos($mode, '_admin_') !== false) ? array('a_') : array('m_');
176
				$permission_victim = array('usergroup');
177
				$this->page_title = ($mode == 'setting_admin_global') ? 'ACP_ADMINISTRATORS' : 'ACP_GLOBAL_MODERATORS';
178
			break;
179
180
			case 'setting_mod_local':
181
			case 'setting_forum_local':
182
				$this->permission_dropdown = ($mode == 'setting_mod_local') ? array('m_') : array('f_');
183
				$permission_victim = array('forums', 'usergroup');
184
				$this->page_title = ($mode == 'setting_mod_local') ? 'ACP_FORUM_MODERATORS' : 'ACP_FORUM_PERMISSIONS';
185
			break;
186
187
			case 'view_admin_global':
188
			case 'view_user_global':
189
			case 'view_mod_global':
190
				$this->permission_dropdown = ($mode == 'view_admin_global') ? array('a_') : (($mode == 'view_user_global') ? array('u_') : array('m_'));
191
				$permission_victim = array('usergroup_view');
192
				$this->page_title = ($mode == 'view_admin_global') ? 'ACP_VIEW_ADMIN_PERMISSIONS' : (($mode == 'view_user_global') ? 'ACP_VIEW_USER_PERMISSIONS' : 'ACP_VIEW_GLOBAL_MOD_PERMISSIONS');
193
			break;
194
195
			case 'view_mod_local':
196
			case 'view_forum_local':
197
				$this->permission_dropdown = ($mode == 'view_mod_local') ? array('m_') : array('f_');
198
				$permission_victim = array('forums', 'usergroup_view');
199
				$this->page_title = ($mode == 'view_mod_local') ? 'ACP_VIEW_FORUM_MOD_PERMISSIONS' : 'ACP_VIEW_FORUM_PERMISSIONS';
200
			break;
201
202
			default:
203
				trigger_error('NO_MODE', E_USER_ERROR);
204
			break;
205
		}
206
207
		$template->assign_vars(array(
208
			'L_TITLE'		=> $user->lang[$this->page_title],
209
			'L_EXPLAIN'		=> $user->lang[$this->page_title . '_EXPLAIN'])
210
		);
211
212
		// Get permission type
213
		$permission_type = request_var('type', $this->permission_dropdown[0]);
214
215
		if (!in_array($permission_type, $this->permission_dropdown))
216
		{
217
			trigger_error($user->lang['WRONG_PERMISSION_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
218
		}
219
220
221
		// Handle actions
222
		if (strpos($mode, 'setting_') === 0 && $action)
223
		{
224
			switch ($action)
225
			{
226
				case 'delete':
227
228
					if (!check_form_key($form_name))
229
					{
230
						trigger_error($user->lang['FORM_INVALID']. adm_back_link($this->u_action), E_USER_WARNING);
231
					}
232
					// All users/groups selected?
233
					$all_users = (isset($_POST['all_users'])) ? true : false;
234
					$all_groups = (isset($_POST['all_groups'])) ? true : false;
235
236
					if ($all_users || $all_groups)
237
					{
238
						$items = $this->retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type);
239
240
						if ($all_users && sizeof($items['user_ids']))
241
						{
242
							$user_id = $items['user_ids'];
243
						}
244
						else if ($all_groups && sizeof($items['group_ids']))
245
						{
246
							$group_id = $items['group_ids'];
247
						}
248
					}
249
250
					if (sizeof($user_id) || sizeof($group_id))
251
					{
252
						$this->remove_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id, $forum_id);
253
					}
254
					else
255
					{
256
						trigger_error($user->lang['NO_USER_GROUP_SELECTED'] . adm_back_link($this->u_action), E_USER_WARNING);
257
					}
258
				break;
259
260
				case 'apply_permissions':
261
					if (!isset($_POST['setting']))
262
					{
263
						trigger_error($user->lang['NO_AUTH_SETTING_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
264
					}
265
					if (!check_form_key($form_name))
266
					{
267
						trigger_error($user->lang['FORM_INVALID']. adm_back_link($this->u_action), E_USER_WARNING);
268
					}
269
270
					$this->set_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id);
271
				break;
272
273
				case 'apply_all_permissions':
274
					if (!isset($_POST['setting']))
275
					{
276
						trigger_error($user->lang['NO_AUTH_SETTING_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
277
					}
278
					if (!check_form_key($form_name))
279
					{
280
						trigger_error($user->lang['FORM_INVALID']. adm_back_link($this->u_action), E_USER_WARNING);
281
					}
282
283
					$this->set_all_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id);
284
				break;
285
			}
286
		}
287
288
289
		// Setting permissions screen
290
		$s_hidden_fields = build_hidden_fields(array(
291
			'user_id'		=> $user_id,
292
			'group_id'		=> $group_id,
293
			'forum_id'		=> $forum_id,
294
			'type'			=> $permission_type)
295
		);
296
297
		// Go through the screens/options needed and present them in correct order
298
		foreach ($permission_victim as $victim)
299
		{
300
			switch ($victim)
301
			{
302
				case 'forum_dropdown':
303
304
					if (sizeof($forum_id))
305
					{
306
						$this->check_existence('forum', $forum_id);
307
						continue 2;
308
					}
309
310
					$template->assign_vars(array(
311
						'S_SELECT_FORUM'		=> true,
312
						'S_FORUM_OPTIONS'		=> make_forum_select(false, false, true, false, false))
313
					);
314
315
				break;
316
317
				case 'forums':
318
319
					if (sizeof($forum_id))
320
					{
321
						$this->check_existence('forum', $forum_id);
322
						continue 2;
323
					}
324
325
					$forum_list = make_forum_select(false, false, true, false, false, false, true);
326
327
					// Build forum options
328
					$s_forum_options = '';
329
					foreach ($forum_list as $f_id => $f_row)
330
					{
331
						$s_forum_options .= '<option value="' . $f_id . '"' . (($f_row['selected']) ? ' selected="selected"' : '') . (($f_row['disabled']) ? ' disabled="disabled" class="disabled-option"' : '') . '>' . $f_row['padding'] . $f_row['forum_name'] . '</option>';
332
					}
333
334
					// Build subforum options
335
					$s_subforum_options = $this->build_subforum_options($forum_list);
336
337
					$template->assign_vars(array(
338
						'S_SELECT_FORUM'		=> true,
339
						'S_FORUM_OPTIONS'		=> $s_forum_options,
340
						'S_SUBFORUM_OPTIONS'	=> $s_subforum_options,
341
						'S_FORUM_ALL'			=> true,
342
						'S_FORUM_MULTIPLE'		=> true)
343
					);
344
345
				break;
346
347
				case 'user':
348
349
					if (sizeof($user_id))
350
					{
351
						$this->check_existence('user', $user_id);
352
						continue 2;
353
					}
354
355
					$template->assign_vars(array(
356
						'S_SELECT_USER'			=> true,
357
						'U_FIND_USERNAME'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=select_victim&amp;field=username&amp;select_single=true'),
358
					));
359
360
				break;
361
362
				case 'group':
363
364
					if (sizeof($group_id))
365
					{
366
						$this->check_existence('group', $group_id);
367
						continue 2;
368
					}
369
370
					$template->assign_vars(array(
371
						'S_SELECT_GROUP'		=> true,
372
						'S_GROUP_OPTIONS'		=> group_select_options(false, false, (($user->data['user_type'] == USER_FOUNDER) ? false : 0)))
373
					);
374
375
				break;
376
377
				case 'usergroup':
378
				case 'usergroup_view':
379
380
					$all_users = (isset($_POST['all_users'])) ? true : false;
381
					$all_groups = (isset($_POST['all_groups'])) ? true : false;
382
383
					if ((sizeof($user_id) && !$all_users) || (sizeof($group_id) && !$all_groups))
384
					{
385
						if (sizeof($user_id))
386
						{
387
							$this->check_existence('user', $user_id);
388
						}
389
390
						if (sizeof($group_id))
391
						{
392
							$this->check_existence('group', $group_id);
393
						}
394
395
						continue 2;
396
					}
397
398
					// Now we check the users... because the "all"-selection is different here (all defined users/groups)
399
					$items = $this->retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type);
400
401
					if ($all_users && sizeof($items['user_ids']))
402
					{
403
						$user_id = $items['user_ids'];
404
						continue 2;
405
					}
406
407
					if ($all_groups && sizeof($items['group_ids']))
408
					{
409
						$group_id = $items['group_ids'];
410
						continue 2;
411
					}
412
413
					$template->assign_vars(array(
414
						'S_SELECT_USERGROUP'		=> ($victim == 'usergroup') ? true : false,
415
						'S_SELECT_USERGROUP_VIEW'	=> ($victim == 'usergroup_view') ? true : false,
416
						'S_DEFINED_USER_OPTIONS'	=> $items['user_ids_options'],
417
						'S_DEFINED_GROUP_OPTIONS'	=> $items['group_ids_options'],
418
						'S_ADD_GROUP_OPTIONS'		=> group_select_options(false, $items['group_ids'], (($user->data['user_type'] == USER_FOUNDER) ? false : 0)),
419
						'U_FIND_USERNAME'			=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=add_user&amp;field=username&amp;select_single=true'),
420
					));
421
422
				break;
423
			}
424
425
			// The S_ALLOW_SELECT parameter below is a measure to lower memory usage.
426
			// If there are more than 5 forums selected the admin is not able to select all users/groups too.
427
			// We need to see if the number of forums can be increased or need to be decreased.
428
429
			$template->assign_vars(array(
430
				'U_ACTION'				=> $this->u_action,
431
				'ANONYMOUS_USER_ID'		=> ANONYMOUS,
432
433
				'S_SELECT_VICTIM'		=> true,
434
				'S_ALLOW_ALL_SELECT'	=> (sizeof($forum_id) > 5) ? false : true,
435
				'S_CAN_SELECT_USER'		=> ($auth->acl_get('a_authusers')) ? true : false,
436
				'S_CAN_SELECT_GROUP'	=> ($auth->acl_get('a_authgroups')) ? true : false,
437
				'S_HIDDEN_FIELDS'		=> $s_hidden_fields)
438
			);
439
440
			// Let the forum names being displayed
441
			if (sizeof($forum_id))
442
			{
443
				$sql = 'SELECT forum_name
444
					FROM ' . FORUMS_TABLE . '
445
					WHERE ' . $db->sql_in_set('forum_id', $forum_id) . '
446
					ORDER BY left_id ASC';
447
				$result = $db->sql_query($sql);
448
449
				$forum_names = array();
450
				while ($row = $db->sql_fetchrow($result))
451
				{
452
					$forum_names[] = $row['forum_name'];
453
				}
454
				$db->sql_freeresult($result);
455
456
				$template->assign_vars(array(
457
					'S_FORUM_NAMES'		=> (sizeof($forum_names)) ? true : false,
458
					'FORUM_NAMES'		=> implode(', ', $forum_names))
459
				);
460
			}
461
462
			return;
463
		}
464
465
		// Do not allow forum_ids being set and no other setting defined (will bog down the server too much)
466
		if (sizeof($forum_id) && !sizeof($user_id) && !sizeof($group_id))
467
		{
468
			trigger_error($user->lang['ONLY_FORUM_DEFINED'] . adm_back_link($this->u_action), E_USER_WARNING);
469
		}
470
471
		$template->assign_vars(array(
472
			'S_PERMISSION_DROPDOWN'		=> (sizeof($this->permission_dropdown) > 1) ? $this->build_permission_dropdown($this->permission_dropdown, $permission_type, $permission_scope) : false,
473
			'L_PERMISSION_TYPE'			=> $user->lang['ACL_TYPE_' . strtoupper($permission_type)],
474
475
			'U_ACTION'					=> $this->u_action,
476
			'S_HIDDEN_FIELDS'			=> $s_hidden_fields)
477
		);
478
479
		if (strpos($mode, 'setting_') === 0)
480
		{
481
			$template->assign_vars(array(
482
				'S_SETTING_PERMISSIONS'		=> true)
483
			);
484
485
			$hold_ary = $auth_admin->get_mask('set', (sizeof($user_id)) ? $user_id : false, (sizeof($group_id)) ? $group_id : false, (sizeof($forum_id)) ? $forum_id : false, $permission_type, $permission_scope, ACL_NO);
486
			$auth_admin->display_mask('set', $permission_type, $hold_ary, ((sizeof($user_id)) ? 'user' : 'group'), (($permission_scope == 'local') ? true : false));
487
		}
488
		else
489
		{
490
			$template->assign_vars(array(
491
				'S_VIEWING_PERMISSIONS'		=> true)
492
			);
493
494
			$hold_ary = $auth_admin->get_mask('view', (sizeof($user_id)) ? $user_id : false, (sizeof($group_id)) ? $group_id : false, (sizeof($forum_id)) ? $forum_id : false, $permission_type, $permission_scope, ACL_NEVER);
495
			$auth_admin->display_mask('view', $permission_type, $hold_ary, ((sizeof($user_id)) ? 'user' : 'group'), (($permission_scope == 'local') ? true : false));
496
		}
497
	}
498
499
	/**
500
	* Build +subforum options
501
	*/
502
	function build_subforum_options($forum_list)
503
	{
504
		global $user;
505
506
		$s_options = '';
507
508
		$forum_list = array_merge($forum_list);
509
510
		foreach ($forum_list as $key => $row)
511
		{
512
			if ($row['disabled'])
513
			{
514
				continue;
515
			}
516
517
			$s_options .= '<option value="' . $row['forum_id'] . '"' . (($row['selected']) ? ' selected="selected"' : '') . '>' . $row['padding'] . $row['forum_name'];
518
519
			// We check if a branch is there...
520
			$branch_there = false;
521
522
			foreach (array_slice($forum_list, $key + 1) as $temp_row)
523
			{
524
				if ($temp_row['left_id'] > $row['left_id'] && $temp_row['left_id'] < $row['right_id'])
525
				{
526
					$branch_there = true;
527
					break;
528
				}
529
				continue;
530
			}
531
			
532
			if ($branch_there)
533
			{
534
				$s_options .= ' [' . $user->lang['PLUS_SUBFORUMS'] . ']';
535
			}
536
537
			$s_options .= '</option>';
538
		}
539
540
		return $s_options;
541
	}
542
	
543
	/**
544
	* Build dropdown field for changing permission types
545
	*/
546
	function build_permission_dropdown($options, $default_option, $permission_scope)
547
	{
548
		global $user, $auth;
549
		
550
		$s_dropdown_options = '';
551
		foreach ($options as $setting)
552
		{
553
			if (!$auth->acl_get('a_' . str_replace('_', '', $setting) . 'auth'))
554
			{
555
				continue;
556
			}
557
558
			$selected = ($setting == $default_option) ? ' selected="selected"' : '';
559
			$l_setting = (isset($user->lang['permission_type'][$permission_scope][$setting])) ? $user->lang['permission_type'][$permission_scope][$setting] : $user->lang['permission_type'][$setting];
560
			$s_dropdown_options .= '<option value="' . $setting . '"' . $selected . '>' . $l_setting . '</option>';
561
		}
562
563
		return $s_dropdown_options;
564
	}
565
566
	/**
567
	* Check if selected items exist. Remove not found ids and if empty return error.
568
	*/
569
	function check_existence($mode, &$ids)
570
	{
571
		global $db, $user;
572
573
		switch ($mode)
574
		{
575
			case 'user':
576
				$table = USERS_TABLE;
577
				$sql_id = 'user_id';
578
			break;
579
580
			case 'group':
581
				$table = GROUPS_TABLE;
582
				$sql_id = 'group_id';
583
			break;
584
585
			case 'forum':
586
				$table = FORUMS_TABLE;
587
				$sql_id = 'forum_id';
588
			break;
589
		}
590
591
		if (sizeof($ids))
592
		{
593
			$sql = "SELECT $sql_id
594
				FROM $table
595
				WHERE " . $db->sql_in_set($sql_id, $ids);
596
			$result = $db->sql_query($sql);
597
598
			$ids = array();
599
			while ($row = $db->sql_fetchrow($result))
600
			{
601
				$ids[] = $row[$sql_id];
602
			}
603
			$db->sql_freeresult($result);
604
		}
605
606
		if (!sizeof($ids))
607
		{
608
			trigger_error($user->lang['SELECTED_' . strtoupper($mode) . '_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
609
		}
610
	}
611
612
	/**
613
	* Apply permissions
614
	*/
615
	function set_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id)
616
	{
617
		global $user, $auth;
618
619
		$psubmit = request_var('psubmit', array(0 => array(0 => 0)));
620
621
		// User or group to be set?
622
		$ug_type = (sizeof($user_id)) ? 'user' : 'group';
623
624
		// Check the permission setting again
625
		if (!$auth->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !$auth->acl_get('a_auth' . $ug_type . 's'))
626
		{
627
			trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
628
		}
629
		
630
		$ug_id = $forum_id = 0;
631
632
		// We loop through the auth settings defined in our submit
633
		list($ug_id, ) = each($psubmit);
634
		list($forum_id, ) = each($psubmit[$ug_id]);
635
636
		if (empty($_POST['setting']) || empty($_POST['setting'][$ug_id]) || empty($_POST['setting'][$ug_id][$forum_id]) || !is_array($_POST['setting'][$ug_id][$forum_id]))
637
		{
638
			trigger_error('WRONG_PERMISSION_SETTING_FORMAT', E_USER_WARNING);
639
		}
640
641
		// We obtain and check $_POST['setting'][$ug_id][$forum_id] directly and not using request_var() because request_var()
642
		// currently does not support the amount of dimensions required. ;)
643
		//		$auth_settings = request_var('setting', array(0 => array(0 => array('' => 0))));
644
		$auth_settings = array_map('intval', $_POST['setting'][$ug_id][$forum_id]);
645
646
		// Do we have a role we want to set?
647
		$assigned_role = (isset($_POST['role'][$ug_id][$forum_id])) ? (int) $_POST['role'][$ug_id][$forum_id] : 0;
648
649
		// Do the admin want to set these permissions to other items too?
650
		$inherit = request_var('inherit', array(0 => array(0)));
651
652
		$ug_id = array($ug_id);
653
		$forum_id = array($forum_id);
654
655
		if (sizeof($inherit))
656
		{
657
			foreach ($inherit as $_ug_id => $forum_id_ary)
658
			{
659
				// Inherit users/groups?
660
				if (!in_array($_ug_id, $ug_id))
661
				{
662
					$ug_id[] = $_ug_id;
663
				}
664
665
				// Inherit forums?
666
				$forum_id = array_merge($forum_id, array_keys($forum_id_ary));
667
			}
668
		}
669
670
		$forum_id = array_unique($forum_id);
671
672
		// If the auth settings differ from the assigned role, then do not set a role...
673
		if ($assigned_role)
674
		{
675
			if (!$this->check_assigned_role($assigned_role, $auth_settings))
676
			{
677
				$assigned_role = 0;
678
			}
679
		}
680
681
		// Update the permission set...
682
		$auth_admin->acl_set($ug_type, $forum_id, $ug_id, $auth_settings, $assigned_role);
683
684
		// Do we need to recache the moderator lists?
685
		if ($permission_type == 'm_')
686
		{
687
			cache_moderators();
688
		}
689
690
		// Remove users who are now moderators or admins from everyones foes list
691
		if ($permission_type == 'm_' || $permission_type == 'a_')
692
		{
693
			update_foes($group_id, $user_id);
694
		}
695
696
		$this->log_action($mode, 'add', $permission_type, $ug_type, $ug_id, $forum_id);
697
698
		trigger_error($user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action));
699
	}
700
701
	/**
702
	* Apply all permissions
703
	*/
704
	function set_all_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id)
705
	{
706
		global $user, $auth;
707
708
		// User or group to be set?
709
		$ug_type = (sizeof($user_id)) ? 'user' : 'group';
710
711
		// Check the permission setting again
712
		if (!$auth->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !$auth->acl_get('a_auth' . $ug_type . 's'))
713
		{
714
			trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
715
		}
716
717
		$auth_settings = (isset($_POST['setting'])) ? $_POST['setting'] : array();
718
		$auth_roles = (isset($_POST['role'])) ? $_POST['role'] : array();
719
		$ug_ids = $forum_ids = array();
720
721
		// We need to go through the auth settings
722
		foreach ($auth_settings as $ug_id => $forum_auth_row)
723
		{
724
			$ug_id = (int) $ug_id;
725
			$ug_ids[] = $ug_id;
726
727
			foreach ($forum_auth_row as $forum_id => $auth_options)
728
			{
729
				$forum_id = (int) $forum_id;
730
				$forum_ids[] = $forum_id;
731
732
				// Check role...
733
				$assigned_role = (isset($auth_roles[$ug_id][$forum_id])) ? (int) $auth_roles[$ug_id][$forum_id] : 0;
734
735
				// If the auth settings differ from the assigned role, then do not set a role...
736
				if ($assigned_role)
737
				{
738
					if (!$this->check_assigned_role($assigned_role, $auth_options))
739
					{
740
						$assigned_role = 0;
741
					}
742
				}
743
744
				// Update the permission set...
745
				$auth_admin->acl_set($ug_type, $forum_id, $ug_id, $auth_options, $assigned_role, false);
746
			}
747
		}
748
749
		$auth_admin->acl_clear_prefetch();
750
751
		// Do we need to recache the moderator lists?
752
		if ($permission_type == 'm_')
753
		{
754
			cache_moderators();
755
		}
756
757
		// Remove users who are now moderators or admins from everyones foes list
758
		if ($permission_type == 'm_' || $permission_type == 'a_')
759
		{
760
			update_foes($group_id, $user_id);
761
		}
762
763
		$this->log_action($mode, 'add', $permission_type, $ug_type, $ug_ids, $forum_ids);
764
765
		trigger_error($user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action));
766
	}
767
768
	/**
769
	* Compare auth settings with auth settings from role
770
	* returns false if they differ, true if they are equal
771
	*/
772
	function check_assigned_role($role_id, &$auth_settings)
773
	{
774
		global $db;
775
776
		$sql = 'SELECT o.auth_option, r.auth_setting
777
			FROM ' . ACL_OPTIONS_TABLE . ' o, ' . ACL_ROLES_DATA_TABLE . ' r
778
			WHERE o.auth_option_id = r.auth_option_id
779
				AND r.role_id = ' . $role_id;
780
		$result = $db->sql_query($sql);
781
782
		$test_auth_settings = array();
783
		while ($row = $db->sql_fetchrow($result))
784
		{
785
			$test_auth_settings[$row['auth_option']] = $row['auth_setting'];
786
		}
787
		$db->sql_freeresult($result);
788
789
		// We need to add any ACL_NO setting from auth_settings to compare correctly
790
		foreach ($auth_settings as $option => $setting)
791
		{
792
			if ($setting == ACL_NO)
793
			{
794
				$test_auth_settings[$option] = $setting;
795
			}
796
		}
797
798
		if (sizeof(array_diff_assoc($auth_settings, $test_auth_settings)))
799
		{
800
			return false;
801
		}
802
803
		return true;
804
	}
805
806
	/**
807
	* Remove permissions
808
	*/
809
	function remove_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id, &$forum_id)
810
	{
811
		global $user, $db, $auth;
812
			
813
		// User or group to be set?
814
		$ug_type = (sizeof($user_id)) ? 'user' : 'group';
815
816
		// Check the permission setting again
817
		if (!$auth->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !$auth->acl_get('a_auth' . $ug_type . 's'))
818
		{
819
			trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
820
		}
821
822
		$auth_admin->acl_delete($ug_type, (($ug_type == 'user') ? $user_id : $group_id), (sizeof($forum_id) ? $forum_id : false), $permission_type);
823
824
		// Do we need to recache the moderator lists?
825
		if ($permission_type == 'm_')
826
		{
827
			cache_moderators();
828
		}
829
830
		$this->log_action($mode, 'del', $permission_type, $ug_type, (($ug_type == 'user') ? $user_id : $group_id), (sizeof($forum_id) ? $forum_id : array(0 => 0)));
831
832
		trigger_error($user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action));
833
	}
834
835
	/**
836
	* Log permission changes
837
	*/
838
	function log_action($mode, $action, $permission_type, $ug_type, $ug_id, $forum_id)
839
	{
840
		global $db, $user;
841
842
		if (!is_array($ug_id))
843
		{
844
			$ug_id = array($ug_id);
845
		}
846
847
		if (!is_array($forum_id))
848
		{
849
			$forum_id = array($forum_id);
850
		}
851
852
		// Logging ... first grab user or groupnames ...
853
		$sql = ($ug_type == 'group') ? 'SELECT group_name as name, group_type FROM ' . GROUPS_TABLE . ' WHERE ' : 'SELECT username as name FROM ' . USERS_TABLE . ' WHERE ';
854
		$sql .= $db->sql_in_set(($ug_type == 'group') ? 'group_id' : 'user_id', array_map('intval', $ug_id));
855
		$result = $db->sql_query($sql);
856
857
		$l_ug_list = '';
858
		while ($row = $db->sql_fetchrow($result))
859
		{
860
			$l_ug_list .= (($l_ug_list != '') ? ', ' : '') . ((isset($row['group_type']) && $row['group_type'] == GROUP_SPECIAL) ? '<span class="sep">' . $user->lang['G_' . $row['name']] . '</span>' : $row['name']);
861
		}
862
		$db->sql_freeresult($result);
863
864
		$mode = str_replace('setting_', '', $mode);
865
866
		if ($forum_id[0] == 0)
867
		{
868
			add_log('admin', 'LOG_ACL_' . strtoupper($action) . '_' . strtoupper($mode) . '_' . strtoupper($permission_type), $l_ug_list);
869
		}
870
		else
871
		{
872
			// Grab the forum details if non-zero forum_id
873
			$sql = 'SELECT forum_name
874
				FROM ' . FORUMS_TABLE . '
875
				WHERE ' . $db->sql_in_set('forum_id', $forum_id);
876
			$result = $db->sql_query($sql);
877
878
			$l_forum_list = '';
879
			while ($row = $db->sql_fetchrow($result))
880
			{
881
				$l_forum_list .= (($l_forum_list != '') ? ', ' : '') . $row['forum_name'];
882
			}
883
			$db->sql_freeresult($result);
884
885
			add_log('admin', 'LOG_ACL_' . strtoupper($action) . '_' . strtoupper($mode) . '_' . strtoupper($permission_type), $l_forum_list, $l_ug_list);
886
		}
887
	}
888
889
	/**
890
	* Display a complete trace tree for the selected permission to determine where settings are set/unset
891
	*/
892
	function permission_trace($user_id, $forum_id, $permission)
893
	{
894
		global $db, $template, $user, $auth;
895
896
		if ($user_id != $user->data['user_id'])
897
		{
898
			$sql = 'SELECT user_id, username, user_permissions, user_type
899
				FROM ' . USERS_TABLE . '
900
				WHERE user_id = ' . $user_id;
901
			$result = $db->sql_query($sql);
902
			$userdata = $db->sql_fetchrow($result);
903
			$db->sql_freeresult($result);
904
		}
905
		else
906
		{
907
			$userdata = $user->data;
908
		}
909
910
		if (!$userdata)
911
		{
912
			trigger_error('NO_USERS', E_USER_ERROR);
913
		}
914
915
		$forum_name = false;
916
917
		if ($forum_id)
918
		{
919
			$sql = 'SELECT forum_name
920
				FROM ' . FORUMS_TABLE . "
921
				WHERE forum_id = $forum_id";
922
			$result = $db->sql_query($sql, 3600);
923
			$forum_name = $db->sql_fetchfield('forum_name');
924
			$db->sql_freeresult($result);
925
		}
926
927
		$back = request_var('back', 0);
928
929
		$template->assign_vars(array(
930
			'PERMISSION'			=> $user->lang['acl_' . $permission]['lang'],
931
			'PERMISSION_USERNAME'	=> $userdata['username'],
932
			'FORUM_NAME'			=> $forum_name,
933
934
			'S_GLOBAL_TRACE'		=> ($forum_id) ? false : true,
935
936
			'U_BACK'				=> ($back) ? build_url(array('f', 'back')) . "&amp;f=$back" : '')
937
		);
938
939
		$template->assign_block_vars('trace', array(
940
			'WHO'			=> $user->lang['DEFAULT'],
941
			'INFORMATION'	=> $user->lang['TRACE_DEFAULT'],
942
943
			'S_SETTING_NO'		=> true,
944
			'S_TOTAL_NO'		=> true)
945
		);
946
947
		$sql = 'SELECT DISTINCT g.group_name, g.group_id, g.group_type
948
			FROM ' . GROUPS_TABLE . ' g
949
				LEFT JOIN ' . USER_GROUP_TABLE . ' ug ON (ug.group_id = g.group_id)
950
			WHERE ug.user_id = ' . $user_id . '
951
				AND ug.user_pending = 0
952
			ORDER BY g.group_type DESC, g.group_id DESC';
953
		$result = $db->sql_query($sql);
954
955
		$groups = array();
956
		while ($row = $db->sql_fetchrow($result))
957
		{
958
			$groups[$row['group_id']] = array(
959
				'auth_setting'		=> ACL_NO,
960
				'group_name'		=> ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']
961
			);
962
		}
963
		$db->sql_freeresult($result);
964
965
		$total = ACL_NO;
966
		$add_key = (($forum_id) ? '_LOCAL' : '');
967
968
		if (sizeof($groups))
969
		{
970
			// Get group auth settings
971
			$hold_ary = $auth->acl_group_raw_data(array_keys($groups), $permission, $forum_id);
972
973
			foreach ($hold_ary as $group_id => $forum_ary)
974
			{
975
				$groups[$group_id]['auth_setting'] = $hold_ary[$group_id][$forum_id][$permission];
976
			}
977
			unset($hold_ary);
978
979
			foreach ($groups as $id => $row)
980
			{
981
				switch ($row['auth_setting'])
982
				{
983
					case ACL_NO:
984
						$information = $user->lang['TRACE_GROUP_NO' . $add_key];
985
					break;
986
987
					case ACL_YES:
988
						$information = ($total == ACL_YES) ? $user->lang['TRACE_GROUP_YES_TOTAL_YES' . $add_key] : (($total == ACL_NEVER) ? $user->lang['TRACE_GROUP_YES_TOTAL_NEVER' . $add_key] : $user->lang['TRACE_GROUP_YES_TOTAL_NO' . $add_key]);
989
						$total = ($total == ACL_NO) ? ACL_YES : $total;
990
					break;
991
992
					case ACL_NEVER:
993
						$information = ($total == ACL_YES) ? $user->lang['TRACE_GROUP_NEVER_TOTAL_YES' . $add_key] : (($total == ACL_NEVER) ? $user->lang['TRACE_GROUP_NEVER_TOTAL_NEVER' . $add_key] : $user->lang['TRACE_GROUP_NEVER_TOTAL_NO' . $add_key]);
994
						$total = ACL_NEVER;
995
					break;
996
				}
997
998
				$template->assign_block_vars('trace', array(
999
					'WHO'			=> $row['group_name'],
1000
					'INFORMATION'	=> $information,
1001
1002
					'S_SETTING_NO'		=> ($row['auth_setting'] == ACL_NO) ? true : false,
1003
					'S_SETTING_YES'		=> ($row['auth_setting'] == ACL_YES) ? true : false,
1004
					'S_SETTING_NEVER'	=> ($row['auth_setting'] == ACL_NEVER) ? true : false,
1005
					'S_TOTAL_NO'		=> ($total == ACL_NO) ? true : false,
1006
					'S_TOTAL_YES'		=> ($total == ACL_YES) ? true : false,
1007
					'S_TOTAL_NEVER'		=> ($total == ACL_NEVER) ? true : false)
1008
				);
1009
			}
1010
		}
1011
1012
		// Get user specific permission... globally or for this forum
1013
		$hold_ary = $auth->acl_user_raw_data($user_id, $permission, $forum_id);
1014
		$auth_setting = (!sizeof($hold_ary)) ? ACL_NO : $hold_ary[$user_id][$forum_id][$permission];
1015
1016
		switch ($auth_setting)
1017
		{
1018
			case ACL_NO:
1019
				$information = ($total == ACL_NO) ? $user->lang['TRACE_USER_NO_TOTAL_NO' . $add_key] : $user->lang['TRACE_USER_KEPT' . $add_key];
1020
				$total = ($total == ACL_NO) ? ACL_NEVER : $total;
1021
			break;
1022
1023
			case ACL_YES:
1024
				$information = ($total == ACL_YES) ? $user->lang['TRACE_USER_YES_TOTAL_YES' . $add_key] : (($total == ACL_NEVER) ? $user->lang['TRACE_USER_YES_TOTAL_NEVER' . $add_key] : $user->lang['TRACE_USER_YES_TOTAL_NO' . $add_key]);
1025
				$total = ($total == ACL_NO) ? ACL_YES : $total;
1026
			break;
1027
1028
			case ACL_NEVER:
1029
				$information = ($total == ACL_YES) ? $user->lang['TRACE_USER_NEVER_TOTAL_YES' . $add_key] : (($total == ACL_NEVER) ? $user->lang['TRACE_USER_NEVER_TOTAL_NEVER' . $add_key] : $user->lang['TRACE_USER_NEVER_TOTAL_NO' . $add_key]);
1030
				$total = ACL_NEVER;
1031
			break;
1032
		}
1033
1034
		$template->assign_block_vars('trace', array(
1035
			'WHO'			=> $userdata['username'],
1036
			'INFORMATION'	=> $information,
1037
1038
			'S_SETTING_NO'		=> ($auth_setting == ACL_NO) ? true : false,
1039
			'S_SETTING_YES'		=> ($auth_setting == ACL_YES) ? true : false,
1040
			'S_SETTING_NEVER'	=> ($auth_setting == ACL_NEVER) ? true : false,
1041
			'S_TOTAL_NO'		=> false,
1042
			'S_TOTAL_YES'		=> ($total == ACL_YES) ? true : false,
1043
			'S_TOTAL_NEVER'		=> ($total == ACL_NEVER) ? true : false)
1044
		);
1045
1046
		if ($forum_id != 0 && isset($auth->acl_options['global'][$permission]))
1047
		{
1048
			if ($user_id != $user->data['user_id'])
1049
			{
1050
				$auth2 = new auth();
1051
				$auth2->acl($userdata);
1052
				$auth_setting = $auth2->acl_get($permission);
1053
			}
1054
			else
1055
			{
1056
				$auth_setting = $auth->acl_get($permission);
1057
			}
1058
1059
			if ($auth_setting)
1060
			{
1061
				$information = ($total == ACL_YES) ? $user->lang['TRACE_USER_GLOBAL_YES_TOTAL_YES'] : $user->lang['TRACE_USER_GLOBAL_YES_TOTAL_NEVER'];
1062
				$total = ACL_YES;
1063
			}
1064
			else
1065
			{
1066
				$information = $user->lang['TRACE_USER_GLOBAL_NEVER_TOTAL_KEPT'];
1067
			}
1068
1069
			// If there is no auth information we do not need to worry the user by showing non-relevant data.
1070
			if ($auth_setting)
1071
			{
1072
				$template->assign_block_vars('trace', array(
1073
					'WHO'			=> sprintf($user->lang['TRACE_GLOBAL_SETTING'], $userdata['username']),
1074
					'INFORMATION'	=> sprintf($information, '<a href="' . $this->u_action . "&amp;u=$user_id&amp;f=0&amp;auth=$permission&amp;back=$forum_id\">", '</a>'),
1075
1076
					'S_SETTING_NO'		=> false,
1077
					'S_SETTING_YES'		=> $auth_setting,
1078
					'S_SETTING_NEVER'	=> !$auth_setting,
1079
					'S_TOTAL_NO'		=> false,
1080
					'S_TOTAL_YES'		=> ($total == ACL_YES) ? true : false,
1081
					'S_TOTAL_NEVER'		=> ($total == ACL_NEVER) ? true : false)
1082
				);
1083
			}
1084
		}
1085
1086
		// Take founder status into account, overwriting the default values
1087
		if ($userdata['user_type'] == USER_FOUNDER && strpos($permission, 'a_') === 0)
1088
		{
1089
			$template->assign_block_vars('trace', array(
1090
				'WHO'			=> $userdata['username'],
1091
				'INFORMATION'	=> $user->lang['TRACE_USER_FOUNDER'],
1092
1093
				'S_SETTING_NO'		=> ($auth_setting == ACL_NO) ? true : false,
1094
				'S_SETTING_YES'		=> ($auth_setting == ACL_YES) ? true : false,
1095
				'S_SETTING_NEVER'	=> ($auth_setting == ACL_NEVER) ? true : false,
1096
				'S_TOTAL_NO'		=> false,
1097
				'S_TOTAL_YES'		=> true,
1098
				'S_TOTAL_NEVER'		=> false)
1099
			);
1100
1101
			$total = ACL_YES;
1102
		}
1103
1104
		// Total value...
1105
		$template->assign_vars(array(
1106
			'S_RESULT_NO'		=> ($total == ACL_NO) ? true : false,
1107
			'S_RESULT_YES'		=> ($total == ACL_YES) ? true : false,
1108
			'S_RESULT_NEVER'	=> ($total == ACL_NEVER) ? true : false,
1109
		));
1110
	}
1111
1112
	/**
1113
	* Get already assigned users/groups
1114
	*/
1115
	function retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type)
1116
	{
1117
		global $db, $user;
1118
1119
		$sql_forum_id = ($permission_scope == 'global') ? 'AND a.forum_id = 0' : ((sizeof($forum_id)) ? 'AND ' . $db->sql_in_set('a.forum_id', $forum_id) : 'AND a.forum_id <> 0');
1120
		$sql_permission_option = ' AND o.auth_option ' . $db->sql_like_expression($permission_type . $db->any_char);
1121
		
1122
		$sql = $db->sql_build_query('SELECT_DISTINCT', array(
1123
			'SELECT'	=> 'u.username, u.username_clean, u.user_regdate, u.user_id',
1124
1125
			'FROM'		=> array(
1126
				USERS_TABLE			=> 'u',
1127
				ACL_OPTIONS_TABLE	=> 'o',
1128
				ACL_USERS_TABLE		=> 'a'
1129
			),
1130
1131
			'LEFT_JOIN'	=> array(
1132
				array(
1133
					'FROM'	=> array(ACL_ROLES_DATA_TABLE => 'r'),
1134
					'ON'	=> 'a.auth_role_id = r.role_id'
1135
				)
1136
			),
1137
1138
			'WHERE'		=> "(a.auth_option_id = o.auth_option_id OR r.auth_option_id = o.auth_option_id)
1139
				$sql_permission_option
1140
				$sql_forum_id
1141
				AND u.user_id = a.user_id",
1142
1143
			'ORDER_BY'	=> 'u.username_clean, u.user_regdate ASC'
1144
		));
1145
		$result = $db->sql_query($sql);
1146
1147
		$s_defined_user_options = '';
1148
		$defined_user_ids = array();
1149
		while ($row = $db->sql_fetchrow($result))
1150
		{
1151
			$s_defined_user_options .= '<option value="' . $row['user_id'] . '">' . $row['username'] . '</option>';
1152
			$defined_user_ids[] = $row['user_id'];
1153
		}
1154
		$db->sql_freeresult($result);
1155
1156
		$sql = $db->sql_build_query('SELECT_DISTINCT', array(
1157
			'SELECT'	=> 'g.group_type, g.group_name, g.group_id',
1158
1159
			'FROM'		=> array(
1160
				GROUPS_TABLE		=> 'g',
1161
				ACL_OPTIONS_TABLE	=> 'o',
1162
				ACL_GROUPS_TABLE	=> 'a'
1163
			),
1164
1165
			'LEFT_JOIN'	=> array(
1166
				array(
1167
					'FROM'	=> array(ACL_ROLES_DATA_TABLE => 'r'),
1168
					'ON'	=> 'a.auth_role_id = r.role_id'
1169
				)
1170
			),
1171
1172
			'WHERE'		=> "(a.auth_option_id = o.auth_option_id OR r.auth_option_id = o.auth_option_id)
1173
				$sql_permission_option
1174
				$sql_forum_id
1175
				AND g.group_id = a.group_id",
1176
1177
			'ORDER_BY'	=> 'g.group_type DESC, g.group_name ASC'
1178
		));
1179
		$result = $db->sql_query($sql);
1180
1181
		$s_defined_group_options = '';
1182
		$defined_group_ids = array();
1183
		while ($row = $db->sql_fetchrow($result))
1184
		{
1185
			$s_defined_group_options .= '<option' . (($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '') . ' value="' . $row['group_id'] . '">' . (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']) . '</option>';
1186
			$defined_group_ids[] = $row['group_id'];
1187
		}
1188
		$db->sql_freeresult($result);
1189
1190
		return array(
1191
			'group_ids'			=> $defined_group_ids,
1192
			'group_ids_options'	=> $s_defined_group_options,
1193
			'user_ids'			=> $defined_user_ids,
1194
			'user_ids_options'	=> $s_defined_user_options
1195
		);
1196
	}
1197
}
1198
1199
?>