This repository was archived by the owner on Mar 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions_admin.php
3374 lines (2830 loc) · 89.3 KB
/
functions_admin.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
/**
*
* @package acp
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* Recalculate Nested Sets
*
* @param int $new_id first left_id (should start with 1)
* @param string $pkey primary key-column (containing the id for the parent_id of the children)
* @param string $table constant or fullname of the table
* @param int $parent_id parent_id of the current set (default = 0)
* @param array $where contains strings to compare closer on the where statement (additional)
*
* @author EXreaction
*/
function recalc_nested_sets(&$new_id, $pkey, $table, $parent_id = 0, $where = array())
{
global $db;
$sql = 'SELECT *
FROM ' . $table . '
WHERE parent_id = ' . (int) $parent_id .
((!empty($where)) ? ' AND ' . implode(' AND ', $where) : '') . '
ORDER BY left_id ASC';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
// First we update the left_id for this module
if ($row['left_id'] != $new_id)
{
$db->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', array('left_id' => $new_id)) . " WHERE $pkey = {$row[$pkey]}");
}
$new_id++;
// Then we go through any children and update their left/right id's
recalc_nested_sets($new_id, $pkey, $table, $row[$pkey], $where);
// Then we come back and update the right_id for this module
if ($row['right_id'] != $new_id)
{
$db->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', array('right_id' => $new_id)) . " WHERE $pkey = {$row[$pkey]}");
}
$new_id++;
}
$db->sql_freeresult($result);
}
/**
* Simple version of jumpbox, just lists authed forums
*/
function make_forum_select($select_id = false, $ignore_id = false, $ignore_acl = false, $ignore_nonpost = false, $ignore_emptycat = true, $only_acl_post = false, $return_array = false)
{
global $db, $user, $auth;
// This query is identical to the jumpbox one
$sql = 'SELECT forum_id, forum_name, parent_id, forum_type, forum_flags, forum_options, left_id, right_id
FROM ' . FORUMS_TABLE . '
ORDER BY left_id ASC';
$result = $db->sql_query($sql, 600);
$right = 0;
$padding_store = array('0' => '');
$padding = '';
$forum_list = ($return_array) ? array() : '';
// Sometimes it could happen that forums will be displayed here not be displayed within the index page
// This is the result of forums not displayed at index, having list permissions and a parent of a forum with no permissions.
// If this happens, the padding could be "broken"
while ($row = $db->sql_fetchrow($result))
{
if ($row['left_id'] < $right)
{
$padding .= ' ';
$padding_store[$row['parent_id']] = $padding;
}
else if ($row['left_id'] > $right + 1)
{
$padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
}
$right = $row['right_id'];
$disabled = false;
if (!$ignore_acl && $auth->acl_gets(array('f_list', 'a_forum', 'a_forumadd', 'a_forumdel'), $row['forum_id']))
{
if ($only_acl_post && !$auth->acl_get('f_post', $row['forum_id']) || (!$auth->acl_get('m_approve', $row['forum_id']) && !$auth->acl_get('f_noapprove', $row['forum_id'])))
{
$disabled = true;
}
}
else if (!$ignore_acl)
{
continue;
}
if (
((is_array($ignore_id) && in_array($row['forum_id'], $ignore_id)) || $row['forum_id'] == $ignore_id)
||
// Non-postable forum with no subforums, don't display
($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']) && $ignore_emptycat)
||
($row['forum_type'] != FORUM_POST && $ignore_nonpost)
)
{
$disabled = true;
}
if ($return_array)
{
// Include some more information...
$selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? true : false) : (($row['forum_id'] == $select_id) ? true : false);
$forum_list[$row['forum_id']] = array_merge(array('padding' => $padding, 'selected' => ($selected && !$disabled), 'disabled' => $disabled), $row);
}
else
{
$selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? ' selected="selected"' : '') : (($row['forum_id'] == $select_id) ? ' selected="selected"' : '');
$forum_list .= '<option value="' . $row['forum_id'] . '"' . (($disabled) ? ' disabled="disabled" class="disabled-option"' : $selected) . '>' . $padding . $row['forum_name'] . '</option>';
}
}
$db->sql_freeresult($result);
unset($padding_store);
return $forum_list;
}
/**
* Generate size select options
*/
function size_select_options($size_compare)
{
global $user;
$size_types_text = array($user->lang['BYTES'], $user->lang['KIB'], $user->lang['MIB']);
$size_types = array('b', 'kb', 'mb');
$s_size_options = '';
for ($i = 0, $size = sizeof($size_types_text); $i < $size; $i++)
{
$selected = ($size_compare == $size_types[$i]) ? ' selected="selected"' : '';
$s_size_options .= '<option value="' . $size_types[$i] . '"' . $selected . '>' . $size_types_text[$i] . '</option>';
}
return $s_size_options;
}
/**
* Generate list of groups (option fields without select)
*
* @param int $group_id The default group id to mark as selected
* @param array $exclude_ids The group ids to exclude from the list, false (default) if you whish to exclude no id
* @param int $manage_founder If set to false (default) all groups are returned, if 0 only those groups returned not being managed by founders only, if 1 only those groups returned managed by founders only.
*
* @return string The list of options.
*/
function group_select_options($group_id, $exclude_ids = false, $manage_founder = false)
{
global $db, $user, $config;
$exclude_sql = ($exclude_ids !== false && sizeof($exclude_ids)) ? 'WHERE ' . $db->sql_in_set('group_id', array_map('intval', $exclude_ids), true) : '';
$sql_and = (!$config['coppa_enable']) ? (($exclude_sql) ? ' AND ' : ' WHERE ') . "group_name <> 'REGISTERED_COPPA'" : '';
$sql_founder = ($manage_founder !== false) ? (($exclude_sql || $sql_and) ? ' AND ' : ' WHERE ') . 'group_founder_manage = ' . (int) $manage_founder : '';
$sql = 'SELECT group_id, group_name, group_type
FROM ' . GROUPS_TABLE . "
$exclude_sql
$sql_and
$sql_founder
ORDER BY group_type DESC, group_name ASC";
$result = $db->sql_query($sql);
$s_group_options = '';
while ($row = $db->sql_fetchrow($result))
{
$selected = ($row['group_id'] == $group_id) ? ' selected="selected"' : '';
$s_group_options .= '<option' . (($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '') . ' value="' . $row['group_id'] . '"' . $selected . '>' . (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']) . '</option>';
}
$db->sql_freeresult($result);
return $s_group_options;
}
/**
* Obtain authed forums list
*/
function get_forum_list($acl_list = 'f_list', $id_only = true, $postable_only = false, $no_cache = false)
{
global $db, $auth;
static $forum_rows;
if (!isset($forum_rows))
{
// This query is identical to the jumpbox one
$expire_time = ($no_cache) ? 0 : 600;
$sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
FROM ' . FORUMS_TABLE . '
ORDER BY left_id ASC';
$result = $db->sql_query($sql, $expire_time);
$forum_rows = array();
$right = $padding = 0;
$padding_store = array('0' => 0);
while ($row = $db->sql_fetchrow($result))
{
if ($row['left_id'] < $right)
{
$padding++;
$padding_store[$row['parent_id']] = $padding;
}
else if ($row['left_id'] > $right + 1)
{
// Ok, if the $padding_store for this parent is empty there is something wrong. For now we will skip over it.
// @todo digging deep to find out "how" this can happen.
$padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : $padding;
}
$right = $row['right_id'];
$row['padding'] = $padding;
$forum_rows[] = $row;
}
$db->sql_freeresult($result);
unset($padding_store);
}
$rowset = array();
foreach ($forum_rows as $row)
{
if ($postable_only && $row['forum_type'] != FORUM_POST)
{
continue;
}
if ($acl_list == '' || ($acl_list != '' && $auth->acl_gets($acl_list, $row['forum_id'])))
{
$rowset[] = ($id_only) ? (int) $row['forum_id'] : $row;
}
}
return $rowset;
}
/**
* Get forum branch
*/
function get_forum_branch($forum_id, $type = 'all', $order = 'descending', $include_forum = true)
{
global $db;
switch ($type)
{
case 'parents':
$condition = 'f1.left_id BETWEEN f2.left_id AND f2.right_id';
break;
case 'children':
$condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id';
break;
default:
$condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id OR f1.left_id BETWEEN f2.left_id AND f2.right_id';
break;
}
$rows = array();
$sql = 'SELECT f2.*
FROM ' . FORUMS_TABLE . ' f1
LEFT JOIN ' . FORUMS_TABLE . " f2 ON ($condition)
WHERE f1.forum_id = $forum_id
ORDER BY f2.left_id " . (($order == 'descending') ? 'ASC' : 'DESC');
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
if (!$include_forum && $row['forum_id'] == $forum_id)
{
continue;
}
$rows[] = $row;
}
$db->sql_freeresult($result);
return $rows;
}
/**
* Copies permissions from one forum to others
*
* @param int $src_forum_id The source forum we want to copy permissions from
* @param array $dest_forum_ids The destination forum(s) we want to copy to
* @param bool $clear_dest_perms True if destination permissions should be deleted
* @param bool $add_log True if log entry should be added
*
* @return bool False on error
*
* @author bantu
*/
function copy_forum_permissions($src_forum_id, $dest_forum_ids, $clear_dest_perms = true, $add_log = true)
{
global $db;
// Only one forum id specified
if (!is_array($dest_forum_ids))
{
$dest_forum_ids = array($dest_forum_ids);
}
// Make sure forum ids are integers
$src_forum_id = (int) $src_forum_id;
$dest_forum_ids = array_map('intval', $dest_forum_ids);
// No source forum or no destination forums specified
if (empty($src_forum_id) || empty($dest_forum_ids))
{
return false;
}
// Check if source forum exists
$sql = 'SELECT forum_name
FROM ' . FORUMS_TABLE . '
WHERE forum_id = ' . $src_forum_id;
$result = $db->sql_query($sql);
$src_forum_name = $db->sql_fetchfield('forum_name');
$db->sql_freeresult($result);
// Source forum doesn't exist
if (empty($src_forum_name))
{
return false;
}
// Check if destination forums exists
$sql = 'SELECT forum_id, forum_name
FROM ' . FORUMS_TABLE . '
WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
$result = $db->sql_query($sql);
$dest_forum_ids = $dest_forum_names = array();
while ($row = $db->sql_fetchrow($result))
{
$dest_forum_ids[] = (int) $row['forum_id'];
$dest_forum_names[] = $row['forum_name'];
}
$db->sql_freeresult($result);
// No destination forum exists
if (empty($dest_forum_ids))
{
return false;
}
// From the mysql documentation:
// Prior to MySQL 4.0.14, the target table of the INSERT statement cannot appear
// in the FROM clause of the SELECT part of the query. This limitation is lifted in 4.0.14.
// Due to this we stay on the safe side if we do the insertion "the manual way"
// Rowsets we're going to insert
$users_sql_ary = $groups_sql_ary = array();
// Query acl users table for source forum data
$sql = 'SELECT user_id, auth_option_id, auth_role_id, auth_setting
FROM ' . ACL_USERS_TABLE . '
WHERE forum_id = ' . $src_forum_id;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$row = array(
'user_id' => (int) $row['user_id'],
'auth_option_id' => (int) $row['auth_option_id'],
'auth_role_id' => (int) $row['auth_role_id'],
'auth_setting' => (int) $row['auth_setting'],
);
foreach ($dest_forum_ids as $dest_forum_id)
{
$users_sql_ary[] = $row + array('forum_id' => $dest_forum_id);
}
}
$db->sql_freeresult($result);
// Query acl groups table for source forum data
$sql = 'SELECT group_id, auth_option_id, auth_role_id, auth_setting
FROM ' . ACL_GROUPS_TABLE . '
WHERE forum_id = ' . $src_forum_id;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$row = array(
'group_id' => (int) $row['group_id'],
'auth_option_id' => (int) $row['auth_option_id'],
'auth_role_id' => (int) $row['auth_role_id'],
'auth_setting' => (int) $row['auth_setting'],
);
foreach ($dest_forum_ids as $dest_forum_id)
{
$groups_sql_ary[] = $row + array('forum_id' => $dest_forum_id);
}
}
$db->sql_freeresult($result);
$db->sql_transaction('begin');
// Clear current permissions of destination forums
if ($clear_dest_perms)
{
$sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
$db->sql_query($sql);
$sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
$db->sql_query($sql);
}
$db->sql_multi_insert(ACL_USERS_TABLE, $users_sql_ary);
$db->sql_multi_insert(ACL_GROUPS_TABLE, $groups_sql_ary);
if ($add_log)
{
add_log('admin', 'LOG_FORUM_COPIED_PERMISSIONS', $src_forum_name, implode(', ', $dest_forum_names));
}
$db->sql_transaction('commit');
return true;
}
/**
* Get physical file listing
*/
function filelist($rootdir, $dir = '', $type = 'gif|jpg|jpeg|png')
{
$matches = array($dir => array());
// Remove initial / if present
$rootdir = (substr($rootdir, 0, 1) == '/') ? substr($rootdir, 1) : $rootdir;
// Add closing / if not present
$rootdir = ($rootdir && substr($rootdir, -1) != '/') ? $rootdir . '/' : $rootdir;
// Remove initial / if present
$dir = (substr($dir, 0, 1) == '/') ? substr($dir, 1) : $dir;
// Add closing / if not present
$dir = ($dir && substr($dir, -1) != '/') ? $dir . '/' : $dir;
if (!is_dir($rootdir . $dir))
{
return $matches;
}
$dh = @opendir($rootdir . $dir);
if (!$dh)
{
return $matches;
}
while (($fname = readdir($dh)) !== false)
{
if (is_file("$rootdir$dir$fname"))
{
if (filesize("$rootdir$dir$fname") && preg_match('#\.' . $type . '$#i', $fname))
{
$matches[$dir][] = $fname;
}
}
else if ($fname[0] != '.' && is_dir("$rootdir$dir$fname"))
{
$matches += filelist($rootdir, "$dir$fname", $type);
}
}
closedir($dh);
return $matches;
}
/**
* Move topic(s)
*/
function move_topics($topic_ids, $forum_id, $auto_sync = true)
{
global $db;
if (empty($topic_ids))
{
return;
}
$forum_ids = array($forum_id);
if (!is_array($topic_ids))
{
$topic_ids = array($topic_ids);
}
$sql = 'DELETE FROM ' . TOPICS_TABLE . '
WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids) . '
AND forum_id = ' . $forum_id;
$db->sql_query($sql);
if ($auto_sync)
{
$sql = 'SELECT DISTINCT forum_id
FROM ' . TOPICS_TABLE . '
WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$forum_ids[] = $row['forum_id'];
}
$db->sql_freeresult($result);
}
$table_ary = array(TOPICS_TABLE, POSTS_TABLE, LOG_TABLE, DRAFTS_TABLE, TOPICS_TRACK_TABLE);
foreach ($table_ary as $table)
{
$sql = "UPDATE $table
SET forum_id = $forum_id
WHERE " . $db->sql_in_set('topic_id', $topic_ids);
$db->sql_query($sql);
}
unset($table_ary);
if ($auto_sync)
{
sync('forum', 'forum_id', $forum_ids, true, true);
unset($forum_ids);
}
}
/**
* Move post(s)
*/
function move_posts($post_ids, $topic_id, $auto_sync = true)
{
global $db;
if (!is_array($post_ids))
{
$post_ids = array($post_ids);
}
$forum_ids = array();
$topic_ids = array($topic_id);
$sql = 'SELECT DISTINCT topic_id, forum_id
FROM ' . POSTS_TABLE . '
WHERE ' . $db->sql_in_set('post_id', $post_ids);
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$forum_ids[] = (int) $row['forum_id'];
$topic_ids[] = (int) $row['topic_id'];
}
$db->sql_freeresult($result);
$sql = 'SELECT forum_id
FROM ' . TOPICS_TABLE . '
WHERE topic_id = ' . $topic_id;
$result = $db->sql_query($sql);
$forum_row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
if (!$forum_row)
{
trigger_error('NO_TOPIC');
}
$sql = 'UPDATE ' . POSTS_TABLE . '
SET forum_id = ' . (int) $forum_row['forum_id'] . ", topic_id = $topic_id
WHERE " . $db->sql_in_set('post_id', $post_ids);
$db->sql_query($sql);
$sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
SET topic_id = $topic_id, in_message = 0
WHERE " . $db->sql_in_set('post_msg_id', $post_ids);
$db->sql_query($sql);
if ($auto_sync)
{
$forum_ids[] = (int) $forum_row['forum_id'];
sync('topic_reported', 'topic_id', $topic_ids);
sync('topic_attachment', 'topic_id', $topic_ids);
sync('topic', 'topic_id', $topic_ids, true);
sync('forum', 'forum_id', $forum_ids, true, true);
}
// Update posted information
update_posted_info($topic_ids);
}
/**
* Remove topic(s)
*/
function delete_topics($where_type, $where_ids, $auto_sync = true, $post_count_sync = true, $call_delete_posts = true)
{
global $db, $config;
$approved_topics = 0;
$forum_ids = $topic_ids = array();
if ($where_type === 'range')
{
$where_clause = $where_ids;
}
else
{
$where_ids = (is_array($where_ids)) ? array_unique($where_ids) : array($where_ids);
if (!sizeof($where_ids))
{
return array('topics' => 0, 'posts' => 0);
}
$where_clause = $db->sql_in_set($where_type, $where_ids);
}
// Making sure that delete_posts does not call delete_topics again...
$return = array(
'posts' => ($call_delete_posts) ? delete_posts($where_type, $where_ids, false, true, $post_count_sync, false) : 0,
);
$sql = 'SELECT topic_id, forum_id, topic_approved, topic_moved_id
FROM ' . TOPICS_TABLE . '
WHERE ' . $where_clause;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$forum_ids[] = $row['forum_id'];
$topic_ids[] = $row['topic_id'];
if ($row['topic_approved'] && !$row['topic_moved_id'])
{
$approved_topics++;
}
}
$db->sql_freeresult($result);
$return['topics'] = sizeof($topic_ids);
if (!sizeof($topic_ids))
{
return $return;
}
$db->sql_transaction('begin');
$table_ary = array(BOOKMARKS_TABLE, TOPICS_TRACK_TABLE, TOPICS_POSTED_TABLE, POLL_VOTES_TABLE, POLL_OPTIONS_TABLE, TOPICS_WATCH_TABLE, TOPICS_TABLE);
foreach ($table_ary as $table)
{
$sql = "DELETE FROM $table
WHERE " . $db->sql_in_set('topic_id', $topic_ids);
$db->sql_query($sql);
}
unset($table_ary);
$moved_topic_ids = array();
// update the other forums
$sql = 'SELECT topic_id, forum_id
FROM ' . TOPICS_TABLE . '
WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids);
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$forum_ids[] = $row['forum_id'];
$moved_topic_ids[] = $row['topic_id'];
}
$db->sql_freeresult($result);
if (sizeof($moved_topic_ids))
{
$sql = 'DELETE FROM ' . TOPICS_TABLE . '
WHERE ' . $db->sql_in_set('topic_id', $moved_topic_ids);
$db->sql_query($sql);
}
$db->sql_transaction('commit');
if ($auto_sync)
{
sync('forum', 'forum_id', array_unique($forum_ids), true, true);
sync('topic_reported', $where_type, $where_ids);
}
if ($approved_topics)
{
set_config_count('num_topics', $approved_topics * (-1), true);
}
return $return;
}
/**
* Remove post(s)
*/
function delete_posts($where_type, $where_ids, $auto_sync = true, $posted_sync = true, $post_count_sync = true, $call_delete_topics = true)
{
global $db, $config, $phpbb_root_path, $phpEx;
if ($where_type === 'range')
{
$where_clause = $where_ids;
}
else
{
if (is_array($where_ids))
{
$where_ids = array_unique($where_ids);
}
else
{
$where_ids = array($where_ids);
}
if (!sizeof($where_ids))
{
return false;
}
$where_ids = array_map('intval', $where_ids);
/* Possible code for splitting post deletion
if (sizeof($where_ids) >= 1001)
{
// Split into chunks of 1000
$chunks = array_chunk($where_ids, 1000);
foreach ($chunks as $_where_ids)
{
delete_posts($where_type, $_where_ids, $auto_sync, $posted_sync, $post_count_sync, $call_delete_topics);
}
return;
}*/
$where_clause = $db->sql_in_set($where_type, $where_ids);
}
$approved_posts = 0;
$post_ids = $topic_ids = $forum_ids = $post_counts = $remove_topics = array();
$sql = 'SELECT post_id, poster_id, post_approved, post_postcount, topic_id, forum_id
FROM ' . POSTS_TABLE . '
WHERE ' . $where_clause;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$post_ids[] = (int) $row['post_id'];
$poster_ids[] = (int) $row['poster_id'];
$topic_ids[] = (int) $row['topic_id'];
$forum_ids[] = (int) $row['forum_id'];
if ($row['post_postcount'] && $post_count_sync && $row['post_approved'])
{
$post_counts[$row['poster_id']] = (!empty($post_counts[$row['poster_id']])) ? $post_counts[$row['poster_id']] + 1 : 1;
}
if ($row['post_approved'])
{
$approved_posts++;
}
}
$db->sql_freeresult($result);
if (!sizeof($post_ids))
{
return false;
}
$db->sql_transaction('begin');
$table_ary = array(POSTS_TABLE, REPORTS_TABLE);
foreach ($table_ary as $table)
{
$sql = "DELETE FROM $table
WHERE " . $db->sql_in_set('post_id', $post_ids);
$db->sql_query($sql);
}
unset($table_ary);
// Adjust users post counts
if (sizeof($post_counts) && $post_count_sync)
{
foreach ($post_counts as $poster_id => $substract)
{
$sql = 'UPDATE ' . USERS_TABLE . '
SET user_posts = 0
WHERE user_id = ' . $poster_id . '
AND user_posts < ' . $substract;
$db->sql_query($sql);
$sql = 'UPDATE ' . USERS_TABLE . '
SET user_posts = user_posts - ' . $substract . '
WHERE user_id = ' . $poster_id . '
AND user_posts >= ' . $substract;
$db->sql_query($sql);
}
}
// Remove topics now having no posts?
if (sizeof($topic_ids))
{
$sql = 'SELECT topic_id
FROM ' . POSTS_TABLE . '
WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
GROUP BY topic_id';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$remove_topics[] = $row['topic_id'];
}
$db->sql_freeresult($result);
// Actually, those not within remove_topics should be removed. ;)
$remove_topics = array_diff($topic_ids, $remove_topics);
}
// Remove the message from the search index
$search_type = basename($config['search_type']);
if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
{
trigger_error('NO_SUCH_SEARCH_MODULE');
}
include_once("{$phpbb_root_path}includes/search/$search_type.$phpEx");
$error = false;
$search = new $search_type($error);
if ($error)
{
trigger_error($error);
}
$search->index_remove($post_ids, $poster_ids, $forum_ids);
delete_attachments('post', $post_ids, false);
$db->sql_transaction('commit');
// Resync topics_posted table
if ($posted_sync)
{
update_posted_info($topic_ids);
}
if ($auto_sync)
{
sync('topic_reported', 'topic_id', $topic_ids);
sync('topic', 'topic_id', $topic_ids, true);
sync('forum', 'forum_id', $forum_ids, true, true);
}
if ($approved_posts)
{
set_config_count('num_posts', $approved_posts * (-1), true);
}
// We actually remove topics now to not be inconsistent (the delete_topics function calls this function too)
if (sizeof($remove_topics) && $call_delete_topics)
{
delete_topics('topic_id', $remove_topics, $auto_sync, $post_count_sync, false);
}
return sizeof($post_ids);
}
/**
* Delete Attachments
*
* @param string $mode can be: post|message|topic|attach|user
* @param mixed $ids can be: post_ids, message_ids, topic_ids, attach_ids, user_ids
* @param bool $resync set this to false if you are deleting posts or topics
*/
function delete_attachments($mode, $ids, $resync = true)
{
global $db, $config;
// 0 is as bad as an empty array
if (empty($ids))
{
return false;
}
if (is_array($ids))
{
$ids = array_unique($ids);
$ids = array_map('intval', $ids);
}
else
{
$ids = array((int) $ids);
}
$sql_where = '';
switch ($mode)
{
case 'post':
case 'message':
$sql_id = 'post_msg_id';
$sql_where = ' AND in_message = ' . ($mode == 'message' ? 1 : 0);
break;
case 'topic':
$sql_id = 'topic_id';
break;
case 'user':
$sql_id = 'poster_id';
break;
case 'attach':
default:
$sql_id = 'attach_id';
$mode = 'attach';
break;
}
$post_ids = $message_ids = $topic_ids = $physical = array();
// Collect post and topic ids for later use if we need to touch remaining entries (if resync is enabled)
$sql = 'SELECT post_msg_id, topic_id, in_message, physical_filename, thumbnail, filesize, is_orphan
FROM ' . ATTACHMENTS_TABLE . '
WHERE ' . $db->sql_in_set($sql_id, $ids);
$sql .= $sql_where;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
// We only need to store post/message/topic ids if resync is enabled and the file is not orphaned
if ($resync && !$row['is_orphan'])
{
if (!$row['in_message'])
{
$post_ids[] = $row['post_msg_id'];
$topic_ids[] = $row['topic_id'];
}
else
{
$message_ids[] = $row['post_msg_id'];
}
}
$physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize'], 'is_orphan' => $row['is_orphan']);
}
$db->sql_freeresult($result);
// Delete attachments
$sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
WHERE ' . $db->sql_in_set($sql_id, $ids);
$sql .= $sql_where;
$db->sql_query($sql);
$num_deleted = $db->sql_affectedrows();
if (!$num_deleted)
{
return 0;
}
// Delete attachments from filesystem
$space_removed = $files_removed = 0;
foreach ($physical as $file_ary)