-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewtopic.php
executable file
·2380 lines (2064 loc) · 89.5 KB
/
viewtopic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
// Start session management
$user->session_begin();
$auth->acl($user->data);
// Initial var setup
$forum_id = $request->variable('f', 0);
$topic_id = $request->variable('t', 0);
$post_id = $request->variable('p', 0);
$voted_id = $request->variable('vote_id', array('' => 0));
$voted_id = (count($voted_id) > 1) ? array_unique($voted_id) : $voted_id;
$start = $request->variable('start', 0);
$view = $request->variable('view', '');
$default_sort_days = (!empty($user->data['user_post_show_days'])) ? $user->data['user_post_show_days'] : 0;
$default_sort_key = (!empty($user->data['user_post_sortby_type'])) ? $user->data['user_post_sortby_type'] : 't';
$default_sort_dir = (!empty($user->data['user_post_sortby_dir'])) ? $user->data['user_post_sortby_dir'] : 'a';
$sort_days = $request->variable('st', $default_sort_days);
$sort_key = $request->variable('sk', $default_sort_key);
$sort_dir = $request->variable('sd', $default_sort_dir);
$update = $request->variable('update', false);
/* @var $pagination \phpbb\pagination */
$pagination = $phpbb_container->get('pagination');
$s_can_vote = false;
/**
* @todo normalize?
*/
$hilit_words = $request->variable('hilit', '', true);
// Do we have a topic or post id?
if (!$topic_id && !$post_id)
{
trigger_error('NO_TOPIC');
}
/* @var $phpbb_content_visibility \phpbb\content_visibility */
$phpbb_content_visibility = $phpbb_container->get('content.visibility');
// Find topic id if user requested a newer or older topic
if ($view && !$post_id)
{
if (!$forum_id)
{
$sql = 'SELECT forum_id
FROM ' . TOPICS_TABLE . "
WHERE topic_id = $topic_id";
$result = $db->sql_query($sql);
$forum_id = (int) $db->sql_fetchfield('forum_id');
$db->sql_freeresult($result);
if (!$forum_id)
{
trigger_error('NO_TOPIC');
}
}
if ($view == 'unread')
{
// Get topic tracking info
$topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
$topic_last_read = (isset($topic_tracking_info[$topic_id])) ? $topic_tracking_info[$topic_id] : 0;
$sql = 'SELECT post_id, topic_id, forum_id
FROM ' . POSTS_TABLE . "
WHERE topic_id = $topic_id
AND " . $phpbb_content_visibility->get_visibility_sql('post', $forum_id) . "
AND post_time > $topic_last_read
AND forum_id = $forum_id
ORDER BY post_time ASC, post_id ASC";
$result = $db->sql_query_limit($sql, 1);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
if (!$row)
{
$sql = 'SELECT topic_last_post_id as post_id, topic_id, forum_id
FROM ' . TOPICS_TABLE . '
WHERE topic_id = ' . $topic_id;
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
}
if (!$row)
{
// Setup user environment so we can process lang string
$user->setup('viewtopic');
trigger_error('NO_TOPIC');
}
$post_id = $row['post_id'];
$topic_id = $row['topic_id'];
}
else if ($view == 'next' || $view == 'previous')
{
$sql_condition = ($view == 'next') ? '>' : '<';
$sql_ordering = ($view == 'next') ? 'ASC' : 'DESC';
$sql = 'SELECT forum_id, topic_last_post_time
FROM ' . TOPICS_TABLE . '
WHERE topic_id = ' . $topic_id;
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
if (!$row)
{
$user->setup('viewtopic');
// OK, the topic doesn't exist. This error message is not helpful, but technically correct.
trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
}
else
{
$sql = 'SELECT topic_id, forum_id
FROM ' . TOPICS_TABLE . '
WHERE forum_id = ' . $row['forum_id'] . "
AND topic_moved_id = 0
AND topic_last_post_time $sql_condition {$row['topic_last_post_time']}
AND " . $phpbb_content_visibility->get_visibility_sql('topic', $row['forum_id']) . "
ORDER BY topic_last_post_time $sql_ordering, topic_last_post_id $sql_ordering";
$result = $db->sql_query_limit($sql, 1);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
if (!$row)
{
$sql = 'SELECT forum_style
FROM ' . FORUMS_TABLE . "
WHERE forum_id = $forum_id";
$result = $db->sql_query($sql);
$forum_style = (int) $db->sql_fetchfield('forum_style');
$db->sql_freeresult($result);
$user->setup('viewtopic', $forum_style);
trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
}
else
{
$topic_id = $row['topic_id'];
$forum_id = $row['forum_id'];
}
}
}
if (isset($row) && $row['forum_id'])
{
$forum_id = $row['forum_id'];
}
}
// This rather complex gaggle of code handles querying for topics but
// also allows for direct linking to a post (and the calculation of which
// page the post is on and the correct display of viewtopic)
$sql_array = array(
'SELECT' => 't.*, f.*',
'FROM' => array(FORUMS_TABLE => 'f'),
);
// The FROM-Order is quite important here, else t.* columns can not be correctly bound.
if ($post_id)
{
$sql_array['SELECT'] .= ', p.post_visibility, p.post_time, p.post_id';
$sql_array['FROM'][POSTS_TABLE] = 'p';
}
// Topics table need to be the last in the chain
$sql_array['FROM'][TOPICS_TABLE] = 't';
if ($user->data['is_registered'])
{
$sql_array['SELECT'] .= ', tw.notify_status';
$sql_array['LEFT_JOIN'] = array();
$sql_array['LEFT_JOIN'][] = array(
'FROM' => array(TOPICS_WATCH_TABLE => 'tw'),
'ON' => 'tw.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tw.topic_id'
);
if ($config['allow_bookmarks'])
{
$sql_array['SELECT'] .= ', bm.topic_id as bookmarked';
$sql_array['LEFT_JOIN'][] = array(
'FROM' => array(BOOKMARKS_TABLE => 'bm'),
'ON' => 'bm.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = bm.topic_id'
);
}
if ($config['load_db_lastread'])
{
$sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
$sql_array['LEFT_JOIN'][] = array(
'FROM' => array(TOPICS_TRACK_TABLE => 'tt'),
'ON' => 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
);
$sql_array['LEFT_JOIN'][] = array(
'FROM' => array(FORUMS_TRACK_TABLE => 'ft'),
'ON' => 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
);
}
}
if (!$post_id)
{
$sql_array['WHERE'] = "t.topic_id = $topic_id";
}
else
{
$sql_array['WHERE'] = "p.post_id = $post_id AND t.topic_id = p.topic_id";
}
$sql_array['WHERE'] .= ' AND f.forum_id = t.forum_id';
$sql = $db->sql_build_query('SELECT', $sql_array);
$result = $db->sql_query($sql);
$topic_data = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
// link to unapproved post or incorrect link
if (!$topic_data)
{
// If post_id was submitted, we try at least to display the topic as a last resort...
if ($post_id && $topic_id)
{
redirect(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t=$topic_id" . (($forum_id) ? "&f=$forum_id" : '')));
}
trigger_error('NO_TOPIC');
}
$forum_id = (int) $topic_data['forum_id'];
/**
* Modify the forum ID to handle the correct display of viewtopic if needed
*
* @event core.viewtopic_modify_forum_id
* @var string forum_id forum ID
* @var array topic_data array of topic's data
* @since 3.2.5-RC1
*/
$vars = array(
'forum_id',
'topic_data',
);
extract($phpbb_dispatcher->trigger_event('core.viewtopic_modify_forum_id', compact($vars)));
// If the request is missing the f parameter, the forum id in the user session data is 0 at the moment.
// Let's fix that now so that the user can't hide from the forum's Who Is Online list.
$user->page['forum'] = $forum_id;
// Now we know the forum_id and can check the permissions
if (!$phpbb_content_visibility->is_visible('topic', $forum_id, $topic_data))
{
trigger_error('NO_TOPIC');
}
// This is for determining where we are (page)
if ($post_id)
{
// are we where we are supposed to be?
if (($topic_data['post_visibility'] == ITEM_UNAPPROVED || $topic_data['post_visibility'] == ITEM_REAPPROVE) && !$auth->acl_get('m_approve', $topic_data['forum_id']))
{
// If post_id was submitted, we try at least to display the topic as a last resort...
if ($topic_id)
{
redirect(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t=$topic_id" . (($forum_id) ? "&f=$forum_id" : '')));
}
trigger_error('NO_TOPIC');
}
if ($post_id == $topic_data['topic_first_post_id'] || $post_id == $topic_data['topic_last_post_id'])
{
$check_sort = ($post_id == $topic_data['topic_first_post_id']) ? 'd' : 'a';
if ($sort_dir == $check_sort)
{
$topic_data['prev_posts'] = $phpbb_content_visibility->get_count('topic_posts', $topic_data, $forum_id) - 1;
}
else
{
$topic_data['prev_posts'] = 0;
}
}
else
{
$sql = 'SELECT COUNT(p.post_id) AS prev_posts
FROM ' . POSTS_TABLE . " p
WHERE p.topic_id = {$topic_data['topic_id']}
AND " . $phpbb_content_visibility->get_visibility_sql('post', $forum_id, 'p.');
if ($sort_dir == 'd')
{
$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']}))";
}
else
{
$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']}))";
}
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
$topic_data['prev_posts'] = $row['prev_posts'] - 1;
}
}
$topic_id = (int) $topic_data['topic_id'];
$topic_replies = $phpbb_content_visibility->get_count('topic_posts', $topic_data, $forum_id) - 1;
// Check sticky/announcement/global time limit
if (($topic_data['topic_type'] != POST_NORMAL) && $topic_data['topic_time_limit'] && ($topic_data['topic_time'] + $topic_data['topic_time_limit']) < time())
{
$sql = 'UPDATE ' . TOPICS_TABLE . '
SET topic_type = ' . POST_NORMAL . ', topic_time_limit = 0
WHERE topic_id = ' . $topic_id;
$db->sql_query($sql);
$topic_data['topic_type'] = POST_NORMAL;
$topic_data['topic_time_limit'] = 0;
}
// Setup look and feel
$user->setup('viewtopic', $topic_data['forum_style']);
if ($view == 'print' && !$auth->acl_get('f_print', $forum_id))
{
send_status_line(403, 'Forbidden');
trigger_error('NO_AUTH_PRINT_TOPIC');
}
$overrides_f_read_check = false;
$overrides_forum_password_check = false;
$topic_tracking_info = isset($topic_tracking_info) ? $topic_tracking_info : null;
/**
* Event to apply extra permissions and to override original phpBB's f_read permission and forum password check
* on viewtopic access
*
* @event core.viewtopic_before_f_read_check
* @var int forum_id The forum id from where the topic belongs
* @var int topic_id The id of the topic the user tries to access
* @var int post_id The id of the post the user tries to start viewing at.
* It may be 0 for none given.
* @var array topic_data All the information from the topic and forum tables for this topic
* It includes posts information if post_id is not 0
* @var bool overrides_f_read_check Set true to remove f_read check afterwards
* @var bool overrides_forum_password_check Set true to remove forum_password check afterwards
* @var array topic_tracking_info Information upon calling get_topic_tracking()
* Set it to NULL to allow auto-filling later.
* Set it to an array to override original data.
* @since 3.1.3-RC1
*/
$vars = array(
'forum_id',
'topic_id',
'post_id',
'topic_data',
'overrides_f_read_check',
'overrides_forum_password_check',
'topic_tracking_info',
);
extract($phpbb_dispatcher->trigger_event('core.viewtopic_before_f_read_check', compact($vars)));
// Start auth check
if (!$overrides_f_read_check && !$auth->acl_get('f_read', $forum_id))
{
if ($user->data['user_id'] != ANONYMOUS)
{
send_status_line(403, 'Forbidden');
trigger_error('SORRY_AUTH_READ');
}
login_box('', $user->lang['LOGIN_VIEWFORUM']);
}
// Forum is passworded ... check whether access has been granted to this
// user this session, if not show login box
if (!$overrides_forum_password_check && $topic_data['forum_password'])
{
login_forum_box($topic_data);
}
// Redirect to login upon emailed notification links if user is not logged in.
if (isset($_GET['e']) && $user->data['user_id'] == ANONYMOUS)
{
login_box(build_url('e') . '#unread', $user->lang['LOGIN_NOTIFY_TOPIC']);
}
// What is start equal to?
if ($post_id)
{
$start = floor(($topic_data['prev_posts']) / $config['posts_per_page']) * $config['posts_per_page'];
}
// Get topic tracking info
if (!isset($topic_tracking_info))
{
$topic_tracking_info = array();
// Get topic tracking info
if ($config['load_db_lastread'] && $user->data['is_registered'])
{
$tmp_topic_data = array($topic_id => $topic_data);
$topic_tracking_info = get_topic_tracking($forum_id, $topic_id, $tmp_topic_data, array($forum_id => $topic_data['forum_mark_time']));
unset($tmp_topic_data);
}
else if ($config['load_anon_lastread'] || $user->data['is_registered'])
{
$topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
}
}
// Post ordering options
$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']);
$sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
$sort_by_sql = array('a' => array('u.username_clean', 'p.post_id'), 't' => array('p.post_time', 'p.post_id'), 's' => array('p.post_subject', 'p.post_id'));
$join_user_sql = array('a' => true, 't' => false, 's' => false);
$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
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);
// Obtain correct post count and ordering SQL if user has
// requested anything different
if ($sort_days)
{
$min_post_time = time() - ($sort_days * 86400);
$sql = 'SELECT COUNT(post_id) AS num_posts
FROM ' . POSTS_TABLE . "
WHERE topic_id = $topic_id
AND post_time >= $min_post_time
AND " . $phpbb_content_visibility->get_visibility_sql('post', $forum_id);
$result = $db->sql_query($sql);
$total_posts = (int) $db->sql_fetchfield('num_posts');
$db->sql_freeresult($result);
$limit_posts_time = "AND p.post_time >= $min_post_time ";
if (isset($_POST['sort']))
{
$start = 0;
}
}
else
{
$total_posts = $topic_replies + 1;
$limit_posts_time = '';
}
// Was a highlight request part of the URI?
$highlight_match = $highlight = '';
if ($hilit_words)
{
$highlight_match = phpbb_clean_search_string($hilit_words);
$highlight = urlencode($highlight_match);
$highlight_match = str_replace('\*', '\w+?', preg_quote($highlight_match, '#'));
$highlight_match = preg_replace('#(?<=^|\s)\\\\w\*\?(?=\s|$)#', '\w+?', $highlight_match);
$highlight_match = str_replace(' ', '|', $highlight_match);
}
// Make sure $start is set to the last page if it exceeds the amount
$start = $pagination->validate_start($start, $config['posts_per_page'], $total_posts);
// General Viewtopic URL for return links
$viewtopic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&t=$topic_id" . (($start == 0) ? '' : "&start=$start") . ((strlen($u_sort_param)) ? "&$u_sort_param" : '') . (($highlight_match) ? "&hilit=$highlight" : ''));
// Are we watching this topic?
$s_watching_topic = array(
'link' => '',
'link_toggle' => '',
'title' => '',
'title_toggle' => '',
'is_watching' => false,
);
if ($config['allow_topic_notify'])
{
$notify_status = (isset($topic_data['notify_status'])) ? $topic_data['notify_status'] : null;
watch_topic_forum('topic', $s_watching_topic, $user->data['user_id'], $forum_id, $topic_id, $notify_status, $start, $topic_data['topic_title']);
// Reset forum notification if forum notify is set
if ($config['allow_forum_notify'] && $auth->acl_get('f_subscribe', $forum_id))
{
$s_watching_forum = $s_watching_topic;
watch_topic_forum('forum', $s_watching_forum, $user->data['user_id'], $forum_id, 0);
}
}
/**
* Event to modify highlight.
*
* @event core.viewtopic_highlight_modify
* @var string highlight String to be highlighted
* @var string highlight_match Highlight string to be used in preg_replace
* @var array topic_data Topic data
* @var int start Pagination start
* @var int total_posts Number of posts
* @var string viewtopic_url Current viewtopic URL
* @since 3.1.11-RC1
*/
$vars = array(
'highlight',
'highlight_match',
'topic_data',
'start',
'total_posts',
'viewtopic_url',
);
extract($phpbb_dispatcher->trigger_event('core.viewtopic_highlight_modify', compact($vars)));
// Bookmarks
if ($config['allow_bookmarks'] && $user->data['is_registered'] && $request->variable('bookmark', 0))
{
if (check_link_hash($request->variable('hash', ''), "topic_$topic_id"))
{
if (!$topic_data['bookmarked'])
{
$sql = 'INSERT INTO ' . BOOKMARKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
'user_id' => $user->data['user_id'],
'topic_id' => $topic_id,
));
$db->sql_query($sql);
}
else
{
$sql = 'DELETE FROM ' . BOOKMARKS_TABLE . "
WHERE user_id = {$user->data['user_id']}
AND topic_id = $topic_id";
$db->sql_query($sql);
}
$message = (($topic_data['bookmarked']) ? $user->lang['BOOKMARK_REMOVED'] : $user->lang['BOOKMARK_ADDED']);
if (!$request->is_ajax())
{
$message .= '<br /><br />' . $user->lang('RETURN_TOPIC', '<a href="' . $viewtopic_url . '">', '</a>');
}
}
else
{
$message = $user->lang['BOOKMARK_ERR'];
if (!$request->is_ajax())
{
$message .= '<br /><br />' . $user->lang('RETURN_TOPIC', '<a href="' . $viewtopic_url . '">', '</a>');
}
}
meta_refresh(3, $viewtopic_url);
trigger_error($message);
}
// Grab ranks
$ranks = $cache->obtain_ranks();
// Grab icons
$icons = $cache->obtain_icons();
// Grab extensions
$extensions = array();
if ($topic_data['topic_attachment'])
{
$extensions = $cache->obtain_attach_extensions($forum_id);
}
// Forum rules listing
$s_forum_rules = '';
gen_forum_auth_level('topic', $forum_id, $topic_data['forum_status']);
// Quick mod tools
$allow_change_type = ($auth->acl_get('m_', $forum_id) || ($user->data['is_registered'] && $user->data['user_id'] == $topic_data['topic_poster'])) ? true : false;
$s_quickmod_action = append_sid(
"{$phpbb_root_path}mcp.$phpEx",
array(
'f' => $forum_id,
't' => $topic_id,
'start' => $start,
'quickmod' => 1,
'redirect' => urlencode(str_replace('&', '&', $viewtopic_url)),
),
true,
$user->session_id
);
$quickmod_array = array(
// 'key' => array('LANG_KEY', $userHasPermissions),
'lock' => array('LOCK_TOPIC', ($topic_data['topic_status'] == ITEM_UNLOCKED) && ($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']))),
'unlock' => array('UNLOCK_TOPIC', ($topic_data['topic_status'] != ITEM_UNLOCKED) && ($auth->acl_get('m_lock', $forum_id))),
'delete_topic' => array('DELETE_TOPIC', ($auth->acl_get('m_delete', $forum_id) || (($topic_data['topic_visibility'] != ITEM_DELETED) && $auth->acl_get('m_softdelete', $forum_id)))),
'restore_topic' => array('RESTORE_TOPIC', (($topic_data['topic_visibility'] == ITEM_DELETED) && $auth->acl_get('m_approve', $forum_id))),
'move' => array('MOVE_TOPIC', $auth->acl_get('m_move', $forum_id) && $topic_data['topic_status'] != ITEM_MOVED),
'split' => array('SPLIT_TOPIC', $auth->acl_get('m_split', $forum_id)),
'merge' => array('MERGE_POSTS', $auth->acl_get('m_merge', $forum_id)),
'merge_topic' => array('MERGE_TOPIC', $auth->acl_get('m_merge', $forum_id)),
'fork' => array('FORK_TOPIC', $auth->acl_get('m_move', $forum_id)),
'make_normal' => array('MAKE_NORMAL', ($allow_change_type && $auth->acl_gets('f_sticky', 'f_announce', 'f_announce_global', $forum_id) && $topic_data['topic_type'] != POST_NORMAL)),
'make_sticky' => array('MAKE_STICKY', ($allow_change_type && $auth->acl_get('f_sticky', $forum_id) && $topic_data['topic_type'] != POST_STICKY)),
'make_announce' => array('MAKE_ANNOUNCE', ($allow_change_type && $auth->acl_get('f_announce', $forum_id) && $topic_data['topic_type'] != POST_ANNOUNCE)),
'make_global' => array('MAKE_GLOBAL', ($allow_change_type && $auth->acl_get('f_announce_global', $forum_id) && $topic_data['topic_type'] != POST_GLOBAL)),
'topic_logs' => array('VIEW_TOPIC_LOGS', $auth->acl_get('m_', $forum_id)),
);
/**
* Event to modify data in the quickmod_array before it gets sent to the
* phpbb_add_quickmod_option function.
*
* @event core.viewtopic_add_quickmod_option_before
* @var int forum_id Forum ID
* @var int post_id Post ID
* @var array quickmod_array Array with quick moderation options data
* @var array topic_data Array with topic data
* @var int topic_id Topic ID
* @var array topic_tracking_info Array with topic tracking data
* @var string viewtopic_url URL to the topic page
* @var bool allow_change_type Topic change permissions check
* @since 3.1.9-RC1
*/
$vars = array(
'forum_id',
'post_id',
'quickmod_array',
'topic_data',
'topic_id',
'topic_tracking_info',
'viewtopic_url',
'allow_change_type',
);
extract($phpbb_dispatcher->trigger_event('core.viewtopic_add_quickmod_option_before', compact($vars)));
foreach ($quickmod_array as $option => $qm_ary)
{
if (!empty($qm_ary[1]))
{
phpbb_add_quickmod_option($s_quickmod_action, $option, $qm_ary[0]);
}
}
// Navigation links
generate_forum_nav($topic_data);
// Forum Rules
generate_forum_rules($topic_data);
// Moderators
$forum_moderators = array();
if ($config['load_moderators'])
{
get_moderators($forum_moderators, $forum_id);
}
// This is only used for print view so ...
$server_path = (!$view) ? $phpbb_root_path : generate_board_url() . '/';
// Replace naughty words in title
$topic_data['topic_title'] = censor_text($topic_data['topic_title']);
$s_search_hidden_fields = array(
't' => $topic_id,
'sf' => 'msgonly',
);
if ($_SID)
{
$s_search_hidden_fields['sid'] = $_SID;
}
if (!empty($_EXTRA_URL))
{
foreach ($_EXTRA_URL as $url_param)
{
$url_param = explode('=', $url_param, 2);
$s_search_hidden_fields[$url_param[0]] = $url_param[1];
}
}
// If we've got a hightlight set pass it on to pagination.
$base_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&t=$topic_id" . ((strlen($u_sort_param)) ? "&$u_sort_param" : '') . (($highlight_match) ? "&hilit=$highlight" : ''));
/**
* Event to modify data before template variables are being assigned
*
* @event core.viewtopic_assign_template_vars_before
* @var string base_url URL to be passed to generate pagination
* @var int forum_id Forum ID
* @var int post_id Post ID
* @var array quickmod_array Array with quick moderation options data
* @var int start Pagination information
* @var array topic_data Array with topic data
* @var int topic_id Topic ID
* @var array topic_tracking_info Array with topic tracking data
* @var int total_posts Topic total posts count
* @var string viewtopic_url URL to the topic page
* @since 3.1.0-RC4
* @changed 3.1.2-RC1 Added viewtopic_url
*/
$vars = array(
'base_url',
'forum_id',
'post_id',
'quickmod_array',
'start',
'topic_data',
'topic_id',
'topic_tracking_info',
'total_posts',
'viewtopic_url',
);
extract($phpbb_dispatcher->trigger_event('core.viewtopic_assign_template_vars_before', compact($vars)));
$pagination->generate_template_pagination($base_url, 'pagination', 'start', $total_posts, $config['posts_per_page'], $start);
// Send vars to template
$template->assign_vars(array(
'FORUM_ID' => $forum_id,
'FORUM_NAME' => $topic_data['forum_name'],
'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']),
'TOPIC_ID' => $topic_id,
'TOPIC_TITLE' => $topic_data['topic_title'],
'TOPIC_POSTER' => $topic_data['topic_poster'],
'TOPIC_AUTHOR_FULL' => get_username_string('full', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
'TOPIC_AUTHOR_COLOUR' => get_username_string('colour', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
'TOPIC_AUTHOR' => get_username_string('username', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
'TOTAL_POSTS' => $user->lang('VIEW_TOPIC_POSTS', (int) $total_posts),
'U_MCP' => ($auth->acl_get('m_', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&mode=topic_view&f=$forum_id&t=$topic_id" . (($start == 0) ? '' : "&start=$start") . ((strlen($u_sort_param)) ? "&$u_sort_param" : ''), true, $user->session_id) : '',
'MODERATORS' => (isset($forum_moderators[$forum_id]) && count($forum_moderators[$forum_id])) ? implode($user->lang['COMMA_SEPARATOR'], $forum_moderators[$forum_id]) : '',
'POST_IMG' => ($topic_data['forum_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', 'FORUM_LOCKED') : $user->img('button_topic_new', 'POST_NEW_TOPIC'),
'QUOTE_IMG' => $user->img('icon_post_quote', 'REPLY_WITH_QUOTE'),
'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'),
'EDIT_IMG' => $user->img('icon_post_edit', 'EDIT_POST'),
'DELETE_IMG' => $user->img('icon_post_delete', 'DELETE_POST'),
'DELETED_IMG' => $user->img('icon_topic_deleted', 'POST_DELETED_RESTORE'),
'INFO_IMG' => $user->img('icon_post_info', 'VIEW_INFO'),
'PROFILE_IMG' => $user->img('icon_user_profile', 'READ_PROFILE'),
'SEARCH_IMG' => $user->img('icon_user_search', 'SEARCH_USER_POSTS'),
'PM_IMG' => $user->img('icon_contact_pm', 'SEND_PRIVATE_MESSAGE'),
'EMAIL_IMG' => $user->img('icon_contact_email', 'SEND_EMAIL'),
'JABBER_IMG' => $user->img('icon_contact_jabber', 'JABBER') ,
'REPORT_IMG' => $user->img('icon_post_report', 'REPORT_POST'),
'REPORTED_IMG' => $user->img('icon_topic_reported', 'POST_REPORTED'),
'UNAPPROVED_IMG' => $user->img('icon_topic_unapproved', 'POST_UNAPPROVED'),
'WARN_IMG' => $user->img('icon_user_warn', 'WARN_USER'),
'S_IS_LOCKED' => ($topic_data['topic_status'] == ITEM_UNLOCKED && $topic_data['forum_status'] == ITEM_UNLOCKED) ? false : true,
'S_SELECT_SORT_DIR' => $s_sort_dir,
'S_SELECT_SORT_KEY' => $s_sort_key,
'S_SELECT_SORT_DAYS' => $s_limit_days,
'S_SINGLE_MODERATOR' => (!empty($forum_moderators[$forum_id]) && count($forum_moderators[$forum_id]) > 1) ? false : true,
'S_TOPIC_ACTION' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&t=$topic_id" . (($start == 0) ? '' : "&start=$start")),
'S_MOD_ACTION' => $s_quickmod_action,
'L_RETURN_TO_FORUM' => $user->lang('RETURN_TO', $topic_data['forum_name']),
'S_VIEWTOPIC' => true,
'S_UNREAD_VIEW' => $view == 'unread',
'S_DISPLAY_SEARCHBOX' => ($auth->acl_get('u_search') && $auth->acl_get('f_search', $forum_id) && $config['load_search']) ? true : false,
'S_SEARCHBOX_ACTION' => append_sid("{$phpbb_root_path}search.$phpEx"),
'S_SEARCH_LOCAL_HIDDEN_FIELDS' => build_hidden_fields($s_search_hidden_fields),
'S_DISPLAY_POST_INFO' => ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_post', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
'S_DISPLAY_REPLY_INFO' => ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_reply', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
'S_ENABLE_FEEDS_TOPIC' => ($config['feed_topic'] && !phpbb_optionget(FORUM_OPTION_FEED_EXCLUDE, $topic_data['forum_options'])) ? true : false,
'U_TOPIC' => "{$server_path}viewtopic.$phpEx?f=$forum_id&t=$topic_id",
'U_FORUM' => $server_path,
'U_VIEW_TOPIC' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&t=$topic_id" . (($start == 0) ? '' : "&start=$start") . (strlen($u_sort_param) ? "&$u_sort_param" : '')),
'U_CANONICAL' => generate_board_url() . '/' . append_sid("viewtopic.$phpEx", "t=$topic_id" . (($start) ? "&start=$start" : ''), true, ''),
'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
'U_VIEW_OLDER_TOPIC' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&t=$topic_id&view=previous"),
'U_VIEW_NEWER_TOPIC' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&t=$topic_id&view=next"),
'U_PRINT_TOPIC' => ($auth->acl_get('f_print', $forum_id)) ? $viewtopic_url . '&view=print' : '',
'U_EMAIL_TOPIC' => ($auth->acl_get('f_email', $forum_id) && $config['email_enable']) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=email&t=$topic_id") : '',
'U_WATCH_TOPIC' => $s_watching_topic['link'],
'U_WATCH_TOPIC_TOGGLE' => $s_watching_topic['link_toggle'],
'S_WATCH_TOPIC_TITLE' => $s_watching_topic['title'],
'S_WATCH_TOPIC_TOGGLE' => $s_watching_topic['title_toggle'],
'S_WATCHING_TOPIC' => $s_watching_topic['is_watching'],
'U_BOOKMARK_TOPIC' => ($user->data['is_registered'] && $config['allow_bookmarks']) ? $viewtopic_url . '&bookmark=1&hash=' . generate_link_hash("topic_$topic_id") : '',
'S_BOOKMARK_TOPIC' => ($user->data['is_registered'] && $config['allow_bookmarks'] && $topic_data['bookmarked']) ? $user->lang['BOOKMARK_TOPIC_REMOVE'] : $user->lang['BOOKMARK_TOPIC'],
'S_BOOKMARK_TOGGLE' => (!$user->data['is_registered'] || !$config['allow_bookmarks'] || !$topic_data['bookmarked']) ? $user->lang['BOOKMARK_TOPIC_REMOVE'] : $user->lang['BOOKMARK_TOPIC'],
'S_BOOKMARKED_TOPIC' => ($user->data['is_registered'] && $config['allow_bookmarks'] && $topic_data['bookmarked']) ? true : false,
'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&f=$forum_id") : '',
'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&f=$forum_id&t=$topic_id") : '',
'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&f=$forum_id&t=$topic_id&hash=" . generate_link_hash("topic_$topic_id")) : '')
);
// Does this topic contain a poll?
if (!empty($topic_data['poll_start']))
{
$sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid
FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p
WHERE o.topic_id = $topic_id
AND p.post_id = {$topic_data['topic_first_post_id']}
AND p.topic_id = o.topic_id
ORDER BY o.poll_option_id";
$result = $db->sql_query($sql);
$poll_info = $vote_counts = array();
while ($row = $db->sql_fetchrow($result))
{
$poll_info[] = $row;
$option_id = (int) $row['poll_option_id'];
$vote_counts[$option_id] = (int) $row['poll_option_total'];
}
$db->sql_freeresult($result);
$cur_voted_id = array();
if ($user->data['is_registered'])
{
$sql = 'SELECT poll_option_id
FROM ' . POLL_VOTES_TABLE . '
WHERE topic_id = ' . $topic_id . '
AND vote_user_id = ' . $user->data['user_id'];
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$cur_voted_id[] = $row['poll_option_id'];
}
$db->sql_freeresult($result);
}
else
{
// Cookie based guest tracking ... I don't like this but hum ho
// it's oft requested. This relies on "nice" users who don't feel
// the need to delete cookies to mess with results.
if ($request->is_set($config['cookie_name'] . '_poll_' . $topic_id, \phpbb\request\request_interface::COOKIE))
{
$cur_voted_id = explode(',', $request->variable($config['cookie_name'] . '_poll_' . $topic_id, '', true, \phpbb\request\request_interface::COOKIE));
$cur_voted_id = array_map('intval', $cur_voted_id);
}
}
// Can not vote at all if no vote permission
$s_can_vote = ($auth->acl_get('f_vote', $forum_id) &&
(($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0) &&
$topic_data['topic_status'] != ITEM_LOCKED &&
$topic_data['forum_status'] != ITEM_LOCKED &&
(!count($cur_voted_id) ||
($auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change']))) ? true : false;
$s_display_results = (!$s_can_vote || ($s_can_vote && count($cur_voted_id)) || $view == 'viewpoll') ? true : false;
/**
* Event to manipulate the poll data
*
* @event core.viewtopic_modify_poll_data
* @var array cur_voted_id Array with options' IDs current user has voted for
* @var int forum_id The topic's forum id
* @var array poll_info Array with the poll information
* @var bool s_can_vote Flag indicating if a user can vote
* @var bool s_display_results Flag indicating if results or poll options should be displayed
* @var int topic_id The id of the topic the user tries to access
* @var array topic_data All the information from the topic and forum tables for this topic
* @var string viewtopic_url URL to the topic page
* @var array vote_counts Array with the vote counts for every poll option
* @var array voted_id Array with updated options' IDs current user is voting for
* @since 3.1.5-RC1
*/
$vars = array(
'cur_voted_id',
'forum_id',
'poll_info',
's_can_vote',
's_display_results',
'topic_id',
'topic_data',
'viewtopic_url',
'vote_counts',
'voted_id',
);
extract($phpbb_dispatcher->trigger_event('core.viewtopic_modify_poll_data', compact($vars)));
if ($update && $s_can_vote)
{
if (!count($voted_id) || count($voted_id) > $topic_data['poll_max_options'] || in_array(VOTE_CONVERTED, $cur_voted_id) || !check_form_key('posting'))
{
$redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&t=$topic_id" . (($start == 0) ? '' : "&start=$start"));
meta_refresh(5, $redirect_url);
if (!count($voted_id))
{
$message = 'NO_VOTE_OPTION';
}
else if (count($voted_id) > $topic_data['poll_max_options'])
{
$message = 'TOO_MANY_VOTE_OPTIONS';
}
else if (in_array(VOTE_CONVERTED, $cur_voted_id))
{
$message = 'VOTE_CONVERTED';
}
else
{
$message = 'FORM_INVALID';
}
$message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
trigger_error($message);
}
foreach ($voted_id as $option)
{
if (in_array($option, $cur_voted_id))
{
continue;
}
$sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
SET poll_option_total = poll_option_total + 1
WHERE poll_option_id = ' . (int) $option . '
AND topic_id = ' . (int) $topic_id;
$db->sql_query($sql);
$vote_counts[$option]++;
if ($user->data['is_registered'])
{
$sql_ary = array(
'topic_id' => (int) $topic_id,
'poll_option_id' => (int) $option,
'vote_user_id' => (int) $user->data['user_id'],
'vote_user_ip' => (string) $user->ip,
);
$sql = 'INSERT INTO ' . POLL_VOTES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
$db->sql_query($sql);
}
}
foreach ($cur_voted_id as $option)
{
if (!in_array($option, $voted_id))
{
$sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
SET poll_option_total = poll_option_total - 1
WHERE poll_option_id = ' . (int) $option . '
AND topic_id = ' . (int) $topic_id;
$db->sql_query($sql);
$vote_counts[$option]--;
if ($user->data['is_registered'])
{
$sql = 'DELETE FROM ' . POLL_VOTES_TABLE . '
WHERE topic_id = ' . (int) $topic_id . '
AND poll_option_id = ' . (int) $option . '
AND vote_user_id = ' . (int) $user->data['user_id'];
$db->sql_query($sql);
}
}
}
if ($user->data['user_id'] == ANONYMOUS && !$user->data['is_bot'])
{
$user->set_cookie('poll_' . $topic_id, implode(',', $voted_id), time() + 31536000);
}
$sql = 'UPDATE ' . TOPICS_TABLE . '
SET poll_last_vote = ' . time() . "