mcp.php
changeset 0 ceef6d64c21d
equal deleted inserted replaced
-1:000000000000 0:ceef6d64c21d
       
     1 <?php
       
     2 /**
       
     3 *
       
     4 * @package mcp
       
     5 * @version $Id$
       
     6 * @copyright (c) 2005 phpBB Group
       
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
       
     8 *
       
     9 */
       
    10 
       
    11 /**
       
    12 * @ignore
       
    13 */
       
    14 define('IN_PHPBB', true);
       
    15 $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
       
    16 $phpEx = substr(strrchr(__FILE__, '.'), 1);
       
    17 include($phpbb_root_path . 'common.' . $phpEx);
       
    18 include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
       
    19 require($phpbb_root_path . 'includes/functions_module.' . $phpEx);
       
    20 
       
    21 // Start session management
       
    22 $user->session_begin();
       
    23 $auth->acl($user->data);
       
    24 $user->setup('mcp');
       
    25 
       
    26 $module = new p_master();
       
    27 
       
    28 // Setting a variable to let the style designer know where he is...
       
    29 $template->assign_var('S_IN_MCP', true);
       
    30 
       
    31 // Basic parameter data
       
    32 $id = request_var('i', '');
       
    33 
       
    34 if (isset($_REQUEST['mode']) && is_array($_REQUEST['mode']))
       
    35 {
       
    36 	$mode = request_var('mode', array(''));
       
    37 	list($mode, ) = each($mode);
       
    38 }
       
    39 else
       
    40 {
       
    41 	$mode = request_var('mode', '');
       
    42 }
       
    43 
       
    44 // Only Moderators can go beyond this point
       
    45 if (!$user->data['is_registered'])
       
    46 {
       
    47 	if ($user->data['is_bot'])
       
    48 	{
       
    49 		redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
       
    50 	}
       
    51 
       
    52 	login_box('', $user->lang['LOGIN_EXPLAIN_MCP']);
       
    53 }
       
    54 
       
    55 $quickmod = (isset($_REQUEST['quickmod'])) ? true : false;
       
    56 $action = request_var('action', '');
       
    57 $action_ary = request_var('action', array('' => 0));
       
    58 
       
    59 $forum_action = request_var('forum_action', '');
       
    60 if ($forum_action !== '' && !empty($_POST['sort']))
       
    61 {
       
    62 	$action = $forum_action;
       
    63 }
       
    64 
       
    65 if (sizeof($action_ary))
       
    66 {
       
    67 	list($action, ) = each($action_ary);
       
    68 }
       
    69 unset($action_ary);
       
    70 
       
    71 if ($mode == 'topic_logs')
       
    72 {
       
    73 	$id = 'logs';
       
    74 	$quickmod = false;
       
    75 }
       
    76 
       
    77 $post_id = request_var('p', 0);
       
    78 $topic_id = request_var('t', 0);
       
    79 $forum_id = request_var('f', 0);
       
    80 $report_id = request_var('r', 0);
       
    81 $user_id = request_var('u', 0);
       
    82 $username = utf8_normalize_nfc(request_var('username', '', true));
       
    83 
       
    84 if ($post_id)
       
    85 {
       
    86 	// We determine the topic and forum id here, to make sure the moderator really has moderative rights on this post
       
    87 	$sql = 'SELECT topic_id, forum_id
       
    88 		FROM ' . POSTS_TABLE . "
       
    89 		WHERE post_id = $post_id";
       
    90 	$result = $db->sql_query($sql);
       
    91 	$row = $db->sql_fetchrow($result);
       
    92 	$db->sql_freeresult($result);
       
    93 
       
    94 	$topic_id = (int) $row['topic_id'];
       
    95 	$forum_id = (int) ($row['forum_id']) ? $row['forum_id'] : $forum_id;
       
    96 }
       
    97 else if ($topic_id)
       
    98 {
       
    99 	$sql = 'SELECT forum_id
       
   100 		FROM ' . TOPICS_TABLE . "
       
   101 		WHERE topic_id = $topic_id";
       
   102 	$result = $db->sql_query($sql);
       
   103 	$row = $db->sql_fetchrow($result);
       
   104 	$db->sql_freeresult($result);
       
   105 
       
   106 	$forum_id = (int) $row['forum_id'];
       
   107 }
       
   108 
       
   109 // If the user doesn't have any moderator powers (globally or locally) he can't access the mcp
       
   110 if (!$auth->acl_getf_global('m_'))
       
   111 {
       
   112 	// Except he is using one of the quickmod tools for users
       
   113 	$user_quickmod_actions = array(
       
   114 		'lock'			=> 'f_user_lock',
       
   115 		'make_sticky'	=> 'f_sticky',
       
   116 		'make_announce'	=> 'f_announce',
       
   117 		'make_global'	=> 'f_announce',
       
   118 		'make_normal'	=> array('f_announce', 'f_sticky')
       
   119 	);
       
   120 
       
   121 	$allow_user = false;
       
   122 	if ($quickmod && isset($user_quickmod_actions[$action]) && $user->data['is_registered'] && $auth->acl_gets($user_quickmod_actions[$action], $forum_id))
       
   123 	{
       
   124 		$topic_info = get_topic_data(array($topic_id));
       
   125 		if ($topic_info[$topic_id]['topic_poster'] == $user->data['user_id'])
       
   126 		{
       
   127 			$allow_user = true;
       
   128 		}
       
   129 	}
       
   130 
       
   131 	if (!$allow_user)
       
   132 	{
       
   133 		trigger_error('NOT_AUTHORISED');
       
   134 	}
       
   135 }
       
   136 
       
   137 // if the user cannot read the forum he tries to access then we won't allow mcp access either
       
   138 if ($forum_id && !$auth->acl_get('f_read', $forum_id))
       
   139 {
       
   140 	trigger_error('NOT_AUTHORISED');
       
   141 }
       
   142 
       
   143 if ($forum_id)
       
   144 {
       
   145 	$module->acl_forum_id = $forum_id;
       
   146 }
       
   147 
       
   148 // Instantiate module system and generate list of available modules
       
   149 $module->list_modules('mcp');
       
   150 
       
   151 if ($quickmod)
       
   152 {
       
   153 	$mode = 'quickmod';
       
   154 
       
   155 	switch ($action)
       
   156 	{
       
   157 		case 'lock':
       
   158 		case 'unlock':
       
   159 		case 'lock_post':
       
   160 		case 'unlock_post':
       
   161 		case 'make_sticky':
       
   162 		case 'make_announce':
       
   163 		case 'make_global':
       
   164 		case 'make_normal':
       
   165 		case 'fork':
       
   166 		case 'move':
       
   167 		case 'delete_post':
       
   168 		case 'delete_topic':
       
   169 			$module->load('mcp', 'main', 'quickmod');
       
   170 			return;
       
   171 		break;
       
   172 
       
   173 		case 'topic_logs':
       
   174 			// Reset start parameter if we jumped from the quickmod dropdown
       
   175 			if (request_var('start', 0))
       
   176 			{
       
   177 				$_REQUEST['start'] = 0;
       
   178 			}
       
   179 
       
   180 			$module->set_active('logs', 'topic_logs');
       
   181 		break;
       
   182 
       
   183 		case 'merge_topic':
       
   184 			$module->set_active('main', 'forum_view');
       
   185 		break;
       
   186 
       
   187 		case 'split':
       
   188 		case 'merge':
       
   189 			$module->set_active('main', 'topic_view');
       
   190 		break;
       
   191 
       
   192 		default:
       
   193 			trigger_error("$action not allowed as quickmod", E_USER_ERROR);
       
   194 		break;
       
   195 	}
       
   196 }
       
   197 else
       
   198 {
       
   199 	// Select the active module
       
   200 	$module->set_active($id, $mode);
       
   201 }
       
   202 
       
   203 // Hide some of the options if we don't have the relevant information to use them
       
   204 if (!$post_id)
       
   205 {
       
   206 	$module->set_display('main', 'post_details', false);
       
   207 	$module->set_display('warn', 'warn_post', false);
       
   208 }
       
   209 
       
   210 if ($mode == '' || $mode == 'unapproved_topics' || $mode == 'unapproved_posts')
       
   211 {
       
   212 	$module->set_display('queue', 'approve_details', false);
       
   213 }
       
   214 
       
   215 if ($mode == '' || $mode == 'reports' || $mode == 'reports_closed' || $mode == 'pm_reports' || $mode == 'pm_reports_closed' || $mode == 'pm_report_details')
       
   216 {
       
   217 	$module->set_display('reports', 'report_details', false);
       
   218 }
       
   219 
       
   220 if ($mode == '' || $mode == 'reports' || $mode == 'reports_closed' || $mode == 'pm_reports' || $mode == 'pm_reports_closed' || $mode == 'report_details')
       
   221 {
       
   222 	$module->set_display('pm_reports', 'pm_report_details', false);
       
   223 }
       
   224 
       
   225 if (!$topic_id)
       
   226 {
       
   227 	$module->set_display('main', 'topic_view', false);
       
   228 	$module->set_display('logs', 'topic_logs', false);
       
   229 }
       
   230 
       
   231 if (!$forum_id)
       
   232 {
       
   233 	$module->set_display('main', 'forum_view', false);
       
   234 	$module->set_display('logs', 'forum_logs', false);
       
   235 }
       
   236 
       
   237 if (!$user_id && $username == '')
       
   238 {
       
   239 	$module->set_display('notes', 'user_notes', false);
       
   240 	$module->set_display('warn', 'warn_user', false);
       
   241 }
       
   242 
       
   243 // Load and execute the relevant module
       
   244 $module->load_active();
       
   245 
       
   246 // Assign data to the template engine for the list of modules
       
   247 $module->assign_tpl_vars(append_sid("{$phpbb_root_path}mcp.$phpEx"));
       
   248 
       
   249 // Generate urls for letting the moderation control panel being accessed in different modes
       
   250 $template->assign_vars(array(
       
   251 	'U_MCP'			=> append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main'),
       
   252 	'U_MCP_FORUM'	=> ($forum_id) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=forum_view&amp;f=$forum_id") : '',
       
   253 	'U_MCP_TOPIC'	=> ($forum_id && $topic_id) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=topic_view&amp;t=$topic_id") : '',
       
   254 	'U_MCP_POST'	=> ($forum_id && $topic_id && $post_id) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=post_details&amp;t=$topic_id&amp;p=$post_id") : '',
       
   255 ));
       
   256 
       
   257 // Generate the page, do not display/query online list
       
   258 $module->display($module->get_page_title(), false);
       
   259 
       
   260 /**
       
   261 * Functions used to generate additional URL paramters
       
   262 */
       
   263 function _module__url($mode, &$module_row)
       
   264 {
       
   265 	return extra_url();
       
   266 }
       
   267 
       
   268 function _module_notes_url($mode, &$module_row)
       
   269 {
       
   270 	if ($mode == 'front')
       
   271 	{
       
   272 		return '';
       
   273 	}
       
   274 
       
   275 	global $user_id;
       
   276 	return ($user_id) ? "&amp;u=$user_id" : '';
       
   277 }
       
   278 
       
   279 function _module_warn_url($mode, &$module_row)
       
   280 {
       
   281 	if ($mode == 'front' || $mode == 'list')
       
   282 	{
       
   283 		global $forum_id;
       
   284 
       
   285 		return ($forum_id) ? "&amp;f=$forum_id" : '';
       
   286 	}
       
   287 
       
   288 	if ($mode == 'warn_post')
       
   289 	{
       
   290 		global $forum_id, $post_id;
       
   291 
       
   292 		$url_extra = ($forum_id) ? "&amp;f=$forum_id" : '';
       
   293 		$url_extra .= ($post_id) ? "&amp;p=$post_id" : '';
       
   294 
       
   295 		return $url_extra;
       
   296 	}
       
   297 	else
       
   298 	{
       
   299 		global $user_id;
       
   300 
       
   301 		return ($user_id) ? "&amp;u=$user_id" : '';
       
   302 	}
       
   303 }
       
   304 
       
   305 function _module_main_url($mode, &$module_row)
       
   306 {
       
   307 	return extra_url();
       
   308 }
       
   309 
       
   310 function _module_logs_url($mode, &$module_row)
       
   311 {
       
   312 	return extra_url();
       
   313 }
       
   314 
       
   315 function _module_ban_url($mode, &$module_row)
       
   316 {
       
   317 	return extra_url();
       
   318 }
       
   319 
       
   320 function _module_queue_url($mode, &$module_row)
       
   321 {
       
   322 	return extra_url();
       
   323 }
       
   324 
       
   325 function _module_reports_url($mode, &$module_row)
       
   326 {
       
   327 	return extra_url();
       
   328 }
       
   329 
       
   330 function extra_url()
       
   331 {
       
   332 	global $forum_id, $topic_id, $post_id, $report_id, $user_id;
       
   333 
       
   334 	$url_extra = '';
       
   335 	$url_extra .= ($forum_id) ? "&amp;f=$forum_id" : '';
       
   336 	$url_extra .= ($topic_id) ? "&amp;t=$topic_id" : '';
       
   337 	$url_extra .= ($post_id) ? "&amp;p=$post_id" : '';
       
   338 	$url_extra .= ($user_id) ? "&amp;u=$user_id" : '';
       
   339 	$url_extra .= ($report_id) ? "&amp;r=$report_id" : '';
       
   340 
       
   341 	return $url_extra;
       
   342 }
       
   343 
       
   344 /**
       
   345 * Get simple topic data
       
   346 */
       
   347 function get_topic_data($topic_ids, $acl_list = false, $read_tracking = false)
       
   348 {
       
   349 	global $auth, $db, $config, $user;
       
   350 	static $rowset = array();
       
   351 
       
   352 	$topics = array();
       
   353 
       
   354 	if (!sizeof($topic_ids))
       
   355 	{
       
   356 		return array();
       
   357 	}
       
   358 
       
   359 	// cache might not contain read tracking info, so we can't use it if read
       
   360 	// tracking information is requested
       
   361 	if (!$read_tracking)
       
   362 	{
       
   363 		$cache_topic_ids = array_intersect($topic_ids, array_keys($rowset));
       
   364 		$topic_ids = array_diff($topic_ids, array_keys($rowset));
       
   365 	}
       
   366 	else
       
   367 	{
       
   368 		$cache_topic_ids = array();
       
   369 	}
       
   370 
       
   371 	if (sizeof($topic_ids))
       
   372 	{
       
   373 		$sql_array = array(
       
   374 			'SELECT'	=> 't.*, f.*',
       
   375 
       
   376 			'FROM'		=> array(
       
   377 				TOPICS_TABLE	=> 't',
       
   378 			),
       
   379 
       
   380 			'LEFT_JOIN'	=> array(
       
   381 				array(
       
   382 					'FROM'	=> array(FORUMS_TABLE => 'f'),
       
   383 					'ON'	=> 'f.forum_id = t.forum_id'
       
   384 				)
       
   385 			),
       
   386 
       
   387 			'WHERE'		=> $db->sql_in_set('t.topic_id', $topic_ids)
       
   388 		);
       
   389 
       
   390 		if ($read_tracking && $config['load_db_lastread'])
       
   391 		{
       
   392 			$sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
       
   393 
       
   394 			$sql_array['LEFT_JOIN'][] = array(
       
   395 				'FROM'	=> array(TOPICS_TRACK_TABLE => 'tt'),
       
   396 				'ON'	=> 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
       
   397 			);
       
   398 
       
   399 			$sql_array['LEFT_JOIN'][] = array(
       
   400 				'FROM'	=> array(FORUMS_TRACK_TABLE => 'ft'),
       
   401 				'ON'	=> 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
       
   402 			);
       
   403 		}
       
   404 
       
   405 		$sql = $db->sql_build_query('SELECT', $sql_array);
       
   406 		$result = $db->sql_query($sql);
       
   407 
       
   408 		while ($row = $db->sql_fetchrow($result))
       
   409 		{
       
   410 			if (!$row['forum_id'])
       
   411 			{
       
   412 				// Global Announcement?
       
   413 				$row['forum_id'] = request_var('f', 0);
       
   414 			}
       
   415 
       
   416 			$rowset[$row['topic_id']] = $row;
       
   417 
       
   418 			if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
       
   419 			{
       
   420 				continue;
       
   421 			}
       
   422 
       
   423 			$topics[$row['topic_id']] = $row;
       
   424 		}
       
   425 		$db->sql_freeresult($result);
       
   426 	}
       
   427 
       
   428 	foreach ($cache_topic_ids as $id)
       
   429 	{
       
   430 		if (!$acl_list || $auth->acl_gets($acl_list, $rowset[$id]['forum_id']))
       
   431 		{
       
   432 			$topics[$id] = $rowset[$id];
       
   433 		}
       
   434 	}
       
   435 
       
   436 	return $topics;
       
   437 }
       
   438 
       
   439 /**
       
   440 * Get simple post data
       
   441 */
       
   442 function get_post_data($post_ids, $acl_list = false, $read_tracking = false)
       
   443 {
       
   444 	global $db, $auth, $config, $user;
       
   445 
       
   446 	$rowset = array();
       
   447 
       
   448 	if (!sizeof($post_ids))
       
   449 	{
       
   450 		return array();
       
   451 	}
       
   452 
       
   453 	$sql_array = array(
       
   454 		'SELECT'	=> 'p.*, u.*, t.*, f.*',
       
   455 
       
   456 		'FROM'		=> array(
       
   457 			USERS_TABLE		=> 'u',
       
   458 			POSTS_TABLE		=> 'p',
       
   459 			TOPICS_TABLE	=> 't',
       
   460 		),
       
   461 
       
   462 		'LEFT_JOIN'	=> array(
       
   463 			array(
       
   464 				'FROM'	=> array(FORUMS_TABLE => 'f'),
       
   465 				'ON'	=> 'f.forum_id = t.forum_id'
       
   466 			)
       
   467 		),
       
   468 
       
   469 		'WHERE'		=> $db->sql_in_set('p.post_id', $post_ids) . '
       
   470 			AND u.user_id = p.poster_id
       
   471 			AND t.topic_id = p.topic_id',
       
   472 	);
       
   473 
       
   474 	if ($read_tracking && $config['load_db_lastread'])
       
   475 	{
       
   476 		$sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
       
   477 
       
   478 		$sql_array['LEFT_JOIN'][] = array(
       
   479 			'FROM'	=> array(TOPICS_TRACK_TABLE => 'tt'),
       
   480 			'ON'	=> 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
       
   481 		);
       
   482 
       
   483 		$sql_array['LEFT_JOIN'][] = array(
       
   484 			'FROM'	=> array(FORUMS_TRACK_TABLE => 'ft'),
       
   485 			'ON'	=> 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
       
   486 		);
       
   487 	}
       
   488 
       
   489 	$sql = $db->sql_build_query('SELECT', $sql_array);
       
   490 	$result = $db->sql_query($sql);
       
   491 	unset($sql_array);
       
   492 
       
   493 	while ($row = $db->sql_fetchrow($result))
       
   494 	{
       
   495 		if (!$row['forum_id'])
       
   496 		{
       
   497 			// Global Announcement?
       
   498 			$row['forum_id'] = request_var('f', 0);
       
   499 		}
       
   500 
       
   501 		if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
       
   502 		{
       
   503 			continue;
       
   504 		}
       
   505 
       
   506 		if (!$row['post_approved'] && !$auth->acl_get('m_approve', $row['forum_id']))
       
   507 		{
       
   508 			// Moderators without the permission to approve post should at least not see them. ;)
       
   509 			continue;
       
   510 		}
       
   511 
       
   512 		$rowset[$row['post_id']] = $row;
       
   513 	}
       
   514 	$db->sql_freeresult($result);
       
   515 
       
   516 	return $rowset;
       
   517 }
       
   518 
       
   519 /**
       
   520 * Get simple forum data
       
   521 */
       
   522 function get_forum_data($forum_id, $acl_list = 'f_list', $read_tracking = false)
       
   523 {
       
   524 	global $auth, $db, $user, $config;
       
   525 
       
   526 	$rowset = array();
       
   527 
       
   528 	if (!is_array($forum_id))
       
   529 	{
       
   530 		$forum_id = array($forum_id);
       
   531 	}
       
   532 
       
   533 	if (!sizeof($forum_id))
       
   534 	{
       
   535 		return array();
       
   536 	}
       
   537 
       
   538 	if ($read_tracking && $config['load_db_lastread'])
       
   539 	{
       
   540 		$read_tracking_join = ' LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . '
       
   541 			AND ft.forum_id = f.forum_id)';
       
   542 		$read_tracking_select = ', ft.mark_time';
       
   543 	}
       
   544 	else
       
   545 	{
       
   546 		$read_tracking_join = $read_tracking_select = '';
       
   547 	}
       
   548 
       
   549 	$sql = "SELECT f.* $read_tracking_select
       
   550 		FROM " . FORUMS_TABLE . " f$read_tracking_join
       
   551 		WHERE " . $db->sql_in_set('f.forum_id', $forum_id);
       
   552 	$result = $db->sql_query($sql);
       
   553 
       
   554 	while ($row = $db->sql_fetchrow($result))
       
   555 	{
       
   556 		if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
       
   557 		{
       
   558 			continue;
       
   559 		}
       
   560 
       
   561 		if ($auth->acl_get('m_approve', $row['forum_id']))
       
   562 		{
       
   563 			$row['forum_topics'] = $row['forum_topics_real'];
       
   564 		}
       
   565 
       
   566 		$rowset[$row['forum_id']] = $row;
       
   567 	}
       
   568 	$db->sql_freeresult($result);
       
   569 
       
   570 	return $rowset;
       
   571 }
       
   572 
       
   573 /**
       
   574 * Get simple pm data
       
   575 */
       
   576 function get_pm_data($pm_ids)
       
   577 {
       
   578 	global $db, $auth, $config, $user;
       
   579 
       
   580 	$rowset = array();
       
   581 
       
   582 	if (!sizeof($pm_ids))
       
   583 	{
       
   584 		return array();
       
   585 	}
       
   586 
       
   587 	$sql_array = array(
       
   588 		'SELECT'	=> 'p.*, u.*',
       
   589 
       
   590 		'FROM'		=> array(
       
   591 			USERS_TABLE			=> 'u',
       
   592 			PRIVMSGS_TABLE		=> 'p',
       
   593 		),
       
   594 
       
   595 		'WHERE'		=> $db->sql_in_set('p.msg_id', $pm_ids) . '
       
   596 			AND u.user_id = p.author_id',
       
   597 	);
       
   598 
       
   599 	$sql = $db->sql_build_query('SELECT', $sql_array);
       
   600 	$result = $db->sql_query($sql);
       
   601 	unset($sql_array);
       
   602 
       
   603 	while ($row = $db->sql_fetchrow($result))
       
   604 	{
       
   605 		$rowset[$row['msg_id']] = $row;
       
   606 	}
       
   607 	$db->sql_freeresult($result);
       
   608 
       
   609 	return $rowset;
       
   610 }
       
   611 
       
   612 /**
       
   613 * sorting in mcp
       
   614 *
       
   615 * @param string $where_sql should either be WHERE (default if ommited) or end with AND or OR
       
   616 *
       
   617 * $mode reports and reports_closed: the $where parameters uses aliases p for posts table and r for report table
       
   618 * $mode unapproved_posts: the $where parameters uses aliases p for posts table and t for topic table
       
   619 */
       
   620 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')
       
   621 {
       
   622 	global $db, $user, $auth, $template;
       
   623 
       
   624 	$sort_days = request_var('st', 0);
       
   625 	$min_time = ($sort_days) ? time() - ($sort_days * 86400) : 0;
       
   626 
       
   627 	switch ($mode)
       
   628 	{
       
   629 		case 'viewforum':
       
   630 			$type = 'topics';
       
   631 			$default_key = 't';
       
   632 			$default_dir = 'd';
       
   633 
       
   634 			$sql = 'SELECT COUNT(topic_id) AS total
       
   635 				FROM ' . TOPICS_TABLE . "
       
   636 				$where_sql forum_id = $forum_id
       
   637 					AND topic_type NOT IN (" . POST_ANNOUNCE . ', ' . POST_GLOBAL . ")
       
   638 					AND topic_last_post_time >= $min_time";
       
   639 
       
   640 			if (!$auth->acl_get('m_approve', $forum_id))
       
   641 			{
       
   642 				$sql .= 'AND topic_approved = 1';
       
   643 			}
       
   644 		break;
       
   645 
       
   646 		case 'viewtopic':
       
   647 			$type = 'posts';
       
   648 			$default_key = 't';
       
   649 			$default_dir = 'a';
       
   650 
       
   651 			$sql = 'SELECT COUNT(post_id) AS total
       
   652 				FROM ' . POSTS_TABLE . "
       
   653 				$where_sql topic_id = $topic_id
       
   654 					AND post_time >= $min_time";
       
   655 
       
   656 			if (!$auth->acl_get('m_approve', $forum_id))
       
   657 			{
       
   658 				$sql .= 'AND post_approved = 1';
       
   659 			}
       
   660 		break;
       
   661 
       
   662 		case 'unapproved_posts':
       
   663 			$type = 'posts';
       
   664 			$default_key = 't';
       
   665 			$default_dir = 'd';
       
   666 			$where_sql .= ($topic_id) ? ' p.topic_id = ' . $topic_id . ' AND' : '';
       
   667 
       
   668 			$sql = 'SELECT COUNT(p.post_id) AS total
       
   669 				FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t
       
   670 				$where_sql " . $db->sql_in_set('p.forum_id', ($forum_id) ? array($forum_id) : array_intersect(get_forum_list('f_read'), get_forum_list('m_approve'))) . '
       
   671 					AND p.post_approved = 0
       
   672 					AND t.topic_id = p.topic_id
       
   673 					AND t.topic_first_post_id <> p.post_id';
       
   674 
       
   675 			if ($min_time)
       
   676 			{
       
   677 				$sql .= ' AND post_time >= ' . $min_time;
       
   678 			}
       
   679 		break;
       
   680 
       
   681 		case 'unapproved_topics':
       
   682 			$type = 'topics';
       
   683 			$default_key = 't';
       
   684 			$default_dir = 'd';
       
   685 
       
   686 			$sql = 'SELECT COUNT(topic_id) AS total
       
   687 				FROM ' . TOPICS_TABLE . "
       
   688 				$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'))) . '
       
   689 					AND topic_approved = 0';
       
   690 
       
   691 			if ($min_time)
       
   692 			{
       
   693 				$sql .= ' AND topic_time >= ' . $min_time;
       
   694 			}
       
   695 		break;
       
   696 
       
   697 		case 'pm_reports':
       
   698 		case 'pm_reports_closed':
       
   699 		case 'reports':
       
   700 		case 'reports_closed':
       
   701 			$pm = (strpos($mode, 'pm_') === 0) ? true : false;
       
   702 
       
   703 			$type = ($pm) ? 'pm_reports' : 'reports';
       
   704 			$default_key = 't';
       
   705 			$default_dir = 'd';
       
   706 			$limit_time_sql = ($min_time) ? "AND r.report_time >= $min_time" : '';
       
   707 
       
   708 			if ($topic_id)
       
   709 			{
       
   710 				$where_sql .= ' p.topic_id = ' . $topic_id . ' AND ';
       
   711 			}
       
   712 			else if ($forum_id)
       
   713 			{
       
   714 				$where_sql .= ' p.forum_id = ' . $forum_id . ' AND ';
       
   715 			}
       
   716 			else if (!$pm)
       
   717 			{
       
   718 				$where_sql .= ' ' . $db->sql_in_set('p.forum_id', get_forum_list(array('!f_read', '!m_report')), true, true) . ' AND ';
       
   719 			}
       
   720 
       
   721 			if ($mode == 'reports' || $mode == 'pm_reports')
       
   722 			{
       
   723 				$where_sql .= ' r.report_closed = 0 AND ';
       
   724 			}
       
   725 			else
       
   726 			{
       
   727 				$where_sql .= ' r.report_closed = 1 AND ';
       
   728 			}
       
   729 
       
   730 			if ($pm)
       
   731 			{
       
   732 				$sql = 'SELECT COUNT(r.report_id) AS total
       
   733 					FROM ' . REPORTS_TABLE . ' r, ' . PRIVMSGS_TABLE . " p
       
   734 					$where_sql r.post_id = 0
       
   735 						AND p.msg_id = r.pm_id
       
   736 						$limit_time_sql";
       
   737 			}
       
   738 			else
       
   739 			{
       
   740 				$sql = 'SELECT COUNT(r.report_id) AS total
       
   741 					FROM ' . REPORTS_TABLE . ' r, ' . POSTS_TABLE . " p
       
   742 					$where_sql r.pm_id = 0
       
   743 						AND p.post_id = r.post_id
       
   744 						$limit_time_sql";
       
   745 			}
       
   746 		break;
       
   747 
       
   748 		case 'viewlogs':
       
   749 			$type = 'logs';
       
   750 			$default_key = 't';
       
   751 			$default_dir = 'd';
       
   752 
       
   753 			$sql = 'SELECT COUNT(log_id) AS total
       
   754 				FROM ' . LOG_TABLE . "
       
   755 				$where_sql " . $db->sql_in_set('forum_id', ($forum_id) ? array($forum_id) : array_intersect(get_forum_list('f_read'), get_forum_list('m_'))) . '
       
   756 					AND log_time >= ' . $min_time . '
       
   757 					AND log_type = ' . LOG_MOD;
       
   758 		break;
       
   759 	}
       
   760 
       
   761 	$sort_key = request_var('sk', $default_key);
       
   762 	$sort_dir = request_var('sd', $default_dir);
       
   763 	$sort_dir_text = array('a' => $user->lang['ASCENDING'], 'd' => $user->lang['DESCENDING']);
       
   764 
       
   765 	switch ($type)
       
   766 	{
       
   767 		case 'topics':
       
   768 			$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']);
       
   769 			$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']);
       
   770 
       
   771 			$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');
       
   772 			$limit_time_sql = ($min_time) ? "AND t.topic_last_post_time >= $min_time" : '';
       
   773 		break;
       
   774 
       
   775 		case 'posts':
       
   776 			$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']);
       
   777 			$sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
       
   778 			$sort_by_sql = array('a' => 'u.username_clean', 't' => 'p.post_time', 's' => 'p.post_subject');
       
   779 			$limit_time_sql = ($min_time) ? "AND p.post_time >= $min_time" : '';
       
   780 		break;
       
   781 
       
   782 		case 'reports':
       
   783 			$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']);
       
   784 			$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']);
       
   785 			$sort_by_sql = array('a' => 'u.username_clean', 'r' => 'ru.username', 'p' => 'p.post_time', 't' => 'r.report_time', 's' => 'p.post_subject');
       
   786 		break;
       
   787 
       
   788 		case 'pm_reports':
       
   789 			$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']);
       
   790 			$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']);
       
   791 			$sort_by_sql = array('a' => 'u.username_clean', 'r' => 'ru.username', 'p' => 'p.message_time', 't' => 'r.report_time', 's' => 'p.message_subject');
       
   792 		break;
       
   793 
       
   794 		case 'logs':
       
   795 			$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']);
       
   796 			$sort_by_text = array('u' => $user->lang['SORT_USERNAME'], 't' => $user->lang['SORT_DATE'], 'i' => $user->lang['SORT_IP'], 'o' => $user->lang['SORT_ACTION']);
       
   797 
       
   798 			$sort_by_sql = array('u' => 'u.username_clean', 't' => 'l.log_time', 'i' => 'l.log_ip', 'o' => 'l.log_operation');
       
   799 			$limit_time_sql = ($min_time) ? "AND l.log_time >= $min_time" : '';
       
   800 		break;
       
   801 	}
       
   802 
       
   803 	if (!isset($sort_by_sql[$sort_key]))
       
   804 	{
       
   805 		$sort_key = $default_key;
       
   806 	}
       
   807 
       
   808 	$sort_order_sql = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
       
   809 
       
   810 	$s_limit_days = $s_sort_key = $s_sort_dir = $sort_url = '';
       
   811 	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);
       
   812 
       
   813 	$template->assign_vars(array(
       
   814 		'S_SELECT_SORT_DIR'		=> $s_sort_dir,
       
   815 		'S_SELECT_SORT_KEY'		=> $s_sort_key,
       
   816 		'S_SELECT_SORT_DAYS'	=> $s_limit_days)
       
   817 	);
       
   818 
       
   819 	if (($sort_days && $mode != 'viewlogs') || in_array($mode, array('reports', 'unapproved_topics', 'unapproved_posts')) || $where_sql != 'WHERE')
       
   820 	{
       
   821 		$result = $db->sql_query($sql);
       
   822 		$total = (int) $db->sql_fetchfield('total');
       
   823 		$db->sql_freeresult($result);
       
   824 	}
       
   825 	else
       
   826 	{
       
   827 		$total = -1;
       
   828 	}
       
   829 }
       
   830 
       
   831 /**
       
   832 * Validate ids
       
   833 *
       
   834 * @param	array	&$ids			The relevant ids to check
       
   835 * @param	string	$table			The table to find the ids in
       
   836 * @param	string	$sql_id			The ids relevant column name
       
   837 * @param	array	$acl_list		A list of permissions the user need to have
       
   838 * @param	mixed	$singe_forum	Limit to one forum id (int) or the first forum found (true)
       
   839 *
       
   840 * @return	mixed	False if no ids were able to be retrieved, true if at least one id left.
       
   841 *					Additionally, this value can be the forum_id assigned if $single_forum was set.
       
   842 *					Therefore checking the result for with !== false is the best method.
       
   843 */
       
   844 function check_ids(&$ids, $table, $sql_id, $acl_list = false, $single_forum = false)
       
   845 {
       
   846 	global $db, $auth;
       
   847 
       
   848 	if (!is_array($ids) || empty($ids))
       
   849 	{
       
   850 		return false;
       
   851 	}
       
   852 
       
   853 	$sql = "SELECT $sql_id, forum_id FROM $table
       
   854 		WHERE " . $db->sql_in_set($sql_id, $ids);
       
   855 	$result = $db->sql_query($sql);
       
   856 
       
   857 	$ids = array();
       
   858 	$forum_id = false;
       
   859 
       
   860 	while ($row = $db->sql_fetchrow($result))
       
   861 	{
       
   862 		if ($acl_list && $row['forum_id'] && !$auth->acl_gets($acl_list, $row['forum_id']))
       
   863 		{
       
   864 			continue;
       
   865 		}
       
   866 
       
   867 		if ($acl_list && !$row['forum_id'] && !$auth->acl_getf_global($acl_list))
       
   868 		{
       
   869 			continue;
       
   870 		}
       
   871 
       
   872 		// Limit forum? If not, just assign the id.
       
   873 		if ($single_forum === false)
       
   874 		{
       
   875 			$ids[] = $row[$sql_id];
       
   876 			continue;
       
   877 		}
       
   878 
       
   879 		// Limit forum to a specific forum id?
       
   880 		// This can get really tricky, because we do not want to create a failure on global topics. :)
       
   881 		if ($row['forum_id'])
       
   882 		{
       
   883 			if ($single_forum !== true && $row['forum_id'] == (int) $single_forum)
       
   884 			{
       
   885 				$forum_id = (int) $single_forum;
       
   886 			}
       
   887 			else if ($forum_id === false)
       
   888 			{
       
   889 				$forum_id = $row['forum_id'];
       
   890 			}
       
   891 
       
   892 			if ($row['forum_id'] == $forum_id)
       
   893 			{
       
   894 				$ids[] = $row[$sql_id];
       
   895 			}
       
   896 		}
       
   897 		else
       
   898 		{
       
   899 			// Always add a global topic
       
   900 			$ids[] = $row[$sql_id];
       
   901 		}
       
   902 	}
       
   903 	$db->sql_freeresult($result);
       
   904 
       
   905 	if (!sizeof($ids))
       
   906 	{
       
   907 		return false;
       
   908 	}
       
   909 
       
   910 	// If forum id is false and ids populated we may have only global announcements selected (returning 0 because of (int) $forum_id)
       
   911 
       
   912 	return ($single_forum === false) ? true : (int) $forum_id;
       
   913 }
       
   914 
       
   915 ?>