Blame Extras/phpBB/3.0.4/mcp.php

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