getLastPost()   B
last analyzed

Complexity

Conditions 7
Paths 3

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 28
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 44
rs 8.5386
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 2022 Simple Machines and individual contributors
11
 * @license https://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1.0
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
 *
24
 * @return array An array of information about the last post that you can see
25
 */
26
function getLastPost()
27
{
28
	global $scripturl, $modSettings, $smcFunc;
29
30
	// Find it by the board - better to order by board than sort the entire messages table.
31
	$request = $smcFunc['db_query']('substring', '
32
		SELECT m.poster_time, m.subject, m.id_topic, m.poster_name, SUBSTRING(m.body, 1, 385) AS body,
33
			m.smileys_enabled
34
		FROM {db_prefix}messages AS m' . (!empty($modSettings['postmod_active']) ? '
35
			INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)' : '') . '
36
		WHERE {query_wanna_see_message_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
37
			AND m.id_board != {int:recycle_board}' : '') . (!empty($modSettings['postmod_active']) ? '
38
			AND m.approved = {int:is_approved}
39
			AND t.approved = {int:is_approved}' : '') . '
40
		ORDER BY m.id_msg DESC
41
		LIMIT 1',
42
		array(
43
			'recycle_board' => $modSettings['recycle_board'],
44
			'is_approved' => 1,
45
		)
46
	);
47
	if ($smcFunc['db_num_rows']($request) == 0)
48
		return array();
49
	$row = $smcFunc['db_fetch_assoc']($request);
50
	$smcFunc['db_free_result']($request);
51
52
	// Censor the subject and post...
53
	censorText($row['subject']);
54
	censorText($row['body']);
55
56
	$row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled']), array('<br>' => '&#10;')));
57
	if ($smcFunc['strlen']($row['body']) > 128)
58
		$row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...';
59
60
	// Send the data.
61
	return array(
62
		'topic' => $row['id_topic'],
63
		'subject' => $row['subject'],
64
		'short_subject' => shorten_subject($row['subject'], 24),
65
		'preview' => $row['body'],
66
		'time' => timeformat($row['poster_time']),
67
		'timestamp' => $row['poster_time'],
68
		'href' => $scripturl . '?topic=' . $row['id_topic'] . '.new;topicseen#new',
69
		'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.new;topicseen#new">' . $row['subject'] . '</a>'
70
	);
71
}
72
73
/**
74
 * Find the ten most recent posts.
75
 */
76
function RecentPosts()
77
{
78
	global $txt, $scripturl, $user_info, $context, $modSettings, $board, $smcFunc, $cache_enable;
79
80
	loadTemplate('Recent');
81
	$context['page_title'] = $txt['recent_posts'];
82
	$context['sub_template'] = 'recent';
83
84
	$context['is_redirect'] = false;
85
86
	if (isset($_REQUEST['start']) && $_REQUEST['start'] > 95)
87
		$_REQUEST['start'] = 95;
88
89
	$_REQUEST['start'] = (int) $_REQUEST['start'];
90
91
	$query_parameters = array();
92
	if (!empty($_REQUEST['c']) && empty($board))
93
	{
94
		$_REQUEST['c'] = explode(',', $_REQUEST['c']);
95
		foreach ($_REQUEST['c'] as $i => $c)
96
			$_REQUEST['c'][$i] = (int) $c;
97
98
		if (count($_REQUEST['c']) == 1)
99
		{
100
			$request = $smcFunc['db_query']('', '
101
				SELECT name
102
				FROM {db_prefix}categories
103
				WHERE id_cat = {int:id_cat}
104
				LIMIT 1',
105
				array(
106
					'id_cat' => $_REQUEST['c'][0],
107
				)
108
			);
109
			list ($name) = $smcFunc['db_fetch_row']($request);
110
			$smcFunc['db_free_result']($request);
111
112
			if (empty($name))
113
				fatal_lang_error('no_access', false);
114
115
			$context['linktree'][] = array(
116
				'url' => $scripturl . '#c' . (int) $_REQUEST['c'],
117
				'name' => $name
118
			);
119
		}
120
121
		$recycling = !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']);
122
123
		$request = $smcFunc['db_query']('', '
124
			SELECT b.id_board, b.num_posts
125
			FROM {db_prefix}boards AS b
126
			WHERE b.id_cat IN ({array_int:category_list})
127
				AND b.redirect = {string:empty}' . ($recycling ? '
128
				AND b.id_board != {int:recycle_board}' : '') . '
129
				AND {query_wanna_see_board}',
130
			array(
131
				'category_list' => $_REQUEST['c'],
132
				'empty' => '',
133
				'recycle_board' => !empty($modSettings['recycle_board']) ? $modSettings['recycle_board'] : 0,
134
			)
135
		);
136
		$total_cat_posts = 0;
137
		$boards = array();
138
		while ($row = $smcFunc['db_fetch_assoc']($request))
139
		{
140
			$boards[] = $row['id_board'];
141
			$total_cat_posts += $row['num_posts'];
142
		}
143
		$smcFunc['db_free_result']($request);
144
145
		if (empty($boards))
146
			fatal_lang_error('error_no_boards_selected');
147
148
		$query_this_board = 'm.id_board IN ({array_int:boards})';
149
		$query_parameters['boards'] = $boards;
150
151
		// If this category has a significant number of posts in it...
152
		if ($total_cat_posts > 100 && $total_cat_posts > $modSettings['totalMessages'] / 15)
153
		{
154
			$query_this_board .= '
155
					AND m.id_msg >= {int:max_id_msg}';
156
			$query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 400 - $_REQUEST['start'] * 7);
157
		}
158
159
		$context['page_index'] = constructPageIndex($scripturl . '?action=recent;c=' . implode(',', $_REQUEST['c']), $_REQUEST['start'], min(100, $total_cat_posts), 10, false);
160
	}
161
	elseif (!empty($_REQUEST['boards']))
162
	{
163
		$_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
164
		foreach ($_REQUEST['boards'] as $i => $b)
165
			$_REQUEST['boards'][$i] = (int) $b;
166
167
		$request = $smcFunc['db_query']('', '
168
			SELECT b.id_board, b.num_posts
169
			FROM {db_prefix}boards AS b
170
			WHERE b.id_board IN ({array_int:board_list})
171
				AND b.redirect = {string:empty}
172
				AND {query_see_board}
173
			LIMIT {int:limit}',
174
			array(
175
				'board_list' => $_REQUEST['boards'],
176
				'limit' => count($_REQUEST['boards']),
177
				'empty' => '',
178
			)
179
		);
180
		$total_posts = 0;
181
		$boards = array();
182
		while ($row = $smcFunc['db_fetch_assoc']($request))
183
		{
184
			$boards[] = $row['id_board'];
185
			$total_posts += $row['num_posts'];
186
		}
187
		$smcFunc['db_free_result']($request);
188
189
		if (empty($boards))
190
			fatal_lang_error('error_no_boards_selected');
191
192
		$query_this_board = 'm.id_board IN ({array_int:boards})';
193
		$query_parameters['boards'] = $boards;
194
195
		// If these boards have a significant number of posts in them...
196
		if ($total_posts > 100 && $total_posts > $modSettings['totalMessages'] / 12)
197
		{
198
			$query_this_board .= '
199
					AND m.id_msg >= {int:max_id_msg}';
200
			$query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 500 - $_REQUEST['start'] * 9);
201
		}
202
203
		$context['page_index'] = constructPageIndex($scripturl . '?action=recent;boards=' . implode(',', $_REQUEST['boards']), $_REQUEST['start'], min(100, $total_posts), 10, false);
204
	}
205
	elseif (!empty($board))
206
	{
207
		$request = $smcFunc['db_query']('', '
208
			SELECT num_posts, redirect
209
			FROM {db_prefix}boards
210
			WHERE id_board = {int:current_board}
211
			LIMIT 1',
212
			array(
213
				'current_board' => $board,
214
			)
215
		);
216
		list ($total_posts, $redirect) = $smcFunc['db_fetch_row']($request);
217
		$smcFunc['db_free_result']($request);
218
219
		// If this is a redirection board, don't bother counting topics here...
220
		if ($redirect != '')
221
		{
222
			$total_posts = 0;
223
			$context['is_redirect'] = true;
224
		}
225
226
		$query_this_board = 'm.id_board = {int:board}';
227
		$query_parameters['board'] = $board;
228
229
		// If this board has a significant number of posts in it...
230
		if ($total_posts > 80 && $total_posts > $modSettings['totalMessages'] / 10)
231
		{
232
			$query_this_board .= '
233
					AND m.id_msg >= {int:max_id_msg}';
234
			$query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 600 - $_REQUEST['start'] * 10);
235
		}
236
237
		$context['page_index'] = constructPageIndex($scripturl . '?action=recent;board=' . $board . '.%1$d', $_REQUEST['start'], min(100, $total_posts), 10, true);
238
	}
239
	else
240
	{
241
		$query_this_board = '{query_wanna_see_message_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
242
					AND m.id_board != {int:recycle_board}' : '') . '
243
					AND m.id_msg >= {int:max_id_msg}';
244
		$query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 100 - $_REQUEST['start'] * 6);
245
		$query_parameters['recycle_board'] = $modSettings['recycle_board'];
246
247
		$query_these_boards = '{query_wanna_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
248
					AND b.id_board != {int:recycle_board}' : '');
249
		$query_these_boards_params = $query_parameters;
250
		unset($query_these_boards_params['max_id_msg']);
251
252
		$get_num_posts = $smcFunc['db_query']('', '
253
			SELECT COALESCE(SUM(b.num_posts), 0)
254
			FROM {db_prefix}boards AS b
255
			WHERE ' . $query_these_boards . '
256
				AND b.redirect = {string:empty}',
257
			array_merge($query_these_boards_params, array('empty' => ''))
258
		);
259
260
		list($db_num_posts) = $smcFunc['db_fetch_row']($get_num_posts);
261
		$num_posts = min(100, $db_num_posts);
262
263
		$smcFunc['db_free_result']($get_num_posts);
264
265
		$context['page_index'] = constructPageIndex($scripturl . '?action=recent', $_REQUEST['start'], $num_posts, 10, false);
266
	}
267
268
	$context['linktree'][] = array(
269
		'url' => $scripturl . '?action=recent' . (empty($board) ? (empty($_REQUEST['c']) ? '' : ';c=' . (int) $_REQUEST['c']) : ';board=' . $board . '.0'),
270
		'name' => $context['page_title']
271
	);
272
273
	// If you selected a redirection board, don't try getting posts for it...
274
	if ($context['is_redirect'])
275
		$messages = 0;
276
277
	$key = 'recent-' . $user_info['id'] . '-' . md5($smcFunc['json_encode'](array_diff_key($query_parameters, array('max_id_msg' => 0)))) . '-' . (int) $_REQUEST['start'];
278
	if (!$context['is_redirect'] && (empty($cache_enable) || ($messages = cache_get_data($key, 120)) == null))
279
	{
280
		$done = false;
281
		while (!$done)
282
		{
283
			// Find the 10 most recent messages they can *view*.
284
			// @todo SLOW This query is really slow still, probably?
285
			$request = $smcFunc['db_query']('', '
286
				SELECT m.id_msg
287
				FROM {db_prefix}messages AS m ' . (!empty($modSettings['postmod_active']) ? '
288
					INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)' : '') . '
289
				WHERE ' . $query_this_board . (!empty($modSettings['postmod_active']) ? '
290
					AND m.approved = {int:is_approved}
291
					AND t.approved = {int:is_approved}' : '') . '
292
				ORDER BY m.id_msg DESC
293
				LIMIT {int:offset}, {int:limit}',
294
				array_merge($query_parameters, array(
295
					'is_approved' => 1,
296
					'offset' => $_REQUEST['start'],
297
					'limit' => 10,
298
				))
299
			);
300
			// If we don't have 10 results, try again with an unoptimized version covering all rows, and cache the result.
301
			if (isset($query_parameters['max_id_msg']) && $smcFunc['db_num_rows']($request) < 10)
302
			{
303
				$smcFunc['db_free_result']($request);
304
				$query_this_board = str_replace('AND m.id_msg >= {int:max_id_msg}', '', $query_this_board);
305
				$cache_results = true;
306
				unset($query_parameters['max_id_msg']);
307
			}
308
			else
309
				$done = true;
310
		}
311
		$messages = array();
312
		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...
313
			$messages[] = $row['id_msg'];
314
		$smcFunc['db_free_result']($request);
315
		if (!empty($cache_results))
316
			cache_put_data($key, $messages, 120);
317
	}
318
319
	// Nothing here... Or at least, nothing you can see...
320
	if (empty($messages))
321
	{
322
		$context['posts'] = array();
323
		return;
324
	}
325
326
	// Get all the most recent posts.
327
	$request = $smcFunc['db_query']('', '
328
		SELECT
329
			m.id_msg, m.subject, m.smileys_enabled, m.poster_time, m.body, m.id_topic, t.id_board, b.id_cat,
330
			b.name AS bname, c.name AS cname, t.num_replies, m.id_member, m2.id_member AS id_first_member,
331
			COALESCE(mem2.real_name, m2.poster_name) AS first_poster_name, t.id_first_msg,
332
			COALESCE(mem.real_name, m.poster_name) AS poster_name, t.id_last_msg
333
		FROM {db_prefix}messages AS m
334
			INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
335
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
336
			INNER JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
337
			INNER JOIN {db_prefix}messages AS m2 ON (m2.id_msg = t.id_first_msg)
338
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
339
			LEFT JOIN {db_prefix}members AS mem2 ON (mem2.id_member = m2.id_member)
340
		WHERE m.id_msg IN ({array_int:message_list})
341
		ORDER BY m.id_msg DESC
342
		LIMIT {int:limit}',
343
		array(
344
			'message_list' => $messages,
345
			'limit' => count($messages),
346
		)
347
	);
348
	$counter = $_REQUEST['start'] + 1;
349
	$context['posts'] = array();
350
	$board_ids = array('own' => array(), 'any' => array());
351
	while ($row = $smcFunc['db_fetch_assoc']($request))
352
	{
353
		// Censor everything.
354
		censorText($row['body']);
355
		censorText($row['subject']);
356
357
		// BBC-atize the message.
358
		$row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
359
360
		// And build the array.
361
		$context['posts'][$row['id_msg']] = array(
362
			'id' => $row['id_msg'],
363
			'counter' => $counter++,
364
			'category' => array(
365
				'id' => $row['id_cat'],
366
				'name' => $row['cname'],
367
				'href' => $scripturl . '#c' . $row['id_cat'],
368
				'link' => '<a href="' . $scripturl . '#c' . $row['id_cat'] . '">' . $row['cname'] . '</a>'
369
			),
370
			'board' => array(
371
				'id' => $row['id_board'],
372
				'name' => $row['bname'],
373
				'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
374
				'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['bname'] . '</a>'
375
			),
376
			'topic' => $row['id_topic'],
377
			'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
378
			'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>',
379
			'start' => $row['num_replies'],
380
			'subject' => $row['subject'],
381
			'shorten_subject' => shorten_subject($row['subject'], 30),
382
			'time' => timeformat($row['poster_time']),
383
			'timestamp' => $row['poster_time'],
384
			'first_poster' => array(
385
				'id' => $row['id_first_member'],
386
				'name' => $row['first_poster_name'],
387
				'href' => empty($row['id_first_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_first_member'],
388
				'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>'
389
			),
390
			'poster' => array(
391
				'id' => $row['id_member'],
392
				'name' => $row['poster_name'],
393
				'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
394
				'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
395
			),
396
			'message' => $row['body'],
397
			'can_reply' => false,
398
			'can_delete' => false,
399
			'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()),
400
			'css_class' => 'windowbg',
401
		);
402
403
		if ($user_info['id'] == $row['id_first_member'])
404
			$board_ids['own'][$row['id_board']][] = $row['id_msg'];
405
		$board_ids['any'][$row['id_board']][] = $row['id_msg'];
406
	}
407
	$smcFunc['db_free_result']($request);
408
409
	// There might be - and are - different permissions between any and own.
410
	$permissions = array(
411
		'own' => array(
412
			'post_reply_own' => 'can_reply',
413
			'delete_own' => 'can_delete',
414
		),
415
		'any' => array(
416
			'post_reply_any' => 'can_reply',
417
			'delete_any' => 'can_delete',
418
		)
419
	);
420
421
	// Create an array for the permissions.
422
	$boards_can = boardsAllowedTo(array_keys(iterator_to_array(
423
		new RecursiveIteratorIterator(new RecursiveArrayIterator($permissions)))
424
	), true, false);
425
426
	// Now go through all the permissions, looking for boards they can do it on.
427
	foreach ($permissions as $type => $list)
428
	{
429
		foreach ($list as $permission => $allowed)
430
		{
431
			// They can do it on these boards...
432
			$boards = $boards_can[$permission];
433
434
			// If 0 is the only thing in the array, they can do it everywhere!
435
			if (!empty($boards) && $boards[0] == 0)
436
				$boards = array_keys($board_ids[$type]);
437
438
			// Go through the boards, and look for posts they can do this on.
439
			foreach ($boards as $board_id)
440
			{
441
				// Hmm, they have permission, but there are no topics from that board on this page.
442
				if (!isset($board_ids[$type][$board_id]))
443
					continue;
444
445
				// Okay, looks like they can do it for these posts.
446
				foreach ($board_ids[$type][$board_id] as $counter)
447
					if ($type == 'any' || $context['posts'][$counter]['poster']['id'] == $user_info['id'])
448
						$context['posts'][$counter][$allowed] = true;
449
			}
450
		}
451
	}
452
453
	$quote_enabled = empty($modSettings['disabledBBC']) || !in_array('quote', explode(',', $modSettings['disabledBBC']));
454
	foreach ($context['posts'] as $counter => $dummy)
455
	{
456
		// Some posts - the first posts - can't just be deleted.
457
		$context['posts'][$counter]['can_delete'] &= $context['posts'][$counter]['delete_possible'];
458
459
		// And some cannot be quoted...
460
		$context['posts'][$counter]['can_quote'] = $context['posts'][$counter]['can_reply'] && $quote_enabled;
461
	}
462
463
	// Last but not least, the quickbuttons
464
	foreach ($context['posts'] as $key => $post)
465
	{
466
		$context['posts'][$key]['quickbuttons'] = array(
467
			'reply' => array(
468
				'label' => $txt['reply'],
469
				'href' => $scripturl.'?action=post;topic='.$post['topic'].'.'.$post['start'],
470
				'icon' => 'reply_button',
471
				'show' => $post['can_reply']
472
			),
473
			'quote' => array(
474
				'label' => $txt['quote_action'],
475
				'href' => $scripturl.'?action=post;topic='.$post['topic'].'.'.$post['start'].';quote='.$post['id'],
476
				'icon' => 'quote',
477
				'show' => $post['can_quote']
478
			),
479
			'delete' => array(
480
				'label' => $txt['remove'],
481
				'href' => $scripturl.'?action=deletemsg;msg='.$post['id'].';topic='.$post['topic'].';recent;'.$context['session_var'].'='.$context['session_id'],
482
				'javascript' => 'data-confirm="'.$txt['remove_message'].'"',
483
				'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
Best Practice introduced by
Using exit here is not recommended.

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

Loading history...
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(sprintf($txt['unread_topics_visit_none'], $scripturl), 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})') . '
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
				AND COALESCE(lt.unwatched, 0) != 1',
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})') . '
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
				AND COALESCE(lt.unwatched, 0) != 1
1005
			ORDER BY {raw:order}
1006
			LIMIT {int:offset}, {int:limit}',
1007
			array_merge($query_parameters, array(
1008
				'current_member' => $user_info['id'],
1009
				'min_message' => $min_message,
1010
				'is_approved' => 1,
1011
				'order' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'),
1012
				'offset' => $_REQUEST['start'],
1013
				'limit' => $context['topics_per_page'],
1014
			))
1015
		);
1016
	}
1017
	else
1018
	{
1019
		if ($modSettings['totalMessages'] > 100000)
1020
		{
1021
			$smcFunc['db_query']('', '
1022
				DROP TABLE IF EXISTS {db_prefix}topics_posted_in',
1023
				array(
1024
				)
1025
			);
1026
1027
			$smcFunc['db_query']('', '
1028
				DROP TABLE IF EXISTS {db_prefix}log_topics_posted_in',
1029
				array(
1030
				)
1031
			);
1032
1033
			$sortKey_joins = array(
1034
				'ms.subject' => '
1035
					INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)',
1036
				'COALESCE(mems.real_name, ms.poster_name)' => '
1037
					INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
1038
					LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)',
1039
			);
1040
1041
			// The main benefit of this temporary table is not that it's faster; it's that it avoids locks later.
1042
			$have_temp_table = $smcFunc['db_query']('', '
1043
				CREATE TEMPORARY TABLE {db_prefix}topics_posted_in (
1044
					id_topic mediumint(8) unsigned NOT NULL default {string:string_zero},
1045
					id_board smallint(5) unsigned NOT NULL default {string:string_zero},
1046
					id_last_msg int(10) unsigned NOT NULL default {string:string_zero},
1047
					id_msg int(10) unsigned NOT NULL default {string:string_zero},
1048
					PRIMARY KEY (id_topic)
1049
				)
1050
				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' : '') . '
1051
				FROM {db_prefix}messages AS m
1052
					INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
1053
					LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)
1054
					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']] : '') . '
1055
				WHERE m.id_member = {int:current_member}' . (!empty($board) ? '
1056
					AND t.id_board = {int:current_board}' : '') . ($modSettings['postmod_active'] ? '
1057
					AND t.approved = {int:is_approved}' : '') . '
1058
				GROUP BY m.id_topic',
1059
				array(
1060
					'current_board' => $board,
1061
					'current_member' => $user_info['id'],
1062
					'is_approved' => 1,
1063
					'string_zero' => '0',
1064
					'db_error_skip' => true,
1065
				)
1066
			) !== false;
1067
1068
			// If that worked, create a sample of the log_topics table too.
1069
			if ($have_temp_table)
1070
				$have_temp_table = $smcFunc['db_query']('', '
1071
					CREATE TEMPORARY TABLE {db_prefix}log_topics_posted_in (
1072
						PRIMARY KEY (id_topic)
1073
					)
1074
					SELECT lt.id_topic, lt.id_msg
1075
					FROM {db_prefix}log_topics AS lt
1076
						INNER JOIN {db_prefix}topics_posted_in AS pi ON (pi.id_topic = lt.id_topic)
1077
					WHERE lt.id_member = {int:current_member}',
1078
					array(
1079
						'current_member' => $user_info['id'],
1080
						'db_error_skip' => true,
1081
					)
1082
				) !== false;
1083
		}
1084
1085
		if (!empty($have_temp_table))
1086
		{
1087
			$request = $smcFunc['db_query']('', '
1088
				SELECT COUNT(*)
1089
				FROM {db_prefix}topics_posted_in AS pi
1090
					LEFT JOIN {db_prefix}log_topics_posted_in AS lt ON (lt.id_topic = pi.id_topic)
1091
				WHERE pi.' . $query_this_board . '
1092
					AND COALESCE(lt.id_msg, pi.id_msg) < pi.id_last_msg',
1093
				array_merge($query_parameters, array(
1094
				))
1095
			);
1096
			list ($num_topics) = $smcFunc['db_fetch_row']($request);
1097
			$smcFunc['db_free_result']($request);
1098
		}
1099
		else
1100
		{
1101
			$request = $smcFunc['db_query']('unread_fetch_topic_count', '
1102
				SELECT COUNT(DISTINCT t.id_topic), MIN(t.id_last_msg)
1103
				FROM {db_prefix}topics AS t
1104
					INNER JOIN {db_prefix}messages AS m ON (m.id_topic = t.id_topic)
1105
					LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
1106
					LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
1107
				WHERE t.' . $query_this_board . '
1108
					AND m.id_member = {int:current_member}
1109
					AND COALESCE(lt.id_msg, lmr.id_msg, 0) < t.id_last_msg' . ($modSettings['postmod_active'] ? '
1110
					AND t.approved = {int:is_approved}' : '') . '
1111
					AND COALESCE(lt.unwatched, 0) != 1',
1112
				array_merge($query_parameters, array(
1113
					'current_member' => $user_info['id'],
1114
					'is_approved' => 1,
1115
				))
1116
			);
1117
			list ($num_topics, $min_message) = $smcFunc['db_fetch_row']($request);
1118
			$smcFunc['db_free_result']($request);
1119
		}
1120
1121
		// Make sure the starting place makes sense and construct the page index.
1122
		$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);
1123
		$context['current_page'] = (int) $_REQUEST['start'] / $context['topics_per_page'];
1124
1125
		$context['links'] = array(
1126
			'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'] : '',
1127
			'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'] : '',
1128
			'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'] : '',
1129
			'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'] : '',
1130
			'up' => $scripturl,
1131
		);
1132
		$context['page_info'] = array(
1133
			'current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1,
1134
			'num_pages' => floor(($num_topics - 1) / $context['topics_per_page']) + 1
1135
		);
1136
1137
		if ($num_topics == 0)
1138
		{
1139
			$context['topics'] = array();
1140
			$context['no_topic_listing'] = true;
1141
			if ($context['querystring_board_limits'] == ';start=%d')
1142
				$context['querystring_board_limits'] = '';
1143
			else
1144
				$context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
1145
			return;
1146
		}
1147
1148
		if (!empty($have_temp_table))
1149
			$request = $smcFunc['db_query']('', '
1150
				SELECT t.id_topic
1151
				FROM {db_prefix}topics_posted_in AS t
1152
					LEFT JOIN {db_prefix}log_topics_posted_in AS lt ON (lt.id_topic = t.id_topic)
1153
				WHERE t.' . $query_this_board . '
1154
					AND COALESCE(lt.id_msg, t.id_msg) < t.id_last_msg
1155
				ORDER BY {raw:order}
1156
				LIMIT {int:offset}, {int:limit}',
1157
				array_merge($query_parameters, array(
1158
					'order' => (in_array($_REQUEST['sort'], array('t.id_last_msg', 't.id_topic')) ? $_REQUEST['sort'] : 't.sort_key') . ($ascending ? '' : ' DESC'),
1159
					'offset' => $_REQUEST['start'],
1160
					'limit' => $context['topics_per_page'],
1161
				))
1162
			);
1163
		else
1164
			$request = $smcFunc['db_query']('', '
1165
				SELECT DISTINCT t.id_topic,' . $_REQUEST['sort'] . '
1166
				FROM {db_prefix}topics AS t
1167
					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 ? '' : '
1168
					INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)') . (strpos($_REQUEST['sort'], 'mems.') === false ? '' : '
1169
					LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)') . '
1170
					LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
1171
					LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
1172
				WHERE t.' . $query_this_board . '
1173
					AND t.id_last_msg >= {int:min_message}
1174
					AND (COALESCE(lt.id_msg, lmr.id_msg, 0)) < t.id_last_msg
1175
					AND t.approved = {int:is_approved}
1176
					AND COALESCE(lt.unwatched, 0) != 1
1177
				ORDER BY {raw:order}
1178
				LIMIT {int:offset}, {int:limit}',
1179
				array_merge($query_parameters, array(
1180
					'current_member' => $user_info['id'],
1181
					'min_message' => (int) $min_message,
1182
					'is_approved' => 1,
1183
					'order' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'),
1184
					'offset' => $_REQUEST['start'],
1185
					'limit' => $context['topics_per_page'],
1186
					'sort' => $_REQUEST['sort'],
1187
				))
1188
			);
1189
1190
		$topics = array();
1191
		while ($row = $smcFunc['db_fetch_assoc']($request))
1192
			$topics[] = $row['id_topic'];
1193
		$smcFunc['db_free_result']($request);
1194
1195
		// Sanity... where have you gone?
1196
		if (empty($topics))
1197
		{
1198
			$context['topics'] = array();
1199
			$context['no_topic_listing'] = true;
1200
			if ($context['querystring_board_limits'] == ';start=%d')
1201
				$context['querystring_board_limits'] = '';
1202
			else
1203
				$context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
1204
			return;
1205
		}
1206
1207
		$request = $smcFunc['db_query']('substring', '
1208
			SELECT ' . $select_clause . '
1209
			FROM {db_prefix}topics AS t
1210
				INNER JOIN {db_prefix}messages AS ms ON (ms.id_topic = t.id_topic AND ms.id_msg = t.id_first_msg)
1211
				INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
1212
				INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
1213
				LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)
1214
				LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)' . (!empty($settings['avatars_on_indexes']) ? '
1215
				LEFT JOIN {db_prefix}attachments AS af ON (af.id_member = mems.id_member)
1216
				LEFT JOIN {db_prefix}attachments AS al ON (al.id_member = meml.id_member)' : '') . '
1217
				LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
1218
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
1219
			WHERE t.id_topic IN ({array_int:topic_list})
1220
			ORDER BY {raw:sort}' . ($ascending ? '' : ' DESC') . '
1221
			LIMIT {int:limit}',
1222
			array(
1223
				'current_member' => $user_info['id'],
1224
				'topic_list' => $topics,
1225
				'sort' => $_REQUEST['sort'],
1226
				'limit' => count($topics),
1227
			)
1228
		);
1229
	}
1230
1231
	$context['topics'] = array();
1232
	$topic_ids = array();
1233
	$recycle_board = !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) ? $modSettings['recycle_board'] : 0;
1234
1235
	while ($row = $smcFunc['db_fetch_assoc']($request))
1236
	{
1237
		if ($row['id_poll'] > 0 && $modSettings['pollMode'] == '0')
1238
			continue;
1239
1240
		$topic_ids[] = $row['id_topic'];
1241
1242
		if (!empty($modSettings['preview_characters']))
1243
		{
1244
			// Limit them to 128 characters - do this FIRST because it's a lot of wasted censoring otherwise.
1245
			$row['first_body'] = strip_tags(strtr(parse_bbc($row['first_body'], $row['first_smileys'], $row['id_first_msg']), array('<br>' => '&#10;')));
1246
			if ($smcFunc['strlen']($row['first_body']) > 128)
1247
				$row['first_body'] = $smcFunc['substr']($row['first_body'], 0, 128) . '...';
1248
			$row['last_body'] = strip_tags(strtr(parse_bbc($row['last_body'], $row['last_smileys'], $row['id_last_msg']), array('<br>' => '&#10;')));
1249
			if ($smcFunc['strlen']($row['last_body']) > 128)
1250
				$row['last_body'] = $smcFunc['substr']($row['last_body'], 0, 128) . '...';
1251
1252
			// Censor the subject and message preview.
1253
			censorText($row['first_subject']);
1254
			censorText($row['first_body']);
1255
1256
			// Don't censor them twice!
1257
			if ($row['id_first_msg'] == $row['id_last_msg'])
1258
			{
1259
				$row['last_subject'] = $row['first_subject'];
1260
				$row['last_body'] = $row['first_body'];
1261
			}
1262
			else
1263
			{
1264
				censorText($row['last_subject']);
1265
				censorText($row['last_body']);
1266
			}
1267
		}
1268
		else
1269
		{
1270
			$row['first_body'] = '';
1271
			$row['last_body'] = '';
1272
			censorText($row['first_subject']);
1273
1274
			if ($row['id_first_msg'] == $row['id_last_msg'])
1275
				$row['last_subject'] = $row['first_subject'];
1276
			else
1277
				censorText($row['last_subject']);
1278
		}
1279
1280
		// Decide how many pages the topic should have.
1281
		$topic_length = $row['num_replies'] + 1;
1282
		$messages_per_page = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
1283
		if ($topic_length > $messages_per_page)
1284
		{
1285
			$start = -1;
1286
			$pages = constructPageIndex($scripturl . '?topic=' . $row['id_topic'] . '.%1$d', $start, $topic_length, $messages_per_page, true, false);
1287
1288
			// If we can use all, show all.
1289
			if (!empty($modSettings['enableAllMessages']) && $topic_length < $modSettings['enableAllMessages'])
1290
				$pages .= sprintf(strtr($settings['page_index']['page'], array('{URL}' => $scripturl . '?topic=' . $row['id_topic'] . '.0;all')), '', $txt['all']);
1291
		}
1292
1293
		else
1294
			$pages = '';
1295
1296
		// We need to check the topic icons exist... you can never be too sure!
1297
		if (!empty($modSettings['messageIconChecks_enable']))
1298
		{
1299
			// First icon first... as you'd expect.
1300
			if (!isset($context['icon_sources'][$row['first_icon']]))
1301
				$context['icon_sources'][$row['first_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['first_icon'] . '.png') ? 'images_url' : 'default_images_url';
1302
			// Last icon... last... duh.
1303
			if (!isset($context['icon_sources'][$row['last_icon']]))
1304
				$context['icon_sources'][$row['last_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['last_icon'] . '.png') ? 'images_url' : 'default_images_url';
1305
		}
1306
		else
1307
		{
1308
			if (!isset($context['icon_sources'][$row['first_icon']]))
1309
				$context['icon_sources'][$row['first_icon']] = 'images_url';
1310
			if (!isset($context['icon_sources'][$row['last_icon']]))
1311
				$context['icon_sources'][$row['last_icon']] = 'images_url';
1312
		}
1313
1314
		// Force the recycling icon if appropriate
1315
		if ($recycle_board == $row['id_board'])
1316
		{
1317
			$row['first_icon'] = 'recycled';
1318
			$row['last_icon'] = 'recycled';
1319
		}
1320
1321
		// Reference the main color class.
1322
		$colorClass = 'windowbg';
1323
1324
		// Sticky topics should get a different color, too.
1325
		if ($row['is_sticky'])
1326
			$colorClass .= ' sticky';
1327
1328
		// Locked topics get special treatment as well.
1329
		if ($row['locked'])
1330
			$colorClass .= ' locked';
1331
1332
		// And build the array.
1333
		$context['topics'][$row['id_topic']] = array(
1334
			'id' => $row['id_topic'],
1335
			'first_post' => array(
1336
				'id' => $row['id_first_msg'],
1337
				'member' => array(
1338
					'name' => $row['first_poster_name'],
1339
					'id' => $row['id_first_member'],
1340
					'href' => $scripturl . '?action=profile;u=' . $row['id_first_member'],
1341
					'link' => !empty($row['id_first_member']) ? '<a class="preview" href="' . $scripturl . '?action=profile;u=' . $row['id_first_member'] . '" title="' . sprintf($txt['view_profile_of_username'], $row['first_poster_name']) . '">' . $row['first_poster_name'] . '</a>' : $row['first_poster_name']
1342
				),
1343
				'time' => timeformat($row['first_poster_time']),
1344
				'timestamp' => $row['first_poster_time'],
1345
				'subject' => $row['first_subject'],
1346
				'preview' => $row['first_body'],
1347
				'icon' => $row['first_icon'],
1348
				'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.png',
1349
				'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0;topicseen',
1350
				'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0;topicseen">' . $row['first_subject'] . '</a>'
1351
			),
1352
			'last_post' => array(
1353
				'id' => $row['id_last_msg'],
1354
				'member' => array(
1355
					'name' => $row['last_poster_name'],
1356
					'id' => $row['id_last_member'],
1357
					'href' => $scripturl . '?action=profile;u=' . $row['id_last_member'],
1358
					'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']
1359
				),
1360
				'time' => timeformat($row['last_poster_time']),
1361
				'timestamp' => $row['last_poster_time'],
1362
				'subject' => $row['last_subject'],
1363
				'preview' => $row['last_body'],
1364
				'icon' => $row['last_icon'],
1365
				'icon_url' => $settings[$context['icon_sources'][$row['last_icon']]] . '/post/' . $row['last_icon'] . '.png',
1366
				'href' => $scripturl . '?topic=' . $row['id_topic'] . ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['id_last_msg']) . ';topicseen#msg' . $row['id_last_msg'],
1367
				'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>'
1368
			),
1369
			'new_from' => $row['new_from'],
1370
			'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . ';topicseen#new',
1371
			'href' => $scripturl . '?topic=' . $row['id_topic'] . ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['new_from']) . ';topicseen' . ($row['num_replies'] == 0 ? '' : 'new'),
1372
			'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>',
1373
			'is_sticky' => !empty($row['is_sticky']),
1374
			'is_locked' => !empty($row['locked']),
1375
			'css_class' => $colorClass,
1376
			'is_poll' => $modSettings['pollMode'] == '1' && $row['id_poll'] > 0,
1377
			'is_posted_in' => false,
1378
			'icon' => $row['first_icon'],
1379
			'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.png',
1380
			'subject' => $row['first_subject'],
1381
			'pages' => $pages,
1382
			'replies' => comma_format($row['num_replies']),
1383
			'views' => comma_format($row['num_views']),
1384
			'board' => array(
1385
				'id' => $row['id_board'],
1386
				'name' => $row['bname'],
1387
				'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
1388
				'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['bname'] . '</a>'
1389
			)
1390
		);
1391
		if (!empty($settings['avatars_on_indexes']))
1392
		{
1393
			$context['topics'][$row['id_topic']]['last_post']['member']['avatar'] = set_avatar_data(array(
1394
				'avatar' => $row['avatar'],
1395
				'email' => $row['email_address'],
1396
				'filename' => $row['last_poster_filename'],
1397
			));
1398
1399
			$context['topics'][$row['id_topic']]['first_post']['member']['avatar'] = set_avatar_data(array(
1400
				'avatar' => $row['first_poster_avatar'],
1401
				'email' => $row['first_poster_email'],
1402
				'filename' => $row['first_poster_filename'],
1403
			));
1404
		}
1405
1406
		$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']);
1407
	}
1408
	$smcFunc['db_free_result']($request);
1409
1410
	if ($is_topics && !empty($modSettings['enableParticipation']) && !empty($topic_ids))
1411
	{
1412
		$result = $smcFunc['db_query']('', '
1413
			SELECT id_topic
1414
			FROM {db_prefix}messages
1415
			WHERE id_topic IN ({array_int:topic_list})
1416
				AND id_member = {int:current_member}
1417
			GROUP BY id_topic
1418
			LIMIT {int:limit}',
1419
			array(
1420
				'current_member' => $user_info['id'],
1421
				'topic_list' => $topic_ids,
1422
				'limit' => count($topic_ids),
1423
			)
1424
		);
1425
		while ($row = $smcFunc['db_fetch_assoc']($result))
1426
		{
1427
			if (empty($context['topics'][$row['id_topic']]['is_posted_in']))
1428
				$context['topics'][$row['id_topic']]['is_posted_in'] = true;
1429
		}
1430
		$smcFunc['db_free_result']($result);
1431
	}
1432
1433
	$context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
1434
	$context['topics_to_mark'] = implode('-', $topic_ids);
1435
1436
	// Build the recent button array.
1437
	if ($is_topics)
1438
	{
1439
		$context['recent_buttons'] = array(
1440
			'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']),
1441
		);
1442
1443
		if ($context['showCheckboxes'])
1444
			$context['recent_buttons']['markselectread'] = array(
1445
				'text' => 'quick_mod_markread',
1446
				'image' => 'markselectedread.png',
1447
				'url' => 'javascript:document.quickModForm.submit();',
1448
			);
1449
1450
		if (!empty($context['topics']) && !$context['showing_all_topics'])
1451
			$context['recent_buttons']['readall'] = array('text' => 'unread_topics_all', 'image' => 'markreadall.png', 'url' => $scripturl . '?action=unread;all' . $context['querystring_board_limits'], 'active' => true);
1452
	}
1453
	elseif (!$is_topics && isset($context['topics_to_mark']))
1454
	{
1455
		$context['recent_buttons'] = array(
1456
			'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']),
1457
		);
1458
1459
		if ($context['showCheckboxes'])
1460
			$context['recent_buttons']['markselectread'] = array(
1461
				'text' => 'quick_mod_markread',
1462
				'image' => 'markselectedread.png',
1463
				'url' => 'javascript:document.quickModForm.submit();',
1464
			);
1465
	}
1466
1467
	// Allow mods to add additional buttons here
1468
	call_integration_hook('integrate_recent_buttons');
1469
1470
	$context['no_topic_listing'] = empty($context['topics']);
1471
1472
	// 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!)
1473
	call_integration_hook('integrate_unread_list');
1474
}
1475
1476
?>