Issues (1061)

Sources/Recent.php (1 issue)

1
<?php
2
3
/**
4
 * Find and retrieve information about recently posted topics, messages, and the like.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines https://www.simplemachines.org
10
 * @copyright 2020 Simple Machines and individual contributors
11
 * @license https://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 RC2
14
 */
15
16
if (!defined('SMF'))
17
	die('No direct access...');
18
19
/**
20
 * Get the latest post made on the system
21
 *
22
 * - respects approved, recycled, and board permissions
23
 * - @todo is this even used anywhere?
24
 *
25
 * @return array An array of information about the last post that you can see
26
 */
27
function getLastPost()
28
{
29
	global $scripturl, $modSettings, $smcFunc;
30
31
	// Find it by the board - better to order by board than sort the entire messages table.
32
	$request = $smcFunc['db_query']('substring', '
33
		SELECT m.poster_time, m.subject, m.id_topic, m.poster_name, SUBSTRING(m.body, 1, 385) AS body,
34
			m.smileys_enabled
35
		FROM {db_prefix}messages AS m' . (!empty($modSettings['postmod_active']) ? '
36
			INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)' : '') . '
37
		WHERE {query_wanna_see_message_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
38
			AND m.id_board != {int:recycle_board}' : '') . (!empty($modSettings['postmod_active']) ? '
39
			AND m.approved = {int:is_approved}
40
			AND t.approved = {int:is_approved}' : '') . '
41
		ORDER BY m.id_msg DESC
42
		LIMIT 1',
43
		array(
44
			'recycle_board' => $modSettings['recycle_board'],
45
			'is_approved' => 1,
46
		)
47
	);
48
	if ($smcFunc['db_num_rows']($request) == 0)
49
		return array();
50
	$row = $smcFunc['db_fetch_assoc']($request);
51
	$smcFunc['db_free_result']($request);
52
53
	// Censor the subject and post...
54
	censorText($row['subject']);
55
	censorText($row['body']);
56
57
	$row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled']), array('<br>' => '&#10;')));
58
	if ($smcFunc['strlen']($row['body']) > 128)
59
		$row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...';
60
61
	// Send the data.
62
	return array(
63
		'topic' => $row['id_topic'],
64
		'subject' => $row['subject'],
65
		'short_subject' => shorten_subject($row['subject'], 24),
66
		'preview' => $row['body'],
67
		'time' => timeformat($row['poster_time']),
68
		'timestamp' => forum_time(true, $row['poster_time']),
69
		'href' => $scripturl . '?topic=' . $row['id_topic'] . '.new;topicseen#new',
70
		'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.new;topicseen#new">' . $row['subject'] . '</a>'
71
	);
72
}
73
74
/**
75
 * Find the ten most recent posts.
76
 */
77
function RecentPosts()
78
{
79
	global $txt, $scripturl, $user_info, $context, $modSettings, $board, $smcFunc, $cache_enable;
80
81
	loadTemplate('Recent');
82
	$context['page_title'] = $txt['recent_posts'];
83
	$context['sub_template'] = 'recent';
84
85
	$context['is_redirect'] = false;
86
87
	if (isset($_REQUEST['start']) && $_REQUEST['start'] > 95)
88
		$_REQUEST['start'] = 95;
89
90
	$_REQUEST['start'] = (int) $_REQUEST['start'];
91
92
	$query_parameters = array();
93
	if (!empty($_REQUEST['c']) && empty($board))
94
	{
95
		$_REQUEST['c'] = explode(',', $_REQUEST['c']);
96
		foreach ($_REQUEST['c'] as $i => $c)
97
			$_REQUEST['c'][$i] = (int) $c;
98
99
		if (count($_REQUEST['c']) == 1)
100
		{
101
			$request = $smcFunc['db_query']('', '
102
				SELECT name
103
				FROM {db_prefix}categories
104
				WHERE id_cat = {int:id_cat}
105
				LIMIT 1',
106
				array(
107
					'id_cat' => $_REQUEST['c'][0],
108
				)
109
			);
110
			list ($name) = $smcFunc['db_fetch_row']($request);
111
			$smcFunc['db_free_result']($request);
112
113
			if (empty($name))
114
				fatal_lang_error('no_access', false);
115
116
			$context['linktree'][] = array(
117
				'url' => $scripturl . '#c' . (int) $_REQUEST['c'],
118
				'name' => $name
119
			);
120
		}
121
122
		$recycling = !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']);
123
124
		$request = $smcFunc['db_query']('', '
125
			SELECT b.id_board, b.num_posts
126
			FROM {db_prefix}boards AS b
127
			WHERE b.id_cat IN ({array_int:category_list})
128
				AND b.redirect = {string:empty}' . ($recycling ? '
129
				AND b.id_board != {int:recycle_board}' : '') . '
130
				AND {query_wanna_see_board}',
131
			array(
132
				'category_list' => $_REQUEST['c'],
133
				'empty' => '',
134
				'recycle_board' => !empty($modSettings['recycle_board']) ? $modSettings['recycle_board'] : 0,
135
			)
136
		);
137
		$total_cat_posts = 0;
138
		$boards = array();
139
		while ($row = $smcFunc['db_fetch_assoc']($request))
140
		{
141
			$boards[] = $row['id_board'];
142
			$total_cat_posts += $row['num_posts'];
143
		}
144
		$smcFunc['db_free_result']($request);
145
146
		if (empty($boards))
147
			fatal_lang_error('error_no_boards_selected');
148
149
		$query_this_board = 'm.id_board IN ({array_int:boards})';
150
		$query_parameters['boards'] = $boards;
151
152
		// If this category has a significant number of posts in it...
153
		if ($total_cat_posts > 100 && $total_cat_posts > $modSettings['totalMessages'] / 15)
154
		{
155
			$query_this_board .= '
156
					AND m.id_msg >= {int:max_id_msg}';
157
			$query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 400 - $_REQUEST['start'] * 7);
158
		}
159
160
		$context['page_index'] = constructPageIndex($scripturl . '?action=recent;c=' . implode(',', $_REQUEST['c']), $_REQUEST['start'], min(100, $total_cat_posts), 10, false);
161
	}
162
	elseif (!empty($_REQUEST['boards']))
163
	{
164
		$_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
165
		foreach ($_REQUEST['boards'] as $i => $b)
166
			$_REQUEST['boards'][$i] = (int) $b;
167
168
		$request = $smcFunc['db_query']('', '
169
			SELECT b.id_board, b.num_posts
170
			FROM {db_prefix}boards AS b
171
			WHERE b.id_board IN ({array_int:board_list})
172
				AND b.redirect = {string:empty}
173
				AND {query_see_board}
174
			LIMIT {int:limit}',
175
			array(
176
				'board_list' => $_REQUEST['boards'],
177
				'limit' => count($_REQUEST['boards']),
178
				'empty' => '',
179
			)
180
		);
181
		$total_posts = 0;
182
		$boards = array();
183
		while ($row = $smcFunc['db_fetch_assoc']($request))
184
		{
185
			$boards[] = $row['id_board'];
186
			$total_posts += $row['num_posts'];
187
		}
188
		$smcFunc['db_free_result']($request);
189
190
		if (empty($boards))
191
			fatal_lang_error('error_no_boards_selected');
192
193
		$query_this_board = 'm.id_board IN ({array_int:boards})';
194
		$query_parameters['boards'] = $boards;
195
196
		// If these boards have a significant number of posts in them...
197
		if ($total_posts > 100 && $total_posts > $modSettings['totalMessages'] / 12)
198
		{
199
			$query_this_board .= '
200
					AND m.id_msg >= {int:max_id_msg}';
201
			$query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 500 - $_REQUEST['start'] * 9);
202
		}
203
204
		$context['page_index'] = constructPageIndex($scripturl . '?action=recent;boards=' . implode(',', $_REQUEST['boards']), $_REQUEST['start'], min(100, $total_posts), 10, false);
205
	}
206
	elseif (!empty($board))
207
	{
208
		$request = $smcFunc['db_query']('', '
209
			SELECT num_posts, redirect
210
			FROM {db_prefix}boards
211
			WHERE id_board = {int:current_board}
212
			LIMIT 1',
213
			array(
214
				'current_board' => $board,
215
			)
216
		);
217
		list ($total_posts, $redirect) = $smcFunc['db_fetch_row']($request);
218
		$smcFunc['db_free_result']($request);
219
220
		// If this is a redirection board, don't bother counting topics here...
221
		if ($redirect != '')
222
		{
223
			$total_posts = 0;
224
			$context['is_redirect'] = true;
225
		}
226
227
		$query_this_board = 'm.id_board = {int:board}';
228
		$query_parameters['board'] = $board;
229
230
		// If this board has a significant number of posts in it...
231
		if ($total_posts > 80 && $total_posts > $modSettings['totalMessages'] / 10)
232
		{
233
			$query_this_board .= '
234
					AND m.id_msg >= {int:max_id_msg}';
235
			$query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 600 - $_REQUEST['start'] * 10);
236
		}
237
238
		$context['page_index'] = constructPageIndex($scripturl . '?action=recent;board=' . $board . '.%1$d', $_REQUEST['start'], min(100, $total_posts), 10, true);
239
	}
240
	else
241
	{
242
		$query_this_board = '{query_wanna_see_message_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
243
					AND m.id_board != {int:recycle_board}' : '') . '
244
					AND m.id_msg >= {int:max_id_msg}';
245
		$query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 100 - $_REQUEST['start'] * 6);
246
		$query_parameters['recycle_board'] = $modSettings['recycle_board'];
247
248
		$query_these_boards = '{query_wanna_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
249
					AND b.id_board != {int:recycle_board}' : '');
250
		$query_these_boards_params = $query_parameters;
251
		unset($query_these_boards_params['max_id_msg']);
252
253
		$get_num_posts = $smcFunc['db_query']('', '
254
			SELECT COALESCE(SUM(b.num_posts), 0)
255
			FROM {db_prefix}boards AS b
256
			WHERE ' . $query_these_boards . '
257
				AND b.redirect = {string:empty}',
258
			array_merge($query_these_boards_params, array('empty' => ''))
259
		);
260
261
		list($db_num_posts) = $smcFunc['db_fetch_row']($get_num_posts);
262
		$num_posts = min(100, $db_num_posts);
263
264
		$smcFunc['db_free_result']($get_num_posts);
265
266
		$context['page_index'] = constructPageIndex($scripturl . '?action=recent', $_REQUEST['start'], $num_posts, 10, false);
267
	}
268
269
	$context['linktree'][] = array(
270
		'url' => $scripturl . '?action=recent' . (empty($board) ? (empty($_REQUEST['c']) ? '' : ';c=' . (int) $_REQUEST['c']) : ';board=' . $board . '.0'),
271
		'name' => $context['page_title']
272
	);
273
274
	// If you selected a redirection board, don't try getting posts for it...
275
	if ($context['is_redirect'])
276
		$messages = 0;
277
278
	$key = 'recent-' . $user_info['id'] . '-' . md5($smcFunc['json_encode'](array_diff_key($query_parameters, array('max_id_msg' => 0)))) . '-' . (int) $_REQUEST['start'];
279
	if (!$context['is_redirect'] && (empty($cache_enable) || ($messages = cache_get_data($key, 120)) == null))
280
	{
281
		$done = false;
282
		while (!$done)
283
		{
284
			// Find the 10 most recent messages they can *view*.
285
			// @todo SLOW This query is really slow still, probably?
286
			$request = $smcFunc['db_query']('', '
287
				SELECT m.id_msg
288
				FROM {db_prefix}messages AS m ' . (!empty($modSettings['postmod_active']) ? '
289
					INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)' : '') . '
290
				WHERE ' . $query_this_board . (!empty($modSettings['postmod_active']) ? '
291
					AND m.approved = {int:is_approved}
292
					AND t.approved = {int:is_approved}' : '') . '
293
				ORDER BY m.id_msg DESC
294
				LIMIT {int:offset}, {int:limit}',
295
				array_merge($query_parameters, array(
296
					'is_approved' => 1,
297
					'offset' => $_REQUEST['start'],
298
					'limit' => 10,
299
				))
300
			);
301
			// If we don't have 10 results, try again with an unoptimized version covering all rows, and cache the result.
302
			if (isset($query_parameters['max_id_msg']) && $smcFunc['db_num_rows']($request) < 10)
303
			{
304
				$smcFunc['db_free_result']($request);
305
				$query_this_board = str_replace('AND m.id_msg >= {int:max_id_msg}', '', $query_this_board);
306
				$cache_results = true;
307
				unset($query_parameters['max_id_msg']);
308
			}
309
			else
310
				$done = true;
311
		}
312
		$messages = array();
313
		while ($row = $smcFunc['db_fetch_assoc']($request))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $request does not seem to be defined for all execution paths leading up to this point.
Loading history...
314
			$messages[] = $row['id_msg'];
315
		$smcFunc['db_free_result']($request);
316
		if (!empty($cache_results))
317
			cache_put_data($key, $messages, 120);
318
	}
319
320
	// Nothing here... Or at least, nothing you can see...
321
	if (empty($messages))
322
	{
323
		$context['posts'] = array();
324
		return;
325
	}
326
327
	// Get all the most recent posts.
328
	$request = $smcFunc['db_query']('', '
329
		SELECT
330
			m.id_msg, m.subject, m.smileys_enabled, m.poster_time, m.body, m.id_topic, t.id_board, b.id_cat,
331
			b.name AS bname, c.name AS cname, t.num_replies, m.id_member, m2.id_member AS id_first_member,
332
			COALESCE(mem2.real_name, m2.poster_name) AS first_poster_name, t.id_first_msg,
333
			COALESCE(mem.real_name, m.poster_name) AS poster_name, t.id_last_msg
334
		FROM {db_prefix}messages AS m
335
			INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
336
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
337
			INNER JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
338
			INNER JOIN {db_prefix}messages AS m2 ON (m2.id_msg = t.id_first_msg)
339
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
340
			LEFT JOIN {db_prefix}members AS mem2 ON (mem2.id_member = m2.id_member)
341
		WHERE m.id_msg IN ({array_int:message_list})
342
		ORDER BY m.id_msg DESC
343
		LIMIT {int:limit}',
344
		array(
345
			'message_list' => $messages,
346
			'limit' => count($messages),
347
		)
348
	);
349
	$counter = $_REQUEST['start'] + 1;
350
	$context['posts'] = array();
351
	$board_ids = array('own' => array(), 'any' => array());
352
	while ($row = $smcFunc['db_fetch_assoc']($request))
353
	{
354
		// Censor everything.
355
		censorText($row['body']);
356
		censorText($row['subject']);
357
358
		// BBC-atize the message.
359
		$row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
360
361
		// And build the array.
362
		$context['posts'][$row['id_msg']] = array(
363
			'id' => $row['id_msg'],
364
			'counter' => $counter++,
365
			'category' => array(
366
				'id' => $row['id_cat'],
367
				'name' => $row['cname'],
368
				'href' => $scripturl . '#c' . $row['id_cat'],
369
				'link' => '<a href="' . $scripturl . '#c' . $row['id_cat'] . '">' . $row['cname'] . '</a>'
370
			),
371
			'board' => array(
372
				'id' => $row['id_board'],
373
				'name' => $row['bname'],
374
				'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
375
				'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['bname'] . '</a>'
376
			),
377
			'topic' => $row['id_topic'],
378
			'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
379
			'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '" rel="nofollow" title="' . $row['subject'] . '">' . shorten_subject($row['subject'], 30) . '</a>',
380
			'start' => $row['num_replies'],
381
			'subject' => $row['subject'],
382
			'shorten_subject' => shorten_subject($row['subject'], 30),
383
			'time' => timeformat($row['poster_time']),
384
			'timestamp' => forum_time(true, $row['poster_time']),
385
			'first_poster' => array(
386
				'id' => $row['id_first_member'],
387
				'name' => $row['first_poster_name'],
388
				'href' => empty($row['id_first_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_first_member'],
389
				'link' => empty($row['id_first_member']) ? $row['first_poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_first_member'] . '">' . $row['first_poster_name'] . '</a>'
390
			),
391
			'poster' => array(
392
				'id' => $row['id_member'],
393
				'name' => $row['poster_name'],
394
				'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
395
				'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
396
			),
397
			'message' => $row['body'],
398
			'can_reply' => false,
399
			'can_delete' => false,
400
			'delete_possible' => ($row['id_first_msg'] != $row['id_msg'] || $row['id_last_msg'] == $row['id_msg']) && (empty($modSettings['edit_disable_time']) || $row['poster_time'] + $modSettings['edit_disable_time'] * 60 >= time()),
401
			'css_class' => 'windowbg',
402
		);
403
404
		if ($user_info['id'] == $row['id_first_member'])
405
			$board_ids['own'][$row['id_board']][] = $row['id_msg'];
406
		$board_ids['any'][$row['id_board']][] = $row['id_msg'];
407
	}
408
	$smcFunc['db_free_result']($request);
409
410
	// There might be - and are - different permissions between any and own.
411
	$permissions = array(
412
		'own' => array(
413
			'post_reply_own' => 'can_reply',
414
			'delete_own' => 'can_delete',
415
		),
416
		'any' => array(
417
			'post_reply_any' => 'can_reply',
418
			'delete_any' => 'can_delete',
419
		)
420
	);
421
422
	// Create an array for the permissions.
423
	$boards_can = boardsAllowedTo(array_keys(iterator_to_array(
424
		new RecursiveIteratorIterator(new RecursiveArrayIterator($permissions)))
425
	), true, false);
426
427
	// Now go through all the permissions, looking for boards they can do it on.
428
	foreach ($permissions as $type => $list)
429
	{
430
		foreach ($list as $permission => $allowed)
431
		{
432
			// They can do it on these boards...
433
			$boards = $boards_can[$permission];
434
435
			// If 0 is the only thing in the array, they can do it everywhere!
436
			if (!empty($boards) && $boards[0] == 0)
437
				$boards = array_keys($board_ids[$type]);
438
439
			// Go through the boards, and look for posts they can do this on.
440
			foreach ($boards as $board_id)
441
			{
442
				// Hmm, they have permission, but there are no topics from that board on this page.
443
				if (!isset($board_ids[$type][$board_id]))
444
					continue;
445
446
				// Okay, looks like they can do it for these posts.
447
				foreach ($board_ids[$type][$board_id] as $counter)
448
					if ($type == 'any' || $context['posts'][$counter]['poster']['id'] == $user_info['id'])
449
						$context['posts'][$counter][$allowed] = true;
450
			}
451
		}
452
	}
453
454
	$quote_enabled = empty($modSettings['disabledBBC']) || !in_array('quote', explode(',', $modSettings['disabledBBC']));
455
	foreach ($context['posts'] as $counter => $dummy)
456
	{
457
		// Some posts - the first posts - can't just be deleted.
458
		$context['posts'][$counter]['can_delete'] &= $context['posts'][$counter]['delete_possible'];
459
460
		// And some cannot be quoted...
461
		$context['posts'][$counter]['can_quote'] = $context['posts'][$counter]['can_reply'] && $quote_enabled;
462
	}
463
464
	// Last but not least, the quickbuttons
465
	foreach ($context['posts'] as $key => $post)
466
	{
467
		$context['posts'][$key]['quickbuttons'] = array(
468
			'reply' => array(
469
				'label' => $txt['reply'],
470
				'href' => $scripturl.'?action=post;topic='.$post['topic'].'.'.$post['start'],
471
				'icon' => 'reply_button',
472
				'show' => $post['can_reply']
473
			),
474
			'quote' => array(
475
				'label' => $txt['quote_action'],
476
				'href' => $scripturl.'?action=post;topic='.$post['topic'].'.'.$post['start'].';quote='.$post['id'],
477
				'icon' => 'quote',
478
				'show' => $post['can_quote']
479
			),
480
			'delete' => array(
481
				'label' => $txt['remove'],
482
				'href' => $scripturl.'?action=deletemsg;msg='.$post['id'].';topic='.$post['topic'].';recent;'.$context['session_var'].'='.$context['session_id'],
483
				'javascript' => 'data-confirm="'.$txt['remove_message'].'"',
484
				'class' => 'you_sure',
485
				'icon' => 'remove_button',
486
				'show' => $post['can_delete']
487
			),
488
		);
489
	}
490
491
	// Allow last minute changes.
492
	call_integration_hook('integrate_recent_RecentPosts');
493
}
494
495
/**
496
 * Find unread topics and replies.
497
 */
498
function UnreadTopics()
499
{
500
	global $board, $txt, $scripturl, $sourcedir;
501
	global $user_info, $context, $settings, $modSettings, $smcFunc, $options;
502
503
	// Guests can't have unread things, we don't know anything about them.
504
	is_not_guest();
505
506
	// Prefetching + lots of MySQL work = bad mojo.
507
	if (isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch')
508
	{
509
		ob_end_clean();
510
		send_http_status(403);
511
		die;
512
	}
513
514
	$context['showCheckboxes'] = !empty($options['display_quick_mod']) && $options['display_quick_mod'] == 1;
515
516
	$context['showing_all_topics'] = isset($_GET['all']);
517
	$context['start'] = (int) $_REQUEST['start'];
518
	$context['topics_per_page'] = empty($modSettings['disableCustomPerPage']) && !empty($options['topics_per_page']) ? $options['topics_per_page'] : $modSettings['defaultMaxTopics'];
519
	if ($_REQUEST['action'] == 'unread')
520
		$context['page_title'] = $context['showing_all_topics'] ? $txt['unread_topics_all'] : $txt['unread_topics_visit'];
521
	else
522
		$context['page_title'] = $txt['unread_replies'];
523
524
	if ($context['showing_all_topics'] && !empty($context['load_average']) && !empty($modSettings['loadavg_allunread']) && $context['load_average'] >= $modSettings['loadavg_allunread'])
525
		fatal_lang_error('loadavg_allunread_disabled', false);
526
	elseif ($_REQUEST['action'] != 'unread' && !empty($context['load_average']) && !empty($modSettings['loadavg_unreadreplies']) && $context['load_average'] >= $modSettings['loadavg_unreadreplies'])
527
		fatal_lang_error('loadavg_unreadreplies_disabled', false);
528
	elseif (!$context['showing_all_topics'] && $_REQUEST['action'] == 'unread' && !empty($context['load_average']) && !empty($modSettings['loadavg_unread']) && $context['load_average'] >= $modSettings['loadavg_unread'])
529
		fatal_lang_error('loadavg_unread_disabled', false);
530
531
	// Parameters for the main query.
532
	$query_parameters = array();
533
534
	// Are we specifying any specific board?
535
	if (isset($_REQUEST['children']) && (!empty($board) || !empty($_REQUEST['boards'])))
536
	{
537
		$boards = array();
538
539
		if (!empty($_REQUEST['boards']))
540
		{
541
			$_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
542
			foreach ($_REQUEST['boards'] as $b)
543
				$boards[] = (int) $b;
544
		}
545
546
		if (!empty($board))
547
			$boards[] = (int) $board;
548
549
		// 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
550
		$request = $smcFunc['db_query']('', '
551
			SELECT b.id_board, b.id_parent
552
			FROM {db_prefix}boards AS b
553
			WHERE {query_wanna_see_board}
554
				AND b.child_level > {int:no_child}
555
				AND b.id_board NOT IN ({array_int:boards})
556
			ORDER BY child_level ASC',
557
			array(
558
				'no_child' => 0,
559
				'boards' => $boards,
560
			)
561
		);
562
563
		while ($row = $smcFunc['db_fetch_assoc']($request))
564
			if (in_array($row['id_parent'], $boards))
565
				$boards[] = $row['id_board'];
566
567
		$smcFunc['db_free_result']($request);
568
569
		if (empty($boards))
570
			fatal_lang_error('error_no_boards_selected');
571
572
		$query_this_board = 'id_board IN ({array_int:boards})';
573
		$query_parameters['boards'] = $boards;
574
		$context['querystring_board_limits'] = ';boards=' . implode(',', $boards) . ';start=%d';
575
	}
576
	elseif (!empty($board))
577
	{
578
		$query_this_board = 'id_board = {int:board}';
579
		$query_parameters['board'] = $board;
580
		$context['querystring_board_limits'] = ';board=' . $board . '.%1$d';
581
	}
582
	elseif (!empty($_REQUEST['boards']))
583
	{
584
		$_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
585
		foreach ($_REQUEST['boards'] as $i => $b)
586
			$_REQUEST['boards'][$i] = (int) $b;
587
588
		$request = $smcFunc['db_query']('', '
589
			SELECT b.id_board
590
			FROM {db_prefix}boards AS b
591
			WHERE {query_see_board}
592
				AND b.id_board IN ({array_int:board_list})',
593
			array(
594
				'board_list' => $_REQUEST['boards'],
595
			)
596
		);
597
		$boards = array();
598
		while ($row = $smcFunc['db_fetch_assoc']($request))
599
			$boards[] = $row['id_board'];
600
		$smcFunc['db_free_result']($request);
601
602
		if (empty($boards))
603
			fatal_lang_error('error_no_boards_selected');
604
605
		$query_this_board = 'id_board IN ({array_int:boards})';
606
		$query_parameters['boards'] = $boards;
607
		$context['querystring_board_limits'] = ';boards=' . implode(',', $boards) . ';start=%1$d';
608
	}
609
	elseif (!empty($_REQUEST['c']))
610
	{
611
		$_REQUEST['c'] = explode(',', $_REQUEST['c']);
612
		foreach ($_REQUEST['c'] as $i => $c)
613
			$_REQUEST['c'][$i] = (int) $c;
614
615
		$see_board = isset($_REQUEST['action']) && $_REQUEST['action'] == 'unreadreplies' ? 'query_see_board' : 'query_wanna_see_board';
616
617
		$request = $smcFunc['db_query']('', '
618
			SELECT b.id_board
619
			FROM {db_prefix}boards AS b
620
			WHERE ' . $user_info[$see_board] . '
621
				AND b.id_cat IN ({array_int:id_cat})',
622
			array(
623
				'id_cat' => $_REQUEST['c'],
624
			)
625
		);
626
		$boards = array();
627
		while ($row = $smcFunc['db_fetch_assoc']($request))
628
			$boards[] = $row['id_board'];
629
		$smcFunc['db_free_result']($request);
630
631
		if (empty($boards))
632
			fatal_lang_error('error_no_boards_selected');
633
634
		$query_this_board = 'id_board IN ({array_int:boards})';
635
		$query_parameters['boards'] = $boards;
636
		$context['querystring_board_limits'] = ';c=' . implode(',', $_REQUEST['c']) . ';start=%1$d';
637
	}
638
	else
639
	{
640
		$see_board = isset($_REQUEST['action']) && $_REQUEST['action'] == 'unreadreplies' ? 'query_see_board' : 'query_wanna_see_board';
641
		// Don't bother to show deleted posts!
642
		$request = $smcFunc['db_query']('', '
643
			SELECT b.id_board
644
			FROM {db_prefix}boards AS b
645
			WHERE ' . $user_info[$see_board] . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
646
				AND b.id_board != {int:recycle_board}' : ''),
647
			array(
648
				'recycle_board' => (int) $modSettings['recycle_board'],
649
			)
650
		);
651
		$boards = array();
652
		while ($row = $smcFunc['db_fetch_assoc']($request))
653
			$boards[] = $row['id_board'];
654
		$smcFunc['db_free_result']($request);
655
656
		if (empty($boards))
657
			fatal_lang_error('error_no_boards_available', false);
658
659
		$query_this_board = 'id_board IN ({array_int:boards})';
660
		$query_parameters['boards'] = $boards;
661
		$context['querystring_board_limits'] = ';start=%1$d';
662
		$context['no_board_limits'] = true;
663
	}
664
665
	$sort_methods = array(
666
		'subject' => 'ms.subject',
667
		'starter' => 'COALESCE(mems.real_name, ms.poster_name)',
668
		'replies' => 't.num_replies',
669
		'views' => 't.num_views',
670
		'first_post' => 't.id_topic',
671
		'last_post' => 't.id_last_msg'
672
	);
673
674
	// The default is the most logical: newest first.
675
	if (!isset($_REQUEST['sort']) || !isset($sort_methods[$_REQUEST['sort']]))
676
	{
677
		$context['sort_by'] = 'last_post';
678
		$_REQUEST['sort'] = 't.id_last_msg';
679
		$ascending = isset($_REQUEST['asc']);
680
681
		$context['querystring_sort_limits'] = $ascending ? ';asc' : '';
682
	}
683
	// But, for other methods the default sort is ascending.
684
	else
685
	{
686
		$context['sort_by'] = $_REQUEST['sort'];
687
		$_REQUEST['sort'] = $sort_methods[$_REQUEST['sort']];
688
		$ascending = !isset($_REQUEST['desc']);
689
690
		$context['querystring_sort_limits'] = ';sort=' . $context['sort_by'] . ($ascending ? '' : ';desc');
691
	}
692
	$context['sort_direction'] = $ascending ? 'up' : 'down';
693
694
	if (!empty($_REQUEST['c']) && is_array($_REQUEST['c']) && count($_REQUEST['c']) == 1)
695
	{
696
		$request = $smcFunc['db_query']('', '
697
			SELECT name
698
			FROM {db_prefix}categories
699
			WHERE id_cat = {int:id_cat}
700
			LIMIT 1',
701
			array(
702
				'id_cat' => (int) $_REQUEST['c'][0],
703
			)
704
		);
705
		list ($name) = $smcFunc['db_fetch_row']($request);
706
		$smcFunc['db_free_result']($request);
707
708
		$context['linktree'][] = array(
709
			'url' => $scripturl . '#c' . (int) $_REQUEST['c'][0],
710
			'name' => $name
711
		);
712
	}
713
714
	$context['linktree'][] = array(
715
		'url' => $scripturl . '?action=' . $_REQUEST['action'] . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'],
716
		'name' => $_REQUEST['action'] == 'unread' ? $txt['unread_topics_visit'] : $txt['unread_replies']
717
	);
718
719
	if ($context['showing_all_topics'])
720
		$context['linktree'][] = array(
721
			'url' => $scripturl . '?action=' . $_REQUEST['action'] . ';all' . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'],
722
			'name' => $txt['unread_topics_all']
723
		);
724
	else
725
		$txt['unread_topics_visit_none'] = strtr($txt['unread_topics_visit_none'], array('?action=unread;all' => '?action=unread;all' . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits']));
726
727
	loadTemplate('Recent');
728
	loadTemplate('MessageIndex');
729
	$context['sub_template'] = $_REQUEST['action'] == 'unread' ? 'unread' : 'replies';
730
731
	// Setup the default topic icons... for checking they exist and the like ;)
732
	$context['icon_sources'] = array();
733
	foreach ($context['stable_icons'] as $icon)
734
		$context['icon_sources'][$icon] = 'images_url';
735
736
	$is_topics = $_REQUEST['action'] == 'unread';
737
738
	// This part is the same for each query.
739
	$select_clause = '
740
		ms.subject AS first_subject, ms.poster_time AS first_poster_time, ms.id_topic, t.id_board, b.name AS bname,
741
		t.num_replies, t.num_views, ms.id_member AS id_first_member, ml.id_member AS id_last_member,' . (!empty($settings['avatars_on_indexes']) ? ' meml.avatar, meml.email_address, mems.avatar AS first_poster_avatar, mems.email_address AS first_poster_email, COALESCE(af.id_attach, 0) AS first_poster_id_attach, af.filename AS first_poster_filename, af.attachment_type AS first_poster_attach_type, COALESCE(al.id_attach, 0) AS last_poster_id_attach, al.filename AS last_poster_filename, al.attachment_type AS last_poster_attach_type,' : '') . '
742
		ml.poster_time AS last_poster_time, COALESCE(mems.real_name, ms.poster_name) AS first_poster_name,
743
		COALESCE(meml.real_name, ml.poster_name) AS last_poster_name, ml.subject AS last_subject,
744
		ml.icon AS last_icon, ms.icon AS first_icon, t.id_poll, t.is_sticky, t.locked, ml.modified_time AS last_modified_time,
745
		COALESCE(lt.id_msg, lmr.id_msg, -1) + 1 AS new_from, SUBSTRING(ml.body, 1, 385) AS last_body,
746
		SUBSTRING(ms.body, 1, 385) AS first_body, ml.smileys_enabled AS last_smileys, ms.smileys_enabled AS first_smileys, t.id_first_msg, t.id_last_msg';
747
748
	if ($context['showing_all_topics'])
749
	{
750
		if (!empty($board))
751
		{
752
			$request = $smcFunc['db_query']('', '
753
				SELECT MIN(id_msg)
754
				FROM {db_prefix}log_mark_read
755
				WHERE id_member = {int:current_member}
756
					AND id_board = {int:current_board}',
757
				array(
758
					'current_board' => $board,
759
					'current_member' => $user_info['id'],
760
				)
761
			);
762
			list ($earliest_msg) = $smcFunc['db_fetch_row']($request);
763
			$smcFunc['db_free_result']($request);
764
		}
765
		else
766
		{
767
			$request = $smcFunc['db_query']('', '
768
				SELECT MIN(lmr.id_msg)
769
				FROM {db_prefix}boards AS b
770
					LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = {int:current_member})
771
				WHERE {query_see_board}',
772
				array(
773
					'current_member' => $user_info['id'],
774
				)
775
			);
776
			list ($earliest_msg) = $smcFunc['db_fetch_row']($request);
777
			$smcFunc['db_free_result']($request);
778
		}
779
780
		// This is needed in case of topics marked unread.
781
		if (empty($earliest_msg))
782
			$earliest_msg = 0;
783
		else
784
		{
785
			// Using caching, when possible, to ignore the below slow query.
786
			if (isset($_SESSION['cached_log_time']) && $_SESSION['cached_log_time'][0] + 45 > time())
787
				$earliest_msg2 = $_SESSION['cached_log_time'][1];
788
			else
789
			{
790
				// This query is pretty slow, but it's needed to ensure nothing crucial is ignored.
791
				$request = $smcFunc['db_query']('', '
792
					SELECT MIN(id_msg)
793
					FROM {db_prefix}log_topics
794
					WHERE id_member = {int:current_member}',
795
					array(
796
						'current_member' => $user_info['id'],
797
					)
798
				);
799
				list ($earliest_msg2) = $smcFunc['db_fetch_row']($request);
800
				$smcFunc['db_free_result']($request);
801
802
				// In theory this could be zero, if the first ever post is unread, so fudge it ;)
803
				if ($earliest_msg2 == 0)
804
					$earliest_msg2 = -1;
805
806
				$_SESSION['cached_log_time'] = array(time(), $earliest_msg2);
807
			}
808
809
			$earliest_msg = min($earliest_msg2, $earliest_msg);
810
		}
811
	}
812
813
	// @todo Add modified_time in for log_time check?
814
815
	if ($modSettings['totalMessages'] > 100000 && $context['showing_all_topics'])
816
	{
817
		$smcFunc['db_query']('', '
818
			DROP TABLE IF EXISTS {db_prefix}log_topics_unread',
819
			array(
820
			)
821
		);
822
823
		// Let's copy things out of the log_topics table, to reduce searching.
824
		$have_temp_table = $smcFunc['db_query']('', '
825
			CREATE TEMPORARY TABLE {db_prefix}log_topics_unread (
826
				PRIMARY KEY (id_topic)
827
			)
828
			SELECT lt.id_topic, lt.id_msg
829
			FROM {db_prefix}topics AS t
830
				INNER JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic)
831
			WHERE lt.id_member = {int:current_member}
832
				AND t.' . $query_this_board . (empty($earliest_msg) ? '' : '
833
				AND t.id_last_msg > {int:earliest_msg}') . ($modSettings['postmod_active'] ? '
834
				AND t.approved = {int:is_approved}' : '') . ' AND lt.unwatched != 1',
835
			array_merge($query_parameters, array(
836
				'current_member' => $user_info['id'],
837
				'earliest_msg' => !empty($earliest_msg) ? $earliest_msg : 0,
838
				'is_approved' => 1,
839
				'db_error_skip' => true,
840
			))
841
		) !== false;
842
	}
843
	else
844
		$have_temp_table = false;
845
846
	if ($context['showing_all_topics'] && $have_temp_table)
847
	{
848
		$request = $smcFunc['db_query']('', '
849
			SELECT COUNT(*), MIN(t.id_last_msg)
850
			FROM {db_prefix}topics AS t
851
				LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)
852
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
853
			WHERE t.' . $query_this_board . (!empty($earliest_msg) ? '
854
				AND t.id_last_msg > {int:earliest_msg}' : '') . '
855
				AND COALESCE(lt.id_msg, lmr.id_msg, 0) < t.id_last_msg' . ($modSettings['postmod_active'] ? '
856
				AND t.approved = {int:is_approved}' : ''),
857
			array_merge($query_parameters, array(
858
				'current_member' => $user_info['id'],
859
				'earliest_msg' => !empty($earliest_msg) ? $earliest_msg : 0,
860
				'is_approved' => 1,
861
			))
862
		);
863
		list ($num_topics, $min_message) = $smcFunc['db_fetch_row']($request);
864
		$smcFunc['db_free_result']($request);
865
866
		// Make sure the starting place makes sense and construct the page index.
867
		$context['page_index'] = constructPageIndex($scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . $context['querystring_board_limits'] . $context['querystring_sort_limits'], $_REQUEST['start'], $num_topics, $context['topics_per_page'], true);
868
		$context['current_page'] = (int) $_REQUEST['start'] / $context['topics_per_page'];
869
870
		$context['links'] = array(
871
			'first' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'] : '',
872
			'prev' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] - $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
873
			'next' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] + $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
874
			'last' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], floor(($num_topics - 1) / $context['topics_per_page']) * $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
875
			'up' => $scripturl,
876
		);
877
		$context['page_info'] = array(
878
			'current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1,
879
			'num_pages' => floor(($num_topics - 1) / $context['topics_per_page']) + 1
880
		);
881
882
		if ($num_topics == 0)
883
		{
884
			// Mark the boards as read if there are no unread topics!
885
			require_once($sourcedir . '/Subs-Boards.php');
886
			markBoardsRead(empty($boards) ? $board : $boards);
887
888
			$context['topics'] = array();
889
			$context['no_topic_listing'] = true;
890
			if ($context['querystring_board_limits'] == ';start=%1$d')
891
				$context['querystring_board_limits'] = '';
892
			else
893
				$context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
894
			return;
895
		}
896
		else
897
			$min_message = (int) $min_message;
898
899
		$request = $smcFunc['db_query']('substring', '
900
			SELECT ' . $select_clause . '
901
			FROM {db_prefix}messages AS ms
902
				INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ms.id_topic AND t.id_first_msg = ms.id_msg)
903
				INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
904
				LEFT JOIN {db_prefix}boards AS b ON (b.id_board = ms.id_board)
905
				LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)
906
				LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)' . (!empty($settings['avatars_on_indexes']) ? '
907
				LEFT JOIN {db_prefix}attachments AS af ON (af.id_member = mems.id_member)
908
				LEFT JOIN {db_prefix}attachments AS al ON (al.id_member = meml.id_member)' : '') . '
909
				LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)
910
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
911
			WHERE b.' . $query_this_board . '
912
				AND t.id_last_msg >= {int:min_message}
913
				AND COALESCE(lt.id_msg, lmr.id_msg, 0) < t.id_last_msg' . ($modSettings['postmod_active'] ? '
914
				AND ms.approved = {int:is_approved}' : '') . '
915
			ORDER BY {raw:sort}
916
			LIMIT {int:offset}, {int:limit}',
917
			array_merge($query_parameters, array(
918
				'current_member' => $user_info['id'],
919
				'min_message' => $min_message,
920
				'is_approved' => 1,
921
				'sort' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'),
922
				'offset' => $_REQUEST['start'],
923
				'limit' => $context['topics_per_page'],
924
			))
925
		);
926
	}
927
	elseif ($is_topics)
928
	{
929
		$request = $smcFunc['db_query']('', '
930
			SELECT COUNT(*), MIN(t.id_last_msg)
931
			FROM {db_prefix}topics AS t' . (!empty($have_temp_table) ? '
932
				LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)' : '
933
				LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member} AND lt.unwatched != 1)') . '
934
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
935
			WHERE t.' . $query_this_board . ($context['showing_all_topics'] && !empty($earliest_msg) ? '
936
				AND t.id_last_msg > {int:earliest_msg}' : (!$context['showing_all_topics'] && empty($_SESSION['first_login']) ? '
937
				AND t.id_last_msg > {int:id_msg_last_visit}' : '')) . '
938
				AND COALESCE(lt.id_msg, lmr.id_msg, 0) < t.id_last_msg' . ($modSettings['postmod_active'] ? '
939
				AND t.approved = {int:is_approved}' : '') . '',
940
			array_merge($query_parameters, array(
941
				'current_member' => $user_info['id'],
942
				'earliest_msg' => !empty($earliest_msg) ? $earliest_msg : 0,
943
				'id_msg_last_visit' => $_SESSION['id_msg_last_visit'],
944
				'is_approved' => 1,
945
			))
946
		);
947
		list ($num_topics, $min_message) = $smcFunc['db_fetch_row']($request);
948
		$smcFunc['db_free_result']($request);
949
950
		// Make sure the starting place makes sense and construct the page index.
951
		$context['page_index'] = constructPageIndex($scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . $context['querystring_board_limits'] . $context['querystring_sort_limits'], $_REQUEST['start'], $num_topics, $context['topics_per_page'], true);
952
		$context['current_page'] = (int) $_REQUEST['start'] / $context['topics_per_page'];
953
954
		$context['links'] = array(
955
			'first' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'] : '',
956
			'prev' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] - $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
957
			'next' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] + $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
958
			'last' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], floor(($num_topics - 1) / $context['topics_per_page']) * $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
959
			'up' => $scripturl,
960
		);
961
		$context['page_info'] = array(
962
			'current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1,
963
			'num_pages' => floor(($num_topics - 1) / $context['topics_per_page']) + 1
964
		);
965
966
		if ($num_topics == 0)
967
		{
968
			// Is this an all topics query?
969
			if ($context['showing_all_topics'])
970
			{
971
				// Since there are no unread topics, mark the boards as read!
972
				require_once($sourcedir . '/Subs-Boards.php');
973
				markBoardsRead(empty($boards) ? $board : $boards);
974
			}
975
976
			$context['topics'] = array();
977
			$context['no_topic_listing'] = true;
978
			if ($context['querystring_board_limits'] == ';start=%d')
979
				$context['querystring_board_limits'] = '';
980
			else
981
				$context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
982
			return;
983
		}
984
		else
985
			$min_message = (int) $min_message;
986
987
		$request = $smcFunc['db_query']('substring', '
988
			SELECT ' . $select_clause . '
989
			FROM {db_prefix}messages AS ms
990
				INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ms.id_topic AND t.id_first_msg = ms.id_msg)
991
				INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
992
				LEFT JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
993
				LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)
994
				LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)' . (!empty($settings['avatars_on_indexes']) ? '
995
				LEFT JOIN {db_prefix}attachments AS af ON (af.id_member = mems.id_member)
996
				LEFT JOIN {db_prefix}attachments AS al ON (al.id_member = meml.id_member)' : '') . '' . (!empty($have_temp_table) ? '
997
				LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)' : '
998
				LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member} AND lt.unwatched != 1)') . '
999
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
1000
			WHERE t.' . $query_this_board . '
1001
				AND t.id_last_msg >= {int:min_message}
1002
				AND COALESCE(lt.id_msg, lmr.id_msg, 0) < ml.id_msg' . ($modSettings['postmod_active'] ? '
1003
				AND ms.approved = {int:is_approved}' : '') . '
1004
			ORDER BY {raw:order}
1005
			LIMIT {int:offset}, {int:limit}',
1006
			array_merge($query_parameters, array(
1007
				'current_member' => $user_info['id'],
1008
				'min_message' => $min_message,
1009
				'is_approved' => 1,
1010
				'order' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'),
1011
				'offset' => $_REQUEST['start'],
1012
				'limit' => $context['topics_per_page'],
1013
			))
1014
		);
1015
	}
1016
	else
1017
	{
1018
		if ($modSettings['totalMessages'] > 100000)
1019
		{
1020
			$smcFunc['db_query']('', '
1021
				DROP TABLE IF EXISTS {db_prefix}topics_posted_in',
1022
				array(
1023
				)
1024
			);
1025
1026
			$smcFunc['db_query']('', '
1027
				DROP TABLE IF EXISTS {db_prefix}log_topics_posted_in',
1028
				array(
1029
				)
1030
			);
1031
1032
			$sortKey_joins = array(
1033
				'ms.subject' => '
1034
					INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)',
1035
				'COALESCE(mems.real_name, ms.poster_name)' => '
1036
					INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
1037
					LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)',
1038
			);
1039
1040
			// The main benefit of this temporary table is not that it's faster; it's that it avoids locks later.
1041
			$have_temp_table = $smcFunc['db_query']('', '
1042
				CREATE TEMPORARY TABLE {db_prefix}topics_posted_in (
1043
					id_topic mediumint(8) unsigned NOT NULL default {string:string_zero},
1044
					id_board smallint(5) unsigned NOT NULL default {string:string_zero},
1045
					id_last_msg int(10) unsigned NOT NULL default {string:string_zero},
1046
					id_msg int(10) unsigned NOT NULL default {string:string_zero},
1047
					PRIMARY KEY (id_topic)
1048
				)
1049
				SELECT t.id_topic, t.id_board, t.id_last_msg, COALESCE(lmr.id_msg, 0) AS id_msg' . (!in_array($_REQUEST['sort'], array('t.id_last_msg', 't.id_topic')) ? ', ' . $_REQUEST['sort'] . ' AS sort_key' : '') . '
1050
				FROM {db_prefix}messages AS m
1051
					INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
1052
					LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)
1053
					LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})' . (isset($sortKey_joins[$_REQUEST['sort']]) ? $sortKey_joins[$_REQUEST['sort']] : '') . '
1054
				WHERE m.id_member = {int:current_member}' . (!empty($board) ? '
1055
					AND t.id_board = {int:current_board}' : '') . ($modSettings['postmod_active'] ? '
1056
					AND t.approved = {int:is_approved}' : '') . '
1057
				GROUP BY m.id_topic',
1058
				array(
1059
					'current_board' => $board,
1060
					'current_member' => $user_info['id'],
1061
					'is_approved' => 1,
1062
					'string_zero' => '0',
1063
					'db_error_skip' => true,
1064
				)
1065
			) !== false;
1066
1067
			// If that worked, create a sample of the log_topics table too.
1068
			if ($have_temp_table)
1069
				$have_temp_table = $smcFunc['db_query']('', '
1070
					CREATE TEMPORARY TABLE {db_prefix}log_topics_posted_in (
1071
						PRIMARY KEY (id_topic)
1072
					)
1073
					SELECT lt.id_topic, lt.id_msg
1074
					FROM {db_prefix}log_topics AS lt
1075
						INNER JOIN {db_prefix}topics_posted_in AS pi ON (pi.id_topic = lt.id_topic)
1076
					WHERE lt.id_member = {int:current_member}',
1077
					array(
1078
						'current_member' => $user_info['id'],
1079
						'db_error_skip' => true,
1080
					)
1081
				) !== false;
1082
		}
1083
1084
		if (!empty($have_temp_table))
1085
		{
1086
			$request = $smcFunc['db_query']('', '
1087
				SELECT COUNT(*)
1088
				FROM {db_prefix}topics_posted_in AS pi
1089
					LEFT JOIN {db_prefix}log_topics_posted_in AS lt ON (lt.id_topic = pi.id_topic)
1090
				WHERE pi.' . $query_this_board . '
1091
					AND COALESCE(lt.id_msg, pi.id_msg) < pi.id_last_msg',
1092
				array_merge($query_parameters, array(
1093
				))
1094
			);
1095
			list ($num_topics) = $smcFunc['db_fetch_row']($request);
1096
			$smcFunc['db_free_result']($request);
1097
		}
1098
		else
1099
		{
1100
			$request = $smcFunc['db_query']('unread_fetch_topic_count', '
1101
				SELECT COUNT(DISTINCT t.id_topic), MIN(t.id_last_msg)
1102
				FROM {db_prefix}topics AS t
1103
					INNER JOIN {db_prefix}messages AS m ON (m.id_topic = t.id_topic)
1104
					LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
1105
					LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
1106
				WHERE t.' . $query_this_board . '
1107
					AND m.id_member = {int:current_member}
1108
					AND COALESCE(lt.id_msg, lmr.id_msg, 0) < t.id_last_msg' . ($modSettings['postmod_active'] ? '
1109
					AND t.approved = {int:is_approved}' : '') . ' AND lt.unwatched != 1',
1110
				array_merge($query_parameters, array(
1111
					'current_member' => $user_info['id'],
1112
					'is_approved' => 1,
1113
				))
1114
			);
1115
			list ($num_topics, $min_message) = $smcFunc['db_fetch_row']($request);
1116
			$smcFunc['db_free_result']($request);
1117
		}
1118
1119
		// Make sure the starting place makes sense and construct the page index.
1120
		$context['page_index'] = constructPageIndex($scripturl . '?action=' . $_REQUEST['action'] . $context['querystring_board_limits'] . $context['querystring_sort_limits'], $_REQUEST['start'], $num_topics, $context['topics_per_page'], true);
1121
		$context['current_page'] = (int) $_REQUEST['start'] / $context['topics_per_page'];
1122
1123
		$context['links'] = array(
1124
			'first' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'] : '',
1125
			'prev' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] - $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
1126
			'next' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] + $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
1127
			'last' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], floor(($num_topics - 1) / $context['topics_per_page']) * $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
1128
			'up' => $scripturl,
1129
		);
1130
		$context['page_info'] = array(
1131
			'current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1,
1132
			'num_pages' => floor(($num_topics - 1) / $context['topics_per_page']) + 1
1133
		);
1134
1135
		if ($num_topics == 0)
1136
		{
1137
			$context['topics'] = array();
1138
			$context['no_topic_listing'] = true;
1139
			if ($context['querystring_board_limits'] == ';start=%d')
1140
				$context['querystring_board_limits'] = '';
1141
			else
1142
				$context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
1143
			return;
1144
		}
1145
1146
		if (!empty($have_temp_table))
1147
			$request = $smcFunc['db_query']('', '
1148
				SELECT t.id_topic
1149
				FROM {db_prefix}topics_posted_in AS t
1150
					LEFT JOIN {db_prefix}log_topics_posted_in AS lt ON (lt.id_topic = t.id_topic)
1151
				WHERE t.' . $query_this_board . '
1152
					AND COALESCE(lt.id_msg, t.id_msg) < t.id_last_msg
1153
				ORDER BY {raw:order}
1154
				LIMIT {int:offset}, {int:limit}',
1155
				array_merge($query_parameters, array(
1156
					'order' => (in_array($_REQUEST['sort'], array('t.id_last_msg', 't.id_topic')) ? $_REQUEST['sort'] : 't.sort_key') . ($ascending ? '' : ' DESC'),
1157
					'offset' => $_REQUEST['start'],
1158
					'limit' => $context['topics_per_page'],
1159
				))
1160
			);
1161
		else
1162
			$request = $smcFunc['db_query']('', '
1163
				SELECT DISTINCT t.id_topic,' . $_REQUEST['sort'] . '
1164
				FROM {db_prefix}topics AS t
1165
					INNER JOIN {db_prefix}messages AS m ON (m.id_topic = t.id_topic AND m.id_member = {int:current_member})' . (strpos($_REQUEST['sort'], 'ms.') === false ? '' : '
1166
					INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)') . (strpos($_REQUEST['sort'], 'mems.') === false ? '' : '
1167
					LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)') . '
1168
					LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
1169
					LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
1170
				WHERE t.' . $query_this_board . '
1171
					AND t.id_last_msg >= {int:min_message}
1172
					AND (COALESCE(lt.id_msg, lmr.id_msg, 0)) < t.id_last_msg
1173
					AND t.approved = {int:is_approved} AND lt.unwatched != 1
1174
				ORDER BY {raw:order}
1175
				LIMIT {int:offset}, {int:limit}',
1176
				array_merge($query_parameters, array(
1177
					'current_member' => $user_info['id'],
1178
					'min_message' => (int) $min_message,
1179
					'is_approved' => 1,
1180
					'order' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'),
1181
					'offset' => $_REQUEST['start'],
1182
					'limit' => $context['topics_per_page'],
1183
					'sort' => $_REQUEST['sort'],
1184
				))
1185
			);
1186
1187
		$topics = array();
1188
		while ($row = $smcFunc['db_fetch_assoc']($request))
1189
			$topics[] = $row['id_topic'];
1190
		$smcFunc['db_free_result']($request);
1191
1192
		// Sanity... where have you gone?
1193
		if (empty($topics))
1194
		{
1195
			$context['topics'] = array();
1196
			$context['no_topic_listing'] = true;
1197
			if ($context['querystring_board_limits'] == ';start=%d')
1198
				$context['querystring_board_limits'] = '';
1199
			else
1200
				$context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
1201
			return;
1202
		}
1203
1204
		$request = $smcFunc['db_query']('substring', '
1205
			SELECT ' . $select_clause . '
1206
			FROM {db_prefix}topics AS t
1207
				INNER JOIN {db_prefix}messages AS ms ON (ms.id_topic = t.id_topic AND ms.id_msg = t.id_first_msg)
1208
				INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
1209
				INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
1210
				LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)
1211
				LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)' . (!empty($settings['avatars_on_indexes']) ? '
1212
				LEFT JOIN {db_prefix}attachments AS af ON (af.id_member = mems.id_member)
1213
				LEFT JOIN {db_prefix}attachments AS al ON (al.id_member = meml.id_member)' : '') . '
1214
				LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
1215
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
1216
			WHERE t.id_topic IN ({array_int:topic_list})
1217
			ORDER BY {raw:sort}' . ($ascending ? '' : ' DESC') . '
1218
			LIMIT {int:limit}',
1219
			array(
1220
				'current_member' => $user_info['id'],
1221
				'topic_list' => $topics,
1222
				'sort' => $_REQUEST['sort'],
1223
				'limit' => count($topics),
1224
			)
1225
		);
1226
	}
1227
1228
	$context['topics'] = array();
1229
	$topic_ids = array();
1230
	$recycle_board = !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) ? $modSettings['recycle_board'] : 0;
1231
1232
	while ($row = $smcFunc['db_fetch_assoc']($request))
1233
	{
1234
		if ($row['id_poll'] > 0 && $modSettings['pollMode'] == '0')
1235
			continue;
1236
1237
		$topic_ids[] = $row['id_topic'];
1238
1239
		if (!empty($modSettings['preview_characters']))
1240
		{
1241
			// Limit them to 128 characters - do this FIRST because it's a lot of wasted censoring otherwise.
1242
			$row['first_body'] = strip_tags(strtr(parse_bbc($row['first_body'], $row['first_smileys'], $row['id_first_msg']), array('<br>' => '&#10;')));
1243
			if ($smcFunc['strlen']($row['first_body']) > 128)
1244
				$row['first_body'] = $smcFunc['substr']($row['first_body'], 0, 128) . '...';
1245
			$row['last_body'] = strip_tags(strtr(parse_bbc($row['last_body'], $row['last_smileys'], $row['id_last_msg']), array('<br>' => '&#10;')));
1246
			if ($smcFunc['strlen']($row['last_body']) > 128)
1247
				$row['last_body'] = $smcFunc['substr']($row['last_body'], 0, 128) . '...';
1248
1249
			// Censor the subject and message preview.
1250
			censorText($row['first_subject']);
1251
			censorText($row['first_body']);
1252
1253
			// Don't censor them twice!
1254
			if ($row['id_first_msg'] == $row['id_last_msg'])
1255
			{
1256
				$row['last_subject'] = $row['first_subject'];
1257
				$row['last_body'] = $row['first_body'];
1258
			}
1259
			else
1260
			{
1261
				censorText($row['last_subject']);
1262
				censorText($row['last_body']);
1263
			}
1264
		}
1265
		else
1266
		{
1267
			$row['first_body'] = '';
1268
			$row['last_body'] = '';
1269
			censorText($row['first_subject']);
1270
1271
			if ($row['id_first_msg'] == $row['id_last_msg'])
1272
				$row['last_subject'] = $row['first_subject'];
1273
			else
1274
				censorText($row['last_subject']);
1275
		}
1276
1277
		// Decide how many pages the topic should have.
1278
		$topic_length = $row['num_replies'] + 1;
1279
		$messages_per_page = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
1280
		if ($topic_length > $messages_per_page)
1281
		{
1282
			$start = -1;
1283
			$pages = constructPageIndex($scripturl . '?topic=' . $row['id_topic'] . '.%1$d', $start, $topic_length, $messages_per_page, true, false);
1284
1285
			// If we can use all, show all.
1286
			if (!empty($modSettings['enableAllMessages']) && $topic_length < $modSettings['enableAllMessages'])
1287
				$pages .= ' &nbsp;<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0;all">' . $txt['all'] . '</a>';
1288
		}
1289
1290
		else
1291
			$pages = '';
1292
1293
		// We need to check the topic icons exist... you can never be too sure!
1294
		if (!empty($modSettings['messageIconChecks_enable']))
1295
		{
1296
			// First icon first... as you'd expect.
1297
			if (!isset($context['icon_sources'][$row['first_icon']]))
1298
				$context['icon_sources'][$row['first_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['first_icon'] . '.png') ? 'images_url' : 'default_images_url';
1299
			// Last icon... last... duh.
1300
			if (!isset($context['icon_sources'][$row['last_icon']]))
1301
				$context['icon_sources'][$row['last_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['last_icon'] . '.png') ? 'images_url' : 'default_images_url';
1302
		}
1303
		else
1304
		{
1305
			if (!isset($context['icon_sources'][$row['first_icon']]))
1306
				$context['icon_sources'][$row['first_icon']] = 'images_url';
1307
			if (!isset($context['icon_sources'][$row['last_icon']]))
1308
				$context['icon_sources'][$row['last_icon']] = 'images_url';
1309
		}
1310
1311
		// Force the recycling icon if appropriate
1312
		if ($recycle_board == $row['id_board'])
1313
		{
1314
			$row['first_icon'] = 'recycled';
1315
			$row['last_icon'] = 'recycled';
1316
		}
1317
1318
		// Reference the main color class.
1319
		$colorClass = 'windowbg';
1320
1321
		// Sticky topics should get a different color, too.
1322
		if ($row['is_sticky'])
1323
			$colorClass .= ' sticky';
1324
1325
		// Locked topics get special treatment as well.
1326
		if ($row['locked'])
1327
			$colorClass .= ' locked';
1328
1329
		// And build the array.
1330
		$context['topics'][$row['id_topic']] = array(
1331
			'id' => $row['id_topic'],
1332
			'first_post' => array(
1333
				'id' => $row['id_first_msg'],
1334
				'member' => array(
1335
					'name' => $row['first_poster_name'],
1336
					'id' => $row['id_first_member'],
1337
					'href' => $scripturl . '?action=profile;u=' . $row['id_first_member'],
1338
					'link' => !empty($row['id_first_member']) ? '<a class="preview" href="' . $scripturl . '?action=profile;u=' . $row['id_first_member'] . '" title="' . $txt['profile_of'] . ' ' . $row['first_poster_name'] . '">' . $row['first_poster_name'] . '</a>' : $row['first_poster_name']
1339
				),
1340
				'time' => timeformat($row['first_poster_time']),
1341
				'timestamp' => forum_time(true, $row['first_poster_time']),
1342
				'subject' => $row['first_subject'],
1343
				'preview' => $row['first_body'],
1344
				'icon' => $row['first_icon'],
1345
				'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.png',
1346
				'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0;topicseen',
1347
				'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0;topicseen">' . $row['first_subject'] . '</a>'
1348
			),
1349
			'last_post' => array(
1350
				'id' => $row['id_last_msg'],
1351
				'member' => array(
1352
					'name' => $row['last_poster_name'],
1353
					'id' => $row['id_last_member'],
1354
					'href' => $scripturl . '?action=profile;u=' . $row['id_last_member'],
1355
					'link' => !empty($row['id_last_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_last_member'] . '">' . $row['last_poster_name'] . '</a>' : $row['last_poster_name']
1356
				),
1357
				'time' => timeformat($row['last_poster_time']),
1358
				'timestamp' => forum_time(true, $row['last_poster_time']),
1359
				'subject' => $row['last_subject'],
1360
				'preview' => $row['last_body'],
1361
				'icon' => $row['last_icon'],
1362
				'icon_url' => $settings[$context['icon_sources'][$row['last_icon']]] . '/post/' . $row['last_icon'] . '.png',
1363
				'href' => $scripturl . '?topic=' . $row['id_topic'] . ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['id_last_msg']) . ';topicseen#msg' . $row['id_last_msg'],
1364
				'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['id_last_msg']) . ';topicseen#msg' . $row['id_last_msg'] . '" rel="nofollow">' . $row['last_subject'] . '</a>'
1365
			),
1366
			'new_from' => $row['new_from'],
1367
			'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . ';topicseen#new',
1368
			'href' => $scripturl . '?topic=' . $row['id_topic'] . ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['new_from']) . ';topicseen' . ($row['num_replies'] == 0 ? '' : 'new'),
1369
			'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['new_from']) . ';topicseen#msg' . $row['new_from'] . '" rel="nofollow">' . $row['first_subject'] . '</a>',
1370
			'is_sticky' => !empty($row['is_sticky']),
1371
			'is_locked' => !empty($row['locked']),
1372
			'css_class' => $colorClass,
1373
			'is_poll' => $modSettings['pollMode'] == '1' && $row['id_poll'] > 0,
1374
			'is_posted_in' => false,
1375
			'icon' => $row['first_icon'],
1376
			'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.png',
1377
			'subject' => $row['first_subject'],
1378
			'pages' => $pages,
1379
			'replies' => comma_format($row['num_replies']),
1380
			'views' => comma_format($row['num_views']),
1381
			'board' => array(
1382
				'id' => $row['id_board'],
1383
				'name' => $row['bname'],
1384
				'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
1385
				'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['bname'] . '</a>'
1386
			)
1387
		);
1388
		if (!empty($settings['avatars_on_indexes']))
1389
		{
1390
			$context['topics'][$row['id_topic']]['last_post']['member']['avatar'] = set_avatar_data(array(
1391
				'avatar' => $row['avatar'],
1392
				'email' => $row['email_address'],
1393
				'filename' => $row['last_poster_filename'],
1394
			));
1395
1396
			$context['topics'][$row['id_topic']]['first_post']['member']['avatar'] = set_avatar_data(array(
1397
				'avatar' => $row['first_poster_avatar'],
1398
				'email' => $row['first_poster_email'],
1399
				'filename' => $row['first_poster_filename'],
1400
			));
1401
		}
1402
1403
		$context['topics'][$row['id_topic']]['first_post']['started_by'] = sprintf($txt['topic_started_by'], $context['topics'][$row['id_topic']]['first_post']['member']['link'], $context['topics'][$row['id_topic']]['board']['link']);
1404
	}
1405
	$smcFunc['db_free_result']($request);
1406
1407
	if ($is_topics && !empty($modSettings['enableParticipation']) && !empty($topic_ids))
1408
	{
1409
		$result = $smcFunc['db_query']('', '
1410
			SELECT id_topic
1411
			FROM {db_prefix}messages
1412
			WHERE id_topic IN ({array_int:topic_list})
1413
				AND id_member = {int:current_member}
1414
			GROUP BY id_topic
1415
			LIMIT {int:limit}',
1416
			array(
1417
				'current_member' => $user_info['id'],
1418
				'topic_list' => $topic_ids,
1419
				'limit' => count($topic_ids),
1420
			)
1421
		);
1422
		while ($row = $smcFunc['db_fetch_assoc']($result))
1423
		{
1424
			if (empty($context['topics'][$row['id_topic']]['is_posted_in']))
1425
				$context['topics'][$row['id_topic']]['is_posted_in'] = true;
1426
		}
1427
		$smcFunc['db_free_result']($result);
1428
	}
1429
1430
	$context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
1431
	$context['topics_to_mark'] = implode('-', $topic_ids);
1432
1433
	// Build the recent button array.
1434
	if ($is_topics)
1435
	{
1436
		$context['recent_buttons'] = array(
1437
			'markread' => array('text' => !empty($context['no_board_limits']) ? 'mark_as_read' : 'mark_read_short', 'image' => 'markread.png', 'custom' => 'data-confirm="' . $txt['are_sure_mark_read'] . '"', 'class' => 'you_sure', 'url' => $scripturl . '?action=markasread;sa=' . (!empty($context['no_board_limits']) ? 'all' : 'board' . $context['querystring_board_limits']) . ';' . $context['session_var'] . '=' . $context['session_id']),
1438
		);
1439
1440
		if ($context['showCheckboxes'])
1441
			$context['recent_buttons']['markselectread'] = array(
1442
				'text' => 'quick_mod_markread',
1443
				'image' => 'markselectedread.png',
1444
				'url' => 'javascript:document.quickModForm.submit();',
1445
			);
1446
1447
		if (!empty($context['topics']) && !$context['showing_all_topics'])
1448
			$context['recent_buttons']['readall'] = array('text' => 'unread_topics_all', 'image' => 'markreadall.png', 'url' => $scripturl . '?action=unread;all' . $context['querystring_board_limits'], 'active' => true);
1449
	}
1450
	elseif (!$is_topics && isset($context['topics_to_mark']))
1451
	{
1452
		$context['recent_buttons'] = array(
1453
			'markread' => array('text' => 'mark_as_read', 'image' => 'markread.png', 'custom' => 'data-confirm="' . $txt['are_sure_mark_read'] . '"', 'class' => 'you_sure', 'url' => $scripturl . '?action=markasread;sa=unreadreplies;topics=' . $context['topics_to_mark'] . ';' . $context['session_var'] . '=' . $context['session_id']),
1454
		);
1455
1456
		if ($context['showCheckboxes'])
1457
			$context['recent_buttons']['markselectread'] = array(
1458
				'text' => 'quick_mod_markread',
1459
				'image' => 'markselectedread.png',
1460
				'url' => 'javascript:document.quickModForm.submit();',
1461
			);
1462
	}
1463
1464
	// Allow mods to add additional buttons here
1465
	call_integration_hook('integrate_recent_buttons');
1466
1467
	$context['no_topic_listing'] = empty($context['topics']);
1468
1469
	// Allow helpdesks and bug trackers and what not to add their own unread data (just add a template_layer to show custom stuff in the template!)
1470
	call_integration_hook('integrate_unread_list');
1471
}
1472
1473
?>