phpBB
Statistics
| Revision:

root / trunk / phpBB / includes / functions_admin.php

History | View | Annotate | Download (88 kB)

1 2286 psotfx
<?php
2 7736 acydburn
/**
3 5114 acydburn
*
4 5553 acydburn
* @package acp
5 7736 acydburn
* @copyright (c) 2005 phpBB Group
6 11653 git-gate
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7 5114 acydburn
*
8 5114 acydburn
*/
9 2286 psotfx
10 5114 acydburn
/**
11 8146 acydburn
* @ignore
12 8146 acydburn
*/
13 8146 acydburn
if (!defined('IN_PHPBB'))
14 8146 acydburn
{
15 8146 acydburn
        exit;
16 8146 acydburn
}
17 8146 acydburn
18 8146 acydburn
/**
19 9606 nickvergessen
* Recalculate Nested Sets
20 9605 nickvergessen
*
21 9605 nickvergessen
* @param int        $new_id        first left_id (should start with 1)
22 9605 nickvergessen
* @param string        $pkey        primary key-column (containing the id for the parent_id of the children)
23 9605 nickvergessen
* @param string        $table        constant or fullname of the table
24 9606 nickvergessen
* @param int        $parent_id parent_id of the current set (default = 0)
25 9605 nickvergessen
* @param array        $where        contains strings to compare closer on the where statement (additional)
26 9605 nickvergessen
*
27 9605 nickvergessen
* @author EXreaction
28 9605 nickvergessen
*/
29 9606 nickvergessen
function recalc_nested_sets(&$new_id, $pkey, $table, $parent_id = 0, $where = array())
30 5255 acydburn
{
31 5255 acydburn
        global $db;
32 5255 acydburn
33 9605 nickvergessen
        $sql = 'SELECT *
34 9605 nickvergessen
                FROM ' . $table . '
35 9605 nickvergessen
                WHERE parent_id = ' . (int) $parent_id .
36 9605 nickvergessen
                ((!empty($where)) ? ' AND ' . implode(' AND ', $where) : '') . '
37 9605 nickvergessen
                ORDER BY left_id ASC';
38 5272 acydburn
        $result = $db->sql_query($sql);
39 9605 nickvergessen
        while ($row = $db->sql_fetchrow($result))
40 5272 acydburn
        {
41 9605 nickvergessen
                // First we update the left_id for this module
42 9605 nickvergessen
                if ($row['left_id'] != $new_id)
43 5255 acydburn
                {
44 9605 nickvergessen
                        $db->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', array('left_id' => $new_id)) . " WHERE $pkey = {$row[$pkey]}");
45 9605 nickvergessen
                }
46 9605 nickvergessen
                $new_id++;
47 5255 acydburn
48 9605 nickvergessen
                // Then we go through any children and update their left/right id's
49 9606 nickvergessen
                recalc_nested_sets($new_id, $pkey, $table, $row[$pkey], $where);
50 5255 acydburn
51 9605 nickvergessen
                // Then we come back and update the right_id for this module
52 9605 nickvergessen
                if ($row['right_id'] != $new_id)
53 5255 acydburn
                {
54 9605 nickvergessen
                        $db->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', array('right_id' => $new_id)) . " WHERE $pkey = {$row[$pkey]}");
55 5255 acydburn
                }
56 9605 nickvergessen
                $new_id++;
57 5255 acydburn
        }
58 9605 nickvergessen
        $db->sql_freeresult($result);
59 5255 acydburn
}
60 5255 acydburn
61 5255 acydburn
/**
62 5114 acydburn
* Simple version of jumpbox, just lists authed forums
63 5114 acydburn
*/
64 6285 grahamje
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)
65 2286 psotfx
{
66 3099 psotfx
        global $db, $user, $auth;
67 2286 psotfx
68 5114 acydburn
        // This query is identical to the jumpbox one
69 9965 acydburn
        $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, forum_flags, forum_options, left_id, right_id
70 5114 acydburn
                FROM ' . FORUMS_TABLE . '
71 5114 acydburn
                ORDER BY left_id ASC';
72 7599 davidmj
        $result = $db->sql_query($sql, 600);
73 5114 acydburn
74 6930 acydburn
        $right = 0;
75 3977 psotfx
        $padding_store = array('0' => '');
76 5517 acydburn
        $padding = '';
77 5517 acydburn
        $forum_list = ($return_array) ? array() : '';
78 4813 psotfx
79 5114 acydburn
        // Sometimes it could happen that forums will be displayed here not be displayed within the index page
80 5114 acydburn
        // This is the result of forums not displayed at index, having list permissions and a parent of a forum with no permissions.
81 5114 acydburn
        // If this happens, the padding could be "broken"
82 5114 acydburn
83 5114 acydburn
        while ($row = $db->sql_fetchrow($result))
84 2286 psotfx
        {
85 3099 psotfx
                if ($row['left_id'] < $right)
86 2944 psotfx
                {
87 5114 acydburn
                        $padding .= '&nbsp; &nbsp;';
88 3977 psotfx
                        $padding_store[$row['parent_id']] = $padding;
89 2944 psotfx
                }
90 3099 psotfx
                else if ($row['left_id'] > $right + 1)
91 3099 psotfx
                {
92 4898 acydburn
                        $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
93 3099 psotfx
                }
94 2942 psotfx
95 2944 psotfx
                $right = $row['right_id'];
96 6930 acydburn
                $disabled = false;
97 2942 psotfx
98 10637 git-gate
                if (!$ignore_acl && $auth->acl_gets(array('f_list', 'a_forum', 'a_forumadd', 'a_forumdel'), $row['forum_id']))
99 5114 acydburn
                {
100 10558 git-gate
                        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'])))
101 6930 acydburn
                        {
102 6930 acydburn
                                $disabled = true;
103 6930 acydburn
                        }
104 5114 acydburn
                }
105 10558 git-gate
                else if (!$ignore_acl)
106 10558 git-gate
                {
107 10558 git-gate
                        continue;
108 10558 git-gate
                }
109 3099 psotfx
110 6930 acydburn
                if (
111 6930 acydburn
                        ((is_array($ignore_id) && in_array($row['forum_id'], $ignore_id)) || $row['forum_id'] == $ignore_id)
112 6930 acydburn
                        ||
113 5114 acydburn
                        // Non-postable forum with no subforums, don't display
114 6930 acydburn
                        ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']) && $ignore_emptycat)
115 6930 acydburn
                        ||
116 6930 acydburn
                        ($row['forum_type'] != FORUM_POST && $ignore_nonpost)
117 6930 acydburn
                        )
118 3099 psotfx
                {
119 6930 acydburn
                        $disabled = true;
120 3099 psotfx
                }
121 3099 psotfx
122 5517 acydburn
                if ($return_array)
123 5517 acydburn
                {
124 6650 acydburn
                        // Include some more information...
125 5517 acydburn
                        $selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? true : false) : (($row['forum_id'] == $select_id) ? true : false);
126 6930 acydburn
                        $forum_list[$row['forum_id']] = array_merge(array('padding' => $padding, 'selected' => ($selected && !$disabled), 'disabled' => $disabled), $row);
127 5517 acydburn
                }
128 5517 acydburn
                else
129 5517 acydburn
                {
130 5517 acydburn
                        $selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? ' selected="selected"' : '') : (($row['forum_id'] == $select_id) ? ' selected="selected"' : '');
131 7456 acydburn
                        $forum_list .= '<option value="' . $row['forum_id'] . '"' . (($disabled) ? ' disabled="disabled" class="disabled-option"' : $selected) . '>' . $padding . $row['forum_name'] . '</option>';
132 5517 acydburn
                }
133 3099 psotfx
        }
134 5929 acydburn
        $db->sql_freeresult($result);
135 5114 acydburn
        unset($padding_store);
136 3099 psotfx
137 3099 psotfx
        return $forum_list;
138 2286 psotfx
}
139 2286 psotfx
140 5114 acydburn
/**
141 6015 acydburn
* Generate size select options
142 5114 acydburn
*/
143 5486 acydburn
function size_select_options($size_compare)
144 4634 acydburn
{
145 4634 acydburn
        global $user;
146 4634 acydburn
147 8389 acydburn
        $size_types_text = array($user->lang['BYTES'], $user->lang['KIB'], $user->lang['MIB']);
148 4634 acydburn
        $size_types = array('b', 'kb', 'mb');
149 4634 acydburn
150 5486 acydburn
        $s_size_options = '';
151 6015 acydburn
152 4984 acydburn
        for ($i = 0, $size = sizeof($size_types_text); $i < $size; $i++)
153 4634 acydburn
        {
154 4634 acydburn
                $selected = ($size_compare == $size_types[$i]) ? ' selected="selected"' : '';
155 5486 acydburn
                $s_size_options .= '<option value="' . $size_types[$i] . '"' . $selected . '>' . $size_types_text[$i] . '</option>';
156 4634 acydburn
        }
157 6015 acydburn
158 5486 acydburn
        return $s_size_options;
159 5486 acydburn
}
160 4634 acydburn
161 5486 acydburn
/**
162 6816 acydburn
* Generate list of groups (option fields without select)
163 6816 acydburn
*
164 6816 acydburn
* @param int $group_id The default group id to mark as selected
165 6816 acydburn
* @param array $exclude_ids The group ids to exclude from the list, false (default) if you whish to exclude no id
166 6816 acydburn
* @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.
167 6816 acydburn
*
168 6816 acydburn
* @return string The list of options.
169 5486 acydburn
*/
170 6816 acydburn
function group_select_options($group_id, $exclude_ids = false, $manage_founder = false)
171 5486 acydburn
{
172 5744 acydburn
        global $db, $user, $config;
173 5486 acydburn
174 6271 acydburn
        $exclude_sql = ($exclude_ids !== false && sizeof($exclude_ids)) ? 'WHERE ' . $db->sql_in_set('group_id', array_map('intval', $exclude_ids), true) : '';
175 6436 acydburn
        $sql_and = (!$config['coppa_enable']) ? (($exclude_sql) ? ' AND ' : ' WHERE ') . "group_name <> 'REGISTERED_COPPA'" : '';
176 6816 acydburn
        $sql_founder = ($manage_founder !== false) ? (($exclude_sql || $sql_and) ? ' AND ' : ' WHERE ') . 'group_founder_manage = ' . (int) $manage_founder : '';
177 5744 acydburn
178 8146 acydburn
        $sql = 'SELECT group_id, group_name, group_type
179 5744 acydburn
                FROM ' . GROUPS_TABLE . "
180 5744 acydburn
                $exclude_sql
181 5744 acydburn
                $sql_and
182 6816 acydburn
                $sql_founder
183 5744 acydburn
                ORDER BY group_type DESC, group_name ASC";
184 5486 acydburn
        $result = $db->sql_query($sql);
185 5486 acydburn
186 5486 acydburn
        $s_group_options = '';
187 5486 acydburn
        while ($row = $db->sql_fetchrow($result))
188 5486 acydburn
        {
189 5486 acydburn
                $selected = ($row['group_id'] == $group_id) ? ' selected="selected"' : '';
190 5486 acydburn
                $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>';
191 5486 acydburn
        }
192 5486 acydburn
        $db->sql_freeresult($result);
193 6015 acydburn
194 5486 acydburn
        return $s_group_options;
195 4634 acydburn
}
196 4634 acydburn
197 5114 acydburn
/**
198 5114 acydburn
* Obtain authed forums list
199 5114 acydburn
*/
200 5313 acydburn
function get_forum_list($acl_list = 'f_list', $id_only = true, $postable_only = false, $no_cache = false)
201 3781 ludovic_arnaud
{
202 3833 psotfx
        global $db, $auth;
203 3781 ludovic_arnaud
        static $forum_rows;
204 3781 ludovic_arnaud
205 3781 ludovic_arnaud
        if (!isset($forum_rows))
206 3781 ludovic_arnaud
        {
207 3781 ludovic_arnaud
                // This query is identical to the jumpbox one
208 7327 davidmj
                $expire_time = ($no_cache) ? 0 : 600;
209 6015 acydburn
210 6135 acydburn
                $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
211 3781 ludovic_arnaud
                        FROM ' . FORUMS_TABLE . '
212 3781 ludovic_arnaud
                        ORDER BY left_id ASC';
213 3781 ludovic_arnaud
                $result = $db->sql_query($sql, $expire_time);
214 4813 psotfx
215 6436 acydburn
                $forum_rows = array();
216 7775 davidmj
217 7775 davidmj
                $right = $padding = 0;
218 7775 davidmj
                $padding_store = array('0' => 0);
219 7775 davidmj
220 3781 ludovic_arnaud
                while ($row = $db->sql_fetchrow($result))
221 3781 ludovic_arnaud
                {
222 7775 davidmj
                        if ($row['left_id'] < $right)
223 7775 davidmj
                        {
224 7775 davidmj
                                $padding++;
225 7775 davidmj
                                $padding_store[$row['parent_id']] = $padding;
226 7775 davidmj
                        }
227 7775 davidmj
                        else if ($row['left_id'] > $right + 1)
228 7775 davidmj
                        {
229 7775 davidmj
                                // Ok, if the $padding_store for this parent is empty there is something wrong. For now we will skip over it.
230 7775 davidmj
                                // @todo digging deep to find out "how" this can happen.
231 7775 davidmj
                                $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : $padding;
232 7775 davidmj
                        }
233 7775 davidmj
234 7775 davidmj
                        $right = $row['right_id'];
235 7775 davidmj
                        $row['padding'] = $padding;
236 7775 davidmj
237 3781 ludovic_arnaud
                        $forum_rows[] = $row;
238 3781 ludovic_arnaud
                }
239 5494 acydburn
                $db->sql_freeresult($result);
240 7775 davidmj
                unset($padding_store);
241 3781 ludovic_arnaud
        }
242 3781 ludovic_arnaud
243 3781 ludovic_arnaud
        $rowset = array();
244 3781 ludovic_arnaud
        foreach ($forum_rows as $row)
245 3781 ludovic_arnaud
        {
246 4671 ludovic_arnaud
                if ($postable_only && $row['forum_type'] != FORUM_POST)
247 3781 ludovic_arnaud
                {
248 3781 ludovic_arnaud
                        continue;
249 3781 ludovic_arnaud
                }
250 3833 psotfx
251 3833 psotfx
                if ($acl_list == '' || ($acl_list != '' && $auth->acl_gets($acl_list, $row['forum_id'])))
252 3781 ludovic_arnaud
                {
253 10159 bantu
                        $rowset[] = ($id_only) ? (int) $row['forum_id'] : $row;
254 3781 ludovic_arnaud
                }
255 3781 ludovic_arnaud
        }
256 3781 ludovic_arnaud
257 3781 ludovic_arnaud
        return $rowset;
258 3781 ludovic_arnaud
}
259 3781 ludovic_arnaud
260 5114 acydburn
/**
261 5114 acydburn
* Get forum branch
262 5114 acydburn
*/
263 5313 acydburn
function get_forum_branch($forum_id, $type = 'all', $order = 'descending', $include_forum = true)
264 4039 ludovic_arnaud
{
265 4039 ludovic_arnaud
        global $db;
266 4039 ludovic_arnaud
267 4039 ludovic_arnaud
        switch ($type)
268 4039 ludovic_arnaud
        {
269 4039 ludovic_arnaud
                case 'parents':
270 4039 ludovic_arnaud
                        $condition = 'f1.left_id BETWEEN f2.left_id AND f2.right_id';
271 6015 acydburn
                break;
272 4039 ludovic_arnaud
273 4039 ludovic_arnaud
                case 'children':
274 4039 ludovic_arnaud
                        $condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id';
275 6015 acydburn
                break;
276 4039 ludovic_arnaud
277 4039 ludovic_arnaud
                default:
278 4039 ludovic_arnaud
                        $condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id OR f1.left_id BETWEEN f2.left_id AND f2.right_id';
279 6015 acydburn
                break;
280 4039 ludovic_arnaud
        }
281 4039 ludovic_arnaud
282 4039 ludovic_arnaud
        $rows = array();
283 4039 ludovic_arnaud
284 4039 ludovic_arnaud
        $sql = 'SELECT f2.*
285 5313 acydburn
                FROM ' . FORUMS_TABLE . ' f1
286 5313 acydburn
                LEFT JOIN ' . FORUMS_TABLE . " f2 ON ($condition)
287 4039 ludovic_arnaud
                WHERE f1.forum_id = $forum_id
288 4039 ludovic_arnaud
                ORDER BY f2.left_id " . (($order == 'descending') ? 'ASC' : 'DESC');
289 4039 ludovic_arnaud
        $result = $db->sql_query($sql);
290 4039 ludovic_arnaud
291 4039 ludovic_arnaud
        while ($row = $db->sql_fetchrow($result))
292 4039 ludovic_arnaud
        {
293 4039 ludovic_arnaud
                if (!$include_forum && $row['forum_id'] == $forum_id)
294 4039 ludovic_arnaud
                {
295 4039 ludovic_arnaud
                        continue;
296 4039 ludovic_arnaud
                }
297 4039 ludovic_arnaud
298 4039 ludovic_arnaud
                $rows[] = $row;
299 4039 ludovic_arnaud
        }
300 4039 ludovic_arnaud
        $db->sql_freeresult($result);
301 4039 ludovic_arnaud
302 4039 ludovic_arnaud
        return $rows;
303 4039 ludovic_arnaud
}
304 4039 ludovic_arnaud
305 5114 acydburn
/**
306 9887 bantu
* Copies permissions from one forum to others
307 9887 bantu
*
308 9887 bantu
* @param int        $src_forum_id                The source forum we want to copy permissions from
309 9887 bantu
* @param array        $dest_forum_ids                The destination forum(s) we want to copy to
310 9887 bantu
* @param bool        $clear_dest_perms        True if destination permissions should be deleted
311 9887 bantu
* @param bool        $add_log                        True if log entry should be added
312 9887 bantu
*
313 9887 bantu
* @return bool                                                False on error
314 9887 bantu
*
315 9887 bantu
* @author bantu
316 9887 bantu
*/
317 9887 bantu
function copy_forum_permissions($src_forum_id, $dest_forum_ids, $clear_dest_perms = true, $add_log = true)
318 9887 bantu
{
319 9887 bantu
        global $db;
320 9887 bantu
321 9887 bantu
        // Only one forum id specified
322 9887 bantu
        if (!is_array($dest_forum_ids))
323 9887 bantu
        {
324 9887 bantu
                $dest_forum_ids = array($dest_forum_ids);
325 9887 bantu
        }
326 9887 bantu
327 9887 bantu
        // Make sure forum ids are integers
328 9887 bantu
        $src_forum_id = (int) $src_forum_id;
329 9887 bantu
        $dest_forum_ids = array_map('intval', $dest_forum_ids);
330 9887 bantu
331 9887 bantu
        // No source forum or no destination forums specified
332 9887 bantu
        if (empty($src_forum_id) || empty($dest_forum_ids))
333 9887 bantu
        {
334 9887 bantu
                return false;
335 9887 bantu
        }
336 9887 bantu
337 10042 terrafrost
        // Check if source forum exists
338 9887 bantu
        $sql = 'SELECT forum_name
339 9887 bantu
                FROM ' . FORUMS_TABLE . '
340 9887 bantu
                WHERE forum_id = ' . $src_forum_id;
341 9887 bantu
        $result = $db->sql_query($sql);
342 9887 bantu
        $src_forum_name = $db->sql_fetchfield('forum_name');
343 10042 terrafrost
        $db->sql_freeresult($result);
344 9887 bantu
345 9887 bantu
        // Source forum doesn't exist
346 9887 bantu
        if (empty($src_forum_name))
347 9887 bantu
        {
348 9887 bantu
                return false;
349 9887 bantu
        }
350 9887 bantu
351 9887 bantu
        // Check if destination forums exists
352 9887 bantu
        $sql = 'SELECT forum_id, forum_name
353 9887 bantu
                FROM ' . FORUMS_TABLE . '
354 9887 bantu
                WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
355 9887 bantu
        $result = $db->sql_query($sql);
356 9887 bantu
357 10042 terrafrost
        $dest_forum_ids = $dest_forum_names = array();
358 9887 bantu
        while ($row = $db->sql_fetchrow($result))
359 9887 bantu
        {
360 9887 bantu
                $dest_forum_ids[]        = (int) $row['forum_id'];
361 9887 bantu
                $dest_forum_names[]        = $row['forum_name'];
362 9887 bantu
        }
363 9887 bantu
        $db->sql_freeresult($result);
364 9887 bantu
365 9887 bantu
        // No destination forum exists
366 9887 bantu
        if (empty($dest_forum_ids))
367 9887 bantu
        {
368 9887 bantu
                return false;
369 9887 bantu
        }
370 9887 bantu
371 9887 bantu
        // From the mysql documentation:
372 9887 bantu
        // Prior to MySQL 4.0.14, the target table of the INSERT statement cannot appear
373 9887 bantu
        // in the FROM clause of the SELECT part of the query. This limitation is lifted in 4.0.14.
374 9887 bantu
        // Due to this we stay on the safe side if we do the insertion "the manual way"
375 9887 bantu
376 9887 bantu
        // Rowsets we're going to insert
377 9887 bantu
        $users_sql_ary = $groups_sql_ary = array();
378 9887 bantu
379 9887 bantu
        // Query acl users table for source forum data
380 9887 bantu
        $sql = 'SELECT user_id, auth_option_id, auth_role_id, auth_setting
381 9887 bantu
                FROM ' . ACL_USERS_TABLE . '
382 9887 bantu
                WHERE forum_id = ' . $src_forum_id;
383 9887 bantu
        $result = $db->sql_query($sql);
384 9887 bantu
385 9887 bantu
        while ($row = $db->sql_fetchrow($result))
386 9887 bantu
        {
387 9887 bantu
                $row = array(
388 9887 bantu
                        'user_id'                        => (int) $row['user_id'],
389 9887 bantu
                        'auth_option_id'        => (int) $row['auth_option_id'],
390 9887 bantu
                        'auth_role_id'                => (int) $row['auth_role_id'],
391 9887 bantu
                        'auth_setting'                => (int) $row['auth_setting'],
392 9887 bantu
                );
393 9887 bantu
394 9887 bantu
                foreach ($dest_forum_ids as $dest_forum_id)
395 9887 bantu
                {
396 9887 bantu
                        $users_sql_ary[] = $row + array('forum_id' => $dest_forum_id);
397 9887 bantu
                }
398 9887 bantu
        }
399 9887 bantu
        $db->sql_freeresult($result);
400 9887 bantu
401 9887 bantu
        // Query acl groups table for source forum data
402 9887 bantu
        $sql = 'SELECT group_id, auth_option_id, auth_role_id, auth_setting
403 9887 bantu
                FROM ' . ACL_GROUPS_TABLE . '
404 9887 bantu
                WHERE forum_id = ' . $src_forum_id;
405 9887 bantu
        $result = $db->sql_query($sql);
406 9887 bantu
407 9887 bantu
        while ($row = $db->sql_fetchrow($result))
408 9887 bantu
        {
409 9887 bantu
                $row = array(
410 9887 bantu
                        'group_id'                        => (int) $row['group_id'],
411 9887 bantu
                        'auth_option_id'        => (int) $row['auth_option_id'],
412 9887 bantu
                        'auth_role_id'                => (int) $row['auth_role_id'],
413 9887 bantu
                        'auth_setting'                => (int) $row['auth_setting'],
414 9887 bantu
                );
415 9887 bantu
416 9887 bantu
                foreach ($dest_forum_ids as $dest_forum_id)
417 9887 bantu
                {
418 9887 bantu
                        $groups_sql_ary[] = $row + array('forum_id' => $dest_forum_id);
419 9887 bantu
                }
420 9887 bantu
        }
421 9887 bantu
        $db->sql_freeresult($result);
422 9887 bantu
423 9887 bantu
        $db->sql_transaction('begin');
424 9887 bantu
425 9887 bantu
        // Clear current permissions of destination forums
426 9887 bantu
        if ($clear_dest_perms)
427 9887 bantu
        {
428 9887 bantu
                $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
429 9887 bantu
                        WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
430 9887 bantu
                $db->sql_query($sql);
431 9887 bantu
432 9887 bantu
                $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
433 9887 bantu
                        WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
434 9887 bantu
                $db->sql_query($sql);
435 9887 bantu
        }
436 9887 bantu
437 9887 bantu
        $db->sql_multi_insert(ACL_USERS_TABLE, $users_sql_ary);
438 9887 bantu
        $db->sql_multi_insert(ACL_GROUPS_TABLE, $groups_sql_ary);
439 9887 bantu
440 9887 bantu
        if ($add_log)
441 9887 bantu
        {
442 9887 bantu
                add_log('admin', 'LOG_FORUM_COPIED_PERMISSIONS', $src_forum_name, implode(', ', $dest_forum_names));
443 9887 bantu
        }
444 9887 bantu
445 9887 bantu
        $db->sql_transaction('commit');
446 9887 bantu
447 9887 bantu
        return true;
448 9887 bantu
}
449 9887 bantu
450 9887 bantu
/**
451 5114 acydburn
* Get physical file listing
452 5114 acydburn
*/
453 4090 psotfx
function filelist($rootdir, $dir = '', $type = 'gif|jpg|jpeg|png')
454 4326 psotfx
{
455 9565 bantu
        $matches = array($dir => array());
456 4090 psotfx
457 4354 psotfx
        // Remove initial / if present
458 4354 psotfx
        $rootdir = (substr($rootdir, 0, 1) == '/') ? substr($rootdir, 1) : $rootdir;
459 6015 acydburn
        // Add closing / if not present
460 4354 psotfx
        $rootdir = ($rootdir && substr($rootdir, -1) != '/') ? $rootdir . '/' : $rootdir;
461 4354 psotfx
462 4354 psotfx
        // Remove initial / if present
463 4354 psotfx
        $dir = (substr($dir, 0, 1) == '/') ? substr($dir, 1) : $dir;
464 6015 acydburn
        // Add closing / if not present
465 4354 psotfx
        $dir = ($dir && substr($dir, -1) != '/') ? $dir . '/' : $dir;
466 4354 psotfx
467 5315 acydburn
        if (!is_dir($rootdir . $dir))
468 5315 acydburn
        {
469 7884 acydburn
                return $matches;
470 5315 acydburn
        }
471 5315 acydburn
472 6912 acydburn
        $dh = @opendir($rootdir . $dir);
473 6912 acydburn
474 6912 acydburn
        if (!$dh)
475 6912 acydburn
        {
476 7884 acydburn
                return $matches;
477 6912 acydburn
        }
478 6912 acydburn
479 4430 psotfx
        while (($fname = readdir($dh)) !== false)
480 4090 psotfx
        {
481 4354 psotfx
                if (is_file("$rootdir$dir$fname"))
482 4090 psotfx
                {
483 4354 psotfx
                        if (filesize("$rootdir$dir$fname") && preg_match('#\.' . $type . '$#i', $fname))
484 4354 psotfx
                        {
485 4354 psotfx
                                $matches[$dir][] = $fname;
486 4354 psotfx
                        }
487 4090 psotfx
                }
488 6459 acydburn
                else if ($fname[0] != '.' && is_dir("$rootdir$dir$fname"))
489 4090 psotfx
                {
490 4354 psotfx
                        $matches += filelist($rootdir, "$dir$fname", $type);
491 4090 psotfx
                }
492 4090 psotfx
        }
493 4090 psotfx
        closedir($dh);
494 4090 psotfx
495 4090 psotfx
        return $matches;
496 4090 psotfx
}
497 4090 psotfx
498 6152 naderman
/**
499 5114 acydburn
* Move topic(s)
500 5114 acydburn
*/
501 4920 acydburn
function move_topics($topic_ids, $forum_id, $auto_sync = true)
502 2286 psotfx
{
503 2286 psotfx
        global $db;
504 2286 psotfx
505 5933 acydburn
        if (empty($topic_ids))
506 5933 acydburn
        {
507 5933 acydburn
                return;
508 5933 acydburn
        }
509 5933 acydburn
510 3737 ludovic_arnaud
        $forum_ids = array($forum_id);
511 6015 acydburn
512 5933 acydburn
        if (!is_array($topic_ids))
513 5933 acydburn
        {
514 5933 acydburn
                $topic_ids = array($topic_ids);
515 5933 acydburn
        }
516 3737 ludovic_arnaud
517 5933 acydburn
        $sql = 'DELETE FROM ' . TOPICS_TABLE . '
518 6271 acydburn
                WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids) . '
519 5933 acydburn
                        AND forum_id = ' . $forum_id;
520 3961 psotfx
        $db->sql_query($sql);
521 3961 psotfx
522 3737 ludovic_arnaud
        if ($auto_sync)
523 2286 psotfx
        {
524 3737 ludovic_arnaud
                $sql = 'SELECT DISTINCT forum_id
525 3737 ludovic_arnaud
                        FROM ' . TOPICS_TABLE . '
526 6271 acydburn
                        WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
527 3737 ludovic_arnaud
                $result = $db->sql_query($sql);
528 3961 psotfx
529 3737 ludovic_arnaud
                while ($row = $db->sql_fetchrow($result))
530 3737 ludovic_arnaud
                {
531 3737 ludovic_arnaud
                        $forum_ids[] = $row['forum_id'];
532 3737 ludovic_arnaud
                }
533 3961 psotfx
                $db->sql_freeresult($result);
534 4920 acydburn
        }
535 5933 acydburn
536 5933 acydburn
        $table_ary = array(TOPICS_TABLE, POSTS_TABLE, LOG_TABLE, DRAFTS_TABLE, TOPICS_TRACK_TABLE);
537 4920 acydburn
        foreach ($table_ary as $table)
538 4920 acydburn
        {
539 4920 acydburn
                $sql = "UPDATE $table
540 4920 acydburn
                        SET forum_id = $forum_id
541 6271 acydburn
                        WHERE " . $db->sql_in_set('topic_id', $topic_ids);
542 4920 acydburn
                $db->sql_query($sql);
543 4920 acydburn
        }
544 4920 acydburn
        unset($table_ary);
545 3737 ludovic_arnaud
546 4920 acydburn
        if ($auto_sync)
547 4920 acydburn
        {
548 7466 davidmj
                sync('forum', 'forum_id', $forum_ids, true, true);
549 3961 psotfx
                unset($forum_ids);
550 3737 ludovic_arnaud
        }
551 3737 ludovic_arnaud
}
552 3737 ludovic_arnaud
553 5114 acydburn
/**
554 5114 acydburn
* Move post(s)
555 5114 acydburn
*/
556 4923 acydburn
function move_posts($post_ids, $topic_id, $auto_sync = true)
557 3737 ludovic_arnaud
{
558 3737 ludovic_arnaud
        global $db;
559 3961 psotfx
560 3737 ludovic_arnaud
        if (!is_array($post_ids))
561 3737 ludovic_arnaud
        {
562 3737 ludovic_arnaud
                $post_ids = array($post_ids);
563 3737 ludovic_arnaud
        }
564 3737 ludovic_arnaud
565 5933 acydburn
        $forum_ids = array();
566 5933 acydburn
        $topic_ids = array($topic_id);
567 3737 ludovic_arnaud
568 5933 acydburn
        $sql = 'SELECT DISTINCT topic_id, forum_id
569 5933 acydburn
                FROM ' . POSTS_TABLE . '
570 6271 acydburn
                WHERE ' . $db->sql_in_set('post_id', $post_ids);
571 5933 acydburn
        $result = $db->sql_query($sql);
572 3961 psotfx
573 5933 acydburn
        while ($row = $db->sql_fetchrow($result))
574 5933 acydburn
        {
575 10751 git-gate
                $forum_ids[] = (int) $row['forum_id'];
576 10751 git-gate
                $topic_ids[] = (int) $row['topic_id'];
577 3737 ludovic_arnaud
        }
578 5933 acydburn
        $db->sql_freeresult($result);
579 3737 ludovic_arnaud
580 8146 acydburn
        $sql = 'SELECT forum_id
581 8146 acydburn
                FROM ' . TOPICS_TABLE . '
582 3961 psotfx
                WHERE topic_id = ' . $topic_id;
583 3737 ludovic_arnaud
        $result = $db->sql_query($sql);
584 5933 acydburn
        $forum_row = $db->sql_fetchrow($result);
585 5933 acydburn
        $db->sql_freeresult($result);
586 3961 psotfx
587 5933 acydburn
        if (!$forum_row)
588 3737 ludovic_arnaud
        {
589 3961 psotfx
                trigger_error('NO_TOPIC');
590 3737 ludovic_arnaud
        }
591 3737 ludovic_arnaud
592 3737 ludovic_arnaud
        $sql = 'UPDATE ' . POSTS_TABLE . '
593 10751 git-gate
                SET forum_id = ' . (int) $forum_row['forum_id'] . ", topic_id = $topic_id
594 6271 acydburn
                WHERE " . $db->sql_in_set('post_id', $post_ids);
595 3737 ludovic_arnaud
        $db->sql_query($sql);
596 3737 ludovic_arnaud
597 4677 acydburn
        $sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
598 5933 acydburn
                SET topic_id = $topic_id, in_message = 0
599 6271 acydburn
                WHERE " . $db->sql_in_set('post_msg_id', $post_ids);
600 4677 acydburn
        $db->sql_query($sql);
601 4677 acydburn
602 3737 ludovic_arnaud
        if ($auto_sync)
603 3737 ludovic_arnaud
        {
604 10751 git-gate
                $forum_ids[] = (int) $forum_row['forum_id'];
605 3737 ludovic_arnaud
606 5622 acydburn
                sync('topic_reported', 'topic_id', $topic_ids);
607 6266 naderman
                sync('topic_attachment', 'topic_id', $topic_ids);
608 8081 davidmj
                sync('topic', 'topic_id', $topic_ids, true);
609 7466 davidmj
                sync('forum', 'forum_id', $forum_ids, true, true);
610 3737 ludovic_arnaud
        }
611 5933 acydburn
612 6650 acydburn
        // Update posted information
613 5933 acydburn
        update_posted_info($topic_ids);
614 3737 ludovic_arnaud
}
615 3737 ludovic_arnaud
616 5114 acydburn
/**
617 5114 acydburn
* Remove topic(s)
618 5114 acydburn
*/
619 7221 acydburn
function delete_topics($where_type, $where_ids, $auto_sync = true, $post_count_sync = true, $call_delete_posts = true)
620 3737 ludovic_arnaud
{
621 6149 acydburn
        global $db, $config;
622 6015 acydburn
623 6584 acydburn
        $approved_topics = 0;
624 3737 ludovic_arnaud
        $forum_ids = $topic_ids = array();
625 3737 ludovic_arnaud
626 7000 davidmj
        if ($where_type === 'range')
627 3737 ludovic_arnaud
        {
628 7000 davidmj
                $where_clause = $where_ids;
629 3737 ludovic_arnaud
        }
630 6271 acydburn
        else
631 6271 acydburn
        {
632 7114 acydburn
                $where_ids = (is_array($where_ids)) ? array_unique($where_ids) : array($where_ids);
633 7066 davidmj
634 7066 davidmj
                if (!sizeof($where_ids))
635 7066 davidmj
                {
636 7066 davidmj
                        return array('topics' => 0, 'posts' => 0);
637 7066 davidmj
                }
638 7066 davidmj
639 7000 davidmj
                $where_clause = $db->sql_in_set($where_type, $where_ids);
640 6271 acydburn
        }
641 3961 psotfx
642 7221 acydburn
        // Making sure that delete_posts does not call delete_topics again...
643 3737 ludovic_arnaud
        $return = array(
644 7221 acydburn
                'posts' => ($call_delete_posts) ? delete_posts($where_type, $where_ids, false, true, $post_count_sync, false) : 0,
645 3737 ludovic_arnaud
        );
646 3737 ludovic_arnaud
647 8836 toonarmy
        $sql = 'SELECT topic_id, forum_id, topic_approved, topic_moved_id
648 6271 acydburn
                FROM ' . TOPICS_TABLE . '
649 7001 davidmj
                WHERE ' . $where_clause;
650 3961 psotfx
        $result = $db->sql_query($sql);
651 3737 ludovic_arnaud
652 3737 ludovic_arnaud
        while ($row = $db->sql_fetchrow($result))
653 3737 ludovic_arnaud
        {
654 3737 ludovic_arnaud
                $forum_ids[] = $row['forum_id'];
655 3737 ludovic_arnaud
                $topic_ids[] = $row['topic_id'];
656 6584 acydburn
657 8836 toonarmy
                if ($row['topic_approved'] && !$row['topic_moved_id'])
658 6584 acydburn
                {
659 6584 acydburn
                        $approved_topics++;
660 6584 acydburn
                }
661 3737 ludovic_arnaud
        }
662 6345 acydburn
        $db->sql_freeresult($result);
663 3737 ludovic_arnaud
664 4920 acydburn
        $return['topics'] = sizeof($topic_ids);
665 3737 ludovic_arnaud
666 4920 acydburn
        if (!sizeof($topic_ids))
667 3737 ludovic_arnaud
        {
668 3737 ludovic_arnaud
                return $return;
669 3737 ludovic_arnaud
        }
670 3737 ludovic_arnaud
671 3737 ludovic_arnaud
        $db->sql_transaction('begin');
672 3961 psotfx
673 10637 git-gate
        $table_ary = array(BOOKMARKS_TABLE, TOPICS_TRACK_TABLE, TOPICS_POSTED_TABLE, POLL_VOTES_TABLE, POLL_OPTIONS_TABLE, TOPICS_WATCH_TABLE, TOPICS_TABLE);
674 5973 acydburn
675 3961 psotfx
        foreach ($table_ary as $table)
676 3961 psotfx
        {
677 8146 acydburn
                $sql = "DELETE FROM $table
678 6271 acydburn
                        WHERE " . $db->sql_in_set('topic_id', $topic_ids);
679 3961 psotfx
                $db->sql_query($sql);
680 3961 psotfx
        }
681 3961 psotfx
        unset($table_ary);
682 3961 psotfx
683 7679 davidmj
        $moved_topic_ids = array();
684 7679 davidmj
685 7679 davidmj
        // update the other forums
686 7679 davidmj
        $sql = 'SELECT topic_id, forum_id
687 7679 davidmj
                FROM ' . TOPICS_TABLE . '
688 6271 acydburn
                WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids);
689 7679 davidmj
        $result = $db->sql_query($sql);
690 3961 psotfx
691 7679 davidmj
        while ($row = $db->sql_fetchrow($result))
692 7679 davidmj
        {
693 7679 davidmj
                $forum_ids[] = $row['forum_id'];
694 7679 davidmj
                $moved_topic_ids[] = $row['topic_id'];
695 7679 davidmj
        }
696 7679 davidmj
        $db->sql_freeresult($result);
697 7679 davidmj
698 7679 davidmj
        if (sizeof($moved_topic_ids))
699 7679 davidmj
        {
700 8146 acydburn
                $sql = 'DELETE FROM ' . TOPICS_TABLE . '
701 7679 davidmj
                        WHERE ' . $db->sql_in_set('topic_id', $moved_topic_ids);
702 7679 davidmj
                $db->sql_query($sql);
703 7679 davidmj
        }
704 7679 davidmj
705 3737 ludovic_arnaud
        $db->sql_transaction('commit');
706 3737 ludovic_arnaud
707 3737 ludovic_arnaud
        if ($auto_sync)
708 3737 ludovic_arnaud
        {
709 7679 davidmj
                sync('forum', 'forum_id', array_unique($forum_ids), true, true);
710 3936 ludovic_arnaud
                sync('topic_reported', $where_type, $where_ids);
711 3737 ludovic_arnaud
        }
712 3737 ludovic_arnaud
713 6584 acydburn
        if ($approved_topics)
714 6584 acydburn
        {
715 9398 acydburn
                set_config_count('num_topics', $approved_topics * (-1), true);
716 6584 acydburn
        }
717 6149 acydburn
718 3737 ludovic_arnaud
        return $return;
719 3737 ludovic_arnaud
}
720 3737 ludovic_arnaud
721 5114 acydburn
/**
722 5114 acydburn
* Remove post(s)
723 5114 acydburn
*/
724 7221 acydburn
function delete_posts($where_type, $where_ids, $auto_sync = true, $posted_sync = true, $post_count_sync = true, $call_delete_topics = true)
725 3737 ludovic_arnaud
{
726 5441 naderman
        global $db, $config, $phpbb_root_path, $phpEx;
727 3737 ludovic_arnaud
728 7000 davidmj
        if ($where_type === 'range')
729 3737 ludovic_arnaud
        {
730 7000 davidmj
                $where_clause = $where_ids;
731 3737 ludovic_arnaud
        }
732 6271 acydburn
        else
733 6271 acydburn
        {
734 7000 davidmj
                if (is_array($where_ids))
735 7000 davidmj
                {
736 7000 davidmj
                        $where_ids = array_unique($where_ids);
737 7000 davidmj
                }
738 7000 davidmj
                else
739 7000 davidmj
                {
740 7000 davidmj
                        $where_ids = array($where_ids);
741 7000 davidmj
                }
742 6015 acydburn
743 7000 davidmj
                if (!sizeof($where_ids))
744 7000 davidmj
                {
745 7000 davidmj
                        return false;
746 7000 davidmj
                }
747 7000 davidmj
748 9439 acydburn
                $where_ids = array_map('intval', $where_ids);
749 9439 acydburn
750 9439 acydburn
/*                Possible code for splitting post deletion
751 9439 acydburn
                if (sizeof($where_ids) >= 1001)
752 9439 acydburn
                {
753 9439 acydburn
                        // Split into chunks of 1000
754 9439 acydburn
                        $chunks = array_chunk($where_ids, 1000);
755 9439 acydburn
756 9439 acydburn
                        foreach ($chunks as $_where_ids)
757 9439 acydburn
                        {
758 9439 acydburn
                                delete_posts($where_type, $_where_ids, $auto_sync, $posted_sync, $post_count_sync, $call_delete_topics);
759 9439 acydburn
                        }
760 9441 acydburn
761 9441 acydburn
                        return;
762 9439 acydburn
                }*/
763 9439 acydburn
764 9439 acydburn
                $where_clause = $db->sql_in_set($where_type, $where_ids);
765 3737 ludovic_arnaud
        }
766 6015 acydburn
767 6584 acydburn
        $approved_posts = 0;
768 6970 acydburn
        $post_ids = $topic_ids = $forum_ids = $post_counts = $remove_topics = array();
769 3737 ludovic_arnaud
770 6584 acydburn
        $sql = 'SELECT post_id, poster_id, post_approved, post_postcount, topic_id, forum_id
771 6271 acydburn
                FROM ' . POSTS_TABLE . '
772 7001 davidmj
                WHERE ' . $where_clause;
773 3737 ludovic_arnaud
        $result = $db->sql_query($sql);
774 4813 psotfx
775 3737 ludovic_arnaud
        while ($row = $db->sql_fetchrow($result))
776 3737 ludovic_arnaud
        {
777 9439 acydburn
                $post_ids[] = (int) $row['post_id'];
778 9439 acydburn
                $poster_ids[] = (int) $row['poster_id'];
779 9439 acydburn
                $topic_ids[] = (int) $row['topic_id'];
780 9439 acydburn
                $forum_ids[] = (int) $row['forum_id'];
781 6224 acydburn
782 8805 acydburn
                if ($row['post_postcount'] && $post_count_sync && $row['post_approved'])
783 6224 acydburn
                {
784 6224 acydburn
                        $post_counts[$row['poster_id']] = (!empty($post_counts[$row['poster_id']])) ? $post_counts[$row['poster_id']] + 1 : 1;
785 6224 acydburn
                }
786 6584 acydburn
787 6584 acydburn
                if ($row['post_approved'])
788 6584 acydburn
                {
789 6584 acydburn
                        $approved_posts++;
790 6584 acydburn
                }
791 3737 ludovic_arnaud
        }
792 6015 acydburn
        $db->sql_freeresult($result);
793 3737 ludovic_arnaud
794 5033 acydburn
        if (!sizeof($post_ids))
795 3737 ludovic_arnaud
        {
796 3883 acydburn
                return false;
797 3737 ludovic_arnaud
        }
798 3737 ludovic_arnaud
799 3737 ludovic_arnaud
        $db->sql_transaction('begin');
800 3961 psotfx
801 5441 naderman
        $table_ary = array(POSTS_TABLE, REPORTS_TABLE);
802 4813 psotfx
803 3961 psotfx
        foreach ($table_ary as $table)
804 3961 psotfx
        {
805 8146 acydburn
                $sql = "DELETE FROM $table
806 6271 acydburn
                        WHERE " . $db->sql_in_set('post_id', $post_ids);
807 3961 psotfx
                $db->sql_query($sql);
808 3961 psotfx
        }
809 3961 psotfx
        unset($table_ary);
810 3961 psotfx
811 6224 acydburn
        // Adjust users post counts
812 6974 naderman
        if (sizeof($post_counts) && $post_count_sync)
813 6224 acydburn
        {
814 6224 acydburn
                foreach ($post_counts as $poster_id => $substract)
815 6224 acydburn
                {
816 6224 acydburn
                        $sql = 'UPDATE ' . USERS_TABLE . '
817 7677 kellanved
                                SET user_posts = 0
818 8146 acydburn
                                WHERE user_id = ' . $poster_id . '
819 7677 kellanved
                                AND user_posts < ' . $substract;
820 7677 kellanved
                        $db->sql_query($sql);
821 8805 acydburn
822 7677 kellanved
                        $sql = 'UPDATE ' . USERS_TABLE . '
823 6224 acydburn
                                SET user_posts = user_posts - ' . $substract . '
824 8146 acydburn
                                WHERE user_id = ' . $poster_id . '
825 7677 kellanved
                                AND user_posts >= ' . $substract;
826 6224 acydburn
                        $db->sql_query($sql);
827 6224 acydburn
                }
828 6224 acydburn
        }
829 6224 acydburn
830 6970 acydburn
        // Remove topics now having no posts?
831 6970 acydburn
        if (sizeof($topic_ids))
832 6970 acydburn
        {
833 6970 acydburn
                $sql = 'SELECT topic_id
834 6970 acydburn
                        FROM ' . POSTS_TABLE . '
835 6970 acydburn
                        WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
836 6970 acydburn
                        GROUP BY topic_id';
837 6970 acydburn
                $result = $db->sql_query($sql);
838 6970 acydburn
839 6970 acydburn
                while ($row = $db->sql_fetchrow($result))
840 6970 acydburn
                {
841 6970 acydburn
                        $remove_topics[] = $row['topic_id'];
842 6970 acydburn
                }
843 6970 acydburn
                $db->sql_freeresult($result);
844 6970 acydburn
845 6970 acydburn
                // Actually, those not within remove_topics should be removed. ;)
846 6970 acydburn
                $remove_topics = array_diff($topic_ids, $remove_topics);
847 6970 acydburn
        }
848 6970 acydburn
849 5441 naderman
        // Remove the message from the search index
850 11676 git-gate
        $search_type = $config['search_type'];
851 5441 naderman
852 11676 git-gate
        if (!class_exists($search_type))
853 5441 naderman
        {
854 5441 naderman
                trigger_error('NO_SUCH_SEARCH_MODULE');
855 5441 naderman
        }
856 5441 naderman
857 5441 naderman
        $error = false;
858 5441 naderman
        $search = new $search_type($error);
859 5441 naderman
860 5441 naderman
        if ($error)
861 5441 naderman
        {
862 5441 naderman
                trigger_error($error);
863 5441 naderman
        }
864 5441 naderman
865 6152 naderman
        $search->index_remove($post_ids, $poster_ids, $forum_ids);
866 5441 naderman
867 4920 acydburn
        delete_attachments('post', $post_ids, false);
868 4482 acydburn
869 3737 ludovic_arnaud
        $db->sql_transaction('commit');
870 3737 ludovic_arnaud
871 5973 acydburn
        // Resync topics_posted table
872 5973 acydburn
        if ($posted_sync)
873 5973 acydburn
        {
874 5973 acydburn
                update_posted_info($topic_ids);
875 5973 acydburn
        }
876 5973 acydburn
877 3737 ludovic_arnaud
        if ($auto_sync)
878 3737 ludovic_arnaud
        {
879 5622 acydburn
                sync('topic_reported', 'topic_id', $topic_ids);
880 4920 acydburn
                sync('topic', 'topic_id', $topic_ids, true);
881 7466 davidmj
                sync('forum', 'forum_id', $forum_ids, true, true);
882 3737 ludovic_arnaud
        }
883 3737 ludovic_arnaud
884 6584 acydburn
        if ($approved_posts)
885 6584 acydburn
        {
886 9398 acydburn
                set_config_count('num_posts', $approved_posts * (-1), true);
887 6584 acydburn
        }
888 6149 acydburn
889 6970 acydburn
        // We actually remove topics now to not be inconsistent (the delete_topics function calls this function too)
890 7221 acydburn
        if (sizeof($remove_topics) && $call_delete_topics)
891 6970 acydburn
        {
892 7221 acydburn
                delete_topics('topic_id', $remove_topics, $auto_sync, $post_count_sync, false);
893 6970 acydburn
        }
894 6970 acydburn
895 4920 acydburn
        return sizeof($post_ids);
896 3737 ludovic_arnaud
}
897 3737 ludovic_arnaud
898 5114 acydburn
/**
899 5114 acydburn
* Delete Attachments
900 6015 acydburn
*
901 8890 acydburn
* @param string $mode can be: post|message|topic|attach|user
902 8890 acydburn
* @param mixed $ids can be: post_ids, message_ids, topic_ids, attach_ids, user_ids
903 6015 acydburn
* @param bool $resync set this to false if you are deleting posts or topics
904 5114 acydburn
*/
905 5313 acydburn
function delete_attachments($mode, $ids, $resync = true)
906 4482 acydburn
{
907 4668 acydburn
        global $db, $config;
908 4482 acydburn
909 10558 git-gate
        // 0 is as bad as an empty array
910 10558 git-gate
        if (empty($ids))
911 4482 acydburn
        {
912 10558 git-gate
                return false;
913 10558 git-gate
        }
914 10558 git-gate
915 10558 git-gate
        if (is_array($ids))
916 10558 git-gate
        {
917 4637 acydburn
                $ids = array_unique($ids);
918 6015 acydburn
                $ids = array_map('intval', $ids);
919 4482 acydburn
        }
920 6015 acydburn
        else
921 6015 acydburn
        {
922 6015 acydburn
                $ids = array((int) $ids);
923 6015 acydburn
        }
924 6015 acydburn
925 9379 toonarmy
        $sql_where = '';
926 9379 toonarmy
927 8890 acydburn
        switch ($mode)
928 8890 acydburn
        {
929 8890 acydburn
                case 'post':
930 8890 acydburn
                case 'message':
931 8890 acydburn
                        $sql_id = 'post_msg_id';
932 9379 toonarmy
                        $sql_where = ' AND in_message = ' . ($mode == 'message' ? 1 : 0);
933 8890 acydburn
                break;
934 4482 acydburn
935 8890 acydburn
                case 'topic':
936 8890 acydburn
                        $sql_id = 'topic_id';
937 8890 acydburn
                break;
938 4637 acydburn
939 8890 acydburn
                case 'user':
940 8890 acydburn
                        $sql_id = 'poster_id';
941 8890 acydburn
                break;
942 6015 acydburn
943 8890 acydburn
                case 'attach':
944 8890 acydburn
                default:
945 8890 acydburn
                        $sql_id = 'attach_id';
946 8890 acydburn
                        $mode = 'attach';
947 8890 acydburn
                break;
948 4637 acydburn
        }
949 4482 acydburn
950 8890 acydburn
        $post_ids = $message_ids = $topic_ids = $physical = array();
951 8890 acydburn
952 8890 acydburn
        // Collect post and topic ids for later use if we need to touch remaining entries (if resync is enabled)
953 8890 acydburn
        $sql = 'SELECT post_msg_id, topic_id, in_message, physical_filename, thumbnail, filesize, is_orphan
954 4637 acydburn
                        FROM ' . ATTACHMENTS_TABLE . '
955 8890 acydburn
                        WHERE ' . $db->sql_in_set($sql_id, $ids);
956 9379 toonarmy
957 9379 toonarmy
        $sql .= $sql_where;
958 9379 toonarmy
959 8890 acydburn
        $result = $db->sql_query($sql);
960 6015 acydburn
961 8890 acydburn
        while ($row = $db->sql_fetchrow($result))
962 8890 acydburn
        {
963 8890 acydburn
                // We only need to store post/message/topic ids if resync is enabled and the file is not orphaned
964 8890 acydburn
                if ($resync && !$row['is_orphan'])
965 4482 acydburn
                {
966 8890 acydburn
                        if (!$row['in_message'])
967 8890 acydburn
                        {
968 8890 acydburn
                                $post_ids[] = $row['post_msg_id'];
969 8890 acydburn
                                $topic_ids[] = $row['topic_id'];
970 8890 acydburn
                        }
971 8890 acydburn
                        else
972 8890 acydburn
                        {
973 8890 acydburn
                                $message_ids[] = $row['post_msg_id'];
974 8890 acydburn
                        }
975 4482 acydburn
                }
976 8890 acydburn
977 8890 acydburn
                $physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize'], 'is_orphan' => $row['is_orphan']);
978 4482 acydburn
        }
979 8890 acydburn
        $db->sql_freeresult($result);
980 4637 acydburn
981 4637 acydburn
        // Delete attachments
982 6015 acydburn
        $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
983 6271 acydburn
                WHERE ' . $db->sql_in_set($sql_id, $ids);
984 9379 toonarmy
985 9379 toonarmy
        $sql .= $sql_where;
986 9379 toonarmy
987 6015 acydburn
        $db->sql_query($sql);
988 4637 acydburn
        $num_deleted = $db->sql_affectedrows();
989 4637 acydburn
990 4677 acydburn
        if (!$num_deleted)
991 4677 acydburn
        {
992 4677 acydburn
                return 0;
993 4677 acydburn
        }
994 6015 acydburn
995 4637 acydburn
        // Delete attachments from filesystem
996 4668 acydburn
        $space_removed = $files_removed = 0;
997 4637 acydburn
        foreach ($physical as $file_ary)
998 4482 acydburn
        {
999 8890 acydburn
                if (phpbb_unlink($file_ary['filename'], 'file', true) && !$file_ary['is_orphan'])
1000 4668 acydburn
                {
1001 8890 acydburn
                        // Only non-orphaned files count to the file size
1002 4668 acydburn
                        $space_removed += $file_ary['filesize'];
1003 4668 acydburn
                        $files_removed++;
1004 4668 acydburn
                }
1005 4668 acydburn
1006 4637 acydburn
                if ($file_ary['thumbnail'])
1007 4482 acydburn
                {
1008 6831 acydburn
                        phpbb_unlink($file_ary['filename'], 'thumbnail', true);
1009 4482 acydburn
                }
1010 4637 acydburn
        }
1011 4482 acydburn
1012 8890 acydburn
        if ($space_removed || $files_removed)
1013 4637 acydburn
        {
1014 9398 acydburn
                set_config_count('upload_dir_size', $space_removed * (-1), true);
1015 9398 acydburn
                set_config_count('num_files', $files_removed * (-1), true);
1016 4482 acydburn
        }
1017 4637 acydburn
1018 8890 acydburn
        // If we do not resync, we do not need to adjust any message, post, topic or user entries
1019 8890 acydburn
        if (!$resync)
1020 4482 acydburn
        {
1021 8890 acydburn
                return $num_deleted;
1022 4482 acydburn
        }
1023 8890 acydburn
1024 8890 acydburn
        // No more use for the original ids
1025 4637 acydburn
        unset($ids);
1026 4482 acydburn
1027 8890 acydburn
        // Now, we need to resync posts, messages, topics. We go through every one of them
1028 4637 acydburn
        $post_ids = array_unique($post_ids);
1029 8890 acydburn
        $message_ids = array_unique($message_ids);
1030 4637 acydburn
        $topic_ids = array_unique($topic_ids);
1031 4637 acydburn
1032 8890 acydburn
        // Update post indicators for posts now no longer having attachments
1033 4663 acydburn
        if (sizeof($post_ids))
1034 4482 acydburn
        {
1035 9843 nickvergessen
                // Just check which posts are still having an assigned attachment not orphaned by querying the attachments table
1036 9843 nickvergessen
                $sql = 'SELECT post_msg_id
1037 9843 nickvergessen
                        FROM ' . ATTACHMENTS_TABLE . '
1038 9843 nickvergessen
                        WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
1039 9843 nickvergessen
                                AND in_message = 0
1040 9843 nickvergessen
                                AND is_orphan = 0';
1041 9843 nickvergessen
                $result = $db->sql_query($sql);
1042 9843 nickvergessen
1043 9843 nickvergessen
                $remaining_ids = array();
1044 9843 nickvergessen
                while ($row = $db->sql_fetchrow($result))
1045 9843 nickvergessen
                {
1046 9843 nickvergessen
                        $remaining_ids[] = $row['post_msg_id'];
1047 9843 nickvergessen
                }
1048 9843 nickvergessen
                $db->sql_freeresult($result);
1049 9843 nickvergessen
1050 9843 nickvergessen
                // Now only unset those ids remaining
1051 9843 nickvergessen
                $post_ids = array_diff($post_ids, $remaining_ids);
1052 9843 nickvergessen
1053 9843 nickvergessen
                if (sizeof($post_ids))
1054 9843 nickvergessen
                {
1055 9843 nickvergessen
                        $sql = 'UPDATE ' . POSTS_TABLE . '
1056 9843 nickvergessen
                                SET post_attachment = 0
1057 9843 nickvergessen
                                WHERE ' . $db->sql_in_set('post_id', $post_ids);
1058 9843 nickvergessen
                        $db->sql_query($sql);
1059 9843 nickvergessen
                }
1060 8890 acydburn
        }
1061 4482 acydburn
1062 8890 acydburn
        // Update message table if messages are affected
1063 8890 acydburn
        if (sizeof($message_ids))
1064 8890 acydburn
        {
1065 9843 nickvergessen
                // Just check which messages are still having an assigned attachment not orphaned by querying the attachments table
1066 9843 nickvergessen
                $sql = 'SELECT post_msg_id
1067 9843 nickvergessen
                        FROM ' . ATTACHMENTS_TABLE . '
1068 9843 nickvergessen
                        WHERE ' . $db->sql_in_set('post_msg_id', $message_ids) . '
1069 9843 nickvergessen
                                AND in_message = 1
1070 9843 nickvergessen
                                AND is_orphan = 0';
1071 9843 nickvergessen
                $result = $db->sql_query($sql);
1072 9843 nickvergessen
1073 9843 nickvergessen
                $remaining_ids = array();
1074 9843 nickvergessen
                while ($row = $db->sql_fetchrow($result))
1075 9843 nickvergessen
                {
1076 9843 nickvergessen
                        $remaining_ids[] = $row['post_msg_id'];
1077 9843 nickvergessen
                }
1078 9843 nickvergessen
                $db->sql_freeresult($result);
1079 9843 nickvergessen
1080 9843 nickvergessen
                // Now only unset those ids remaining
1081 9843 nickvergessen
                $message_ids = array_diff($message_ids, $remaining_ids);
1082 9843 nickvergessen
1083 9843 nickvergessen
                if (sizeof($message_ids))
1084 9843 nickvergessen
                {
1085 9843 nickvergessen
                        $sql = 'UPDATE ' . PRIVMSGS_TABLE . '
1086 9843 nickvergessen
                                SET message_attachment = 0
1087 9843 nickvergessen
                                WHERE ' . $db->sql_in_set('msg_id', $message_ids);
1088 9843 nickvergessen
                        $db->sql_query($sql);
1089 9843 nickvergessen
                }
1090 4482 acydburn
        }
1091 4482 acydburn
1092 8890 acydburn
        // Now update the topics. This is a bit trickier, because there could be posts still having attachments within the topic
1093 4663 acydburn
        if (sizeof($topic_ids))
1094 4482 acydburn
        {
1095 8890 acydburn
                // Just check which topics are still having an assigned attachment not orphaned by querying the attachments table (much less entries expected)
1096 8890 acydburn
                $sql = 'SELECT topic_id
1097 8890 acydburn
                        FROM ' . ATTACHMENTS_TABLE . '
1098 8890 acydburn
                        WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
1099 8890 acydburn
                                AND is_orphan = 0';
1100 8890 acydburn
                $result = $db->sql_query($sql);
1101 8890 acydburn
1102 8890 acydburn
                $remaining_ids = array();
1103 8890 acydburn
                while ($row = $db->sql_fetchrow($result))
1104 4663 acydburn
                {
1105 8890 acydburn
                        $remaining_ids[] = $row['topic_id'];
1106 8890 acydburn
                }
1107 8890 acydburn
                $db->sql_freeresult($result);
1108 8890 acydburn
1109 8890 acydburn
                // Now only unset those ids remaining
1110 8890 acydburn
                $topic_ids = array_diff($topic_ids, $remaining_ids);
1111 8890 acydburn
1112 8890 acydburn
                if (sizeof($topic_ids))
1113 8890 acydburn
                {
1114 6015 acydburn
                        $sql = 'UPDATE ' . TOPICS_TABLE . '
1115 4663 acydburn
                                SET topic_attachment = 0
1116 6271 acydburn
                                WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1117 6015 acydburn
                        $db->sql_query($sql);
1118 4663 acydburn
                }
1119 4482 acydburn
        }
1120 4663 acydburn
1121 4637 acydburn
        return $num_deleted;
1122 4482 acydburn
}
1123 4482 acydburn
1124 5114 acydburn
/**
1125 10751 git-gate
* Deletes shadow topics pointing to a specified forum.
1126 10751 git-gate
*
1127 10751 git-gate
* @param int                $forum_id                The forum id
1128 10751 git-gate
* @param string                $sql_more                Additional WHERE statement, e.g. t.topic_time < (time() - 1234)
1129 10751 git-gate
* @param bool                $auto_sync                Will call sync() if this is true
1130 10751 git-gate
*
1131 10751 git-gate
* @return array                Array with affected forums
1132 10751 git-gate
*
1133 10751 git-gate
* @author bantu
1134 5114 acydburn
*/
1135 10751 git-gate
function delete_topic_shadows($forum_id, $sql_more = '', $auto_sync = true)
1136 4534 ludovic_arnaud
{
1137 10751 git-gate
        global $db;
1138 4534 ludovic_arnaud
1139 10751 git-gate
        if (!$forum_id)
1140 4534 ludovic_arnaud
        {
1141 10751 git-gate
                // Nothing to do.
1142 10751 git-gate
                return;
1143 10751 git-gate
        }
1144 8782 acydburn
1145 10751 git-gate
        // Set of affected forums we have to resync
1146 10751 git-gate
        $sync_forum_ids = array();
1147 6015 acydburn
1148 10751 git-gate
        // Amount of topics we select and delete at once.
1149 10751 git-gate
        $batch_size = 500;
1150 4534 ludovic_arnaud
1151 10751 git-gate
        do
1152 10751 git-gate
        {
1153 10751 git-gate
                $sql = 'SELECT t2.forum_id, t2.topic_id
1154 10751 git-gate
                        FROM ' . TOPICS_TABLE . ' t2, ' . TOPICS_TABLE . ' t
1155 10751 git-gate
                        WHERE t2.topic_moved_id = t.topic_id
1156 10751 git-gate
                                AND t.forum_id = ' . (int) $forum_id . '
1157 10751 git-gate
                                ' . (($sql_more) ? 'AND ' . $sql_more : '');
1158 10751 git-gate
                $result = $db->sql_query_limit($sql, $batch_size);
1159 10751 git-gate
1160 10751 git-gate
                $topic_ids = array();
1161 10751 git-gate
                while ($row = $db->sql_fetchrow($result))
1162 10751 git-gate
                {
1163 10751 git-gate
                        $topic_ids[] = (int) $row['topic_id'];
1164 10751 git-gate
1165 10751 git-gate
                        $sync_forum_ids[(int) $row['forum_id']] = (int) $row['forum_id'];
1166 10751 git-gate
                }
1167 10751 git-gate
                $db->sql_freeresult($result);
1168 10751 git-gate
1169 10751 git-gate
                if (!empty($topic_ids))
1170 10751 git-gate
                {
1171 10751 git-gate
                        $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1172 10751 git-gate
                                WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1173 10751 git-gate
                        $db->sql_query($sql);
1174 10751 git-gate
                }
1175 4534 ludovic_arnaud
        }
1176 10751 git-gate
        while (sizeof($topic_ids) == $batch_size);
1177 4534 ludovic_arnaud
1178 4534 ludovic_arnaud
        if ($auto_sync)
1179 4534 ludovic_arnaud
        {
1180 10751 git-gate
                sync('forum', 'forum_id', $sync_forum_ids, true, true);
1181 4534 ludovic_arnaud
        }
1182 10751 git-gate
1183 10751 git-gate
        return $sync_forum_ids;
1184 4534 ludovic_arnaud
}
1185 4534 ludovic_arnaud
1186 5114 acydburn
/**
1187 6650 acydburn
* Update/Sync posted information for topics
1188 5933 acydburn
*/
1189 5973 acydburn
function update_posted_info(&$topic_ids)
1190 5933 acydburn
{
1191 5973 acydburn
        global $db, $config;
1192 5933 acydburn
1193 5973 acydburn
        if (empty($topic_ids) || !$config['load_db_track'])
1194 5933 acydburn
        {
1195 5933 acydburn
                return;
1196 5933 acydburn
        }
1197 5933 acydburn
1198 5933 acydburn
        // First of all, let us remove any posted information for these topics
1199 5933 acydburn
        $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
1200 6271 acydburn
                WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1201 5933 acydburn
        $db->sql_query($sql);
1202 5933 acydburn
1203 5933 acydburn
        // Now, let us collect the user/topic combos for rebuilding the information
1204 5973 acydburn
        $sql = 'SELECT poster_id, topic_id
1205 5933 acydburn
                FROM ' . POSTS_TABLE . '
1206 6271 acydburn
                WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
1207 5973 acydburn
                        AND poster_id <> ' . ANONYMOUS . '
1208 5973 acydburn
                GROUP BY poster_id, topic_id';
1209 5933 acydburn
        $result = $db->sql_query($sql);
1210 5933 acydburn
1211 5933 acydburn
        $posted = array();
1212 5933 acydburn
        while ($row = $db->sql_fetchrow($result))
1213 5933 acydburn
        {
1214 5933 acydburn
                // Add as key to make them unique (grouping by) and circumvent empty keys on array_unique
1215 5973 acydburn
                $posted[$row['poster_id']][] = $row['topic_id'];
1216 5933 acydburn
        }
1217 5933 acydburn
        $db->sql_freeresult($result);
1218 5933 acydburn
1219 5933 acydburn
        // Now add the information...
1220 5933 acydburn
        $sql_ary = array();
1221 5973 acydburn
        foreach ($posted as $user_id => $topic_row)
1222 5933 acydburn
        {
1223 5973 acydburn
                foreach ($topic_row as $topic_id)
1224 5933 acydburn
                {
1225 5933 acydburn
                        $sql_ary[] = array(
1226 7961 acydburn
                                'user_id'                => (int) $user_id,
1227 7961 acydburn
                                'topic_id'                => (int) $topic_id,
1228 5933 acydburn
                                'topic_posted'        => 1,
1229 5933 acydburn
                        );
1230 5933 acydburn
                }
1231 5933 acydburn
        }
1232 5973 acydburn
        unset($posted);
1233 5933 acydburn
1234 6497 acydburn
        $db->sql_multi_insert(TOPICS_POSTED_TABLE, $sql_ary);
1235 5933 acydburn
}
1236 5933 acydburn
1237 5933 acydburn
/**
1238 6831 acydburn
* Delete attached file
1239 5114 acydburn
*/
1240 6831 acydburn
function phpbb_unlink($filename, $mode = 'file', $entry_removed = false)
1241 4572 acydburn
{
1242 6831 acydburn
        global $db, $phpbb_root_path, $config;
1243 4572 acydburn
1244 6831 acydburn
        // Because of copying topics or modifications a physical filename could be assigned more than once. If so, do not remove the file itself.
1245 6831 acydburn
        $sql = 'SELECT COUNT(attach_id) AS num_entries
1246 6831 acydburn
                FROM ' . ATTACHMENTS_TABLE . "
1247 9905 acydburn
                WHERE physical_filename = '" . $db->sql_escape(utf8_basename($filename)) . "'";
1248 6831 acydburn
        $result = $db->sql_query($sql);
1249 6831 acydburn
        $num_entries = (int) $db->sql_fetchfield('num_entries');
1250 6831 acydburn
        $db->sql_freeresult($result);
1251 6831 acydburn
1252 6831 acydburn
        // Do not remove file if at least one additional entry with the same name exist.
1253 6831 acydburn
        if (($entry_removed && $num_entries > 0) || (!$entry_removed && $num_entries > 1))
1254 6831 acydburn
        {
1255 6831 acydburn
                return false;
1256 6831 acydburn
        }
1257 6831 acydburn
1258 9905 acydburn
        $filename = ($mode == 'thumbnail') ? 'thumb_' . utf8_basename($filename) : utf8_basename($filename);
1259 6831 acydburn
        return @unlink($phpbb_root_path . $config['upload_path'] . '/' . $filename);
1260 4572 acydburn
}
1261 4572 acydburn
1262 5114 acydburn
/**
1263 5114 acydburn
* All-encompasing sync function
1264 5114 acydburn
*
1265 6015 acydburn
* Exaples:
1266 6015 acydburn
* <code>
1267 6015 acydburn
* sync('topic', 'topic_id', 123);                        // resync topic #123
1268 6015 acydburn
* sync('topic', 'forum_id', array(2, 3));        // resync topics from forum #2 and #3
1269 6015 acydburn
* sync('topic');                                                        // resync all topics
1270 6015 acydburn
* sync('topic', 'range', 'topic_id BETWEEN 1 AND 60');        // resync a range of topics/forums (only available for 'topic' and 'forum' modes)
1271 6015 acydburn
* </code>
1272 5114 acydburn
*
1273 5114 acydburn
* Modes:
1274 6015 acydburn
* - forum                                Resync complete forum
1275 6015 acydburn
* - topic                                Resync topics
1276 6015 acydburn
* - topic_moved                        Removes topic shadows that would be in the same forum as the topic they link to
1277 5114 acydburn
* - topic_approved                Resyncs the topic_approved flag according to the status of the first post
1278 5114 acydburn
* - post_reported                Resyncs the post_reported flag, relying on actual reports
1279 5114 acydburn
* - topic_reported                Resyncs the topic_reported flag, relying on post_reported flags
1280 6015 acydburn
* - post_attachement        Same as post_reported, but with attachment flags
1281 6015 acydburn
* - topic_attachement        Same as topic_reported, but with attachment flags
1282 5114 acydburn
*/
1283 5313 acydburn
function sync($mode, $where_type = '', $where_ids = '', $resync_parents = false, $sync_extra = false)
1284 3737 ludovic_arnaud
{
1285 3961 psotfx
        global $db;
1286 3737 ludovic_arnaud
1287 3737 ludovic_arnaud
        if (is_array($where_ids))
1288 3737 ludovic_arnaud
        {
1289 3737 ludovic_arnaud
                $where_ids = array_unique($where_ids);
1290 6015 acydburn
                $where_ids = array_map('intval', $where_ids);
1291 3737 ludovic_arnaud
        }
1292 5313 acydburn
        else if ($where_type != 'range')
1293 3737 ludovic_arnaud
        {
1294 6015 acydburn
                $where_ids = ($where_ids) ? array((int) $where_ids) : array();
1295 3737 ludovic_arnaud
        }
1296 3737 ludovic_arnaud
1297 7504 davidmj
        if ($mode == 'forum' || $mode == 'topic' || $mode == 'topic_approved' || $mode == 'topic_reported' || $mode == 'post_reported')
1298 3737 ludovic_arnaud
        {
1299 3737 ludovic_arnaud
                if (!$where_type)
1300 3737 ludovic_arnaud
                {
1301 5033 acydburn
                        $where_sql = '';
1302 5033 acydburn
                        $where_sql_and = 'WHERE';
1303 3737 ludovic_arnaud
                }
1304 5313 acydburn
                else if ($where_type == 'range')
1305 4507 ludovic_arnaud
                {
1306 5026 bartvb
                        // Only check a range of topics/forums. For instance: 'topic_id BETWEEN 1 AND 60'
1307 6459 acydburn
                        $where_sql = 'WHERE (' . $mode[0] . ".$where_ids)";
1308 4671 ludovic_arnaud
                        $where_sql_and = $where_sql . "\n\tAND";
1309 4507 ludovic_arnaud
                }
1310 3737 ludovic_arnaud
                else
1311 3737 ludovic_arnaud
                {
1312 5779 naderman
                        // Do not sync the "global forum"
1313 5779 naderman
                        $where_ids = array_diff($where_ids, array(0));
1314 5779 naderman
1315 4903 acydburn
                        if (!sizeof($where_ids))
1316 4903 acydburn
                        {
1317 5026 bartvb
                                // Empty array with IDs. This means that we don't have any work to do. Just return.
1318 4903 acydburn
                                return;
1319 4903 acydburn
                        }
1320 5779 naderman
1321 5026 bartvb
                        // Limit the topics/forums we are syncing, use specific topic/forum IDs.
1322 5033 acydburn
                        // $where_type contains the field for the where clause (forum_id, topic_id)
1323 6459 acydburn
                        $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
1324 4671 ludovic_arnaud
                        $where_sql_and = $where_sql . "\n\tAND";
1325 3737 ludovic_arnaud
                }
1326 3737 ludovic_arnaud
        }
1327 3936 ludovic_arnaud
        else
1328 3936 ludovic_arnaud
        {
1329 6420 acydburn
                if (!sizeof($where_ids))
1330 4903 acydburn
                {
1331 6420 acydburn
                        return;
1332 4903 acydburn
                }
1333 6015 acydburn
1334 6420 acydburn
                // $where_type contains the field for the where clause (forum_id, topic_id)
1335 6459 acydburn
                $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
1336 6420 acydburn
                $where_sql_and = $where_sql . "\n\tAND";
1337 3936 ludovic_arnaud
        }
1338 3737 ludovic_arnaud
1339 3737 ludovic_arnaud
        switch ($mode)
1340 3737 ludovic_arnaud
        {
1341 4507 ludovic_arnaud
                case 'topic_moved':
1342 9937 Kellanved
                        $db->sql_transaction('begin');
1343 6497 acydburn
                        switch ($db->sql_layer)
1344 4507 ludovic_arnaud
                        {
1345 4507 ludovic_arnaud
                                case 'mysql4':
1346 5135 acydburn
                                case 'mysqli':
1347 4507 ludovic_arnaud
                                        $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1348 4507 ludovic_arnaud
                                                USING ' . TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
1349 4507 ludovic_arnaud
                                                WHERE t1.topic_moved_id = t2.topic_id
1350 4507 ludovic_arnaud
                                                        AND t1.forum_id = t2.forum_id";
1351 4507 ludovic_arnaud
                                        $db->sql_query($sql);
1352 4507 ludovic_arnaud
                                break;
1353 8782 acydburn
1354 4507 ludovic_arnaud
                                default:
1355 4507 ludovic_arnaud
                                        $sql = 'SELECT t1.topic_id
1356 4507 ludovic_arnaud
                                                FROM ' .TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
1357 4507 ludovic_arnaud
                                                WHERE t1.topic_moved_id = t2.topic_id
1358 4507 ludovic_arnaud
                                                        AND t1.forum_id = t2.forum_id";
1359 4903 acydburn
                                        $result = $db->sql_query($sql);
1360 4507 ludovic_arnaud
1361 6015 acydburn
                                        $topic_id_ary = array();
1362 6015 acydburn
                                        while ($row = $db->sql_fetchrow($result))
1363 4507 ludovic_arnaud
                                        {
1364 6015 acydburn
                                                $topic_id_ary[] = $row['topic_id'];
1365 6015 acydburn
                                        }
1366 6015 acydburn
                                        $db->sql_freeresult($result);
1367 4507 ludovic_arnaud
1368 6015 acydburn
                                        if (!sizeof($topic_id_ary))
1369 6015 acydburn
                                        {
1370 6015 acydburn
                                                return;
1371 4507 ludovic_arnaud
                                        }
1372 6015 acydburn
1373 6015 acydburn
                                        $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1374 6271 acydburn
                                                WHERE ' . $db->sql_in_set('topic_id', $topic_id_ary);
1375 6015 acydburn
                                        $db->sql_query($sql);
1376 9965 acydburn
1377 6015 acydburn
                                break;
1378 4507 ludovic_arnaud
                        }
1379 9965 acydburn
1380 9937 Kellanved
                        $db->sql_transaction('commit');
1381 9937 Kellanved
                        break;
1382 4507 ludovic_arnaud
1383 3936 ludovic_arnaud
                case 'topic_approved':
1384 9965 acydburn
1385 9937 Kellanved
                        $db->sql_transaction('begin');
1386 6497 acydburn
                        switch ($db->sql_layer)
1387 2286 psotfx
                        {
1388 4671 ludovic_arnaud
                                case 'mysql4':
1389 5135 acydburn
                                case 'mysqli':
1390 4671 ludovic_arnaud
                                        $sql = 'UPDATE ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
1391 4671 ludovic_arnaud
                                                SET t.topic_approved = p.post_approved
1392 4671 ludovic_arnaud
                                                $where_sql_and t.topic_first_post_id = p.post_id";
1393 4671 ludovic_arnaud
                                        $db->sql_query($sql);
1394 4671 ludovic_arnaud
                                break;
1395 6419 acydburn
1396 4671 ludovic_arnaud
                                default:
1397 4671 ludovic_arnaud
                                        $sql = 'SELECT t.topic_id, p.post_approved
1398 4671 ludovic_arnaud
                                                FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
1399 4671 ludovic_arnaud
                                                $where_sql_and p.post_id = t.topic_first_post_id
1400 4671 ludovic_arnaud
                                                        AND p.post_approved <> t.topic_approved";
1401 4671 ludovic_arnaud
                                        $result = $db->sql_query($sql);
1402 2286 psotfx
1403 4671 ludovic_arnaud
                                        $topic_ids = array();
1404 4671 ludovic_arnaud
                                        while ($row = $db->sql_fetchrow($result))
1405 4671 ludovic_arnaud
                                        {
1406 4671 ludovic_arnaud
                                                $topic_ids[] = $row['topic_id'];
1407 4671 ludovic_arnaud
                                        }
1408 6345 acydburn
                                        $db->sql_freeresult($result);
1409 4671 ludovic_arnaud
1410 5033 acydburn
                                        if (!sizeof($topic_ids))
1411 4671 ludovic_arnaud
                                        {
1412 4671 ludovic_arnaud
                                                return;
1413 4671 ludovic_arnaud
                                        }
1414 4671 ludovic_arnaud
1415 4671 ludovic_arnaud
                                        $sql = 'UPDATE ' . TOPICS_TABLE . '
1416 4671 ludovic_arnaud
                                                SET topic_approved = 1 - topic_approved
1417 6271 acydburn
                                                WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1418 4671 ludovic_arnaud
                                        $db->sql_query($sql);
1419 6015 acydburn
                                break;
1420 3737 ludovic_arnaud
                        }
1421 9965 acydburn
1422 9937 Kellanved
                        $db->sql_transaction('commit');
1423 9937 Kellanved
                        break;
1424 3760 ludovic_arnaud
1425 4671 ludovic_arnaud
                case 'post_reported':
1426 4671 ludovic_arnaud
                        $post_ids = $post_reported = array();
1427 9965 acydburn
1428 9937 Kellanved
                        $db->sql_transaction('begin');
1429 9965 acydburn
1430 4671 ludovic_arnaud
                        $sql = 'SELECT p.post_id, p.post_reported
1431 4671 ludovic_arnaud
                                FROM ' . POSTS_TABLE . " p
1432 4671 ludovic_arnaud
                                $where_sql
1433 4671 ludovic_arnaud
                                GROUP BY p.post_id, p.post_reported";
1434 4671 ludovic_arnaud
                        $result = $db->sql_query($sql);
1435 6015 acydburn
1436 4671 ludovic_arnaud
                        while ($row = $db->sql_fetchrow($result))
1437 3936 ludovic_arnaud
                        {
1438 4671 ludovic_arnaud
                                $post_ids[$row['post_id']] = $row['post_id'];
1439 4671 ludovic_arnaud
                                if ($row['post_reported'])
1440 4671 ludovic_arnaud
                                {
1441 4671 ludovic_arnaud
                                        $post_reported[$row['post_id']] = 1;
1442 4671 ludovic_arnaud
                                }
1443 4671 ludovic_arnaud
                        }
1444 6015 acydburn
                        $db->sql_freeresult($result);
1445 3961 psotfx
1446 4671 ludovic_arnaud
                        $sql = 'SELECT DISTINCT(post_id)
1447 4671 ludovic_arnaud
                                FROM ' . REPORTS_TABLE . '
1448 6271 acydburn
                                WHERE ' . $db->sql_in_set('post_id', $post_ids) . '
1449 6063 naderman
                                        AND report_closed = 0';
1450 3961 psotfx
                        $result = $db->sql_query($sql);
1451 3936 ludovic_arnaud
1452 4671 ludovic_arnaud
                        $post_ids = array();
1453 3961 psotfx
                        while ($row = $db->sql_fetchrow($result))
1454 3961 psotfx
                        {
1455 4671 ludovic_arnaud
                                if (!isset($post_reported[$row['post_id']]))
1456 3961 psotfx
                                {
1457 3961 psotfx
                                        $post_ids[] = $row['post_id'];
1458 3961 psotfx
                                }
1459 4671 ludovic_arnaud
                                else
1460 4671 ludovic_arnaud
                                {
1461 4671 ludovic_arnaud
                                        unset($post_reported[$row['post_id']]);
1462 4671 ludovic_arnaud
                                }
1463 3936 ludovic_arnaud
                        }
1464 6015 acydburn
                        $db->sql_freeresult($result);
1465 3936 ludovic_arnaud
1466 4671 ludovic_arnaud
                        // $post_reported should be empty by now, if it's not it contains
1467 4671 ludovic_arnaud
                        // posts that are falsely flagged as reported
1468 4671 ludovic_arnaud
                        foreach ($post_reported as $post_id => $void)
1469 3936 ludovic_arnaud
                        {
1470 4671 ludovic_arnaud
                                $post_ids[] = $post_id;
1471 3936 ludovic_arnaud
                        }
1472 3936 ludovic_arnaud
1473 5033 acydburn
                        if (sizeof($post_ids))
1474 4671 ludovic_arnaud
                        {
1475 4671 ludovic_arnaud
                                $sql = 'UPDATE ' . POSTS_TABLE . '
1476 4671 ludovic_arnaud
                                        SET post_reported = 1 - post_reported
1477 6271 acydburn
                                        WHERE ' . $db->sql_in_set('post_id', $post_ids);
1478 4671 ludovic_arnaud
                                $db->sql_query($sql);
1479 4671 ludovic_arnaud
                        }
1480 9965 acydburn
1481 9937 Kellanved
                        $db->sql_transaction('commit');
1482 9937 Kellanved
                        break;
1483 3936 ludovic_arnaud
1484 4671 ludovic_arnaud
                case 'topic_reported':
1485 4671 ludovic_arnaud
                        if ($sync_extra)
1486 4671 ludovic_arnaud
                        {
1487 4671 ludovic_arnaud
                                sync('post_reported', $where_type, $where_ids);
1488 4671 ludovic_arnaud
                        }
1489 3936 ludovic_arnaud
1490 4671 ludovic_arnaud
                        $topic_ids = $topic_reported = array();
1491 4671 ludovic_arnaud
1492 9937 Kellanved
                        $db->sql_transaction('begin');
1493 9965 acydburn
1494 4671 ludovic_arnaud
                        $sql = 'SELECT DISTINCT(t.topic_id)
1495 4671 ludovic_arnaud
                                FROM ' . POSTS_TABLE . " t
1496 4671 ludovic_arnaud
                                $where_sql_and t.post_reported = 1";
1497 4671 ludovic_arnaud
                        $result = $db->sql_query($sql);
1498 6015 acydburn
1499 4671 ludovic_arnaud
                        while ($row = $db->sql_fetchrow($result))
1500 3936 ludovic_arnaud
                        {
1501 4671 ludovic_arnaud
                                $topic_reported[$row['topic_id']] = 1;
1502 4671 ludovic_arnaud
                        }
1503 6015 acydburn
                        $db->sql_freeresult($result);
1504 3961 psotfx
1505 4671 ludovic_arnaud
                        $sql = 'SELECT t.topic_id, t.topic_reported
1506 4671 ludovic_arnaud
                                FROM ' . TOPICS_TABLE . " t
1507 4671 ludovic_arnaud
                                $where_sql";
1508 3961 psotfx
                        $result = $db->sql_query($sql);
1509 6015 acydburn
1510 3961 psotfx
                        while ($row = $db->sql_fetchrow($result))
1511 3961 psotfx
                        {
1512 4671 ludovic_arnaud
                                if ($row['topic_reported'] ^ isset($topic_reported[$row['topic_id']]))
1513 4671 ludovic_arnaud
                                {
1514 3961 psotfx
                                        $topic_ids[] = $row['topic_id'];
1515 4671 ludovic_arnaud
                                }
1516 3936 ludovic_arnaud
                        }
1517 6015 acydburn
                        $db->sql_freeresult($result);
1518 3936 ludovic_arnaud
1519 5033 acydburn
                        if (sizeof($topic_ids))
1520 3936 ludovic_arnaud
                        {
1521 4671 ludovic_arnaud
                                $sql = 'UPDATE ' . TOPICS_TABLE . '
1522 4671 ludovic_arnaud
                                        SET topic_reported = 1 - topic_reported
1523 6271 acydburn
                                        WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1524 4671 ludovic_arnaud
                                $db->sql_query($sql);
1525 3936 ludovic_arnaud
                        }
1526 9965 acydburn
1527 9937 Kellanved
                        $db->sql_transaction('commit');
1528 9937 Kellanved
                        break;
1529 3936 ludovic_arnaud
1530 4671 ludovic_arnaud
                case 'post_attachment':
1531 4671 ludovic_arnaud
                        $post_ids = $post_attachment = array();
1532 3760 ludovic_arnaud
1533 9937 Kellanved
                        $db->sql_transaction('begin');
1534 9965 acydburn
1535 4671 ludovic_arnaud
                        $sql = 'SELECT p.post_id, p.post_attachment
1536 4671 ludovic_arnaud
                                FROM ' . POSTS_TABLE . " p
1537 4671 ludovic_arnaud
                                $where_sql
1538 4671 ludovic_arnaud
                                GROUP BY p.post_id, p.post_attachment";
1539 4671 ludovic_arnaud
                        $result = $db->sql_query($sql);
1540 6015 acydburn
1541 4671 ludovic_arnaud
                        while ($row = $db->sql_fetchrow($result))
1542 3737 ludovic_arnaud
                        {
1543 4671 ludovic_arnaud
                                $post_ids[$row['post_id']] = $row['post_id'];
1544 4671 ludovic_arnaud
                                if ($row['post_attachment'])
1545 3760 ludovic_arnaud
                                {
1546 4671 ludovic_arnaud
                                        $post_attachment[$row['post_id']] = 1;
1547 3760 ludovic_arnaud
                                }
1548 3737 ludovic_arnaud
                        }
1549 6015 acydburn
                        $db->sql_freeresult($result);
1550 3737 ludovic_arnaud
1551 4915 acydburn
                        $sql = 'SELECT DISTINCT(post_msg_id)
1552 4671 ludovic_arnaud
                                FROM ' . ATTACHMENTS_TABLE . '
1553 6271 acydburn
                                WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
1554 4915 acydburn
                                        AND in_message = 0';
1555 6015 acydburn
                        $result = $db->sql_query($sql);
1556 3961 psotfx
1557 4671 ludovic_arnaud
                        $post_ids = array();
1558 4671 ludovic_arnaud
                        while ($row = $db->sql_fetchrow($result))
1559 2286 psotfx
                        {
1560 7330 acydburn
                                if (!isset($post_attachment[$row['post_msg_id']]))
1561 3737 ludovic_arnaud
                                {
1562 7330 acydburn
                                        $post_ids[] = $row['post_msg_id'];
1563 3737 ludovic_arnaud
                                }
1564 4671 ludovic_arnaud
                                else
1565 3760 ludovic_arnaud
                                {
1566 7330 acydburn
                                        unset($post_attachment[$row['post_msg_id']]);
1567 3760 ludovic_arnaud
                                }
1568 4671 ludovic_arnaud
                        }
1569 6015 acydburn
                        $db->sql_freeresult($result);
1570 2286 psotfx
1571 4671 ludovic_arnaud
                        // $post_attachment should be empty by now, if it's not it contains
1572 4671 ludovic_arnaud
                        // posts that are falsely flagged as having attachments
1573 4671 ludovic_arnaud
                        foreach ($post_attachment as $post_id => $void)
1574 4671 ludovic_arnaud
                        {
1575 4671 ludovic_arnaud
                                $post_ids[] = $post_id;
1576 2286 psotfx
                        }
1577 3961 psotfx
1578 5033 acydburn
                        if (sizeof($post_ids))
1579 4671 ludovic_arnaud
                        {
1580 4671 ludovic_arnaud
                                $sql = 'UPDATE ' . POSTS_TABLE . '
1581 4671 ludovic_arnaud
                                        SET post_attachment = 1 - post_attachment
1582 6271 acydburn
                                        WHERE ' . $db->sql_in_set('post_id', $post_ids);
1583 4671 ludovic_arnaud
                                $db->sql_query($sql);
1584 4671 ludovic_arnaud
                        }
1585 9965 acydburn
1586 9937 Kellanved
                        $db->sql_transaction('commit');
1587 9937 Kellanved
                        break;
1588 3737 ludovic_arnaud
1589 4671 ludovic_arnaud
                case 'topic_attachment':
1590 4671 ludovic_arnaud
                        if ($sync_extra)
1591 2286 psotfx
                        {
1592 4671 ludovic_arnaud
                                sync('post_attachment', $where_type, $where_ids);
1593 4671 ludovic_arnaud
                        }
1594 3737 ludovic_arnaud
1595 4671 ludovic_arnaud
                        $topic_ids = $topic_attachment = array();
1596 4671 ludovic_arnaud
1597 9937 Kellanved
                        $db->sql_transaction('begin');
1598 9937 Kellanved
1599 4671 ludovic_arnaud
                        $sql = 'SELECT DISTINCT(t.topic_id)
1600 4671 ludovic_arnaud
                                FROM ' . POSTS_TABLE . " t
1601 4671 ludovic_arnaud
                                $where_sql_and t.post_attachment = 1";
1602 4671 ludovic_arnaud
                        $result = $db->sql_query($sql);
1603 6015 acydburn
1604 4671 ludovic_arnaud
                        while ($row = $db->sql_fetchrow($result))
1605 4671 ludovic_arnaud
                        {
1606 4671 ludovic_arnaud
                                $topic_attachment[$row['topic_id']] = 1;
1607 4671 ludovic_arnaud
                        }
1608 6015 acydburn
                        $db->sql_freeresult($result);
1609 4671 ludovic_arnaud
1610 4671 ludovic_arnaud
                        $sql = 'SELECT t.topic_id, t.topic_attachment
1611 4671 ludovic_arnaud
                                FROM ' . TOPICS_TABLE . " t
1612 4671 ludovic_arnaud
                                $where_sql";
1613 4671 ludovic_arnaud
                        $result = $db->sql_query($sql);
1614 6015 acydburn
1615 4671 ludovic_arnaud
                        while ($row = $db->sql_fetchrow($result))
1616 4671 ludovic_arnaud
                        {
1617 4671 ludovic_arnaud
                                if ($row['topic_attachment'] ^ isset($topic_attachment[$row['topic_id']]))
1618 3737 ludovic_arnaud
                                {
1619 4671 ludovic_arnaud
                                        $topic_ids[] = $row['topic_id'];
1620 3737 ludovic_arnaud
                                }
1621 4671 ludovic_arnaud
                        }
1622 6015 acydburn
                        $db->sql_freeresult($result);
1623 3737 ludovic_arnaud
1624 5033 acydburn
                        if (sizeof($topic_ids))
1625 4671 ludovic_arnaud
                        {
1626 4671 ludovic_arnaud
                                $sql = 'UPDATE ' . TOPICS_TABLE . '
1627 4671 ludovic_arnaud
                                        SET topic_attachment = 1 - topic_attachment
1628 6271 acydburn
                                        WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1629 4671 ludovic_arnaud
                                $db->sql_query($sql);
1630 2286 psotfx
                        }
1631 9965 acydburn
1632 9937 Kellanved
                        $db->sql_transaction('commit');
1633 9965 acydburn
1634 9937 Kellanved
                        break;
1635 2286 psotfx
1636 4671 ludovic_arnaud
                case 'forum':
1637 6015 acydburn
1638 9937 Kellanved
                        $db->sql_transaction('begin');
1639 9965 acydburn
1640 5026 bartvb
                        // 1: Get the list of all forums
1641 4534 ludovic_arnaud
                        $sql = 'SELECT f.*
1642 4534 ludovic_arnaud
                                FROM ' . FORUMS_TABLE . " f
1643 4534 ludovic_arnaud
                                $where_sql";
1644 3961 psotfx
                        $result = $db->sql_query($sql);
1645 3737 ludovic_arnaud
1646 4534 ludovic_arnaud
                        $forum_data = $forum_ids = $post_ids = $last_post_id = $post_info = array();
1647 3737 ludovic_arnaud
                        while ($row = $db->sql_fetchrow($result))
1648 3737 ludovic_arnaud
                        {
1649 3961 psotfx
                                if ($row['forum_type'] == FORUM_LINK)
1650 3961 psotfx
                                {
1651 3961 psotfx
                                        continue;
1652 3961 psotfx
                                }
1653 3961 psotfx
1654 4534 ludovic_arnaud
                                $forum_id = (int) $row['forum_id'];
1655 4534 ludovic_arnaud
                                $forum_ids[$forum_id] = $forum_id;
1656 4534 ludovic_arnaud
1657 4534 ludovic_arnaud
                                $forum_data[$forum_id] = $row;
1658 7466 davidmj
                                if ($sync_extra)
1659 7466 davidmj
                                {
1660 7466 davidmj
                                        $forum_data[$forum_id]['posts'] = 0;
1661 7466 davidmj
                                        $forum_data[$forum_id]['topics'] = 0;
1662 7466 davidmj
                                        $forum_data[$forum_id]['topics_real'] = 0;
1663 7466 davidmj
                                }
1664 4534 ludovic_arnaud
                                $forum_data[$forum_id]['last_post_id'] = 0;
1665 6360 grahamje
                                $forum_data[$forum_id]['last_post_subject'] = '';
1666 4534 ludovic_arnaud
                                $forum_data[$forum_id]['last_post_time'] = 0;
1667 4534 ludovic_arnaud
                                $forum_data[$forum_id]['last_poster_id'] = 0;
1668 4534 ludovic_arnaud
                                $forum_data[$forum_id]['last_poster_name'] = '';
1669 6315 grahamje
                                $forum_data[$forum_id]['last_poster_colour'] = '';
1670 3737 ludovic_arnaud
                        }
1671 6015 acydburn
                        $db->sql_freeresult($result);
1672 2286 psotfx
1673 6114 acydburn
                        if (!sizeof($forum_ids))
1674 6114 acydburn
                        {
1675 6114 acydburn
                                break;
1676 6114 acydburn
                        }
1677 6114 acydburn
1678 6411 acydburn
                        $forum_ids = array_values($forum_ids);
1679 6411 acydburn
1680 7466 davidmj
                        // 2: Get topic counts for each forum (optional)
1681 7466 davidmj
                        if ($sync_extra)
1682 7466 davidmj
                        {
1683 7466 davidmj
                                $sql = 'SELECT forum_id, topic_approved, COUNT(topic_id) AS forum_topics
1684 7466 davidmj
                                        FROM ' . TOPICS_TABLE . '
1685 7466 davidmj
                                        WHERE ' . $db->sql_in_set('forum_id', $forum_ids) . '
1686 7466 davidmj
                                        GROUP BY forum_id, topic_approved';
1687 7466 davidmj
                                $result = $db->sql_query($sql);
1688 6015 acydburn
1689 7466 davidmj
                                while ($row = $db->sql_fetchrow($result))
1690 7466 davidmj
                                {
1691 7466 davidmj
                                        $forum_id = (int) $row['forum_id'];
1692 7466 davidmj
                                        $forum_data[$forum_id]['topics_real'] += $row['forum_topics'];
1693 7466 davidmj
1694 7466 davidmj
                                        if ($row['topic_approved'])
1695 7466 davidmj
                                        {
1696 7466 davidmj
                                                $forum_data[$forum_id]['topics'] = $row['forum_topics'];
1697 7466 davidmj
                                        }
1698 7466 davidmj
                                }
1699 7466 davidmj
                                $db->sql_freeresult($result);
1700 7466 davidmj
                        }
1701 7466 davidmj
1702 7466 davidmj
                        // 3: Get post count for each forum (optional)
1703 7466 davidmj
                        if ($sync_extra)
1704 3737 ludovic_arnaud
                        {
1705 7466 davidmj
                                if (sizeof($forum_ids) == 1)
1706 7466 davidmj
                                {
1707 7485 acydburn
                                        $sql = 'SELECT SUM(t.topic_replies + 1) AS forum_posts
1708 7466 davidmj
                                                FROM ' . TOPICS_TABLE . ' t
1709 7466 davidmj
                                                WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
1710 8506 davidmj
                                                        AND t.topic_approved = 1
1711 8506 davidmj
                                                        AND t.topic_status <> ' . ITEM_MOVED;
1712 7466 davidmj
                                }
1713 7466 davidmj
                                else
1714 7466 davidmj
                                {
1715 7485 acydburn
                                        $sql = 'SELECT t.forum_id, SUM(t.topic_replies + 1) AS forum_posts
1716 7466 davidmj
                                                FROM ' . TOPICS_TABLE . ' t
1717 7466 davidmj
                                                WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
1718 7466 davidmj
                                                        AND t.topic_approved = 1
1719 8508 davidmj
                                                        AND t.topic_status <> ' . ITEM_MOVED . '
1720 7485 acydburn
                                                GROUP BY t.forum_id';
1721 7466 davidmj
                                }
1722 3961 psotfx
1723 7466 davidmj
                                $result = $db->sql_query($sql);
1724 7466 davidmj
1725 7466 davidmj
                                while ($row = $db->sql_fetchrow($result))
1726 3737 ludovic_arnaud
                                {
1727 7466 davidmj
                                        $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
1728 7466 davidmj
1729 7466 davidmj
                                        $forum_data[$forum_id]['posts'] = (int) $row['forum_posts'];
1730 3737 ludovic_arnaud
                                }
1731 7466 davidmj
                                $db->sql_freeresult($result);
1732 3737 ludovic_arnaud
                        }
1733 2286 psotfx
1734 7466 davidmj
                        // 4: Get last_post_id for each forum
1735 6411 acydburn
                        if (sizeof($forum_ids) == 1)
1736 6411 acydburn
                        {
1737 7466 davidmj
                                $sql = 'SELECT MAX(t.topic_last_post_id) as last_post_id
1738 7466 davidmj
                                        FROM ' . TOPICS_TABLE . ' t
1739 7466 davidmj
                                        WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
1740 7466 davidmj
                                                AND t.topic_approved = 1';
1741 6411 acydburn
                        }
1742 6411 acydburn
                        else
1743 6411 acydburn
                        {
1744 7466 davidmj
                                $sql = 'SELECT t.forum_id, MAX(t.topic_last_post_id) as last_post_id
1745 7466 davidmj
                                        FROM ' . TOPICS_TABLE . ' t
1746 7466 davidmj
                                        WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
1747 7191 naderman
                                                AND t.topic_approved = 1
1748 7468 davidmj
                                        GROUP BY t.forum_id';
1749 6411 acydburn
                        }
1750 6411 acydburn
1751 3737 ludovic_arnaud
                        $result = $db->sql_query($sql);
1752 6015 acydburn
1753 3737 ludovic_arnaud
                        while ($row = $db->sql_fetchrow($result))
1754 3737 ludovic_arnaud
                        {
1755 6411 acydburn
                                $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
1756 2286 psotfx
1757 6015 acydburn
                                $forum_data[$forum_id]['last_post_id'] = (int) $row['last_post_id'];
1758 3961 psotfx
1759 4534 ludovic_arnaud
                                $post_ids[] = $row['last_post_id'];
1760 3737 ludovic_arnaud
                        }
1761 6015 acydburn
                        $db->sql_freeresult($result);
1762 3737 ludovic_arnaud
1763 7466 davidmj
                        // 5: Retrieve last_post infos
1764 5033 acydburn
                        if (sizeof($post_ids))
1765 3737 ludovic_arnaud
                        {
1766 6360 grahamje
                                $sql = 'SELECT p.post_id, p.poster_id, p.post_subject, p.post_time, p.post_username, u.username, u.user_colour
1767 3737 ludovic_arnaud
                                        FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1768 6271 acydburn
                                        WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
1769 3737 ludovic_arnaud
                                                AND p.poster_id = u.user_id';
1770 3737 ludovic_arnaud
                                $result = $db->sql_query($sql);
1771 6015 acydburn
1772 3737 ludovic_arnaud
                                while ($row = $db->sql_fetchrow($result))
1773 3737 ludovic_arnaud
                                {
1774 6015 acydburn
                                        $post_info[$row['post_id']] = $row;
1775 3737 ludovic_arnaud
                                }
1776 3833 psotfx
                                $db->sql_freeresult($result);
1777 3737 ludovic_arnaud
1778 3737 ludovic_arnaud
                                foreach ($forum_data as $forum_id => $data)
1779 3737 ludovic_arnaud
                                {
1780 3737 ludovic_arnaud
                                        if ($data['last_post_id'])
1781 3737 ludovic_arnaud
                                        {
1782 4534 ludovic_arnaud
                                                if (isset($post_info[$data['last_post_id']]))
1783 4534 ludovic_arnaud
                                                {
1784 6360 grahamje
                                                        $forum_data[$forum_id]['last_post_subject'] = $post_info[$data['last_post_id']]['post_subject'];
1785 4534 ludovic_arnaud
                                                        $forum_data[$forum_id]['last_post_time'] = $post_info[$data['last_post_id']]['post_time'];
1786 4534 ludovic_arnaud
                                                        $forum_data[$forum_id]['last_poster_id'] = $post_info[$data['last_post_id']]['poster_id'];
1787 4534 ludovic_arnaud
                                                        $forum_data[$forum_id]['last_poster_name'] = ($post_info[$data['last_post_id']]['poster_id'] != ANONYMOUS) ? $post_info[$data['last_post_id']]['username'] : $post_info[$data['last_post_id']]['post_username'];
1788 6315 grahamje
                                                        $forum_data[$forum_id]['last_poster_colour'] = $post_info[$data['last_post_id']]['user_colour'];
1789 4534 ludovic_arnaud
                                                }
1790 4534 ludovic_arnaud
                                                else
1791 4534 ludovic_arnaud
                                                {
1792 4534 ludovic_arnaud
                                                        // For some reason we did not find the post in the db
1793 4534 ludovic_arnaud
                                                        $forum_data[$forum_id]['last_post_id'] = 0;
1794 6360 grahamje
                                                        $forum_data[$forum_id]['last_post_subject'] = '';
1795 4534 ludovic_arnaud
                                                        $forum_data[$forum_id]['last_post_time'] = 0;
1796 4534 ludovic_arnaud
                                                        $forum_data[$forum_id]['last_poster_id'] = 0;
1797 4534 ludovic_arnaud
                                                        $forum_data[$forum_id]['last_poster_name'] = '';
1798 6315 grahamje
                                                        $forum_data[$forum_id]['last_poster_colour'] = '';
1799 4534 ludovic_arnaud
                                                }
1800 3737 ludovic_arnaud
                                        }
1801 3737 ludovic_arnaud
                                }
1802 4534 ludovic_arnaud
                                unset($post_info);
1803 3737 ludovic_arnaud
                        }
1804 3737 ludovic_arnaud
1805 7466 davidmj
                        // 6: Now do that thing
1806 7466 davidmj
                        $fieldnames = array('last_post_id', 'last_post_subject', 'last_post_time', 'last_poster_id', 'last_poster_name', 'last_poster_colour');
1807 3737 ludovic_arnaud
1808 7466 davidmj
                        if ($sync_extra)
1809 7466 davidmj
                        {
1810 7466 davidmj
                                array_push($fieldnames, 'posts', 'topics', 'topics_real');
1811 7466 davidmj
                        }
1812 7466 davidmj
1813 3737 ludovic_arnaud
                        foreach ($forum_data as $forum_id => $row)
1814 3737 ludovic_arnaud
                        {
1815 6015 acydburn
                                $sql_ary = array();
1816 3737 ludovic_arnaud
1817 3737 ludovic_arnaud
                                foreach ($fieldnames as $fieldname)
1818 3737 ludovic_arnaud
                                {
1819 4534 ludovic_arnaud
                                        if ($row['forum_' . $fieldname] != $row[$fieldname])
1820 3737 ludovic_arnaud
                                        {
1821 6360 grahamje
                                                if (preg_match('#(name|colour|subject)$#', $fieldname))
1822 3737 ludovic_arnaud
                                                {
1823 6015 acydburn
                                                        $sql_ary['forum_' . $fieldname] = (string) $row[$fieldname];
1824 3737 ludovic_arnaud
                                                }
1825 3737 ludovic_arnaud
                                                else
1826 3737 ludovic_arnaud
                                                {
1827 6015 acydburn
                                                        $sql_ary['forum_' . $fieldname] = (int) $row[$fieldname];
1828 3737 ludovic_arnaud
                                                }
1829 3737 ludovic_arnaud
                                        }
1830 4534 ludovic_arnaud
                                }
1831 3737 ludovic_arnaud
1832 6015 acydburn
                                if (sizeof($sql_ary))
1833 4534 ludovic_arnaud
                                {
1834 4534 ludovic_arnaud
                                        $sql = 'UPDATE ' . FORUMS_TABLE . '
1835 6015 acydburn
                                                SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
1836 4534 ludovic_arnaud
                                                WHERE forum_id = ' . $forum_id;
1837 3737 ludovic_arnaud
                                        $db->sql_query($sql);
1838 3737 ludovic_arnaud
                                }
1839 3737 ludovic_arnaud
                        }
1840 9965 acydburn
1841 9937 Kellanved
                        $db->sql_transaction('commit');
1842 9937 Kellanved
                        break;
1843 3737 ludovic_arnaud
1844 2286 psotfx
                case 'topic':
1845 6271 acydburn
                        $topic_data = $post_ids = $approved_unapproved_ids = $resync_forums = $delete_topics = $delete_posts = $moved_topics = array();
1846 3737 ludovic_arnaud
1847 9937 Kellanved
                        $db->sql_transaction('begin');
1848 9965 acydburn
1849 6360 grahamje
                        $sql = 'SELECT t.topic_id, t.forum_id, t.topic_moved_id, t.topic_approved, ' . (($sync_extra) ? 't.topic_attachment, t.topic_reported, ' : '') . 't.topic_poster, t.topic_time, t.topic_replies, t.topic_replies_real, t.topic_first_post_id, t.topic_first_poster_name, t.topic_first_poster_colour, t.topic_last_post_id, t.topic_last_post_subject, t.topic_last_poster_id, t.topic_last_poster_name, t.topic_last_poster_colour, t.topic_last_post_time
1850 4534 ludovic_arnaud
                                FROM ' . TOPICS_TABLE . " t
1851 4534 ludovic_arnaud
                                $where_sql";
1852 3961 psotfx
                        $result = $db->sql_query($sql);
1853 3737 ludovic_arnaud
1854 3737 ludovic_arnaud
                        while ($row = $db->sql_fetchrow($result))
1855 3737 ludovic_arnaud
                        {
1856 6271 acydburn
                                if ($row['topic_moved_id'])
1857 6271 acydburn
                                {
1858 6271 acydburn
                                        $moved_topics[] = $row['topic_id'];
1859 6271 acydburn
                                        continue;
1860 6271 acydburn
                                }
1861 6271 acydburn
1862 4534 ludovic_arnaud
                                $topic_id = (int) $row['topic_id'];
1863 4534 ludovic_arnaud
                                $topic_data[$topic_id] = $row;
1864 4534 ludovic_arnaud
                                $topic_data[$topic_id]['replies_real'] = -1;
1865 6411 acydburn
                                $topic_data[$topic_id]['replies'] = 0;
1866 4534 ludovic_arnaud
                                $topic_data[$topic_id]['first_post_id'] = 0;
1867 4534 ludovic_arnaud
                                $topic_data[$topic_id]['last_post_id'] = 0;
1868 4534 ludovic_arnaud
                                unset($topic_data[$topic_id]['topic_id']);
1869 2286 psotfx
1870 4671 ludovic_arnaud
                                // This array holds all topic_ids
1871 4671 ludovic_arnaud
                                $delete_topics[$topic_id] = '';
1872 4671 ludovic_arnaud
1873 4534 ludovic_arnaud
                                if ($sync_extra)
1874 3737 ludovic_arnaud
                                {
1875 4534 ludovic_arnaud
                                        $topic_data[$topic_id]['reported'] = 0;
1876 4534 ludovic_arnaud
                                        $topic_data[$topic_id]['attachment'] = 0;
1877 3737 ludovic_arnaud
                                }
1878 4534 ludovic_arnaud
                        }
1879 4534 ludovic_arnaud
                        $db->sql_freeresult($result);
1880 4534 ludovic_arnaud
1881 4534 ludovic_arnaud
                        // Use "t" as table alias because of the $where_sql clause
1882 5033 acydburn
                        // NOTE: 't.post_approved' in the GROUP BY is causing a major slowdown.
1883 4534 ludovic_arnaud
                        $sql = 'SELECT t.topic_id, t.post_approved, COUNT(t.post_id) AS total_posts, MIN(t.post_id) AS first_post_id, MAX(t.post_id) AS last_post_id
1884 4534 ludovic_arnaud
                                FROM ' . POSTS_TABLE . " t
1885 6411 acydburn
                                $where_sql
1886 6411 acydburn
                                GROUP BY t.topic_id, t.post_approved";
1887 4534 ludovic_arnaud
                        $result = $db->sql_query($sql);
1888 4534 ludovic_arnaud
1889 4534 ludovic_arnaud
                        while ($row = $db->sql_fetchrow($result))
1890 4534 ludovic_arnaud
                        {
1891 4534 ludovic_arnaud
                                $topic_id = (int) $row['topic_id'];
1892 4534 ludovic_arnaud
1893 4534 ludovic_arnaud
                                $row['first_post_id'] = (int) $row['first_post_id'];
1894 4534 ludovic_arnaud
                                $row['last_post_id'] = (int) $row['last_post_id'];
1895 4534 ludovic_arnaud
1896 4534 ludovic_arnaud
                                if (!isset($topic_data[$topic_id]))
1897 4534 ludovic_arnaud
                                {
1898 4534 ludovic_arnaud
                                        // Hey, these posts come from a topic that does not exist
1899 4534 ludovic_arnaud
                                        $delete_posts[$topic_id] = '';
1900 4534 ludovic_arnaud
                                }
1901 3737 ludovic_arnaud
                                else
1902 3737 ludovic_arnaud
                                {
1903 4534 ludovic_arnaud
                                        // Unset the corresponding entry in $delete_topics
1904 4534 ludovic_arnaud
                                        // When we'll be done, only topics with no posts will remain
1905 4534 ludovic_arnaud
                                        unset($delete_topics[$topic_id]);
1906 3737 ludovic_arnaud
1907 4534 ludovic_arnaud
                                        $topic_data[$topic_id]['replies_real'] += $row['total_posts'];
1908 4534 ludovic_arnaud
                                        $topic_data[$topic_id]['first_post_id'] = (!$topic_data[$topic_id]['first_post_id']) ? $row['first_post_id'] : min($topic_data[$topic_id]['first_post_id'], $row['first_post_id']);
1909 4534 ludovic_arnaud
1910 4534 ludovic_arnaud
                                        if ($row['post_approved'] || !$topic_data[$topic_id]['last_post_id'])
1911 3737 ludovic_arnaud
                                        {
1912 4534 ludovic_arnaud
                                                $topic_data[$topic_id]['replies'] = $row['total_posts'] - 1;
1913 4534 ludovic_arnaud
                                                $topic_data[$topic_id]['last_post_id'] = $row['last_post_id'];
1914 3737 ludovic_arnaud
                                        }
1915 3737 ludovic_arnaud
                                }
1916 3737 ludovic_arnaud
                        }
1917 4507 ludovic_arnaud
                        $db->sql_freeresult($result);
1918 3737 ludovic_arnaud
1919 3737 ludovic_arnaud
                        foreach ($topic_data as $topic_id => $row)
1920 2286 psotfx
                        {
1921 3737 ludovic_arnaud
                                $post_ids[] = $row['first_post_id'];
1922 3737 ludovic_arnaud
                                if ($row['first_post_id'] != $row['last_post_id'])
1923 3737 ludovic_arnaud
                                {
1924 3737 ludovic_arnaud
                                        $post_ids[] = $row['last_post_id'];
1925 3737 ludovic_arnaud
                                }
1926 2286 psotfx
                        }
1927 2286 psotfx
1928 4534 ludovic_arnaud
                        // Now we delete empty topics and orphan posts
1929 5033 acydburn
                        if (sizeof($delete_posts))
1930 3737 ludovic_arnaud
                        {
1931 5313 acydburn
                                delete_posts('topic_id', array_keys($delete_posts), false);
1932 4534 ludovic_arnaud
                                unset($delete_posts);
1933 4534 ludovic_arnaud
                        }
1934 5033 acydburn
1935 5033 acydburn
                        if (!sizeof($topic_data))
1936 4534 ludovic_arnaud
                        {
1937 3737 ludovic_arnaud
                                // If we get there, topic ids were invalid or topics did not contain any posts
1938 5313 acydburn
                                delete_topics($where_type, $where_ids, true);
1939 3737 ludovic_arnaud
                                return;
1940 3737 ludovic_arnaud
                        }
1941 6015 acydburn
1942 5033 acydburn
                        if (sizeof($delete_topics))
1943 3737 ludovic_arnaud
                        {
1944 4671 ludovic_arnaud
                                $delete_topic_ids = array();
1945 4671 ludovic_arnaud
                                foreach ($delete_topics as $topic_id => $void)
1946 4671 ludovic_arnaud
                                {
1947 4671 ludovic_arnaud
                                        unset($topic_data[$topic_id]);
1948 4671 ludovic_arnaud
                                        $delete_topic_ids[] = $topic_id;
1949 4671 ludovic_arnaud
                                }
1950 4671 ludovic_arnaud
1951 5313 acydburn
                                delete_topics('topic_id', $delete_topic_ids, false);
1952 4671 ludovic_arnaud
                                unset($delete_topics, $delete_topic_ids);
1953 3737 ludovic_arnaud
                        }
1954 2664 psotfx
1955 6360 grahamje
                        $sql = 'SELECT p.post_id, p.topic_id, p.post_approved, p.poster_id, p.post_subject, p.post_username, p.post_time, u.username, u.user_colour
1956 3737 ludovic_arnaud
                                FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1957 6271 acydburn
                                WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
1958 3737 ludovic_arnaud
                                        AND u.user_id = p.poster_id';
1959 3961 psotfx
                        $result = $db->sql_query($sql);
1960 3760 ludovic_arnaud
1961 3760 ludovic_arnaud
                        $post_ids = array();
1962 3737 ludovic_arnaud
                        while ($row = $db->sql_fetchrow($result))
1963 3737 ludovic_arnaud
                        {
1964 4534 ludovic_arnaud
                                $topic_id = intval($row['topic_id']);
1965 4534 ludovic_arnaud
1966 4534 ludovic_arnaud
                                if ($row['post_id'] == $topic_data[$topic_id]['first_post_id'])
1967 3737 ludovic_arnaud
                                {
1968 4534 ludovic_arnaud
                                        if ($topic_data[$topic_id]['topic_approved'] != $row['post_approved'])
1969 3737 ludovic_arnaud
                                        {
1970 4534 ludovic_arnaud
                                                $approved_unapproved_ids[] = $topic_id;
1971 3737 ludovic_arnaud
                                        }
1972 4534 ludovic_arnaud
                                        $topic_data[$topic_id]['time'] = $row['post_time'];
1973 4534 ludovic_arnaud
                                        $topic_data[$topic_id]['poster'] = $row['poster_id'];
1974 4534 ludovic_arnaud
                                        $topic_data[$topic_id]['first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
1975 6309 grahamje
                                        $topic_data[$topic_id]['first_poster_colour'] = $row['user_colour'];
1976 3737 ludovic_arnaud
                                }
1977 6015 acydburn
1978 4534 ludovic_arnaud
                                if ($row['post_id'] == $topic_data[$topic_id]['last_post_id'])
1979 3737 ludovic_arnaud
                                {
1980 4534 ludovic_arnaud
                                        $topic_data[$topic_id]['last_poster_id'] = $row['poster_id'];
1981 6360 grahamje
                                        $topic_data[$topic_id]['last_post_subject'] = $row['post_subject'];
1982 4534 ludovic_arnaud
                                        $topic_data[$topic_id]['last_post_time'] = $row['post_time'];
1983 4534 ludovic_arnaud
                                        $topic_data[$topic_id]['last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
1984 6309 grahamje
                                        $topic_data[$topic_id]['last_poster_colour'] = $row['user_colour'];
1985 3737 ludovic_arnaud
                                }
1986 3737 ludovic_arnaud
                        }
1987 4507 ludovic_arnaud
                        $db->sql_freeresult($result);
1988 2942 psotfx
1989 7188 acydburn
                        // Make sure shadow topics do link to existing topics
1990 7188 acydburn
                        if (sizeof($moved_topics))
1991 7188 acydburn
                        {
1992 7188 acydburn
                                $delete_topics = array();
1993 7188 acydburn
1994 7188 acydburn
                                $sql = 'SELECT t1.topic_id, t1.topic_moved_id
1995 7188 acydburn
                                        FROM ' . TOPICS_TABLE . ' t1
1996 7188 acydburn
                                        LEFT JOIN ' . TOPICS_TABLE . ' t2 ON (t2.topic_id = t1.topic_moved_id)
1997 7188 acydburn
                                        WHERE ' . $db->sql_in_set('t1.topic_id', $moved_topics) . '
1998 7188 acydburn
                                                AND t2.topic_id IS NULL';
1999 7188 acydburn
                                $result = $db->sql_query($sql);
2000 7188 acydburn
2001 7188 acydburn
                                while ($row = $db->sql_fetchrow($result))
2002 7188 acydburn
                                {
2003 7188 acydburn
                                        $delete_topics[] = $row['topic_id'];
2004 7188 acydburn
                                }
2005 7188 acydburn
                                $db->sql_freeresult($result);
2006 7188 acydburn
2007 7188 acydburn
                                if (sizeof($delete_topics))
2008 7188 acydburn
                                {
2009 7188 acydburn
                                        delete_topics('topic_id', $delete_topics, false);
2010 7188 acydburn
                                }
2011 7188 acydburn
                                unset($delete_topics);
2012 7188 acydburn
2013 7188 acydburn
                                // Make sure shadow topics having no last post data being updated (this only rarely happens...)
2014 7188 acydburn
                                $sql = 'SELECT topic_id, topic_moved_id, topic_last_post_id, topic_first_post_id
2015 7188 acydburn
                                        FROM ' . TOPICS_TABLE . '
2016 7188 acydburn
                                        WHERE ' . $db->sql_in_set('topic_id', $moved_topics) . '
2017 7188 acydburn
                                                AND topic_last_post_time = 0';
2018 7188 acydburn
                                $result = $db->sql_query($sql);
2019 7188 acydburn
2020 7188 acydburn
                                $shadow_topic_data = $post_ids = array();
2021 7188 acydburn
                                while ($row = $db->sql_fetchrow($result))
2022 7188 acydburn
                                {
2023 7188 acydburn
                                        $shadow_topic_data[$row['topic_moved_id']] = $row;
2024 7188 acydburn
                                        $post_ids[] = $row['topic_last_post_id'];
2025 7188 acydburn
                                        $post_ids[] = $row['topic_first_post_id'];
2026 7188 acydburn
                                }
2027 7188 acydburn
                                $db->sql_freeresult($result);
2028 7188 acydburn
2029 7188 acydburn
                                $sync_shadow_topics = array();
2030 7188 acydburn
                                if (sizeof($post_ids))
2031 7188 acydburn
                                {
2032 7188 acydburn
                                        $sql = 'SELECT p.post_id, p.topic_id, p.post_approved, p.poster_id, p.post_subject, p.post_username, p.post_time, u.username, u.user_colour
2033 7188 acydburn
                                                FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
2034 7188 acydburn
                                                WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
2035 7188 acydburn
                                                        AND u.user_id = p.poster_id';
2036 7188 acydburn
                                        $result = $db->sql_query($sql);
2037 7188 acydburn
2038 7188 acydburn
                                        $post_ids = array();
2039 7188 acydburn
                                        while ($row = $db->sql_fetchrow($result))
2040 7188 acydburn
                                        {
2041 7188 acydburn
                                                $topic_id = (int) $row['topic_id'];
2042 7188 acydburn
2043 8146 acydburn
                                                // Ok, there should be a shadow topic. If there isn't, then there's something wrong with the db.
2044 7648 kellanved
                                                // However, there's not much we can do about it.
2045 7648 kellanved
                                                if (!empty($shadow_topic_data[$topic_id]))
2046 7188 acydburn
                                                {
2047 7648 kellanved
                                                        if ($row['post_id'] == $shadow_topic_data[$topic_id]['topic_first_post_id'])
2048 7648 kellanved
                                                        {
2049 7648 kellanved
                                                                $orig_topic_id = $shadow_topic_data[$topic_id]['topic_id'];
2050 7188 acydburn
2051 7648 kellanved
                                                                if (!isset($sync_shadow_topics[$orig_topic_id]))
2052 7648 kellanved
                                                                {
2053 7648 kellanved
                                                                        $sync_shadow_topics[$orig_topic_id] = array();
2054 7648 kellanved
                                                                }
2055 7648 kellanved
2056 7648 kellanved
                                                                $sync_shadow_topics[$orig_topic_id]['topic_time'] = $row['post_time'];
2057 7648 kellanved
                                                                $sync_shadow_topics[$orig_topic_id]['topic_poster'] = $row['poster_id'];
2058 7648 kellanved
                                                                $sync_shadow_topics[$orig_topic_id]['topic_first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
2059 7648 kellanved
                                                                $sync_shadow_topics[$orig_topic_id]['topic_first_poster_colour'] = $row['user_colour'];
2060 7188 acydburn
                                                        }
2061 7188 acydburn
2062 7648 kellanved
                                                        if ($row['post_id'] == $shadow_topic_data[$topic_id]['topic_last_post_id'])
2063 7648 kellanved
                                                        {
2064 7648 kellanved
                                                                $orig_topic_id = $shadow_topic_data[$topic_id]['topic_id'];
2065 7188 acydburn
2066 7648 kellanved
                                                                if (!isset($sync_shadow_topics[$orig_topic_id]))
2067 7648 kellanved
                                                                {
2068 7648 kellanved
                                                                        $sync_shadow_topics[$orig_topic_id] = array();
2069 7648 kellanved
                                                                }
2070 7188 acydburn
2071 7648 kellanved
                                                                $sync_shadow_topics[$orig_topic_id]['topic_last_poster_id'] = $row['poster_id'];
2072 7648 kellanved
                                                                $sync_shadow_topics[$orig_topic_id]['topic_last_post_subject'] = $row['post_subject'];
2073 7648 kellanved
                                                                $sync_shadow_topics[$orig_topic_id]['topic_last_post_time'] = $row['post_time'];
2074 7648 kellanved
                                                                $sync_shadow_topics[$orig_topic_id]['topic_last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
2075 7648 kellanved
                                                                $sync_shadow_topics[$orig_topic_id]['topic_last_poster_colour'] = $row['user_colour'];
2076 7188 acydburn
                                                        }
2077 7188 acydburn
                                                }
2078 7188 acydburn
                                        }
2079 7188 acydburn
                                        $db->sql_freeresult($result);
2080 7188 acydburn
2081 7188 acydburn
                                        $shadow_topic_data = array();
2082 7188 acydburn
2083 7188 acydburn
                                        // Update the information we collected
2084 7188 acydburn
                                        if (sizeof($sync_shadow_topics))
2085 7188 acydburn
                                        {
2086 7188 acydburn
                                                foreach ($sync_shadow_topics as $sync_topic_id => $sql_ary)
2087 7188 acydburn
                                                {
2088 7188 acydburn
                                                        $sql = 'UPDATE ' . TOPICS_TABLE . '
2089 7188 acydburn
                                                                SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
2090 7188 acydburn
                                                                WHERE topic_id = ' . $sync_topic_id;
2091 7188 acydburn
                                                        $db->sql_query($sql);
2092 7188 acydburn
                                                }
2093 7188 acydburn
                                        }
2094 7188 acydburn
                                }
2095 7188 acydburn
2096 7188 acydburn
                                unset($sync_shadow_topics, $shadow_topic_data);
2097 7188 acydburn
                        }
2098 7188 acydburn
2099 3760 ludovic_arnaud
                        // approved becomes unapproved, and vice-versa
2100 5033 acydburn
                        if (sizeof($approved_unapproved_ids))
2101 3737 ludovic_arnaud
                        {
2102 3737 ludovic_arnaud
                                $sql = 'UPDATE ' . TOPICS_TABLE . '
2103 3760 ludovic_arnaud
                                        SET topic_approved = 1 - topic_approved
2104 6271 acydburn
                                        WHERE ' . $db->sql_in_set('topic_id', $approved_unapproved_ids);
2105 3737 ludovic_arnaud
                                $db->sql_query($sql);
2106 3737 ludovic_arnaud
                        }
2107 4507 ludovic_arnaud
                        unset($approved_unapproved_ids);
2108 3760 ludovic_arnaud
2109 5026 bartvb
                        // These are fields that will be synchronised
2110 6360 grahamje
                        $fieldnames = array('time', 'replies', 'replies_real', 'poster', 'first_post_id', 'first_poster_name', 'first_poster_colour', 'last_post_id', 'last_post_subject', 'last_post_time', 'last_poster_id', 'last_poster_name', 'last_poster_colour');
2111 3760 ludovic_arnaud
2112 3760 ludovic_arnaud
                        if ($sync_extra)
2113 3737 ludovic_arnaud
                        {
2114 3760 ludovic_arnaud
                                // This routine assumes that post_reported values are correct
2115 4671 ludovic_arnaud
                                // if they are not, use sync('post_reported') first
2116 3760 ludovic_arnaud
                                $sql = 'SELECT t.topic_id, p.post_id
2117 3760 ludovic_arnaud
                                        FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
2118 3760 ludovic_arnaud
                                        $where_sql_and p.topic_id = t.topic_id
2119 3760 ludovic_arnaud
                                                AND p.post_reported = 1
2120 5933 acydburn
                                        GROUP BY t.topic_id, p.post_id";
2121 3961 psotfx
                                $result = $db->sql_query($sql);
2122 3760 ludovic_arnaud
2123 3961 psotfx
                                $fieldnames[] = 'reported';
2124 3760 ludovic_arnaud
                                while ($row = $db->sql_fetchrow($result))
2125 3760 ludovic_arnaud
                                {
2126 4534 ludovic_arnaud
                                        $topic_data[intval($row['topic_id'])]['reported'] = 1;
2127 3760 ludovic_arnaud
                                }
2128 3961 psotfx
                                $db->sql_freeresult($result);
2129 3760 ludovic_arnaud
2130 3936 ludovic_arnaud
                                // This routine assumes that post_attachment values are correct
2131 4671 ludovic_arnaud
                                // if they are not, use sync('post_attachment') first
2132 3936 ludovic_arnaud
                                $sql = 'SELECT t.topic_id, p.post_id
2133 3936 ludovic_arnaud
                                        FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
2134 3936 ludovic_arnaud
                                        $where_sql_and p.topic_id = t.topic_id
2135 3936 ludovic_arnaud
                                                AND p.post_attachment = 1
2136 5933 acydburn
                                        GROUP BY t.topic_id, p.post_id";
2137 3961 psotfx
                                $result = $db->sql_query($sql);
2138 3936 ludovic_arnaud
2139 3961 psotfx
                                $fieldnames[] = 'attachment';
2140 3936 ludovic_arnaud
                                while ($row = $db->sql_fetchrow($result))
2141 3936 ludovic_arnaud
                                {
2142 4534 ludovic_arnaud
                                        $topic_data[intval($row['topic_id'])]['attachment'] = 1;
2143 3936 ludovic_arnaud
                                }
2144 3961 psotfx
                                $db->sql_freeresult($result);
2145 3737 ludovic_arnaud
                        }
2146 2664 psotfx
2147 3737 ludovic_arnaud
                        foreach ($topic_data as $topic_id => $row)
2148 3737 ludovic_arnaud
                        {
2149 6015 acydburn
                                $sql_ary = array();
2150 2664 psotfx
2151 3737 ludovic_arnaud
                                foreach ($fieldnames as $fieldname)
2152 3737 ludovic_arnaud
                                {
2153 6345 acydburn
                                        if (isset($row[$fieldname]) && isset($row['topic_' . $fieldname]) && $row['topic_' . $fieldname] != $row[$fieldname])
2154 3737 ludovic_arnaud
                                        {
2155 6015 acydburn
                                                $sql_ary['topic_' . $fieldname] = $row[$fieldname];
2156 3737 ludovic_arnaud
                                        }
2157 4534 ludovic_arnaud
                                }
2158 2664 psotfx
2159 6015 acydburn
                                if (sizeof($sql_ary))
2160 4534 ludovic_arnaud
                                {
2161 3737 ludovic_arnaud
                                        $sql = 'UPDATE ' . TOPICS_TABLE . '
2162 6015 acydburn
                                                SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
2163 3961 psotfx
                                                WHERE topic_id = ' . $topic_id;
2164 3737 ludovic_arnaud
                                        $db->sql_query($sql);
2165 2664 psotfx
2166 4534 ludovic_arnaud
                                        $resync_forums[$row['forum_id']] = $row['forum_id'];
2167 3737 ludovic_arnaud
                                }
2168 3737 ludovic_arnaud
                        }
2169 3760 ludovic_arnaud
                        unset($topic_data);
2170 2664 psotfx
2171 9937 Kellanved
                        $db->sql_transaction('commit');
2172 9965 acydburn
2173 3737 ludovic_arnaud
                        // if some topics have been resync'ed then resync parent forums
2174 5313 acydburn
                        // except when we're only syncing a range, we don't want to sync forums during
2175 5313 acydburn
                        // batch processing.
2176 5076 bartvb
                        if ($resync_parents && sizeof($resync_forums) && $where_type != 'range')
2177 3737 ludovic_arnaud
                        {
2178 7466 davidmj
                                sync('forum', 'forum_id', array_values($resync_forums), true, true);
2179 3737 ludovic_arnaud
                        }
2180 9937 Kellanved
                        break;
2181 3737 ludovic_arnaud
        }
2182 6015 acydburn
2183 6015 acydburn
        return;
2184 3737 ludovic_arnaud
}
2185 2664 psotfx
2186 5114 acydburn
/**
2187 5114 acydburn
* Prune function
2188 5114 acydburn
*/
2189 4482 acydburn
function prune($forum_id, $prune_mode, $prune_date, $prune_flags = 0, $auto_sync = true)
2190 2664 psotfx
{
2191 3737 ludovic_arnaud
        global $db;
2192 2664 psotfx
2193 6271 acydburn
        if (!is_array($forum_id))
2194 6271 acydburn
        {
2195 6271 acydburn
                $forum_id = array($forum_id);
2196 6271 acydburn
        }
2197 3992 psotfx
2198 6271 acydburn
        if (!sizeof($forum_id))
2199 6271 acydburn
        {
2200 6271 acydburn
                return;
2201 6271 acydburn
        }
2202 6271 acydburn
2203 3989 psotfx
        $sql_and = '';
2204 6271 acydburn
2205 6364 acydburn
        if (!($prune_flags & FORUM_FLAG_PRUNE_ANNOUNCE))
2206 3989 psotfx
        {
2207 3989 psotfx
                $sql_and .= ' AND topic_type <> ' . POST_ANNOUNCE;
2208 11100 git-gate
                $sql_and .= ' AND topic_type <> ' . POST_GLOBAL;
2209 3989 psotfx
        }
2210 5325 acydburn
2211 6364 acydburn
        if (!($prune_flags & FORUM_FLAG_PRUNE_STICKY))
2212 3989 psotfx
        {
2213 3989 psotfx
                $sql_and .= ' AND topic_type <> ' . POST_STICKY;
2214 3989 psotfx
        }
2215 3989 psotfx
2216 4482 acydburn
        if ($prune_mode == 'posted')
2217 4482 acydburn
        {
2218 4482 acydburn
                $sql_and .= " AND topic_last_post_time < $prune_date";
2219 4482 acydburn
        }
2220 5325 acydburn
2221 4482 acydburn
        if ($prune_mode == 'viewed')
2222 4482 acydburn
        {
2223 4482 acydburn
                $sql_and .= " AND topic_last_view_time < $prune_date";
2224 4482 acydburn
        }
2225 4482 acydburn
2226 3737 ludovic_arnaud
        $sql = 'SELECT topic_id
2227 6271 acydburn
                FROM ' . TOPICS_TABLE . '
2228 6271 acydburn
                WHERE ' . $db->sql_in_set('forum_id', $forum_id) . "
2229 8146 acydburn
                        AND poll_start = 0
2230 3989 psotfx
                        $sql_and";
2231 2664 psotfx
        $result = $db->sql_query($sql);
2232 2664 psotfx
2233 3737 ludovic_arnaud
        $topic_list = array();
2234 3737 ludovic_arnaud
        while ($row = $db->sql_fetchrow($result))
2235 2664 psotfx
        {
2236 3737 ludovic_arnaud
                $topic_list[] = $row['topic_id'];
2237 3737 ludovic_arnaud
        }
2238 3737 ludovic_arnaud
        $db->sql_freeresult($result);
2239 2664 psotfx
2240 6364 acydburn
        if ($prune_flags & FORUM_FLAG_PRUNE_POLL)
2241 3989 psotfx
        {
2242 3989 psotfx
                $sql = 'SELECT topic_id
2243 6271 acydburn
                        FROM ' . TOPICS_TABLE . '
2244 6271 acydburn
                        WHERE ' . $db->sql_in_set('forum_id', $forum_id) . "
2245 8146 acydburn
                                AND poll_start > 0
2246 8146 acydburn
                                AND poll_last_vote < $prune_date
2247 3989 psotfx
                                $sql_and";
2248 3989 psotfx
                $result = $db->sql_query($sql);
2249 3989 psotfx
2250 3989 psotfx
                while ($row = $db->sql_fetchrow($result))
2251 3989 psotfx
                {
2252 3989 psotfx
                        $topic_list[] = $row['topic_id'];
2253 3989 psotfx
                }
2254 3989 psotfx
                $db->sql_freeresult($result);
2255 3989 psotfx
2256 3989 psotfx
                $topic_list = array_unique($topic_list);
2257 3989 psotfx
        }
2258 3989 psotfx
2259 6974 naderman
        return delete_topics('topic_id', $topic_list, $auto_sync, false);
2260 3737 ludovic_arnaud
}
2261 2664 psotfx
2262 5114 acydburn
/**
2263 5114 acydburn
* Function auto_prune(), this function now relies on passed vars
2264 5114 acydburn
*/
2265 4482 acydburn
function auto_prune($forum_id, $prune_mode, $prune_flags, $prune_days, $prune_freq)
2266 3737 ludovic_arnaud
{
2267 3989 psotfx
        global $db;
2268 3989 psotfx
2269 3992 psotfx
        $sql = 'SELECT forum_name
2270 3992 psotfx
                FROM ' . FORUMS_TABLE . "
2271 3992 psotfx
                WHERE forum_id = $forum_id";
2272 6009 naderman
        $result = $db->sql_query($sql, 3600);
2273 6015 acydburn
        $row = $db->sql_fetchrow($result);
2274 6015 acydburn
        $db->sql_freeresult($result);
2275 2664 psotfx
2276 6015 acydburn
        if ($row)
2277 3992 psotfx
        {
2278 3992 psotfx
                $prune_date = time() - ($prune_days * 86400);
2279 3992 psotfx
                $next_prune = time() + ($prune_freq * 86400);
2280 3737 ludovic_arnaud
2281 4482 acydburn
                prune($forum_id, $prune_mode, $prune_date, $prune_flags, true);
2282 3737 ludovic_arnaud
2283 3992 psotfx
                $sql = 'UPDATE ' . FORUMS_TABLE . "
2284 3992 psotfx
                        SET prune_next = $next_prune
2285 3992 psotfx
                        WHERE forum_id = $forum_id";
2286 3992 psotfx
                $db->sql_query($sql);
2287 3992 psotfx
2288 3992 psotfx
                add_log('admin', 'LOG_AUTO_PRUNE', $row['forum_name']);
2289 3992 psotfx
        }
2290 3992 psotfx
2291 2664 psotfx
        return;
2292 2664 psotfx
}
2293 2664 psotfx
2294 5114 acydburn
/**
2295 5114 acydburn
* Cache moderators, called whenever permissions are changed via admin_permissions. Changes of username
2296 5114 acydburn
* and group names must be carried through for the moderators table
2297 5114 acydburn
*/
2298 3064 psotfx
function cache_moderators()
2299 3061 psotfx
{
2300 5603 acydburn
        global $db, $cache, $auth, $phpbb_root_path, $phpEx;
2301 3061 psotfx
2302 6019 acydburn
        // Remove cached sql results
2303 6021 acydburn
        $cache->destroy('sql', MODERATOR_CACHE_TABLE);
2304 6019 acydburn
2305 3061 psotfx
        // Clear table
2306 6954 davidmj
        switch ($db->sql_layer)
2307 6954 davidmj
        {
2308 6954 davidmj
                case 'sqlite':
2309 6954 davidmj
                case 'firebird':
2310 6954 davidmj
                        $db->sql_query('DELETE FROM ' . MODERATOR_CACHE_TABLE);
2311 6954 davidmj
                break;
2312 3061 psotfx
2313 6954 davidmj
                default:
2314 6954 davidmj
                        $db->sql_query('TRUNCATE TABLE ' . MODERATOR_CACHE_TABLE);
2315 6954 davidmj
                break;
2316 6954 davidmj
        }
2317 6954 davidmj
2318 6115 acydburn
        // We add moderators who have forum moderator permissions without an explicit ACL_NEVER setting
2319 5603 acydburn
        $hold_ary = $ug_id_ary = $sql_ary = array();
2320 3061 psotfx
2321 5603 acydburn
        // Grab all users having moderative options...
2322 5603 acydburn
        $hold_ary = $auth->acl_user_raw_data(false, 'm_%', false);
2323 3061 psotfx
2324 5603 acydburn
        // Add users?
2325 5603 acydburn
        if (sizeof($hold_ary))
2326 3061 psotfx
        {
2327 5603 acydburn
                // At least one moderative option warrants a display
2328 5603 acydburn
                $ug_id_ary = array_keys($hold_ary);
2329 3061 psotfx
2330 5603 acydburn
                // Remove users who have group memberships with DENY moderator permissions
2331 11677 git-gate
                $sql_ary_deny = array(
2332 9625 acydburn
                        'SELECT'        => 'a.forum_id, ug.user_id, g.group_id',
2333 5885 davidmj
2334 5885 davidmj
                        'FROM'                => array(
2335 5885 davidmj
                                ACL_OPTIONS_TABLE        => 'o',
2336 5885 davidmj
                                USER_GROUP_TABLE        => 'ug',
2337 10342 acydburn
                                GROUPS_TABLE                => 'g',
2338 9625 acydburn
                                ACL_GROUPS_TABLE        => 'a',
2339 5885 davidmj
                        ),
2340 5885 davidmj
2341 5885 davidmj
                        'LEFT_JOIN'        => array(
2342 5885 davidmj
                                array(
2343 5885 davidmj
                                        'FROM'        => array(ACL_ROLES_DATA_TABLE => 'r'),
2344 11585 git-gate
                                        'ON'        => 'a.auth_role_id = r.role_id',
2345 11585 git-gate
                                ),
2346 5885 davidmj
                        ),
2347 5885 davidmj
2348 5885 davidmj
                        'WHERE'                => '(o.auth_option_id = a.auth_option_id OR o.auth_option_id = r.auth_option_id)
2349 6115 acydburn
                                AND ((a.auth_setting = ' . ACL_NEVER . ' AND r.auth_setting IS NULL)
2350 6115 acydburn
                                        OR r.auth_setting = ' . ACL_NEVER . ')
2351 6015 acydburn
                                AND a.group_id = ug.group_id
2352 9651 acydburn
                                AND g.group_id = ug.group_id
2353 9625 acydburn
                                AND NOT (ug.group_leader = 1 AND g.group_skip_auth = 1)
2354 6271 acydburn
                                AND ' . $db->sql_in_set('ug.user_id', $ug_id_ary) . "
2355 6081 acydburn
                                AND ug.user_pending = 0
2356 7789 acydburn
                                AND o.auth_option " . $db->sql_like_expression('m_' . $db->any_char),
2357 11585 git-gate
                );
2358 11677 git-gate
                $sql = $db->sql_build_query('SELECT', $sql_ary_deny);
2359 3061 psotfx
                $result = $db->sql_query($sql);
2360 3061 psotfx
2361 3061 psotfx
                while ($row = $db->sql_fetchrow($result))
2362 3061 psotfx
                {
2363 5603 acydburn
                        if (isset($hold_ary[$row['user_id']][$row['forum_id']]))
2364 5603 acydburn
                        {
2365 5603 acydburn
                                unset($hold_ary[$row['user_id']][$row['forum_id']]);
2366 5603 acydburn
                        }
2367 3061 psotfx
                }
2368 3061 psotfx
                $db->sql_freeresult($result);
2369 5603 acydburn
2370 5603 acydburn
                if (sizeof($hold_ary))
2371 5603 acydburn
                {
2372 5603 acydburn
                        // Get usernames...
2373 5603 acydburn
                        $sql = 'SELECT user_id, username
2374 5603 acydburn
                                FROM ' . USERS_TABLE . '
2375 6271 acydburn
                                WHERE ' . $db->sql_in_set('user_id', array_keys($hold_ary));
2376 5603 acydburn
                        $result = $db->sql_query($sql);
2377 5603 acydburn
2378 5603 acydburn
                        $usernames_ary = array();
2379 5603 acydburn
                        while ($row = $db->sql_fetchrow($result))
2380 5603 acydburn
                        {
2381 5603 acydburn
                                $usernames_ary[$row['user_id']] = $row['username'];
2382 5603 acydburn
                        }
2383 5603 acydburn
2384 5603 acydburn
                        foreach ($hold_ary as $user_id => $forum_id_ary)
2385 5603 acydburn
                        {
2386 6511 acydburn
                                // Do not continue if user does not exist
2387 6511 acydburn
                                if (!isset($usernames_ary[$user_id]))
2388 6511 acydburn
                                {
2389 6511 acydburn
                                        continue;
2390 6511 acydburn
                                }
2391 6511 acydburn
2392 5603 acydburn
                                foreach ($forum_id_ary as $forum_id => $auth_ary)
2393 5603 acydburn
                                {
2394 5603 acydburn
                                        $sql_ary[] = array(
2395 7961 acydburn
                                                'forum_id'                => (int) $forum_id,
2396 7961 acydburn
                                                'user_id'                => (int) $user_id,
2397 7961 acydburn
                                                'username'                => (string) $usernames_ary[$user_id],
2398 5603 acydburn
                                                'group_id'                => 0,
2399 5784 acydburn
                                                'group_name'        => ''
2400 5603 acydburn
                                        );
2401 5603 acydburn
                                }
2402 5603 acydburn
                        }
2403 5603 acydburn
                }
2404 3061 psotfx
        }
2405 3061 psotfx
2406 5603 acydburn
        // Now to the groups...
2407 5603 acydburn
        $hold_ary = $auth->acl_group_raw_data(false, 'm_%', false);
2408 3061 psotfx
2409 5603 acydburn
        if (sizeof($hold_ary))
2410 3061 psotfx
        {
2411 5603 acydburn
                $ug_id_ary = array_keys($hold_ary);
2412 5603 acydburn
2413 5603 acydburn
                // Make sure not hidden or special groups are involved...
2414 5603 acydburn
                $sql = 'SELECT group_name, group_id, group_type
2415 6930 acydburn
                        FROM ' . GROUPS_TABLE . '
2416 6271 acydburn
                        WHERE ' . $db->sql_in_set('group_id', $ug_id_ary);
2417 5603 acydburn
                $result = $db->sql_query($sql);
2418 5603 acydburn
2419 5603 acydburn
                $groupnames_ary = array();
2420 5603 acydburn
                while ($row = $db->sql_fetchrow($result))
2421 5603 acydburn
                {
2422 5603 acydburn
                        if ($row['group_type'] == GROUP_HIDDEN || $row['group_type'] == GROUP_SPECIAL)
2423 5603 acydburn
                        {
2424 5603 acydburn
                                unset($hold_ary[$row['group_id']]);
2425 5603 acydburn
                        }
2426 5603 acydburn
2427 5603 acydburn
                        $groupnames_ary[$row['group_id']] = $row['group_name'];
2428 5603 acydburn
                }
2429 5603 acydburn
                $db->sql_freeresult($result);
2430 5603 acydburn
2431 5603 acydburn
                foreach ($hold_ary as $group_id => $forum_id_ary)
2432 5603 acydburn
                {
2433 6511 acydburn
                        // If there is no group, we do not assign it...
2434 6511 acydburn
                        if (!isset($groupnames_ary[$group_id]))
2435 6511 acydburn
                        {
2436 6511 acydburn
                                continue;
2437 6511 acydburn
                        }
2438 6511 acydburn
2439 5603 acydburn
                        foreach ($forum_id_ary as $forum_id => $auth_ary)
2440 5603 acydburn
                        {
2441 5870 acydburn
                                $flag = false;
2442 5870 acydburn
                                foreach ($auth_ary as $auth_option => $setting)
2443 5870 acydburn
                                {
2444 5870 acydburn
                                        // Make sure at least one ACL_YES option is set...
2445 5870 acydburn
                                        if ($setting == ACL_YES)
2446 5870 acydburn
                                        {
2447 5870 acydburn
                                                $flag = true;
2448 5870 acydburn
                                                break;
2449 5870 acydburn
                                        }
2450 5870 acydburn
                                }
2451 5870 acydburn
2452 5870 acydburn
                                if (!$flag)
2453 5870 acydburn
                                {
2454 5870 acydburn
                                        continue;
2455 5870 acydburn
                                }
2456 5870 acydburn
2457 5603 acydburn
                                $sql_ary[] = array(
2458 7961 acydburn
                                        'forum_id'                => (int) $forum_id,
2459 5603 acydburn
                                        'user_id'                => 0,
2460 5603 acydburn
                                        'username'                => '',
2461 7961 acydburn
                                        'group_id'                => (int) $group_id,
2462 7961 acydburn
                                        'group_name'        => (string) $groupnames_ary[$group_id]
2463 5603 acydburn
                                );
2464 5603 acydburn
                        }
2465 5603 acydburn
                }
2466 3061 psotfx
        }
2467 3061 psotfx
2468 6497 acydburn
        $db->sql_multi_insert(MODERATOR_CACHE_TABLE, $sql_ary);
2469 3061 psotfx
}
2470 3061 psotfx
2471 5114 acydburn
/**
2472 5114 acydburn
* View log
2473 10990 git-gate
* If $log_count is set to false, we will skip counting all entries in the database.
2474 5114 acydburn
*/
2475 10041 terrafrost
function view_log($mode, &$log, &$log_count, $limit = 0, $offset = 0, $forum_id = 0, $topic_id = 0, $user_id = 0, $limit_days = 0, $sort_by = 'l.log_time DESC', $keywords = '')
2476 3399 psotfx
{
2477 6015 acydburn
        global $db, $user, $auth, $phpEx, $phpbb_root_path, $phpbb_admin_path;
2478 3833 psotfx
2479 5848 acydburn
        $topic_id_list = $reportee_id_list = $is_auth = $is_mod = array();
2480 3833 psotfx
2481 6015 acydburn
        $profile_url = (defined('IN_ADMIN')) ? append_sid("{$phpbb_admin_path}index.$phpEx", 'i=users&amp;mode=overview') : append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile');
2482 3399 psotfx
2483 4210 acydburn
        switch ($mode)
2484 3781 ludovic_arnaud
        {
2485 4210 acydburn
                case 'admin':
2486 4210 acydburn
                        $log_type = LOG_ADMIN;
2487 4210 acydburn
                        $sql_forum = '';
2488 5848 acydburn
                break;
2489 6015 acydburn
2490 4210 acydburn
                case 'mod':
2491 4210 acydburn
                        $log_type = LOG_MOD;
2492 10146 acydburn
                        $sql_forum = '';
2493 3399 psotfx
2494 4210 acydburn
                        if ($topic_id)
2495 4210 acydburn
                        {
2496 10112 bantu
                                $sql_forum = 'AND l.topic_id = ' . (int) $topic_id;
2497 4210 acydburn
                        }
2498 4210 acydburn
                        else if (is_array($forum_id))
2499 4210 acydburn
                        {
2500 6271 acydburn
                                $sql_forum = 'AND ' . $db->sql_in_set('l.forum_id', array_map('intval', $forum_id));
2501 4210 acydburn
                        }
2502 10112 bantu
                        else if ($forum_id)
2503 4210 acydburn
                        {
2504 10112 bantu
                                $sql_forum = 'AND l.forum_id = ' . (int) $forum_id;
2505 4210 acydburn
                        }
2506 5848 acydburn
                break;
2507 4824 psotfx
2508 4824 psotfx
                case 'user':
2509 4824 psotfx
                        $log_type = LOG_USERS;
2510 6650 acydburn
                        $sql_forum = 'AND l.reportee_id = ' . (int) $user_id;
2511 5848 acydburn
                break;
2512 8782 acydburn
2513 5848 acydburn
                case 'users':
2514 5848 acydburn
                        $log_type = LOG_USERS;
2515 5848 acydburn
                        $sql_forum = '';
2516 5848 acydburn
                break;
2517 5848 acydburn
2518 4210 acydburn
                case 'critical':
2519 4210 acydburn
                        $log_type = LOG_CRITICAL;
2520 4210 acydburn
                        $sql_forum = '';
2521 5848 acydburn
                break;
2522 8782 acydburn
2523 4210 acydburn
                default:
2524 4210 acydburn
                        return;
2525 3781 ludovic_arnaud
        }
2526 3781 ludovic_arnaud
2527 10342 acydburn
        // Use no preg_quote for $keywords because this would lead to sole backslashes being added
2528 10342 acydburn
        // We also use an OR connection here for spaces and the | string. Currently, regex is not supported for searching (but may come later).
2529 10342 acydburn
        $keywords = preg_split('#[\s|]+#u', utf8_strtolower($keywords), 0, PREG_SPLIT_NO_EMPTY);
2530 10041 terrafrost
        $sql_keywords = '';
2531 10041 terrafrost
2532 10041 terrafrost
        if (!empty($keywords))
2533 10041 terrafrost
        {
2534 10342 acydburn
                $keywords_pattern = array();
2535 10342 acydburn
2536 10342 acydburn
                // Build pattern and keywords...
2537 10041 terrafrost
                for ($i = 0, $num_keywords = sizeof($keywords); $i < $num_keywords; $i++)
2538 10041 terrafrost
                {
2539 10342 acydburn
                        $keywords_pattern[] = preg_quote($keywords[$i], '#');
2540 10041 terrafrost
                        $keywords[$i] = $db->sql_like_expression($db->any_char . $keywords[$i] . $db->any_char);
2541 10041 terrafrost
                }
2542 10041 terrafrost
2543 10342 acydburn
                $keywords_pattern = '#' . implode('|', $keywords_pattern) . '#ui';
2544 10342 acydburn
2545 10041 terrafrost
                $operations = array();
2546 10112 bantu
                foreach ($user->lang as $key => $value)
2547 10041 terrafrost
                {
2548 10041 terrafrost
                        if (substr($key, 0, 4) == 'LOG_' && preg_match($keywords_pattern, $value))
2549 10041 terrafrost
                        {
2550 10041 terrafrost
                                $operations[] = $key;
2551 10041 terrafrost
                        }
2552 10041 terrafrost
                }
2553 10041 terrafrost
2554 10041 terrafrost
                $sql_keywords = 'AND (';
2555 10041 terrafrost
                if (!empty($operations))
2556 10041 terrafrost
                {
2557 10112 bantu
                        $sql_keywords .= $db->sql_in_set('l.log_operation', $operations) . ' OR ';
2558 10041 terrafrost
                }
2559 10112 bantu
                $sql_keywords .= 'LOWER(l.log_data) ' . implode(' OR LOWER(l.log_data) ', $keywords) . ')';
2560 10041 terrafrost
        }
2561 10041 terrafrost
2562 11302 git-gate
        if ($log_count !== false)
2563 11302 git-gate
        {
2564 11302 git-gate
                $sql = 'SELECT COUNT(l.log_id) AS total_entries
2565 11302 git-gate
                        FROM ' . LOG_TABLE . ' l, ' . USERS_TABLE . " u
2566 11302 git-gate
                        WHERE l.log_type = $log_type
2567 11302 git-gate
                                AND l.user_id = u.user_id
2568 11302 git-gate
                                AND l.log_time >= $limit_days
2569 11302 git-gate
                                $sql_keywords
2570 11302 git-gate
                                $sql_forum";
2571 11302 git-gate
                $result = $db->sql_query($sql);
2572 11302 git-gate
                $log_count = (int) $db->sql_fetchfield('total_entries');
2573 11302 git-gate
                $db->sql_freeresult($result);
2574 11302 git-gate
        }
2575 11302 git-gate
2576 11577 git-gate
        // $log_count may be false here if false was passed in for it,
2577 11577 git-gate
        // because in this case we did not run the COUNT() query above.
2578 11577 git-gate
        // If we ran the COUNT() query and it returned zero rows, return;
2579 11577 git-gate
        // otherwise query for logs below.
2580 11577 git-gate
        if ($log_count === 0)
2581 11302 git-gate
        {
2582 11302 git-gate
                // Save the queries, because there are no logs to display
2583 11302 git-gate
                return 0;
2584 11302 git-gate
        }
2585 11302 git-gate
2586 11302 git-gate
        if ($offset >= $log_count)
2587 11302 git-gate
        {
2588 11302 git-gate
                $offset = ($offset - $limit < 0) ? 0 : $offset - $limit;
2589 11302 git-gate
        }
2590 11302 git-gate
2591 6698 acydburn
        $sql = "SELECT l.*, u.username, u.username_clean, u.user_colour
2592 4210 acydburn
                FROM " . LOG_TABLE . " l, " . USERS_TABLE . " u
2593 4210 acydburn
                WHERE l.log_type = $log_type
2594 4210 acydburn
                        AND u.user_id = l.user_id
2595 10041 terrafrost
                        " . (($limit_days) ? "AND l.log_time >= $limit_days" : '') . "
2596 10041 terrafrost
                        $sql_keywords
2597 3833 psotfx
                        $sql_forum
2598 3781 ludovic_arnaud
                ORDER BY $sort_by";
2599 3781 ludovic_arnaud
        $result = $db->sql_query_limit($sql, $limit, $offset);
2600 3399 psotfx
2601 3781 ludovic_arnaud
        $i = 0;
2602 3399 psotfx
        $log = array();
2603 3781 ludovic_arnaud
        while ($row = $db->sql_fetchrow($result))
2604 3399 psotfx
        {
2605 3781 ludovic_arnaud
                if ($row['topic_id'])
2606 3399 psotfx
                {
2607 3781 ludovic_arnaud
                        $topic_id_list[] = $row['topic_id'];
2608 3781 ludovic_arnaud
                }
2609 3781 ludovic_arnaud
2610 5848 acydburn
                if ($row['reportee_id'])
2611 5848 acydburn
                {
2612 5848 acydburn
                        $reportee_id_list[] = $row['reportee_id'];
2613 5848 acydburn
                }
2614 3781 ludovic_arnaud
2615 5848 acydburn
                $log[$i] = array(
2616 6015 acydburn
                        'id'                                => $row['log_id'],
2617 6650 acydburn
2618 6650 acydburn
                        'reportee_id'                        => $row['reportee_id'],
2619 6650 acydburn
                        'reportee_username'                => '',
2620 6650 acydburn
                        'reportee_username_full'=> '',
2621 6650 acydburn
2622 5848 acydburn
                        'user_id'                        => $row['user_id'],
2623 6650 acydburn
                        'username'                        => $row['username'],
2624 6650 acydburn
                        'username_full'                => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], false, $profile_url),
2625 6650 acydburn
2626 5848 acydburn
                        'ip'                                => $row['log_ip'],
2627 5848 acydburn
                        'time'                                => $row['log_time'],
2628 5848 acydburn
                        'forum_id'                        => $row['forum_id'],
2629 5848 acydburn
                        'topic_id'                        => $row['topic_id'],
2630 6015 acydburn
2631 6055 acydburn
                        'viewforum'                        => ($row['forum_id'] && $auth->acl_get('f_read', $row['forum_id'])) ? append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']) : false,
2632 5848 acydburn
                        'action'                        => (isset($user->lang[$row['log_operation']])) ? $user->lang[$row['log_operation']] : '{' . ucfirst(str_replace('_', ' ', $row['log_operation'])) . '}',
2633 5848 acydburn
                );
2634 5848 acydburn
2635 3781 ludovic_arnaud
                if (!empty($row['log_data']))
2636 3781 ludovic_arnaud
                {
2637 9589 toonarmy
                        $log_data_ary = @unserialize($row['log_data']);
2638 9589 toonarmy
                        $log_data_ary = ($log_data_ary === false) ? array() : $log_data_ary;
2639 3781 ludovic_arnaud
2640 5114 acydburn
                        if (isset($user->lang[$row['log_operation']]))
2641 3781 ludovic_arnaud
                        {
2642 8908 toonarmy
                                // Check if there are more occurrences of % than arguments, if there are we fill out the arguments array
2643 8908 toonarmy
                                // It doesn't matter if we add more arguments than placeholders
2644 9065 toonarmy
                                if ((substr_count($log[$i]['action'], '%') - sizeof($log_data_ary)) > 0)
2645 8908 toonarmy
                                {
2646 8908 toonarmy
                                        $log_data_ary = array_merge($log_data_ary, array_fill(0, substr_count($log[$i]['action'], '%') - sizeof($log_data_ary), ''));
2647 8908 toonarmy
                                }
2648 7266 acydburn
2649 8908 toonarmy
                                $log[$i]['action'] = vsprintf($log[$i]['action'], $log_data_ary);
2650 8908 toonarmy
2651 7266 acydburn
                                // If within the admin panel we do not censor text out
2652 7266 acydburn
                                if (defined('IN_ADMIN'))
2653 7266 acydburn
                                {
2654 8050 naderman
                                        $log[$i]['action'] = bbcode_nl2br($log[$i]['action']);
2655 7266 acydburn
                                }
2656 7266 acydburn
                                else
2657 7266 acydburn
                                {
2658 8050 naderman
                                        $log[$i]['action'] = bbcode_nl2br(censor_text($log[$i]['action']));
2659 7266 acydburn
                                }
2660 3781 ludovic_arnaud
                        }
2661 9589 toonarmy
                        else if (!empty($log_data_ary))
2662 4210 acydburn
                        {
2663 5114 acydburn
                                $log[$i]['action'] .= '<br />' . implode('', $log_data_ary);
2664 4210 acydburn
                        }
2665 7491 acydburn
2666 7589 acydburn
                        /* Apply make_clickable... has to be seen if it is for good. :/
2667 7589 acydburn
                        // Seems to be not for the moment, reconsider later...
2668 7491 acydburn
                        $log[$i]['action'] = make_clickable($log[$i]['action']);
2669 7589 acydburn
                        */
2670 3781 ludovic_arnaud
                }
2671 3399 psotfx
2672 3781 ludovic_arnaud
                $i++;
2673 3781 ludovic_arnaud
        }
2674 3781 ludovic_arnaud
        $db->sql_freeresult($result);
2675 3399 psotfx
2676 5033 acydburn
        if (sizeof($topic_id_list))
2677 3781 ludovic_arnaud
        {
2678 3800 ludovic_arnaud
                $topic_id_list = array_unique($topic_id_list);
2679 3800 ludovic_arnaud
2680 8146 acydburn
                // This query is not really needed if move_topics() updates the forum_id field,
2681 6015 acydburn
                // although it's also used to determine if the topic still exists in the database
2682 3781 ludovic_arnaud
                $sql = 'SELECT topic_id, forum_id
2683 3781 ludovic_arnaud
                        FROM ' . TOPICS_TABLE . '
2684 6271 acydburn
                        WHERE ' . $db->sql_in_set('topic_id', array_map('intval', $topic_id_list));
2685 3781 ludovic_arnaud
                $result = $db->sql_query($sql);
2686 3833 psotfx
2687 6015 acydburn
                $default_forum_id = 0;
2688 6015 acydburn
2689 3781 ludovic_arnaud
                while ($row = $db->sql_fetchrow($result))
2690 3781 ludovic_arnaud
                {
2691 11100 git-gate
                        if ($auth->acl_get('f_read', $row['forum_id']))
2692 3399 psotfx
                        {
2693 11100 git-gate
                                $is_auth[$row['topic_id']] = $row['forum_id'];
2694 3781 ludovic_arnaud
                        }
2695 3399 psotfx
2696 3833 psotfx
                        if ($auth->acl_gets('a_', 'm_', $row['forum_id']))
2697 3781 ludovic_arnaud
                        {
2698 3781 ludovic_arnaud
                                $is_mod[$row['topic_id']] = $row['forum_id'];
2699 3399 psotfx
                        }
2700 3781 ludovic_arnaud
                }
2701 6015 acydburn
                $db->sql_freeresult($result);
2702 3399 psotfx
2703 3781 ludovic_arnaud
                foreach ($log as $key => $row)
2704 3781 ludovic_arnaud
                {
2705 6055 acydburn
                        $log[$key]['viewtopic'] = (isset($is_auth[$row['topic_id']])) ? append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $is_auth[$row['topic_id']] . '&amp;t=' . $row['topic_id']) : false;
2706 6055 acydburn
                        $log[$key]['viewlogs'] = (isset($is_mod[$row['topic_id']])) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=logs&amp;mode=topic_logs&amp;t=' . $row['topic_id'], true, $user->session_id) : false;
2707 3399 psotfx
                }
2708 3399 psotfx
        }
2709 3399 psotfx
2710 6650 acydburn
        if (sizeof($reportee_id_list))
2711 5848 acydburn
        {
2712 5848 acydburn
                $reportee_id_list = array_unique($reportee_id_list);
2713 5848 acydburn
                $reportee_names_list = array();
2714 5848 acydburn
2715 6650 acydburn
                $sql = 'SELECT user_id, username, user_colour
2716 6650 acydburn
                        FROM ' . USERS_TABLE . '
2717 6650 acydburn
                        WHERE ' . $db->sql_in_set('user_id', $reportee_id_list);
2718 6650 acydburn
                $result = $db->sql_query($sql);
2719 6650 acydburn
2720 6650 acydburn
                while ($row = $db->sql_fetchrow($result))
2721 5848 acydburn
                {
2722 6650 acydburn
                        $reportee_names_list[$row['user_id']] = $row;
2723 5848 acydburn
                }
2724 6650 acydburn
                $db->sql_freeresult($result);
2725 5848 acydburn
2726 5848 acydburn
                foreach ($log as $key => $row)
2727 5848 acydburn
                {
2728 6650 acydburn
                        if (!isset($reportee_names_list[$row['reportee_id']]))
2729 6650 acydburn
                        {
2730 6650 acydburn
                                continue;
2731 6650 acydburn
                        }
2732 6650 acydburn
2733 6650 acydburn
                        $log[$key]['reportee_username'] = $reportee_names_list[$row['reportee_id']]['username'];
2734 6650 acydburn
                        $log[$key]['reportee_username_full'] = get_username_string('full', $row['reportee_id'], $reportee_names_list[$row['reportee_id']]['username'], $reportee_names_list[$row['reportee_id']]['user_colour'], false, $profile_url);
2735 5848 acydburn
                }
2736 5848 acydburn
        }
2737 5848 acydburn
2738 11302 git-gate
        return $offset;
2739 3399 psotfx
}
2740 3399 psotfx
2741 5329 grahamje
/**
2742 6366 acydburn
* Update foes - remove moderators and administrators from foe lists...
2743 6366 acydburn
*/
2744 7109 davidmj
function update_foes($group_id = false, $user_id = false)
2745 6366 acydburn
{
2746 6366 acydburn
        global $db, $auth;
2747 6366 acydburn
2748 7109 davidmj
        // update foes for some user
2749 7109 davidmj
        if (is_array($user_id) && sizeof($user_id))
2750 7109 davidmj
        {
2751 8146 acydburn
                $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
2752 7109 davidmj
                        WHERE ' . $db->sql_in_set('zebra_id', $user_id) . '
2753 7109 davidmj
                                AND foe = 1';
2754 7109 davidmj
                $db->sql_query($sql);
2755 7109 davidmj
                return;
2756 7109 davidmj
        }
2757 7109 davidmj
2758 7109 davidmj
        // update foes for some group
2759 7109 davidmj
        if (is_array($group_id) && sizeof($group_id))
2760 7109 davidmj
        {
2761 8146 acydburn
                // Grab group settings...
2762 11585 git-gate
                $sql_ary = array(
2763 7109 davidmj
                        'SELECT'        => 'a.group_id',
2764 7109 davidmj
2765 7109 davidmj
                        'FROM'                => array(
2766 7109 davidmj
                                ACL_OPTIONS_TABLE        => 'ao',
2767 11585 git-gate
                                ACL_GROUPS_TABLE        => 'a',
2768 7109 davidmj
                        ),
2769 7109 davidmj
2770 7109 davidmj
                        'LEFT_JOIN'        => array(
2771 7109 davidmj
                                array(
2772 7109 davidmj
                                        'FROM'        => array(ACL_ROLES_DATA_TABLE => 'r'),
2773 11585 git-gate
                                        'ON'        => 'a.auth_role_id = r.role_id',
2774 7109 davidmj
                                ),
2775 7109 davidmj
                        ),
2776 7109 davidmj
2777 7109 davidmj
                        'WHERE'                => '(ao.auth_option_id = a.auth_option_id OR ao.auth_option_id = r.auth_option_id)
2778 7109 davidmj
                                AND ' . $db->sql_in_set('a.group_id', $group_id) . "
2779 7109 davidmj
                                AND ao.auth_option IN ('a_', 'm_')",
2780 7109 davidmj
2781 11585 git-gate
                        'GROUP_BY'        => 'a.group_id',
2782 11585 git-gate
                );
2783 11585 git-gate
                $sql = $db->sql_build_query('SELECT', $sql_ary);
2784 7109 davidmj
                $result = $db->sql_query($sql);
2785 7109 davidmj
2786 7109 davidmj
                $groups = array();
2787 7109 davidmj
                while ($row = $db->sql_fetchrow($result))
2788 7109 davidmj
                {
2789 7109 davidmj
                        $groups[] = (int) $row['group_id'];
2790 7109 davidmj
                }
2791 7109 davidmj
                $db->sql_freeresult($result);
2792 7109 davidmj
2793 7109 davidmj
                if (!sizeof($groups))
2794 7109 davidmj
                {
2795 7109 davidmj
                        return;
2796 7109 davidmj
                }
2797 7109 davidmj
2798 7109 davidmj
                switch ($db->sql_layer)
2799 7109 davidmj
                {
2800 7109 davidmj
                        case 'mysqli':
2801 7109 davidmj
                        case 'mysql4':
2802 8814 acydburn
                                $sql = 'DELETE ' . (($db->sql_layer === 'mysqli' || version_compare($db->sql_server_info(true), '4.1', '>=')) ? 'z.*' : ZEBRA_TABLE) . '
2803 7109 davidmj
                                        FROM ' . ZEBRA_TABLE . ' z, ' . USER_GROUP_TABLE . ' ug
2804 7109 davidmj
                                        WHERE z.zebra_id = ug.user_id
2805 7109 davidmj
                                                AND z.foe = 1
2806 7109 davidmj
                                                AND ' . $db->sql_in_set('ug.group_id', $groups);
2807 7109 davidmj
                                $db->sql_query($sql);
2808 7109 davidmj
                        break;
2809 7109 davidmj
2810 7109 davidmj
                        default:
2811 7109 davidmj
                                $sql = 'SELECT user_id
2812 7109 davidmj
                                        FROM ' . USER_GROUP_TABLE . '
2813 7109 davidmj
                                        WHERE ' . $db->sql_in_set('group_id', $groups);
2814 7109 davidmj
                                $result = $db->sql_query($sql);
2815 7109 davidmj
2816 7109 davidmj
                                $users = array();
2817 7109 davidmj
                                while ($row = $db->sql_fetchrow($result))
2818 7109 davidmj
                                {
2819 7109 davidmj
                                        $users[] = (int) $row['user_id'];
2820 7109 davidmj
                                }
2821 7109 davidmj
                                $db->sql_freeresult($result);
2822 7109 davidmj
2823 7109 davidmj
                                if (sizeof($users))
2824 8782 acydburn
                                {
2825 8146 acydburn
                                        $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
2826 7109 davidmj
                                                WHERE ' . $db->sql_in_set('zebra_id', $users) . '
2827 7109 davidmj
                                                        AND foe = 1';
2828 7109 davidmj
                                        $db->sql_query($sql);
2829 7109 davidmj
                                }
2830 7109 davidmj
                        break;
2831 7109 davidmj
                }
2832 7109 davidmj
2833 7109 davidmj
                return;
2834 7109 davidmj
        }
2835 7109 davidmj
2836 7109 davidmj
        // update foes for everyone
2837 6366 acydburn
        $perms = array();
2838 6366 acydburn
        foreach ($auth->acl_get_list(false, array('a_', 'm_'), false) as $forum_id => $forum_ary)
2839 6366 acydburn
        {
2840 6366 acydburn
                foreach ($forum_ary as $auth_option => $user_ary)
2841 6366 acydburn
                {
2842 6366 acydburn
                        $perms = array_merge($perms, $user_ary);
2843 6366 acydburn
                }
2844 6366 acydburn
        }
2845 6366 acydburn
2846 6366 acydburn
        if (sizeof($perms))
2847 6366 acydburn
        {
2848 8146 acydburn
                $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
2849 6366 acydburn
                        WHERE ' . $db->sql_in_set('zebra_id', array_unique($perms)) . '
2850 6366 acydburn
                                AND foe = 1';
2851 6366 acydburn
                $db->sql_query($sql);
2852 6366 acydburn
        }
2853 6366 acydburn
        unset($perms);
2854 6366 acydburn
}
2855 6366 acydburn
2856 6366 acydburn
/**
2857 6394 grahamje
* Lists inactive users
2858 6394 grahamje
*/
2859 6394 grahamje
function view_inactive_users(&$users, &$user_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'user_inactive_time DESC')
2860 6394 grahamje
{
2861 6394 grahamje
        global $db, $user;
2862 6394 grahamje
2863 8239 acydburn
        $sql = 'SELECT COUNT(user_id) AS user_count
2864 8239 acydburn
                FROM ' . USERS_TABLE . '
2865 8239 acydburn
                WHERE user_type = ' . USER_INACTIVE .
2866 8239 acydburn
                (($limit_days) ? " AND user_inactive_time >= $limit_days" : '');
2867 8239 acydburn
        $result = $db->sql_query($sql);
2868 8239 acydburn
        $user_count = (int) $db->sql_fetchfield('user_count');
2869 8239 acydburn
        $db->sql_freeresult($result);
2870 8239 acydburn
2871 11302 git-gate
        if ($user_count == 0)
2872 11302 git-gate
        {
2873 11302 git-gate
                // Save the queries, because there are no users to display
2874 11302 git-gate
                return 0;
2875 11302 git-gate
        }
2876 11302 git-gate
2877 8239 acydburn
        if ($offset >= $user_count)
2878 8239 acydburn
        {
2879 8239 acydburn
                $offset = ($offset - $limit < 0) ? 0 : $offset - $limit;
2880 8239 acydburn
        }
2881 8239 acydburn
2882 9845 acydburn
        $sql = 'SELECT *
2883 8146 acydburn
                FROM ' . USERS_TABLE . '
2884 8146 acydburn
                WHERE user_type = ' . USER_INACTIVE .
2885 8146 acydburn
                (($limit_days) ? " AND user_inactive_time >= $limit_days" : '') . "
2886 6394 grahamje
                ORDER BY $sort_by";
2887 6394 grahamje
        $result = $db->sql_query_limit($sql, $limit, $offset);
2888 6394 grahamje
2889 6394 grahamje
        while ($row = $db->sql_fetchrow($result))
2890 6394 grahamje
        {
2891 6394 grahamje
                $row['inactive_reason'] = $user->lang['INACTIVE_REASON_UNKNOWN'];
2892 6394 grahamje
                switch ($row['user_inactive_reason'])
2893 6394 grahamje
                {
2894 6394 grahamje
                        case INACTIVE_REGISTER:
2895 6394 grahamje
                                $row['inactive_reason'] = $user->lang['INACTIVE_REASON_REGISTER'];
2896 6394 grahamje
                        break;
2897 6394 grahamje
2898 6394 grahamje
                        case INACTIVE_PROFILE:
2899 6394 grahamje
                                $row['inactive_reason'] = $user->lang['INACTIVE_REASON_PROFILE'];
2900 6394 grahamje
                        break;
2901 6394 grahamje
2902 6394 grahamje
                        case INACTIVE_MANUAL:
2903 6394 grahamje
                                $row['inactive_reason'] = $user->lang['INACTIVE_REASON_MANUAL'];
2904 6394 grahamje
                        break;
2905 6436 acydburn
2906 6436 acydburn
                        case INACTIVE_REMIND:
2907 6436 acydburn
                                $row['inactive_reason'] = $user->lang['INACTIVE_REASON_REMIND'];
2908 6436 acydburn
                        break;
2909 6394 grahamje
                }
2910 8782 acydburn
2911 6394 grahamje
                $users[] = $row;
2912 6394 grahamje
        }
2913 6394 grahamje
2914 8239 acydburn
        return $offset;
2915 6394 grahamje
}
2916 6394 grahamje
2917 6394 grahamje
/**
2918 5329 grahamje
* Lists warned users
2919 5329 grahamje
*/
2920 5333 grahamje
function view_warned_users(&$users, &$user_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'user_warnings DESC')
2921 5329 grahamje
{
2922 5329 grahamje
        global $db;
2923 5329 grahamje
2924 6650 acydburn
        $sql = 'SELECT user_id, username, user_colour, user_warnings, user_last_warning
2925 5333 grahamje
                FROM ' . USERS_TABLE . '
2926 5329 grahamje
                WHERE user_warnings > 0
2927 5333 grahamje
                ' . (($limit_days) ? "AND user_last_warning >= $limit_days" : '') . "
2928 5333 grahamje
                ORDER BY $sort_by";
2929 5329 grahamje
        $result = $db->sql_query_limit($sql, $limit, $offset);
2930 5329 grahamje
        $users = $db->sql_fetchrowset($result);
2931 5329 grahamje
        $db->sql_freeresult($result);
2932 5329 grahamje
2933 5329 grahamje
        $sql = 'SELECT count(user_id) AS user_count
2934 5329 grahamje
                FROM ' . USERS_TABLE . '
2935 5333 grahamje
                WHERE user_warnings > 0
2936 5333 grahamje
                ' . (($limit_days) ? "AND user_last_warning >= $limit_days" : '');
2937 5329 grahamje
        $result = $db->sql_query($sql);
2938 6015 acydburn
        $user_count = (int) $db->sql_fetchfield('user_count');
2939 5329 grahamje
        $db->sql_freeresult($result);
2940 5329 grahamje
2941 5329 grahamje
        return;
2942 5329 grahamje
}
2943 5329 grahamje
2944 5114 acydburn
/**
2945 5302 acydburn
* Get database size
2946 5302 acydburn
* Currently only mysql and mssql are supported
2947 5302 acydburn
*/
2948 5302 acydburn
function get_database_size()
2949 5302 acydburn
{
2950 5302 acydburn
        global $db, $user, $table_prefix;
2951 6048 acydburn
2952 6048 acydburn
        $database_size = false;
2953 6048 acydburn
2954 6015 acydburn
        // This code is heavily influenced by a similar routine in phpMyAdmin 2.2.0
2955 6497 acydburn
        switch ($db->sql_layer)
2956 5302 acydburn
        {
2957 6042 davidmj
                case 'mysql':
2958 6042 davidmj
                case 'mysql4':
2959 6042 davidmj
                case 'mysqli':
2960 6042 davidmj
                        $sql = 'SELECT VERSION() AS mysql_version';
2961 6042 davidmj
                        $result = $db->sql_query($sql);
2962 6042 davidmj
                        $row = $db->sql_fetchrow($result);
2963 6042 davidmj
                        $db->sql_freeresult($result);
2964 5302 acydburn
2965 6042 davidmj
                        if ($row)
2966 5302 acydburn
                        {
2967 6042 davidmj
                                $version = $row['mysql_version'];
2968 5302 acydburn
2969 6042 davidmj
                                if (preg_match('#(3\.23|[45]\.)#', $version))
2970 6042 davidmj
                                {
2971 6042 davidmj
                                        $db_name = (preg_match('#^(?:3\.23\.(?:[6-9]|[1-9]{2}))|[45]\.#', $version)) ? "`{$db->dbname}`" : $db->dbname;
2972 5302 acydburn
2973 6042 davidmj
                                        $sql = 'SHOW TABLE STATUS
2974 6042 davidmj
                                                FROM ' . $db_name;
2975 8231 acydburn
                                        $result = $db->sql_query($sql, 7200);
2976 6042 davidmj
2977 6048 acydburn
                                        $database_size = 0;
2978 6042 davidmj
                                        while ($row = $db->sql_fetchrow($result))
2979 5302 acydburn
                                        {
2980 6042 davidmj
                                                if ((isset($row['Type']) && $row['Type'] != 'MRG_MyISAM') || (isset($row['Engine']) && ($row['Engine'] == 'MyISAM' || $row['Engine'] == 'InnoDB')))
2981 5302 acydburn
                                                {
2982 6042 davidmj
                                                        if ($table_prefix != '')
2983 5302 acydburn
                                                        {
2984 6549 davidmj
                                                                if (strpos($row['Name'], $table_prefix) !== false)
2985 6042 davidmj
                                                                {
2986 6048 acydburn
                                                                        $database_size += $row['Data_length'] + $row['Index_length'];
2987 6042 davidmj
                                                                }
2988 6042 davidmj
                                                        }
2989 6042 davidmj
                                                        else
2990 6042 davidmj
                                                        {
2991 6048 acydburn
                                                                $database_size += $row['Data_length'] + $row['Index_length'];
2992 5302 acydburn
                                                        }
2993 5302 acydburn
                                                }
2994 5302 acydburn
                                        }
2995 6042 davidmj
                                        $db->sql_freeresult($result);
2996 5302 acydburn
                                }
2997 6042 davidmj
                        }
2998 6042 davidmj
                break;
2999 6042 davidmj
3000 7965 davidmj
                case 'firebird':
3001 7965 davidmj
                        global $dbname;
3002 7965 davidmj
3003 7965 davidmj
                        // if it on the local machine, we can get lucky
3004 7965 davidmj
                        if (file_exists($dbname))
3005 7965 davidmj
                        {
3006 7965 davidmj
                                $database_size = filesize($dbname);
3007 7965 davidmj
                        }
3008 7965 davidmj
3009 7965 davidmj
                break;
3010 7965 davidmj
3011 7012 davidmj
                case 'sqlite':
3012 7011 davidmj
                        global $dbhost;
3013 7011 davidmj
3014 7011 davidmj
                        if (file_exists($dbhost))
3015 7011 davidmj
                        {
3016 7011 davidmj
                                $database_size = filesize($dbhost);
3017 7011 davidmj
                        }
3018 7011 davidmj
3019 7011 davidmj
                break;
3020 7011 davidmj
3021 7013 davidmj
                case 'mssql':
3022 7013 davidmj
                case 'mssql_odbc':
3023 10558 git-gate
                case 'mssqlnative':
3024 6042 davidmj
                        $sql = 'SELECT ((SUM(size) * 8.0) * 1024.0) as dbsize
3025 6042 davidmj
                                FROM sysfiles';
3026 8231 acydburn
                        $result = $db->sql_query($sql, 7200);
3027 6048 acydburn
                        $database_size = ($row = $db->sql_fetchrow($result)) ? $row['dbsize'] : false;
3028 6042 davidmj
                        $db->sql_freeresult($result);
3029 6042 davidmj
                break;
3030 6042 davidmj
3031 6042 davidmj
                case 'postgres':
3032 6042 davidmj
                        $sql = "SELECT proname
3033 6042 davidmj
                                FROM pg_proc
3034 6042 davidmj
                                WHERE proname = 'pg_database_size'";
3035 6042 davidmj
                        $result = $db->sql_query($sql);
3036 6042 davidmj
                        $row = $db->sql_fetchrow($result);
3037 6042 davidmj
                        $db->sql_freeresult($result);
3038 6042 davidmj
3039 6042 davidmj
                        if ($row['proname'] == 'pg_database_size')
3040 6042 davidmj
                        {
3041 7369 davidmj
                                $database = $db->dbname;
3042 7369 davidmj
                                if (strpos($database, '.') !== false)
3043 7369 davidmj
                                {
3044 7369 davidmj
                                        list($database, ) = explode('.', $database);
3045 7369 davidmj
                                }
3046 7369 davidmj
3047 6042 davidmj
                                $sql = "SELECT oid
3048 6042 davidmj
                                        FROM pg_database
3049 7369 davidmj
                                        WHERE datname = '$database'";
3050 6042 davidmj
                                $result = $db->sql_query($sql);
3051 6042 davidmj
                                $row = $db->sql_fetchrow($result);
3052 5302 acydburn
                                $db->sql_freeresult($result);
3053 6042 davidmj
3054 6042 davidmj
                                $oid = $row['oid'];
3055 6042 davidmj
3056 6042 davidmj
                                $sql = 'SELECT pg_database_size(' . $oid . ') as size';
3057 6042 davidmj
                                $result = $db->sql_query($sql);
3058 6042 davidmj
                                $row = $db->sql_fetchrow($result);
3059 6042 davidmj
                                $db->sql_freeresult($result);
3060 6042 davidmj
3061 6048 acydburn
                                $database_size = $row['size'];
3062 5302 acydburn
                        }
3063 6042 davidmj
                break;
3064 7721 davidmj
3065 7721 davidmj
                case 'oracle':
3066 7721 davidmj
                        $sql = 'SELECT SUM(bytes) as dbsize
3067 7721 davidmj
                                FROM user_segments';
3068 8231 acydburn
                        $result = $db->sql_query($sql, 7200);
3069 7721 davidmj
                        $database_size = ($row = $db->sql_fetchrow($result)) ? $row['dbsize'] : false;
3070 7721 davidmj
                        $db->sql_freeresult($result);
3071 7721 davidmj
                break;
3072 5302 acydburn
        }
3073 5302 acydburn
3074 8389 acydburn
        $database_size = ($database_size !== false) ? get_formatted_filesize($database_size) : $user->lang['NOT_AVAILABLE'];
3075 5302 acydburn
3076 6048 acydburn
        return $database_size;
3077 5302 acydburn
}
3078 5302 acydburn
3079 5302 acydburn
/**
3080 5416 acydburn
* Retrieve contents from remotely stored file
3081 5416 acydburn
*/
3082 11465 git-gate
function get_remote_file($host, $directory, $filename, &$errstr, &$errno, $port = 80, $timeout = 6)
3083 5416 acydburn
{
3084 5416 acydburn
        global $user;
3085 5416 acydburn
3086 5416 acydburn
        if ($fsock = @fsockopen($host, $port, $errno, $errstr, $timeout))
3087 5416 acydburn
        {
3088 5416 acydburn
                @fputs($fsock, "GET $directory/$filename HTTP/1.1\r\n");
3089 5416 acydburn
                @fputs($fsock, "HOST: $host\r\n");
3090 5416 acydburn
                @fputs($fsock, "Connection: close\r\n\r\n");
3091 8782 acydburn
3092 11465 git-gate
                $timer_stop = time() + $timeout;
3093 11465 git-gate
                stream_set_timeout($fsock, $timeout);
3094 11465 git-gate
3095 5416 acydburn
                $file_info = '';
3096 5416 acydburn
                $get_info = false;
3097 5416 acydburn
3098 5416 acydburn
                while (!@feof($fsock))
3099 5416 acydburn
                {
3100 5416 acydburn
                        if ($get_info)
3101 5416 acydburn
                        {
3102 5416 acydburn
                                $file_info .= @fread($fsock, 1024);
3103 5416 acydburn
                        }
3104 5416 acydburn
                        else
3105 5416 acydburn
                        {
3106 5416 acydburn
                                $line = @fgets($fsock, 1024);
3107 5416 acydburn
                                if ($line == "\r\n")
3108 5416 acydburn
                                {
3109 5416 acydburn
                                        $get_info = true;
3110 5416 acydburn
                                }
3111 6846 acydburn
                                else if (stripos($line, '404 not found') !== false)
3112 5416 acydburn
                                {
3113 6312 acydburn
                                        $errstr = $user->lang['FILE_NOT_FOUND'] . ': ' . $filename;
3114 5416 acydburn
                                        return false;
3115 5416 acydburn
                                }
3116 5416 acydburn
                        }
3117 11465 git-gate
3118 11465 git-gate
                        $stream_meta_data = stream_get_meta_data($fsock);
3119 11465 git-gate
3120 11465 git-gate
                        if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop)
3121 11465 git-gate
                        {
3122 11465 git-gate
                                $errstr = $user->lang['FSOCK_TIMEOUT'];
3123 11465 git-gate
                                return false;
3124 11465 git-gate
                        }
3125 5416 acydburn
                }
3126 5416 acydburn
                @fclose($fsock);
3127 5416 acydburn
        }
3128 5416 acydburn
        else
3129 5416 acydburn
        {
3130 5416 acydburn
                if ($errstr)
3131 5416 acydburn
                {
3132 6850 acydburn
                        $errstr = utf8_convert_message($errstr);
3133 5416 acydburn
                        return false;
3134 5416 acydburn
                }
3135 5416 acydburn
                else
3136 5416 acydburn
                {
3137 6846 acydburn
                        $errstr = $user->lang['FSOCK_DISABLED'];
3138 5416 acydburn
                        return false;
3139 5416 acydburn
                }
3140 5416 acydburn
        }
3141 8782 acydburn
3142 5416 acydburn
        return $file_info;
3143 5416 acydburn
}
3144 5416 acydburn
3145 5416 acydburn
/**
3146 5602 grahamje
* Tidy Warnings
3147 5602 grahamje
* Remove all warnings which have now expired from the database
3148 5602 grahamje
* The duration of a warning can be defined by the administrator
3149 6915 acydburn
* This only removes the warning and reduces the associated count,
3150 5602 grahamje
* it does not remove the user note recording the contents of the warning
3151 5602 grahamje
*/
3152 5602 grahamje
function tidy_warnings()
3153 5602 grahamje
{
3154 5602 grahamje
        global $db, $config;
3155 5602 grahamje
3156 5602 grahamje
        $expire_date = time() - ($config['warnings_expire_days'] * 86400);
3157 5602 grahamje
        $warning_list = $user_list = array();
3158 5602 grahamje
3159 7027 davidmj
        $sql = 'SELECT * FROM ' . WARNINGS_TABLE . "
3160 7027 davidmj
                WHERE warning_time < $expire_date";
3161 5602 grahamje
        $result = $db->sql_query($sql);
3162 5602 grahamje
3163 5602 grahamje
        while ($row = $db->sql_fetchrow($result))
3164 5602 grahamje
        {
3165 5602 grahamje
                $warning_list[] = $row['warning_id'];
3166 7024 davidmj
                $user_list[$row['user_id']] = isset($user_list[$row['user_id']]) ? ++$user_list[$row['user_id']] : 1;
3167 5602 grahamje
        }
3168 5602 grahamje
        $db->sql_freeresult($result);
3169 5602 grahamje
3170 5602 grahamje
        if (sizeof($warning_list))
3171 5602 grahamje
        {
3172 5602 grahamje
                $db->sql_transaction('begin');
3173 5602 grahamje
3174 6271 acydburn
                $sql = 'DELETE FROM ' . WARNINGS_TABLE . '
3175 6271 acydburn
                        WHERE ' . $db->sql_in_set('warning_id', $warning_list);
3176 5602 grahamje
                $db->sql_query($sql);
3177 8782 acydburn
3178 5765 acydburn
                foreach ($user_list as $user_id => $value)
3179 5602 grahamje
                {
3180 5602 grahamje
                        $sql = 'UPDATE ' . USERS_TABLE . " SET user_warnings = user_warnings - $value
3181 5602 grahamje
                                WHERE user_id = $user_id";
3182 5602 grahamje
                        $db->sql_query($sql);
3183 5602 grahamje
                }
3184 5602 grahamje
3185 5602 grahamje
                $db->sql_transaction('commit');
3186 5602 grahamje
        }
3187 5602 grahamje
3188 5929 acydburn
        set_config('warnings_last_gc', time(), true);
3189 5602 grahamje
}
3190 5602 grahamje
3191 5602 grahamje
/**
3192 5929 acydburn
* Tidy database, doing some maintanance tasks
3193 5135 acydburn
*/
3194 5135 acydburn
function tidy_database()
3195 5135 acydburn
{
3196 5135 acydburn
        global $db;
3197 5135 acydburn
3198 8384 acydburn
        // Here we check permission consistency
3199 8384 acydburn
3200 8384 acydburn
        // Sometimes, it can happen permission tables having forums listed which do not exist
3201 8384 acydburn
        $sql = 'SELECT forum_id
3202 8384 acydburn
                FROM ' . FORUMS_TABLE;
3203 8384 acydburn
        $result = $db->sql_query($sql);
3204 8384 acydburn
3205 8384 acydburn
        $forum_ids = array(0);
3206 8384 acydburn
        while ($row = $db->sql_fetchrow($result))
3207 8384 acydburn
        {
3208 8384 acydburn
                $forum_ids[] = $row['forum_id'];
3209 8384 acydburn
        }
3210 8384 acydburn
        $db->sql_freeresult($result);
3211 8384 acydburn
3212 8384 acydburn
        // Delete those rows from the acl tables not having listed the forums above
3213 8384 acydburn
        $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
3214 8384 acydburn
                WHERE ' . $db->sql_in_set('forum_id', $forum_ids, true);
3215 8384 acydburn
        $db->sql_query($sql);
3216 8384 acydburn
3217 8384 acydburn
        $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
3218 8384 acydburn
                WHERE ' . $db->sql_in_set('forum_id', $forum_ids, true);
3219 8384 acydburn
        $db->sql_query($sql);
3220 8384 acydburn
3221 6539 acydburn
        set_config('database_last_gc', time(), true);
3222 6539 acydburn
}
3223 6111 acydburn
3224 6539 acydburn
/**
3225 6539 acydburn
* Add permission language - this will make sure custom files will be included
3226 6539 acydburn
*/
3227 6539 acydburn
function add_permission_language()
3228 6539 acydburn
{
3229 6539 acydburn
        global $user, $phpEx;
3230 5135 acydburn
3231 7502 acydburn
        // First of all, our own file. We need to include it as the first file because it presets all relevant variables.
3232 7502 acydburn
        $user->add_lang('acp/permissions_phpbb');
3233 7502 acydburn
3234 6539 acydburn
        $files_to_add = array();
3235 6539 acydburn
3236 7502 acydburn
        // Now search in acp and mods folder for permissions_ files.
3237 6539 acydburn
        foreach (array('acp/', 'mods/') as $path)
3238 6539 acydburn
        {
3239 8782 acydburn
                $dh = @opendir($user->lang_path . $user->lang_name . '/' . $path);
3240 6539 acydburn
3241 6912 acydburn
                if ($dh)
3242 6539 acydburn
                {
3243 6539 acydburn
                        while (($file = readdir($dh)) !== false)
3244 6539 acydburn
                        {
3245 7502 acydburn
                                if ($file !== 'permissions_phpbb.' . $phpEx && strpos($file, 'permissions_') === 0 && substr($file, -(strlen($phpEx) + 1)) === '.' . $phpEx)
3246 6539 acydburn
                                {
3247 6539 acydburn
                                        $files_to_add[] = $path . substr($file, 0, -(strlen($phpEx) + 1));
3248 6539 acydburn
                                }
3249 6539 acydburn
                        }
3250 6539 acydburn
                        closedir($dh);
3251 6539 acydburn
                }
3252 6539 acydburn
        }
3253 6539 acydburn
3254 6539 acydburn
        if (!sizeof($files_to_add))
3255 6539 acydburn
        {
3256 6539 acydburn
                return false;
3257 6539 acydburn
        }
3258 6539 acydburn
3259 6539 acydburn
        $user->add_lang($files_to_add);
3260 6539 acydburn
        return true;
3261 5135 acydburn
}
3262 5135 acydburn
3263 9880 aptx
/**
3264 9880 aptx
 * Obtains the latest version information
3265 9880 aptx
 *
3266 9880 aptx
 * @param bool $force_update Ignores cached data. Defaults to false.
3267 9880 aptx
 * @param bool $warn_fail Trigger a warning if obtaining the latest version information fails. Defaults to false.
3268 9880 aptx
 * @param int $ttl Cache version information for $ttl seconds. Defaults to 86400 (24 hours).
3269 9880 aptx
 *
3270 9880 aptx
 * @return string | false Version info on success, false on failure.
3271 9880 aptx
 */
3272 9880 aptx
function obtain_latest_version_info($force_update = false, $warn_fail = false, $ttl = 86400)
3273 9880 aptx
{
3274 9880 aptx
        global $cache;
3275 9880 aptx
3276 9880 aptx
        $info = $cache->get('versioncheck');
3277 9880 aptx
3278 9880 aptx
        if ($info === false || $force_update)
3279 9880 aptx
        {
3280 9880 aptx
                $errstr = '';
3281 9880 aptx
                $errno = 0;
3282 9880 aptx
3283 10836 git-gate
                $info = get_remote_file('version.phpbb.com', '/phpbb',
3284 9880 aptx
                                ((defined('PHPBB_QA')) ? '30x_qa.txt' : '30x.txt'), $errstr, $errno);
3285 9880 aptx
3286 9880 aptx
                if ($info === false)
3287 9880 aptx
                {
3288 9880 aptx
                        $cache->destroy('versioncheck');
3289 9880 aptx
                        if ($warn_fail)
3290 9880 aptx
                        {
3291 9880 aptx
                                trigger_error($errstr, E_USER_WARNING);
3292 9880 aptx
                        }
3293 9880 aptx
                        return false;
3294 9880 aptx
                }
3295 9880 aptx
3296 9880 aptx
                $cache->put('versioncheck', $info, $ttl);
3297 9880 aptx
        }
3298 9880 aptx
3299 9880 aptx
        return $info;
3300 9880 aptx
}
3301 9880 aptx
3302 10558 git-gate
/**
3303 10558 git-gate
 * Enables a particular flag in a bitfield column of a given table.
3304 10558 git-gate
 *
3305 10558 git-gate
 * @param string        $table_name                The table to update
3306 10558 git-gate
 * @param string        $column_name        The column containing a bitfield to update
3307 10558 git-gate
 * @param int                $flag                        The binary flag which is OR-ed with the current column value
3308 10558 git-gate
 * @param string        $sql_more                This string is attached to the sql query generated to update the table.
3309 10558 git-gate
 *
3310 10558 git-gate
 * @return void
3311 10558 git-gate
 */
3312 10558 git-gate
function enable_bitfield_column_flag($table_name, $column_name, $flag, $sql_more = '')
3313 10558 git-gate
{
3314 10558 git-gate
        global $db;
3315 10558 git-gate
3316 10558 git-gate
        $sql = 'UPDATE ' . $table_name . '
3317 10558 git-gate
                SET ' . $column_name . ' = ' . $db->sql_bit_or($column_name, $flag) . '
3318 10558 git-gate
                ' . $sql_more;
3319 10558 git-gate
        $db->sql_query($sql);
3320 10558 git-gate
}