PageRenderTime 354ms CodeModel.GetById 102ms app.highlight 158ms RepoModel.GetById 83ms app.codeStats 0ms

/wwwroot/phpbb/viewforum.php

https://github.com/spring/spring-website
PHP | 944 lines | 645 code | 144 blank | 155 comment | 156 complexity | 9c02f768d6e7672879dc40064c020f42 MD5 | raw file
  1<?php
  2/**
  3*
  4* This file is part of the phpBB Forum Software package.
  5*
  6* @copyright (c) phpBB Limited <https://www.phpbb.com>
  7* @license GNU General Public License, version 2 (GPL-2.0)
  8*
  9* For full copyright and license information, please see
 10* the docs/CREDITS.txt file.
 11*
 12*/
 13
 14/**
 15* @ignore
 16*/
 17define('IN_PHPBB', true);
 18$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
 19$phpEx = substr(strrchr(__FILE__, '.'), 1);
 20include($phpbb_root_path . 'common.' . $phpEx);
 21include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
 22
 23// Start session
 24$user->session_begin();
 25$auth->acl($user->data);
 26
 27// Start initial var setup
 28$forum_id	= request_var('f', 0);
 29$mark_read	= request_var('mark', '');
 30$start		= request_var('start', 0);
 31
 32$default_sort_days	= (!empty($user->data['user_topic_show_days'])) ? $user->data['user_topic_show_days'] : 0;
 33$default_sort_key	= (!empty($user->data['user_topic_sortby_type'])) ? $user->data['user_topic_sortby_type'] : 't';
 34$default_sort_dir	= (!empty($user->data['user_topic_sortby_dir'])) ? $user->data['user_topic_sortby_dir'] : 'd';
 35
 36$sort_days	= request_var('st', $default_sort_days);
 37$sort_key	= request_var('sk', $default_sort_key);
 38$sort_dir	= request_var('sd', $default_sort_dir);
 39
 40$pagination = $phpbb_container->get('pagination');
 41
 42// Check if the user has actually sent a forum ID with his/her request
 43// If not give them a nice error page.
 44if (!$forum_id)
 45{
 46	trigger_error('NO_FORUM');
 47}
 48
 49$sql_from = FORUMS_TABLE . ' f';
 50$lastread_select = '';
 51
 52// Grab appropriate forum data
 53if ($config['load_db_lastread'] && $user->data['is_registered'])
 54{
 55	$sql_from .= ' LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . '
 56		AND ft.forum_id = f.forum_id)';
 57	$lastread_select .= ', ft.mark_time';
 58}
 59
 60if ($user->data['is_registered'])
 61{
 62	$sql_from .= ' LEFT JOIN ' . FORUMS_WATCH_TABLE . ' fw ON (fw.forum_id = f.forum_id AND fw.user_id = ' . $user->data['user_id'] . ')';
 63	$lastread_select .= ', fw.notify_status';
 64}
 65
 66$sql = "SELECT f.* $lastread_select
 67	FROM $sql_from
 68	WHERE f.forum_id = $forum_id";
 69$result = $db->sql_query($sql);
 70$forum_data = $db->sql_fetchrow($result);
 71$db->sql_freeresult($result);
 72
 73if (!$forum_data)
 74{
 75	trigger_error('NO_FORUM');
 76}
 77
 78
 79// Configure style, language, etc.
 80$user->setup('viewforum', $forum_data['forum_style']);
 81
 82// Redirect to login upon emailed notification links
 83if (isset($_GET['e']) && !$user->data['is_registered'])
 84{
 85	login_box('', $user->lang['LOGIN_NOTIFY_FORUM']);
 86}
 87
 88// Permissions check
 89if (!$auth->acl_gets('f_list', 'f_read', $forum_id) || ($forum_data['forum_type'] == FORUM_LINK && $forum_data['forum_link'] && !$auth->acl_get('f_read', $forum_id)))
 90{
 91	if ($user->data['user_id'] != ANONYMOUS)
 92	{
 93		trigger_error('SORRY_AUTH_READ');
 94	}
 95
 96	login_box('', $user->lang['LOGIN_VIEWFORUM']);
 97}
 98
 99// Forum is passworded ... check whether access has been granted to this
100// user this session, if not show login box
101if ($forum_data['forum_password'])
102{
103	login_forum_box($forum_data);
104}
105
106// Is this forum a link? ... User got here either because the
107// number of clicks is being tracked or they guessed the id
108if ($forum_data['forum_type'] == FORUM_LINK && $forum_data['forum_link'])
109{
110	// Does it have click tracking enabled?
111	if ($forum_data['forum_flags'] & FORUM_FLAG_LINK_TRACK)
112	{
113		$sql = 'UPDATE ' . FORUMS_TABLE . '
114			SET forum_posts_approved = forum_posts_approved + 1
115			WHERE forum_id = ' . $forum_id;
116		$db->sql_query($sql);
117	}
118
119	// We redirect to the url. The third parameter indicates that external redirects are allowed.
120	redirect($forum_data['forum_link'], false, true);
121	return;
122}
123
124// Build navigation links
125generate_forum_nav($forum_data);
126
127// Forum Rules
128if ($auth->acl_get('f_read', $forum_id))
129{
130	generate_forum_rules($forum_data);
131}
132
133// Do we have subforums?
134$active_forum_ary = $moderators = array();
135
136if ($forum_data['left_id'] != $forum_data['right_id'] - 1)
137{
138	list($active_forum_ary, $moderators) = display_forums($forum_data, $config['load_moderators'], $config['load_moderators']);
139}
140else
141{
142	$template->assign_var('S_HAS_SUBFORUM', false);
143	if ($config['load_moderators'])
144	{
145		get_moderators($moderators, $forum_id);
146	}
147}
148
149$phpbb_content_visibility = $phpbb_container->get('content.visibility');
150
151// Dump out the page header and load viewforum template
152$topics_count = $phpbb_content_visibility->get_count('forum_topics', $forum_data, $forum_id);
153$start = $pagination->validate_start($start, $config['topics_per_page'], $topics_count);
154
155page_header($forum_data['forum_name'] . ($start ? ' - ' . $user->lang('PAGE_TITLE_NUMBER', $pagination->get_on_page($config['topics_per_page'], $start)) : ''), true, $forum_id);
156
157$template->set_filenames(array(
158	'body' => 'viewforum_body.html')
159);
160
161make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"), $forum_id);
162
163$template->assign_vars(array(
164	'U_VIEW_FORUM'			=> append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id" . (($start == 0) ? '' : "&amp;start=$start")),
165));
166
167// Not postable forum or showing active topics?
168if (!($forum_data['forum_type'] == FORUM_POST || (($forum_data['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS) && $forum_data['forum_type'] == FORUM_CAT)))
169{
170	page_footer();
171}
172
173// Ok, if someone has only list-access, we only display the forum list.
174// We also make this circumstance available to the template in case we want to display a notice. ;)
175if (!$auth->acl_get('f_read', $forum_id))
176{
177	$template->assign_vars(array(
178		'S_NO_READ_ACCESS'		=> true,
179	));
180
181	page_footer();
182}
183
184// Handle marking posts
185if ($mark_read == 'topics')
186{
187	$token = request_var('hash', '');
188	if (check_link_hash($token, 'global'))
189	{
190		markread('topics', array($forum_id), false, request_var('mark_time', 0));
191	}
192	$redirect_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id);
193	meta_refresh(3, $redirect_url);
194
195	if ($request->is_ajax())
196	{
197		// Tell the ajax script what language vars and URL need to be replaced
198		$data = array(
199			'NO_UNREAD_POSTS'	=> $user->lang['NO_UNREAD_POSTS'],
200			'UNREAD_POSTS'		=> $user->lang['UNREAD_POSTS'],
201			'U_MARK_TOPICS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}viewforum.$phpEx", 'hash=' . generate_link_hash('global') . "&f=$forum_id&mark=topics&mark_time=" . time()) : '',
202			'MESSAGE_TITLE'		=> $user->lang['INFORMATION'],
203			'MESSAGE_TEXT'		=> $user->lang['TOPICS_MARKED']
204		);
205		$json_response = new \phpbb\json_response();
206		$json_response->send($data);
207	}
208
209	trigger_error($user->lang['TOPICS_MARKED'] . '<br /><br />' . sprintf($user->lang['RETURN_FORUM'], '<a href="' . $redirect_url . '">', '</a>'));
210}
211
212// Is a forum specific topic count required?
213if ($forum_data['forum_topics_per_page'])
214{
215	$config['topics_per_page'] = $forum_data['forum_topics_per_page'];
216}
217
218// Do the forum Prune thang - cron type job ...
219if (!$config['use_system_cron'])
220{
221	$cron = $phpbb_container->get('cron.manager');
222
223	$task = $cron->find_task('cron.task.core.prune_forum');
224	$task->set_forum_data($forum_data);
225
226	if ($task->is_ready())
227	{
228		$url = $task->get_url();
229		$template->assign_var('RUN_CRON_TASK', '<img src="' . $url . '" width="1" height="1" alt="cron" />');
230	}
231	else
232	{
233		// See if we should prune the shadow topics instead
234		$task = $cron->find_task('cron.task.core.prune_shadow_topics');
235		$task->set_forum_data($forum_data);
236
237		if ($task->is_ready())
238		{
239			$url = $task->get_url();
240			$template->assign_var('RUN_CRON_TASK', '<img src="' . $url . '" width="1" height="1" alt="cron" />');
241		}
242	}
243}
244
245// Forum rules and subscription info
246$s_watching_forum = array(
247	'link'			=> '',
248	'link_toggle'	=> '',
249	'title'			=> '',
250	'title_toggle'	=> '',
251	'is_watching'	=> false,
252);
253
254if ($config['allow_forum_notify'] && $forum_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_subscribe', $forum_id) || $user->data['user_id'] == ANONYMOUS))
255{
256	$notify_status = (isset($forum_data['notify_status'])) ? $forum_data['notify_status'] : NULL;
257	watch_topic_forum('forum', $s_watching_forum, $user->data['user_id'], $forum_id, 0, $notify_status, $start, $forum_data['forum_name']);
258}
259
260$s_forum_rules = '';
261gen_forum_auth_level('forum', $forum_id, $forum_data['forum_status']);
262
263// Topic ordering options
264$limit_days = array(0 => $user->lang['ALL_TOPICS'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
265
266$sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 'r' => $user->lang['REPLIES'], 's' => $user->lang['SUBJECT'], 'v' => $user->lang['VIEWS']);
267$sort_by_sql = array('a' => 't.topic_first_poster_name', 't' => array('t.topic_last_post_time', 't.topic_last_post_id'), 'r' => (($auth->acl_get('m_approve', $forum_id)) ? 't.topic_posts_approved + t.topic_posts_unapproved + t.topic_posts_softdeleted' : 't.topic_posts_approved'), 's' => 't.topic_title', 'v' => 't.topic_views');
268
269$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
270gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param, $default_sort_days, $default_sort_key, $default_sort_dir);
271
272// Limit topics to certain time frame, obtain correct topic count
273if ($sort_days)
274{
275	$min_post_time = time() - ($sort_days * 86400);
276
277	$sql = 'SELECT COUNT(topic_id) AS num_topics
278		FROM ' . TOPICS_TABLE . "
279		WHERE forum_id = $forum_id
280			AND (topic_last_post_time >= $min_post_time
281				OR topic_type = " . POST_ANNOUNCE . '
282				OR topic_type = ' . POST_GLOBAL . ')
283			AND ' . $phpbb_content_visibility->get_visibility_sql('topic', $forum_id);
284	$result = $db->sql_query($sql);
285	$topics_count = (int) $db->sql_fetchfield('num_topics');
286	$db->sql_freeresult($result);
287
288	if (isset($_POST['sort']))
289	{
290		$start = 0;
291	}
292	$sql_limit_time = "AND t.topic_last_post_time >= $min_post_time";
293
294	// Make sure we have information about day selection ready
295	$template->assign_var('S_SORT_DAYS', true);
296}
297else
298{
299	$sql_limit_time = '';
300}
301
302// Basic pagewide vars
303$post_alt = ($forum_data['forum_status'] == ITEM_LOCKED) ? $user->lang['FORUM_LOCKED'] : $user->lang['POST_NEW_TOPIC'];
304
305// Display active topics?
306$s_display_active = ($forum_data['forum_type'] == FORUM_CAT && ($forum_data['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS)) ? true : false;
307
308$s_search_hidden_fields = array('fid' => array($forum_id));
309if ($_SID)
310{
311	$s_search_hidden_fields['sid'] = $_SID;
312}
313
314if (!empty($_EXTRA_URL))
315{
316	foreach ($_EXTRA_URL as $url_param)
317	{
318		$url_param = explode('=', $url_param, 2);
319		$s_search_hidden_fields[$url_param[0]] = $url_param[1];
320	}
321}
322
323$template->assign_vars(array(
324	'MODERATORS'	=> (!empty($moderators[$forum_id])) ? implode($user->lang['COMMA_SEPARATOR'], $moderators[$forum_id]) : '',
325
326	'POST_IMG'					=> ($forum_data['forum_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', $post_alt) : $user->img('button_topic_new', $post_alt),
327	'NEWEST_POST_IMG'			=> $user->img('icon_topic_newest', 'VIEW_NEWEST_POST'),
328	'LAST_POST_IMG'				=> $user->img('icon_topic_latest', 'VIEW_LATEST_POST'),
329	'FOLDER_IMG'				=> $user->img('topic_read', 'NO_UNREAD_POSTS'),
330	'FOLDER_UNREAD_IMG'			=> $user->img('topic_unread', 'UNREAD_POSTS'),
331	'FOLDER_HOT_IMG'			=> $user->img('topic_read_hot', 'NO_UNREAD_POSTS_HOT'),
332	'FOLDER_HOT_UNREAD_IMG'		=> $user->img('topic_unread_hot', 'UNREAD_POSTS_HOT'),
333	'FOLDER_LOCKED_IMG'			=> $user->img('topic_read_locked', 'NO_UNREAD_POSTS_LOCKED'),
334	'FOLDER_LOCKED_UNREAD_IMG'	=> $user->img('topic_unread_locked', 'UNREAD_POSTS_LOCKED'),
335	'FOLDER_STICKY_IMG'			=> $user->img('sticky_read', 'POST_STICKY'),
336	'FOLDER_STICKY_UNREAD_IMG'	=> $user->img('sticky_unread', 'POST_STICKY'),
337	'FOLDER_ANNOUNCE_IMG'		=> $user->img('announce_read', 'POST_ANNOUNCEMENT'),
338	'FOLDER_ANNOUNCE_UNREAD_IMG'=> $user->img('announce_unread', 'POST_ANNOUNCEMENT'),
339	'FOLDER_MOVED_IMG'			=> $user->img('topic_moved', 'TOPIC_MOVED'),
340	'REPORTED_IMG'				=> $user->img('icon_topic_reported', 'TOPIC_REPORTED'),
341	'UNAPPROVED_IMG'			=> $user->img('icon_topic_unapproved', 'TOPIC_UNAPPROVED'),
342	'DELETED_IMG'				=> $user->img('icon_topic_deleted', 'TOPIC_DELETED'),
343	'POLL_IMG'					=> $user->img('icon_topic_poll', 'TOPIC_POLL'),
344	'GOTO_PAGE_IMG'				=> $user->img('icon_post_target', 'GOTO_PAGE'),
345
346	'L_NO_TOPICS' 			=> ($forum_data['forum_status'] == ITEM_LOCKED) ? $user->lang['POST_FORUM_LOCKED'] : $user->lang['NO_TOPICS'],
347
348	'S_DISPLAY_POST_INFO'	=> ($forum_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_post', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
349
350	'S_IS_POSTABLE'			=> ($forum_data['forum_type'] == FORUM_POST) ? true : false,
351	'S_USER_CAN_POST'		=> ($auth->acl_get('f_post', $forum_id)) ? true : false,
352	'S_DISPLAY_ACTIVE'		=> $s_display_active,
353	'S_SELECT_SORT_DIR'		=> $s_sort_dir,
354	'S_SELECT_SORT_KEY'		=> $s_sort_key,
355	'S_SELECT_SORT_DAYS'	=> $s_limit_days,
356	'S_TOPIC_ICONS'			=> ($s_display_active && sizeof($active_forum_ary)) ? max($active_forum_ary['enable_icons']) : (($forum_data['enable_icons']) ? true : false),
357	'U_WATCH_FORUM_LINK'	=> $s_watching_forum['link'],
358	'U_WATCH_FORUM_TOGGLE'	=> $s_watching_forum['link_toggle'],
359	'S_WATCH_FORUM_TITLE'	=> $s_watching_forum['title'],
360	'S_WATCH_FORUM_TOGGLE'	=> $s_watching_forum['title_toggle'],
361	'S_WATCHING_FORUM'		=> $s_watching_forum['is_watching'],
362	'S_FORUM_ACTION'		=> append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id" . (($start == 0) ? '' : "&amp;start=$start")),
363	'S_DISPLAY_SEARCHBOX'	=> ($auth->acl_get('u_search') && $auth->acl_get('f_search', $forum_id) && $config['load_search']) ? true : false,
364	'S_SEARCHBOX_ACTION'	=> append_sid("{$phpbb_root_path}search.$phpEx"),
365	'S_SEARCH_LOCAL_HIDDEN_FIELDS'	=> build_hidden_fields($s_search_hidden_fields),
366	'S_SINGLE_MODERATOR'	=> (!empty($moderators[$forum_id]) && sizeof($moderators[$forum_id]) > 1) ? false : true,
367	'S_IS_LOCKED'			=> ($forum_data['forum_status'] == ITEM_LOCKED) ? true : false,
368	'S_VIEWFORUM'			=> true,
369
370	'U_MCP'				=> ($auth->acl_get('m_', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "f=$forum_id&amp;i=main&amp;mode=forum_view", true, $user->session_id) : '',
371	'U_POST_NEW_TOPIC'	=> ($auth->acl_get('f_post', $forum_id) || $user->data['user_id'] == ANONYMOUS) ? append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=post&amp;f=' . $forum_id) : '',
372	'U_VIEW_FORUM'		=> append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id" . ((strlen($u_sort_param)) ? "&amp;$u_sort_param" : '') . (($start == 0) ? '' : "&amp;start=$start")),
373	'U_CANONICAL'		=> generate_board_url() . '/' . append_sid("viewforum.$phpEx", "f=$forum_id" . (($start) ? "&amp;start=$start" : ''), true, ''),
374	'U_MARK_TOPICS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}viewforum.$phpEx", 'hash=' . generate_link_hash('global') . "&amp;f=$forum_id&amp;mark=topics&amp;mark_time=" . time()) : '',
375));
376
377// Grab icons
378$icons = $cache->obtain_icons();
379
380// Grab all topic data
381$rowset = $announcement_list = $topic_list = $global_announce_forums = array();
382
383$sql_array = array(
384	'SELECT'	=> 't.*',
385	'FROM'		=> array(
386		TOPICS_TABLE		=> 't'
387	),
388	'LEFT_JOIN'	=> array(),
389);
390
391/**
392* Event to modify the SQL query before the topic data is retrieved
393*
394* It may also be used to override the above assigned template vars
395*
396* @event core.viewforum_get_topic_data
397* @var	array	forum_data			Array with forum data
398* @var	array	sql_array			The SQL array to get the data of all topics
399* @var	array	forum_id			The forum_id whose topics are being listed
400* @var	array	topics_count		The total number of topics for display
401* @var	array	sort_days			The oldest topic displayable in elapsed days
402* @var	array	sort_key			The sorting by. It is one of the first character of (in low case):
403*									Author, Post time, Replies, Subject, Views
404* @var	array	sort_dir			Either "a" for ascending or "d" for descending
405* @since 3.1.0-a1
406* @change 3.1.0-RC4 Added forum_data var
407* @change 3.1.4-RC1 Added forum_id, topics_count, sort_days, sort_key and sort_dir vars
408*/
409$vars = array(
410	'forum_data',
411	'sql_array',
412	'forum_id',
413	'topics_count',
414	'sort_days',
415	'sort_key',
416	'sort_dir',
417);
418extract($phpbb_dispatcher->trigger_event('core.viewforum_get_topic_data', compact($vars)));
419
420$sql_approved = ' AND ' . $phpbb_content_visibility->get_visibility_sql('topic', $forum_id, 't.');
421
422if ($user->data['is_registered'])
423{
424	if ($config['load_db_track'])
425	{
426		$sql_array['LEFT_JOIN'][] = array('FROM' => array(TOPICS_POSTED_TABLE => 'tp'), 'ON' => 'tp.topic_id = t.topic_id AND tp.user_id = ' . $user->data['user_id']);
427		$sql_array['SELECT'] .= ', tp.topic_posted';
428	}
429
430	if ($config['load_db_lastread'])
431	{
432		$sql_array['LEFT_JOIN'][] = array('FROM' => array(TOPICS_TRACK_TABLE => 'tt'), 'ON' => 'tt.topic_id = t.topic_id AND tt.user_id = ' . $user->data['user_id']);
433		$sql_array['SELECT'] .= ', tt.mark_time';
434
435		if ($s_display_active && sizeof($active_forum_ary))
436		{
437			$sql_array['LEFT_JOIN'][] = array('FROM' => array(FORUMS_TRACK_TABLE => 'ft'), 'ON' => 'ft.forum_id = t.forum_id AND ft.user_id = ' . $user->data['user_id']);
438			$sql_array['SELECT'] .= ', ft.mark_time AS forum_mark_time';
439		}
440	}
441}
442
443if ($forum_data['forum_type'] == FORUM_POST)
444{
445	// Get global announcement forums
446	$g_forum_ary = $auth->acl_getf('f_read', true);
447	$g_forum_ary = array_unique(array_keys($g_forum_ary));
448
449	$sql_anounce_array['LEFT_JOIN'] = $sql_array['LEFT_JOIN'];
450	$sql_anounce_array['LEFT_JOIN'][] = array('FROM' => array(FORUMS_TABLE => 'f'), 'ON' => 'f.forum_id = t.forum_id');
451	$sql_anounce_array['SELECT'] = $sql_array['SELECT'] . ', f.forum_name';
452
453	// Obtain announcements ... removed sort ordering, sort by time in all cases
454	$sql_ary = array(
455		'SELECT'	=> $sql_anounce_array['SELECT'],
456		'FROM'		=> $sql_array['FROM'],
457		'LEFT_JOIN'	=> $sql_anounce_array['LEFT_JOIN'],
458
459		'WHERE'		=> '(t.forum_id = ' . $forum_id . '
460				AND t.topic_type = ' . POST_ANNOUNCE . ') OR
461			(' . $db->sql_in_set('t.forum_id', $g_forum_ary) . '
462				AND t.topic_type = ' . POST_GLOBAL . ')',
463
464		'ORDER_BY'	=> 't.topic_time DESC',
465	);
466	$sql = $db->sql_build_query('SELECT', $sql_ary);
467	$result = $db->sql_query($sql);
468
469	while ($row = $db->sql_fetchrow($result))
470	{
471		if ($row['topic_visibility'] != ITEM_APPROVED && !$auth->acl_get('m_approve', $row['forum_id']))
472		{
473			// Do not display announcements that are waiting for approval or soft deleted.
474			continue;
475		}
476
477		$rowset[$row['topic_id']] = $row;
478		$announcement_list[] = $row['topic_id'];
479
480		if ($forum_id != $row['forum_id'])
481		{
482			$topics_count++;
483			$global_announce_forums[] = $row['forum_id'];
484		}
485	}
486	$db->sql_freeresult($result);
487}
488
489$forum_tracking_info = array();
490
491if ($user->data['is_registered'] && $config['load_db_lastread'])
492{
493	$forum_tracking_info[$forum_id] = $forum_data['mark_time'];
494
495	if (!empty($global_announce_forums))
496	{
497		$sql = 'SELECT forum_id, mark_time
498			FROM ' . FORUMS_TRACK_TABLE . '
499			WHERE ' . $db->sql_in_set('forum_id', $global_announce_forums) . '
500				AND user_id = ' . $user->data['user_id'];
501		$result = $db->sql_query($sql);
502
503		while ($row = $db->sql_fetchrow($result))
504		{
505			$forum_tracking_info[$row['forum_id']] = $row['mark_time'];
506		}
507		$db->sql_freeresult($result);
508	}
509}
510
511// If the user is trying to reach late pages, start searching from the end
512$store_reverse = false;
513$sql_limit = $config['topics_per_page'];
514if ($start > $topics_count / 2)
515{
516	$store_reverse = true;
517
518	// Select the sort order
519	$direction = (($sort_dir == 'd') ? 'ASC' : 'DESC');
520
521	$sql_limit = $pagination->reverse_limit($start, $sql_limit, $topics_count - sizeof($announcement_list));
522	$sql_start = $pagination->reverse_start($start, $sql_limit, $topics_count - sizeof($announcement_list));
523}
524else
525{
526	// Select the sort order
527	$direction = (($sort_dir == 'd') ? 'DESC' : 'ASC');
528	$sql_start = $start;
529}
530
531if (is_array($sort_by_sql[$sort_key]))
532{
533	$sql_sort_order = implode(' ' . $direction . ', ', $sort_by_sql[$sort_key]) . ' ' . $direction;
534}
535else
536{
537	$sql_sort_order = $sort_by_sql[$sort_key] . ' ' . $direction;
538}
539
540if ($forum_data['forum_type'] == FORUM_POST || !sizeof($active_forum_ary))
541{
542	$sql_where = 't.forum_id = ' . $forum_id;
543}
544else if (empty($active_forum_ary['exclude_forum_id']))
545{
546	$sql_where = $db->sql_in_set('t.forum_id', $active_forum_ary['forum_id']);
547}
548else
549{
550	$get_forum_ids = array_diff($active_forum_ary['forum_id'], $active_forum_ary['exclude_forum_id']);
551	$sql_where = (sizeof($get_forum_ids)) ? $db->sql_in_set('t.forum_id', $get_forum_ids) : 't.forum_id = ' . $forum_id;
552}
553
554// Grab just the sorted topic ids
555$sql_ary = array(
556	'SELECT'	=> 't.topic_id',
557	'FROM'		=> array(
558		TOPICS_TABLE => 't',
559	),
560	'WHERE'		=> "$sql_where
561		AND t.topic_type IN (" . POST_NORMAL . ', ' . POST_STICKY . ")
562		$sql_approved
563		$sql_limit_time",
564	'ORDER_BY'	=> 't.topic_type ' . ((!$store_reverse) ? 'DESC' : 'ASC') . ', ' . $sql_sort_order,
565);
566
567/**
568* Event to modify the SQL query before the topic ids data is retrieved
569*
570* @event core.viewforum_get_topic_ids_data
571* @var	array	forum_data		Data about the forum
572* @var	array	sql_ary			SQL query array to get the topic ids data
573* @var	string	sql_approved	Topic visibility SQL string
574* @var	int		sql_limit		Number of records to select
575* @var	string	sql_limit_time	SQL string to limit topic_last_post_time data
576* @var	array	sql_sort_order	SQL sorting string
577* @var	int		sql_start		Offset point to start selection from
578* @var	string	sql_where		SQL WHERE clause string
579* @var	bool	store_reverse	Flag indicating if we select from the late pages
580*
581* @since 3.1.0-RC4
582*
583* @changed 3.1.3 Added forum_data
584*/
585$vars = array(
586	'forum_data',
587	'sql_ary',
588	'sql_approved',
589	'sql_limit',
590	'sql_limit_time',
591	'sql_sort_order',
592	'sql_start',
593	'sql_where',
594	'store_reverse',
595);
596extract($phpbb_dispatcher->trigger_event('core.viewforum_get_topic_ids_data', compact($vars)));
597
598$sql = $db->sql_build_query('SELECT', $sql_ary);
599$result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
600
601while ($row = $db->sql_fetchrow($result))
602{
603	$topic_list[] = (int) $row['topic_id'];
604}
605$db->sql_freeresult($result);
606
607// For storing shadow topics
608$shadow_topic_list = array();
609
610if (sizeof($topic_list))
611{
612	// SQL array for obtaining topics/stickies
613	$sql_array = array(
614		'SELECT'		=> $sql_array['SELECT'],
615		'FROM'			=> $sql_array['FROM'],
616		'LEFT_JOIN'		=> $sql_array['LEFT_JOIN'],
617
618		'WHERE'			=> $db->sql_in_set('t.topic_id', $topic_list),
619	);
620
621	// If store_reverse, then first obtain topics, then stickies, else the other way around...
622	// Funnily enough you typically save one query if going from the last page to the middle (store_reverse) because
623	// the number of stickies are not known
624	$sql = $db->sql_build_query('SELECT', $sql_array);
625	$result = $db->sql_query($sql);
626
627	while ($row = $db->sql_fetchrow($result))
628	{
629		if ($row['topic_status'] == ITEM_MOVED)
630		{
631			$shadow_topic_list[$row['topic_moved_id']] = $row['topic_id'];
632		}
633
634		$rowset[$row['topic_id']] = $row;
635	}
636	$db->sql_freeresult($result);
637}
638
639// If we have some shadow topics, update the rowset to reflect their topic information
640if (sizeof($shadow_topic_list))
641{
642	// SQL array for obtaining shadow topics
643	$sql_array = array(
644		'SELECT'	=> 't.*',
645		'FROM'		=> array(
646			TOPICS_TABLE		=> 't'
647		),
648		'WHERE'		=> $db->sql_in_set('t.topic_id', array_keys($shadow_topic_list)),
649	);
650
651	/**
652	* Event to modify the SQL query before the shadowtopic data is retrieved
653	*
654	* @event core.viewforum_get_shadowtopic_data
655	* @var	array	sql_array		SQL array to get the data of any shadowtopics
656	* @since 3.1.0-a1
657	*/
658	$vars = array('sql_array');
659	extract($phpbb_dispatcher->trigger_event('core.viewforum_get_shadowtopic_data', compact($vars)));
660
661	$sql = $db->sql_build_query('SELECT', $sql_array);
662	$result = $db->sql_query($sql);
663
664	while ($row = $db->sql_fetchrow($result))
665	{
666		$orig_topic_id = $shadow_topic_list[$row['topic_id']];
667
668		// If the shadow topic is already listed within the rowset (happens for active topics for example), then do not include it...
669		if (isset($rowset[$row['topic_id']]))
670		{
671			// We need to remove any trace regarding this topic. :)
672			unset($rowset[$orig_topic_id]);
673			unset($topic_list[array_search($orig_topic_id, $topic_list)]);
674			$topics_count--;
675
676			continue;
677		}
678
679		// Do not include those topics the user has no permission to access
680		if (!$auth->acl_get('f_read', $row['forum_id']))
681		{
682			// We need to remove any trace regarding this topic. :)
683			unset($rowset[$orig_topic_id]);
684			unset($topic_list[array_search($orig_topic_id, $topic_list)]);
685			$topics_count--;
686
687			continue;
688		}
689
690		// We want to retain some values
691		$row = array_merge($row, array(
692			'topic_moved_id'	=> $rowset[$orig_topic_id]['topic_moved_id'],
693			'topic_status'		=> $rowset[$orig_topic_id]['topic_status'],
694			'topic_type'		=> $rowset[$orig_topic_id]['topic_type'],
695			'topic_title'		=> $rowset[$orig_topic_id]['topic_title'],
696		));
697
698		// Shadow topics are never reported
699		$row['topic_reported'] = 0;
700
701		$rowset[$orig_topic_id] = $row;
702	}
703	$db->sql_freeresult($result);
704}
705unset($shadow_topic_list);
706
707// Ok, adjust topics count for active topics list
708if ($s_display_active)
709{
710	$topics_count = 1;
711}
712
713// We need to remove the global announcements from the forums total topic count,
714// otherwise the number is different from the one on the forum list
715$total_topic_count = $topics_count - sizeof($announcement_list);
716
717$base_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id" . ((strlen($u_sort_param)) ? "&amp;$u_sort_param" : ''));
718$pagination->generate_template_pagination($base_url, 'pagination', 'start', $total_topic_count, $config['topics_per_page'], $start);
719
720$template->assign_vars(array(
721	'TOTAL_TOPICS'	=> ($s_display_active) ? false : $user->lang('VIEW_FORUM_TOPICS', (int) $total_topic_count),
722));
723
724$topic_list = ($store_reverse) ? array_merge($announcement_list, array_reverse($topic_list)) : array_merge($announcement_list, $topic_list);
725$topic_tracking_info = $tracking_topics = array();
726
727/**
728* Modify topics data before we display the viewforum page
729*
730* @event core.viewforum_modify_topics_data
731* @var	array	topic_list			Array with current viewforum page topic ids
732* @var	array	rowset				Array with topics data (in topic_id => topic_data format)
733* @var	int		total_topic_count	Forum's total topic count
734* @since 3.1.0-b3
735*/
736$vars = array('topic_list', 'rowset', 'total_topic_count');
737extract($phpbb_dispatcher->trigger_event('core.viewforum_modify_topics_data', compact($vars)));
738
739// Okay, lets dump out the page ...
740if (sizeof($topic_list))
741{
742	$mark_forum_read = true;
743	$mark_time_forum = 0;
744
745	// Generate topic forum list...
746	$topic_forum_list = array();
747	foreach ($rowset as $t_id => $row)
748	{
749		if (isset($forum_tracking_info[$row['forum_id']]))
750		{
751			$row['forum_mark_time'] = $forum_tracking_info[$row['forum_id']];
752		}
753
754		$topic_forum_list[$row['forum_id']]['forum_mark_time'] = ($config['load_db_lastread'] && $user->data['is_registered'] && isset($row['forum_mark_time'])) ? $row['forum_mark_time'] : 0;
755		$topic_forum_list[$row['forum_id']]['topics'][] = (int) $t_id;
756	}
757
758	if ($config['load_db_lastread'] && $user->data['is_registered'])
759	{
760		foreach ($topic_forum_list as $f_id => $topic_row)
761		{
762			$topic_tracking_info += get_topic_tracking($f_id, $topic_row['topics'], $rowset, array($f_id => $topic_row['forum_mark_time']));
763		}
764	}
765	else if ($config['load_anon_lastread'] || $user->data['is_registered'])
766	{
767		foreach ($topic_forum_list as $f_id => $topic_row)
768		{
769			$topic_tracking_info += get_complete_topic_tracking($f_id, $topic_row['topics']);
770		}
771	}
772
773	unset($topic_forum_list);
774
775	if (!$s_display_active)
776	{
777		if ($config['load_db_lastread'] && $user->data['is_registered'])
778		{
779			$mark_time_forum = (!empty($forum_data['mark_time'])) ? $forum_data['mark_time'] : $user->data['user_lastmark'];
780		}
781		else if ($config['load_anon_lastread'] || $user->data['is_registered'])
782		{
783			if (!$user->data['is_registered'])
784			{
785				$user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
786			}
787			$mark_time_forum = (isset($tracking_topics['f'][$forum_id])) ? (int) (base_convert($tracking_topics['f'][$forum_id], 36, 10) + $config['board_startdate']) : $user->data['user_lastmark'];
788		}
789	}
790
791	$s_type_switch = 0;
792	foreach ($topic_list as $topic_id)
793	{
794		$row = &$rowset[$topic_id];
795
796		$topic_forum_id = ($row['forum_id']) ? (int) $row['forum_id'] : $forum_id;
797
798		// This will allow the style designer to output a different header
799		// or even separate the list of announcements from sticky and normal topics
800		$s_type_switch_test = ($row['topic_type'] == POST_ANNOUNCE || $row['topic_type'] == POST_GLOBAL) ? 1 : 0;
801
802		// Replies
803		$replies = $phpbb_content_visibility->get_count('topic_posts', $row, $topic_forum_id) - 1;
804
805		if ($row['topic_status'] == ITEM_MOVED)
806		{
807			$topic_id = $row['topic_moved_id'];
808			$unread_topic = false;
809		}
810		else
811		{
812			$unread_topic = (isset($topic_tracking_info[$topic_id]) && $row['topic_last_post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
813		}
814
815		// Get folder img, topic status/type related information
816		$folder_img = $folder_alt = $topic_type = '';
817		topic_status($row, $replies, $unread_topic, $folder_img, $folder_alt, $topic_type);
818
819		// Generate all the URIs ...
820		$view_topic_url_params = 'f=' . $row['forum_id'] . '&amp;t=' . $topic_id;
821		$view_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params);
822
823		$topic_unapproved = (($row['topic_visibility'] == ITEM_UNAPPROVED || $row['topic_visibility'] == ITEM_REAPPROVE) && $auth->acl_get('m_approve', $row['forum_id']));
824		$posts_unapproved = ($row['topic_visibility'] == ITEM_APPROVED && $row['topic_posts_unapproved'] && $auth->acl_get('m_approve', $row['forum_id']));
825		$topic_deleted = $row['topic_visibility'] == ITEM_DELETED;
826
827		$u_mcp_queue = ($topic_unapproved || $posts_unapproved) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=' . (($topic_unapproved) ? 'approve_details' : 'unapproved_posts') . "&amp;t=$topic_id", true, $user->session_id) : '';
828		$u_mcp_queue = (!$u_mcp_queue && $topic_deleted) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=deleted_topics&amp;t=' . $topic_id, true, $user->session_id) : $u_mcp_queue;
829
830		// Send vars to template
831		$topic_row = array(
832			'FORUM_ID'					=> $row['forum_id'],
833			'TOPIC_ID'					=> $topic_id,
834			'TOPIC_AUTHOR'				=> get_username_string('username', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
835			'TOPIC_AUTHOR_COLOUR'		=> get_username_string('colour', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
836			'TOPIC_AUTHOR_FULL'			=> get_username_string('full', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
837			'FIRST_POST_TIME'			=> $user->format_date($row['topic_time']),
838			'LAST_POST_SUBJECT'			=> censor_text($row['topic_last_post_subject']),
839			'LAST_POST_TIME'			=> $user->format_date($row['topic_last_post_time']),
840			'LAST_VIEW_TIME'			=> $user->format_date($row['topic_last_view_time']),
841			'LAST_POST_AUTHOR'			=> get_username_string('username', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
842			'LAST_POST_AUTHOR_COLOUR'	=> get_username_string('colour', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
843			'LAST_POST_AUTHOR_FULL'		=> get_username_string('full', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
844
845			'REPLIES'			=> $replies,
846			'VIEWS'				=> $row['topic_views'],
847			'TOPIC_TITLE'		=> censor_text($row['topic_title']),
848			'TOPIC_TYPE'		=> $topic_type,
849			'FORUM_NAME'		=> (isset($row['forum_name'])) ? $row['forum_name'] : $forum_data['forum_name'],
850
851			'TOPIC_IMG_STYLE'		=> $folder_img,
852			'TOPIC_FOLDER_IMG'		=> $user->img($folder_img, $folder_alt),
853			'TOPIC_FOLDER_IMG_ALT'	=> $user->lang[$folder_alt],
854
855			'TOPIC_ICON_IMG'		=> (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['img'] : '',
856			'TOPIC_ICON_IMG_WIDTH'	=> (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['width'] : '',
857			'TOPIC_ICON_IMG_HEIGHT'	=> (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['height'] : '',
858			'ATTACH_ICON_IMG'		=> ($auth->acl_get('u_download') && $auth->acl_get('f_download', $row['forum_id']) && $row['topic_attachment']) ? $user->img('icon_topic_attach', $user->lang['TOTAL_ATTACHMENTS']) : '',
859			'UNAPPROVED_IMG'		=> ($topic_unapproved || $posts_unapproved) ? $user->img('icon_topic_unapproved', ($topic_unapproved) ? 'TOPIC_UNAPPROVED' : 'POSTS_UNAPPROVED') : '',
860
861			'S_TOPIC_TYPE'			=> $row['topic_type'],
862			'S_USER_POSTED'			=> (isset($row['topic_posted']) && $row['topic_posted']) ? true : false,
863			'S_UNREAD_TOPIC'		=> $unread_topic,
864			'S_TOPIC_REPORTED'		=> (!empty($row['topic_reported']) && $auth->acl_get('m_report', $row['forum_id'])) ? true : false,
865			'S_TOPIC_UNAPPROVED'	=> $topic_unapproved,
866			'S_POSTS_UNAPPROVED'	=> $posts_unapproved,
867			'S_TOPIC_DELETED'		=> $topic_deleted,
868			'S_HAS_POLL'			=> ($row['poll_start']) ? true : false,
869			'S_POST_ANNOUNCE'		=> ($row['topic_type'] == POST_ANNOUNCE) ? true : false,
870			'S_POST_GLOBAL'			=> ($row['topic_type'] == POST_GLOBAL) ? true : false,
871			'S_POST_STICKY'			=> ($row['topic_type'] == POST_STICKY) ? true : false,
872			'S_TOPIC_LOCKED'		=> ($row['topic_status'] == ITEM_LOCKED) ? true : false,
873			'S_TOPIC_MOVED'			=> ($row['topic_status'] == ITEM_MOVED) ? true : false,
874
875			'U_NEWEST_POST'			=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params . '&amp;view=unread') . '#unread',
876			'U_LAST_POST'			=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params . '&amp;p=' . $row['topic_last_post_id']) . '#p' . $row['topic_last_post_id'],
877			'U_LAST_POST_AUTHOR'	=> get_username_string('profile', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
878			'U_TOPIC_AUTHOR'		=> get_username_string('profile', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
879			'U_VIEW_TOPIC'			=> $view_topic_url,
880			'U_VIEW_FORUM'			=> append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']),
881			'U_MCP_REPORT'			=> append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=reports&amp;f=' . $row['forum_id'] . '&amp;t=' . $topic_id, true, $user->session_id),
882			'U_MCP_QUEUE'			=> $u_mcp_queue,
883
884			'S_TOPIC_TYPE_SWITCH'	=> ($s_type_switch == $s_type_switch_test) ? -1 : $s_type_switch_test,
885		);
886
887		/**
888		* Modify the topic data before it is assigned to the template
889		*
890		* @event core.viewforum_modify_topicrow
891		* @var	array	row			Array with topic data
892		* @var	array	topic_row	Template array with topic data
893		* @since 3.1.0-a1
894		*/
895		$vars = array('row', 'topic_row');
896		extract($phpbb_dispatcher->trigger_event('core.viewforum_modify_topicrow', compact($vars)));
897
898		$template->assign_block_vars('topicrow', $topic_row);
899
900		$pagination->generate_template_pagination($view_topic_url, 'topicrow.pagination', 'start', $replies + 1, $config['posts_per_page'], 1, true, true);
901
902		$s_type_switch = ($row['topic_type'] == POST_ANNOUNCE || $row['topic_type'] == POST_GLOBAL) ? 1 : 0;
903
904		/**
905		* Event after the topic data has been assigned to the template
906		*
907		* @event core.viewforum_topic_row_after
908		* @var	array	row				Array with the topic data
909		* @var	array	rowset			Array with topics data (in topic_id => topic_data format)
910		* @var	bool	s_type_switch	Flag indicating if the topic type is [global] announcement
911		* @var	int		topic_id		The topic ID
912		* @var	array	topic_list		Array with current viewforum page topic ids
913		* @var	array	topic_row		Template array with topic data
914		* @since 3.1.3-RC1
915		*/
916		$vars = array(
917			'row',
918			'rowset',
919			's_type_switch',
920			'topic_id',
921			'topic_list',
922			'topic_row',
923		);
924		extract($phpbb_dispatcher->trigger_event('core.viewforum_topic_row_after', compact($vars)));
925
926		if ($unread_topic)
927		{
928			$mark_forum_read = false;
929		}
930
931		unset($rowset[$topic_id]);
932	}
933}
934
935// This is rather a fudge but it's the best I can think of without requiring information
936// on all topics (as we do in 2.0.x). It looks for unread or new topics, if it doesn't find
937// any it updates the forum last read cookie. This requires that the user visit the forum
938// after reading a topic
939if ($forum_data['forum_type'] == FORUM_POST && sizeof($topic_list) && $mark_forum_read)
940{
941	update_forum_tracking_info($forum_id, $forum_data['forum_last_post_time'], false, $mark_time_forum);
942}
943
944page_footer();