1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/functions_admin.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,3377 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* @package acp
1.8 +* @version $Id$
1.9 +* @copyright (c) 2005 phpBB Group
1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.11 +*
1.12 +*/
1.13 +
1.14 +/**
1.15 +* @ignore
1.16 +*/
1.17 +if (!defined('IN_PHPBB'))
1.18 +{
1.19 + exit;
1.20 +}
1.21 +
1.22 +/**
1.23 +* Recalculate Nested Sets
1.24 +*
1.25 +* @param int $new_id first left_id (should start with 1)
1.26 +* @param string $pkey primary key-column (containing the id for the parent_id of the children)
1.27 +* @param string $table constant or fullname of the table
1.28 +* @param int $parent_id parent_id of the current set (default = 0)
1.29 +* @param array $where contains strings to compare closer on the where statement (additional)
1.30 +*
1.31 +* @author EXreaction
1.32 +*/
1.33 +function recalc_nested_sets(&$new_id, $pkey, $table, $parent_id = 0, $where = array())
1.34 +{
1.35 + global $db;
1.36 +
1.37 + $sql = 'SELECT *
1.38 + FROM ' . $table . '
1.39 + WHERE parent_id = ' . (int) $parent_id .
1.40 + ((!empty($where)) ? ' AND ' . implode(' AND ', $where) : '') . '
1.41 + ORDER BY left_id ASC';
1.42 + $result = $db->sql_query($sql);
1.43 + while ($row = $db->sql_fetchrow($result))
1.44 + {
1.45 + // First we update the left_id for this module
1.46 + if ($row['left_id'] != $new_id)
1.47 + {
1.48 + $db->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', array('left_id' => $new_id)) . " WHERE $pkey = {$row[$pkey]}");
1.49 + }
1.50 + $new_id++;
1.51 +
1.52 + // Then we go through any children and update their left/right id's
1.53 + recalc_nested_sets($new_id, $pkey, $table, $row[$pkey], $where);
1.54 +
1.55 + // Then we come back and update the right_id for this module
1.56 + if ($row['right_id'] != $new_id)
1.57 + {
1.58 + $db->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', array('right_id' => $new_id)) . " WHERE $pkey = {$row[$pkey]}");
1.59 + }
1.60 + $new_id++;
1.61 + }
1.62 + $db->sql_freeresult($result);
1.63 +}
1.64 +
1.65 +/**
1.66 +* Simple version of jumpbox, just lists authed forums
1.67 +*/
1.68 +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)
1.69 +{
1.70 + global $db, $user, $auth;
1.71 +
1.72 + // This query is identical to the jumpbox one
1.73 + $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, forum_flags, forum_options, left_id, right_id
1.74 + FROM ' . FORUMS_TABLE . '
1.75 + ORDER BY left_id ASC';
1.76 + $result = $db->sql_query($sql, 600);
1.77 +
1.78 + $right = 0;
1.79 + $padding_store = array('0' => '');
1.80 + $padding = '';
1.81 + $forum_list = ($return_array) ? array() : '';
1.82 +
1.83 + // Sometimes it could happen that forums will be displayed here not be displayed within the index page
1.84 + // This is the result of forums not displayed at index, having list permissions and a parent of a forum with no permissions.
1.85 + // If this happens, the padding could be "broken"
1.86 +
1.87 + while ($row = $db->sql_fetchrow($result))
1.88 + {
1.89 + if ($row['left_id'] < $right)
1.90 + {
1.91 + $padding .= ' ';
1.92 + $padding_store[$row['parent_id']] = $padding;
1.93 + }
1.94 + else if ($row['left_id'] > $right + 1)
1.95 + {
1.96 + $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
1.97 + }
1.98 +
1.99 + $right = $row['right_id'];
1.100 + $disabled = false;
1.101 +
1.102 + if (!$ignore_acl && $auth->acl_gets(array('f_list', 'a_forum', 'a_forumadd', 'a_forumdel'), $row['forum_id']))
1.103 + {
1.104 + 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'])))
1.105 + {
1.106 + $disabled = true;
1.107 + }
1.108 + }
1.109 + else if (!$ignore_acl)
1.110 + {
1.111 + continue;
1.112 + }
1.113 +
1.114 + if (
1.115 + ((is_array($ignore_id) && in_array($row['forum_id'], $ignore_id)) || $row['forum_id'] == $ignore_id)
1.116 + ||
1.117 + // Non-postable forum with no subforums, don't display
1.118 + ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']) && $ignore_emptycat)
1.119 + ||
1.120 + ($row['forum_type'] != FORUM_POST && $ignore_nonpost)
1.121 + )
1.122 + {
1.123 + $disabled = true;
1.124 + }
1.125 +
1.126 + if ($return_array)
1.127 + {
1.128 + // Include some more information...
1.129 + $selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? true : false) : (($row['forum_id'] == $select_id) ? true : false);
1.130 + $forum_list[$row['forum_id']] = array_merge(array('padding' => $padding, 'selected' => ($selected && !$disabled), 'disabled' => $disabled), $row);
1.131 + }
1.132 + else
1.133 + {
1.134 + $selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? ' selected="selected"' : '') : (($row['forum_id'] == $select_id) ? ' selected="selected"' : '');
1.135 + $forum_list .= '<option value="' . $row['forum_id'] . '"' . (($disabled) ? ' disabled="disabled" class="disabled-option"' : $selected) . '>' . $padding . $row['forum_name'] . '</option>';
1.136 + }
1.137 + }
1.138 + $db->sql_freeresult($result);
1.139 + unset($padding_store);
1.140 +
1.141 + return $forum_list;
1.142 +}
1.143 +
1.144 +/**
1.145 +* Generate size select options
1.146 +*/
1.147 +function size_select_options($size_compare)
1.148 +{
1.149 + global $user;
1.150 +
1.151 + $size_types_text = array($user->lang['BYTES'], $user->lang['KIB'], $user->lang['MIB']);
1.152 + $size_types = array('b', 'kb', 'mb');
1.153 +
1.154 + $s_size_options = '';
1.155 +
1.156 + for ($i = 0, $size = sizeof($size_types_text); $i < $size; $i++)
1.157 + {
1.158 + $selected = ($size_compare == $size_types[$i]) ? ' selected="selected"' : '';
1.159 + $s_size_options .= '<option value="' . $size_types[$i] . '"' . $selected . '>' . $size_types_text[$i] . '</option>';
1.160 + }
1.161 +
1.162 + return $s_size_options;
1.163 +}
1.164 +
1.165 +/**
1.166 +* Generate list of groups (option fields without select)
1.167 +*
1.168 +* @param int $group_id The default group id to mark as selected
1.169 +* @param array $exclude_ids The group ids to exclude from the list, false (default) if you whish to exclude no id
1.170 +* @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.
1.171 +*
1.172 +* @return string The list of options.
1.173 +*/
1.174 +function group_select_options($group_id, $exclude_ids = false, $manage_founder = false)
1.175 +{
1.176 + global $db, $user, $config;
1.177 +
1.178 + $exclude_sql = ($exclude_ids !== false && sizeof($exclude_ids)) ? 'WHERE ' . $db->sql_in_set('group_id', array_map('intval', $exclude_ids), true) : '';
1.179 + $sql_and = (!$config['coppa_enable']) ? (($exclude_sql) ? ' AND ' : ' WHERE ') . "group_name <> 'REGISTERED_COPPA'" : '';
1.180 + $sql_founder = ($manage_founder !== false) ? (($exclude_sql || $sql_and) ? ' AND ' : ' WHERE ') . 'group_founder_manage = ' . (int) $manage_founder : '';
1.181 +
1.182 + $sql = 'SELECT group_id, group_name, group_type
1.183 + FROM ' . GROUPS_TABLE . "
1.184 + $exclude_sql
1.185 + $sql_and
1.186 + $sql_founder
1.187 + ORDER BY group_type DESC, group_name ASC";
1.188 + $result = $db->sql_query($sql);
1.189 +
1.190 + $s_group_options = '';
1.191 + while ($row = $db->sql_fetchrow($result))
1.192 + {
1.193 + $selected = ($row['group_id'] == $group_id) ? ' selected="selected"' : '';
1.194 + $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>';
1.195 + }
1.196 + $db->sql_freeresult($result);
1.197 +
1.198 + return $s_group_options;
1.199 +}
1.200 +
1.201 +/**
1.202 +* Obtain authed forums list
1.203 +*/
1.204 +function get_forum_list($acl_list = 'f_list', $id_only = true, $postable_only = false, $no_cache = false)
1.205 +{
1.206 + global $db, $auth;
1.207 + static $forum_rows;
1.208 +
1.209 + if (!isset($forum_rows))
1.210 + {
1.211 + // This query is identical to the jumpbox one
1.212 + $expire_time = ($no_cache) ? 0 : 600;
1.213 +
1.214 + $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
1.215 + FROM ' . FORUMS_TABLE . '
1.216 + ORDER BY left_id ASC';
1.217 + $result = $db->sql_query($sql, $expire_time);
1.218 +
1.219 + $forum_rows = array();
1.220 +
1.221 + $right = $padding = 0;
1.222 + $padding_store = array('0' => 0);
1.223 +
1.224 + while ($row = $db->sql_fetchrow($result))
1.225 + {
1.226 + if ($row['left_id'] < $right)
1.227 + {
1.228 + $padding++;
1.229 + $padding_store[$row['parent_id']] = $padding;
1.230 + }
1.231 + else if ($row['left_id'] > $right + 1)
1.232 + {
1.233 + // Ok, if the $padding_store for this parent is empty there is something wrong. For now we will skip over it.
1.234 + // @todo digging deep to find out "how" this can happen.
1.235 + $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : $padding;
1.236 + }
1.237 +
1.238 + $right = $row['right_id'];
1.239 + $row['padding'] = $padding;
1.240 +
1.241 + $forum_rows[] = $row;
1.242 + }
1.243 + $db->sql_freeresult($result);
1.244 + unset($padding_store);
1.245 + }
1.246 +
1.247 + $rowset = array();
1.248 + foreach ($forum_rows as $row)
1.249 + {
1.250 + if ($postable_only && $row['forum_type'] != FORUM_POST)
1.251 + {
1.252 + continue;
1.253 + }
1.254 +
1.255 + if ($acl_list == '' || ($acl_list != '' && $auth->acl_gets($acl_list, $row['forum_id'])))
1.256 + {
1.257 + $rowset[] = ($id_only) ? (int) $row['forum_id'] : $row;
1.258 + }
1.259 + }
1.260 +
1.261 + return $rowset;
1.262 +}
1.263 +
1.264 +/**
1.265 +* Get forum branch
1.266 +*/
1.267 +function get_forum_branch($forum_id, $type = 'all', $order = 'descending', $include_forum = true)
1.268 +{
1.269 + global $db;
1.270 +
1.271 + switch ($type)
1.272 + {
1.273 + case 'parents':
1.274 + $condition = 'f1.left_id BETWEEN f2.left_id AND f2.right_id';
1.275 + break;
1.276 +
1.277 + case 'children':
1.278 + $condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id';
1.279 + break;
1.280 +
1.281 + default:
1.282 + $condition = 'f2.left_id BETWEEN f1.left_id AND f1.right_id OR f1.left_id BETWEEN f2.left_id AND f2.right_id';
1.283 + break;
1.284 + }
1.285 +
1.286 + $rows = array();
1.287 +
1.288 + $sql = 'SELECT f2.*
1.289 + FROM ' . FORUMS_TABLE . ' f1
1.290 + LEFT JOIN ' . FORUMS_TABLE . " f2 ON ($condition)
1.291 + WHERE f1.forum_id = $forum_id
1.292 + ORDER BY f2.left_id " . (($order == 'descending') ? 'ASC' : 'DESC');
1.293 + $result = $db->sql_query($sql);
1.294 +
1.295 + while ($row = $db->sql_fetchrow($result))
1.296 + {
1.297 + if (!$include_forum && $row['forum_id'] == $forum_id)
1.298 + {
1.299 + continue;
1.300 + }
1.301 +
1.302 + $rows[] = $row;
1.303 + }
1.304 + $db->sql_freeresult($result);
1.305 +
1.306 + return $rows;
1.307 +}
1.308 +
1.309 +/**
1.310 +* Copies permissions from one forum to others
1.311 +*
1.312 +* @param int $src_forum_id The source forum we want to copy permissions from
1.313 +* @param array $dest_forum_ids The destination forum(s) we want to copy to
1.314 +* @param bool $clear_dest_perms True if destination permissions should be deleted
1.315 +* @param bool $add_log True if log entry should be added
1.316 +*
1.317 +* @return bool False on error
1.318 +*
1.319 +* @author bantu
1.320 +*/
1.321 +function copy_forum_permissions($src_forum_id, $dest_forum_ids, $clear_dest_perms = true, $add_log = true)
1.322 +{
1.323 + global $db;
1.324 +
1.325 + // Only one forum id specified
1.326 + if (!is_array($dest_forum_ids))
1.327 + {
1.328 + $dest_forum_ids = array($dest_forum_ids);
1.329 + }
1.330 +
1.331 + // Make sure forum ids are integers
1.332 + $src_forum_id = (int) $src_forum_id;
1.333 + $dest_forum_ids = array_map('intval', $dest_forum_ids);
1.334 +
1.335 + // No source forum or no destination forums specified
1.336 + if (empty($src_forum_id) || empty($dest_forum_ids))
1.337 + {
1.338 + return false;
1.339 + }
1.340 +
1.341 + // Check if source forum exists
1.342 + $sql = 'SELECT forum_name
1.343 + FROM ' . FORUMS_TABLE . '
1.344 + WHERE forum_id = ' . $src_forum_id;
1.345 + $result = $db->sql_query($sql);
1.346 + $src_forum_name = $db->sql_fetchfield('forum_name');
1.347 + $db->sql_freeresult($result);
1.348 +
1.349 + // Source forum doesn't exist
1.350 + if (empty($src_forum_name))
1.351 + {
1.352 + return false;
1.353 + }
1.354 +
1.355 + // Check if destination forums exists
1.356 + $sql = 'SELECT forum_id, forum_name
1.357 + FROM ' . FORUMS_TABLE . '
1.358 + WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
1.359 + $result = $db->sql_query($sql);
1.360 +
1.361 + $dest_forum_ids = $dest_forum_names = array();
1.362 + while ($row = $db->sql_fetchrow($result))
1.363 + {
1.364 + $dest_forum_ids[] = (int) $row['forum_id'];
1.365 + $dest_forum_names[] = $row['forum_name'];
1.366 + }
1.367 + $db->sql_freeresult($result);
1.368 +
1.369 + // No destination forum exists
1.370 + if (empty($dest_forum_ids))
1.371 + {
1.372 + return false;
1.373 + }
1.374 +
1.375 + // From the mysql documentation:
1.376 + // Prior to MySQL 4.0.14, the target table of the INSERT statement cannot appear
1.377 + // in the FROM clause of the SELECT part of the query. This limitation is lifted in 4.0.14.
1.378 + // Due to this we stay on the safe side if we do the insertion "the manual way"
1.379 +
1.380 + // Rowsets we're going to insert
1.381 + $users_sql_ary = $groups_sql_ary = array();
1.382 +
1.383 + // Query acl users table for source forum data
1.384 + $sql = 'SELECT user_id, auth_option_id, auth_role_id, auth_setting
1.385 + FROM ' . ACL_USERS_TABLE . '
1.386 + WHERE forum_id = ' . $src_forum_id;
1.387 + $result = $db->sql_query($sql);
1.388 +
1.389 + while ($row = $db->sql_fetchrow($result))
1.390 + {
1.391 + $row = array(
1.392 + 'user_id' => (int) $row['user_id'],
1.393 + 'auth_option_id' => (int) $row['auth_option_id'],
1.394 + 'auth_role_id' => (int) $row['auth_role_id'],
1.395 + 'auth_setting' => (int) $row['auth_setting'],
1.396 + );
1.397 +
1.398 + foreach ($dest_forum_ids as $dest_forum_id)
1.399 + {
1.400 + $users_sql_ary[] = $row + array('forum_id' => $dest_forum_id);
1.401 + }
1.402 + }
1.403 + $db->sql_freeresult($result);
1.404 +
1.405 + // Query acl groups table for source forum data
1.406 + $sql = 'SELECT group_id, auth_option_id, auth_role_id, auth_setting
1.407 + FROM ' . ACL_GROUPS_TABLE . '
1.408 + WHERE forum_id = ' . $src_forum_id;
1.409 + $result = $db->sql_query($sql);
1.410 +
1.411 + while ($row = $db->sql_fetchrow($result))
1.412 + {
1.413 + $row = array(
1.414 + 'group_id' => (int) $row['group_id'],
1.415 + 'auth_option_id' => (int) $row['auth_option_id'],
1.416 + 'auth_role_id' => (int) $row['auth_role_id'],
1.417 + 'auth_setting' => (int) $row['auth_setting'],
1.418 + );
1.419 +
1.420 + foreach ($dest_forum_ids as $dest_forum_id)
1.421 + {
1.422 + $groups_sql_ary[] = $row + array('forum_id' => $dest_forum_id);
1.423 + }
1.424 + }
1.425 + $db->sql_freeresult($result);
1.426 +
1.427 + $db->sql_transaction('begin');
1.428 +
1.429 + // Clear current permissions of destination forums
1.430 + if ($clear_dest_perms)
1.431 + {
1.432 + $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
1.433 + WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
1.434 + $db->sql_query($sql);
1.435 +
1.436 + $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
1.437 + WHERE ' . $db->sql_in_set('forum_id', $dest_forum_ids);
1.438 + $db->sql_query($sql);
1.439 + }
1.440 +
1.441 + $db->sql_multi_insert(ACL_USERS_TABLE, $users_sql_ary);
1.442 + $db->sql_multi_insert(ACL_GROUPS_TABLE, $groups_sql_ary);
1.443 +
1.444 + if ($add_log)
1.445 + {
1.446 + add_log('admin', 'LOG_FORUM_COPIED_PERMISSIONS', $src_forum_name, implode(', ', $dest_forum_names));
1.447 + }
1.448 +
1.449 + $db->sql_transaction('commit');
1.450 +
1.451 + return true;
1.452 +}
1.453 +
1.454 +/**
1.455 +* Get physical file listing
1.456 +*/
1.457 +function filelist($rootdir, $dir = '', $type = 'gif|jpg|jpeg|png')
1.458 +{
1.459 + $matches = array($dir => array());
1.460 +
1.461 + // Remove initial / if present
1.462 + $rootdir = (substr($rootdir, 0, 1) == '/') ? substr($rootdir, 1) : $rootdir;
1.463 + // Add closing / if not present
1.464 + $rootdir = ($rootdir && substr($rootdir, -1) != '/') ? $rootdir . '/' : $rootdir;
1.465 +
1.466 + // Remove initial / if present
1.467 + $dir = (substr($dir, 0, 1) == '/') ? substr($dir, 1) : $dir;
1.468 + // Add closing / if not present
1.469 + $dir = ($dir && substr($dir, -1) != '/') ? $dir . '/' : $dir;
1.470 +
1.471 + if (!is_dir($rootdir . $dir))
1.472 + {
1.473 + return $matches;
1.474 + }
1.475 +
1.476 + $dh = @opendir($rootdir . $dir);
1.477 +
1.478 + if (!$dh)
1.479 + {
1.480 + return $matches;
1.481 + }
1.482 +
1.483 + while (($fname = readdir($dh)) !== false)
1.484 + {
1.485 + if (is_file("$rootdir$dir$fname"))
1.486 + {
1.487 + if (filesize("$rootdir$dir$fname") && preg_match('#\.' . $type . '$#i', $fname))
1.488 + {
1.489 + $matches[$dir][] = $fname;
1.490 + }
1.491 + }
1.492 + else if ($fname[0] != '.' && is_dir("$rootdir$dir$fname"))
1.493 + {
1.494 + $matches += filelist($rootdir, "$dir$fname", $type);
1.495 + }
1.496 + }
1.497 + closedir($dh);
1.498 +
1.499 + return $matches;
1.500 +}
1.501 +
1.502 +/**
1.503 +* Move topic(s)
1.504 +*/
1.505 +function move_topics($topic_ids, $forum_id, $auto_sync = true)
1.506 +{
1.507 + global $db;
1.508 +
1.509 + if (empty($topic_ids))
1.510 + {
1.511 + return;
1.512 + }
1.513 +
1.514 + $forum_ids = array($forum_id);
1.515 +
1.516 + if (!is_array($topic_ids))
1.517 + {
1.518 + $topic_ids = array($topic_ids);
1.519 + }
1.520 +
1.521 + $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1.522 + WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids) . '
1.523 + AND forum_id = ' . $forum_id;
1.524 + $db->sql_query($sql);
1.525 +
1.526 + if ($auto_sync)
1.527 + {
1.528 + $sql = 'SELECT DISTINCT forum_id
1.529 + FROM ' . TOPICS_TABLE . '
1.530 + WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1.531 + $result = $db->sql_query($sql);
1.532 +
1.533 + while ($row = $db->sql_fetchrow($result))
1.534 + {
1.535 + $forum_ids[] = $row['forum_id'];
1.536 + }
1.537 + $db->sql_freeresult($result);
1.538 + }
1.539 +
1.540 + $table_ary = array(TOPICS_TABLE, POSTS_TABLE, LOG_TABLE, DRAFTS_TABLE, TOPICS_TRACK_TABLE);
1.541 + foreach ($table_ary as $table)
1.542 + {
1.543 + $sql = "UPDATE $table
1.544 + SET forum_id = $forum_id
1.545 + WHERE " . $db->sql_in_set('topic_id', $topic_ids);
1.546 + $db->sql_query($sql);
1.547 + }
1.548 + unset($table_ary);
1.549 +
1.550 + if ($auto_sync)
1.551 + {
1.552 + sync('forum', 'forum_id', $forum_ids, true, true);
1.553 + unset($forum_ids);
1.554 + }
1.555 +}
1.556 +
1.557 +/**
1.558 +* Move post(s)
1.559 +*/
1.560 +function move_posts($post_ids, $topic_id, $auto_sync = true)
1.561 +{
1.562 + global $db;
1.563 +
1.564 + if (!is_array($post_ids))
1.565 + {
1.566 + $post_ids = array($post_ids);
1.567 + }
1.568 +
1.569 + $forum_ids = array();
1.570 + $topic_ids = array($topic_id);
1.571 +
1.572 + $sql = 'SELECT DISTINCT topic_id, forum_id
1.573 + FROM ' . POSTS_TABLE . '
1.574 + WHERE ' . $db->sql_in_set('post_id', $post_ids);
1.575 + $result = $db->sql_query($sql);
1.576 +
1.577 + while ($row = $db->sql_fetchrow($result))
1.578 + {
1.579 + $forum_ids[] = (int) $row['forum_id'];
1.580 + $topic_ids[] = (int) $row['topic_id'];
1.581 + }
1.582 + $db->sql_freeresult($result);
1.583 +
1.584 + $sql = 'SELECT forum_id
1.585 + FROM ' . TOPICS_TABLE . '
1.586 + WHERE topic_id = ' . $topic_id;
1.587 + $result = $db->sql_query($sql);
1.588 + $forum_row = $db->sql_fetchrow($result);
1.589 + $db->sql_freeresult($result);
1.590 +
1.591 + if (!$forum_row)
1.592 + {
1.593 + trigger_error('NO_TOPIC');
1.594 + }
1.595 +
1.596 + $sql = 'UPDATE ' . POSTS_TABLE . '
1.597 + SET forum_id = ' . (int) $forum_row['forum_id'] . ", topic_id = $topic_id
1.598 + WHERE " . $db->sql_in_set('post_id', $post_ids);
1.599 + $db->sql_query($sql);
1.600 +
1.601 + $sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
1.602 + SET topic_id = $topic_id, in_message = 0
1.603 + WHERE " . $db->sql_in_set('post_msg_id', $post_ids);
1.604 + $db->sql_query($sql);
1.605 +
1.606 + if ($auto_sync)
1.607 + {
1.608 + $forum_ids[] = (int) $forum_row['forum_id'];
1.609 +
1.610 + sync('topic_reported', 'topic_id', $topic_ids);
1.611 + sync('topic_attachment', 'topic_id', $topic_ids);
1.612 + sync('topic', 'topic_id', $topic_ids, true);
1.613 + sync('forum', 'forum_id', $forum_ids, true, true);
1.614 + }
1.615 +
1.616 + // Update posted information
1.617 + update_posted_info($topic_ids);
1.618 +}
1.619 +
1.620 +/**
1.621 +* Remove topic(s)
1.622 +*/
1.623 +function delete_topics($where_type, $where_ids, $auto_sync = true, $post_count_sync = true, $call_delete_posts = true)
1.624 +{
1.625 + global $db, $config;
1.626 +
1.627 + $approved_topics = 0;
1.628 + $forum_ids = $topic_ids = array();
1.629 +
1.630 + if ($where_type === 'range')
1.631 + {
1.632 + $where_clause = $where_ids;
1.633 + }
1.634 + else
1.635 + {
1.636 + $where_ids = (is_array($where_ids)) ? array_unique($where_ids) : array($where_ids);
1.637 +
1.638 + if (!sizeof($where_ids))
1.639 + {
1.640 + return array('topics' => 0, 'posts' => 0);
1.641 + }
1.642 +
1.643 + $where_clause = $db->sql_in_set($where_type, $where_ids);
1.644 + }
1.645 +
1.646 + // Making sure that delete_posts does not call delete_topics again...
1.647 + $return = array(
1.648 + 'posts' => ($call_delete_posts) ? delete_posts($where_type, $where_ids, false, true, $post_count_sync, false) : 0,
1.649 + );
1.650 +
1.651 + $sql = 'SELECT topic_id, forum_id, topic_approved, topic_moved_id
1.652 + FROM ' . TOPICS_TABLE . '
1.653 + WHERE ' . $where_clause;
1.654 + $result = $db->sql_query($sql);
1.655 +
1.656 + while ($row = $db->sql_fetchrow($result))
1.657 + {
1.658 + $forum_ids[] = $row['forum_id'];
1.659 + $topic_ids[] = $row['topic_id'];
1.660 +
1.661 + if ($row['topic_approved'] && !$row['topic_moved_id'])
1.662 + {
1.663 + $approved_topics++;
1.664 + }
1.665 + }
1.666 + $db->sql_freeresult($result);
1.667 +
1.668 + $return['topics'] = sizeof($topic_ids);
1.669 +
1.670 + if (!sizeof($topic_ids))
1.671 + {
1.672 + return $return;
1.673 + }
1.674 +
1.675 + $db->sql_transaction('begin');
1.676 +
1.677 + $table_ary = array(BOOKMARKS_TABLE, TOPICS_TRACK_TABLE, TOPICS_POSTED_TABLE, POLL_VOTES_TABLE, POLL_OPTIONS_TABLE, TOPICS_WATCH_TABLE, TOPICS_TABLE);
1.678 +
1.679 + foreach ($table_ary as $table)
1.680 + {
1.681 + $sql = "DELETE FROM $table
1.682 + WHERE " . $db->sql_in_set('topic_id', $topic_ids);
1.683 + $db->sql_query($sql);
1.684 + }
1.685 + unset($table_ary);
1.686 +
1.687 + $moved_topic_ids = array();
1.688 +
1.689 + // update the other forums
1.690 + $sql = 'SELECT topic_id, forum_id
1.691 + FROM ' . TOPICS_TABLE . '
1.692 + WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids);
1.693 + $result = $db->sql_query($sql);
1.694 +
1.695 + while ($row = $db->sql_fetchrow($result))
1.696 + {
1.697 + $forum_ids[] = $row['forum_id'];
1.698 + $moved_topic_ids[] = $row['topic_id'];
1.699 + }
1.700 + $db->sql_freeresult($result);
1.701 +
1.702 + if (sizeof($moved_topic_ids))
1.703 + {
1.704 + $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1.705 + WHERE ' . $db->sql_in_set('topic_id', $moved_topic_ids);
1.706 + $db->sql_query($sql);
1.707 + }
1.708 +
1.709 + $db->sql_transaction('commit');
1.710 +
1.711 + if ($auto_sync)
1.712 + {
1.713 + sync('forum', 'forum_id', array_unique($forum_ids), true, true);
1.714 + sync('topic_reported', $where_type, $where_ids);
1.715 + }
1.716 +
1.717 + if ($approved_topics)
1.718 + {
1.719 + set_config_count('num_topics', $approved_topics * (-1), true);
1.720 + }
1.721 +
1.722 + return $return;
1.723 +}
1.724 +
1.725 +/**
1.726 +* Remove post(s)
1.727 +*/
1.728 +function delete_posts($where_type, $where_ids, $auto_sync = true, $posted_sync = true, $post_count_sync = true, $call_delete_topics = true)
1.729 +{
1.730 + global $db, $config, $phpbb_root_path, $phpEx;
1.731 +
1.732 + if ($where_type === 'range')
1.733 + {
1.734 + $where_clause = $where_ids;
1.735 + }
1.736 + else
1.737 + {
1.738 + if (is_array($where_ids))
1.739 + {
1.740 + $where_ids = array_unique($where_ids);
1.741 + }
1.742 + else
1.743 + {
1.744 + $where_ids = array($where_ids);
1.745 + }
1.746 +
1.747 + if (!sizeof($where_ids))
1.748 + {
1.749 + return false;
1.750 + }
1.751 +
1.752 + $where_ids = array_map('intval', $where_ids);
1.753 +
1.754 +/* Possible code for splitting post deletion
1.755 + if (sizeof($where_ids) >= 1001)
1.756 + {
1.757 + // Split into chunks of 1000
1.758 + $chunks = array_chunk($where_ids, 1000);
1.759 +
1.760 + foreach ($chunks as $_where_ids)
1.761 + {
1.762 + delete_posts($where_type, $_where_ids, $auto_sync, $posted_sync, $post_count_sync, $call_delete_topics);
1.763 + }
1.764 +
1.765 + return;
1.766 + }*/
1.767 +
1.768 + $where_clause = $db->sql_in_set($where_type, $where_ids);
1.769 + }
1.770 +
1.771 + $approved_posts = 0;
1.772 + $post_ids = $topic_ids = $forum_ids = $post_counts = $remove_topics = array();
1.773 +
1.774 + $sql = 'SELECT post_id, poster_id, post_approved, post_postcount, topic_id, forum_id
1.775 + FROM ' . POSTS_TABLE . '
1.776 + WHERE ' . $where_clause;
1.777 + $result = $db->sql_query($sql);
1.778 +
1.779 + while ($row = $db->sql_fetchrow($result))
1.780 + {
1.781 + $post_ids[] = (int) $row['post_id'];
1.782 + $poster_ids[] = (int) $row['poster_id'];
1.783 + $topic_ids[] = (int) $row['topic_id'];
1.784 + $forum_ids[] = (int) $row['forum_id'];
1.785 +
1.786 + if ($row['post_postcount'] && $post_count_sync && $row['post_approved'])
1.787 + {
1.788 + $post_counts[$row['poster_id']] = (!empty($post_counts[$row['poster_id']])) ? $post_counts[$row['poster_id']] + 1 : 1;
1.789 + }
1.790 +
1.791 + if ($row['post_approved'])
1.792 + {
1.793 + $approved_posts++;
1.794 + }
1.795 + }
1.796 + $db->sql_freeresult($result);
1.797 +
1.798 + if (!sizeof($post_ids))
1.799 + {
1.800 + return false;
1.801 + }
1.802 +
1.803 + $db->sql_transaction('begin');
1.804 +
1.805 + $table_ary = array(POSTS_TABLE, REPORTS_TABLE);
1.806 +
1.807 + foreach ($table_ary as $table)
1.808 + {
1.809 + $sql = "DELETE FROM $table
1.810 + WHERE " . $db->sql_in_set('post_id', $post_ids);
1.811 + $db->sql_query($sql);
1.812 + }
1.813 + unset($table_ary);
1.814 +
1.815 + // Adjust users post counts
1.816 + if (sizeof($post_counts) && $post_count_sync)
1.817 + {
1.818 + foreach ($post_counts as $poster_id => $substract)
1.819 + {
1.820 + $sql = 'UPDATE ' . USERS_TABLE . '
1.821 + SET user_posts = 0
1.822 + WHERE user_id = ' . $poster_id . '
1.823 + AND user_posts < ' . $substract;
1.824 + $db->sql_query($sql);
1.825 +
1.826 + $sql = 'UPDATE ' . USERS_TABLE . '
1.827 + SET user_posts = user_posts - ' . $substract . '
1.828 + WHERE user_id = ' . $poster_id . '
1.829 + AND user_posts >= ' . $substract;
1.830 + $db->sql_query($sql);
1.831 + }
1.832 + }
1.833 +
1.834 + // Remove topics now having no posts?
1.835 + if (sizeof($topic_ids))
1.836 + {
1.837 + $sql = 'SELECT topic_id
1.838 + FROM ' . POSTS_TABLE . '
1.839 + WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
1.840 + GROUP BY topic_id';
1.841 + $result = $db->sql_query($sql);
1.842 +
1.843 + while ($row = $db->sql_fetchrow($result))
1.844 + {
1.845 + $remove_topics[] = $row['topic_id'];
1.846 + }
1.847 + $db->sql_freeresult($result);
1.848 +
1.849 + // Actually, those not within remove_topics should be removed. ;)
1.850 + $remove_topics = array_diff($topic_ids, $remove_topics);
1.851 + }
1.852 +
1.853 + // Remove the message from the search index
1.854 + $search_type = basename($config['search_type']);
1.855 +
1.856 + if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
1.857 + {
1.858 + trigger_error('NO_SUCH_SEARCH_MODULE');
1.859 + }
1.860 +
1.861 + include_once("{$phpbb_root_path}includes/search/$search_type.$phpEx");
1.862 +
1.863 + $error = false;
1.864 + $search = new $search_type($error);
1.865 +
1.866 + if ($error)
1.867 + {
1.868 + trigger_error($error);
1.869 + }
1.870 +
1.871 + $search->index_remove($post_ids, $poster_ids, $forum_ids);
1.872 +
1.873 + delete_attachments('post', $post_ids, false);
1.874 +
1.875 + $db->sql_transaction('commit');
1.876 +
1.877 + // Resync topics_posted table
1.878 + if ($posted_sync)
1.879 + {
1.880 + update_posted_info($topic_ids);
1.881 + }
1.882 +
1.883 + if ($auto_sync)
1.884 + {
1.885 + sync('topic_reported', 'topic_id', $topic_ids);
1.886 + sync('topic', 'topic_id', $topic_ids, true);
1.887 + sync('forum', 'forum_id', $forum_ids, true, true);
1.888 + }
1.889 +
1.890 + if ($approved_posts)
1.891 + {
1.892 + set_config_count('num_posts', $approved_posts * (-1), true);
1.893 + }
1.894 +
1.895 + // We actually remove topics now to not be inconsistent (the delete_topics function calls this function too)
1.896 + if (sizeof($remove_topics) && $call_delete_topics)
1.897 + {
1.898 + delete_topics('topic_id', $remove_topics, $auto_sync, $post_count_sync, false);
1.899 + }
1.900 +
1.901 + return sizeof($post_ids);
1.902 +}
1.903 +
1.904 +/**
1.905 +* Delete Attachments
1.906 +*
1.907 +* @param string $mode can be: post|message|topic|attach|user
1.908 +* @param mixed $ids can be: post_ids, message_ids, topic_ids, attach_ids, user_ids
1.909 +* @param bool $resync set this to false if you are deleting posts or topics
1.910 +*/
1.911 +function delete_attachments($mode, $ids, $resync = true)
1.912 +{
1.913 + global $db, $config;
1.914 +
1.915 + // 0 is as bad as an empty array
1.916 + if (empty($ids))
1.917 + {
1.918 + return false;
1.919 + }
1.920 +
1.921 + if (is_array($ids))
1.922 + {
1.923 + $ids = array_unique($ids);
1.924 + $ids = array_map('intval', $ids);
1.925 + }
1.926 + else
1.927 + {
1.928 + $ids = array((int) $ids);
1.929 + }
1.930 +
1.931 + $sql_where = '';
1.932 +
1.933 + switch ($mode)
1.934 + {
1.935 + case 'post':
1.936 + case 'message':
1.937 + $sql_id = 'post_msg_id';
1.938 + $sql_where = ' AND in_message = ' . ($mode == 'message' ? 1 : 0);
1.939 + break;
1.940 +
1.941 + case 'topic':
1.942 + $sql_id = 'topic_id';
1.943 + break;
1.944 +
1.945 + case 'user':
1.946 + $sql_id = 'poster_id';
1.947 + break;
1.948 +
1.949 + case 'attach':
1.950 + default:
1.951 + $sql_id = 'attach_id';
1.952 + $mode = 'attach';
1.953 + break;
1.954 + }
1.955 +
1.956 + $post_ids = $message_ids = $topic_ids = $physical = array();
1.957 +
1.958 + // Collect post and topic ids for later use if we need to touch remaining entries (if resync is enabled)
1.959 + $sql = 'SELECT post_msg_id, topic_id, in_message, physical_filename, thumbnail, filesize, is_orphan
1.960 + FROM ' . ATTACHMENTS_TABLE . '
1.961 + WHERE ' . $db->sql_in_set($sql_id, $ids);
1.962 +
1.963 + $sql .= $sql_where;
1.964 +
1.965 + $result = $db->sql_query($sql);
1.966 +
1.967 + while ($row = $db->sql_fetchrow($result))
1.968 + {
1.969 + // We only need to store post/message/topic ids if resync is enabled and the file is not orphaned
1.970 + if ($resync && !$row['is_orphan'])
1.971 + {
1.972 + if (!$row['in_message'])
1.973 + {
1.974 + $post_ids[] = $row['post_msg_id'];
1.975 + $topic_ids[] = $row['topic_id'];
1.976 + }
1.977 + else
1.978 + {
1.979 + $message_ids[] = $row['post_msg_id'];
1.980 + }
1.981 + }
1.982 +
1.983 + $physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize'], 'is_orphan' => $row['is_orphan']);
1.984 + }
1.985 + $db->sql_freeresult($result);
1.986 +
1.987 + // Delete attachments
1.988 + $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
1.989 + WHERE ' . $db->sql_in_set($sql_id, $ids);
1.990 +
1.991 + $sql .= $sql_where;
1.992 +
1.993 + $db->sql_query($sql);
1.994 + $num_deleted = $db->sql_affectedrows();
1.995 +
1.996 + if (!$num_deleted)
1.997 + {
1.998 + return 0;
1.999 + }
1.1000 +
1.1001 + // Delete attachments from filesystem
1.1002 + $space_removed = $files_removed = 0;
1.1003 + foreach ($physical as $file_ary)
1.1004 + {
1.1005 + if (phpbb_unlink($file_ary['filename'], 'file', true) && !$file_ary['is_orphan'])
1.1006 + {
1.1007 + // Only non-orphaned files count to the file size
1.1008 + $space_removed += $file_ary['filesize'];
1.1009 + $files_removed++;
1.1010 + }
1.1011 +
1.1012 + if ($file_ary['thumbnail'])
1.1013 + {
1.1014 + phpbb_unlink($file_ary['filename'], 'thumbnail', true);
1.1015 + }
1.1016 + }
1.1017 +
1.1018 + if ($space_removed || $files_removed)
1.1019 + {
1.1020 + set_config_count('upload_dir_size', $space_removed * (-1), true);
1.1021 + set_config_count('num_files', $files_removed * (-1), true);
1.1022 + }
1.1023 +
1.1024 + // If we do not resync, we do not need to adjust any message, post, topic or user entries
1.1025 + if (!$resync)
1.1026 + {
1.1027 + return $num_deleted;
1.1028 + }
1.1029 +
1.1030 + // No more use for the original ids
1.1031 + unset($ids);
1.1032 +
1.1033 + // Now, we need to resync posts, messages, topics. We go through every one of them
1.1034 + $post_ids = array_unique($post_ids);
1.1035 + $message_ids = array_unique($message_ids);
1.1036 + $topic_ids = array_unique($topic_ids);
1.1037 +
1.1038 + // Update post indicators for posts now no longer having attachments
1.1039 + if (sizeof($post_ids))
1.1040 + {
1.1041 + // Just check which posts are still having an assigned attachment not orphaned by querying the attachments table
1.1042 + $sql = 'SELECT post_msg_id
1.1043 + FROM ' . ATTACHMENTS_TABLE . '
1.1044 + WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
1.1045 + AND in_message = 0
1.1046 + AND is_orphan = 0';
1.1047 + $result = $db->sql_query($sql);
1.1048 +
1.1049 + $remaining_ids = array();
1.1050 + while ($row = $db->sql_fetchrow($result))
1.1051 + {
1.1052 + $remaining_ids[] = $row['post_msg_id'];
1.1053 + }
1.1054 + $db->sql_freeresult($result);
1.1055 +
1.1056 + // Now only unset those ids remaining
1.1057 + $post_ids = array_diff($post_ids, $remaining_ids);
1.1058 +
1.1059 + if (sizeof($post_ids))
1.1060 + {
1.1061 + $sql = 'UPDATE ' . POSTS_TABLE . '
1.1062 + SET post_attachment = 0
1.1063 + WHERE ' . $db->sql_in_set('post_id', $post_ids);
1.1064 + $db->sql_query($sql);
1.1065 + }
1.1066 + }
1.1067 +
1.1068 + // Update message table if messages are affected
1.1069 + if (sizeof($message_ids))
1.1070 + {
1.1071 + // Just check which messages are still having an assigned attachment not orphaned by querying the attachments table
1.1072 + $sql = 'SELECT post_msg_id
1.1073 + FROM ' . ATTACHMENTS_TABLE . '
1.1074 + WHERE ' . $db->sql_in_set('post_msg_id', $message_ids) . '
1.1075 + AND in_message = 1
1.1076 + AND is_orphan = 0';
1.1077 + $result = $db->sql_query($sql);
1.1078 +
1.1079 + $remaining_ids = array();
1.1080 + while ($row = $db->sql_fetchrow($result))
1.1081 + {
1.1082 + $remaining_ids[] = $row['post_msg_id'];
1.1083 + }
1.1084 + $db->sql_freeresult($result);
1.1085 +
1.1086 + // Now only unset those ids remaining
1.1087 + $message_ids = array_diff($message_ids, $remaining_ids);
1.1088 +
1.1089 + if (sizeof($message_ids))
1.1090 + {
1.1091 + $sql = 'UPDATE ' . PRIVMSGS_TABLE . '
1.1092 + SET message_attachment = 0
1.1093 + WHERE ' . $db->sql_in_set('msg_id', $message_ids);
1.1094 + $db->sql_query($sql);
1.1095 + }
1.1096 + }
1.1097 +
1.1098 + // Now update the topics. This is a bit trickier, because there could be posts still having attachments within the topic
1.1099 + if (sizeof($topic_ids))
1.1100 + {
1.1101 + // Just check which topics are still having an assigned attachment not orphaned by querying the attachments table (much less entries expected)
1.1102 + $sql = 'SELECT topic_id
1.1103 + FROM ' . ATTACHMENTS_TABLE . '
1.1104 + WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
1.1105 + AND is_orphan = 0';
1.1106 + $result = $db->sql_query($sql);
1.1107 +
1.1108 + $remaining_ids = array();
1.1109 + while ($row = $db->sql_fetchrow($result))
1.1110 + {
1.1111 + $remaining_ids[] = $row['topic_id'];
1.1112 + }
1.1113 + $db->sql_freeresult($result);
1.1114 +
1.1115 + // Now only unset those ids remaining
1.1116 + $topic_ids = array_diff($topic_ids, $remaining_ids);
1.1117 +
1.1118 + if (sizeof($topic_ids))
1.1119 + {
1.1120 + $sql = 'UPDATE ' . TOPICS_TABLE . '
1.1121 + SET topic_attachment = 0
1.1122 + WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1.1123 + $db->sql_query($sql);
1.1124 + }
1.1125 + }
1.1126 +
1.1127 + return $num_deleted;
1.1128 +}
1.1129 +
1.1130 +/**
1.1131 +* Deletes shadow topics pointing to a specified forum.
1.1132 +*
1.1133 +* @param int $forum_id The forum id
1.1134 +* @param string $sql_more Additional WHERE statement, e.g. t.topic_time < (time() - 1234)
1.1135 +* @param bool $auto_sync Will call sync() if this is true
1.1136 +*
1.1137 +* @return array Array with affected forums
1.1138 +*
1.1139 +* @author bantu
1.1140 +*/
1.1141 +function delete_topic_shadows($forum_id, $sql_more = '', $auto_sync = true)
1.1142 +{
1.1143 + global $db;
1.1144 +
1.1145 + if (!$forum_id)
1.1146 + {
1.1147 + // Nothing to do.
1.1148 + return;
1.1149 + }
1.1150 +
1.1151 + // Set of affected forums we have to resync
1.1152 + $sync_forum_ids = array();
1.1153 +
1.1154 + // Amount of topics we select and delete at once.
1.1155 + $batch_size = 500;
1.1156 +
1.1157 + do
1.1158 + {
1.1159 + $sql = 'SELECT t2.forum_id, t2.topic_id
1.1160 + FROM ' . TOPICS_TABLE . ' t2, ' . TOPICS_TABLE . ' t
1.1161 + WHERE t2.topic_moved_id = t.topic_id
1.1162 + AND t.forum_id = ' . (int) $forum_id . '
1.1163 + ' . (($sql_more) ? 'AND ' . $sql_more : '');
1.1164 + $result = $db->sql_query_limit($sql, $batch_size);
1.1165 +
1.1166 + $topic_ids = array();
1.1167 + while ($row = $db->sql_fetchrow($result))
1.1168 + {
1.1169 + $topic_ids[] = (int) $row['topic_id'];
1.1170 +
1.1171 + $sync_forum_ids[(int) $row['forum_id']] = (int) $row['forum_id'];
1.1172 + }
1.1173 + $db->sql_freeresult($result);
1.1174 +
1.1175 + if (!empty($topic_ids))
1.1176 + {
1.1177 + $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1.1178 + WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1.1179 + $db->sql_query($sql);
1.1180 + }
1.1181 + }
1.1182 + while (sizeof($topic_ids) == $batch_size);
1.1183 +
1.1184 + if ($auto_sync)
1.1185 + {
1.1186 + sync('forum', 'forum_id', $sync_forum_ids, true, true);
1.1187 + }
1.1188 +
1.1189 + return $sync_forum_ids;
1.1190 +}
1.1191 +
1.1192 +/**
1.1193 +* Update/Sync posted information for topics
1.1194 +*/
1.1195 +function update_posted_info(&$topic_ids)
1.1196 +{
1.1197 + global $db, $config;
1.1198 +
1.1199 + if (empty($topic_ids) || !$config['load_db_track'])
1.1200 + {
1.1201 + return;
1.1202 + }
1.1203 +
1.1204 + // First of all, let us remove any posted information for these topics
1.1205 + $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
1.1206 + WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1.1207 + $db->sql_query($sql);
1.1208 +
1.1209 + // Now, let us collect the user/topic combos for rebuilding the information
1.1210 + $sql = 'SELECT poster_id, topic_id
1.1211 + FROM ' . POSTS_TABLE . '
1.1212 + WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
1.1213 + AND poster_id <> ' . ANONYMOUS . '
1.1214 + GROUP BY poster_id, topic_id';
1.1215 + $result = $db->sql_query($sql);
1.1216 +
1.1217 + $posted = array();
1.1218 + while ($row = $db->sql_fetchrow($result))
1.1219 + {
1.1220 + // Add as key to make them unique (grouping by) and circumvent empty keys on array_unique
1.1221 + $posted[$row['poster_id']][] = $row['topic_id'];
1.1222 + }
1.1223 + $db->sql_freeresult($result);
1.1224 +
1.1225 + // Now add the information...
1.1226 + $sql_ary = array();
1.1227 + foreach ($posted as $user_id => $topic_row)
1.1228 + {
1.1229 + foreach ($topic_row as $topic_id)
1.1230 + {
1.1231 + $sql_ary[] = array(
1.1232 + 'user_id' => (int) $user_id,
1.1233 + 'topic_id' => (int) $topic_id,
1.1234 + 'topic_posted' => 1,
1.1235 + );
1.1236 + }
1.1237 + }
1.1238 + unset($posted);
1.1239 +
1.1240 + $db->sql_multi_insert(TOPICS_POSTED_TABLE, $sql_ary);
1.1241 +}
1.1242 +
1.1243 +/**
1.1244 +* Delete attached file
1.1245 +*/
1.1246 +function phpbb_unlink($filename, $mode = 'file', $entry_removed = false)
1.1247 +{
1.1248 + global $db, $phpbb_root_path, $config;
1.1249 +
1.1250 + // Because of copying topics or modifications a physical filename could be assigned more than once. If so, do not remove the file itself.
1.1251 + $sql = 'SELECT COUNT(attach_id) AS num_entries
1.1252 + FROM ' . ATTACHMENTS_TABLE . "
1.1253 + WHERE physical_filename = '" . $db->sql_escape(utf8_basename($filename)) . "'";
1.1254 + $result = $db->sql_query($sql);
1.1255 + $num_entries = (int) $db->sql_fetchfield('num_entries');
1.1256 + $db->sql_freeresult($result);
1.1257 +
1.1258 + // Do not remove file if at least one additional entry with the same name exist.
1.1259 + if (($entry_removed && $num_entries > 0) || (!$entry_removed && $num_entries > 1))
1.1260 + {
1.1261 + return false;
1.1262 + }
1.1263 +
1.1264 + $filename = ($mode == 'thumbnail') ? 'thumb_' . utf8_basename($filename) : utf8_basename($filename);
1.1265 + return @unlink($phpbb_root_path . $config['upload_path'] . '/' . $filename);
1.1266 +}
1.1267 +
1.1268 +/**
1.1269 +* All-encompasing sync function
1.1270 +*
1.1271 +* Exaples:
1.1272 +* <code>
1.1273 +* sync('topic', 'topic_id', 123); // resync topic #123
1.1274 +* sync('topic', 'forum_id', array(2, 3)); // resync topics from forum #2 and #3
1.1275 +* sync('topic'); // resync all topics
1.1276 +* sync('topic', 'range', 'topic_id BETWEEN 1 AND 60'); // resync a range of topics/forums (only available for 'topic' and 'forum' modes)
1.1277 +* </code>
1.1278 +*
1.1279 +* Modes:
1.1280 +* - forum Resync complete forum
1.1281 +* - topic Resync topics
1.1282 +* - topic_moved Removes topic shadows that would be in the same forum as the topic they link to
1.1283 +* - topic_approved Resyncs the topic_approved flag according to the status of the first post
1.1284 +* - post_reported Resyncs the post_reported flag, relying on actual reports
1.1285 +* - topic_reported Resyncs the topic_reported flag, relying on post_reported flags
1.1286 +* - post_attachement Same as post_reported, but with attachment flags
1.1287 +* - topic_attachement Same as topic_reported, but with attachment flags
1.1288 +*/
1.1289 +function sync($mode, $where_type = '', $where_ids = '', $resync_parents = false, $sync_extra = false)
1.1290 +{
1.1291 + global $db;
1.1292 +
1.1293 + if (is_array($where_ids))
1.1294 + {
1.1295 + $where_ids = array_unique($where_ids);
1.1296 + $where_ids = array_map('intval', $where_ids);
1.1297 + }
1.1298 + else if ($where_type != 'range')
1.1299 + {
1.1300 + $where_ids = ($where_ids) ? array((int) $where_ids) : array();
1.1301 + }
1.1302 +
1.1303 + if ($mode == 'forum' || $mode == 'topic' || $mode == 'topic_approved' || $mode == 'topic_reported' || $mode == 'post_reported')
1.1304 + {
1.1305 + if (!$where_type)
1.1306 + {
1.1307 + $where_sql = '';
1.1308 + $where_sql_and = 'WHERE';
1.1309 + }
1.1310 + else if ($where_type == 'range')
1.1311 + {
1.1312 + // Only check a range of topics/forums. For instance: 'topic_id BETWEEN 1 AND 60'
1.1313 + $where_sql = 'WHERE (' . $mode[0] . ".$where_ids)";
1.1314 + $where_sql_and = $where_sql . "\n\tAND";
1.1315 + }
1.1316 + else
1.1317 + {
1.1318 + // Do not sync the "global forum"
1.1319 + $where_ids = array_diff($where_ids, array(0));
1.1320 +
1.1321 + if (!sizeof($where_ids))
1.1322 + {
1.1323 + // Empty array with IDs. This means that we don't have any work to do. Just return.
1.1324 + return;
1.1325 + }
1.1326 +
1.1327 + // Limit the topics/forums we are syncing, use specific topic/forum IDs.
1.1328 + // $where_type contains the field for the where clause (forum_id, topic_id)
1.1329 + $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
1.1330 + $where_sql_and = $where_sql . "\n\tAND";
1.1331 + }
1.1332 + }
1.1333 + else
1.1334 + {
1.1335 + if (!sizeof($where_ids))
1.1336 + {
1.1337 + return;
1.1338 + }
1.1339 +
1.1340 + // $where_type contains the field for the where clause (forum_id, topic_id)
1.1341 + $where_sql = 'WHERE ' . $db->sql_in_set($mode[0] . '.' . $where_type, $where_ids);
1.1342 + $where_sql_and = $where_sql . "\n\tAND";
1.1343 + }
1.1344 +
1.1345 + switch ($mode)
1.1346 + {
1.1347 + case 'topic_moved':
1.1348 + $db->sql_transaction('begin');
1.1349 + switch ($db->sql_layer)
1.1350 + {
1.1351 + case 'mysql4':
1.1352 + case 'mysqli':
1.1353 + $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1.1354 + USING ' . TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
1.1355 + WHERE t1.topic_moved_id = t2.topic_id
1.1356 + AND t1.forum_id = t2.forum_id";
1.1357 + $db->sql_query($sql);
1.1358 + break;
1.1359 +
1.1360 + default:
1.1361 + $sql = 'SELECT t1.topic_id
1.1362 + FROM ' .TOPICS_TABLE . ' t1, ' . TOPICS_TABLE . " t2
1.1363 + WHERE t1.topic_moved_id = t2.topic_id
1.1364 + AND t1.forum_id = t2.forum_id";
1.1365 + $result = $db->sql_query($sql);
1.1366 +
1.1367 + $topic_id_ary = array();
1.1368 + while ($row = $db->sql_fetchrow($result))
1.1369 + {
1.1370 + $topic_id_ary[] = $row['topic_id'];
1.1371 + }
1.1372 + $db->sql_freeresult($result);
1.1373 +
1.1374 + if (!sizeof($topic_id_ary))
1.1375 + {
1.1376 + return;
1.1377 + }
1.1378 +
1.1379 + $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1.1380 + WHERE ' . $db->sql_in_set('topic_id', $topic_id_ary);
1.1381 + $db->sql_query($sql);
1.1382 +
1.1383 + break;
1.1384 + }
1.1385 +
1.1386 + $db->sql_transaction('commit');
1.1387 + break;
1.1388 +
1.1389 + case 'topic_approved':
1.1390 +
1.1391 + $db->sql_transaction('begin');
1.1392 + switch ($db->sql_layer)
1.1393 + {
1.1394 + case 'mysql4':
1.1395 + case 'mysqli':
1.1396 + $sql = 'UPDATE ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
1.1397 + SET t.topic_approved = p.post_approved
1.1398 + $where_sql_and t.topic_first_post_id = p.post_id";
1.1399 + $db->sql_query($sql);
1.1400 + break;
1.1401 +
1.1402 + default:
1.1403 + $sql = 'SELECT t.topic_id, p.post_approved
1.1404 + FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
1.1405 + $where_sql_and p.post_id = t.topic_first_post_id
1.1406 + AND p.post_approved <> t.topic_approved";
1.1407 + $result = $db->sql_query($sql);
1.1408 +
1.1409 + $topic_ids = array();
1.1410 + while ($row = $db->sql_fetchrow($result))
1.1411 + {
1.1412 + $topic_ids[] = $row['topic_id'];
1.1413 + }
1.1414 + $db->sql_freeresult($result);
1.1415 +
1.1416 + if (!sizeof($topic_ids))
1.1417 + {
1.1418 + return;
1.1419 + }
1.1420 +
1.1421 + $sql = 'UPDATE ' . TOPICS_TABLE . '
1.1422 + SET topic_approved = 1 - topic_approved
1.1423 + WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1.1424 + $db->sql_query($sql);
1.1425 + break;
1.1426 + }
1.1427 +
1.1428 + $db->sql_transaction('commit');
1.1429 + break;
1.1430 +
1.1431 + case 'post_reported':
1.1432 + $post_ids = $post_reported = array();
1.1433 +
1.1434 + $db->sql_transaction('begin');
1.1435 +
1.1436 + $sql = 'SELECT p.post_id, p.post_reported
1.1437 + FROM ' . POSTS_TABLE . " p
1.1438 + $where_sql
1.1439 + GROUP BY p.post_id, p.post_reported";
1.1440 + $result = $db->sql_query($sql);
1.1441 +
1.1442 + while ($row = $db->sql_fetchrow($result))
1.1443 + {
1.1444 + $post_ids[$row['post_id']] = $row['post_id'];
1.1445 + if ($row['post_reported'])
1.1446 + {
1.1447 + $post_reported[$row['post_id']] = 1;
1.1448 + }
1.1449 + }
1.1450 + $db->sql_freeresult($result);
1.1451 +
1.1452 + $sql = 'SELECT DISTINCT(post_id)
1.1453 + FROM ' . REPORTS_TABLE . '
1.1454 + WHERE ' . $db->sql_in_set('post_id', $post_ids) . '
1.1455 + AND report_closed = 0';
1.1456 + $result = $db->sql_query($sql);
1.1457 +
1.1458 + $post_ids = array();
1.1459 + while ($row = $db->sql_fetchrow($result))
1.1460 + {
1.1461 + if (!isset($post_reported[$row['post_id']]))
1.1462 + {
1.1463 + $post_ids[] = $row['post_id'];
1.1464 + }
1.1465 + else
1.1466 + {
1.1467 + unset($post_reported[$row['post_id']]);
1.1468 + }
1.1469 + }
1.1470 + $db->sql_freeresult($result);
1.1471 +
1.1472 + // $post_reported should be empty by now, if it's not it contains
1.1473 + // posts that are falsely flagged as reported
1.1474 + foreach ($post_reported as $post_id => $void)
1.1475 + {
1.1476 + $post_ids[] = $post_id;
1.1477 + }
1.1478 +
1.1479 + if (sizeof($post_ids))
1.1480 + {
1.1481 + $sql = 'UPDATE ' . POSTS_TABLE . '
1.1482 + SET post_reported = 1 - post_reported
1.1483 + WHERE ' . $db->sql_in_set('post_id', $post_ids);
1.1484 + $db->sql_query($sql);
1.1485 + }
1.1486 +
1.1487 + $db->sql_transaction('commit');
1.1488 + break;
1.1489 +
1.1490 + case 'topic_reported':
1.1491 + if ($sync_extra)
1.1492 + {
1.1493 + sync('post_reported', $where_type, $where_ids);
1.1494 + }
1.1495 +
1.1496 + $topic_ids = $topic_reported = array();
1.1497 +
1.1498 + $db->sql_transaction('begin');
1.1499 +
1.1500 + $sql = 'SELECT DISTINCT(t.topic_id)
1.1501 + FROM ' . POSTS_TABLE . " t
1.1502 + $where_sql_and t.post_reported = 1";
1.1503 + $result = $db->sql_query($sql);
1.1504 +
1.1505 + while ($row = $db->sql_fetchrow($result))
1.1506 + {
1.1507 + $topic_reported[$row['topic_id']] = 1;
1.1508 + }
1.1509 + $db->sql_freeresult($result);
1.1510 +
1.1511 + $sql = 'SELECT t.topic_id, t.topic_reported
1.1512 + FROM ' . TOPICS_TABLE . " t
1.1513 + $where_sql";
1.1514 + $result = $db->sql_query($sql);
1.1515 +
1.1516 + while ($row = $db->sql_fetchrow($result))
1.1517 + {
1.1518 + if ($row['topic_reported'] ^ isset($topic_reported[$row['topic_id']]))
1.1519 + {
1.1520 + $topic_ids[] = $row['topic_id'];
1.1521 + }
1.1522 + }
1.1523 + $db->sql_freeresult($result);
1.1524 +
1.1525 + if (sizeof($topic_ids))
1.1526 + {
1.1527 + $sql = 'UPDATE ' . TOPICS_TABLE . '
1.1528 + SET topic_reported = 1 - topic_reported
1.1529 + WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1.1530 + $db->sql_query($sql);
1.1531 + }
1.1532 +
1.1533 + $db->sql_transaction('commit');
1.1534 + break;
1.1535 +
1.1536 + case 'post_attachment':
1.1537 + $post_ids = $post_attachment = array();
1.1538 +
1.1539 + $db->sql_transaction('begin');
1.1540 +
1.1541 + $sql = 'SELECT p.post_id, p.post_attachment
1.1542 + FROM ' . POSTS_TABLE . " p
1.1543 + $where_sql
1.1544 + GROUP BY p.post_id, p.post_attachment";
1.1545 + $result = $db->sql_query($sql);
1.1546 +
1.1547 + while ($row = $db->sql_fetchrow($result))
1.1548 + {
1.1549 + $post_ids[$row['post_id']] = $row['post_id'];
1.1550 + if ($row['post_attachment'])
1.1551 + {
1.1552 + $post_attachment[$row['post_id']] = 1;
1.1553 + }
1.1554 + }
1.1555 + $db->sql_freeresult($result);
1.1556 +
1.1557 + $sql = 'SELECT DISTINCT(post_msg_id)
1.1558 + FROM ' . ATTACHMENTS_TABLE . '
1.1559 + WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
1.1560 + AND in_message = 0';
1.1561 + $result = $db->sql_query($sql);
1.1562 +
1.1563 + $post_ids = array();
1.1564 + while ($row = $db->sql_fetchrow($result))
1.1565 + {
1.1566 + if (!isset($post_attachment[$row['post_msg_id']]))
1.1567 + {
1.1568 + $post_ids[] = $row['post_msg_id'];
1.1569 + }
1.1570 + else
1.1571 + {
1.1572 + unset($post_attachment[$row['post_msg_id']]);
1.1573 + }
1.1574 + }
1.1575 + $db->sql_freeresult($result);
1.1576 +
1.1577 + // $post_attachment should be empty by now, if it's not it contains
1.1578 + // posts that are falsely flagged as having attachments
1.1579 + foreach ($post_attachment as $post_id => $void)
1.1580 + {
1.1581 + $post_ids[] = $post_id;
1.1582 + }
1.1583 +
1.1584 + if (sizeof($post_ids))
1.1585 + {
1.1586 + $sql = 'UPDATE ' . POSTS_TABLE . '
1.1587 + SET post_attachment = 1 - post_attachment
1.1588 + WHERE ' . $db->sql_in_set('post_id', $post_ids);
1.1589 + $db->sql_query($sql);
1.1590 + }
1.1591 +
1.1592 + $db->sql_transaction('commit');
1.1593 + break;
1.1594 +
1.1595 + case 'topic_attachment':
1.1596 + if ($sync_extra)
1.1597 + {
1.1598 + sync('post_attachment', $where_type, $where_ids);
1.1599 + }
1.1600 +
1.1601 + $topic_ids = $topic_attachment = array();
1.1602 +
1.1603 + $db->sql_transaction('begin');
1.1604 +
1.1605 + $sql = 'SELECT DISTINCT(t.topic_id)
1.1606 + FROM ' . POSTS_TABLE . " t
1.1607 + $where_sql_and t.post_attachment = 1";
1.1608 + $result = $db->sql_query($sql);
1.1609 +
1.1610 + while ($row = $db->sql_fetchrow($result))
1.1611 + {
1.1612 + $topic_attachment[$row['topic_id']] = 1;
1.1613 + }
1.1614 + $db->sql_freeresult($result);
1.1615 +
1.1616 + $sql = 'SELECT t.topic_id, t.topic_attachment
1.1617 + FROM ' . TOPICS_TABLE . " t
1.1618 + $where_sql";
1.1619 + $result = $db->sql_query($sql);
1.1620 +
1.1621 + while ($row = $db->sql_fetchrow($result))
1.1622 + {
1.1623 + if ($row['topic_attachment'] ^ isset($topic_attachment[$row['topic_id']]))
1.1624 + {
1.1625 + $topic_ids[] = $row['topic_id'];
1.1626 + }
1.1627 + }
1.1628 + $db->sql_freeresult($result);
1.1629 +
1.1630 + if (sizeof($topic_ids))
1.1631 + {
1.1632 + $sql = 'UPDATE ' . TOPICS_TABLE . '
1.1633 + SET topic_attachment = 1 - topic_attachment
1.1634 + WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
1.1635 + $db->sql_query($sql);
1.1636 + }
1.1637 +
1.1638 + $db->sql_transaction('commit');
1.1639 +
1.1640 + break;
1.1641 +
1.1642 + case 'forum':
1.1643 +
1.1644 + $db->sql_transaction('begin');
1.1645 +
1.1646 + // 1: Get the list of all forums
1.1647 + $sql = 'SELECT f.*
1.1648 + FROM ' . FORUMS_TABLE . " f
1.1649 + $where_sql";
1.1650 + $result = $db->sql_query($sql);
1.1651 +
1.1652 + $forum_data = $forum_ids = $post_ids = $last_post_id = $post_info = array();
1.1653 + while ($row = $db->sql_fetchrow($result))
1.1654 + {
1.1655 + if ($row['forum_type'] == FORUM_LINK)
1.1656 + {
1.1657 + continue;
1.1658 + }
1.1659 +
1.1660 + $forum_id = (int) $row['forum_id'];
1.1661 + $forum_ids[$forum_id] = $forum_id;
1.1662 +
1.1663 + $forum_data[$forum_id] = $row;
1.1664 + if ($sync_extra)
1.1665 + {
1.1666 + $forum_data[$forum_id]['posts'] = 0;
1.1667 + $forum_data[$forum_id]['topics'] = 0;
1.1668 + $forum_data[$forum_id]['topics_real'] = 0;
1.1669 + }
1.1670 + $forum_data[$forum_id]['last_post_id'] = 0;
1.1671 + $forum_data[$forum_id]['last_post_subject'] = '';
1.1672 + $forum_data[$forum_id]['last_post_time'] = 0;
1.1673 + $forum_data[$forum_id]['last_poster_id'] = 0;
1.1674 + $forum_data[$forum_id]['last_poster_name'] = '';
1.1675 + $forum_data[$forum_id]['last_poster_colour'] = '';
1.1676 + }
1.1677 + $db->sql_freeresult($result);
1.1678 +
1.1679 + if (!sizeof($forum_ids))
1.1680 + {
1.1681 + break;
1.1682 + }
1.1683 +
1.1684 + $forum_ids = array_values($forum_ids);
1.1685 +
1.1686 + // 2: Get topic counts for each forum (optional)
1.1687 + if ($sync_extra)
1.1688 + {
1.1689 + $sql = 'SELECT forum_id, topic_approved, COUNT(topic_id) AS forum_topics
1.1690 + FROM ' . TOPICS_TABLE . '
1.1691 + WHERE ' . $db->sql_in_set('forum_id', $forum_ids) . '
1.1692 + GROUP BY forum_id, topic_approved';
1.1693 + $result = $db->sql_query($sql);
1.1694 +
1.1695 + while ($row = $db->sql_fetchrow($result))
1.1696 + {
1.1697 + $forum_id = (int) $row['forum_id'];
1.1698 + $forum_data[$forum_id]['topics_real'] += $row['forum_topics'];
1.1699 +
1.1700 + if ($row['topic_approved'])
1.1701 + {
1.1702 + $forum_data[$forum_id]['topics'] = $row['forum_topics'];
1.1703 + }
1.1704 + }
1.1705 + $db->sql_freeresult($result);
1.1706 + }
1.1707 +
1.1708 + // 3: Get post count for each forum (optional)
1.1709 + if ($sync_extra)
1.1710 + {
1.1711 + if (sizeof($forum_ids) == 1)
1.1712 + {
1.1713 + $sql = 'SELECT SUM(t.topic_replies + 1) AS forum_posts
1.1714 + FROM ' . TOPICS_TABLE . ' t
1.1715 + WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
1.1716 + AND t.topic_approved = 1
1.1717 + AND t.topic_status <> ' . ITEM_MOVED;
1.1718 + }
1.1719 + else
1.1720 + {
1.1721 + $sql = 'SELECT t.forum_id, SUM(t.topic_replies + 1) AS forum_posts
1.1722 + FROM ' . TOPICS_TABLE . ' t
1.1723 + WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
1.1724 + AND t.topic_approved = 1
1.1725 + AND t.topic_status <> ' . ITEM_MOVED . '
1.1726 + GROUP BY t.forum_id';
1.1727 + }
1.1728 +
1.1729 + $result = $db->sql_query($sql);
1.1730 +
1.1731 + while ($row = $db->sql_fetchrow($result))
1.1732 + {
1.1733 + $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
1.1734 +
1.1735 + $forum_data[$forum_id]['posts'] = (int) $row['forum_posts'];
1.1736 + }
1.1737 + $db->sql_freeresult($result);
1.1738 + }
1.1739 +
1.1740 + // 4: Get last_post_id for each forum
1.1741 + if (sizeof($forum_ids) == 1)
1.1742 + {
1.1743 + $sql = 'SELECT MAX(t.topic_last_post_id) as last_post_id
1.1744 + FROM ' . TOPICS_TABLE . ' t
1.1745 + WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
1.1746 + AND t.topic_approved = 1';
1.1747 + }
1.1748 + else
1.1749 + {
1.1750 + $sql = 'SELECT t.forum_id, MAX(t.topic_last_post_id) as last_post_id
1.1751 + FROM ' . TOPICS_TABLE . ' t
1.1752 + WHERE ' . $db->sql_in_set('t.forum_id', $forum_ids) . '
1.1753 + AND t.topic_approved = 1
1.1754 + GROUP BY t.forum_id';
1.1755 + }
1.1756 +
1.1757 + $result = $db->sql_query($sql);
1.1758 +
1.1759 + while ($row = $db->sql_fetchrow($result))
1.1760 + {
1.1761 + $forum_id = (sizeof($forum_ids) == 1) ? (int) $forum_ids[0] : (int) $row['forum_id'];
1.1762 +
1.1763 + $forum_data[$forum_id]['last_post_id'] = (int) $row['last_post_id'];
1.1764 +
1.1765 + $post_ids[] = $row['last_post_id'];
1.1766 + }
1.1767 + $db->sql_freeresult($result);
1.1768 +
1.1769 + // 5: Retrieve last_post infos
1.1770 + if (sizeof($post_ids))
1.1771 + {
1.1772 + $sql = 'SELECT p.post_id, p.poster_id, p.post_subject, p.post_time, p.post_username, u.username, u.user_colour
1.1773 + FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1.1774 + WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
1.1775 + AND p.poster_id = u.user_id';
1.1776 + $result = $db->sql_query($sql);
1.1777 +
1.1778 + while ($row = $db->sql_fetchrow($result))
1.1779 + {
1.1780 + $post_info[$row['post_id']] = $row;
1.1781 + }
1.1782 + $db->sql_freeresult($result);
1.1783 +
1.1784 + foreach ($forum_data as $forum_id => $data)
1.1785 + {
1.1786 + if ($data['last_post_id'])
1.1787 + {
1.1788 + if (isset($post_info[$data['last_post_id']]))
1.1789 + {
1.1790 + $forum_data[$forum_id]['last_post_subject'] = $post_info[$data['last_post_id']]['post_subject'];
1.1791 + $forum_data[$forum_id]['last_post_time'] = $post_info[$data['last_post_id']]['post_time'];
1.1792 + $forum_data[$forum_id]['last_poster_id'] = $post_info[$data['last_post_id']]['poster_id'];
1.1793 + $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'];
1.1794 + $forum_data[$forum_id]['last_poster_colour'] = $post_info[$data['last_post_id']]['user_colour'];
1.1795 + }
1.1796 + else
1.1797 + {
1.1798 + // For some reason we did not find the post in the db
1.1799 + $forum_data[$forum_id]['last_post_id'] = 0;
1.1800 + $forum_data[$forum_id]['last_post_subject'] = '';
1.1801 + $forum_data[$forum_id]['last_post_time'] = 0;
1.1802 + $forum_data[$forum_id]['last_poster_id'] = 0;
1.1803 + $forum_data[$forum_id]['last_poster_name'] = '';
1.1804 + $forum_data[$forum_id]['last_poster_colour'] = '';
1.1805 + }
1.1806 + }
1.1807 + }
1.1808 + unset($post_info);
1.1809 + }
1.1810 +
1.1811 + // 6: Now do that thing
1.1812 + $fieldnames = array('last_post_id', 'last_post_subject', 'last_post_time', 'last_poster_id', 'last_poster_name', 'last_poster_colour');
1.1813 +
1.1814 + if ($sync_extra)
1.1815 + {
1.1816 + array_push($fieldnames, 'posts', 'topics', 'topics_real');
1.1817 + }
1.1818 +
1.1819 + foreach ($forum_data as $forum_id => $row)
1.1820 + {
1.1821 + $sql_ary = array();
1.1822 +
1.1823 + foreach ($fieldnames as $fieldname)
1.1824 + {
1.1825 + if ($row['forum_' . $fieldname] != $row[$fieldname])
1.1826 + {
1.1827 + if (preg_match('#(name|colour|subject)$#', $fieldname))
1.1828 + {
1.1829 + $sql_ary['forum_' . $fieldname] = (string) $row[$fieldname];
1.1830 + }
1.1831 + else
1.1832 + {
1.1833 + $sql_ary['forum_' . $fieldname] = (int) $row[$fieldname];
1.1834 + }
1.1835 + }
1.1836 + }
1.1837 +
1.1838 + if (sizeof($sql_ary))
1.1839 + {
1.1840 + $sql = 'UPDATE ' . FORUMS_TABLE . '
1.1841 + SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
1.1842 + WHERE forum_id = ' . $forum_id;
1.1843 + $db->sql_query($sql);
1.1844 + }
1.1845 + }
1.1846 +
1.1847 + $db->sql_transaction('commit');
1.1848 + break;
1.1849 +
1.1850 + case 'topic':
1.1851 + $topic_data = $post_ids = $approved_unapproved_ids = $resync_forums = $delete_topics = $delete_posts = $moved_topics = array();
1.1852 +
1.1853 + $db->sql_transaction('begin');
1.1854 +
1.1855 + $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
1.1856 + FROM ' . TOPICS_TABLE . " t
1.1857 + $where_sql";
1.1858 + $result = $db->sql_query($sql);
1.1859 +
1.1860 + while ($row = $db->sql_fetchrow($result))
1.1861 + {
1.1862 + if ($row['topic_moved_id'])
1.1863 + {
1.1864 + $moved_topics[] = $row['topic_id'];
1.1865 + continue;
1.1866 + }
1.1867 +
1.1868 + $topic_id = (int) $row['topic_id'];
1.1869 + $topic_data[$topic_id] = $row;
1.1870 + $topic_data[$topic_id]['replies_real'] = -1;
1.1871 + $topic_data[$topic_id]['replies'] = 0;
1.1872 + $topic_data[$topic_id]['first_post_id'] = 0;
1.1873 + $topic_data[$topic_id]['last_post_id'] = 0;
1.1874 + unset($topic_data[$topic_id]['topic_id']);
1.1875 +
1.1876 + // This array holds all topic_ids
1.1877 + $delete_topics[$topic_id] = '';
1.1878 +
1.1879 + if ($sync_extra)
1.1880 + {
1.1881 + $topic_data[$topic_id]['reported'] = 0;
1.1882 + $topic_data[$topic_id]['attachment'] = 0;
1.1883 + }
1.1884 + }
1.1885 + $db->sql_freeresult($result);
1.1886 +
1.1887 + // Use "t" as table alias because of the $where_sql clause
1.1888 + // NOTE: 't.post_approved' in the GROUP BY is causing a major slowdown.
1.1889 + $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
1.1890 + FROM ' . POSTS_TABLE . " t
1.1891 + $where_sql
1.1892 + GROUP BY t.topic_id, t.post_approved";
1.1893 + $result = $db->sql_query($sql);
1.1894 +
1.1895 + while ($row = $db->sql_fetchrow($result))
1.1896 + {
1.1897 + $topic_id = (int) $row['topic_id'];
1.1898 +
1.1899 + $row['first_post_id'] = (int) $row['first_post_id'];
1.1900 + $row['last_post_id'] = (int) $row['last_post_id'];
1.1901 +
1.1902 + if (!isset($topic_data[$topic_id]))
1.1903 + {
1.1904 + // Hey, these posts come from a topic that does not exist
1.1905 + $delete_posts[$topic_id] = '';
1.1906 + }
1.1907 + else
1.1908 + {
1.1909 + // Unset the corresponding entry in $delete_topics
1.1910 + // When we'll be done, only topics with no posts will remain
1.1911 + unset($delete_topics[$topic_id]);
1.1912 +
1.1913 + $topic_data[$topic_id]['replies_real'] += $row['total_posts'];
1.1914 + $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']);
1.1915 +
1.1916 + if ($row['post_approved'] || !$topic_data[$topic_id]['last_post_id'])
1.1917 + {
1.1918 + $topic_data[$topic_id]['replies'] = $row['total_posts'] - 1;
1.1919 + $topic_data[$topic_id]['last_post_id'] = $row['last_post_id'];
1.1920 + }
1.1921 + }
1.1922 + }
1.1923 + $db->sql_freeresult($result);
1.1924 +
1.1925 + foreach ($topic_data as $topic_id => $row)
1.1926 + {
1.1927 + $post_ids[] = $row['first_post_id'];
1.1928 + if ($row['first_post_id'] != $row['last_post_id'])
1.1929 + {
1.1930 + $post_ids[] = $row['last_post_id'];
1.1931 + }
1.1932 + }
1.1933 +
1.1934 + // Now we delete empty topics and orphan posts
1.1935 + if (sizeof($delete_posts))
1.1936 + {
1.1937 + delete_posts('topic_id', array_keys($delete_posts), false);
1.1938 + unset($delete_posts);
1.1939 + }
1.1940 +
1.1941 + if (!sizeof($topic_data))
1.1942 + {
1.1943 + // If we get there, topic ids were invalid or topics did not contain any posts
1.1944 + delete_topics($where_type, $where_ids, true);
1.1945 + return;
1.1946 + }
1.1947 +
1.1948 + if (sizeof($delete_topics))
1.1949 + {
1.1950 + $delete_topic_ids = array();
1.1951 + foreach ($delete_topics as $topic_id => $void)
1.1952 + {
1.1953 + unset($topic_data[$topic_id]);
1.1954 + $delete_topic_ids[] = $topic_id;
1.1955 + }
1.1956 +
1.1957 + delete_topics('topic_id', $delete_topic_ids, false);
1.1958 + unset($delete_topics, $delete_topic_ids);
1.1959 + }
1.1960 +
1.1961 + $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
1.1962 + FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1.1963 + WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
1.1964 + AND u.user_id = p.poster_id';
1.1965 + $result = $db->sql_query($sql);
1.1966 +
1.1967 + $post_ids = array();
1.1968 + while ($row = $db->sql_fetchrow($result))
1.1969 + {
1.1970 + $topic_id = intval($row['topic_id']);
1.1971 +
1.1972 + if ($row['post_id'] == $topic_data[$topic_id]['first_post_id'])
1.1973 + {
1.1974 + if ($topic_data[$topic_id]['topic_approved'] != $row['post_approved'])
1.1975 + {
1.1976 + $approved_unapproved_ids[] = $topic_id;
1.1977 + }
1.1978 + $topic_data[$topic_id]['time'] = $row['post_time'];
1.1979 + $topic_data[$topic_id]['poster'] = $row['poster_id'];
1.1980 + $topic_data[$topic_id]['first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
1.1981 + $topic_data[$topic_id]['first_poster_colour'] = $row['user_colour'];
1.1982 + }
1.1983 +
1.1984 + if ($row['post_id'] == $topic_data[$topic_id]['last_post_id'])
1.1985 + {
1.1986 + $topic_data[$topic_id]['last_poster_id'] = $row['poster_id'];
1.1987 + $topic_data[$topic_id]['last_post_subject'] = $row['post_subject'];
1.1988 + $topic_data[$topic_id]['last_post_time'] = $row['post_time'];
1.1989 + $topic_data[$topic_id]['last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
1.1990 + $topic_data[$topic_id]['last_poster_colour'] = $row['user_colour'];
1.1991 + }
1.1992 + }
1.1993 + $db->sql_freeresult($result);
1.1994 +
1.1995 + // Make sure shadow topics do link to existing topics
1.1996 + if (sizeof($moved_topics))
1.1997 + {
1.1998 + $delete_topics = array();
1.1999 +
1.2000 + $sql = 'SELECT t1.topic_id, t1.topic_moved_id
1.2001 + FROM ' . TOPICS_TABLE . ' t1
1.2002 + LEFT JOIN ' . TOPICS_TABLE . ' t2 ON (t2.topic_id = t1.topic_moved_id)
1.2003 + WHERE ' . $db->sql_in_set('t1.topic_id', $moved_topics) . '
1.2004 + AND t2.topic_id IS NULL';
1.2005 + $result = $db->sql_query($sql);
1.2006 +
1.2007 + while ($row = $db->sql_fetchrow($result))
1.2008 + {
1.2009 + $delete_topics[] = $row['topic_id'];
1.2010 + }
1.2011 + $db->sql_freeresult($result);
1.2012 +
1.2013 + if (sizeof($delete_topics))
1.2014 + {
1.2015 + delete_topics('topic_id', $delete_topics, false);
1.2016 + }
1.2017 + unset($delete_topics);
1.2018 +
1.2019 + // Make sure shadow topics having no last post data being updated (this only rarely happens...)
1.2020 + $sql = 'SELECT topic_id, topic_moved_id, topic_last_post_id, topic_first_post_id
1.2021 + FROM ' . TOPICS_TABLE . '
1.2022 + WHERE ' . $db->sql_in_set('topic_id', $moved_topics) . '
1.2023 + AND topic_last_post_time = 0';
1.2024 + $result = $db->sql_query($sql);
1.2025 +
1.2026 + $shadow_topic_data = $post_ids = array();
1.2027 + while ($row = $db->sql_fetchrow($result))
1.2028 + {
1.2029 + $shadow_topic_data[$row['topic_moved_id']] = $row;
1.2030 + $post_ids[] = $row['topic_last_post_id'];
1.2031 + $post_ids[] = $row['topic_first_post_id'];
1.2032 + }
1.2033 + $db->sql_freeresult($result);
1.2034 +
1.2035 + $sync_shadow_topics = array();
1.2036 + if (sizeof($post_ids))
1.2037 + {
1.2038 + $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
1.2039 + FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1.2040 + WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
1.2041 + AND u.user_id = p.poster_id';
1.2042 + $result = $db->sql_query($sql);
1.2043 +
1.2044 + $post_ids = array();
1.2045 + while ($row = $db->sql_fetchrow($result))
1.2046 + {
1.2047 + $topic_id = (int) $row['topic_id'];
1.2048 +
1.2049 + // Ok, there should be a shadow topic. If there isn't, then there's something wrong with the db.
1.2050 + // However, there's not much we can do about it.
1.2051 + if (!empty($shadow_topic_data[$topic_id]))
1.2052 + {
1.2053 + if ($row['post_id'] == $shadow_topic_data[$topic_id]['topic_first_post_id'])
1.2054 + {
1.2055 + $orig_topic_id = $shadow_topic_data[$topic_id]['topic_id'];
1.2056 +
1.2057 + if (!isset($sync_shadow_topics[$orig_topic_id]))
1.2058 + {
1.2059 + $sync_shadow_topics[$orig_topic_id] = array();
1.2060 + }
1.2061 +
1.2062 + $sync_shadow_topics[$orig_topic_id]['topic_time'] = $row['post_time'];
1.2063 + $sync_shadow_topics[$orig_topic_id]['topic_poster'] = $row['poster_id'];
1.2064 + $sync_shadow_topics[$orig_topic_id]['topic_first_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
1.2065 + $sync_shadow_topics[$orig_topic_id]['topic_first_poster_colour'] = $row['user_colour'];
1.2066 + }
1.2067 +
1.2068 + if ($row['post_id'] == $shadow_topic_data[$topic_id]['topic_last_post_id'])
1.2069 + {
1.2070 + $orig_topic_id = $shadow_topic_data[$topic_id]['topic_id'];
1.2071 +
1.2072 + if (!isset($sync_shadow_topics[$orig_topic_id]))
1.2073 + {
1.2074 + $sync_shadow_topics[$orig_topic_id] = array();
1.2075 + }
1.2076 +
1.2077 + $sync_shadow_topics[$orig_topic_id]['topic_last_poster_id'] = $row['poster_id'];
1.2078 + $sync_shadow_topics[$orig_topic_id]['topic_last_post_subject'] = $row['post_subject'];
1.2079 + $sync_shadow_topics[$orig_topic_id]['topic_last_post_time'] = $row['post_time'];
1.2080 + $sync_shadow_topics[$orig_topic_id]['topic_last_poster_name'] = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
1.2081 + $sync_shadow_topics[$orig_topic_id]['topic_last_poster_colour'] = $row['user_colour'];
1.2082 + }
1.2083 + }
1.2084 + }
1.2085 + $db->sql_freeresult($result);
1.2086 +
1.2087 + $shadow_topic_data = array();
1.2088 +
1.2089 + // Update the information we collected
1.2090 + if (sizeof($sync_shadow_topics))
1.2091 + {
1.2092 + foreach ($sync_shadow_topics as $sync_topic_id => $sql_ary)
1.2093 + {
1.2094 + $sql = 'UPDATE ' . TOPICS_TABLE . '
1.2095 + SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
1.2096 + WHERE topic_id = ' . $sync_topic_id;
1.2097 + $db->sql_query($sql);
1.2098 + }
1.2099 + }
1.2100 + }
1.2101 +
1.2102 + unset($sync_shadow_topics, $shadow_topic_data);
1.2103 + }
1.2104 +
1.2105 + // approved becomes unapproved, and vice-versa
1.2106 + if (sizeof($approved_unapproved_ids))
1.2107 + {
1.2108 + $sql = 'UPDATE ' . TOPICS_TABLE . '
1.2109 + SET topic_approved = 1 - topic_approved
1.2110 + WHERE ' . $db->sql_in_set('topic_id', $approved_unapproved_ids);
1.2111 + $db->sql_query($sql);
1.2112 + }
1.2113 + unset($approved_unapproved_ids);
1.2114 +
1.2115 + // These are fields that will be synchronised
1.2116 + $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');
1.2117 +
1.2118 + if ($sync_extra)
1.2119 + {
1.2120 + // This routine assumes that post_reported values are correct
1.2121 + // if they are not, use sync('post_reported') first
1.2122 + $sql = 'SELECT t.topic_id, p.post_id
1.2123 + FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
1.2124 + $where_sql_and p.topic_id = t.topic_id
1.2125 + AND p.post_reported = 1
1.2126 + GROUP BY t.topic_id, p.post_id";
1.2127 + $result = $db->sql_query($sql);
1.2128 +
1.2129 + $fieldnames[] = 'reported';
1.2130 + while ($row = $db->sql_fetchrow($result))
1.2131 + {
1.2132 + $topic_data[intval($row['topic_id'])]['reported'] = 1;
1.2133 + }
1.2134 + $db->sql_freeresult($result);
1.2135 +
1.2136 + // This routine assumes that post_attachment values are correct
1.2137 + // if they are not, use sync('post_attachment') first
1.2138 + $sql = 'SELECT t.topic_id, p.post_id
1.2139 + FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
1.2140 + $where_sql_and p.topic_id = t.topic_id
1.2141 + AND p.post_attachment = 1
1.2142 + GROUP BY t.topic_id, p.post_id";
1.2143 + $result = $db->sql_query($sql);
1.2144 +
1.2145 + $fieldnames[] = 'attachment';
1.2146 + while ($row = $db->sql_fetchrow($result))
1.2147 + {
1.2148 + $topic_data[intval($row['topic_id'])]['attachment'] = 1;
1.2149 + }
1.2150 + $db->sql_freeresult($result);
1.2151 + }
1.2152 +
1.2153 + foreach ($topic_data as $topic_id => $row)
1.2154 + {
1.2155 + $sql_ary = array();
1.2156 +
1.2157 + foreach ($fieldnames as $fieldname)
1.2158 + {
1.2159 + if (isset($row[$fieldname]) && isset($row['topic_' . $fieldname]) && $row['topic_' . $fieldname] != $row[$fieldname])
1.2160 + {
1.2161 + $sql_ary['topic_' . $fieldname] = $row[$fieldname];
1.2162 + }
1.2163 + }
1.2164 +
1.2165 + if (sizeof($sql_ary))
1.2166 + {
1.2167 + $sql = 'UPDATE ' . TOPICS_TABLE . '
1.2168 + SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
1.2169 + WHERE topic_id = ' . $topic_id;
1.2170 + $db->sql_query($sql);
1.2171 +
1.2172 + $resync_forums[$row['forum_id']] = $row['forum_id'];
1.2173 + }
1.2174 + }
1.2175 + unset($topic_data);
1.2176 +
1.2177 + $db->sql_transaction('commit');
1.2178 +
1.2179 + // if some topics have been resync'ed then resync parent forums
1.2180 + // except when we're only syncing a range, we don't want to sync forums during
1.2181 + // batch processing.
1.2182 + if ($resync_parents && sizeof($resync_forums) && $where_type != 'range')
1.2183 + {
1.2184 + sync('forum', 'forum_id', array_values($resync_forums), true, true);
1.2185 + }
1.2186 + break;
1.2187 + }
1.2188 +
1.2189 + return;
1.2190 +}
1.2191 +
1.2192 +/**
1.2193 +* Prune function
1.2194 +*/
1.2195 +function prune($forum_id, $prune_mode, $prune_date, $prune_flags = 0, $auto_sync = true)
1.2196 +{
1.2197 + global $db;
1.2198 +
1.2199 + if (!is_array($forum_id))
1.2200 + {
1.2201 + $forum_id = array($forum_id);
1.2202 + }
1.2203 +
1.2204 + if (!sizeof($forum_id))
1.2205 + {
1.2206 + return;
1.2207 + }
1.2208 +
1.2209 + $sql_and = '';
1.2210 +
1.2211 + if (!($prune_flags & FORUM_FLAG_PRUNE_ANNOUNCE))
1.2212 + {
1.2213 + $sql_and .= ' AND topic_type <> ' . POST_ANNOUNCE;
1.2214 + }
1.2215 +
1.2216 + if (!($prune_flags & FORUM_FLAG_PRUNE_STICKY))
1.2217 + {
1.2218 + $sql_and .= ' AND topic_type <> ' . POST_STICKY;
1.2219 + }
1.2220 +
1.2221 + if ($prune_mode == 'posted')
1.2222 + {
1.2223 + $sql_and .= " AND topic_last_post_time < $prune_date";
1.2224 + }
1.2225 +
1.2226 + if ($prune_mode == 'viewed')
1.2227 + {
1.2228 + $sql_and .= " AND topic_last_view_time < $prune_date";
1.2229 + }
1.2230 +
1.2231 + $sql = 'SELECT topic_id
1.2232 + FROM ' . TOPICS_TABLE . '
1.2233 + WHERE ' . $db->sql_in_set('forum_id', $forum_id) . "
1.2234 + AND poll_start = 0
1.2235 + $sql_and";
1.2236 + $result = $db->sql_query($sql);
1.2237 +
1.2238 + $topic_list = array();
1.2239 + while ($row = $db->sql_fetchrow($result))
1.2240 + {
1.2241 + $topic_list[] = $row['topic_id'];
1.2242 + }
1.2243 + $db->sql_freeresult($result);
1.2244 +
1.2245 + if ($prune_flags & FORUM_FLAG_PRUNE_POLL)
1.2246 + {
1.2247 + $sql = 'SELECT topic_id
1.2248 + FROM ' . TOPICS_TABLE . '
1.2249 + WHERE ' . $db->sql_in_set('forum_id', $forum_id) . "
1.2250 + AND poll_start > 0
1.2251 + AND poll_last_vote < $prune_date
1.2252 + $sql_and";
1.2253 + $result = $db->sql_query($sql);
1.2254 +
1.2255 + while ($row = $db->sql_fetchrow($result))
1.2256 + {
1.2257 + $topic_list[] = $row['topic_id'];
1.2258 + }
1.2259 + $db->sql_freeresult($result);
1.2260 +
1.2261 + $topic_list = array_unique($topic_list);
1.2262 + }
1.2263 +
1.2264 + return delete_topics('topic_id', $topic_list, $auto_sync, false);
1.2265 +}
1.2266 +
1.2267 +/**
1.2268 +* Function auto_prune(), this function now relies on passed vars
1.2269 +*/
1.2270 +function auto_prune($forum_id, $prune_mode, $prune_flags, $prune_days, $prune_freq)
1.2271 +{
1.2272 + global $db;
1.2273 +
1.2274 + $sql = 'SELECT forum_name
1.2275 + FROM ' . FORUMS_TABLE . "
1.2276 + WHERE forum_id = $forum_id";
1.2277 + $result = $db->sql_query($sql, 3600);
1.2278 + $row = $db->sql_fetchrow($result);
1.2279 + $db->sql_freeresult($result);
1.2280 +
1.2281 + if ($row)
1.2282 + {
1.2283 + $prune_date = time() - ($prune_days * 86400);
1.2284 + $next_prune = time() + ($prune_freq * 86400);
1.2285 +
1.2286 + prune($forum_id, $prune_mode, $prune_date, $prune_flags, true);
1.2287 +
1.2288 + $sql = 'UPDATE ' . FORUMS_TABLE . "
1.2289 + SET prune_next = $next_prune
1.2290 + WHERE forum_id = $forum_id";
1.2291 + $db->sql_query($sql);
1.2292 +
1.2293 + add_log('admin', 'LOG_AUTO_PRUNE', $row['forum_name']);
1.2294 + }
1.2295 +
1.2296 + return;
1.2297 +}
1.2298 +
1.2299 +/**
1.2300 +* remove_comments will strip the sql comment lines out of an uploaded sql file
1.2301 +* specifically for mssql and postgres type files in the install....
1.2302 +*/
1.2303 +function remove_comments(&$output)
1.2304 +{
1.2305 + $lines = explode("\n", $output);
1.2306 + $output = '';
1.2307 +
1.2308 + // try to keep mem. use down
1.2309 + $linecount = sizeof($lines);
1.2310 +
1.2311 + $in_comment = false;
1.2312 + for ($i = 0; $i < $linecount; $i++)
1.2313 + {
1.2314 + if (trim($lines[$i]) == '/*')
1.2315 + {
1.2316 + $in_comment = true;
1.2317 + }
1.2318 +
1.2319 + if (!$in_comment)
1.2320 + {
1.2321 + $output .= $lines[$i] . "\n";
1.2322 + }
1.2323 +
1.2324 + if (trim($lines[$i]) == '*/')
1.2325 + {
1.2326 + $in_comment = false;
1.2327 + }
1.2328 + }
1.2329 +
1.2330 + unset($lines);
1.2331 + return $output;
1.2332 +}
1.2333 +
1.2334 +/**
1.2335 +* Cache moderators, called whenever permissions are changed via admin_permissions. Changes of username
1.2336 +* and group names must be carried through for the moderators table
1.2337 +*/
1.2338 +function cache_moderators()
1.2339 +{
1.2340 + global $db, $cache, $auth, $phpbb_root_path, $phpEx;
1.2341 +
1.2342 + // Remove cached sql results
1.2343 + $cache->destroy('sql', MODERATOR_CACHE_TABLE);
1.2344 +
1.2345 + // Clear table
1.2346 + switch ($db->sql_layer)
1.2347 + {
1.2348 + case 'sqlite':
1.2349 + case 'firebird':
1.2350 + $db->sql_query('DELETE FROM ' . MODERATOR_CACHE_TABLE);
1.2351 + break;
1.2352 +
1.2353 + default:
1.2354 + $db->sql_query('TRUNCATE TABLE ' . MODERATOR_CACHE_TABLE);
1.2355 + break;
1.2356 + }
1.2357 +
1.2358 + // We add moderators who have forum moderator permissions without an explicit ACL_NEVER setting
1.2359 + $hold_ary = $ug_id_ary = $sql_ary = array();
1.2360 +
1.2361 + // Grab all users having moderative options...
1.2362 + $hold_ary = $auth->acl_user_raw_data(false, 'm_%', false);
1.2363 +
1.2364 + // Add users?
1.2365 + if (sizeof($hold_ary))
1.2366 + {
1.2367 + // At least one moderative option warrants a display
1.2368 + $ug_id_ary = array_keys($hold_ary);
1.2369 +
1.2370 + // Remove users who have group memberships with DENY moderator permissions
1.2371 + $sql = $db->sql_build_query('SELECT', array(
1.2372 + 'SELECT' => 'a.forum_id, ug.user_id, g.group_id',
1.2373 +
1.2374 + 'FROM' => array(
1.2375 + ACL_OPTIONS_TABLE => 'o',
1.2376 + USER_GROUP_TABLE => 'ug',
1.2377 + GROUPS_TABLE => 'g',
1.2378 + ACL_GROUPS_TABLE => 'a',
1.2379 + ),
1.2380 +
1.2381 + 'LEFT_JOIN' => array(
1.2382 + array(
1.2383 + 'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
1.2384 + 'ON' => 'a.auth_role_id = r.role_id'
1.2385 + )
1.2386 + ),
1.2387 +
1.2388 + 'WHERE' => '(o.auth_option_id = a.auth_option_id OR o.auth_option_id = r.auth_option_id)
1.2389 + AND ((a.auth_setting = ' . ACL_NEVER . ' AND r.auth_setting IS NULL)
1.2390 + OR r.auth_setting = ' . ACL_NEVER . ')
1.2391 + AND a.group_id = ug.group_id
1.2392 + AND g.group_id = ug.group_id
1.2393 + AND NOT (ug.group_leader = 1 AND g.group_skip_auth = 1)
1.2394 + AND ' . $db->sql_in_set('ug.user_id', $ug_id_ary) . "
1.2395 + AND ug.user_pending = 0
1.2396 + AND o.auth_option " . $db->sql_like_expression('m_' . $db->any_char),
1.2397 + ));
1.2398 + $result = $db->sql_query($sql);
1.2399 +
1.2400 + while ($row = $db->sql_fetchrow($result))
1.2401 + {
1.2402 + if (isset($hold_ary[$row['user_id']][$row['forum_id']]))
1.2403 + {
1.2404 + unset($hold_ary[$row['user_id']][$row['forum_id']]);
1.2405 + }
1.2406 + }
1.2407 + $db->sql_freeresult($result);
1.2408 +
1.2409 + if (sizeof($hold_ary))
1.2410 + {
1.2411 + // Get usernames...
1.2412 + $sql = 'SELECT user_id, username
1.2413 + FROM ' . USERS_TABLE . '
1.2414 + WHERE ' . $db->sql_in_set('user_id', array_keys($hold_ary));
1.2415 + $result = $db->sql_query($sql);
1.2416 +
1.2417 + $usernames_ary = array();
1.2418 + while ($row = $db->sql_fetchrow($result))
1.2419 + {
1.2420 + $usernames_ary[$row['user_id']] = $row['username'];
1.2421 + }
1.2422 +
1.2423 + foreach ($hold_ary as $user_id => $forum_id_ary)
1.2424 + {
1.2425 + // Do not continue if user does not exist
1.2426 + if (!isset($usernames_ary[$user_id]))
1.2427 + {
1.2428 + continue;
1.2429 + }
1.2430 +
1.2431 + foreach ($forum_id_ary as $forum_id => $auth_ary)
1.2432 + {
1.2433 + $sql_ary[] = array(
1.2434 + 'forum_id' => (int) $forum_id,
1.2435 + 'user_id' => (int) $user_id,
1.2436 + 'username' => (string) $usernames_ary[$user_id],
1.2437 + 'group_id' => 0,
1.2438 + 'group_name' => ''
1.2439 + );
1.2440 + }
1.2441 + }
1.2442 + }
1.2443 + }
1.2444 +
1.2445 + // Now to the groups...
1.2446 + $hold_ary = $auth->acl_group_raw_data(false, 'm_%', false);
1.2447 +
1.2448 + if (sizeof($hold_ary))
1.2449 + {
1.2450 + $ug_id_ary = array_keys($hold_ary);
1.2451 +
1.2452 + // Make sure not hidden or special groups are involved...
1.2453 + $sql = 'SELECT group_name, group_id, group_type
1.2454 + FROM ' . GROUPS_TABLE . '
1.2455 + WHERE ' . $db->sql_in_set('group_id', $ug_id_ary);
1.2456 + $result = $db->sql_query($sql);
1.2457 +
1.2458 + $groupnames_ary = array();
1.2459 + while ($row = $db->sql_fetchrow($result))
1.2460 + {
1.2461 + if ($row['group_type'] == GROUP_HIDDEN || $row['group_type'] == GROUP_SPECIAL)
1.2462 + {
1.2463 + unset($hold_ary[$row['group_id']]);
1.2464 + }
1.2465 +
1.2466 + $groupnames_ary[$row['group_id']] = $row['group_name'];
1.2467 + }
1.2468 + $db->sql_freeresult($result);
1.2469 +
1.2470 + foreach ($hold_ary as $group_id => $forum_id_ary)
1.2471 + {
1.2472 + // If there is no group, we do not assign it...
1.2473 + if (!isset($groupnames_ary[$group_id]))
1.2474 + {
1.2475 + continue;
1.2476 + }
1.2477 +
1.2478 + foreach ($forum_id_ary as $forum_id => $auth_ary)
1.2479 + {
1.2480 + $flag = false;
1.2481 + foreach ($auth_ary as $auth_option => $setting)
1.2482 + {
1.2483 + // Make sure at least one ACL_YES option is set...
1.2484 + if ($setting == ACL_YES)
1.2485 + {
1.2486 + $flag = true;
1.2487 + break;
1.2488 + }
1.2489 + }
1.2490 +
1.2491 + if (!$flag)
1.2492 + {
1.2493 + continue;
1.2494 + }
1.2495 +
1.2496 + $sql_ary[] = array(
1.2497 + 'forum_id' => (int) $forum_id,
1.2498 + 'user_id' => 0,
1.2499 + 'username' => '',
1.2500 + 'group_id' => (int) $group_id,
1.2501 + 'group_name' => (string) $groupnames_ary[$group_id]
1.2502 + );
1.2503 + }
1.2504 + }
1.2505 + }
1.2506 +
1.2507 + $db->sql_multi_insert(MODERATOR_CACHE_TABLE, $sql_ary);
1.2508 +}
1.2509 +
1.2510 +/**
1.2511 +* View log
1.2512 +* If $log_count is set to false, we will skip counting all entries in the database.
1.2513 +*/
1.2514 +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 = '')
1.2515 +{
1.2516 + global $db, $user, $auth, $phpEx, $phpbb_root_path, $phpbb_admin_path;
1.2517 +
1.2518 + $topic_id_list = $reportee_id_list = $is_auth = $is_mod = array();
1.2519 +
1.2520 + $profile_url = (defined('IN_ADMIN')) ? append_sid("{$phpbb_admin_path}index.$phpEx", 'i=users&mode=overview') : append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile');
1.2521 +
1.2522 + switch ($mode)
1.2523 + {
1.2524 + case 'admin':
1.2525 + $log_type = LOG_ADMIN;
1.2526 + $sql_forum = '';
1.2527 + break;
1.2528 +
1.2529 + case 'mod':
1.2530 + $log_type = LOG_MOD;
1.2531 + $sql_forum = '';
1.2532 +
1.2533 + if ($topic_id)
1.2534 + {
1.2535 + $sql_forum = 'AND l.topic_id = ' . (int) $topic_id;
1.2536 + }
1.2537 + else if (is_array($forum_id))
1.2538 + {
1.2539 + $sql_forum = 'AND ' . $db->sql_in_set('l.forum_id', array_map('intval', $forum_id));
1.2540 + }
1.2541 + else if ($forum_id)
1.2542 + {
1.2543 + $sql_forum = 'AND l.forum_id = ' . (int) $forum_id;
1.2544 + }
1.2545 + break;
1.2546 +
1.2547 + case 'user':
1.2548 + $log_type = LOG_USERS;
1.2549 + $sql_forum = 'AND l.reportee_id = ' . (int) $user_id;
1.2550 + break;
1.2551 +
1.2552 + case 'users':
1.2553 + $log_type = LOG_USERS;
1.2554 + $sql_forum = '';
1.2555 + break;
1.2556 +
1.2557 + case 'critical':
1.2558 + $log_type = LOG_CRITICAL;
1.2559 + $sql_forum = '';
1.2560 + break;
1.2561 +
1.2562 + default:
1.2563 + return;
1.2564 + }
1.2565 +
1.2566 + // Use no preg_quote for $keywords because this would lead to sole backslashes being added
1.2567 + // We also use an OR connection here for spaces and the | string. Currently, regex is not supported for searching (but may come later).
1.2568 + $keywords = preg_split('#[\s|]+#u', utf8_strtolower($keywords), 0, PREG_SPLIT_NO_EMPTY);
1.2569 + $sql_keywords = '';
1.2570 +
1.2571 + if (!empty($keywords))
1.2572 + {
1.2573 + $keywords_pattern = array();
1.2574 +
1.2575 + // Build pattern and keywords...
1.2576 + for ($i = 0, $num_keywords = sizeof($keywords); $i < $num_keywords; $i++)
1.2577 + {
1.2578 + $keywords_pattern[] = preg_quote($keywords[$i], '#');
1.2579 + $keywords[$i] = $db->sql_like_expression($db->any_char . $keywords[$i] . $db->any_char);
1.2580 + }
1.2581 +
1.2582 + $keywords_pattern = '#' . implode('|', $keywords_pattern) . '#ui';
1.2583 +
1.2584 + $operations = array();
1.2585 + foreach ($user->lang as $key => $value)
1.2586 + {
1.2587 + if (substr($key, 0, 4) == 'LOG_' && preg_match($keywords_pattern, $value))
1.2588 + {
1.2589 + $operations[] = $key;
1.2590 + }
1.2591 + }
1.2592 +
1.2593 + $sql_keywords = 'AND (';
1.2594 + if (!empty($operations))
1.2595 + {
1.2596 + $sql_keywords .= $db->sql_in_set('l.log_operation', $operations) . ' OR ';
1.2597 + }
1.2598 + $sql_keywords .= 'LOWER(l.log_data) ' . implode(' OR LOWER(l.log_data) ', $keywords) . ')';
1.2599 + }
1.2600 +
1.2601 + if ($log_count !== false)
1.2602 + {
1.2603 + $sql = 'SELECT COUNT(l.log_id) AS total_entries
1.2604 + FROM ' . LOG_TABLE . ' l, ' . USERS_TABLE . " u
1.2605 + WHERE l.log_type = $log_type
1.2606 + AND l.user_id = u.user_id
1.2607 + AND l.log_time >= $limit_days
1.2608 + $sql_keywords
1.2609 + $sql_forum";
1.2610 + $result = $db->sql_query($sql);
1.2611 + $log_count = (int) $db->sql_fetchfield('total_entries');
1.2612 + $db->sql_freeresult($result);
1.2613 + }
1.2614 +
1.2615 + // $log_count may be false here if false was passed in for it,
1.2616 + // because in this case we did not run the COUNT() query above.
1.2617 + // If we ran the COUNT() query and it returned zero rows, return;
1.2618 + // otherwise query for logs below.
1.2619 + if ($log_count === 0)
1.2620 + {
1.2621 + // Save the queries, because there are no logs to display
1.2622 + return 0;
1.2623 + }
1.2624 +
1.2625 + if ($offset >= $log_count)
1.2626 + {
1.2627 + $offset = ($offset - $limit < 0) ? 0 : $offset - $limit;
1.2628 + }
1.2629 +
1.2630 + $sql = "SELECT l.*, u.username, u.username_clean, u.user_colour
1.2631 + FROM " . LOG_TABLE . " l, " . USERS_TABLE . " u
1.2632 + WHERE l.log_type = $log_type
1.2633 + AND u.user_id = l.user_id
1.2634 + " . (($limit_days) ? "AND l.log_time >= $limit_days" : '') . "
1.2635 + $sql_keywords
1.2636 + $sql_forum
1.2637 + ORDER BY $sort_by";
1.2638 + $result = $db->sql_query_limit($sql, $limit, $offset);
1.2639 +
1.2640 + $i = 0;
1.2641 + $log = array();
1.2642 + while ($row = $db->sql_fetchrow($result))
1.2643 + {
1.2644 + if ($row['topic_id'])
1.2645 + {
1.2646 + $topic_id_list[] = $row['topic_id'];
1.2647 + }
1.2648 +
1.2649 + if ($row['reportee_id'])
1.2650 + {
1.2651 + $reportee_id_list[] = $row['reportee_id'];
1.2652 + }
1.2653 +
1.2654 + $log[$i] = array(
1.2655 + 'id' => $row['log_id'],
1.2656 +
1.2657 + 'reportee_id' => $row['reportee_id'],
1.2658 + 'reportee_username' => '',
1.2659 + 'reportee_username_full'=> '',
1.2660 +
1.2661 + 'user_id' => $row['user_id'],
1.2662 + 'username' => $row['username'],
1.2663 + 'username_full' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], false, $profile_url),
1.2664 +
1.2665 + 'ip' => $row['log_ip'],
1.2666 + 'time' => $row['log_time'],
1.2667 + 'forum_id' => $row['forum_id'],
1.2668 + 'topic_id' => $row['topic_id'],
1.2669 +
1.2670 + 'viewforum' => ($row['forum_id'] && $auth->acl_get('f_read', $row['forum_id'])) ? append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']) : false,
1.2671 + 'action' => (isset($user->lang[$row['log_operation']])) ? $user->lang[$row['log_operation']] : '{' . ucfirst(str_replace('_', ' ', $row['log_operation'])) . '}',
1.2672 + );
1.2673 +
1.2674 + if (!empty($row['log_data']))
1.2675 + {
1.2676 + $log_data_ary = @unserialize($row['log_data']);
1.2677 + $log_data_ary = ($log_data_ary === false) ? array() : $log_data_ary;
1.2678 +
1.2679 + if (isset($user->lang[$row['log_operation']]))
1.2680 + {
1.2681 + // Check if there are more occurrences of % than arguments, if there are we fill out the arguments array
1.2682 + // It doesn't matter if we add more arguments than placeholders
1.2683 + if ((substr_count($log[$i]['action'], '%') - sizeof($log_data_ary)) > 0)
1.2684 + {
1.2685 + $log_data_ary = array_merge($log_data_ary, array_fill(0, substr_count($log[$i]['action'], '%') - sizeof($log_data_ary), ''));
1.2686 + }
1.2687 +
1.2688 + $log[$i]['action'] = vsprintf($log[$i]['action'], $log_data_ary);
1.2689 +
1.2690 + // If within the admin panel we do not censor text out
1.2691 + if (defined('IN_ADMIN'))
1.2692 + {
1.2693 + $log[$i]['action'] = bbcode_nl2br($log[$i]['action']);
1.2694 + }
1.2695 + else
1.2696 + {
1.2697 + $log[$i]['action'] = bbcode_nl2br(censor_text($log[$i]['action']));
1.2698 + }
1.2699 + }
1.2700 + else if (!empty($log_data_ary))
1.2701 + {
1.2702 + $log[$i]['action'] .= '<br />' . implode('', $log_data_ary);
1.2703 + }
1.2704 +
1.2705 + /* Apply make_clickable... has to be seen if it is for good. :/
1.2706 + // Seems to be not for the moment, reconsider later...
1.2707 + $log[$i]['action'] = make_clickable($log[$i]['action']);
1.2708 + */
1.2709 + }
1.2710 +
1.2711 + $i++;
1.2712 + }
1.2713 + $db->sql_freeresult($result);
1.2714 +
1.2715 + if (sizeof($topic_id_list))
1.2716 + {
1.2717 + $topic_id_list = array_unique($topic_id_list);
1.2718 +
1.2719 + // This query is not really needed if move_topics() updates the forum_id field,
1.2720 + // although it's also used to determine if the topic still exists in the database
1.2721 + $sql = 'SELECT topic_id, forum_id
1.2722 + FROM ' . TOPICS_TABLE . '
1.2723 + WHERE ' . $db->sql_in_set('topic_id', array_map('intval', $topic_id_list));
1.2724 + $result = $db->sql_query($sql);
1.2725 +
1.2726 + $default_forum_id = 0;
1.2727 +
1.2728 + while ($row = $db->sql_fetchrow($result))
1.2729 + {
1.2730 + if (!$row['forum_id'])
1.2731 + {
1.2732 + if ($auth->acl_getf_global('f_read'))
1.2733 + {
1.2734 + if (!$default_forum_id)
1.2735 + {
1.2736 + $sql = 'SELECT forum_id
1.2737 + FROM ' . FORUMS_TABLE . '
1.2738 + WHERE forum_type = ' . FORUM_POST;
1.2739 + $f_result = $db->sql_query_limit($sql, 1);
1.2740 + $default_forum_id = (int) $db->sql_fetchfield('forum_id', false, $f_result);
1.2741 + $db->sql_freeresult($f_result);
1.2742 + }
1.2743 +
1.2744 + $is_auth[$row['topic_id']] = $default_forum_id;
1.2745 + }
1.2746 + }
1.2747 + else
1.2748 + {
1.2749 + if ($auth->acl_get('f_read', $row['forum_id']))
1.2750 + {
1.2751 + $is_auth[$row['topic_id']] = $row['forum_id'];
1.2752 + }
1.2753 + }
1.2754 +
1.2755 + if ($auth->acl_gets('a_', 'm_', $row['forum_id']))
1.2756 + {
1.2757 + $is_mod[$row['topic_id']] = $row['forum_id'];
1.2758 + }
1.2759 + }
1.2760 + $db->sql_freeresult($result);
1.2761 +
1.2762 + foreach ($log as $key => $row)
1.2763 + {
1.2764 + $log[$key]['viewtopic'] = (isset($is_auth[$row['topic_id']])) ? append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $is_auth[$row['topic_id']] . '&t=' . $row['topic_id']) : false;
1.2765 + $log[$key]['viewlogs'] = (isset($is_mod[$row['topic_id']])) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=logs&mode=topic_logs&t=' . $row['topic_id'], true, $user->session_id) : false;
1.2766 + }
1.2767 + }
1.2768 +
1.2769 + if (sizeof($reportee_id_list))
1.2770 + {
1.2771 + $reportee_id_list = array_unique($reportee_id_list);
1.2772 + $reportee_names_list = array();
1.2773 +
1.2774 + $sql = 'SELECT user_id, username, user_colour
1.2775 + FROM ' . USERS_TABLE . '
1.2776 + WHERE ' . $db->sql_in_set('user_id', $reportee_id_list);
1.2777 + $result = $db->sql_query($sql);
1.2778 +
1.2779 + while ($row = $db->sql_fetchrow($result))
1.2780 + {
1.2781 + $reportee_names_list[$row['user_id']] = $row;
1.2782 + }
1.2783 + $db->sql_freeresult($result);
1.2784 +
1.2785 + foreach ($log as $key => $row)
1.2786 + {
1.2787 + if (!isset($reportee_names_list[$row['reportee_id']]))
1.2788 + {
1.2789 + continue;
1.2790 + }
1.2791 +
1.2792 + $log[$key]['reportee_username'] = $reportee_names_list[$row['reportee_id']]['username'];
1.2793 + $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);
1.2794 + }
1.2795 + }
1.2796 +
1.2797 + return $offset;
1.2798 +}
1.2799 +
1.2800 +/**
1.2801 +* Update foes - remove moderators and administrators from foe lists...
1.2802 +*/
1.2803 +function update_foes($group_id = false, $user_id = false)
1.2804 +{
1.2805 + global $db, $auth;
1.2806 +
1.2807 + // update foes for some user
1.2808 + if (is_array($user_id) && sizeof($user_id))
1.2809 + {
1.2810 + $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
1.2811 + WHERE ' . $db->sql_in_set('zebra_id', $user_id) . '
1.2812 + AND foe = 1';
1.2813 + $db->sql_query($sql);
1.2814 + return;
1.2815 + }
1.2816 +
1.2817 + // update foes for some group
1.2818 + if (is_array($group_id) && sizeof($group_id))
1.2819 + {
1.2820 + // Grab group settings...
1.2821 + $sql = $db->sql_build_query('SELECT', array(
1.2822 + 'SELECT' => 'a.group_id',
1.2823 +
1.2824 + 'FROM' => array(
1.2825 + ACL_OPTIONS_TABLE => 'ao',
1.2826 + ACL_GROUPS_TABLE => 'a'
1.2827 + ),
1.2828 +
1.2829 + 'LEFT_JOIN' => array(
1.2830 + array(
1.2831 + 'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
1.2832 + 'ON' => 'a.auth_role_id = r.role_id'
1.2833 + ),
1.2834 + ),
1.2835 +
1.2836 + 'WHERE' => '(ao.auth_option_id = a.auth_option_id OR ao.auth_option_id = r.auth_option_id)
1.2837 + AND ' . $db->sql_in_set('a.group_id', $group_id) . "
1.2838 + AND ao.auth_option IN ('a_', 'm_')",
1.2839 +
1.2840 + 'GROUP_BY' => 'a.group_id'
1.2841 + ));
1.2842 + $result = $db->sql_query($sql);
1.2843 +
1.2844 + $groups = array();
1.2845 + while ($row = $db->sql_fetchrow($result))
1.2846 + {
1.2847 + $groups[] = (int) $row['group_id'];
1.2848 + }
1.2849 + $db->sql_freeresult($result);
1.2850 +
1.2851 + if (!sizeof($groups))
1.2852 + {
1.2853 + return;
1.2854 + }
1.2855 +
1.2856 + switch ($db->sql_layer)
1.2857 + {
1.2858 + case 'mysqli':
1.2859 + case 'mysql4':
1.2860 + $sql = 'DELETE ' . (($db->sql_layer === 'mysqli' || version_compare($db->sql_server_info(true), '4.1', '>=')) ? 'z.*' : ZEBRA_TABLE) . '
1.2861 + FROM ' . ZEBRA_TABLE . ' z, ' . USER_GROUP_TABLE . ' ug
1.2862 + WHERE z.zebra_id = ug.user_id
1.2863 + AND z.foe = 1
1.2864 + AND ' . $db->sql_in_set('ug.group_id', $groups);
1.2865 + $db->sql_query($sql);
1.2866 + break;
1.2867 +
1.2868 + default:
1.2869 + $sql = 'SELECT user_id
1.2870 + FROM ' . USER_GROUP_TABLE . '
1.2871 + WHERE ' . $db->sql_in_set('group_id', $groups);
1.2872 + $result = $db->sql_query($sql);
1.2873 +
1.2874 + $users = array();
1.2875 + while ($row = $db->sql_fetchrow($result))
1.2876 + {
1.2877 + $users[] = (int) $row['user_id'];
1.2878 + }
1.2879 + $db->sql_freeresult($result);
1.2880 +
1.2881 + if (sizeof($users))
1.2882 + {
1.2883 + $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
1.2884 + WHERE ' . $db->sql_in_set('zebra_id', $users) . '
1.2885 + AND foe = 1';
1.2886 + $db->sql_query($sql);
1.2887 + }
1.2888 + break;
1.2889 + }
1.2890 +
1.2891 + return;
1.2892 + }
1.2893 +
1.2894 + // update foes for everyone
1.2895 + $perms = array();
1.2896 + foreach ($auth->acl_get_list(false, array('a_', 'm_'), false) as $forum_id => $forum_ary)
1.2897 + {
1.2898 + foreach ($forum_ary as $auth_option => $user_ary)
1.2899 + {
1.2900 + $perms = array_merge($perms, $user_ary);
1.2901 + }
1.2902 + }
1.2903 +
1.2904 + if (sizeof($perms))
1.2905 + {
1.2906 + $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
1.2907 + WHERE ' . $db->sql_in_set('zebra_id', array_unique($perms)) . '
1.2908 + AND foe = 1';
1.2909 + $db->sql_query($sql);
1.2910 + }
1.2911 + unset($perms);
1.2912 +}
1.2913 +
1.2914 +/**
1.2915 +* Lists inactive users
1.2916 +*/
1.2917 +function view_inactive_users(&$users, &$user_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'user_inactive_time DESC')
1.2918 +{
1.2919 + global $db, $user;
1.2920 +
1.2921 + $sql = 'SELECT COUNT(user_id) AS user_count
1.2922 + FROM ' . USERS_TABLE . '
1.2923 + WHERE user_type = ' . USER_INACTIVE .
1.2924 + (($limit_days) ? " AND user_inactive_time >= $limit_days" : '');
1.2925 + $result = $db->sql_query($sql);
1.2926 + $user_count = (int) $db->sql_fetchfield('user_count');
1.2927 + $db->sql_freeresult($result);
1.2928 +
1.2929 + if ($user_count == 0)
1.2930 + {
1.2931 + // Save the queries, because there are no users to display
1.2932 + return 0;
1.2933 + }
1.2934 +
1.2935 + if ($offset >= $user_count)
1.2936 + {
1.2937 + $offset = ($offset - $limit < 0) ? 0 : $offset - $limit;
1.2938 + }
1.2939 +
1.2940 + $sql = 'SELECT *
1.2941 + FROM ' . USERS_TABLE . '
1.2942 + WHERE user_type = ' . USER_INACTIVE .
1.2943 + (($limit_days) ? " AND user_inactive_time >= $limit_days" : '') . "
1.2944 + ORDER BY $sort_by";
1.2945 + $result = $db->sql_query_limit($sql, $limit, $offset);
1.2946 +
1.2947 + while ($row = $db->sql_fetchrow($result))
1.2948 + {
1.2949 + $row['inactive_reason'] = $user->lang['INACTIVE_REASON_UNKNOWN'];
1.2950 + switch ($row['user_inactive_reason'])
1.2951 + {
1.2952 + case INACTIVE_REGISTER:
1.2953 + $row['inactive_reason'] = $user->lang['INACTIVE_REASON_REGISTER'];
1.2954 + break;
1.2955 +
1.2956 + case INACTIVE_PROFILE:
1.2957 + $row['inactive_reason'] = $user->lang['INACTIVE_REASON_PROFILE'];
1.2958 + break;
1.2959 +
1.2960 + case INACTIVE_MANUAL:
1.2961 + $row['inactive_reason'] = $user->lang['INACTIVE_REASON_MANUAL'];
1.2962 + break;
1.2963 +
1.2964 + case INACTIVE_REMIND:
1.2965 + $row['inactive_reason'] = $user->lang['INACTIVE_REASON_REMIND'];
1.2966 + break;
1.2967 + }
1.2968 +
1.2969 + $users[] = $row;
1.2970 + }
1.2971 +
1.2972 + return $offset;
1.2973 +}
1.2974 +
1.2975 +/**
1.2976 +* Lists warned users
1.2977 +*/
1.2978 +function view_warned_users(&$users, &$user_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'user_warnings DESC')
1.2979 +{
1.2980 + global $db;
1.2981 +
1.2982 + $sql = 'SELECT user_id, username, user_colour, user_warnings, user_last_warning
1.2983 + FROM ' . USERS_TABLE . '
1.2984 + WHERE user_warnings > 0
1.2985 + ' . (($limit_days) ? "AND user_last_warning >= $limit_days" : '') . "
1.2986 + ORDER BY $sort_by";
1.2987 + $result = $db->sql_query_limit($sql, $limit, $offset);
1.2988 + $users = $db->sql_fetchrowset($result);
1.2989 + $db->sql_freeresult($result);
1.2990 +
1.2991 + $sql = 'SELECT count(user_id) AS user_count
1.2992 + FROM ' . USERS_TABLE . '
1.2993 + WHERE user_warnings > 0
1.2994 + ' . (($limit_days) ? "AND user_last_warning >= $limit_days" : '');
1.2995 + $result = $db->sql_query($sql);
1.2996 + $user_count = (int) $db->sql_fetchfield('user_count');
1.2997 + $db->sql_freeresult($result);
1.2998 +
1.2999 + return;
1.3000 +}
1.3001 +
1.3002 +/**
1.3003 +* Get database size
1.3004 +* Currently only mysql and mssql are supported
1.3005 +*/
1.3006 +function get_database_size()
1.3007 +{
1.3008 + global $db, $user, $table_prefix;
1.3009 +
1.3010 + $database_size = false;
1.3011 +
1.3012 + // This code is heavily influenced by a similar routine in phpMyAdmin 2.2.0
1.3013 + switch ($db->sql_layer)
1.3014 + {
1.3015 + case 'mysql':
1.3016 + case 'mysql4':
1.3017 + case 'mysqli':
1.3018 + $sql = 'SELECT VERSION() AS mysql_version';
1.3019 + $result = $db->sql_query($sql);
1.3020 + $row = $db->sql_fetchrow($result);
1.3021 + $db->sql_freeresult($result);
1.3022 +
1.3023 + if ($row)
1.3024 + {
1.3025 + $version = $row['mysql_version'];
1.3026 +
1.3027 + if (preg_match('#(3\.23|[45]\.)#', $version))
1.3028 + {
1.3029 + $db_name = (preg_match('#^(?:3\.23\.(?:[6-9]|[1-9]{2}))|[45]\.#', $version)) ? "`{$db->dbname}`" : $db->dbname;
1.3030 +
1.3031 + $sql = 'SHOW TABLE STATUS
1.3032 + FROM ' . $db_name;
1.3033 + $result = $db->sql_query($sql, 7200);
1.3034 +
1.3035 + $database_size = 0;
1.3036 + while ($row = $db->sql_fetchrow($result))
1.3037 + {
1.3038 + if ((isset($row['Type']) && $row['Type'] != 'MRG_MyISAM') || (isset($row['Engine']) && ($row['Engine'] == 'MyISAM' || $row['Engine'] == 'InnoDB')))
1.3039 + {
1.3040 + if ($table_prefix != '')
1.3041 + {
1.3042 + if (strpos($row['Name'], $table_prefix) !== false)
1.3043 + {
1.3044 + $database_size += $row['Data_length'] + $row['Index_length'];
1.3045 + }
1.3046 + }
1.3047 + else
1.3048 + {
1.3049 + $database_size += $row['Data_length'] + $row['Index_length'];
1.3050 + }
1.3051 + }
1.3052 + }
1.3053 + $db->sql_freeresult($result);
1.3054 + }
1.3055 + }
1.3056 + break;
1.3057 +
1.3058 + case 'firebird':
1.3059 + global $dbname;
1.3060 +
1.3061 + // if it on the local machine, we can get lucky
1.3062 + if (file_exists($dbname))
1.3063 + {
1.3064 + $database_size = filesize($dbname);
1.3065 + }
1.3066 +
1.3067 + break;
1.3068 +
1.3069 + case 'sqlite':
1.3070 + global $dbhost;
1.3071 +
1.3072 + if (file_exists($dbhost))
1.3073 + {
1.3074 + $database_size = filesize($dbhost);
1.3075 + }
1.3076 +
1.3077 + break;
1.3078 +
1.3079 + case 'mssql':
1.3080 + case 'mssql_odbc':
1.3081 + case 'mssqlnative':
1.3082 + $sql = 'SELECT ((SUM(size) * 8.0) * 1024.0) as dbsize
1.3083 + FROM sysfiles';
1.3084 + $result = $db->sql_query($sql, 7200);
1.3085 + $database_size = ($row = $db->sql_fetchrow($result)) ? $row['dbsize'] : false;
1.3086 + $db->sql_freeresult($result);
1.3087 + break;
1.3088 +
1.3089 + case 'postgres':
1.3090 + $sql = "SELECT proname
1.3091 + FROM pg_proc
1.3092 + WHERE proname = 'pg_database_size'";
1.3093 + $result = $db->sql_query($sql);
1.3094 + $row = $db->sql_fetchrow($result);
1.3095 + $db->sql_freeresult($result);
1.3096 +
1.3097 + if ($row['proname'] == 'pg_database_size')
1.3098 + {
1.3099 + $database = $db->dbname;
1.3100 + if (strpos($database, '.') !== false)
1.3101 + {
1.3102 + list($database, ) = explode('.', $database);
1.3103 + }
1.3104 +
1.3105 + $sql = "SELECT oid
1.3106 + FROM pg_database
1.3107 + WHERE datname = '$database'";
1.3108 + $result = $db->sql_query($sql);
1.3109 + $row = $db->sql_fetchrow($result);
1.3110 + $db->sql_freeresult($result);
1.3111 +
1.3112 + $oid = $row['oid'];
1.3113 +
1.3114 + $sql = 'SELECT pg_database_size(' . $oid . ') as size';
1.3115 + $result = $db->sql_query($sql);
1.3116 + $row = $db->sql_fetchrow($result);
1.3117 + $db->sql_freeresult($result);
1.3118 +
1.3119 + $database_size = $row['size'];
1.3120 + }
1.3121 + break;
1.3122 +
1.3123 + case 'oracle':
1.3124 + $sql = 'SELECT SUM(bytes) as dbsize
1.3125 + FROM user_segments';
1.3126 + $result = $db->sql_query($sql, 7200);
1.3127 + $database_size = ($row = $db->sql_fetchrow($result)) ? $row['dbsize'] : false;
1.3128 + $db->sql_freeresult($result);
1.3129 + break;
1.3130 + }
1.3131 +
1.3132 + $database_size = ($database_size !== false) ? get_formatted_filesize($database_size) : $user->lang['NOT_AVAILABLE'];
1.3133 +
1.3134 + return $database_size;
1.3135 +}
1.3136 +
1.3137 +/**
1.3138 +* Retrieve contents from remotely stored file
1.3139 +*/
1.3140 +function get_remote_file($host, $directory, $filename, &$errstr, &$errno, $port = 80, $timeout = 6)
1.3141 +{
1.3142 + global $user;
1.3143 +
1.3144 + if ($fsock = @fsockopen($host, $port, $errno, $errstr, $timeout))
1.3145 + {
1.3146 + @fputs($fsock, "GET $directory/$filename HTTP/1.1\r\n");
1.3147 + @fputs($fsock, "HOST: $host\r\n");
1.3148 + @fputs($fsock, "Connection: close\r\n\r\n");
1.3149 +
1.3150 + $timer_stop = time() + $timeout;
1.3151 + stream_set_timeout($fsock, $timeout);
1.3152 +
1.3153 + $file_info = '';
1.3154 + $get_info = false;
1.3155 +
1.3156 + while (!@feof($fsock))
1.3157 + {
1.3158 + if ($get_info)
1.3159 + {
1.3160 + $file_info .= @fread($fsock, 1024);
1.3161 + }
1.3162 + else
1.3163 + {
1.3164 + $line = @fgets($fsock, 1024);
1.3165 + if ($line == "\r\n")
1.3166 + {
1.3167 + $get_info = true;
1.3168 + }
1.3169 + else if (stripos($line, '404 not found') !== false)
1.3170 + {
1.3171 + $errstr = $user->lang['FILE_NOT_FOUND'] . ': ' . $filename;
1.3172 + return false;
1.3173 + }
1.3174 + }
1.3175 +
1.3176 + $stream_meta_data = stream_get_meta_data($fsock);
1.3177 +
1.3178 + if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop)
1.3179 + {
1.3180 + $errstr = $user->lang['FSOCK_TIMEOUT'];
1.3181 + return false;
1.3182 + }
1.3183 + }
1.3184 + @fclose($fsock);
1.3185 + }
1.3186 + else
1.3187 + {
1.3188 + if ($errstr)
1.3189 + {
1.3190 + $errstr = utf8_convert_message($errstr);
1.3191 + return false;
1.3192 + }
1.3193 + else
1.3194 + {
1.3195 + $errstr = $user->lang['FSOCK_DISABLED'];
1.3196 + return false;
1.3197 + }
1.3198 + }
1.3199 +
1.3200 + return $file_info;
1.3201 +}
1.3202 +
1.3203 +/**
1.3204 +* Tidy Warnings
1.3205 +* Remove all warnings which have now expired from the database
1.3206 +* The duration of a warning can be defined by the administrator
1.3207 +* This only removes the warning and reduces the associated count,
1.3208 +* it does not remove the user note recording the contents of the warning
1.3209 +*/
1.3210 +function tidy_warnings()
1.3211 +{
1.3212 + global $db, $config;
1.3213 +
1.3214 + $expire_date = time() - ($config['warnings_expire_days'] * 86400);
1.3215 + $warning_list = $user_list = array();
1.3216 +
1.3217 + $sql = 'SELECT * FROM ' . WARNINGS_TABLE . "
1.3218 + WHERE warning_time < $expire_date";
1.3219 + $result = $db->sql_query($sql);
1.3220 +
1.3221 + while ($row = $db->sql_fetchrow($result))
1.3222 + {
1.3223 + $warning_list[] = $row['warning_id'];
1.3224 + $user_list[$row['user_id']] = isset($user_list[$row['user_id']]) ? ++$user_list[$row['user_id']] : 1;
1.3225 + }
1.3226 + $db->sql_freeresult($result);
1.3227 +
1.3228 + if (sizeof($warning_list))
1.3229 + {
1.3230 + $db->sql_transaction('begin');
1.3231 +
1.3232 + $sql = 'DELETE FROM ' . WARNINGS_TABLE . '
1.3233 + WHERE ' . $db->sql_in_set('warning_id', $warning_list);
1.3234 + $db->sql_query($sql);
1.3235 +
1.3236 + foreach ($user_list as $user_id => $value)
1.3237 + {
1.3238 + $sql = 'UPDATE ' . USERS_TABLE . " SET user_warnings = user_warnings - $value
1.3239 + WHERE user_id = $user_id";
1.3240 + $db->sql_query($sql);
1.3241 + }
1.3242 +
1.3243 + $db->sql_transaction('commit');
1.3244 + }
1.3245 +
1.3246 + set_config('warnings_last_gc', time(), true);
1.3247 +}
1.3248 +
1.3249 +/**
1.3250 +* Tidy database, doing some maintanance tasks
1.3251 +*/
1.3252 +function tidy_database()
1.3253 +{
1.3254 + global $db;
1.3255 +
1.3256 + // Here we check permission consistency
1.3257 +
1.3258 + // Sometimes, it can happen permission tables having forums listed which do not exist
1.3259 + $sql = 'SELECT forum_id
1.3260 + FROM ' . FORUMS_TABLE;
1.3261 + $result = $db->sql_query($sql);
1.3262 +
1.3263 + $forum_ids = array(0);
1.3264 + while ($row = $db->sql_fetchrow($result))
1.3265 + {
1.3266 + $forum_ids[] = $row['forum_id'];
1.3267 + }
1.3268 + $db->sql_freeresult($result);
1.3269 +
1.3270 + // Delete those rows from the acl tables not having listed the forums above
1.3271 + $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
1.3272 + WHERE ' . $db->sql_in_set('forum_id', $forum_ids, true);
1.3273 + $db->sql_query($sql);
1.3274 +
1.3275 + $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
1.3276 + WHERE ' . $db->sql_in_set('forum_id', $forum_ids, true);
1.3277 + $db->sql_query($sql);
1.3278 +
1.3279 + set_config('database_last_gc', time(), true);
1.3280 +}
1.3281 +
1.3282 +/**
1.3283 +* Add permission language - this will make sure custom files will be included
1.3284 +*/
1.3285 +function add_permission_language()
1.3286 +{
1.3287 + global $user, $phpEx;
1.3288 +
1.3289 + // First of all, our own file. We need to include it as the first file because it presets all relevant variables.
1.3290 + $user->add_lang('acp/permissions_phpbb');
1.3291 +
1.3292 + $files_to_add = array();
1.3293 +
1.3294 + // Now search in acp and mods folder for permissions_ files.
1.3295 + foreach (array('acp/', 'mods/') as $path)
1.3296 + {
1.3297 + $dh = @opendir($user->lang_path . $user->lang_name . '/' . $path);
1.3298 +
1.3299 + if ($dh)
1.3300 + {
1.3301 + while (($file = readdir($dh)) !== false)
1.3302 + {
1.3303 + if ($file !== 'permissions_phpbb.' . $phpEx && strpos($file, 'permissions_') === 0 && substr($file, -(strlen($phpEx) + 1)) === '.' . $phpEx)
1.3304 + {
1.3305 + $files_to_add[] = $path . substr($file, 0, -(strlen($phpEx) + 1));
1.3306 + }
1.3307 + }
1.3308 + closedir($dh);
1.3309 + }
1.3310 + }
1.3311 +
1.3312 + if (!sizeof($files_to_add))
1.3313 + {
1.3314 + return false;
1.3315 + }
1.3316 +
1.3317 + $user->add_lang($files_to_add);
1.3318 + return true;
1.3319 +}
1.3320 +
1.3321 +/**
1.3322 + * Obtains the latest version information
1.3323 + *
1.3324 + * @param bool $force_update Ignores cached data. Defaults to false.
1.3325 + * @param bool $warn_fail Trigger a warning if obtaining the latest version information fails. Defaults to false.
1.3326 + * @param int $ttl Cache version information for $ttl seconds. Defaults to 86400 (24 hours).
1.3327 + *
1.3328 + * @return string | false Version info on success, false on failure.
1.3329 + */
1.3330 +function obtain_latest_version_info($force_update = false, $warn_fail = false, $ttl = 86400)
1.3331 +{
1.3332 + global $cache;
1.3333 +
1.3334 + $info = $cache->get('versioncheck');
1.3335 +
1.3336 + if ($info === false || $force_update)
1.3337 + {
1.3338 + $errstr = '';
1.3339 + $errno = 0;
1.3340 +
1.3341 + $info = get_remote_file('version.phpbb.com', '/phpbb',
1.3342 + ((defined('PHPBB_QA')) ? '30x_qa.txt' : '30x.txt'), $errstr, $errno);
1.3343 +
1.3344 + if ($info === false)
1.3345 + {
1.3346 + $cache->destroy('versioncheck');
1.3347 + if ($warn_fail)
1.3348 + {
1.3349 + trigger_error($errstr, E_USER_WARNING);
1.3350 + }
1.3351 + return false;
1.3352 + }
1.3353 +
1.3354 + $cache->put('versioncheck', $info, $ttl);
1.3355 + }
1.3356 +
1.3357 + return $info;
1.3358 +}
1.3359 +
1.3360 +/**
1.3361 + * Enables a particular flag in a bitfield column of a given table.
1.3362 + *
1.3363 + * @param string $table_name The table to update
1.3364 + * @param string $column_name The column containing a bitfield to update
1.3365 + * @param int $flag The binary flag which is OR-ed with the current column value
1.3366 + * @param string $sql_more This string is attached to the sql query generated to update the table.
1.3367 + *
1.3368 + * @return void
1.3369 + */
1.3370 +function enable_bitfield_column_flag($table_name, $column_name, $flag, $sql_more = '')
1.3371 +{
1.3372 + global $db;
1.3373 +
1.3374 + $sql = 'UPDATE ' . $table_name . '
1.3375 + SET ' . $column_name . ' = ' . $db->sql_bit_or($column_name, $flag) . '
1.3376 + ' . $sql_more;
1.3377 + $db->sql_query($sql);
1.3378 +}
1.3379 +
1.3380 +?>
1.3381 \ No newline at end of file