Blame Identity/Webenv/phpBB/3.0.4/includes/functions_display.php

ef5584
ef5584
/**
ef5584
*
ef5584
* @package phpBB3
ef5584
* @version $Id: functions_display.php 9082 2008-11-22 20:26:09Z acydburn $
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
if (!defined('IN_PHPBB'))
ef5584
{
ef5584
	exit;
ef5584
}
ef5584
ef5584
/**
ef5584
* Display Forums
ef5584
*/
ef5584
function display_forums($root_data = '', $display_moderators = true, $return_moderators = false)
ef5584
{
ef5584
	global $db, $auth, $user, $template;
ef5584
	global $phpbb_root_path, $phpEx, $config;
ef5584
ef5584
	$forum_rows = $subforums = $forum_ids = $forum_ids_moderator = $forum_moderators = $active_forum_ary = array();
ef5584
	$parent_id = $visible_forums = 0;
ef5584
	$sql_from = '';
ef5584
ef5584
	// Mark forums read?
ef5584
	$mark_read = request_var('mark', '');
ef5584
ef5584
	if ($mark_read == 'all')
ef5584
	{
ef5584
		$mark_read = '';
ef5584
	}
ef5584
ef5584
	if (!$root_data)
ef5584
	{
ef5584
		if ($mark_read == 'forums')
ef5584
		{
ef5584
			$mark_read = 'all';
ef5584
		}
ef5584
ef5584
		$root_data = array('forum_id' => 0);
ef5584
		$sql_where = '';
ef5584
	}
ef5584
	else
ef5584
	{
ef5584
		$sql_where = 'left_id > ' . $root_data['left_id'] . ' AND left_id < ' . $root_data['right_id'];
ef5584
	}
ef5584
ef5584
	// Display list of active topics for this category?
ef5584
	$show_active = (isset($root_data['forum_flags']) && ($root_data['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS)) ? true : false;
ef5584
ef5584
	$sql_array = array(
ef5584
		'SELECT'	=> 'f.*',
ef5584
		'FROM'		=> array(
ef5584
			FORUMS_TABLE		=> 'f'
ef5584
		),
ef5584
		'LEFT_JOIN'	=> array(),
ef5584
	);
ef5584
ef5584
	if ($config['load_db_lastread'] && $user->data['is_registered'])
ef5584
	{
ef5584
		$sql_array['LEFT_JOIN'][] = array('FROM' => array(FORUMS_TRACK_TABLE => 'ft'), 'ON' => 'ft.user_id = ' . $user->data['user_id'] . ' AND ft.forum_id = f.forum_id');
ef5584
		$sql_array['SELECT'] .= ', ft.mark_time';
ef5584
	}
ef5584
	else if ($config['load_anon_lastread'] || $user->data['is_registered'])
ef5584
	{
ef5584
		$tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : '';
ef5584
		$tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array();
ef5584
ef5584
		if (!$user->data['is_registered'])
ef5584
		{
ef5584
			$user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
ef5584
		}
ef5584
	}
ef5584
ef5584
	if ($show_active)
ef5584
	{
ef5584
		$sql_array['LEFT_JOIN'][] = array(
ef5584
			'FROM'	=> array(FORUMS_ACCESS_TABLE => 'fa'),
ef5584
			'ON'	=> "fa.forum_id = f.forum_id AND fa.session_id = '" . $db->sql_escape($user->session_id) . "'"
ef5584
		);
ef5584
ef5584
		$sql_array['SELECT'] .= ', fa.user_id';
ef5584
	}
ef5584
ef5584
	$sql = $db->sql_build_query('SELECT', array(
ef5584
		'SELECT'	=> $sql_array['SELECT'],
ef5584
		'FROM'		=> $sql_array['FROM'],
ef5584
		'LEFT_JOIN'	=> $sql_array['LEFT_JOIN'],
ef5584
ef5584
		'WHERE'		=> $sql_where,
ef5584
ef5584
		'ORDER_BY'	=> 'f.left_id',
ef5584
	));
ef5584
ef5584
	$result = $db->sql_query($sql);
ef5584
ef5584
	$forum_tracking_info = array();
ef5584
	$branch_root_id = $root_data['forum_id'];
ef5584
	while ($row = $db->sql_fetchrow($result))
ef5584
	{
ef5584
		$forum_id = $row['forum_id'];
ef5584
ef5584
		// Mark forums read?
ef5584
		if ($mark_read == 'forums' || $mark_read == 'all')
ef5584
		{
ef5584
			if ($auth->acl_get('f_list', $forum_id))
ef5584
			{
ef5584
				$forum_ids[] = $forum_id;
ef5584
				continue;
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Category with no members
ef5584
		if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
ef5584
		{
ef5584
			continue;
ef5584
		}
ef5584
ef5584
		// Skip branch
ef5584
		if (isset($right_id))
ef5584
		{
ef5584
			if ($row['left_id'] < $right_id)
ef5584
			{
ef5584
				continue;
ef5584
			}
ef5584
			unset($right_id);
ef5584
		}
ef5584
ef5584
		if (!$auth->acl_get('f_list', $forum_id))
ef5584
		{
ef5584
			// if the user does not have permissions to list this forum, skip everything until next branch
ef5584
			$right_id = $row['right_id'];
ef5584
			continue;
ef5584
		}
ef5584
ef5584
		$forum_ids[] = $forum_id;
ef5584
ef5584
		if ($config['load_db_lastread'] && $user->data['is_registered'])
ef5584
		{
ef5584
			$forum_tracking_info[$forum_id] = (!empty($row['mark_time'])) ? $row['mark_time'] : $user->data['user_lastmark'];
ef5584
		}
ef5584
		else if ($config['load_anon_lastread'] || $user->data['is_registered'])
ef5584
		{
ef5584
			if (!$user->data['is_registered'])
ef5584
			{
ef5584
				$user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
ef5584
			}
ef5584
			$forum_tracking_info[$forum_id] = (isset($tracking_topics['f'][$forum_id])) ? (int) (base_convert($tracking_topics['f'][$forum_id], 36, 10) + $config['board_startdate']) : $user->data['user_lastmark'];
ef5584
		}
ef5584
ef5584
		$row['forum_topics'] = ($auth->acl_get('m_approve', $forum_id)) ? $row['forum_topics_real'] : $row['forum_topics'];
ef5584
ef5584
		// Display active topics from this forum?
ef5584
		if ($show_active && $row['forum_type'] == FORUM_POST && $auth->acl_get('f_read', $forum_id) && ($row['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS))
ef5584
		{
ef5584
			if (!isset($active_forum_ary['forum_topics']))
ef5584
			{
ef5584
				$active_forum_ary['forum_topics'] = 0;
ef5584
			}
ef5584
ef5584
			if (!isset($active_forum_ary['forum_posts']))
ef5584
			{
ef5584
				$active_forum_ary['forum_posts'] = 0;
ef5584
			}
ef5584
ef5584
			$active_forum_ary['forum_id'][]		= $forum_id;
ef5584
			$active_forum_ary['enable_icons'][]	= $row['enable_icons'];
ef5584
			$active_forum_ary['forum_topics']	+= $row['forum_topics'];
ef5584
			$active_forum_ary['forum_posts']	+= $row['forum_posts'];
ef5584
ef5584
			// If this is a passworded forum we do not show active topics from it if the user is not authorised to view it...
ef5584
			if ($row['forum_password'] && $row['user_id'] != $user->data['user_id'])
ef5584
			{
ef5584
				$active_forum_ary['exclude_forum_id'][] = $forum_id;
ef5584
			}
ef5584
		}
ef5584
ef5584
		//
ef5584
		if ($row['parent_id'] == $root_data['forum_id'] || $row['parent_id'] == $branch_root_id)
ef5584
		{
ef5584
			if ($row['forum_type'] != FORUM_CAT)
ef5584
			{
ef5584
				$forum_ids_moderator[] = (int) $forum_id;
ef5584
			}
ef5584
ef5584
			// Direct child of current branch
ef5584
			$parent_id = $forum_id;
ef5584
			$forum_rows[$forum_id] = $row;
ef5584
ef5584
			if ($row['forum_type'] == FORUM_CAT && $row['parent_id'] == $root_data['forum_id'])
ef5584
			{
ef5584
				$branch_root_id = $forum_id;
ef5584
			}
ef5584
			$forum_rows[$parent_id]['forum_id_last_post'] = $row['forum_id'];
ef5584
			$forum_rows[$parent_id]['orig_forum_last_post_time'] = $row['forum_last_post_time'];
ef5584
		}
ef5584
		else if ($row['forum_type'] != FORUM_CAT)
ef5584
		{
ef5584
			$subforums[$parent_id][$forum_id]['display'] = ($row['display_on_index']) ? true : false;
ef5584
			$subforums[$parent_id][$forum_id]['name'] = $row['forum_name'];
ef5584
			$subforums[$parent_id][$forum_id]['orig_forum_last_post_time'] = $row['forum_last_post_time'];
ef5584
			$subforums[$parent_id][$forum_id]['children'] = array();
ef5584
ef5584
			if (isset($subforums[$parent_id][$row['parent_id']]) && !$row['display_on_index'])
ef5584
			{
ef5584
				$subforums[$parent_id][$row['parent_id']]['children'][] = $forum_id;
ef5584
			}
ef5584
ef5584
			$forum_rows[$parent_id]['forum_topics'] += $row['forum_topics'];
ef5584
ef5584
			// Do not list redirects in LINK Forums as Posts.
ef5584
			if ($row['forum_type'] != FORUM_LINK)
ef5584
			{
ef5584
				$forum_rows[$parent_id]['forum_posts'] += $row['forum_posts'];
ef5584
			}
ef5584
ef5584
			if ($row['forum_last_post_time'] > $forum_rows[$parent_id]['forum_last_post_time'])
ef5584
			{
ef5584
				$forum_rows[$parent_id]['forum_last_post_id'] = $row['forum_last_post_id'];
ef5584
				$forum_rows[$parent_id]['forum_last_post_subject'] = $row['forum_last_post_subject'];
ef5584
				$forum_rows[$parent_id]['forum_last_post_time'] = $row['forum_last_post_time'];
ef5584
				$forum_rows[$parent_id]['forum_last_poster_id'] = $row['forum_last_poster_id'];
ef5584
				$forum_rows[$parent_id]['forum_last_poster_name'] = $row['forum_last_poster_name'];
ef5584
				$forum_rows[$parent_id]['forum_last_poster_colour'] = $row['forum_last_poster_colour'];
ef5584
				$forum_rows[$parent_id]['forum_id_last_post'] = $forum_id;
ef5584
			}
ef5584
		}
ef5584
	}
ef5584
	$db->sql_freeresult($result);
ef5584
ef5584
	// Handle marking posts
ef5584
	if ($mark_read == 'forums' || $mark_read == 'all')
ef5584
	{
ef5584
		$redirect = build_url('mark', 'hash');
ef5584
		$token = request_var('hash', '');
ef5584
		if (check_link_hash($token, 'global'))
ef5584
		{
ef5584
			if ($mark_read == 'all')
ef5584
			{
ef5584
				markread('all');
ef5584
				$message = sprintf($user->lang['RETURN_INDEX'], '', '');
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				markread('topics', $forum_ids);
ef5584
				$message = sprintf($user->lang['RETURN_FORUM'], '', '');
ef5584
			}
ef5584
			meta_refresh(3, $redirect);
ef5584
			trigger_error($user->lang['FORUMS_MARKED'] . '

' . $message);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$message = sprintf($user->lang['RETURN_PAGE'], '', '');
ef5584
			meta_refresh(3, $redirect);
ef5584
			trigger_error($message);
ef5584
		}
ef5584
ef5584
	}
ef5584
ef5584
	// Grab moderators ... if necessary
ef5584
	if ($display_moderators)
ef5584
	{
ef5584
		if ($return_moderators)
ef5584
		{
ef5584
			$forum_ids_moderator[] = $root_data['forum_id'];
ef5584
		}
ef5584
		get_moderators($forum_moderators, $forum_ids_moderator);
ef5584
	}
ef5584
ef5584
	// Used to tell whatever we have to create a dummy category or not.
ef5584
	$last_catless = true;
ef5584
	foreach ($forum_rows as $row)
ef5584
	{
ef5584
		// Empty category
ef5584
		if ($row['parent_id'] == $root_data['forum_id'] && $row['forum_type'] == FORUM_CAT)
ef5584
		{
ef5584
			$template->assign_block_vars('forumrow', array(
ef5584
				'S_IS_CAT'				=> true,
ef5584
				'FORUM_ID'				=> $row['forum_id'],
ef5584
				'FORUM_NAME'			=> $row['forum_name'],
ef5584
				'FORUM_DESC'			=> generate_text_for_display($row['forum_desc'], $row['forum_desc_uid'], $row['forum_desc_bitfield'], $row['forum_desc_options']),
ef5584
				'FORUM_FOLDER_IMG'		=> '',
ef5584
				'FORUM_FOLDER_IMG_SRC'	=> '',
ef5584
				'FORUM_IMAGE'			=> ($row['forum_image']) ? '' . $user->lang['FORUM_CAT'] . '' : '',
ef5584
				'FORUM_IMAGE_SRC'		=> ($row['forum_image']) ? $phpbb_root_path . $row['forum_image'] : '',
ef5584
				'U_VIEWFORUM'			=> append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']))
ef5584
			);
ef5584
ef5584
			continue;
ef5584
		}
ef5584
ef5584
		$visible_forums++;
ef5584
		$forum_id = $row['forum_id'];
ef5584
ef5584
		$forum_unread = (isset($forum_tracking_info[$forum_id]) && $row['orig_forum_last_post_time'] > $forum_tracking_info[$forum_id]) ? true : false;
ef5584
ef5584
		$folder_image = $folder_alt = $l_subforums = '';
ef5584
		$subforums_list = array();
ef5584
ef5584
		// Generate list of subforums if we need to
ef5584
		if (isset($subforums[$forum_id]))
ef5584
		{
ef5584
			foreach ($subforums[$forum_id] as $subforum_id => $subforum_row)
ef5584
			{
ef5584
				$subforum_unread = (isset($forum_tracking_info[$subforum_id]) && $subforum_row['orig_forum_last_post_time'] > $forum_tracking_info[$subforum_id]) ? true : false;
ef5584
ef5584
				if (!$subforum_unread && !empty($subforum_row['children']))
ef5584
				{
ef5584
					foreach ($subforum_row['children'] as $child_id)
ef5584
					{
ef5584
						if (isset($forum_tracking_info[$child_id]) && $subforums[$forum_id][$child_id]['orig_forum_last_post_time'] > $forum_tracking_info[$child_id])
ef5584
						{
ef5584
							// Once we found an unread child forum, we can drop out of this loop
ef5584
							$subforum_unread = true;
ef5584
							break;
ef5584
						}
ef5584
					}
ef5584
				}
ef5584
ef5584
				if ($subforum_row['display'] && $subforum_row['name'])
ef5584
				{
ef5584
					$subforums_list[] = array(
ef5584
						'link'		=> append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $subforum_id),
ef5584
						'name'		=> $subforum_row['name'],
ef5584
						'unread'	=> $subforum_unread,
ef5584
					);
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					unset($subforums[$forum_id][$subforum_id]);
ef5584
				}
ef5584
ef5584
				// If one subforum is unread the forum gets unread too...
ef5584
				if ($subforum_unread)
ef5584
				{
ef5584
					$forum_unread = true;
ef5584
				}
ef5584
			}
ef5584
ef5584
			$l_subforums = (sizeof($subforums[$forum_id]) == 1) ? $user->lang['SUBFORUM'] . ': ' : $user->lang['SUBFORUMS'] . ': ';
ef5584
			$folder_image = ($forum_unread) ? 'forum_unread_subforum' : 'forum_read_subforum';
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			switch ($row['forum_type'])
ef5584
			{
ef5584
				case FORUM_POST:
ef5584
					$folder_image = ($forum_unread) ? 'forum_unread' : 'forum_read';
ef5584
				break;
ef5584
ef5584
				case FORUM_LINK:
ef5584
					$folder_image = 'forum_link';
ef5584
				break;
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Which folder should we display?
ef5584
		if ($row['forum_status'] == ITEM_LOCKED)
ef5584
		{
ef5584
			$folder_image = ($forum_unread) ? 'forum_unread_locked' : 'forum_read_locked';
ef5584
			$folder_alt = 'FORUM_LOCKED';
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$folder_alt = ($forum_unread) ? 'NEW_POSTS' : 'NO_NEW_POSTS';
ef5584
		}
ef5584
ef5584
		// Create last post link information, if appropriate
ef5584
		if ($row['forum_last_post_id'])
ef5584
		{
ef5584
			$last_post_subject = $row['forum_last_post_subject'];
ef5584
			$last_post_time = $user->format_date($row['forum_last_post_time']);
ef5584
			$last_post_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $row['forum_id_last_post'] . '&p=' . $row['forum_last_post_id']) . '#p' . $row['forum_last_post_id'];
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$last_post_subject = $last_post_time = $last_post_url = '';
ef5584
		}
ef5584
ef5584
		// Output moderator listing ... if applicable
ef5584
		$l_moderator = $moderators_list = '';
ef5584
		if ($display_moderators && !empty($forum_moderators[$forum_id]))
ef5584
		{
ef5584
			$l_moderator = (sizeof($forum_moderators[$forum_id]) == 1) ? $user->lang['MODERATOR'] : $user->lang['MODERATORS'];
ef5584
			$moderators_list = implode(', ', $forum_moderators[$forum_id]);
ef5584
		}
ef5584
ef5584
		$l_post_click_count = ($row['forum_type'] == FORUM_LINK) ? 'CLICKS' : 'POSTS';
ef5584
		$post_click_count = ($row['forum_type'] != FORUM_LINK || $row['forum_flags'] & FORUM_FLAG_LINK_TRACK) ? $row['forum_posts'] : '';
ef5584
ef5584
		$s_subforums_list = array();
ef5584
		foreach ($subforums_list as $subforum)
ef5584
		{
ef5584
			$s_subforums_list[] = '' . $subforum['name'] . '';
ef5584
		}
ef5584
		$s_subforums_list = (string) implode(', ', $s_subforums_list);
ef5584
		$catless = ($row['parent_id'] == $root_data['forum_id']) ? true : false;
ef5584
ef5584
		if ($row['forum_type'] != FORUM_LINK)
ef5584
		{
ef5584
			$u_viewforum = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			// If the forum is a link and we count redirects we need to visit it
ef5584
			// If the forum is having a password or no read access we do not expose the link, but instead handle it in viewforum
ef5584
			if (($row['forum_flags'] & FORUM_FLAG_LINK_TRACK) || $row['forum_password'] || !$auth->acl_get('f_read', $forum_id))
ef5584
			{
ef5584
				$u_viewforum = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']);
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				$u_viewforum = $row['forum_link'];
ef5584
			}
ef5584
		}
ef5584
ef5584
		$template->assign_block_vars('forumrow', array(
ef5584
			'S_IS_CAT'			=> false,
ef5584
			'S_NO_CAT'			=> $catless && !$last_catless,
ef5584
			'S_IS_LINK'			=> ($row['forum_type'] == FORUM_LINK) ? true : false,
ef5584
			'S_UNREAD_FORUM'	=> $forum_unread,
ef5584
			'S_LOCKED_FORUM'	=> ($row['forum_status'] == ITEM_LOCKED) ? true : false,
ef5584
			'S_LIST_SUBFORUMS'	=> ($row['display_subforum_list']) ? true : false,
ef5584
			'S_SUBFORUMS'		=> (sizeof($subforums_list)) ? true : false,
ef5584
ef5584
			'FORUM_ID'				=> $row['forum_id'],
ef5584
			'FORUM_NAME'			=> $row['forum_name'],
ef5584
			'FORUM_DESC'			=> generate_text_for_display($row['forum_desc'], $row['forum_desc_uid'], $row['forum_desc_bitfield'], $row['forum_desc_options']),
ef5584
			'TOPICS'				=> $row['forum_topics'],
ef5584
			$l_post_click_count		=> $post_click_count,
ef5584
			'FORUM_FOLDER_IMG'		=> $user->img($folder_image, $folder_alt),
ef5584
			'FORUM_FOLDER_IMG_SRC'	=> $user->img($folder_image, $folder_alt, false, '', 'src'),
ef5584
			'FORUM_FOLDER_IMG_ALT'	=> isset($user->lang[$folder_alt]) ? $user->lang[$folder_alt] : '',
ef5584
			'FORUM_IMAGE'			=> ($row['forum_image']) ? '' . $user->lang[$folder_alt] . '' : '',
ef5584
			'FORUM_IMAGE_SRC'		=> ($row['forum_image']) ? $phpbb_root_path . $row['forum_image'] : '',
ef5584
			'LAST_POST_SUBJECT'		=> censor_text($last_post_subject),
ef5584
			'LAST_POST_TIME'		=> $last_post_time,
ef5584
			'LAST_POSTER'			=> get_username_string('username', $row['forum_last_poster_id'], $row['forum_last_poster_name'], $row['forum_last_poster_colour']),
ef5584
			'LAST_POSTER_COLOUR'	=> get_username_string('colour', $row['forum_last_poster_id'], $row['forum_last_poster_name'], $row['forum_last_poster_colour']),
ef5584
			'LAST_POSTER_FULL'		=> get_username_string('full', $row['forum_last_poster_id'], $row['forum_last_poster_name'], $row['forum_last_poster_colour']),
ef5584
			'MODERATORS'			=> $moderators_list,
ef5584
			'SUBFORUMS'				=> $s_subforums_list,
ef5584
ef5584
			'L_SUBFORUM_STR'		=> $l_subforums,
ef5584
			'L_FORUM_FOLDER_ALT'	=> $folder_alt,
ef5584
			'L_MODERATOR_STR'		=> $l_moderator,
ef5584
ef5584
			'U_VIEWFORUM'		=> $u_viewforum,
ef5584
			'U_LAST_POSTER'		=> get_username_string('profile', $row['forum_last_poster_id'], $row['forum_last_poster_name'], $row['forum_last_poster_colour']),
ef5584
			'U_LAST_POST'		=> $last_post_url)
ef5584
		);
ef5584
ef5584
		// Assign subforums loop for style authors
ef5584
		foreach ($subforums_list as $subforum)
ef5584
		{
ef5584
			$template->assign_block_vars('forumrow.subforum', array(
ef5584
				'U_SUBFORUM'	=> $subforum['link'],
ef5584
				'SUBFORUM_NAME'	=> $subforum['name'],
ef5584
				'S_UNREAD'		=> $subforum['unread'])
ef5584
			);
ef5584
		}
ef5584
ef5584
		$last_catless = $catless;
ef5584
	}
ef5584
ef5584
	$template->assign_vars(array(
ef5584
		'U_MARK_FORUMS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}viewforum.$phpEx", 'hash=' . generate_link_hash('global') . '&f=' . $root_data['forum_id'] . '&mark=forums') : '',
ef5584
		'S_HAS_SUBFORUM'	=> ($visible_forums) ? true : false,
ef5584
		'L_SUBFORUM'		=> ($visible_forums == 1) ? $user->lang['SUBFORUM'] : $user->lang['SUBFORUMS'],
ef5584
		'LAST_POST_IMG'		=> $user->img('icon_topic_latest', 'VIEW_LATEST_POST'))
ef5584
	);
ef5584
ef5584
	if ($return_moderators)
ef5584
	{
ef5584
		return array($active_forum_ary, $forum_moderators);
ef5584
	}
ef5584
ef5584
	return array($active_forum_ary, array());
ef5584
}
ef5584
ef5584
/**
ef5584
* Create forum rules for given forum
ef5584
*/
ef5584
function generate_forum_rules(&$forum_data)
ef5584
{
ef5584
	if (!$forum_data['forum_rules'] && !$forum_data['forum_rules_link'])
ef5584
	{
ef5584
		return;
ef5584
	}
ef5584
ef5584
	global $template, $phpbb_root_path, $phpEx;
ef5584
ef5584
	if ($forum_data['forum_rules'])
ef5584
	{
ef5584
		$forum_data['forum_rules'] = generate_text_for_display($forum_data['forum_rules'], $forum_data['forum_rules_uid'], $forum_data['forum_rules_bitfield'], $forum_data['forum_rules_options']);
ef5584
	}
ef5584
ef5584
	$template->assign_vars(array(
ef5584
		'S_FORUM_RULES'	=> true,
ef5584
		'U_FORUM_RULES'	=> $forum_data['forum_rules_link'],
ef5584
		'FORUM_RULES'	=> $forum_data['forum_rules'])
ef5584
	);
ef5584
}
ef5584
ef5584
/**
ef5584
* Create forum navigation links for given forum, create parent
ef5584
* list if currently null, assign basic forum info to template
ef5584
*/
ef5584
function generate_forum_nav(&$forum_data)
ef5584
{
ef5584
	global $db, $user, $template, $auth;
ef5584
	global $phpEx, $phpbb_root_path;
ef5584
ef5584
	if (!$auth->acl_get('f_list', $forum_data['forum_id']))
ef5584
	{
ef5584
		return;
ef5584
	}
ef5584
ef5584
	// Get forum parents
ef5584
	$forum_parents = get_forum_parents($forum_data);
ef5584
ef5584
	// Build navigation links
ef5584
	if (!empty($forum_parents))
ef5584
	{
ef5584
		foreach ($forum_parents as $parent_forum_id => $parent_data)
ef5584
		{
ef5584
			list($parent_name, $parent_type) = array_values($parent_data);
ef5584
ef5584
			// Skip this parent if the user does not have the permission to view it
ef5584
			if (!$auth->acl_get('f_list', $parent_forum_id))
ef5584
			{
ef5584
				continue;
ef5584
			}
ef5584
ef5584
			$template->assign_block_vars('navlinks', array(
ef5584
				'S_IS_CAT'		=> ($parent_type == FORUM_CAT) ? true : false,
ef5584
				'S_IS_LINK'		=> ($parent_type == FORUM_LINK) ? true : false,
ef5584
				'S_IS_POST'		=> ($parent_type == FORUM_POST) ? true : false,
ef5584
				'FORUM_NAME'	=> $parent_name,
ef5584
				'FORUM_ID'		=> $parent_forum_id,
ef5584
				'U_VIEW_FORUM'	=> append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $parent_forum_id))
ef5584
			);
ef5584
		}
ef5584
	}
ef5584
ef5584
	$template->assign_block_vars('navlinks', array(
ef5584
		'S_IS_CAT'		=> ($forum_data['forum_type'] == FORUM_CAT) ? true : false,
ef5584
		'S_IS_LINK'		=> ($forum_data['forum_type'] == FORUM_LINK) ? true : false,
ef5584
		'S_IS_POST'		=> ($forum_data['forum_type'] == FORUM_POST) ? true : false,
ef5584
		'FORUM_NAME'	=> $forum_data['forum_name'],
ef5584
		'FORUM_ID'		=> $forum_data['forum_id'],
ef5584
		'U_VIEW_FORUM'	=> append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_data['forum_id']))
ef5584
	);
ef5584
ef5584
	$template->assign_vars(array(
ef5584
		'FORUM_ID' 		=> $forum_data['forum_id'],
ef5584
		'FORUM_NAME'	=> $forum_data['forum_name'],
ef5584
		'FORUM_DESC'	=> generate_text_for_display($forum_data['forum_desc'], $forum_data['forum_desc_uid'], $forum_data['forum_desc_bitfield'], $forum_data['forum_desc_options']))
ef5584
	);
ef5584
ef5584
	return;
ef5584
}
ef5584
ef5584
/**
ef5584
* Returns forum parents as an array. Get them from forum_data if available, or update the database otherwise
ef5584
*/
ef5584
function get_forum_parents(&$forum_data)
ef5584
{
ef5584
	global $db;
ef5584
ef5584
	$forum_parents = array();
ef5584
ef5584
	if ($forum_data['parent_id'] > 0)
ef5584
	{
ef5584
		if ($forum_data['forum_parents'] == '')
ef5584
		{
ef5584
			$sql = 'SELECT forum_id, forum_name, forum_type
ef5584
				FROM ' . FORUMS_TABLE . '
ef5584
				WHERE left_id < ' . $forum_data['left_id'] . '
ef5584
					AND right_id > ' . $forum_data['right_id'] . '
ef5584
				ORDER BY left_id ASC';
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			while ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				$forum_parents[$row['forum_id']] = array($row['forum_name'], (int) $row['forum_type']);
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			$forum_data['forum_parents'] = serialize($forum_parents);
ef5584
ef5584
			$sql = 'UPDATE ' . FORUMS_TABLE . "
ef5584
				SET forum_parents = '" . $db->sql_escape($forum_data['forum_parents']) . "'
ef5584
				WHERE parent_id = " . $forum_data['parent_id'];
ef5584
			$db->sql_query($sql);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$forum_parents = unserialize($forum_data['forum_parents']);
ef5584
		}
ef5584
	}
ef5584
ef5584
	return $forum_parents;
ef5584
}
ef5584
ef5584
/**
ef5584
* Generate topic pagination
ef5584
*/
ef5584
function topic_generate_pagination($replies, $url)
ef5584
{
ef5584
	global $config, $user;
ef5584
ef5584
	// Make sure $per_page is a valid value
ef5584
	$per_page = ($config['posts_per_page'] <= 0) ? 1 : $config['posts_per_page'];
ef5584
ef5584
	if (($replies + 1) > $per_page)
ef5584
	{
ef5584
		$total_pages = ceil(($replies + 1) / $per_page);
ef5584
		$pagination = '';
ef5584
ef5584
		$times = 1;
ef5584
		for ($j = 0; $j < $replies + 1; $j += $per_page)
ef5584
		{
ef5584
			$pagination .= '' . $times . '';
ef5584
			if ($times == 1 && $total_pages > 5)
ef5584
			{
ef5584
				$pagination .= ' ... ';
ef5584
ef5584
				// Display the last three pages
ef5584
				$times = $total_pages - 3;
ef5584
				$j += ($total_pages - 4) * $per_page;
ef5584
			}
ef5584
			else if ($times < $total_pages)
ef5584
			{
ef5584
				$pagination .= '' . $user->lang['COMMA_SEPARATOR'] . '';
ef5584
			}
ef5584
			$times++;
ef5584
		}
ef5584
	}
ef5584
	else
ef5584
	{
ef5584
		$pagination = '';
ef5584
	}
ef5584
ef5584
	return $pagination;
ef5584
}
ef5584
ef5584
/**
ef5584
* Obtain list of moderators of each forum
ef5584
*/
ef5584
function get_moderators(&$forum_moderators, $forum_id = false)
ef5584
{
ef5584
	global $config, $template, $db, $phpbb_root_path, $phpEx, $user, $auth;
ef5584
ef5584
	// Have we disabled the display of moderators? If so, then return
ef5584
	// from whence we came ...
ef5584
	if (!$config['load_moderators'])
ef5584
	{
ef5584
		return;
ef5584
	}
ef5584
ef5584
	$forum_sql = '';
ef5584
ef5584
	if ($forum_id !== false)
ef5584
	{
ef5584
		if (!is_array($forum_id))
ef5584
		{
ef5584
			$forum_id = array($forum_id);
ef5584
		}
ef5584
ef5584
		// If we don't have a forum then we can't have a moderator
ef5584
		if (!sizeof($forum_id))
ef5584
		{
ef5584
			return;
ef5584
		}
ef5584
ef5584
		$forum_sql = 'AND m.' . $db->sql_in_set('forum_id', $forum_id);
ef5584
	}
ef5584
ef5584
	$sql_array = array(
ef5584
		'SELECT'	=> 'm.*, u.user_colour, g.group_colour, g.group_type',
ef5584
ef5584
		'FROM'		=> array(
ef5584
			MODERATOR_CACHE_TABLE	=> 'm',
ef5584
		),
ef5584
ef5584
		'LEFT_JOIN'	=> array(
ef5584
			array(
ef5584
				'FROM'	=> array(USERS_TABLE => 'u'),
ef5584
				'ON'	=> 'm.user_id = u.user_id',
ef5584
			),
ef5584
			array(
ef5584
				'FROM'	=> array(GROUPS_TABLE => 'g'),
ef5584
				'ON'	=> 'm.group_id = g.group_id',
ef5584
			),
ef5584
		),
ef5584
ef5584
		'WHERE'		=> "m.display_on_index = 1 $forum_sql",
ef5584
	);
ef5584
ef5584
	$sql = $db->sql_build_query('SELECT', $sql_array);
ef5584
	$result = $db->sql_query($sql, 3600);
ef5584
ef5584
	while ($row = $db->sql_fetchrow($result))
ef5584
	{
ef5584
		if (!empty($row['user_id']))
ef5584
		{
ef5584
			$forum_moderators[$row['forum_id']][] = get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$group_name = (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']);
ef5584
ef5584
			if ($user->data['user_id'] != ANONYMOUS && !$auth->acl_get('u_viewprofile'))
ef5584
			{
ef5584
				$forum_moderators[$row['forum_id']][] = '<span' . (($row['group_colour']) ? ' style="color:#' . $row['group_colour'] . ';"' : '') . '>' . $group_name . '';
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				$forum_moderators[$row['forum_id']][] = '<a' . (($row['group_colour']) ? ' style="color:#' . $row['group_colour'] . ';"' : '') . ' href="' . append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=group&g=' . $row['group_id']) . '">' . $group_name . '';
ef5584
			}
ef5584
		}
ef5584
	}
ef5584
	$db->sql_freeresult($result);
ef5584
ef5584
	return;
ef5584
}
ef5584
ef5584
/**
ef5584
* User authorisation levels output
ef5584
*
ef5584
* @param	string	$mode			Can be forum or topic. Not in use at the moment.
ef5584
* @param	int		$forum_id		The current forum the user is in.
ef5584
* @param	int		$forum_status	The forums status bit.
ef5584
*/
ef5584
function gen_forum_auth_level($mode, $forum_id, $forum_status)
ef5584
{
ef5584
	global $template, $auth, $user, $config;
ef5584
ef5584
	$locked = ($forum_status == ITEM_LOCKED && !$auth->acl_get('m_edit', $forum_id)) ? true : false;
ef5584
ef5584
	$rules = array(
ef5584
		($auth->acl_get('f_post', $forum_id) && !$locked) ? $user->lang['RULES_POST_CAN'] : $user->lang['RULES_POST_CANNOT'],
ef5584
		($auth->acl_get('f_reply', $forum_id) && !$locked) ? $user->lang['RULES_REPLY_CAN'] : $user->lang['RULES_REPLY_CANNOT'],
ef5584
		($user->data['is_registered'] && $auth->acl_gets('f_edit', 'm_edit', $forum_id) && !$locked) ? $user->lang['RULES_EDIT_CAN'] : $user->lang['RULES_EDIT_CANNOT'],
ef5584
		($user->data['is_registered'] && $auth->acl_gets('f_delete', 'm_delete', $forum_id) && !$locked) ? $user->lang['RULES_DELETE_CAN'] : $user->lang['RULES_DELETE_CANNOT'],
ef5584
	);
ef5584
ef5584
	if ($config['allow_attachments'])
ef5584
	{
ef5584
		$rules[] = ($auth->acl_get('f_attach', $forum_id) && $auth->acl_get('u_attach') && !$locked) ? $user->lang['RULES_ATTACH_CAN'] : $user->lang['RULES_ATTACH_CANNOT'];
ef5584
	}
ef5584
ef5584
	foreach ($rules as $rule)
ef5584
	{
ef5584
		$template->assign_block_vars('rules', array('RULE' => $rule));
ef5584
	}
ef5584
ef5584
	return;
ef5584
}
ef5584
ef5584
/**
ef5584
* Generate topic status
ef5584
*/
ef5584
function topic_status(&$topic_row, $replies, $unread_topic, &$folder_img, &$folder_alt, &$topic_type)
ef5584
{
ef5584
	global $user, $config;
ef5584
ef5584
	$folder = $folder_new = '';
ef5584
ef5584
	if ($topic_row['topic_status'] == ITEM_MOVED)
ef5584
	{
ef5584
		$topic_type = $user->lang['VIEW_TOPIC_MOVED'];
ef5584
		$folder_img = 'topic_moved';
ef5584
		$folder_alt = 'TOPIC_MOVED';
ef5584
	}
ef5584
	else
ef5584
	{
ef5584
		switch ($topic_row['topic_type'])
ef5584
		{
ef5584
			case POST_GLOBAL:
ef5584
				$topic_type = $user->lang['VIEW_TOPIC_GLOBAL'];
ef5584
				$folder = 'global_read';
ef5584
				$folder_new = 'global_unread';
ef5584
			break;
ef5584
ef5584
			case POST_ANNOUNCE:
ef5584
				$topic_type = $user->lang['VIEW_TOPIC_ANNOUNCEMENT'];
ef5584
				$folder = 'announce_read';
ef5584
				$folder_new = 'announce_unread';
ef5584
			break;
ef5584
ef5584
			case POST_STICKY:
ef5584
				$topic_type = $user->lang['VIEW_TOPIC_STICKY'];
ef5584
				$folder = 'sticky_read';
ef5584
				$folder_new = 'sticky_unread';
ef5584
			break;
ef5584
ef5584
			default:
ef5584
				$topic_type = '';
ef5584
				$folder = 'topic_read';
ef5584
				$folder_new = 'topic_unread';
ef5584
ef5584
				// Hot topic threshold is for posts in a topic, which is replies + the first post. ;)
ef5584
				if ($config['hot_threshold'] && ($replies + 1) >= $config['hot_threshold'] && $topic_row['topic_status'] != ITEM_LOCKED)
ef5584
				{
ef5584
					$folder .= '_hot';
ef5584
					$folder_new .= '_hot';
ef5584
				}
ef5584
			break;
ef5584
		}
ef5584
ef5584
		if ($topic_row['topic_status'] == ITEM_LOCKED)
ef5584
		{
ef5584
			$topic_type = $user->lang['VIEW_TOPIC_LOCKED'];
ef5584
			$folder .= '_locked';
ef5584
			$folder_new .= '_locked';
ef5584
		}
ef5584
ef5584
ef5584
		$folder_img = ($unread_topic) ? $folder_new : $folder;
ef5584
		$folder_alt = ($unread_topic) ? 'NEW_POSTS' : (($topic_row['topic_status'] == ITEM_LOCKED) ? 'TOPIC_LOCKED' : 'NO_NEW_POSTS');
ef5584
ef5584
		// Posted image?
ef5584
		if (!empty($topic_row['topic_posted']) && $topic_row['topic_posted'])
ef5584
		{
ef5584
			$folder_img .= '_mine';
ef5584
		}
ef5584
	}
ef5584
ef5584
	if ($topic_row['poll_start'] && $topic_row['topic_status'] != ITEM_MOVED)
ef5584
	{
ef5584
		$topic_type = $user->lang['VIEW_TOPIC_POLL'];
ef5584
	}
ef5584
}
ef5584
ef5584
/**
ef5584
* Assign/Build custom bbcodes for display in screens supporting using of bbcodes
ef5584
* The custom bbcodes buttons will be placed within the template block 'custom_codes'
ef5584
*/
ef5584
function display_custom_bbcodes()
ef5584
{
ef5584
	global $db, $template;
ef5584
ef5584
	// Start counting from 22 for the bbcode ids (every bbcode takes two ids - opening/closing)
ef5584
	$num_predefined_bbcodes = 22;
ef5584
ef5584
	$sql = 'SELECT bbcode_id, bbcode_tag, bbcode_helpline
ef5584
		FROM ' . BBCODES_TABLE . '
ef5584
		WHERE display_on_posting = 1
ef5584
		ORDER BY bbcode_tag';
ef5584
	$result = $db->sql_query($sql);
ef5584
ef5584
	$i = 0;
ef5584
	while ($row = $db->sql_fetchrow($result))
ef5584
	{
ef5584
		$template->assign_block_vars('custom_tags', array(
ef5584
			'BBCODE_NAME'		=> "'[{$row['bbcode_tag']}]', '[/" . str_replace('=', '', $row['bbcode_tag']) . "]'",
ef5584
			'BBCODE_ID'			=> $num_predefined_bbcodes + ($i * 2),
ef5584
			'BBCODE_TAG'		=> $row['bbcode_tag'],
ef5584
			'BBCODE_HELPLINE'	=> $row['bbcode_helpline'],
ef5584
			'A_BBCODE_HELPLINE'	=> str_replace(array('&', '"', "'", '<', '>'), array('&', '"', "\'", '<', '>'), $row['bbcode_helpline']),
ef5584
		));
ef5584
ef5584
		$i++;
ef5584
	}
ef5584
	$db->sql_freeresult($result);
ef5584
}
ef5584
ef5584
/**
ef5584
* Display reasons
ef5584
*/
ef5584
function display_reasons($reason_id = 0)
ef5584
{
ef5584
	global $db, $user, $template;
ef5584
ef5584
	$sql = 'SELECT *
ef5584
		FROM ' . REPORTS_REASONS_TABLE . '
ef5584
		ORDER BY reason_order ASC';
ef5584
	$result = $db->sql_query($sql);
ef5584
ef5584
	while ($row = $db->sql_fetchrow($result))
ef5584
	{
ef5584
		// If the reason is defined within the language file, we will use the localized version, else just use the database entry...
ef5584
		if (isset($user->lang['report_reasons']['TITLE'][strtoupper($row['reason_title'])]) && isset($user->lang['report_reasons']['DESCRIPTION'][strtoupper($row['reason_title'])]))
ef5584
		{
ef5584
			$row['reason_description'] = $user->lang['report_reasons']['DESCRIPTION'][strtoupper($row['reason_title'])];
ef5584
			$row['reason_title'] = $user->lang['report_reasons']['TITLE'][strtoupper($row['reason_title'])];
ef5584
		}
ef5584
ef5584
		$template->assign_block_vars('reason', array(
ef5584
			'ID'			=> $row['reason_id'],
ef5584
			'TITLE'			=> $row['reason_title'],
ef5584
			'DESCRIPTION'	=> $row['reason_description'],
ef5584
			'S_SELECTED'	=> ($row['reason_id'] == $reason_id) ? true : false)
ef5584
		);
ef5584
	}
ef5584
	$db->sql_freeresult($result);
ef5584
}
ef5584
ef5584
/**
ef5584
* Display user activity (action forum/topic)
ef5584
*/
ef5584
function display_user_activity(&$userdata)
ef5584
{
ef5584
	global $auth, $template, $db, $user;
ef5584
	global $phpbb_root_path, $phpEx;
ef5584
ef5584
	// Do not display user activity for users having more than 5000 posts...
ef5584
	if ($userdata['user_posts'] > 5000)
ef5584
	{
ef5584
		return;
ef5584
	}
ef5584
ef5584
	$forum_ary = array();
ef5584
ef5584
	// Do not include those forums the user is not having read access to...
ef5584
	$forum_read_ary = $auth->acl_getf('!f_read');
ef5584
ef5584
	foreach ($forum_read_ary as $forum_id => $not_allowed)
ef5584
	{
ef5584
		if ($not_allowed['f_read'])
ef5584
		{
ef5584
			$forum_ary[] = (int) $forum_id;
ef5584
		}
ef5584
	}
ef5584
ef5584
	$forum_ary = array_unique($forum_ary);
ef5584
	$forum_sql = (sizeof($forum_ary)) ? 'AND ' . $db->sql_in_set('forum_id', $forum_ary, true) : '';
ef5584
ef5584
	// Obtain active forum
ef5584
	$sql = 'SELECT forum_id, COUNT(post_id) AS num_posts
ef5584
		FROM ' . POSTS_TABLE . '
ef5584
		WHERE poster_id = ' . $userdata['user_id'] . "
ef5584
			AND post_postcount = 1
ef5584
			$forum_sql
ef5584
		GROUP BY forum_id
ef5584
		ORDER BY num_posts DESC";
ef5584
	$result = $db->sql_query_limit($sql, 1);
ef5584
	$active_f_row = $db->sql_fetchrow($result);
ef5584
	$db->sql_freeresult($result);
ef5584
ef5584
	if (!empty($active_f_row))
ef5584
	{
ef5584
		$sql = 'SELECT forum_name
ef5584
			FROM ' . FORUMS_TABLE . '
ef5584
			WHERE forum_id = ' . $active_f_row['forum_id'];
ef5584
		$result = $db->sql_query($sql, 3600);
ef5584
		$active_f_row['forum_name'] = (string) $db->sql_fetchfield('forum_name');
ef5584
		$db->sql_freeresult($result);
ef5584
	}
ef5584
ef5584
	// Obtain active topic
ef5584
	$sql = 'SELECT topic_id, COUNT(post_id) AS num_posts
ef5584
		FROM ' . POSTS_TABLE . '
ef5584
		WHERE poster_id = ' . $userdata['user_id'] . "
ef5584
			AND post_postcount = 1
ef5584
			$forum_sql
ef5584
		GROUP BY topic_id
ef5584
		ORDER BY num_posts DESC";
ef5584
	$result = $db->sql_query_limit($sql, 1);
ef5584
	$active_t_row = $db->sql_fetchrow($result);
ef5584
	$db->sql_freeresult($result);
ef5584
ef5584
	if (!empty($active_t_row))
ef5584
	{
ef5584
		$sql = 'SELECT topic_title
ef5584
			FROM ' . TOPICS_TABLE . '
ef5584
			WHERE topic_id = ' . $active_t_row['topic_id'];
ef5584
		$result = $db->sql_query($sql);
ef5584
		$active_t_row['topic_title'] = (string) $db->sql_fetchfield('topic_title');
ef5584
		$db->sql_freeresult($result);
ef5584
	}
ef5584
ef5584
	$userdata['active_t_row'] = $active_t_row;
ef5584
	$userdata['active_f_row'] = $active_f_row;
ef5584
ef5584
	$active_f_name = $active_f_id = $active_f_count = $active_f_pct = '';
ef5584
	if (!empty($active_f_row['num_posts']))
ef5584
	{
ef5584
		$active_f_name = $active_f_row['forum_name'];
ef5584
		$active_f_id = $active_f_row['forum_id'];
ef5584
		$active_f_count = $active_f_row['num_posts'];
ef5584
		$active_f_pct = ($userdata['user_posts']) ? ($active_f_count / $userdata['user_posts']) * 100 : 0;
ef5584
	}
ef5584
ef5584
	$active_t_name = $active_t_id = $active_t_count = $active_t_pct = '';
ef5584
	if (!empty($active_t_row['num_posts']))
ef5584
	{
ef5584
		$active_t_name = $active_t_row['topic_title'];
ef5584
		$active_t_id = $active_t_row['topic_id'];
ef5584
		$active_t_count = $active_t_row['num_posts'];
ef5584
		$active_t_pct = ($userdata['user_posts']) ? ($active_t_count / $userdata['user_posts']) * 100 : 0;
ef5584
	}
ef5584
ef5584
	$l_active_pct = ($userdata['user_id'] != ANONYMOUS && $userdata['user_id'] == $user->data['user_id']) ? $user->lang['POST_PCT_ACTIVE_OWN'] : $user->lang['POST_PCT_ACTIVE'];
ef5584
ef5584
	$template->assign_vars(array(
ef5584
		'ACTIVE_FORUM'			=> $active_f_name,
ef5584
		'ACTIVE_FORUM_POSTS'	=> ($active_f_count == 1) ? sprintf($user->lang['USER_POST'], 1) : sprintf($user->lang['USER_POSTS'], $active_f_count),
ef5584
		'ACTIVE_FORUM_PCT'		=> sprintf($l_active_pct, $active_f_pct),
ef5584
		'ACTIVE_TOPIC'			=> censor_text($active_t_name),
ef5584
		'ACTIVE_TOPIC_POSTS'	=> ($active_t_count == 1) ? sprintf($user->lang['USER_POST'], 1) : sprintf($user->lang['USER_POSTS'], $active_t_count),
ef5584
		'ACTIVE_TOPIC_PCT'		=> sprintf($l_active_pct, $active_t_pct),
ef5584
		'U_ACTIVE_FORUM'		=> append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $active_f_id),
ef5584
		'U_ACTIVE_TOPIC'		=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", 't=' . $active_t_id),
ef5584
		'S_SHOW_ACTIVITY'		=> true)
ef5584
	);
ef5584
}
ef5584
ef5584
/**
ef5584
* Topic and forum watching common code
ef5584
*/
ef5584
function watch_topic_forum($mode, &$s_watching, $user_id, $forum_id, $topic_id, $notify_status = 'unset', $start = 0)
ef5584
{
ef5584
	global $template, $db, $user, $phpEx, $start, $phpbb_root_path;
ef5584
ef5584
	$table_sql = ($mode == 'forum') ? FORUMS_WATCH_TABLE : TOPICS_WATCH_TABLE;
ef5584
	$where_sql = ($mode == 'forum') ? 'forum_id' : 'topic_id';
ef5584
	$match_id = ($mode == 'forum') ? $forum_id : $topic_id;
ef5584
	$u_url = "uid={$user->data['user_id']}";
ef5584
	$u_url .= ($mode == 'forum') ? '&f' : '&f=' . $forum_id . '&t';
ef5584
ef5584
	// Is user watching this thread?
ef5584
	if ($user_id != ANONYMOUS)
ef5584
	{
ef5584
		$can_watch = true;
ef5584
ef5584
		if ($notify_status == 'unset')
ef5584
		{
ef5584
			$sql = "SELECT notify_status
ef5584
				FROM $table_sql
ef5584
				WHERE $where_sql = $match_id
ef5584
					AND user_id = $user_id";
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			$notify_status = ($row = $db->sql_fetchrow($result)) ? $row['notify_status'] : NULL;
ef5584
			$db->sql_freeresult($result);
ef5584
		}
ef5584
ef5584
		if (!is_null($notify_status) && $notify_status !== '')
ef5584
		{
ef5584
ef5584
			if (isset($_GET['unwatch']))
ef5584
			{
ef5584
				$uid = request_var('uid', 0);
ef5584
				if ($uid != $user_id)
ef5584
				{
ef5584
					$redirect_url = append_sid("{$phpbb_root_path}view$mode.$phpEx", "$u_url=$match_id&start=$start");
ef5584
					$message = $user->lang['ERR_UNWATCHING'] . '

' . sprintf($user->lang['RETURN_' . strtoupper($mode)], '', '');
ef5584
					trigger_error($message);
ef5584
				}
ef5584
				if ($_GET['unwatch'] == $mode)
ef5584
				{
ef5584
					$is_watching = 0;
ef5584
ef5584
					$sql = 'DELETE FROM ' . $table_sql . "
ef5584
						WHERE $where_sql = $match_id
ef5584
							AND user_id = $user_id";
ef5584
					$db->sql_query($sql);
ef5584
				}
ef5584
ef5584
				$redirect_url = append_sid("{$phpbb_root_path}view$mode.$phpEx", "$u_url=$match_id&start=$start");
ef5584
ef5584
				meta_refresh(3, $redirect_url);
ef5584
ef5584
				$message = $user->lang['NOT_WATCHING_' . strtoupper($mode)] . '

' . sprintf($user->lang['RETURN_' . strtoupper($mode)], '', '');
ef5584
				trigger_error($message);
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				$is_watching = true;
ef5584
ef5584
				if ($notify_status)
ef5584
				{
ef5584
					$sql = 'UPDATE ' . $table_sql . "
ef5584
						SET notify_status = 0
ef5584
						WHERE $where_sql = $match_id
ef5584
							AND user_id = $user_id";
ef5584
					$db->sql_query($sql);
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			if (isset($_GET['watch']))
ef5584
			{
ef5584
				$token = request_var('hash', '');
ef5584
				$redirect_url = append_sid("{$phpbb_root_path}view$mode.$phpEx", "$u_url=$match_id&start=$start");
ef5584
ef5584
				if ($_GET['watch'] == $mode && check_link_hash($token, "{$mode}_$match_id"))
ef5584
				{
ef5584
					$is_watching = true;
ef5584
ef5584
					$sql = 'INSERT INTO ' . $table_sql . " (user_id, $where_sql, notify_status)
ef5584
						VALUES ($user_id, $match_id, 0)";
ef5584
					$db->sql_query($sql);
ef5584
					$message = $user->lang['ARE_WATCHING_' . strtoupper($mode)] . '

' . sprintf($user->lang['RETURN_' . strtoupper($mode)], '', '');
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					$message = $user->lang['ERR_WATCHING'] . '

' . sprintf($user->lang['RETURN_' . strtoupper($mode)], '', '');
ef5584
				}
ef5584
ef5584
				meta_refresh(3, $redirect_url);
ef5584
ef5584
				trigger_error($message);
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				$is_watching = 0;
ef5584
			}
ef5584
		}
ef5584
	}
ef5584
	else
ef5584
	{
ef5584
		if (isset($_GET['unwatch']) && $_GET['unwatch'] == $mode)
ef5584
		{
ef5584
			login_box();
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$can_watch = 0;
ef5584
			$is_watching = 0;
ef5584
		}
ef5584
	}
ef5584
ef5584
	if ($can_watch)
ef5584
	{
ef5584
		$s_watching['link'] = append_sid("{$phpbb_root_path}view$mode.$phpEx", "$u_url=$match_id&" . (($is_watching) ? 'unwatch' : 'watch') . "=$mode&start=$start&hash=" . generate_link_hash("{$mode}_$match_id"));
ef5584
		$s_watching['title'] = $user->lang[(($is_watching) ? 'STOP' : 'START') . '_WATCHING_' . strtoupper($mode)];
ef5584
		$s_watching['is_watching'] = $is_watching;
ef5584
	}
ef5584
ef5584
	return;
ef5584
}
ef5584
ef5584
/**
ef5584
* Get user rank title and image
ef5584
*
ef5584
* @param int $user_rank the current stored users rank id
ef5584
* @param int $user_posts the users number of posts
ef5584
* @param string &$rank_title the rank title will be stored here after execution
ef5584
* @param string &$rank_img the rank image as full img tag is stored here after execution
ef5584
* @param string &$rank_img_src the rank image source is stored here after execution
ef5584
*
ef5584
* Note: since we do not want to break backwards-compatibility, this function will only properly assign ranks to guests if you call it for them with user_posts == false
ef5584
*/
ef5584
function get_user_rank($user_rank, $user_posts, &$rank_title, &$rank_img, &$rank_img_src)
ef5584
{
ef5584
	global $ranks, $config, $phpbb_root_path;
ef5584
ef5584
	if (empty($ranks))
ef5584
	{
ef5584
		global $cache;
ef5584
		$ranks = $cache->obtain_ranks();
ef5584
	}
ef5584
ef5584
	if (!empty($user_rank))
ef5584
	{
ef5584
		$rank_title = (isset($ranks['special'][$user_rank]['rank_title'])) ? $ranks['special'][$user_rank]['rank_title'] : '';
ef5584
		$rank_img = (!empty($ranks['special'][$user_rank]['rank_image'])) ? '' . $ranks['special'][$user_rank]['rank_title'] . '' : '';
ef5584
		$rank_img_src = (!empty($ranks['special'][$user_rank]['rank_image'])) ? $phpbb_root_path . $config['ranks_path'] . '/' . $ranks['special'][$user_rank]['rank_image'] : '';
ef5584
	}
ef5584
	else if ($user_posts !== false)
ef5584
	{
ef5584
		if (!empty($ranks['normal']))
ef5584
		{
ef5584
			foreach ($ranks['normal'] as $rank)
ef5584
			{
ef5584
				if ($user_posts >= $rank['rank_min'])
ef5584
				{
ef5584
					$rank_title = $rank['rank_title'];
ef5584
					$rank_img = (!empty($rank['rank_image'])) ? '' . $rank['rank_title'] . '' : '';
ef5584
					$rank_img_src = (!empty($rank['rank_image'])) ? $phpbb_root_path . $config['ranks_path'] . '/' . $rank['rank_image'] : '';
ef5584
					break;
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
	}
ef5584
}
ef5584
ef5584
/**
ef5584
* Get user avatar
ef5584
*
ef5584
* @param string $avatar Users assigned avatar name
ef5584
* @param int $avatar_type Type of avatar
ef5584
* @param string $avatar_width Width of users avatar
ef5584
* @param string $avatar_height Height of users avatar
ef5584
* @param string $alt Optional language string for alt tag within image, can be a language key or text
ef5584
*
ef5584
* @return string Avatar image
ef5584
*/
ef5584
function get_user_avatar($avatar, $avatar_type, $avatar_width, $avatar_height, $alt = 'USER_AVATAR')
ef5584
{
ef5584
	global $user, $config, $phpbb_root_path, $phpEx;
ef5584
ef5584
	if (empty($avatar) || !$avatar_type)
ef5584
	{
ef5584
		return '';
ef5584
	}
ef5584
ef5584
	$avatar_img = '';
ef5584
ef5584
	switch ($avatar_type)
ef5584
	{
ef5584
		case AVATAR_UPLOAD:
ef5584
			$avatar_img = $phpbb_root_path . "download/file.$phpEx?avatar=";
ef5584
		break;
ef5584
ef5584
		case AVATAR_GALLERY:
ef5584
			$avatar_img = $phpbb_root_path . $config['avatar_gallery_path'] . '/';
ef5584
		break;
ef5584
	}
ef5584
ef5584
	$avatar_img .= $avatar;
ef5584
	return '' . ((!empty($user->lang[$alt])) ? $user->lang[$alt] : $alt) . '';
ef5584
}
ef5584
ef5584
?>