phpBB
Statistics
| Revision:

root / trunk / phpBB / viewforum.php

History | View | Annotate | Download (29.5 kB)

1
<?php
2
/**
3
*
4
* @package phpBB3
5
* @copyright (c) 2005 phpBB Group
6
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
*
8
*/
9
10
/**
11
* @ignore
12
*/
13
define('IN_PHPBB', true);
14
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
15
$phpEx = substr(strrchr(__FILE__, '.'), 1);
16
include($phpbb_root_path . 'common.' . $phpEx);
17
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
18
19
// Start session
20
$user->session_begin();
21
$auth->acl($user->data);
22
23
// Start initial var setup
24
$forum_id        = request_var('f', 0);
25
$mark_read        = request_var('mark', '');
26
$start                = request_var('start', 0);
27
28
$default_sort_days        = (!empty($user->data['user_topic_show_days'])) ? $user->data['user_topic_show_days'] : 0;
29
$default_sort_key        = (!empty($user->data['user_topic_sortby_type'])) ? $user->data['user_topic_sortby_type'] : 't';
30
$default_sort_dir        = (!empty($user->data['user_topic_sortby_dir'])) ? $user->data['user_topic_sortby_dir'] : 'd';
31
32
$sort_days        = request_var('st', $default_sort_days);
33
$sort_key        = request_var('sk', $default_sort_key);
34
$sort_dir        = request_var('sd', $default_sort_dir);
35
36
// Check if the user has actually sent a forum ID with his/her request
37
// If not give them a nice error page.
38
if (!$forum_id)
39
{
40
        trigger_error('NO_FORUM');
41
}
42
43
$sql_from = FORUMS_TABLE . ' f';
44
$lastread_select = '';
45
46
// Grab appropriate forum data
47
if ($config['load_db_lastread'] && $user->data['is_registered'])
48
{
49
        $sql_from .= ' LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . '
50
                AND ft.forum_id = f.forum_id)';
51
        $lastread_select .= ', ft.mark_time';
52
}
53
54
if ($user->data['is_registered'])
55
{
56
        $sql_from .= ' LEFT JOIN ' . FORUMS_WATCH_TABLE . ' fw ON (fw.forum_id = f.forum_id AND fw.user_id = ' . $user->data['user_id'] . ')';
57
        $lastread_select .= ', fw.notify_status';
58
}
59
60
$sql = "SELECT f.* $lastread_select
61
        FROM $sql_from
62
        WHERE f.forum_id = $forum_id";
63
$result = $db->sql_query($sql);
64
$forum_data = $db->sql_fetchrow($result);
65
$db->sql_freeresult($result);
66
67
if (!$forum_data)
68
{
69
        trigger_error('NO_FORUM');
70
}
71
72
73
// Configure style, language, etc.
74
$user->setup('viewforum', $forum_data['forum_style']);
75
76
// Redirect to login upon emailed notification links
77
if (isset($_GET['e']) && !$user->data['is_registered'])
78
{
79
        login_box('', $user->lang['LOGIN_NOTIFY_FORUM']);
80
}
81
82
// Permissions check
83
if (!$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)))
84
{
85
        if ($user->data['user_id'] != ANONYMOUS)
86
        {
87
                trigger_error('SORRY_AUTH_READ');
88
        }
89
90
        login_box('', $user->lang['LOGIN_VIEWFORUM']);
91
}
92
93
// Forum is passworded ... check whether access has been granted to this
94
// user this session, if not show login box
95
if ($forum_data['forum_password'])
96
{
97
        login_forum_box($forum_data);
98
}
99
100
// Is this forum a link? ... User got here either because the
101
// number of clicks is being tracked or they guessed the id
102
if ($forum_data['forum_type'] == FORUM_LINK && $forum_data['forum_link'])
103
{
104
        // Does it have click tracking enabled?
105
        if ($forum_data['forum_flags'] & FORUM_FLAG_LINK_TRACK)
106
        {
107
                $sql = 'UPDATE ' . FORUMS_TABLE . '
108
                        SET forum_posts = forum_posts + 1
109
                        WHERE forum_id = ' . $forum_id;
110
                $db->sql_query($sql);
111
        }
112
113
        // We redirect to the url. The third parameter indicates that external redirects are allowed.
114
        redirect($forum_data['forum_link'], false, true);
115
        return;
116
}
117
118
// Build navigation links
119
generate_forum_nav($forum_data);
120
121
// Forum Rules
122
if ($auth->acl_get('f_read', $forum_id))
123
{
124
        generate_forum_rules($forum_data);
125
}
126
127
// Do we have subforums?
128
$active_forum_ary = $moderators = array();
129
130
if ($forum_data['left_id'] != $forum_data['right_id'] - 1)
131
{
132
        list($active_forum_ary, $moderators) = display_forums($forum_data, $config['load_moderators'], $config['load_moderators']);
133
}
134
else
135
{
136
        $template->assign_var('S_HAS_SUBFORUM', false);
137
        if ($config['load_moderators'])
138
        {
139
                get_moderators($moderators, $forum_id);
140
        }
141
}
142
143
// Dump out the page header and load viewforum template
144
page_header($forum_data['forum_name'] . ($start ? ' - ' . sprintf($user->lang['PAGE_TITLE_NUMBER'], floor($start / $config['topics_per_page']) + 1) : ''), true, $forum_id);
145
146
$template->set_filenames(array(
147
        'body' => 'viewforum_body.html')
148
);
149
150
make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"), $forum_id);
151
152
$template->assign_vars(array(
153
        'U_VIEW_FORUM'                        => append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id" . (($start == 0) ? '' : "&amp;start=$start")),
154
));
155
156
// Not postable forum or showing active topics?
157
if (!($forum_data['forum_type'] == FORUM_POST || (($forum_data['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS) && $forum_data['forum_type'] == FORUM_CAT)))
158
{
159
        page_footer();
160
}
161
162
// Ok, if someone has only list-access, we only display the forum list.
163
// We also make this circumstance available to the template in case we want to display a notice. ;)
164
if (!$auth->acl_get('f_read', $forum_id))
165
{
166
        $template->assign_vars(array(
167
                'S_NO_READ_ACCESS'                => true,
168
        ));
169
170
        page_footer();
171
}
172
173
// Handle marking posts
174
if ($mark_read == 'topics')
175
{
176
        $token = request_var('hash', '');
177
        if (check_link_hash($token, 'global'))
178
        {
179
                markread('topics', array($forum_id));
180
        }
181
        $redirect_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id);
182
        meta_refresh(3, $redirect_url);
183
184
        trigger_error($user->lang['TOPICS_MARKED'] . '<br /><br />' . sprintf($user->lang['RETURN_FORUM'], '<a href="' . $redirect_url . '">', '</a>'));
185
}
186
187
// Is a forum specific topic count required?
188
if ($forum_data['forum_topics_per_page'])
189
{
190
        $config['topics_per_page'] = $forum_data['forum_topics_per_page'];
191
}
192
193
// Do the forum Prune thang - cron type job ...
194
if (!$config['use_system_cron'])
195
{
196
        $task = $cron->instantiate_task('cron_task_core_prune_forum', $forum_data);
197
        if ($task && $task->is_ready())
198
        {
199
                $url = $task->get_url();
200
                $template->assign_var('RUN_CRON_TASK', '<img src="' . $url . '" width="1" height="1" alt="cron" />');
201
        }
202
}
203
204
// Forum rules and subscription info
205
$s_watching_forum = array(
206
        'link'                        => '',
207
        'title'                        => '',
208
        'is_watching'        => false,
209
);
210
211
if (($config['email_enable'] || $config['jab_enable']) && $config['allow_forum_notify'] && $forum_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_subscribe', $forum_id) || $user->data['user_id'] == ANONYMOUS))
212
{
213
        $notify_status = (isset($forum_data['notify_status'])) ? $forum_data['notify_status'] : NULL;
214
        watch_topic_forum('forum', $s_watching_forum, $user->data['user_id'], $forum_id, 0, $notify_status, $start, $forum_data['forum_name']);
215
}
216
217
$s_forum_rules = '';
218
gen_forum_auth_level('forum', $forum_id, $forum_data['forum_status']);
219
220
// Topic ordering options
221
$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']);
222
223
$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']);
224
$sort_by_sql = array('a' => 't.topic_first_poster_name', 't' => 't.topic_last_post_time', 'r' => 't.topic_replies', 's' => 't.topic_title', 'v' => 't.topic_views');
225
226
$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
227
gen_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);
228
229
// Limit topics to certain time frame, obtain correct topic count
230
// global announcements must not be counted, normal announcements have to
231
// be counted, as forum_topics(_real) includes them
232
if ($sort_days)
233
{
234
        $min_post_time = time() - ($sort_days * 86400);
235
236
        $sql = 'SELECT COUNT(topic_id) AS num_topics
237
                FROM ' . TOPICS_TABLE . "
238
                WHERE forum_id = $forum_id
239
                        AND (topic_last_post_time >= $min_post_time
240
                                OR topic_type = " . POST_ANNOUNCE . '
241
                                OR topic_type = ' . POST_GLOBAL . ')
242
                ' . (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND topic_approved = 1');
243
        $result = $db->sql_query($sql);
244
        $topics_count = (int) $db->sql_fetchfield('num_topics');
245
        $db->sql_freeresult($result);
246
247
        if (isset($_POST['sort']))
248
        {
249
                $start = 0;
250
        }
251
        $sql_limit_time = "AND t.topic_last_post_time >= $min_post_time";
252
253
        // Make sure we have information about day selection ready
254
        $template->assign_var('S_SORT_DAYS', true);
255
}
256
else
257
{
258
        $topics_count = ($auth->acl_get('m_approve', $forum_id)) ? $forum_data['forum_topics_real'] : $forum_data['forum_topics'];
259
        $sql_limit_time = '';
260
}
261
262
// Make sure $start is set to the last page if it exceeds the amount
263
if ($start < 0 || $start > $topics_count)
264
{
265
        $start = ($start < 0) ? 0 : floor(($topics_count - 1) / $config['topics_per_page']) * $config['topics_per_page'];
266
}
267
268
// Basic pagewide vars
269
$post_alt = ($forum_data['forum_status'] == ITEM_LOCKED) ? $user->lang['FORUM_LOCKED'] : $user->lang['POST_NEW_TOPIC'];
270
271
// Display active topics?
272
$s_display_active = ($forum_data['forum_type'] == FORUM_CAT && ($forum_data['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS)) ? true : false;
273
274
$s_search_hidden_fields = array('fid' => array($forum_id));
275
if ($_SID)
276
{
277
        $s_search_hidden_fields['sid'] = $_SID;
278
}
279
280
if (!empty($_EXTRA_URL))
281
{
282
        foreach ($_EXTRA_URL as $url_param)
283
        {
284
                $url_param = explode('=', $url_param, 2);
285
                $s_search_hidden_fields[$url_param[0]] = $url_param[1];
286
        }
287
}
288
289
$template->assign_vars(array(
290
        'MODERATORS'        => (!empty($moderators[$forum_id])) ? implode(', ', $moderators[$forum_id]) : '',
291
292
        'POST_IMG'                                        => ($forum_data['forum_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', $post_alt) : $user->img('button_topic_new', $post_alt),
293
        'NEWEST_POST_IMG'                        => $user->img('icon_topic_newest', 'VIEW_NEWEST_POST'),
294
        'LAST_POST_IMG'                                => $user->img('icon_topic_latest', 'VIEW_LATEST_POST'),
295
        'FOLDER_IMG'                                => $user->img('topic_read', 'NO_UNREAD_POSTS'),
296
        'FOLDER_UNREAD_IMG'                        => $user->img('topic_unread', 'UNREAD_POSTS'),
297
        'FOLDER_HOT_IMG'                        => $user->img('topic_read_hot', 'NO_UNREAD_POSTS_HOT'),
298
        'FOLDER_HOT_UNREAD_IMG'                => $user->img('topic_unread_hot', 'UNREAD_POSTS_HOT'),
299
        'FOLDER_LOCKED_IMG'                        => $user->img('topic_read_locked', 'NO_UNREAD_POSTS_LOCKED'),
300
        'FOLDER_LOCKED_UNREAD_IMG'        => $user->img('topic_unread_locked', 'UNREAD_POSTS_LOCKED'),
301
        'FOLDER_STICKY_IMG'                        => $user->img('sticky_read', 'POST_STICKY'),
302
        'FOLDER_STICKY_UNREAD_IMG'        => $user->img('sticky_unread', 'POST_STICKY'),
303
        'FOLDER_ANNOUNCE_IMG'                => $user->img('announce_read', 'POST_ANNOUNCEMENT'),
304
        'FOLDER_ANNOUNCE_UNREAD_IMG'=> $user->img('announce_unread', 'POST_ANNOUNCEMENT'),
305
        'FOLDER_MOVED_IMG'                        => $user->img('topic_moved', 'TOPIC_MOVED'),
306
        'REPORTED_IMG'                                => $user->img('icon_topic_reported', 'TOPIC_REPORTED'),
307
        'UNAPPROVED_IMG'                        => $user->img('icon_topic_unapproved', 'TOPIC_UNAPPROVED'),
308
        'GOTO_PAGE_IMG'                                => $user->img('icon_post_target', 'GOTO_PAGE'),
309
310
        'L_NO_TOPICS'                         => ($forum_data['forum_status'] == ITEM_LOCKED) ? $user->lang['POST_FORUM_LOCKED'] : $user->lang['NO_TOPICS'],
311
312
        'S_DISPLAY_POST_INFO'        => ($forum_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_post', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
313
314
        'S_IS_POSTABLE'                        => ($forum_data['forum_type'] == FORUM_POST) ? true : false,
315
        'S_USER_CAN_POST'                => ($auth->acl_get('f_post', $forum_id)) ? true : false,
316
        'S_DISPLAY_ACTIVE'                => $s_display_active,
317
        'S_SELECT_SORT_DIR'                => $s_sort_dir,
318
        'S_SELECT_SORT_KEY'                => $s_sort_key,
319
        'S_SELECT_SORT_DAYS'        => $s_limit_days,
320
        'S_TOPIC_ICONS'                        => ($s_display_active && sizeof($active_forum_ary)) ? max($active_forum_ary['enable_icons']) : (($forum_data['enable_icons']) ? true : false),
321
        'S_WATCH_FORUM_LINK'        => $s_watching_forum['link'],
322
        'S_WATCH_FORUM_TITLE'        => $s_watching_forum['title'],
323
        'S_WATCHING_FORUM'                => $s_watching_forum['is_watching'],
324
        'S_FORUM_ACTION'                => append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id" . (($start == 0) ? '' : "&amp;start=$start")),
325
        'S_DISPLAY_SEARCHBOX'        => ($auth->acl_get('u_search') && $auth->acl_get('f_search', $forum_id) && $config['load_search']) ? true : false,
326
        'S_SEARCHBOX_ACTION'        => append_sid("{$phpbb_root_path}search.$phpEx"),
327
        'S_SEARCH_LOCAL_HIDDEN_FIELDS'        => build_hidden_fields($s_search_hidden_fields),
328
        'S_SINGLE_MODERATOR'        => (!empty($moderators[$forum_id]) && sizeof($moderators[$forum_id]) > 1) ? false : true,
329
        'S_IS_LOCKED'                        => ($forum_data['forum_status'] == ITEM_LOCKED) ? true : false,
330
        'S_VIEWFORUM'                        => true,
331
332
        '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) : '',
333
        '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) : '',
334
        '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")),
335
        '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") : '',
336
));
337
338
// Grab icons
339
$icons = $cache->obtain_icons();
340
341
// Grab all topic data
342
$rowset = $announcement_list = $topic_list = $global_announce_forums = array();
343
344
$sql_array = array(
345
        'SELECT'        => 't.*',
346
        'FROM'                => array(
347
                TOPICS_TABLE                => 't'
348
        ),
349
        'LEFT_JOIN'        => array(),
350
);
351
352
$sql_approved = ($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND t.topic_approved = 1';
353
354
if ($user->data['is_registered'])
355
{
356
        if ($config['load_db_track'])
357
        {
358
                $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']);
359
                $sql_array['SELECT'] .= ', tp.topic_posted';
360
        }
361
362
        if ($config['load_db_lastread'])
363
        {
364
                $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']);
365
                $sql_array['SELECT'] .= ', tt.mark_time';
366
367
                if ($s_display_active && sizeof($active_forum_ary))
368
                {
369
                        $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']);
370
                        $sql_array['SELECT'] .= ', ft.mark_time AS forum_mark_time';
371
                }
372
        }
373
}
374
375
if ($forum_data['forum_type'] == FORUM_POST)
376
{
377
        // Get global announcement forums
378
        $g_forum_ary = $auth->acl_getf('f_read', true);
379
        $g_forum_ary = array_unique(array_keys($g_forum_ary));
380
381
        $sql_anounce_array['LEFT_JOIN'] = $sql_array['LEFT_JOIN'];
382
        $sql_anounce_array['LEFT_JOIN'][] = array('FROM' => array(FORUMS_TABLE => 'f'), 'ON' => 'f.forum_id = t.forum_id');
383
        $sql_anounce_array['SELECT'] = $sql_array['SELECT'] . ', f.forum_name';
384
385
        // Obtain announcements ... removed sort ordering, sort by time in all cases
386
        $sql_ary = array(
387
                'SELECT'        => $sql_anounce_array['SELECT'],
388
                'FROM'                => $sql_array['FROM'],
389
                'LEFT_JOIN'        => $sql_anounce_array['LEFT_JOIN'],
390
391
                'WHERE'                => '(t.forum_id = ' . $forum_id . '
392
                                AND t.topic_type = ' . POST_ANNOUNCE . ') OR
393
                        (' . $db->sql_in_set('t.forum_id', $g_forum_ary) . '
394
                                AND t.topic_type = ' . POST_GLOBAL . ')',
395
396
                'ORDER_BY'        => 't.topic_time DESC',
397
        );
398
        $sql = $db->sql_build_query('SELECT', $sql_ary);
399
        $result = $db->sql_query($sql);
400
401
        while ($row = $db->sql_fetchrow($result))
402
        {
403
                if (!$row['topic_approved'] && !$auth->acl_get('m_approve', $row['forum_id']))
404
                {
405
                        // Do not display announcements that are waiting for approval.
406
                        continue;
407
                }
408
409
                $rowset[$row['topic_id']] = $row;
410
                $announcement_list[] = $row['topic_id'];
411
412
                if ($forum_id != $row['forum_id'])
413
                {
414
                        $topics_count++;
415
                        $global_announce_forums[] = $row['forum_id'];
416
                }
417
        }
418
        $db->sql_freeresult($result);
419
}
420
421
$forum_tracking_info = array();
422
423
if ($user->data['is_registered'])
424
{
425
        $forum_tracking_info[$forum_id] = $forum_data['mark_time'];
426
427
        if (!empty($global_announce_forums) && $config['load_db_lastread'])
428
        {
429
                $sql = 'SELECT forum_id, mark_time
430
                        FROM ' . FORUMS_TRACK_TABLE . '
431
                        WHERE ' . $db->sql_in_set('forum_id', $global_announce_forums) . '
432
                                AND user_id = ' . $user->data['user_id'];
433
                $result = $db->sql_query($sql);
434
435
                while ($row = $db->sql_fetchrow($result))
436
                {
437
                        $forum_tracking_info[$row['forum_id']] = $row['mark_time'];
438
                }
439
                $db->sql_freeresult($result);
440
        }
441
}
442
443
// If the user is trying to reach late pages, start searching from the end
444
$store_reverse = false;
445
$sql_limit = $config['topics_per_page'];
446
if ($start > $topics_count / 2)
447
{
448
        $store_reverse = true;
449
450
        if ($start + $config['topics_per_page'] > $topics_count)
451
        {
452
                $sql_limit = min($config['topics_per_page'], max(1, $topics_count - $start));
453
        }
454
455
        // Select the sort order
456
        $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'ASC' : 'DESC');
457
        $sql_start = max(0, $topics_count - $sql_limit - $start);
458
}
459
else
460
{
461
        // Select the sort order
462
        $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
463
        $sql_start = $start;
464
}
465
466
if ($forum_data['forum_type'] == FORUM_POST || !sizeof($active_forum_ary))
467
{
468
        $sql_where = 't.forum_id = ' . $forum_id;
469
}
470
else if (empty($active_forum_ary['exclude_forum_id']))
471
{
472
        $sql_where = $db->sql_in_set('t.forum_id', $active_forum_ary['forum_id']);
473
}
474
else
475
{
476
        $get_forum_ids = array_diff($active_forum_ary['forum_id'], $active_forum_ary['exclude_forum_id']);
477
        $sql_where = (sizeof($get_forum_ids)) ? $db->sql_in_set('t.forum_id', $get_forum_ids) : 't.forum_id = ' . $forum_id;
478
}
479
480
// Grab just the sorted topic ids
481
$sql = 'SELECT t.topic_id
482
        FROM ' . TOPICS_TABLE . " t
483
        WHERE $sql_where
484
                AND t.topic_type IN (" . POST_NORMAL . ', ' . POST_STICKY . ")
485
                $sql_approved
486
                $sql_limit_time
487
        ORDER BY t.topic_type " . ((!$store_reverse) ? 'DESC' : 'ASC') . ', ' . $sql_sort_order;
488
$result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
489
490
while ($row = $db->sql_fetchrow($result))
491
{
492
        $topic_list[] = (int) $row['topic_id'];
493
}
494
$db->sql_freeresult($result);
495
496
// For storing shadow topics
497
$shadow_topic_list = array();
498
499
if (sizeof($topic_list))
500
{
501
        // SQL array for obtaining topics/stickies
502
        $sql_array = array(
503
                'SELECT'                => $sql_array['SELECT'],
504
                'FROM'                        => $sql_array['FROM'],
505
                'LEFT_JOIN'                => $sql_array['LEFT_JOIN'],
506
507
                'WHERE'                        => $db->sql_in_set('t.topic_id', $topic_list),
508
        );
509
510
        // If store_reverse, then first obtain topics, then stickies, else the other way around...
511
        // Funnily enough you typically save one query if going from the last page to the middle (store_reverse) because
512
        // the number of stickies are not known
513
        $sql = $db->sql_build_query('SELECT', $sql_array);
514
        $result = $db->sql_query($sql);
515
516
        while ($row = $db->sql_fetchrow($result))
517
        {
518
                if ($row['topic_status'] == ITEM_MOVED)
519
                {
520
                        $shadow_topic_list[$row['topic_moved_id']] = $row['topic_id'];
521
                }
522
523
                $rowset[$row['topic_id']] = $row;
524
        }
525
        $db->sql_freeresult($result);
526
}
527
528
// If we have some shadow topics, update the rowset to reflect their topic information
529
if (sizeof($shadow_topic_list))
530
{
531
        $sql = 'SELECT *
532
                FROM ' . TOPICS_TABLE . '
533
                WHERE ' . $db->sql_in_set('topic_id', array_keys($shadow_topic_list));
534
        $result = $db->sql_query($sql);
535
536
        while ($row = $db->sql_fetchrow($result))
537
        {
538
                $orig_topic_id = $shadow_topic_list[$row['topic_id']];
539
540
                // If the shadow topic is already listed within the rowset (happens for active topics for example), then do not include it...
541
                if (isset($rowset[$row['topic_id']]))
542
                {
543
                        // We need to remove any trace regarding this topic. :)
544
                        unset($rowset[$orig_topic_id]);
545
                        unset($topic_list[array_search($orig_topic_id, $topic_list)]);
546
                        $topics_count--;
547
548
                        continue;
549
                }
550
551
                // Do not include those topics the user has no permission to access
552
                if (!$auth->acl_get('f_read', $row['forum_id']))
553
                {
554
                        // We need to remove any trace regarding this topic. :)
555
                        unset($rowset[$orig_topic_id]);
556
                        unset($topic_list[array_search($orig_topic_id, $topic_list)]);
557
                        $topics_count--;
558
559
                        continue;
560
                }
561
562
                // We want to retain some values
563
                $row = array_merge($row, array(
564
                        'topic_moved_id'        => $rowset[$orig_topic_id]['topic_moved_id'],
565
                        'topic_status'                => $rowset[$orig_topic_id]['topic_status'],
566
                        'topic_type'                => $rowset[$orig_topic_id]['topic_type'],
567
                        'topic_title'                => $rowset[$orig_topic_id]['topic_title'],
568
                ));
569
570
                // Shadow topics are never reported
571
                $row['topic_reported'] = 0;
572
573
                $rowset[$orig_topic_id] = $row;
574
        }
575
        $db->sql_freeresult($result);
576
}
577
unset($shadow_topic_list);
578
579
// Ok, adjust topics count for active topics list
580
if ($s_display_active)
581
{
582
        $topics_count = 1;
583
}
584
585
// We need to remove the global announcements from the forums total topic count,
586
// otherwise the number is different from the one on the forum list
587
$total_topic_count = $topics_count - sizeof($global_announce_forums);
588
589
$template->assign_vars(array(
590
        'PAGINATION'        => generate_pagination(append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id" . ((strlen($u_sort_param)) ? "&amp;$u_sort_param" : '')), $topics_count, $config['topics_per_page'], $start),
591
        'PAGE_NUMBER'        => on_page($topics_count, $config['topics_per_page'], $start),
592
        'TOTAL_TOPICS'        => ($s_display_active) ? false : $user->lang('VIEW_FORUM_TOPICS', (int) $total_topic_count),
593
));
594
595
$topic_list = ($store_reverse) ? array_merge($announcement_list, array_reverse($topic_list)) : array_merge($announcement_list, $topic_list);
596
$topic_tracking_info = $tracking_topics = array();
597
598
// Okay, lets dump out the page ...
599
if (sizeof($topic_list))
600
{
601
        $mark_forum_read = true;
602
        $mark_time_forum = 0;
603
604
        // Generate topic forum list...
605
        $topic_forum_list = array();
606
        foreach ($rowset as $t_id => $row)
607
        {
608
                if (isset($forum_tracking_info[$row['forum_id']]))
609
                {
610
                        $row['forum_mark_time'] = $forum_tracking_info[$row['forum_id']];
611
                }
612
613
                $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;
614
                $topic_forum_list[$row['forum_id']]['topics'][] = (int) $t_id;
615
        }
616
617
        if ($config['load_db_lastread'] && $user->data['is_registered'])
618
        {
619
                foreach ($topic_forum_list as $f_id => $topic_row)
620
                {
621
                        $topic_tracking_info += get_topic_tracking($f_id, $topic_row['topics'], $rowset, array($f_id => $topic_row['forum_mark_time']));
622
                }
623
        }
624
        else if ($config['load_anon_lastread'] || $user->data['is_registered'])
625
        {
626
                foreach ($topic_forum_list as $f_id => $topic_row)
627
                {
628
                        $topic_tracking_info += get_complete_topic_tracking($f_id, $topic_row['topics']);
629
                }
630
        }
631
632
        unset($topic_forum_list);
633
634
        if (!$s_display_active)
635
        {
636
                if ($config['load_db_lastread'] && $user->data['is_registered'])
637
                {
638
                        $mark_time_forum = (!empty($forum_data['mark_time'])) ? $forum_data['mark_time'] : $user->data['user_lastmark'];
639
                }
640
                else if ($config['load_anon_lastread'] || $user->data['is_registered'])
641
                {
642
                        if (!$user->data['is_registered'])
643
                        {
644
                                $user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
645
                        }
646
                        $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'];
647
                }
648
        }
649
650
        $s_type_switch = 0;
651
        foreach ($topic_list as $topic_id)
652
        {
653
                $row = &$rowset[$topic_id];
654
655
                $topic_forum_id = ($row['forum_id']) ? (int) $row['forum_id'] : $forum_id;
656
657
                // This will allow the style designer to output a different header
658
                // or even separate the list of announcements from sticky and normal topics
659
                $s_type_switch_test = ($row['topic_type'] == POST_ANNOUNCE || $row['topic_type'] == POST_GLOBAL) ? 1 : 0;
660
661
                // Replies
662
                $replies = ($auth->acl_get('m_approve', $topic_forum_id)) ? $row['topic_replies_real'] : $row['topic_replies'];
663
664
                if ($row['topic_status'] == ITEM_MOVED)
665
                {
666
                        $topic_id = $row['topic_moved_id'];
667
                        $unread_topic = false;
668
                }
669
                else
670
                {
671
                        $unread_topic = (isset($topic_tracking_info[$topic_id]) && $row['topic_last_post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
672
                }
673
674
                // Get folder img, topic status/type related information
675
                $folder_img = $folder_alt = $topic_type = '';
676
                topic_status($row, $replies, $unread_topic, $folder_img, $folder_alt, $topic_type);
677
678
                // Generate all the URIs ...
679
                $view_topic_url_params = 'f=' . $row['forum_id'] . '&amp;t=' . $topic_id;
680
                $view_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params);
681
682
                $topic_unapproved = (!$row['topic_approved'] && $auth->acl_get('m_approve', $row['forum_id'])) ? true : false;
683
                $posts_unapproved = ($row['topic_approved'] && $row['topic_replies'] < $row['topic_replies_real'] && $auth->acl_get('m_approve', $row['forum_id'])) ? true : false;
684
                $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) : '';
685
686
                // Send vars to template
687
                $template->assign_block_vars('topicrow', array(
688
                        'FORUM_ID'                                        => $row['forum_id'],
689
                        'TOPIC_ID'                                        => $topic_id,
690
                        'TOPIC_AUTHOR'                                => get_username_string('username', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
691
                        'TOPIC_AUTHOR_COLOUR'                => get_username_string('colour', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
692
                        'TOPIC_AUTHOR_FULL'                        => get_username_string('full', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
693
                        'FIRST_POST_TIME'                        => $user->format_date($row['topic_time']),
694
                        'LAST_POST_SUBJECT'                        => censor_text($row['topic_last_post_subject']),
695
                        'LAST_POST_TIME'                        => $user->format_date($row['topic_last_post_time']),
696
                        'LAST_VIEW_TIME'                        => $user->format_date($row['topic_last_view_time']),
697
                        'LAST_POST_AUTHOR'                        => get_username_string('username', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
698
                        'LAST_POST_AUTHOR_COLOUR'        => get_username_string('colour', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
699
                        'LAST_POST_AUTHOR_FULL'                => get_username_string('full', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
700
701
                        'PAGINATION'                => topic_generate_pagination($replies, $view_topic_url),
702
                        'REPLIES'                        => $replies,
703
                        'VIEWS'                                => $row['topic_views'],
704
                        'TOPIC_TITLE'                => censor_text($row['topic_title']),
705
                        'TOPIC_TYPE'                => $topic_type,
706
                        'FORUM_NAME'                => (isset($row['forum_name'])) ? $row['forum_name'] : $forum_data['forum_name'],
707
708
                        'TOPIC_IMG_STYLE'                => $folder_img,
709
                        'TOPIC_FOLDER_IMG'                => $user->img($folder_img, $folder_alt),
710
                        'TOPIC_FOLDER_IMG_ALT'        => $user->lang[$folder_alt],
711
712
                        'TOPIC_ICON_IMG'                => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['img'] : '',
713
                        'TOPIC_ICON_IMG_WIDTH'        => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['width'] : '',
714
                        'TOPIC_ICON_IMG_HEIGHT'        => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['height'] : '',
715
                        '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']) : '',
716
                        'UNAPPROVED_IMG'                => ($topic_unapproved || $posts_unapproved) ? $user->img('icon_topic_unapproved', ($topic_unapproved) ? 'TOPIC_UNAPPROVED' : 'POSTS_UNAPPROVED') : '',
717
718
                        'S_TOPIC_TYPE'                        => $row['topic_type'],
719
                        'S_USER_POSTED'                        => (isset($row['topic_posted']) && $row['topic_posted']) ? true : false,
720
                        'S_UNREAD_TOPIC'                => $unread_topic,
721
                        'S_TOPIC_REPORTED'                => (!empty($row['topic_reported']) && $auth->acl_get('m_report', $row['forum_id'])) ? true : false,
722
                        'S_TOPIC_UNAPPROVED'        => $topic_unapproved,
723
                        'S_POSTS_UNAPPROVED'        => $posts_unapproved,
724
                        'S_HAS_POLL'                        => ($row['poll_start']) ? true : false,
725
                        'S_POST_ANNOUNCE'                => ($row['topic_type'] == POST_ANNOUNCE) ? true : false,
726
                        'S_POST_GLOBAL'                        => ($row['topic_type'] == POST_GLOBAL) ? true : false,
727
                        'S_POST_STICKY'                        => ($row['topic_type'] == POST_STICKY) ? true : false,
728
                        'S_TOPIC_LOCKED'                => ($row['topic_status'] == ITEM_LOCKED) ? true : false,
729
                        'S_TOPIC_MOVED'                        => ($row['topic_status'] == ITEM_MOVED) ? true : false,
730
731
                        'U_NEWEST_POST'                        => append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params . '&amp;view=unread') . '#unread',
732
                        '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'],
733
                        'U_LAST_POST_AUTHOR'        => get_username_string('profile', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
734
                        'U_TOPIC_AUTHOR'                => get_username_string('profile', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
735
                        'U_VIEW_TOPIC'                        => $view_topic_url,
736
                        'U_VIEW_FORUM'                        => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']),
737
                        '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),
738
                        'U_MCP_QUEUE'                        => $u_mcp_queue,
739
740
                        'S_TOPIC_TYPE_SWITCH'        => ($s_type_switch == $s_type_switch_test) ? -1 : $s_type_switch_test)
741
                );
742
743
                $s_type_switch = ($row['topic_type'] == POST_ANNOUNCE || $row['topic_type'] == POST_GLOBAL) ? 1 : 0;
744
745
                if ($unread_topic)
746
                {
747
                        $mark_forum_read = false;
748
                }
749
750
                unset($rowset[$topic_id]);
751
        }
752
}
753
754
// This is rather a fudge but it's the best I can think of without requiring information
755
// on all topics (as we do in 2.0.x). It looks for unread or new topics, if it doesn't find
756
// any it updates the forum last read cookie. This requires that the user visit the forum
757
// after reading a topic
758
if ($forum_data['forum_type'] == FORUM_POST && sizeof($topic_list) && $mark_forum_read)
759
{
760
        update_forum_tracking_info($forum_id, $forum_data['forum_last_post_time'], false, $mark_time_forum);
761
}
762
763
page_footer();