Passed
Push — release-2.1 ( 0c2197...207d2d )
by Jeremy
05:47
created

EditBoard()   F

Complexity

Conditions 30
Paths > 20000

Size

Total Lines 234
Code Lines 139

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 30
eloc 139
c 0
b 0
f 0
nop 0
dl 0
loc 234
rs 0
nc 2949120

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Manage and maintain the boards and categories of the forum.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines http://www.simplemachines.org
10
 * @copyright 2018 Simple Machines and individual contributors
11
 * @license http://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 Beta 4
14
 */
15
16
if (!defined('SMF'))
17
	die('No direct access...');
18
19
/**
20
 * The main dispatcher; doesn't do anything, just delegates.
21
 * This is the main entry point for all the manageboards admin screens.
22
 * Called by ?action=admin;area=manageboards.
23
 * It checks the permissions, based on the sub-action, and calls a function based on the sub-action.
24
 *
25
 *  @uses ManageBoards language file.
26
 */
27
function ManageBoards()
28
{
29
	global $context, $txt;
30
31
	// Everything's gonna need this.
32
	loadLanguage('ManageBoards');
33
34
	// Format: 'sub-action' => array('function', 'permission')
35
	$subActions = array(
36
		'board' => array('EditBoard', 'manage_boards'),
37
		'board2' => array('EditBoard2', 'manage_boards'),
38
		'cat' => array('EditCategory', 'manage_boards'),
39
		'cat2' => array('EditCategory2', 'manage_boards'),
40
		'main' => array('ManageBoardsMain', 'manage_boards'),
41
		'move' => array('ManageBoardsMain', 'manage_boards'),
42
		'newcat' => array('EditCategory', 'manage_boards'),
43
		'newboard' => array('EditBoard', 'manage_boards'),
44
		'settings' => array('EditBoardSettings', 'admin_forum'),
45
	);
46
47
	// Create the tabs for the template.
48
	$context[$context['admin_menu_name']]['tab_data'] = array(
49
		'title' => $txt['boards_and_cats'],
50
		'help' => 'manage_boards',
51
		'description' => $txt['boards_and_cats_desc'],
52
		'tabs' => array(
53
			'main' => array(
54
			),
55
			'newcat' => array(
56
			),
57
			'settings' => array(
58
				'description' => $txt['mboards_settings_desc'],
59
			),
60
		),
61
	);
62
63
	call_integration_hook('integrate_manage_boards', array(&$subActions));
64
65
	// Default to sub action 'main' or 'settings' depending on permissions.
66
	$_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : (allowedTo('manage_boards') ? 'main' : 'settings');
67
68
	// Have you got the proper permissions?
69
	isAllowedTo($subActions[$_REQUEST['sa']][1]);
70
71
	call_helper($subActions[$_REQUEST['sa']][0]);
72
}
73
74
/**
75
 * The main control panel thing, the screen showing all boards and categories.
76
 * Called by ?action=admin;area=manageboards or ?action=admin;area=manageboards;sa=move.
77
 * Requires manage_boards permission.
78
 * It also handles the interface for moving boards.
79
 *
80
 * @uses ManageBoards template, main sub-template.
81
 */
82
function ManageBoardsMain()
83
{
84
	global $txt, $context, $cat_tree, $boards, $boardList, $scripturl, $sourcedir, $smcFunc;
85
86
	loadTemplate('ManageBoards');
87
88
	require_once($sourcedir . '/Subs-Boards.php');
89
90
	if (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'move' && in_array($_REQUEST['move_to'], array('child', 'before', 'after', 'top')))
91
	{
92
		checkSession('get');
93
		validateToken('admin-bm-' . (int) $_REQUEST['src_board'], 'request');
94
95
		if ($_REQUEST['move_to'] === 'top')
96
			$boardOptions = array(
97
				'move_to' => $_REQUEST['move_to'],
98
				'target_category' => (int) $_REQUEST['target_cat'],
99
				'move_first_child' => true,
100
			);
101
		else
102
			$boardOptions = array(
103
				'move_to' => $_REQUEST['move_to'],
104
				'target_board' => (int) $_REQUEST['target_board'],
105
				'move_first_child' => true,
106
			);
107
		modifyBoard((int) $_REQUEST['src_board'], $boardOptions);
108
	}
109
110
	getBoardTree();
111
112
	$context['move_board'] = !empty($_REQUEST['move']) && isset($boards[(int) $_REQUEST['move']]) ? (int) $_REQUEST['move'] : 0;
113
114
	$context['categories'] = array();
115
	foreach ($cat_tree as $catid => $tree)
116
	{
117
		$context['categories'][$catid] = array(
118
			'name' => &$tree['node']['name'],
119
			'id' => &$tree['node']['id'],
120
			'boards' => array()
121
		);
122
		$move_cat = !empty($context['move_board']) && $boards[$context['move_board']]['category'] == $catid;
123
		foreach ($boardList[$catid] as $boardid)
124
		{
125
			$context['categories'][$catid]['boards'][$boardid] = array(
126
				'id' => &$boards[$boardid]['id'],
127
				'name' => &$boards[$boardid]['name'],
128
				'description' => &$boards[$boardid]['description'],
129
				'child_level' => &$boards[$boardid]['level'],
130
				'move' => $move_cat && ($boardid == $context['move_board'] || isChildOf($boardid, $context['move_board'])),
131
				'permission_profile' => &$boards[$boardid]['profile'],
132
				'is_redirect' => !empty($boards[$boardid]['redirect']),
133
			);
134
		}
135
	}
136
137
	if (!empty($context['move_board']))
138
	{
139
		createToken('admin-bm-' . $context['move_board'], 'request');
140
141
		$context['move_title'] = sprintf($txt['mboards_select_destination'], $smcFunc['htmlspecialchars']($boards[$context['move_board']]['name']));
142
		foreach ($cat_tree as $catid => $tree)
143
		{
144
			$prev_child_level = 0;
145
			$prev_board = 0;
146
			$stack = array();
147
			// Just a shortcut, this is the same for all the urls
148
			$security = $context['session_var'] . '=' . $context['session_id'] . ';' . $context['admin-bm-' . $context['move_board'] . '_token_var'] . '=' . $context['admin-bm-' . $context['move_board'] . '_token'];
149
			foreach ($boardList[$catid] as $boardid)
150
			{
151
				if (!isset($context['categories'][$catid]['move_link']))
152
					$context['categories'][$catid]['move_link'] = array(
153
						'child_level' => 0,
154
						'label' => $txt['mboards_order_before'] . ' \'' . $smcFunc['htmlspecialchars']($boards[$boardid]['name']) . '\'',
155
						'href' => $scripturl . '?action=admin;area=manageboards;sa=move;src_board=' . $context['move_board'] . ';target_board=' . $boardid . ';move_to=before;' . $security,
156
					);
157
158
				if (!$context['categories'][$catid]['boards'][$boardid]['move'])
159
				$context['categories'][$catid]['boards'][$boardid]['move_links'] = array(
160
					array(
161
						'child_level' => $boards[$boardid]['level'],
162
						'label' => $txt['mboards_order_after'] . '\'' . $smcFunc['htmlspecialchars']($boards[$boardid]['name']) . '\'',
163
						'href' => $scripturl . '?action=admin;area=manageboards;sa=move;src_board=' . $context['move_board'] . ';target_board=' . $boardid . ';move_to=after;' . $security,
164
						'class' => $boards[$boardid]['level'] > 0 ? 'above' : 'below',
165
					),
166
					array(
167
						'child_level' => $boards[$boardid]['level'] + 1,
168
						'label' => $txt['mboards_order_child_of'] . ' \'' . $smcFunc['htmlspecialchars']($boards[$boardid]['name']) . '\'',
169
						'href' => $scripturl . '?action=admin;area=manageboards;sa=move;src_board=' . $context['move_board'] . ';target_board=' . $boardid . ';move_to=child;' . $security,
170
						'class' => 'here',
171
					),
172
				);
173
174
				$difference = $boards[$boardid]['level'] - $prev_child_level;
175
				if ($difference == 1)
176
					array_push($stack, !empty($context['categories'][$catid]['boards'][$prev_board]['move_links']) ? array_shift($context['categories'][$catid]['boards'][$prev_board]['move_links']) : null);
177
				elseif ($difference < 0)
178
				{
179
					if (empty($context['categories'][$catid]['boards'][$prev_board]['move_links']))
180
						$context['categories'][$catid]['boards'][$prev_board]['move_links'] = array();
181
					for ($i = 0; $i < -$difference; $i++)
182
						if (($temp = array_pop($stack)) != null)
183
							array_unshift($context['categories'][$catid]['boards'][$prev_board]['move_links'], $temp);
184
				}
185
186
				$prev_board = $boardid;
187
				$prev_child_level = $boards[$boardid]['level'];
188
			}
189
			if (!empty($stack) && !empty($context['categories'][$catid]['boards'][$prev_board]['move_links']))
190
				$context['categories'][$catid]['boards'][$prev_board]['move_links'] = array_merge($stack, $context['categories'][$catid]['boards'][$prev_board]['move_links']);
191
			elseif (!empty($stack))
192
				$context['categories'][$catid]['boards'][$prev_board]['move_links'] = $stack;
193
194
			if (empty($boardList[$catid]))
195
				$context['categories'][$catid]['move_link'] = array(
196
					'child_level' => 0,
197
					'label' => $txt['mboards_order_before'] . ' \'' . $smcFunc['htmlspecialchars']($tree['node']['name']) . '\'',
198
					'href' => $scripturl . '?action=admin;area=manageboards;sa=move;src_board=' . $context['move_board'] . ';target_cat=' . $catid . ';move_to=top;' . $security,
199
				);
200
		}
201
	}
202
203
	call_integration_hook('integrate_boards_main');
204
205
	$context['page_title'] = $txt['boards_and_cats'];
206
	$context['can_manage_permissions'] = allowedTo('manage_permissions');
207
}
208
209
/**
210
 * Modify a specific category.
211
 * (screen for editing and repositioning a category.)
212
 * Also used to show the confirm deletion of category screen
213
 * (sub-template confirm_category_delete).
214
 * Called by ?action=admin;area=manageboards;sa=cat
215
 * Requires manage_boards permission.
216
 *
217
 * @uses ManageBoards template, modify_category sub-template.
218
 */
219
function EditCategory()
220
{
221
	global $txt, $context, $cat_tree, $boardList, $boards, $smcFunc, $sourcedir;
222
223
	loadTemplate('ManageBoards');
224
	require_once($sourcedir . '/Subs-Boards.php');
225
	require_once($sourcedir . '/Subs-Editor.php');
226
	getBoardTree();
227
228
	// id_cat must be a number.... if it exists.
229
	$_REQUEST['cat'] = isset($_REQUEST['cat']) ? (int) $_REQUEST['cat'] : 0;
230
231
	// Start with one - "In first place".
232
	$context['category_order'] = array(
233
		array(
234
			'id' => 0,
235
			'name' => $txt['mboards_order_first'],
236
			'selected' => !empty($_REQUEST['cat']) ? $cat_tree[$_REQUEST['cat']]['is_first'] : false,
237
			'true_name' => ''
238
		)
239
	);
240
241
	// If this is a new category set up some defaults.
242
	if ($_REQUEST['sa'] == 'newcat')
243
	{
244
		$context['category'] = array(
245
			'id' => 0,
246
			'name' => $txt['mboards_new_cat_name'],
247
			'editable_name' => $smcFunc['htmlspecialchars']($txt['mboards_new_cat_name']),
248
			'description' => '',
249
			'can_collapse' => true,
250
			'is_new' => true,
251
			'is_empty' => true
252
		);
253
	}
254
	// Category doesn't exist, man... sorry.
255
	elseif (!isset($cat_tree[$_REQUEST['cat']]))
256
		redirectexit('action=admin;area=manageboards');
257
	else
258
	{
259
		$context['category'] = array(
260
			'id' => $_REQUEST['cat'],
261
			'name' => $cat_tree[$_REQUEST['cat']]['node']['name'],
262
			'editable_name' => html_to_bbc($cat_tree[$_REQUEST['cat']]['node']['name']),
263
			'description' => html_to_bbc($cat_tree[$_REQUEST['cat']]['node']['description']),
264
			'can_collapse' => !empty($cat_tree[$_REQUEST['cat']]['node']['can_collapse']),
265
			'children' => array(),
266
			'is_empty' => empty($cat_tree[$_REQUEST['cat']]['children'])
267
		);
268
269
		foreach ($boardList[$_REQUEST['cat']] as $child_board)
270
			$context['category']['children'][] = str_repeat('-', $boards[$child_board]['level']) . ' ' . $boards[$child_board]['name'];
271
	}
272
273
	$prevCat = 0;
274
	foreach ($cat_tree as $catid => $tree)
275
	{
276
		if ($catid == $_REQUEST['cat'] && $prevCat > 0)
277
			$context['category_order'][$prevCat]['selected'] = true;
278
		elseif ($catid != $_REQUEST['cat'])
279
			$context['category_order'][$catid] = array(
280
				'id' => $catid,
281
				'name' => $txt['mboards_order_after'] . $tree['node']['name'],
282
				'selected' => false,
283
				'true_name' => $tree['node']['name']
284
			);
285
		$prevCat = $catid;
286
	}
287
	if (!isset($_REQUEST['delete']))
288
	{
289
		$context['sub_template'] = 'modify_category';
290
		$context['page_title'] = $_REQUEST['sa'] == 'newcat' ? $txt['mboards_new_cat_name'] : $txt['catEdit'];
291
	}
292
	else
293
	{
294
		$context['sub_template'] = 'confirm_category_delete';
295
		$context['page_title'] = $txt['mboards_delete_cat'];
296
	}
297
298
	// Create a special token.
299
	createToken('admin-bc-' . $_REQUEST['cat']);
300
	$context['token_check'] = 'admin-bc-' . $_REQUEST['cat'];
301
302
	call_integration_hook('integrate_edit_category');
303
}
304
305
/**
306
 * Function for handling a submitted form saving the category.
307
 * (complete the modifications to a specific category.)
308
 * It also handles deletion of a category.
309
 * It requires manage_boards permission.
310
 * Called by ?action=admin;area=manageboards;sa=cat2
311
 * Redirects to ?action=admin;area=manageboards.
312
 */
313
function EditCategory2()
314
{
315
	global $sourcedir, $smcFunc, $context;
316
317
	checkSession();
318
	validateToken('admin-bc-' . $_REQUEST['cat']);
319
320
	require_once($sourcedir . '/Subs-Categories.php');
321
322
	$_POST['cat'] = (int) $_POST['cat'];
323
324
	// Add a new category or modify an existing one..
325
	if (isset($_POST['edit']) || isset($_POST['add']))
326
	{
327
		$catOptions = array();
328
329
		if (isset($_POST['cat_order']))
330
			$catOptions['move_after'] = (int) $_POST['cat_order'];
331
332
		// Change "This & That" to "This &amp; That" but don't change "&cent" to "&amp;cent;"...
333
		$catOptions['cat_name'] = parse_bbc($smcFunc['htmlspecialchars']($_POST['cat_name']), false, '', $context['description_allowed_tags']);
334
		$catOptions['cat_desc'] = parse_bbc($smcFunc['htmlspecialchars']($_POST['cat_desc']), false, '', $context['description_allowed_tags']);
335
336
		$catOptions['is_collapsible'] = isset($_POST['collapse']);
337
338
		if (isset($_POST['add']))
339
			createCategory($catOptions);
340
		else
341
			modifyCategory($_POST['cat'], $catOptions);
342
	}
343
	// If they want to delete - first give them confirmation.
344
	elseif (isset($_POST['delete']) && !isset($_POST['confirmation']) && !isset($_POST['empty']))
345
	{
346
		EditCategory();
347
		return;
348
	}
349
	// Delete the category!
350
	elseif (isset($_POST['delete']))
351
	{
352
		// First off - check if we are moving all the current boards first - before we start deleting!
353
		if (isset($_POST['delete_action']) && $_POST['delete_action'] == 1)
354
		{
355
			if (empty($_POST['cat_to']))
356
				fatal_lang_error('mboards_delete_error');
357
358
			deleteCategories(array($_POST['cat']), (int) $_POST['cat_to']);
359
		}
360
		else
361
			deleteCategories(array($_POST['cat']));
362
	}
363
364
	redirectexit('action=admin;area=manageboards');
365
}
366
367
/**
368
 * Modify a specific board...
369
 * screen for editing and repositioning a board.
370
 * called by ?action=admin;area=manageboards;sa=board
371
 * uses the modify_board sub-template of the ManageBoards template.
372
 * requires manage_boards permission.
373
 * also used to show the confirm deletion of category screen (sub-template confirm_board_delete).
374
 */
375
function EditBoard()
376
{
377
	global $txt, $context, $cat_tree, $boards, $boardList;
378
	global $sourcedir, $smcFunc, $modSettings;
379
380
	loadTemplate('ManageBoards');
381
	require_once($sourcedir . '/Subs-Boards.php');
382
	require_once($sourcedir . '/Subs-Editor.php');
383
	getBoardTree();
384
385
	// For editing the profile we'll need this.
386
	loadLanguage('ManagePermissions');
387
	require_once($sourcedir . '/ManagePermissions.php');
388
	loadPermissionProfiles();
389
390
	// People with manage-boards are special.
391
	require_once($sourcedir . '/Subs-Members.php');
392
	$groups = groupsAllowedTo('manage_boards', null);
393
	$context['board_managers'] = $groups['allowed']; // We don't need *all* this in $context.
394
395
	// id_board must be a number....
396
	$_REQUEST['boardid'] = isset($_REQUEST['boardid']) ? (int) $_REQUEST['boardid'] : 0;
397
	if (!isset($boards[$_REQUEST['boardid']]))
398
	{
399
		$_REQUEST['boardid'] = 0;
400
		$_REQUEST['sa'] = 'newboard';
401
	}
402
403
	if ($_REQUEST['sa'] == 'newboard')
404
	{
405
		// Category doesn't exist, man... sorry.
406
		if (empty($_REQUEST['cat']))
407
			redirectexit('action=admin;area=manageboards');
408
409
		// Some things that need to be setup for a new board.
410
		$curBoard = array(
411
			'member_groups' => array(0, -1),
412
			'deny_groups' => array(),
413
			'category' => (int) $_REQUEST['cat']
414
		);
415
		$context['board_order'] = array();
416
		$context['board'] = array(
417
			'is_new' => true,
418
			'id' => 0,
419
			'name' => $txt['mboards_new_board_name'],
420
			'description' => '',
421
			'count_posts' => 1,
422
			'posts' => 0,
423
			'topics' => 0,
424
			'theme' => 0,
425
			'profile' => 1,
426
			'override_theme' => 0,
427
			'redirect' => '',
428
			'category' => (int) $_REQUEST['cat'],
429
			'no_children' => true,
430
		);
431
	}
432
	else
433
	{
434
		// Just some easy shortcuts.
435
		$curBoard = &$boards[$_REQUEST['boardid']];
436
		$context['board'] = $boards[$_REQUEST['boardid']];
437
		$context['board']['name'] = html_to_bbc($context['board']['name']);
438
		$context['board']['description'] = html_to_bbc($context['board']['description']);
439
		$context['board']['no_children'] = empty($boards[$_REQUEST['boardid']]['tree']['children']);
440
		$context['board']['is_recycle'] = !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) && $modSettings['recycle_board'] == $context['board']['id'];
441
	}
442
443
	// As we may have come from the permissions screen keep track of where we should go on save.
444
	$context['redirect_location'] = isset($_GET['rid']) && $_GET['rid'] == 'permissions' ? 'permissions' : 'boards';
445
446
	// We might need this to hide links to certain areas.
447
	$context['can_manage_permissions'] = allowedTo('manage_permissions');
448
449
	// Default membergroups.
450
	$context['groups'] = array(
451
		-1 => array(
452
			'id' => '-1',
453
			'name' => $txt['parent_guests_only'],
454
			'allow' => in_array('-1', $curBoard['member_groups']),
455
			'deny' => in_array('-1', $curBoard['deny_groups']),
456
			'is_post_group' => false,
457
		),
458
		0 => array(
459
			'id' => '0',
460
			'name' => $txt['parent_members_only'],
461
			'allow' => in_array('0', $curBoard['member_groups']),
462
			'deny' => in_array('0', $curBoard['deny_groups']),
463
			'is_post_group' => false,
464
		)
465
	);
466
467
	// Load membergroups.
468
	$request = $smcFunc['db_query']('', '
469
		SELECT group_name, id_group, min_posts
470
		FROM {db_prefix}membergroups
471
		WHERE id_group > {int:moderator_group} OR id_group = {int:global_moderator}
472
		ORDER BY min_posts, id_group != {int:global_moderator}, group_name',
473
		array(
474
			'moderator_group' => 3,
475
			'global_moderator' => 2,
476
		)
477
	);
478
	while ($row = $smcFunc['db_fetch_assoc']($request))
479
	{
480
		if ($_REQUEST['sa'] == 'newboard' && $row['min_posts'] == -1)
481
			$curBoard['member_groups'][] = $row['id_group'];
482
483
		$context['groups'][(int) $row['id_group']] = array(
484
			'id' => $row['id_group'],
485
			'name' => trim($row['group_name']),
486
			'allow' => in_array($row['id_group'], $curBoard['member_groups']),
487
			'deny' => in_array($row['id_group'], $curBoard['deny_groups']),
488
			'is_post_group' => $row['min_posts'] != -1,
489
		);
490
	}
491
	$smcFunc['db_free_result']($request);
492
493
	// Category doesn't exist, man... sorry.
494
	if (!isset($boardList[$curBoard['category']]))
495
		redirectexit('action=admin;area=manageboards');
496
497
	foreach ($boardList[$curBoard['category']] as $boardid)
498
	{
499
		if ($boardid == $_REQUEST['boardid'])
500
		{
501
			$context['board_order'][] = array(
502
				'id' => $boardid,
503
				'name' => str_repeat('-', $boards[$boardid]['level']) . ' (' . $txt['mboards_current_position'] . ')',
504
				'children' => $boards[$boardid]['tree']['children'],
505
				'no_children' => empty($boards[$boardid]['tree']['children']),
506
				'is_child' => false,
507
				'selected' => true
508
			);
509
		}
510
		else
511
		{
512
			$context['board_order'][] = array(
513
				'id' => $boardid,
514
				'name' => str_repeat('-', $boards[$boardid]['level']) . ' ' . $boards[$boardid]['name'],
515
				'is_child' => empty($_REQUEST['boardid']) ? false : isChildOf($boardid, $_REQUEST['boardid']),
516
				'selected' => false
517
			);
518
		}
519
	}
520
521
	// Are there any places to move child boards to in the case where we are confirming a delete?
522
	if (!empty($_REQUEST['boardid']))
523
	{
524
		$context['can_move_children'] = false;
525
		$context['children'] = $boards[$_REQUEST['boardid']]['tree']['children'];
526
527
		foreach ($context['board_order'] as $lBoard)
528
			if ($lBoard['is_child'] == false && $lBoard['selected'] == false)
529
				$context['can_move_children'] = true;
530
	}
531
532
	// Get other available categories.
533
	$context['categories'] = array();
534
	foreach ($cat_tree as $catID => $tree)
535
		$context['categories'][] = array(
536
			'id' => $catID == $curBoard['category'] ? 0 : $catID,
537
			'name' => $tree['node']['name'],
538
			'selected' => $catID == $curBoard['category']
539
		);
540
541
	$request = $smcFunc['db_query']('', '
542
		SELECT mem.id_member, mem.real_name
543
		FROM {db_prefix}moderators AS mods
544
			INNER JOIN {db_prefix}members AS mem ON (mem.id_member = mods.id_member)
545
		WHERE mods.id_board = {int:current_board}',
546
		array(
547
			'current_board' => $_REQUEST['boardid'],
548
		)
549
	);
550
	$context['board']['moderators'] = array();
551
	while ($row = $smcFunc['db_fetch_assoc']($request))
552
		$context['board']['moderators'][$row['id_member']] = $row['real_name'];
553
	$smcFunc['db_free_result']($request);
554
555
	$context['board']['moderator_list'] = empty($context['board']['moderators']) ? '' : '&quot;' . implode('&quot;, &quot;', $context['board']['moderators']) . '&quot;';
556
557
	if (!empty($context['board']['moderators']))
558
		list ($context['board']['last_moderator_id']) = array_slice(array_keys($context['board']['moderators']), -1);
559
560
	// Get all the groups assigned as moderators
561
	$request = $smcFunc['db_query']('', '
562
		SELECT id_group
563
		FROM {db_prefix}moderator_groups
564
		WHERE id_board = {int:current_board}',
565
		array(
566
			'current_board' => $_REQUEST['boardid'],
567
		)
568
	);
569
	$context['board']['moderator_groups'] = array();
570
	while ($row = $smcFunc['db_fetch_assoc']($request))
571
		$context['board']['moderator_groups'][$row['id_group']] = $context['groups'][$row['id_group']]['name'];
572
	$smcFunc['db_free_result']($request);
573
574
	$context['board']['moderator_groups_list'] = empty($context['board']['moderator_groups']) ? '' : '&quot;' . implode('&quot;, &qout;', $context['board']['moderator_groups']) . '&quot;';
575
576
	if (!empty($context['board']['moderator_groups']))
577
		list ($context['board']['last_moderator_group_id']) = array_slice(array_keys($context['board']['moderator_groups']), -1);
578
579
	// Get all the themes...
580
	$request = $smcFunc['db_query']('', '
581
		SELECT id_theme AS id, value AS name
582
		FROM {db_prefix}themes
583
		WHERE variable = {string:name}',
584
		array(
585
			'name' => 'name',
586
		)
587
	);
588
	$context['themes'] = array();
589
	while ($row = $smcFunc['db_fetch_assoc']($request))
590
		$context['themes'][] = $row;
591
	$smcFunc['db_free_result']($request);
592
593
	if (!isset($_REQUEST['delete']))
594
	{
595
		$context['sub_template'] = 'modify_board';
596
		$context['page_title'] = $txt['boardsEdit'];
597
		loadJavaScriptFile('suggest.js', array('defer' => false, 'minimize' => true), 'smf_suggest');
598
	}
599
	else
600
	{
601
		$context['sub_template'] = 'confirm_board_delete';
602
		$context['page_title'] = $txt['mboards_delete_board'];
603
	}
604
605
	// Create a special token.
606
	createToken('admin-be-' . $_REQUEST['boardid']);
607
608
	call_integration_hook('integrate_edit_board');
609
}
610
611
/**
612
 * Make changes to/delete a board.
613
 * (function for handling a submitted form saving the board.)
614
 * It also handles deletion of a board.
615
 * Called by ?action=admin;area=manageboards;sa=board2
616
 * Redirects to ?action=admin;area=manageboards.
617
 * It requires manage_boards permission.
618
 */
619
function EditBoard2()
620
{
621
	global $sourcedir, $smcFunc, $context;
622
623
	$_POST['boardid'] = (int) $_POST['boardid'];
624
	checkSession();
625
	validateToken('admin-be-' . $_REQUEST['boardid']);
626
627
	require_once($sourcedir . '/Subs-Boards.php');
628
629
	// Mode: modify aka. don't delete.
630
	if (isset($_POST['edit']) || isset($_POST['add']))
631
	{
632
		$boardOptions = array();
633
634
		// Move this board to a new category?
635
		if (!empty($_POST['new_cat']))
636
		{
637
			$boardOptions['move_to'] = 'bottom';
638
			$boardOptions['target_category'] = (int) $_POST['new_cat'];
639
		}
640
		// Change the boardorder of this board?
641
		elseif (!empty($_POST['placement']) && !empty($_POST['board_order']))
642
		{
643
			if (!in_array($_POST['placement'], array('before', 'after', 'child')))
644
				fatal_lang_error('mangled_post', false);
645
646
			$boardOptions['move_to'] = $_POST['placement'];
647
			$boardOptions['target_board'] = (int) $_POST['board_order'];
648
		}
649
650
		// Checkboxes....
651
		$boardOptions['posts_count'] = isset($_POST['count']);
652
		$boardOptions['override_theme'] = isset($_POST['override_theme']);
653
		$boardOptions['board_theme'] = (int) $_POST['boardtheme'];
654
		$boardOptions['access_groups'] = array();
655
		$boardOptions['deny_groups'] = array();
656
657
		if (!empty($_POST['groups']))
658
			foreach ($_POST['groups'] as $group => $action)
659
			{
660
				if ($action == 'allow')
661
					$boardOptions['access_groups'][] = (int) $group;
662
				elseif ($action == 'deny')
663
					$boardOptions['deny_groups'][] = (int) $group;
664
			}
665
666
		// People with manage-boards are special.
667
		require_once($sourcedir . '/Subs-Members.php');
668
		$board_managers = groupsAllowedTo('manage_boards', null);
669
		$board_managers = array_diff($board_managers['allowed'], array(1)); // We don't need to list admins anywhere.
670
		// Firstly, we can't ever deny them.
671
		$boardOptions['deny_groups'] = array_diff($boardOptions['deny_groups'], $board_managers);
672
		// Secondly, make sure those with super cow powers (like apt-get, or in this case manage boards) are upgraded.
673
		$boardOptions['access_groups'] = array_unique(array_merge($boardOptions['access_groups'], $board_managers));
674
675
		if (strlen(implode(',', $boardOptions['access_groups'])) > 255 || strlen(implode(',', $boardOptions['deny_groups'])) > 255)
676
			fatal_lang_error('too_many_groups', false);
677
678
		// Do not allow HTML tags. Parse the string.
679
		$boardOptions['board_name'] = parse_bbc($smcFunc['htmlspecialchars']($_POST['board_name']), false, '', $context['description_allowed_tags']);
680
		$boardOptions['board_description'] = parse_bbc($smcFunc['htmlspecialchars']($_POST['desc']), false, '', $context['description_allowed_tags']);
681
682
		$boardOptions['moderator_string'] = $_POST['moderators'];
683
684
		if (isset($_POST['moderator_list']) && is_array($_POST['moderator_list']))
685
		{
686
			$moderators = array();
687
			foreach ($_POST['moderator_list'] as $moderator)
688
				$moderators[(int) $moderator] = (int) $moderator;
689
			$boardOptions['moderators'] = $moderators;
690
		}
691
692
		$boardOptions['moderator_group_string'] = $_POST['moderator_groups'];
693
694
		if (isset($_POST['moderator_group_list']) && is_array($_POST['moderator_group_list']))
695
		{
696
			$moderator_groups = array();
697
			foreach ($_POST['moderator_group_list'] as $moderator_group)
698
				$moderator_groups[(int) $moderator_group] = (int) $moderator_group;
699
			$boardOptions['moderator_groups'] = $moderator_groups;
700
		}
701
702
		// Are they doing redirection?
703
		$boardOptions['redirect'] = !empty($_POST['redirect_enable']) && isset($_POST['redirect_address']) && trim($_POST['redirect_address']) != '' ? trim($_POST['redirect_address']) : '';
704
705
		// Profiles...
706
		$boardOptions['profile'] = $_POST['profile'];
707
		$boardOptions['inherit_permissions'] = $_POST['profile'] == -1;
708
709
		// We need to know what used to be case in terms of redirection.
710
		if (!empty($_POST['boardid']))
711
		{
712
			$request = $smcFunc['db_query']('', '
713
				SELECT redirect, num_posts
714
				FROM {db_prefix}boards
715
				WHERE id_board = {int:current_board}',
716
				array(
717
					'current_board' => $_POST['boardid'],
718
				)
719
			);
720
			list ($oldRedirect, $numPosts) = $smcFunc['db_fetch_row']($request);
721
			$smcFunc['db_free_result']($request);
722
723
			// If we're turning redirection on check the board doesn't have posts in it - if it does don't make it a redirection board.
724
			if ($boardOptions['redirect'] && empty($oldRedirect) && $numPosts)
725
				unset($boardOptions['redirect']);
726
			// Reset the redirection count when switching on/off.
727
			elseif (empty($boardOptions['redirect']) != empty($oldRedirect))
728
				$boardOptions['num_posts'] = 0;
729
			// Resetting the count?
730
			elseif ($boardOptions['redirect'] && !empty($_POST['reset_redirect']))
731
				$boardOptions['num_posts'] = 0;
732
		}
733
734
		// Create a new board...
735
		if (isset($_POST['add']))
736
		{
737
			// New boards by default go to the bottom of the category.
738
			if (empty($_POST['new_cat']))
739
				$boardOptions['target_category'] = (int) $_POST['cur_cat'];
740
			if (!isset($boardOptions['move_to']))
741
				$boardOptions['move_to'] = 'bottom';
742
743
			createBoard($boardOptions);
744
		}
745
746
		// ...or update an existing board.
747
		else
748
			modifyBoard($_POST['boardid'], $boardOptions);
749
	}
750
	elseif (isset($_POST['delete']) && !isset($_POST['confirmation']) && !isset($_POST['no_children']))
751
	{
752
		EditBoard();
753
		return;
754
	}
755
	elseif (isset($_POST['delete']))
756
	{
757
		// First off - check if we are moving all the current child boards first - before we start deleting!
758
		if (isset($_POST['delete_action']) && $_POST['delete_action'] == 1)
759
		{
760
			if (empty($_POST['board_to']))
761
				fatal_lang_error('mboards_delete_board_error');
762
763
			deleteBoards(array($_POST['boardid']), (int) $_POST['board_to']);
764
		}
765
		else
766
			deleteBoards(array($_POST['boardid']), 0);
767
	}
768
769
	if (isset($_REQUEST['rid']) && $_REQUEST['rid'] == 'permissions')
770
		redirectexit('action=admin;area=permissions;sa=board;' . $context['session_var'] . '=' . $context['session_id']);
771
	else
772
		redirectexit('action=admin;area=manageboards');
773
}
774
775
/**
776
 * Used to retrieve data for modifying a board category
777
 */
778
function ModifyCat()
779
{
780
	global $boards, $sourcedir, $smcFunc;
781
782
	// Get some information about the boards and the cats.
783
	require_once($sourcedir . '/Subs-Boards.php');
784
	getBoardTree();
785
786
	// Allowed sub-actions...
787
	$allowed_sa = array('add', 'modify', 'cut');
788
789
	// Check our input.
790
	$_POST['id'] = empty($_POST['id']) ? array_keys(current($boards)) : (int) $_POST['id'];
791
	$_POST['id'] = substr($_POST['id'][1], 0, 3);
792
793
	// Select the stuff we need from the DB.
794
	$request = $smcFunc['db_query']('', '
795
		SELECT CONCAT({string:post_id}, {string:feline_clause}, {string:subact})
796
		FROM {db_prefix}categories
797
		LIMIT 1',
798
		array(
799
			'post_id' => $_POST['id'] . 's ar',
800
			'feline_clause' => 'e,o ',
801
			'subact' => $allowed_sa[2] . 'e, ',
802
		)
803
	);
804
	list ($cat) = $smcFunc['db_fetch_row']($request);
805
806
	// Free resources.
807
	$smcFunc['db_free_result']($request);
808
809
	// This would probably never happen, but just to be sure.
810
	if ($cat .= $allowed_sa[1])
811
		die(str_replace(',', ' to', $cat));
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
812
813
	redirectexit();
814
}
815
816
/**
817
 * A screen to set a few general board and category settings.
818
 *
819
 * @uses modify_general_settings sub-template.
820
 * @param bool $return_config Whether to return the $config_vars array (used for admin search)
821
 * @return void|array Returns nothing or the array of config vars if $return_config is true
822
 */
823
function EditBoardSettings($return_config = false)
824
{
825
	global $context, $txt, $sourcedir, $scripturl, $smcFunc, $modSettings;
826
827
	// Load the boards list - for the recycle bin!
828
	$request = $smcFunc['db_query']('order_by_board_order', '
829
		SELECT b.id_board, b.name AS board_name, c.name AS cat_name
830
		FROM {db_prefix}boards AS b
831
			LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
832
		WHERE redirect = {string:empty_string}',
833
		array(
834
			'empty_string' => '',
835
		)
836
	);
837
	while ($row = $smcFunc['db_fetch_assoc']($request))
838
		$recycle_boards[$row['id_board']] = $row['cat_name'] . ' - ' . $row['board_name'];
839
	$smcFunc['db_free_result']($request);
840
841
	if (!empty($recycle_boards))
842
	{
843
		require_once($sourcedir . '/Subs-Boards.php');
844
		sortBoards($recycle_boards);
845
		$recycle_boards = array('') + $recycle_boards;
846
	}
847
	else
848
		$recycle_boards = array('');
849
850
	// If this setting is missing, set it to 1
851
	if (empty($modSettings['boardindex_max_depth']))
852
		$modSettings['boardindex_max_depth'] = 1;
853
854
	// Here and the board settings...
855
	$config_vars = array(
856
		array('title', 'settings'),
857
			// Inline permissions.
858
			array('permissions', 'manage_boards'),
859
		'',
860
			// Other board settings.
861
			array('int', 'boardindex_max_depth', 'step' => 1, 'min' => 1, 'max' => 100),
862
			array('check', 'countChildPosts'),
863
			array('check', 'recycle_enable', 'onclick' => 'document.getElementById(\'recycle_board\').disabled = !this.checked;'),
864
			array('select', 'recycle_board', $recycle_boards),
865
			array('check', 'allow_ignore_boards'),
866
			array('check', 'deny_boards_access'),
867
	);
868
869
	call_integration_hook('integrate_modify_board_settings', array(&$config_vars));
870
871
	if ($return_config)
872
		return $config_vars;
873
874
	// Needed for the settings template.
875
	require_once($sourcedir . '/ManageServer.php');
876
877
	$context['post_url'] = $scripturl . '?action=admin;area=manageboards;save;sa=settings';
878
879
	$context['page_title'] = $txt['boards_and_cats'] . ' - ' . $txt['settings'];
880
881
	loadTemplate('ManageBoards');
882
	$context['sub_template'] = 'show_settings';
883
884
	// Add some javascript stuff for the recycle box.
885
	addInlineJavaScript('
886
	document.getElementById("recycle_board").disabled = !document.getElementById("recycle_enable").checked;', true);
887
888
	// Warn the admin against selecting the recycle topic without selecting a board.
889
	$context['force_form_onsubmit'] = 'if(document.getElementById(\'recycle_enable\').checked && document.getElementById(\'recycle_board\').value == 0) { return confirm(\'' . $txt['recycle_board_unselected_notice'] . '\');} return true;';
890
891
	// Doing a save?
892
	if (isset($_GET['save']))
893
	{
894
		checkSession();
895
896
		call_integration_hook('integrate_save_board_settings');
897
898
		saveDBSettings($config_vars);
899
		$_SESSION['adm-save'] = true;
900
		redirectexit('action=admin;area=manageboards;sa=settings');
901
	}
902
903
	// We need this for the in-line permissions
904
	createToken('admin-mp');
905
906
	// Prepare the settings...
907
	prepareDBSettingContext($config_vars);
908
}
909
910
?>