phpBB
Statistics
| Revision:

root / trunk / phpBB / viewtopic.php

History | View | Annotate | Download (65.2 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
include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
19
20
// Start session management
21
$user->session_begin();
22
$auth->acl($user->data);
23
24
// Initial var setup
25
$forum_id        = request_var('f', 0);
26
$topic_id        = request_var('t', 0);
27
$post_id        = request_var('p', 0);
28
$voted_id        = request_var('vote_id', array('' => 0));
29
30
$voted_id = (sizeof($voted_id) > 1) ? array_unique($voted_id) : $voted_id;
31
32
33
$start                = request_var('start', 0);
34
$view                = request_var('view', '');
35
36
$default_sort_days        = (!empty($user->data['user_post_show_days'])) ? $user->data['user_post_show_days'] : 0;
37
$default_sort_key        = (!empty($user->data['user_post_sortby_type'])) ? $user->data['user_post_sortby_type'] : 't';
38
$default_sort_dir        = (!empty($user->data['user_post_sortby_dir'])) ? $user->data['user_post_sortby_dir'] : 'a';
39
40
$sort_days        = request_var('st', $default_sort_days);
41
$sort_key        = request_var('sk', $default_sort_key);
42
$sort_dir        = request_var('sd', $default_sort_dir);
43
44
$update                = request_var('update', false);
45
46
$s_can_vote = false;
47
/**
48
* @todo normalize?
49
*/
50
$hilit_words        = request_var('hilit', '', true);
51
52
// Do we have a topic or post id?
53
if (!$topic_id && !$post_id)
54
{
55
        trigger_error('NO_TOPIC');
56
}
57
58
// Find topic id if user requested a newer or older topic
59
if ($view && !$post_id)
60
{
61
        if (!$forum_id)
62
        {
63
                $sql = 'SELECT forum_id
64
                        FROM ' . TOPICS_TABLE . "
65
                        WHERE topic_id = $topic_id";
66
                $result = $db->sql_query($sql);
67
                $forum_id = (int) $db->sql_fetchfield('forum_id');
68
                $db->sql_freeresult($result);
69
70
                if (!$forum_id)
71
                {
72
                        trigger_error('NO_TOPIC');
73
                }
74
        }
75
76
        if ($view == 'unread')
77
        {
78
                // Get topic tracking info
79
                $topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
80
81
                $topic_last_read = (isset($topic_tracking_info[$topic_id])) ? $topic_tracking_info[$topic_id] : 0;
82
83
                $sql = 'SELECT post_id, topic_id, forum_id
84
                        FROM ' . POSTS_TABLE . "
85
                        WHERE topic_id = $topic_id
86
                                " . (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND post_approved = 1') . "
87
                                AND post_time > $topic_last_read
88
                                AND forum_id = $forum_id
89
                        ORDER BY post_time ASC";
90
                $result = $db->sql_query_limit($sql, 1);
91
                $row = $db->sql_fetchrow($result);
92
                $db->sql_freeresult($result);
93
94
                if (!$row)
95
                {
96
                        $sql = 'SELECT topic_last_post_id as post_id, topic_id, forum_id
97
                                FROM ' . TOPICS_TABLE . '
98
                                WHERE topic_id = ' . $topic_id;
99
                        $result = $db->sql_query($sql);
100
                        $row = $db->sql_fetchrow($result);
101
                        $db->sql_freeresult($result);
102
                }
103
104
                if (!$row)
105
                {
106
                        // Setup user environment so we can process lang string
107
                        $user->setup('viewtopic');
108
109
                        trigger_error('NO_TOPIC');
110
                }
111
112
                $post_id = $row['post_id'];
113
                $topic_id = $row['topic_id'];
114
        }
115
        else if ($view == 'next' || $view == 'previous')
116
        {
117
                $sql_condition = ($view == 'next') ? '>' : '<';
118
                $sql_ordering = ($view == 'next') ? 'ASC' : 'DESC';
119
120
                $sql = 'SELECT forum_id, topic_last_post_time
121
                        FROM ' . TOPICS_TABLE . '
122
                        WHERE topic_id = ' . $topic_id;
123
                $result = $db->sql_query($sql);
124
                $row = $db->sql_fetchrow($result);
125
                $db->sql_freeresult($result);
126
127
                if (!$row)
128
                {
129
                        $user->setup('viewtopic');
130
                        // OK, the topic doesn't exist. This error message is not helpful, but technically correct.
131
                        trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
132
                }
133
                else
134
                {
135
                        $sql = 'SELECT topic_id, forum_id
136
                                FROM ' . TOPICS_TABLE . '
137
                                WHERE forum_id = ' . $row['forum_id'] . "
138
                                        AND topic_moved_id = 0
139
                                        AND topic_last_post_time $sql_condition {$row['topic_last_post_time']}
140
                                        " . (($auth->acl_get('m_approve', $row['forum_id'])) ? '' : 'AND topic_approved = 1') . "
141
                                ORDER BY topic_last_post_time $sql_ordering";
142
                        $result = $db->sql_query_limit($sql, 1);
143
                        $row = $db->sql_fetchrow($result);
144
                        $db->sql_freeresult($result);
145
146
                        if (!$row)
147
                        {
148
                                $user->setup('viewtopic');
149
                                trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
150
                        }
151
                        else
152
                        {
153
                                $topic_id = $row['topic_id'];
154
                                $forum_id = $row['forum_id'];
155
                        }
156
                }
157
        }
158
159
        if (isset($row) && $row['forum_id'])
160
        {
161
                $forum_id = $row['forum_id'];
162
        }
163
}
164
165
// This rather complex gaggle of code handles querying for topics but
166
// also allows for direct linking to a post (and the calculation of which
167
// page the post is on and the correct display of viewtopic)
168
$sql_array = array(
169
        'SELECT'        => 't.*, f.*',
170
171
        'FROM'                => array(FORUMS_TABLE => 'f'),
172
);
173
174
// The FROM-Order is quite important here, else t.* columns can not be correctly bound.
175
if ($post_id)
176
{
177
        $sql_array['SELECT'] .= ', p.post_approved, p.post_time, p.post_id';
178
        $sql_array['FROM'][POSTS_TABLE] = 'p';
179
}
180
181
// Topics table need to be the last in the chain
182
$sql_array['FROM'][TOPICS_TABLE] = 't';
183
184
if ($user->data['is_registered'])
185
{
186
        $sql_array['SELECT'] .= ', tw.notify_status';
187
        $sql_array['LEFT_JOIN'] = array();
188
189
        $sql_array['LEFT_JOIN'][] = array(
190
                'FROM'        => array(TOPICS_WATCH_TABLE => 'tw'),
191
                'ON'        => 'tw.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tw.topic_id'
192
        );
193
194
        if ($config['allow_bookmarks'])
195
        {
196
                $sql_array['SELECT'] .= ', bm.topic_id as bookmarked';
197
                $sql_array['LEFT_JOIN'][] = array(
198
                        'FROM'        => array(BOOKMARKS_TABLE => 'bm'),
199
                        'ON'        => 'bm.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = bm.topic_id'
200
                );
201
        }
202
203
        if ($config['load_db_lastread'])
204
        {
205
                $sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
206
207
                $sql_array['LEFT_JOIN'][] = array(
208
                        'FROM'        => array(TOPICS_TRACK_TABLE => 'tt'),
209
                        'ON'        => 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
210
                );
211
212
                $sql_array['LEFT_JOIN'][] = array(
213
                        'FROM'        => array(FORUMS_TRACK_TABLE => 'ft'),
214
                        'ON'        => 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
215
                );
216
        }
217
}
218
219
if (!$post_id)
220
{
221
        $sql_array['WHERE'] = "t.topic_id = $topic_id";
222
}
223
else
224
{
225
        $sql_array['WHERE'] = "p.post_id = $post_id AND t.topic_id = p.topic_id";
226
}
227
228
$sql_array['WHERE'] .= ' AND f.forum_id = t.forum_id';
229
230
$sql = $db->sql_build_query('SELECT', $sql_array);
231
$result = $db->sql_query($sql);
232
$topic_data = $db->sql_fetchrow($result);
233
$db->sql_freeresult($result);
234
235
// link to unapproved post or incorrect link
236
if (!$topic_data)
237
{
238
        // If post_id was submitted, we try at least to display the topic as a last resort...
239
        if ($post_id && $topic_id)
240
        {
241
                redirect(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t=$topic_id" . (($forum_id) ? "&amp;f=$forum_id" : '')));
242
        }
243
244
        trigger_error('NO_TOPIC');
245
}
246
247
$forum_id = (int) $topic_data['forum_id'];
248
// This is for determining where we are (page)
249
if ($post_id)
250
{
251
        // are we where we are supposed to be?
252
        if (!$topic_data['post_approved'] && !$auth->acl_get('m_approve', $topic_data['forum_id']))
253
        {
254
                // If post_id was submitted, we try at least to display the topic as a last resort...
255
                if ($topic_id)
256
                {
257
                        redirect(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t=$topic_id" . (($forum_id) ? "&amp;f=$forum_id" : '')));
258
                }
259
260
                trigger_error('NO_TOPIC');
261
        }
262
        if ($post_id == $topic_data['topic_first_post_id'] || $post_id == $topic_data['topic_last_post_id'])
263
        {
264
                $check_sort = ($post_id == $topic_data['topic_first_post_id']) ? 'd' : 'a';
265
266
                if ($sort_dir == $check_sort)
267
                {
268
                        $topic_data['prev_posts'] = ($auth->acl_get('m_approve', $forum_id)) ? $topic_data['topic_replies_real'] : $topic_data['topic_replies'];
269
                }
270
                else
271
                {
272
                        $topic_data['prev_posts'] = 0;
273
                }
274
        }
275
        else
276
        {
277
                $sql = 'SELECT COUNT(p.post_id) AS prev_posts
278
                        FROM ' . POSTS_TABLE . " p
279
                        WHERE p.topic_id = {$topic_data['topic_id']}
280
                                " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '');
281
282
                if ($sort_dir == 'd')
283
                {
284
                        $sql .= " AND (p.post_time > {$topic_data['post_time']} OR (p.post_time = {$topic_data['post_time']} AND p.post_id >= {$topic_data['post_id']}))";
285
                }
286
                else
287
                {
288
                        $sql .= " AND (p.post_time < {$topic_data['post_time']} OR (p.post_time = {$topic_data['post_time']} AND p.post_id <= {$topic_data['post_id']}))";
289
                }
290
291
                $result = $db->sql_query($sql);
292
                $row = $db->sql_fetchrow($result);
293
                $db->sql_freeresult($result);
294
295
                $topic_data['prev_posts'] = $row['prev_posts'] - 1;
296
        }
297
}
298
299
$topic_id = (int) $topic_data['topic_id'];
300
//
301
$topic_replies = ($auth->acl_get('m_approve', $forum_id)) ? $topic_data['topic_replies_real'] : $topic_data['topic_replies'];
302
303
// Check sticky/announcement time limit
304
if (($topic_data['topic_type'] == POST_STICKY || $topic_data['topic_type'] == POST_ANNOUNCE) && $topic_data['topic_time_limit'] && ($topic_data['topic_time'] + $topic_data['topic_time_limit']) < time())
305
{
306
        $sql = 'UPDATE ' . TOPICS_TABLE . '
307
                SET topic_type = ' . POST_NORMAL . ', topic_time_limit = 0
308
                WHERE topic_id = ' . $topic_id;
309
        $db->sql_query($sql);
310
311
        $topic_data['topic_type'] = POST_NORMAL;
312
        $topic_data['topic_time_limit'] = 0;
313
}
314
315
// Setup look and feel
316
$user->setup('viewtopic', $topic_data['forum_style']);
317
318
if (!$topic_data['topic_approved'] && !$auth->acl_get('m_approve', $forum_id))
319
{
320
        trigger_error('NO_TOPIC');
321
}
322
323
// Start auth check
324
if (!$auth->acl_get('f_read', $forum_id))
325
{
326
        if ($user->data['user_id'] != ANONYMOUS)
327
        {
328
                trigger_error('SORRY_AUTH_READ');
329
        }
330
331
        login_box('', $user->lang['LOGIN_VIEWFORUM']);
332
}
333
334
// Forum is passworded ... check whether access has been granted to this
335
// user this session, if not show login box
336
if ($topic_data['forum_password'])
337
{
338
        login_forum_box($topic_data);
339
}
340
341
// Redirect to login or to the correct post upon emailed notification links
342
if (isset($_GET['e']))
343
{
344
        $jump_to = request_var('e', 0);
345
346
        $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id");
347
348
        if ($user->data['user_id'] == ANONYMOUS)
349
        {
350
                login_box($redirect_url . "&amp;p=$post_id&amp;e=$jump_to", $user->lang['LOGIN_NOTIFY_TOPIC']);
351
        }
352
353
        if ($jump_to > 0)
354
        {
355
                // We direct the already logged in user to the correct post...
356
                redirect($redirect_url . ((!$post_id) ? "&amp;p=$jump_to" : "&amp;p=$post_id") . "#p$jump_to");
357
        }
358
}
359
360
// What is start equal to?
361
if ($post_id)
362
{
363
        $start = floor(($topic_data['prev_posts']) / $config['posts_per_page']) * $config['posts_per_page'];
364
}
365
366
// Get topic tracking info
367
if (!isset($topic_tracking_info))
368
{
369
        $topic_tracking_info = array();
370
371
        // Get topic tracking info
372
        if ($config['load_db_lastread'] && $user->data['is_registered'])
373
        {
374
                $tmp_topic_data = array($topic_id => $topic_data);
375
                $topic_tracking_info = get_topic_tracking($forum_id, $topic_id, $tmp_topic_data, array($forum_id => $topic_data['forum_mark_time']));
376
                unset($tmp_topic_data);
377
        }
378
        else if ($config['load_anon_lastread'] || $user->data['is_registered'])
379
        {
380
                $topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
381
        }
382
}
383
384
// Post ordering options
385
$limit_days = array(0 => $user->lang['ALL_POSTS'], 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']);
386
387
$sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
388
$sort_by_sql = array('a' => array('u.username_clean', 'p.post_id'), 't' => 'p.post_time', 's' => array('p.post_subject', 'p.post_id'));
389
$join_user_sql = array('a' => true, 't' => false, 's' => false);
390
391
$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
392
393
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);
394
395
// Obtain correct post count and ordering SQL if user has
396
// requested anything different
397
if ($sort_days)
398
{
399
        $min_post_time = time() - ($sort_days * 86400);
400
401
        $sql = 'SELECT COUNT(post_id) AS num_posts
402
                FROM ' . POSTS_TABLE . "
403
                WHERE topic_id = $topic_id
404
                        AND post_time >= $min_post_time
405
                " . (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND post_approved = 1');
406
        $result = $db->sql_query($sql);
407
        $total_posts = (int) $db->sql_fetchfield('num_posts');
408
        $db->sql_freeresult($result);
409
410
        $limit_posts_time = "AND p.post_time >= $min_post_time ";
411
412
        if (isset($_POST['sort']))
413
        {
414
                $start = 0;
415
        }
416
}
417
else
418
{
419
        $total_posts = $topic_replies + 1;
420
        $limit_posts_time = '';
421
}
422
423
// Was a highlight request part of the URI?
424
$highlight_match = $highlight = '';
425
if ($hilit_words)
426
{
427
        foreach (explode(' ', trim($hilit_words)) as $word)
428
        {
429
                if (trim($word))
430
                {
431
                        $word = str_replace('\*', '\w+?', preg_quote($word, '#'));
432
                        $word = preg_replace('#(^|\s)\\\\w\*\?(\s|$)#', '$1\w+?$2', $word);
433
                        $highlight_match .= (($highlight_match != '') ? '|' : '') . $word;
434
                }
435
        }
436
437
        $highlight = urlencode($hilit_words);
438
}
439
440
// Make sure $start is set to the last page if it exceeds the amount
441
if ($start < 0 || $start >= $total_posts)
442
{
443
        $start = ($start < 0) ? 0 : floor(($total_posts - 1) / $config['posts_per_page']) * $config['posts_per_page'];
444
}
445
446
// General Viewtopic URL for return links
447
$viewtopic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start") . ((strlen($u_sort_param)) ? "&amp;$u_sort_param" : '') . (($highlight_match) ? "&amp;hilit=$highlight" : ''));
448
449
// Are we watching this topic?
450
$s_watching_topic = array(
451
        'link'                        => '',
452
        'title'                        => '',
453
        'is_watching'        => false,
454
);
455
456
if (($config['email_enable'] || $config['jab_enable']) && $config['allow_topic_notify'])
457
{
458
        $notify_status = (isset($topic_data['notify_status'])) ? $topic_data['notify_status'] : null;
459
        watch_topic_forum('topic', $s_watching_topic, $user->data['user_id'], $forum_id, $topic_id, $notify_status, $start, $topic_data['topic_title']);
460
461
        // Reset forum notification if forum notify is set
462
        if ($config['allow_forum_notify'] && $auth->acl_get('f_subscribe', $forum_id))
463
        {
464
                $s_watching_forum = $s_watching_topic;
465
                watch_topic_forum('forum', $s_watching_forum, $user->data['user_id'], $forum_id, 0);
466
        }
467
}
468
469
// Bookmarks
470
if ($config['allow_bookmarks'] && $user->data['is_registered'] && request_var('bookmark', 0))
471
{
472
        if (check_link_hash(request_var('hash', ''), "topic_$topic_id"))
473
        {
474
                if (!$topic_data['bookmarked'])
475
                {
476
                        $sql = 'INSERT INTO ' . BOOKMARKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
477
                                'user_id'        => $user->data['user_id'],
478
                                'topic_id'        => $topic_id,
479
                        ));
480
                        $db->sql_query($sql);
481
                }
482
                else
483
                {
484
                        $sql = 'DELETE FROM ' . BOOKMARKS_TABLE . "
485
                                WHERE user_id = {$user->data['user_id']}
486
                                        AND topic_id = $topic_id";
487
                        $db->sql_query($sql);
488
                }
489
                $message = (($topic_data['bookmarked']) ? $user->lang['BOOKMARK_REMOVED'] : $user->lang['BOOKMARK_ADDED']) . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $viewtopic_url . '">', '</a>');
490
        }
491
        else
492
        {
493
                $message = $user->lang['BOOKMARK_ERR'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $viewtopic_url . '">', '</a>');
494
        }
495
        meta_refresh(3, $viewtopic_url);
496
497
        trigger_error($message);
498
}
499
500
// Grab ranks
501
$ranks = $cache->obtain_ranks();
502
503
// Grab icons
504
$icons = $cache->obtain_icons();
505
506
// Grab extensions
507
$extensions = array();
508
if ($topic_data['topic_attachment'])
509
{
510
        $extensions = $cache->obtain_attach_extensions($forum_id);
511
}
512
513
// Forum rules listing
514
$s_forum_rules = '';
515
gen_forum_auth_level('topic', $forum_id, $topic_data['forum_status']);
516
517
// Quick mod tools
518
$allow_change_type = ($auth->acl_get('m_', $forum_id) || ($user->data['is_registered'] && $user->data['user_id'] == $topic_data['topic_poster'])) ? true : false;
519
520
$topic_mod = '';
521
$topic_mod .= ($auth->acl_get('m_lock', $forum_id) || ($auth->acl_get('f_user_lock', $forum_id) && $user->data['is_registered'] && $user->data['user_id'] == $topic_data['topic_poster'] && $topic_data['topic_status'] == ITEM_UNLOCKED)) ? (($topic_data['topic_status'] == ITEM_UNLOCKED) ? '<option value="lock">' . $user->lang['LOCK_TOPIC'] . '</option>' : '<option value="unlock">' . $user->lang['UNLOCK_TOPIC'] . '</option>') : '';
522
$topic_mod .= ($auth->acl_get('m_delete', $forum_id)) ? '<option value="delete_topic">' . $user->lang['DELETE_TOPIC'] . '</option>' : '';
523
$topic_mod .= ($auth->acl_get('m_move', $forum_id) && $topic_data['topic_status'] != ITEM_MOVED) ? '<option value="move">' . $user->lang['MOVE_TOPIC'] . '</option>' : '';
524
$topic_mod .= ($auth->acl_get('m_split', $forum_id)) ? '<option value="split">' . $user->lang['SPLIT_TOPIC'] . '</option>' : '';
525
$topic_mod .= ($auth->acl_get('m_merge', $forum_id)) ? '<option value="merge">' . $user->lang['MERGE_POSTS'] . '</option>' : '';
526
$topic_mod .= ($auth->acl_get('m_merge', $forum_id)) ? '<option value="merge_topic">' . $user->lang['MERGE_TOPIC'] . '</option>' : '';
527
$topic_mod .= ($auth->acl_get('m_move', $forum_id)) ? '<option value="fork">' . $user->lang['FORK_TOPIC'] . '</option>' : '';
528
$topic_mod .= ($allow_change_type && $auth->acl_gets('f_sticky', 'f_announce', $forum_id) && $topic_data['topic_type'] != POST_NORMAL) ? '<option value="make_normal">' . $user->lang['MAKE_NORMAL'] . '</option>' : '';
529
$topic_mod .= ($allow_change_type && $auth->acl_get('f_sticky', $forum_id) && $topic_data['topic_type'] != POST_STICKY) ? '<option value="make_sticky">' . $user->lang['MAKE_STICKY'] . '</option>' : '';
530
$topic_mod .= ($allow_change_type && $auth->acl_get('f_announce', $forum_id) && $topic_data['topic_type'] != POST_ANNOUNCE) ? '<option value="make_announce">' . $user->lang['MAKE_ANNOUNCE'] . '</option>' : '';
531
$topic_mod .= ($allow_change_type && $auth->acl_get('f_announce', $forum_id) && $topic_data['topic_type'] != POST_GLOBAL) ? '<option value="make_global">' . $user->lang['MAKE_GLOBAL'] . '</option>' : '';
532
$topic_mod .= ($auth->acl_get('m_', $forum_id)) ? '<option value="topic_logs">' . $user->lang['VIEW_TOPIC_LOGS'] . '</option>' : '';
533
534
// If we've got a hightlight set pass it on to pagination.
535
$pagination = generate_pagination(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . ((strlen($u_sort_param)) ? "&amp;$u_sort_param" : '') . (($highlight_match) ? "&amp;hilit=$highlight" : '')), $total_posts, $config['posts_per_page'], $start);
536
537
// Navigation links
538
generate_forum_nav($topic_data);
539
540
// Forum Rules
541
generate_forum_rules($topic_data);
542
543
// Moderators
544
$forum_moderators = array();
545
if ($config['load_moderators'])
546
{
547
        get_moderators($forum_moderators, $forum_id);
548
}
549
550
// This is only used for print view so ...
551
$server_path = (!$view) ? $phpbb_root_path : generate_board_url() . '/';
552
553
// Replace naughty words in title
554
$topic_data['topic_title'] = censor_text($topic_data['topic_title']);
555
556
$s_search_hidden_fields = array(
557
        't' => $topic_id,
558
        'sf' => 'msgonly',
559
);
560
if ($_SID)
561
{
562
        $s_search_hidden_fields['sid'] = $_SID;
563
}
564
565
if (!empty($_EXTRA_URL))
566
{
567
        foreach ($_EXTRA_URL as $url_param)
568
        {
569
                $url_param = explode('=', $url_param, 2);
570
                $s_search_hidden_fields[$url_param[0]] = $url_param[1];
571
        }
572
}
573
574
// Send vars to template
575
$template->assign_vars(array(
576
        'FORUM_ID'                 => $forum_id,
577
        'FORUM_NAME'         => $topic_data['forum_name'],
578
        'FORUM_DESC'        => generate_text_for_display($topic_data['forum_desc'], $topic_data['forum_desc_uid'], $topic_data['forum_desc_bitfield'], $topic_data['forum_desc_options']),
579
        'TOPIC_ID'                 => $topic_id,
580
        'TOPIC_TITLE'         => $topic_data['topic_title'],
581
        'TOPIC_POSTER'        => $topic_data['topic_poster'],
582
583
        'TOPIC_AUTHOR_FULL'                => get_username_string('full', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
584
        'TOPIC_AUTHOR_COLOUR'        => get_username_string('colour', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
585
        'TOPIC_AUTHOR'                        => get_username_string('username', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
586
587
        'PAGINATION'         => $pagination,
588
        'PAGE_NUMBER'         => on_page($total_posts, $config['posts_per_page'], $start),
589
        'TOTAL_POSTS'        => $user->lang('VIEW_TOPIC_POSTS', (int) $total_posts),
590
        'U_MCP'                 => ($auth->acl_get('m_', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=topic_view&amp;f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start") . ((strlen($u_sort_param)) ? "&amp;$u_sort_param" : ''), true, $user->session_id) : '',
591
        'MODERATORS'        => (isset($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id])) ? implode(', ', $forum_moderators[$forum_id]) : '',
592
593
        'POST_IMG'                         => ($topic_data['forum_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', 'FORUM_LOCKED') : $user->img('button_topic_new', 'POST_NEW_TOPIC'),
594
        'QUOTE_IMG'                 => $user->img('icon_post_quote', 'REPLY_WITH_QUOTE'),
595
        'REPLY_IMG'                        => ($topic_data['forum_status'] == ITEM_LOCKED || $topic_data['topic_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', 'TOPIC_LOCKED') : $user->img('button_topic_reply', 'REPLY_TO_TOPIC'),
596
        'EDIT_IMG'                         => $user->img('icon_post_edit', 'EDIT_POST'),
597
        'DELETE_IMG'                 => $user->img('icon_post_delete', 'DELETE_POST'),
598
        'INFO_IMG'                         => $user->img('icon_post_info', 'VIEW_INFO'),
599
        'PROFILE_IMG'                => $user->img('icon_user_profile', 'READ_PROFILE'),
600
        'SEARCH_IMG'                 => $user->img('icon_user_search', 'SEARCH_USER_POSTS'),
601
        'PM_IMG'                         => $user->img('icon_contact_pm', 'SEND_PRIVATE_MESSAGE'),
602
        'EMAIL_IMG'                 => $user->img('icon_contact_email', 'SEND_EMAIL'),
603
        'WWW_IMG'                         => $user->img('icon_contact_www', 'VISIT_WEBSITE'),
604
        'ICQ_IMG'                         => $user->img('icon_contact_icq', 'ICQ'),
605
        'AIM_IMG'                         => $user->img('icon_contact_aim', 'AIM'),
606
        'MSN_IMG'                         => $user->img('icon_contact_msnm', 'MSNM'),
607
        'YIM_IMG'                         => $user->img('icon_contact_yahoo', 'YIM'),
608
        'JABBER_IMG'                => $user->img('icon_contact_jabber', 'JABBER') ,
609
        'REPORT_IMG'                => $user->img('icon_post_report', 'REPORT_POST'),
610
        'REPORTED_IMG'                => $user->img('icon_topic_reported', 'POST_REPORTED'),
611
        'UNAPPROVED_IMG'        => $user->img('icon_topic_unapproved', 'POST_UNAPPROVED'),
612
        'WARN_IMG'                        => $user->img('icon_user_warn', 'WARN_USER'),
613
614
        'S_IS_LOCKED'                        => ($topic_data['topic_status'] == ITEM_UNLOCKED && $topic_data['forum_status'] == ITEM_UNLOCKED) ? false : true,
615
        'S_SELECT_SORT_DIR'         => $s_sort_dir,
616
        'S_SELECT_SORT_KEY'         => $s_sort_key,
617
        'S_SELECT_SORT_DAYS'         => $s_limit_days,
618
        'S_SINGLE_MODERATOR'        => (!empty($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id]) > 1) ? false : true,
619
        'S_TOPIC_ACTION'                 => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start")),
620
        'S_TOPIC_MOD'                         => ($topic_mod != '') ? '<select name="action" id="quick-mod-select">' . $topic_mod . '</select>' : '',
621
        'S_MOD_ACTION'                         => append_sid("{$phpbb_root_path}mcp.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start") . "&amp;quickmod=1&amp;redirect=" . urlencode(str_replace('&amp;', '&', $viewtopic_url)), true, $user->session_id),
622
623
        'S_VIEWTOPIC'                        => true,
624
        'S_DISPLAY_SEARCHBOX'        => ($auth->acl_get('u_search') && $auth->acl_get('f_search', $forum_id) && $config['load_search']) ? true : false,
625
        'S_SEARCHBOX_ACTION'        => append_sid("{$phpbb_root_path}search.$phpEx"),
626
        'S_SEARCH_LOCAL_HIDDEN_FIELDS'        => build_hidden_fields($s_search_hidden_fields),
627
628
        'S_DISPLAY_POST_INFO'        => ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_post', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
629
        'S_DISPLAY_REPLY_INFO'        => ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_reply', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
630
        'S_ENABLE_FEEDS_TOPIC'        => ($config['feed_topic'] && !phpbb_optionget(FORUM_OPTION_FEED_EXCLUDE, $topic_data['forum_options'])) ? true : false,
631
632
        'U_TOPIC'                                => "{$server_path}viewtopic.$phpEx?f=$forum_id&amp;t=$topic_id",
633
        'U_FORUM'                                => $server_path,
634
        'U_VIEW_TOPIC'                         => $viewtopic_url,
635
        'U_VIEW_FORUM'                         => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
636
        'U_VIEW_OLDER_TOPIC'        => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=previous"),
637
        'U_VIEW_NEWER_TOPIC'        => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=next"),
638
        'U_PRINT_TOPIC'                        => ($auth->acl_get('f_print', $forum_id)) ? $viewtopic_url . '&amp;view=print' : '',
639
        'U_EMAIL_TOPIC'                        => ($auth->acl_get('f_email', $forum_id) && $config['email_enable']) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=email&amp;t=$topic_id") : '',
640
641
        'U_WATCH_TOPIC'                 => $s_watching_topic['link'],
642
        'L_WATCH_TOPIC'                 => $s_watching_topic['title'],
643
        'S_WATCHING_TOPIC'                => $s_watching_topic['is_watching'],
644
645
        'U_BOOKMARK_TOPIC'                => ($user->data['is_registered'] && $config['allow_bookmarks']) ? $viewtopic_url . '&amp;bookmark=1&amp;hash=' . generate_link_hash("topic_$topic_id") : '',
646
        'L_BOOKMARK_TOPIC'                => ($user->data['is_registered'] && $config['allow_bookmarks'] && $topic_data['bookmarked']) ? $user->lang['BOOKMARK_TOPIC_REMOVE'] : $user->lang['BOOKMARK_TOPIC'],
647
648
        '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") : '',
649
        'U_POST_REPLY_TOPIC'         => ($auth->acl_get('f_reply', $forum_id) || $user->data['user_id'] == ANONYMOUS) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=reply&amp;f=$forum_id&amp;t=$topic_id") : '',
650
        'U_BUMP_TOPIC'                        => (bump_topic_allowed($forum_id, $topic_data['topic_bumped'], $topic_data['topic_last_post_time'], $topic_data['topic_poster'], $topic_data['topic_last_poster_id'])) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=bump&amp;f=$forum_id&amp;t=$topic_id&amp;hash=" . generate_link_hash("topic_$topic_id")) : '')
651
);
652
653
// Does this topic contain a poll?
654
if (!empty($topic_data['poll_start']))
655
{
656
        $sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid
657
                FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p
658
                WHERE o.topic_id = $topic_id
659
                        AND p.post_id = {$topic_data['topic_first_post_id']}
660
                        AND p.topic_id = o.topic_id
661
                ORDER BY o.poll_option_id";
662
        $result = $db->sql_query($sql);
663
664
        $poll_info = array();
665
        while ($row = $db->sql_fetchrow($result))
666
        {
667
                $poll_info[] = $row;
668
        }
669
        $db->sql_freeresult($result);
670
671
        $cur_voted_id = array();
672
        if ($user->data['is_registered'])
673
        {
674
                $sql = 'SELECT poll_option_id
675
                        FROM ' . POLL_VOTES_TABLE . '
676
                        WHERE topic_id = ' . $topic_id . '
677
                                AND vote_user_id = ' . $user->data['user_id'];
678
                $result = $db->sql_query($sql);
679
680
                while ($row = $db->sql_fetchrow($result))
681
                {
682
                        $cur_voted_id[] = $row['poll_option_id'];
683
                }
684
                $db->sql_freeresult($result);
685
        }
686
        else
687
        {
688
                // Cookie based guest tracking ... I don't like this but hum ho
689
                // it's oft requested. This relies on "nice" users who don't feel
690
                // the need to delete cookies to mess with results.
691
                if ($request->is_set($config['cookie_name'] . '_poll_' . $topic_id, phpbb_request_interface::COOKIE))
692
                {
693
                        $cur_voted_id = explode(',', $request->variable($config['cookie_name'] . '_poll_' . $topic_id, '', true, phpbb_request_interface::COOKIE));
694
                        $cur_voted_id = array_map('intval', $cur_voted_id);
695
                }
696
        }
697
698
        // Can not vote at all if no vote permission
699
        $s_can_vote = ($auth->acl_get('f_vote', $forum_id) &&
700
                (($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0) &&
701
                $topic_data['topic_status'] != ITEM_LOCKED &&
702
                $topic_data['forum_status'] != ITEM_LOCKED &&
703
                (!sizeof($cur_voted_id) ||
704
                ($auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change']))) ? true : false;
705
        $s_display_results = (!$s_can_vote || ($s_can_vote && sizeof($cur_voted_id)) || $view == 'viewpoll') ? true : false;
706
707
        if ($update && $s_can_vote)
708
        {
709
710
                if (!sizeof($voted_id) || sizeof($voted_id) > $topic_data['poll_max_options'] || in_array(VOTE_CONVERTED, $cur_voted_id) || !check_form_key('posting'))
711
                {
712
                        $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start"));
713
714
                        meta_refresh(5, $redirect_url);
715
                        if (!sizeof($voted_id))
716
                        {
717
                                $message = 'NO_VOTE_OPTION';
718
                        }
719
                        else if (sizeof($voted_id) > $topic_data['poll_max_options'])
720
                        {
721
                                $message = 'TOO_MANY_VOTE_OPTIONS';
722
                        }
723
                        else if (in_array(VOTE_CONVERTED, $cur_voted_id))
724
                        {
725
                                $message = 'VOTE_CONVERTED';
726
                        }
727
                        else
728
                        {
729
                                $message = 'FORM_INVALID';
730
                        }
731
732
                        $message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
733
                        trigger_error($message);
734
                }
735
736
                foreach ($voted_id as $option)
737
                {
738
                        if (in_array($option, $cur_voted_id))
739
                        {
740
                                continue;
741
                        }
742
743
                        $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
744
                                SET poll_option_total = poll_option_total + 1
745
                                WHERE poll_option_id = ' . (int) $option . '
746
                                        AND topic_id = ' . (int) $topic_id;
747
                        $db->sql_query($sql);
748
749
                        if ($user->data['is_registered'])
750
                        {
751
                                $sql_ary = array(
752
                                        'topic_id'                        => (int) $topic_id,
753
                                        'poll_option_id'        => (int) $option,
754
                                        'vote_user_id'                => (int) $user->data['user_id'],
755
                                        'vote_user_ip'                => (string) $user->ip,
756
                                );
757
758
                                $sql = 'INSERT INTO ' . POLL_VOTES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
759
                                $db->sql_query($sql);
760
                        }
761
                }
762
763
                foreach ($cur_voted_id as $option)
764
                {
765
                        if (!in_array($option, $voted_id))
766
                        {
767
                                $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
768
                                        SET poll_option_total = poll_option_total - 1
769
                                        WHERE poll_option_id = ' . (int) $option . '
770
                                                AND topic_id = ' . (int) $topic_id;
771
                                $db->sql_query($sql);
772
773
                                if ($user->data['is_registered'])
774
                                {
775
                                        $sql = 'DELETE FROM ' . POLL_VOTES_TABLE . '
776
                                                WHERE topic_id = ' . (int) $topic_id . '
777
                                                        AND poll_option_id = ' . (int) $option . '
778
                                                        AND vote_user_id = ' . (int) $user->data['user_id'];
779
                                        $db->sql_query($sql);
780
                                }
781
                        }
782
                }
783
784
                if ($user->data['user_id'] == ANONYMOUS && !$user->data['is_bot'])
785
                {
786
                        $user->set_cookie('poll_' . $topic_id, implode(',', $voted_id), time() + 31536000);
787
                }
788
789
                $sql = 'UPDATE ' . TOPICS_TABLE . '
790
                        SET poll_last_vote = ' . time() . "
791
                        WHERE topic_id = $topic_id";
792
                //, topic_last_post_time = ' . time() . " -- for bumping topics with new votes, ignore for now
793
                $db->sql_query($sql);
794
795
                $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start"));
796
797
                meta_refresh(5, $redirect_url);
798
                trigger_error($user->lang['VOTE_SUBMITTED'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>'));
799
        }
800
801
        $poll_total = 0;
802
        foreach ($poll_info as $poll_option)
803
        {
804
                $poll_total += $poll_option['poll_option_total'];
805
        }
806
807
        if ($poll_info[0]['bbcode_bitfield'])
808
        {
809
                $poll_bbcode = new bbcode();
810
        }
811
        else
812
        {
813
                $poll_bbcode = false;
814
        }
815
816
        for ($i = 0, $size = sizeof($poll_info); $i < $size; $i++)
817
        {
818
                $poll_info[$i]['poll_option_text'] = censor_text($poll_info[$i]['poll_option_text']);
819
820
                if ($poll_bbcode !== false)
821
                {
822
                        $poll_bbcode->bbcode_second_pass($poll_info[$i]['poll_option_text'], $poll_info[$i]['bbcode_uid'], $poll_option['bbcode_bitfield']);
823
                }
824
825
                $poll_info[$i]['poll_option_text'] = bbcode_nl2br($poll_info[$i]['poll_option_text']);
826
                $poll_info[$i]['poll_option_text'] = smiley_text($poll_info[$i]['poll_option_text']);
827
        }
828
829
        $topic_data['poll_title'] = censor_text($topic_data['poll_title']);
830
831
        if ($poll_bbcode !== false)
832
        {
833
                $poll_bbcode->bbcode_second_pass($topic_data['poll_title'], $poll_info[0]['bbcode_uid'], $poll_info[0]['bbcode_bitfield']);
834
        }
835
836
        $topic_data['poll_title'] = bbcode_nl2br($topic_data['poll_title']);
837
        $topic_data['poll_title'] = smiley_text($topic_data['poll_title']);
838
839
        unset($poll_bbcode);
840
841
        foreach ($poll_info as $poll_option)
842
        {
843
                $option_pct = ($poll_total > 0) ? $poll_option['poll_option_total'] / $poll_total : 0;
844
                $option_pct_txt = sprintf("%.1d%%", round($option_pct * 100));
845
846
                $template->assign_block_vars('poll_option', array(
847
                        'POLL_OPTION_ID'                 => $poll_option['poll_option_id'],
848
                        'POLL_OPTION_CAPTION'         => $poll_option['poll_option_text'],
849
                        'POLL_OPTION_RESULT'         => $poll_option['poll_option_total'],
850
                        'POLL_OPTION_PERCENT'         => $option_pct_txt,
851
                        'POLL_OPTION_PCT'                => round($option_pct * 100),
852
                        'POLL_OPTION_WIDTH'     => round($option_pct * 250),
853
                        'POLL_OPTION_VOTED'                => (in_array($poll_option['poll_option_id'], $cur_voted_id)) ? true : false)
854
                );
855
        }
856
857
        $poll_end = $topic_data['poll_length'] + $topic_data['poll_start'];
858
859
        $template->assign_vars(array(
860
                'POLL_QUESTION'                => $topic_data['poll_title'],
861
                'TOTAL_VOTES'                 => $poll_total,
862
                'POLL_LEFT_CAP_IMG'        => $user->img('poll_left'),
863
                'POLL_RIGHT_CAP_IMG'=> $user->img('poll_right'),
864
865
                'L_MAX_VOTES'                => $user->lang('MAX_OPTIONS_SELECT', (int) $topic_data['poll_max_options']),
866
                'L_POLL_LENGTH'                => ($topic_data['poll_length']) ? sprintf($user->lang[($poll_end > time()) ? 'POLL_RUN_TILL' : 'POLL_ENDED_AT'], $user->format_date($poll_end)) : '',
867
868
                'S_HAS_POLL'                => true,
869
                'S_CAN_VOTE'                => $s_can_vote,
870
                'S_DISPLAY_RESULTS'        => $s_display_results,
871
                'S_IS_MULTI_CHOICE'        => ($topic_data['poll_max_options'] > 1) ? true : false,
872
                'S_POLL_ACTION'                => $viewtopic_url,
873
874
                'U_VIEW_RESULTS'        => $viewtopic_url . '&amp;view=viewpoll')
875
        );
876
877
        unset($poll_end, $poll_info, $voted_id);
878
}
879
880
// If the user is trying to reach the second half of the topic, fetch it starting from the end
881
$store_reverse = false;
882
$sql_limit = $config['posts_per_page'];
883
$sql_sort_order = $direction = '';
884
885
if ($start > $total_posts / 2)
886
{
887
        $store_reverse = true;
888
889
        if ($start + $config['posts_per_page'] > $total_posts)
890
        {
891
                $sql_limit = min($config['posts_per_page'], max(1, $total_posts - $start));
892
        }
893
894
        // Select the sort order
895
        $direction = (($sort_dir == 'd') ? 'ASC' : 'DESC');
896
        $sql_start = max(0, $total_posts - $sql_limit - $start);
897
}
898
else
899
{
900
        // Select the sort order
901
        $direction = (($sort_dir == 'd') ? 'DESC' : 'ASC');
902
        $sql_start = $start;
903
}
904
905
if (is_array($sort_by_sql[$sort_key]))
906
{
907
        $sql_sort_order = implode(' ' . $direction . ', ', $sort_by_sql[$sort_key]) . ' ' . $direction;
908
}
909
else
910
{
911
        $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . $direction;
912
}
913
914
// Container for user details, only process once
915
$post_list = $user_cache = $id_cache = $attachments = $attach_list = $rowset = $update_count = $post_edit_list = array();
916
$has_attachments = $display_notice = false;
917
$bbcode_bitfield = '';
918
$i = $i_total = 0;
919
920
// Go ahead and pull all data for this topic
921
$sql = 'SELECT p.post_id
922
        FROM ' . POSTS_TABLE . ' p' . (($join_user_sql[$sort_key]) ? ', ' . USERS_TABLE . ' u': '') . "
923
        WHERE p.topic_id = $topic_id
924
                " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . "
925
                " . (($join_user_sql[$sort_key]) ? 'AND u.user_id = p.poster_id': '') . "
926
                $limit_posts_time
927
        ORDER BY $sql_sort_order";
928
$result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
929
930
$i = ($store_reverse) ? $sql_limit - 1 : 0;
931
while ($row = $db->sql_fetchrow($result))
932
{
933
        $post_list[$i] = (int) $row['post_id'];
934
        ($store_reverse) ? $i-- : $i++;
935
}
936
$db->sql_freeresult($result);
937
938
if (!sizeof($post_list))
939
{
940
        if ($sort_days)
941
        {
942
                trigger_error('NO_POSTS_TIME_FRAME');
943
        }
944
        else
945
        {
946
                trigger_error('NO_TOPIC');
947
        }
948
}
949
950
// Holding maximum post time for marking topic read
951
// We need to grab it because we do reverse ordering sometimes
952
$max_post_time = 0;
953
954
$sql_ary = array(
955
        'SELECT'        => 'u.*, z.friend, z.foe, p.*',
956
957
        'FROM'                => array(
958
                USERS_TABLE                => 'u',
959
                POSTS_TABLE                => 'p',
960
        ),
961
962
        'LEFT_JOIN'        => array(
963
                array(
964
                        'FROM'        => array(ZEBRA_TABLE => 'z'),
965
                        'ON'        => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id',
966
                ),
967
        ),
968
969
        'WHERE'                => $db->sql_in_set('p.post_id', $post_list) . '
970
                AND u.user_id = p.poster_id',
971
);
972
973
$sql = $db->sql_build_query('SELECT', $sql_ary);
974
$result = $db->sql_query($sql);
975
976
$now = phpbb_gmgetdate(time() + $user->timezone + $user->dst);
977
978
// Posts are stored in the $rowset array while $attach_list, $user_cache
979
// and the global bbcode_bitfield are built
980
while ($row = $db->sql_fetchrow($result))
981
{
982
        // Set max_post_time
983
        if ($row['post_time'] > $max_post_time)
984
        {
985
                $max_post_time = $row['post_time'];
986
        }
987
988
        $poster_id = (int) $row['poster_id'];
989
990
        // Does post have an attachment? If so, add it to the list
991
        if ($row['post_attachment'] && $config['allow_attachments'])
992
        {
993
                $attach_list[] = (int) $row['post_id'];
994
995
                if ($row['post_approved'])
996
                {
997
                        $has_attachments = true;
998
                }
999
        }
1000
1001
        $rowset[$row['post_id']] = array(
1002
                'hide_post'                        => ($row['foe'] && ($view != 'show' || $post_id != $row['post_id'])) ? true : false,
1003
1004
                'post_id'                        => $row['post_id'],
1005
                'post_time'                        => $row['post_time'],
1006
                'user_id'                        => $row['user_id'],
1007
                'username'                        => $row['username'],
1008
                'user_colour'                => $row['user_colour'],
1009
                'topic_id'                        => $row['topic_id'],
1010
                'forum_id'                        => $row['forum_id'],
1011
                'post_subject'                => $row['post_subject'],
1012
                'post_edit_count'        => $row['post_edit_count'],
1013
                'post_edit_time'        => $row['post_edit_time'],
1014
                'post_edit_reason'        => $row['post_edit_reason'],
1015
                'post_edit_user'        => $row['post_edit_user'],
1016
                'post_edit_locked'        => $row['post_edit_locked'],
1017
1018
                // Make sure the icon actually exists
1019
                'icon_id'                        => (isset($icons[$row['icon_id']]['img'], $icons[$row['icon_id']]['height'], $icons[$row['icon_id']]['width'])) ? $row['icon_id'] : 0,
1020
                'post_attachment'        => $row['post_attachment'],
1021
                'post_approved'                => $row['post_approved'],
1022
                'post_reported'                => $row['post_reported'],
1023
                'post_username'                => $row['post_username'],
1024
                'post_text'                        => $row['post_text'],
1025
                'bbcode_uid'                => $row['bbcode_uid'],
1026
                'bbcode_bitfield'        => $row['bbcode_bitfield'],
1027
                'enable_smilies'        => $row['enable_smilies'],
1028
                'enable_sig'                => $row['enable_sig'],
1029
                'friend'                        => $row['friend'],
1030
                'foe'                                => $row['foe'],
1031
        );
1032
1033
        // Define the global bbcode bitfield, will be used to load bbcodes
1034
        $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
1035
1036
        // Is a signature attached? Are we going to display it?
1037
        if ($row['enable_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
1038
        {
1039
                $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['user_sig_bbcode_bitfield']);
1040
        }
1041
1042
        // Cache various user specific data ... so we don't have to recompute
1043
        // this each time the same user appears on this page
1044
        if (!isset($user_cache[$poster_id]))
1045
        {
1046
                if ($poster_id == ANONYMOUS)
1047
                {
1048
                        $user_cache[$poster_id] = array(
1049
                                'joined'                => '',
1050
                                'posts'                        => '',
1051
                                'from'                        => '',
1052
1053
                                'sig'                                        => '',
1054
                                'sig_bbcode_uid'                => '',
1055
                                'sig_bbcode_bitfield'        => '',
1056
1057
                                'online'                        => false,
1058
                                'avatar'                        => ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height']) : '',
1059
                                'rank_title'                => '',
1060
                                'rank_image'                => '',
1061
                                'rank_image_src'        => '',
1062
                                'sig'                                => '',
1063
                                'profile'                        => '',
1064
                                'pm'                                => '',
1065
                                'email'                                => '',
1066
                                'www'                                => '',
1067
                                'icq_status_img'        => '',
1068
                                'icq'                                => '',
1069
                                'aim'                                => '',
1070
                                'msn'                                => '',
1071
                                'yim'                                => '',
1072
                                'jabber'                        => '',
1073
                                'search'                        => '',
1074
                                'age'                                => '',
1075
1076
                                'username'                        => $row['username'],
1077
                                'user_colour'                => $row['user_colour'],
1078
1079
                                'warnings'                        => 0,
1080
                                'allow_pm'                        => 0,
1081
                        );
1082
1083
                        get_user_rank($row['user_rank'], false, $user_cache[$poster_id]['rank_title'], $user_cache[$poster_id]['rank_image'], $user_cache[$poster_id]['rank_image_src']);
1084
                }
1085
                else
1086
                {
1087
                        $user_sig = '';
1088
1089
                        // We add the signature to every posters entry because enable_sig is post dependant
1090
                        if ($row['user_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
1091
                        {
1092
                                $user_sig = $row['user_sig'];
1093
                        }
1094
1095
                        $id_cache[] = $poster_id;
1096
1097
                        $user_cache[$poster_id] = array(
1098
                                'joined'                => $user->format_date($row['user_regdate']),
1099
                                'posts'                        => $row['user_posts'],
1100
                                'warnings'                => (isset($row['user_warnings'])) ? $row['user_warnings'] : 0,
1101
                                'from'                        => (!empty($row['user_from'])) ? $row['user_from'] : '',
1102
1103
                                'sig'                                        => $user_sig,
1104
                                'sig_bbcode_uid'                => (!empty($row['user_sig_bbcode_uid'])) ? $row['user_sig_bbcode_uid'] : '',
1105
                                'sig_bbcode_bitfield'        => (!empty($row['user_sig_bbcode_bitfield'])) ? $row['user_sig_bbcode_bitfield'] : '',
1106
1107
                                'viewonline'        => $row['user_allow_viewonline'],
1108
                                'allow_pm'                => $row['user_allow_pm'],
1109
1110
                                'avatar'                => ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height']) : '',
1111
                                'age'                        => '',
1112
1113
                                'rank_title'                => '',
1114
                                'rank_image'                => '',
1115
                                'rank_image_src'        => '',
1116
1117
                                'username'                        => $row['username'],
1118
                                'user_colour'                => $row['user_colour'],
1119
1120
                                'online'                => false,
1121
                                'profile'                => append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=viewprofile&amp;u=$poster_id"),
1122
                                'www'                        => $row['user_website'],
1123
                                'aim'                        => ($row['user_aim'] && $auth->acl_get('u_sendim')) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=contact&amp;action=aim&amp;u=$poster_id") : '',
1124
                                'msn'                        => ($row['user_msnm'] && $auth->acl_get('u_sendim')) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=contact&amp;action=msnm&amp;u=$poster_id") : '',
1125
                                'yim'                        => ($row['user_yim']) ? 'http://edit.yahoo.com/config/send_webmesg?.target=' . urlencode($row['user_yim']) . '&amp;.src=pg' : '',
1126
                                'jabber'                => ($row['user_jabber'] && $auth->acl_get('u_sendim')) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=contact&amp;action=jabber&amp;u=$poster_id") : '',
1127
                                'search'                => ($auth->acl_get('u_search')) ? append_sid("{$phpbb_root_path}search.$phpEx", "author_id=$poster_id&amp;sr=posts") : '',
1128
1129
                                'author_full'                => get_username_string('full', $poster_id, $row['username'], $row['user_colour']),
1130
                                'author_colour'                => get_username_string('colour', $poster_id, $row['username'], $row['user_colour']),
1131
                                'author_username'        => get_username_string('username', $poster_id, $row['username'], $row['user_colour']),
1132
                                'author_profile'        => get_username_string('profile', $poster_id, $row['username'], $row['user_colour']),
1133
                        );
1134
1135
                        get_user_rank($row['user_rank'], $row['user_posts'], $user_cache[$poster_id]['rank_title'], $user_cache[$poster_id]['rank_image'], $user_cache[$poster_id]['rank_image_src']);
1136
1137
                        if ((!empty($row['user_allow_viewemail']) && $auth->acl_get('u_sendemail')) || $auth->acl_get('a_email'))
1138
                        {
1139
                                $user_cache[$poster_id]['email'] = ($config['board_email_form'] && $config['email_enable']) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=email&amp;u=$poster_id") : (($config['board_hide_emails'] && !$auth->acl_get('a_email')) ? '' : 'mailto:' . $row['user_email']);
1140
                        }
1141
                        else
1142
                        {
1143
                                $user_cache[$poster_id]['email'] = '';
1144
                        }
1145
1146
                        if (!empty($row['user_icq']))
1147
                        {
1148
                                $user_cache[$poster_id]['icq'] = 'http://www.icq.com/people/' . urlencode($row['user_icq']) . '/';
1149
                                $user_cache[$poster_id]['icq_status_img'] = '<img src="http://web.icq.com/whitepages/online?icq=' . $row['user_icq'] . '&amp;img=5" width="18" height="18" alt="" />';
1150
                        }
1151
                        else
1152
                        {
1153
                                $user_cache[$poster_id]['icq_status_img'] = '';
1154
                                $user_cache[$poster_id]['icq'] = '';
1155
                        }
1156
1157
                        if ($config['allow_birthdays'] && !empty($row['user_birthday']))
1158
                        {
1159
                                list($bday_day, $bday_month, $bday_year) = array_map('intval', explode('-', $row['user_birthday']));
1160
1161
                                if ($bday_year)
1162
                                {
1163
                                        $diff = $now['mon'] - $bday_month;
1164
                                        if ($diff == 0)
1165
                                        {
1166
                                                $diff = ($now['mday'] - $bday_day < 0) ? 1 : 0;
1167
                                        }
1168
                                        else
1169
                                        {
1170
                                                $diff = ($diff < 0) ? 1 : 0;
1171
                                        }
1172
1173
                                        $user_cache[$poster_id]['age'] = (int) ($now['year'] - $bday_year - $diff);
1174
                                }
1175
                        }
1176
                }
1177
        }
1178
}
1179
$db->sql_freeresult($result);
1180
1181
// Load custom profile fields
1182
if ($config['load_cpf_viewtopic'])
1183
{
1184
        if (!class_exists('custom_profile'))
1185
        {
1186
                include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
1187
        }
1188
        $cp = new custom_profile();
1189
1190
        // Grab all profile fields from users in id cache for later use - similar to the poster cache
1191
        $profile_fields_tmp = $cp->generate_profile_fields_template('grab', $id_cache);
1192
1193
        // filter out fields not to be displayed on viewtopic. Yes, it's a hack, but this shouldn't break any MODs.
1194
        $profile_fields_cache = array();
1195
        foreach ($profile_fields_tmp as $profile_user_id => $profile_fields)
1196
        {
1197
                $profile_fields_cache[$profile_user_id] = array();
1198
                foreach ($profile_fields as $used_ident => $profile_field)
1199
                {
1200
                        if ($profile_field['data']['field_show_on_vt'])
1201
                        {
1202
                                $profile_fields_cache[$profile_user_id][$used_ident] = $profile_field;
1203
                        }
1204
                }
1205
        }
1206
        unset($profile_fields_tmp);
1207
}
1208
1209
// Generate online information for user
1210
if ($config['load_onlinetrack'] && sizeof($id_cache))
1211
{
1212
        $sql = 'SELECT session_user_id, MAX(session_time) as online_time, MIN(session_viewonline) AS viewonline
1213
                FROM ' . SESSIONS_TABLE . '
1214
                WHERE ' . $db->sql_in_set('session_user_id', $id_cache) . '
1215
                GROUP BY session_user_id';
1216
        $result = $db->sql_query($sql);
1217
1218
        $update_time = $config['load_online_time'] * 60;
1219
        while ($row = $db->sql_fetchrow($result))
1220
        {
1221
                $user_cache[$row['session_user_id']]['online'] = (time() - $update_time < $row['online_time'] && (($row['viewonline']) || $auth->acl_get('u_viewonline'))) ? true : false;
1222
        }
1223
        $db->sql_freeresult($result);
1224
}
1225
unset($id_cache);
1226
1227
// Pull attachment data
1228
if (sizeof($attach_list))
1229
{
1230
        if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
1231
        {
1232
                $sql = 'SELECT *
1233
                        FROM ' . ATTACHMENTS_TABLE . '
1234
                        WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
1235
                                AND in_message = 0
1236
                        ORDER BY filetime DESC, post_msg_id ASC';
1237
                $result = $db->sql_query($sql);
1238
1239
                while ($row = $db->sql_fetchrow($result))
1240
                {
1241
                        $attachments[$row['post_msg_id']][] = $row;
1242
                }
1243
                $db->sql_freeresult($result);
1244
1245
                // No attachments exist, but post table thinks they do so go ahead and reset post_attach flags
1246
                if (!sizeof($attachments))
1247
                {
1248
                        $sql = 'UPDATE ' . POSTS_TABLE . '
1249
                                SET post_attachment = 0
1250
                                WHERE ' . $db->sql_in_set('post_id', $attach_list);
1251
                        $db->sql_query($sql);
1252
1253
                        // We need to update the topic indicator too if the complete topic is now without an attachment
1254
                        if (sizeof($rowset) != $total_posts)
1255
                        {
1256
                                // Not all posts are displayed so we query the db to find if there's any attachment for this topic
1257
                                $sql = 'SELECT a.post_msg_id as post_id
1258
                                        FROM ' . ATTACHMENTS_TABLE . ' a, ' . POSTS_TABLE . " p
1259
                                        WHERE p.topic_id = $topic_id
1260
                                                AND p.post_approved = 1
1261
                                                AND p.topic_id = a.topic_id";
1262
                                $result = $db->sql_query_limit($sql, 1);
1263
                                $row = $db->sql_fetchrow($result);
1264
                                $db->sql_freeresult($result);
1265
1266
                                if (!$row)
1267
                                {
1268
                                        $sql = 'UPDATE ' . TOPICS_TABLE . "
1269
                                                SET topic_attachment = 0
1270
                                                WHERE topic_id = $topic_id";
1271
                                        $db->sql_query($sql);
1272
                                }
1273
                        }
1274
                        else
1275
                        {
1276
                                $sql = 'UPDATE ' . TOPICS_TABLE . "
1277
                                        SET topic_attachment = 0
1278
                                        WHERE topic_id = $topic_id";
1279
                                $db->sql_query($sql);
1280
                        }
1281
                }
1282
                else if ($has_attachments && !$topic_data['topic_attachment'])
1283
                {
1284
                        // Topic has approved attachments but its flag is wrong
1285
                        $sql = 'UPDATE ' . TOPICS_TABLE . "
1286
                                SET topic_attachment = 1
1287
                                WHERE topic_id = $topic_id";
1288
                        $db->sql_query($sql);
1289
1290
                        $topic_data['topic_attachment'] = 1;
1291
                }
1292
        }
1293
        else
1294
        {
1295
                $display_notice = true;
1296
        }
1297
}
1298
1299
// Instantiate BBCode if need be
1300
if ($bbcode_bitfield !== '')
1301
{
1302
        $bbcode = new bbcode(base64_encode($bbcode_bitfield));
1303
}
1304
1305
$i_total = sizeof($rowset) - 1;
1306
$prev_post_id = '';
1307
1308
$template->assign_vars(array(
1309
        'S_NUM_POSTS' => sizeof($post_list))
1310
);
1311
1312
// Output the posts
1313
$first_unread = $post_unread = false;
1314
for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
1315
{
1316
        // A non-existing rowset only happens if there was no user present for the entered poster_id
1317
        // This could be a broken posts table.
1318
        if (!isset($rowset[$post_list[$i]]))
1319
        {
1320
                continue;
1321
        }
1322
1323
        $row = $rowset[$post_list[$i]];
1324
        $poster_id = $row['user_id'];
1325
1326
        // End signature parsing, only if needed
1327
        if ($user_cache[$poster_id]['sig'] && $row['enable_sig'] && empty($user_cache[$poster_id]['sig_parsed']))
1328
        {
1329
                $user_cache[$poster_id]['sig'] = censor_text($user_cache[$poster_id]['sig']);
1330
1331
                if ($user_cache[$poster_id]['sig_bbcode_bitfield'])
1332
                {
1333
                        $bbcode->bbcode_second_pass($user_cache[$poster_id]['sig'], $user_cache[$poster_id]['sig_bbcode_uid'], $user_cache[$poster_id]['sig_bbcode_bitfield']);
1334
                }
1335
1336
                $user_cache[$poster_id]['sig'] = bbcode_nl2br($user_cache[$poster_id]['sig']);
1337
                $user_cache[$poster_id]['sig'] = smiley_text($user_cache[$poster_id]['sig']);
1338
                $user_cache[$poster_id]['sig_parsed'] = true;
1339
        }
1340
1341
        // Parse the message and subject
1342
        $message = censor_text($row['post_text']);
1343
1344
        // Second parse bbcode here
1345
        if ($row['bbcode_bitfield'])
1346
        {
1347
                $bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
1348
        }
1349
1350
        $message = bbcode_nl2br($message);
1351
        $message = smiley_text($message);
1352
1353
        if (!empty($attachments[$row['post_id']]))
1354
        {
1355
                parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
1356
        }
1357
1358
        // Replace naughty words such as farty pants
1359
        $row['post_subject'] = censor_text($row['post_subject']);
1360
1361
        // Highlight active words (primarily for search)
1362
        if ($highlight_match)
1363
        {
1364
                $message = preg_replace('#(?!<.*)(?<!\w)(' . $highlight_match . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">\1</span>', $message);
1365
                $row['post_subject'] = preg_replace('#(?!<.*)(?<!\w)(' . $highlight_match . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">\1</span>', $row['post_subject']);
1366
        }
1367
1368
        // Editing information
1369
        if (($row['post_edit_count'] && $config['display_last_edited']) || $row['post_edit_reason'])
1370
        {
1371
                // Get usernames for all following posts if not already stored
1372
                if (!sizeof($post_edit_list) && ($row['post_edit_reason'] || ($row['post_edit_user'] && !isset($user_cache[$row['post_edit_user']]))))
1373
                {
1374
                        // Remove all post_ids already parsed (we do not have to check them)
1375
                        $post_storage_list = (!$store_reverse) ? array_slice($post_list, $i) : array_slice(array_reverse($post_list), $i);
1376
1377
                        $sql = 'SELECT DISTINCT u.user_id, u.username, u.user_colour
1378
                                FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1379
                                WHERE ' . $db->sql_in_set('p.post_id', $post_storage_list) . '
1380
                                        AND p.post_edit_count <> 0
1381
                                        AND p.post_edit_user <> 0
1382
                                        AND p.post_edit_user = u.user_id';
1383
                        $result2 = $db->sql_query($sql);
1384
                        while ($user_edit_row = $db->sql_fetchrow($result2))
1385
                        {
1386
                                $post_edit_list[$user_edit_row['user_id']] = $user_edit_row;
1387
                        }
1388
                        $db->sql_freeresult($result2);
1389
1390
                        unset($post_storage_list);
1391
                }
1392
1393
                if ($row['post_edit_reason'])
1394
                {
1395
                        // User having edited the post also being the post author?
1396
                        if (!$row['post_edit_user'] || $row['post_edit_user'] == $poster_id)
1397
                        {
1398
                                $display_username = get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']);
1399
                        }
1400
                        else
1401
                        {
1402
                                $display_username = get_username_string('full', $row['post_edit_user'], $post_edit_list[$row['post_edit_user']]['username'], $post_edit_list[$row['post_edit_user']]['user_colour']);
1403
                        }
1404
1405
                        $l_edited_by = $user->lang('EDITED_TIMES_TOTAL', (int) $row['post_edit_count'], $display_username, $user->format_date($row['post_edit_time'], false, true));
1406
                }
1407
                else
1408
                {
1409
                        if ($row['post_edit_user'] && !isset($user_cache[$row['post_edit_user']]))
1410
                        {
1411
                                $user_cache[$row['post_edit_user']] = $post_edit_list[$row['post_edit_user']];
1412
                        }
1413
1414
                        // User having edited the post also being the post author?
1415
                        if (!$row['post_edit_user'] || $row['post_edit_user'] == $poster_id)
1416
                        {
1417
                                $display_username = get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']);
1418
                        }
1419
                        else
1420
                        {
1421
                                $display_username = get_username_string('full', $row['post_edit_user'], $user_cache[$row['post_edit_user']]['username'], $user_cache[$row['post_edit_user']]['user_colour']);
1422
                        }
1423
1424
                        $l_edited_by = $user->lang('EDITED_TIMES_TOTAL', (int) $row['post_edit_count'], $display_username, $user->format_date($row['post_edit_time'], false, true));
1425
                }
1426
        }
1427
        else
1428
        {
1429
                $l_edited_by = '';
1430
        }
1431
1432
        // Bump information
1433
        if ($topic_data['topic_bumped'] && $row['post_id'] == $topic_data['topic_last_post_id'] && isset($user_cache[$topic_data['topic_bumper']]) )
1434
        {
1435
                // It is safe to grab the username from the user cache array, we are at the last
1436
                // post and only the topic poster and last poster are allowed to bump.
1437
                // Admins and mods are bound to the above rules too...
1438
                $l_bumped_by = sprintf($user->lang['BUMPED_BY'], $user_cache[$topic_data['topic_bumper']]['username'], $user->format_date($topic_data['topic_last_post_time'], false, true));
1439
        }
1440
        else
1441
        {
1442
                $l_bumped_by = '';
1443
        }
1444
1445
        $cp_row = array();
1446
1447
        //
1448
        if ($config['load_cpf_viewtopic'])
1449
        {
1450
                $cp_row = (isset($profile_fields_cache[$poster_id])) ? $cp->generate_profile_fields_template('show', false, $profile_fields_cache[$poster_id]) : array();
1451
        }
1452
1453
        $post_unread = (isset($topic_tracking_info[$topic_id]) && $row['post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
1454
1455
        $s_first_unread = false;
1456
        if (!$first_unread && $post_unread)
1457
        {
1458
                $s_first_unread = $first_unread = true;
1459
        }
1460
1461
        $edit_allowed = ($user->data['is_registered'] && ($auth->acl_get('m_edit', $forum_id) || (
1462
                $user->data['user_id'] == $poster_id &&
1463
                $auth->acl_get('f_edit', $forum_id) &&
1464
                !$row['post_edit_locked'] &&
1465
                ($row['post_time'] > time() - ($config['edit_time'] * 60) || !$config['edit_time'])
1466
        )));
1467
1468
        $delete_allowed = ($user->data['is_registered'] && ($auth->acl_get('m_delete', $forum_id) || (
1469
                $user->data['user_id'] == $poster_id &&
1470
                $auth->acl_get('f_delete', $forum_id) &&
1471
                $topic_data['topic_last_post_id'] == $row['post_id'] &&
1472
                ($row['post_time'] > time() - ($config['delete_time'] * 60) || !$config['delete_time']) &&
1473
                // we do not want to allow removal of the last post if a moderator locked it!
1474
                !$row['post_edit_locked']
1475
        )));
1476
1477
        //
1478
        $postrow = array(
1479
                'POST_AUTHOR_FULL'                => ($poster_id != ANONYMOUS) ? $user_cache[$poster_id]['author_full'] : get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1480
                'POST_AUTHOR_COLOUR'        => ($poster_id != ANONYMOUS) ? $user_cache[$poster_id]['author_colour'] : get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1481
                'POST_AUTHOR'                        => ($poster_id != ANONYMOUS) ? $user_cache[$poster_id]['author_username'] : get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1482
                'U_POST_AUTHOR'                        => ($poster_id != ANONYMOUS) ? $user_cache[$poster_id]['author_profile'] : get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1483
1484
                'RANK_TITLE'                => $user_cache[$poster_id]['rank_title'],
1485
                'RANK_IMG'                        => $user_cache[$poster_id]['rank_image'],
1486
                'RANK_IMG_SRC'                => $user_cache[$poster_id]['rank_image_src'],
1487
                'POSTER_JOINED'                => $user_cache[$poster_id]['joined'],
1488
                'POSTER_POSTS'                => $user_cache[$poster_id]['posts'],
1489
                'POSTER_FROM'                => $user_cache[$poster_id]['from'],
1490
                'POSTER_AVATAR'                => $user_cache[$poster_id]['avatar'],
1491
                'POSTER_WARNINGS'        => $user_cache[$poster_id]['warnings'],
1492
                'POSTER_AGE'                => $user_cache[$poster_id]['age'],
1493
1494
                'POST_DATE'                        => $user->format_date($row['post_time'], false, ($view == 'print') ? true : false),
1495
                'POST_SUBJECT'                => $row['post_subject'],
1496
                'MESSAGE'                        => $message,
1497
                'SIGNATURE'                        => ($row['enable_sig']) ? $user_cache[$poster_id]['sig'] : '',
1498
                'EDITED_MESSAGE'        => $l_edited_by,
1499
                'EDIT_REASON'                => $row['post_edit_reason'],
1500
                'BUMPED_MESSAGE'        => $l_bumped_by,
1501
1502
                'MINI_POST_IMG'                        => ($post_unread) ? $user->img('icon_post_target_unread', 'UNREAD_POST') : $user->img('icon_post_target', 'POST'),
1503
                'POST_ICON_IMG'                        => ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['img'] : '',
1504
                'POST_ICON_IMG_WIDTH'        => ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['width'] : '',
1505
                'POST_ICON_IMG_HEIGHT'        => ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['height'] : '',
1506
                'ICQ_STATUS_IMG'                => $user_cache[$poster_id]['icq_status_img'],
1507
                'ONLINE_IMG'                        => ($poster_id == ANONYMOUS || !$config['load_onlinetrack']) ? '' : (($user_cache[$poster_id]['online']) ? $user->img('icon_user_online', 'ONLINE') : $user->img('icon_user_offline', 'OFFLINE')),
1508
                'S_ONLINE'                                => ($poster_id == ANONYMOUS || !$config['load_onlinetrack']) ? false : (($user_cache[$poster_id]['online']) ? true : false),
1509
1510
                'U_EDIT'                        => ($edit_allowed) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=edit&amp;f=$forum_id&amp;p={$row['post_id']}") : '',
1511
                'U_QUOTE'                        => ($auth->acl_get('f_reply', $forum_id)) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=quote&amp;f=$forum_id&amp;p={$row['post_id']}") : '',
1512
                'U_INFO'                        => ($auth->acl_get('m_info', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=post_details&amp;f=$forum_id&amp;p=" . $row['post_id'], true, $user->session_id) : '',
1513
                'U_DELETE'                        => ($delete_allowed) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=delete&amp;f=$forum_id&amp;p={$row['post_id']}") : '',
1514
1515
                'U_PROFILE'                => $user_cache[$poster_id]['profile'],
1516
                'U_SEARCH'                => $user_cache[$poster_id]['search'],
1517
                'U_PM'                        => ($poster_id != ANONYMOUS && $config['allow_privmsg'] && $auth->acl_get('u_sendpm') && ($user_cache[$poster_id]['allow_pm'] || $auth->acl_gets('a_', 'm_') || $auth->acl_getf_global('m_'))) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;mode=compose&amp;action=quotepost&amp;p=' . $row['post_id']) : '',
1518
                'U_EMAIL'                => $user_cache[$poster_id]['email'],
1519
                'U_WWW'                        => $user_cache[$poster_id]['www'],
1520
                'U_ICQ'                        => $user_cache[$poster_id]['icq'],
1521
                'U_AIM'                        => $user_cache[$poster_id]['aim'],
1522
                'U_MSN'                        => $user_cache[$poster_id]['msn'],
1523
                'U_YIM'                        => $user_cache[$poster_id]['yim'],
1524
                'U_JABBER'                => $user_cache[$poster_id]['jabber'],
1525
1526
                'U_REPORT'                        => ($auth->acl_get('f_report', $forum_id)) ? append_sid("{$phpbb_root_path}report.$phpEx", 'f=' . $forum_id . '&amp;p=' . $row['post_id']) : '',
1527
                'U_MCP_REPORT'                => ($auth->acl_get('m_report', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=report_details&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
1528
                'U_MCP_APPROVE'                => ($auth->acl_get('m_approve', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=approve_details&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
1529
                'U_MINI_POST'                => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id']) . '#p' . $row['post_id'],
1530
                'U_NEXT_POST_ID'        => ($i < $i_total && isset($rowset[$post_list[$i + 1]])) ? $rowset[$post_list[$i + 1]]['post_id'] : '',
1531
                'U_PREV_POST_ID'        => $prev_post_id,
1532
                'U_NOTES'                        => ($auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=notes&amp;mode=user_notes&amp;u=' . $poster_id, true, $user->session_id) : '',
1533
                'U_WARN'                        => ($auth->acl_get('m_warn') && $poster_id != $user->data['user_id'] && $poster_id != ANONYMOUS) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=warn&amp;mode=warn_post&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
1534
1535
                'POST_ID'                        => $row['post_id'],
1536
                'POST_NUMBER'                => $i + $start + 1,
1537
                'POSTER_ID'                        => $poster_id,
1538
1539
                'S_HAS_ATTACHMENTS'        => (!empty($attachments[$row['post_id']])) ? true : false,
1540
                'S_POST_UNAPPROVED'        => ($row['post_approved']) ? false : true,
1541
                'S_POST_REPORTED'        => ($row['post_reported'] && $auth->acl_get('m_report', $forum_id)) ? true : false,
1542
                'S_DISPLAY_NOTICE'        => $display_notice && $row['post_attachment'],
1543
                'S_FRIEND'                        => ($row['friend']) ? true : false,
1544
                'S_UNREAD_POST'                => $post_unread,
1545
                'S_FIRST_UNREAD'        => $s_first_unread,
1546
                'S_CUSTOM_FIELDS'        => (isset($cp_row['row']) && sizeof($cp_row['row'])) ? true : false,
1547
                'S_TOPIC_POSTER'        => ($topic_data['topic_poster'] == $poster_id) ? true : false,
1548
1549
                'S_IGNORE_POST'                => ($row['hide_post']) ? true : false,
1550
                'L_IGNORE_POST'                => ($row['hide_post']) ? sprintf($user->lang['POST_BY_FOE'], get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']), '<a href="' . $viewtopic_url . "&amp;p={$row['post_id']}&amp;view=show#p{$row['post_id']}" . '">', '</a>') : '',
1551
        );
1552
1553
        if (isset($cp_row['row']) && sizeof($cp_row['row']))
1554
        {
1555
                $postrow = array_merge($postrow, $cp_row['row']);
1556
        }
1557
1558
        // Dump vars into template
1559
        $template->assign_block_vars('postrow', $postrow);
1560
1561
        if (!empty($cp_row['blockrow']))
1562
        {
1563
                foreach ($cp_row['blockrow'] as $field_data)
1564
                {
1565
                        $template->assign_block_vars('postrow.custom_fields', $field_data);
1566
                }
1567
        }
1568
1569
        // Display not already displayed Attachments for this post, we already parsed them. ;)
1570
        if (!empty($attachments[$row['post_id']]))
1571
        {
1572
                foreach ($attachments[$row['post_id']] as $attachment)
1573
                {
1574
                        $template->assign_block_vars('postrow.attachment', array(
1575
                                'DISPLAY_ATTACHMENT'        => $attachment)
1576
                        );
1577
                }
1578
        }
1579
1580
        $prev_post_id = $row['post_id'];
1581
1582
        unset($rowset[$post_list[$i]]);
1583
        unset($attachments[$row['post_id']]);
1584
}
1585
unset($rowset, $user_cache);
1586
1587
// Update topic view and if necessary attachment view counters ... but only for humans and if this is the first 'page view'
1588
if (isset($user->data['session_page']) && !$user->data['is_bot'] && (strpos($user->data['session_page'], '&t=' . $topic_id) === false || isset($user->data['session_created'])))
1589
{
1590
        $sql = 'UPDATE ' . TOPICS_TABLE . '
1591
                SET topic_views = topic_views + 1, topic_last_view_time = ' . time() . "
1592
                WHERE topic_id = $topic_id";
1593
        $db->sql_query($sql);
1594
1595
        // Update the attachment download counts
1596
        if (sizeof($update_count))
1597
        {
1598
                $sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
1599
                        SET download_count = download_count + 1
1600
                        WHERE ' . $db->sql_in_set('attach_id', array_unique($update_count));
1601
                $db->sql_query($sql);
1602
        }
1603
}
1604
1605
// Only mark topic if it's currently unread. Also make sure we do not set topic tracking back if earlier pages are viewed.
1606
if (isset($topic_tracking_info[$topic_id]) && $topic_data['topic_last_post_time'] > $topic_tracking_info[$topic_id] && $max_post_time > $topic_tracking_info[$topic_id])
1607
{
1608
        markread('topic', $forum_id, $topic_id, $max_post_time);
1609
1610
        // Update forum info
1611
        $all_marked_read = update_forum_tracking_info($forum_id, $topic_data['forum_last_post_time'], (isset($topic_data['forum_mark_time'])) ? $topic_data['forum_mark_time'] : false, false);
1612
}
1613
else
1614
{
1615
        $all_marked_read = true;
1616
}
1617
1618
// If there are absolutely no more unread posts in this forum and unread posts shown, we can savely show the #unread link
1619
if ($all_marked_read)
1620
{
1621
        if ($post_unread)
1622
        {
1623
                $template->assign_vars(array(
1624
                        'U_VIEW_UNREAD_POST'        => '#unread',
1625
                ));
1626
        }
1627
        else if (isset($topic_tracking_info[$topic_id]) && $topic_data['topic_last_post_time'] > $topic_tracking_info[$topic_id])
1628
        {
1629
                $template->assign_vars(array(
1630
                        'U_VIEW_UNREAD_POST'        => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
1631
                ));
1632
        }
1633
}
1634
else if (!$all_marked_read)
1635
{
1636
        $last_page = ((floor($start / $config['posts_per_page']) + 1) == max(ceil($total_posts / $config['posts_per_page']), 1)) ? true : false;
1637
1638
        // What can happen is that we are at the last displayed page. If so, we also display the #unread link based in $post_unread
1639
        if ($last_page && $post_unread)
1640
        {
1641
                $template->assign_vars(array(
1642
                        'U_VIEW_UNREAD_POST'        => '#unread',
1643
                ));
1644
        }
1645
        else if (!$last_page)
1646
        {
1647
                $template->assign_vars(array(
1648
                        'U_VIEW_UNREAD_POST'        => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
1649
                ));
1650
        }
1651
}
1652
1653
// let's set up quick_reply
1654
$s_quick_reply = false;
1655
if ($user->data['is_registered'] && $config['allow_quick_reply'] && ($topic_data['forum_flags'] & FORUM_FLAG_QUICK_REPLY) && $auth->acl_get('f_reply', $forum_id))
1656
{
1657
        // Quick reply enabled forum
1658
        $s_quick_reply = (($topic_data['forum_status'] == ITEM_UNLOCKED && $topic_data['topic_status'] == ITEM_UNLOCKED) || $auth->acl_get('m_edit', $forum_id)) ? true : false;
1659
}
1660
1661
if ($s_can_vote || $s_quick_reply)
1662
{
1663
        add_form_key('posting');
1664
1665
        if ($s_quick_reply)
1666
        {
1667
                $s_attach_sig        = $config['allow_sig'] && $user->optionget('attachsig') && $auth->acl_get('f_sigs', $forum_id) && $auth->acl_get('u_sig');
1668
                $s_smilies                = $config['allow_smilies'] && $user->optionget('smilies') && $auth->acl_get('f_smilies', $forum_id);
1669
                $s_bbcode                = $config['allow_bbcode'] && $user->optionget('bbcode') && $auth->acl_get('f_bbcode', $forum_id);
1670
                $s_notify                = $config['allow_topic_notify'] && ($user->data['user_notify'] || $s_watching_topic['is_watching']);
1671
1672
                $qr_hidden_fields = array(
1673
                        'topic_cur_post_id'                => (int) $topic_data['topic_last_post_id'],
1674
                        'lastclick'                                => (int) time(),
1675
                        'topic_id'                                => (int) $topic_data['topic_id'],
1676
                        'forum_id'                                => (int) $forum_id,
1677
                );
1678
1679
                // Originally we use checkboxes and check with isset(), so we only provide them if they would be checked
1680
                (!$s_bbcode)                                        ? $qr_hidden_fields['disable_bbcode'] = 1                : true;
1681
                (!$s_smilies)                                        ? $qr_hidden_fields['disable_smilies'] = 1                : true;
1682
                (!$config['allow_post_links'])        ? $qr_hidden_fields['disable_magic_url'] = 1        : true;
1683
                ($s_attach_sig)                                        ? $qr_hidden_fields['attach_sig'] = 1                        : true;
1684
                ($s_notify)                                                ? $qr_hidden_fields['notify'] = 1                                : true;
1685
                ($topic_data['topic_status'] == ITEM_LOCKED) ? $qr_hidden_fields['lock_topic'] = 1 : true;
1686
1687
                $template->assign_vars(array(
1688
                        'S_QUICK_REPLY'                        => true,
1689
                        'U_QR_ACTION'                        => append_sid("{$phpbb_root_path}posting.$phpEx", "mode=reply&amp;f=$forum_id&amp;t=$topic_id"),
1690
                        'QR_HIDDEN_FIELDS'                => build_hidden_fields($qr_hidden_fields),
1691
                        'SUBJECT'                                => 'Re: ' . censor_text($topic_data['topic_title']),
1692
                ));
1693
        }
1694
}
1695
// now I have the urge to wash my hands :(
1696
1697
1698
// We overwrite $_REQUEST['f'] if there is no forum specified
1699
// to be able to display the correct online list.
1700
// One downside is that the user currently viewing this topic/post is not taken into account.
1701
if (!request_var('f', 0))
1702
{
1703
        $request->overwrite('f', $forum_id);
1704
}
1705
1706
// We need to do the same with the topic_id. See #53025.
1707
if (!request_var('t', 0) && !empty($topic_id))
1708
{
1709
        $request->overwrite('t', $topic_id);
1710
}
1711
1712
// Output the page
1713
page_header($topic_data['topic_title'] . ($start ? ' - ' . sprintf($user->lang['PAGE_TITLE_NUMBER'], floor($start / $config['posts_per_page']) + 1) : ''), true, $forum_id);
1714
1715
$template->set_filenames(array(
1716
        'body' => ($view == 'print') ? 'viewtopic_print.html' : 'viewtopic_body.html')
1717
);
1718
make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"), $forum_id);
1719
1720
page_footer();