Issues (1027)

Sources/Recent.php (2 issues)

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 http://www.simplemachines.org
10
 * @copyright 2019 Simple Machines and individual contributors
11
 * @license http://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'].'" class="you_sure"',
484
				'icon' => 'remove_button',
485
				'show' => $post['can_delete']
486
			),
487
		);
488
	}
489
490
	// Allow last minute changes.
491
	call_integration_hook('integrate_recent_RecentPosts');
492
}
493
494
/**
495
 * Find unread topics and replies.
496
 */
497
function UnreadTopics()
498
{
499
	global $board, $txt, $scripturl, $sourcedir;
500
	global $user_info, $context, $settings, $modSettings, $smcFunc, $options;
501
502
	// Guests can't have unread things, we don't know anything about them.
503
	is_not_guest();
504
505
	// Prefetching + lots of MySQL work = bad mojo.
506
	if (isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch')
507
	{
508
		ob_end_clean();
509
		send_http_status(403);
510
		die;
0 ignored issues
show
Using exit here is not recommended.

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

Loading history...
511
	}
512
513
	$context['showCheckboxes'] = !empty($options['display_quick_mod']) && $options['display_quick_mod'] == 1;
514
515
	$context['showing_all_topics'] = isset($_GET['all']);
516
	$context['start'] = (int) $_REQUEST['start'];
517
	$context['topics_per_page'] = empty($modSettings['disableCustomPerPage']) && !empty($options['topics_per_page']) ? $options['topics_per_page'] : $modSettings['defaultMaxTopics'];
518
	if ($_REQUEST['action'] == 'unread')
519
		$context['page_title'] = $context['showing_all_topics'] ? $txt['unread_topics_all'] : $txt['unread_topics_visit'];
520
	else
521
		$context['page_title'] = $txt['unread_replies'];
522
523
	if ($context['showing_all_topics'] && !empty($context['load_average']) && !empty($modSettings['loadavg_allunread']) && $context['load_average'] >= $modSettings['loadavg_allunread'])
524
		fatal_lang_error('loadavg_allunread_disabled', false);
525
	elseif ($_REQUEST['action'] != 'unread' && !empty($context['load_average']) && !empty($modSettings['loadavg_unreadreplies']) && $context['load_average'] >= $modSettings['loadavg_unreadreplies'])
526
		fatal_lang_error('loadavg_unreadreplies_disabled', false);
527
	elseif (!$context['showing_all_topics'] && $_REQUEST['action'] == 'unread' && !empty($context['load_average']) && !empty($modSettings['loadavg_unread']) && $context['load_average'] >= $modSettings['loadavg_unread'])
528
		fatal_lang_error('loadavg_unread_disabled', false);
529
530
	// Parameters for the main query.
531
	$query_parameters = array();
532
533
	// Are we specifying any specific board?
534
	if (isset($_REQUEST['children']) && (!empty($board) || !empty($_REQUEST['boards'])))
535
	{
536
		$boards = array();
537
538
		if (!empty($_REQUEST['boards']))
539
		{
540
			$_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
541
			foreach ($_REQUEST['boards'] as $b)
542
				$boards[] = (int) $b;
543
		}
544
545
		if (!empty($board))
546
			$boards[] = (int) $board;
547
548
		// 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
549
		$request = $smcFunc['db_query']('', '
550
			SELECT b.id_board, b.id_parent
551
			FROM {db_prefix}boards AS b
552
			WHERE {query_wanna_see_board}
553
				AND b.child_level > {int:no_child}
554
				AND b.id_board NOT IN ({array_int:boards})
555
			ORDER BY child_level ASC',
556
			array(
557
				'no_child' => 0,
558
				'boards' => $boards,
559
			)
560
		);
561
562
		while ($row = $smcFunc['db_fetch_assoc']($request))
563
			if (in_array($row['id_parent'], $boards))
564
				$boards[] = $row['id_board'];
565
566
		$smcFunc['db_free_result']($request);
567
568
		if (empty($boards))
569
			fatal_lang_error('error_no_boards_selected');
570
571
		$query_this_board = 'id_board IN ({array_int:boards})';
572
		$query_parameters['boards'] = $boards;
573
		$context['querystring_board_limits'] = ';boards=' . implode(',', $boards) . ';start=%d';
574
	}
575
	elseif (!empty($board))
576
	{
577
		$query_this_board = 'id_board = {int:board}';
578
		$query_parameters['board'] = $board;
579
		$context['querystring_board_limits'] = ';board=' . $board . '.%1$d';
580
	}
581
	elseif (!empty($_REQUEST['boards']))
582
	{
583
		$_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
584
		foreach ($_REQUEST['boards'] as $i => $b)
585
			$_REQUEST['boards'][$i] = (int) $b;
586
587
		$request = $smcFunc['db_query']('', '
588
			SELECT b.id_board
589
			FROM {db_prefix}boards AS b
590
			WHERE {query_see_board}
591
				AND b.id_board IN ({array_int:board_list})',
592
			array(
593
				'board_list' => $_REQUEST['boards'],
594
			)
595
		);
596
		$boards = array();
597
		while ($row = $smcFunc['db_fetch_assoc']($request))
598
			$boards[] = $row['id_board'];
599
		$smcFunc['db_free_result']($request);
600
601
		if (empty($boards))
602
			fatal_lang_error('error_no_boards_selected');
603
604
		$query_this_board = 'id_board IN ({array_int:boards})';
605
		$query_parameters['boards'] = $boards;
606
		$context['querystring_board_limits'] = ';boards=' . implode(',', $boards) . ';start=%1$d';
607
	}
608
	elseif (!empty($_REQUEST['c']))
609
	{
610
		$_REQUEST['c'] = explode(',', $_REQUEST['c']);
611
		foreach ($_REQUEST['c'] as $i => $c)
612
			$_REQUEST['c'][$i] = (int) $c;
613
614
		$see_board = isset($_REQUEST['action']) && $_REQUEST['action'] == 'unreadreplies' ? 'query_see_board' : 'query_wanna_see_board';
615
616
		$request = $smcFunc['db_query']('', '
617
			SELECT b.id_board
618
			FROM {db_prefix}boards AS b
619
			WHERE ' . $user_info[$see_board] . '
620
				AND b.id_cat IN ({array_int:id_cat})',
621
			array(
622
				'id_cat' => $_REQUEST['c'],
623
			)
624
		);
625
		$boards = array();
626
		while ($row = $smcFunc['db_fetch_assoc']($request))
627
			$boards[] = $row['id_board'];
628
		$smcFunc['db_free_result']($request);
629
630
		if (empty($boards))
631
			fatal_lang_error('error_no_boards_selected');
632
633
		$query_this_board = 'id_board IN ({array_int:boards})';
634
		$query_parameters['boards'] = $boards;
635
		$context['querystring_board_limits'] = ';c=' . implode(',', $_REQUEST['c']) . ';start=%1$d';
636
	}
637
	else
638
	{
639
		$see_board = isset($_REQUEST['action']) && $_REQUEST['action'] == 'unreadreplies' ? 'query_see_board' : 'query_wanna_see_board';
640
		// Don't bother to show deleted posts!
641
		$request = $smcFunc['db_query']('', '
642
			SELECT b.id_board
643
			FROM {db_prefix}boards AS b
644
			WHERE ' . $user_info[$see_board] . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
645
				AND b.id_board != {int:recycle_board}' : ''),
646
			array(
647
				'recycle_board' => (int) $modSettings['recycle_board'],
648
			)
649
		);
650
		$boards = array();
651
		while ($row = $smcFunc['db_fetch_assoc']($request))
652
			$boards[] = $row['id_board'];
653
		$smcFunc['db_free_result']($request);
654
655
		if (empty($boards))
656
			fatal_lang_error('error_no_boards_available', false);
657
658
		$query_this_board = 'id_board IN ({array_int:boards})';
659
		$query_parameters['boards'] = $boards;
660
		$context['querystring_board_limits'] = ';start=%1$d';
661
		$context['no_board_limits'] = true;
662
	}
663
664
	$sort_methods = array(
665
		'subject' => 'ms.subject',
666
		'starter' => 'COALESCE(mems.real_name, ms.poster_name)',
667
		'replies' => 't.num_replies',
668
		'views' => 't.num_views',
669
		'first_post' => 't.id_topic',
670
		'last_post' => 't.id_last_msg'
671
	);
672
673
	// The default is the most logical: newest first.
674
	if (!isset($_REQUEST['sort']) || !isset($sort_methods[$_REQUEST['sort']]))
675
	{
676
		$context['sort_by'] = 'last_post';
677
		$_REQUEST['sort'] = 't.id_last_msg';
678
		$ascending = isset($_REQUEST['asc']);
679
680
		$context['querystring_sort_limits'] = $ascending ? ';asc' : '';
681
	}
682
	// But, for other methods the default sort is ascending.
683
	else
684
	{
685
		$context['sort_by'] = $_REQUEST['sort'];
686
		$_REQUEST['sort'] = $sort_methods[$_REQUEST['sort']];
687
		$ascending = !isset($_REQUEST['desc']);
688
689
		$context['querystring_sort_limits'] = ';sort=' . $context['sort_by'] . ($ascending ? '' : ';desc');
690
	}
691
	$context['sort_direction'] = $ascending ? 'up' : 'down';
692
693
	if (!empty($_REQUEST['c']) && is_array($_REQUEST['c']) && count($_REQUEST['c']) == 1)
694
	{
695
		$request = $smcFunc['db_query']('', '
696
			SELECT name
697
			FROM {db_prefix}categories
698
			WHERE id_cat = {int:id_cat}
699
			LIMIT 1',
700
			array(
701
				'id_cat' => (int) $_REQUEST['c'][0],
702
			)
703
		);
704
		list ($name) = $smcFunc['db_fetch_row']($request);
705
		$smcFunc['db_free_result']($request);
706
707
		$context['linktree'][] = array(
708
			'url' => $scripturl . '#c' . (int) $_REQUEST['c'][0],
709
			'name' => $name
710
		);
711
	}
712
713
	$context['linktree'][] = array(
714
		'url' => $scripturl . '?action=' . $_REQUEST['action'] . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'],
715
		'name' => $_REQUEST['action'] == 'unread' ? $txt['unread_topics_visit'] : $txt['unread_replies']
716
	);
717
718
	if ($context['showing_all_topics'])
719
		$context['linktree'][] = array(
720
			'url' => $scripturl . '?action=' . $_REQUEST['action'] . ';all' . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'],
721
			'name' => $txt['unread_topics_all']
722
		);
723
	else
724
		$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']));
725
726
	loadTemplate('Recent');
727
	loadTemplate('MessageIndex');
728
	$context['sub_template'] = $_REQUEST['action'] == 'unread' ? 'unread' : 'replies';
729
730
	// Setup the default topic icons... for checking they exist and the like ;)
731
	$context['icon_sources'] = array();
732
	foreach ($context['stable_icons'] as $icon)
733
		$context['icon_sources'][$icon] = 'images_url';
734
735
	$is_topics = $_REQUEST['action'] == 'unread';
736
737
	// This part is the same for each query.
738
	$select_clause = '
739
		ms.subject AS first_subject, ms.poster_time AS first_poster_time, ms.id_topic, t.id_board, b.name AS bname,
740
		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,' : '') . '
741
		ml.poster_time AS last_poster_time, COALESCE(mems.real_name, ms.poster_name) AS first_poster_name,
742
		COALESCE(meml.real_name, ml.poster_name) AS last_poster_name, ml.subject AS last_subject,
743
		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,
744
		COALESCE(lt.id_msg, lmr.id_msg, -1) + 1 AS new_from, SUBSTRING(ml.body, 1, 385) AS last_body,
745
		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';
746
747
	if ($context['showing_all_topics'])
748
	{
749
		if (!empty($board))
750
		{
751
			$request = $smcFunc['db_query']('', '
752
				SELECT MIN(id_msg)
753
				FROM {db_prefix}log_mark_read
754
				WHERE id_member = {int:current_member}
755
					AND id_board = {int:current_board}',
756
				array(
757
					'current_board' => $board,
758
					'current_member' => $user_info['id'],
759
				)
760
			);
761
			list ($earliest_msg) = $smcFunc['db_fetch_row']($request);
762
			$smcFunc['db_free_result']($request);
763
		}
764
		else
765
		{
766
			$request = $smcFunc['db_query']('', '
767
				SELECT MIN(lmr.id_msg)
768
				FROM {db_prefix}boards AS b
769
					LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = {int:current_member})
770
				WHERE {query_see_board}',
771
				array(
772
					'current_member' => $user_info['id'],
773
				)
774
			);
775
			list ($earliest_msg) = $smcFunc['db_fetch_row']($request);
776
			$smcFunc['db_free_result']($request);
777
		}
778
779
		// This is needed in case of topics marked unread.
780
		if (empty($earliest_msg))
781
			$earliest_msg = 0;
782
		else
783
		{
784
			// Using caching, when possible, to ignore the below slow query.
785
			if (isset($_SESSION['cached_log_time']) && $_SESSION['cached_log_time'][0] + 45 > time())
786
				$earliest_msg2 = $_SESSION['cached_log_time'][1];
787
			else
788
			{
789
				// This query is pretty slow, but it's needed to ensure nothing crucial is ignored.
790
				$request = $smcFunc['db_query']('', '
791
					SELECT MIN(id_msg)
792
					FROM {db_prefix}log_topics
793
					WHERE id_member = {int:current_member}',
794
					array(
795
						'current_member' => $user_info['id'],
796
					)
797
				);
798
				list ($earliest_msg2) = $smcFunc['db_fetch_row']($request);
799
				$smcFunc['db_free_result']($request);
800
801
				// In theory this could be zero, if the first ever post is unread, so fudge it ;)
802
				if ($earliest_msg2 == 0)
803
					$earliest_msg2 = -1;
804
805
				$_SESSION['cached_log_time'] = array(time(), $earliest_msg2);
806
			}
807
808
			$earliest_msg = min($earliest_msg2, $earliest_msg);
809
		}
810
	}
811
812
	// @todo Add modified_time in for log_time check?
813
814
	if ($modSettings['totalMessages'] > 100000 && $context['showing_all_topics'])
815
	{
816
		$smcFunc['db_query']('', '
817
			DROP TABLE IF EXISTS {db_prefix}log_topics_unread',
818
			array(
819
			)
820
		);
821
822
		// Let's copy things out of the log_topics table, to reduce searching.
823
		$have_temp_table = $smcFunc['db_query']('', '
824
			CREATE TEMPORARY TABLE {db_prefix}log_topics_unread (
825
				PRIMARY KEY (id_topic)
826
			)
827
			SELECT lt.id_topic, lt.id_msg
828
			FROM {db_prefix}topics AS t
829
				INNER JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic)
830
			WHERE lt.id_member = {int:current_member}
831
				AND t.' . $query_this_board . (empty($earliest_msg) ? '' : '
832
				AND t.id_last_msg > {int:earliest_msg}') . ($modSettings['postmod_active'] ? '
833
				AND t.approved = {int:is_approved}' : '') . ' AND lt.unwatched != 1',
834
			array_merge($query_parameters, array(
835
				'current_member' => $user_info['id'],
836
				'earliest_msg' => !empty($earliest_msg) ? $earliest_msg : 0,
837
				'is_approved' => 1,
838
				'db_error_skip' => true,
839
			))
840
		) !== false;
841
	}
842
	else
843
		$have_temp_table = false;
844
845
	if ($context['showing_all_topics'] && $have_temp_table)
846
	{
847
		$request = $smcFunc['db_query']('', '
848
			SELECT COUNT(*), MIN(t.id_last_msg)
849
			FROM {db_prefix}topics AS t
850
				LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)
851
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
852
			WHERE t.' . $query_this_board . (!empty($earliest_msg) ? '
853
				AND t.id_last_msg > {int:earliest_msg}' : '') . '
854
				AND COALESCE(lt.id_msg, lmr.id_msg, 0) < t.id_last_msg' . ($modSettings['postmod_active'] ? '
855
				AND t.approved = {int:is_approved}' : ''),
856
			array_merge($query_parameters, array(
857
				'current_member' => $user_info['id'],
858
				'earliest_msg' => !empty($earliest_msg) ? $earliest_msg : 0,
859
				'is_approved' => 1,
860
			))
861
		);
862
		list ($num_topics, $min_message) = $smcFunc['db_fetch_row']($request);
863
		$smcFunc['db_free_result']($request);
864
865
		// Make sure the starting place makes sense and construct the page index.
866
		$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);
867
		$context['current_page'] = (int) $_REQUEST['start'] / $context['topics_per_page'];
868
869
		$context['links'] = array(
870
			'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'] : '',
871
			'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'] : '',
872
			'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'] : '',
873
			'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'] : '',
874
			'up' => $scripturl,
875
		);
876
		$context['page_info'] = array(
877
			'current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1,
878
			'num_pages' => floor(($num_topics - 1) / $context['topics_per_page']) + 1
879
		);
880
881
		if ($num_topics == 0)
882
		{
883
			// Mark the boards as read if there are no unread topics!
884
			require_once($sourcedir . '/Subs-Boards.php');
885
			markBoardsRead(empty($boards) ? $board : $boards);
886
887
			$context['topics'] = array();
888
			$context['no_topic_listing'] = true;
889
			if ($context['querystring_board_limits'] == ';start=%1$d')
890
				$context['querystring_board_limits'] = '';
891
			else
892
				$context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
893
			return;
894
		}
895
		else
896
			$min_message = (int) $min_message;
897
898
		$request = $smcFunc['db_query']('substring', '
899
			SELECT ' . $select_clause . '
900
			FROM {db_prefix}messages AS ms
901
				INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ms.id_topic AND t.id_first_msg = ms.id_msg)
902
				INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
903
				LEFT JOIN {db_prefix}boards AS b ON (b.id_board = ms.id_board)
904
				LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)
905
				LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)' . (!empty($settings['avatars_on_indexes']) ? '
906
				LEFT JOIN {db_prefix}attachments AS af ON (af.id_member = mems.id_member)
907
				LEFT JOIN {db_prefix}attachments AS al ON (al.id_member = meml.id_member)' : '') . '
908
				LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)
909
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
910
			WHERE b.' . $query_this_board . '
911
				AND t.id_last_msg >= {int:min_message}
912
				AND COALESCE(lt.id_msg, lmr.id_msg, 0) < t.id_last_msg' . ($modSettings['postmod_active'] ? '
913
				AND ms.approved = {int:is_approved}' : '') . '
914
			ORDER BY {raw:sort}
915
			LIMIT {int:offset}, {int:limit}',
916
			array_merge($query_parameters, array(
917
				'current_member' => $user_info['id'],
918
				'min_message' => $min_message,
919
				'is_approved' => 1,
920
				'sort' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'),
921
				'offset' => $_REQUEST['start'],
922
				'limit' => $context['topics_per_page'],
923
			))
924
		);
925
	}
926
	elseif ($is_topics)
927
	{
928
		$request = $smcFunc['db_query']('', '
929
			SELECT COUNT(*), MIN(t.id_last_msg)
930
			FROM {db_prefix}topics AS t' . (!empty($have_temp_table) ? '
931
				LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)' : '
932
				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)') . '
933
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
934
			WHERE t.' . $query_this_board . ($context['showing_all_topics'] && !empty($earliest_msg) ? '
935
				AND t.id_last_msg > {int:earliest_msg}' : (!$context['showing_all_topics'] && empty($_SESSION['first_login']) ? '
936
				AND t.id_last_msg > {int:id_msg_last_visit}' : '')) . '
937
				AND COALESCE(lt.id_msg, lmr.id_msg, 0) < t.id_last_msg' . ($modSettings['postmod_active'] ? '
938
				AND t.approved = {int:is_approved}' : '') . '',
939
			array_merge($query_parameters, array(
940
				'current_member' => $user_info['id'],
941
				'earliest_msg' => !empty($earliest_msg) ? $earliest_msg : 0,
942
				'id_msg_last_visit' => $_SESSION['id_msg_last_visit'],
943
				'is_approved' => 1,
944
			))
945
		);
946
		list ($num_topics, $min_message) = $smcFunc['db_fetch_row']($request);
947
		$smcFunc['db_free_result']($request);
948
949
		// Make sure the starting place makes sense and construct the page index.
950
		$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);
951
		$context['current_page'] = (int) $_REQUEST['start'] / $context['topics_per_page'];
952
953
		$context['links'] = array(
954
			'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'] : '',
955
			'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'] : '',
956
			'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'] : '',
957
			'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'] : '',
958
			'up' => $scripturl,
959
		);
960
		$context['page_info'] = array(
961
			'current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1,
962
			'num_pages' => floor(($num_topics - 1) / $context['topics_per_page']) + 1
963
		);
964
965
		if ($num_topics == 0)
966
		{
967
			// Is this an all topics query?
968
			if ($context['showing_all_topics'])
969
			{
970
				// Since there are no unread topics, mark the boards as read!
971
				require_once($sourcedir . '/Subs-Boards.php');
972
				markBoardsRead(empty($boards) ? $board : $boards);
973
			}
974
975
			$context['topics'] = array();
976
			$context['no_topic_listing'] = true;
977
			if ($context['querystring_board_limits'] == ';start=%d')
978
				$context['querystring_board_limits'] = '';
979
			else
980
				$context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
981
			return;
982
		}
983
		else
984
			$min_message = (int) $min_message;
985
986
		$request = $smcFunc['db_query']('substring', '
987
			SELECT ' . $select_clause . '
988
			FROM {db_prefix}messages AS ms
989
				INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ms.id_topic AND t.id_first_msg = ms.id_msg)
990
				INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
991
				LEFT JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
992
				LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)
993
				LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)' . (!empty($settings['avatars_on_indexes']) ? '
994
				LEFT JOIN {db_prefix}attachments AS af ON (af.id_member = mems.id_member)
995
				LEFT JOIN {db_prefix}attachments AS al ON (al.id_member = meml.id_member)' : '') . '' . (!empty($have_temp_table) ? '
996
				LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)' : '
997
				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)') . '
998
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
999
			WHERE t.' . $query_this_board . '
1000
				AND t.id_last_msg >= {int:min_message}
1001
				AND COALESCE(lt.id_msg, lmr.id_msg, 0) < ml.id_msg' . ($modSettings['postmod_active'] ? '
1002
				AND ms.approved = {int:is_approved}' : '') . '
1003
			ORDER BY {raw:order}
1004
			LIMIT {int:offset}, {int:limit}',
1005
			array_merge($query_parameters, array(
1006
				'current_member' => $user_info['id'],
1007
				'min_message' => $min_message,
1008
				'is_approved' => 1,
1009
				'order' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'),
1010
				'offset' => $_REQUEST['start'],
1011
				'limit' => $context['topics_per_page'],
1012
			))
1013
		);
1014
	}
1015
	else
1016
	{
1017
		if ($modSettings['totalMessages'] > 100000)
1018
		{
1019
			$smcFunc['db_query']('', '
1020
				DROP TABLE IF EXISTS {db_prefix}topics_posted_in',
1021
				array(
1022
				)
1023
			);
1024
1025
			$smcFunc['db_query']('', '
1026
				DROP TABLE IF EXISTS {db_prefix}log_topics_posted_in',
1027
				array(
1028
				)
1029
			);
1030
1031
			$sortKey_joins = array(
1032
				'ms.subject' => '
1033
					INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)',
1034
				'COALESCE(mems.real_name, ms.poster_name)' => '
1035
					INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
1036
					LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)',
1037
			);
1038
1039
			// The main benefit of this temporary table is not that it's faster; it's that it avoids locks later.
1040
			$have_temp_table = $smcFunc['db_query']('', '
1041
				CREATE TEMPORARY TABLE {db_prefix}topics_posted_in (
1042
					id_topic mediumint(8) unsigned NOT NULL default {string:string_zero},
1043
					id_board smallint(5) unsigned NOT NULL default {string:string_zero},
1044
					id_last_msg int(10) unsigned NOT NULL default {string:string_zero},
1045
					id_msg int(10) unsigned NOT NULL default {string:string_zero},
1046
					PRIMARY KEY (id_topic)
1047
				)
1048
				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' : '') . '
1049
				FROM {db_prefix}messages AS m
1050
					INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
1051
					LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)
1052
					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']] : '') . '
1053
				WHERE m.id_member = {int:current_member}' . (!empty($board) ? '
1054
					AND t.id_board = {int:current_board}' : '') . ($modSettings['postmod_active'] ? '
1055
					AND t.approved = {int:is_approved}' : '') . '
1056
				GROUP BY m.id_topic',
1057
				array(
1058
					'current_board' => $board,
1059
					'current_member' => $user_info['id'],
1060
					'is_approved' => 1,
1061
					'string_zero' => '0',
1062
					'db_error_skip' => true,
1063
				)
1064
			) !== false;
1065
1066
			// If that worked, create a sample of the log_topics table too.
1067
			if ($have_temp_table)
1068
				$have_temp_table = $smcFunc['db_query']('', '
1069
					CREATE TEMPORARY TABLE {db_prefix}log_topics_posted_in (
1070
						PRIMARY KEY (id_topic)
1071
					)
1072
					SELECT lt.id_topic, lt.id_msg
1073
					FROM {db_prefix}log_topics AS lt
1074
						INNER JOIN {db_prefix}topics_posted_in AS pi ON (pi.id_topic = lt.id_topic)
1075
					WHERE lt.id_member = {int:current_member}',
1076
					array(
1077
						'current_member' => $user_info['id'],
1078
						'db_error_skip' => true,
1079
					)
1080
				) !== false;
1081
		}
1082
1083
		if (!empty($have_temp_table))
1084
		{
1085
			$request = $smcFunc['db_query']('', '
1086
				SELECT COUNT(*)
1087
				FROM {db_prefix}topics_posted_in AS pi
1088
					LEFT JOIN {db_prefix}log_topics_posted_in AS lt ON (lt.id_topic = pi.id_topic)
1089
				WHERE pi.' . $query_this_board . '
1090
					AND COALESCE(lt.id_msg, pi.id_msg) < pi.id_last_msg',
1091
				array_merge($query_parameters, array(
1092
				))
1093
			);
1094
			list ($num_topics) = $smcFunc['db_fetch_row']($request);
1095
			$smcFunc['db_free_result']($request);
1096
		}
1097
		else
1098
		{
1099
			$request = $smcFunc['db_query']('unread_fetch_topic_count', '
1100
				SELECT COUNT(DISTINCT t.id_topic), MIN(t.id_last_msg)
1101
				FROM {db_prefix}topics AS t
1102
					INNER JOIN {db_prefix}messages AS m ON (m.id_topic = t.id_topic)
1103
					LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
1104
					LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
1105
				WHERE t.' . $query_this_board . '
1106
					AND m.id_member = {int:current_member}
1107
					AND COALESCE(lt.id_msg, lmr.id_msg, 0) < t.id_last_msg' . ($modSettings['postmod_active'] ? '
1108
					AND t.approved = {int:is_approved}' : '') . ' AND lt.unwatched != 1',
1109
				array_merge($query_parameters, array(
1110
					'current_member' => $user_info['id'],
1111
					'is_approved' => 1,
1112
				))
1113
			);
1114
			list ($num_topics, $min_message) = $smcFunc['db_fetch_row']($request);
1115
			$smcFunc['db_free_result']($request);
1116
		}
1117
1118
		// Make sure the starting place makes sense and construct the page index.
1119
		$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);
1120
		$context['current_page'] = (int) $_REQUEST['start'] / $context['topics_per_page'];
1121
1122
		$context['links'] = array(
1123
			'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'] : '',
1124
			'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'] : '',
1125
			'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'] : '',
1126
			'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'] : '',
1127
			'up' => $scripturl,
1128
		);
1129
		$context['page_info'] = array(
1130
			'current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1,
1131
			'num_pages' => floor(($num_topics - 1) / $context['topics_per_page']) + 1
1132
		);
1133
1134
		if ($num_topics == 0)
1135
		{
1136
			$context['topics'] = array();
1137
			$context['no_topic_listing'] = true;
1138
			if ($context['querystring_board_limits'] == ';start=%d')
1139
				$context['querystring_board_limits'] = '';
1140
			else
1141
				$context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
1142
			return;
1143
		}
1144
1145
		if (!empty($have_temp_table))
1146
			$request = $smcFunc['db_query']('', '
1147
				SELECT t.id_topic
1148
				FROM {db_prefix}topics_posted_in AS t
1149
					LEFT JOIN {db_prefix}log_topics_posted_in AS lt ON (lt.id_topic = t.id_topic)
1150
				WHERE t.' . $query_this_board . '
1151
					AND COALESCE(lt.id_msg, t.id_msg) < t.id_last_msg
1152
				ORDER BY {raw:order}
1153
				LIMIT {int:offset}, {int:limit}',
1154
				array_merge($query_parameters, array(
1155
					'order' => (in_array($_REQUEST['sort'], array('t.id_last_msg', 't.id_topic')) ? $_REQUEST['sort'] : 't.sort_key') . ($ascending ? '' : ' DESC'),
1156
					'offset' => $_REQUEST['start'],
1157
					'limit' => $context['topics_per_page'],
1158
				))
1159
			);
1160
		else
1161
			$request = $smcFunc['db_query']('', '
1162
				SELECT DISTINCT t.id_topic,' . $_REQUEST['sort'] . '
1163
				FROM {db_prefix}topics AS t
1164
					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 ? '' : '
1165
					INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)') . (strpos($_REQUEST['sort'], 'mems.') === false ? '' : '
1166
					LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)') . '
1167
					LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
1168
					LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
1169
				WHERE t.' . $query_this_board . '
1170
					AND t.id_last_msg >= {int:min_message}
1171
					AND (COALESCE(lt.id_msg, lmr.id_msg, 0)) < t.id_last_msg
1172
					AND t.approved = {int:is_approved} AND lt.unwatched != 1
1173
				ORDER BY {raw:order}
1174
				LIMIT {int:offset}, {int:limit}',
1175
				array_merge($query_parameters, array(
1176
					'current_member' => $user_info['id'],
1177
					'min_message' => (int) $min_message,
1178
					'is_approved' => 1,
1179
					'order' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'),
1180
					'offset' => $_REQUEST['start'],
1181
					'limit' => $context['topics_per_page'],
1182
					'sort' => $_REQUEST['sort'],
1183
				))
1184
			);
1185
1186
		$topics = array();
1187
		while ($row = $smcFunc['db_fetch_assoc']($request))
1188
			$topics[] = $row['id_topic'];
1189
		$smcFunc['db_free_result']($request);
1190
1191
		// Sanity... where have you gone?
1192
		if (empty($topics))
1193
		{
1194
			$context['topics'] = array();
1195
			$context['no_topic_listing'] = true;
1196
			if ($context['querystring_board_limits'] == ';start=%d')
1197
				$context['querystring_board_limits'] = '';
1198
			else
1199
				$context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
1200
			return;
1201
		}
1202
1203
		$request = $smcFunc['db_query']('substring', '
1204
			SELECT ' . $select_clause . '
1205
			FROM {db_prefix}topics AS t
1206
				INNER JOIN {db_prefix}messages AS ms ON (ms.id_topic = t.id_topic AND ms.id_msg = t.id_first_msg)
1207
				INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
1208
				INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
1209
				LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)
1210
				LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)' . (!empty($settings['avatars_on_indexes']) ? '
1211
				LEFT JOIN {db_prefix}attachments AS af ON (af.id_member = mems.id_member)
1212
				LEFT JOIN {db_prefix}attachments AS al ON (al.id_member = meml.id_member)' : '') . '
1213
				LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
1214
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
1215
			WHERE t.id_topic IN ({array_int:topic_list})
1216
			ORDER BY {raw:sort}' . ($ascending ? '' : ' DESC') . '
1217
			LIMIT {int:limit}',
1218
			array(
1219
				'current_member' => $user_info['id'],
1220
				'topic_list' => $topics,
1221
				'sort' => $_REQUEST['sort'],
1222
				'limit' => count($topics),
1223
			)
1224
		);
1225
	}
1226
1227
	$context['topics'] = array();
1228
	$topic_ids = array();
1229
	$recycle_board = !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) ? $modSettings['recycle_board'] : 0;
1230
1231
	while ($row = $smcFunc['db_fetch_assoc']($request))
1232
	{
1233
		if ($row['id_poll'] > 0 && $modSettings['pollMode'] == '0')
1234
			continue;
1235
1236
		$topic_ids[] = $row['id_topic'];
1237
1238
		if (!empty($modSettings['preview_characters']))
1239
		{
1240
			// Limit them to 128 characters - do this FIRST because it's a lot of wasted censoring otherwise.
1241
			$row['first_body'] = strip_tags(strtr(parse_bbc($row['first_body'], $row['first_smileys'], $row['id_first_msg']), array('<br>' => '&#10;')));
1242
			if ($smcFunc['strlen']($row['first_body']) > 128)
1243
				$row['first_body'] = $smcFunc['substr']($row['first_body'], 0, 128) . '...';
1244
			$row['last_body'] = strip_tags(strtr(parse_bbc($row['last_body'], $row['last_smileys'], $row['id_last_msg']), array('<br>' => '&#10;')));
1245
			if ($smcFunc['strlen']($row['last_body']) > 128)
1246
				$row['last_body'] = $smcFunc['substr']($row['last_body'], 0, 128) . '...';
1247
1248
			// Censor the subject and message preview.
1249
			censorText($row['first_subject']);
1250
			censorText($row['first_body']);
1251
1252
			// Don't censor them twice!
1253
			if ($row['id_first_msg'] == $row['id_last_msg'])
1254
			{
1255
				$row['last_subject'] = $row['first_subject'];
1256
				$row['last_body'] = $row['first_body'];
1257
			}
1258
			else
1259
			{
1260
				censorText($row['last_subject']);
1261
				censorText($row['last_body']);
1262
			}
1263
		}
1264
		else
1265
		{
1266
			$row['first_body'] = '';
1267
			$row['last_body'] = '';
1268
			censorText($row['first_subject']);
1269
1270
			if ($row['id_first_msg'] == $row['id_last_msg'])
1271
				$row['last_subject'] = $row['first_subject'];
1272
			else
1273
				censorText($row['last_subject']);
1274
		}
1275
1276
		// Decide how many pages the topic should have.
1277
		$topic_length = $row['num_replies'] + 1;
1278
		$messages_per_page = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
1279
		if ($topic_length > $messages_per_page)
1280
		{
1281
			$start = -1;
1282
			$pages = constructPageIndex($scripturl . '?topic=' . $row['id_topic'] . '.%1$d', $start, $topic_length, $messages_per_page, true, false);
1283
1284
			// If we can use all, show all.
1285
			if (!empty($modSettings['enableAllMessages']) && $topic_length < $modSettings['enableAllMessages'])
1286
				$pages .= ' &nbsp;<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0;all">' . $txt['all'] . '</a>';
1287
		}
1288
1289
		else
1290
			$pages = '';
1291
1292
		// We need to check the topic icons exist... you can never be too sure!
1293
		if (!empty($modSettings['messageIconChecks_enable']))
1294
		{
1295
			// First icon first... as you'd expect.
1296
			if (!isset($context['icon_sources'][$row['first_icon']]))
1297
				$context['icon_sources'][$row['first_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['first_icon'] . '.png') ? 'images_url' : 'default_images_url';
1298
			// Last icon... last... duh.
1299
			if (!isset($context['icon_sources'][$row['last_icon']]))
1300
				$context['icon_sources'][$row['last_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['last_icon'] . '.png') ? 'images_url' : 'default_images_url';
1301
		}
1302
		else
1303
		{
1304
			if (!isset($context['icon_sources'][$row['first_icon']]))
1305
				$context['icon_sources'][$row['first_icon']] = 'images_url';
1306
			if (!isset($context['icon_sources'][$row['last_icon']]))
1307
				$context['icon_sources'][$row['last_icon']] = 'images_url';
1308
		}
1309
1310
		// Force the recycling icon if appropriate
1311
		if ($recycle_board == $row['id_board'])
1312
		{
1313
			$row['first_icon'] = 'recycled';
1314
			$row['last_icon'] = 'recycled';
1315
		}
1316
1317
		// Reference the main color class.
1318
		$colorClass = 'windowbg';
1319
1320
		// Sticky topics should get a different color, too.
1321
		if ($row['is_sticky'])
1322
			$colorClass .= ' sticky';
1323
1324
		// Locked topics get special treatment as well.
1325
		if ($row['locked'])
1326
			$colorClass .= ' locked';
1327
1328
		// And build the array.
1329
		$context['topics'][$row['id_topic']] = array(
1330
			'id' => $row['id_topic'],
1331
			'first_post' => array(
1332
				'id' => $row['id_first_msg'],
1333
				'member' => array(
1334
					'name' => $row['first_poster_name'],
1335
					'id' => $row['id_first_member'],
1336
					'href' => $scripturl . '?action=profile;u=' . $row['id_first_member'],
1337
					'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']
1338
				),
1339
				'time' => timeformat($row['first_poster_time']),
1340
				'timestamp' => forum_time(true, $row['first_poster_time']),
1341
				'subject' => $row['first_subject'],
1342
				'preview' => $row['first_body'],
1343
				'icon' => $row['first_icon'],
1344
				'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.png',
1345
				'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0;topicseen',
1346
				'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0;topicseen">' . $row['first_subject'] . '</a>'
1347
			),
1348
			'last_post' => array(
1349
				'id' => $row['id_last_msg'],
1350
				'member' => array(
1351
					'name' => $row['last_poster_name'],
1352
					'id' => $row['id_last_member'],
1353
					'href' => $scripturl . '?action=profile;u=' . $row['id_last_member'],
1354
					'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']
1355
				),
1356
				'time' => timeformat($row['last_poster_time']),
1357
				'timestamp' => forum_time(true, $row['last_poster_time']),
1358
				'subject' => $row['last_subject'],
1359
				'preview' => $row['last_body'],
1360
				'icon' => $row['last_icon'],
1361
				'icon_url' => $settings[$context['icon_sources'][$row['last_icon']]] . '/post/' . $row['last_icon'] . '.png',
1362
				'href' => $scripturl . '?topic=' . $row['id_topic'] . ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['id_last_msg']) . ';topicseen#msg' . $row['id_last_msg'],
1363
				'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>'
1364
			),
1365
			'new_from' => $row['new_from'],
1366
			'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . ';topicseen#new',
1367
			'href' => $scripturl . '?topic=' . $row['id_topic'] . ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['new_from']) . ';topicseen' . ($row['num_replies'] == 0 ? '' : 'new'),
1368
			'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>',
1369
			'is_sticky' => !empty($row['is_sticky']),
1370
			'is_locked' => !empty($row['locked']),
1371
			'css_class' => $colorClass,
1372
			'is_poll' => $modSettings['pollMode'] == '1' && $row['id_poll'] > 0,
1373
			'is_posted_in' => false,
1374
			'icon' => $row['first_icon'],
1375
			'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.png',
1376
			'subject' => $row['first_subject'],
1377
			'pages' => $pages,
1378
			'replies' => comma_format($row['num_replies']),
1379
			'views' => comma_format($row['num_views']),
1380
			'board' => array(
1381
				'id' => $row['id_board'],
1382
				'name' => $row['bname'],
1383
				'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
1384
				'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['bname'] . '</a>'
1385
			)
1386
		);
1387
		if (!empty($settings['avatars_on_indexes']))
1388
		{
1389
			$context['topics'][$row['id_topic']]['last_post']['member']['avatar'] = set_avatar_data(array(
1390
				'avatar' => $row['avatar'],
1391
				'email' => $row['email_address'],
1392
				'filename' => $row['last_poster_filename'],
1393
			));
1394
1395
			$context['topics'][$row['id_topic']]['first_post']['member']['avatar'] = set_avatar_data(array(
1396
				'avatar' => $row['first_poster_avatar'],
1397
				'email' => $row['first_poster_email'],
1398
				'filename' => $row['first_poster_filename'],
1399
			));
1400
		}
1401
1402
		$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']);
1403
	}
1404
	$smcFunc['db_free_result']($request);
1405
1406
	if ($is_topics && !empty($modSettings['enableParticipation']) && !empty($topic_ids))
1407
	{
1408
		$result = $smcFunc['db_query']('', '
1409
			SELECT id_topic
1410
			FROM {db_prefix}messages
1411
			WHERE id_topic IN ({array_int:topic_list})
1412
				AND id_member = {int:current_member}
1413
			GROUP BY id_topic
1414
			LIMIT {int:limit}',
1415
			array(
1416
				'current_member' => $user_info['id'],
1417
				'topic_list' => $topic_ids,
1418
				'limit' => count($topic_ids),
1419
			)
1420
		);
1421
		while ($row = $smcFunc['db_fetch_assoc']($result))
1422
		{
1423
			if (empty($context['topics'][$row['id_topic']]['is_posted_in']))
1424
				$context['topics'][$row['id_topic']]['is_posted_in'] = true;
1425
		}
1426
		$smcFunc['db_free_result']($result);
1427
	}
1428
1429
	$context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
1430
	$context['topics_to_mark'] = implode('-', $topic_ids);
1431
1432
	// Build the recent button array.
1433
	if ($is_topics)
1434
	{
1435
		$context['recent_buttons'] = array(
1436
			'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']),
1437
		);
1438
1439
		if ($context['showCheckboxes'])
1440
			$context['recent_buttons']['markselectread'] = array(
1441
				'text' => 'quick_mod_markread',
1442
				'image' => 'markselectedread.png',
1443
				'url' => 'javascript:document.quickModForm.submit();',
1444
			);
1445
1446
		if (!empty($context['topics']) && !$context['showing_all_topics'])
1447
			$context['recent_buttons']['readall'] = array('text' => 'unread_topics_all', 'image' => 'markreadall.png', 'url' => $scripturl . '?action=unread;all' . $context['querystring_board_limits'], 'active' => true);
1448
	}
1449
	elseif (!$is_topics && isset($context['topics_to_mark']))
1450
	{
1451
		$context['recent_buttons'] = array(
1452
			'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']),
1453
		);
1454
1455
		if ($context['showCheckboxes'])
1456
			$context['recent_buttons']['markselectread'] = array(
1457
				'text' => 'quick_mod_markread',
1458
				'image' => 'markselectedread.png',
1459
				'url' => 'javascript:document.quickModForm.submit();',
1460
			);
1461
	}
1462
1463
	// Allow mods to add additional buttons here
1464
	call_integration_hook('integrate_recent_buttons');
1465
1466
	$context['no_topic_listing'] = empty($context['topics']);
1467
1468
	// 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!)
1469
	call_integration_hook('integrate_unread_list');
1470
}
1471
1472
?>