mcp.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mcp.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,915 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package mcp
     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 +define('IN_PHPBB', true);
    1.18 +$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
    1.19 +$phpEx = substr(strrchr(__FILE__, '.'), 1);
    1.20 +include($phpbb_root_path . 'common.' . $phpEx);
    1.21 +include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
    1.22 +require($phpbb_root_path . 'includes/functions_module.' . $phpEx);
    1.23 +
    1.24 +// Start session management
    1.25 +$user->session_begin();
    1.26 +$auth->acl($user->data);
    1.27 +$user->setup('mcp');
    1.28 +
    1.29 +$module = new p_master();
    1.30 +
    1.31 +// Setting a variable to let the style designer know where he is...
    1.32 +$template->assign_var('S_IN_MCP', true);
    1.33 +
    1.34 +// Basic parameter data
    1.35 +$id = request_var('i', '');
    1.36 +
    1.37 +if (isset($_REQUEST['mode']) && is_array($_REQUEST['mode']))
    1.38 +{
    1.39 +	$mode = request_var('mode', array(''));
    1.40 +	list($mode, ) = each($mode);
    1.41 +}
    1.42 +else
    1.43 +{
    1.44 +	$mode = request_var('mode', '');
    1.45 +}
    1.46 +
    1.47 +// Only Moderators can go beyond this point
    1.48 +if (!$user->data['is_registered'])
    1.49 +{
    1.50 +	if ($user->data['is_bot'])
    1.51 +	{
    1.52 +		redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
    1.53 +	}
    1.54 +
    1.55 +	login_box('', $user->lang['LOGIN_EXPLAIN_MCP']);
    1.56 +}
    1.57 +
    1.58 +$quickmod = (isset($_REQUEST['quickmod'])) ? true : false;
    1.59 +$action = request_var('action', '');
    1.60 +$action_ary = request_var('action', array('' => 0));
    1.61 +
    1.62 +$forum_action = request_var('forum_action', '');
    1.63 +if ($forum_action !== '' && !empty($_POST['sort']))
    1.64 +{
    1.65 +	$action = $forum_action;
    1.66 +}
    1.67 +
    1.68 +if (sizeof($action_ary))
    1.69 +{
    1.70 +	list($action, ) = each($action_ary);
    1.71 +}
    1.72 +unset($action_ary);
    1.73 +
    1.74 +if ($mode == 'topic_logs')
    1.75 +{
    1.76 +	$id = 'logs';
    1.77 +	$quickmod = false;
    1.78 +}
    1.79 +
    1.80 +$post_id = request_var('p', 0);
    1.81 +$topic_id = request_var('t', 0);
    1.82 +$forum_id = request_var('f', 0);
    1.83 +$report_id = request_var('r', 0);
    1.84 +$user_id = request_var('u', 0);
    1.85 +$username = utf8_normalize_nfc(request_var('username', '', true));
    1.86 +
    1.87 +if ($post_id)
    1.88 +{
    1.89 +	// We determine the topic and forum id here, to make sure the moderator really has moderative rights on this post
    1.90 +	$sql = 'SELECT topic_id, forum_id
    1.91 +		FROM ' . POSTS_TABLE . "
    1.92 +		WHERE post_id = $post_id";
    1.93 +	$result = $db->sql_query($sql);
    1.94 +	$row = $db->sql_fetchrow($result);
    1.95 +	$db->sql_freeresult($result);
    1.96 +
    1.97 +	$topic_id = (int) $row['topic_id'];
    1.98 +	$forum_id = (int) ($row['forum_id']) ? $row['forum_id'] : $forum_id;
    1.99 +}
   1.100 +else if ($topic_id)
   1.101 +{
   1.102 +	$sql = 'SELECT forum_id
   1.103 +		FROM ' . TOPICS_TABLE . "
   1.104 +		WHERE topic_id = $topic_id";
   1.105 +	$result = $db->sql_query($sql);
   1.106 +	$row = $db->sql_fetchrow($result);
   1.107 +	$db->sql_freeresult($result);
   1.108 +
   1.109 +	$forum_id = (int) $row['forum_id'];
   1.110 +}
   1.111 +
   1.112 +// If the user doesn't have any moderator powers (globally or locally) he can't access the mcp
   1.113 +if (!$auth->acl_getf_global('m_'))
   1.114 +{
   1.115 +	// Except he is using one of the quickmod tools for users
   1.116 +	$user_quickmod_actions = array(
   1.117 +		'lock'			=> 'f_user_lock',
   1.118 +		'make_sticky'	=> 'f_sticky',
   1.119 +		'make_announce'	=> 'f_announce',
   1.120 +		'make_global'	=> 'f_announce',
   1.121 +		'make_normal'	=> array('f_announce', 'f_sticky')
   1.122 +	);
   1.123 +
   1.124 +	$allow_user = false;
   1.125 +	if ($quickmod && isset($user_quickmod_actions[$action]) && $user->data['is_registered'] && $auth->acl_gets($user_quickmod_actions[$action], $forum_id))
   1.126 +	{
   1.127 +		$topic_info = get_topic_data(array($topic_id));
   1.128 +		if ($topic_info[$topic_id]['topic_poster'] == $user->data['user_id'])
   1.129 +		{
   1.130 +			$allow_user = true;
   1.131 +		}
   1.132 +	}
   1.133 +
   1.134 +	if (!$allow_user)
   1.135 +	{
   1.136 +		trigger_error('NOT_AUTHORISED');
   1.137 +	}
   1.138 +}
   1.139 +
   1.140 +// if the user cannot read the forum he tries to access then we won't allow mcp access either
   1.141 +if ($forum_id && !$auth->acl_get('f_read', $forum_id))
   1.142 +{
   1.143 +	trigger_error('NOT_AUTHORISED');
   1.144 +}
   1.145 +
   1.146 +if ($forum_id)
   1.147 +{
   1.148 +	$module->acl_forum_id = $forum_id;
   1.149 +}
   1.150 +
   1.151 +// Instantiate module system and generate list of available modules
   1.152 +$module->list_modules('mcp');
   1.153 +
   1.154 +if ($quickmod)
   1.155 +{
   1.156 +	$mode = 'quickmod';
   1.157 +
   1.158 +	switch ($action)
   1.159 +	{
   1.160 +		case 'lock':
   1.161 +		case 'unlock':
   1.162 +		case 'lock_post':
   1.163 +		case 'unlock_post':
   1.164 +		case 'make_sticky':
   1.165 +		case 'make_announce':
   1.166 +		case 'make_global':
   1.167 +		case 'make_normal':
   1.168 +		case 'fork':
   1.169 +		case 'move':
   1.170 +		case 'delete_post':
   1.171 +		case 'delete_topic':
   1.172 +			$module->load('mcp', 'main', 'quickmod');
   1.173 +			return;
   1.174 +		break;
   1.175 +
   1.176 +		case 'topic_logs':
   1.177 +			// Reset start parameter if we jumped from the quickmod dropdown
   1.178 +			if (request_var('start', 0))
   1.179 +			{
   1.180 +				$_REQUEST['start'] = 0;
   1.181 +			}
   1.182 +
   1.183 +			$module->set_active('logs', 'topic_logs');
   1.184 +		break;
   1.185 +
   1.186 +		case 'merge_topic':
   1.187 +			$module->set_active('main', 'forum_view');
   1.188 +		break;
   1.189 +
   1.190 +		case 'split':
   1.191 +		case 'merge':
   1.192 +			$module->set_active('main', 'topic_view');
   1.193 +		break;
   1.194 +
   1.195 +		default:
   1.196 +			trigger_error("$action not allowed as quickmod", E_USER_ERROR);
   1.197 +		break;
   1.198 +	}
   1.199 +}
   1.200 +else
   1.201 +{
   1.202 +	// Select the active module
   1.203 +	$module->set_active($id, $mode);
   1.204 +}
   1.205 +
   1.206 +// Hide some of the options if we don't have the relevant information to use them
   1.207 +if (!$post_id)
   1.208 +{
   1.209 +	$module->set_display('main', 'post_details', false);
   1.210 +	$module->set_display('warn', 'warn_post', false);
   1.211 +}
   1.212 +
   1.213 +if ($mode == '' || $mode == 'unapproved_topics' || $mode == 'unapproved_posts')
   1.214 +{
   1.215 +	$module->set_display('queue', 'approve_details', false);
   1.216 +}
   1.217 +
   1.218 +if ($mode == '' || $mode == 'reports' || $mode == 'reports_closed' || $mode == 'pm_reports' || $mode == 'pm_reports_closed' || $mode == 'pm_report_details')
   1.219 +{
   1.220 +	$module->set_display('reports', 'report_details', false);
   1.221 +}
   1.222 +
   1.223 +if ($mode == '' || $mode == 'reports' || $mode == 'reports_closed' || $mode == 'pm_reports' || $mode == 'pm_reports_closed' || $mode == 'report_details')
   1.224 +{
   1.225 +	$module->set_display('pm_reports', 'pm_report_details', false);
   1.226 +}
   1.227 +
   1.228 +if (!$topic_id)
   1.229 +{
   1.230 +	$module->set_display('main', 'topic_view', false);
   1.231 +	$module->set_display('logs', 'topic_logs', false);
   1.232 +}
   1.233 +
   1.234 +if (!$forum_id)
   1.235 +{
   1.236 +	$module->set_display('main', 'forum_view', false);
   1.237 +	$module->set_display('logs', 'forum_logs', false);
   1.238 +}
   1.239 +
   1.240 +if (!$user_id && $username == '')
   1.241 +{
   1.242 +	$module->set_display('notes', 'user_notes', false);
   1.243 +	$module->set_display('warn', 'warn_user', false);
   1.244 +}
   1.245 +
   1.246 +// Load and execute the relevant module
   1.247 +$module->load_active();
   1.248 +
   1.249 +// Assign data to the template engine for the list of modules
   1.250 +$module->assign_tpl_vars(append_sid("{$phpbb_root_path}mcp.$phpEx"));
   1.251 +
   1.252 +// Generate urls for letting the moderation control panel being accessed in different modes
   1.253 +$template->assign_vars(array(
   1.254 +	'U_MCP'			=> append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main'),
   1.255 +	'U_MCP_FORUM'	=> ($forum_id) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=forum_view&amp;f=$forum_id") : '',
   1.256 +	'U_MCP_TOPIC'	=> ($forum_id && $topic_id) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=topic_view&amp;t=$topic_id") : '',
   1.257 +	'U_MCP_POST'	=> ($forum_id && $topic_id && $post_id) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=post_details&amp;t=$topic_id&amp;p=$post_id") : '',
   1.258 +));
   1.259 +
   1.260 +// Generate the page, do not display/query online list
   1.261 +$module->display($module->get_page_title(), false);
   1.262 +
   1.263 +/**
   1.264 +* Functions used to generate additional URL paramters
   1.265 +*/
   1.266 +function _module__url($mode, &$module_row)
   1.267 +{
   1.268 +	return extra_url();
   1.269 +}
   1.270 +
   1.271 +function _module_notes_url($mode, &$module_row)
   1.272 +{
   1.273 +	if ($mode == 'front')
   1.274 +	{
   1.275 +		return '';
   1.276 +	}
   1.277 +
   1.278 +	global $user_id;
   1.279 +	return ($user_id) ? "&amp;u=$user_id" : '';
   1.280 +}
   1.281 +
   1.282 +function _module_warn_url($mode, &$module_row)
   1.283 +{
   1.284 +	if ($mode == 'front' || $mode == 'list')
   1.285 +	{
   1.286 +		global $forum_id;
   1.287 +
   1.288 +		return ($forum_id) ? "&amp;f=$forum_id" : '';
   1.289 +	}
   1.290 +
   1.291 +	if ($mode == 'warn_post')
   1.292 +	{
   1.293 +		global $forum_id, $post_id;
   1.294 +
   1.295 +		$url_extra = ($forum_id) ? "&amp;f=$forum_id" : '';
   1.296 +		$url_extra .= ($post_id) ? "&amp;p=$post_id" : '';
   1.297 +
   1.298 +		return $url_extra;
   1.299 +	}
   1.300 +	else
   1.301 +	{
   1.302 +		global $user_id;
   1.303 +
   1.304 +		return ($user_id) ? "&amp;u=$user_id" : '';
   1.305 +	}
   1.306 +}
   1.307 +
   1.308 +function _module_main_url($mode, &$module_row)
   1.309 +{
   1.310 +	return extra_url();
   1.311 +}
   1.312 +
   1.313 +function _module_logs_url($mode, &$module_row)
   1.314 +{
   1.315 +	return extra_url();
   1.316 +}
   1.317 +
   1.318 +function _module_ban_url($mode, &$module_row)
   1.319 +{
   1.320 +	return extra_url();
   1.321 +}
   1.322 +
   1.323 +function _module_queue_url($mode, &$module_row)
   1.324 +{
   1.325 +	return extra_url();
   1.326 +}
   1.327 +
   1.328 +function _module_reports_url($mode, &$module_row)
   1.329 +{
   1.330 +	return extra_url();
   1.331 +}
   1.332 +
   1.333 +function extra_url()
   1.334 +{
   1.335 +	global $forum_id, $topic_id, $post_id, $report_id, $user_id;
   1.336 +
   1.337 +	$url_extra = '';
   1.338 +	$url_extra .= ($forum_id) ? "&amp;f=$forum_id" : '';
   1.339 +	$url_extra .= ($topic_id) ? "&amp;t=$topic_id" : '';
   1.340 +	$url_extra .= ($post_id) ? "&amp;p=$post_id" : '';
   1.341 +	$url_extra .= ($user_id) ? "&amp;u=$user_id" : '';
   1.342 +	$url_extra .= ($report_id) ? "&amp;r=$report_id" : '';
   1.343 +
   1.344 +	return $url_extra;
   1.345 +}
   1.346 +
   1.347 +/**
   1.348 +* Get simple topic data
   1.349 +*/
   1.350 +function get_topic_data($topic_ids, $acl_list = false, $read_tracking = false)
   1.351 +{
   1.352 +	global $auth, $db, $config, $user;
   1.353 +	static $rowset = array();
   1.354 +
   1.355 +	$topics = array();
   1.356 +
   1.357 +	if (!sizeof($topic_ids))
   1.358 +	{
   1.359 +		return array();
   1.360 +	}
   1.361 +
   1.362 +	// cache might not contain read tracking info, so we can't use it if read
   1.363 +	// tracking information is requested
   1.364 +	if (!$read_tracking)
   1.365 +	{
   1.366 +		$cache_topic_ids = array_intersect($topic_ids, array_keys($rowset));
   1.367 +		$topic_ids = array_diff($topic_ids, array_keys($rowset));
   1.368 +	}
   1.369 +	else
   1.370 +	{
   1.371 +		$cache_topic_ids = array();
   1.372 +	}
   1.373 +
   1.374 +	if (sizeof($topic_ids))
   1.375 +	{
   1.376 +		$sql_array = array(
   1.377 +			'SELECT'	=> 't.*, f.*',
   1.378 +
   1.379 +			'FROM'		=> array(
   1.380 +				TOPICS_TABLE	=> 't',
   1.381 +			),
   1.382 +
   1.383 +			'LEFT_JOIN'	=> array(
   1.384 +				array(
   1.385 +					'FROM'	=> array(FORUMS_TABLE => 'f'),
   1.386 +					'ON'	=> 'f.forum_id = t.forum_id'
   1.387 +				)
   1.388 +			),
   1.389 +
   1.390 +			'WHERE'		=> $db->sql_in_set('t.topic_id', $topic_ids)
   1.391 +		);
   1.392 +
   1.393 +		if ($read_tracking && $config['load_db_lastread'])
   1.394 +		{
   1.395 +			$sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
   1.396 +
   1.397 +			$sql_array['LEFT_JOIN'][] = array(
   1.398 +				'FROM'	=> array(TOPICS_TRACK_TABLE => 'tt'),
   1.399 +				'ON'	=> 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
   1.400 +			);
   1.401 +
   1.402 +			$sql_array['LEFT_JOIN'][] = array(
   1.403 +				'FROM'	=> array(FORUMS_TRACK_TABLE => 'ft'),
   1.404 +				'ON'	=> 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
   1.405 +			);
   1.406 +		}
   1.407 +
   1.408 +		$sql = $db->sql_build_query('SELECT', $sql_array);
   1.409 +		$result = $db->sql_query($sql);
   1.410 +
   1.411 +		while ($row = $db->sql_fetchrow($result))
   1.412 +		{
   1.413 +			if (!$row['forum_id'])
   1.414 +			{
   1.415 +				// Global Announcement?
   1.416 +				$row['forum_id'] = request_var('f', 0);
   1.417 +			}
   1.418 +
   1.419 +			$rowset[$row['topic_id']] = $row;
   1.420 +
   1.421 +			if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
   1.422 +			{
   1.423 +				continue;
   1.424 +			}
   1.425 +
   1.426 +			$topics[$row['topic_id']] = $row;
   1.427 +		}
   1.428 +		$db->sql_freeresult($result);
   1.429 +	}
   1.430 +
   1.431 +	foreach ($cache_topic_ids as $id)
   1.432 +	{
   1.433 +		if (!$acl_list || $auth->acl_gets($acl_list, $rowset[$id]['forum_id']))
   1.434 +		{
   1.435 +			$topics[$id] = $rowset[$id];
   1.436 +		}
   1.437 +	}
   1.438 +
   1.439 +	return $topics;
   1.440 +}
   1.441 +
   1.442 +/**
   1.443 +* Get simple post data
   1.444 +*/
   1.445 +function get_post_data($post_ids, $acl_list = false, $read_tracking = false)
   1.446 +{
   1.447 +	global $db, $auth, $config, $user;
   1.448 +
   1.449 +	$rowset = array();
   1.450 +
   1.451 +	if (!sizeof($post_ids))
   1.452 +	{
   1.453 +		return array();
   1.454 +	}
   1.455 +
   1.456 +	$sql_array = array(
   1.457 +		'SELECT'	=> 'p.*, u.*, t.*, f.*',
   1.458 +
   1.459 +		'FROM'		=> array(
   1.460 +			USERS_TABLE		=> 'u',
   1.461 +			POSTS_TABLE		=> 'p',
   1.462 +			TOPICS_TABLE	=> 't',
   1.463 +		),
   1.464 +
   1.465 +		'LEFT_JOIN'	=> array(
   1.466 +			array(
   1.467 +				'FROM'	=> array(FORUMS_TABLE => 'f'),
   1.468 +				'ON'	=> 'f.forum_id = t.forum_id'
   1.469 +			)
   1.470 +		),
   1.471 +
   1.472 +		'WHERE'		=> $db->sql_in_set('p.post_id', $post_ids) . '
   1.473 +			AND u.user_id = p.poster_id
   1.474 +			AND t.topic_id = p.topic_id',
   1.475 +	);
   1.476 +
   1.477 +	if ($read_tracking && $config['load_db_lastread'])
   1.478 +	{
   1.479 +		$sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
   1.480 +
   1.481 +		$sql_array['LEFT_JOIN'][] = array(
   1.482 +			'FROM'	=> array(TOPICS_TRACK_TABLE => 'tt'),
   1.483 +			'ON'	=> 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
   1.484 +		);
   1.485 +
   1.486 +		$sql_array['LEFT_JOIN'][] = array(
   1.487 +			'FROM'	=> array(FORUMS_TRACK_TABLE => 'ft'),
   1.488 +			'ON'	=> 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
   1.489 +		);
   1.490 +	}
   1.491 +
   1.492 +	$sql = $db->sql_build_query('SELECT', $sql_array);
   1.493 +	$result = $db->sql_query($sql);
   1.494 +	unset($sql_array);
   1.495 +
   1.496 +	while ($row = $db->sql_fetchrow($result))
   1.497 +	{
   1.498 +		if (!$row['forum_id'])
   1.499 +		{
   1.500 +			// Global Announcement?
   1.501 +			$row['forum_id'] = request_var('f', 0);
   1.502 +		}
   1.503 +
   1.504 +		if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
   1.505 +		{
   1.506 +			continue;
   1.507 +		}
   1.508 +
   1.509 +		if (!$row['post_approved'] && !$auth->acl_get('m_approve', $row['forum_id']))
   1.510 +		{
   1.511 +			// Moderators without the permission to approve post should at least not see them. ;)
   1.512 +			continue;
   1.513 +		}
   1.514 +
   1.515 +		$rowset[$row['post_id']] = $row;
   1.516 +	}
   1.517 +	$db->sql_freeresult($result);
   1.518 +
   1.519 +	return $rowset;
   1.520 +}
   1.521 +
   1.522 +/**
   1.523 +* Get simple forum data
   1.524 +*/
   1.525 +function get_forum_data($forum_id, $acl_list = 'f_list', $read_tracking = false)
   1.526 +{
   1.527 +	global $auth, $db, $user, $config;
   1.528 +
   1.529 +	$rowset = array();
   1.530 +
   1.531 +	if (!is_array($forum_id))
   1.532 +	{
   1.533 +		$forum_id = array($forum_id);
   1.534 +	}
   1.535 +
   1.536 +	if (!sizeof($forum_id))
   1.537 +	{
   1.538 +		return array();
   1.539 +	}
   1.540 +
   1.541 +	if ($read_tracking && $config['load_db_lastread'])
   1.542 +	{
   1.543 +		$read_tracking_join = ' LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . '
   1.544 +			AND ft.forum_id = f.forum_id)';
   1.545 +		$read_tracking_select = ', ft.mark_time';
   1.546 +	}
   1.547 +	else
   1.548 +	{
   1.549 +		$read_tracking_join = $read_tracking_select = '';
   1.550 +	}
   1.551 +
   1.552 +	$sql = "SELECT f.* $read_tracking_select
   1.553 +		FROM " . FORUMS_TABLE . " f$read_tracking_join
   1.554 +		WHERE " . $db->sql_in_set('f.forum_id', $forum_id);
   1.555 +	$result = $db->sql_query($sql);
   1.556 +
   1.557 +	while ($row = $db->sql_fetchrow($result))
   1.558 +	{
   1.559 +		if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
   1.560 +		{
   1.561 +			continue;
   1.562 +		}
   1.563 +
   1.564 +		if ($auth->acl_get('m_approve', $row['forum_id']))
   1.565 +		{
   1.566 +			$row['forum_topics'] = $row['forum_topics_real'];
   1.567 +		}
   1.568 +
   1.569 +		$rowset[$row['forum_id']] = $row;
   1.570 +	}
   1.571 +	$db->sql_freeresult($result);
   1.572 +
   1.573 +	return $rowset;
   1.574 +}
   1.575 +
   1.576 +/**
   1.577 +* Get simple pm data
   1.578 +*/
   1.579 +function get_pm_data($pm_ids)
   1.580 +{
   1.581 +	global $db, $auth, $config, $user;
   1.582 +
   1.583 +	$rowset = array();
   1.584 +
   1.585 +	if (!sizeof($pm_ids))
   1.586 +	{
   1.587 +		return array();
   1.588 +	}
   1.589 +
   1.590 +	$sql_array = array(
   1.591 +		'SELECT'	=> 'p.*, u.*',
   1.592 +
   1.593 +		'FROM'		=> array(
   1.594 +			USERS_TABLE			=> 'u',
   1.595 +			PRIVMSGS_TABLE		=> 'p',
   1.596 +		),
   1.597 +
   1.598 +		'WHERE'		=> $db->sql_in_set('p.msg_id', $pm_ids) . '
   1.599 +			AND u.user_id = p.author_id',
   1.600 +	);
   1.601 +
   1.602 +	$sql = $db->sql_build_query('SELECT', $sql_array);
   1.603 +	$result = $db->sql_query($sql);
   1.604 +	unset($sql_array);
   1.605 +
   1.606 +	while ($row = $db->sql_fetchrow($result))
   1.607 +	{
   1.608 +		$rowset[$row['msg_id']] = $row;
   1.609 +	}
   1.610 +	$db->sql_freeresult($result);
   1.611 +
   1.612 +	return $rowset;
   1.613 +}
   1.614 +
   1.615 +/**
   1.616 +* sorting in mcp
   1.617 +*
   1.618 +* @param string $where_sql should either be WHERE (default if ommited) or end with AND or OR
   1.619 +*
   1.620 +* $mode reports and reports_closed: the $where parameters uses aliases p for posts table and r for report table
   1.621 +* $mode unapproved_posts: the $where parameters uses aliases p for posts table and t for topic table
   1.622 +*/
   1.623 +function mcp_sorting($mode, &$sort_days, &$sort_key, &$sort_dir, &$sort_by_sql, &$sort_order_sql, &$total, $forum_id = 0, $topic_id = 0, $where_sql = 'WHERE')
   1.624 +{
   1.625 +	global $db, $user, $auth, $template;
   1.626 +
   1.627 +	$sort_days = request_var('st', 0);
   1.628 +	$min_time = ($sort_days) ? time() - ($sort_days * 86400) : 0;
   1.629 +
   1.630 +	switch ($mode)
   1.631 +	{
   1.632 +		case 'viewforum':
   1.633 +			$type = 'topics';
   1.634 +			$default_key = 't';
   1.635 +			$default_dir = 'd';
   1.636 +
   1.637 +			$sql = 'SELECT COUNT(topic_id) AS total
   1.638 +				FROM ' . TOPICS_TABLE . "
   1.639 +				$where_sql forum_id = $forum_id
   1.640 +					AND topic_type NOT IN (" . POST_ANNOUNCE . ', ' . POST_GLOBAL . ")
   1.641 +					AND topic_last_post_time >= $min_time";
   1.642 +
   1.643 +			if (!$auth->acl_get('m_approve', $forum_id))
   1.644 +			{
   1.645 +				$sql .= 'AND topic_approved = 1';
   1.646 +			}
   1.647 +		break;
   1.648 +
   1.649 +		case 'viewtopic':
   1.650 +			$type = 'posts';
   1.651 +			$default_key = 't';
   1.652 +			$default_dir = 'a';
   1.653 +
   1.654 +			$sql = 'SELECT COUNT(post_id) AS total
   1.655 +				FROM ' . POSTS_TABLE . "
   1.656 +				$where_sql topic_id = $topic_id
   1.657 +					AND post_time >= $min_time";
   1.658 +
   1.659 +			if (!$auth->acl_get('m_approve', $forum_id))
   1.660 +			{
   1.661 +				$sql .= 'AND post_approved = 1';
   1.662 +			}
   1.663 +		break;
   1.664 +
   1.665 +		case 'unapproved_posts':
   1.666 +			$type = 'posts';
   1.667 +			$default_key = 't';
   1.668 +			$default_dir = 'd';
   1.669 +			$where_sql .= ($topic_id) ? ' p.topic_id = ' . $topic_id . ' AND' : '';
   1.670 +
   1.671 +			$sql = 'SELECT COUNT(p.post_id) AS total
   1.672 +				FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t
   1.673 +				$where_sql " . $db->sql_in_set('p.forum_id', ($forum_id) ? array($forum_id) : array_intersect(get_forum_list('f_read'), get_forum_list('m_approve'))) . '
   1.674 +					AND p.post_approved = 0
   1.675 +					AND t.topic_id = p.topic_id
   1.676 +					AND t.topic_first_post_id <> p.post_id';
   1.677 +
   1.678 +			if ($min_time)
   1.679 +			{
   1.680 +				$sql .= ' AND post_time >= ' . $min_time;
   1.681 +			}
   1.682 +		break;
   1.683 +
   1.684 +		case 'unapproved_topics':
   1.685 +			$type = 'topics';
   1.686 +			$default_key = 't';
   1.687 +			$default_dir = 'd';
   1.688 +
   1.689 +			$sql = 'SELECT COUNT(topic_id) AS total
   1.690 +				FROM ' . TOPICS_TABLE . "
   1.691 +				$where_sql " . $db->sql_in_set('forum_id', ($forum_id) ? array($forum_id) : array_intersect(get_forum_list('f_read'), get_forum_list('m_approve'))) . '
   1.692 +					AND topic_approved = 0';
   1.693 +
   1.694 +			if ($min_time)
   1.695 +			{
   1.696 +				$sql .= ' AND topic_time >= ' . $min_time;
   1.697 +			}
   1.698 +		break;
   1.699 +
   1.700 +		case 'pm_reports':
   1.701 +		case 'pm_reports_closed':
   1.702 +		case 'reports':
   1.703 +		case 'reports_closed':
   1.704 +			$pm = (strpos($mode, 'pm_') === 0) ? true : false;
   1.705 +
   1.706 +			$type = ($pm) ? 'pm_reports' : 'reports';
   1.707 +			$default_key = 't';
   1.708 +			$default_dir = 'd';
   1.709 +			$limit_time_sql = ($min_time) ? "AND r.report_time >= $min_time" : '';
   1.710 +
   1.711 +			if ($topic_id)
   1.712 +			{
   1.713 +				$where_sql .= ' p.topic_id = ' . $topic_id . ' AND ';
   1.714 +			}
   1.715 +			else if ($forum_id)
   1.716 +			{
   1.717 +				$where_sql .= ' p.forum_id = ' . $forum_id . ' AND ';
   1.718 +			}
   1.719 +			else if (!$pm)
   1.720 +			{
   1.721 +				$where_sql .= ' ' . $db->sql_in_set('p.forum_id', get_forum_list(array('!f_read', '!m_report')), true, true) . ' AND ';
   1.722 +			}
   1.723 +
   1.724 +			if ($mode == 'reports' || $mode == 'pm_reports')
   1.725 +			{
   1.726 +				$where_sql .= ' r.report_closed = 0 AND ';
   1.727 +			}
   1.728 +			else
   1.729 +			{
   1.730 +				$where_sql .= ' r.report_closed = 1 AND ';
   1.731 +			}
   1.732 +
   1.733 +			if ($pm)
   1.734 +			{
   1.735 +				$sql = 'SELECT COUNT(r.report_id) AS total
   1.736 +					FROM ' . REPORTS_TABLE . ' r, ' . PRIVMSGS_TABLE . " p
   1.737 +					$where_sql r.post_id = 0
   1.738 +						AND p.msg_id = r.pm_id
   1.739 +						$limit_time_sql";
   1.740 +			}
   1.741 +			else
   1.742 +			{
   1.743 +				$sql = 'SELECT COUNT(r.report_id) AS total
   1.744 +					FROM ' . REPORTS_TABLE . ' r, ' . POSTS_TABLE . " p
   1.745 +					$where_sql r.pm_id = 0
   1.746 +						AND p.post_id = r.post_id
   1.747 +						$limit_time_sql";
   1.748 +			}
   1.749 +		break;
   1.750 +
   1.751 +		case 'viewlogs':
   1.752 +			$type = 'logs';
   1.753 +			$default_key = 't';
   1.754 +			$default_dir = 'd';
   1.755 +
   1.756 +			$sql = 'SELECT COUNT(log_id) AS total
   1.757 +				FROM ' . LOG_TABLE . "
   1.758 +				$where_sql " . $db->sql_in_set('forum_id', ($forum_id) ? array($forum_id) : array_intersect(get_forum_list('f_read'), get_forum_list('m_'))) . '
   1.759 +					AND log_time >= ' . $min_time . '
   1.760 +					AND log_type = ' . LOG_MOD;
   1.761 +		break;
   1.762 +	}
   1.763 +
   1.764 +	$sort_key = request_var('sk', $default_key);
   1.765 +	$sort_dir = request_var('sd', $default_dir);
   1.766 +	$sort_dir_text = array('a' => $user->lang['ASCENDING'], 'd' => $user->lang['DESCENDING']);
   1.767 +
   1.768 +	switch ($type)
   1.769 +	{
   1.770 +		case 'topics':
   1.771 +			$limit_days = array(0 => $user->lang['ALL_TOPICS'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
   1.772 +			$sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 'tt' => $user->lang['TOPIC_TIME'], 'r' => $user->lang['REPLIES'], 's' => $user->lang['SUBJECT'], 'v' => $user->lang['VIEWS']);
   1.773 +
   1.774 +			$sort_by_sql = array('a' => 't.topic_first_poster_name', 't' => 't.topic_last_post_time', 'tt' => 't.topic_time', 'r' => (($auth->acl_get('m_approve', $forum_id)) ? 't.topic_replies_real' : 't.topic_replies'), 's' => 't.topic_title', 'v' => 't.topic_views');
   1.775 +			$limit_time_sql = ($min_time) ? "AND t.topic_last_post_time >= $min_time" : '';
   1.776 +		break;
   1.777 +
   1.778 +		case 'posts':
   1.779 +			$limit_days = array(0 => $user->lang['ALL_POSTS'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
   1.780 +			$sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
   1.781 +			$sort_by_sql = array('a' => 'u.username_clean', 't' => 'p.post_time', 's' => 'p.post_subject');
   1.782 +			$limit_time_sql = ($min_time) ? "AND p.post_time >= $min_time" : '';
   1.783 +		break;
   1.784 +
   1.785 +		case 'reports':
   1.786 +			$limit_days = array(0 => $user->lang['ALL_REPORTS'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
   1.787 +			$sort_by_text = array('a' => $user->lang['AUTHOR'], 'r' => $user->lang['REPORTER'], 'p' => $user->lang['POST_TIME'], 't' => $user->lang['REPORT_TIME'], 's' => $user->lang['SUBJECT']);
   1.788 +			$sort_by_sql = array('a' => 'u.username_clean', 'r' => 'ru.username', 'p' => 'p.post_time', 't' => 'r.report_time', 's' => 'p.post_subject');
   1.789 +		break;
   1.790 +
   1.791 +		case 'pm_reports':
   1.792 +			$limit_days = array(0 => $user->lang['ALL_REPORTS'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
   1.793 +			$sort_by_text = array('a' => $user->lang['AUTHOR'], 'r' => $user->lang['REPORTER'], 'p' => $user->lang['POST_TIME'], 't' => $user->lang['REPORT_TIME'], 's' => $user->lang['SUBJECT']);
   1.794 +			$sort_by_sql = array('a' => 'u.username_clean', 'r' => 'ru.username', 'p' => 'p.message_time', 't' => 'r.report_time', 's' => 'p.message_subject');
   1.795 +		break;
   1.796 +
   1.797 +		case 'logs':
   1.798 +			$limit_days = array(0 => $user->lang['ALL_ENTRIES'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
   1.799 +			$sort_by_text = array('u' => $user->lang['SORT_USERNAME'], 't' => $user->lang['SORT_DATE'], 'i' => $user->lang['SORT_IP'], 'o' => $user->lang['SORT_ACTION']);
   1.800 +
   1.801 +			$sort_by_sql = array('u' => 'u.username_clean', 't' => 'l.log_time', 'i' => 'l.log_ip', 'o' => 'l.log_operation');
   1.802 +			$limit_time_sql = ($min_time) ? "AND l.log_time >= $min_time" : '';
   1.803 +		break;
   1.804 +	}
   1.805 +
   1.806 +	if (!isset($sort_by_sql[$sort_key]))
   1.807 +	{
   1.808 +		$sort_key = $default_key;
   1.809 +	}
   1.810 +
   1.811 +	$sort_order_sql = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
   1.812 +
   1.813 +	$s_limit_days = $s_sort_key = $s_sort_dir = $sort_url = '';
   1.814 +	gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $sort_url);
   1.815 +
   1.816 +	$template->assign_vars(array(
   1.817 +		'S_SELECT_SORT_DIR'		=> $s_sort_dir,
   1.818 +		'S_SELECT_SORT_KEY'		=> $s_sort_key,
   1.819 +		'S_SELECT_SORT_DAYS'	=> $s_limit_days)
   1.820 +	);
   1.821 +
   1.822 +	if (($sort_days && $mode != 'viewlogs') || in_array($mode, array('reports', 'unapproved_topics', 'unapproved_posts')) || $where_sql != 'WHERE')
   1.823 +	{
   1.824 +		$result = $db->sql_query($sql);
   1.825 +		$total = (int) $db->sql_fetchfield('total');
   1.826 +		$db->sql_freeresult($result);
   1.827 +	}
   1.828 +	else
   1.829 +	{
   1.830 +		$total = -1;
   1.831 +	}
   1.832 +}
   1.833 +
   1.834 +/**
   1.835 +* Validate ids
   1.836 +*
   1.837 +* @param	array	&$ids			The relevant ids to check
   1.838 +* @param	string	$table			The table to find the ids in
   1.839 +* @param	string	$sql_id			The ids relevant column name
   1.840 +* @param	array	$acl_list		A list of permissions the user need to have
   1.841 +* @param	mixed	$singe_forum	Limit to one forum id (int) or the first forum found (true)
   1.842 +*
   1.843 +* @return	mixed	False if no ids were able to be retrieved, true if at least one id left.
   1.844 +*					Additionally, this value can be the forum_id assigned if $single_forum was set.
   1.845 +*					Therefore checking the result for with !== false is the best method.
   1.846 +*/
   1.847 +function check_ids(&$ids, $table, $sql_id, $acl_list = false, $single_forum = false)
   1.848 +{
   1.849 +	global $db, $auth;
   1.850 +
   1.851 +	if (!is_array($ids) || empty($ids))
   1.852 +	{
   1.853 +		return false;
   1.854 +	}
   1.855 +
   1.856 +	$sql = "SELECT $sql_id, forum_id FROM $table
   1.857 +		WHERE " . $db->sql_in_set($sql_id, $ids);
   1.858 +	$result = $db->sql_query($sql);
   1.859 +
   1.860 +	$ids = array();
   1.861 +	$forum_id = false;
   1.862 +
   1.863 +	while ($row = $db->sql_fetchrow($result))
   1.864 +	{
   1.865 +		if ($acl_list && $row['forum_id'] && !$auth->acl_gets($acl_list, $row['forum_id']))
   1.866 +		{
   1.867 +			continue;
   1.868 +		}
   1.869 +
   1.870 +		if ($acl_list && !$row['forum_id'] && !$auth->acl_getf_global($acl_list))
   1.871 +		{
   1.872 +			continue;
   1.873 +		}
   1.874 +
   1.875 +		// Limit forum? If not, just assign the id.
   1.876 +		if ($single_forum === false)
   1.877 +		{
   1.878 +			$ids[] = $row[$sql_id];
   1.879 +			continue;
   1.880 +		}
   1.881 +
   1.882 +		// Limit forum to a specific forum id?
   1.883 +		// This can get really tricky, because we do not want to create a failure on global topics. :)
   1.884 +		if ($row['forum_id'])
   1.885 +		{
   1.886 +			if ($single_forum !== true && $row['forum_id'] == (int) $single_forum)
   1.887 +			{
   1.888 +				$forum_id = (int) $single_forum;
   1.889 +			}
   1.890 +			else if ($forum_id === false)
   1.891 +			{
   1.892 +				$forum_id = $row['forum_id'];
   1.893 +			}
   1.894 +
   1.895 +			if ($row['forum_id'] == $forum_id)
   1.896 +			{
   1.897 +				$ids[] = $row[$sql_id];
   1.898 +			}
   1.899 +		}
   1.900 +		else
   1.901 +		{
   1.902 +			// Always add a global topic
   1.903 +			$ids[] = $row[$sql_id];
   1.904 +		}
   1.905 +	}
   1.906 +	$db->sql_freeresult($result);
   1.907 +
   1.908 +	if (!sizeof($ids))
   1.909 +	{
   1.910 +		return false;
   1.911 +	}
   1.912 +
   1.913 +	// If forum id is false and ids populated we may have only global announcements selected (returning 0 because of (int) $forum_id)
   1.914 +
   1.915 +	return ($single_forum === false) ? true : (int) $forum_id;
   1.916 +}
   1.917 +
   1.918 +?>
   1.919 \ No newline at end of file