includes/acp/acp_prune.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Wed, 07 Mar 2012 23:20:21 +0400
changeset 8 c9487b1e7ffc
parent 0 ceef6d64c21d
permissions -rw-r--r--
Removing install folder
     1 <?php
     2 /**
     3 *
     4 * @package acp
     5 * @version $Id$
     6 * @copyright (c) 2005 phpBB Group
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
     8 *
     9 */
    10 
    11 /**
    12 * @ignore
    13 */
    14 if (!defined('IN_PHPBB'))
    15 {
    16 	exit;
    17 }
    18 
    19 /**
    20 * @package acp
    21 */
    22 class acp_prune
    23 {
    24 	var $u_action;
    25 
    26 	function main($id, $mode)
    27 	{
    28 		global $user, $phpEx, $phpbb_admin_path, $phpbb_root_path;
    29 
    30 		$user->add_lang('acp/prune');
    31 		include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
    32 
    33 		switch ($mode)
    34 		{
    35 			case 'forums':
    36 				$this->tpl_name = 'acp_prune_forums';
    37 				$this->page_title = 'ACP_PRUNE_FORUMS';
    38 				$this->prune_forums($id, $mode);
    39 			break;
    40 
    41 			case 'users':
    42 				$this->tpl_name = 'acp_prune_users';
    43 				$this->page_title = 'ACP_PRUNE_USERS';
    44 				$this->prune_users($id, $mode);
    45 			break;
    46 		}
    47 	}
    48 
    49 	/**
    50 	* Prune forums
    51 	*/
    52 	function prune_forums($id, $mode)
    53 	{
    54 		global $db, $user, $auth, $template, $cache;
    55 		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
    56 
    57 		$all_forums = request_var('all_forums', 0);
    58 		$forum_id = request_var('f', array(0));
    59 		$submit = (isset($_POST['submit'])) ? true : false;
    60 
    61 		if ($all_forums)
    62 		{
    63 			$sql = 'SELECT forum_id
    64 				FROM ' . FORUMS_TABLE . '
    65 				ORDER BY left_id';
    66 			$result = $db->sql_query($sql);
    67 
    68 			$forum_id = array();
    69 			while ($row = $db->sql_fetchrow($result))
    70 			{
    71 				$forum_id[] = $row['forum_id'];
    72 			}
    73 			$db->sql_freeresult($result);
    74 		}
    75 
    76 		if ($submit)
    77 		{
    78 			if (confirm_box(true))
    79 			{
    80 				$prune_posted = request_var('prune_days', 0);
    81 				$prune_viewed = request_var('prune_vieweddays', 0);
    82 				$prune_all = (!$prune_posted && !$prune_viewed) ? true : false;
    83 		
    84 				$prune_flags = 0;
    85 				$prune_flags += (request_var('prune_old_polls', 0)) ? 2 : 0;
    86 				$prune_flags += (request_var('prune_announce', 0)) ? 4 : 0;
    87 				$prune_flags += (request_var('prune_sticky', 0)) ? 8 : 0;
    88 
    89 				// Convert days to seconds for timestamp functions...
    90 				$prunedate_posted = time() - ($prune_posted * 86400);
    91 				$prunedate_viewed = time() - ($prune_viewed * 86400);
    92 
    93 				$template->assign_vars(array(
    94 					'S_PRUNED'		=> true)
    95 				);
    96 
    97 				$sql_forum = (sizeof($forum_id)) ? ' AND ' . $db->sql_in_set('forum_id', $forum_id) : '';
    98 
    99 				// Get a list of forum's or the data for the forum that we are pruning.
   100 				$sql = 'SELECT forum_id, forum_name
   101 					FROM ' . FORUMS_TABLE . '
   102 					WHERE forum_type = ' . FORUM_POST . "
   103 						$sql_forum
   104 					ORDER BY left_id ASC";
   105 				$result = $db->sql_query($sql);
   106 
   107 				if ($row = $db->sql_fetchrow($result))
   108 				{
   109 					$prune_ids = array();
   110 					$p_result['topics'] = 0;
   111 					$p_result['posts'] = 0;
   112 					$log_data = '';
   113 			
   114 					do
   115 					{
   116 						if (!$auth->acl_get('f_list', $row['forum_id']))
   117 						{
   118 							continue;
   119 						}
   120 
   121 						if ($prune_all)
   122 						{
   123 							$p_result = prune($row['forum_id'], 'posted', time(), $prune_flags, false);
   124 						}
   125 						else
   126 						{
   127 							if ($prune_posted)
   128 							{
   129 								$return = prune($row['forum_id'], 'posted', $prunedate_posted, $prune_flags, false);
   130 								$p_result['topics'] += $return['topics'];
   131 								$p_result['posts'] += $return['posts'];
   132 							}
   133 			
   134 							if ($prune_viewed)
   135 							{
   136 								$return = prune($row['forum_id'], 'viewed', $prunedate_viewed, $prune_flags, false);
   137 								$p_result['topics'] += $return['topics'];
   138 								$p_result['posts'] += $return['posts'];
   139 							}
   140 						}
   141 
   142 						$prune_ids[] = $row['forum_id'];
   143 
   144 						$template->assign_block_vars('pruned', array(
   145 							'FORUM_NAME'	=> $row['forum_name'],
   146 							'NUM_TOPICS'	=> $p_result['topics'],
   147 							'NUM_POSTS'		=> $p_result['posts'])
   148 						);
   149 		
   150 						$log_data .= (($log_data != '') ? ', ' : '') . $row['forum_name'];
   151 					}
   152 					while ($row = $db->sql_fetchrow($result));
   153 		
   154 					// Sync all pruned forums at once
   155 					sync('forum', 'forum_id', $prune_ids, true, true);
   156 					add_log('admin', 'LOG_PRUNE', $log_data);
   157 				}
   158 				$db->sql_freeresult($result);
   159 
   160 				return;
   161 			}
   162 			else
   163 			{
   164 				confirm_box(false, $user->lang['PRUNE_FORUM_CONFIRM'], build_hidden_fields(array(
   165 					'i'				=> $id,
   166 					'mode'			=> $mode,
   167 					'submit'		=> 1,
   168 					'all_forums'	=> $all_forums,
   169 					'f'				=> $forum_id,
   170 
   171 					'prune_days'		=> request_var('prune_days', 0),
   172 					'prune_vieweddays'	=> request_var('prune_vieweddays', 0),
   173 					'prune_old_polls'	=> request_var('prune_old_polls', 0),
   174 					'prune_announce'	=> request_var('prune_announce', 0),
   175 					'prune_sticky'		=> request_var('prune_sticky', 0),
   176 				)));
   177 			}
   178 		}
   179 
   180 		// If they haven't selected a forum for pruning yet then
   181 		// display a select box to use for pruning.
   182 		if (!sizeof($forum_id))
   183 		{
   184 			$template->assign_vars(array(
   185 				'U_ACTION'			=> $this->u_action,
   186 				'S_SELECT_FORUM'	=> true,
   187 				'S_FORUM_OPTIONS'	=> make_forum_select(false, false, false))
   188 			);
   189 		}
   190 		else
   191 		{
   192 			$sql = 'SELECT forum_id, forum_name
   193 				FROM ' . FORUMS_TABLE . '
   194 				WHERE ' . $db->sql_in_set('forum_id', $forum_id);
   195 			$result = $db->sql_query($sql);
   196 			$row = $db->sql_fetchrow($result);
   197 
   198 			if (!$row)
   199 			{
   200 				$db->sql_freeresult($result);
   201 				trigger_error($user->lang['NO_FORUM'] . adm_back_link($this->u_action), E_USER_WARNING);
   202 			}
   203 
   204 			$forum_list = $s_hidden_fields = '';
   205 			do
   206 			{
   207 				$forum_list .= (($forum_list != '') ? ', ' : '') . '<b>' . $row['forum_name'] . '</b>';
   208 				$s_hidden_fields .= '<input type="hidden" name="f[]" value="' . $row['forum_id'] . '" />';
   209 			}
   210 			while ($row = $db->sql_fetchrow($result));
   211 
   212 			$db->sql_freeresult($result);
   213 
   214 			$l_selected_forums = (sizeof($forum_id) == 1) ? 'SELECTED_FORUM' : 'SELECTED_FORUMS';
   215 
   216 			$template->assign_vars(array(
   217 				'L_SELECTED_FORUMS'		=> $user->lang[$l_selected_forums],
   218 				'U_ACTION'				=> $this->u_action,
   219 				'U_BACK'				=> $this->u_action,
   220 				'FORUM_LIST'			=> $forum_list,
   221 				'S_HIDDEN_FIELDS'		=> $s_hidden_fields)
   222 			);
   223 		}
   224 	}
   225 
   226 	/**
   227 	* Prune users
   228 	*/
   229 	function prune_users($id, $mode)
   230 	{
   231 		global $db, $user, $auth, $template, $cache;
   232 		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
   233 
   234 		$user->add_lang('memberlist');
   235 
   236 		$prune = (isset($_POST['prune'])) ? true : false;
   237 
   238 		if ($prune)
   239 		{
   240 			$action = request_var('action', 'deactivate');
   241 			$deleteposts = request_var('deleteposts', 0);
   242 
   243 			if (confirm_box(true))
   244 			{
   245 				$user_ids = $usernames = array();
   246 				$this->get_prune_users($user_ids, $usernames);
   247 
   248 				if (sizeof($user_ids))
   249 				{
   250 					if ($action == 'deactivate')
   251 					{
   252 						user_active_flip('deactivate', $user_ids);
   253 						$l_log = 'LOG_PRUNE_USER_DEAC';
   254 					}
   255 					else if ($action == 'delete')
   256 					{
   257 						if ($deleteposts)
   258 						{
   259 							foreach ($user_ids as $user_id)
   260 							{
   261 								user_delete('remove', $user_id);
   262 							}
   263 							
   264 							$l_log = 'LOG_PRUNE_USER_DEL_DEL';
   265 						}
   266 						else
   267 						{
   268 							foreach ($user_ids as $user_id)
   269 							{
   270 								user_delete('retain', $user_id, $usernames[$user_id]);
   271 							}
   272 
   273 							$l_log = 'LOG_PRUNE_USER_DEL_ANON';
   274 						}
   275 					}
   276 
   277 					add_log('admin', $l_log, implode(', ', $usernames));
   278 					$msg = $user->lang['USER_' . strtoupper($action) . '_SUCCESS'];
   279 				}
   280 				else
   281 				{
   282 					$msg = $user->lang['USER_PRUNE_FAILURE'];
   283 				}
   284 
   285 				trigger_error($msg . adm_back_link($this->u_action));
   286 			}
   287 			else
   288 			{
   289 				// We list the users which will be pruned...
   290 				$user_ids = $usernames = array();
   291 				$this->get_prune_users($user_ids, $usernames);
   292 
   293 				if (!sizeof($user_ids))
   294 				{
   295 					trigger_error($user->lang['USER_PRUNE_FAILURE'] . adm_back_link($this->u_action), E_USER_WARNING);
   296 				}
   297 
   298 				// Assign to template
   299 				foreach ($user_ids as $user_id)
   300 				{
   301 					$template->assign_block_vars('users', array(
   302 						'USERNAME'			=> $usernames[$user_id],
   303 						'U_PROFILE'			=> append_sid($phpbb_root_path . 'memberlist.' . $phpEx, 'mode=viewprofile&amp;u=' . $user_id),
   304 						'U_USER_ADMIN'		=> ($auth->acl_get('a_user')) ? append_sid("{$phpbb_admin_path}index.$phpEx", 'i=users&amp;mode=overview&amp;u=' . $user_id, true, $user->session_id) : '',
   305 					));
   306 				}
   307 
   308 				$template->assign_vars(array(
   309 					'S_DEACTIVATE'		=> ($action == 'deactivate') ? true : false,
   310 					'S_DELETE'			=> ($action == 'delete') ? true : false,
   311 				));
   312 
   313 				confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
   314 					'i'				=> $id,
   315 					'mode'			=> $mode,
   316 					'prune'			=> 1,
   317 
   318 					'users'			=> utf8_normalize_nfc(request_var('users', '', true)),
   319 					'username'		=> utf8_normalize_nfc(request_var('username', '', true)),
   320 					'email'			=> request_var('email', ''),
   321 					'joined_select'	=> request_var('joined_select', ''),
   322 					'joined'		=> request_var('joined', ''),
   323 					'active_select'	=> request_var('active_select', ''),
   324 					'active'		=> request_var('active', ''),
   325 					'count_select'	=> request_var('count_select', ''),
   326 					'count'			=> request_var('count', ''),
   327 					'deleteposts'	=> request_var('deleteposts', 0),
   328 
   329 					'action'		=> request_var('action', ''),
   330 				)), 'confirm_body_prune.html');
   331 			}
   332 		}
   333 
   334 		$find_count = array('lt' => $user->lang['LESS_THAN'], 'eq' => $user->lang['EQUAL_TO'], 'gt' => $user->lang['MORE_THAN']);
   335 		$s_find_count = '';
   336 
   337 		foreach ($find_count as $key => $value)
   338 		{
   339 			$selected = ($key == 'eq') ? ' selected="selected"' : '';
   340 			$s_find_count .= '<option value="' . $key . '"' . $selected . '>' . $value . '</option>';
   341 		}
   342 
   343 		$find_time = array('lt' => $user->lang['BEFORE'], 'gt' => $user->lang['AFTER']);
   344 		$s_find_join_time = '';
   345 		foreach ($find_time as $key => $value)
   346 		{
   347 			$s_find_join_time .= '<option value="' . $key . '">' . $value . '</option>';
   348 		}
   349 		
   350 		$s_find_active_time = '';
   351 		foreach ($find_time as $key => $value)
   352 		{
   353 			$s_find_active_time .= '<option value="' . $key . '">' . $value . '</option>';
   354 		}
   355 
   356 		$template->assign_vars(array(
   357 			'U_ACTION'			=> $this->u_action,
   358 			'S_JOINED_OPTIONS'	=> $s_find_join_time,
   359 			'S_ACTIVE_OPTIONS'	=> $s_find_active_time,
   360 			'S_COUNT_OPTIONS'	=> $s_find_count,
   361 			'U_FIND_USERNAME'	=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=acp_prune&amp;field=users'),
   362 		));
   363 	}
   364 
   365 	/**
   366 	* Get user_ids/usernames from those being pruned
   367 	*/
   368 	function get_prune_users(&$user_ids, &$usernames)
   369 	{
   370 		global $user, $db;
   371 
   372 		$users = utf8_normalize_nfc(request_var('users', '', true));
   373 		
   374 		if ($users)
   375 		{
   376 			$users = explode("\n", $users);
   377 			$where_sql = ' AND ' . $db->sql_in_set('username_clean', array_map('utf8_clean_string', $users));
   378 		}
   379 		else
   380 		{
   381 			$username = utf8_normalize_nfc(request_var('username', '', true));
   382 			$email = request_var('email', '');
   383 
   384 			$joined_select = request_var('joined_select', 'lt');
   385 			$active_select = request_var('active_select', 'lt');
   386 			$count_select = request_var('count_select', 'eq');
   387 			$joined = request_var('joined', '');
   388 			$active = request_var('active', '');
   389 
   390 			$active = ($active) ? explode('-', $active) : array();
   391 			$joined = ($joined) ? explode('-', $joined) : array();
   392 
   393 			if ((sizeof($active) && sizeof($active) != 3) || (sizeof($joined) && sizeof($joined) != 3))
   394 			{
   395 				trigger_error($user->lang['WRONG_ACTIVE_JOINED_DATE'] . adm_back_link($this->u_action), E_USER_WARNING);
   396 			}
   397 
   398 			$count = request_var('count', '');
   399 
   400 			$key_match = array('lt' => '<', 'gt' => '>', 'eq' => '=');
   401 			$sort_by_types = array('username', 'user_email', 'user_posts', 'user_regdate', 'user_lastvisit');
   402 
   403 			$where_sql = '';
   404 			$where_sql .= ($username) ? ' AND username_clean ' . $db->sql_like_expression(str_replace('*', $db->any_char, utf8_clean_string($username))) : '';
   405 			$where_sql .= ($email) ? ' AND user_email ' . $db->sql_like_expression(str_replace('*', $db->any_char, $email)) . ' ' : '';
   406 			$where_sql .= (sizeof($joined)) ? " AND user_regdate " . $key_match[$joined_select] . ' ' . gmmktime(0, 0, 0, (int) $joined[1], (int) $joined[2], (int) $joined[0]) : '';
   407 			$where_sql .= ($count !== '') ? " AND user_posts " . $key_match[$count_select] . ' ' . (int) $count . ' ' : '';
   408 
   409 			// First handle pruning of users who never logged in, last active date is 0000-00-00
   410 			if (sizeof($active) && (int) $active[0] == 0 && (int) $active[1] == 0 && (int) $active[2] == 0)
   411 			{
   412 				$where_sql .= ' AND user_lastvisit = 0';
   413 			}			
   414 			else if (sizeof($active) && $active_select != 'lt')
   415 			{
   416 				$where_sql .= ' AND user_lastvisit ' . $key_match[$active_select] . ' ' . gmmktime(0, 0, 0, (int) $active[1], (int) $active[2], (int) $active[0]);
   417 			}
   418 			else if (sizeof($active))
   419 			{
   420 				$where_sql .= ' AND (user_lastvisit > 0 AND user_lastvisit < ' . gmmktime(0, 0, 0, (int) $active[1], (int) $active[2], (int) $active[0]) . ')';
   421 			}
   422 		}
   423 
   424 		// Protect the admin, do not prune if no options are given...
   425 		if (!$where_sql)
   426 		{
   427 			return;
   428 		}
   429 
   430 		// Get bot ids
   431 		$sql = 'SELECT user_id
   432 			FROM ' . BOTS_TABLE;
   433 		$result = $db->sql_query($sql);
   434 
   435 		$bot_ids = array();
   436 		while ($row = $db->sql_fetchrow($result))
   437 		{
   438 			$bot_ids[] = $row['user_id'];
   439 		}
   440 		$db->sql_freeresult($result);
   441 
   442 		// Do not prune founder members
   443 		$sql = 'SELECT user_id, username
   444 			FROM ' . USERS_TABLE . '
   445 			WHERE user_id <> ' . ANONYMOUS . '
   446 				AND user_type <> ' . USER_FOUNDER . "
   447 			$where_sql";
   448 		$result = $db->sql_query($sql);
   449 
   450 		$where_sql = '';
   451 		$user_ids = $usernames = array();
   452 
   453 		while ($row = $db->sql_fetchrow($result))
   454 		{
   455 			// Do not prune bots and the user currently pruning.
   456 			if ($row['user_id'] != $user->data['user_id'] && !in_array($row['user_id'], $bot_ids))
   457 			{
   458 				$user_ids[] = $row['user_id'];
   459 				$usernames[$row['user_id']] = $row['username'];
   460 			}
   461 		}
   462 		$db->sql_freeresult($result);
   463 	}
   464 }
   465 
   466 ?>