Issues (1065)

Sources/Reports.php (4 issues)

1
<?php
2
3
/**
4
 * This file is exclusively for generating reports to help assist forum
5
 * administrators keep track of their forum configuration and state. The
6
 * core report generation is done in two areas. Firstly, a report "generator"
7
 * will fill context with relevant data. Secondly, the choice of sub-template
8
 * will determine how this data is shown to the user
9
 *
10
 * Functions ending with "Report" are responsible for generating data for reporting.
11
 * They are all called from ReportsMain.
12
 * Never access the context directly, but use the data handling functions to do so.
13
 *
14
 * Simple Machines Forum (SMF)
15
 *
16
 * @package SMF
17
 * @author Simple Machines https://www.simplemachines.org
18
 * @copyright 2025 Simple Machines and individual contributors
19
 * @license https://www.simplemachines.org/about/smf/license.php BSD
20
 *
21
 * @version 2.1.5
22
 */
23
24
if (!defined('SMF'))
25
	die('No direct access...');
26
27
/**
28
 * Handling function for generating reports.
29
 * Requires the admin_forum permission.
30
 * Loads the Reports template and language files.
31
 * Decides which type of report to generate, if this isn't passed
32
 * through the querystring it will set the report_type sub-template to
33
 * force the user to choose which type.
34
 * When generating a report chooses which sub_template to use.
35
 * Depends on the cal_enabled setting, and many of the other cal_
36
 * settings.
37
 * Will call the relevant report generation function.
38
 * If generating report will call finishTables before returning.
39
 * Accessed through ?action=admin;area=reports.
40
 */
41
function ReportsMain()
42
{
43
	global $txt, $context, $scripturl;
44
45
	// Only admins, only EVER admins!
46
	isAllowedTo('admin_forum');
47
48
	// Let's get our things running...
49
	loadTemplate('Reports');
50
	loadLanguage('Reports');
51
52
	$context['page_title'] = $txt['generate_reports'];
53
54
	// These are the types of reports which exist - and the functions to generate them.
55
	$context['report_types'] = array(
56
		'boards' => 'BoardReport',
57
		'board_perms' => 'BoardPermissionsReport',
58
		'member_groups' => 'MemberGroupsReport',
59
		'group_perms' => 'GroupPermissionsReport',
60
		'staff' => 'StaffReport',
61
	);
62
63
	call_integration_hook('integrate_report_types');
64
	// Load up all the tabs...
65
	$context[$context['admin_menu_name']]['tab_data'] = array(
66
		'title' => $txt['generate_reports'],
67
		'help' => '',
68
		'description' => $txt['generate_reports_desc'],
69
	);
70
71
	$is_first = 0;
72
	foreach ($context['report_types'] as $k => $temp)
73
		$context['report_types'][$k] = array(
74
			'id' => $k,
75
			'title' => isset($txt['gr_type_' . $k]) ? $txt['gr_type_' . $k] : $k,
76
			'description' => isset($txt['gr_type_desc_' . $k]) ? $txt['gr_type_desc_' . $k] : null,
77
			'function' => $temp,
78
			'is_first' => $is_first++ == 0,
79
		);
80
81
	// If they haven't chosen a report type which is valid, send them off to the report type chooser!
82
	if (empty($_REQUEST['rt']) || !isset($context['report_types'][$_REQUEST['rt']]))
83
	{
84
		$context['sub_template'] = 'report_type';
85
		return;
86
	}
87
	$context['report_type'] = $_REQUEST['rt'];
88
89
	// What are valid templates for showing reports?
90
	$reportTemplates = array(
91
		'main' => array(
92
			'layers' => null,
93
		),
94
		'print' => array(
95
			'layers' => array('print'),
96
		),
97
	);
98
99
	// Specific template? Use that instead of main!
100
	if (isset($_REQUEST['st']) && isset($reportTemplates[$_REQUEST['st']]))
101
	{
102
		$context['sub_template'] = $_REQUEST['st'];
103
104
		// Are we disabling the other layers - print friendly for example?
105
		if ($reportTemplates[$_REQUEST['st']]['layers'] !== null)
106
			$context['template_layers'] = $reportTemplates[$_REQUEST['st']]['layers'];
107
	}
108
109
	// Make the page title more descriptive.
110
	$context['page_title'] .= ' - ' . (isset($txt['gr_type_' . $context['report_type']]) ? $txt['gr_type_' . $context['report_type']] : $context['report_type']);
111
112
	// Build the reports button array.
113
	$context['report_buttons'] = array(
114
		'generate_reports' => array('text' => 'generate_reports', 'image' => 'print.png', 'url' => $scripturl . '?action=admin;area=reports', 'active' => true),
115
		'print' => array('text' => 'print', 'image' => 'print.png', 'url' => $scripturl . '?action=admin;area=reports;rt=' . $context['report_type'] . ';st=print', 'custom' => 'target="_blank"'),
116
	);
117
118
	// Allow mods to add additional buttons here
119
	call_integration_hook('integrate_report_buttons');
120
121
	// Now generate the data.
122
	$context['report_types'][$context['report_type']]['function']();
123
124
	// Finish the tables before exiting - this is to help the templates a little more.
125
	finishTables();
126
}
127
128
/**
129
 * Standard report about what settings the boards have.
130
 * functions ending with "Report" are responsible for generating data
131
 * for reporting.
132
 * they are all called from ReportsMain.
133
 * never access the context directly, but use the data handling
134
 * functions to do so.
135
 */
136
function BoardReport()
137
{
138
	global $context, $txt, $sourcedir, $smcFunc, $modSettings;
139
140
	// Load the permission profiles.
141
	require_once($sourcedir . '/ManagePermissions.php');
142
	loadLanguage('ManagePermissions');
143
	loadPermissionProfiles();
144
145
	// Get every moderator.
146
	$request = $smcFunc['db_query']('', '
147
		SELECT mods.id_board, mods.id_member, mem.real_name
148
		FROM {db_prefix}moderators AS mods
149
			INNER JOIN {db_prefix}members AS mem ON (mem.id_member = mods.id_member)',
150
		array(
151
		)
152
	);
153
	$moderators = array();
154
	while ($row = $smcFunc['db_fetch_assoc']($request))
155
		$moderators[$row['id_board']][] = $row['real_name'];
156
	$smcFunc['db_free_result']($request);
157
158
	// Get every moderator gruop.
159
	$request = $smcFunc['db_query']('', '
160
		SELECT modgs.id_board, modgs.id_group, memg.group_name
161
		FROM {db_prefix}moderator_groups AS modgs
162
			INNER JOIN {db_prefix}membergroups AS memg ON (memg.id_group = modgs.id_group)',
163
		array(
164
		)
165
	);
166
	$moderator_groups = array();
167
	while ($row = $smcFunc['db_fetch_assoc']($request))
168
		$moderator_groups[$row['id_board']][] = $row['group_name'];
169
	$smcFunc['db_free_result']($request);
170
171
	// Get all the possible membergroups!
172
	$request = $smcFunc['db_query']('', '
173
		SELECT id_group, group_name, online_color
174
		FROM {db_prefix}membergroups',
175
		array(
176
		)
177
	);
178
	$groups = array(-1 => $txt['guest_title'], 0 => $txt['membergroups_members']);
179
	while ($row = $smcFunc['db_fetch_assoc']($request))
180
		$groups[$row['id_group']] = empty($row['online_color']) ? $row['group_name'] : '<span style="color: ' . $row['online_color'] . '">' . $row['group_name'] . '</span>';
181
	$smcFunc['db_free_result']($request);
182
183
	// All the fields we'll show.
184
	$boardSettings = array(
185
		'category' => $txt['board_category'],
186
		'parent' => $txt['board_parent'],
187
		'redirect' => $txt['board_redirect'],
188
		'num_topics' => $txt['board_num_topics'],
189
		'num_posts' => $txt['board_num_posts'],
190
		'count_posts' => $txt['board_count_posts'],
191
		'theme' => $txt['board_theme'],
192
		'override_theme' => $txt['board_override_theme'],
193
		'profile' => $txt['board_profile'],
194
		'moderators' => $txt['board_moderators'],
195
		'moderator_groups' => $txt['board_moderator_groups'],
196
		'groups' => $txt['board_groups'],
197
	);
198
	if (!empty($modSettings['deny_boards_access']))
199
		$boardSettings['disallowed_groups'] = $txt['board_disallowed_groups'];
200
201
	// Do it in columns, it's just easier.
202
	setKeys('cols');
203
204
	// Go through each board!
205
	$request = $smcFunc['db_query']('order_by_board_order', '
206
		SELECT b.id_board, b.name, b.num_posts, b.num_topics, b.count_posts, b.member_groups, b.override_theme, b.id_profile, b.deny_member_groups,
207
			b.redirect, c.name AS cat_name, COALESCE(par.name, {string:text_none}) AS parent_name, COALESCE(th.value, {string:text_none}) AS theme_name
208
		FROM {db_prefix}boards AS b
209
			LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
210
			LEFT JOIN {db_prefix}boards AS par ON (par.id_board = b.id_parent)
211
			LEFT JOIN {db_prefix}themes AS th ON (th.id_theme = b.id_theme AND th.variable = {string:name})
212
		ORDER BY b.board_order',
213
		array(
214
			'name' => 'name',
215
			'text_none' => $txt['none'],
216
		)
217
	);
218
219
	while ($row = $smcFunc['db_fetch_assoc']($request))
220
	{
221
		// Each board has it's own table.
222
		newTable($row['name'], '', 'left', 'auto', 'left', 200, 'left');
223
224
		$this_boardSettings = $boardSettings;
225
		if (empty($row['redirect']))
226
			unset($this_boardSettings['redirect']);
227
228
		// First off, add in the side key.
229
		addData($this_boardSettings);
230
231
		// Format the profile name.
232
		$profile_name = $context['profiles'][$row['id_profile']]['name'];
233
234
		// Create the main data array.
235
		$boardData = array(
236
			'category' => $row['cat_name'],
237
			'parent' => $row['parent_name'],
238
			'redirect' => $row['redirect'],
239
			'num_posts' => $row['num_posts'],
240
			'num_topics' => $row['num_topics'],
241
			'count_posts' => empty($row['count_posts']) ? $txt['yes'] : $txt['no'],
242
			'theme' => $row['theme_name'],
243
			'profile' => $profile_name,
244
			'override_theme' => $row['override_theme'] ? $txt['yes'] : $txt['no'],
245
			'moderators' => empty($moderators[$row['id_board']]) ? $txt['none'] : implode(', ', $moderators[$row['id_board']]),
246
			'moderator_groups' => empty($moderator_groups[$row['id_board']]) ? $txt['none'] : implode(', ', $moderator_groups[$row['id_board']]),
247
		);
248
249
		// Work out the membergroups who can and cannot access it (but only if enabled).
250
		$allowedGroups = explode(',', $row['member_groups']);
251
		foreach ($allowedGroups as $key => $group)
252
		{
253
			if (isset($groups[$group]))
254
				$allowedGroups[$key] = $groups[$group];
255
			else
256
				unset($allowedGroups[$key]);
257
		}
258
		$boardData['groups'] = implode(', ', $allowedGroups);
259
		if (!empty($modSettings['deny_boards_access']))
260
		{
261
			$disallowedGroups = explode(',', $row['deny_member_groups']);
262
			foreach ($disallowedGroups as $key => $group)
263
			{
264
				if (isset($groups[$group]))
265
					$disallowedGroups[$key] = $groups[$group];
266
				else
267
					unset($disallowedGroups[$key]);
268
			}
269
			$boardData['disallowed_groups'] = implode(', ', $disallowedGroups);
270
		}
271
272
		if (empty($row['redirect']))
273
			unset ($boardData['redirect']);
274
275
		// Next add the main data.
276
		addData($boardData);
277
	}
278
	$smcFunc['db_free_result']($request);
279
}
280
281
/**
282
 * Generate a report on the current permissions by board and membergroup.
283
 * functions ending with "Report" are responsible for generating data
284
 * for reporting.
285
 * they are all called from ReportsMain.
286
 * never access the context directly, but use the data handling
287
 * functions to do so.
288
 */
289
function BoardPermissionsReport()
290
{
291
	global $txt, $modSettings, $smcFunc;
292
293
	// Get as much memory as possible as this can be big.
294
	setMemoryLimit('256M');
295
296
	if (isset($_REQUEST['boards']))
297
	{
298
		if (!is_array($_REQUEST['boards']))
299
			$_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
300
		foreach ($_REQUEST['boards'] as $k => $dummy)
301
			$_REQUEST['boards'][$k] = (int) $dummy;
302
303
		$board_clause = 'id_board IN ({array_int:boards})';
304
	}
305
	else
306
		$board_clause = '1=1';
307
308
	if (isset($_REQUEST['groups']))
309
	{
310
		if (!is_array($_REQUEST['groups']))
311
			$_REQUEST['groups'] = explode(',', $_REQUEST['groups']);
312
		foreach ($_REQUEST['groups'] as $k => $dummy)
313
			$_REQUEST['groups'][$k] = (int) $dummy;
314
315
		$group_clause = 'id_group IN ({array_int:groups})';
316
	}
317
	else
318
		$group_clause = '1=1';
319
320
	$request = $smcFunc['db_query']('', '
321
		SELECT id_profile, profile_name
322
		FROM {db_prefix}permission_profiles');
323
	$board_perms_names = array();
324
	while ($row = $smcFunc['db_fetch_assoc']($request))
325
		$board_perms_names[$row['id_profile']] = $row['profile_name'];
326
	$smcFunc['db_free_result']($request);
327
328
	$request = $smcFunc['db_query']('', '
329
		SELECT id_board, name, id_profile
330
		FROM {db_prefix}boards
331
		WHERE ' . $board_clause . '
332
		ORDER BY id_board',
333
		array(
334
			'boards' => isset($_REQUEST['boards']) ? $_REQUEST['boards'] : array(),
335
		)
336
	);
337
	$profiles = array();
338
	while ($row = $smcFunc['db_fetch_assoc']($request))
339
	{
340
		$boards[$row['id_board']] = array(
341
			'name' => $row['name'],
342
			'profile' => $row['id_profile'],
343
		);
344
		$profiles[] = $row['id_profile'];
345
	}
346
	$smcFunc['db_free_result']($request);
347
348
	$request = $smcFunc['db_query']('', '
349
		SELECT id_group, group_name
350
		FROM {db_prefix}membergroups
351
		WHERE ' . $group_clause . '
352
			AND id_group != {int:admin_group}' . (empty($modSettings['permission_enable_postgroups']) ? '
353
			AND min_posts = {int:min_posts}' : '') . '
354
		ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
355
		array(
356
			'admin_group' => 1,
357
			'min_posts' => -1,
358
			'newbie_group' => 4,
359
			'groups' => isset($_REQUEST['groups']) ? $_REQUEST['groups'] : array(),
360
		)
361
	);
362
	if (!isset($_REQUEST['groups']) || in_array(-1, $_REQUEST['groups']) || in_array(0, $_REQUEST['groups']))
363
		$member_groups = array('col' => '', -1 => $txt['membergroups_guests'], 0 => $txt['membergroups_members']);
364
	else
365
		$member_groups = array('col' => '');
366
	while ($row = $smcFunc['db_fetch_assoc']($request))
367
		$member_groups[$row['id_group']] = $row['group_name'];
368
	$smcFunc['db_free_result']($request);
369
370
	// Make sure that every group is represented - plus in rows!
371
	setKeys('rows', $member_groups);
372
373
	// Certain permissions should not really be shown.
374
	$disabled_permissions = array();
375
	if (!$modSettings['postmod_active'])
376
	{
377
		$disabled_permissions[] = 'approve_posts';
378
		$disabled_permissions[] = 'post_unapproved_topics';
379
		$disabled_permissions[] = 'post_unapproved_replies_own';
380
		$disabled_permissions[] = 'post_unapproved_replies_any';
381
		$disabled_permissions[] = 'post_unapproved_attachments';
382
	}
383
384
	call_integration_hook('integrate_reports_boardperm', array(&$disabled_permissions));
385
386
	// Cache every permission setting, to make sure we don't miss any allows.
387
	$permissions = array();
388
	$board_permissions = array();
389
	$request = $smcFunc['db_query']('', '
390
		SELECT id_profile, id_group, add_deny, permission
391
		FROM {db_prefix}board_permissions
392
		WHERE id_profile IN ({array_int:profile_list})
393
			AND ' . $group_clause . (empty($modSettings['permission_enable_deny']) ? '
394
			AND add_deny = {int:not_deny}' : '') . '
395
		ORDER BY id_profile, permission',
396
		array(
397
			'profile_list' => $profiles,
398
			'not_deny' => 1,
399
			'groups' => isset($_REQUEST['groups']) ? $_REQUEST['groups'] : array(),
400
		)
401
	);
402
	while ($row = $smcFunc['db_fetch_assoc']($request))
403
	{
404
		if (in_array($row['permission'], $disabled_permissions))
405
			continue;
406
407
		$board_permissions[$row['id_profile']][$row['id_group']][$row['permission']] = $row['add_deny'];
408
409
		// Make sure we get every permission.
410
		if (!isset($permissions[$row['permission']]))
411
		{
412
			// This will be reused on other boards.
413
			$permissions[$row['permission']] = array(
414
				'title' => isset($txt['board_perms_name_' . $row['permission']]) ? $txt['board_perms_name_' . $row['permission']] : $row['permission'],
415
			);
416
		}
417
	}
418
	$smcFunc['db_free_result']($request);
419
420
	$board_names = array_reduce(
421
		$boards,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $boards does not seem to be defined for all execution paths leading up to this point.
Loading history...
422
		function (array $accumulator, array $board)
423
		{
424
			$accumulator[$board['profile']][] = $board['name'];
425
426
			return $accumulator;
427
		},
428
		[]
429
	);
430
431
	// Now cycle through the board permissions array... lots to do ;)
432
	foreach ($board_permissions as $id_profile => $groups)
433
	{
434
		// Create the table for this board first.
435
		newTable($board_perms_names[$id_profile] . ' (' . implode(', ', $board_names[$id_profile]) . ')', 'x', 'all', 100, 'center', 200, 'left');
436
437
		// Add the header row - shows all the membergroups.
438
		addData($member_groups);
439
440
		// Add the separator.
441
		addSeparator($txt['board_perms_permission']);
442
443
		// Here cycle through all the detected permissions.
444
		foreach ($permissions as $ID_PERM => $perm_info)
445
		{
446
			// Default data for this row.
447
			$curData = array('col' => $perm_info['title']);
448
449
			// Now cycle each membergroup in this set of permissions.
450
			foreach ($member_groups as $id_group => $name)
451
			{
452
				// Don't overwrite the key column!
453
				if ($id_group === 'col')
454
					continue;
455
456
				$group_permissions = isset($groups[$id_group]) ? $groups[$id_group] : array();
457
458
				// Do we have any data for this group?
459
				if (isset($group_permissions[$ID_PERM]))
460
				{
461
					// Set the data for this group to be the local permission.
462
					$curData[$id_group] = $group_permissions[$ID_PERM];
463
				}
464
				// Otherwise means it's set to disallow..
465
				else
466
				{
467
					$curData[$id_group] = 'x';
468
				}
469
470
				// Now actually make the data for the group look right.
471
				if (empty($curData[$id_group]))
472
					$curData[$id_group] = '<span class="red">' . $txt['board_perms_deny'] . '</span>';
473
				elseif ($curData[$id_group] == 1)
474
					$curData[$id_group] = '<span style="color: darkgreen;">' . $txt['board_perms_allow'] . '</span>';
475
				else
476
					$curData[$id_group] = 'x';
477
478
				// Embolden those permissions different from global (makes it a lot easier!)
479
				if (@$board_permissions[0][$id_group][$ID_PERM] != @$group_permissions[$ID_PERM])
480
					$curData[$id_group] = '<strong>' . $curData[$id_group] . '</strong>';
481
			}
482
483
			// Now add the data for this permission.
484
			addData($curData);
485
		}
486
	}
487
}
488
489
/**
490
 * Show what the membergroups are made of.
491
 * functions ending with "Report" are responsible for generating data
492
 * for reporting.
493
 * they are all called from ReportsMain.
494
 * never access the context directly, but use the data handling
495
 * functions to do so.
496
 */
497
function MemberGroupsReport()
498
{
499
	global $txt, $settings, $modSettings, $smcFunc;
500
501
	// Fetch all the board names.
502
	$request = $smcFunc['db_query']('', '
503
		SELECT id_board, name, member_groups, id_profile, deny_member_groups
504
		FROM {db_prefix}boards',
505
		array(
506
		)
507
	);
508
	while ($row = $smcFunc['db_fetch_assoc']($request))
509
	{
510
		if (trim($row['member_groups']) == '')
511
			$groups = array(1);
512
		else
513
			$groups = array_merge(array(1), explode(',', $row['member_groups']));
514
515
		if (trim($row['deny_member_groups']) == '')
516
			$denyGroups = array();
517
		else
518
			$denyGroups = explode(',', $row['deny_member_groups']);
519
520
		$boards[$row['id_board']] = array(
521
			'id' => $row['id_board'],
522
			'name' => $row['name'],
523
			'profile' => $row['id_profile'],
524
			'groups' => $groups,
525
			'deny_groups' => $denyGroups,
526
		);
527
	}
528
	$smcFunc['db_free_result']($request);
529
530
	// Standard settings.
531
	$mgSettings = array(
532
		'name' => '',
533
		'#sep#1' => $txt['member_group_settings'],
534
		'color' => $txt['member_group_color'],
535
		'min_posts' => $txt['member_group_min_posts'],
536
		'max_messages' => $txt['member_group_max_messages'],
537
		'icons' => $txt['member_group_icons'],
538
		'#sep#2' => $txt['member_group_access'],
539
	);
540
541
	// Add on the boards!
542
	foreach ($boards as $board)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $boards does not seem to be defined for all execution paths leading up to this point.
Loading history...
543
		$mgSettings['board_' . $board['id']] = $board['name'];
544
545
	// Add all the membergroup settings, plus we'll be adding in columns!
546
	setKeys('cols', $mgSettings);
547
548
	// Only one table this time!
549
	newTable($txt['gr_type_member_groups'], '-', 'all', 100, 'center', 200, 'left');
550
551
	// Get the shaded column in.
552
	addData($mgSettings);
553
554
	// Now start cycling the membergroups!
555
	$request = $smcFunc['db_query']('', '
556
		SELECT mg.id_group, mg.group_name, mg.online_color, mg.min_posts, mg.max_messages, mg.icons,
557
			CASE WHEN bp.permission IS NOT NULL OR mg.id_group = {int:admin_group} THEN 1 ELSE 0 END AS can_moderate
558
		FROM {db_prefix}membergroups AS mg
559
			LEFT JOIN {db_prefix}board_permissions AS bp ON (bp.id_group = mg.id_group AND bp.id_profile = {int:default_profile} AND bp.permission = {string:moderate_board})
560
		ORDER BY mg.min_posts, CASE WHEN mg.id_group < {int:newbie_group} THEN mg.id_group ELSE 4 END, mg.group_name',
561
		array(
562
			'admin_group' => 1,
563
			'default_profile' => 1,
564
			'newbie_group' => 4,
565
			'moderate_board' => 'moderate_board',
566
		)
567
	);
568
569
	// Cache them so we get regular members too.
570
	$rows = array(
571
		array(
572
			'id_group' => -1,
573
			'group_name' => $txt['membergroups_guests'],
574
			'online_color' => '',
575
			'min_posts' => -1,
576
			'max_messages' => null,
577
			'icons' => ''
578
		),
579
		array(
580
			'id_group' => 0,
581
			'group_name' => $txt['membergroups_members'],
582
			'online_color' => '',
583
			'min_posts' => -1,
584
			'max_messages' => null,
585
			'icons' => ''
586
		),
587
	);
588
	while ($row = $smcFunc['db_fetch_assoc']($request))
589
		$rows[] = $row;
590
	$smcFunc['db_free_result']($request);
591
592
	foreach ($rows as $row)
593
	{
594
		$row['icons'] = explode('#', $row['icons']);
595
596
		$group = array(
597
			'name' => $row['group_name'],
598
			'color' => empty($row['online_color']) ? '-' : '<span style="color: ' . $row['online_color'] . ';">' . $row['online_color'] . '</span>',
599
			'min_posts' => $row['min_posts'] == -1 ? 'N/A' : $row['min_posts'],
600
			'max_messages' => $row['max_messages'],
601
			'icons' => !empty($row['icons'][0]) && !empty($row['icons'][1]) ? str_repeat('<img src="' . $settings['images_url'] . '/membericons/' . $row['icons'][1] . '" alt="*">', $row['icons'][0]) : '',
602
		);
603
604
		// Board permissions.
605
		foreach ($boards as $board)
606
			$group['board_' . $board['id']] = in_array($row['id_group'], $board['groups']) ? '<span class="success">' . $txt['board_perms_allow'] . '</span>' : (!empty($modSettings['deny_boards_access']) && in_array($row['id_group'], $board['deny_groups']) ? '<span class="alert">' . $txt['board_perms_deny'] . '</span>' : 'x');
607
608
		addData($group);
609
	}
610
}
611
612
/**
613
 * Show the large variety of group permissions assigned to each membergroup.
614
 * functions ending with "Report" are responsible for generating data
615
 * for reporting.
616
 * they are all called from ReportsMain.
617
 * never access the context directly, but use the data handling
618
 * functions to do so.
619
 */
620
function GroupPermissionsReport()
621
{
622
	global $txt, $modSettings, $smcFunc;
623
624
	if (isset($_REQUEST['groups']))
625
	{
626
		if (!is_array($_REQUEST['groups']))
627
			$_REQUEST['groups'] = explode(',', $_REQUEST['groups']);
628
		foreach ($_REQUEST['groups'] as $k => $dummy)
629
			$_REQUEST['groups'][$k] = (int) $dummy;
630
		$_REQUEST['groups'] = array_diff($_REQUEST['groups'], array(3));
631
632
		$clause = 'id_group IN ({array_int:groups})';
633
	}
634
	else
635
		$clause = 'id_group != {int:moderator_group}';
636
637
	// Get all the possible membergroups, except admin!
638
	$request = $smcFunc['db_query']('', '
639
		SELECT id_group, group_name
640
		FROM {db_prefix}membergroups
641
		WHERE ' . $clause . '
642
			AND id_group != {int:admin_group}' . (empty($modSettings['permission_enable_postgroups']) ? '
643
			AND min_posts = {int:min_posts}' : '') . '
644
		ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
645
		array(
646
			'admin_group' => 1,
647
			'min_posts' => -1,
648
			'newbie_group' => 4,
649
			'moderator_group' => 3,
650
			'groups' => isset($_REQUEST['groups']) ? $_REQUEST['groups'] : array(),
651
		)
652
	);
653
	if (!isset($_REQUEST['groups']) || in_array(-1, $_REQUEST['groups']) || in_array(0, $_REQUEST['groups']))
654
		$groups = array('col' => '', -1 => $txt['membergroups_guests'], 0 => $txt['membergroups_members']);
655
	else
656
		$groups = array('col' => '');
657
	while ($row = $smcFunc['db_fetch_assoc']($request))
658
		$groups[$row['id_group']] = $row['group_name'];
659
	$smcFunc['db_free_result']($request);
660
661
	// Make sure that every group is represented!
662
	setKeys('rows', $groups);
663
664
	// Create the table first.
665
	newTable($txt['gr_type_group_perms'], '-', 'all', 100, 'center', 200, 'left');
666
667
	// Show all the groups
668
	addData($groups);
669
670
	// Add a separator
671
	addSeparator($txt['board_perms_permission']);
672
673
	// Certain permissions should not really be shown.
674
	$disabled_permissions = array();
675
	if (empty($modSettings['cal_enabled']))
676
	{
677
		$disabled_permissions[] = 'calendar_view';
678
		$disabled_permissions[] = 'calendar_post';
679
		$disabled_permissions[] = 'calendar_edit_own';
680
		$disabled_permissions[] = 'calendar_edit_any';
681
	}
682
	if (empty($modSettings['warning_settings']) || $modSettings['warning_settings'][0] == 0)
683
		$disabled_permissions[] = 'issue_warning';
684
685
	call_integration_hook('integrate_reports_groupperm', array(&$disabled_permissions));
686
687
	// Now the big permission fetch!
688
	$request = $smcFunc['db_query']('', '
689
		SELECT id_group, add_deny, permission
690
		FROM {db_prefix}permissions
691
		WHERE ' . $clause . (empty($modSettings['permission_enable_deny']) ? '
692
			AND add_deny = {int:not_denied}' : '') . '
693
		ORDER BY permission',
694
		array(
695
			'not_denied' => 1,
696
			'moderator_group' => 3,
697
			'groups' => isset($_REQUEST['groups']) ? $_REQUEST['groups'] : array(),
698
		)
699
	);
700
	$lastPermission = null;
701
	$curData = array();
702
	while ($row = $smcFunc['db_fetch_assoc']($request))
703
	{
704
		if (in_array($row['permission'], $disabled_permissions))
705
			continue;
706
707
		if (strpos($row['permission'], 'bbc_') === 0)
708
			$txt['group_perms_name_' . $row['permission']] = sprintf($txt['group_perms_name_bbc'], substr($row['permission'], 4));
709
710
		// If this is a new permission flush the last row.
711
		if ($row['permission'] != $lastPermission)
712
		{
713
			// Send the data!
714
			if ($lastPermission !== null)
715
				addData($curData);
716
717
			// Add the permission name in the left column.
718
			$curData = array('col' => isset($txt['group_perms_name_' . $row['permission']]) ? $txt['group_perms_name_' . $row['permission']] : $row['permission']);
719
720
			$lastPermission = $row['permission'];
721
		}
722
723
		// Good stuff - add the permission to the list!
724
		if ($row['add_deny'])
725
			$curData[$row['id_group']] = '<span style="color: darkgreen;">' . $txt['board_perms_allow'] . '</span>';
726
		else
727
			$curData[$row['id_group']] = '<span class="red">' . $txt['board_perms_deny'] . '</span>';
728
	}
729
	$smcFunc['db_free_result']($request);
730
731
	// Flush the last data!
732
	addData($curData);
733
}
734
735
/**
736
 * Report for showing all the forum staff members - quite a feat!
737
 * functions ending with "Report" are responsible for generating data
738
 * for reporting.
739
 * they are all called from ReportsMain.
740
 * never access the context directly, but use the data handling
741
 * functions to do so.
742
 */
743
function StaffReport()
744
{
745
	global $sourcedir, $txt, $smcFunc;
746
747
	require_once($sourcedir . '/Subs-Members.php');
748
749
	// Fetch all the board names.
750
	$request = $smcFunc['db_query']('', '
751
		SELECT id_board, name
752
		FROM {db_prefix}boards',
753
		array(
754
		)
755
	);
756
	$boards = array();
757
	while ($row = $smcFunc['db_fetch_assoc']($request))
758
		$boards[$row['id_board']] = $row['name'];
759
	$smcFunc['db_free_result']($request);
760
761
	// Get every moderator.
762
	$request = $smcFunc['db_query']('', '
763
		SELECT mods.id_board, mods.id_member
764
		FROM {db_prefix}moderators AS mods',
765
		array(
766
		)
767
	);
768
	$moderators = array();
769
	$local_mods = array();
770
	while ($row = $smcFunc['db_fetch_assoc']($request))
771
	{
772
		$moderators[$row['id_member']][] = $row['id_board'];
773
		$local_mods[$row['id_member']] = $row['id_member'];
774
	}
775
	$smcFunc['db_free_result']($request);
776
777
	// Get any additional boards they can moderate through group-based board moderation
778
	$request = $smcFunc['db_query']('', '
779
		SELECT mem.id_member, modgs.id_board
780
		FROM {db_prefix}members AS mem
781
			INNER JOIN {db_prefix}moderator_groups AS modgs ON (modgs.id_group = mem.id_group OR FIND_IN_SET(modgs.id_group, mem.additional_groups) != 0)',
782
		array(
783
		)
784
	);
785
786
	// Add each board/member to the arrays, but only if they aren't already there
787
	while ($row = $smcFunc['db_fetch_assoc']($request))
788
	{
789
		// Either we don't have them as a moderator at all or at least not as a moderator of this board
790
		if (!array_key_exists($row['id_member'], $moderators) || !in_array($row['id_board'], $moderators[$row['id_member']]))
791
			$moderators[$row['id_member']][] = $row['id_board'];
792
793
		// We don't have them listed as a moderator yet
794
		if (!array_key_exists($row['id_member'], $local_mods))
795
			$local_mods[$row['id_member']] = $row['id_member'];
796
	}
797
798
	// Get a list of global moderators (i.e. members with moderation powers).
799
	$global_mods = array_intersect(membersAllowedTo('moderate_board', 0), membersAllowedTo('approve_posts', 0), membersAllowedTo('remove_any', 0), membersAllowedTo('modify_any', 0));
800
801
	// How about anyone else who is special?
802
	$allStaff = array_merge(membersAllowedTo('admin_forum'), membersAllowedTo('manage_membergroups'), membersAllowedTo('manage_permissions'), $local_mods, $global_mods);
803
804
	// Make sure everyone is there once - no admin less important than any other!
805
	$allStaff = array_unique($allStaff);
806
807
	// This is a bit of a cop out - but we're protecting their forum, really!
808
	if (count($allStaff) > 300)
809
		fatal_lang_error('report_error_too_many_staff');
810
811
	// Get all the possible membergroups!
812
	$request = $smcFunc['db_query']('', '
813
		SELECT id_group, group_name, online_color
814
		FROM {db_prefix}membergroups',
815
		array(
816
		)
817
	);
818
	$groups = array(0 => $txt['membergroups_members']);
819
	while ($row = $smcFunc['db_fetch_assoc']($request))
820
		$groups[$row['id_group']] = empty($row['online_color']) ? $row['group_name'] : '<span style="color: ' . $row['online_color'] . '">' . $row['group_name'] . '</span>';
821
	$smcFunc['db_free_result']($request);
822
823
	// All the fields we'll show.
824
	$staffSettings = array(
825
		'position' => $txt['report_staff_position'],
826
		'moderates' => $txt['report_staff_moderates'],
827
		'posts' => $txt['report_staff_posts'],
828
		'last_login' => $txt['report_staff_last_login'],
829
	);
830
831
	// Do it in columns, it's just easier.
832
	setKeys('cols');
833
834
	// Get each member!
835
	$request = $smcFunc['db_query']('', '
836
		SELECT id_member, real_name, id_group, posts, last_login
837
		FROM {db_prefix}members
838
		WHERE id_member IN ({array_int:staff_list})
839
		ORDER BY real_name',
840
		array(
841
			'staff_list' => $allStaff,
842
		)
843
	);
844
	while ($row = $smcFunc['db_fetch_assoc']($request))
845
	{
846
		// Each member gets their own table!.
847
		newTable($row['real_name'], '', 'left', 'auto', 'left', 200, 'center');
848
849
		// First off, add in the side key.
850
		addData($staffSettings);
851
852
		// Create the main data array.
853
		$staffData = array(
854
			'position' => isset($groups[$row['id_group']]) ? $groups[$row['id_group']] : $groups[0],
855
			'posts' => $row['posts'],
856
			'last_login' => timeformat($row['last_login']),
857
			'moderates' => array(),
858
		);
859
860
		// What do they moderate?
861
		if (in_array($row['id_member'], $global_mods))
862
			$staffData['moderates'] = '<em>' . $txt['report_staff_all_boards'] . '</em>';
863
		elseif (isset($moderators[$row['id_member']]))
864
		{
865
			// Get the names
866
			foreach ($moderators[$row['id_member']] as $board)
867
				if (isset($boards[$board]))
868
					$staffData['moderates'][] = $boards[$board];
869
870
			$staffData['moderates'] = implode(', ', $staffData['moderates']);
871
		}
872
		else
873
			$staffData['moderates'] = '<em>' . $txt['report_staff_no_boards'] . '</em>';
874
875
		// Next add the main data.
876
		addData($staffData);
877
	}
878
	$smcFunc['db_free_result']($request);
879
}
880
881
/**
882
 * This function creates a new table of data, most functions will only use it once.
883
 * The core of this file, it creates a new, but empty, table of data in
884
 * context, ready for filling using addData().
885
 * Fills the context variable current_table with the ID of the table created.
886
 * Keeps track of the current table count using context variable table_count.
887
 *
888
 * @param string $title Title to be displayed with this data table.
889
 * @param string $default_value Value to be displayed if a key is missing from a row.
890
 * @param string $shading Should the left, top or both (all) parts of the table beshaded?
891
 * @param string $width_normal The width of an unshaded column (auto means not defined).
892
 * @param string $align_normal The alignment of data in an unshaded column.
893
 * @param string $width_shaded The width of a shaded column (auto means not defined).
894
 * @param string $align_shaded The alignment of data in a shaded column.
895
 */
896
function newTable($title = '', $default_value = '', $shading = 'all', $width_normal = 'auto', $align_normal = 'center', $width_shaded = 'auto', $align_shaded = 'auto')
897
{
898
	global $context;
899
900
	// Set the table count if needed.
901
	if (empty($context['table_count']))
902
		$context['table_count'] = 0;
903
904
	// Create the table!
905
	$context['tables'][$context['table_count']] = array(
906
		'title' => $title,
907
		'default_value' => $default_value,
908
		'shading' => array(
909
			'left' => $shading == 'all' || $shading == 'left',
910
			'top' => $shading == 'all' || $shading == 'top',
911
		),
912
		'width' => array(
913
			'normal' => $width_normal,
914
			'shaded' => $width_shaded,
915
		),
916
		/* Align usage deprecated due to HTML5 */
917
		'align' => array(
918
			'normal' => $align_normal,
919
			'shaded' => $align_shaded,
920
		),
921
		'data' => array(),
922
	);
923
924
	$context['current_table'] = $context['table_count'];
925
926
	// Increment the count...
927
	$context['table_count']++;
928
}
929
930
/**
931
 * Adds an array of data into an existing table.
932
 * if there are no existing tables, will create one with default
933
 * attributes.
934
 * if custom_table isn't specified, it will use the last table created,
935
 * if it is specified and doesn't exist the function will return false.
936
 * if a set of keys have been specified, the function will check each
937
 * required key is present in the incoming data. If this data is missing
938
 * the current tables default value will be used.
939
 * if any key in the incoming data begins with '#sep#', the function
940
 * will add a separator across the table at this point.
941
 * once the incoming data has been sanitized, it is added to the table.
942
 *
943
 * @param array $inc_data The data to include
944
 * @param null|string $custom_table = null The ID of a custom table to put the data in
945
 * @return void|false Doesn't return anything unless we've specified an invalid custom_table
946
 */
947
function addData($inc_data, $custom_table = null)
948
{
949
	global $context;
950
951
	// No tables? Create one even though we are probably already in a bad state!
952
	if (empty($context['table_count']))
953
		newTable();
954
955
	// Specific table?
956
	if ($custom_table !== null && !isset($context['tables'][$custom_table]))
957
		return false;
958
	elseif ($custom_table !== null)
959
		$table = $custom_table;
960
	else
961
		$table = $context['current_table'];
962
963
	// If we have keys, sanitise the data...
964
	if (!empty($context['keys']))
965
	{
966
		// Basically, check every key exists!
967
		foreach ($context['keys'] as $key => $dummy)
968
		{
969
			$data[$key] = array(
970
				'v' => empty($inc_data[$key]) ? $context['tables'][$table]['default_value'] : $inc_data[$key],
971
			);
972
			// Special "hack" the adding separators when doing data by column.
973
			if (substr($key, 0, 5) == '#sep#')
974
				$data[$key]['separator'] = true;
975
		}
976
	}
977
	else
978
	{
979
		$data = $inc_data;
980
		foreach ($data as $key => $value)
981
		{
982
			$data[$key] = array(
983
				'v' => $value,
984
			);
985
			if (substr($key, 0, 5) == '#sep#')
986
				$data[$key]['separator'] = true;
987
		}
988
	}
989
990
	// Is it by row?
991
	if (empty($context['key_method']) || $context['key_method'] == 'rows')
992
	{
993
		// Add the data!
994
		$context['tables'][$table]['data'][] = $data;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.
Loading history...
995
	}
996
	// Otherwise, tricky!
997
	else
998
	{
999
		foreach ($data as $key => $item)
1000
			$context['tables'][$table]['data'][$key][] = $item;
1001
	}
1002
}
1003
1004
/**
1005
 * Add a separator row, only really used when adding data by rows.
1006
 *
1007
 * @param string $title The title of the separator
1008
 * @param null|string $custom_table The ID of the custom table
1009
 *
1010
 * @return void|bool Returns false if there are no tables
1011
 */
1012
function addSeparator($title = '', $custom_table = null)
1013
{
1014
	global $context;
1015
1016
	// No tables - return?
1017
	if (empty($context['table_count']))
1018
		return;
1019
1020
	// Specific table?
1021
	if ($custom_table !== null && !isset($context['tables'][$table]))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $table seems to be never defined.
Loading history...
1022
		return false;
1023
	elseif ($custom_table !== null)
1024
		$table = $custom_table;
1025
	else
1026
		$table = $context['current_table'];
1027
1028
	// Plumb in the separator
1029
	$context['tables'][$table]['data'][] = array(0 => array(
1030
		'separator' => true,
1031
		'v' => $title
1032
	));
1033
}
1034
1035
/**
1036
 * This does the necessary count of table data before displaying them.
1037
 * is (unfortunately) required to create some useful variables for templates.
1038
 * foreach data table created, it will count the number of rows and
1039
 * columns in the table.
1040
 * will also create a max_width variable for the table, to give an
1041
 * estimate width for the whole table * * if it can.
1042
 */
1043
function finishTables()
1044
{
1045
	global $context;
1046
1047
	if (empty($context['tables']))
1048
		return;
1049
1050
	// Loop through each table counting up some basic values, to help with the templating.
1051
	foreach ($context['tables'] as $id => $table)
1052
	{
1053
		$context['tables'][$id]['id'] = $id;
1054
		$context['tables'][$id]['row_count'] = count($table['data']);
1055
		$curElement = current($table['data']);
1056
		$context['tables'][$id]['column_count'] = count($curElement);
1057
1058
		// Work out the rough width - for templates like the print template. Without this we might get funny tables.
1059
		if ($table['shading']['left'] && $table['width']['shaded'] != 'auto' && $table['width']['normal'] != 'auto')
1060
			$context['tables'][$id]['max_width'] = $table['width']['shaded'] + ($context['tables'][$id]['column_count'] - 1) * $table['width']['normal'];
1061
		elseif ($table['width']['normal'] != 'auto')
1062
			$context['tables'][$id]['max_width'] = $context['tables'][$id]['column_count'] * $table['width']['normal'];
1063
		else
1064
			$context['tables'][$id]['max_width'] = 'auto';
1065
	}
1066
}
1067
1068
/**
1069
 * Set the keys in use by the tables - these ensure entries MUST exist if the data isn't sent.
1070
 *
1071
 * sets the current set of "keys" expected in each data array passed to
1072
 * addData. It also sets the way we are adding data to the data table.
1073
 * method specifies whether the data passed to addData represents a new
1074
 * column, or a new row.
1075
 * keys is an array whose keys are the keys for data being passed to
1076
 * addData().
1077
 * if reverse is set to true, then the values of the variable "keys"
1078
 * are used as opposed to the keys(!
1079
 *
1080
 * @param string $method The method. Can be 'rows' or 'columns'
1081
 * @param array $keys The keys
1082
 * @param bool $reverse Whether we want to use the values as the keys
1083
 */
1084
function setKeys($method = 'rows', $keys = array(), $reverse = false)
1085
{
1086
	global $context;
1087
1088
	// Do we want to use the keys of the keys as the keys? :P
1089
	if ($reverse)
1090
		$context['keys'] = array_flip($keys);
1091
	else
1092
		$context['keys'] = $keys;
1093
1094
	// Rows or columns?
1095
	$context['key_method'] = $method == 'rows' ? 'rows' : 'cols';
1096
}
1097
1098
?>