Blame Identity/Webenv/phpBB/3.0.4/includes/acp/acp_prune.php

ef5584
ef5584
/**
ef5584
*
ef5584
* @package acp
ef5584
* @version $Id: acp_prune.php 8479 2008-03-29 00:22:48Z naderman $
ef5584
* @copyright (c) 2005 phpBB Group
ef5584
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
ef5584
*
ef5584
*/
ef5584
ef5584
/**
ef5584
* @ignore
ef5584
*/
ef5584
if (!defined('IN_PHPBB'))
ef5584
{
ef5584
	exit;
ef5584
}
ef5584
ef5584
/**
ef5584
* @package acp
ef5584
*/
ef5584
class acp_prune
ef5584
{
ef5584
	var $u_action;
ef5584
ef5584
	function main($id, $mode)
ef5584
	{
ef5584
		global $user, $phpEx, $phpbb_admin_path, $phpbb_root_path;
ef5584
ef5584
		$user->add_lang('acp/prune');
ef5584
		include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
ef5584
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'forums':
ef5584
				$this->tpl_name = 'acp_prune_forums';
ef5584
				$this->page_title = 'ACP_PRUNE_FORUMS';
ef5584
				$this->prune_forums($id, $mode);
ef5584
			break;
ef5584
ef5584
			case 'users':
ef5584
				$this->tpl_name = 'acp_prune_users';
ef5584
				$this->page_title = 'ACP_PRUNE_USERS';
ef5584
				$this->prune_users($id, $mode);
ef5584
			break;
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Prune forums
ef5584
	*/
ef5584
	function prune_forums($id, $mode)
ef5584
	{
ef5584
		global $db, $user, $auth, $template, $cache;
ef5584
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
ef5584
ef5584
		$all_forums = request_var('all_forums', 0);
ef5584
		$forum_id = request_var('f', array(0));
ef5584
		$submit = (isset($_POST['submit'])) ? true : false;
ef5584
ef5584
		if ($all_forums)
ef5584
		{
ef5584
			$sql = 'SELECT forum_id
ef5584
				FROM ' . FORUMS_TABLE . '
ef5584
				ORDER BY left_id';
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			$forum_id = array();
ef5584
			while ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				$forum_id[] = $row['forum_id'];
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
		}
ef5584
ef5584
		if ($submit)
ef5584
		{
ef5584
			if (confirm_box(true))
ef5584
			{
ef5584
				$prune_posted = request_var('prune_days', 0);
ef5584
				$prune_viewed = request_var('prune_vieweddays', 0);
ef5584
				$prune_all = (!$prune_posted && !$prune_viewed) ? true : false;
ef5584
		
ef5584
				$prune_flags = 0;
ef5584
				$prune_flags += (request_var('prune_old_polls', 0)) ? 2 : 0;
ef5584
				$prune_flags += (request_var('prune_announce', 0)) ? 4 : 0;
ef5584
				$prune_flags += (request_var('prune_sticky', 0)) ? 8 : 0;
ef5584
ef5584
				// Convert days to seconds for timestamp functions...
ef5584
				$prunedate_posted = time() - ($prune_posted * 86400);
ef5584
				$prunedate_viewed = time() - ($prune_viewed * 86400);
ef5584
ef5584
				$template->assign_vars(array(
ef5584
					'S_PRUNED'		=> true)
ef5584
				);
ef5584
ef5584
				$sql_forum = (sizeof($forum_id)) ? ' AND ' . $db->sql_in_set('forum_id', $forum_id) : '';
ef5584
ef5584
				// Get a list of forum's or the data for the forum that we are pruning.
ef5584
				$sql = 'SELECT forum_id, forum_name
ef5584
					FROM ' . FORUMS_TABLE . '
ef5584
					WHERE forum_type = ' . FORUM_POST . "
ef5584
						$sql_forum
ef5584
					ORDER BY left_id ASC";
ef5584
				$result = $db->sql_query($sql);
ef5584
ef5584
				if ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$prune_ids = array();
ef5584
					$p_result['topics'] = 0;
ef5584
					$p_result['posts'] = 0;
ef5584
					$log_data = '';
ef5584
			
ef5584
					do
ef5584
					{
ef5584
						if (!$auth->acl_get('f_list', $row['forum_id']))
ef5584
						{
ef5584
							continue;
ef5584
						}
ef5584
ef5584
						if ($prune_all)
ef5584
						{
ef5584
							$p_result = prune($row['forum_id'], 'posted', time(), $prune_flags, false);
ef5584
						}
ef5584
						else
ef5584
						{
ef5584
							if ($prune_posted)
ef5584
							{
ef5584
								$return = prune($row['forum_id'], 'posted', $prunedate_posted, $prune_flags, false);
ef5584
								$p_result['topics'] += $return['topics'];
ef5584
								$p_result['posts'] += $return['posts'];
ef5584
							}
ef5584
			
ef5584
							if ($prune_viewed)
ef5584
							{
ef5584
								$return = prune($row['forum_id'], 'viewed', $prunedate_viewed, $prune_flags, false);
ef5584
								$p_result['topics'] += $return['topics'];
ef5584
								$p_result['posts'] += $return['posts'];
ef5584
							}
ef5584
						}
ef5584
ef5584
						$prune_ids[] = $row['forum_id'];
ef5584
ef5584
						$template->assign_block_vars('pruned', array(
ef5584
							'FORUM_NAME'	=> $row['forum_name'],
ef5584
							'NUM_TOPICS'	=> $p_result['topics'],
ef5584
							'NUM_POSTS'		=> $p_result['posts'])
ef5584
						);
ef5584
		
ef5584
						$log_data .= (($log_data != '') ? ', ' : '') . $row['forum_name'];
ef5584
					}
ef5584
					while ($row = $db->sql_fetchrow($result));
ef5584
		
ef5584
					// Sync all pruned forums at once
ef5584
					sync('forum', 'forum_id', $prune_ids, true, true);
ef5584
					add_log('admin', 'LOG_PRUNE', $log_data);
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
				return;
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				confirm_box(false, $user->lang['PRUNE_FORUM_CONFIRM'], build_hidden_fields(array(
ef5584
					'i'				=> $id,
ef5584
					'mode'			=> $mode,
ef5584
					'submit'		=> 1,
ef5584
					'all_forums'	=> $all_forums,
ef5584
					'f'				=> $forum_id,
ef5584
ef5584
					'prune_days'		=> request_var('prune_days', 0),
ef5584
					'prune_vieweddays'	=> request_var('prune_vieweddays', 0),
ef5584
					'prune_old_polls'	=> request_var('prune_old_polls', 0),
ef5584
					'prune_announce'	=> request_var('prune_announce', 0),
ef5584
					'prune_sticky'		=> request_var('prune_sticky', 0),
ef5584
				)));
ef5584
			}
ef5584
		}
ef5584
ef5584
		// If they haven't selected a forum for pruning yet then
ef5584
		// display a select box to use for pruning.
ef5584
		if (!sizeof($forum_id))
ef5584
		{
ef5584
			$template->assign_vars(array(
ef5584
				'U_ACTION'			=> $this->u_action,
ef5584
				'S_SELECT_FORUM'	=> true,
ef5584
				'S_FORUM_OPTIONS'	=> make_forum_select(false, false, false))
ef5584
			);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$sql = 'SELECT forum_id, forum_name
ef5584
				FROM ' . FORUMS_TABLE . '
ef5584
				WHERE ' . $db->sql_in_set('forum_id', $forum_id);
ef5584
			$result = $db->sql_query($sql);
ef5584
			$row = $db->sql_fetchrow($result);
ef5584
ef5584
			if (!$row)
ef5584
			{
ef5584
				$db->sql_freeresult($result);
ef5584
				trigger_error($user->lang['NO_FORUM'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
			}
ef5584
ef5584
			$forum_list = $s_hidden_fields = '';
ef5584
			do
ef5584
			{
ef5584
				$forum_list .= (($forum_list != '') ? ', ' : '') . '' . $row['forum_name'] . '';
ef5584
				$s_hidden_fields .= '<input type="hidden" name="f[]" value="' . $row['forum_id'] . '" />';
ef5584
			}
ef5584
			while ($row = $db->sql_fetchrow($result));
ef5584
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			$l_selected_forums = (sizeof($forum_id) == 1) ? 'SELECTED_FORUM' : 'SELECTED_FORUMS';
ef5584
ef5584
			$template->assign_vars(array(
ef5584
				'L_SELECTED_FORUMS'		=> $user->lang[$l_selected_forums],
ef5584
				'U_ACTION'				=> $this->u_action,
ef5584
				'U_BACK'				=> $this->u_action,
ef5584
				'FORUM_LIST'			=> $forum_list,
ef5584
				'S_HIDDEN_FIELDS'		=> $s_hidden_fields)
ef5584
			);
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Prune users
ef5584
	*/
ef5584
	function prune_users($id, $mode)
ef5584
	{
ef5584
		global $db, $user, $auth, $template, $cache;
ef5584
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
ef5584
ef5584
		$user->add_lang('memberlist');
ef5584
ef5584
		$prune = (isset($_POST['prune'])) ? true : false;
ef5584
ef5584
		if ($prune)
ef5584
		{
ef5584
			$action = request_var('action', 'deactivate');
ef5584
			$deleteposts = request_var('deleteposts', 0);
ef5584
ef5584
			if (confirm_box(true))
ef5584
			{
ef5584
				$user_ids = $usernames = array();
ef5584
				$this->get_prune_users($user_ids, $usernames);
ef5584
ef5584
				if (sizeof($user_ids))
ef5584
				{
ef5584
					if ($action == 'deactivate')
ef5584
					{
ef5584
						user_active_flip('deactivate', $user_ids);
ef5584
						$l_log = 'LOG_PRUNE_USER_DEAC';
ef5584
					}
ef5584
					else if ($action == 'delete')
ef5584
					{
ef5584
						if ($deleteposts)
ef5584
						{
ef5584
							foreach ($user_ids as $user_id)
ef5584
							{
ef5584
								user_delete('remove', $user_id);
ef5584
							}
ef5584
							
ef5584
							$l_log = 'LOG_PRUNE_USER_DEL_DEL';
ef5584
						}
ef5584
						else
ef5584
						{
ef5584
							foreach ($user_ids as $user_id)
ef5584
							{
ef5584
								user_delete('retain', $user_id, $usernames[$user_id]);
ef5584
							}
ef5584
ef5584
							$l_log = 'LOG_PRUNE_USER_DEL_ANON';
ef5584
						}
ef5584
					}
ef5584
ef5584
					add_log('admin', $l_log, implode(', ', $usernames));
ef5584
					$msg = $user->lang['USER_' . strtoupper($action) . '_SUCCESS'];
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					$msg = $user->lang['USER_PRUNE_FAILURE'];
ef5584
				}
ef5584
ef5584
				trigger_error($msg . adm_back_link($this->u_action));
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				// We list the users which will be pruned...
ef5584
				$user_ids = $usernames = array();
ef5584
				$this->get_prune_users($user_ids, $usernames);
ef5584
ef5584
				if (!sizeof($user_ids))
ef5584
				{
ef5584
					trigger_error($user->lang['USER_PRUNE_FAILURE'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
				}
ef5584
ef5584
				// Assign to template
ef5584
				foreach ($user_ids as $user_id)
ef5584
				{
ef5584
					$template->assign_block_vars('users', array(
ef5584
						'USERNAME'			=> $usernames[$user_id],
ef5584
						'U_PROFILE'			=> append_sid($phpbb_root_path . 'memberlist.' . $phpEx, 'mode=viewprofile&u=' . $user_id),
ef5584
						'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) : '',
ef5584
					));
ef5584
				}
ef5584
ef5584
				$template->assign_vars(array(
ef5584
					'S_DEACTIVATE'		=> ($action == 'deactivate') ? true : false,
ef5584
					'S_DELETE'			=> ($action == 'delete') ? true : false,
ef5584
				));
ef5584
ef5584
				confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
ef5584
					'i'				=> $id,
ef5584
					'mode'			=> $mode,
ef5584
					'prune'			=> 1,
ef5584
ef5584
					'users'			=> request_var('users', '', true),
ef5584
					'username'		=> request_var('username', '', true),
ef5584
					'email'			=> request_var('email', ''),
ef5584
					'joined_select'	=> request_var('joined_select', ''),
ef5584
					'joined'		=> request_var('joined', ''),
ef5584
					'active_select'	=> request_var('active_select', ''),
ef5584
					'active'		=> request_var('active', ''),
ef5584
					'count_select'	=> request_var('count_select', ''),
ef5584
					'count'			=> request_var('count', ''),
ef5584
					'deleteposts'	=> request_var('deleteposts', 0),
ef5584
ef5584
					'action'		=> request_var('action', ''),
ef5584
				)), 'confirm_body_prune.html');
ef5584
			}
ef5584
		}
ef5584
ef5584
		$find_count = array('lt' => $user->lang['LESS_THAN'], 'eq' => $user->lang['EQUAL_TO'], 'gt' => $user->lang['MORE_THAN']);
ef5584
		$s_find_count = '';
ef5584
ef5584
		foreach ($find_count as $key => $value)
ef5584
		{
ef5584
			$selected = ($key == 'eq') ? ' selected="selected"' : '';
ef5584
			$s_find_count .= '<option value="' . $key . '"' . $selected . '>' . $value . '</option>';
ef5584
		}
ef5584
ef5584
		$find_time = array('lt' => $user->lang['BEFORE'], 'gt' => $user->lang['AFTER']);
ef5584
		$s_find_join_time = '';
ef5584
		foreach ($find_time as $key => $value)
ef5584
		{
ef5584
			$s_find_join_time .= '<option value="' . $key . '">' . $value . '</option>';
ef5584
		}
ef5584
		
ef5584
		$s_find_active_time = '';
ef5584
		foreach ($find_time as $key => $value)
ef5584
		{
ef5584
			$s_find_active_time .= '<option value="' . $key . '">' . $value . '</option>';
ef5584
		}
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'U_ACTION'			=> $this->u_action,
ef5584
			'S_JOINED_OPTIONS'	=> $s_find_join_time,
ef5584
			'S_ACTIVE_OPTIONS'	=> $s_find_active_time,
ef5584
			'S_COUNT_OPTIONS'	=> $s_find_count,
ef5584
			'U_FIND_USERNAME'	=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&form=acp_prune&field=users'),
ef5584
		));
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Get user_ids/usernames from those being pruned
ef5584
	*/
ef5584
	function get_prune_users(&$user_ids, &$usernames)
ef5584
	{
ef5584
		global $user, $db;
ef5584
ef5584
		$users = request_var('users', '', true);
ef5584
		
ef5584
		if ($users)
ef5584
		{
ef5584
			$users = explode("\n", $users);
ef5584
			$where_sql = ' AND ' . $db->sql_in_set('username_clean', array_map('utf8_clean_string', $users));
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$username = request_var('username', '', true);
ef5584
			$email = request_var('email', '');
ef5584
ef5584
			$joined_select = request_var('joined_select', 'lt');
ef5584
			$active_select = request_var('active_select', 'lt');
ef5584
			$count_select = request_var('count_select', 'eq');
ef5584
			$joined = request_var('joined', '');
ef5584
			$active = request_var('active', '');
ef5584
ef5584
			$active = ($active) ? explode('-', $active) : array();
ef5584
			$joined = ($joined) ? explode('-', $joined) : array();
ef5584
ef5584
			if ((sizeof($active) && sizeof($active) != 3) || (sizeof($joined) && sizeof($joined) != 3))
ef5584
			{
ef5584
				trigger_error($user->lang['WRONG_ACTIVE_JOINED_DATE'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
			}
ef5584
ef5584
			$count = request_var('count', '');
ef5584
ef5584
			$key_match = array('lt' => '<', 'gt' => '>', 'eq' => '=');
ef5584
			$sort_by_types = array('username', 'user_email', 'user_posts', 'user_regdate', 'user_lastvisit');
ef5584
ef5584
			$where_sql = '';
ef5584
			$where_sql .= ($username) ? ' AND username_clean ' . $db->sql_like_expression(str_replace('*', $db->any_char, utf8_clean_string($username))) : '';
ef5584
			$where_sql .= ($email) ? ' AND user_email ' . $db->sql_like_expression(str_replace('*', $db->any_char, $email)) . ' ' : '';
ef5584
			$where_sql .= (sizeof($joined)) ? " AND user_regdate " . $key_match[$joined_select] . ' ' . gmmktime(0, 0, 0, (int) $joined[1], (int) $joined[2], (int) $joined[0]) : '';
ef5584
			$where_sql .= ($count !== '') ? " AND user_posts " . $key_match[$count_select] . ' ' . (int) $count . ' ' : '';
ef5584
ef5584
			if (sizeof($active) && $active_select != 'lt')
ef5584
			{
ef5584
				$where_sql .= ' AND user_lastvisit ' . $key_match[$active_select] . ' ' . gmmktime(0, 0, 0, (int) $active[1], (int) $active[2], (int) $active[0]);
ef5584
			}
ef5584
			else if (sizeof($active))
ef5584
			{
ef5584
				$where_sql .= ' AND (user_lastvisit > 0 AND user_lastvisit < ' . gmmktime(0, 0, 0, (int) $active[1], (int) $active[2], (int) $active[0]) . ')';
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Protect the admin, do not prune if no options are given...
ef5584
		if (!$where_sql)
ef5584
		{
ef5584
			return;
ef5584
		}
ef5584
ef5584
		// Get bot ids
ef5584
		$sql = 'SELECT user_id
ef5584
			FROM ' . BOTS_TABLE;
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		$bot_ids = array();
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$bot_ids[] = $row['user_id'];
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		// Do not prune founder members
ef5584
		$sql = 'SELECT user_id, username
ef5584
			FROM ' . USERS_TABLE . '
ef5584
			WHERE user_id <> ' . ANONYMOUS . '
ef5584
				AND user_type <> ' . USER_FOUNDER . "
ef5584
			$where_sql";
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		$where_sql = '';
ef5584
		$user_ids = $usernames = array();
ef5584
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			// Do not prune bots and the user currently pruning.
ef5584
			if ($row['user_id'] != $user->data['user_id'] && !in_array($row['user_id'], $bot_ids))
ef5584
			{
ef5584
				$user_ids[] = $row['user_id'];
ef5584
				$usernames[$row['user_id']] = $row['username'];
ef5584
			}
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
	}
ef5584
}
ef5584
ef5584
?>