forked from featherbb/featherbb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoderate.php
More file actions
978 lines (779 loc) · 35.8 KB
/
moderate.php
File metadata and controls
978 lines (779 loc) · 35.8 KB
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
<?php
/**
* Copyright (C) 2015 FeatherBB
* based on code by (C) 2008-2012 FluxBB
* and Rickard Andersson (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
namespace model;
use DB;
class moderate
{
public function __construct()
{
$this->feather = \Slim\Slim::getInstance();
$this->start = $this->feather->start;
$this->config = $this->feather->config;
$this->user = $this->feather->user;
$this->request = $this->feather->request;
}
public function display_ip_info($ip)
{
global $lang_misc;
// Load the misc.php language file
require FEATHER_ROOT.'lang/'.$this->user->language.'/misc.php';
message(sprintf($lang_misc['Host info 1'], $ip).'<br />'.sprintf($lang_misc['Host info 2'], @gethostbyaddr($ip)).'<br /><br /><a href="'.get_link('admin/users/show-users/ip/'.$ip.'/').'">'.$lang_misc['Show more users'].'</a>');
}
public function display_ip_address_post($pid)
{
global $lang_common;
$ip = DB::for_table('posts')
->where('id', $pid)
->find_one_col('poster_ip');
if (!$ip) {
message($lang_common['Bad request'], '404');
}
// Load the misc.php language file
require FEATHER_ROOT.'lang/'.$this->user->language.'/misc.php';
message(sprintf($lang_misc['Host info 1'], $ip).'<br />'.sprintf($lang_misc['Host info 2'], @gethostbyaddr($ip)).'<br /><br /><a href="'.get_link('admin/users/show-users/ip/'.$ip.'/').'">'.$lang_misc['Show more users'].'</a>');
}
public function get_moderators($fid)
{
$moderators = DB::for_table('forums')
->where('id', $fid)
->find_one_col('moderators');
return $moderators;
}
public function get_topic_info($fid, $tid)
{
global $lang_common;
// Fetch some info about the topic
$select_get_topic_info = array('forum_id' => 'f.id', 'f.forum_name', 't.subject', 't.num_replies', 't.first_post_id');
$where_get_topic_info = array(
array('fp.read_forum' => 'IS NULL'),
array('fp.read_forum' => '1')
);
$cur_topic = DB::for_table('topics')
->table_alias('t')
->select_many($select_get_topic_info)
->inner_join('forums', array('f.id', '=', 't.forum_id'), 'f')
->left_outer_join('forum_perms', array('fp.forum_id', '=', 'f.id'), 'fp')
->left_outer_join('forum_perms', array('fp.group_id', '=', $this->user->g_id), null, true)
->where_any_is($where_get_topic_info)
->where('f.id', $fid)
->where('t.id', $tid)
->where_null('t.moved_to')
->find_one();
if (!$cur_topic) {
message($lang_common['Bad request'], '404');
}
return $cur_topic;
}
public function delete_posts($tid, $fid, $p = null)
{
global $lang_common, $lang_misc;
$posts = $this->request->post('posts') ? $this->request->post('posts') : array();
if (empty($posts)) {
message($lang_misc['No posts selected']);
}
if ($this->request->post('delete_posts_comply')) {
if (@preg_match('%[^0-9,]%', $posts)) {
message($lang_common['Bad request'], '404');
}
// Verify that the post IDs are valid
$posts_array = explode(',', $posts);
$result = DB::for_table('posts')
->where_in('id', $posts_array)
->where('topic_id', $tid)
->find_many();
if ($this->user->g_id != FEATHER_ADMIN) {
$result->where_not_in('poster_id', get_admin_ids());
}
if (count($result) != substr_count($posts, ',') + 1) {
message($lang_common['Bad request'], '404');
}
// Delete the posts
DB::for_table('posts')
->where_in('id', $posts_array)
->delete_many();
require FEATHER_ROOT.'include/search_idx.php';
strip_search_index($posts);
// Get last_post, last_post_id, and last_poster for the topic after deletion
$select_last_post = array('id', 'poster', 'posted');
$last_post = DB::for_table('posts')
->select_many($select_last_post)
->where('topic_id', $tid)
->find_one();
// How many posts did we just delete?
$num_posts_deleted = substr_count($posts, ',') + 1;
// Update the topic
$update_topic = array(
'last_post' => $this->user->id,
'last_post_id' => $last_post['id'],
'last_poster' => $last_post['poster'],
);
DB::for_table('topics')->where('id', $tid)
->find_one()
->set($update_topic)
->set_expr('num_replies', 'num_replies-'.$num_posts_deleted)
->save();
update_forum($fid);
redirect(get_link('topic/'.$tid.'/'), $lang_misc['Delete posts redirect']);
}
return $posts;
}
public function split_posts($tid, $fid, $p = null)
{
global $lang_common, $lang_misc, $lang_post;
$posts = $this->request->post('posts') ? $this->request->post('posts') : array();
if (empty($posts)) {
message($lang_misc['No posts selected']);
}
if ($this->request->post('split_posts_comply')) {
if (@preg_match('%[^0-9,]%', $posts)) {
message($lang_common['Bad request'], '404');
}
$move_to_forum = $this->request->post('move_to_forum') ? intval($this->request->post('move_to_forum')) : 0;
if ($move_to_forum < 1) {
message($lang_common['Bad request'], '404');
}
// How many posts did we just split off?
$num_posts_splitted = substr_count($posts, ',') + 1;
// Verify that the post IDs are valid
$posts_array = explode(',', $posts);
$result = DB::for_table('posts')
->where_in('id', $posts_array)
->where('topic_id', $tid)
->find_many();
if (count($result) != $num_posts_splitted) {
message($lang_common['Bad request'], '404');
}
// Verify that the move to forum ID is valid
$where_split_posts = array(
array('fp.post_topics' => 'IS NULL'),
array('fp.post_topics' => '1')
);
$result = DB::for_table('forums')
->table_alias('f')
->left_outer_join('forum_perms', array('fp.forum_id', '=', $move_to_forum), 'fp', true)
->left_outer_join('forum_perms', array('fp.group_id', '=', $this->user->g_id), null, true)
->where_any_is($where_split_posts)
->where_null('f.redirect_url')
->find_one();
if (!$result) {
message($lang_common['Bad request'], '404');
}
// Load the post.php language file
require FEATHER_ROOT.'lang/'.$this->user->language.'/post.php';
// Check subject
$new_subject = $this->request->post('new_subject') ? feather_trim($this->request->post('new_subject')) : '';
if ($new_subject == '') {
message($lang_post['No subject']);
} elseif (feather_strlen($new_subject) > 70) {
message($lang_post['Too long subject']);
}
// Get data from the new first post
$select_first_post = array('id', 'poster', 'posted');
$first_post_data = DB::for_table('posts')
->select_many($select_first_post)
->where_in('id',$posts_array )
->order_by_asc('id')
->find_one();
// Create the new topic
$insert_topic = array(
'poster' => $first_post_data['poster'],
'subject' => $new_subject,
'posted' => $first_post_data['posted'],
'first_post_id' => $first_post_data['id'],
'forum_id' => $move_to_forum,
);
DB::for_table('topics')
->create()
->set($insert_topic)
->save();
$new_tid = DB::get_db()->lastInsertId($this->feather->prefix.'topics');
// Move the posts to the new topic
DB::for_table('posts')->where_in('id', $posts_array)
->find_one()
->set('topic_id', $new_tid)
->save();
// Apply every subscription to both topics
DB::for_table('topic_subscriptions')->raw_query('INSERT INTO '.$this->feather->prefix.'topic_subscriptions (user_id, topic_id) SELECT user_id, '.$new_tid.' FROM '.$this->feather->prefix.'topic_subscriptions WHERE topic_id=:tid', array('tid' => $tid));
// Get last_post, last_post_id, and last_poster from the topic and update it
$select_last_post = array('id', 'poster', 'posted');
$last_old_post_data = DB::for_table('posts')
->select_many($select_last_post)
->where('topic_id', $tid)
->order_by_desc('id')
->find_one();
// Update the old topic
$update_old_topic = array(
'last_post' => $last_old_post_data['posted'],
'last_post_id' => $last_old_post_data['id'],
'last_poster' => $last_old_post_data['poster'],
);
DB::for_table('topics')
->where('id', $tid)
->find_one()
->set($update_old_topic)
->set_expr('num_replies', 'num_replies-'.$num_posts_splitted)
->save();
// Get last_post, last_post_id, and last_poster from the new topic and update it
$select_new_post = array('id', 'poster', 'posted');
$last_new_post_data = DB::for_table('posts')
->select_many($select_new_post)
->where('topic_id', $new_tid)
->order_by_desc('id')
->find_one();
// Update the new topic
$update_new_topic = array(
'last_post' => $last_new_post_data['posted'],
'last_post_id' => $last_new_post_data['id'],
'last_poster' => $last_new_post_data['poster'],
);
DB::for_table('topics')
->where('id', $new_tid)
->find_one()
->set($update_new_topic)
->set_expr('num_replies', 'num_replies-'.$num_posts_splitted-1)
->save();
update_forum($fid);
update_forum($move_to_forum);
redirect(get_link('topic/'.$new_tid.'/'), $lang_misc['Split posts redirect']);
}
return $posts;
}
public function get_forum_list_split($id)
{
$output = '';
$select_get_forum_list_split = array('cid' => 'c.id', 'c.cat_name', 'fid' => 'f.id', 'f.forum_name');
$where_get_forum_list_split = array(
array('fp.post_topics' => 'IS NULL'),
array('fp.post_topics' => '1')
);
$order_by_get_forum_list_split = array('c.disp_position', 'c.id', 'f.disp_position');
$result = DB::for_table('categories')
->table_alias('c')
->select_many($select_get_forum_list_split)
->inner_join('forums', array('c.id', '=', 'f.cat_id'), 'f')
->left_outer_join('forum_perms', array('fp.forum_id', '=', 'f.id'), 'fp')
->left_outer_join('forum_perms', array('fp.group_id', '=', $this->user->g_id), null, true)
->where_any_is($where_get_forum_list_split)
->where_null('f.redirect_url')
->order_by_many($order_by_get_forum_list_split)
->find_result_set();
$cur_category = 0;
foreach($result as $cur_forum) {
if ($cur_forum->cid != $cur_category) {
// A new category since last iteration?
if ($cur_category) {
$output .= "\t\t\t\t\t\t\t".'</optgroup>'."\n";
}
$output .= "\t\t\t\t\t\t\t".'<optgroup label="'.feather_escape($cur_forum->cat_name).'">'."\n";
$cur_category = $cur_forum->cid;
}
$output .= "\t\t\t\t\t\t\t\t".'<option value="'.$cur_forum->fid.'"'.($id == $cur_forum->fid ? ' selected="selected"' : '').'>'.feather_escape($cur_forum->forum_name).'</option>'."\n";
}
return $output;
}
public function get_forum_list_move($id)
{
$output = '';
$select_get_forum_list_move = array('cid' => 'c.id', 'c.cat_name', 'fid' => 'f.id', 'f.forum_name');
$where_get_forum_list_move = array(
array('fp.post_topics' => 'IS NULL'),
array('fp.post_topics' => '1')
);
$order_by_get_forum_list_move = array('c.disp_position', 'c.id', 'f.disp_position');
$result = DB::for_table('categories')
->table_alias('c')
->select_many($select_get_forum_list_move)
->inner_join('forums', array('c.id', '=', 'f.cat_id'), 'f')
->left_outer_join('forum_perms', array('fp.forum_id', '=', 'f.id'), 'fp')
->left_outer_join('forum_perms', array('fp.group_id', '=', $this->user->g_id), null, true)
->where_any_is($where_get_forum_list_move)
->where_null('f.redirect_url')
->order_by_many($order_by_get_forum_list_move)
->find_result_set();
$cur_category = 0;
foreach($result as $cur_forum) {
if ($cur_forum->cid != $cur_category) {
// A new category since last iteration?
if ($cur_category) {
$output .= "\t\t\t\t\t\t\t".'</optgroup>'."\n";
}
$output .= "\t\t\t\t\t\t\t".'<optgroup label="'.feather_escape($cur_forum->cat_name).'">'."\n";
$cur_category = $cur_forum->cid;
}
if ($cur_forum->fid != $id) {
$output .= "\t\t\t\t\t\t\t\t".'<option value="'.$cur_forum->fid.'">'.feather_escape($cur_forum->forum_name).'</option>'."\n";
}
}
return $output;
}
public function display_posts_view($tid, $start_from)
{
global $pd, $lang_topic;
$post_data = array();
require FEATHER_ROOT.'include/parser.php';
$post_count = 0; // Keep track of post numbers
// Retrieve a list of post IDs, LIMIT is (really) expensive so we only fetch the IDs here then later fetch the remaining data
$find_ids = DB::for_table('posts')->select('id')
->where('topic_id', $tid)
->order_by('id')
->limit($this->user->disp_posts)
->offset($start_from)
->find_many();
foreach ($find_ids as $id) {
$post_ids[] = $id['id'];
}
// Retrieve the posts (and their respective poster)
$select_display_posts_view = array('u.title', 'u.num_posts', 'g.g_id', 'g.g_user_title', 'p.id', 'p.poster', 'p.poster_id', 'p.message', 'p.hide_smilies', 'p.posted', 'p.edited', 'p.edited_by');
$result = DB::for_table('posts')
->table_alias('p')
->select_many($select_display_posts_view)
->inner_join('users', array('u.id', '=', 'p.poster_id'), 'u')
->inner_join('groups', array('g.g_id', '=', 'u.group_id'), 'g')
->where_in('p.id', $post_ids)
->order_by('p.id')
->find_many();
foreach($result as $cur_post) {
$post_count++;
// If the poster is a registered user
if ($cur_post->poster_id > 1) {
if ($this->user->g_view_users == '1') {
$cur_post->poster_disp = '<a href="'.get_link('user/'.$cur_post->poster_id.'/').'">'.feather_escape($cur_post->poster).'</a>';
} else {
$cur_post->poster_disp = feather_escape($cur_post->poster);
}
// get_title() requires that an element 'username' be present in the array
$cur_post->username = $cur_post->poster;
$cur_post->user_title = get_title($cur_post);
if ($this->config['o_censoring'] == '1') {
$cur_post->user_title = censor_words($cur_post->user_title);
}
}
// If the poster is a guest (or a user that has been deleted)
else {
$cur_post->poster_disp = feather_escape($cur_post->poster);
$cur_post->user_title = $lang_topic['Guest'];
}
// Perform the main parsing of the message (BBCode, smilies, censor words etc)
$cur_post->message = parse_message($cur_post->message, $cur_post->hide_smilies);
$post_data[] = $cur_post;
}
return $post_data;
}
public function move_topics_to($fid, $tfid = null, $param = null)
{
global $lang_common, $lang_misc;
if (@preg_match('%[^0-9,]%', $this->request->post('topics'))) {
message($lang_common['Bad request'], '404');
}
$topics = explode(',', $this->request->post('topics'));
$move_to_forum = $this->request->post('move_to_forum') ? intval($this->request->post('move_to_forum')) : 0;
if (empty($topics) || $move_to_forum < 1) {
message($lang_common['Bad request'], '404');
}
// Verify that the topic IDs are valid
$result = DB::for_table('topics')
->where_in('id', $topics)
->where('forum_id', $fid)
->find_many();
if (count($result) != count($topics)) {
message($lang_common['Bad request'], '404');
}
// Verify that the move to forum ID is valid
$where_move_topics_to = array(
array('fp.post_topics' => 'IS NULL'),
array('fp.post_topics' => '1')
);
$authorized = DB::for_table('forums')
->table_alias('f')
->left_outer_join('forum_perms', array('fp.forum_id', '=', $move_to_forum), 'fp', true)
->left_outer_join('forum_perms', array('fp.group_id', '=', $this->user->g_id), null, true)
->where_any_is($where_move_topics_to)
->where_null('f.redirect_url')
->find_one();
if (!$authorized) {
message($lang_common['Bad request'], '404');
}
// Delete any redirect topics if there are any (only if we moved/copied the topic back to where it was once moved from)
DB::for_table('topics')
->where('forum_id', $move_to_forum)
->where_in('moved_to', $topics)
->delete_many();
// Move the topic(s)
DB::for_table('topics')->where_in('id', $topics)
->find_one()
->set('forum_id', $move_to_forum)
->save();
// Should we create redirect topics?
if ($this->request->post('with_redirect')) {
foreach ($topics as $cur_topic) {
// Fetch info for the redirect topic
$select_move_topics_to = array('poster', 'subject', 'posted', 'last_post');
$moved_to = DB::for_table('topics')->select_many($select_move_topics_to)
->where('id', $cur_topic)
->find_one();
// Create the redirect topic
$insert_move_topics_to = array(
'poster' => $moved_to['poster'],
'subject' => $moved_to['subject'],
'posted' => $moved_to['posted'],
'last_post' => $moved_to['last_post'],
'moved_to' => $cur_topic,
'forum_id' => $fid,
);
// Insert the report
DB::for_table('topics')
->create()
->set($insert_move_topics_to)
->save();
}
}
update_forum($fid); // Update the forum FROM which the topic was moved
update_forum($move_to_forum); // Update the forum TO which the topic was moved
$redirect_msg = (count($topics) > 1) ? $lang_misc['Move topics redirect'] : $lang_misc['Move topic redirect'];
redirect(get_link('forum/'.$move_to_forum.'/'), $redirect_msg);
}
public function check_move_possible()
{
global $lang_misc;
$select_check_move_possible = array('cid' => 'c.id', 'c.cat_name', 'fid' => 'f.id', 'f.forum_name');
$where_check_move_possible = array(
array('fp.post_topics' => 'IS NULL'),
array('fp.post_topics' => '1')
);
$order_by_check_move_possible = array('c.disp_position', 'c.id', 'f.disp_position');
$result = DB::for_table('categories')
->table_alias('c')
->select_many($select_check_move_possible)
->inner_join('forums', array('c.id', '=', 'f.cat_id'), 'f')
->left_outer_join('forum_perms', array('fp.forum_id', '=', 'f.id'), 'fp')
->left_outer_join('forum_perms', array('fp.group_id', '=', $this->user->g_id), null, true)
->where_any_is($where_check_move_possible)
->where_null('f.redirect_url')
->order_by_many($order_by_check_move_possible)
->find_many();
if (count($result) < 2) {
message($lang_misc['Nowhere to move']);
}
}
public function merge_topics($fid)
{
global $lang_common, $lang_misc;
if (@preg_match('%[^0-9,]%', $this->request->post('topics'))) {
message($lang_common['Bad request'], '404');
}
$topics = explode(',', $this->request->post('topics'));
if (count($topics) < 2) {
message($lang_misc['Not enough topics selected']);
}
// Verify that the topic IDs are valid (redirect links will point to the merged topic after the merge)
$result = DB::for_table('topics')
->where_in('id', $topics)
->where('forum_id', $fid)
->find_many();
if (count($result) != count($topics)) {
message($lang_common['Bad request'], '404');
}
// The topic that we are merging into is the one with the smallest ID
$merge_to_tid = DB::for_table('topics')
->where_in('id', $topics)
->where('forum_id', $fid)
->order_by_asc('id')
->find_one_col('id');
// Make any redirect topics point to our new, merged topic
$query = 'UPDATE '.$this->feather->prefix.'topics SET moved_to='.$merge_to_tid.' WHERE moved_to IN('.implode(',', $topics).')';
// Should we create redirect topics?
if ($this->request->post('with_redirect')) {
$query .= ' OR (id IN('.implode(',', $topics).') AND id != '.$merge_to_tid.')';
}
// TODO ?
DB::for_table('topics')->raw_execute($query);
// Merge the posts into the topic
DB::for_table('posts')
->where_in('topic_id', $topics)
->update_many('topic_id', $merge_to_tid);
// Update any subscriptions
$find_ids = DB::for_table('topic_subscriptions')->select('user_id')
->distinct()
->where_in('topic_id', $topics)
->find_many();
foreach ($find_ids as $id) {
$subscribed_users[] = $id['user_id'];
}
// Delete the subscriptions
DB::for_table('topic_subscriptions')
->where_in('topic_id', $topics)
->delete_many();
foreach ($subscribed_users as $cur_user_id) {
$insert_topic_subscription = array(
'topic_id' => $merge_to_tid,
'user_id' => $cur_user_id,
);
// Insert the subscription
DB::for_table('topic_subscriptions')
->create()
->set($insert_topic_subscription)
->save();
}
// Without redirection the old topics are removed
if ($this->request->post('with_redirect') == 0) {
DB::for_table('topics')
->where_in('id', $topics)
->where_not_equal('id', $merge_to_tid)
->delete_many();
}
// Count number of replies in the topic
$num_replies = DB::for_table('posts')->where('topic_id', $merge_to_tid)->count('id') - 1;
// Get last_post, last_post_id and last_poster
$select_last_post = array('posted', 'id', 'poster');
$last_post = DB::for_table('posts')->select_many($select_last_post)
->where('topic_id', $merge_to_tid)
->order_by_desc('id')
->find_one();
// Update topic
$insert_topic = array(
'num_replies' => $num_replies,
'last_post' => $last_post['posted'],
'last_post_id' => $last_post['id'],
'last_poster' => $last_post['poster'],
);
DB::for_table('topics')
->where('id', $merge_to_tid)
->find_one()
->set($insert_topic)
->save();
// Update the forum FROM which the topic was moved and redirect
update_forum($fid);
redirect(get_link('forum/'.$fid.'/'), $lang_misc['Merge topics redirect']);
}
public function delete_topics($topics, $fid)
{
global $lang_misc, $lang_common;
if (@preg_match('%[^0-9,]%', $topics)) {
message($lang_common['Bad request'], '404');
}
require FEATHER_ROOT.'include/search_idx.php';
$topics_sql = explode(',', $topics);
// Verify that the topic IDs are valid
$result = DB::for_table('topics')
->where_in('id', $topics_sql)
->where('forum_id', $fid)
->find_many();
if (count($result) != substr_count($topics, ',') + 1) {
message($lang_common['Bad request'], '404');
}
// Verify that the posts are not by admins
if ($this->user->g_id != FEATHER_ADMIN) {
$authorized = DB::for_table('posts')
->where_in('topic_id', $topics_sql)
->where('poster_id', get_admin_ids())
->find_many();
if ($authorized) {
message($lang_common['No permission'], '403');
}
}
// Delete the topics
DB::for_table('topics')
->where_in('id', $topics_sql)
->delete_many();
// Delete any redirect topics
DB::for_table('topics')
->where_in('moved_to', $topics_sql)
->delete_many();
// Delete any subscriptions
DB::for_table('topic_subscriptions')
->where_in('topic_id', $topics_sql)
->delete_many();
// Create a list of the post IDs in this topic and then strip the search index
$find_ids = DB::for_table('posts')->select('id')
->where_in('topic_id', $topics_sql)
->find_many();
foreach ($find_ids as $id) {
$ids_post[] = $id['id'];
}
$post_ids = implode(', ', $ids_post);
// We have to check that we actually have a list of post IDs since we could be deleting just a redirect topic
if ($post_ids != '') {
strip_search_index($post_ids);
}
// Delete posts
DB::for_table('posts')
->where_in('topic_id', $topics_sql)
->delete_many();
update_forum($fid);
redirect(get_link('forum/'.$fid.'/'), $lang_misc['Delete topics redirect']);
}
public function get_forum_info($fid)
{
global $lang_common;
$select_get_forum_info = array('f.forum_name', 'f.redirect_url', 'f.num_topics', 'f.sort_by');
$where_get_forum_info = array(
array('fp.read_forum' => 'IS NULL'),
array('fp.read_forum' => '1')
);
$cur_forum = DB::for_table('forums')
->table_alias('f')
->select_many($select_get_forum_info)
->left_outer_join('forum_perms', array('fp.forum_id', '=', 'f.id'), 'fp')
->left_outer_join('forum_perms', array('fp.group_id', '=', $this->user->g_id), null, true)
->where_any_is($where_get_forum_info)
->where('f.id', $fid)
->find_one();
if (!$cur_forum) {
message($lang_common['Bad request'], '404');
}
return $cur_forum;
}
public function forum_sort_by($forum_sort)
{
switch ($forum_sort) {
case 0:
$sort_by = 'last_post DESC';
break;
case 1:
$sort_by = 'posted DESC';
break;
case 2:
$sort_by = 'subject ASC';
break;
default:
$sort_by = 'last_post DESC';
break;
}
return $sort_by;
}
public function display_topics($fid, $sort_by, $start_from)
{
global $lang_forum, $lang_common;
$topic_data = array();
// Get topic/forum tracking data
if (!$this->user->is_guest) {
$tracked_topics = get_tracked_topics();
}
// Retrieve a list of topic IDs, LIMIT is (really) expensive so we only fetch the IDs here then later fetch the remaining data
$result = DB::for_table('topics')->select('id')
->where('forum_id', $fid)
->order_by_expr('sticky DESC, '.$sort_by)
->limit($this->user->disp_topics)
->offset($start_from)
->find_many();
// If there are topics in this forum
if ($result) {
foreach ($result as $id) {
$topic_ids[] = $id['id'];
}
// Select topics
$select_display_topics = array('id', 'poster', 'subject', 'posted', 'last_post', 'last_post_id', 'last_poster', 'num_views', 'num_replies', 'closed', 'sticky', 'moved_to');
// TODO: order_by_expr && result_set
$result = DB::for_table('topics')->select_many($select_display_topics)
->where_in('id', $topic_ids)
->order_by_expr('sticky DESC, '.$sort_by.', id DESC')
->find_many();
$topic_count = 0;
foreach($result as $cur_topic) {
++$topic_count;
$status_text = array();
$cur_topic['item_status'] = ($topic_count % 2 == 0) ? 'roweven' : 'rowodd';
$cur_topic['icon_type'] = 'icon';
$url_topic = url_friendly($cur_topic['subject']);
if (is_null($cur_topic['moved_to'])) {
$cur_topic['last_post_disp'] = '<a href="'.get_link('post/'.$cur_topic['last_post_id'].'/#p'.$cur_topic['last_post_id']).'">'.format_time($cur_topic['last_post']).'</a> <span class="byuser">'.$lang_common['by'].' '.feather_escape($cur_topic['last_poster']).'</span>';
$cur_topic['ghost_topic'] = false;
} else {
$cur_topic['last_post_disp'] = '- - -';
$cur_topic['ghost_topic'] = true;
}
if ($this->config['o_censoring'] == '1') {
$cur_topic['subject'] = censor_words($cur_topic['subject']);
}
if ($cur_topic['sticky'] == '1') {
$cur_topic['item_status'] .= ' isticky';
$status_text[] = '<span class="stickytext">'.$lang_forum['Sticky'].'</span>';
}
if ($cur_topic['moved_to'] != 0) {
$cur_topic['subject_disp'] = '<a href="'.get_link('topic/'.$cur_topic['moved_to'].'/'.$url_topic.'/').'">'.feather_escape($cur_topic['subject']).'</a> <span class="byuser">'.$lang_common['by'].' '.feather_escape($cur_topic['poster']).'</span>';
$status_text[] = '<span class="movedtext">'.$lang_forum['Moved'].'</span>';
$cur_topic['item_status'] .= ' imoved';
} elseif ($cur_topic['closed'] == '0') {
$cur_topic['subject_disp'] = '<a href="'.get_link('topic/'.$cur_topic['id'].'/'.$url_topic.'/').'">'.feather_escape($cur_topic['subject']).'</a> <span class="byuser">'.$lang_common['by'].' '.feather_escape($cur_topic['poster']).'</span>';
} else {
$cur_topic['subject_disp'] = '<a href="'.get_link('topic/'.$cur_topic['id'].'/'.$url_topic.'/').'">'.feather_escape($cur_topic['subject']).'</a> <span class="byuser">'.$lang_common['by'].' '.feather_escape($cur_topic['poster']).'</span>';
$status_text[] = '<span class="closedtext">'.$lang_forum['Closed'].'</span>';
$cur_topic['item_status'] .= ' iclosed';
}
if (!$cur_topic['ghost_topic'] && $cur_topic['last_post'] > $this->user->last_visit && (!isset($tracked_topics['topics'][$cur_topic['id']]) || $tracked_topics['topics'][$cur_topic['id']] < $cur_topic['last_post']) && (!isset($tracked_topics['forums'][$fid]) || $tracked_topics['forums'][$fid] < $cur_topic['last_post'])) {
$cur_topic['item_status'] .= ' inew';
$cur_topic['icon_type'] = 'icon icon-new';
$cur_topic['subject_disp'] = '<strong>'.$cur_topic['subject_disp'].'</strong>';
$subject_new_posts = '<span class="newtext">[ <a href="'.get_link('topic/'.$cur_topic['id'].'/action/new/').'" title="'.$lang_common['New posts info'].'">'.$lang_common['New posts'].'</a> ]</span>';
} else {
$subject_new_posts = null;
}
// Insert the status text before the subject
$cur_topic['subject_disp'] = implode(' ', $status_text).' '.$cur_topic['subject_disp'];
$num_pages_topic = ceil(($cur_topic['num_replies'] + 1) / $this->user->disp_posts);
if ($num_pages_topic > 1) {
$subject_multipage = '<span class="pagestext">[ '.paginate($num_pages_topic, -1, 'topic/'.$cur_topic['id'].'/'.$url_topic.'/#').' ]</span>';
} else {
$subject_multipage = null;
}
// Should we show the "New posts" and/or the multipage links?
if (!empty($subject_new_posts) || !empty($subject_multipage)) {
$cur_topic['subject_disp'] .= !empty($subject_new_posts) ? ' '.$subject_new_posts : '';
$cur_topic['subject_disp'] .= !empty($subject_multipage) ? ' '.$subject_multipage : '';
}
$topic_data[] = $cur_topic;
}
}
return $topic_data;
}
public function stick_topic($id, $fid)
{
DB::for_table('topics')->where('id', $id)->where('forum_id', $fid)
->find_one()
->set('sticky', 1)
->save();
}
public function unstick_topic($id, $fid)
{
DB::for_table('topics')->where('id', $id)->where('forum_id', $fid)
->find_one()
->set('sticky', 0)
->save();
}
public function open_topic($id, $fid)
{
DB::for_table('topics')->where('id', $id)->where('forum_id', $fid)
->find_one()
->set('closed', 0)
->save();
}
public function close_topic($id, $fid)
{
DB::for_table('topics')->where('id', $id)->where('forum_id', $fid)
->find_one()
->set('closed', 1)
->save();
}
public function close_multiple_topics($action, $topics, $fid)
{
DB::for_table('topics')
->where_in('id', $topics)
->update_many('closed', $action);
}
public function get_subject_tid($id)
{
global $lang_common;
$subject = DB::for_table('topics')
->where('id', $id)
->find_one_col('subject');
if (!$subject) {
message($lang_common['Bad request'], '404');
}
return $subject;
}
}