1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14/**
15* @ignore
16*/
17if (!defined('IN_PHPBB'))
18{
19	exit;
20}
21
22/**
23* Functions used to generate additional URL paramters
24*/
25function phpbb_module__url($mode, $module_row)
26{
27	return phpbb_extra_url();
28}
29
30function phpbb_module_notes_url($mode, $module_row)
31{
32	if ($mode == 'front')
33	{
34		return '';
35	}
36
37	global $user_id;
38	return ($user_id) ? "&amp;u=$user_id" : '';
39}
40
41function phpbb_module_warn_url($mode, $module_row)
42{
43	if ($mode == 'front' || $mode == 'list')
44	{
45		global $forum_id;
46
47		return ($forum_id) ? "&amp;f=$forum_id" : '';
48	}
49
50	if ($mode == 'warn_post')
51	{
52		global $forum_id, $post_id;
53
54		$url_extra = ($forum_id) ? "&amp;f=$forum_id" : '';
55		$url_extra .= ($post_id) ? "&amp;p=$post_id" : '';
56
57		return $url_extra;
58	}
59	else
60	{
61		global $user_id;
62
63		return ($user_id) ? "&amp;u=$user_id" : '';
64	}
65}
66
67function phpbb_module_main_url($mode, $module_row)
68{
69	return phpbb_extra_url();
70}
71
72function phpbb_module_logs_url($mode, $module_row)
73{
74	return phpbb_extra_url();
75}
76
77function phpbb_module_ban_url($mode, $module_row)
78{
79	return phpbb_extra_url();
80}
81
82function phpbb_module_queue_url($mode, $module_row)
83{
84	return phpbb_extra_url();
85}
86
87function phpbb_module_reports_url($mode, $module_row)
88{
89	return phpbb_extra_url();
90}
91
92function phpbb_extra_url()
93{
94	global $forum_id, $topic_id, $post_id, $report_id, $user_id;
95
96	$url_extra = '';
97	$url_extra .= ($forum_id) ? "&amp;f=$forum_id" : '';
98	$url_extra .= ($topic_id) ? "&amp;t=$topic_id" : '';
99	$url_extra .= ($post_id) ? "&amp;p=$post_id" : '';
100	$url_extra .= ($user_id) ? "&amp;u=$user_id" : '';
101	$url_extra .= ($report_id) ? "&amp;r=$report_id" : '';
102
103	return $url_extra;
104}
105
106/**
107* Get simple topic data
108*/
109function phpbb_get_topic_data($topic_ids, $acl_list = false, $read_tracking = false)
110{
111	global $auth, $db, $config, $user;
112	static $rowset = array();
113
114	$topics = array();
115
116	if (!count($topic_ids))
117	{
118		return array();
119	}
120
121	// cache might not contain read tracking info, so we can't use it if read
122	// tracking information is requested
123	if (!$read_tracking)
124	{
125		$cache_topic_ids = array_intersect($topic_ids, array_keys($rowset));
126		$topic_ids = array_diff($topic_ids, array_keys($rowset));
127	}
128	else
129	{
130		$cache_topic_ids = array();
131	}
132
133	if (count($topic_ids))
134	{
135		$sql_array = array(
136			'SELECT'	=> 't.*, f.*',
137
138			'FROM'		=> array(
139				TOPICS_TABLE	=> 't',
140			),
141
142			'LEFT_JOIN'	=> array(
143				array(
144					'FROM'	=> array(FORUMS_TABLE => 'f'),
145					'ON'	=> 'f.forum_id = t.forum_id'
146				)
147			),
148
149			'WHERE'		=> $db->sql_in_set('t.topic_id', $topic_ids)
150		);
151
152		if ($read_tracking && $config['load_db_lastread'])
153		{
154			$sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
155
156			$sql_array['LEFT_JOIN'][] = array(
157				'FROM'	=> array(TOPICS_TRACK_TABLE => 'tt'),
158				'ON'	=> 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
159			);
160
161			$sql_array['LEFT_JOIN'][] = array(
162				'FROM'	=> array(FORUMS_TRACK_TABLE => 'ft'),
163				'ON'	=> 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
164			);
165		}
166
167		$sql = $db->sql_build_query('SELECT', $sql_array);
168		$result = $db->sql_query($sql);
169
170		while ($row = $db->sql_fetchrow($result))
171		{
172			$rowset[$row['topic_id']] = $row;
173
174			if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
175			{
176				continue;
177			}
178
179			$topics[$row['topic_id']] = $row;
180		}
181		$db->sql_freeresult($result);
182	}
183
184	foreach ($cache_topic_ids as $id)
185	{
186		if (!$acl_list || $auth->acl_gets($acl_list, $rowset[$id]['forum_id']))
187		{
188			$topics[$id] = $rowset[$id];
189		}
190	}
191
192	return $topics;
193}
194
195/**
196* Get simple post data
197*/
198function phpbb_get_post_data($post_ids, $acl_list = false, $read_tracking = false)
199{
200	global $db, $auth, $config, $user, $phpbb_dispatcher, $phpbb_container;
201
202	$rowset = array();
203
204	if (!count($post_ids))
205	{
206		return array();
207	}
208
209	$sql_array = array(
210		'SELECT'	=> 'p.*, u.*, t.*, f.*',
211
212		'FROM'		=> array(
213			USERS_TABLE		=> 'u',
214			POSTS_TABLE		=> 'p',
215			TOPICS_TABLE	=> 't',
216		),
217
218		'LEFT_JOIN'	=> array(
219			array(
220				'FROM'	=> array(FORUMS_TABLE => 'f'),
221				'ON'	=> 'f.forum_id = t.forum_id'
222			)
223		),
224
225		'WHERE'		=> $db->sql_in_set('p.post_id', $post_ids) . '
226			AND u.user_id = p.poster_id
227			AND t.topic_id = p.topic_id',
228	);
229
230	if ($read_tracking && $config['load_db_lastread'])
231	{
232		$sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
233
234		$sql_array['LEFT_JOIN'][] = array(
235			'FROM'	=> array(TOPICS_TRACK_TABLE => 'tt'),
236			'ON'	=> 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
237		);
238
239		$sql_array['LEFT_JOIN'][] = array(
240			'FROM'	=> array(FORUMS_TRACK_TABLE => 'ft'),
241			'ON'	=> 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
242		);
243	}
244
245	$sql = $db->sql_build_query('SELECT', $sql_array);
246	$result = $db->sql_query($sql);
247	unset($sql_array);
248
249	$phpbb_content_visibility = $phpbb_container->get('content.visibility');
250
251	while ($row = $db->sql_fetchrow($result))
252	{
253		if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
254		{
255			continue;
256		}
257
258		if (!$phpbb_content_visibility->is_visible('post', $row['forum_id'], $row))
259		{
260			// Moderators without the permission to approve post should at least not see them. ;)
261			continue;
262		}
263
264		$rowset[$row['post_id']] = $row;
265	}
266	$db->sql_freeresult($result);
267
268	/**
269	* This event allows you to modify post data displayed in the MCP
270	*
271	* @event core.mcp_get_post_data_after
272	* @var array	post_ids		Array with post ids that have been fetched
273	* @var mixed	acl_list		Either false or an array with permission strings to check
274	* @var bool		read_tracking	Whether or not to take last mark read time into account
275	* @var array	rowset			The array of posts to be returned
276	* @since 3.2.10-RC1
277	* @since 3.3.1-RC1
278	*/
279	$vars = [
280		'post_ids',
281		'acl_list',
282		'read_tracking',
283		'rowset',
284	];
285	extract($phpbb_dispatcher->trigger_event('core.mcp_get_post_data_after', compact($vars)));
286
287	return $rowset;
288}
289
290/**
291* Get simple forum data
292*/
293function phpbb_get_forum_data($forum_id, $acl_list = 'f_list', $read_tracking = false)
294{
295	global $auth, $db, $user, $config, $phpbb_container;
296
297	$rowset = array();
298
299	if (!is_array($forum_id))
300	{
301		$forum_id = array($forum_id);
302	}
303
304	if (!count($forum_id))
305	{
306		return array();
307	}
308
309	if ($read_tracking && $config['load_db_lastread'])
310	{
311		$read_tracking_join = ' LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . '
312			AND ft.forum_id = f.forum_id)';
313		$read_tracking_select = ', ft.mark_time';
314	}
315	else
316	{
317		$read_tracking_join = $read_tracking_select = '';
318	}
319
320	$sql = "SELECT f.* $read_tracking_select
321		FROM " . FORUMS_TABLE . " f$read_tracking_join
322		WHERE " . $db->sql_in_set('f.forum_id', $forum_id);
323	$result = $db->sql_query($sql);
324
325	/* @var $phpbb_content_visibility \phpbb\content_visibility */
326	$phpbb_content_visibility = $phpbb_container->get('content.visibility');
327
328	while ($row = $db->sql_fetchrow($result))
329	{
330		if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
331		{
332			continue;
333		}
334
335		$row['forum_topics_approved'] = $phpbb_content_visibility->get_count('forum_topics', $row, $row['forum_id']);
336
337		$rowset[$row['forum_id']] = $row;
338	}
339	$db->sql_freeresult($result);
340
341	return $rowset;
342}
343
344/**
345* Get simple pm data
346*/
347function phpbb_get_pm_data($pm_ids)
348{
349	global $db;
350
351	$rowset = array();
352
353	if (!count($pm_ids))
354	{
355		return array();
356	}
357
358	$sql_array = array(
359		'SELECT'	=> 'p.*, u.*',
360
361		'FROM'		=> array(
362			USERS_TABLE			=> 'u',
363			PRIVMSGS_TABLE		=> 'p',
364		),
365
366		'WHERE'		=> $db->sql_in_set('p.msg_id', $pm_ids) . '
367			AND u.user_id = p.author_id',
368	);
369
370	$sql = $db->sql_build_query('SELECT', $sql_array);
371	$result = $db->sql_query($sql);
372	unset($sql_array);
373
374	while ($row = $db->sql_fetchrow($result))
375	{
376		$rowset[$row['msg_id']] = $row;
377	}
378	$db->sql_freeresult($result);
379
380	return $rowset;
381}
382
383/**
384* sorting in mcp
385*
386* @param string $where_sql should either be WHERE (default if ommited) or end with AND or OR
387*
388* $mode reports and reports_closed: the $where parameters uses aliases p for posts table and r for report table
389* $mode unapproved_posts: the $where parameters uses aliases p for posts table and t for topic table
390*/
391function phpbb_mcp_sorting($mode, &$sort_days_val, &$sort_key_val, &$sort_dir_val, &$sort_by_sql_ary, &$sort_order_sql, &$total_val, $forum_id = 0, $topic_id = 0, $where_sql = 'WHERE')
392{
393	global $db, $user, $auth, $template, $request, $phpbb_dispatcher;
394
395	$sort_days_val = $request->variable('st', 0);
396	$min_time = ($sort_days_val) ? time() - ($sort_days_val * 86400) : 0;
397
398	switch ($mode)
399	{
400		case 'viewforum':
401			$type = 'topics';
402			$default_key = 't';
403			$default_dir = 'd';
404
405			$sql = 'SELECT COUNT(topic_id) AS total
406				FROM ' . TOPICS_TABLE . "
407				$where_sql forum_id = $forum_id
408					AND topic_type NOT IN (" . POST_ANNOUNCE . ', ' . POST_GLOBAL . ")
409					AND topic_last_post_time >= $min_time";
410
411			if (!$auth->acl_get('m_approve', $forum_id))
412			{
413				$sql .= ' AND topic_visibility = ' . ITEM_APPROVED;
414			}
415			break;
416
417		case 'viewtopic':
418			$type = 'posts';
419			$default_key = 't';
420			$default_dir = 'a';
421
422			$sql = 'SELECT COUNT(post_id) AS total
423				FROM ' . POSTS_TABLE . "
424				$where_sql topic_id = $topic_id
425					AND post_time >= $min_time";
426
427			if (!$auth->acl_get('m_approve', $forum_id))
428			{
429				$sql .= ' AND post_visibility = ' . ITEM_APPROVED;
430			}
431			break;
432
433		case 'unapproved_posts':
434		case 'deleted_posts':
435			$visibility_const = ($mode == 'unapproved_posts') ? array(ITEM_UNAPPROVED, ITEM_REAPPROVE) : ITEM_DELETED;
436			$type = 'posts';
437			$default_key = 't';
438			$default_dir = 'd';
439			$where_sql .= ($topic_id) ? ' p.topic_id = ' . $topic_id . ' AND' : '';
440
441			$sql = 'SELECT COUNT(p.post_id) AS total
442				FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t
443				$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'))) . '
444					AND ' . $db->sql_in_set('p.post_visibility', $visibility_const) .'
445					AND t.topic_id = p.topic_id
446					AND t.topic_visibility <> p.post_visibility';
447
448			if ($min_time)
449			{
450				$sql .= ' AND post_time >= ' . $min_time;
451			}
452			break;
453
454		case 'unapproved_topics':
455		case 'deleted_topics':
456			$visibility_const = ($mode == 'unapproved_topics') ? array(ITEM_UNAPPROVED, ITEM_REAPPROVE) : ITEM_DELETED;
457			$type = 'topics';
458			$default_key = 't';
459			$default_dir = 'd';
460
461			$sql = 'SELECT COUNT(topic_id) AS total
462				FROM ' . TOPICS_TABLE . "
463				$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'))) . '
464					AND ' . $db->sql_in_set('topic_visibility', $visibility_const);
465
466			if ($min_time)
467			{
468				$sql .= ' AND topic_time >= ' . $min_time;
469			}
470			break;
471
472		case 'pm_reports':
473		case 'pm_reports_closed':
474		case 'reports':
475		case 'reports_closed':
476			$pm = (strpos($mode, 'pm_') === 0) ? true : false;
477
478			$type = ($pm) ? 'pm_reports' : 'reports';
479			$default_key = 't';
480			$default_dir = 'd';
481			$limit_time_sql = ($min_time) ? "AND r.report_time >= $min_time" : '';
482
483			if ($topic_id)
484			{
485				$where_sql .= ' p.topic_id = ' . $topic_id . ' AND ';
486			}
487			else if ($forum_id)
488			{
489				$where_sql .= ' p.forum_id = ' . $forum_id . ' AND ';
490			}
491			else if (!$pm)
492			{
493				$where_sql .= ' ' . $db->sql_in_set('p.forum_id', get_forum_list(array('!f_read', '!m_report')), true, true) . ' AND ';
494			}
495
496			if ($mode == 'reports' || $mode == 'pm_reports')
497			{
498				$where_sql .= ' r.report_closed = 0 AND ';
499			}
500			else
501			{
502				$where_sql .= ' r.report_closed = 1 AND ';
503			}
504
505			if ($pm)
506			{
507				$sql = 'SELECT COUNT(r.report_id) AS total
508					FROM ' . REPORTS_TABLE . ' r, ' . PRIVMSGS_TABLE . " p
509					$where_sql r.post_id = 0
510						AND p.msg_id = r.pm_id
511						$limit_time_sql";
512			}
513			else
514			{
515				$sql = 'SELECT COUNT(r.report_id) AS total
516					FROM ' . REPORTS_TABLE . ' r, ' . POSTS_TABLE . " p
517					$where_sql r.pm_id = 0
518						AND p.post_id = r.post_id
519						$limit_time_sql";
520			}
521			break;
522
523		case 'viewlogs':
524			$type = 'logs';
525			$default_key = 't';
526			$default_dir = 'd';
527
528			$sql = 'SELECT COUNT(log_id) AS total
529				FROM ' . LOG_TABLE . "
530				$where_sql " . $db->sql_in_set('forum_id', ($forum_id) ? array($forum_id) : array_intersect(get_forum_list('f_read'), get_forum_list('m_'))) . '
531					AND log_time >= ' . $min_time . '
532					AND log_type = ' . LOG_MOD;
533			break;
534	}
535
536	$sort_key_val = $request->variable('sk', $default_key);
537	$sort_dir_val = $request->variable('sd', $default_dir);
538	$sort_dir_text = array('a' => $user->lang['ASCENDING'], 'd' => $user->lang['DESCENDING']);
539
540	switch ($type)
541	{
542		case 'topics':
543			$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']);
544			$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']);
545
546			$sort_by_sql_ary = array('a' => 't.topic_first_poster_name', 't' => array('t.topic_last_post_time', 't.topic_last_post_id'), 'tt' => 't.topic_time', 'r' => (($auth->acl_get('m_approve', $forum_id)) ? 't.topic_posts_approved + t.topic_posts_unapproved + t.topic_posts_softdeleted' : 't.topic_posts_approved'), 's' => 't.topic_title', 'v' => 't.topic_views');
547			$limit_time_sql = ($min_time) ? "AND t.topic_last_post_time >= $min_time" : '';
548			break;
549
550		case 'posts':
551			$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']);
552			$sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
553			$sort_by_sql_ary = array('a' => 'u.username_clean', 't' => array('p.post_time', 'p.post_id'), 's' => 'p.post_subject');
554			$limit_time_sql = ($min_time) ? "AND p.post_time >= $min_time" : '';
555			break;
556
557		case 'reports':
558			$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']);
559			$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']);
560			$sort_by_sql_ary = array('a' => 'u.username_clean', 'r' => 'ru.username', 'p' => array('p.post_time', 'p.post_id'), 't' => 'r.report_time', 's' => 'p.post_subject');
561			break;
562
563		case 'pm_reports':
564			$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']);
565			$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']);
566			$sort_by_sql_ary = array('a' => 'u.username_clean', 'r' => 'ru.username', 'p' => 'p.message_time', 't' => 'r.report_time', 's' => 'p.message_subject');
567			break;
568
569		case 'logs':
570			$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']);
571			$sort_by_text = array('u' => $user->lang['SORT_USERNAME'], 't' => $user->lang['SORT_DATE'], 'i' => $user->lang['SORT_IP'], 'o' => $user->lang['SORT_ACTION']);
572
573			$sort_by_sql_ary = array('u' => 'u.username_clean', 't' => 'l.log_time', 'i' => 'l.log_ip', 'o' => 'l.log_operation');
574			$limit_time_sql = ($min_time) ? "AND l.log_time >= $min_time" : '';
575			break;
576	}
577
578	// Default total to -1 to allow editing by the event
579	$total_val = -1;
580
581	$sort_by_sql = $sort_by_sql_ary;
582	$sort_days = $sort_days_val;
583	$sort_dir = $sort_dir_val;
584	$sort_key = $sort_key_val;
585	$total = $total_val;
586	/**
587	* This event allows you to control the SQL query used to get the total number
588	* of reports the user can access.
589	*
590	* This total is used for the pagination and for displaying the total number
591	* of reports to the user
592	*
593	*
594	* @event core.mcp_sorting_query_before
595	* @var	string	sql					The current SQL search string
596	* @var	string	mode				An id related to the module(s) the user is viewing
597	* @var	string	type				Which kind of information is this being used for displaying. Posts, topics, etc...
598	* @var	int		forum_id			The forum id of the posts the user is trying to access, if not 0
599	* @var	int		topic_id			The topic id of the posts the user is trying to access, if not 0
600	* @var	int		sort_days			The max age of the oldest report to be shown, in days
601	* @var	string	sort_key			The way the user has decided to sort the data.
602	*									The valid values must be in the keys of the sort_by_* variables
603	* @var	string	sort_dir			Either 'd' for "DESC" or 'a' for 'ASC' in the SQL query
604	* @var	int		limit_days			The possible max ages of the oldest report for the user to choose, in days.
605	* @var	array	sort_by_sql			SQL text (values) for the possible names of the ways of sorting data (keys).
606	* @var	array	sort_by_text		Language text (values) for the possible names of the ways of sorting data (keys).
607	* @var	int		min_time			Integer with the minimum post time that the user is searching for
608	* @var	int		limit_time_sql		Time limiting options used in the SQL query.
609	* @var	int		total				The total number of reports that exist. Only set if you want to override the result
610	* @var	string	where_sql			Extra information included in the WHERE clause. It must end with "WHERE" or "AND" or "OR".
611	*									Set to "WHERE" and set total above -1 to override the total value
612	* @since 3.1.4-RC1
613	*/
614	$vars = array(
615		'sql',
616		'mode',
617		'type',
618		'forum_id',
619		'topic_id',
620		'sort_days',
621		'sort_key',
622		'sort_dir',
623		'limit_days',
624		'sort_by_sql',
625		'sort_by_text',
626		'min_time',
627		'limit_time_sql',
628		'total',
629		'where_sql',
630	);
631	extract($phpbb_dispatcher->trigger_event('core.mcp_sorting_query_before', compact($vars)));
632	$sort_by_sql_ary = $sort_by_sql;
633	$sort_days_val = $sort_days;
634	$sort_key_val = $sort_key;
635	$sort_dir_val = $sort_dir;
636	$total_val = $total;
637	unset($sort_by_sql);
638	unset($sort_days);
639	unset($sort_key);
640	unset($sort_dir);
641	unset($total);
642
643	if (!isset($sort_by_sql_ary[$sort_key_val]))
644	{
645		$sort_key_val = $default_key;
646	}
647
648	$direction = ($sort_dir_val == 'd') ? 'DESC' : 'ASC';
649
650	if (is_array($sort_by_sql_ary[$sort_key_val]))
651	{
652		$sort_order_sql = implode(' ' . $direction . ', ', $sort_by_sql_ary[$sort_key_val]) . ' ' . $direction;
653	}
654	else
655	{
656		$sort_order_sql = $sort_by_sql_ary[$sort_key_val] . ' ' . $direction;
657	}
658
659	$s_limit_days = $s_sort_key = $s_sort_dir = $sort_url = '';
660	gen_sort_selects($limit_days, $sort_by_text, $sort_days_val, $sort_key_val, $sort_dir_val, $s_limit_days, $s_sort_key, $s_sort_dir, $sort_url);
661
662	$template->assign_vars(array(
663			'S_SELECT_SORT_DIR'		=> $s_sort_dir,
664			'S_SELECT_SORT_KEY'		=> $s_sort_key,
665			'S_SELECT_SORT_DAYS'	=> $s_limit_days)
666	);
667
668	if (($sort_days_val && $mode != 'viewlogs') || in_array($mode, array('reports', 'unapproved_topics', 'unapproved_posts', 'deleted_topics', 'deleted_posts')) || $where_sql != 'WHERE')
669	{
670		$result = $db->sql_query($sql);
671		$total_val = (int) $db->sql_fetchfield('total');
672		$db->sql_freeresult($result);
673	}
674	else if ($total_val < -1)
675	{
676		$total_val = -1;
677	}
678}
679
680/**
681* Validate ids
682*
683* @param	array	&$ids			The relevant ids to check
684* @param	string	$table			The table to find the ids in
685* @param	string	$sql_id			The ids relevant column name
686* @param	array	$acl_list		A list of permissions the user need to have
687* @param	mixed	$singe_forum	Limit to one forum id (int) or the first forum found (true)
688*
689* @return	mixed	False if no ids were able to be retrieved, true if at least one id left.
690*					Additionally, this value can be the forum_id assigned if $single_forum was set.
691*					Therefore checking the result for with !== false is the best method.
692*/
693function phpbb_check_ids(&$ids, $table, $sql_id, $acl_list = false, $single_forum = false)
694{
695	global $db, $auth;
696
697	if (!is_array($ids) || empty($ids))
698	{
699		return false;
700	}
701
702	$sql = "SELECT $sql_id, forum_id FROM $table
703		WHERE " . $db->sql_in_set($sql_id, $ids);
704	$result = $db->sql_query($sql);
705
706	$ids = array();
707	$forum_id = false;
708
709	while ($row = $db->sql_fetchrow($result))
710	{
711		if ($acl_list && $row['forum_id'] && !$auth->acl_gets($acl_list, $row['forum_id']))
712		{
713			continue;
714		}
715
716		if ($acl_list && !$row['forum_id'] && !$auth->acl_getf_global($acl_list))
717		{
718			continue;
719		}
720
721		// Limit forum? If not, just assign the id.
722		if ($single_forum === false)
723		{
724			$ids[] = $row[$sql_id];
725			continue;
726		}
727
728		// Limit forum to a specific forum id?
729		// This can get really tricky, because we do not want to create a failure on global topics. :)
730		if ($row['forum_id'])
731		{
732			if ($single_forum !== true && $row['forum_id'] == (int) $single_forum)
733			{
734				$forum_id = (int) $single_forum;
735			}
736			else if ($forum_id === false)
737			{
738				$forum_id = $row['forum_id'];
739			}
740
741			if ($row['forum_id'] == $forum_id)
742			{
743				$ids[] = $row[$sql_id];
744			}
745		}
746		else
747		{
748			// Always add a global topic
749			$ids[] = $row[$sql_id];
750		}
751	}
752	$db->sql_freeresult($result);
753
754	if (!count($ids))
755	{
756		return false;
757	}
758
759	// If forum id is false and ids populated we may have only global announcements selected (returning 0 because of (int) $forum_id)
760
761	return ($single_forum === false) ? true : (int) $forum_id;
762}
763