Passed
Push — release-2.1 ( 493a4c...4017f8 )
by Mathias
09:59 queued 10s
created

setBoardParsedDescription()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 5 Features 0
Metric Value
cc 4
eloc 12
nop 2
dl 0
loc 21
rs 9.8666
c 5
b 5
f 0
nc 3
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 2020 Simple Machines and individual contributors
12
 * @license https://www.simplemachines.org/about/smf/license.php BSD
13
 *
14
 * @version 2.1 RC3
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;
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
			trigger_error('modifyBoard(): The move_to value \'' . $boardOptions['move_to'] . '\' is incorrect', E_USER_ERROR);
529
530
		// Get a list of children of this board.
531
		$childList = array();
532
		recursiveBoards($childList, $boards[$board_id]['tree']);
533
534
		// See if there are changes that affect children.
535
		$childUpdates = array();
536
		$levelDiff = $child_level - $boards[$board_id]['level'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $child_level does not seem to be defined for all execution paths leading up to this point.
Loading history...
537
538
		if ($levelDiff != 0)
539
			$childUpdates[] = 'child_level = child_level ' . ($levelDiff > 0 ? '+ ' : '') . '{int:level_diff}';
540
541
		if ($id_cat != $boards[$board_id]['category'])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id_cat does not seem to be defined for all execution paths leading up to this point.
Loading history...
542
			$childUpdates[] = 'id_cat = {int:category}';
543
544
		// Fix the children of this board.
545
		if (!empty($childList) && !empty($childUpdates))
546
			$smcFunc['db_query']('', '
547
				UPDATE {db_prefix}boards
548
				SET ' . implode(',
549
					', $childUpdates) . '
550
				WHERE id_board IN ({array_int:board_list})',
551
				array(
552
					'board_list' => $childList,
553
					'category' => $id_cat,
554
					'level_diff' => $levelDiff,
555
				)
556
			);
557
558
		// Make some room for this spot.
559
		$smcFunc['db_query']('', '
560
			UPDATE {db_prefix}boards
561
			SET board_order = board_order + {int:new_order}
562
			WHERE board_order > {int:insert_after}
563
				AND id_board != {int:selected_board}',
564
			array(
565
				'insert_after' => $after,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $after does not seem to be defined for all execution paths leading up to this point.
Loading history...
566
				'selected_board' => $board_id,
567
				'new_order' => 1 + count($childList),
568
			)
569
		);
570
571
		$boardUpdates[] = 'id_cat = {int:id_cat}';
572
		$boardUpdates[] = 'id_parent = {int:id_parent}';
573
		$boardUpdates[] = 'child_level = {int:child_level}';
574
		$boardUpdates[] = 'board_order = {int:board_order}';
575
		$boardUpdateParameters += array(
576
			'id_cat' => $id_cat,
577
			'id_parent' => $id_parent,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id_parent does not seem to be defined for all execution paths leading up to this point.
Loading history...
578
			'child_level' => $child_level,
579
			'board_order' => $after + 1,
580
		);
581
	}
582
583
	// This setting is a little twisted in the database...
584
	if (isset($boardOptions['posts_count']))
585
	{
586
		$boardUpdates[] = 'count_posts = {int:count_posts}';
587
		$boardUpdateParameters['count_posts'] = $boardOptions['posts_count'] ? 0 : 1;
588
	}
589
590
	// Set the theme for this board.
591
	if (isset($boardOptions['board_theme']))
592
	{
593
		$boardUpdates[] = 'id_theme = {int:id_theme}';
594
		$boardUpdateParameters['id_theme'] = (int) $boardOptions['board_theme'];
595
	}
596
597
	// Should the board theme override the user preferred theme?
598
	if (isset($boardOptions['override_theme']))
599
	{
600
		$boardUpdates[] = 'override_theme = {int:override_theme}';
601
		$boardUpdateParameters['override_theme'] = $boardOptions['override_theme'] ? 1 : 0;
602
	}
603
604
	// Who's allowed to access this board.
605
	if (isset($boardOptions['access_groups']))
606
	{
607
		$boardUpdates[] = 'member_groups = {string:member_groups}';
608
		$boardUpdateParameters['member_groups'] = implode(',', $boardOptions['access_groups']);
609
	}
610
611
	// And who isn't.
612
	if (isset($boardOptions['deny_groups']))
613
	{
614
		$boardUpdates[] = 'deny_member_groups = {string:deny_groups}';
615
		$boardUpdateParameters['deny_groups'] = implode(',', $boardOptions['deny_groups']);
616
	}
617
618
	if (isset($boardOptions['board_name']))
619
	{
620
		$boardUpdates[] = 'name = {string:board_name}';
621
		$boardUpdateParameters['board_name'] = $boardOptions['board_name'];
622
	}
623
624
	if (isset($boardOptions['board_description']))
625
	{
626
		$boardUpdates[] = 'description = {string:board_description}';
627
		$boardUpdateParameters['board_description'] = $boardOptions['board_description'];
628
	}
629
630
	if (isset($boardOptions['profile']))
631
	{
632
		$boardUpdates[] = 'id_profile = {int:profile}';
633
		$boardUpdateParameters['profile'] = (int) $boardOptions['profile'];
634
	}
635
636
	if (isset($boardOptions['redirect']))
637
	{
638
		$boardUpdates[] = 'redirect = {string:redirect}';
639
		$boardUpdateParameters['redirect'] = $boardOptions['redirect'];
640
	}
641
642
	if (isset($boardOptions['num_posts']))
643
	{
644
		$boardUpdates[] = 'num_posts = {int:num_posts}';
645
		$boardUpdateParameters['num_posts'] = (int) $boardOptions['num_posts'];
646
	}
647
648
	$id = $board_id;
649
	call_integration_hook('integrate_modify_board', array($id, $boardOptions, &$boardUpdates, &$boardUpdateParameters));
650
651
	// Do the updates (if any).
652
	if (!empty($boardUpdates))
653
		$smcFunc['db_query']('', '
654
			UPDATE {db_prefix}boards
655
			SET
656
				' . implode(',
657
				', $boardUpdates) . '
658
			WHERE id_board = {int:selected_board}',
659
			array_merge($boardUpdateParameters, array(
660
				'selected_board' => $board_id,
661
			))
662
		);
663
664
	// Before we add new access_groups or deny_groups, remove all of the old entries
665
	$smcFunc['db_query']('', '
666
		DELETE FROM {db_prefix}board_permissions_view
667
		WHERE id_board = {int:selected_board}',
668
		array(
669
			'selected_board' => $board_id,
670
		)
671
	);
672
673
	// Do permission sync
674
	if (!empty($boardOptions['deny_groups']))
675
	{
676
		$insert = array();
677
		foreach ($boardOptions['deny_groups'] as $value)
678
			$insert[] = array($value, $board_id, 1);
679
680
		$smcFunc['db_insert']('insert',
681
			'{db_prefix}board_permissions_view',
682
			array('id_group' => 'int', 'id_board' => 'int', 'deny' => 'int'),
683
			$insert,
684
			array('id_group', 'id_board', 'deny')
685
		);
686
	}
687
688
	if (!empty($boardOptions['access_groups']))
689
	{
690
		$insert = array();
691
		foreach ($boardOptions['access_groups'] as $value)
692
			$insert[] = array($value, $board_id, 0);
693
694
		$smcFunc['db_insert']('insert',
695
			'{db_prefix}board_permissions_view',
696
			array('id_group' => 'int', 'id_board' => 'int', 'deny' => 'int'),
697
			$insert,
698
			array('id_group', 'id_board', 'deny')
699
		);
700
	}
701
702
	// Set moderators of this board.
703
	if (isset($boardOptions['moderators']) || isset($boardOptions['moderator_string']) || isset($boardOptions['moderator_groups']) || isset($boardOptions['moderator_group_string']))
704
	{
705
		// Reset current moderators for this board - if there are any!
706
		$smcFunc['db_query']('', '
707
			DELETE FROM {db_prefix}moderators
708
			WHERE id_board = {int:board_list}',
709
			array(
710
				'board_list' => $board_id,
711
			)
712
		);
713
714
		// Validate and get the IDs of the new moderators.
715
		if (isset($boardOptions['moderator_string']) && trim($boardOptions['moderator_string']) != '')
716
		{
717
			// Divvy out the usernames, remove extra space.
718
			$moderator_string = strtr($smcFunc['htmlspecialchars']($boardOptions['moderator_string'], ENT_QUOTES), array('&quot;' => '"'));
719
			preg_match_all('~"([^"]+)"~', $moderator_string, $matches);
720
			$moderators = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $moderator_string)));
721
			for ($k = 0, $n = count($moderators); $k < $n; $k++)
722
			{
723
				$moderators[$k] = trim($moderators[$k]);
724
725
				if (strlen($moderators[$k]) == 0)
726
					unset($moderators[$k]);
727
			}
728
729
			// Find all the id_member's for the member_name's in the list.
730
			if (empty($boardOptions['moderators']))
731
				$boardOptions['moderators'] = array();
732
			if (!empty($moderators))
733
			{
734
				$request = $smcFunc['db_query']('', '
735
					SELECT id_member
736
					FROM {db_prefix}members
737
					WHERE member_name IN ({array_string:moderator_list}) OR real_name IN ({array_string:moderator_list})
738
					LIMIT {int:limit}',
739
					array(
740
						'moderator_list' => $moderators,
741
						'limit' => count($moderators),
742
					)
743
				);
744
				while ($row = $smcFunc['db_fetch_assoc']($request))
745
					$boardOptions['moderators'][] = $row['id_member'];
746
				$smcFunc['db_free_result']($request);
747
			}
748
		}
749
750
		// Add the moderators to the board.
751
		if (!empty($boardOptions['moderators']))
752
		{
753
			$inserts = array();
754
			foreach ($boardOptions['moderators'] as $moderator)
755
				$inserts[] = array($board_id, $moderator);
756
757
			$smcFunc['db_insert']('insert',
758
				'{db_prefix}moderators',
759
				array('id_board' => 'int', 'id_member' => 'int'),
760
				$inserts,
761
				array('id_board', 'id_member')
762
			);
763
		}
764
765
		// Reset current moderator groups for this board - if there are any!
766
		$smcFunc['db_query']('', '
767
			DELETE FROM {db_prefix}moderator_groups
768
			WHERE id_board = {int:board_list}',
769
			array(
770
				'board_list' => $board_id,
771
			)
772
		);
773
774
		// Validate and get the IDs of the new moderator groups.
775
		if (isset($boardOptions['moderator_group_string']) && trim($boardOptions['moderator_group_string']) != '')
776
		{
777
			// Divvy out the group names, remove extra space.
778
			$moderator_group_string = strtr($smcFunc['htmlspecialchars']($boardOptions['moderator_group_string'], ENT_QUOTES), array('&quot;' => '"'));
779
			preg_match_all('~"([^"]+)"~', $moderator_group_string, $matches);
780
			$moderator_groups = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $moderator_group_string)));
781
			for ($k = 0, $n = count($moderator_groups); $k < $n; $k++)
782
			{
783
				$moderator_groups[$k] = trim($moderator_groups[$k]);
784
785
				if (strlen($moderator_groups[$k]) == 0)
786
					unset($moderator_groups[$k]);
787
			}
788
789
			/* 	Find all the id_group's for all the group names in the list
790
				But skip any invalid ones (invisible/post groups/Administrator/Moderator) */
791
			if (empty($boardOptions['moderator_groups']))
792
				$boardOptions['moderator_groups'] = array();
793
			if (!empty($moderator_groups))
794
			{
795
				$request = $smcFunc['db_query']('', '
796
					SELECT id_group
797
					FROM {db_prefix}membergroups
798
					WHERE group_name IN ({array_string:moderator_group_list})
799
						AND hidden = {int:visible}
800
						AND min_posts = {int:negative_one}
801
						AND id_group NOT IN ({array_int:invalid_groups})
802
					LIMIT {int:limit}',
803
					array(
804
						'visible' => 0,
805
						'negative_one' => -1,
806
						'invalid_groups' => array(1, 3),
807
						'moderator_group_list' => $moderator_groups,
808
						'limit' => count($moderator_groups),
809
					)
810
				);
811
				while ($row = $smcFunc['db_fetch_assoc']($request))
812
				{
813
					$boardOptions['moderator_groups'][] = $row['id_group'];
814
				}
815
				$smcFunc['db_free_result']($request);
816
			}
817
		}
818
819
		// Add the moderator groups to the board.
820
		if (!empty($boardOptions['moderator_groups']))
821
		{
822
			$inserts = array();
823
			foreach ($boardOptions['moderator_groups'] as $moderator_group)
824
				$inserts[] = array($board_id, $moderator_group);
825
826
			$smcFunc['db_insert']('insert',
827
				'{db_prefix}moderator_groups',
828
				array('id_board' => 'int', 'id_group' => 'int'),
829
				$inserts,
830
				array('id_board', 'id_group')
831
			);
832
		}
833
834
		// Note that caches can now be wrong!
835
		updateSettings(array('settings_updated' => time()));
836
	}
837
838
	if (isset($boardOptions['move_to']))
839
		reorderBoards();
840
841
	$parsed_boards_cat_id = isset($id_cat) ? $id_cat : $boardOptions['old_id_cat'];
842
	$already_parsed_boards = getBoardsParsedDescription($parsed_boards_cat_id);
843
844
	if (isset($boardOptions['board_description']))
845
		$already_parsed_boards[$board_id] = parse_bbc(
846
			$boardOptions['board_description'],
847
			false,
848
849
			'',
850
			$context['description_allowed_tags']);
851
852
	clean_cache('data');
853
854
	cache_put_data('parsed_boards_descriptions_'. $parsed_boards_cat_id, $already_parsed_boards, 864000);
855
856
	if (empty($boardOptions['dont_log']))
857
		logAction('edit_board', array('board' => $board_id), 'admin');
858
}
859
860
/**
861
 * Create a new board and set its properties and position.
862
 * Allows (almost) the same options as the modifyBoard() function.
863
 * With the option inherit_permissions set, the parent board permissions
864
 * will be inherited.
865
 *
866
 * @param array $boardOptions An array of information for the new board
867
 * @return int The ID of the new board
868
 */
869
function createBoard($boardOptions)
870
{
871
	global $boards, $smcFunc;
872
873
	// Trigger an error if one of the required values is not set.
874
	if (!isset($boardOptions['board_name']) || trim($boardOptions['board_name']) == '' || !isset($boardOptions['move_to']) || !isset($boardOptions['target_category']))
875
		trigger_error('createBoard(): One or more of the required options is not set', E_USER_ERROR);
876
877
	if (in_array($boardOptions['move_to'], array('child', 'before', 'after')) && !isset($boardOptions['target_board']))
878
		trigger_error('createBoard(): Target board is not set', E_USER_ERROR);
879
880
	// Set every optional value to its default value.
881
	$boardOptions += array(
882
		'posts_count' => true,
883
		'override_theme' => false,
884
		'board_theme' => 0,
885
		'access_groups' => array(),
886
		'board_description' => '',
887
		'profile' => 1,
888
		'moderators' => '',
889
		'inherit_permissions' => true,
890
		'dont_log' => true,
891
	);
892
893
	$default_memgrps = '-1,0';
894
895
	$board_columns = array(
896
		'id_cat' => 'int', 'name' => 'string-255', 'description' => 'string', 'board_order' => 'int',
897
		'member_groups' => 'string', 'redirect' => 'string',
898
	);
899
	$board_parameters = array(
900
		$boardOptions['target_category'], $boardOptions['board_name'], '', 0,
901
		$default_memgrps, '',
902
	);
903
904
	call_integration_hook('integrate_create_board', array(&$boardOptions, &$board_columns, &$board_parameters));
905
906
	// Insert a board, the settings are dealt with later.
907
	$board_id = $smcFunc['db_insert']('',
908
		'{db_prefix}boards',
909
		$board_columns,
910
		$board_parameters,
911
		array('id_board'),
912
		1
913
	);
914
915
	$insert = array();
916
917
	foreach (explode(',', $default_memgrps) as $value)
918
		$insert[] = array($value, $board_id, 0);
919
920
	$smcFunc['db_insert']('',
921
		'{db_prefix}board_permissions_view',
922
		array('id_group' => 'int', 'id_board' => 'int', 'deny' => 'int'),
923
		$insert,
924
		array('id_group', 'id_board', 'deny'),
925
		1
926
	);
927
928
	if (empty($board_id))
929
		return 0;
930
931
	// Change the board according to the given specifications.
932
	modifyBoard($board_id, $boardOptions);
933
934
	// Do we want the parent permissions to be inherited?
935
	if ($boardOptions['inherit_permissions'])
936
	{
937
		getBoardTree();
938
939
		if (!empty($boards[$board_id]['parent']))
940
		{
941
			$request = $smcFunc['db_query']('', '
942
				SELECT id_profile
943
				FROM {db_prefix}boards
944
				WHERE id_board = {int:board_parent}
945
				LIMIT 1',
946
				array(
947
					'board_parent' => (int) $boards[$board_id]['parent'],
948
				)
949
			);
950
			list ($boardOptions['profile']) = $smcFunc['db_fetch_row']($request);
951
			$smcFunc['db_free_result']($request);
952
953
			$smcFunc['db_query']('', '
954
				UPDATE {db_prefix}boards
955
				SET id_profile = {int:new_profile}
956
				WHERE id_board = {int:current_board}',
957
				array(
958
					'new_profile' => $boardOptions['profile'],
959
					'current_board' => $board_id,
960
				)
961
			);
962
		}
963
	}
964
965
	// Clean the data cache.
966
	clean_cache('data');
967
968
	// Created it.
969
	logAction('add_board', array('board' => $board_id), 'admin');
970
971
	// Here you are, a new board, ready to be spammed.
972
	return $board_id;
973
}
974
975
/**
976
 * Remove one or more boards.
977
 * Allows to move the children of the board before deleting it
978
 * if moveChildrenTo is set to null, the child boards will be deleted.
979
 * Deletes:
980
 *   - all topics that are on the given boards;
981
 *   - all information that's associated with the given boards;
982
 * updates the statistics to reflect the new situation.
983
 *
984
 * @param array $boards_to_remove The boards to remove
985
 * @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)
986
 */
987
function deleteBoards($boards_to_remove, $moveChildrenTo = null)
988
{
989
	global $sourcedir, $boards, $smcFunc;
990
991
	// No boards to delete? Return!
992
	if (empty($boards_to_remove))
993
		return;
994
995
	getBoardTree();
996
997
	call_integration_hook('integrate_delete_board', array($boards_to_remove, &$moveChildrenTo));
998
999
	// If $moveChildrenTo is set to null, include the children in the removal.
1000
	if ($moveChildrenTo === null)
1001
	{
1002
		// Get a list of the child boards that will also be removed.
1003
		$child_boards_to_remove = array();
1004
		foreach ($boards_to_remove as $board_to_remove)
1005
			recursiveBoards($child_boards_to_remove, $boards[$board_to_remove]['tree']);
1006
1007
		// Merge the children with their parents.
1008
		if (!empty($child_boards_to_remove))
1009
			$boards_to_remove = array_unique(array_merge($boards_to_remove, $child_boards_to_remove));
1010
	}
1011
	// Move the children to a safe home.
1012
	else
1013
	{
1014
		foreach ($boards_to_remove as $id_board)
1015
		{
1016
			// @todo Separate category?
1017
			if ($moveChildrenTo === 0)
1018
				fixChildren($id_board, 0, 0);
1019
			else
1020
				fixChildren($id_board, $boards[$moveChildrenTo]['level'] + 1, $moveChildrenTo);
1021
		}
1022
	}
1023
1024
	// Delete ALL topics in the selected boards (done first so topics can't be marooned.)
1025
	$request = $smcFunc['db_query']('', '
1026
		SELECT id_topic
1027
		FROM {db_prefix}topics
1028
		WHERE id_board IN ({array_int:boards_to_remove})',
1029
		array(
1030
			'boards_to_remove' => $boards_to_remove,
1031
		)
1032
	);
1033
	$topics = array();
1034
	while ($row = $smcFunc['db_fetch_assoc']($request))
1035
		$topics[] = $row['id_topic'];
1036
	$smcFunc['db_free_result']($request);
1037
1038
	require_once($sourcedir . '/RemoveTopic.php');
1039
	removeTopics($topics, false);
1040
1041
	// Delete the board's logs.
1042
	$smcFunc['db_query']('', '
1043
		DELETE FROM {db_prefix}log_mark_read
1044
		WHERE id_board IN ({array_int:boards_to_remove})',
1045
		array(
1046
			'boards_to_remove' => $boards_to_remove,
1047
		)
1048
	);
1049
	$smcFunc['db_query']('', '
1050
		DELETE FROM {db_prefix}log_boards
1051
		WHERE id_board IN ({array_int:boards_to_remove})',
1052
		array(
1053
			'boards_to_remove' => $boards_to_remove,
1054
		)
1055
	);
1056
	$smcFunc['db_query']('', '
1057
		DELETE FROM {db_prefix}log_notify
1058
		WHERE id_board IN ({array_int:boards_to_remove})',
1059
		array(
1060
			'boards_to_remove' => $boards_to_remove,
1061
		)
1062
	);
1063
1064
	// Delete this board's moderators.
1065
	$smcFunc['db_query']('', '
1066
		DELETE FROM {db_prefix}moderators
1067
		WHERE id_board IN ({array_int:boards_to_remove})',
1068
		array(
1069
			'boards_to_remove' => $boards_to_remove,
1070
		)
1071
	);
1072
1073
	// Delete this board's moderator groups.
1074
	$smcFunc['db_query']('', '
1075
		DELETE FROM {db_prefix}moderator_groups
1076
		WHERE id_board IN ({array_int:boards_to_remove})',
1077
		array(
1078
			'boards_to_remove' => $boards_to_remove,
1079
		)
1080
	);
1081
1082
	// Delete any extra events in the calendar.
1083
	$smcFunc['db_query']('', '
1084
		DELETE FROM {db_prefix}calendar
1085
		WHERE id_board IN ({array_int:boards_to_remove})',
1086
		array(
1087
			'boards_to_remove' => $boards_to_remove,
1088
		)
1089
	);
1090
1091
	// Delete any message icons that only appear on these boards.
1092
	$smcFunc['db_query']('', '
1093
		DELETE FROM {db_prefix}message_icons
1094
		WHERE id_board IN ({array_int:boards_to_remove})',
1095
		array(
1096
			'boards_to_remove' => $boards_to_remove,
1097
		)
1098
	);
1099
1100
	// Delete the boards.
1101
	$smcFunc['db_query']('', '
1102
		DELETE FROM {db_prefix}boards
1103
		WHERE id_board IN ({array_int:boards_to_remove})',
1104
		array(
1105
			'boards_to_remove' => $boards_to_remove,
1106
		)
1107
	);
1108
1109
	// Delete permissions
1110
	$smcFunc['db_query']('', '
1111
		DELETE FROM {db_prefix}board_permissions_view
1112
		WHERE id_board IN ({array_int:boards_to_remove})',
1113
		array(
1114
			'boards_to_remove' => $boards_to_remove,
1115
		)
1116
	);
1117
1118
	// Latest message/topic might not be there anymore.
1119
	updateStats('message');
1120
	updateStats('topic');
1121
	updateSettings(array(
1122
		'calendar_updated' => time(),
1123
	));
1124
1125
	// Plus reset the cache to stop people getting odd results.
1126
	updateSettings(array('settings_updated' => time()));
1127
1128
	// Clean the cache as well.
1129
	clean_cache('data');
1130
1131
	// Let's do some serious logging.
1132
	foreach ($boards_to_remove as $id_board)
1133
		logAction('delete_board', array('boardname' => $boards[$id_board]['name']), 'admin');
1134
1135
	reorderBoards();
1136
}
1137
1138
/**
1139
 * Put all boards in the right order and sorts the records of the boards table.
1140
 * Used by modifyBoard(), deleteBoards(), modifyCategory(), and deleteCategories() functions
1141
 */
1142
function reorderBoards()
1143
{
1144
	global $cat_tree, $boardList, $boards, $smcFunc;
1145
1146
	getBoardTree();
1147
1148
	// Set the board order for each category.
1149
	$board_order = 0;
1150
	foreach ($cat_tree as $catID => $dummy)
1151
	{
1152
		foreach ($boardList[$catID] as $boardID)
1153
			if ($boards[$boardID]['order'] != ++$board_order)
1154
				$smcFunc['db_query']('', '
1155
					UPDATE {db_prefix}boards
1156
					SET board_order = {int:new_order}
1157
					WHERE id_board = {int:selected_board}',
1158
					array(
1159
						'new_order' => $board_order,
1160
						'selected_board' => $boardID,
1161
					)
1162
				);
1163
	}
1164
1165
	// Empty the board order cache
1166
	cache_put_data('board_order', null, -3600);
1167
}
1168
1169
/**
1170
 * Fixes the children of a board by setting their child_levels to new values.
1171
 * Used when a board is deleted or moved, to affect its children.
1172
 *
1173
 * @param int $parent The ID of the parent board
1174
 * @param int $newLevel The new child level for each of the child boards
1175
 * @param int $newParent The ID of the new parent board
1176
 */
1177
function fixChildren($parent, $newLevel, $newParent)
1178
{
1179
	global $smcFunc;
1180
1181
	// Grab all children of $parent...
1182
	$result = $smcFunc['db_query']('', '
1183
		SELECT id_board
1184
		FROM {db_prefix}boards
1185
		WHERE id_parent = {int:parent_board}',
1186
		array(
1187
			'parent_board' => $parent,
1188
		)
1189
	);
1190
	$children = array();
1191
	while ($row = $smcFunc['db_fetch_assoc']($result))
1192
		$children[] = $row['id_board'];
1193
	$smcFunc['db_free_result']($result);
1194
1195
	// ...and set it to a new parent and child_level.
1196
	$smcFunc['db_query']('', '
1197
		UPDATE {db_prefix}boards
1198
		SET id_parent = {int:new_parent}, child_level = {int:new_child_level}
1199
		WHERE id_parent = {int:parent_board}',
1200
		array(
1201
			'new_parent' => $newParent,
1202
			'new_child_level' => $newLevel,
1203
			'parent_board' => $parent,
1204
		)
1205
	);
1206
1207
	// Recursively fix the children of the children.
1208
	foreach ($children as $child)
1209
		fixChildren($child, $newLevel + 1, $child);
1210
}
1211
1212
/**
1213
 * Tries to load up the entire board order and category very very quickly
1214
 * Returns an array with two elements, cats and boards
1215
 *
1216
 * @return array An array of categories and boards
1217
 */
1218
function getTreeOrder()
1219
{
1220
	global $smcFunc;
1221
1222
	static $tree_order = array(
1223
		'cats' => array(),
1224
		'boards' => array(),
1225
	);
1226
1227
	if (!empty($tree_order['boards']))
1228
		return $tree_order;
1229
1230
	if (($cached = cache_get_data('board_order', 86400)) !== null)
1231
	{
1232
		$tree_order = $cached;
1233
		return $cached;
1234
	}
1235
1236
	$request = $smcFunc['db_query']('', '
1237
		SELECT b.id_board, b.id_cat
1238
		FROM {db_prefix}boards AS b
1239
		ORDER BY b.board_order',
1240
		array()
1241
	);
1242
1243
	foreach ($smcFunc['db_fetch_all']($request) as $row)
1244
	{
1245
		if (!in_array($row['id_cat'], $tree_order['cats']))
1246
			$tree_order['cats'][] = $row['id_cat'];
1247
		$tree_order['boards'][] = $row['id_board'];
1248
	}
1249
	$smcFunc['db_free_result']($request);
1250
1251
	cache_put_data('board_order', $tree_order, 86400);
1252
1253
	return $tree_order;
1254
}
1255
1256
/**
1257
 * Takes a board array and sorts it
1258
 *
1259
 * @param array &$boards The boards
1260
 */
1261
function sortBoards(array &$boards)
1262
{
1263
	$tree = getTreeOrder();
1264
1265
	$ordered = array();
1266
	foreach ($tree['boards'] as $board)
1267
		if (!empty($boards[$board]))
1268
		{
1269
			$ordered[$board] = $boards[$board];
1270
1271
			if (is_array($ordered[$board]) && !empty($ordered[$board]['boards']))
1272
				sortBoards($ordered[$board]['boards']);
1273
1274
			if (is_array($ordered[$board]) && !empty($ordered[$board]['children']))
1275
				sortBoards($ordered[$board]['children']);
1276
		}
1277
1278
	$boards = $ordered;
1279
}
1280
1281
/**
1282
 * Takes a category array and sorts it
1283
 *
1284
 * @param array &$categories The categories
1285
 */
1286
function sortCategories(array &$categories)
1287
{
1288
	$tree = getTreeOrder();
1289
1290
	$ordered = array();
1291
	foreach ($tree['cats'] as $cat)
1292
		if (!empty($categories[$cat]))
1293
		{
1294
			$ordered[$cat] = $categories[$cat];
1295
			if (!empty($ordered[$cat]['boards']))
1296
				sortBoards($ordered[$cat]['boards']);
1297
		}
1298
1299
	$categories = $ordered;
1300
}
1301
1302
/**
1303
 * Returns the given board's moderators, with their names and links
1304
 *
1305
 * @param array $boards The boards to get moderators of
1306
 * @return array An array containing information about the moderators of each board
1307
 */
1308
function getBoardModerators(array $boards)
1309
{
1310
	global $smcFunc, $scripturl, $txt;
1311
1312
	if (empty($boards))
1313
		return array();
1314
1315
	$request = $smcFunc['db_query']('', '
1316
		SELECT mem.id_member, mem.real_name, mo.id_board
1317
		FROM {db_prefix}moderators AS mo
1318
			INNER JOIN {db_prefix}members AS mem ON (mem.id_member = mo.id_member)
1319
		WHERE mo.id_board IN ({array_int:boards})',
1320
		array(
1321
			'boards' => $boards,
1322
		)
1323
	);
1324
	$moderators = array();
1325
	while ($row = $smcFunc['db_fetch_assoc']($request))
1326
	{
1327
		if (empty($moderators[$row['id_board']]))
1328
			$moderators[$row['id_board']] = array();
1329
1330
		$moderators[$row['id_board']][] = array(
1331
			'id' => $row['id_member'],
1332
			'name' => $row['real_name'],
1333
			'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
1334
			'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '" title="' . $txt['board_moderator'] . '">' . $row['real_name'] . '</a>',
1335
		);
1336
	}
1337
	$smcFunc['db_free_result']($request);
1338
1339
	return $moderators;
1340
}
1341
1342
/**
1343
 * Returns board's moderator groups with their names and link
1344
 *
1345
 * @param array $boards The boards to get moderator groups of
1346
 * @return array An array containing information about the groups assigned to moderate each board
1347
 */
1348
function getBoardModeratorGroups(array $boards)
1349
{
1350
	global $smcFunc, $scripturl, $txt;
1351
1352
	if (empty($boards))
1353
		return array();
1354
1355
	$request = $smcFunc['db_query']('', '
1356
		SELECT mg.id_group, mg.group_name, bg.id_board
1357
		FROM {db_prefix}moderator_groups AS bg
1358
			INNER JOIN {db_prefix}membergroups AS mg ON (mg.id_group = bg.id_group)
1359
		WHERE bg.id_board IN ({array_int:boards})',
1360
		array(
1361
			'boards' => $boards,
1362
		)
1363
	);
1364
	$groups = array();
1365
	while ($row = $smcFunc['db_fetch_assoc']($request))
1366
	{
1367
		if (empty($groups[$row['id_board']]))
1368
			$groups[$row['id_board']] = array();
1369
1370
		$groups[$row['id_board']][] = array(
1371
			'id' => $row['id_group'],
1372
			'name' => $row['group_name'],
1373
			'href' => $scripturl . '?action=groups;sa=members;group=' . $row['id_group'],
1374
			'link' => '<a href="' . $scripturl . '?action=groups;sa=members;group=' . $row['id_group'] . '" title="' . $txt['board_moderator'] . '">' . $row['group_name'] . '</a>',
1375
		);
1376
	}
1377
1378
	return $groups;
1379
}
1380
1381
/**
1382
 * Load a lot of useful information regarding the boards and categories.
1383
 * The information retrieved is stored in globals:
1384
 *  $boards		properties of each board.
1385
 *  $boardList	a list of boards grouped by category ID.
1386
 *  $cat_tree	properties of each category.
1387
 */
1388
function getBoardTree()
1389
{
1390
	global $cat_tree, $boards, $boardList, $smcFunc;
1391
1392
	$boardColumns = array(
1393
		'COALESCE(b.id_board, 0) AS id_board', 'b.id_parent', 'b.name AS board_name',
1394
		'b.description', 'b.child_level', 'b.board_order', 'b.count_posts', 'b.member_groups',
1395
		'b.id_theme', 'b.override_theme', 'b.id_profile', 'b.redirect', 'b.num_posts',
1396
		'b.num_topics', 'b.deny_member_groups', 'c.id_cat', 'c.name AS cat_name',
1397
		'c.description AS cat_desc', 'c.cat_order', 'c.can_collapse',
1398
	);
1399
	$boardParameters = array();
1400
	$boardJoins = array();
1401
	$boardWhere = array();
1402
	$boardOrder = array('c.cat_order', 'b.child_level', 'b.board_order');
1403
1404
	// Let mods add extra columns, parameters, etc., to the SELECT query
1405
	call_integration_hook('integrate_pre_boardtree', array(&$boardColumns, &$boardParameters, &$boardJoins, &$boardWhere, &$boardOrder));
1406
1407
	$boardColumns = array_unique($boardColumns);
1408
	$boardParameters = array_unique($boardParameters);
1409
	$boardJoins = array_unique($boardJoins);
1410
	$boardWhere = array_unique($boardWhere);
1411
	$boardOrder = array_unique($boardOrder);
1412
1413
	// Getting all the board and category information you'd ever wanted.
1414
	$request = $smcFunc['db_query']('', '
1415
		SELECT
1416
			' . implode(', ', $boardColumns) . '
1417
		FROM {db_prefix}categories AS c
1418
			LEFT JOIN {db_prefix}boards AS b ON (b.id_cat = c.id_cat)' . implode('
1419
			', $boardJoins) . '
1420
		WHERE {query_see_board}' . (empty($boardWhere) ? '' : '
1421
			AND (' . implode(') AND (', $boardWhere) . ')') . '
1422
		ORDER BY ' . implode(', ', $boardOrder),
1423
		$boardParameters
1424
	);
1425
	$cat_tree = array();
1426
	$boards = array();
1427
	$last_board_order = 0;
1428
	while ($row = $smcFunc['db_fetch_assoc']($request))
1429
	{
1430
		if (!isset($cat_tree[$row['id_cat']]))
1431
		{
1432
			$cat_tree[$row['id_cat']] = array(
1433
				'node' => array(
1434
					'id' => $row['id_cat'],
1435
					'name' => $row['cat_name'],
1436
					'description' => $row['cat_desc'],
1437
					'order' => $row['cat_order'],
1438
					'can_collapse' => $row['can_collapse']
1439
				),
1440
				'is_first' => empty($cat_tree),
1441
				'last_board_order' => $last_board_order,
1442
				'children' => array()
1443
			);
1444
			$prevBoard = 0;
1445
			$curLevel = 0;
1446
		}
1447
1448
		if (!empty($row['id_board']))
1449
		{
1450
			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...
1451
				$prevBoard = 0;
1452
1453
			$boards[$row['id_board']] = array(
1454
				'id' => $row['id_board'],
1455
				'category' => $row['id_cat'],
1456
				'parent' => $row['id_parent'],
1457
				'level' => $row['child_level'],
1458
				'order' => $row['board_order'],
1459
				'name' => $row['board_name'],
1460
				'member_groups' => explode(',', $row['member_groups']),
1461
				'deny_groups' => explode(',', $row['deny_member_groups']),
1462
				'description' => $row['description'],
1463
				'count_posts' => empty($row['count_posts']),
1464
				'posts' => $row['num_posts'],
1465
				'topics' => $row['num_topics'],
1466
				'theme' => $row['id_theme'],
1467
				'override_theme' => $row['override_theme'],
1468
				'profile' => $row['id_profile'],
1469
				'redirect' => $row['redirect'],
1470
				'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...
1471
			);
1472
			$prevBoard = $row['id_board'];
1473
			$last_board_order = $row['board_order'];
1474
1475
			if (empty($row['child_level']))
1476
			{
1477
				$cat_tree[$row['id_cat']]['children'][$row['id_board']] = array(
1478
					'node' => &$boards[$row['id_board']],
1479
					'is_first' => empty($cat_tree[$row['id_cat']]['children']),
1480
					'children' => array()
1481
				);
1482
				$boards[$row['id_board']]['tree'] = &$cat_tree[$row['id_cat']]['children'][$row['id_board']];
1483
			}
1484
			else
1485
			{
1486
				// Parent doesn't exist!
1487
				if (!isset($boards[$row['id_parent']]['tree']))
1488
					fatal_lang_error('no_valid_parent', false, array($row['board_name']));
1489
1490
				// Wrong childlevel...we can silently fix this...
1491
				if ($boards[$row['id_parent']]['tree']['node']['level'] != $row['child_level'] - 1)
1492
					$smcFunc['db_query']('', '
1493
						UPDATE {db_prefix}boards
1494
						SET child_level = {int:new_child_level}
1495
						WHERE id_board = {int:selected_board}',
1496
						array(
1497
							'new_child_level' => $boards[$row['id_parent']]['tree']['node']['level'] + 1,
1498
							'selected_board' => $row['id_board'],
1499
						)
1500
					);
1501
1502
				$boards[$row['id_parent']]['tree']['children'][$row['id_board']] = array(
1503
					'node' => &$boards[$row['id_board']],
1504
					'is_first' => empty($boards[$row['id_parent']]['tree']['children']),
1505
					'children' => array()
1506
				);
1507
				$boards[$row['id_board']]['tree'] = &$boards[$row['id_parent']]['tree']['children'][$row['id_board']];
1508
			}
1509
		}
1510
1511
		// If mods want to do anything with this board before we move on, now's the time
1512
		call_integration_hook('integrate_boardtree_board', array($row));
1513
	}
1514
	$smcFunc['db_free_result']($request);
1515
1516
	// Get a list of all the boards in each category (using recursion).
1517
	$boardList = array();
1518
	foreach ($cat_tree as $catID => $node)
1519
	{
1520
		$boardList[$catID] = array();
1521
		recursiveBoards($boardList[$catID], $node);
1522
	}
1523
}
1524
1525
/**
1526
 * Recursively get a list of boards.
1527
 * Used by getBoardTree
1528
 *
1529
 * @param array &$_boardList The board list
1530
 * @param array &$_tree The board tree
1531
 */
1532
function recursiveBoards(&$_boardList, &$_tree)
1533
{
1534
	if (empty($_tree['children']))
1535
		return;
1536
1537
	foreach ($_tree['children'] as $id => $node)
1538
	{
1539
		$_boardList[] = $id;
1540
		recursiveBoards($_boardList, $node);
1541
	}
1542
}
1543
1544
/**
1545
 * Returns whether the child board id is actually a child of the parent (recursive).
1546
 *
1547
 * @param int $child The ID of the child board
1548
 * @param int $parent The ID of a parent board
1549
 * @return boolean Whether the specified child board is actually a child of the specified parent board.
1550
 */
1551
function isChildOf($child, $parent)
1552
{
1553
	global $boards;
1554
1555
	if (empty($boards[$child]['parent']))
1556
		return false;
1557
1558
	if ($boards[$child]['parent'] == $parent)
1559
		return true;
1560
1561
	return isChildOf($boards[$child]['parent'], $parent);
1562
}
1563
1564
function setBoardParsedDescription($category_id = 0, $boards_info = array())
1565
{
1566
	global $context;
1567
1568
	if (empty($category_id) || empty($boards_info))
1569
		return array();
1570
1571
	// Get the data we already parsed
1572
	$already_parsed_boards = getBoardsParsedDescription($category_id);
1573
1574
	foreach ($boards_info as $board_id => $board_description)
1575
		$already_parsed_boards[$board_id] = parse_bbc(
1576
			$board_description,
1577
			false,
1578
			'',
1579
			$context['description_allowed_tags']
1580
		);
1581
1582
	cache_put_data('parsed_boards_descriptions_'. $category_id, $already_parsed_boards, 864000);
1583
1584
	return $already_parsed_boards;
1585
}
1586
1587
function getBoardsParsedDescription($category_id = 0)
1588
{
1589
	if (empty($category_id))
1590
		return array();
1591
1592
	return (array) cache_get_data('parsed_boards_descriptions_' . $category_id, 864000);
1593
}
1594
1595
?>