Blame Identity/Webenv/phpBB/3.0.4/mcp.php

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