Issues (1065)

Sources/Subs-Boards.php (2 issues)

1
<?php
2
3
/**
4
 * This file is mainly concerned with minor tasks relating to boards, such as
5
 * marking them read, collapsing categories, or quick moderation.
6
 *
7
 * Simple Machines Forum (SMF)
8
 *
9
 * @package SMF
10
 * @author Simple Machines https://www.simplemachines.org
11
 * @copyright 2025 Simple Machines and individual contributors
12
 * @license https://www.simplemachines.org/about/smf/license.php BSD
13
 *
14
 * @version 2.1.5
15
 */
16
17
if (!defined('SMF'))
18
	die('No direct access...');
19
20
/**
21
 * Mark a board or multiple boards read.
22
 *
23
 * @param int|array $boards The ID of a single board or an array of boards
24
 * @param bool $unread Whether we're marking them as unread
25
 */
26
function markBoardsRead($boards, $unread = false)
27
{
28
	global $user_info, $modSettings, $smcFunc;
29
30
	// Force $boards to be an array.
31
	if (!is_array($boards))
32
		$boards = array($boards);
33
	else
34
		$boards = array_unique($boards);
35
36
	// No boards, nothing to mark as read.
37
	if (empty($boards))
38
		return;
39
40
	// Allow the user to mark a board as unread.
41
	if ($unread)
42
	{
43
		// Clear out all the places where this lovely info is stored.
44
		// @todo Maybe not log_mark_read?
45
		$smcFunc['db_query']('', '
46
			DELETE FROM {db_prefix}log_mark_read
47
			WHERE id_board IN ({array_int:board_list})
48
				AND id_member = {int:current_member}',
49
			array(
50
				'current_member' => $user_info['id'],
51
				'board_list' => $boards,
52
			)
53
		);
54
		$smcFunc['db_query']('', '
55
			DELETE FROM {db_prefix}log_boards
56
			WHERE id_board IN ({array_int:board_list})
57
				AND id_member = {int:current_member}',
58
			array(
59
				'current_member' => $user_info['id'],
60
				'board_list' => $boards,
61
			)
62
		);
63
	}
64
	// Otherwise mark the board as read.
65
	else
66
	{
67
		$markRead = array();
68
		foreach ($boards as $board)
69
			$markRead[] = array($modSettings['maxMsgID'], $user_info['id'], $board);
70
71
		// Update log_mark_read and log_boards.
72
		$smcFunc['db_insert']('replace',
73
			'{db_prefix}log_mark_read',
74
			array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'),
75
			$markRead,
76
			array('id_board', 'id_member')
77
		);
78
79
		$smcFunc['db_insert']('replace',
80
			'{db_prefix}log_boards',
81
			array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'),
82
			$markRead,
83
			array('id_board', 'id_member')
84
		);
85
	}
86
87
	// Get rid of useless log_topics data, because log_mark_read is better for it - even if marking unread - I think so...
88
	// @todo look at this...
89
	// The call to markBoardsRead() in Display() used to be simply
90
	// marking log_boards (the previous query only)
91
	$result = $smcFunc['db_query']('', '
92
		SELECT MIN(id_topic)
93
		FROM {db_prefix}log_topics
94
		WHERE id_member = {int:current_member}',
95
		array(
96
			'current_member' => $user_info['id'],
97
		)
98
	);
99
	list ($lowest_topic) = $smcFunc['db_fetch_row']($result);
100
	$smcFunc['db_free_result']($result);
101
102
	if (empty($lowest_topic))
103
		return;
104
105
	// @todo SLOW This query seems to eat it sometimes.
106
	$result = $smcFunc['db_query']('', '
107
		SELECT lt.id_topic
108
		FROM {db_prefix}log_topics AS lt
109
			INNER JOIN {db_prefix}topics AS t /*!40000 USE INDEX (PRIMARY) */ ON (t.id_topic = lt.id_topic
110
				AND t.id_board IN ({array_int:board_list}))
111
		WHERE lt.id_member = {int:current_member}
112
			AND lt.id_topic >= {int:lowest_topic}
113
			AND lt.unwatched != 1',
114
		array(
115
			'current_member' => $user_info['id'],
116
			'board_list' => $boards,
117
			'lowest_topic' => $lowest_topic,
118
		)
119
	);
120
	$topics = array();
121
	while ($row = $smcFunc['db_fetch_assoc']($result))
122
		$topics[] = $row['id_topic'];
123
	$smcFunc['db_free_result']($result);
124
125
	if (!empty($topics))
126
		$smcFunc['db_query']('', '
127
			DELETE FROM {db_prefix}log_topics
128
			WHERE id_member = {int:current_member}
129
				AND id_topic IN ({array_int:topic_list})',
130
			array(
131
				'current_member' => $user_info['id'],
132
				'topic_list' => $topics,
133
			)
134
		);
135
}
136
137
/**
138
 * Mark one or more boards as read.
139
 */
140
function MarkRead()
141
{
142
	global $board, $topic, $user_info, $board_info, $modSettings, $smcFunc;
143
144
	// No Guests allowed!
145
	is_not_guest();
146
147
	checkSession('get');
148
149
	if (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'all')
150
	{
151
		// Find all the boards this user can see.
152
		$result = $smcFunc['db_query']('', '
153
			SELECT b.id_board
154
			FROM {db_prefix}boards AS b
155
			WHERE {query_see_board}',
156
			array(
157
			)
158
		);
159
		$boards = array();
160
		while ($row = $smcFunc['db_fetch_assoc']($result))
161
			$boards[] = $row['id_board'];
162
		$smcFunc['db_free_result']($result);
163
164
		if (!empty($boards))
165
			markBoardsRead($boards, isset($_REQUEST['unread']));
166
167
		$_SESSION['id_msg_last_visit'] = $modSettings['maxMsgID'];
168
		if (!empty($_SESSION['old_url']) && strpos($_SESSION['old_url'], 'action=unread') !== false)
169
			redirectexit('action=unread');
170
171
		if (isset($_SESSION['topicseen_cache']))
172
			$_SESSION['topicseen_cache'] = array();
173
174
		redirectexit();
175
	}
176
	elseif (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'unreadreplies')
177
	{
178
		// Make sure all the topics are integers!
179
		$topics = array_map('intval', explode('-', $_REQUEST['topics']));
180
181
		$request = $smcFunc['db_query']('', '
182
			SELECT id_topic, unwatched
183
			FROM {db_prefix}log_topics
184
			WHERE id_topic IN ({array_int:selected_topics})
185
				AND id_member = {int:current_user}',
186
			array(
187
				'selected_topics' => $topics,
188
				'current_user' => $user_info['id'],
189
			)
190
		);
191
		$logged_topics = array();
192
		while ($row = $smcFunc['db_fetch_assoc']($request))
193
			$logged_topics[$row['id_topic']] = $row['unwatched'];
194
		$smcFunc['db_free_result']($request);
195
196
		$markRead = array();
197
		foreach ($topics as $id_topic)
198
			$markRead[] = array($modSettings['maxMsgID'], $user_info['id'], $id_topic, (isset($logged_topics[$topic]) ? $logged_topics[$topic] : 0));
199
200
		$smcFunc['db_insert']('replace',
201
			'{db_prefix}log_topics',
202
			array('id_msg' => 'int', 'id_member' => 'int', 'id_topic' => 'int', 'unwatched' => 'int'),
203
			$markRead,
204
			array('id_member', 'id_topic')
205
		);
206
207
		if (isset($_SESSION['topicseen_cache']))
208
			$_SESSION['topicseen_cache'] = array();
209
210
		redirectexit('action=unreadreplies');
211
	}
212
213
	// Special case: mark a topic unread!
214
	elseif (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'topic')
215
	{
216
		// First, let's figure out what the latest message is.
217
		$result = $smcFunc['db_query']('', '
218
			SELECT t.id_first_msg, t.id_last_msg, COALESCE(lt.unwatched, 0) as unwatched
219
			FROM {db_prefix}topics as t
220
				LEFT JOIN {db_prefix}log_topics as lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
221
			WHERE t.id_topic = {int:current_topic}',
222
			array(
223
				'current_topic' => $topic,
224
				'current_member' => $user_info['id'],
225
			)
226
		);
227
		$topicinfo = $smcFunc['db_fetch_assoc']($result);
228
		$smcFunc['db_free_result']($result);
229
230
		if (!empty($_GET['t']))
231
		{
232
			// If they read the whole topic, go back to the beginning.
233
			if ($_GET['t'] >= $topicinfo['id_last_msg'])
234
				$earlyMsg = 0;
235
			// If they want to mark the whole thing read, same.
236
			elseif ($_GET['t'] <= $topicinfo['id_first_msg'])
237
				$earlyMsg = 0;
238
			// Otherwise, get the latest message before the named one.
239
			else
240
			{
241
				$result = $smcFunc['db_query']('', '
242
					SELECT MAX(id_msg)
243
					FROM {db_prefix}messages
244
					WHERE id_topic = {int:current_topic}
245
						AND id_msg >= {int:id_first_msg}
246
						AND id_msg < {int:topic_msg_id}',
247
					array(
248
						'current_topic' => $topic,
249
						'topic_msg_id' => (int) $_GET['t'],
250
						'id_first_msg' => $topicinfo['id_first_msg'],
251
					)
252
				);
253
				list ($earlyMsg) = $smcFunc['db_fetch_row']($result);
254
				$smcFunc['db_free_result']($result);
255
			}
256
		}
257
		// Marking read from first page?  That's the whole topic.
258
		elseif ($_REQUEST['start'] == 0)
259
			$earlyMsg = 0;
260
		else
261
		{
262
			$result = $smcFunc['db_query']('', '
263
				SELECT id_msg
264
				FROM {db_prefix}messages
265
				WHERE id_topic = {int:current_topic}
266
				ORDER BY id_msg
267
				LIMIT {int:start}, 1',
268
				array(
269
					'current_topic' => $topic,
270
					'start' => (int) $_REQUEST['start'],
271
				)
272
			);
273
			list ($earlyMsg) = $smcFunc['db_fetch_row']($result);
274
			$smcFunc['db_free_result']($result);
275
276
			$earlyMsg--;
277
		}
278
279
		// Blam, unread!
280
		$smcFunc['db_insert']('replace',
281
			'{db_prefix}log_topics',
282
			array('id_msg' => 'int', 'id_member' => 'int', 'id_topic' => 'int', 'unwatched' => 'int'),
283
			array($earlyMsg, $user_info['id'], $topic, $topicinfo['unwatched']),
284
			array('id_member', 'id_topic')
285
		);
286
287
		redirectexit('board=' . $board . '.0');
288
	}
289
	else
290
	{
291
		$categories = array();
292
		$boards = array();
293
294
		if (isset($_REQUEST['c']))
295
		{
296
			$_REQUEST['c'] = explode(',', $_REQUEST['c']);
297
			foreach ($_REQUEST['c'] as $c)
298
				$categories[] = (int) $c;
299
		}
300
		if (isset($_REQUEST['boards']))
301
		{
302
			$_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
303
			foreach ($_REQUEST['boards'] as $b)
304
				$boards[] = (int) $b;
305
		}
306
		if (!empty($board))
307
			$boards[] = (int) $board;
308
309
		if (isset($_REQUEST['children']) && !empty($boards))
310
		{
311
			// They want to mark the entire tree starting with the boards specified
312
			// The easiest thing is to just get all the boards they can see, but since we've specified the top of tree we ignore some of them
313
314
			$request = $smcFunc['db_query']('', '
315
				SELECT b.id_board, b.id_parent
316
				FROM {db_prefix}boards AS b
317
				WHERE {query_see_board}
318
					AND b.child_level > {int:no_parents}
319
					AND b.id_board NOT IN ({array_int:board_list})
320
				ORDER BY child_level ASC',
321
				array(
322
					'no_parents' => 0,
323
					'board_list' => $boards,
324
				)
325
			);
326
			while ($row = $smcFunc['db_fetch_assoc']($request))
327
				if (in_array($row['id_parent'], $boards))
328
					$boards[] = $row['id_board'];
329
330
			$smcFunc['db_free_result']($request);
331
		}
332
333
		$clauses = array();
334
		$clauseParameters = array();
335
		if (!empty($categories))
336
		{
337
			$clauses[] = 'id_cat IN ({array_int:category_list})';
338
			$clauseParameters['category_list'] = $categories;
339
		}
340
		if (!empty($boards))
341
		{
342
			$clauses[] = 'id_board IN ({array_int:board_list})';
343
			$clauseParameters['board_list'] = $boards;
344
		}
345
346
		if (empty($clauses))
347
			redirectexit();
348
349
		$request = $smcFunc['db_query']('', '
350
			SELECT b.id_board
351
			FROM {db_prefix}boards AS b
352
			WHERE {query_see_board}
353
				AND b.' . implode(' OR b.', $clauses),
354
			array_merge($clauseParameters, array(
355
			))
356
		);
357
		$boards = array();
358
		while ($row = $smcFunc['db_fetch_assoc']($request))
359
			$boards[] = $row['id_board'];
360
		$smcFunc['db_free_result']($request);
361
362
		if (empty($boards))
363
			redirectexit();
364
365
		markBoardsRead($boards, isset($_REQUEST['unread']));
366
367
		foreach ($boards as $b)
368
		{
369
			if (isset($_SESSION['topicseen_cache'][$b]))
370
				$_SESSION['topicseen_cache'][$b] = array();
371
		}
372
373
		if (!isset($_REQUEST['unread']))
374
		{
375
			// Find all the boards this user can see.
376
			$result = $smcFunc['db_query']('', '
377
				SELECT b.id_board
378
				FROM {db_prefix}boards AS b
379
				WHERE b.id_parent IN ({array_int:parent_list})
380
					AND {query_see_board}',
381
				array(
382
					'parent_list' => $boards,
383
				)
384
			);
385
			if ($smcFunc['db_num_rows']($result) > 0)
386
			{
387
				$logBoardInserts = array();
388
				while ($row = $smcFunc['db_fetch_assoc']($result))
389
					$logBoardInserts[] = array($modSettings['maxMsgID'], $user_info['id'], $row['id_board']);
390
391
				$smcFunc['db_insert']('replace',
392
					'{db_prefix}log_boards',
393
					array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'),
394
					$logBoardInserts,
395
					array('id_member', 'id_board')
396
				);
397
			}
398
			$smcFunc['db_free_result']($result);
399
400
			if (empty($board))
401
				redirectexit();
402
			else
403
				redirectexit('board=' . $board . '.0');
404
		}
405
		else
406
		{
407
			if (empty($board_info['parent']))
408
				redirectexit();
409
			else
410
				redirectexit('board=' . $board_info['parent'] . '.0');
411
		}
412
	}
413
}
414
415
/**
416
 * Get the id_member associated with the specified message.
417
 *
418
 * @param int $messageID The ID of the message
419
 * @return int The ID of the member associated with that post
420
 */
421
function getMsgMemberID($messageID)
422
{
423
	global $smcFunc;
424
425
	// Find the topic and make sure the member still exists.
426
	$result = $smcFunc['db_query']('', '
427
		SELECT COALESCE(mem.id_member, 0)
428
		FROM {db_prefix}messages AS m
429
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
430
		WHERE m.id_msg = {int:selected_message}
431
		LIMIT 1',
432
		array(
433
			'selected_message' => (int) $messageID,
434
		)
435
	);
436
	if ($smcFunc['db_num_rows']($result) > 0)
437
		list ($memberID) = $smcFunc['db_fetch_row']($result);
438
	// The message doesn't even exist.
439
	else
440
		$memberID = 0;
441
442
	$smcFunc['db_free_result']($result);
443
444
	return (int) $memberID;
445
}
446
447
/**
448
 * Modify the settings and position of a board.
449
 * Used by ManageBoards.php to change the settings of a board.
450
 *
451
 * @param int $board_id The ID of the board
452
 * @param array &$boardOptions An array of options related to the board
453
 */
454
function modifyBoard($board_id, &$boardOptions)
455
{
456
	global $cat_tree, $boards, $smcFunc, $context, $txt;
457
458
	// Get some basic information about all boards and categories.
459
	getBoardTree();
460
461
	// Make sure given boards and categories exist.
462
	if (!isset($boards[$board_id]) || (isset($boardOptions['target_board']) && !isset($boards[$boardOptions['target_board']])) || (isset($boardOptions['target_category']) && !isset($cat_tree[$boardOptions['target_category']])))
463
		fatal_lang_error('no_board');
464
465
	$id = $board_id;
466
	call_integration_hook('integrate_pre_modify_board', array($id, &$boardOptions));
467
468
	// All things that will be updated in the database will be in $boardUpdates.
469
	$boardUpdates = array();
470
	$boardUpdateParameters = array();
471
472
	// In case the board has to be moved
473
	if (isset($boardOptions['move_to']))
474
	{
475
		// Move the board to the top of a given category.
476
		if ($boardOptions['move_to'] == 'top')
477
		{
478
			$id_cat = $boardOptions['target_category'];
479
			$child_level = 0;
480
			$id_parent = 0;
481
			$after = $cat_tree[$id_cat]['last_board_order'];
482
		}
483
484
		// Move the board to the bottom of a given category.
485
		elseif ($boardOptions['move_to'] == 'bottom')
486
		{
487
			$id_cat = $boardOptions['target_category'];
488
			$child_level = 0;
489
			$id_parent = 0;
490
			$after = 0;
491
			foreach ($cat_tree[$id_cat]['children'] as $id_board => $dummy)
492
				$after = max($after, $boards[$id_board]['order']);
493
		}
494
495
		// Make the board a child of a given board.
496
		elseif ($boardOptions['move_to'] == 'child')
497
		{
498
			$id_cat = $boards[$boardOptions['target_board']]['category'];
499
			$child_level = $boards[$boardOptions['target_board']]['level'] + 1;
500
			$id_parent = $boardOptions['target_board'];
501
502
			// People can be creative, in many ways...
503
			if (isChildOf($id_parent, $board_id))
504
				fatal_lang_error('mboards_parent_own_child_error', false);
505
506
			elseif ($id_parent == $board_id)
507
				fatal_lang_error('mboards_board_own_child_error', false);
508
509
			$after = $boards[$boardOptions['target_board']]['order'];
510
511
			// Check if there are already children and (if so) get the max board order.
512
			if (!empty($boards[$id_parent]['tree']['children']) && empty($boardOptions['move_first_child']))
513
				foreach ($boards[$id_parent]['tree']['children'] as $childBoard_id => $dummy)
514
					$after = max($after, $boards[$childBoard_id]['order']);
515
		}
516
517
		// Place a board before or after another board, on the same child level.
518
		elseif (in_array($boardOptions['move_to'], array('before', 'after')))
519
		{
520
			$id_cat = $boards[$boardOptions['target_board']]['category'];
521
			$child_level = $boards[$boardOptions['target_board']]['level'];
522
			$id_parent = $boards[$boardOptions['target_board']]['parent'];
523
			$after = $boards[$boardOptions['target_board']]['order'] - ($boardOptions['move_to'] == 'before' ? 1 : 0);
524
		}
525
526
		// Oops...?
527
		else
528
		{
529
			loadLanguage('Errors');
530
			throw new \Exception(sprintf($txt['modify_board_incorrect_move_to'], $boardOptions['move_to']));
531
		}
532
533
		// Get a list of children of this board.
534
		$childList = array();
535
		recursiveBoards($childList, $boards[$board_id]['tree']);
536
537
		// See if there are changes that affect children.
538
		$childUpdates = array();
539
		$levelDiff = $child_level - $boards[$board_id]['level'];
540
541
		if ($levelDiff != 0)
542
			$childUpdates[] = 'child_level = child_level ' . ($levelDiff > 0 ? '+ ' : '') . '{int:level_diff}';
543
544
		if ($id_cat != $boards[$board_id]['category'])
545
			$childUpdates[] = 'id_cat = {int:category}';
546
547
		// Fix the children of this board.
548
		if (!empty($childList) && !empty($childUpdates))
549
			$smcFunc['db_query']('', '
550
				UPDATE {db_prefix}boards
551
				SET ' . implode(',
552
					', $childUpdates) . '
553
				WHERE id_board IN ({array_int:board_list})',
554
				array(
555
					'board_list' => $childList,
556
					'category' => $id_cat,
557
					'level_diff' => $levelDiff,
558
				)
559
			);
560
561
		// Make some room for this spot.
562
		$smcFunc['db_query']('', '
563
			UPDATE {db_prefix}boards
564
			SET board_order = board_order + {int:new_order}
565
			WHERE board_order > {int:insert_after}
566
				AND id_board != {int:selected_board}',
567
			array(
568
				'insert_after' => $after,
569
				'selected_board' => $board_id,
570
				'new_order' => 1 + count($childList),
571
			)
572
		);
573
574
		$boardUpdates[] = 'id_cat = {int:id_cat}';
575
		$boardUpdates[] = 'id_parent = {int:id_parent}';
576
		$boardUpdates[] = 'child_level = {int:child_level}';
577
		$boardUpdates[] = 'board_order = {int:board_order}';
578
		$boardUpdateParameters += array(
579
			'id_cat' => $id_cat,
580
			'id_parent' => $id_parent,
581
			'child_level' => $child_level,
582
			'board_order' => $after + 1,
583
		);
584
	}
585
586
	// This setting is a little twisted in the database...
587
	if (isset($boardOptions['posts_count']))
588
	{
589
		$boardUpdates[] = 'count_posts = {int:count_posts}';
590
		$boardUpdateParameters['count_posts'] = $boardOptions['posts_count'] ? 0 : 1;
591
	}
592
593
	// Set the theme for this board.
594
	if (isset($boardOptions['board_theme']))
595
	{
596
		$boardUpdates[] = 'id_theme = {int:id_theme}';
597
		$boardUpdateParameters['id_theme'] = (int) $boardOptions['board_theme'];
598
	}
599
600
	// Should the board theme override the user preferred theme?
601
	if (isset($boardOptions['override_theme']))
602
	{
603
		$boardUpdates[] = 'override_theme = {int:override_theme}';
604
		$boardUpdateParameters['override_theme'] = $boardOptions['override_theme'] ? 1 : 0;
605
	}
606
607
	// Who's allowed to access this board.
608
	$board_permissions_inserts = array();
609
	if (isset($boardOptions['access_groups']))
610
	{
611
		foreach ($boardOptions['access_groups'] as $value)
612
			$board_permissions_inserts[] = array($value, $board_id, 0);
613
614
		$boardUpdates[] = 'member_groups = {string:member_groups}';
615
		$boardUpdateParameters['member_groups'] = implode(',', $boardOptions['access_groups']);
616
	}
617
618
	// And who isn't.
619
	if (isset($boardOptions['deny_groups']))
620
	{
621
		foreach ($boardOptions['deny_groups'] as $value)
622
			$board_permissions_inserts[] = array($value, $board_id, 1);
623
624
		$boardUpdates[] = 'deny_member_groups = {string:deny_groups}';
625
		$boardUpdateParameters['deny_groups'] = implode(',', $boardOptions['deny_groups']);
626
	}
627
628
	if (isset($boardOptions['board_name']))
629
	{
630
		$boardUpdates[] = 'name = {string:board_name}';
631
		$boardUpdateParameters['board_name'] = $boardOptions['board_name'];
632
	}
633
634
	if (isset($boardOptions['board_description']))
635
	{
636
		$boardUpdates[] = 'description = {string:board_description}';
637
		$boardUpdateParameters['board_description'] = $boardOptions['board_description'];
638
	}
639
640
	if (isset($boardOptions['profile']))
641
	{
642
		$boardUpdates[] = 'id_profile = {int:profile}';
643
		$boardUpdateParameters['profile'] = (int) $boardOptions['profile'];
644
	}
645
646
	if (isset($boardOptions['redirect']))
647
	{
648
		$boardUpdates[] = 'redirect = {string:redirect}';
649
		$boardUpdateParameters['redirect'] = $boardOptions['redirect'];
650
	}
651
652
	if (isset($boardOptions['num_posts']))
653
	{
654
		$boardUpdates[] = 'num_posts = {int:num_posts}';
655
		$boardUpdateParameters['num_posts'] = (int) $boardOptions['num_posts'];
656
	}
657
658
	$id = $board_id;
659
	call_integration_hook('integrate_modify_board', array($id, $boardOptions, &$boardUpdates, &$boardUpdateParameters));
660
661
	// Do the updates (if any).
662
	if (!empty($boardUpdates))
663
		$smcFunc['db_query']('', '
664
			UPDATE {db_prefix}boards
665
			SET
666
				' . implode(',
667
				', $boardUpdates) . '
668
			WHERE id_board = {int:selected_board}',
669
			array_merge($boardUpdateParameters, array(
670
				'selected_board' => $board_id,
671
			))
672
		);
673
674
	// Before we add new access_groups or deny_groups, remove all of the old entries
675
	if (isset($boardOptions['access_groups']) || isset($boardOptions['deny_groups']))
676
		$smcFunc['db_query']('', '
677
			DELETE FROM {db_prefix}board_permissions_view
678
			WHERE id_board = {int:selected_board}',
679
			array(
680
				'selected_board' => $board_id,
681
			)
682
		);
683
684
	if ($board_permissions_inserts != array())
685
		$smcFunc['db_insert']('insert',
686
			'{db_prefix}board_permissions_view',
687
			array('id_group' => 'int', 'id_board' => 'int', 'deny' => 'int'),
688
			$board_permissions_inserts,
689
			array('id_group', 'id_board', 'deny')
690
		);
691
692
	// Set moderators of this board.
693
	if (isset($boardOptions['moderators']) || isset($boardOptions['moderator_string']) || isset($boardOptions['moderator_groups']) || isset($boardOptions['moderator_group_string']))
694
	{
695
		// Reset current moderators for this board - if there are any!
696
		$smcFunc['db_query']('', '
697
			DELETE FROM {db_prefix}moderators
698
			WHERE id_board = {int:board_list}',
699
			array(
700
				'board_list' => $board_id,
701
			)
702
		);
703
704
		// Validate and get the IDs of the new moderators.
705
		if (isset($boardOptions['moderator_string']) && trim($boardOptions['moderator_string']) != '')
706
		{
707
			// Divvy out the usernames, remove extra space.
708
			$moderator_string = strtr($smcFunc['htmlspecialchars']($boardOptions['moderator_string'], ENT_QUOTES), array('&quot;' => '"'));
709
			preg_match_all('~"([^"]+)"~', $moderator_string, $matches);
710
			$moderators = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $moderator_string)));
711
			for ($k = 0, $n = count($moderators); $k < $n; $k++)
712
			{
713
				$moderators[$k] = trim($moderators[$k]);
714
715
				if (strlen($moderators[$k]) == 0)
716
					unset($moderators[$k]);
717
			}
718
719
			// Find all the id_member's for the member_name's in the list.
720
			if (empty($boardOptions['moderators']))
721
				$boardOptions['moderators'] = array();
722
			if (!empty($moderators))
723
			{
724
				$request = $smcFunc['db_query']('', '
725
					SELECT id_member
726
					FROM {db_prefix}members
727
					WHERE member_name IN ({array_string:moderator_list}) OR real_name IN ({array_string:moderator_list})
728
					LIMIT {int:limit}',
729
					array(
730
						'moderator_list' => $moderators,
731
						'limit' => count($moderators),
732
					)
733
				);
734
				while ($row = $smcFunc['db_fetch_assoc']($request))
735
					$boardOptions['moderators'][] = $row['id_member'];
736
				$smcFunc['db_free_result']($request);
737
			}
738
		}
739
740
		// Add the moderators to the board.
741
		if (!empty($boardOptions['moderators']))
742
		{
743
			$inserts = array();
744
			foreach ($boardOptions['moderators'] as $moderator)
745
				$inserts[] = array($board_id, $moderator);
746
747
			$smcFunc['db_insert']('insert',
748
				'{db_prefix}moderators',
749
				array('id_board' => 'int', 'id_member' => 'int'),
750
				$inserts,
751
				array('id_board', 'id_member')
752
			);
753
		}
754
755
		// Reset current moderator groups for this board - if there are any!
756
		$smcFunc['db_query']('', '
757
			DELETE FROM {db_prefix}moderator_groups
758
			WHERE id_board = {int:board_list}',
759
			array(
760
				'board_list' => $board_id,
761
			)
762
		);
763
764
		// Validate and get the IDs of the new moderator groups.
765
		if (isset($boardOptions['moderator_group_string']) && trim($boardOptions['moderator_group_string']) != '')
766
		{
767
			// Divvy out the group names, remove extra space.
768
			$moderator_group_string = strtr($smcFunc['htmlspecialchars']($boardOptions['moderator_group_string'], ENT_QUOTES), array('&quot;' => '"'));
769
			preg_match_all('~"([^"]+)"~', $moderator_group_string, $matches);
770
			$moderator_groups = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $moderator_group_string)));
771
			for ($k = 0, $n = count($moderator_groups); $k < $n; $k++)
772
			{
773
				$moderator_groups[$k] = trim($moderator_groups[$k]);
774
775
				if (strlen($moderator_groups[$k]) == 0)
776
					unset($moderator_groups[$k]);
777
			}
778
779
			/* 	Find all the id_group's for all the group names in the list
780
				But skip any invalid ones (invisible/post groups/Administrator/Moderator) */
781
			if (empty($boardOptions['moderator_groups']))
782
				$boardOptions['moderator_groups'] = array();
783
			if (!empty($moderator_groups))
784
			{
785
				$request = $smcFunc['db_query']('', '
786
					SELECT id_group
787
					FROM {db_prefix}membergroups
788
					WHERE group_name IN ({array_string:moderator_group_list})
789
						AND hidden = {int:visible}
790
						AND min_posts = {int:negative_one}
791
						AND id_group NOT IN ({array_int:invalid_groups})
792
					LIMIT {int:limit}',
793
					array(
794
						'visible' => 0,
795
						'negative_one' => -1,
796
						'invalid_groups' => array(1, 3),
797
						'moderator_group_list' => $moderator_groups,
798
						'limit' => count($moderator_groups),
799
					)
800
				);
801
				while ($row = $smcFunc['db_fetch_assoc']($request))
802
				{
803
					$boardOptions['moderator_groups'][] = $row['id_group'];
804
				}
805
				$smcFunc['db_free_result']($request);
806
			}
807
		}
808
809
		// Add the moderator groups to the board.
810
		if (!empty($boardOptions['moderator_groups']))
811
		{
812
			$inserts = array();
813
			foreach ($boardOptions['moderator_groups'] as $moderator_group)
814
				$inserts[] = array($board_id, $moderator_group);
815
816
			$smcFunc['db_insert']('insert',
817
				'{db_prefix}moderator_groups',
818
				array('id_board' => 'int', 'id_group' => 'int'),
819
				$inserts,
820
				array('id_board', 'id_group')
821
			);
822
		}
823
824
		// Note that caches can now be wrong!
825
		updateSettings(array('settings_updated' => time()));
826
	}
827
828
	if (isset($boardOptions['move_to']))
829
		reorderBoards();
830
831
	$parsed_boards_cat_id = isset($id_cat) ? $id_cat : $boardOptions['old_id_cat'];
832
	$already_parsed_boards = getBoardsParsedDescription($parsed_boards_cat_id);
833
834
	if (isset($boardOptions['board_description']))
835
		$already_parsed_boards[$board_id] = parse_bbc(
836
			$boardOptions['board_description'],
837
			false,
838
839
			'',
840
			$context['description_allowed_tags']);
841
842
	clean_cache('data');
843
844
	cache_put_data('parsed_boards_descriptions_'. $parsed_boards_cat_id, $already_parsed_boards, 864000);
845
846
	if (empty($boardOptions['dont_log']))
847
		logAction('edit_board', array('board' => $board_id), 'admin');
848
}
849
850
/**
851
 * Create a new board and set its properties and position.
852
 * Allows (almost) the same options as the modifyBoard() function.
853
 * With the option inherit_permissions set, the parent board permissions
854
 * will be inherited.
855
 *
856
 * @param array $boardOptions An array of information for the new board
857
 * @return int The ID of the new board
858
 */
859
function createBoard($boardOptions)
860
{
861
	global $boards, $smcFunc, $txt;
862
863
	// Trigger an error if one of the required values is not set.
864
	if (!isset($boardOptions['board_name']) || trim($boardOptions['board_name']) == '' || !isset($boardOptions['move_to']) || !isset($boardOptions['target_category']))
865
	{
866
		loadLanguage('Errors');
867
		throw new \Exception('create_board_missing_options');
868
	}
869
870
	if (in_array($boardOptions['move_to'], array('child', 'before', 'after')) && !isset($boardOptions['target_board']))
871
	{
872
		loadLanguage('Errors');
873
		throw new \Exception('move_board_no_target');
874
	}
875
876
	// Set every optional value to its default value.
877
	$boardOptions += array(
878
		'posts_count' => true,
879
		'override_theme' => false,
880
		'board_theme' => 0,
881
		'access_groups' => array(),
882
		'board_description' => '',
883
		'profile' => 1,
884
		'moderators' => '',
885
		'inherit_permissions' => true,
886
		'dont_log' => true,
887
	);
888
889
	$board_columns = array(
890
		'id_cat' => 'int', 'name' => 'string-255', 'description' => 'string', 'board_order' => 'int',
891
		'member_groups' => 'string', 'redirect' => 'string',
892
	);
893
	$board_parameters = array(
894
		$boardOptions['target_category'], $boardOptions['board_name'], '', 0,
895
		'', '',
896
	);
897
898
	call_integration_hook('integrate_create_board', array(&$boardOptions, &$board_columns, &$board_parameters));
899
900
	// Insert a board, the settings are dealt with later.
901
	$board_id = $smcFunc['db_insert']('',
902
		'{db_prefix}boards',
903
		$board_columns,
904
		$board_parameters,
905
		array('id_board'),
906
		1
907
	);
908
909
	if (empty($board_id))
910
		return 0;
911
912
	// Change the board according to the given specifications.
913
	modifyBoard($board_id, $boardOptions);
914
915
	// Do we want the parent permissions to be inherited?
916
	if ($boardOptions['inherit_permissions'])
917
	{
918
		getBoardTree();
919
920
		if (!empty($boards[$board_id]['parent']))
921
		{
922
			$request = $smcFunc['db_query']('', '
923
				SELECT id_profile
924
				FROM {db_prefix}boards
925
				WHERE id_board = {int:board_parent}
926
				LIMIT 1',
927
				array(
928
					'board_parent' => (int) $boards[$board_id]['parent'],
929
				)
930
			);
931
			list ($boardOptions['profile']) = $smcFunc['db_fetch_row']($request);
932
			$smcFunc['db_free_result']($request);
933
934
			$smcFunc['db_query']('', '
935
				UPDATE {db_prefix}boards
936
				SET id_profile = {int:new_profile}
937
				WHERE id_board = {int:current_board}',
938
				array(
939
					'new_profile' => $boardOptions['profile'],
940
					'current_board' => $board_id,
941
				)
942
			);
943
		}
944
	}
945
946
	// Clean the data cache.
947
	clean_cache('data');
948
949
	// Created it.
950
	logAction('add_board', array('board' => $board_id), 'admin');
951
952
	// Here you are, a new board, ready to be spammed.
953
	return $board_id;
954
}
955
956
/**
957
 * Remove one or more boards.
958
 * Allows to move the children of the board before deleting it
959
 * if moveChildrenTo is set to null, the child boards will be deleted.
960
 * Deletes:
961
 *   - all topics that are on the given boards;
962
 *   - all information that's associated with the given boards;
963
 * updates the statistics to reflect the new situation.
964
 *
965
 * @param array $boards_to_remove The boards to remove
966
 * @param int $moveChildrenTo The ID of the board to move the child boards to (null to remove the child boards, 0 to make them a top-level board)
967
 */
968
function deleteBoards($boards_to_remove, $moveChildrenTo = null)
969
{
970
	global $sourcedir, $boards, $smcFunc;
971
972
	// No boards to delete? Return!
973
	if (empty($boards_to_remove))
974
		return;
975
976
	getBoardTree();
977
978
	call_integration_hook('integrate_delete_board', array($boards_to_remove, &$moveChildrenTo));
979
980
	// If $moveChildrenTo is set to null, include the children in the removal.
981
	if ($moveChildrenTo === null)
982
	{
983
		// Get a list of the child boards that will also be removed.
984
		$child_boards_to_remove = array();
985
		foreach ($boards_to_remove as $board_to_remove)
986
			recursiveBoards($child_boards_to_remove, $boards[$board_to_remove]['tree']);
987
988
		// Merge the children with their parents.
989
		if (!empty($child_boards_to_remove))
990
			$boards_to_remove = array_unique(array_merge($boards_to_remove, $child_boards_to_remove));
991
	}
992
	// Move the children to a safe home.
993
	else
994
	{
995
		foreach ($boards_to_remove as $id_board)
996
		{
997
			// @todo Separate category?
998
			if ($moveChildrenTo === 0)
999
				fixChildren($id_board, 0, 0);
1000
			else
1001
				fixChildren($id_board, $boards[$moveChildrenTo]['level'] + 1, $moveChildrenTo);
1002
		}
1003
	}
1004
1005
	// Delete ALL topics in the selected boards (done first so topics can't be marooned.)
1006
	$request = $smcFunc['db_query']('', '
1007
		SELECT id_topic
1008
		FROM {db_prefix}topics
1009
		WHERE id_board IN ({array_int:boards_to_remove})',
1010
		array(
1011
			'boards_to_remove' => $boards_to_remove,
1012
		)
1013
	);
1014
	$topics = array();
1015
	while ($row = $smcFunc['db_fetch_assoc']($request))
1016
		$topics[] = $row['id_topic'];
1017
	$smcFunc['db_free_result']($request);
1018
1019
	require_once($sourcedir . '/RemoveTopic.php');
1020
	removeTopics($topics, false);
1021
1022
	// Delete the board's logs.
1023
	$smcFunc['db_query']('', '
1024
		DELETE FROM {db_prefix}log_mark_read
1025
		WHERE id_board IN ({array_int:boards_to_remove})',
1026
		array(
1027
			'boards_to_remove' => $boards_to_remove,
1028
		)
1029
	);
1030
	$smcFunc['db_query']('', '
1031
		DELETE FROM {db_prefix}log_boards
1032
		WHERE id_board IN ({array_int:boards_to_remove})',
1033
		array(
1034
			'boards_to_remove' => $boards_to_remove,
1035
		)
1036
	);
1037
	$smcFunc['db_query']('', '
1038
		DELETE FROM {db_prefix}log_notify
1039
		WHERE id_board IN ({array_int:boards_to_remove})',
1040
		array(
1041
			'boards_to_remove' => $boards_to_remove,
1042
		)
1043
	);
1044
1045
	// Delete this board's moderators.
1046
	$smcFunc['db_query']('', '
1047
		DELETE FROM {db_prefix}moderators
1048
		WHERE id_board IN ({array_int:boards_to_remove})',
1049
		array(
1050
			'boards_to_remove' => $boards_to_remove,
1051
		)
1052
	);
1053
1054
	// Delete this board's moderator groups.
1055
	$smcFunc['db_query']('', '
1056
		DELETE FROM {db_prefix}moderator_groups
1057
		WHERE id_board IN ({array_int:boards_to_remove})',
1058
		array(
1059
			'boards_to_remove' => $boards_to_remove,
1060
		)
1061
	);
1062
1063
	// Delete any extra events in the calendar.
1064
	$smcFunc['db_query']('', '
1065
		DELETE FROM {db_prefix}calendar
1066
		WHERE id_board IN ({array_int:boards_to_remove})',
1067
		array(
1068
			'boards_to_remove' => $boards_to_remove,
1069
		)
1070
	);
1071
1072
	// Delete any message icons that only appear on these boards.
1073
	$smcFunc['db_query']('', '
1074
		DELETE FROM {db_prefix}message_icons
1075
		WHERE id_board IN ({array_int:boards_to_remove})',
1076
		array(
1077
			'boards_to_remove' => $boards_to_remove,
1078
		)
1079
	);
1080
1081
	// Delete the boards.
1082
	$smcFunc['db_query']('', '
1083
		DELETE FROM {db_prefix}boards
1084
		WHERE id_board IN ({array_int:boards_to_remove})',
1085
		array(
1086
			'boards_to_remove' => $boards_to_remove,
1087
		)
1088
	);
1089
1090
	// Delete permissions
1091
	$smcFunc['db_query']('', '
1092
		DELETE FROM {db_prefix}board_permissions_view
1093
		WHERE id_board IN ({array_int:boards_to_remove})',
1094
		array(
1095
			'boards_to_remove' => $boards_to_remove,
1096
		)
1097
	);
1098
1099
	// Latest message/topic might not be there anymore.
1100
	updateStats('message');
1101
	updateStats('topic');
1102
	updateSettings(array(
1103
		'calendar_updated' => time(),
1104
	));
1105
1106
	// Plus reset the cache to stop people getting odd results.
1107
	updateSettings(array('settings_updated' => time()));
1108
1109
	// Clean the cache as well.
1110
	clean_cache('data');
1111
1112
	// Let's do some serious logging.
1113
	foreach ($boards_to_remove as $id_board)
1114
		logAction('delete_board', array('boardname' => $boards[$id_board]['name']), 'admin');
1115
1116
	reorderBoards();
1117
}
1118
1119
/**
1120
 * Put all boards in the right order and sorts the records of the boards table.
1121
 * Used by modifyBoard(), deleteBoards(), modifyCategory(), and deleteCategories() functions
1122
 */
1123
function reorderBoards()
1124
{
1125
	global $cat_tree, $boardList, $boards, $smcFunc;
1126
1127
	getBoardTree();
1128
1129
	// Set the board order for each category.
1130
	$board_order = 0;
1131
	foreach ($cat_tree as $catID => $dummy)
1132
	{
1133
		foreach ($boardList[$catID] as $boardID)
1134
			if ($boards[$boardID]['order'] != ++$board_order)
1135
				$smcFunc['db_query']('', '
1136
					UPDATE {db_prefix}boards
1137
					SET board_order = {int:new_order}
1138
					WHERE id_board = {int:selected_board}',
1139
					array(
1140
						'new_order' => $board_order,
1141
						'selected_board' => $boardID,
1142
					)
1143
				);
1144
	}
1145
1146
	// Empty the board order cache
1147
	cache_put_data('board_order', null, -3600);
1148
}
1149
1150
/**
1151
 * Fixes the children of a board by setting their child_levels to new values.
1152
 * Used when a board is deleted or moved, to affect its children.
1153
 *
1154
 * @param int $parent The ID of the parent board
1155
 * @param int $newLevel The new child level for each of the child boards
1156
 * @param int $newParent The ID of the new parent board
1157
 */
1158
function fixChildren($parent, $newLevel, $newParent)
1159
{
1160
	global $smcFunc;
1161
1162
	// Grab all children of $parent...
1163
	$result = $smcFunc['db_query']('', '
1164
		SELECT id_board
1165
		FROM {db_prefix}boards
1166
		WHERE id_parent = {int:parent_board}',
1167
		array(
1168
			'parent_board' => $parent,
1169
		)
1170
	);
1171
	$children = array();
1172
	while ($row = $smcFunc['db_fetch_assoc']($result))
1173
		$children[] = $row['id_board'];
1174
	$smcFunc['db_free_result']($result);
1175
1176
	// ...and set it to a new parent and child_level.
1177
	$smcFunc['db_query']('', '
1178
		UPDATE {db_prefix}boards
1179
		SET id_parent = {int:new_parent}, child_level = {int:new_child_level}
1180
		WHERE id_parent = {int:parent_board}',
1181
		array(
1182
			'new_parent' => $newParent,
1183
			'new_child_level' => $newLevel,
1184
			'parent_board' => $parent,
1185
		)
1186
	);
1187
1188
	// Recursively fix the children of the children.
1189
	foreach ($children as $child)
1190
		fixChildren($child, $newLevel + 1, $child);
1191
}
1192
1193
/**
1194
 * Tries to load up the entire board order and category very very quickly
1195
 * Returns an array with two elements, cats and boards
1196
 *
1197
 * @return array An array of categories and boards
1198
 */
1199
function getTreeOrder()
1200
{
1201
	global $smcFunc;
1202
1203
	static $tree_order = array(
1204
		'cats' => array(),
1205
		'boards' => array(),
1206
	);
1207
1208
	if (!empty($tree_order['boards']))
1209
		return $tree_order;
1210
1211
	if (($cached = cache_get_data('board_order', 86400)) !== null)
1212
	{
1213
		$tree_order = $cached;
1214
		return $cached;
1215
	}
1216
1217
	$request = $smcFunc['db_query']('', '
1218
		SELECT b.id_board, b.id_cat
1219
		FROM {db_prefix}categories AS c
1220
			JOIN {db_prefix}boards AS b ON (b.id_cat = c.id_cat)
1221
		ORDER BY c.cat_order, b.board_order'
1222
	);
1223
1224
	foreach ($smcFunc['db_fetch_all']($request) as $row)
1225
	{
1226
		if (!in_array($row['id_cat'], $tree_order['cats']))
1227
			$tree_order['cats'][] = $row['id_cat'];
1228
		$tree_order['boards'][] = $row['id_board'];
1229
	}
1230
	$smcFunc['db_free_result']($request);
1231
1232
	cache_put_data('board_order', $tree_order, 86400);
1233
1234
	return $tree_order;
1235
}
1236
1237
/**
1238
 * Takes a board array and sorts it
1239
 *
1240
 * @param array &$boards The boards
1241
 */
1242
function sortBoards(array &$boards)
1243
{
1244
	$tree = getTreeOrder();
1245
1246
	$ordered = array();
1247
	foreach ($tree['boards'] as $board)
1248
		if (!empty($boards[$board]))
1249
		{
1250
			$ordered[$board] = $boards[$board];
1251
1252
			if (is_array($ordered[$board]) && !empty($ordered[$board]['boards']))
1253
				sortBoards($ordered[$board]['boards']);
1254
1255
			if (is_array($ordered[$board]) && !empty($ordered[$board]['children']))
1256
				sortBoards($ordered[$board]['children']);
1257
		}
1258
1259
	$boards = $ordered;
1260
}
1261
1262
/**
1263
 * Takes a category array and sorts it
1264
 *
1265
 * @param array &$categories The categories
1266
 */
1267
function sortCategories(array &$categories)
1268
{
1269
	$tree = getTreeOrder();
1270
1271
	$ordered = array();
1272
	foreach ($tree['cats'] as $cat)
1273
		if (!empty($categories[$cat]))
1274
		{
1275
			$ordered[$cat] = $categories[$cat];
1276
			if (!empty($ordered[$cat]['boards']))
1277
				sortBoards($ordered[$cat]['boards']);
1278
		}
1279
1280
	$categories = $ordered;
1281
}
1282
1283
/**
1284
 * Returns the given board's moderators, with their names and links
1285
 *
1286
 * @param array $boards The boards to get moderators of
1287
 * @return array An array containing information about the moderators of each board
1288
 */
1289
function getBoardModerators(array $boards)
1290
{
1291
	global $smcFunc, $scripturl, $txt;
1292
1293
	if (empty($boards))
1294
		return array();
1295
1296
	$request = $smcFunc['db_query']('', '
1297
		SELECT mem.id_member, mem.real_name, mo.id_board
1298
		FROM {db_prefix}moderators AS mo
1299
			INNER JOIN {db_prefix}members AS mem ON (mem.id_member = mo.id_member)
1300
		WHERE mo.id_board IN ({array_int:boards})',
1301
		array(
1302
			'boards' => $boards,
1303
		)
1304
	);
1305
	$moderators = array();
1306
	while ($row = $smcFunc['db_fetch_assoc']($request))
1307
	{
1308
		if (empty($moderators[$row['id_board']]))
1309
			$moderators[$row['id_board']] = array();
1310
1311
		$moderators[$row['id_board']][] = array(
1312
			'id' => $row['id_member'],
1313
			'name' => $row['real_name'],
1314
			'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
1315
			'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '" title="' . $txt['board_moderator'] . '">' . $row['real_name'] . '</a>',
1316
		);
1317
	}
1318
	$smcFunc['db_free_result']($request);
1319
1320
	return $moderators;
1321
}
1322
1323
/**
1324
 * Returns board's moderator groups with their names and link
1325
 *
1326
 * @param array $boards The boards to get moderator groups of
1327
 * @return array An array containing information about the groups assigned to moderate each board
1328
 */
1329
function getBoardModeratorGroups(array $boards)
1330
{
1331
	global $smcFunc, $scripturl, $txt;
1332
1333
	if (empty($boards))
1334
		return array();
1335
1336
	$request = $smcFunc['db_query']('', '
1337
		SELECT mg.id_group, mg.group_name, bg.id_board
1338
		FROM {db_prefix}moderator_groups AS bg
1339
			INNER JOIN {db_prefix}membergroups AS mg ON (mg.id_group = bg.id_group)
1340
		WHERE bg.id_board IN ({array_int:boards})',
1341
		array(
1342
			'boards' => $boards,
1343
		)
1344
	);
1345
	$groups = array();
1346
	while ($row = $smcFunc['db_fetch_assoc']($request))
1347
	{
1348
		if (empty($groups[$row['id_board']]))
1349
			$groups[$row['id_board']] = array();
1350
1351
		$groups[$row['id_board']][] = array(
1352
			'id' => $row['id_group'],
1353
			'name' => $row['group_name'],
1354
			'href' => $scripturl . '?action=groups;sa=members;group=' . $row['id_group'],
1355
			'link' => '<a href="' . $scripturl . '?action=groups;sa=members;group=' . $row['id_group'] . '" title="' . $txt['board_moderator'] . '">' . $row['group_name'] . '</a>',
1356
		);
1357
	}
1358
1359
	return $groups;
1360
}
1361
1362
/**
1363
 * Load a lot of useful information regarding the boards and categories.
1364
 * The information retrieved is stored in globals:
1365
 *  $boards		properties of each board.
1366
 *  $boardList	a list of boards grouped by category ID.
1367
 *  $cat_tree	properties of each category.
1368
 */
1369
function getBoardTree()
1370
{
1371
	global $cat_tree, $boards, $boardList, $smcFunc;
1372
1373
	$boardColumns = array(
1374
		'COALESCE(b.id_board, 0) AS id_board', 'b.id_parent', 'b.name AS board_name',
1375
		'b.description', 'b.child_level', 'b.board_order', 'b.count_posts', 'b.member_groups',
1376
		'b.id_theme', 'b.override_theme', 'b.id_profile', 'b.redirect', 'b.num_posts',
1377
		'b.num_topics', 'b.deny_member_groups', 'c.id_cat', 'c.name AS cat_name',
1378
		'c.description AS cat_desc', 'c.cat_order', 'c.can_collapse',
1379
	);
1380
	$boardParameters = array();
1381
	$boardJoins = array();
1382
	$boardWhere = array();
1383
	$boardOrder = array('c.cat_order', 'b.child_level', 'b.board_order');
1384
1385
	// Let mods add extra columns, parameters, etc., to the SELECT query
1386
	call_integration_hook('integrate_pre_boardtree', array(&$boardColumns, &$boardParameters, &$boardJoins, &$boardWhere, &$boardOrder));
1387
1388
	$boardColumns = array_unique($boardColumns);
1389
	$boardParameters = array_unique($boardParameters);
1390
	$boardJoins = array_unique($boardJoins);
1391
	$boardWhere = array_unique($boardWhere);
1392
	$boardOrder = array_unique($boardOrder);
1393
1394
	// Getting all the board and category information you'd ever wanted.
1395
	$request = $smcFunc['db_query']('', '
1396
		SELECT
1397
			' . implode(', ', $boardColumns) . '
1398
		FROM {db_prefix}categories AS c
1399
			LEFT JOIN {db_prefix}boards AS b ON (b.id_cat = c.id_cat)' . implode('
1400
			', $boardJoins) . '
1401
		WHERE {query_see_board}' . (empty($boardWhere) ? '' : '
1402
			AND (' . implode(') AND (', $boardWhere) . ')') . '
1403
		ORDER BY ' . implode(', ', $boardOrder),
1404
		$boardParameters
1405
	);
1406
	$cat_tree = array();
1407
	$boards = array();
1408
	$last_board_order = 0;
1409
	while ($row = $smcFunc['db_fetch_assoc']($request))
1410
	{
1411
		if (!isset($cat_tree[$row['id_cat']]))
1412
		{
1413
			$cat_tree[$row['id_cat']] = array(
1414
				'node' => array(
1415
					'id' => $row['id_cat'],
1416
					'name' => $row['cat_name'],
1417
					'description' => $row['cat_desc'],
1418
					'order' => $row['cat_order'],
1419
					'can_collapse' => $row['can_collapse']
1420
				),
1421
				'is_first' => empty($cat_tree),
1422
				'last_board_order' => $last_board_order,
1423
				'children' => array()
1424
			);
1425
			$prevBoard = 0;
1426
			$curLevel = 0;
1427
		}
1428
1429
		if (!empty($row['id_board']))
1430
		{
1431
			if ($row['child_level'] != $curLevel)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $curLevel does not seem to be defined for all execution paths leading up to this point.
Loading history...
1432
				$prevBoard = 0;
1433
1434
			$boards[$row['id_board']] = array(
1435
				'id' => $row['id_board'],
1436
				'category' => $row['id_cat'],
1437
				'parent' => $row['id_parent'],
1438
				'level' => $row['child_level'],
1439
				'order' => $row['board_order'],
1440
				'name' => $row['board_name'],
1441
				'member_groups' => explode(',', $row['member_groups']),
1442
				'deny_groups' => explode(',', $row['deny_member_groups']),
1443
				'description' => $row['description'],
1444
				'count_posts' => empty($row['count_posts']),
1445
				'posts' => $row['num_posts'],
1446
				'topics' => $row['num_topics'],
1447
				'theme' => $row['id_theme'],
1448
				'override_theme' => $row['override_theme'],
1449
				'profile' => $row['id_profile'],
1450
				'redirect' => $row['redirect'],
1451
				'prev_board' => $prevBoard
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $prevBoard does not seem to be defined for all execution paths leading up to this point.
Loading history...
1452
			);
1453
			$prevBoard = $row['id_board'];
1454
			$last_board_order = $row['board_order'];
1455
1456
			if (empty($row['child_level']))
1457
			{
1458
				$cat_tree[$row['id_cat']]['children'][$row['id_board']] = array(
1459
					'node' => &$boards[$row['id_board']],
1460
					'is_first' => empty($cat_tree[$row['id_cat']]['children']),
1461
					'children' => array()
1462
				);
1463
				$boards[$row['id_board']]['tree'] = &$cat_tree[$row['id_cat']]['children'][$row['id_board']];
1464
			}
1465
			else
1466
			{
1467
				// Parent doesn't exist!
1468
				if (!isset($boards[$row['id_parent']]['tree']))
1469
					fatal_lang_error('no_valid_parent', false, array($row['board_name']));
1470
1471
				// Wrong childlevel...we can silently fix this...
1472
				if ($boards[$row['id_parent']]['tree']['node']['level'] != $row['child_level'] - 1)
1473
					$smcFunc['db_query']('', '
1474
						UPDATE {db_prefix}boards
1475
						SET child_level = {int:new_child_level}
1476
						WHERE id_board = {int:selected_board}',
1477
						array(
1478
							'new_child_level' => $boards[$row['id_parent']]['tree']['node']['level'] + 1,
1479
							'selected_board' => $row['id_board'],
1480
						)
1481
					);
1482
1483
				$boards[$row['id_parent']]['tree']['children'][$row['id_board']] = array(
1484
					'node' => &$boards[$row['id_board']],
1485
					'is_first' => empty($boards[$row['id_parent']]['tree']['children']),
1486
					'children' => array()
1487
				);
1488
				$boards[$row['id_board']]['tree'] = &$boards[$row['id_parent']]['tree']['children'][$row['id_board']];
1489
			}
1490
		}
1491
1492
		// If mods want to do anything with this board before we move on, now's the time
1493
		call_integration_hook('integrate_boardtree_board', array($row));
1494
	}
1495
	$smcFunc['db_free_result']($request);
1496
1497
	// Get a list of all the boards in each category (using recursion).
1498
	$boardList = array();
1499
	foreach ($cat_tree as $catID => $node)
1500
	{
1501
		$boardList[$catID] = array();
1502
		recursiveBoards($boardList[$catID], $node);
1503
	}
1504
}
1505
1506
/**
1507
 * Recursively get a list of boards.
1508
 * Used by getBoardTree
1509
 *
1510
 * @param array &$_boardList The board list
1511
 * @param array &$_tree The board tree
1512
 */
1513
function recursiveBoards(&$_boardList, &$_tree)
1514
{
1515
	if (empty($_tree['children']))
1516
		return;
1517
1518
	foreach ($_tree['children'] as $id => $node)
1519
	{
1520
		$_boardList[] = $id;
1521
		recursiveBoards($_boardList, $node);
1522
	}
1523
}
1524
1525
/**
1526
 * Returns whether the child board id is actually a child of the parent (recursive).
1527
 *
1528
 * @param int $child The ID of the child board
1529
 * @param int $parent The ID of a parent board
1530
 * @return boolean Whether the specified child board is actually a child of the specified parent board.
1531
 */
1532
function isChildOf($child, $parent)
1533
{
1534
	global $boards;
1535
1536
	if (empty($boards[$child]['parent']))
1537
		return false;
1538
1539
	if ($boards[$child]['parent'] == $parent)
1540
		return true;
1541
1542
	return isChildOf($boards[$child]['parent'], $parent);
1543
}
1544
1545
function setBoardParsedDescription($category_id = 0, $boards_info = array())
1546
{
1547
	global $context;
1548
1549
	if (empty($category_id) || empty($boards_info))
1550
		return array();
1551
1552
	// Get the data we already parsed
1553
	$already_parsed_boards = getBoardsParsedDescription($category_id);
1554
1555
	foreach ($boards_info as $board_id => $board_description)
1556
		$already_parsed_boards[$board_id] = parse_bbc(
1557
			$board_description,
1558
			false,
1559
			'',
1560
			$context['description_allowed_tags']
1561
		);
1562
1563
	cache_put_data('parsed_boards_descriptions_'. $category_id, $already_parsed_boards, 864000);
1564
1565
	return $already_parsed_boards;
1566
}
1567
1568
function getBoardsParsedDescription($category_id = 0)
1569
{
1570
	if (empty($category_id))
1571
		return array();
1572
1573
	return (array) cache_get_data('parsed_boards_descriptions_' . $category_id, 864000);
1574
}
1575
1576
?>