1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/acp/acp_prune.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,466 @@
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 +* @package acp
1.24 +*/
1.25 +class acp_prune
1.26 +{
1.27 + var $u_action;
1.28 +
1.29 + function main($id, $mode)
1.30 + {
1.31 + global $user, $phpEx, $phpbb_admin_path, $phpbb_root_path;
1.32 +
1.33 + $user->add_lang('acp/prune');
1.34 + include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
1.35 +
1.36 + switch ($mode)
1.37 + {
1.38 + case 'forums':
1.39 + $this->tpl_name = 'acp_prune_forums';
1.40 + $this->page_title = 'ACP_PRUNE_FORUMS';
1.41 + $this->prune_forums($id, $mode);
1.42 + break;
1.43 +
1.44 + case 'users':
1.45 + $this->tpl_name = 'acp_prune_users';
1.46 + $this->page_title = 'ACP_PRUNE_USERS';
1.47 + $this->prune_users($id, $mode);
1.48 + break;
1.49 + }
1.50 + }
1.51 +
1.52 + /**
1.53 + * Prune forums
1.54 + */
1.55 + function prune_forums($id, $mode)
1.56 + {
1.57 + global $db, $user, $auth, $template, $cache;
1.58 + global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
1.59 +
1.60 + $all_forums = request_var('all_forums', 0);
1.61 + $forum_id = request_var('f', array(0));
1.62 + $submit = (isset($_POST['submit'])) ? true : false;
1.63 +
1.64 + if ($all_forums)
1.65 + {
1.66 + $sql = 'SELECT forum_id
1.67 + FROM ' . FORUMS_TABLE . '
1.68 + ORDER BY left_id';
1.69 + $result = $db->sql_query($sql);
1.70 +
1.71 + $forum_id = array();
1.72 + while ($row = $db->sql_fetchrow($result))
1.73 + {
1.74 + $forum_id[] = $row['forum_id'];
1.75 + }
1.76 + $db->sql_freeresult($result);
1.77 + }
1.78 +
1.79 + if ($submit)
1.80 + {
1.81 + if (confirm_box(true))
1.82 + {
1.83 + $prune_posted = request_var('prune_days', 0);
1.84 + $prune_viewed = request_var('prune_vieweddays', 0);
1.85 + $prune_all = (!$prune_posted && !$prune_viewed) ? true : false;
1.86 +
1.87 + $prune_flags = 0;
1.88 + $prune_flags += (request_var('prune_old_polls', 0)) ? 2 : 0;
1.89 + $prune_flags += (request_var('prune_announce', 0)) ? 4 : 0;
1.90 + $prune_flags += (request_var('prune_sticky', 0)) ? 8 : 0;
1.91 +
1.92 + // Convert days to seconds for timestamp functions...
1.93 + $prunedate_posted = time() - ($prune_posted * 86400);
1.94 + $prunedate_viewed = time() - ($prune_viewed * 86400);
1.95 +
1.96 + $template->assign_vars(array(
1.97 + 'S_PRUNED' => true)
1.98 + );
1.99 +
1.100 + $sql_forum = (sizeof($forum_id)) ? ' AND ' . $db->sql_in_set('forum_id', $forum_id) : '';
1.101 +
1.102 + // Get a list of forum's or the data for the forum that we are pruning.
1.103 + $sql = 'SELECT forum_id, forum_name
1.104 + FROM ' . FORUMS_TABLE . '
1.105 + WHERE forum_type = ' . FORUM_POST . "
1.106 + $sql_forum
1.107 + ORDER BY left_id ASC";
1.108 + $result = $db->sql_query($sql);
1.109 +
1.110 + if ($row = $db->sql_fetchrow($result))
1.111 + {
1.112 + $prune_ids = array();
1.113 + $p_result['topics'] = 0;
1.114 + $p_result['posts'] = 0;
1.115 + $log_data = '';
1.116 +
1.117 + do
1.118 + {
1.119 + if (!$auth->acl_get('f_list', $row['forum_id']))
1.120 + {
1.121 + continue;
1.122 + }
1.123 +
1.124 + if ($prune_all)
1.125 + {
1.126 + $p_result = prune($row['forum_id'], 'posted', time(), $prune_flags, false);
1.127 + }
1.128 + else
1.129 + {
1.130 + if ($prune_posted)
1.131 + {
1.132 + $return = prune($row['forum_id'], 'posted', $prunedate_posted, $prune_flags, false);
1.133 + $p_result['topics'] += $return['topics'];
1.134 + $p_result['posts'] += $return['posts'];
1.135 + }
1.136 +
1.137 + if ($prune_viewed)
1.138 + {
1.139 + $return = prune($row['forum_id'], 'viewed', $prunedate_viewed, $prune_flags, false);
1.140 + $p_result['topics'] += $return['topics'];
1.141 + $p_result['posts'] += $return['posts'];
1.142 + }
1.143 + }
1.144 +
1.145 + $prune_ids[] = $row['forum_id'];
1.146 +
1.147 + $template->assign_block_vars('pruned', array(
1.148 + 'FORUM_NAME' => $row['forum_name'],
1.149 + 'NUM_TOPICS' => $p_result['topics'],
1.150 + 'NUM_POSTS' => $p_result['posts'])
1.151 + );
1.152 +
1.153 + $log_data .= (($log_data != '') ? ', ' : '') . $row['forum_name'];
1.154 + }
1.155 + while ($row = $db->sql_fetchrow($result));
1.156 +
1.157 + // Sync all pruned forums at once
1.158 + sync('forum', 'forum_id', $prune_ids, true, true);
1.159 + add_log('admin', 'LOG_PRUNE', $log_data);
1.160 + }
1.161 + $db->sql_freeresult($result);
1.162 +
1.163 + return;
1.164 + }
1.165 + else
1.166 + {
1.167 + confirm_box(false, $user->lang['PRUNE_FORUM_CONFIRM'], build_hidden_fields(array(
1.168 + 'i' => $id,
1.169 + 'mode' => $mode,
1.170 + 'submit' => 1,
1.171 + 'all_forums' => $all_forums,
1.172 + 'f' => $forum_id,
1.173 +
1.174 + 'prune_days' => request_var('prune_days', 0),
1.175 + 'prune_vieweddays' => request_var('prune_vieweddays', 0),
1.176 + 'prune_old_polls' => request_var('prune_old_polls', 0),
1.177 + 'prune_announce' => request_var('prune_announce', 0),
1.178 + 'prune_sticky' => request_var('prune_sticky', 0),
1.179 + )));
1.180 + }
1.181 + }
1.182 +
1.183 + // If they haven't selected a forum for pruning yet then
1.184 + // display a select box to use for pruning.
1.185 + if (!sizeof($forum_id))
1.186 + {
1.187 + $template->assign_vars(array(
1.188 + 'U_ACTION' => $this->u_action,
1.189 + 'S_SELECT_FORUM' => true,
1.190 + 'S_FORUM_OPTIONS' => make_forum_select(false, false, false))
1.191 + );
1.192 + }
1.193 + else
1.194 + {
1.195 + $sql = 'SELECT forum_id, forum_name
1.196 + FROM ' . FORUMS_TABLE . '
1.197 + WHERE ' . $db->sql_in_set('forum_id', $forum_id);
1.198 + $result = $db->sql_query($sql);
1.199 + $row = $db->sql_fetchrow($result);
1.200 +
1.201 + if (!$row)
1.202 + {
1.203 + $db->sql_freeresult($result);
1.204 + trigger_error($user->lang['NO_FORUM'] . adm_back_link($this->u_action), E_USER_WARNING);
1.205 + }
1.206 +
1.207 + $forum_list = $s_hidden_fields = '';
1.208 + do
1.209 + {
1.210 + $forum_list .= (($forum_list != '') ? ', ' : '') . '<b>' . $row['forum_name'] . '</b>';
1.211 + $s_hidden_fields .= '<input type="hidden" name="f[]" value="' . $row['forum_id'] . '" />';
1.212 + }
1.213 + while ($row = $db->sql_fetchrow($result));
1.214 +
1.215 + $db->sql_freeresult($result);
1.216 +
1.217 + $l_selected_forums = (sizeof($forum_id) == 1) ? 'SELECTED_FORUM' : 'SELECTED_FORUMS';
1.218 +
1.219 + $template->assign_vars(array(
1.220 + 'L_SELECTED_FORUMS' => $user->lang[$l_selected_forums],
1.221 + 'U_ACTION' => $this->u_action,
1.222 + 'U_BACK' => $this->u_action,
1.223 + 'FORUM_LIST' => $forum_list,
1.224 + 'S_HIDDEN_FIELDS' => $s_hidden_fields)
1.225 + );
1.226 + }
1.227 + }
1.228 +
1.229 + /**
1.230 + * Prune users
1.231 + */
1.232 + function prune_users($id, $mode)
1.233 + {
1.234 + global $db, $user, $auth, $template, $cache;
1.235 + global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
1.236 +
1.237 + $user->add_lang('memberlist');
1.238 +
1.239 + $prune = (isset($_POST['prune'])) ? true : false;
1.240 +
1.241 + if ($prune)
1.242 + {
1.243 + $action = request_var('action', 'deactivate');
1.244 + $deleteposts = request_var('deleteposts', 0);
1.245 +
1.246 + if (confirm_box(true))
1.247 + {
1.248 + $user_ids = $usernames = array();
1.249 + $this->get_prune_users($user_ids, $usernames);
1.250 +
1.251 + if (sizeof($user_ids))
1.252 + {
1.253 + if ($action == 'deactivate')
1.254 + {
1.255 + user_active_flip('deactivate', $user_ids);
1.256 + $l_log = 'LOG_PRUNE_USER_DEAC';
1.257 + }
1.258 + else if ($action == 'delete')
1.259 + {
1.260 + if ($deleteposts)
1.261 + {
1.262 + foreach ($user_ids as $user_id)
1.263 + {
1.264 + user_delete('remove', $user_id);
1.265 + }
1.266 +
1.267 + $l_log = 'LOG_PRUNE_USER_DEL_DEL';
1.268 + }
1.269 + else
1.270 + {
1.271 + foreach ($user_ids as $user_id)
1.272 + {
1.273 + user_delete('retain', $user_id, $usernames[$user_id]);
1.274 + }
1.275 +
1.276 + $l_log = 'LOG_PRUNE_USER_DEL_ANON';
1.277 + }
1.278 + }
1.279 +
1.280 + add_log('admin', $l_log, implode(', ', $usernames));
1.281 + $msg = $user->lang['USER_' . strtoupper($action) . '_SUCCESS'];
1.282 + }
1.283 + else
1.284 + {
1.285 + $msg = $user->lang['USER_PRUNE_FAILURE'];
1.286 + }
1.287 +
1.288 + trigger_error($msg . adm_back_link($this->u_action));
1.289 + }
1.290 + else
1.291 + {
1.292 + // We list the users which will be pruned...
1.293 + $user_ids = $usernames = array();
1.294 + $this->get_prune_users($user_ids, $usernames);
1.295 +
1.296 + if (!sizeof($user_ids))
1.297 + {
1.298 + trigger_error($user->lang['USER_PRUNE_FAILURE'] . adm_back_link($this->u_action), E_USER_WARNING);
1.299 + }
1.300 +
1.301 + // Assign to template
1.302 + foreach ($user_ids as $user_id)
1.303 + {
1.304 + $template->assign_block_vars('users', array(
1.305 + 'USERNAME' => $usernames[$user_id],
1.306 + 'U_PROFILE' => append_sid($phpbb_root_path . 'memberlist.' . $phpEx, 'mode=viewprofile&u=' . $user_id),
1.307 + 'U_USER_ADMIN' => ($auth->acl_get('a_user')) ? append_sid("{$phpbb_admin_path}index.$phpEx", 'i=users&mode=overview&u=' . $user_id, true, $user->session_id) : '',
1.308 + ));
1.309 + }
1.310 +
1.311 + $template->assign_vars(array(
1.312 + 'S_DEACTIVATE' => ($action == 'deactivate') ? true : false,
1.313 + 'S_DELETE' => ($action == 'delete') ? true : false,
1.314 + ));
1.315 +
1.316 + confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
1.317 + 'i' => $id,
1.318 + 'mode' => $mode,
1.319 + 'prune' => 1,
1.320 +
1.321 + 'users' => utf8_normalize_nfc(request_var('users', '', true)),
1.322 + 'username' => utf8_normalize_nfc(request_var('username', '', true)),
1.323 + 'email' => request_var('email', ''),
1.324 + 'joined_select' => request_var('joined_select', ''),
1.325 + 'joined' => request_var('joined', ''),
1.326 + 'active_select' => request_var('active_select', ''),
1.327 + 'active' => request_var('active', ''),
1.328 + 'count_select' => request_var('count_select', ''),
1.329 + 'count' => request_var('count', ''),
1.330 + 'deleteposts' => request_var('deleteposts', 0),
1.331 +
1.332 + 'action' => request_var('action', ''),
1.333 + )), 'confirm_body_prune.html');
1.334 + }
1.335 + }
1.336 +
1.337 + $find_count = array('lt' => $user->lang['LESS_THAN'], 'eq' => $user->lang['EQUAL_TO'], 'gt' => $user->lang['MORE_THAN']);
1.338 + $s_find_count = '';
1.339 +
1.340 + foreach ($find_count as $key => $value)
1.341 + {
1.342 + $selected = ($key == 'eq') ? ' selected="selected"' : '';
1.343 + $s_find_count .= '<option value="' . $key . '"' . $selected . '>' . $value . '</option>';
1.344 + }
1.345 +
1.346 + $find_time = array('lt' => $user->lang['BEFORE'], 'gt' => $user->lang['AFTER']);
1.347 + $s_find_join_time = '';
1.348 + foreach ($find_time as $key => $value)
1.349 + {
1.350 + $s_find_join_time .= '<option value="' . $key . '">' . $value . '</option>';
1.351 + }
1.352 +
1.353 + $s_find_active_time = '';
1.354 + foreach ($find_time as $key => $value)
1.355 + {
1.356 + $s_find_active_time .= '<option value="' . $key . '">' . $value . '</option>';
1.357 + }
1.358 +
1.359 + $template->assign_vars(array(
1.360 + 'U_ACTION' => $this->u_action,
1.361 + 'S_JOINED_OPTIONS' => $s_find_join_time,
1.362 + 'S_ACTIVE_OPTIONS' => $s_find_active_time,
1.363 + 'S_COUNT_OPTIONS' => $s_find_count,
1.364 + 'U_FIND_USERNAME' => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&form=acp_prune&field=users'),
1.365 + ));
1.366 + }
1.367 +
1.368 + /**
1.369 + * Get user_ids/usernames from those being pruned
1.370 + */
1.371 + function get_prune_users(&$user_ids, &$usernames)
1.372 + {
1.373 + global $user, $db;
1.374 +
1.375 + $users = utf8_normalize_nfc(request_var('users', '', true));
1.376 +
1.377 + if ($users)
1.378 + {
1.379 + $users = explode("\n", $users);
1.380 + $where_sql = ' AND ' . $db->sql_in_set('username_clean', array_map('utf8_clean_string', $users));
1.381 + }
1.382 + else
1.383 + {
1.384 + $username = utf8_normalize_nfc(request_var('username', '', true));
1.385 + $email = request_var('email', '');
1.386 +
1.387 + $joined_select = request_var('joined_select', 'lt');
1.388 + $active_select = request_var('active_select', 'lt');
1.389 + $count_select = request_var('count_select', 'eq');
1.390 + $joined = request_var('joined', '');
1.391 + $active = request_var('active', '');
1.392 +
1.393 + $active = ($active) ? explode('-', $active) : array();
1.394 + $joined = ($joined) ? explode('-', $joined) : array();
1.395 +
1.396 + if ((sizeof($active) && sizeof($active) != 3) || (sizeof($joined) && sizeof($joined) != 3))
1.397 + {
1.398 + trigger_error($user->lang['WRONG_ACTIVE_JOINED_DATE'] . adm_back_link($this->u_action), E_USER_WARNING);
1.399 + }
1.400 +
1.401 + $count = request_var('count', '');
1.402 +
1.403 + $key_match = array('lt' => '<', 'gt' => '>', 'eq' => '=');
1.404 + $sort_by_types = array('username', 'user_email', 'user_posts', 'user_regdate', 'user_lastvisit');
1.405 +
1.406 + $where_sql = '';
1.407 + $where_sql .= ($username) ? ' AND username_clean ' . $db->sql_like_expression(str_replace('*', $db->any_char, utf8_clean_string($username))) : '';
1.408 + $where_sql .= ($email) ? ' AND user_email ' . $db->sql_like_expression(str_replace('*', $db->any_char, $email)) . ' ' : '';
1.409 + $where_sql .= (sizeof($joined)) ? " AND user_regdate " . $key_match[$joined_select] . ' ' . gmmktime(0, 0, 0, (int) $joined[1], (int) $joined[2], (int) $joined[0]) : '';
1.410 + $where_sql .= ($count !== '') ? " AND user_posts " . $key_match[$count_select] . ' ' . (int) $count . ' ' : '';
1.411 +
1.412 + // First handle pruning of users who never logged in, last active date is 0000-00-00
1.413 + if (sizeof($active) && (int) $active[0] == 0 && (int) $active[1] == 0 && (int) $active[2] == 0)
1.414 + {
1.415 + $where_sql .= ' AND user_lastvisit = 0';
1.416 + }
1.417 + else if (sizeof($active) && $active_select != 'lt')
1.418 + {
1.419 + $where_sql .= ' AND user_lastvisit ' . $key_match[$active_select] . ' ' . gmmktime(0, 0, 0, (int) $active[1], (int) $active[2], (int) $active[0]);
1.420 + }
1.421 + else if (sizeof($active))
1.422 + {
1.423 + $where_sql .= ' AND (user_lastvisit > 0 AND user_lastvisit < ' . gmmktime(0, 0, 0, (int) $active[1], (int) $active[2], (int) $active[0]) . ')';
1.424 + }
1.425 + }
1.426 +
1.427 + // Protect the admin, do not prune if no options are given...
1.428 + if (!$where_sql)
1.429 + {
1.430 + return;
1.431 + }
1.432 +
1.433 + // Get bot ids
1.434 + $sql = 'SELECT user_id
1.435 + FROM ' . BOTS_TABLE;
1.436 + $result = $db->sql_query($sql);
1.437 +
1.438 + $bot_ids = array();
1.439 + while ($row = $db->sql_fetchrow($result))
1.440 + {
1.441 + $bot_ids[] = $row['user_id'];
1.442 + }
1.443 + $db->sql_freeresult($result);
1.444 +
1.445 + // Do not prune founder members
1.446 + $sql = 'SELECT user_id, username
1.447 + FROM ' . USERS_TABLE . '
1.448 + WHERE user_id <> ' . ANONYMOUS . '
1.449 + AND user_type <> ' . USER_FOUNDER . "
1.450 + $where_sql";
1.451 + $result = $db->sql_query($sql);
1.452 +
1.453 + $where_sql = '';
1.454 + $user_ids = $usernames = array();
1.455 +
1.456 + while ($row = $db->sql_fetchrow($result))
1.457 + {
1.458 + // Do not prune bots and the user currently pruning.
1.459 + if ($row['user_id'] != $user->data['user_id'] && !in_array($row['user_id'], $bot_ids))
1.460 + {
1.461 + $user_ids[] = $row['user_id'];
1.462 + $usernames[$row['user_id']] = $row['username'];
1.463 + }
1.464 + }
1.465 + $db->sql_freeresult($result);
1.466 + }
1.467 +}
1.468 +
1.469 +?>
1.470 \ No newline at end of file