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

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