Blame Identity/Models/Html/phpBB/3.0.4/mcp.php

d6e8d8
d6e8d8
/**
d6e8d8
*
d6e8d8
* @package mcp
d6e8d8
* @version $Id: mcp.php 9015 2008-10-14 18:29:50Z toonarmy $
d6e8d8
* @copyright (c) 2005 phpBB Group
d6e8d8
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
d6e8d8
*
d6e8d8
*/
d6e8d8
d6e8d8
/**
d6e8d8
* @ignore
d6e8d8
*/
d6e8d8
define('IN_PHPBB', true);
d6e8d8
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
d6e8d8
$phpEx = substr(strrchr(__FILE__, '.'), 1);
d6e8d8
include($phpbb_root_path . 'common.' . $phpEx);
d6e8d8
include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
d6e8d8
require($phpbb_root_path . 'includes/functions_module.' . $phpEx);
d6e8d8
d6e8d8
// Start session management
d6e8d8
$user->session_begin();
d6e8d8
$auth->acl($user->data);
d6e8d8
$user->setup('mcp');
d6e8d8
d6e8d8
$module = new p_master();
d6e8d8
d6e8d8
// Setting a variable to let the style designer know where he is...
d6e8d8
$template->assign_var('S_IN_MCP', true);
d6e8d8
d6e8d8
// Basic parameter data
d6e8d8
$id = request_var('i', '');
d6e8d8
d6e8d8
if (isset($_REQUEST['mode']) && is_array($_REQUEST['mode']))
d6e8d8
{
d6e8d8
	$mode = request_var('mode', array(''));
d6e8d8
	list($mode, ) = each($mode);
d6e8d8
}
d6e8d8
else
d6e8d8
{
d6e8d8
	$mode = request_var('mode', '');
d6e8d8
}
d6e8d8
d6e8d8
// Only Moderators can go beyond this point
d6e8d8
if (!$user->data['is_registered'])
d6e8d8
{
d6e8d8
	if ($user->data['is_bot'])
d6e8d8
	{
d6e8d8
		redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
d6e8d8
	}
d6e8d8
d6e8d8
	login_box('', $user->lang['LOGIN_EXPLAIN_MCP']);
d6e8d8
}
d6e8d8
d6e8d8
$quickmod = (isset($_REQUEST['quickmod'])) ? true : false;
d6e8d8
$action = request_var('action', '');
d6e8d8
$action_ary = request_var('action', array('' => 0));
d6e8d8
d6e8d8
$forum_action = request_var('forum_action', '');
d6e8d8
if ($forum_action !== '' && !empty($_POST['sort']))
d6e8d8
{
d6e8d8
	$action = $forum_action;
d6e8d8
}
d6e8d8
d6e8d8
if (sizeof($action_ary))
d6e8d8
{
d6e8d8
	list($action, ) = each($action_ary);
d6e8d8
}
d6e8d8
unset($action_ary);
d6e8d8
d6e8d8
if ($mode == 'topic_logs')
d6e8d8
{
d6e8d8
	$id = 'logs';
d6e8d8
	$quickmod = false;
d6e8d8
}
d6e8d8
d6e8d8
$post_id = request_var('p', 0);
d6e8d8
$topic_id = request_var('t', 0);
d6e8d8
$forum_id = request_var('f', 0);
d6e8d8
$user_id = request_var('u', 0);
d6e8d8
$username = utf8_normalize_nfc(request_var('username', '', true));
d6e8d8
d6e8d8
if ($post_id)
d6e8d8
{
d6e8d8
	// We determine the topic and forum id here, to make sure the moderator really has moderative rights on this post
d6e8d8
	$sql = 'SELECT topic_id, forum_id
d6e8d8
		FROM ' . POSTS_TABLE . "
d6e8d8
		WHERE post_id = $post_id";
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
	$row = $db->sql_fetchrow($result);
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	$topic_id = (int) $row['topic_id'];
d6e8d8
	$forum_id = (int) ($row['forum_id']) ? $row['forum_id'] : $forum_id;
d6e8d8
}
d6e8d8
else if ($topic_id)
d6e8d8
{
d6e8d8
	$sql = 'SELECT forum_id
d6e8d8
		FROM ' . TOPICS_TABLE . "
d6e8d8
		WHERE topic_id = $topic_id";
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
	$row = $db->sql_fetchrow($result);
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	$forum_id = (int) $row['forum_id'];
d6e8d8
}
d6e8d8
d6e8d8
// If the user doesn't have any moderator powers (globally or locally) he can't access the mcp
d6e8d8
if (!$auth->acl_getf_global('m_'))
d6e8d8
{
d6e8d8
	// Except he is using one of the quickmod tools for users
d6e8d8
	$user_quickmod_actions = array(
d6e8d8
		'lock'			=> 'f_user_lock',
d6e8d8
		'make_sticky'	=> 'f_sticky',
d6e8d8
		'make_announce'	=> 'f_announce',
d6e8d8
		'make_global'	=> 'f_announce',
d6e8d8
		'make_normal'	=> array('f_announce', 'f_sticky')
d6e8d8
	);
d6e8d8
d6e8d8
	$allow_user = false;
d6e8d8
	if ($quickmod && isset($user_quickmod_actions[$action]) && $user->data['is_registered'] && $auth->acl_gets($user_quickmod_actions[$action], $forum_id))
d6e8d8
	{
d6e8d8
		$topic_info = get_topic_data(array($topic_id));
d6e8d8
		if ($topic_info[$topic_id]['topic_poster'] == $user->data['user_id'])
d6e8d8
		{
d6e8d8
			$allow_user = true;
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	if (!$allow_user)
d6e8d8
	{
d6e8d8
		trigger_error('NOT_AUTHORISED');
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
// if the user cannot read the forum he tries to access then we won't allow mcp access either
d6e8d8
if ($forum_id && !$auth->acl_get('f_read', $forum_id))
d6e8d8
{
d6e8d8
	trigger_error('NOT_AUTHORISED');
d6e8d8
}
d6e8d8
d6e8d8
if ($forum_id)
d6e8d8
{
d6e8d8
	$module->acl_forum_id = $forum_id;
d6e8d8
}
d6e8d8
d6e8d8
// Instantiate module system and generate list of available modules
d6e8d8
$module->list_modules('mcp');
d6e8d8
d6e8d8
if ($quickmod)
d6e8d8
{
d6e8d8
	$mode = 'quickmod';
d6e8d8
d6e8d8
	switch ($action)
d6e8d8
	{
d6e8d8
		case 'lock':
d6e8d8
		case 'unlock':
d6e8d8
		case 'lock_post':
d6e8d8
		case 'unlock_post':
d6e8d8
		case 'make_sticky':
d6e8d8
		case 'make_announce':
d6e8d8
		case 'make_global':
d6e8d8
		case 'make_normal':
d6e8d8
		case 'fork':
d6e8d8
		case 'move':
d6e8d8
		case 'delete_post':
d6e8d8
		case 'delete_topic':
d6e8d8
			$module->load('mcp', 'main', 'quickmod');
d6e8d8
			return;
d6e8d8
		break;
d6e8d8
d6e8d8
		case 'topic_logs':
d6e8d8
			$module->set_active('logs', 'topic_logs');
d6e8d8
		break;
d6e8d8
d6e8d8
		case 'merge_topic':
d6e8d8
			$module->set_active('main', 'forum_view');
d6e8d8
		break;
d6e8d8
d6e8d8
		case 'split':
d6e8d8
		case 'merge':
d6e8d8
			$module->set_active('main', 'topic_view');
d6e8d8
		break;
d6e8d8
d6e8d8
		default:
d6e8d8
			trigger_error("$action not allowed as quickmod", E_USER_ERROR);
d6e8d8
		break;
d6e8d8
	}
d6e8d8
}
d6e8d8
else
d6e8d8
{
d6e8d8
	// Select the active module
d6e8d8
	$module->set_active($id, $mode);
d6e8d8
}
d6e8d8
d6e8d8
// Hide some of the options if we don't have the relevant information to use them
d6e8d8
if (!$post_id)
d6e8d8
{
d6e8d8
	$module->set_display('main', 'post_details', false);
d6e8d8
	$module->set_display('warn', 'warn_post', false);
d6e8d8
}
d6e8d8
d6e8d8
if ($mode == '' || $mode == 'unapproved_topics' || $mode == 'unapproved_posts')
d6e8d8
{
d6e8d8
	$module->set_display('queue', 'approve_details', false);
d6e8d8
}
d6e8d8
d6e8d8
if ($mode == '' || $mode == 'reports' || $mode == 'reports_closed')
d6e8d8
{
d6e8d8
	$module->set_display('reports', 'report_details', false);
d6e8d8
}
d6e8d8
d6e8d8
if (!$topic_id)
d6e8d8
{
d6e8d8
	$module->set_display('main', 'topic_view', false);
d6e8d8
	$module->set_display('logs', 'topic_logs', false);
d6e8d8
}
d6e8d8
d6e8d8
if (!$forum_id)
d6e8d8
{
d6e8d8
	$module->set_display('main', 'forum_view', false);
d6e8d8
	$module->set_display('logs', 'forum_logs', false);
d6e8d8
}
d6e8d8
d6e8d8
if (!$user_id && $username == '')
d6e8d8
{
d6e8d8
	$module->set_display('notes', 'user_notes', false);
d6e8d8
	$module->set_display('warn', 'warn_user', false);
d6e8d8
}
d6e8d8
d6e8d8
// Load and execute the relevant module
d6e8d8
$module->load_active();
d6e8d8
d6e8d8
// Assign data to the template engine for the list of modules
d6e8d8
$module->assign_tpl_vars(append_sid("{$phpbb_root_path}mcp.$phpEx"));
d6e8d8
d6e8d8
// Generate urls for letting the moderation control panel being accessed in different modes
d6e8d8
$template->assign_vars(array(
d6e8d8
	'U_MCP'			=> append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main'),
d6e8d8
	'U_MCP_FORUM'	=> ($forum_id) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&mode=forum_view&f=$forum_id") : '',
d6e8d8
	'U_MCP_TOPIC'	=> ($forum_id && $topic_id) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&mode=topic_view&t=$topic_id") : '',
d6e8d8
	'U_MCP_POST'	=> ($forum_id && $topic_id && $post_id) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&mode=post_details&t=$topic_id&p=$post_id") : '',
d6e8d8
));
d6e8d8
d6e8d8
// Generate the page, do not display/query online list
d6e8d8
$module->display($module->get_page_title(), false);
d6e8d8
d6e8d8
/**
d6e8d8
* Functions used to generate additional URL paramters
d6e8d8
*/
d6e8d8
function _module__url($mode, &$module_row)
d6e8d8
{
d6e8d8
	return extra_url();
d6e8d8
}
d6e8d8
d6e8d8
function _module_notes_url($mode, &$module_row)
d6e8d8
{
d6e8d8
	if ($mode == 'front')
d6e8d8
	{
d6e8d8
		return '';
d6e8d8
	}
d6e8d8
d6e8d8
	global $user_id;
d6e8d8
	return ($user_id) ? "&u=$user_id" : '';
d6e8d8
}
d6e8d8
d6e8d8
function _module_warn_url($mode, &$module_row)
d6e8d8
{
d6e8d8
	if ($mode == 'front' || $mode == 'list')
d6e8d8
	{
d6e8d8
		global $forum_id;
d6e8d8
d6e8d8
		return ($forum_id) ? "&f=$forum_id" : '';
d6e8d8
	}
d6e8d8
d6e8d8
	if ($mode == 'warn_post')
d6e8d8
	{
d6e8d8
		global $forum_id, $post_id;
d6e8d8
d6e8d8
		$url_extra = ($forum_id) ? "&f=$forum_id" : '';
d6e8d8
		$url_extra .= ($post_id) ? "&p=$post_id" : '';
d6e8d8
d6e8d8
		return $url_extra;
d6e8d8
	}
d6e8d8
	else
d6e8d8
	{
d6e8d8
		global $user_id;
d6e8d8
d6e8d8
		return ($user_id) ? "&u=$user_id" : '';
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
function _module_main_url($mode, &$module_row)
d6e8d8
{
d6e8d8
	return extra_url();
d6e8d8
}
d6e8d8
d6e8d8
function _module_logs_url($mode, &$module_row)
d6e8d8
{
d6e8d8
	return extra_url();
d6e8d8
}
d6e8d8
d6e8d8
function _module_ban_url($mode, &$module_row)
d6e8d8
{
d6e8d8
	return extra_url();
d6e8d8
}
d6e8d8
d6e8d8
function _module_queue_url($mode, &$module_row)
d6e8d8
{
d6e8d8
	return extra_url();
d6e8d8
}
d6e8d8
d6e8d8
function _module_reports_url($mode, &$module_row)
d6e8d8
{
d6e8d8
	return extra_url();
d6e8d8
}
d6e8d8
d6e8d8
function extra_url()
d6e8d8
{
d6e8d8
	global $forum_id, $topic_id, $post_id, $user_id;
d6e8d8
d6e8d8
	$url_extra = '';
d6e8d8
	$url_extra .= ($forum_id) ? "&f=$forum_id" : '';
d6e8d8
	$url_extra .= ($topic_id) ? "&t=$topic_id" : '';
d6e8d8
	$url_extra .= ($post_id) ? "&p=$post_id" : '';
d6e8d8
	$url_extra .= ($user_id) ? "&u=$user_id" : '';
d6e8d8
d6e8d8
	return $url_extra;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Get simple topic data
d6e8d8
*/
d6e8d8
function get_topic_data($topic_ids, $acl_list = false, $read_tracking = false)
d6e8d8
{
d6e8d8
	global $auth, $db, $config, $user;
d6e8d8
	static $rowset = array();
d6e8d8
d6e8d8
	$topics = array();
d6e8d8
d6e8d8
	if (!sizeof($topic_ids))
d6e8d8
	{
d6e8d8
		return array();
d6e8d8
	}
d6e8d8
d6e8d8
	// cache might not contain read tracking info, so we can't use it if read
d6e8d8
	// tracking information is requested
d6e8d8
	if (!$read_tracking)
d6e8d8
	{
d6e8d8
		$cache_topic_ids = array_intersect($topic_ids, array_keys($rowset));
d6e8d8
		$topic_ids = array_diff($topic_ids, array_keys($rowset));
d6e8d8
	}
d6e8d8
	else
d6e8d8
	{
d6e8d8
		$cache_topic_ids = array();
d6e8d8
	}
d6e8d8
d6e8d8
	if (sizeof($topic_ids))
d6e8d8
	{
d6e8d8
		$sql_array = array(
d6e8d8
			'SELECT'	=> 't.*, f.*',
d6e8d8
d6e8d8
			'FROM'		=> array(
d6e8d8
				TOPICS_TABLE	=> 't',
d6e8d8
			),
d6e8d8
d6e8d8
			'LEFT_JOIN'	=> array(
d6e8d8
				array(
d6e8d8
					'FROM'	=> array(FORUMS_TABLE => 'f'),
d6e8d8
					'ON'	=> 'f.forum_id = t.forum_id'
d6e8d8
				)
d6e8d8
			),
d6e8d8
d6e8d8
			'WHERE'		=> $db->sql_in_set('t.topic_id', $topic_ids)
d6e8d8
		);
d6e8d8
d6e8d8
		if ($read_tracking && $config['load_db_lastread'])
d6e8d8
		{
d6e8d8
			$sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
d6e8d8
d6e8d8
			$sql_array['LEFT_JOIN'][] = array(
d6e8d8
				'FROM'	=> array(TOPICS_TRACK_TABLE => 'tt'),
d6e8d8
				'ON'	=> 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
d6e8d8
			);
d6e8d8
d6e8d8
			$sql_array['LEFT_JOIN'][] = array(
d6e8d8
				'FROM'	=> array(FORUMS_TRACK_TABLE => 'ft'),
d6e8d8
				'ON'	=> 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
d6e8d8
			);
d6e8d8
		}
d6e8d8
d6e8d8
		$sql = $db->sql_build_query('SELECT', $sql_array);
d6e8d8
		$result = $db->sql_query($sql);
d6e8d8
d6e8d8
		while ($row = $db->sql_fetchrow($result))
d6e8d8
		{
d6e8d8
			if (!$row['forum_id'])
d6e8d8
			{
d6e8d8
				// Global Announcement?
d6e8d8
				$row['forum_id'] = request_var('f', 0);
d6e8d8
			}
d6e8d8
d6e8d8
			$rowset[$row['topic_id']] = $row;
d6e8d8
d6e8d8
			if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
d6e8d8
			{
d6e8d8
				continue;
d6e8d8
			}
d6e8d8
d6e8d8
			$topics[$row['topic_id']] = $row;
d6e8d8
		}
d6e8d8
		$db->sql_freeresult($result);
d6e8d8
	}
d6e8d8
d6e8d8
	foreach ($cache_topic_ids as $id)
d6e8d8
	{
d6e8d8
		if (!$acl_list || $auth->acl_gets($acl_list, $rowset[$id]['forum_id']))
d6e8d8
		{
d6e8d8
			$topics[$id] = $rowset[$id];
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	return $topics;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Get simple post data
d6e8d8
*/
d6e8d8
function get_post_data($post_ids, $acl_list = false, $read_tracking = false)
d6e8d8
{
d6e8d8
	global $db, $auth, $config, $user;
d6e8d8
d6e8d8
	$rowset = array();
d6e8d8
d6e8d8
	if (!sizeof($post_ids))
d6e8d8
	{
d6e8d8
		return array();
d6e8d8
	}
d6e8d8
d6e8d8
	$sql_array = array(
d6e8d8
		'SELECT'	=> 'p.*, u.*, t.*, f.*',
d6e8d8
d6e8d8
		'FROM'		=> array(
d6e8d8
			USERS_TABLE		=> 'u',
d6e8d8
			POSTS_TABLE		=> 'p',
d6e8d8
			TOPICS_TABLE	=> 't',
d6e8d8
		),
d6e8d8
d6e8d8
		'LEFT_JOIN'	=> array(
d6e8d8
			array(
d6e8d8
				'FROM'	=> array(FORUMS_TABLE => 'f'),
d6e8d8
				'ON'	=> 'f.forum_id = t.forum_id'
d6e8d8
			)
d6e8d8
		),
d6e8d8
d6e8d8
		'WHERE'		=> $db->sql_in_set('p.post_id', $post_ids) . '
d6e8d8
			AND u.user_id = p.poster_id
d6e8d8
			AND t.topic_id = p.topic_id',
d6e8d8
	);
d6e8d8
d6e8d8
	if ($read_tracking && $config['load_db_lastread'])
d6e8d8
	{
d6e8d8
		$sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
d6e8d8
d6e8d8
		$sql_array['LEFT_JOIN'][] = array(
d6e8d8
			'FROM'	=> array(TOPICS_TRACK_TABLE => 'tt'),
d6e8d8
			'ON'	=> 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
d6e8d8
		);
d6e8d8
d6e8d8
		$sql_array['LEFT_JOIN'][] = array(
d6e8d8
			'FROM'	=> array(FORUMS_TRACK_TABLE => 'ft'),
d6e8d8
			'ON'	=> 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
d6e8d8
		);
d6e8d8
	}
d6e8d8
d6e8d8
	$sql = $db->sql_build_query('SELECT', $sql_array);
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
	unset($sql_array);
d6e8d8
d6e8d8
	while ($row = $db->sql_fetchrow($result))
d6e8d8
	{
d6e8d8
		if (!$row['forum_id'])
d6e8d8
		{
d6e8d8
			// Global Announcement?
d6e8d8
			$row['forum_id'] = request_var('f', 0);
d6e8d8
		}
d6e8d8
d6e8d8
		if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
d6e8d8
		{
d6e8d8
			continue;
d6e8d8
		}
d6e8d8
d6e8d8
		if (!$row['post_approved'] && !$auth->acl_get('m_approve', $row['forum_id']))
d6e8d8
		{
d6e8d8
			// Moderators without the permission to approve post should at least not see them. ;)
d6e8d8
			continue;
d6e8d8
		}
d6e8d8
d6e8d8
		$rowset[$row['post_id']] = $row;
d6e8d8
	}
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	return $rowset;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Get simple forum data
d6e8d8
*/
d6e8d8
function get_forum_data($forum_id, $acl_list = 'f_list', $read_tracking = false)
d6e8d8
{
d6e8d8
	global $auth, $db, $user, $config;
d6e8d8
d6e8d8
	$rowset = array();
d6e8d8
d6e8d8
	if (!is_array($forum_id))
d6e8d8
	{
d6e8d8
		$forum_id = array($forum_id);
d6e8d8
	}
d6e8d8
d6e8d8
	if (!sizeof($forum_id))
d6e8d8
	{
d6e8d8
		return array();
d6e8d8
	}
d6e8d8
d6e8d8
	if ($read_tracking && $config['load_db_lastread'])
d6e8d8
	{
d6e8d8
		$read_tracking_join = ' LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . '
d6e8d8
			AND ft.forum_id = f.forum_id)';
d6e8d8
		$read_tracking_select = ', ft.mark_time';
d6e8d8
	}
d6e8d8
	else
d6e8d8
	{
d6e8d8
		$read_tracking_join = $read_tracking_select = '';
d6e8d8
	}
d6e8d8
d6e8d8
	$sql = "SELECT f.* $read_tracking_select
d6e8d8
		FROM " . FORUMS_TABLE . " f$read_tracking_join
d6e8d8
		WHERE " . $db->sql_in_set('f.forum_id', $forum_id);
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
d6e8d8
	while ($row = $db->sql_fetchrow($result))
d6e8d8
	{
d6e8d8
		if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
d6e8d8
		{
d6e8d8
			continue;
d6e8d8
		}
d6e8d8
d6e8d8
		if ($auth->acl_get('m_approve', $row['forum_id']))
d6e8d8
		{
d6e8d8
			$row['forum_topics'] = $row['forum_topics_real'];
d6e8d8
		}
d6e8d8
d6e8d8
		$rowset[$row['forum_id']] = $row;
d6e8d8
	}
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	return $rowset;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* sorting in mcp
d6e8d8
*
d6e8d8
* @param string $where_sql should either be WHERE (default if ommited) or end with AND or OR
d6e8d8
*/
d6e8d8
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')
d6e8d8
{
d6e8d8
	global $db, $user, $auth, $template;
d6e8d8
d6e8d8
	$sort_days = request_var('st', 0);
d6e8d8
	$min_time = ($sort_days) ? time() - ($sort_days * 86400) : 0;
d6e8d8
d6e8d8
	switch ($mode)
d6e8d8
	{
d6e8d8
		case 'viewforum':
d6e8d8
			$type = 'topics';
d6e8d8
			$default_key = 't';
d6e8d8
			$default_dir = 'd';
d6e8d8
d6e8d8
			$sql = 'SELECT COUNT(topic_id) AS total
d6e8d8
				FROM ' . TOPICS_TABLE . "
d6e8d8
				$where_sql forum_id = $forum_id
d6e8d8
					AND topic_type NOT IN (" . POST_ANNOUNCE . ', ' . POST_GLOBAL . ")
d6e8d8
					AND topic_last_post_time >= $min_time";
d6e8d8
d6e8d8
			if (!$auth->acl_get('m_approve', $forum_id))
d6e8d8
			{
d6e8d8
				$sql .= 'AND topic_approved = 1';
d6e8d8
			}
d6e8d8
		break;
d6e8d8
d6e8d8
		case 'viewtopic':
d6e8d8
			$type = 'posts';
d6e8d8
			$default_key = 't';
d6e8d8
			$default_dir = 'a';
d6e8d8
d6e8d8
			$sql = 'SELECT COUNT(post_id) AS total
d6e8d8
				FROM ' . POSTS_TABLE . "
d6e8d8
				$where_sql topic_id = $topic_id
d6e8d8
					AND post_time >= $min_time";
d6e8d8
d6e8d8
			if (!$auth->acl_get('m_approve', $forum_id))
d6e8d8
			{
d6e8d8
				$sql .= 'AND post_approved = 1';
d6e8d8
			}
d6e8d8
		break;
d6e8d8
d6e8d8
		case 'unapproved_posts':
d6e8d8
			$type = 'posts';
d6e8d8
			$default_key = 't';
d6e8d8
			$default_dir = 'd';
d6e8d8
			$where_sql .= ($topic_id) ? ' topic_id = ' . $topic_id . ' AND' : '';
d6e8d8
d6e8d8
			$sql = 'SELECT COUNT(post_id) AS total
d6e8d8
				FROM ' . POSTS_TABLE . "
d6e8d8
				$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'))) . '
d6e8d8
					AND post_approved = 0';
d6e8d8
d6e8d8
			if ($min_time)
d6e8d8
			{
d6e8d8
				$sql .= ' AND post_time >= ' . $min_time;
d6e8d8
			}
d6e8d8
		break;
d6e8d8
d6e8d8
		case 'unapproved_topics':
d6e8d8
			$type = 'topics';
d6e8d8
			$default_key = 't';
d6e8d8
			$default_dir = 'd';
d6e8d8
d6e8d8
			$sql = 'SELECT COUNT(topic_id) AS total
d6e8d8
				FROM ' . TOPICS_TABLE . "
d6e8d8
				$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'))) . '
d6e8d8
					AND topic_approved = 0';
d6e8d8
d6e8d8
			if ($min_time)
d6e8d8
			{
d6e8d8
				$sql .= ' AND topic_time >= ' . $min_time;
d6e8d8
			}
d6e8d8
		break;
d6e8d8
d6e8d8
		case 'reports':
d6e8d8
		case 'reports_closed':
d6e8d8
			$type = 'reports';
d6e8d8
			$default_key = 't';
d6e8d8
			$default_dir = 'd';
d6e8d8
			$limit_time_sql = ($min_time) ? "AND r.report_time >= $min_time" : '';
d6e8d8
d6e8d8
			if ($topic_id)
d6e8d8
			{
d6e8d8
				$where_sql .= ' p.topic_id = ' . $topic_id;
d6e8d8
			}
d6e8d8
			else if ($forum_id)
d6e8d8
			{
d6e8d8
				$where_sql .= ' p.forum_id = ' . $forum_id;
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				$where_sql .= ' ' . $db->sql_in_set('p.forum_id', get_forum_list(array('!f_read', '!m_report')), true, true);
d6e8d8
			}
d6e8d8
d6e8d8
			if ($mode == 'reports')
d6e8d8
			{
d6e8d8
				$where_sql .= ' AND r.report_closed = 0';
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				$where_sql .= ' AND r.report_closed = 1';
d6e8d8
			}
d6e8d8
d6e8d8
			$sql = 'SELECT COUNT(r.report_id) AS total
d6e8d8
				FROM ' . REPORTS_TABLE . ' r, ' . POSTS_TABLE . " p
d6e8d8
				$where_sql
d6e8d8
					AND p.post_id = r.post_id
d6e8d8
					$limit_time_sql";
d6e8d8
		break;
d6e8d8
d6e8d8
		case 'viewlogs':
d6e8d8
			$type = 'logs';
d6e8d8
			$default_key = 't';
d6e8d8
			$default_dir = 'd';
d6e8d8
d6e8d8
			$sql = 'SELECT COUNT(log_id) AS total
d6e8d8
				FROM ' . LOG_TABLE . "
d6e8d8
				$where_sql " . $db->sql_in_set('forum_id', ($forum_id) ? array($forum_id) : array_intersect(get_forum_list('f_read'), get_forum_list('m_'))) . '
d6e8d8
					AND log_time >= ' . $min_time . '
d6e8d8
					AND log_type = ' . LOG_MOD;
d6e8d8
		break;
d6e8d8
	}
d6e8d8
d6e8d8
	$sort_key = request_var('sk', $default_key);
d6e8d8
	$sort_dir = request_var('sd', $default_dir);
d6e8d8
	$sort_dir_text = array('a' => $user->lang['ASCENDING'], 'd' => $user->lang['DESCENDING']);
d6e8d8
d6e8d8
	switch ($type)
d6e8d8
	{
d6e8d8
		case 'topics':
d6e8d8
			$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']);
d6e8d8
			$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']);
d6e8d8
d6e8d8
			$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');
d6e8d8
			$limit_time_sql = ($min_time) ? "AND t.topic_last_post_time >= $min_time" : '';
d6e8d8
		break;
d6e8d8
d6e8d8
		case 'posts':
d6e8d8
			$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']);
d6e8d8
			$sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
d6e8d8
			$sort_by_sql = array('a' => 'u.username_clean', 't' => 'p.post_time', 's' => 'p.post_subject');
d6e8d8
			$limit_time_sql = ($min_time) ? "AND p.post_time >= $min_time" : '';
d6e8d8
		break;
d6e8d8
d6e8d8
		case 'reports':
d6e8d8
			$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']);
d6e8d8
			$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']);
d6e8d8
			$sort_by_sql = array('a' => 'u.username_clean', 'r' => 'ru.username', 'p' => 'p.post_time', 't' => 'r.report_time', 's' => 'p.post_subject');
d6e8d8
		break;
d6e8d8
d6e8d8
		case 'logs':
d6e8d8
			$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']);
d6e8d8
			$sort_by_text = array('u' => $user->lang['SORT_USERNAME'], 't' => $user->lang['SORT_DATE'], 'i' => $user->lang['SORT_IP'], 'o' => $user->lang['SORT_ACTION']);
d6e8d8
d6e8d8
			$sort_by_sql = array('u' => 'u.username_clean', 't' => 'l.log_time', 'i' => 'l.log_ip', 'o' => 'l.log_operation');
d6e8d8
			$limit_time_sql = ($min_time) ? "AND l.log_time >= $min_time" : '';
d6e8d8
		break;
d6e8d8
	}
d6e8d8
d6e8d8
	if (!isset($sort_by_sql[$sort_key]))
d6e8d8
	{
d6e8d8
		$sort_key = $default_key;
d6e8d8
	}
d6e8d8
d6e8d8
	$sort_order_sql = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
d6e8d8
d6e8d8
	$s_limit_days = $s_sort_key = $s_sort_dir = $sort_url = '';
d6e8d8
	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);
d6e8d8
d6e8d8
	$template->assign_vars(array(
d6e8d8
		'S_SELECT_SORT_DIR'		=> $s_sort_dir,
d6e8d8
		'S_SELECT_SORT_KEY'		=> $s_sort_key,
d6e8d8
		'S_SELECT_SORT_DAYS'	=> $s_limit_days)
d6e8d8
	);
d6e8d8
d6e8d8
	if (($sort_days && $mode != 'viewlogs') || in_array($mode, array('reports', 'unapproved_topics', 'unapproved_posts')) || $where_sql != 'WHERE')
d6e8d8
	{
d6e8d8
		$result = $db->sql_query($sql);
d6e8d8
		$total = (int) $db->sql_fetchfield('total');
d6e8d8
		$db->sql_freeresult($result);
d6e8d8
	}
d6e8d8
	else
d6e8d8
	{
d6e8d8
		$total = -1;
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Validate ids
d6e8d8
*
d6e8d8
* @param	array	&$ids			The relevant ids to check
d6e8d8
* @param	string	$table			The table to find the ids in
d6e8d8
* @param	string	$sql_id			The ids relevant column name
d6e8d8
* @param	array	$acl_list		A list of permissions the user need to have
d6e8d8
* @param	mixed	$singe_forum	Limit to one forum id (int) or the first forum found (true)
d6e8d8
*
d6e8d8
* @return	mixed	False if no ids were able to be retrieved, true if at least one id left.
d6e8d8
*					Additionally, this value can be the forum_id assigned if $single_forum was set.
d6e8d8
*					Therefore checking the result for with !== false is the best method.
d6e8d8
*/
d6e8d8
function check_ids(&$ids, $table, $sql_id, $acl_list = false, $single_forum = false)
d6e8d8
{
d6e8d8
	global $db, $auth;
d6e8d8
d6e8d8
	if (!is_array($ids) || empty($ids))
d6e8d8
	{
d6e8d8
		return false;
d6e8d8
	}
d6e8d8
d6e8d8
	$sql = "SELECT $sql_id, forum_id FROM $table
d6e8d8
		WHERE " . $db->sql_in_set($sql_id, $ids);
d6e8d8
	$result = $db->sql_query($sql);
d6e8d8
d6e8d8
	$ids = array();
d6e8d8
	$forum_id = false;
d6e8d8
d6e8d8
	while ($row = $db->sql_fetchrow($result))
d6e8d8
	{
d6e8d8
		if ($acl_list && $row['forum_id'] && !$auth->acl_gets($acl_list, $row['forum_id']))
d6e8d8
		{
d6e8d8
			continue;
d6e8d8
		}
d6e8d8
d6e8d8
		if ($acl_list && !$row['forum_id'] && !$auth->acl_getf_global($acl_list))
d6e8d8
		{
d6e8d8
			continue;
d6e8d8
		}
d6e8d8
d6e8d8
		// Limit forum? If not, just assign the id.
d6e8d8
		if ($single_forum === false)
d6e8d8
		{
d6e8d8
			$ids[] = $row[$sql_id];
d6e8d8
			continue;
d6e8d8
		}
d6e8d8
d6e8d8
		// Limit forum to a specific forum id?
d6e8d8
		// This can get really tricky, because we do not want to create a failure on global topics. :)
d6e8d8
		if ($row['forum_id'])
d6e8d8
		{
d6e8d8
			if ($single_forum !== true && $row['forum_id'] == (int) $single_forum)
d6e8d8
			{
d6e8d8
				$forum_id = (int) $single_forum;
d6e8d8
			}
d6e8d8
			else if ($forum_id === false)
d6e8d8
			{
d6e8d8
				$forum_id = $row['forum_id'];
d6e8d8
			}
d6e8d8
d6e8d8
			if ($row['forum_id'] == $forum_id)
d6e8d8
			{
d6e8d8
				$ids[] = $row[$sql_id];
d6e8d8
			}
d6e8d8
		}
d6e8d8
		else
d6e8d8
		{
d6e8d8
			// Always add a global topic
d6e8d8
			$ids[] = $row[$sql_id];
d6e8d8
		}
d6e8d8
	}
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
d6e8d8
	if (!sizeof($ids))
d6e8d8
	{
d6e8d8
		return false;
d6e8d8
	}
d6e8d8
d6e8d8
	// If forum id is false and ids populated we may have only global announcements selected (returning 0 because of (int) $forum_id)
d6e8d8
d6e8d8
	return ($single_forum === false) ? true : (int) $forum_id;
d6e8d8
}
d6e8d8
d6e8d8
?>